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 : '';
219 local $SIG{HUP} = 'IGNORE';
220 local $SIG{INT} = 'IGNORE';
221 local $SIG{QUIT} = 'IGNORE';
222 local $SIG{TERM} = 'IGNORE';
223 local $SIG{TSTP} = 'IGNORE';
224 local $SIG{PIPE} = 'IGNORE';
226 my $oldAutoCommit = $FS::UID::AutoCommit;
227 local $FS::UID::AutoCommit = 0;
230 my $cust_bill_void = new FS::cust_bill_void ( {
231 map { $_ => $self->get($_) } $self->fields
233 $cust_bill_void->reason($reason);
234 my $error = $cust_bill_void->insert;
236 $dbh->rollback if $oldAutoCommit;
240 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
241 my $error = $cust_bill_pkg->void($reason);
243 $dbh->rollback if $oldAutoCommit;
248 $error = $self->delete;
250 $dbh->rollback if $oldAutoCommit;
254 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
262 This method now works but you probably shouldn't use it. Instead, apply a
263 credit against the invoice, or use the new void method.
265 Using this method to delete invoices outright is really, really bad. There
266 would be no record you ever posted this invoice, and there are no check to
267 make sure charged = 0 or that there are no associated cust_bill_pkg records.
269 Really, don't use it.
275 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
277 local $SIG{HUP} = 'IGNORE';
278 local $SIG{INT} = 'IGNORE';
279 local $SIG{QUIT} = 'IGNORE';
280 local $SIG{TERM} = 'IGNORE';
281 local $SIG{TSTP} = 'IGNORE';
282 local $SIG{PIPE} = 'IGNORE';
284 my $oldAutoCommit = $FS::UID::AutoCommit;
285 local $FS::UID::AutoCommit = 0;
288 foreach my $table (qw(
299 foreach my $linked ( $self->$table() ) {
300 my $error = $linked->delete;
302 $dbh->rollback if $oldAutoCommit;
309 my $error = $self->SUPER::delete(@_);
311 $dbh->rollback if $oldAutoCommit;
315 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
321 =item replace [ OLD_RECORD ]
323 You can, but probably shouldn't modify invoices...
325 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
326 supplied, replaces this record. If there is an error, returns the error,
327 otherwise returns false.
331 #replace can be inherited from Record.pm
333 # replace_check is now the preferred way to #implement replace data checks
334 # (so $object->replace() works without an argument)
337 my( $new, $old ) = ( shift, shift );
338 return "Can't modify closed invoice" if $old->closed =~ /^Y/i;
339 #return "Can't change _date!" unless $old->_date eq $new->_date;
340 return "Can't change _date" unless $old->_date == $new->_date;
341 return "Can't change charged" unless $old->charged == $new->charged
342 || $old->charged == 0
343 || $new->{'Hash'}{'cc_surcharge_replace_hack'};
349 =item add_cc_surcharge
355 sub add_cc_surcharge {
356 my ($self, $pkgnum, $amount) = (shift, shift, shift);
359 my $cust_bill_pkg = new FS::cust_bill_pkg({
360 'invnum' => $self->invnum,
364 $error = $cust_bill_pkg->insert;
365 return $error if $error;
367 $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
368 $self->charged($self->charged+$amount);
369 $error = $self->replace;
370 return $error if $error;
372 $self->apply_payments_and_credits;
378 Checks all fields to make sure this is a valid invoice. If there is an error,
379 returns the error, otherwise returns false. Called by the insert and replace
388 $self->ut_numbern('invnum')
389 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
390 || $self->ut_numbern('_date')
391 || $self->ut_money('charged')
392 || $self->ut_numbern('printed')
393 || $self->ut_enum('closed', [ '', 'Y' ])
394 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum' )
395 || $self->ut_numbern('agent_invid') #varchar?
397 return $error if $error;
399 $self->_date(time) unless $self->_date;
401 $self->printed(0) if $self->printed eq '';
408 Returns the displayed invoice number for this invoice: agent_invid if
409 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
415 if ( $self->agent_invid
416 && FS::Conf->new->exists('cust_bill-default_agent_invid') ) {
417 return $self->agent_invid;
419 return $self->invnum;
425 Returns the customer's last invoice before this one.
431 if ( !$self->get('previous_bill') ) {
432 $self->set('previous_bill', qsearchs({
433 'table' => 'cust_bill',
434 'hashref' => { 'custnum' => $self->custnum,
435 '_date' => { op=>'<', value=>$self->_date } },
436 'order_by' => 'ORDER BY _date DESC LIMIT 1',
439 $self->get('previous_bill');
444 Returns a list consisting of the total previous balance for this customer,
445 followed by the previous outstanding invoices (as FS::cust_bill objects also).
451 # simple memoize; we use this a lot
452 if (!$self->get('previous')) {
454 my @cust_bill = sort { $a->_date <=> $b->_date }
455 grep { $_->owed != 0 }
456 qsearch( 'cust_bill', { 'custnum' => $self->custnum,
457 #'_date' => { op=>'<', value=>$self->_date },
458 'invnum' => { op=>'<', value=>$self->invnum },
461 foreach ( @cust_bill ) { $total += $_->owed; }
462 $self->set('previous', [$total, @cust_bill]);
464 return @{ $self->get('previous') };
467 =item enable_previous
469 Whether to show the 'Previous Charges' section when printing this invoice.
470 The negation of the 'disable_previous_balance' config setting.
474 sub enable_previous {
476 my $agentnum = $self->cust_main->agentnum;
477 !$self->conf->exists('disable_previous_balance', $agentnum);
482 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
489 { 'table' => 'cust_bill_pkg',
490 'hashref' => { 'invnum' => $self->invnum },
491 'order_by' => 'ORDER BY billpkgnum',
496 =item cust_bill_pkg_pkgnum PKGNUM
498 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice and
503 sub cust_bill_pkg_pkgnum {
504 my( $self, $pkgnum ) = @_;
506 { 'table' => 'cust_bill_pkg',
507 'hashref' => { 'invnum' => $self->invnum,
510 'order_by' => 'ORDER BY billpkgnum',
517 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
524 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
525 $self->cust_bill_pkg;
527 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
532 Returns true if any of the packages (or their definitions) corresponding to the
533 line items for this invoice have the no_auto flag set.
539 grep { $_->no_auto || $_->part_pkg->no_auto } $self->cust_pkg;
542 =item open_cust_bill_pkg
544 Returns the open line items for this invoice.
546 Note that cust_bill_pkg with both setup and recur fees are returned as two
547 separate line items, each with only one fee.
551 # modeled after cust_main::open_cust_bill
552 sub open_cust_bill_pkg {
555 # grep { $_->owed > 0 } $self->cust_bill_pkg
557 my %other = ( 'recur' => 'setup',
558 'setup' => 'recur', );
560 foreach my $field ( qw( recur setup )) {
561 push @open, map { $_->set( $other{$field}, 0 ); $_; }
562 grep { $_->owed($field) > 0 }
563 $self->cust_bill_pkg;
569 =item cust_bill_event
571 Returns the completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
575 sub cust_bill_event {
577 qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
580 =item num_cust_bill_event
582 Returns the number of completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
586 sub num_cust_bill_event {
589 "SELECT COUNT(*) FROM cust_bill_event WHERE invnum = ?";
590 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
591 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
592 $sth->fetchrow_arrayref->[0];
597 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
601 #false laziness w/cust_pkg.pm
605 'table' => 'cust_event',
606 'addl_from' => 'JOIN part_event USING ( eventpart )',
607 'hashref' => { 'tablenum' => $self->invnum },
608 'extra_sql' => " AND eventtable = 'cust_bill' ",
614 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
618 #false laziness w/cust_pkg.pm
622 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
623 " WHERE tablenum = ? AND eventtable = 'cust_bill'";
624 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
625 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
626 $sth->fetchrow_arrayref->[0];
631 Returns the customer (see L<FS::cust_main>) for this invoice.
637 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
640 =item cust_suspend_if_balance_over AMOUNT
642 Suspends the customer associated with this invoice if the total amount owed on
643 this invoice and all older invoices is greater than the specified amount.
645 Returns a list: an empty list on success or a list of errors.
649 sub cust_suspend_if_balance_over {
650 my( $self, $amount ) = ( shift, shift );
651 my $cust_main = $self->cust_main;
652 if ( $cust_main->total_owed_date($self->_date) < $amount ) {
655 $cust_main->suspend(@_);
661 Depreciated. See the cust_credited method.
663 #Returns a list consisting of the total previous credited (see
664 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
665 #outstanding credits (FS::cust_credit objects).
671 croak "FS::cust_bill->cust_credit depreciated; see ".
672 "FS::cust_bill->cust_credit_bill";
675 #my @cust_credit = sort { $a->_date <=> $b->_date }
676 # grep { $_->credited != 0 && $_->_date < $self->_date }
677 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
679 #foreach (@cust_credit) { $total += $_->credited; }
680 #$total, @cust_credit;
685 Depreciated. See the cust_bill_pay method.
687 #Returns all payments (see L<FS::cust_pay>) for this invoice.
693 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
695 #sort { $a->_date <=> $b->_date }
696 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
702 qsearch('cust_pay_batch', { 'invnum' => $self->invnum } );
705 sub cust_bill_pay_batch {
707 qsearch('cust_bill_pay_batch', { 'invnum' => $self->invnum } );
712 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
718 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
719 sort { $a->_date <=> $b->_date }
720 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
725 =item cust_credit_bill
727 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
733 map { $_ } #return $self->num_cust_credit_bill unless wantarray;
734 sort { $a->_date <=> $b->_date }
735 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
739 sub cust_credit_bill {
740 shift->cust_credited(@_);
743 #=item cust_bill_pay_pkgnum PKGNUM
745 #Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
746 #with matching pkgnum.
750 #sub cust_bill_pay_pkgnum {
751 # my( $self, $pkgnum ) = @_;
752 # map { $_ } #return $self->num_cust_bill_pay_pkgnum($pkgnum) unless wantarray;
753 # sort { $a->_date <=> $b->_date }
754 # qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum,
755 # 'pkgnum' => $pkgnum,
760 =item cust_bill_pay_pkg PKGNUM
762 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
763 applied against the matching pkgnum.
767 sub cust_bill_pay_pkg {
768 my( $self, $pkgnum ) = @_;
771 'select' => 'cust_bill_pay_pkg.*',
772 'table' => 'cust_bill_pay_pkg',
773 'addl_from' => ' LEFT JOIN cust_bill_pay USING ( billpaynum ) '.
774 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
775 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
776 " AND cust_bill_pkg.pkgnum = $pkgnum",
781 #=item cust_credited_pkgnum PKGNUM
783 #=item cust_credit_bill_pkgnum PKGNUM
785 #Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice
786 #with matching pkgnum.
790 #sub cust_credited_pkgnum {
791 # my( $self, $pkgnum ) = @_;
792 # map { $_ } #return $self->num_cust_credit_bill_pkgnum($pkgnum) unless wantarray;
793 # sort { $a->_date <=> $b->_date }
794 # qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum,
795 # 'pkgnum' => $pkgnum,
800 #sub cust_credit_bill_pkgnum {
801 # shift->cust_credited_pkgnum(@_);
804 =item cust_credit_bill_pkg PKGNUM
806 Returns all credit applications (see L<FS::cust_credit_bill>) for this invoice
807 applied against the matching pkgnum.
811 sub cust_credit_bill_pkg {
812 my( $self, $pkgnum ) = @_;
815 'select' => 'cust_credit_bill_pkg.*',
816 'table' => 'cust_credit_bill_pkg',
817 'addl_from' => ' LEFT JOIN cust_credit_bill USING ( creditbillnum ) '.
818 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
819 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
820 " AND cust_bill_pkg.pkgnum = $pkgnum",
825 =item cust_bill_batch
827 Returns all invoice batch records (L<FS::cust_bill_batch>) for this invoice.
831 sub cust_bill_batch {
833 qsearch('cust_bill_batch', { 'invnum' => $self->invnum });
838 Returns all discount plans (L<FS::discount_plan>) for this invoice, as a
839 hash keyed by term length.
845 FS::discount_plan->all($self);
850 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
857 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
859 foreach (@taxlines) { $total += $_->setup; }
865 Returns the amount owed (still outstanding) on this invoice, which is charged
866 minus all payment applications (see L<FS::cust_bill_pay>) and credit
867 applications (see L<FS::cust_credit_bill>).
873 my $balance = $self->charged;
874 $balance -= $_->amount foreach ( $self->cust_bill_pay );
875 $balance -= $_->amount foreach ( $self->cust_credited );
876 $balance = sprintf( "%.2f", $balance);
877 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
882 my( $self, $pkgnum ) = @_;
884 #my $balance = $self->charged;
886 $balance += $_->setup + $_->recur for $self->cust_bill_pkg_pkgnum($pkgnum);
888 $balance -= $_->amount for $self->cust_bill_pay_pkg($pkgnum);
889 $balance -= $_->amount for $self->cust_credit_bill_pkg($pkgnum);
891 $balance = sprintf( "%.2f", $balance);
892 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
898 Returns true if this invoice should be hidden. See the
899 selfservice-hide_invoices-taxclass configuraiton setting.
905 my $conf = $self->conf;
906 my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass')
908 my @cust_bill_pkg = $self->cust_bill_pkg;
909 my @part_pkg = grep $_, map $_->part_pkg, @cust_bill_pkg;
910 ! grep { $_->taxclass ne $hide_taxclass } @part_pkg;
913 =item apply_payments_and_credits [ OPTION => VALUE ... ]
915 Applies unapplied payments and credits to this invoice.
916 Payments with the no_auto_apply flag set will not be applied.
918 A hash of optional arguments may be passed. Currently "manual" is supported.
919 If true, a payment receipt is sent instead of a statement when
920 'payment_receipt_email' configuration option is set.
922 If there is an error, returns the error, otherwise returns false.
926 sub apply_payments_and_credits {
927 my( $self, %options ) = @_;
928 my $conf = $self->conf;
930 local $SIG{HUP} = 'IGNORE';
931 local $SIG{INT} = 'IGNORE';
932 local $SIG{QUIT} = 'IGNORE';
933 local $SIG{TERM} = 'IGNORE';
934 local $SIG{TSTP} = 'IGNORE';
935 local $SIG{PIPE} = 'IGNORE';
937 my $oldAutoCommit = $FS::UID::AutoCommit;
938 local $FS::UID::AutoCommit = 0;
941 $self->select_for_update; #mutex
943 my @payments = grep { $_->unapplied > 0 }
944 grep { !$_->no_auto_apply }
945 $self->cust_main->cust_pay;
946 my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit;
948 if ( $conf->exists('pkg-balances') ) {
949 # limit @payments & @credits to those w/ a pkgnum grepped from $self
950 my %pkgnums = map { $_ => 1 } map $_->pkgnum, $self->cust_bill_pkg;
951 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
952 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
955 while ( $self->owed > 0 and ( @payments || @credits ) ) {
958 if ( @payments && @credits ) {
960 #decide which goes first by weight of top (unapplied) line item
962 my @open_lineitems = $self->open_cust_bill_pkg;
965 max( map { $_->part_pkg->pay_weight || 0 }
970 my $max_credit_weight =
971 max( map { $_->part_pkg->credit_weight || 0 }
977 #if both are the same... payments first? it has to be something
978 if ( $max_pay_weight >= $max_credit_weight ) {
984 } elsif ( @payments ) {
986 } elsif ( @credits ) {
989 die "guru meditation #12 and 35";
993 if ( $app eq 'pay' ) {
995 my $payment = shift @payments;
996 $unapp_amount = $payment->unapplied;
997 $app = new FS::cust_bill_pay { 'paynum' => $payment->paynum };
998 $app->pkgnum( $payment->pkgnum )
999 if $conf->exists('pkg-balances') && $payment->pkgnum;
1001 } elsif ( $app eq 'credit' ) {
1003 my $credit = shift @credits;
1004 $unapp_amount = $credit->credited;
1005 $app = new FS::cust_credit_bill { 'crednum' => $credit->crednum };
1006 $app->pkgnum( $credit->pkgnum )
1007 if $conf->exists('pkg-balances') && $credit->pkgnum;
1010 die "guru meditation #12 and 35";
1014 if ( $conf->exists('pkg-balances') && $app->pkgnum ) {
1015 warn "owed_pkgnum ". $app->pkgnum;
1016 $owed = $self->owed_pkgnum($app->pkgnum);
1018 $owed = $self->owed;
1020 next unless $owed > 0;
1022 warn "min ( $unapp_amount, $owed )\n" if $DEBUG;
1023 $app->amount( sprintf('%.2f', min( $unapp_amount, $owed ) ) );
1025 $app->invnum( $self->invnum );
1027 my $error = $app->insert(%options);
1029 $dbh->rollback if $oldAutoCommit;
1030 return "Error inserting ". $app->table. " record: $error";
1032 die $error if $error;
1036 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1043 Sends this invoice to the destinations configured for this customer: sends
1044 email, prints and/or faxes. See L<FS::cust_main_invoice>.
1046 Options can be passed as a hashref. Positional parameters are no longer
1049 I<template>: a suffix for alternate invoices
1051 I<agentnum>: obsolete, now does nothing.
1053 I<from> overrides the default email invoice From: address.
1055 I<amount>: obsolete, does nothing
1057 I<notice_name> overrides "Invoice" as the name of the sent document
1058 (templates from 10/2009 or newer required).
1060 I<lpr> overrides the system 'lpr' option as the command to print a document
1061 from standard input.
1067 my $opt = ref($_[0]) ? $_[0] : +{ @_ };
1068 my $conf = $self->conf;
1070 my $cust_main = $self->cust_main;
1072 my @invoicing_list = $cust_main->invoicing_list;
1075 if ( grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list )
1076 && ! $cust_main->invoice_noemail;
1079 if grep { $_ eq 'POST' } @invoicing_list; #postal
1081 #this has never been used post-$ORIGINAL_ISP afaik
1082 $self->fax_invoice($opt)
1083 if grep { $_ eq 'FAX' } @invoicing_list; #fax
1091 my $opt = shift || {};
1092 if ($opt and !ref($opt)) {
1093 die ref($self). '->email called with positional parameters';
1096 my $conf = $self->conf;
1098 my $from = delete $opt->{from};
1100 # this is where we set the From: address
1101 $from ||= $self->_agent_invoice_from || #XXX should go away
1102 $conf->invoice_from_full( $self->cust_main->agentnum );
1104 my @invoicing_list = $self->cust_main->invoicing_list_emailonly;
1106 if ( ! @invoicing_list ) { #no recipients
1107 if ( $conf->exists('cust_bill-no_recipients-error') ) {
1108 die 'No recipients for customer #'. $self->custnum;
1110 #default: better to notify this person than silence
1111 @invoicing_list = ($from);
1115 $self->SUPER::email( {
1117 'to' => \@invoicing_list,
1123 #this stays here for now because its explicitly used as
1124 # FS::cust_bill::queueable_email
1125 sub queueable_email {
1128 my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1129 or die "invalid invoice number: " . $opt{invnum};
1131 $self->set('mode', $opt{mode})
1134 my %args = map {$_ => $opt{$_}}
1136 qw( from notice_name no_coupon template );
1138 my $error = $self->email( \%args );
1139 die $error if $error;
1145 my $conf = $self->conf;
1147 #my $template = scalar(@_) ? shift : '';
1150 my $subject = $conf->config('invoice_subject', $self->cust_main->agentnum)
1153 my $cust_main = $self->cust_main;
1154 my $name = $cust_main->name;
1155 my $name_short = $cust_main->name_short;
1156 my $invoice_number = $self->invnum;
1157 my $invoice_date = $self->_date_pretty;
1159 eval qq("$subject");
1162 =item lpr_data HASHREF
1164 Returns the postscript or plaintext for this invoice as an arrayref.
1166 Options must be passed as a hashref. Positional parameters are no longer
1169 I<template>, if specified, is the name of a suffix for alternate invoices.
1171 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1177 my $conf = $self->conf;
1178 my $opt = shift || {};
1179 if ($opt and !ref($opt)) {
1180 # nobody does this anyway
1181 die "FS::cust_bill::lpr_data called with positional parameters";
1184 my $method = $conf->exists('invoice_latex') ? 'print_ps' : 'print_text';
1185 [ $self->$method( $opt ) ];
1190 Prints this invoice.
1192 Options must be passed as a hashref.
1194 I<template>, if specified, is the name of a suffix for alternate invoices.
1196 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1202 return if $self->hide;
1203 my $conf = $self->conf;
1204 my $opt = shift || {};
1205 if ($opt and !ref($opt)) {
1206 die "FS::cust_bill::print called with positional parameters";
1209 my $lpr = delete $opt->{lpr};
1210 if($conf->exists('invoice_print_pdf')) {
1211 # Add the invoice to the current batch.
1212 $self->batch_invoice($opt);
1216 $self->lpr_data($opt),
1217 'agentnum' => $self->cust_main->agentnum,
1223 =item fax_invoice HASHREF
1227 Options must be passed as a hashref.
1229 I<template>, if specified, is the name of a suffix for alternate invoices.
1231 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1237 return if $self->hide;
1238 my $conf = $self->conf;
1239 my $opt = shift || {};
1240 if ($opt and !ref($opt)) {
1241 die "FS::cust_bill::fax_invoice called with positional parameters";
1244 die 'FAX invoice destination not (yet?) supported with plain text invoices.'
1245 unless $conf->exists('invoice_latex');
1247 my $dialstring = $self->cust_main->getfield('fax');
1250 my $error = send_fax( 'docdata' => $self->lpr_data($opt),
1251 'dialstring' => $dialstring,
1253 die $error if $error;
1257 =item batch_invoice [ HASHREF ]
1259 Place this invoice into the open batch (see C<FS::bill_batch>). If there
1260 isn't an open batch, one will be created.
1262 HASHREF may contain any options to be passed to C<print_pdf>.
1267 my ($self, $opt) = @_;
1268 my $bill_batch = $self->get_open_bill_batch;
1269 my $cust_bill_batch = FS::cust_bill_batch->new({
1270 batchnum => $bill_batch->batchnum,
1271 invnum => $self->invnum,
1273 if ( $self->mode ) {
1274 $opt->{mode} ||= $self->mode;
1275 $opt->{mode} = $opt->{mode}->modenum if ref $opt->{mode};
1277 return $cust_bill_batch->insert($opt);
1280 =item get_open_batch
1282 Returns the currently open batch as an FS::bill_batch object, creating a new
1283 one if necessary. (A per-agent batch if invoice_print_pdf-spoolagent is
1288 sub get_open_bill_batch {
1290 my $conf = $self->conf;
1291 my $hashref = { status => 'O' };
1292 $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent')
1293 ? $self->cust_main->agentnum
1295 my $batch = qsearchs('bill_batch', $hashref);
1296 return $batch if $batch;
1297 $batch = FS::bill_batch->new($hashref);
1298 my $error = $batch->insert;
1299 die $error if $error;
1303 =item ftp_invoice [ TEMPLATENAME ]
1305 Sends this invoice data via FTP.
1307 TEMPLATENAME is unused?
1313 my $conf = $self->conf;
1314 my $template = scalar(@_) ? shift : '';
1317 'protocol' => 'ftp',
1318 'server' => $conf->config('cust_bill-ftpserver'),
1319 'username' => $conf->config('cust_bill-ftpusername'),
1320 'password' => $conf->config('cust_bill-ftppassword'),
1321 'dir' => $conf->config('cust_bill-ftpdir'),
1322 'format' => $conf->config('cust_bill-ftpformat'),
1326 =item spool_invoice [ TEMPLATENAME ]
1328 Spools this invoice data (see L<FS::spool_csv>)
1330 TEMPLATENAME is unused?
1336 my $conf = $self->conf;
1337 my $template = scalar(@_) ? shift : '';
1340 'format' => $conf->config('cust_bill-spoolformat'),
1341 'agent_spools' => $conf->exists('cust_bill-spoolagent'),
1345 =item send_csv OPTION => VALUE, ...
1347 Sends invoice as a CSV data-file to a remote host with the specified protocol.
1351 protocol - currently only "ftp"
1357 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
1358 and YYMMDDHHMMSS is a timestamp.
1360 See L</print_csv> for a description of the output format.
1365 my($self, %opt) = @_;
1369 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1370 mkdir $spooldir, 0700 unless -d $spooldir;
1372 # don't localize dates here, they're a defined format
1373 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1374 my $file = "$spooldir/$tracctnum.csv";
1376 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1378 open(CSV, ">$file") or die "can't open $file: $!";
1386 if ( $opt{protocol} eq 'ftp' ) {
1387 eval "use Net::FTP;";
1389 $net = Net::FTP->new($opt{server}) or die @$;
1391 die "unknown protocol: $opt{protocol}";
1394 $net->login( $opt{username}, $opt{password} )
1395 or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
1397 $net->binary or die "can't set binary mode";
1399 $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
1401 $net->put($file) or die "can't put $file: $!";
1411 Spools CSV invoice data.
1417 =item format - any of FS::Misc::::Invoicing::spool_formats
1419 =item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the
1420 customer has the corresponding invoice destinations set (see
1421 L<FS::cust_main_invoice>).
1423 =item agent_spools - if set to a true value, will spool to per-agent files
1424 rather than a single global file
1426 =item upload_targetnum - if set to a target (see L<FS::upload_target>), will
1427 append to that spool. L<FS::Cron::upload> will then send the spool file to
1430 =item balanceover - if set, only spools the invoice if the total amount owed on
1431 this invoice and all older invoices is greater than the specified amount.
1433 =item time - the "current time". Controls the printing of past due messages
1441 my($self, %opt) = @_;
1443 my $time = $opt{'time'} || time;
1444 my $cust_main = $self->cust_main;
1446 if ( $opt{'dest'} ) {
1447 my %invoicing_list = map { /^(POST|FAX)$/ or 'EMAIL' =~ /^(.*)$/; $1 => 1 }
1448 $cust_main->invoicing_list;
1449 return 'N/A' unless $invoicing_list{$opt{'dest'}}
1450 || ! keys %invoicing_list;
1453 if ( $opt{'balanceover'} ) {
1455 if $cust_main->total_owed_date($self->_date) < $opt{'balanceover'};
1458 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1459 mkdir $spooldir, 0700 unless -d $spooldir;
1461 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', $time);
1464 if ( $opt{'agent_spools'} ) {
1465 $file = 'agentnum'.$cust_main->agentnum;
1470 if ( $opt{'upload_targetnum'} ) {
1471 $spooldir .= '/target'.$opt{'upload_targetnum'};
1472 mkdir $spooldir, 0700 unless -d $spooldir;
1473 } # otherwise it just goes into export.xxx/cust_bill
1475 if ( lc($opt{'format'}) eq 'billco' ) {
1479 $file = "$spooldir/$file.csv";
1481 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum);
1483 open(CSV, ">>$file") or die "can't open $file: $!";
1484 flock(CSV, LOCK_EX);
1489 if ( lc($opt{'format'}) eq 'billco' ) {
1491 flock(CSV, LOCK_UN);
1494 $file =~ s/-header.csv$/-detail.csv/;
1496 open(CSV,">>$file") or die "can't open $file: $!";
1497 flock(CSV, LOCK_EX);
1501 print CSV $detail if defined($detail);
1503 flock(CSV, LOCK_UN);
1510 =item print_csv OPTION => VALUE, ...
1512 Returns CSV data for this invoice.
1516 format - 'default', 'billco', 'oneline', 'bridgestone'
1518 Returns a list consisting of two scalars. The first is a single line of CSV
1519 header information for this invoice. The second is one or more lines of CSV
1520 detail information for this invoice.
1522 If I<format> is not specified or "default", the fields of the CSV file are as
1525 record_type, invnum, custnum, _date, charged, first, last, company, address1,
1526 address2, city, state, zip, country, pkg, setup, recur, sdate, edate
1530 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
1532 B<record_type> is C<cust_bill> for the initial header line only. The
1533 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
1534 fields are filled in.
1536 B<record_type> is C<cust_bill_pkg> for detail lines. Only the first two fields
1537 (B<record_type> and B<invnum>) and the last five fields (B<pkg> through B<edate>)
1540 =item invnum - invoice number
1542 =item custnum - customer number
1544 =item _date - invoice date
1546 =item charged - total invoice amount
1548 =item first - customer first name
1550 =item last - customer first name
1552 =item company - company name
1554 =item address1 - address line 1
1556 =item address2 - address line 1
1566 =item pkg - line item description
1568 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
1570 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
1572 =item sdate - start date for recurring fee
1574 =item edate - end date for recurring fee
1578 If I<format> is "billco", the fields of the header CSV file are as follows:
1580 +-------------------------------------------------------------------+
1581 | FORMAT HEADER FILE |
1582 |-------------------------------------------------------------------|
1583 | Field | Description | Name | Type | Width |
1584 | 1 | N/A-Leave Empty | RC | CHAR | 2 |
1585 | 2 | N/A-Leave Empty | CUSTID | CHAR | 15 |
1586 | 3 | Transaction Account No | TRACCTNUM | CHAR | 15 |
1587 | 4 | Transaction Invoice No | TRINVOICE | CHAR | 15 |
1588 | 5 | Transaction Zip Code | TRZIP | CHAR | 5 |
1589 | 6 | Transaction Company Bill To | TRCOMPANY | CHAR | 30 |
1590 | 7 | Transaction Contact Bill To | TRNAME | CHAR | 30 |
1591 | 8 | Additional Address Unit Info | TRADDR1 | CHAR | 30 |
1592 | 9 | Bill To Street Address | TRADDR2 | CHAR | 30 |
1593 | 10 | Ancillary Billing Information | TRADDR3 | CHAR | 30 |
1594 | 11 | Transaction City Bill To | TRCITY | CHAR | 20 |
1595 | 12 | Transaction State Bill To | TRSTATE | CHAR | 2 |
1596 | 13 | Bill Cycle Close Date | CLOSEDATE | CHAR | 10 |
1597 | 14 | Bill Due Date | DUEDATE | CHAR | 10 |
1598 | 15 | Previous Balance | BALFWD | NUM* | 9 |
1599 | 16 | Pmt/CR Applied | CREDAPPLY | NUM* | 9 |
1600 | 17 | Total Current Charges | CURRENTCHG | NUM* | 9 |
1601 | 18 | Total Amt Due | TOTALDUE | NUM* | 9 |
1602 | 19 | Total Amt Due | AMTDUE | NUM* | 9 |
1603 | 20 | 30 Day Aging | AMT30 | NUM* | 9 |
1604 | 21 | 60 Day Aging | AMT60 | NUM* | 9 |
1605 | 22 | 90 Day Aging | AMT90 | NUM* | 9 |
1606 | 23 | Y/N | AGESWITCH | CHAR | 1 |
1607 | 24 | Remittance automation | SCANLINE | CHAR | 100 |
1608 | 25 | Total Taxes & Fees | TAXTOT | NUM* | 9 |
1609 | 26 | Customer Reference Number | CUSTREF | CHAR | 15 |
1610 | 27 | Federal Tax*** | FEDTAX | NUM* | 9 |
1611 | 28 | State Tax*** | STATETAX | NUM* | 9 |
1612 | 29 | Other Taxes & Fees*** | OTHERTAX | NUM* | 9 |
1613 +-------+-------------------------------+------------+------+-------+
1615 If I<format> is "billco", the fields of the detail CSV file are as follows:
1617 FORMAT FOR DETAIL FILE
1619 Field | Description | Name | Type | Width
1620 1 | N/A-Leave Empty | RC | CHAR | 2
1621 2 | N/A-Leave Empty | CUSTID | CHAR | 15
1622 3 | Account Number | TRACCTNUM | CHAR | 15
1623 4 | Invoice Number | TRINVOICE | CHAR | 15
1624 5 | Line Sequence (sort order) | LINESEQ | NUM | 6
1625 6 | Transaction Detail | DETAILS | CHAR | 100
1626 7 | Amount | AMT | NUM* | 9
1627 8 | Line Format Control** | LNCTRL | CHAR | 2
1628 9 | Grouping Code | GROUP | CHAR | 2
1629 10 | User Defined | ACCT CODE | CHAR | 15
1631 If format is 'oneline', there is no detail file. Each invoice has a
1632 header line only, with the fields:
1634 Agent number, agent name, customer number, first name, last name, address
1635 line 1, address line 2, city, state, zip, invoice date, invoice number,
1636 amount charged, amount due, previous balance, due date.
1638 and then, for each line item, three columns containing the package number,
1639 description, and amount.
1641 If format is 'bridgestone', there is no detail file. Each invoice has a
1642 header line with the following fields in a fixed-width format:
1644 Customer number (in display format), date, name (first last), company,
1645 address 1, address 2, city, state, zip.
1647 This is a mailing list format, and has no per-invoice fields. To avoid
1648 sending redundant notices, the spooling event should have a "once" or
1649 "once_percust_every" condition.
1654 my($self, %opt) = @_;
1656 eval "use Text::CSV_XS";
1659 my $cust_main = $self->cust_main;
1661 my $csv = Text::CSV_XS->new({'always_quote'=>1});
1662 my $format = lc($opt{'format'});
1664 my $time = $opt{'time'} || time;
1666 my $tracctnum = ''; #leaking out from billco-specific sections :/
1667 if ( $format eq 'billco' ) {
1670 $self->conf->config('billco-account_num', $cust_main->agentnum);
1672 $tracctnum = $account_num eq 'display_custnum'
1673 ? $cust_main->display_custnum
1674 : $opt{'tracctnum'};
1677 $taxtotal += $_->{'amount'} foreach $self->_items_tax;
1679 my $duedate = $self->due_date2str('%m/%d/%Y'); # hardcoded, NOT date_format
1681 my( $previous_balance, @unused ) = $self->previous; #previous balance
1683 my $pmt_cr_applied = 0;
1684 $pmt_cr_applied += $_->{'amount'}
1685 foreach ( $self->_items_payments(%opt), $self->_items_credits(%opt) ) ;
1687 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1690 '', # 1 | N/A-Leave Empty CHAR 2
1691 '', # 2 | N/A-Leave Empty CHAR 15
1692 $tracctnum, # 3 | Transaction Account No CHAR 15
1693 $self->invnum, # 4 | Transaction Invoice No CHAR 15
1694 $cust_main->zip, # 5 | Transaction Zip Code CHAR 5
1695 $cust_main->company, # 6 | Transaction Company Bill To CHAR 30
1696 #$cust_main->payname, # 7 | Transaction Contact Bill To CHAR 30
1697 $cust_main->contact, # 7 | Transaction Contact Bill To CHAR 30
1698 $cust_main->address2, # 8 | Additional Address Unit Info CHAR 30
1699 $cust_main->address1, # 9 | Bill To Street Address CHAR 30
1700 '', # 10 | Ancillary Billing Information CHAR 30
1701 $cust_main->city, # 11 | Transaction City Bill To CHAR 20
1702 $cust_main->state, # 12 | Transaction State Bill To CHAR 2
1705 time2str("%m/%d/%Y", $self->_date), # 13 | Bill Cycle Close Date CHAR 10
1708 $duedate, # 14 | Bill Due Date CHAR 10
1710 $previous_balance, # 15 | Previous Balance NUM* 9
1711 $pmt_cr_applied, # 16 | Pmt/CR Applied NUM* 9
1712 sprintf("%.2f", $self->charged), # 17 | Total Current Charges NUM* 9
1713 $totaldue, # 18 | Total Amt Due NUM* 9
1714 $totaldue, # 19 | Total Amt Due NUM* 9
1715 '', # 20 | 30 Day Aging NUM* 9
1716 '', # 21 | 60 Day Aging NUM* 9
1717 '', # 22 | 90 Day Aging NUM* 9
1718 'N', # 23 | Y/N CHAR 1
1719 '', # 24 | Remittance automation CHAR 100
1720 $taxtotal, # 25 | Total Taxes & Fees NUM* 9
1721 $self->custnum, # 26 | Customer Reference Number CHAR 15
1722 '0', # 27 | Federal Tax*** NUM* 9
1723 sprintf("%.2f", $taxtotal), # 28 | State Tax*** NUM* 9
1724 '0', # 29 | Other Taxes & Fees*** NUM* 9
1727 } elsif ( $format eq 'oneline' ) { #name
1729 my ($previous_balance) = $self->previous;
1730 $previous_balance = sprintf('%.2f', $previous_balance);
1731 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1737 $self->_items_pkg, #_items_nontax? no sections or anything
1742 $cust_main->agentnum,
1743 $cust_main->agent->agent,
1747 $cust_main->company,
1748 $cust_main->address1,
1749 $cust_main->address2,
1755 time2str("%x", $self->_date),
1760 $self->due_date2str("%x"),
1765 } elsif ( $format eq 'bridgestone' ) {
1767 # bypass the CSV stuff and just return this
1768 my $longdate = time2str('%B %d, %Y', $time); #current time, right?
1769 my $zip = $cust_main->zip;
1771 my $prefix = $self->conf->config('bridgestone-prefix', $cust_main->agentnum)
1775 "%-5s%-15s%-20s%-30s%-30s%-30s%-30s%-20s%-2s%-9s\n",
1777 $cust_main->display_custnum,
1779 uc(substr($cust_main->contact_firstlast,0,30)),
1780 uc(substr($cust_main->company ,0,30)),
1781 uc(substr($cust_main->address1 ,0,30)),
1782 uc(substr($cust_main->address2 ,0,30)),
1783 uc(substr($cust_main->city ,0,20)),
1784 uc($cust_main->state),
1790 } elsif ( $format eq 'ics' ) {
1792 my $bill = $cust_main->bill_location;
1793 my $zip = $bill->zip;
1797 if ( $zip =~ /^(\d{5})(\d{4})$/ ) {
1802 # minor false laziness with print_generic
1803 my ($previous_balance) = $self->previous;
1804 my $balance_due = $self->owed + $previous_balance;
1805 my $payment_total = sum(0, map { $_->{'amount'} } $self->_items_payments);
1806 my $credit_total = sum(0, map { $_->{'amount'} } $self->_items_credits);
1809 if ( $self->due_date and $time >= $self->due_date ) {
1810 $past_due = sprintf('Past due:$%0.2f Due Immediately', $balance_due);
1814 my $header = sprintf(
1815 '%-10s%-30s%-48s%-2s%-50s%-30s%-30s%-25s%-2s%-5s%-4s%-8s%-8s%-10s%-10s%-10s%-10s%-10s%-10s%-480s%-35s',
1816 $cust_main->display_custnum, #BID
1817 uc($cust_main->first), #FNAME
1818 uc($cust_main->last), #LNAME
1819 '00', #BATCH, should this ever be anything else?
1820 uc($cust_main->company), #COMP
1821 uc($bill->address1), #STREET1
1822 uc($bill->address2), #STREET2
1823 uc($bill->city), #CITY
1824 uc($bill->state), #STATE
1827 time2str('%Y%m%d', $self->_date), #BILL_DATE
1828 $self->due_date2str('%Y%m%d'), #DUE_DATE,
1829 ( map {sprintf('%0.2f', $_)}
1830 $balance_due, #AMNT_DUE
1831 $previous_balance, #PREV_BAL
1832 $payment_total, #PYMT_RCVD
1833 $credit_total, #CREDITS
1834 $previous_balance, #BEG_BAL--is this correct?
1835 $self->charged, #NEW_CHRG
1838 $past_due, #PAST_MSG
1842 my %svc_class = ('' => ''); # maybe cache this more persistently?
1844 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1846 my $show_pkgnum = $cust_bill_pkg->pkgnum || '';
1847 my $cust_pkg = $cust_bill_pkg->cust_pkg if $show_pkgnum;
1851 my @dates = ( $self->_date, undef );
1852 if ( my $prev = $cust_bill_pkg->previous_cust_bill_pkg ) {
1853 $dates[1] = $prev->sdate; #questionable
1856 # generate an 01 detail for each service
1857 my @svcs = $cust_pkg->h_cust_svc(@dates, 'I');
1858 foreach my $cust_svc ( @svcs ) {
1859 $show_pkgnum = ''; # hide it if we're showing svcnums
1861 my $svcpart = $cust_svc->svcpart;
1862 if (!exists($svc_class{$svcpart})) {
1863 my $classnum = $cust_svc->part_svc->classnum;
1864 my $part_svc_class = FS::part_svc_class->by_key($classnum)
1866 $svc_class{$svcpart} = $part_svc_class ?
1867 $part_svc_class->classname :
1871 my @h_label = $cust_svc->label(@dates, 'I');
1872 push @details, sprintf('01%-9s%-20s%-47s',
1874 $svc_class{$svcpart},
1877 } #foreach $cust_svc
1880 my $desc = $cust_bill_pkg->desc; # itemdesc or part_pkg.pkg
1881 if ($cust_bill_pkg->recur > 0) {
1882 $desc .= ' '.time2str('%d-%b-%Y', $cust_bill_pkg->sdate).' to '.
1883 time2str('%d-%b-%Y', $cust_bill_pkg->edate - 86400);
1885 push @details, sprintf('02%-6s%-60s%-10s',
1888 sprintf('%0.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
1890 } #foreach $cust_bill_pkg
1892 # Tag this row so that we know whether this is one page (1), two pages
1893 # (2), # or "big" (B). The tag will be stripped off before uploading.
1894 if ( scalar(@details) < 12 ) {
1896 } elsif ( scalar(@details) < 58 ) {
1902 return join('', $header, @details, "\n");
1910 time2str("%x", $self->_date),
1911 sprintf("%.2f", $self->charged),
1912 ( map { $cust_main->getfield($_) }
1913 qw( first last company address1 address2 city state zip country ) ),
1915 ) or die "can't create csv";
1918 my $header = $csv->string. "\n";
1921 if ( lc($opt{'format'}) eq 'billco' ) {
1924 my %items_opt = ( format => 'template',
1925 escape_function => sub { shift } );
1926 # I don't know what characters billco actually tolerates in spool entries.
1927 # Text::CSV will take care of delimiters, though.
1929 my @items = ( $self->_items_pkg(%items_opt),
1930 $self->_items_fee(%items_opt) );
1931 foreach my $item (@items) {
1933 my $description = $item->{'description'};
1934 if ( $item->{'_is_discount'} and exists($item->{ext_description}[0]) ) {
1935 $description .= ': ' . $item->{ext_description}[0];
1939 '', # 1 | N/A-Leave Empty CHAR 2
1940 '', # 2 | N/A-Leave Empty CHAR 15
1941 $tracctnum, # 3 | Account Number CHAR 15
1942 $self->invnum, # 4 | Invoice Number CHAR 15
1943 $lineseq++, # 5 | Line Sequence (sort order) NUM 6
1944 $description, # 6 | Transaction Detail CHAR 100
1945 $item->{'amount'}, # 7 | Amount NUM* 9
1946 '', # 8 | Line Format Control** CHAR 2
1947 '', # 9 | Grouping Code CHAR 2
1948 '', # 10 | User Defined CHAR 15
1951 $detail .= $csv->string. "\n";
1955 } elsif ( lc($opt{'format'}) eq 'oneline' ) {
1961 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1963 my($pkg, $setup, $recur, $sdate, $edate);
1964 if ( $cust_bill_pkg->pkgnum ) {
1966 ($pkg, $setup, $recur, $sdate, $edate) = (
1967 $cust_bill_pkg->part_pkg->pkg,
1968 ( $cust_bill_pkg->setup != 0
1969 ? sprintf("%.2f", $cust_bill_pkg->setup )
1971 ( $cust_bill_pkg->recur != 0
1972 ? sprintf("%.2f", $cust_bill_pkg->recur )
1974 ( $cust_bill_pkg->sdate
1975 ? time2str("%x", $cust_bill_pkg->sdate)
1977 ($cust_bill_pkg->edate
1978 ? time2str("%x", $cust_bill_pkg->edate)
1982 } else { #pkgnum tax
1983 next unless $cust_bill_pkg->setup != 0;
1984 $pkg = $cust_bill_pkg->desc;
1985 $setup = sprintf('%10.2f', $cust_bill_pkg->setup );
1986 ( $sdate, $edate ) = ( '', '' );
1992 ( map { '' } (1..11) ),
1993 ($pkg, $setup, $recur, $sdate, $edate)
1994 ) or die "can't create csv";
1996 $detail .= $csv->string. "\n";
2002 ( $header, $detail );
2008 Pays this invoice with a compliemntary payment. If there is an error,
2009 returns the error, otherwise returns false.
2015 my $cust_pay = new FS::cust_pay ( {
2016 'invnum' => $self->invnum,
2017 'paid' => $self->owed,
2020 'payinfo' => $self->cust_main->payinfo,
2028 Attempts to pay this invoice with a credit card payment via a
2029 Business::OnlinePayment realtime gateway. See
2030 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2031 for supported processors.
2037 $self->realtime_bop( 'CC', @_ );
2042 Attempts to pay this invoice with an electronic check (ACH) payment via a
2043 Business::OnlinePayment realtime gateway. See
2044 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2045 for supported processors.
2051 $self->realtime_bop( 'ECHECK', @_ );
2056 Attempts to pay this invoice with phone bill (LEC) payment via a
2057 Business::OnlinePayment realtime gateway. See
2058 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2059 for supported processors.
2065 $self->realtime_bop( 'LEC', @_ );
2069 my( $self, $method ) = (shift,shift);
2070 my $conf = $self->conf;
2073 my $cust_main = $self->cust_main;
2074 my $balance = $cust_main->balance;
2075 my $amount = ( $balance < $self->owed ) ? $balance : $self->owed;
2076 $amount = sprintf("%.2f", $amount);
2077 return "not run (balance $balance)" unless $amount > 0;
2079 my $description = 'Internet Services';
2080 if ( $conf->exists('business-onlinepayment-description') ) {
2081 my $dtempl = $conf->config('business-onlinepayment-description');
2083 my $agent_obj = $cust_main->agent
2084 or die "can't retreive agent for $cust_main (agentnum ".
2085 $cust_main->agentnum. ")";
2086 my $agent = $agent_obj->agent;
2087 my $pkgs = join(', ',
2088 map { $_->part_pkg->pkg }
2089 grep { $_->pkgnum } $self->cust_bill_pkg
2091 $description = eval qq("$dtempl");
2094 $cust_main->realtime_bop($method, $amount,
2095 'description' => $description,
2096 'invnum' => $self->invnum,
2097 #this didn't do what we want, it just calls apply_payments_and_credits
2099 'apply_to_invoice' => 1,
2102 #this changes application behavior: auto payments
2103 #triggered against a specific invoice are now applied
2104 #to that invoice instead of oldest open.
2110 =item batch_card OPTION => VALUE...
2112 Adds a payment for this invoice to the pending credit card batch (see
2113 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2114 runs the payment using a realtime gateway.
2119 my ($self, %options) = @_;
2120 my $cust_main = $self->cust_main;
2122 $options{invnum} = $self->invnum;
2124 $cust_main->batch_card(%options);
2127 sub _agent_template {
2129 $self->cust_main->agent_template;
2132 sub _agent_invoice_from {
2134 $self->cust_main->agent_invoice_from;
2137 =item invoice_barcode DIR_OR_FALSE
2139 Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
2140 it is taken as the temp directory where the PNG file will be generated and the
2141 PNG file name is returned. Otherwise, the PNG image itself is returned.
2145 sub invoice_barcode {
2146 my ($self, $dir) = (shift,shift);
2148 my $gdbar = new GD::Barcode('Code39',$self->invnum);
2149 die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
2150 my $gd = $gdbar->plot(Height => 30);
2153 my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX',
2157 ) or die "can't open temp file: $!\n";
2158 print $bh $gd->png or die "cannot write barcode to file: $!\n";
2159 my $png_file = $bh->filename;
2166 =item invnum_date_pretty
2168 Returns a string with the invoice number and date, for example:
2169 "Invoice #54 (3/20/2008)".
2171 Intended for back-end context, with regard to translation and date formatting.
2175 #note: this uses _date_pretty_unlocalized because _date_pretty is too expensive
2176 # for backend use (and also does the wrong thing, localizing for end customer
2177 # instead of backoffice configured date format)
2178 sub invnum_date_pretty {
2180 #$self->mt('Invoice #').
2181 'Invoice #'. #XXX should be translated ala web UI user (not invoice customer)
2182 $self->invnum. ' ('. $self->_date_pretty_unlocalized. ')';
2185 #sub _items_extra_usage_sections {
2187 # my $escape = shift;
2189 # my %sections = ();
2191 # my %usage_class = map{ $_->classname, $_ } qsearch('usage_class', {});
2192 # foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
2194 # next unless $cust_bill_pkg->pkgnum > 0;
2196 # foreach my $section ( keys %usage_class ) {
2198 # my $usage = $cust_bill_pkg->usage($section);
2200 # next unless $usage && $usage > 0;
2202 # $sections{$section} ||= 0;
2203 # $sections{$section} += $usage;
2209 # map { { 'description' => &{$escape}($_),
2210 # 'subtotal' => $sections{$_},
2211 # 'summarized' => '',
2212 # 'tax_section' => '',
2215 # sort {$usage_class{$a}->weight <=> $usage_class{$b}->weight} keys %sections;
2219 sub _items_extra_usage_sections {
2221 my $conf = $self->conf;
2229 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2231 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2232 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2233 next unless $cust_bill_pkg->pkgnum > 0;
2235 foreach my $classnum ( keys %usage_class ) {
2236 my $section = $usage_class{$classnum}->classname;
2237 $classnums{$section} = $classnum;
2239 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail($classnum) ) {
2240 my $amount = $detail->amount;
2241 next unless $amount && $amount > 0;
2243 $sections{$section} ||= { 'subtotal'=>0, 'calls'=>0, 'duration'=>0 };
2244 $sections{$section}{amount} += $amount; #subtotal
2245 $sections{$section}{calls}++;
2246 $sections{$section}{duration} += $detail->duration;
2248 my $desc = $detail->regionname;
2249 my $description = $desc;
2250 $description = substr($desc, 0, $maxlength). '...'
2251 if $format eq 'latex' && length($desc) > $maxlength;
2253 $lines{$section}{$desc} ||= {
2254 description => &{$escape}($description),
2255 #pkgpart => $part_pkg->pkgpart,
2256 pkgnum => $cust_bill_pkg->pkgnum,
2261 #unit_amount => $cust_bill_pkg->unitrecur,
2262 quantity => $cust_bill_pkg->quantity,
2263 product_code => 'N/A',
2264 ext_description => [],
2267 $lines{$section}{$desc}{amount} += $amount;
2268 $lines{$section}{$desc}{calls}++;
2269 $lines{$section}{$desc}{duration} += $detail->duration;
2275 my %sectionmap = ();
2276 foreach (keys %sections) {
2277 my $usage_class = $usage_class{$classnums{$_}};
2278 $sectionmap{$_} = { 'description' => &{$escape}($_),
2279 'amount' => $sections{$_}{amount}, #subtotal
2280 'calls' => $sections{$_}{calls},
2281 'duration' => $sections{$_}{duration},
2283 'tax_section' => '',
2284 'sort_weight' => $usage_class->weight,
2285 ( $usage_class->format
2286 ? ( map { $_ => $usage_class->$_($format) }
2287 qw( description_generator header_generator total_generator total_line_generator )
2294 my @sections = sort { $a->{sort_weight} <=> $b->{sort_weight} }
2298 foreach my $section ( keys %lines ) {
2299 foreach my $line ( keys %{$lines{$section}} ) {
2300 my $l = $lines{$section}{$line};
2301 $l->{section} = $sectionmap{$section};
2302 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2303 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2308 return(\@sections, \@lines);
2314 my $end = $self->_date;
2316 # start at date of previous invoice + 1 second or 0 if no previous invoice
2317 my $start = $self->scalar_sql("SELECT max(_date) FROM cust_bill WHERE custnum = ? and invnum != ?",$self->custnum,$self->invnum);
2318 $start = 0 if !$start;
2321 my $cust_main = $self->cust_main;
2322 my @pkgs = $cust_main->all_pkgs;
2323 my($num_activated,$num_deactivated,$num_portedin,$num_portedout,$minutes)
2326 foreach my $pkg ( @pkgs ) {
2327 my @h_cust_svc = $pkg->h_cust_svc($end);
2328 foreach my $h_cust_svc ( @h_cust_svc ) {
2329 next if grep {$_ eq $h_cust_svc->svcnum} @seen;
2330 next unless $h_cust_svc->part_svc->svcdb eq 'svc_phone';
2332 my $inserted = $h_cust_svc->date_inserted;
2333 my $deleted = $h_cust_svc->date_deleted;
2334 my $phone_inserted = $h_cust_svc->h_svc_x($inserted+5);
2336 $phone_deleted = $h_cust_svc->h_svc_x($deleted) if $deleted;
2338 # DID either activated or ported in; cannot be both for same DID simultaneously
2339 if ($inserted >= $start && $inserted <= $end && $phone_inserted
2340 && (!$phone_inserted->lnp_status
2341 || $phone_inserted->lnp_status eq ''
2342 || $phone_inserted->lnp_status eq 'native')) {
2345 else { # this one not so clean, should probably move to (h_)svc_phone
2346 my $phone_portedin = qsearchs( 'h_svc_phone',
2347 { 'svcnum' => $h_cust_svc->svcnum,
2348 'lnp_status' => 'portedin' },
2349 FS::h_svc_phone->sql_h_searchs($end),
2351 $num_portedin++ if $phone_portedin;
2354 # DID either deactivated or ported out; cannot be both for same DID simultaneously
2355 if($deleted >= $start && $deleted <= $end && $phone_deleted
2356 && (!$phone_deleted->lnp_status
2357 || $phone_deleted->lnp_status ne 'portingout')) {
2360 elsif($deleted >= $start && $deleted <= $end && $phone_deleted
2361 && $phone_deleted->lnp_status
2362 && $phone_deleted->lnp_status eq 'portingout') {
2366 # increment usage minutes
2367 if ( $phone_inserted ) {
2368 my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end,'billsec_sum'=>1);
2369 $minutes = $cdrs[0]->billsec_sum if scalar(@cdrs) == 1;
2372 warn "WARNING: no matching h_svc_phone insert record for insert time $inserted, svcnum " . $h_cust_svc->svcnum;
2375 # don't look at this service again
2376 push @seen, $h_cust_svc->svcnum;
2380 $minutes = sprintf("%d", $minutes);
2381 ("Activated: $num_activated Ported-In: $num_portedin Deactivated: "
2382 . "$num_deactivated Ported-Out: $num_portedout ",
2383 "Total Minutes: $minutes");
2386 sub _items_accountcode_cdr {
2391 my $section = { 'amount' => 0,
2394 'sort_weight' => '',
2396 'description' => 'Usage by Account Code',
2402 my %accountcodes = ();
2404 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2405 next unless $cust_bill_pkg->pkgnum > 0;
2407 my @header = $cust_bill_pkg->details_header;
2408 next unless scalar(@header);
2409 $section->{'header'} = join(',',@header);
2411 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2413 $section->{'header'} = $detail->formatted('format' => $format)
2414 if($detail->detail eq $section->{'header'});
2416 my $accountcode = $detail->accountcode;
2417 next unless $accountcode;
2419 my $amount = $detail->amount;
2420 next unless $amount && $amount > 0;
2422 $accountcodes{$accountcode} ||= {
2423 description => $accountcode,
2430 product_code => 'N/A',
2431 section => $section,
2432 ext_description => [ $section->{'header'} ],
2436 $section->{'amount'} += $amount;
2437 $accountcodes{$accountcode}{'amount'} += $amount;
2438 $accountcodes{$accountcode}{calls}++;
2439 $accountcodes{$accountcode}{duration} += $detail->duration;
2440 push @{$accountcodes{$accountcode}{detail_temp}}, $detail;
2444 foreach my $l ( values %accountcodes ) {
2445 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2446 my @sorted_detail = sort { $a->startdate <=> $b->startdate } @{$l->{detail_temp}};
2447 foreach my $sorted_detail ( @sorted_detail ) {
2448 push @{$l->{ext_description}}, $sorted_detail->formatted('format'=>$format);
2450 delete $l->{detail_temp};
2454 my @sorted_lines = sort { $a->{'description'} <=> $b->{'description'} } @lines;
2456 return ($section,\@sorted_lines);
2459 sub _items_svc_phone_sections {
2461 my $conf = $self->conf;
2469 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2471 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2472 $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
2474 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2475 next unless $cust_bill_pkg->pkgnum > 0;
2477 my @header = $cust_bill_pkg->details_header;
2478 next unless scalar(@header);
2480 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2482 my $phonenum = $detail->phonenum;
2483 next unless $phonenum;
2485 my $amount = $detail->amount;
2486 next unless $amount && $amount > 0;
2488 $sections{$phonenum} ||= { 'amount' => 0,
2491 'sort_weight' => -1,
2492 'phonenum' => $phonenum,
2494 $sections{$phonenum}{amount} += $amount; #subtotal
2495 $sections{$phonenum}{calls}++;
2496 $sections{$phonenum}{duration} += $detail->duration;
2498 my $desc = $detail->regionname;
2499 my $description = $desc;
2500 $description = substr($desc, 0, $maxlength). '...'
2501 if $format eq 'latex' && length($desc) > $maxlength;
2503 $lines{$phonenum}{$desc} ||= {
2504 description => &{$escape}($description),
2505 #pkgpart => $part_pkg->pkgpart,
2513 product_code => 'N/A',
2514 ext_description => [],
2517 $lines{$phonenum}{$desc}{amount} += $amount;
2518 $lines{$phonenum}{$desc}{calls}++;
2519 $lines{$phonenum}{$desc}{duration} += $detail->duration;
2521 my $line = $usage_class{$detail->classnum}->classname;
2522 $sections{"$phonenum $line"} ||=
2526 'sort_weight' => $usage_class{$detail->classnum}->weight,
2527 'phonenum' => $phonenum,
2528 'header' => [ @header ],
2530 $sections{"$phonenum $line"}{amount} += $amount; #subtotal
2531 $sections{"$phonenum $line"}{calls}++;
2532 $sections{"$phonenum $line"}{duration} += $detail->duration;
2534 $lines{"$phonenum $line"}{$desc} ||= {
2535 description => &{$escape}($description),
2536 #pkgpart => $part_pkg->pkgpart,
2544 product_code => 'N/A',
2545 ext_description => [],
2548 $lines{"$phonenum $line"}{$desc}{amount} += $amount;
2549 $lines{"$phonenum $line"}{$desc}{calls}++;
2550 $lines{"$phonenum $line"}{$desc}{duration} += $detail->duration;
2551 push @{$lines{"$phonenum $line"}{$desc}{ext_description}},
2552 $detail->formatted('format' => $format);
2557 my %sectionmap = ();
2558 my $simple = new FS::usage_class { format => 'simple' }; #bleh
2559 foreach ( keys %sections ) {
2560 my @header = @{ $sections{$_}{header} || [] };
2562 new FS::usage_class { format => 'usage_'. (scalar(@header) || 6). 'col' };
2563 my $summary = $sections{$_}{sort_weight} < 0 ? 1 : 0;
2564 my $usage_class = $summary ? $simple : $usage_simple;
2565 my $ending = $summary ? ' usage charges' : '';
2568 $gen_opt{label} = [ map{ &{$escape}($_) } @header ];
2570 $sectionmap{$_} = { 'description' => &{$escape}($_. $ending),
2571 'amount' => $sections{$_}{amount}, #subtotal
2572 'calls' => $sections{$_}{calls},
2573 'duration' => $sections{$_}{duration},
2575 'tax_section' => '',
2576 'phonenum' => $sections{$_}{phonenum},
2577 'sort_weight' => $sections{$_}{sort_weight},
2578 'post_total' => $summary, #inspire pagebreak
2580 ( map { $_ => $usage_class->$_($format, %gen_opt) }
2581 qw( description_generator
2584 total_line_generator
2591 my @sections = sort { $a->{phonenum} cmp $b->{phonenum} ||
2592 $a->{sort_weight} <=> $b->{sort_weight}
2597 foreach my $section ( keys %lines ) {
2598 foreach my $line ( keys %{$lines{$section}} ) {
2599 my $l = $lines{$section}{$line};
2600 $l->{section} = $sectionmap{$section};
2601 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2602 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2607 if($conf->exists('phone_usage_class_summary')) {
2608 # this only works with Latex
2612 # after this, we'll have only two sections per DID:
2613 # Calls Summary and Calls Detail
2614 foreach my $section ( @sections ) {
2615 if($section->{'post_total'}) {
2616 $section->{'description'} = 'Calls Summary: '.$section->{'phonenum'};
2617 $section->{'total_line_generator'} = sub { '' };
2618 $section->{'total_generator'} = sub { '' };
2619 $section->{'header_generator'} = sub { '' };
2620 $section->{'description_generator'} = '';
2621 push @newsections, $section;
2622 my %calls_detail = %$section;
2623 $calls_detail{'post_total'} = '';
2624 $calls_detail{'sort_weight'} = '';
2625 $calls_detail{'description_generator'} = sub { '' };
2626 $calls_detail{'header_generator'} = sub {
2627 return ' & Date/Time & Called Number & Duration & Price'
2628 if $format eq 'latex';
2631 $calls_detail{'description'} = 'Calls Detail: '
2632 . $section->{'phonenum'};
2633 push @newsections, \%calls_detail;
2637 # after this, each usage class is collapsed/summarized into a single
2638 # line under the Calls Summary section
2639 foreach my $newsection ( @newsections ) {
2640 if($newsection->{'post_total'}) { # this means Calls Summary
2641 foreach my $section ( @sections ) {
2642 next unless ($section->{'phonenum'} eq $newsection->{'phonenum'}
2643 && !$section->{'post_total'});
2644 my $newdesc = $section->{'description'};
2645 my $tn = $section->{'phonenum'};
2646 $newdesc =~ s/$tn//g;
2647 my $line = { ext_description => [],
2651 calls => $section->{'calls'},
2652 section => $newsection,
2653 duration => $section->{'duration'},
2654 description => $newdesc,
2655 amount => sprintf("%.2f",$section->{'amount'}),
2656 product_code => 'N/A',
2658 push @newlines, $line;
2663 # after this, Calls Details is populated with all CDRs
2664 foreach my $newsection ( @newsections ) {
2665 if(!$newsection->{'post_total'}) { # this means Calls Details
2666 foreach my $line ( @lines ) {
2667 next unless (scalar(@{$line->{'ext_description'}}) &&
2668 $line->{'section'}->{'phonenum'} eq $newsection->{'phonenum'}
2670 my @extdesc = @{$line->{'ext_description'}};
2672 foreach my $extdesc ( @extdesc ) {
2673 $extdesc =~ s/scriptsize/normalsize/g if $format eq 'latex';
2674 push @newextdesc, $extdesc;
2676 $line->{'ext_description'} = \@newextdesc;
2677 $line->{'section'} = $newsection;
2678 push @newlines, $line;
2683 return(\@newsections, \@newlines);
2686 return(\@sections, \@lines);
2690 =sub _items_usage_class_summary OPTIONS
2692 Returns a list of detail items summarizing the usage charges on this
2693 invoice. Each one will have 'amount', 'description' (the usage charge name),
2694 and 'usage_classnum'.
2696 OPTIONS can include 'escape' (a function to escape the descriptions).
2700 sub _items_usage_class_summary {
2704 my $escape = $opt{escape} || sub { $_[0] };
2705 my $money_char = $opt{money_char};
2706 my $invnum = $self->invnum;
2707 my @classes = qsearch({
2708 'table' => 'usage_class',
2709 'select' => 'classnum, classname, SUM(amount) AS amount,'.
2710 ' COUNT(*) AS calls, SUM(duration) AS duration',
2711 'addl_from' => ' LEFT JOIN cust_bill_pkg_detail USING (classnum)' .
2712 ' LEFT JOIN cust_bill_pkg USING (billpkgnum)',
2713 'extra_sql' => " WHERE cust_bill_pkg.invnum = $invnum".
2714 ' GROUP BY classnum, classname, weight'.
2715 ' HAVING (usage_class.disabled IS NULL OR SUM(amount) > 0)'.
2716 ' ORDER BY weight ASC',
2720 description => &{$escape}($self->mt('Usage Summary')),
2724 foreach my $class (@classes) {
2725 $section->{subtotal} += $class->get('amount');
2727 'description' => &{$escape}($class->classname),
2728 'amount' => $money_char.sprintf('%.2f', $class->get('amount')),
2729 'quantity' => $class->get('calls'),
2730 'duration' => $class->get('duration'),
2731 'usage_classnum' => $class->classnum,
2732 'section' => $section,
2735 $section->{subtotal} = $money_char.sprintf('%.2f', $section->{subtotal});
2739 sub _items_previous {
2741 my $conf = $self->conf;
2742 my $cust_main = $self->cust_main;
2743 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
2745 foreach ( @pr_cust_bill ) {
2746 my $date = $conf->exists('invoice_show_prior_due_date')
2747 ? 'due '. $_->due_date2str('short')
2748 : $self->time2str_local('short', $_->_date);
2750 'description' => $self->mt('Previous Balance, Invoice #'). $_->invnum. " ($date)",
2751 #'pkgpart' => 'N/A',
2753 'amount' => sprintf("%.2f", $_->owed),
2759 # 'description' => 'Previous Balance',
2760 # #'pkgpart' => 'N/A',
2761 # 'pkgnum' => 'N/A',
2762 # 'amount' => sprintf("%10.2f", $pr_total ),
2763 # 'ext_description' => [ map {
2764 # "Invoice ". $_->invnum.
2765 # " (". time2str("%x",$_->_date). ") ".
2766 # sprintf("%10.2f", $_->owed)
2767 # } @pr_cust_bill ],
2772 sub _items_credits {
2773 my( $self, %opt ) = @_;
2774 my $trim_len = $opt{'trim_len'} || 40;
2779 if ( $self->conf->exists('previous_balance-payments_since') ) {
2780 if ( $opt{'template'} eq 'statement' ) {
2781 # then the current bill is a "statement" (i.e. an invoice sent as
2782 # a payment receipt)
2783 # and in that case we want to see payments on or after THIS invoice
2784 @objects = qsearch('cust_credit', {
2785 'custnum' => $self->custnum,
2786 '_date' => {op => '>=', value => $self->_date},
2790 $date = $self->previous_bill->_date if $self->previous_bill;
2791 @objects = qsearch('cust_credit', {
2792 'custnum' => $self->custnum,
2793 '_date' => {op => '>=', value => $date},
2797 @objects = $self->cust_credited;
2800 foreach my $obj ( @objects ) {
2801 my $cust_credit = $obj->isa('FS::cust_credit') ? $obj : $obj->cust_credit;
2803 my $reason = substr($cust_credit->reason, 0, $trim_len);
2804 $reason .= '...' if length($reason) < length($cust_credit->reason);
2805 $reason = " ($reason) " if $reason;
2808 #'description' => 'Credit ref\#'. $_->crednum.
2809 # " (". time2str("%x",$_->cust_credit->_date) .")".
2811 'description' => $self->mt('Credit applied').' '.
2812 $self->time2str_local('short', $obj->_date). $reason,
2813 'amount' => sprintf("%.2f",$obj->amount),
2821 sub _items_payments {
2826 my $detailed = $self->conf->exists('invoice_payment_details');
2828 if ( $self->conf->exists('previous_balance-payments_since') ) {
2829 # then show payments dated on/after the previous bill...
2830 if ( $opt{'template'} eq 'statement' ) {
2831 # then the current bill is a "statement" (i.e. an invoice sent as
2832 # a payment receipt)
2833 # and in that case we want to see payments on or after THIS invoice
2834 @objects = qsearch('cust_pay', {
2835 'custnum' => $self->custnum,
2836 '_date' => {op => '>=', value => $self->_date},
2839 # the normal case: payments on or after the previous invoice
2841 $date = $self->previous_bill->_date if $self->previous_bill;
2842 @objects = qsearch('cust_pay', {
2843 'custnum' => $self->custnum,
2844 '_date' => {op => '>=', value => $date},
2846 # and before the current bill...
2847 @objects = grep { $_->_date < $self->_date } @objects;
2850 @objects = $self->cust_bill_pay;
2853 foreach my $obj (@objects) {
2854 my $cust_pay = $obj->isa('FS::cust_pay') ? $obj : $obj->cust_pay;
2855 my $desc = $self->mt('Payment received').' '.
2856 $self->time2str_local('short', $cust_pay->_date );
2857 $desc .= $self->mt(' via ') .
2858 $cust_pay->payby_payinfo_pretty( $self->cust_main->locale )
2862 'description' => $desc,
2863 'amount' => sprintf("%.2f", $obj->amount )
2873 my $conf = $self->conf;
2876 my ($pr_total) = $self->previous;
2877 my ($previous_charges_desc, $new_charges_desc, $new_charges_amount);
2879 if ( $conf->exists('previous_balance-exclude_from_total') ) {
2880 # if enabled, specifically add a line for the previous balance total
2881 $previous_charges_desc = $self->mt(
2882 $conf->config('previous_balance-text') || 'Previous Balance'
2885 # then return separate lines for previous balance and total new charges
2888 { total_item => $previous_charges_desc,
2889 total_amount => sprintf('%.2f',$pr_total)
2894 if ( $conf->exists('previous_balance-exclude_from_total')
2895 or !$self->enable_previous ) {
2896 # show new charges only
2898 $new_charges_desc = $self->mt(
2899 $conf->config('previous_balance-text-total_new_charges')
2900 || 'Total New Charges'
2903 $new_charges_amount = $self->charged;
2906 # show new charges + previous invoice total
2908 $new_charges_desc = $self->mt('Total Charges');
2909 if ( $self->enable_previous ) {
2910 $new_charges_amount = sprintf('%.2f', $self->charged + $pr_total);
2912 $new_charges_amount = sprintf('%.2f', $self->charged);
2917 if ( $conf->exists('invoice_show_prior_due_date') ) {
2918 # then the due date should be shown with Total New Charges,
2919 # and should NOT be shown with the Balance Due message.
2920 if ( $self->due_date ) {
2921 # localize the "Please pay by" message and the date itself
2922 # (grammar issues with this, yeah)
2923 $new_charges_desc .= ' - ' . $self->mt('Please pay by') . ' ' .
2924 $self->due_date2str('short');
2925 } elsif ( $self->terms ) {
2926 # phrases like "due on receipt" should be localized
2927 $new_charges_desc .= ' - ' . $self->mt($self->terms);
2932 { total_item => $new_charges_desc,
2933 total_amount => $new_charges_amount,
2941 =item call_details [ OPTION => VALUE ... ]
2943 Returns an array of CSV strings representing the call details for this invoice
2944 The only option available is the boolean prepend_billed_number
2949 my ($self, %opt) = @_;
2951 my $format_function = sub { shift };
2953 if ($opt{prepend_billed_number}) {
2954 $format_function = sub {
2958 $row->amount ? $row->phonenum. ",". $detail : '"Billed number",'. $detail;
2963 my @details = map { $_->details( 'format_function' => $format_function,
2964 'escape_function' => sub{ return() },
2968 $self->cust_bill_pkg;
2969 my $header = $details[0];
2970 ( $header, grep { $_ ne $header } @details );
2980 =item process_reprint
2984 sub process_reprint {
2985 process_re_X('print', @_);
2988 =item process_reemail
2992 sub process_reemail {
2993 process_re_X('email', @_);
3001 process_re_X('fax', @_);
3009 process_re_X('ftp', @_);
3016 sub process_respool {
3017 process_re_X('spool', @_);
3020 use Storable qw(thaw);
3024 my( $method, $job ) = ( shift, shift );
3025 warn "$me process_re_X $method for job $job\n" if $DEBUG;
3027 my $param = thaw(decode_base64(shift));
3028 warn Dumper($param) if $DEBUG;
3038 # this is called from search/cust_bill.html and given all its search
3039 # parameters, so it needs to perform the same search.
3042 # spool_invoice ftp_invoice fax_invoice print_invoice
3043 my($method, $job, %param ) = @_;
3045 warn "re_X $method for job $job with param:\n".
3046 join( '', map { " $_ => ". $param{$_}. "\n" } keys %param );
3049 #some false laziness w/search/cust_bill.html
3050 $param{'order_by'} = 'cust_bill._date';
3052 my $query = FS::cust_bill->search(\%param);
3053 delete $query->{'count_query'};
3054 delete $query->{'count_addl'};
3056 $query->{debug} = 1; # was in here before, is obviously useful
3058 my @cust_bill = qsearch( $query );
3060 $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
3062 warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
3065 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
3066 foreach my $cust_bill ( @cust_bill ) {
3067 $cust_bill->$method();
3069 if ( $job ) { #progressbar foo
3071 if ( time - $min_sec > $last ) {
3072 my $error = $job->update_statustext(
3073 int( 100 * $num / scalar(@cust_bill) )
3075 die $error if $error;
3086 =head1 CLASS METHODS
3092 Returns an SQL fragment to retreive the amount owed (charged minus credited and paid).
3097 my ($class, $start, $end) = @_;
3099 $class->paid_sql($start, $end). ' - '.
3100 $class->credited_sql($start, $end);
3105 Returns an SQL fragment to retreive the net amount (charged minus credited).
3110 my ($class, $start, $end) = @_;
3111 'charged - '. $class->credited_sql($start, $end);
3116 Returns an SQL fragment to retreive the amount paid against this invoice.
3121 my ($class, $start, $end) = @_;
3122 $start &&= "AND cust_bill_pay._date <= $start";
3123 $end &&= "AND cust_bill_pay._date > $end";
3124 $start = '' unless defined($start);
3125 $end = '' unless defined($end);
3126 "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
3127 WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end )";
3132 Returns an SQL fragment to retreive the amount credited against this invoice.
3137 my ($class, $start, $end) = @_;
3138 $start &&= "AND cust_credit_bill._date <= $start";
3139 $end &&= "AND cust_credit_bill._date > $end";
3140 $start = '' unless defined($start);
3141 $end = '' unless defined($end);
3142 "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
3143 WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end )";
3148 Returns an SQL fragment to retrieve the due date of an invoice.
3149 Currently only supported on PostgreSQL.
3154 die "don't use: doesn't account for agent-specific invoice_default_terms";
3156 #we're passed a $conf but not a specific customer (that's in the query), so
3157 # to make this work we'd need an agentnum-aware "condition_sql_conf" like
3158 # "condition_sql_option" that retreives a conf value with SQL in an agent-
3161 my $conf = new FS::Conf;
3165 cust_bill.invoice_terms,
3166 cust_main.invoice_terms,
3167 \''.($conf->config('invoice_default_terms') || '').'\'
3168 ), E\'Net (\\\\d+)\'
3170 ) * 86400 + cust_bill._date'
3181 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
3182 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base