1 package FS::cust_bill_pkg_detail;
4 use vars qw( @ISA $me $DEBUG %GetInfoType );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
10 $me = '[ FS::cust_bill_pkg_detail ]';
15 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
19 use FS::cust_bill_pkg_detail;
21 $record = new FS::cust_bill_pkg_detail \%hash;
22 $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
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:
40 =item detailnum - primary key
42 =item billpkgnum - link to cust_bill_pkg
44 =item detail - detail description
54 Creates a new line item detail. To add the line item detail to the database,
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.
62 # the new method can be inherited from FS::Record, if a table method is defined
64 sub table { 'cust_bill_pkg_detail'; }
68 Adds this record to the database. If there is an error, returns the error,
69 otherwise returns false.
73 # the insert method can be inherited from FS::Record
77 Delete this record from the database.
81 # the delete method can be inherited from FS::Record
83 =item replace OLD_RECORD
85 Replaces the OLD_RECORD with this one in the database. If there is an error,
86 returns the error, otherwise returns false.
90 # the replace method can be inherited from FS::Record
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
100 # the check method should currently be supplied - FS::Record contains some
101 # data checking routines
106 my $conf = new FS::Conf;
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';
114 $phonenum =~ s/\D//g;
115 $phonenum_check_method = 'ut_numbern';
117 $self->phonenum($phonenum);
119 $self->ut_numbern('detailnum')
120 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
121 #|| $self->ut_moneyn('amount')
122 || $self->ut_floatn('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
134 # Used by FS::Upgrade to migrate to a new database.
136 sub _upgrade_data { # class method
138 my ($class, %opts) = @_;
140 warn "$me upgrading $class\n" if $DEBUG;
142 my $columndef = dbdef->table($class->table)->column('classnum');
143 unless ($columndef->type eq 'int4') {
146 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
148 eval "use DBI::Const::GetInfoType;";
151 my $major_version = 0;
152 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
153 && ( $major_version = sprintf("%d", $1) );
155 if ( $major_version > 7 ) {
157 # ideally this would be supported in DBIx-DBSchema and friends
159 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
161 warn "updating $table column classnum to integer\n" if $DEBUG;
162 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
164 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
165 $sth->execute or die $sth->errstr;
169 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
171 # ideally this would be supported in DBIx-DBSchema and friends
173 # XXX_FIXME better locking
175 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
177 warn "updating $table column classnum to integer\n" if $DEBUG;
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;
183 my $def = dbdef->table($table)->column('classnum');
184 $def->type('integer');
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;
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;
194 $sql = "ALTER TABLE $table DROP old_classnum";
195 $sth = $dbh->prepare($sql) or die $dbh->errstr;
196 $sth->execute or die $sth->errstr;
202 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
208 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
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') )
220 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
222 my @cbpd = qsearch({ 'table' => $class->table,
224 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
225 'pkgnum IS NOT NULL',
229 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
231 foreach my $cbpd ( @cbpd ) {
232 my $detailnum = $cbpd->detailnum;
233 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
235 qsearchs({ 'table' => 'cust_bill_pkg',
236 'hashref' => { 'invnum' => $cbpd->invnum,
237 'pkgnum' => $cbpd->pkgnum,
239 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
241 if ($cust_bill_pkg) {
242 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
245 my $error = $cbpd->replace;
247 warn "*** WARNING: error replacing line item detail ".
248 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
251 warn "Found orphaned line item detail $detailnum during upgrade.\n";
258 } # if billpkgnum, invnum, and pkgnum columns defined
270 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.