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 my $tax_location = $self->get('cust_bill_pkg_tax_location');
156 if ( $tax_location ) {
157 foreach my $cust_bill_pkg_tax_location ( @$tax_location ) {
158 $cust_bill_pkg_tax_location->billpkgnum($self->billpkgnum);
159 warn $cust_bill_pkg_tax_location;
160 $error = $cust_bill_pkg_tax_location->insert;
163 $dbh->rollback if $oldAutoCommit;
169 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
176 Currently unimplemented. I don't remove line items because there would then be
177 no record the items ever existed (which is bad, no?)
182 return "Can't delete cust_bill_pkg records!";
185 =item replace OLD_RECORD
187 Currently unimplemented. This would be even more of an accounting nightmare
188 than deleteing the items. Just don't do it.
193 return "Can't modify cust_bill_pkg records!";
198 Checks all fields to make sure this is a valid line item. If there is an
199 error, returns the error, otherwise returns false. Called by the insert
208 $self->ut_numbern('billpkgnum')
209 || $self->ut_snumber('pkgnum')
210 || $self->ut_number('invnum')
211 || $self->ut_money('setup')
212 || $self->ut_money('recur')
213 || $self->ut_numbern('sdate')
214 || $self->ut_numbern('edate')
215 || $self->ut_textn('itemdesc')
217 return $error if $error;
219 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
220 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
221 return "Unknown pkgnum ". $self->pkgnum
222 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
225 return "Unknown invnum"
226 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
233 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
239 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
244 Returns the package definition for this invoice line item.
250 if ( $self->pkgpart_override ) {
251 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
253 $self->cust_pkg->part_pkg;
259 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
265 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
268 =item details [ OPTION => VALUE ... ]
270 Returns an array of detail information for the invoice line item.
272 Currently available options are: I<format> I<escape_function>
274 If I<format> is set to html or latex then the array members are improved
275 for tabular appearance in those environments if possible.
277 If I<escape_function> is set then the array members are processed by this
278 function before being returned.
283 my ( $self, %opt ) = @_;
284 my $format = $opt{format} || '';
285 my $escape_function = $opt{escape_function} || sub { shift };
286 return () unless defined dbdef->table('cust_bill_pkg_detail');
288 eval "use Text::CSV_XS;";
290 my $csv = new Text::CSV_XS;
292 my $format_sub = sub { my $detail = shift;
293 $csv->parse($detail) or return "can't parse $detail";
294 join(' - ', map { &$escape_function($_) }
299 $format_sub = sub { my $detail = shift;
300 $csv->parse($detail) or return "can't parse $detail";
301 join('</TD><TD>', map { &$escape_function($_) }
305 if $format eq 'html';
307 $format_sub = sub { my $detail = shift;
308 $csv->parse($detail) or return "can't parse $detail";
309 #join(' & ', map { '\small{'. &$escape_function($_). '}' }
313 foreach ($csv->fields) {
314 $result .= ' & ' if $column > 1;
315 if ($column > 6) { # KLUDGE ALERT!
316 $result .= '\multicolumn{1}{l}{\scriptsize{'.
317 &$escape_function($_). '}}';
319 $result .= '\scriptsize{'. &$escape_function($_). '}';
325 if $format eq 'latex';
327 $format_sub = $opt{format_function} if $opt{format_function};
329 map { ( $_->format eq 'C'
330 ? &{$format_sub}( $_->detail )
331 : &{$escape_function}( $_->detail )
334 qsearch ({ 'table' => 'cust_bill_pkg_detail',
335 'hashref' => { 'billpkgnum' => $self->billpkgnum },
336 'order_by' => 'ORDER BY detailnum',
338 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
343 Returns a description for this line item. For typical line items, this is the
344 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
345 For one-shot line items and named taxes, it is the I<itemdesc> field of this
346 line item, and for generic taxes, simply returns "Tax".
353 if ( $self->pkgnum > 0 ) {
354 $self->itemdesc || $self->part_pkg->pkg;
356 $self->itemdesc || 'Tax';
362 Returns the amount owed (still outstanding) on this line item's setup 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('setup', @_);
376 Returns the amount owed (still outstanding) on this line item's recurring fee,
377 which is the amount of the line item minus all payment applications (see
378 L<FS::cust_bill_pay_pkg> and credit applications (see
379 L<FS::cust_credit_bill_pkg>).
385 $self->owed('recur', @_);
388 # modeled after cust_bill::owed...
390 my( $self, $field ) = @_;
391 my $balance = $self->$field();
392 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
393 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
394 $balance = sprintf( '%.2f', $balance );
395 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
399 sub cust_bill_pay_pkg {
400 my( $self, $field ) = @_;
401 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
402 'setuprecur' => $field,
407 sub cust_credit_bill_pkg {
408 my( $self, $field ) = @_;
409 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
410 'setuprecur' => $field,
417 Returns the number of billing units (for tax purposes) represented by this,
424 $self->pkgnum ? $self->part_pkg->calc_units($self->cust_pkg) : 0; # 1?
432 my( $self, $value ) = @_;
433 if ( defined($value) ) {
434 $self->setfield('quantity', $value);
436 $self->getfield('quantity') || 1;
444 my( $self, $value ) = @_;
445 if ( defined($value) ) {
446 $self->setfield('unitsetup', $value);
448 $self->getfield('unitsetup') eq ''
449 ? $self->getfield('setup')
450 : $self->getfield('unitsetup');
458 my( $self, $value ) = @_;
459 if ( defined($value) ) {
460 $self->setfield('unitrecur', $value);
462 $self->getfield('unitrecur') eq ''
463 ? $self->getfield('recur')
464 : $self->getfield('unitrecur');
469 Returns a list of cust_bill_pkg objects each with no more than a single class
470 (including setup or recur) of charge.
476 # XXX this goes away with cust_bill_pkg refactor
478 my $cust_bill_pkg = new FS::cust_bill_pkg { $self->hash };
479 my %cust_bill_pkg = ();
481 $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
482 $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
485 #split setup and recur
486 if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
487 my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
488 $cust_bill_pkg->set('details', []);
489 $cust_bill_pkg->recur(0);
490 $cust_bill_pkg->unitrecur(0);
491 $cust_bill_pkg->type('');
492 $cust_bill_pkg_recur->setup(0);
493 $cust_bill_pkg_recur->unitsetup(0);
494 $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
498 #split usage from recur
499 my $usage = sprintf( "%.2f", $cust_bill_pkg{recur}->usage );
500 warn "usage is $usage\n" if $DEBUG;
502 my $cust_bill_pkg_usage =
503 new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
504 $cust_bill_pkg_usage->recur( $usage );
505 $cust_bill_pkg_usage->type( 'U' );
506 my $recur = sprintf( "%.2f", $cust_bill_pkg{recur}->recur - $usage );
507 $cust_bill_pkg{recur}->recur( $recur );
508 $cust_bill_pkg{recur}->type( '' );
509 $cust_bill_pkg{recur}->set('details', []);
510 $cust_bill_pkg{''} = $cust_bill_pkg_usage;
513 #subdivide usage by usage_class
514 if (exists($cust_bill_pkg{''})) {
515 foreach my $class (grep { $_ } $self->usage_classes) {
516 my $usage = sprintf( "%.2f", $cust_bill_pkg{''}->usage($class) );
517 my $cust_bill_pkg_usage =
518 new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
519 $cust_bill_pkg_usage->recur( $usage );
520 $cust_bill_pkg_usage->set('details', []);
521 my $classless = sprintf( "%.2f", $cust_bill_pkg{''}->recur - $usage );
522 $cust_bill_pkg{''}->recur( $classless );
523 $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
525 delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
528 # # sort setup,recur,'', and the rest numeric && return
529 # my @result = map { $cust_bill_pkg{$_} }
530 # sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
531 # ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
533 # keys %cust_bill_pkg;
542 Returns the amount of the charge associated with usage class CLASSNUM if
543 CLASSNUM is defined. Otherwise returns the total charge associated with
549 my( $self, $classnum ) = @_;
553 if ( $self->get('details') ) {
557 grep { ref($_) && ( defined($classnum) ? $_->[3] eq $classnum : 1 ) }
558 @{ $self->get('details') };
562 my $hashref = { 'billpkgnum' => $self->billpkgnum };
563 $hashref->{ 'classnum' } = $classnum if defined($classnum);
564 @values = map { $_->amount } qsearch('cust_bill_pkg_detail', $hashref);
568 foreach ( @values ) {
576 Returns a list of usage classnums associated with this invoice line's
584 if ( $self->get('details') ) {
587 foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) {
588 $seen{ $detail->[3] } = 1;
595 qsearch({ table => 'cust_bill_pkg_detail',
596 hashref => { billpkgnum => $self->billpkgnum },
597 select => 'DISTINCT classnum',
604 =item cust_bill_pkg_display [ type => TYPE ]
606 Returns an array of display information for the invoice line item optionally
611 sub cust_bill_pkg_display {
612 my ( $self, %opt ) = @_;
615 new FS::cust_bill_pkg_display { billpkgnum =>$self->billpkgnum };
617 return ( $default ) unless defined dbdef->table('cust_bill_pkg_display');#hmmm
619 my $type = $opt{type} if exists $opt{type};
622 if ( scalar( $self->get('display') ) ) {
623 @result = grep { defined($type) ? ($type eq $_->type) : 1 }
624 @{ $self->get('display') };
626 my $hashref = { 'billpkgnum' => $self->billpkgnum };
627 $hashref->{type} = $type if defined($type);
629 @result = qsearch ({ 'table' => 'cust_bill_pkg_display',
630 'hashref' => { 'billpkgnum' => $self->billpkgnum },
631 'order_by' => 'ORDER BY billpkgdisplaynum',
635 push @result, $default unless ( scalar(@result) || $type );
641 # reserving this name for my friends FS::{tax_rate|cust_main_county}::taxline
642 # and FS::cust_main::bill
644 sub _cust_tax_exempt_pkg {
647 $self->{Hash}->{_cust_tax_exempt_pkg} or
648 $self->{Hash}->{_cust_tax_exempt_pkg} = [];
657 setup and recur shouldn't be separate fields. There should be one "amount"
658 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
660 A line item with both should really be two separate records (preserving
661 sdate and edate for setup fees for recurring packages - that information may
662 be valuable later). Invoice generation (cust_main::bill), invoice printing
663 (cust_bill), tax reports (report_tax.cgi) and line item reports
664 (cust_bill_pkg.cgi) would need to be updated.
666 owed_setup and owed_recur could then be repaced by just owed, and
667 cust_bill::open_cust_bill_pkg and
668 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
672 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
673 from the base documentation.