fix (hopefully the rest of the) fallout from rating CDRs to sub-penny amounts
[freeside.git] / FS / FS / cust_bill_pkg_detail.pm
1 package FS::cust_bill_pkg_detail;
2
3 use strict;
4 use vars qw( @ISA $me $DEBUG %GetInfoType );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_bill_pkg;
7 use FS::Conf;
8
9 @ISA = qw(FS::Record);
10 $me = '[ FS::cust_bill_pkg_detail ]';
11 $DEBUG = 0;
12
13 =head1 NAME
14
15 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
16
17 =head1 SYNOPSIS
18
19   use FS::cust_bill_pkg_detail;
20
21   $record = new FS::cust_bill_pkg_detail \%hash;
22   $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::cust_bill_pkg_detail object represents additional detail information for
35 an invoice line item (see L<FS::cust_bill_pkg>).  FS::cust_bill_pkg_detail
36 inherits from FS::Record.  The following fields are currently supported:
37
38 =over 4
39
40 =item detailnum - primary key
41
42 =item billpkgnum - link to cust_bill_pkg
43
44 =item detail - detail description
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =item new HASHREF
53
54 Creates a new line item detail.  To add the line item detail to the database,
55 see L<"insert">.
56
57 Note that this stores the hash reference, not a distinct copy of the hash it
58 points to.  You can ask the object for a copy with the I<hash> method.
59
60 =cut
61
62 # the new method can be inherited from FS::Record, if a table method is defined
63
64 sub table { 'cust_bill_pkg_detail'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =cut
72
73 # the insert method can be inherited from FS::Record
74
75 =item delete
76
77 Delete this record from the database.
78
79 =cut
80
81 # the delete method can be inherited from FS::Record
82
83 =item replace OLD_RECORD
84
85 Replaces the OLD_RECORD with this one in the database.  If there is an error,
86 returns the error, otherwise returns false.
87
88 =cut
89
90 # the replace method can be inherited from FS::Record
91
92 =item check
93
94 Checks all fields to make sure this is a valid line item detail.  If there is
95 an error, returns the error, otherwise returns false.  Called by the insert
96 and replace methods.
97
98 =cut
99
100 # the check method should currently be supplied - FS::Record contains some
101 # data checking routines
102
103 sub check {
104   my $self = shift;
105
106   my $conf = new FS::Conf;
107
108   my $phonenum = $self->phonenum;
109   my $phonenum_check_method;
110   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
111     $phonenum =~ s/\W//g;
112     $phonenum_check_method = 'ut_alphan';
113   } else {
114     $phonenum =~ s/\D//g;
115     $phonenum_check_method = 'ut_numbern';
116   }
117   $self->phonenum($phonenum);
118
119   $self->ut_numbern('detailnum')
120     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
121     #|| $self->ut_moneyn('amount')
122     || $self->ut_float('amount')
123     || $self->ut_enum('format', [ '', 'C' ] )
124     || $self->ut_text('detail')
125     || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
126     || $self->$phonenum_check_method('phonenum')
127     || $self->SUPER::check
128     ;
129
130 }
131
132 # _upgrade_data
133 #
134 # Used by FS::Upgrade to migrate to a new database.
135
136 sub _upgrade_data { # class method
137
138   my ($class, %opts) = @_;
139
140   warn "$me upgrading $class\n" if $DEBUG;
141
142   my $columndef = dbdef->table($class->table)->column('classnum');
143   unless ($columndef->type eq 'int4') {
144
145     my $dbh = dbh;
146     if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
147
148       eval "use DBI::Const::GetInfoType;";
149       die $@ if $@;
150
151       my $major_version = 0;
152       $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
153         && ( $major_version = sprintf("%d", $1) );
154
155       if ( $major_version > 7 ) {
156
157         # ideally this would be supported in DBIx-DBSchema and friends
158
159         foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
160
161           warn "updating $table column classnum to integer\n" if $DEBUG;
162           my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
163             "int4(classnum)";
164           my $sth = $dbh->prepare($sql) or die $dbh->errstr;
165           $sth->execute or die $sth->errstr;
166
167         }
168
169       } elsif ( $dbh->{pg_server_version} =~ /^704/ ) {  # earlier?
170
171         # ideally this would be supported in DBIx-DBSchema and friends
172
173         #  XXX_FIXME better locking
174
175         foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
176
177           warn "updating $table column classnum to integer\n" if $DEBUG;
178
179           my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
180           my $sth = $dbh->prepare($sql) or die $dbh->errstr;
181           $sth->execute or die $sth->errstr;
182
183           my $def = dbdef->table($table)->column('classnum');
184           $def->type('integer');
185           $def->length(''); 
186           $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
187           $sth = $dbh->prepare($sql) or die $dbh->errstr;
188           $sth->execute or die $sth->errstr;
189
190           $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
191           $sth = $dbh->prepare($sql) or die $dbh->errstr;
192           $sth->execute or die $sth->errstr;
193
194           $sql = "ALTER TABLE $table DROP old_classnum";
195           $sth = $dbh->prepare($sql) or die $dbh->errstr;
196           $sth->execute or die $sth->errstr;
197
198         }
199
200       } else {
201
202         die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
203
204       }
205
206     } else {
207
208       die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
209
210     }
211
212   }
213
214
215   if ( defined( dbdef->table($class->table)->column('billpkgnum') ) &&
216        defined( dbdef->table($class->table)->column('invnum') ) &&
217        defined( dbdef->table($class->table)->column('pkgnum') ) 
218   ) {
219
220     warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
221
222     my @cbpd = qsearch({ 'table'   => $class->table,
223                          'hashref' => {},
224                          'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
225                                         'pkgnum IS NOT NULL',
226                       });
227
228     if (scalar(@cbpd)) {
229       warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
230
231       foreach my $cbpd ( @cbpd ) {
232         my $detailnum = $cbpd->detailnum;
233         warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
234         my $cust_bill_pkg =
235           qsearchs({ 'table' => 'cust_bill_pkg',
236                      'hashref' => { 'invnum' => $cbpd->invnum,
237                                     'pkgnum' => $cbpd->pkgnum,
238                                   },
239                      'order_by' => 'ORDER BY billpkgnum LIMIT 1',
240                   });
241         if ($cust_bill_pkg) {
242           $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
243           $cbpd->invnum('');
244           $cbpd->pkgnum('');
245           my $error = $cbpd->replace;
246
247           warn "*** WARNING: error replacing line item detail ".
248                "(cust_bill_pkg_detail) $detailnum: $error ***\n"
249             if $error;
250         } else {
251           warn "Found orphaned line item detail $detailnum during upgrade.\n";
252         }
253
254       } # foreach $cbpd
255
256     } # if @cbpd
257
258   } # if billpkgnum, invnum, and pkgnum columns defined
259
260   '';
261
262 }                         
263
264 =back
265
266 =head1 BUGS
267
268 =head1 SEE ALSO
269
270 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.
271
272 =cut
273
274 1;
275