1 package FS::cust_bill_pkg_detail;
4 use vars qw( @ISA $me $DEBUG %GetInfoType );
6 use FS::Record qw( qsearch qsearchs dbdef dbh );
10 @ISA = qw(FS::Record);
11 $me = '[ FS::cust_bill_pkg_detail ]';
16 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
20 use FS::cust_bill_pkg_detail;
22 $record = new FS::cust_bill_pkg_detail \%hash;
23 $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::cust_bill_pkg_detail object represents additional detail information for
36 an invoice line item (see L<FS::cust_bill_pkg>). FS::cust_bill_pkg_detail
37 inherits from FS::Record. The following fields are currently supported:
41 =item detailnum - primary key
43 =item billpkgnum - link to cust_bill_pkg
45 =item amount - price of this line item detail
47 =item format - '' for straight text and 'C' for CSV in detail
49 =item classnum - link to usage_class
51 =item duration - granularized number of seconds for this call
57 =item detail - detail description
67 Creates a new line item detail. To add the line item detail to the database,
70 Note that this stores the hash reference, not a distinct copy of the hash it
71 points to. You can ask the object for a copy with the I<hash> method.
75 # the new method can be inherited from FS::Record, if a table method is defined
77 sub table { 'cust_bill_pkg_detail'; }
81 Adds this record to the database. If there is an error, returns the error,
82 otherwise returns false.
86 # the insert method can be inherited from FS::Record
90 Delete this record from the database.
94 # the delete method can be inherited from FS::Record
96 =item replace OLD_RECORD
98 Replaces the OLD_RECORD with this one in the database. If there is an error,
99 returns the error, otherwise returns false.
103 # the replace method can be inherited from FS::Record
107 Checks all fields to make sure this is a valid line item detail. If there is
108 an error, returns the error, otherwise returns false. Called by the insert
113 # the check method should currently be supplied - FS::Record contains some
114 # data checking routines
119 my $conf = new FS::Conf;
121 my $phonenum = $self->phonenum;
122 my $phonenum_check_method;
123 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
124 $phonenum =~ s/\W//g;
125 $phonenum_check_method = 'ut_alphan';
127 $phonenum =~ s/\D//g;
128 $phonenum_check_method = 'ut_numbern';
130 $self->phonenum($phonenum);
132 $self->ut_numbern('detailnum')
133 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
134 #|| $self->ut_moneyn('amount')
135 || $self->ut_floatn('amount')
136 || $self->ut_enum('format', [ '', 'C' ] )
137 || $self->ut_numbern('duration')
138 || $self->ut_textn('regionname')
139 || $self->ut_text('detail')
140 || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
141 || $self->$phonenum_check_method('phonenum')
142 || $self->SUPER::check
147 =item formatted [ OPTION => VALUE ... ]
149 Returns detail information for the invoice line item detail formatted for
152 Currently available options are: I<format> I<escape_function>
154 If I<format> is set to html or latex then the format is improved
155 for tabular appearance in those environments if possible.
157 If I<escape_function> is set then the format is processed by this
158 function before being returned.
160 If I<format_function> is set then the detail is handed to this callback
166 my ( $self, %opt ) = @_;
167 my $format = $opt{format} || '';
168 return () unless defined dbdef->table('cust_bill_pkg_detail');
170 eval "use Text::CSV_XS;";
172 my $csv = new Text::CSV_XS;
174 my $escape_function = sub { shift };
176 $escape_function = \&encode_entities
177 if $format eq 'html';
182 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
183 $value =~ s/([<>])/\$$1\$/g;
186 if $format eq 'latex';
188 $escape_function = $opt{escape_function} if $opt{escape_function};
190 my $format_sub = sub { my $detail = shift;
191 $csv->parse($detail) or return "can't parse $detail";
192 join(' - ', map { &$escape_function($_) }
197 $format_sub = sub { my $detail = shift;
198 $csv->parse($detail) or return "can't parse $detail";
199 join('</TD><TD>', map { &$escape_function($_) }
203 if $format eq 'html';
205 $format_sub = sub { my $detail = shift;
206 $csv->parse($detail) or return "can't parse $detail";
207 #join(' & ', map { '\small{'. &$escape_function($_). '}' } # $csv->fields );
210 foreach ($csv->fields) {
211 $result .= ' & ' if $column > 1;
212 if ($column > 6) { # KLUDGE ALERT!
213 $result .= '\multicolumn{1}{l}{\scriptsize{'.
214 &$escape_function($_). '}}';
216 $result .= '\scriptsize{'. &$escape_function($_). '}';
222 if $format eq 'latex';
224 $format_sub = $opt{format_function} if $opt{format_function};
227 ? &{$format_sub}( $self->detail, $self )
228 : &{$escape_function}( $self->detail )
235 # Used by FS::Upgrade to migrate to a new database.
237 sub _upgrade_data { # class method
239 my ($class, %opts) = @_;
241 warn "$me upgrading $class\n" if $DEBUG;
243 my $columndef = dbdef->table($class->table)->column('classnum');
244 unless ($columndef->type eq 'int4') {
247 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
249 eval "use DBI::Const::GetInfoType;";
252 my $major_version = 0;
253 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
254 && ( $major_version = sprintf("%d", $1) );
256 if ( $major_version > 7 ) {
258 # ideally this would be supported in DBIx-DBSchema and friends
260 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
262 warn "updating $table column classnum to integer\n" if $DEBUG;
263 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
265 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
266 $sth->execute or die $sth->errstr;
270 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
272 # ideally this would be supported in DBIx-DBSchema and friends
274 # XXX_FIXME better locking
276 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
278 warn "updating $table column classnum to integer\n" if $DEBUG;
280 my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
281 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
282 $sth->execute or die $sth->errstr;
284 my $def = dbdef->table($table)->column('classnum');
285 $def->type('integer');
287 $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
288 $sth = $dbh->prepare($sql) or die $dbh->errstr;
289 $sth->execute or die $sth->errstr;
291 $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
292 $sth = $dbh->prepare($sql) or die $dbh->errstr;
293 $sth->execute or die $sth->errstr;
295 $sql = "ALTER TABLE $table DROP old_classnum";
296 $sth = $dbh->prepare($sql) or die $dbh->errstr;
297 $sth->execute or die $sth->errstr;
303 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
309 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
316 if ( defined( dbdef->table($class->table)->column('billpkgnum') ) &&
317 defined( dbdef->table($class->table)->column('invnum') ) &&
318 defined( dbdef->table($class->table)->column('pkgnum') )
321 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
323 my @cbpd = qsearch({ 'table' => $class->table,
325 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
326 'pkgnum IS NOT NULL',
330 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
332 foreach my $cbpd ( @cbpd ) {
333 my $detailnum = $cbpd->detailnum;
334 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
336 qsearchs({ 'table' => 'cust_bill_pkg',
337 'hashref' => { 'invnum' => $cbpd->invnum,
338 'pkgnum' => $cbpd->pkgnum,
340 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
342 if ($cust_bill_pkg) {
343 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
346 my $error = $cbpd->replace;
348 warn "*** WARNING: error replacing line item detail ".
349 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
352 warn "Found orphaned line item detail $detailnum during upgrade.\n";
359 } # if billpkgnum, invnum, and pkgnum columns defined
371 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.