2 use base qw( FS::cust_bill::Search FS::Template_Mixin
3 FS::cust_main_Mixin FS::Record
7 use vars qw( $DEBUG $me );
9 use Fcntl qw(:flock); #for spool_csv
11 use List::Util qw(min max sum);
15 use Storable qw( freeze thaw );
17 use FS::UID qw( datasrc );
18 use FS::Misc qw( send_fax do_print );
19 use FS::Record qw( qsearch qsearchs dbh );
21 use FS::cust_statement;
22 use FS::cust_bill_pkg;
23 use FS::cust_bill_pkg_display;
24 use FS::cust_bill_pkg_detail;
28 use FS::cust_credit_bill;
30 use FS::cust_pay_batch;
31 use FS::cust_bill_event;
34 use FS::cust_bill_pay;
35 use FS::cust_bill_pay_batch;
36 use FS::part_bill_event;
39 use FS::cust_bill_batch;
40 use FS::cust_bill_pay_pkg;
41 use FS::cust_credit_bill_pkg;
42 use FS::discount_plan;
43 use FS::cust_bill_void;
47 $me = '[FS::cust_bill]';
51 FS::cust_bill - Object methods for cust_bill records
57 $record = new FS::cust_bill \%hash;
58 $record = new FS::cust_bill { 'column' => 'value' };
60 $error = $record->insert;
62 $error = $new_record->replace($old_record);
64 $error = $record->delete;
66 $error = $record->check;
68 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
70 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
72 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
74 @cust_pay_objects = $cust_bill->cust_pay;
76 $tax_amount = $record->tax;
78 @lines = $cust_bill->print_text;
79 @lines = $cust_bill->print_text('time' => $time);
83 An FS::cust_bill object represents an invoice; a declaration that a customer
84 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
85 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
86 following fields are currently supported:
92 =item invnum - primary key (assigned automatically for new invoices)
94 =item custnum - customer (see L<FS::cust_main>)
96 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
97 L<Time::Local> and L<Date::Parse> for conversion functions.
99 =item charged - amount of this invoice
101 =item invoice_terms - optional terms override for this specific invoice
109 =item billing_balance - the customer's balance immediately before generating
110 this invoice. DEPRECATED. Use the L<FS::cust_main/balance_date> method
111 to determine the customer's balance at a specific time.
113 =item previous_balance - the customer's balance immediately after generating
114 the invoice before this one. DEPRECATED.
116 =item printed - formerly used to track the number of times an invoice had
117 been printed; no longer used.
125 =item closed - books closed flag, empty or `Y'
127 =item statementnum - invoice aggregation (see L<FS::cust_statement>)
129 =item agent_invid - legacy invoice number
131 =item promised_date - customer promised payment date, for collection
141 Creates a new invoice. To add the invoice to the database, see L<"insert">.
142 Invoices are normally created by calling the bill method of a customer object
143 (see L<FS::cust_main>).
147 sub table { 'cust_bill'; }
149 # should be the ONLY occurrence of "Invoice" in invoice rendering code.
150 # (except email_subject and invnum_date_pretty)
153 $self->conf->config('notice_name') || 'Invoice'
156 sub cust_linked { $_[0]->cust_main_custnum; }
157 sub cust_unlinked_msg {
159 "WARNING: can't find cust_main.custnum ". $self->custnum.
160 ' (cust_bill.invnum '. $self->invnum. ')';
165 Adds this invoice to the database ("Posts" the invoice). If there is an error,
166 returns the error, otherwise returns false.
172 warn "$me insert called\n" if $DEBUG;
174 local $SIG{HUP} = 'IGNORE';
175 local $SIG{INT} = 'IGNORE';
176 local $SIG{QUIT} = 'IGNORE';
177 local $SIG{TERM} = 'IGNORE';
178 local $SIG{TSTP} = 'IGNORE';
179 local $SIG{PIPE} = 'IGNORE';
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
185 my $error = $self->SUPER::insert;
187 $dbh->rollback if $oldAutoCommit;
191 if ( $self->get('cust_bill_pkg') ) {
192 foreach my $cust_bill_pkg ( @{$self->get('cust_bill_pkg')} ) {
193 $cust_bill_pkg->invnum($self->invnum);
194 my $error = $cust_bill_pkg->insert;
196 $dbh->rollback if $oldAutoCommit;
197 return "can't create invoice line item: $error";
202 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
209 Voids this invoice: deletes the invoice and adds a record of the voided invoice
210 to the FS::cust_bill_void table (and related tables starting from
211 FS::cust_bill_pkg_void).
217 my $reason = scalar(@_) ? shift : '';
218 my $reprocess_cdrs = scalar(@_) ? shift : '';
220 local $SIG{HUP} = 'IGNORE';
221 local $SIG{INT} = 'IGNORE';
222 local $SIG{QUIT} = 'IGNORE';
223 local $SIG{TERM} = 'IGNORE';
224 local $SIG{TSTP} = 'IGNORE';
225 local $SIG{PIPE} = 'IGNORE';
227 my $oldAutoCommit = $FS::UID::AutoCommit;
228 local $FS::UID::AutoCommit = 0;
231 my $cust_bill_void = new FS::cust_bill_void ( {
232 map { $_ => $self->get($_) } $self->fields
234 $cust_bill_void->reason($reason);
235 my $error = $cust_bill_void->insert;
237 $dbh->rollback if $oldAutoCommit;
241 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
242 my $error = $cust_bill_pkg->void($reason, $reprocess_cdrs);
244 $dbh->rollback if $oldAutoCommit;
249 $error = $self->delete;
251 $dbh->rollback if $oldAutoCommit;
255 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
263 This method now works but you probably shouldn't use it. Instead, apply a
264 credit against the invoice, or use the new void method.
266 Using this method to delete invoices outright is really, really bad. There
267 would be no record you ever posted this invoice, and there are no check to
268 make sure charged = 0 or that there are no associated cust_bill_pkg records.
270 Really, don't use it.
276 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
278 local $SIG{HUP} = 'IGNORE';
279 local $SIG{INT} = 'IGNORE';
280 local $SIG{QUIT} = 'IGNORE';
281 local $SIG{TERM} = 'IGNORE';
282 local $SIG{TSTP} = 'IGNORE';
283 local $SIG{PIPE} = 'IGNORE';
285 my $oldAutoCommit = $FS::UID::AutoCommit;
286 local $FS::UID::AutoCommit = 0;
289 foreach my $table (qw(
300 foreach my $linked ( $self->$table() ) {
301 my $error = $linked->delete;
303 $dbh->rollback if $oldAutoCommit;
310 my $error = $self->SUPER::delete(@_);
312 $dbh->rollback if $oldAutoCommit;
316 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
322 =item replace [ OLD_RECORD ]
324 You can, but probably shouldn't modify invoices...
326 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
327 supplied, replaces this record. If there is an error, returns the error,
328 otherwise returns false.
332 #replace can be inherited from Record.pm
334 # replace_check is now the preferred way to #implement replace data checks
335 # (so $object->replace() works without an argument)
338 my( $new, $old ) = ( shift, shift );
339 return "Can't modify closed invoice" if $old->closed =~ /^Y/i;
340 #return "Can't change _date!" unless $old->_date eq $new->_date;
341 return "Can't change _date" unless $old->_date == $new->_date;
342 return "Can't change charged" unless $old->charged == $new->charged
343 || $old->charged == 0
344 || $new->{'Hash'}{'cc_surcharge_replace_hack'};
350 =item add_cc_surcharge
356 sub add_cc_surcharge {
357 my ($self, $pkgnum, $amount) = (shift, shift, shift);
360 my $cust_bill_pkg = new FS::cust_bill_pkg({
361 'invnum' => $self->invnum,
365 $error = $cust_bill_pkg->insert;
366 return $error if $error;
368 $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
369 $self->charged($self->charged+$amount);
370 $error = $self->replace;
371 return $error if $error;
373 $self->apply_payments_and_credits;
379 Checks all fields to make sure this is a valid invoice. If there is an error,
380 returns the error, otherwise returns false. Called by the insert and replace
389 $self->ut_numbern('invnum')
390 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
391 || $self->ut_numbern('_date')
392 || $self->ut_money('charged')
393 || $self->ut_numbern('printed')
394 || $self->ut_enum('closed', [ '', 'Y' ])
395 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum' )
396 || $self->ut_numbern('agent_invid') #varchar?
398 return $error if $error;
400 $self->_date(time) unless $self->_date;
402 $self->printed(0) if $self->printed eq '';
409 Returns the displayed invoice number for this invoice: agent_invid if
410 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
416 if ( $self->agent_invid
417 && FS::Conf->new->exists('cust_bill-default_agent_invid') ) {
418 return $self->agent_invid;
420 return $self->invnum;
426 Returns the customer's last invoice before this one.
432 if ( !$self->get('previous_bill') ) {
433 $self->set('previous_bill', qsearchs({
434 'table' => 'cust_bill',
435 'hashref' => { 'custnum' => $self->custnum,
436 '_date' => { op=>'<', value=>$self->_date } },
437 'order_by' => 'ORDER BY _date DESC LIMIT 1',
440 $self->get('previous_bill');
445 Returns a list consisting of the total previous balance for this customer,
446 followed by the previous outstanding invoices (as FS::cust_bill objects also).
452 # simple memoize; we use this a lot
453 if (!$self->get('previous')) {
455 my @cust_bill = sort { $a->_date <=> $b->_date }
456 grep { $_->owed != 0 }
457 qsearch( 'cust_bill', { 'custnum' => $self->custnum,
458 #'_date' => { op=>'<', value=>$self->_date },
459 'invnum' => { op=>'<', value=>$self->invnum },
462 foreach ( @cust_bill ) { $total += $_->owed; }
463 $self->set('previous', [$total, @cust_bill]);
465 return @{ $self->get('previous') };
468 =item enable_previous
470 Whether to show the 'Previous Charges' section when printing this invoice.
471 The negation of the 'disable_previous_balance' config setting.
475 sub enable_previous {
477 my $agentnum = $self->cust_main->agentnum;
478 !$self->conf->exists('disable_previous_balance', $agentnum);
483 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
491 'select' => 'cust_bill_pkg.*, pkg_category.categoryname',
492 'table' => 'cust_bill_pkg',
493 'addl_from' => ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
494 ' LEFT JOIN part_pkg USING ( pkgpart ) '.
495 ' LEFT JOIN pkg_class USING ( classnum ) '.
496 ' LEFT JOIN pkg_category USING ( categorynum ) ',
497 'hashref' => { 'invnum' => $self->invnum },
498 'order_by' => 'ORDER BY billpkgnum',
503 =item cust_bill_pkg_pkgnum PKGNUM
505 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice and
510 sub cust_bill_pkg_pkgnum {
511 my( $self, $pkgnum ) = @_;
513 { 'table' => 'cust_bill_pkg',
514 'hashref' => { 'invnum' => $self->invnum,
517 'order_by' => 'ORDER BY billpkgnum',
524 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
531 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
532 $self->cust_bill_pkg;
534 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
539 Returns true if any of the packages (or their definitions) corresponding to the
540 line items for this invoice have the no_auto flag set.
546 grep { $_->no_auto || $_->part_pkg->no_auto } $self->cust_pkg;
549 =item open_cust_bill_pkg
551 Returns the open line items for this invoice.
553 Note that cust_bill_pkg with both setup and recur fees are returned as two
554 separate line items, each with only one fee.
558 # modeled after cust_main::open_cust_bill
559 sub open_cust_bill_pkg {
562 # grep { $_->owed > 0 } $self->cust_bill_pkg
564 my %other = ( 'recur' => 'setup',
565 'setup' => 'recur', );
567 foreach my $field ( qw( recur setup )) {
568 push @open, map { $_->set( $other{$field}, 0 ); $_; }
569 grep { $_->owed($field) > 0 }
570 $self->cust_bill_pkg;
576 =item cust_bill_event
578 Returns the completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
582 sub cust_bill_event {
584 qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
587 =item num_cust_bill_event
589 Returns the number of completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
593 sub num_cust_bill_event {
596 "SELECT COUNT(*) FROM cust_bill_event WHERE invnum = ?";
597 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
598 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
599 $sth->fetchrow_arrayref->[0];
604 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
608 #false laziness w/cust_pkg.pm
612 'table' => 'cust_event',
613 'addl_from' => 'JOIN part_event USING ( eventpart )',
614 'hashref' => { 'tablenum' => $self->invnum },
615 'extra_sql' => " AND eventtable = 'cust_bill' ",
621 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
625 #false laziness w/cust_pkg.pm
629 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
630 " WHERE tablenum = ? AND eventtable = 'cust_bill'";
631 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
632 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
633 $sth->fetchrow_arrayref->[0];
638 Returns the customer (see L<FS::cust_main>) for this invoice.
644 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
647 =item cust_suspend_if_balance_over AMOUNT
649 Suspends the customer associated with this invoice if the total amount owed on
650 this invoice and all older invoices is greater than the specified amount.
652 Returns a list: an empty list on success or a list of errors.
656 sub cust_suspend_if_balance_over {
657 my( $self, $amount ) = ( shift, shift );
658 my $cust_main = $self->cust_main;
659 if ( $cust_main->total_owed_date($self->_date) < $amount ) {
662 $cust_main->suspend(@_);
668 Depreciated. See the cust_credited method.
670 #Returns a list consisting of the total previous credited (see
671 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
672 #outstanding credits (FS::cust_credit objects).
678 croak "FS::cust_bill->cust_credit depreciated; see ".
679 "FS::cust_bill->cust_credit_bill";
682 #my @cust_credit = sort { $a->_date <=> $b->_date }
683 # grep { $_->credited != 0 && $_->_date < $self->_date }
684 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
686 #foreach (@cust_credit) { $total += $_->credited; }
687 #$total, @cust_credit;
692 Depreciated. See the cust_bill_pay method.
694 #Returns all payments (see L<FS::cust_pay>) for this invoice.
700 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
702 #sort { $a->_date <=> $b->_date }
703 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
709 qsearch('cust_pay_batch', { 'invnum' => $self->invnum } );
712 sub cust_bill_pay_batch {
714 qsearch('cust_bill_pay_batch', { 'invnum' => $self->invnum } );
719 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
725 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
726 sort { $a->_date <=> $b->_date }
727 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
732 =item cust_credit_bill
734 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
740 map { $_ } #return $self->num_cust_credit_bill unless wantarray;
741 sort { $a->_date <=> $b->_date }
742 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
746 sub cust_credit_bill {
747 shift->cust_credited(@_);
750 #=item cust_bill_pay_pkgnum PKGNUM
752 #Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
753 #with matching pkgnum.
757 #sub cust_bill_pay_pkgnum {
758 # my( $self, $pkgnum ) = @_;
759 # map { $_ } #return $self->num_cust_bill_pay_pkgnum($pkgnum) unless wantarray;
760 # sort { $a->_date <=> $b->_date }
761 # qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum,
762 # 'pkgnum' => $pkgnum,
767 =item cust_bill_pay_pkg PKGNUM
769 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
770 applied against the matching pkgnum.
774 sub cust_bill_pay_pkg {
775 my( $self, $pkgnum ) = @_;
778 'select' => 'cust_bill_pay_pkg.*',
779 'table' => 'cust_bill_pay_pkg',
780 'addl_from' => ' LEFT JOIN cust_bill_pay USING ( billpaynum ) '.
781 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
782 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
783 " AND cust_bill_pkg.pkgnum = $pkgnum",
788 #=item cust_credited_pkgnum PKGNUM
790 #=item cust_credit_bill_pkgnum PKGNUM
792 #Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice
793 #with matching pkgnum.
797 #sub cust_credited_pkgnum {
798 # my( $self, $pkgnum ) = @_;
799 # map { $_ } #return $self->num_cust_credit_bill_pkgnum($pkgnum) unless wantarray;
800 # sort { $a->_date <=> $b->_date }
801 # qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum,
802 # 'pkgnum' => $pkgnum,
807 #sub cust_credit_bill_pkgnum {
808 # shift->cust_credited_pkgnum(@_);
811 =item cust_credit_bill_pkg PKGNUM
813 Returns all credit applications (see L<FS::cust_credit_bill>) for this invoice
814 applied against the matching pkgnum.
818 sub cust_credit_bill_pkg {
819 my( $self, $pkgnum ) = @_;
822 'select' => 'cust_credit_bill_pkg.*',
823 'table' => 'cust_credit_bill_pkg',
824 'addl_from' => ' LEFT JOIN cust_credit_bill USING ( creditbillnum ) '.
825 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
826 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
827 " AND cust_bill_pkg.pkgnum = $pkgnum",
832 =item cust_bill_batch
834 Returns all invoice batch records (L<FS::cust_bill_batch>) for this invoice.
838 sub cust_bill_batch {
840 qsearch('cust_bill_batch', { 'invnum' => $self->invnum });
845 Returns all discount plans (L<FS::discount_plan>) for this invoice, as a
846 hash keyed by term length.
852 FS::discount_plan->all($self);
857 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
864 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
866 foreach (@taxlines) { $total += $_->setup; }
872 Returns the amount owed (still outstanding) on this invoice, which is charged
873 minus all payment applications (see L<FS::cust_bill_pay>) and credit
874 applications (see L<FS::cust_credit_bill>).
880 my $balance = $self->charged;
881 $balance -= $_->amount foreach ( $self->cust_bill_pay );
882 $balance -= $_->amount foreach ( $self->cust_credited );
883 $balance = sprintf( "%.2f", $balance);
884 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
889 my( $self, $pkgnum ) = @_;
891 #my $balance = $self->charged;
893 $balance += $_->setup + $_->recur for $self->cust_bill_pkg_pkgnum($pkgnum);
895 $balance -= $_->amount for $self->cust_bill_pay_pkg($pkgnum);
896 $balance -= $_->amount for $self->cust_credit_bill_pkg($pkgnum);
898 $balance = sprintf( "%.2f", $balance);
899 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
905 Returns true if this invoice should be hidden. See the
906 selfservice-hide_invoices-taxclass configuraiton setting.
912 my $conf = $self->conf;
913 my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass')
915 my @cust_bill_pkg = $self->cust_bill_pkg;
916 my @part_pkg = grep $_, map $_->part_pkg, @cust_bill_pkg;
917 ! grep { $_->taxclass ne $hide_taxclass } @part_pkg;
920 =item apply_payments_and_credits [ OPTION => VALUE ... ]
922 Applies unapplied payments and credits to this invoice.
923 Payments with the no_auto_apply flag set will not be applied.
925 A hash of optional arguments may be passed. Currently "manual" is supported.
926 If true, a payment receipt is sent instead of a statement when
927 'payment_receipt_email' configuration option is set.
929 If there is an error, returns the error, otherwise returns false.
933 sub apply_payments_and_credits {
934 my( $self, %options ) = @_;
935 my $conf = $self->conf;
937 local $SIG{HUP} = 'IGNORE';
938 local $SIG{INT} = 'IGNORE';
939 local $SIG{QUIT} = 'IGNORE';
940 local $SIG{TERM} = 'IGNORE';
941 local $SIG{TSTP} = 'IGNORE';
942 local $SIG{PIPE} = 'IGNORE';
944 my $oldAutoCommit = $FS::UID::AutoCommit;
945 local $FS::UID::AutoCommit = 0;
948 $self->select_for_update; #mutex
950 my @payments = grep { $_->unapplied > 0 }
951 grep { !$_->no_auto_apply }
952 $self->cust_main->cust_pay;
953 my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit;
955 if ( $conf->exists('pkg-balances') ) {
956 # limit @payments & @credits to those w/ a pkgnum grepped from $self
957 my %pkgnums = map { $_ => 1 } map $_->pkgnum, $self->cust_bill_pkg;
958 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
959 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
962 while ( $self->owed > 0 and ( @payments || @credits ) ) {
965 if ( @payments && @credits ) {
967 #decide which goes first by weight of top (unapplied) line item
969 my @open_lineitems = $self->open_cust_bill_pkg;
972 max( map { $_->part_pkg->pay_weight || 0 }
977 my $max_credit_weight =
978 max( map { $_->part_pkg->credit_weight || 0 }
984 #if both are the same... payments first? it has to be something
985 if ( $max_pay_weight >= $max_credit_weight ) {
991 } elsif ( @payments ) {
993 } elsif ( @credits ) {
996 die "guru meditation #12 and 35";
1000 if ( $app eq 'pay' ) {
1002 my $payment = shift @payments;
1003 $unapp_amount = $payment->unapplied;
1004 $app = new FS::cust_bill_pay { 'paynum' => $payment->paynum };
1005 $app->pkgnum( $payment->pkgnum )
1006 if $conf->exists('pkg-balances') && $payment->pkgnum;
1008 } elsif ( $app eq 'credit' ) {
1010 my $credit = shift @credits;
1011 $unapp_amount = $credit->credited;
1012 $app = new FS::cust_credit_bill { 'crednum' => $credit->crednum };
1013 $app->pkgnum( $credit->pkgnum )
1014 if $conf->exists('pkg-balances') && $credit->pkgnum;
1017 die "guru meditation #12 and 35";
1021 if ( $conf->exists('pkg-balances') && $app->pkgnum ) {
1022 warn "owed_pkgnum ". $app->pkgnum;
1023 $owed = $self->owed_pkgnum($app->pkgnum);
1025 $owed = $self->owed;
1027 next unless $owed > 0;
1029 warn "min ( $unapp_amount, $owed )\n" if $DEBUG;
1030 $app->amount( sprintf('%.2f', min( $unapp_amount, $owed ) ) );
1032 $app->invnum( $self->invnum );
1034 my $error = $app->insert(%options);
1036 $dbh->rollback if $oldAutoCommit;
1037 return "Error inserting ". $app->table. " record: $error";
1039 die $error if $error;
1043 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1050 Sends this invoice to the destinations configured for this customer: sends
1051 email, prints and/or faxes. See L<FS::cust_main_invoice>.
1053 Options can be passed as a hashref. Positional parameters are no longer
1056 I<template>: a suffix for alternate invoices
1058 I<agentnum>: obsolete, now does nothing.
1060 I<from> overrides the default email invoice From: address.
1062 I<amount>: obsolete, does nothing
1064 I<notice_name> overrides "Invoice" as the name of the sent document
1065 (templates from 10/2009 or newer required).
1067 I<lpr> overrides the system 'lpr' option as the command to print a document
1068 from standard input.
1074 my $opt = ref($_[0]) ? $_[0] : +{ @_ };
1075 my $conf = $self->conf;
1077 my $cust_main = $self->cust_main;
1079 my @invoicing_list = $cust_main->invoicing_list;
1082 if ( grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list )
1083 && ! $cust_main->invoice_noemail;
1086 if grep { $_ eq 'POST' } @invoicing_list; #postal
1088 #this has never been used post-$ORIGINAL_ISP afaik
1089 $self->fax_invoice($opt)
1090 if grep { $_ eq 'FAX' } @invoicing_list; #fax
1098 my $opt = shift || {};
1099 if ($opt and !ref($opt)) {
1100 die ref($self). '->email called with positional parameters';
1103 my $conf = $self->conf;
1105 my $from = delete $opt->{from};
1107 # this is where we set the From: address
1108 $from ||= $self->_agent_invoice_from || #XXX should go away
1109 $conf->invoice_from_full( $self->cust_main->agentnum );
1111 my @invoicing_list = $self->cust_main->invoicing_list_emailonly;
1113 if ( ! @invoicing_list ) { #no recipients
1114 if ( $conf->exists('cust_bill-no_recipients-error') ) {
1115 die 'No recipients for customer #'. $self->custnum;
1117 #default: better to notify this person than silence
1118 @invoicing_list = ($from);
1122 $self->SUPER::email( {
1124 'to' => \@invoicing_list,
1130 #this stays here for now because its explicitly used as
1131 # FS::cust_bill::queueable_email
1132 sub queueable_email {
1135 my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1136 or die "invalid invoice number: " . $opt{invnum};
1138 $self->set('mode', $opt{mode})
1141 my %args = map {$_ => $opt{$_}}
1143 qw( from notice_name no_coupon template );
1145 my $error = $self->email( \%args );
1146 die $error if $error;
1152 my $conf = $self->conf;
1154 #my $template = scalar(@_) ? shift : '';
1157 my $subject = $conf->config('invoice_subject', $self->cust_main->agentnum)
1160 my $cust_main = $self->cust_main;
1161 my $name = $cust_main->name;
1162 my $name_short = $cust_main->name_short;
1163 my $invoice_number = $self->invnum;
1164 my $invoice_date = $self->_date_pretty;
1166 eval qq("$subject");
1171 'Invoice-'. $self->invnum. '.pdf';
1174 =item lpr_data HASHREF
1176 Returns the postscript or plaintext for this invoice as an arrayref.
1178 Options must be passed as a hashref. Positional parameters are no longer
1181 I<template>, if specified, is the name of a suffix for alternate invoices.
1183 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1189 my $conf = $self->conf;
1190 my $opt = shift || {};
1191 if ($opt and !ref($opt)) {
1192 # nobody does this anyway
1193 die "FS::cust_bill::lpr_data called with positional parameters";
1196 my $method = $conf->exists('invoice_latex') ? 'print_ps' : 'print_text';
1197 [ $self->$method( $opt ) ];
1202 Prints this invoice.
1204 Options must be passed as a hashref.
1206 I<template>, if specified, is the name of a suffix for alternate invoices.
1208 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1214 return if $self->hide;
1215 my $conf = $self->conf;
1216 my $opt = shift || {};
1217 if ($opt and !ref($opt)) {
1218 die "FS::cust_bill::print called with positional parameters";
1221 my $lpr = delete $opt->{lpr};
1222 if($conf->exists('invoice_print_pdf')) {
1223 # Add the invoice to the current batch.
1224 $self->batch_invoice($opt);
1228 $self->lpr_data($opt),
1229 'agentnum' => $self->cust_main->agentnum,
1235 =item fax_invoice HASHREF
1239 Options must be passed as a hashref.
1241 I<template>, if specified, is the name of a suffix for alternate invoices.
1243 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1249 return if $self->hide;
1250 my $conf = $self->conf;
1251 my $opt = shift || {};
1252 if ($opt and !ref($opt)) {
1253 die "FS::cust_bill::fax_invoice called with positional parameters";
1256 die 'FAX invoice destination not (yet?) supported with plain text invoices.'
1257 unless $conf->exists('invoice_latex');
1259 my $dialstring = $self->cust_main->getfield('fax');
1262 my $error = send_fax( 'docdata' => $self->lpr_data($opt),
1263 'dialstring' => $dialstring,
1265 die $error if $error;
1269 =item batch_invoice [ HASHREF ]
1271 Place this invoice into the open batch (see C<FS::bill_batch>). If there
1272 isn't an open batch, one will be created.
1274 HASHREF may contain any options to be passed to C<print_pdf>.
1279 my ($self, $opt) = @_;
1280 my $bill_batch = $self->get_open_bill_batch;
1281 my $cust_bill_batch = FS::cust_bill_batch->new({
1282 batchnum => $bill_batch->batchnum,
1283 invnum => $self->invnum,
1285 if ( $self->mode ) {
1286 $opt->{mode} ||= $self->mode;
1287 $opt->{mode} = $opt->{mode}->modenum if ref $opt->{mode};
1289 return $cust_bill_batch->insert($opt);
1292 =item get_open_batch
1294 Returns the currently open batch as an FS::bill_batch object, creating a new
1295 one if necessary. (A per-agent batch if invoice_print_pdf-spoolagent is
1300 sub get_open_bill_batch {
1302 my $conf = $self->conf;
1303 my $hashref = { status => 'O' };
1304 $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent')
1305 ? $self->cust_main->agentnum
1307 my $batch = qsearchs('bill_batch', $hashref);
1308 return $batch if $batch;
1309 $batch = FS::bill_batch->new($hashref);
1310 my $error = $batch->insert;
1311 die $error if $error;
1315 =item ftp_invoice [ TEMPLATENAME ]
1317 Sends this invoice data via FTP.
1319 TEMPLATENAME is unused?
1325 my $conf = $self->conf;
1326 my $template = scalar(@_) ? shift : '';
1329 'protocol' => 'ftp',
1330 'server' => $conf->config('cust_bill-ftpserver'),
1331 'username' => $conf->config('cust_bill-ftpusername'),
1332 'password' => $conf->config('cust_bill-ftppassword'),
1333 'dir' => $conf->config('cust_bill-ftpdir'),
1334 'format' => $conf->config('cust_bill-ftpformat'),
1338 =item spool_invoice [ TEMPLATENAME ]
1340 Spools this invoice data (see L<FS::spool_csv>)
1342 TEMPLATENAME is unused?
1348 my $conf = $self->conf;
1349 my $template = scalar(@_) ? shift : '';
1352 'format' => $conf->config('cust_bill-spoolformat'),
1353 'agent_spools' => $conf->exists('cust_bill-spoolagent'),
1357 =item send_csv OPTION => VALUE, ...
1359 Sends invoice as a CSV data-file to a remote host with the specified protocol.
1363 protocol - currently only "ftp"
1369 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
1370 and YYMMDDHHMMSS is a timestamp.
1372 See L</print_csv> for a description of the output format.
1377 my($self, %opt) = @_;
1381 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1382 mkdir $spooldir, 0700 unless -d $spooldir;
1384 # don't localize dates here, they're a defined format
1385 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1386 my $file = "$spooldir/$tracctnum.csv";
1388 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1390 open(CSV, ">$file") or die "can't open $file: $!";
1398 if ( $opt{protocol} eq 'ftp' ) {
1399 eval "use Net::FTP;";
1401 $net = Net::FTP->new($opt{server}) or die @$;
1403 die "unknown protocol: $opt{protocol}";
1406 $net->login( $opt{username}, $opt{password} )
1407 or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
1409 $net->binary or die "can't set binary mode";
1411 $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
1413 $net->put($file) or die "can't put $file: $!";
1423 Spools CSV invoice data.
1429 =item format - any of FS::Misc::::Invoicing::spool_formats
1431 =item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the
1432 customer has the corresponding invoice destinations set (see
1433 L<FS::cust_main_invoice>).
1435 =item agent_spools - if set to a true value, will spool to per-agent files
1436 rather than a single global file
1438 =item upload_targetnum - if set to a target (see L<FS::upload_target>), will
1439 append to that spool. L<FS::Cron::upload> will then send the spool file to
1442 =item balanceover - if set, only spools the invoice if the total amount owed on
1443 this invoice and all older invoices is greater than the specified amount.
1445 =item time - the "current time". Controls the printing of past due messages
1453 my($self, %opt) = @_;
1455 my $time = $opt{'time'} || time;
1456 my $cust_main = $self->cust_main;
1458 if ( $opt{'dest'} ) {
1459 my %invoicing_list = map { /^(POST|FAX)$/ or 'EMAIL' =~ /^(.*)$/; $1 => 1 }
1460 $cust_main->invoicing_list;
1461 return 'N/A' unless $invoicing_list{$opt{'dest'}}
1462 || ! keys %invoicing_list;
1465 if ( $opt{'balanceover'} ) {
1467 if $cust_main->total_owed_date($self->_date) < $opt{'balanceover'};
1470 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1471 mkdir $spooldir, 0700 unless -d $spooldir;
1473 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', $time);
1476 if ( $opt{'agent_spools'} ) {
1477 $file = 'agentnum'.$cust_main->agentnum;
1482 if ( $opt{'upload_targetnum'} ) {
1483 $spooldir .= '/target'.$opt{'upload_targetnum'};
1484 mkdir $spooldir, 0700 unless -d $spooldir;
1485 } # otherwise it just goes into export.xxx/cust_bill
1487 if ( lc($opt{'format'}) eq 'billco' ) {
1491 $file = "$spooldir/$file.csv";
1493 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum);
1495 open(CSV, ">>$file") or die "can't open $file: $!";
1496 flock(CSV, LOCK_EX);
1501 if ( lc($opt{'format'}) eq 'billco' ) {
1503 flock(CSV, LOCK_UN);
1506 $file =~ s/-header.csv$/-detail.csv/;
1508 open(CSV,">>$file") or die "can't open $file: $!";
1509 flock(CSV, LOCK_EX);
1513 print CSV $detail if defined($detail);
1515 flock(CSV, LOCK_UN);
1522 =item print_csv OPTION => VALUE, ...
1524 Returns CSV data for this invoice.
1528 format - 'default', 'billco', 'oneline', 'bridgestone'
1530 Returns a list consisting of two scalars. The first is a single line of CSV
1531 header information for this invoice. The second is one or more lines of CSV
1532 detail information for this invoice.
1534 If I<format> is not specified or "default", the fields of the CSV file are as
1537 record_type, invnum, custnum, _date, charged, first, last, company, address1,
1538 address2, city, state, zip, country, pkg, setup, recur, sdate, edate
1542 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
1544 B<record_type> is C<cust_bill> for the initial header line only. The
1545 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
1546 fields are filled in.
1548 B<record_type> is C<cust_bill_pkg> for detail lines. Only the first two fields
1549 (B<record_type> and B<invnum>) and the last five fields (B<pkg> through B<edate>)
1552 =item invnum - invoice number
1554 =item custnum - customer number
1556 =item _date - invoice date
1558 =item charged - total invoice amount
1560 =item first - customer first name
1562 =item last - customer first name
1564 =item company - company name
1566 =item address1 - address line 1
1568 =item address2 - address line 1
1578 =item pkg - line item description
1580 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
1582 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
1584 =item sdate - start date for recurring fee
1586 =item edate - end date for recurring fee
1590 If I<format> is "billco", the fields of the header CSV file are as follows:
1592 +-------------------------------------------------------------------+
1593 | FORMAT HEADER FILE |
1594 |-------------------------------------------------------------------|
1595 | Field | Description | Name | Type | Width |
1596 | 1 | N/A-Leave Empty | RC | CHAR | 2 |
1597 | 2 | N/A-Leave Empty | CUSTID | CHAR | 15 |
1598 | 3 | Transaction Account No | TRACCTNUM | CHAR | 15 |
1599 | 4 | Transaction Invoice No | TRINVOICE | CHAR | 15 |
1600 | 5 | Transaction Zip Code | TRZIP | CHAR | 5 |
1601 | 6 | Transaction Company Bill To | TRCOMPANY | CHAR | 30 |
1602 | 7 | Transaction Contact Bill To | TRNAME | CHAR | 30 |
1603 | 8 | Additional Address Unit Info | TRADDR1 | CHAR | 30 |
1604 | 9 | Bill To Street Address | TRADDR2 | CHAR | 30 |
1605 | 10 | Ancillary Billing Information | TRADDR3 | CHAR | 30 |
1606 | 11 | Transaction City Bill To | TRCITY | CHAR | 20 |
1607 | 12 | Transaction State Bill To | TRSTATE | CHAR | 2 |
1608 | 13 | Bill Cycle Close Date | CLOSEDATE | CHAR | 10 |
1609 | 14 | Bill Due Date | DUEDATE | CHAR | 10 |
1610 | 15 | Previous Balance | BALFWD | NUM* | 9 |
1611 | 16 | Pmt/CR Applied | CREDAPPLY | NUM* | 9 |
1612 | 17 | Total Current Charges | CURRENTCHG | NUM* | 9 |
1613 | 18 | Total Amt Due | TOTALDUE | NUM* | 9 |
1614 | 19 | Total Amt Due | AMTDUE | NUM* | 9 |
1615 | 20 | 30 Day Aging | AMT30 | NUM* | 9 |
1616 | 21 | 60 Day Aging | AMT60 | NUM* | 9 |
1617 | 22 | 90 Day Aging | AMT90 | NUM* | 9 |
1618 | 23 | Y/N | AGESWITCH | CHAR | 1 |
1619 | 24 | Remittance automation | SCANLINE | CHAR | 100 |
1620 | 25 | Total Taxes & Fees | TAXTOT | NUM* | 9 |
1621 | 26 | Customer Reference Number | CUSTREF | CHAR | 15 |
1622 | 27 | Federal Tax*** | FEDTAX | NUM* | 9 |
1623 | 28 | State Tax*** | STATETAX | NUM* | 9 |
1624 | 29 | Other Taxes & Fees*** | OTHERTAX | NUM* | 9 |
1625 +-------+-------------------------------+------------+------+-------+
1627 If I<format> is "billco", the fields of the detail CSV file are as follows:
1629 FORMAT FOR DETAIL FILE
1631 Field | Description | Name | Type | Width
1632 1 | N/A-Leave Empty | RC | CHAR | 2
1633 2 | N/A-Leave Empty | CUSTID | CHAR | 15
1634 3 | Account Number | TRACCTNUM | CHAR | 15
1635 4 | Invoice Number | TRINVOICE | CHAR | 15
1636 5 | Line Sequence (sort order) | LINESEQ | NUM | 6
1637 6 | Transaction Detail | DETAILS | CHAR | 100
1638 7 | Amount | AMT | NUM* | 9
1639 8 | Line Format Control** | LNCTRL | CHAR | 2
1640 9 | Grouping Code | GROUP | CHAR | 2
1641 10 | User Defined | ACCT CODE | CHAR | 15
1643 If format is 'oneline', there is no detail file. Each invoice has a
1644 header line only, with the fields:
1646 Agent number, agent name, customer number, first name, last name, address
1647 line 1, address line 2, city, state, zip, invoice date, invoice number,
1648 amount charged, amount due, previous balance, due date.
1650 and then, for each line item, three columns containing the package number,
1651 description, and amount.
1653 If format is 'bridgestone', there is no detail file. Each invoice has a
1654 header line with the following fields in a fixed-width format:
1656 Customer number (in display format), date, name (first last), company,
1657 address 1, address 2, city, state, zip.
1659 This is a mailing list format, and has no per-invoice fields. To avoid
1660 sending redundant notices, the spooling event should have a "once" or
1661 "once_percust_every" condition.
1666 my($self, %opt) = @_;
1668 eval "use Text::CSV_XS";
1671 my $cust_main = $self->cust_main;
1673 my $csv = Text::CSV_XS->new({'always_quote'=>1});
1674 my $format = lc($opt{'format'});
1676 my $time = $opt{'time'} || time;
1678 my $tracctnum = ''; #leaking out from billco-specific sections :/
1679 if ( $format eq 'billco' ) {
1682 $self->conf->config('billco-account_num', $cust_main->agentnum);
1684 $tracctnum = $account_num eq 'display_custnum'
1685 ? $cust_main->display_custnum
1686 : $opt{'tracctnum'};
1689 $taxtotal += $_->{'amount'} foreach $self->_items_tax;
1691 my $duedate = $self->due_date2str('%m/%d/%Y'); # hardcoded, NOT date_format
1693 my( $previous_balance, @unused ) = $self->previous; #previous balance
1695 my $pmt_cr_applied = 0;
1696 $pmt_cr_applied += $_->{'amount'}
1697 foreach ( $self->_items_payments(%opt), $self->_items_credits(%opt) ) ;
1699 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1702 '', # 1 | N/A-Leave Empty CHAR 2
1703 '', # 2 | N/A-Leave Empty CHAR 15
1704 $tracctnum, # 3 | Transaction Account No CHAR 15
1705 $self->invnum, # 4 | Transaction Invoice No CHAR 15
1706 $cust_main->zip, # 5 | Transaction Zip Code CHAR 5
1707 $cust_main->company, # 6 | Transaction Company Bill To CHAR 30
1708 #$cust_main->payname, # 7 | Transaction Contact Bill To CHAR 30
1709 $cust_main->contact, # 7 | Transaction Contact Bill To CHAR 30
1710 $cust_main->address2, # 8 | Additional Address Unit Info CHAR 30
1711 $cust_main->address1, # 9 | Bill To Street Address CHAR 30
1712 '', # 10 | Ancillary Billing Information CHAR 30
1713 $cust_main->city, # 11 | Transaction City Bill To CHAR 20
1714 $cust_main->state, # 12 | Transaction State Bill To CHAR 2
1717 time2str("%m/%d/%Y", $self->_date), # 13 | Bill Cycle Close Date CHAR 10
1720 $duedate, # 14 | Bill Due Date CHAR 10
1722 $previous_balance, # 15 | Previous Balance NUM* 9
1723 $pmt_cr_applied, # 16 | Pmt/CR Applied NUM* 9
1724 sprintf("%.2f", $self->charged), # 17 | Total Current Charges NUM* 9
1725 $totaldue, # 18 | Total Amt Due NUM* 9
1726 $totaldue, # 19 | Total Amt Due NUM* 9
1727 '', # 20 | 30 Day Aging NUM* 9
1728 '', # 21 | 60 Day Aging NUM* 9
1729 '', # 22 | 90 Day Aging NUM* 9
1730 'N', # 23 | Y/N CHAR 1
1731 '', # 24 | Remittance automation CHAR 100
1732 $taxtotal, # 25 | Total Taxes & Fees NUM* 9
1733 $self->custnum, # 26 | Customer Reference Number CHAR 15
1734 '0', # 27 | Federal Tax*** NUM* 9
1735 sprintf("%.2f", $taxtotal), # 28 | State Tax*** NUM* 9
1736 '0', # 29 | Other Taxes & Fees*** NUM* 9
1739 } elsif ( $format eq 'oneline' ) { #name
1741 my ($previous_balance) = $self->previous;
1742 $previous_balance = sprintf('%.2f', $previous_balance);
1743 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1749 $self->_items_pkg, #_items_nontax? no sections or anything
1754 $cust_main->agentnum,
1755 $cust_main->agent->agent,
1759 $cust_main->company,
1760 $cust_main->address1,
1761 $cust_main->address2,
1767 time2str("%x", $self->_date),
1772 $self->due_date2str("%x"),
1777 } elsif ( $format eq 'bridgestone' ) {
1779 # bypass the CSV stuff and just return this
1780 my $longdate = time2str('%B %d, %Y', $time); #current time, right?
1781 my $zip = $cust_main->zip;
1783 my $prefix = $self->conf->config('bridgestone-prefix', $cust_main->agentnum)
1787 "%-5s%-15s%-20s%-30s%-30s%-30s%-30s%-20s%-2s%-9s\n",
1789 $cust_main->display_custnum,
1791 uc(substr($cust_main->contact_firstlast,0,30)),
1792 uc(substr($cust_main->company ,0,30)),
1793 uc(substr($cust_main->address1 ,0,30)),
1794 uc(substr($cust_main->address2 ,0,30)),
1795 uc(substr($cust_main->city ,0,20)),
1796 uc($cust_main->state),
1802 } elsif ( $format eq 'ics' ) {
1804 my $bill = $cust_main->bill_location;
1805 my $zip = $bill->zip;
1809 if ( $zip =~ /^(\d{5})(\d{4})$/ ) {
1814 # minor false laziness with print_generic
1815 my ($previous_balance) = $self->previous;
1816 my $balance_due = $self->owed + $previous_balance;
1817 my $payment_total = sum(0, map { $_->{'amount'} } $self->_items_payments);
1818 my $credit_total = sum(0, map { $_->{'amount'} } $self->_items_credits);
1821 if ( $self->due_date and $time >= $self->due_date ) {
1822 $past_due = sprintf('Past due:$%0.2f Due Immediately', $balance_due);
1826 my $header = sprintf(
1827 '%-10s%-30s%-48s%-2s%-50s%-30s%-30s%-25s%-2s%-5s%-4s%-8s%-8s%-10s%-10s%-10s%-10s%-10s%-10s%-480s%-35s',
1828 $cust_main->display_custnum, #BID
1829 uc($cust_main->first), #FNAME
1830 uc($cust_main->last), #LNAME
1831 '00', #BATCH, should this ever be anything else?
1832 uc($cust_main->company), #COMP
1833 uc($bill->address1), #STREET1
1834 uc($bill->address2), #STREET2
1835 uc($bill->city), #CITY
1836 uc($bill->state), #STATE
1839 time2str('%Y%m%d', $self->_date), #BILL_DATE
1840 $self->due_date2str('%Y%m%d'), #DUE_DATE,
1841 ( map {sprintf('%0.2f', $_)}
1842 $balance_due, #AMNT_DUE
1843 $previous_balance, #PREV_BAL
1844 $payment_total, #PYMT_RCVD
1845 $credit_total, #CREDITS
1846 $previous_balance, #BEG_BAL--is this correct?
1847 $self->charged, #NEW_CHRG
1850 $past_due, #PAST_MSG
1854 my %svc_class = ('' => ''); # maybe cache this more persistently?
1856 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1858 my $show_pkgnum = $cust_bill_pkg->pkgnum || '';
1859 my $cust_pkg = $cust_bill_pkg->cust_pkg if $show_pkgnum;
1863 my @dates = ( $self->_date, undef );
1864 if ( my $prev = $cust_bill_pkg->previous_cust_bill_pkg ) {
1865 $dates[1] = $prev->sdate; #questionable
1868 # generate an 01 detail for each service
1869 my @svcs = $cust_pkg->h_cust_svc(@dates, 'I');
1870 foreach my $cust_svc ( @svcs ) {
1871 $show_pkgnum = ''; # hide it if we're showing svcnums
1873 my $svcpart = $cust_svc->svcpart;
1874 if (!exists($svc_class{$svcpart})) {
1875 my $classnum = $cust_svc->part_svc->classnum;
1876 my $part_svc_class = FS::part_svc_class->by_key($classnum)
1878 $svc_class{$svcpart} = $part_svc_class ?
1879 $part_svc_class->classname :
1883 my @h_label = $cust_svc->label(@dates, 'I');
1884 push @details, sprintf('01%-9s%-20s%-47s',
1886 $svc_class{$svcpart},
1889 } #foreach $cust_svc
1892 my $desc = $cust_bill_pkg->desc; # itemdesc or part_pkg.pkg
1893 if ($cust_bill_pkg->recur > 0) {
1894 $desc .= ' '.time2str('%d-%b-%Y', $cust_bill_pkg->sdate).' to '.
1895 time2str('%d-%b-%Y', $cust_bill_pkg->edate - 86400);
1897 push @details, sprintf('02%-6s%-60s%-10s',
1900 sprintf('%0.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
1902 } #foreach $cust_bill_pkg
1904 # Tag this row so that we know whether this is one page (1), two pages
1905 # (2), # or "big" (B). The tag will be stripped off before uploading.
1906 if ( scalar(@details) < 12 ) {
1908 } elsif ( scalar(@details) < 58 ) {
1914 return join('', $header, @details, "\n");
1922 time2str("%x", $self->_date),
1923 sprintf("%.2f", $self->charged),
1924 ( map { $cust_main->getfield($_) }
1925 qw( first last company address1 address2 city state zip country ) ),
1927 ) or die "can't create csv";
1930 my $header = $csv->string. "\n";
1933 if ( lc($opt{'format'}) eq 'billco' ) {
1936 my %items_opt = ( format => 'template',
1937 escape_function => sub { shift } );
1938 # I don't know what characters billco actually tolerates in spool entries.
1939 # Text::CSV will take care of delimiters, though.
1941 my @items = ( $self->_items_pkg(%items_opt),
1942 $self->_items_fee(%items_opt) );
1943 foreach my $item (@items) {
1945 my $description = $item->{'description'};
1946 if ( $item->{'_is_discount'} and exists($item->{ext_description}[0]) ) {
1947 $description .= ': ' . $item->{ext_description}[0];
1951 '', # 1 | N/A-Leave Empty CHAR 2
1952 '', # 2 | N/A-Leave Empty CHAR 15
1953 $tracctnum, # 3 | Account Number CHAR 15
1954 $self->invnum, # 4 | Invoice Number CHAR 15
1955 $lineseq++, # 5 | Line Sequence (sort order) NUM 6
1956 $description, # 6 | Transaction Detail CHAR 100
1957 $item->{'amount'}, # 7 | Amount NUM* 9
1958 '', # 8 | Line Format Control** CHAR 2
1959 '', # 9 | Grouping Code CHAR 2
1960 '', # 10 | User Defined CHAR 15
1963 $detail .= $csv->string. "\n";
1967 } elsif ( lc($opt{'format'}) eq 'oneline' ) {
1973 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1975 my($pkg, $setup, $recur, $sdate, $edate);
1976 if ( $cust_bill_pkg->pkgnum ) {
1978 ($pkg, $setup, $recur, $sdate, $edate) = (
1979 $cust_bill_pkg->part_pkg->pkg,
1980 ( $cust_bill_pkg->setup != 0
1981 ? sprintf("%.2f", $cust_bill_pkg->setup )
1983 ( $cust_bill_pkg->recur != 0
1984 ? sprintf("%.2f", $cust_bill_pkg->recur )
1986 ( $cust_bill_pkg->sdate
1987 ? time2str("%x", $cust_bill_pkg->sdate)
1989 ($cust_bill_pkg->edate
1990 ? time2str("%x", $cust_bill_pkg->edate)
1994 } else { #pkgnum tax
1995 next unless $cust_bill_pkg->setup != 0;
1996 $pkg = $cust_bill_pkg->desc;
1997 $setup = sprintf('%10.2f', $cust_bill_pkg->setup );
1998 ( $sdate, $edate ) = ( '', '' );
2004 ( map { '' } (1..11) ),
2005 ($pkg, $setup, $recur, $sdate, $edate)
2006 ) or die "can't create csv";
2008 $detail .= $csv->string. "\n";
2014 ( $header, $detail );
2020 Pays this invoice with a compliemntary payment. If there is an error,
2021 returns the error, otherwise returns false.
2027 my $cust_pay = new FS::cust_pay ( {
2028 'invnum' => $self->invnum,
2029 'paid' => $self->owed,
2032 'payinfo' => $self->cust_main->payinfo,
2040 Attempts to pay this invoice with a credit card payment via a
2041 Business::OnlinePayment realtime gateway. See
2042 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2043 for supported processors.
2049 $self->realtime_bop( 'CC', @_ );
2054 Attempts to pay this invoice with an electronic check (ACH) payment via a
2055 Business::OnlinePayment realtime gateway. See
2056 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2057 for supported processors.
2063 $self->realtime_bop( 'ECHECK', @_ );
2068 Attempts to pay this invoice with phone bill (LEC) payment via a
2069 Business::OnlinePayment realtime gateway. See
2070 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2071 for supported processors.
2077 $self->realtime_bop( 'LEC', @_ );
2081 my( $self, $method ) = (shift,shift);
2082 my $conf = $self->conf;
2085 my $cust_main = $self->cust_main;
2086 my $balance = $cust_main->balance;
2087 my $amount = ( $balance < $self->owed ) ? $balance : $self->owed;
2088 $amount = sprintf("%.2f", $amount);
2089 return "not run (balance $balance)" unless $amount > 0;
2091 my $description = 'Internet Services';
2092 if ( $conf->exists('business-onlinepayment-description') ) {
2093 my $dtempl = $conf->config('business-onlinepayment-description');
2095 my $agent_obj = $cust_main->agent
2096 or die "can't retreive agent for $cust_main (agentnum ".
2097 $cust_main->agentnum. ")";
2098 my $agent = $agent_obj->agent;
2099 my $pkgs = join(', ',
2100 map { $_->part_pkg->pkg }
2101 grep { $_->pkgnum } $self->cust_bill_pkg
2103 $description = eval qq("$dtempl");
2106 $cust_main->realtime_bop($method, $amount,
2107 'description' => $description,
2108 'invnum' => $self->invnum,
2109 #this didn't do what we want, it just calls apply_payments_and_credits
2111 'apply_to_invoice' => 1,
2114 #this changes application behavior: auto payments
2115 #triggered against a specific invoice are now applied
2116 #to that invoice instead of oldest open.
2122 =item batch_card OPTION => VALUE...
2124 Adds a payment for this invoice to the pending credit card batch (see
2125 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2126 runs the payment using a realtime gateway.
2131 my ($self, %options) = @_;
2132 my $cust_main = $self->cust_main;
2134 $options{invnum} = $self->invnum;
2136 $cust_main->batch_card(%options);
2139 sub _agent_template {
2141 $self->cust_main->agent_template;
2144 sub _agent_invoice_from {
2146 $self->cust_main->agent_invoice_from;
2149 =item invoice_barcode DIR_OR_FALSE
2151 Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
2152 it is taken as the temp directory where the PNG file will be generated and the
2153 PNG file name is returned. Otherwise, the PNG image itself is returned.
2157 sub invoice_barcode {
2158 my ($self, $dir) = (shift,shift);
2160 my $gdbar = new GD::Barcode('Code39',$self->invnum);
2161 die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
2162 my $gd = $gdbar->plot(Height => 30);
2165 my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX',
2169 ) or die "can't open temp file: $!\n";
2170 print $bh $gd->png or die "cannot write barcode to file: $!\n";
2171 my $png_file = $bh->filename;
2178 =item invnum_date_pretty
2180 Returns a string with the invoice number and date, for example:
2181 "Invoice #54 (3/20/2008)".
2183 Intended for back-end context, with regard to translation and date formatting.
2187 #note: this uses _date_pretty_unlocalized because _date_pretty is too expensive
2188 # for backend use (and also does the wrong thing, localizing for end customer
2189 # instead of backoffice configured date format)
2190 sub invnum_date_pretty {
2192 #$self->mt('Invoice #').
2193 'Invoice #'. #XXX should be translated ala web UI user (not invoice customer)
2194 $self->invnum. ' ('. $self->_date_pretty_unlocalized. ')';
2197 #sub _items_extra_usage_sections {
2199 # my $escape = shift;
2201 # my %sections = ();
2203 # my %usage_class = map{ $_->classname, $_ } qsearch('usage_class', {});
2204 # foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
2206 # next unless $cust_bill_pkg->pkgnum > 0;
2208 # foreach my $section ( keys %usage_class ) {
2210 # my $usage = $cust_bill_pkg->usage($section);
2212 # next unless $usage && $usage > 0;
2214 # $sections{$section} ||= 0;
2215 # $sections{$section} += $usage;
2221 # map { { 'description' => &{$escape}($_),
2222 # 'subtotal' => $sections{$_},
2223 # 'summarized' => '',
2224 # 'tax_section' => '',
2227 # sort {$usage_class{$a}->weight <=> $usage_class{$b}->weight} keys %sections;
2231 sub _items_extra_usage_sections {
2233 my $conf = $self->conf;
2241 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2243 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2244 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2245 next unless $cust_bill_pkg->pkgnum > 0;
2247 foreach my $classnum ( keys %usage_class ) {
2248 my $section = $usage_class{$classnum}->classname;
2249 $classnums{$section} = $classnum;
2251 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail($classnum) ) {
2252 my $amount = $detail->amount;
2253 next unless $amount && $amount > 0;
2255 $sections{$section} ||= { 'subtotal'=>0, 'calls'=>0, 'duration'=>0 };
2256 $sections{$section}{amount} += $amount; #subtotal
2257 $sections{$section}{calls}++;
2258 $sections{$section}{duration} += $detail->duration;
2260 my $desc = $detail->regionname;
2261 my $description = $desc;
2262 $description = substr($desc, 0, $maxlength). '...'
2263 if $format eq 'latex' && length($desc) > $maxlength;
2265 $lines{$section}{$desc} ||= {
2266 description => &{$escape}($description),
2267 #pkgpart => $part_pkg->pkgpart,
2268 pkgnum => $cust_bill_pkg->pkgnum,
2273 #unit_amount => $cust_bill_pkg->unitrecur,
2274 quantity => $cust_bill_pkg->quantity,
2275 product_code => 'N/A',
2276 ext_description => [],
2279 $lines{$section}{$desc}{amount} += $amount;
2280 $lines{$section}{$desc}{calls}++;
2281 $lines{$section}{$desc}{duration} += $detail->duration;
2287 my %sectionmap = ();
2288 foreach (keys %sections) {
2289 my $usage_class = $usage_class{$classnums{$_}};
2290 $sectionmap{$_} = { 'description' => &{$escape}($_),
2291 'amount' => $sections{$_}{amount}, #subtotal
2292 'calls' => $sections{$_}{calls},
2293 'duration' => $sections{$_}{duration},
2295 'tax_section' => '',
2296 'sort_weight' => $usage_class->weight,
2297 ( $usage_class->format
2298 ? ( map { $_ => $usage_class->$_($format) }
2299 qw( description_generator header_generator total_generator total_line_generator )
2306 my @sections = sort { $a->{sort_weight} <=> $b->{sort_weight} }
2310 foreach my $section ( keys %lines ) {
2311 foreach my $line ( keys %{$lines{$section}} ) {
2312 my $l = $lines{$section}{$line};
2313 $l->{section} = $sectionmap{$section};
2314 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2315 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2320 return(\@sections, \@lines);
2326 my $end = $self->_date;
2328 # start at date of previous invoice + 1 second or 0 if no previous invoice
2329 my $start = $self->scalar_sql("SELECT max(_date) FROM cust_bill WHERE custnum = ? and invnum != ?",$self->custnum,$self->invnum);
2330 $start = 0 if !$start;
2333 my $cust_main = $self->cust_main;
2334 my @pkgs = $cust_main->all_pkgs;
2335 my($num_activated,$num_deactivated,$num_portedin,$num_portedout,$minutes)
2338 foreach my $pkg ( @pkgs ) {
2339 my @h_cust_svc = $pkg->h_cust_svc($end);
2340 foreach my $h_cust_svc ( @h_cust_svc ) {
2341 next if grep {$_ eq $h_cust_svc->svcnum} @seen;
2342 next unless $h_cust_svc->part_svc->svcdb eq 'svc_phone';
2344 my $inserted = $h_cust_svc->date_inserted;
2345 my $deleted = $h_cust_svc->date_deleted;
2346 my $phone_inserted = $h_cust_svc->h_svc_x($inserted+5);
2348 $phone_deleted = $h_cust_svc->h_svc_x($deleted) if $deleted;
2350 # DID either activated or ported in; cannot be both for same DID simultaneously
2351 if ($inserted >= $start && $inserted <= $end && $phone_inserted
2352 && (!$phone_inserted->lnp_status
2353 || $phone_inserted->lnp_status eq ''
2354 || $phone_inserted->lnp_status eq 'native')) {
2357 else { # this one not so clean, should probably move to (h_)svc_phone
2358 my $phone_portedin = qsearchs( 'h_svc_phone',
2359 { 'svcnum' => $h_cust_svc->svcnum,
2360 'lnp_status' => 'portedin' },
2361 FS::h_svc_phone->sql_h_searchs($end),
2363 $num_portedin++ if $phone_portedin;
2366 # DID either deactivated or ported out; cannot be both for same DID simultaneously
2367 if($deleted >= $start && $deleted <= $end && $phone_deleted
2368 && (!$phone_deleted->lnp_status
2369 || $phone_deleted->lnp_status ne 'portingout')) {
2372 elsif($deleted >= $start && $deleted <= $end && $phone_deleted
2373 && $phone_deleted->lnp_status
2374 && $phone_deleted->lnp_status eq 'portingout') {
2378 # increment usage minutes
2379 if ( $phone_inserted ) {
2380 my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end,'billsec_sum'=>1);
2381 $minutes = $cdrs[0]->billsec_sum if scalar(@cdrs) == 1;
2384 warn "WARNING: no matching h_svc_phone insert record for insert time $inserted, svcnum " . $h_cust_svc->svcnum;
2387 # don't look at this service again
2388 push @seen, $h_cust_svc->svcnum;
2392 $minutes = sprintf("%d", $minutes);
2393 ("Activated: $num_activated Ported-In: $num_portedin Deactivated: "
2394 . "$num_deactivated Ported-Out: $num_portedout ",
2395 "Total Minutes: $minutes");
2398 sub _items_accountcode_cdr {
2403 my $section = { 'amount' => 0,
2406 'sort_weight' => '',
2408 'description' => 'Usage by Account Code',
2414 my %accountcodes = ();
2416 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2417 next unless $cust_bill_pkg->pkgnum > 0;
2419 my @header = $cust_bill_pkg->details_header;
2420 next unless scalar(@header);
2421 $section->{'header'} = join(',',@header);
2423 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2425 $section->{'header'} = $detail->formatted('format' => $format)
2426 if($detail->detail eq $section->{'header'});
2428 my $accountcode = $detail->accountcode;
2429 next unless $accountcode;
2431 my $amount = $detail->amount;
2432 next unless $amount && $amount > 0;
2434 $accountcodes{$accountcode} ||= {
2435 description => $accountcode,
2442 product_code => 'N/A',
2443 section => $section,
2444 ext_description => [ $section->{'header'} ],
2448 $section->{'amount'} += $amount;
2449 $accountcodes{$accountcode}{'amount'} += $amount;
2450 $accountcodes{$accountcode}{calls}++;
2451 $accountcodes{$accountcode}{duration} += $detail->duration;
2452 push @{$accountcodes{$accountcode}{detail_temp}}, $detail;
2456 foreach my $l ( values %accountcodes ) {
2457 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2458 my @sorted_detail = sort { $a->startdate <=> $b->startdate } @{$l->{detail_temp}};
2459 foreach my $sorted_detail ( @sorted_detail ) {
2460 push @{$l->{ext_description}}, $sorted_detail->formatted('format'=>$format);
2462 delete $l->{detail_temp};
2466 my @sorted_lines = sort { $a->{'description'} <=> $b->{'description'} } @lines;
2468 return ($section,\@sorted_lines);
2471 sub _items_svc_phone_sections {
2473 my $conf = $self->conf;
2481 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2483 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2484 $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
2486 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2487 next unless $cust_bill_pkg->pkgnum > 0;
2489 my @header = $cust_bill_pkg->details_header;
2490 next unless scalar(@header);
2492 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2494 my $phonenum = $detail->phonenum;
2495 next unless $phonenum;
2497 my $amount = $detail->amount;
2498 next unless $amount && $amount > 0;
2500 $sections{$phonenum} ||= { 'amount' => 0,
2503 'sort_weight' => -1,
2504 'phonenum' => $phonenum,
2506 $sections{$phonenum}{amount} += $amount; #subtotal
2507 $sections{$phonenum}{calls}++;
2508 $sections{$phonenum}{duration} += $detail->duration;
2510 my $desc = $detail->regionname;
2511 my $description = $desc;
2512 $description = substr($desc, 0, $maxlength). '...'
2513 if $format eq 'latex' && length($desc) > $maxlength;
2515 $lines{$phonenum}{$desc} ||= {
2516 description => &{$escape}($description),
2517 #pkgpart => $part_pkg->pkgpart,
2525 product_code => 'N/A',
2526 ext_description => [],
2529 $lines{$phonenum}{$desc}{amount} += $amount;
2530 $lines{$phonenum}{$desc}{calls}++;
2531 $lines{$phonenum}{$desc}{duration} += $detail->duration;
2533 my $line = $usage_class{$detail->classnum}->classname;
2534 $sections{"$phonenum $line"} ||=
2538 'sort_weight' => $usage_class{$detail->classnum}->weight,
2539 'phonenum' => $phonenum,
2540 'header' => [ @header ],
2542 $sections{"$phonenum $line"}{amount} += $amount; #subtotal
2543 $sections{"$phonenum $line"}{calls}++;
2544 $sections{"$phonenum $line"}{duration} += $detail->duration;
2546 $lines{"$phonenum $line"}{$desc} ||= {
2547 description => &{$escape}($description),
2548 #pkgpart => $part_pkg->pkgpart,
2556 product_code => 'N/A',
2557 ext_description => [],
2560 $lines{"$phonenum $line"}{$desc}{amount} += $amount;
2561 $lines{"$phonenum $line"}{$desc}{calls}++;
2562 $lines{"$phonenum $line"}{$desc}{duration} += $detail->duration;
2563 push @{$lines{"$phonenum $line"}{$desc}{ext_description}},
2564 $detail->formatted('format' => $format);
2569 my %sectionmap = ();
2570 my $simple = new FS::usage_class { format => 'simple' }; #bleh
2571 foreach ( keys %sections ) {
2572 my @header = @{ $sections{$_}{header} || [] };
2574 new FS::usage_class { format => 'usage_'. (scalar(@header) || 6). 'col' };
2575 my $summary = $sections{$_}{sort_weight} < 0 ? 1 : 0;
2576 my $usage_class = $summary ? $simple : $usage_simple;
2577 my $ending = $summary ? ' usage charges' : '';
2580 $gen_opt{label} = [ map{ &{$escape}($_) } @header ];
2582 $sectionmap{$_} = { 'description' => &{$escape}($_. $ending),
2583 'amount' => $sections{$_}{amount}, #subtotal
2584 'calls' => $sections{$_}{calls},
2585 'duration' => $sections{$_}{duration},
2587 'tax_section' => '',
2588 'phonenum' => $sections{$_}{phonenum},
2589 'sort_weight' => $sections{$_}{sort_weight},
2590 'post_total' => $summary, #inspire pagebreak
2592 ( map { $_ => $usage_class->$_($format, %gen_opt) }
2593 qw( description_generator
2596 total_line_generator
2603 my @sections = sort { $a->{phonenum} cmp $b->{phonenum} ||
2604 $a->{sort_weight} <=> $b->{sort_weight}
2609 foreach my $section ( keys %lines ) {
2610 foreach my $line ( keys %{$lines{$section}} ) {
2611 my $l = $lines{$section}{$line};
2612 $l->{section} = $sectionmap{$section};
2613 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2614 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2619 if($conf->exists('phone_usage_class_summary')) {
2620 # this only works with Latex
2624 # after this, we'll have only two sections per DID:
2625 # Calls Summary and Calls Detail
2626 foreach my $section ( @sections ) {
2627 if($section->{'post_total'}) {
2628 $section->{'description'} = 'Calls Summary: '.$section->{'phonenum'};
2629 $section->{'total_line_generator'} = sub { '' };
2630 $section->{'total_generator'} = sub { '' };
2631 $section->{'header_generator'} = sub { '' };
2632 $section->{'description_generator'} = '';
2633 push @newsections, $section;
2634 my %calls_detail = %$section;
2635 $calls_detail{'post_total'} = '';
2636 $calls_detail{'sort_weight'} = '';
2637 $calls_detail{'description_generator'} = sub { '' };
2638 $calls_detail{'header_generator'} = sub {
2639 return ' & Date/Time & Called Number & Duration & Price'
2640 if $format eq 'latex';
2643 $calls_detail{'description'} = 'Calls Detail: '
2644 . $section->{'phonenum'};
2645 push @newsections, \%calls_detail;
2649 # after this, each usage class is collapsed/summarized into a single
2650 # line under the Calls Summary section
2651 foreach my $newsection ( @newsections ) {
2652 if($newsection->{'post_total'}) { # this means Calls Summary
2653 foreach my $section ( @sections ) {
2654 next unless ($section->{'phonenum'} eq $newsection->{'phonenum'}
2655 && !$section->{'post_total'});
2656 my $newdesc = $section->{'description'};
2657 my $tn = $section->{'phonenum'};
2658 $newdesc =~ s/$tn//g;
2659 my $line = { ext_description => [],
2663 calls => $section->{'calls'},
2664 section => $newsection,
2665 duration => $section->{'duration'},
2666 description => $newdesc,
2667 amount => sprintf("%.2f",$section->{'amount'}),
2668 product_code => 'N/A',
2670 push @newlines, $line;
2675 # after this, Calls Details is populated with all CDRs
2676 foreach my $newsection ( @newsections ) {
2677 if(!$newsection->{'post_total'}) { # this means Calls Details
2678 foreach my $line ( @lines ) {
2679 next unless (scalar(@{$line->{'ext_description'}}) &&
2680 $line->{'section'}->{'phonenum'} eq $newsection->{'phonenum'}
2682 my @extdesc = @{$line->{'ext_description'}};
2684 foreach my $extdesc ( @extdesc ) {
2685 $extdesc =~ s/scriptsize/normalsize/g if $format eq 'latex';
2686 push @newextdesc, $extdesc;
2688 $line->{'ext_description'} = \@newextdesc;
2689 $line->{'section'} = $newsection;
2690 push @newlines, $line;
2695 return(\@newsections, \@newlines);
2698 return(\@sections, \@lines);
2702 =sub _items_usage_class_summary OPTIONS
2704 Returns a list of detail items summarizing the usage charges on this
2705 invoice. Each one will have 'amount', 'description' (the usage charge name),
2706 and 'usage_classnum'.
2708 OPTIONS can include 'escape' (a function to escape the descriptions).
2712 sub _items_usage_class_summary {
2716 my $escape = $opt{escape} || sub { $_[0] };
2717 my $money_char = $opt{money_char};
2718 my $invnum = $self->invnum;
2719 my @classes = qsearch({
2720 'table' => 'usage_class',
2721 'select' => 'classnum, classname, SUM(amount) AS amount,'.
2722 ' COUNT(*) AS calls, SUM(duration) AS duration',
2723 'addl_from' => ' LEFT JOIN cust_bill_pkg_detail USING (classnum)' .
2724 ' LEFT JOIN cust_bill_pkg USING (billpkgnum)',
2725 'extra_sql' => " WHERE cust_bill_pkg.invnum = $invnum".
2726 ' GROUP BY classnum, classname, weight'.
2727 ' HAVING (usage_class.disabled IS NULL OR SUM(amount) > 0)'.
2728 ' ORDER BY weight ASC',
2732 description => &{$escape}($self->mt('Usage Summary')),
2736 foreach my $class (@classes) {
2737 $section->{subtotal} += $class->get('amount');
2739 'description' => &{$escape}($class->classname),
2740 'amount' => $money_char.sprintf('%.2f', $class->get('amount')),
2741 'quantity' => $class->get('calls'),
2742 'duration' => $class->get('duration'),
2743 'usage_classnum' => $class->classnum,
2744 'section' => $section,
2747 $section->{subtotal} = $money_char.sprintf('%.2f', $section->{subtotal});
2751 sub _items_previous {
2753 my $conf = $self->conf;
2754 my $cust_main = $self->cust_main;
2755 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
2757 foreach ( @pr_cust_bill ) {
2758 my $date = $conf->exists('invoice_show_prior_due_date')
2759 ? 'due '. $_->due_date2str('short')
2760 : $self->time2str_local('short', $_->_date);
2762 'description' => $self->mt('Previous Balance, Invoice #'). $_->invnum. " ($date)",
2763 #'pkgpart' => 'N/A',
2765 'amount' => sprintf("%.2f", $_->owed),
2771 # 'description' => 'Previous Balance',
2772 # #'pkgpart' => 'N/A',
2773 # 'pkgnum' => 'N/A',
2774 # 'amount' => sprintf("%10.2f", $pr_total ),
2775 # 'ext_description' => [ map {
2776 # "Invoice ". $_->invnum.
2777 # " (". time2str("%x",$_->_date). ") ".
2778 # sprintf("%10.2f", $_->owed)
2779 # } @pr_cust_bill ],
2784 sub _items_credits {
2785 my( $self, %opt ) = @_;
2786 my $trim_len = $opt{'trim_len'} || 40;
2791 if ( $self->conf->exists('previous_balance-payments_since') ) {
2792 if ( $opt{'template'} eq 'statement' ) {
2793 # then the current bill is a "statement" (i.e. an invoice sent as
2794 # a payment receipt)
2795 # and in that case we want to see payments on or after THIS invoice
2796 @objects = qsearch('cust_credit', {
2797 'custnum' => $self->custnum,
2798 '_date' => {op => '>=', value => $self->_date},
2802 $date = $self->previous_bill->_date if $self->previous_bill;
2803 @objects = qsearch('cust_credit', {
2804 'custnum' => $self->custnum,
2805 '_date' => {op => '>=', value => $date},
2809 @objects = $self->cust_credited;
2812 foreach my $obj ( @objects ) {
2813 my $cust_credit = $obj->isa('FS::cust_credit') ? $obj : $obj->cust_credit;
2815 my $reason = substr($cust_credit->reason, 0, $trim_len);
2816 $reason .= '...' if length($reason) < length($cust_credit->reason);
2817 $reason = " ($reason) " if $reason;
2820 #'description' => 'Credit ref\#'. $_->crednum.
2821 # " (". time2str("%x",$_->cust_credit->_date) .")".
2823 'description' => $self->mt('Credit applied').' '.
2824 $self->time2str_local('short', $obj->_date). $reason,
2825 'amount' => sprintf("%.2f",$obj->amount),
2833 sub _items_payments {
2838 my $detailed = $self->conf->exists('invoice_payment_details');
2840 if ( $self->conf->exists('previous_balance-payments_since') ) {
2841 # then show payments dated on/after the previous bill...
2842 if ( $opt{'template'} eq 'statement' ) {
2843 # then the current bill is a "statement" (i.e. an invoice sent as
2844 # a payment receipt)
2845 # and in that case we want to see payments on or after THIS invoice
2846 @objects = qsearch('cust_pay', {
2847 'custnum' => $self->custnum,
2848 '_date' => {op => '>=', value => $self->_date},
2851 # the normal case: payments on or after the previous invoice
2853 $date = $self->previous_bill->_date if $self->previous_bill;
2854 @objects = qsearch('cust_pay', {
2855 'custnum' => $self->custnum,
2856 '_date' => {op => '>=', value => $date},
2858 # and before the current bill...
2859 @objects = grep { $_->_date < $self->_date } @objects;
2862 @objects = $self->cust_bill_pay;
2865 foreach my $obj (@objects) {
2866 my $cust_pay = $obj->isa('FS::cust_pay') ? $obj : $obj->cust_pay;
2867 my $desc = $self->mt('Payment received').' '.
2868 $self->time2str_local('short', $cust_pay->_date );
2869 $desc .= $self->mt(' via ') .
2870 $cust_pay->payby_payinfo_pretty( $self->cust_main->locale )
2874 'description' => $desc,
2875 'amount' => sprintf("%.2f", $obj->amount )
2885 my $conf = $self->conf;
2888 my ($pr_total) = $self->previous;
2889 my ($previous_charges_desc, $new_charges_desc, $new_charges_amount);
2891 if ( $conf->exists('previous_balance-exclude_from_total') ) {
2892 # if enabled, specifically add a line for the previous balance total
2893 $previous_charges_desc = $self->mt(
2894 $conf->config('previous_balance-text') || 'Previous Balance'
2897 # then return separate lines for previous balance and total new charges
2900 { total_item => $previous_charges_desc,
2901 total_amount => sprintf('%.2f',$pr_total)
2906 if ( $conf->exists('previous_balance-exclude_from_total')
2907 or !$self->enable_previous ) {
2908 # show new charges only
2910 $new_charges_desc = $self->mt(
2911 $conf->config('previous_balance-text-total_new_charges')
2912 || 'Total New Charges'
2915 $new_charges_amount = $self->charged;
2918 # show new charges + previous invoice total
2920 $new_charges_desc = $self->mt('Total Charges');
2921 if ( $self->enable_previous ) {
2922 $new_charges_amount = sprintf('%.2f', $self->charged + $pr_total);
2924 $new_charges_amount = sprintf('%.2f', $self->charged);
2928 if ( $conf->exists('invoice_show_prior_due_date') && !$conf->exists('invoice_omit_due_date') ) {
2929 # then the due date should be shown with Total New Charges,
2930 # and should NOT be shown with the Balance Due message.
2931 if ( $self->due_date ) {
2932 $new_charges_desc .= $self->invoice_pay_by_msg;
2933 } elsif ( $self->terms ) {
2934 # phrases like "due on receipt" should be localized
2935 $new_charges_desc .= ' - ' . $self->mt($self->terms);
2940 { total_item => $new_charges_desc,
2941 total_amount => $new_charges_amount,
2949 =item has_call_details
2951 Returns true if this invoice has call details.
2955 sub has_call_details {
2958 SELECT 1 FROM cust_bill_pkg_detail
2959 LEFT JOIN cust_bill_pkg USING (billpkgnum)
2960 WHERE cust_bill_pkg_detail.format = 'C'
2961 AND cust_bill_pkg.invnum = ?
2966 =item call_details [ OPTION => VALUE ... ]
2968 Returns an array of CSV strings representing the call details for this invoice
2969 The only option available is the boolean prepend_billed_number
2974 my ($self, %opt) = @_;
2976 my $format_function = sub { shift };
2978 if ($opt{prepend_billed_number}) {
2979 $format_function = sub {
2983 $row->amount ? $row->phonenum. ",". $detail : '"Billed number",'. $detail;
2988 my @details = map { $_->details( 'format_function' => $format_function,
2989 'escape_function' => sub{ return() },
2993 $self->cust_bill_pkg;
2994 my $header = $details[0];
2995 ( $header, grep { $_ ne $header } @details );
3005 =item process_reprint
3009 sub process_reprint {
3010 process_re_X('print', @_);
3013 =item process_reemail
3017 sub process_reemail {
3018 process_re_X('email', @_);
3026 process_re_X('fax', @_);
3034 process_re_X('ftp', @_);
3041 sub process_respool {
3042 process_re_X('spool', @_);
3045 use Storable qw(thaw);
3049 my( $method, $job ) = ( shift, shift );
3050 warn "$me process_re_X $method for job $job\n" if $DEBUG;
3052 my $param = thaw(decode_base64(shift));
3053 warn Dumper($param) if $DEBUG;
3063 # this is called from search/cust_bill.html and given all its search
3064 # parameters, so it needs to perform the same search.
3067 # spool_invoice ftp_invoice fax_invoice print_invoice
3068 my($method, $job, %param ) = @_;
3070 warn "re_X $method for job $job with param:\n".
3071 join( '', map { " $_ => ". $param{$_}. "\n" } keys %param );
3074 #some false laziness w/search/cust_bill.html
3075 $param{'order_by'} = 'cust_bill._date';
3077 my $query = FS::cust_bill->search(\%param);
3078 delete $query->{'count_query'};
3079 delete $query->{'count_addl'};
3081 $query->{debug} = 1; # was in here before, is obviously useful
3083 my @cust_bill = qsearch( $query );
3085 $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
3087 warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
3090 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
3091 foreach my $cust_bill ( @cust_bill ) {
3092 $cust_bill->$method();
3094 if ( $job ) { #progressbar foo
3096 if ( time - $min_sec > $last ) {
3097 my $error = $job->update_statustext(
3098 int( 100 * $num / scalar(@cust_bill) )
3100 die $error if $error;
3111 =head1 CLASS METHODS
3117 Returns an SQL fragment to retreive the amount owed (charged minus credited and paid).
3122 my ($class, $start, $end) = @_;
3124 $class->paid_sql($start, $end). ' - '.
3125 $class->credited_sql($start, $end);
3130 Returns an SQL fragment to retreive the net amount (charged minus credited).
3135 my ($class, $start, $end) = @_;
3136 'charged - '. $class->credited_sql($start, $end);
3141 Returns an SQL fragment to retreive the amount paid against this invoice.
3146 my ($class, $start, $end) = @_;
3147 $start &&= "AND cust_bill_pay._date <= $start";
3148 $end &&= "AND cust_bill_pay._date > $end";
3149 $start = '' unless defined($start);
3150 $end = '' unless defined($end);
3151 "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
3152 WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end )";
3157 Returns an SQL fragment to retreive the amount credited against this invoice.
3162 my ($class, $start, $end) = @_;
3163 $start &&= "AND cust_credit_bill._date <= $start";
3164 $end &&= "AND cust_credit_bill._date > $end";
3165 $start = '' unless defined($start);
3166 $end = '' unless defined($end);
3167 "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
3168 WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end )";
3173 Returns an SQL fragment to retrieve the due date of an invoice.
3174 Currently only supported on PostgreSQL.
3179 die "don't use: doesn't account for agent-specific invoice_default_terms";
3181 #we're passed a $conf but not a specific customer (that's in the query), so
3182 # to make this work we'd need an agentnum-aware "condition_sql_conf" like
3183 # "condition_sql_option" that retreives a conf value with SQL in an agent-
3186 my $conf = new FS::Conf;
3190 cust_bill.invoice_terms,
3191 cust_main.invoice_terms,
3192 \''.($conf->config('invoice_default_terms') || '').'\'
3193 ), E\'Net (\\\\d+)\'
3195 ) * 86400 + cust_bill._date'
3206 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
3207 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base