008f3ff7782a8c7901769ffbe5d7429a1bc2326e
[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_enum('format', [ '', 'C' ] )
123     || $self->ut_text('detail')
124     || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
125     || $self->$phonenum_check_method('phonenum')
126     || $self->SUPER::check
127     ;
128
129 }
130
131 # _upgrade_data
132 #
133 # Used by FS::Upgrade to migrate to a new database.
134
135 sub _upgrade_data { # class method
136
137   my ($class, %opts) = @_;
138
139   warn "$me upgrading $class\n" if $DEBUG;
140
141   my $columndef = dbdef->table($class->table)->column('classnum');
142   unless ($columndef->type eq 'int4') {
143
144     my $dbh = dbh;
145     if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
146
147       eval "use DBI::Const::GetInfoType;";
148       die $@ if $@;
149
150       my $major_version = 0;
151       $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
152         && ( $major_version = sprintf("%d", $1) );
153
154       if ( $major_version > 7 ) {
155
156         # ideally this would be supported in DBIx-DBSchema and friends
157
158         foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
159
160           warn "updating $table column classnum to integer\n" if $DEBUG;
161           my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
162             "int4(classnum)";
163           my $sth = $dbh->prepare($sql) or die $dbh->errstr;
164           $sth->execute or die $sth->errstr;
165
166         }
167
168       } elsif ( $dbh->{pg_server_version} =~ /^704/ ) {  # earlier?
169
170         # ideally this would be supported in DBIx-DBSchema and friends
171
172         #  XXX_FIXME better locking
173
174         foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
175
176           warn "updating $table column classnum to integer\n" if $DEBUG;
177
178           my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
179           my $sth = $dbh->prepare($sql) or die $dbh->errstr;
180           $sth->execute or die $sth->errstr;
181
182           my $def = dbdef->table($table)->column('classnum');
183           $def->type('integer');
184           $def->length(''); 
185           $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
186           $sth = $dbh->prepare($sql) or die $dbh->errstr;
187           $sth->execute or die $sth->errstr;
188
189           $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
190           $sth = $dbh->prepare($sql) or die $dbh->errstr;
191           $sth->execute or die $sth->errstr;
192
193           $sql = "ALTER TABLE $table DROP old_classnum";
194           $sth = $dbh->prepare($sql) or die $dbh->errstr;
195           $sth->execute or die $sth->errstr;
196
197         }
198
199       } else {
200
201         die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
202
203       }
204
205     } else {
206
207       die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
208
209     }
210
211   }
212
213
214   if ( defined( dbdef->table($class->table)->column('billpkgnum') ) &&
215        defined( dbdef->table($class->table)->column('invnum') ) &&
216        defined( dbdef->table($class->table)->column('pkgnum') ) 
217   ) {
218
219     warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
220
221     my @cbpd = qsearch({ 'table'   => $class->table,
222                          'hashref' => {},
223                          'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
224                                         'pkgnum IS NOT NULL',
225                       });
226
227     if (scalar(@cbpd)) {
228       warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
229
230       foreach my $cbpd ( @cbpd ) {
231         my $detailnum = $cbpd->detailnum;
232         warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
233         my $cust_bill_pkg =
234           qsearchs({ 'table' => 'cust_bill_pkg',
235                      'hashref' => { 'invnum' => $cbpd->invnum,
236                                     'pkgnum' => $cbpd->pkgnum,
237                                   },
238                      'order_by' => 'ORDER BY billpkgnum LIMIT 1',
239                   });
240         if ($cust_bill_pkg) {
241           $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
242           $cbpd->invnum('');
243           $cbpd->pkgnum('');
244           my $error = $cbpd->replace;
245
246           warn "*** WARNING: error replacing line item detail ".
247                "(cust_bill_pkg_detail) $detailnum: $error ***\n"
248             if $error;
249         } else {
250           warn "Found orphaned line item detail $detailnum during upgrade.\n";
251         }
252
253       } # foreach $cbpd
254
255     } # if @cbpd
256
257   } # if billpkgnum, invnum, and pkgnum columns defined
258
259   '';
260
261 }                         
262
263 =back
264
265 =head1 BUGS
266
267 =head1 SEE ALSO
268
269 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.
270
271 =cut
272
273 1;
274