1 package FS::cust_bill_pkg;
4 use vars qw( @ISA $DEBUG );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_main_Mixin;
10 use FS::cust_bill_pkg_detail;
11 use FS::cust_bill_pkg_display;
12 use FS::cust_bill_pay_pkg;
13 use FS::cust_credit_bill_pkg;
14 use FS::cust_tax_exempt_pkg;
16 @ISA = qw( FS::cust_main_Mixin FS::Record );
22 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
26 use FS::cust_bill_pkg;
28 $record = new FS::cust_bill_pkg \%hash;
29 $record = new FS::cust_bill_pkg { 'column' => 'value' };
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
41 An FS::cust_bill_pkg object represents an invoice line item.
42 FS::cust_bill_pkg inherits from FS::Record. The following fields are currently
47 =item billpkgnum - primary key
49 =item invnum - invoice (see L<FS::cust_bill>)
51 =item pkgnum - package (see L<FS::cust_pkg>) or 0 for the special virtual sales tax package, or -1 for the virtual line item (itemdesc is used for the line)
53 =item pkgpart_override - optional package definition (see L<FS::part_pkg>) override
54 =item setup - setup fee
56 =item recur - recurring fee
58 =item sdate - starting date of recurring fee
60 =item edate - ending date of recurring fee
62 =item itemdesc - Line item description (overrides normal package description)
64 =item quantity - If not set, defaults to 1
66 =item unitsetup - If not set, defaults to setup
68 =item unitrecur - If not set, defaults to recur
72 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
73 see L<Time::Local> and L<Date::Parse> for conversion functions.
81 Creates a new line item. To add the line item to the database, see
82 L<"insert">. Line items are normally created by calling the bill method of a
83 customer object (see L<FS::cust_main>).
87 sub table { 'cust_bill_pkg'; }
91 Adds this line item to the database. If there is an error, returns the error,
92 otherwise returns false.
99 local $SIG{HUP} = 'IGNORE';
100 local $SIG{INT} = 'IGNORE';
101 local $SIG{QUIT} = 'IGNORE';
102 local $SIG{TERM} = 'IGNORE';
103 local $SIG{TSTP} = 'IGNORE';
104 local $SIG{PIPE} = 'IGNORE';
106 my $oldAutoCommit = $FS::UID::AutoCommit;
107 local $FS::UID::AutoCommit = 0;
110 my $error = $self->SUPER::insert;
112 $dbh->rollback if $oldAutoCommit;
116 if ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
117 foreach my $detail ( @{$self->get('details')} ) {
118 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
119 'billpkgnum' => $self->billpkgnum,
120 'format' => (ref($detail) ? $detail->[0] : '' ),
121 'detail' => (ref($detail) ? $detail->[1] : $detail ),
122 'amount' => (ref($detail) ? $detail->[2] : '' ),
123 'classnum' => (ref($detail) ? $detail->[3] : '' ),
125 $error = $cust_bill_pkg_detail->insert;
127 $dbh->rollback if $oldAutoCommit;
133 if ( defined dbdef->table('cust_bill_pkg_display') && $self->get('display') ){
134 foreach my $cust_bill_pkg_display ( @{ $self->get('display') } ) {
135 $cust_bill_pkg_display->billpkgnum($self->billpkgnum);
136 $error = $cust_bill_pkg_display->insert;
138 $dbh->rollback if $oldAutoCommit;
144 if ( $self->_cust_tax_exempt_pkg ) {
145 foreach my $cust_tax_exempt_pkg ( @{$self->_cust_tax_exempt_pkg} ) {
146 $cust_tax_exempt_pkg->billpkgnum($self->billpkgnum);
147 $error = $cust_tax_exempt_pkg->insert;
149 $dbh->rollback if $oldAutoCommit;
155 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
162 Currently unimplemented. I don't remove line items because there would then be
163 no record the items ever existed (which is bad, no?)
168 return "Can't delete cust_bill_pkg records!";
171 =item replace OLD_RECORD
173 Currently unimplemented. This would be even more of an accounting nightmare
174 than deleteing the items. Just don't do it.
179 return "Can't modify cust_bill_pkg records!";
184 Checks all fields to make sure this is a valid line item. If there is an
185 error, returns the error, otherwise returns false. Called by the insert
194 $self->ut_numbern('billpkgnum')
195 || $self->ut_snumber('pkgnum')
196 || $self->ut_number('invnum')
197 || $self->ut_money('setup')
198 || $self->ut_money('recur')
199 || $self->ut_numbern('sdate')
200 || $self->ut_numbern('edate')
201 || $self->ut_textn('itemdesc')
203 return $error if $error;
205 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
206 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
207 return "Unknown pkgnum ". $self->pkgnum
208 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
211 return "Unknown invnum"
212 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
219 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
225 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
230 Returns the package definition for this invoice line item.
236 if ( $self->pkgpart_override ) {
237 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
239 $self->cust_pkg->part_pkg;
245 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
251 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
254 =item details [ OPTION => VALUE ... ]
256 Returns an array of detail information for the invoice line item.
258 Currently available options are: I<format> I<escape_function>
260 If I<format> is set to html or latex then the array members are improved
261 for tabular appearance in those environments if possible.
263 If I<escape_function> is set then the array members are processed by this
264 function before being returned.
269 my ( $self, %opt ) = @_;
270 my $format = $opt{format} || '';
271 my $escape_function = $opt{escape_function} || sub { shift };
272 return () unless defined dbdef->table('cust_bill_pkg_detail');
274 eval "use Text::CSV_XS;";
276 my $csv = new Text::CSV_XS;
278 my $format_sub = sub { my $detail = shift;
279 $csv->parse($detail) or return "can't parse $detail";
280 join(' - ', map { &$escape_function($_) }
285 $format_sub = sub { my $detail = shift;
286 $csv->parse($detail) or return "can't parse $detail";
287 join('</TD><TD>', map { &$escape_function($_) }
291 if $format eq 'html';
293 $format_sub = sub { my $detail = shift;
294 $csv->parse($detail) or return "can't parse $detail";
295 #join(' & ', map { '\small{'. &$escape_function($_). '}' }
299 foreach ($csv->fields) {
300 $result .= ' & ' if $column > 1;
301 if ($column > 6) { # KLUDGE ALERT!
302 $result .= '\multicolumn{1}{l}{\scriptsize{'.
303 &$escape_function($_). '}}';
305 $result .= '\scriptsize{'. &$escape_function($_). '}';
311 if $format eq 'latex';
313 $format_sub = $opt{format_function} if $opt{format_function};
315 map { ( $_->format eq 'C'
316 ? &{$format_sub}( $_->detail )
317 : &{$escape_function}( $_->detail )
320 qsearch ({ 'table' => 'cust_bill_pkg_detail',
321 'hashref' => { 'billpkgnum' => $self->billpkgnum },
322 'order_by' => 'ORDER BY detailnum',
324 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
329 Returns a description for this line item. For typical line items, this is the
330 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
331 For one-shot line items and named taxes, it is the I<itemdesc> field of this
332 line item, and for generic taxes, simply returns "Tax".
339 if ( $self->pkgnum > 0 ) {
340 $self->itemdesc || $self->part_pkg->pkg;
342 $self->itemdesc || 'Tax';
348 Returns the amount owed (still outstanding) on this line item's setup fee,
349 which is the amount of the line item minus all payment applications (see
350 L<FS::cust_bill_pay_pkg> and credit applications (see
351 L<FS::cust_credit_bill_pkg>).
357 $self->owed('setup', @_);
362 Returns the amount owed (still outstanding) on this line item's recurring fee,
363 which is the amount of the line item minus all payment applications (see
364 L<FS::cust_bill_pay_pkg> and credit applications (see
365 L<FS::cust_credit_bill_pkg>).
371 $self->owed('recur', @_);
374 # modeled after cust_bill::owed...
376 my( $self, $field ) = @_;
377 my $balance = $self->$field();
378 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
379 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
380 $balance = sprintf( '%.2f', $balance );
381 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
385 sub cust_bill_pay_pkg {
386 my( $self, $field ) = @_;
387 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
388 'setuprecur' => $field,
393 sub cust_credit_bill_pkg {
394 my( $self, $field ) = @_;
395 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
396 'setuprecur' => $field,
403 Returns the number of billing units (for tax purposes) represented by this,
410 $self->pkgnum ? $self->part_pkg->calc_units($self->cust_pkg) : 0; # 1?
418 my( $self, $value ) = @_;
419 if ( defined($value) ) {
420 $self->setfield('quantity', $value);
422 $self->getfield('quantity') || 1;
430 my( $self, $value ) = @_;
431 if ( defined($value) ) {
432 $self->setfield('unitsetup', $value);
434 $self->getfield('unitsetup') eq ''
435 ? $self->getfield('setup')
436 : $self->getfield('unitsetup');
444 my( $self, $value ) = @_;
445 if ( defined($value) ) {
446 $self->setfield('unitrecur', $value);
448 $self->getfield('unitrecur') eq ''
449 ? $self->getfield('recur')
450 : $self->getfield('unitrecur');
455 Returns a list of cust_bill_pkg objects each with no more than a single class
456 (including setup or recur) of charge.
462 # XXX this goes away with cust_bill_pkg refactor
464 my $cust_bill_pkg = new FS::cust_bill_pkg { $self->hash };
465 my %cust_bill_pkg = ();
467 $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
468 $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
471 #split setup and recur
472 if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
473 my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
474 $cust_bill_pkg->set('details', []);
475 $cust_bill_pkg->recur(0);
476 $cust_bill_pkg->unitrecur(0);
477 $cust_bill_pkg->type('');
478 $cust_bill_pkg_recur->setup(0);
479 $cust_bill_pkg_recur->unitsetup(0);
480 $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
484 #split usage from recur
485 my $usage = sprintf( "%.2f", $cust_bill_pkg{recur}->usage );
486 warn "usage is $usage\n" if $DEBUG;
488 my $cust_bill_pkg_usage =
489 new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
490 $cust_bill_pkg_usage->recur( $usage );
491 $cust_bill_pkg_usage->type( 'U' );
492 my $recur = sprintf( "%.2f", $cust_bill_pkg{recur}->recur - $usage );
493 $cust_bill_pkg{recur}->recur( $recur );
494 $cust_bill_pkg{recur}->type( '' );
495 $cust_bill_pkg{recur}->set('details', []);
496 $cust_bill_pkg{''} = $cust_bill_pkg_usage;
499 #subdivide usage by usage_class
500 if (exists($cust_bill_pkg{''})) {
501 foreach my $class (grep { $_ } $self->usage_classes) {
502 my $usage = sprintf( "%.2f", $cust_bill_pkg{''}->usage($class) );
503 my $cust_bill_pkg_usage =
504 new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
505 $cust_bill_pkg_usage->recur( $usage );
506 $cust_bill_pkg_usage->set('details', []);
507 my $classless = sprintf( "%.2f", $cust_bill_pkg{''}->recur - $usage );
508 $cust_bill_pkg{''}->recur( $classless );
509 $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
511 delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
514 # # sort setup,recur,'', and the rest numeric && return
515 # my @result = map { $cust_bill_pkg{$_} }
516 # sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
517 # ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
519 # keys %cust_bill_pkg;
528 Returns the amount of the charge associated with usage class CLASSNUM if
529 CLASSNUM is defined. Otherwise returns the total charge associated with
535 my( $self, $classnum ) = @_;
539 if ( $self->get('details') ) {
543 grep { ref($_) && ( defined($classnum) ? $_->[3] eq $classnum : 1 ) }
544 @{ $self->get('details') };
548 my $hashref = { 'billpkgnum' => $self->billpkgnum };
549 $hashref->{ 'classnum' } = $classnum if defined($classnum);
550 @values = map { $_->amount } qsearch('cust_bill_pkg_detail', $hashref);
554 foreach ( @values ) {
562 Returns a list of usage classnums associated with this invoice line's
570 if ( $self->get('details') ) {
573 foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) {
574 $seen{ $detail->[3] } = 1;
581 qsearch({ table => 'cust_bill_pkg_detail',
582 hashref => { billpkgnum => $self->billpkgnum },
583 select => 'DISTINCT classnum',
590 =item cust_bill_pkg_display [ type => TYPE ]
592 Returns an array of display information for the invoice line item optionally
597 sub cust_bill_pkg_display {
598 my ( $self, %opt ) = @_;
601 new FS::cust_bill_pkg_display { billpkgnum =>$self->billpkgnum };
603 return ( $default ) unless defined dbdef->table('cust_bill_pkg_display');#hmmm
605 my $type = $opt{type} if exists $opt{type};
608 if ( scalar( $self->get('display') ) ) {
609 @result = grep { defined($type) ? ($type eq $_->type) : 1 }
610 @{ $self->get('display') };
612 my $hashref = { 'billpkgnum' => $self->billpkgnum };
613 $hashref->{type} = $type if defined($type);
615 @result = qsearch ({ 'table' => 'cust_bill_pkg_display',
616 'hashref' => { 'billpkgnum' => $self->billpkgnum },
617 'order_by' => 'ORDER BY billpkgdisplaynum',
621 push @result, $default unless ( scalar(@result) || $type );
627 # reserving this name for my friends FS::{tax_rate|cust_main_county}::taxline
628 # and FS::cust_main::bill
630 sub _cust_tax_exempt_pkg {
633 $self->{Hash}->{_cust_tax_exempt_pkg} or
634 $self->{Hash}->{_cust_tax_exempt_pkg} = [];
643 setup and recur shouldn't be separate fields. There should be one "amount"
644 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
646 A line item with both should really be two separate records (preserving
647 sdate and edate for setup fees for recurring packages - that information may
648 be valuable later). Invoice generation (cust_main::bill), invoice printing
649 (cust_bill), tax reports (report_tax.cgi) and line item reports
650 (cust_bill_pkg.cgi) would need to be updated.
652 owed_setup and owed_recur could then be repaced by just owed, and
653 cust_bill::open_cust_bill_pkg and
654 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
658 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
659 from the base documentation.