add skip_dcontext_suffix to skip CDRs with dcontext ending in a definable string...
[freeside.git] / FS / FS / h_cust_svc.pm
1 package FS::h_cust_svc;
2
3 use strict;
4 use vars qw( @ISA $DEBUG );
5 use Carp;
6 use FS::Record qw(qsearchs);
7 use FS::h_Common;
8 use FS::cust_svc;
9
10 @ISA = qw( FS::h_Common FS::cust_svc );
11
12 $DEBUG = 0;
13
14 sub table { 'h_cust_svc'; }
15
16 =head1 NAME
17
18 FS::h_cust_svc - Object method for h_cust_svc objects
19
20 =head1 SYNOPSIS
21
22 =head1 DESCRIPTION
23
24 An FS::h_cust_svc object  represents a historical service.  FS::h_cust_svc
25 inherits from FS::h_Common and FS::cust_svc.
26
27 =head1 METHODS
28
29 =over 4
30
31 =item date_deleted
32
33 Returns the date this service was deleted, if any.
34
35 =cut
36
37 sub date_deleted {
38   my $self = shift;
39   $self->h_date('delete');
40 }
41
42 =item label END_TIMESTAMP [ START_TIMESTAMP ] 
43
44 Returns a label for this historical service, if the service was created before
45 END_TIMESTAMP and (optionally) not deleted before START_TIMESTAMP.  Otherwise,
46 returns an empty list.
47
48 If a service is found, returns a list consisting of:
49 - The name of this historical service (from part_svc)
50 - A meaningful identifier (username, domain, or mail alias)
51 - The table name (i.e. svc_domain) for this historical service
52
53 =cut
54
55 sub label      { shift->_label('svc_label',      @_); }
56 sub label_long { shift->_label('svc_label_long', @_); }
57
58 sub _label {
59   my $self = shift;
60   my $method = shift;
61
62   #carp "FS::h_cust_svc::_label called on $self" if $DEBUG;
63   warn "FS::h_cust_svc::_label called on $self for $method" if $DEBUG;
64   my $svc_x = $self->h_svc_x(@_);
65   return () unless $svc_x;
66   my $part_svc = $self->part_svc;
67
68   unless ($svc_x) {
69     carp "can't find h_". $self->part_svc->svcdb. '.svcnum '. $self->svcnum if $DEBUG;
70     return $part_svc->svc, 'n/a', $part_svc->svcdb;
71   }
72
73   my @label;
74   eval { @label = $self->$method($svc_x, @_); };
75
76   if ($@) {
77     carp 'while resolving history record for svcdb/svcnum ' . 
78          $part_svc->svcdb . '/' . $self->svcnum . ': ' . $@ if $DEBUG;
79     return $part_svc->svc, 'n/a', $part_svc->svcdb;
80   } else {
81     return @label;
82   }
83
84 }
85
86 =item h_svc_x END_TIMESTAMP [ START_TIMESTAMP ] 
87
88 Returns the FS::h_svc_XXX object for this service as of END_TIMESTAMP (i.e. an
89 FS::h_svc_acct object or FS::h_svc_domain object, etc.) and (optionally) not
90 cancelled before START_TIMESTAMP.
91
92 =cut
93
94 #false laziness w/cust_pkg::h_cust_svc
95 sub h_svc_x {
96   my $self = shift;
97   my $svcdb = $self->part_svc->svcdb;
98
99   warn "requiring FS/h_$svcdb.pm" if $DEBUG;
100   require "FS/h_$svcdb.pm";
101   local($FS::Record::qsearch_qualify_columns) = 0;
102   my $svc_x = qsearchs(
103     "h_$svcdb",
104     { 'svcnum' => $self->svcnum, },
105     "FS::h_$svcdb"->sql_h_searchs(@_),
106   ) || $self->SUPER::svc_x;
107
108   if ($svc_x) {
109     carp "Using $svcdb in place of missing h_${svcdb} record."
110       if ($svc_x->isa('FS::' . $svcdb) and $DEBUG);
111     return $svc_x;
112   } else {
113     return '';
114   }
115
116 }
117
118 # _upgrade_data
119 #
120 # Used by FS::Upgrade to migrate to a new database.
121
122 use FS::UID qw( driver_name dbh );
123
124 sub _upgrade_data {  # class method
125   my ($class, %opts) = @_;
126
127   warn "[FS::h_cust_svc] upgrading $class\n" if $DEBUG;
128
129   return if driver_name =~ /^mysql/; #You can't specify target table 'h_cust_svc' for update in FROM clause
130
131   my $sql = "
132     DELETE FROM h_cust_svc
133       WHERE history_action = 'delete'
134         AND historynum != ( SELECT min(historynum) FROM h_cust_svc AS main
135                               WHERE main.history_date = h_cust_svc.history_date
136                                 AND main.history_user = h_cust_svc.history_user
137                                 AND main.svcnum       = h_cust_svc.svcnum
138                                 AND main.svcpart      = h_cust_svc.svcpart
139                                 AND ( main.pkgnum     = h_cust_svc.pkgnum
140                                       OR ( main.pkgnum IS NULL AND h_cust_svc.pkgnum IS NULL )
141                                     )
142                                 AND ( main.overlimit  = h_cust_svc.overlimit
143                                       OR ( main.overlimit IS NULL AND h_cust_svc.overlimit IS NULL )
144                                     )
145                           )
146   ";
147
148   warn $sql if $DEBUG;
149   my $sth = dbh->prepare($sql) or die dbh->errstr;
150   $sth->execute or die $sth->errstr;
151
152 }
153
154 =back
155
156 =head1 BUGS
157
158 =head1 SEE ALSO
159
160 L<FS::h_Common>, L<FS::cust_svc>, L<FS::Record>, schema.html from the base
161 documentation.
162
163 =cut
164
165 1;
166