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_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
133 # Used by FS::Upgrade to migrate to a new database.
135 sub _upgrade_data { # class method
137 my ($class, %opts) = @_;
139 warn "$me upgrading $class\n" if $DEBUG;
141 my $columndef = dbdef->table($class->table)->column('classnum');
142 unless ($columndef->type eq 'int4') {
145 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
147 eval "use DBI::Const::GetInfoType;";
150 my $major_version = 0;
151 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
152 && ( $major_version = sprintf("%d", $1) );
154 if ( $major_version > 7 ) {
156 # ideally this would be supported in DBIx-DBSchema and friends
158 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
160 warn "updating $table column classnum to integer\n" if $DEBUG;
161 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
163 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
164 $sth->execute or die $sth->errstr;
168 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
170 # ideally this would be supported in DBIx-DBSchema and friends
172 # XXX_FIXME better locking
174 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
176 warn "updating $table column classnum to integer\n" if $DEBUG;
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;
182 my $def = dbdef->table($table)->column('classnum');
183 $def->type('integer');
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;
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;
193 $sql = "ALTER TABLE $table DROP old_classnum";
194 $sth = $dbh->prepare($sql) or die $dbh->errstr;
195 $sth->execute or die $sth->errstr;
201 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
207 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
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') )
219 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
221 my @cbpd = qsearch({ 'table' => $class->table,
223 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
224 'pkgnum IS NOT NULL',
228 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
230 foreach my $cbpd ( @cbpd ) {
231 my $detailnum = $cbpd->detailnum;
232 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
234 qsearchs({ 'table' => 'cust_bill_pkg',
235 'hashref' => { 'invnum' => $cbpd->invnum,
236 'pkgnum' => $cbpd->pkgnum,
238 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
240 if ($cust_bill_pkg) {
241 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
244 my $error = $cbpd->replace;
246 warn "*** WARNING: error replacing line item detail ".
247 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
250 warn "Found orphaned line item detail $detailnum during upgrade.\n";
257 } # if billpkgnum, invnum, and pkgnum columns defined
269 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.