1 package FS::cust_bill_pkg_detail;
4 use vars qw( @ISA $me $DEBUG %GetInfoType );
6 use FS::Record qw( qsearch qsearchs dbdef dbh );
11 @ISA = qw(FS::Record);
12 $me = '[ FS::cust_bill_pkg_detail ]';
17 FS::cust_bill_pkg_detail - Object methods for cust_bill_pkg_detail records
21 use FS::cust_bill_pkg_detail;
23 $record = new FS::cust_bill_pkg_detail \%hash;
24 $record = new FS::cust_bill_pkg_detail { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::cust_bill_pkg_detail object represents additional detail information for
37 an invoice line item (see L<FS::cust_bill_pkg>). FS::cust_bill_pkg_detail
38 inherits from FS::Record. The following fields are currently supported:
42 =item detailnum - primary key
44 =item billpkgnum - link to cust_bill_pkg
46 =item amount - price of this line item detail
48 =item format - '' for straight text and 'C' for CSV in detail
50 =item classnum - link to usage_class
52 =item duration - granularized number of seconds for this call
58 =item accountcode - accountcode
60 =item startdate - CDR startdate, if any
62 =item detail - detail description
72 Creates a new line item detail. To add the line item detail to the database,
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to. You can ask the object for a copy with the I<hash> method.
80 # the new method can be inherited from FS::Record, if a table method is defined
82 sub table { 'cust_bill_pkg_detail'; }
86 Adds this record to the database. If there is an error, returns the error,
87 otherwise returns false.
93 my $error = $self->SUPER::insert(@_);
94 return $error if $error;
97 my $acctids = $self->get('acctid') or return '';
98 $acctids = [ $acctids ] unless ref $acctids;
99 foreach my $acctid ( @$acctids ) {
100 my $cdr = FS::cdr->by_key($acctid);
101 $cdr->set('detailnum', $self->detailnum);
102 $error = $cdr->replace;
103 # this should never happen
104 return "error linking CDR #$acctid: $error" if $error;
109 =item delete [ ARG => VALUE ... ]
111 Delete this record from the database.
113 If the "reprocess_cdrs" argument is set to true, resets the status of any
114 related CDRs (and deletes their associated cdr_termination records, if any).
119 my( $self, %args ) = @_;
121 my $error = $self->SUPER::delete;
122 return $error if $error;
124 foreach my $cdr (qsearch('cdr', { detailnum => $self->detailnum })) {
126 $cdr->set('detailnum', '');
127 $cdr->set('freesidestatus', '') if $args{'reprocess_cdrs'};
128 $error = $cdr->replace;
129 return "error unlinking CDR #" . $cdr->acctid . ": $error" if $error;
131 #well, technically this could have been on other invoices / termination
132 # partners... separate flag?
133 $self->scalar_sql( 'DELETE FROM cdr_termination WHERE acctid = ?',
135 if $args{'reprocess_cdrs'};
142 =item replace OLD_RECORD
144 Replaces the OLD_RECORD with this one in the database. If there is an error,
145 returns the error, otherwise returns false.
149 # the replace method can be inherited from FS::Record (doesn't touch CDRs)
153 Checks all fields to make sure this is a valid line item detail. If there is
154 an error, returns the error, otherwise returns false. Called by the insert
159 # the check method should currently be supplied - FS::Record contains some
160 # data checking routines
165 my $conf = new FS::Conf;
167 my $phonenum = $self->phonenum;
168 my $phonenum_check_method;
169 if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
170 $phonenum =~ s/\W//g;
171 $phonenum_check_method = 'ut_alphan';
173 $phonenum =~ s/\D//g;
174 $phonenum_check_method = 'ut_numbern';
176 $self->phonenum($phonenum);
178 $self->ut_numbern('detailnum')
179 || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum')
180 #|| $self->ut_moneyn('amount')
181 || $self->ut_floatn('amount')
182 || $self->ut_enum('format', [ '', 'C' ] )
183 || $self->ut_numbern('duration')
184 || $self->ut_textn('regionname')
185 || $self->ut_textn('accountcode')
186 || $self->ut_text('detail')
187 || $self->ut_foreign_keyn('classnum', 'usage_class', 'classnum')
188 || $self->$phonenum_check_method('phonenum')
189 || $self->ut_numbern('startdate')
190 || $self->SUPER::check
195 =item formatted [ OPTION => VALUE ... ]
197 Returns detail information for the invoice line item detail formatted for
200 Currently available options are: I<format> I<escape_function>
202 If I<format> is set to html or latex then the format is improved
203 for tabular appearance in those environments if possible.
205 If I<escape_function> is set then the format is processed by this
206 function before being returned.
208 DEPRECATED? (mostly unused, expensive)
209 If I<format_function> is set then the detail is handed to this callback
214 #totally false laziness w/cust_bill_pkg->detail
216 my ( $self, %opt ) = @_;
217 my $format = $opt{format} || '';
218 return () unless defined dbdef->table('cust_bill_pkg_detail');
220 eval "use Text::CSV_XS;";
222 my $csv = new Text::CSV_XS;
224 my $escape_function = sub { shift };
226 $escape_function = \&encode_entities
227 if $format eq 'html';
232 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
233 $value =~ s/([<>])/\$$1\$/g;
236 if $format eq 'latex';
238 $escape_function = $opt{escape_function} if $opt{escape_function};
240 my $format_sub = sub { my $detail = shift;
241 $csv->parse($detail) or return "can't parse $detail";
242 join(' - ', map { &$escape_function($_) }
247 $format_sub = sub { my $detail = shift;
248 $csv->parse($detail) or return "can't parse $detail";
249 join('</TD><TD>', map { &$escape_function($_) }
253 if $format eq 'html';
255 $format_sub = sub { my $detail = shift;
256 $csv->parse($detail) or return "can't parse $detail";
257 #join(' & ', map { '\small{'. &$escape_function($_). '}' } # $csv->fields );
260 foreach ($csv->fields) {
261 $result .= ' & ' if $column > 1;
262 if ($column > 6) { # KLUDGE ALERT!
263 $result .= '\multicolumn{1}{l}{\scriptsize{'.
264 &$escape_function($_). '}}';
266 $result .= '\scriptsize{'. &$escape_function($_). '}';
272 if $format eq 'latex';
274 $format_sub = $opt{format_function} if $opt{format_function};
277 ? &{$format_sub}( $self->detail, $self )
278 : &{$escape_function}( $self->detail )
284 Returns the L<FS::cust_bill_pkg> object (the invoice line item) that
285 this detail belongs to.
291 my $billpkgnum = $self->billpkgnum or return '';
292 FS::cust_bill_pkg->by_key($billpkgnum);
295 # Used by FS::Upgrade to migrate to a new database schema
296 sub _upgrade_schema { # class method
298 my ($class, %opts) = @_;
300 warn "$me upgrading $class\n" if $DEBUG;
302 my $classnum = dbdef->table($class->table)->column('classnum')
305 my $type = $classnum->type;
306 unless ( $type =~ /^int/i || $type =~ /int$/i ) {
309 if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
311 eval "use DBI::Const::GetInfoType;";
314 my $major_version = 0;
315 $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
316 && ( $major_version = sprintf("%d", $1) );
318 if ( $major_version > 7 ) {
320 # ideally this would be supported in DBIx-DBSchema and friends
322 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
324 warn "updating $table column classnum to integer\n" if $DEBUG;
325 my $sql = "ALTER TABLE $table ALTER classnum TYPE int USING ".
327 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
328 $sth->execute or die $sth->errstr;
332 } elsif ( $dbh->{pg_server_version} =~ /^704/ ) { # earlier?
334 # ideally this would be supported in DBIx-DBSchema and friends
336 # XXX_FIXME better locking
338 foreach my $table ( qw( cust_bill_pkg_detail h_cust_bill_pkg_detail ) ){
340 warn "updating $table column classnum to integer\n" if $DEBUG;
342 my $sql = "ALTER TABLE $table RENAME classnum TO old_classnum";
343 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
344 $sth->execute or die $sth->errstr;
346 my $def = dbdef->table($table)->column('classnum');
347 $def->type('integer');
349 $sql = "ALTER TABLE $table ADD COLUMN ". $def->line($dbh);
350 $sth = $dbh->prepare($sql) or die $dbh->errstr;
351 $sth->execute or die $sth->errstr;
353 $sql = "UPDATE $table SET classnum = int4( text( old_classnum ) )";
354 $sth = $dbh->prepare($sql) or die $dbh->errstr;
355 $sth->execute or die $sth->errstr;
357 $sql = "ALTER TABLE $table DROP old_classnum";
358 $sth = $dbh->prepare($sql) or die $dbh->errstr;
359 $sth->execute or die $sth->errstr;
365 die "cust_bill_pkg_detail classnum upgrade unsupported for this Pg version\n";
371 die "cust_bill_pkg_detail classnum upgrade only supported for Pg 8+\n";
379 # Used by FS::Upgrade to migrate to a new database
380 sub _upgrade_data { # class method
382 my ($class, %opts) = @_;
384 warn "$me Checking for unmigrated invoice line item details\n" if $DEBUG;
386 my @cbpd = qsearch({ 'table' => $class->table,
388 'extra_sql' => 'WHERE invnum IS NOT NULL AND '.
389 'pkgnum IS NOT NULL',
393 warn "$me Found unmigrated invoice line item details\n" if $DEBUG;
395 foreach my $cbpd ( @cbpd ) {
396 my $detailnum = $cbpd->detailnum;
397 warn "$me Contemplating detail $detailnum\n" if $DEBUG > 1;
399 qsearchs({ 'table' => 'cust_bill_pkg',
400 'hashref' => { 'invnum' => $cbpd->invnum,
401 'pkgnum' => $cbpd->pkgnum,
403 'order_by' => 'ORDER BY billpkgnum LIMIT 1',
405 if ($cust_bill_pkg) {
406 $cbpd->billpkgnum($cust_bill_pkg->billpkgnum);
409 my $error = $cbpd->replace;
411 warn "*** WARNING: error replacing line item detail ".
412 "(cust_bill_pkg_detail) $detailnum: $error ***\n"
415 warn "Found orphaned line item detail $detailnum during upgrade.\n";
432 L<FS::cust_bill_pkg>, L<FS::Record>, schema.html from the base documentation.