enable CardFortress in test database, #71513
[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 ] [ LOCALE ]
43
44 Returns a label for this historical service, if the service was created
45 before END_TIMESTAMP and (optionally) not deleted before START_TIMESTAMP.
46 Otherwise, 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), optionally localized
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 # Parameters to _label:
59 #
60 # 1: the cust_svc method we should call to produce the label. (svc_label
61 # and svc_label_long are defined in FS::cust_svc, not here, and take a svc_x
62 # object as first argument.)
63 # 2, 3: date range to use to find the h_svc_x, which will be passed to
64 # svc_label(_long) and eventually have ->label called on it.
65 # 4: locale, passed to svc_label(_long) also.
66 #
67 # however, if label is called with a locale only, must DTRT (this is a
68 # FS::cust_svc subclass)
69
70 sub _label {
71   my $self = shift;
72   my $method = shift;
73   my ($end, $start, $locale);
74   if (defined($_[0])) {
75     if ( $_[0] =~ /^\d+$/ ) {
76       ($end, $start, $locale) = @_;
77     } else {
78       $locale = shift;
79       $end = $self->history_date;
80     }
81   }
82
83   #carp "FS::h_cust_svc::_label called on $self" if $DEBUG;
84   warn "FS::h_cust_svc::_label called on $self for $method" if $DEBUG;
85   my $svc_x = $self->h_svc_x($end, $start);
86   return () unless $svc_x;
87   my $part_svc = $self->part_svc;
88
89   unless ($svc_x) {
90     carp "can't find h_". $self->part_svc->svcdb. '.svcnum '. $self->svcnum if $DEBUG;
91     return $part_svc->svc, 'n/a', $part_svc->svcdb;
92   }
93
94   my @label;
95   eval { @label = $self->$method($svc_x, $end, $start, $locale); };
96
97   if ($@) {
98     carp 'while resolving history record for svcdb/svcnum ' . 
99          $part_svc->svcdb . '/' . $self->svcnum . ': ' . $@ if $DEBUG;
100     return $part_svc->svc, 'n/a', $part_svc->svcdb;
101   } else {
102     return @label;
103   }
104
105 }
106
107 =item h_svc_x END_TIMESTAMP [ START_TIMESTAMP ] 
108
109 Returns the FS::h_svc_XXX object for this service as of END_TIMESTAMP (i.e.
110 an FS::h_svc_acct object or FS::h_svc_domain object, etc.) and (optionally)
111 not cancelled before START_TIMESTAMP.
112
113 =cut
114
115 #false laziness w/cust_pkg::h_cust_svc
116 sub h_svc_x {
117   my $self = shift;
118   my $svcdb = $self->part_svc->svcdb;
119
120   warn "requiring FS/h_$svcdb.pm" if $DEBUG;
121   require "FS/h_$svcdb.pm";
122   local($FS::Record::qsearch_qualify_columns) = 0;
123   my $svc_x = qsearchs(
124     "h_$svcdb",
125     { 'svcnum' => $self->svcnum, },
126     "FS::h_$svcdb"->sql_h_searchs(@_),
127   ) || $self->SUPER::svc_x;
128
129   if ($svc_x) {
130     carp "Using $svcdb in place of missing h_${svcdb} record."
131       if ($svc_x->isa('FS::' . $svcdb) and $DEBUG);
132     return $svc_x;
133   } else {
134     return '';
135   }
136
137 }
138
139 # _upgrade_data
140 #
141 # Used by FS::Upgrade to migrate to a new database.
142
143 use FS::UID qw( driver_name dbh );
144
145 sub _upgrade_data {  # class method
146   my ($class, %opts) = @_;
147
148   warn "[FS::h_cust_svc] upgrading $class\n" if $DEBUG;
149
150   return if driver_name =~ /^mysql/; #You can't specify target table 'h_cust_svc' for update in FROM clause
151
152   my $sql = "
153     DELETE FROM h_cust_svc
154       WHERE history_action = 'delete'
155         AND historynum != ( SELECT min(historynum) FROM h_cust_svc AS main
156                               WHERE main.history_date = h_cust_svc.history_date
157                                 AND main.history_user = h_cust_svc.history_user
158                                 AND main.svcnum       = h_cust_svc.svcnum
159                                 AND main.svcpart      = h_cust_svc.svcpart
160                                 AND ( main.pkgnum     = h_cust_svc.pkgnum
161                                       OR ( main.pkgnum IS NULL AND h_cust_svc.pkgnum IS NULL )
162                                     )
163                                 AND ( main.overlimit  = h_cust_svc.overlimit
164                                       OR ( main.overlimit IS NULL AND h_cust_svc.overlimit IS NULL )
165                                     )
166                           )
167   ";
168
169   warn $sql if $DEBUG;
170   my $sth = dbh->prepare($sql) or die dbh->errstr;
171   $sth->execute or die $sth->errstr;
172
173 }
174
175 =back
176
177 =head1 BUGS
178
179 =head1 SEE ALSO
180
181 L<FS::h_Common>, L<FS::cust_svc>, L<FS::Record>, schema.html from the base
182 documentation.
183
184 =cut
185
186 1;
187