communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[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   my $svc_x = qsearchs(
102     "h_$svcdb",
103     { 'svcnum' => $self->svcnum, },
104     "FS::h_$svcdb"->sql_h_searchs(@_),
105   ) || $self->SUPER::svc_x;
106
107   if ($svc_x) {
108     carp "Using $svcdb in place of missing h_${svcdb} record."
109       if ($svc_x->isa('FS::' . $svcdb) and $DEBUG);
110     return $svc_x;
111   } else {
112     return '';
113   }
114
115 }
116
117 # _upgrade_data
118 #
119 # Used by FS::Upgrade to migrate to a new database.
120
121 use FS::UID qw( driver_name dbh );
122
123 sub _upgrade_data {  # class method
124   my ($class, %opts) = @_;
125
126   warn "[FS::h_cust_svc] upgrading $class\n" if $DEBUG;
127
128   return if driver_name =~ /^mysql/; #You can't specify target table 'h_cust_svc' for update in FROM clause
129
130   my $sql = "
131     DELETE FROM h_cust_svc
132       WHERE history_action = 'delete'
133         AND historynum != ( SELECT min(historynum) FROM h_cust_svc AS main
134                               WHERE main.history_date = h_cust_svc.history_date
135                                 AND main.history_user = h_cust_svc.history_user
136                                 AND main.svcnum       = h_cust_svc.svcnum
137                                 AND main.svcpart      = h_cust_svc.svcpart
138                                 AND ( main.pkgnum     = h_cust_svc.pkgnum
139                                       OR ( main.pkgnum IS NULL AND h_cust_svc.pkgnum IS NULL )
140                                     )
141                                 AND ( main.overlimit  = h_cust_svc.overlimit
142                                       OR ( main.overlimit IS NULL AND h_cust_svc.overlimit IS NULL )
143                                     )
144                           )
145   ";
146
147   warn $sql if $DEBUG;
148   my $sth = dbh->prepare($sql) or die dbh->errstr;
149   $sth->execute or die $sth->errstr;
150
151 }
152
153 =back
154
155 =head1 BUGS
156
157 =head1 SEE ALSO
158
159 L<FS::h_Common>, L<FS::cust_svc>, L<FS::Record>, schema.html from the base
160 documentation.
161
162 =cut
163
164 1;
165