fix & cleanup duplicate history records
[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 {
56   my $self = shift;
57   carp "FS::h_cust_svc::label called on $self" if $DEBUG;
58   my $svc_x = $self->h_svc_x(@_);
59   return () unless $svc_x;
60   my $part_svc = $self->part_svc;
61
62   unless ($svc_x) {
63     carp "can't find h_". $self->part_svc->svcdb. '.svcnum '. $self->svcnum if $DEBUG;
64     return $part_svc->svc, 'n/a', $part_svc->svcdb;
65   }
66
67   my @label;
68   eval { @label = $self->_svc_label($svc_x, @_); };
69
70   if ($@) {
71     carp 'while resolving history record for svcdb/svcnum ' . 
72          $part_svc->svcdb . '/' . $self->svcnum . ': ' . $@ if $DEBUG;
73     return $part_svc->svc, 'n/a', $part_svc->svcdb;
74   } else {
75     return @label;
76   }
77
78 }
79
80 =item h_svc_x END_TIMESTAMP [ START_TIMESTAMP ] 
81
82 Returns the FS::h_svc_XXX object for this service as of END_TIMESTAMP (i.e. an
83 FS::h_svc_acct object or FS::h_svc_domain object, etc.) and (optionally) not
84 cancelled before START_TIMESTAMP.
85
86 =cut
87
88 #false laziness w/cust_pkg::h_cust_svc
89 sub h_svc_x {
90   my $self = shift;
91   my $svcdb = $self->part_svc->svcdb;
92
93   warn "requiring FS/h_$svcdb.pm" if $DEBUG;
94   require "FS/h_$svcdb.pm";
95   my $svc_x = qsearchs(
96     "h_$svcdb",
97     { 'svcnum' => $self->svcnum, },
98     "FS::h_$svcdb"->sql_h_searchs(@_),
99   ) || $self->SUPER::svc_x;
100
101   if ($svc_x) {
102     carp "Using $svcdb in place of missing h_${svcdb} record."
103       if ($svc_x->isa('FS::' . $svcdb) and $DEBUG);
104     return $svc_x;
105   } else {
106     return '';
107   }
108
109 }
110
111 # _upgrade_data
112 #
113 # Used by FS::Upgrade to migrate to a new database.
114 #
115 #
116
117 use FS::UID qw( driver_name dbh );
118
119 sub _upgrade_data {  # class method
120   my ($class, %opts) = @_;
121
122   warn "[FS::h_cust_svc] upgrading $class\n" if $DEBUG;
123
124   return if driver_name =~ /^mysql/; #You can't specify target table 'h_cust_svc' for update in FROM clause
125
126   my $sql = "
127     DELETE FROM h_cust_svc
128       WHERE history_action = 'delete'
129         AND historynum != ( SELECT min(historynum) FROM h_cust_svc AS main
130                               WHERE main.history_date = h_cust_svc.history_date
131                                 AND main.history_user = h_cust_svc.history_user
132                                 AND main.svcnum       = h_cust_svc.svcnum
133                                 AND main.svcpart      = h_cust_svc.svcpart
134                                 AND ( main.pkgnum     = h_cust_svc.pkgnum
135                                       OR ( main.pkgnum IS NULL AND h_cust_svc.pkgnum IS NULL )
136                                     )
137                                 AND ( main.overlimit  = h_cust_svc.overlimit
138                                       OR ( main.overlimit IS NULL AND h_cust_svc.overlimit IS NULL )
139                                     )
140                           )
141   ";
142
143   warn $sql if $DEBUG;
144   my $sth = dbh->prepare($sql) or die dbh->errstr;
145   $sth->execute or die $sth->errstr;
146
147 }
148
149 =back
150
151 =head1 BUGS
152
153 =head1 SEE ALSO
154
155 L<FS::h_Common>, L<FS::cust_svc>, L<FS::Record>, schema.html from the base
156 documentation.
157
158 =cut
159
160 1;
161