2 use base qw( FS::cust_bill::Search FS::Template_Mixin
3 FS::cust_main_Mixin FS::Record
7 use vars qw( $DEBUG $me );
10 use Fcntl qw(:flock); #for spool_csv
12 use List::Util qw(min max sum);
17 use Storable qw( freeze thaw );
19 use FS::UID qw( datasrc );
20 use FS::Misc qw( send_fax do_print );
21 use FS::Record qw( qsearch qsearchs dbh );
22 use FS::cust_statement;
23 use FS::cust_bill_pkg;
24 use FS::cust_bill_pkg_display;
25 use FS::cust_bill_pkg_detail;
29 use FS::cust_credit_bill;
33 use FS::cust_bill_pay;
36 use FS::cust_bill_batch;
37 use FS::cust_bill_pay_pkg;
38 use FS::cust_credit_bill_pkg;
39 use FS::discount_plan;
40 use FS::cust_bill_void;
46 $me = '[FS::cust_bill]';
50 FS::cust_bill - Object methods for cust_bill records
56 $record = new FS::cust_bill \%hash;
57 $record = new FS::cust_bill { 'column' => 'value' };
59 $error = $record->insert;
61 $error = $new_record->replace($old_record);
63 $error = $record->delete;
65 $error = $record->check;
67 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
69 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
71 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
73 @cust_pay_objects = $cust_bill->cust_pay;
75 $tax_amount = $record->tax;
77 @lines = $cust_bill->print_text;
78 @lines = $cust_bill->print_text('time' => $time);
82 An FS::cust_bill object represents an invoice; a declaration that a customer
83 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
84 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
85 following fields are currently supported:
91 =item invnum - primary key (assigned automatically for new invoices)
93 =item custnum - customer (see L<FS::cust_main>)
95 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
96 L<Time::Local> and L<Date::Parse> for conversion functions.
98 =item charged - amount of this invoice
100 =item invoice_terms - optional terms override for this specific invoice
108 =item billing_balance - the customer's balance immediately before generating
109 this invoice. DEPRECATED. Use the L<FS::cust_main/balance_date> method
110 to determine the customer's balance at a specific time.
112 =item previous_balance - the customer's balance immediately after generating
113 the invoice before this one. DEPRECATED.
115 =item printed - formerly used to track the number of times an invoice had
116 been printed; no longer used.
124 =item closed - books closed flag, empty or `Y'
126 =item statementnum - invoice aggregation (see L<FS::cust_statement>)
128 =item agent_invid - legacy invoice number
130 =item promised_date - customer promised payment date, for collection
132 =item pending - invoice is still being generated, empty or 'Y'
142 Creates a new invoice. To add the invoice to the database, see L<"insert">.
143 Invoices are normally created by calling the bill method of a customer object
144 (see L<FS::cust_main>).
148 sub table { 'cust_bill'; }
149 sub template_conf { 'invoice_'; }
153 my $agentnum = $self->cust_main->agentnum;
154 my $tc = $self->template_conf;
156 $self->conf->exists($tc.'sections', $agentnum) ||
157 $self->conf->exists($tc.'sections_by_location', $agentnum);
160 # should be the ONLY occurrence of "Invoice" in invoice rendering code.
161 # (except email_subject and invnum_date_pretty)
164 $self->conf->config('notice_name') || 'Invoice'
167 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
168 sub cust_unlinked_msg {
170 "WARNING: can't find cust_main.custnum ". $self->custnum.
171 ' (cust_bill.invnum '. $self->invnum. ')';
176 Adds this invoice to the database ("Posts" the invoice). If there is an error,
177 returns the error, otherwise returns false.
183 warn "$me insert called\n" if $DEBUG;
185 local $SIG{HUP} = 'IGNORE';
186 local $SIG{INT} = 'IGNORE';
187 local $SIG{QUIT} = 'IGNORE';
188 local $SIG{TERM} = 'IGNORE';
189 local $SIG{TSTP} = 'IGNORE';
190 local $SIG{PIPE} = 'IGNORE';
192 my $oldAutoCommit = $FS::UID::AutoCommit;
193 local $FS::UID::AutoCommit = 0;
196 my $error = $self->SUPER::insert;
198 $dbh->rollback if $oldAutoCommit;
202 if ( $self->get('cust_bill_pkg') ) {
203 foreach my $cust_bill_pkg ( @{$self->get('cust_bill_pkg')} ) {
204 $cust_bill_pkg->invnum($self->invnum);
205 my $error = $cust_bill_pkg->insert;
207 $dbh->rollback if $oldAutoCommit;
208 return "can't create invoice line item: $error";
213 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
218 =item void [ REASON [ , REPROCESS_CDRS ] ]
220 Voids this invoice: deletes the invoice and adds a record of the voided invoice
221 to the FS::cust_bill_void table (and related tables starting from
222 FS::cust_bill_pkg_void).
228 my $reason = scalar(@_) ? shift : '';
229 my $reprocess_cdrs = scalar(@_) ? shift : '';
231 unless (ref($reason) || !$reason) {
232 $reason = FS::reason->new_or_existing(
234 'type' => 'Invoice void',
239 local $SIG{HUP} = 'IGNORE';
240 local $SIG{INT} = 'IGNORE';
241 local $SIG{QUIT} = 'IGNORE';
242 local $SIG{TERM} = 'IGNORE';
243 local $SIG{TSTP} = 'IGNORE';
244 local $SIG{PIPE} = 'IGNORE';
246 my $oldAutoCommit = $FS::UID::AutoCommit;
247 local $FS::UID::AutoCommit = 0;
250 my $cust_bill_void = new FS::cust_bill_void ( {
251 map { $_ => $self->get($_) } $self->fields
253 $cust_bill_void->reasonnum($reason->reasonnum) if $reason;
254 my $error = $cust_bill_void->insert;
256 $dbh->rollback if $oldAutoCommit;
260 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
261 my $error = $cust_bill_pkg->void($reason, $reprocess_cdrs);
263 $dbh->rollback if $oldAutoCommit;
268 $error = $self->_delete;
270 $dbh->rollback if $oldAutoCommit;
274 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
280 # removed docs entirely and renamed method to _delete to further indicate it is
281 # internal-only and discourage use
285 # DO NOT USE THIS METHOD. Instead, apply a credit against the invoice, or use
286 # the B<void> method.
288 # This is only for internal use by V<void>, which is what you should be using.
290 # DO NOT USE THIS METHOD. Whatever reason you think you have is almost certainly
291 # wrong. Use B<void>, that's what it is for. Really. This means you.
297 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
299 local $SIG{HUP} = 'IGNORE';
300 local $SIG{INT} = 'IGNORE';
301 local $SIG{QUIT} = 'IGNORE';
302 local $SIG{TERM} = 'IGNORE';
303 local $SIG{TSTP} = 'IGNORE';
304 local $SIG{PIPE} = 'IGNORE';
306 my $oldAutoCommit = $FS::UID::AutoCommit;
307 local $FS::UID::AutoCommit = 0;
310 foreach my $table (qw(
317 #cust_event # problematic
318 #cust_pay_batch # unnecessary
320 foreach my $linked ( $self->$table() ) {
321 my $error = $linked->delete;
323 $dbh->rollback if $oldAutoCommit;
330 my $error = $self->SUPER::delete(@_);
332 $dbh->rollback if $oldAutoCommit;
336 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
342 =item replace [ OLD_RECORD ]
344 You can, but probably shouldn't modify invoices...
346 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
347 supplied, replaces this record. If there is an error, returns the error,
348 otherwise returns false.
352 #replace can be inherited from Record.pm
354 # replace_check is now the preferred way to #implement replace data checks
355 # (so $object->replace() works without an argument)
358 my( $new, $old ) = ( shift, shift );
359 return "Can't modify closed invoice" if $old->closed =~ /^Y/i;
360 #return "Can't change _date!" unless $old->_date eq $new->_date;
361 return "Can't change _date" unless $old->_date == $new->_date;
362 return "Can't change charged" unless $old->charged == $new->charged
363 || $old->pending eq 'Y'
364 || $old->charged == 0
365 || $new->{'Hash'}{'cc_surcharge_replace_hack'};
371 =item add_cc_surcharge
377 sub add_cc_surcharge {
378 my ($self, $pkgnum, $amount) = (shift, shift, shift);
381 my $cust_bill_pkg = new FS::cust_bill_pkg({
382 'invnum' => $self->invnum,
386 $error = $cust_bill_pkg->insert;
387 return $error if $error;
389 $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
390 $self->charged($self->charged+$amount);
391 $error = $self->replace;
392 return $error if $error;
394 $self->apply_payments_and_credits;
400 Checks all fields to make sure this is a valid invoice. If there is an error,
401 returns the error, otherwise returns false. Called by the insert and replace
410 $self->ut_numbern('invnum')
411 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
412 || $self->ut_numbern('_date')
413 || $self->ut_money('charged')
414 || $self->ut_numbern('printed')
415 || $self->ut_enum('closed', [ '', 'Y' ])
416 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum' )
417 || $self->ut_numbern('agent_invid') #varchar?
418 || $self->ut_flag('pending')
420 return $error if $error;
422 $self->_date(time) unless $self->_date;
424 $self->printed(0) if $self->printed eq '';
431 Returns the displayed invoice number for this invoice: agent_invid if
432 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
438 if ( $self->agent_invid
439 && FS::Conf->new->exists('cust_bill-default_agent_invid') ) {
440 return $self->agent_invid;
442 return $self->invnum;
448 Returns the customer's last invoice before this one.
454 if ( !$self->get('previous_bill') ) {
455 $self->set('previous_bill', qsearchs({
456 'table' => 'cust_bill',
457 'hashref' => { 'custnum' => $self->custnum,
458 '_date' => { op=>'<', value=>$self->_date } },
459 'order_by' => 'ORDER BY _date DESC LIMIT 1',
462 $self->get('previous_bill');
467 Returns the customer's invoice that follows this one
473 if (!$self->get('following_bill')) {
474 $self->set('following_bill', qsearchs({
475 table => 'cust_bill',
477 custnum => $self->custnum,
478 invnum => { op => '>', value => $self->invnum },
480 order_by => 'ORDER BY invnum ASC LIMIT 1',
483 $self->get('following_bill');
488 Returns a list consisting of the total previous balance for this customer,
489 followed by the previous outstanding invoices (as FS::cust_bill objects also).
495 # simple memoize; we use this a lot
496 if (!$self->get('previous')) {
498 my @cust_bill = sort { $a->_date <=> $b->_date }
499 grep { $_->owed != 0 }
500 qsearch( 'cust_bill', { 'custnum' => $self->custnum,
501 #'_date' => { op=>'<', value=>$self->_date },
502 'invnum' => { op=>'<', value=>$self->invnum },
505 foreach ( @cust_bill ) { $total += $_->owed; }
506 $self->set('previous', [$total, @cust_bill]);
508 return @{ $self->get('previous') };
511 =item enable_previous
513 Whether to show the 'Previous Charges' section when printing this invoice.
514 The negation of the 'disable_previous_balance' config setting.
518 sub enable_previous {
520 my $agentnum = $self->cust_main->agentnum;
521 !$self->conf->exists('disable_previous_balance', $agentnum);
526 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
533 { 'table' => 'cust_bill_pkg',
534 'hashref' => { 'invnum' => $self->invnum },
535 'order_by' => 'ORDER BY billpkgnum', #important? otherwise we could use
536 # the AUTLOADED FK search. or should
537 # that default to ORDER by the pkey?
542 =item cust_bill_pkg_pkgnum PKGNUM
544 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice and
549 sub cust_bill_pkg_pkgnum {
550 my( $self, $pkgnum ) = @_;
552 { 'table' => 'cust_bill_pkg',
553 'hashref' => { 'invnum' => $self->invnum,
556 'order_by' => 'ORDER BY billpkgnum',
563 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
570 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
571 $self->cust_bill_pkg;
573 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
578 Returns true if any of the packages (or their definitions) corresponding to the
579 line items for this invoice have the no_auto flag set.
585 grep { $_->no_auto || $_->part_pkg->no_auto } $self->cust_pkg;
588 =item open_cust_bill_pkg
590 Returns the open line items for this invoice.
592 Note that cust_bill_pkg with both setup and recur fees are returned as two
593 separate line items, each with only one fee.
597 # modeled after cust_main::open_cust_bill
598 sub open_cust_bill_pkg {
601 # grep { $_->owed > 0 } $self->cust_bill_pkg
603 my %other = ( 'recur' => 'setup',
604 'setup' => 'recur', );
606 foreach my $field ( qw( recur setup )) {
607 push @open, map { $_->set( $other{$field}, 0 ); $_; }
608 grep { $_->owed($field) > 0 }
609 $self->cust_bill_pkg;
617 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
621 #false laziness w/cust_pkg.pm
625 'table' => 'cust_event',
626 'addl_from' => 'JOIN part_event USING ( eventpart )',
627 'hashref' => { 'tablenum' => $self->invnum },
628 'extra_sql' => " AND eventtable = 'cust_bill' ",
634 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
638 #false laziness w/cust_pkg.pm
642 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
643 " WHERE tablenum = ? AND eventtable = 'cust_bill'";
644 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
645 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
646 $sth->fetchrow_arrayref->[0];
651 Returns the customer (see L<FS::cust_main>) for this invoice.
655 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this invoice
657 Returns a list: an empty list on success or a list of errors.
664 grep { $_->suspend(@_) }
665 grep {! $_->getfield('cancel') }
670 =item cust_suspend_if_balance_over AMOUNT
672 Suspends the customer associated with this invoice if the total amount owed on
673 this invoice and all older invoices is greater than the specified amount.
675 Returns a list: an empty list on success or a list of errors.
679 sub cust_suspend_if_balance_over {
680 my( $self, $amount ) = ( shift, shift );
681 my $cust_main = $self->cust_main;
682 if ( $cust_main->total_owed_date($self->_date) < $amount ) {
685 $cust_main->suspend(@_);
691 Cancel the packages on this invoice. Largely similar to the cust_main version, but does not bother yet with banned payment options
696 my( $self, %opt ) = @_;
698 warn "$me cancel called on cust_bill ". $self->invnum . " with options ".
699 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
702 return ( 'Access denied' )
703 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
705 my @pkgs = $self->cust_pkg;
707 if ( !$opt{nobill} && $self->conf->exists('bill_usage_on_cancel') ) {
709 my $error = $self->cust_main->bill( pkg_list => [ @pkgs ], cancel => 1 );
710 warn "Error billing during cancel, custnum ". $self->custnum. ": $error"
715 map { $_->cancel(%opt) }
716 grep { ! $_->getfield('cancel') }
722 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
728 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
729 sort { $a->_date <=> $b->_date }
730 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
735 =item cust_credit_bill
737 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
743 map { $_ } #return $self->num_cust_credit_bill unless wantarray;
744 sort { $a->_date <=> $b->_date }
745 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
749 sub cust_credit_bill {
750 shift->cust_credited(@_);
753 #=item cust_bill_pay_pkgnum PKGNUM
755 #Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
756 #with matching pkgnum.
760 #sub cust_bill_pay_pkgnum {
761 # my( $self, $pkgnum ) = @_;
762 # map { $_ } #return $self->num_cust_bill_pay_pkgnum($pkgnum) unless wantarray;
763 # sort { $a->_date <=> $b->_date }
764 # qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum,
765 # 'pkgnum' => $pkgnum,
770 =item cust_bill_pay_pkg PKGNUM
772 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
773 applied against the matching pkgnum.
777 sub cust_bill_pay_pkg {
778 my( $self, $pkgnum ) = @_;
781 'select' => 'cust_bill_pay_pkg.*',
782 'table' => 'cust_bill_pay_pkg',
783 'addl_from' => ' LEFT JOIN cust_bill_pay USING ( billpaynum ) '.
784 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
785 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
786 " AND cust_bill_pkg.pkgnum = $pkgnum",
791 #=item cust_credited_pkgnum PKGNUM
793 #=item cust_credit_bill_pkgnum PKGNUM
795 #Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice
796 #with matching pkgnum.
800 #sub cust_credited_pkgnum {
801 # my( $self, $pkgnum ) = @_;
802 # map { $_ } #return $self->num_cust_credit_bill_pkgnum($pkgnum) unless wantarray;
803 # sort { $a->_date <=> $b->_date }
804 # qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum,
805 # 'pkgnum' => $pkgnum,
810 #sub cust_credit_bill_pkgnum {
811 # shift->cust_credited_pkgnum(@_);
814 =item cust_credit_bill_pkg PKGNUM
816 Returns all credit applications (see L<FS::cust_credit_bill>) for this invoice
817 applied against the matching pkgnum.
821 sub cust_credit_bill_pkg {
822 my( $self, $pkgnum ) = @_;
825 'select' => 'cust_credit_bill_pkg.*',
826 'table' => 'cust_credit_bill_pkg',
827 'addl_from' => ' LEFT JOIN cust_credit_bill USING ( creditbillnum ) '.
828 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
829 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
830 " AND cust_bill_pkg.pkgnum = $pkgnum",
835 =item cust_bill_batch
837 Returns all invoice batch records (L<FS::cust_bill_batch>) for this invoice.
841 sub cust_bill_batch {
843 qsearch('cust_bill_batch', { 'invnum' => $self->invnum });
848 Returns all discount plans (L<FS::discount_plan>) for this invoice, as a
849 hash keyed by term length.
855 FS::discount_plan->all($self);
860 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
867 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
869 foreach (@taxlines) { $total += $_->setup; }
875 Returns the amount owed (still outstanding) on this invoice, which is charged
876 minus all payment applications (see L<FS::cust_bill_pay>) and credit
877 applications (see L<FS::cust_credit_bill>).
883 my $balance = $self->charged;
884 $balance -= $_->amount foreach ( $self->cust_bill_pay );
885 $balance -= $_->amount foreach ( $self->cust_credited );
886 $balance = sprintf( "%.2f", $balance);
887 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
891 =item owed_on_invoice
893 Returns the amount to be displayed as the "Balance Due" on this
894 invoice. Amount returned depends on conf flags for invoicing
896 See L<FS::cust_bill::owed> for the true amount currently owed
900 sub owed_on_invoice {
903 #return $self->owed()
904 # unless $self->conf->exists('previous_balance-payments_since')
906 # Add charges from this invoice
907 my $owed = $self->charged();
909 # Add carried balances from previous invoices
910 # If previous items aren't to be displayed on the invoice,
911 # _items_previous() is aware of this and responds appropriately.
912 $owed += $_->{amount} for $self->_items_previous();
914 # Subtract payments and credits displayed on this invoice
915 $owed -= $_->{amount} for $self->_items_payments(), $self->_items_credits();
921 my( $self, $pkgnum ) = @_;
923 #my $balance = $self->charged;
925 $balance += $_->setup + $_->recur for $self->cust_bill_pkg_pkgnum($pkgnum);
927 $balance -= $_->amount for $self->cust_bill_pay_pkg($pkgnum);
928 $balance -= $_->amount for $self->cust_credit_bill_pkg($pkgnum);
930 $balance = sprintf( "%.2f", $balance);
931 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
937 Returns true if this invoice should be hidden. See the
938 selfservice-hide_invoices-taxclass configuraiton setting.
944 my $conf = $self->conf;
945 my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass')
947 my @cust_bill_pkg = $self->cust_bill_pkg;
948 my @part_pkg = grep $_, map $_->part_pkg, @cust_bill_pkg;
949 ! grep { $_->taxclass ne $hide_taxclass } @part_pkg;
952 =item apply_payments_and_credits [ OPTION => VALUE ... ]
954 Applies unapplied payments and credits to this invoice.
955 Payments with the no_auto_apply flag set will not be applied.
957 A hash of optional arguments may be passed. Currently "manual" is supported.
958 If true, a payment receipt is sent instead of a statement when
959 'payment_receipt_email' configuration option is set.
961 If there is an error, returns the error, otherwise returns false.
965 sub apply_payments_and_credits {
966 my( $self, %options ) = @_;
967 my $conf = $self->conf;
969 local $SIG{HUP} = 'IGNORE';
970 local $SIG{INT} = 'IGNORE';
971 local $SIG{QUIT} = 'IGNORE';
972 local $SIG{TERM} = 'IGNORE';
973 local $SIG{TSTP} = 'IGNORE';
974 local $SIG{PIPE} = 'IGNORE';
976 my $oldAutoCommit = $FS::UID::AutoCommit;
977 local $FS::UID::AutoCommit = 0;
980 $self->select_for_update; #mutex
982 my @payments = grep { $_->unapplied > 0 }
983 grep { !$_->no_auto_apply }
984 $self->cust_main->cust_pay;
985 my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit;
987 if ( $conf->exists('pkg-balances') ) {
988 # limit @payments & @credits to those w/ a pkgnum grepped from $self
989 my %pkgnums = map { $_ => 1 } map $_->pkgnum, $self->cust_bill_pkg;
990 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
991 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
994 while ( $self->owed > 0 and ( @payments || @credits ) ) {
997 if ( @payments && @credits ) {
999 #decide which goes first by weight of top (unapplied) line item
1001 my @open_lineitems = $self->open_cust_bill_pkg;
1003 my $max_pay_weight =
1004 max( map { $_->part_pkg->pay_weight || 0 }
1006 map { $_->cust_pkg }
1009 my $max_credit_weight =
1010 max( map { $_->part_pkg->credit_weight || 0 }
1012 map { $_->cust_pkg }
1016 #if both are the same... payments first? it has to be something
1017 if ( $max_pay_weight >= $max_credit_weight ) {
1023 } elsif ( @payments ) {
1025 } elsif ( @credits ) {
1028 die "guru meditation #12 and 35";
1032 if ( $app eq 'pay' ) {
1034 my $payment = shift @payments;
1035 $unapp_amount = $payment->unapplied;
1036 $app = new FS::cust_bill_pay { 'paynum' => $payment->paynum };
1037 $app->pkgnum( $payment->pkgnum )
1038 if $conf->exists('pkg-balances') && $payment->pkgnum;
1040 } elsif ( $app eq 'credit' ) {
1042 my $credit = shift @credits;
1043 $unapp_amount = $credit->credited;
1044 $app = new FS::cust_credit_bill { 'crednum' => $credit->crednum };
1045 $app->pkgnum( $credit->pkgnum )
1046 if $conf->exists('pkg-balances') && $credit->pkgnum;
1049 die "guru meditation #12 and 35";
1053 if ( $conf->exists('pkg-balances') && $app->pkgnum ) {
1054 warn "owed_pkgnum ". $app->pkgnum;
1055 $owed = $self->owed_pkgnum($app->pkgnum);
1057 $owed = $self->owed;
1059 next unless $owed > 0;
1061 warn "min ( $unapp_amount, $owed )\n" if $DEBUG;
1062 $app->amount( sprintf('%.2f', min( $unapp_amount, $owed ) ) );
1064 $app->invnum( $self->invnum );
1066 my $error = $app->insert(%options);
1068 $dbh->rollback if $oldAutoCommit;
1069 return "Error inserting ". $app->table. " record: $error";
1071 die $error if $error;
1075 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1082 Sends this invoice to the destinations configured for this customer: sends
1083 email, prints and/or faxes. See L<FS::cust_main_invoice>.
1085 Options can be passed as a hashref. Positional parameters are no longer
1088 I<template>: a suffix for alternate invoices
1090 I<agentnum>: obsolete, now does nothing.
1092 I<from> overrides the default email invoice From: address.
1094 I<amount>: obsolete, does nothing
1096 I<notice_name> overrides "Invoice" as the name of the sent document
1097 (templates from 10/2009 or newer required).
1099 I<lpr> overrides the system 'lpr' option as the command to print a document
1100 from standard input.
1106 my $opt = ref($_[0]) ? $_[0] : +{ @_ };
1107 my $conf = $self->conf;
1109 my $cust_main = $self->cust_main;
1111 my @invoicing_list = $cust_main->invoicing_list;
1114 if ( grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list )
1115 && ! $cust_main->invoice_noemail;
1118 if grep { $_ eq 'POST' } @invoicing_list; #postal
1120 #this has never been used post-$ORIGINAL_ISP afaik
1121 $self->fax_invoice($opt)
1122 if grep { $_ eq 'FAX' } @invoicing_list; #fax
1130 my $opt = shift || {};
1131 if ($opt and !ref($opt)) {
1132 die ref($self). '->email called with positional parameters';
1135 my $conf = $self->conf;
1137 my $from = delete $opt->{from};
1139 # this is where we set the From: address
1140 $from ||= $self->_agent_invoice_from || #XXX should go away
1141 $conf->invoice_from_full( $self->cust_main->agentnum );
1143 my @invoicing_list = $self->cust_main->invoicing_list_emailonly;
1145 if ( ! @invoicing_list ) { #no recipients
1146 if ( $conf->exists('cust_bill-no_recipients-error') ) {
1147 die 'No recipients for customer #'. $self->custnum;
1149 #default: better to notify this person than silence
1150 @invoicing_list = ($from);
1154 $self->SUPER::email( {
1156 'to' => \@invoicing_list,
1162 #this stays here for now because its explicitly used as
1163 # FS::cust_bill::queueable_email
1164 sub queueable_email {
1167 my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1168 or die "invalid invoice number: " . $opt{invnum};
1170 $self->set('mode', $opt{mode})
1173 my %args = map {$_ => $opt{$_}}
1175 qw( from notice_name no_coupon template );
1177 my $error = $self->email( \%args );
1178 die $error if $error;
1184 my $conf = $self->conf;
1186 #my $template = scalar(@_) ? shift : '';
1189 my $subject = $conf->config('invoice_subject', $self->cust_main->agentnum)
1192 my $cust_main = $self->cust_main;
1193 my $name = $cust_main->name;
1194 my $name_short = $cust_main->name_short;
1195 my $invoice_number = $self->invnum;
1196 my $invoice_date = $self->_date_pretty;
1198 eval qq("$subject");
1203 'Invoice-'. $self->invnum. '.pdf';
1206 =item lpr_data HASHREF
1208 Returns the postscript or plaintext for this invoice as an arrayref.
1210 Options must be passed as a hashref. Positional parameters are no longer
1213 I<template>, if specified, is the name of a suffix for alternate invoices.
1215 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1221 my $conf = $self->conf;
1222 my $opt = shift || {};
1223 if ($opt and !ref($opt)) {
1224 # nobody does this anyway
1225 die "FS::cust_bill::lpr_data called with positional parameters";
1228 my $method = $conf->exists('invoice_latex') ? 'print_ps' : 'print_text';
1229 [ $self->$method( $opt ) ];
1234 Prints this invoice.
1236 Options must be passed as a hashref.
1238 I<template>, if specified, is the name of a suffix for alternate invoices.
1240 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1246 return if $self->hide;
1247 my $conf = $self->conf;
1248 my $opt = shift || {};
1249 if ($opt and !ref($opt)) {
1250 die "FS::cust_bill::print called with positional parameters";
1253 my $lpr = delete $opt->{lpr};
1254 if($conf->exists('invoice_print_pdf')) {
1255 # Add the invoice to the current batch.
1256 $self->batch_invoice($opt);
1260 $self->lpr_data($opt),
1261 'agentnum' => $self->cust_main->agentnum,
1267 =item fax_invoice HASHREF
1271 Options must be passed as a hashref.
1273 I<template>, if specified, is the name of a suffix for alternate invoices.
1275 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1281 return if $self->hide;
1282 my $conf = $self->conf;
1283 my $opt = shift || {};
1284 if ($opt and !ref($opt)) {
1285 die "FS::cust_bill::fax_invoice called with positional parameters";
1288 die 'FAX invoice destination not (yet?) supported with plain text invoices.'
1289 unless $conf->exists('invoice_latex');
1291 my $dialstring = $self->cust_main->getfield('fax');
1294 my $error = send_fax( 'docdata' => $self->lpr_data($opt),
1295 'dialstring' => $dialstring,
1297 die $error if $error;
1301 =item batch_invoice [ HASHREF ]
1303 Place this invoice into the open batch (see C<FS::bill_batch>). If there
1304 isn't an open batch, one will be created.
1306 HASHREF may contain any options to be passed to C<print_pdf>.
1311 my ($self, $opt) = @_;
1312 my $bill_batch = $self->get_open_bill_batch;
1313 my $cust_bill_batch = FS::cust_bill_batch->new({
1314 batchnum => $bill_batch->batchnum,
1315 invnum => $self->invnum,
1317 if ( $self->mode ) {
1318 $opt->{mode} ||= $self->mode;
1319 $opt->{mode} = $opt->{mode}->modenum if ref $opt->{mode};
1321 return $cust_bill_batch->insert($opt);
1324 =item get_open_batch
1326 Returns the currently open batch as an FS::bill_batch object, creating a new
1327 one if necessary. (A per-agent batch if invoice_print_pdf-spoolagent is
1332 sub get_open_bill_batch {
1334 my $conf = $self->conf;
1335 my $hashref = { status => 'O' };
1336 $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent')
1337 ? $self->cust_main->agentnum
1339 my $batch = qsearchs('bill_batch', $hashref);
1340 return $batch if $batch;
1341 $batch = FS::bill_batch->new($hashref);
1342 my $error = $batch->insert;
1343 die $error if $error;
1347 =item ftp_invoice [ TEMPLATENAME ]
1349 Sends this invoice data via FTP.
1351 TEMPLATENAME is unused?
1357 my $conf = $self->conf;
1358 my $template = scalar(@_) ? shift : '';
1361 'protocol' => 'ftp',
1362 'server' => $conf->config('cust_bill-ftpserver'),
1363 'username' => $conf->config('cust_bill-ftpusername'),
1364 'password' => $conf->config('cust_bill-ftppassword'),
1365 'dir' => $conf->config('cust_bill-ftpdir'),
1366 'format' => $conf->config('cust_bill-ftpformat'),
1370 =item spool_invoice [ TEMPLATENAME ]
1372 Spools this invoice data (see L<FS::spool_csv>)
1374 TEMPLATENAME is unused?
1380 my $conf = $self->conf;
1381 my $template = scalar(@_) ? shift : '';
1384 'format' => $conf->config('cust_bill-spoolformat'),
1385 'agent_spools' => $conf->exists('cust_bill-spoolagent'),
1389 =item send_csv OPTION => VALUE, ...
1391 Sends invoice as a CSV data-file to a remote host with the specified protocol.
1395 protocol - currently only "ftp"
1401 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
1402 and YYMMDDHHMMSS is a timestamp.
1404 See L</print_csv> for a description of the output format.
1409 my($self, %opt) = @_;
1413 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1414 mkdir $spooldir, 0700 unless -d $spooldir;
1416 # don't localize dates here, they're a defined format
1417 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1418 my $file = "$spooldir/$tracctnum.csv";
1420 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1422 open(CSV, ">$file") or die "can't open $file: $!";
1430 if ( $opt{protocol} eq 'ftp' ) {
1431 eval "use Net::FTP;";
1433 $net = Net::FTP->new($opt{server}) or die @$;
1435 die "unknown protocol: $opt{protocol}";
1438 $net->login( $opt{username}, $opt{password} )
1439 or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
1441 $net->binary or die "can't set binary mode";
1443 $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
1445 $net->put($file) or die "can't put $file: $!";
1455 Spools CSV invoice data.
1461 =item format - any of FS::Misc::::Invoicing::spool_formats
1463 =item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the
1464 customer has the corresponding invoice destinations set (see
1465 L<FS::cust_main_invoice>).
1467 =item agent_spools - if set to a true value, will spool to per-agent files
1468 rather than a single global file
1470 =item upload_targetnum - if set to a target (see L<FS::upload_target>), will
1471 append to that spool. L<FS::Cron::upload> will then send the spool file to
1474 =item balanceover - if set, only spools the invoice if the total amount owed on
1475 this invoice and all older invoices is greater than the specified amount.
1477 =item time - the "current time". Controls the printing of past due messages
1485 my($self, %opt) = @_;
1487 my $time = $opt{'time'} || time;
1488 my $cust_main = $self->cust_main;
1490 if ( $opt{'dest'} ) {
1491 my %invoicing_list = map { /^(POST|FAX)$/ or 'EMAIL' =~ /^(.*)$/; $1 => 1 }
1492 $cust_main->invoicing_list;
1493 return 'N/A' unless $invoicing_list{$opt{'dest'}}
1494 || ! keys %invoicing_list;
1497 if ( $opt{'balanceover'} ) {
1499 if $cust_main->total_owed_date($self->_date) < $opt{'balanceover'};
1502 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1503 mkdir $spooldir, 0700 unless -d $spooldir;
1505 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', $time);
1508 if ( $opt{'agent_spools'} ) {
1509 $file = 'agentnum'.$cust_main->agentnum;
1514 if ( $opt{'upload_targetnum'} ) {
1515 $spooldir .= '/target'.$opt{'upload_targetnum'};
1516 mkdir $spooldir, 0700 unless -d $spooldir;
1517 } # otherwise it just goes into export.xxx/cust_bill
1519 if ( lc($opt{'format'}) eq 'billco' ) {
1523 $file = "$spooldir/$file.csv";
1525 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum);
1527 open(CSV, ">>$file") or die "can't open $file: $!";
1528 flock(CSV, LOCK_EX);
1533 if ( lc($opt{'format'}) eq 'billco' ) {
1535 flock(CSV, LOCK_UN);
1538 $file =~ s/-header.csv$/-detail.csv/;
1540 open(CSV,">>$file") or die "can't open $file: $!";
1541 flock(CSV, LOCK_EX);
1545 print CSV $detail if defined($detail);
1547 flock(CSV, LOCK_UN);
1554 =item print_csv OPTION => VALUE, ...
1556 Returns CSV data for this invoice.
1560 format - 'default', 'billco', 'oneline', 'bridgestone'
1562 Returns a list consisting of two scalars. The first is a single line of CSV
1563 header information for this invoice. The second is one or more lines of CSV
1564 detail information for this invoice.
1566 If I<format> is not specified or "default", the fields of the CSV file are as
1569 record_type, invnum, custnum, _date, charged, first, last, company, address1,
1570 address2, city, state, zip, country, pkg, setup, recur, sdate, edate
1574 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
1576 B<record_type> is C<cust_bill> for the initial header line only. The
1577 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
1578 fields are filled in.
1580 B<record_type> is C<cust_bill_pkg> for detail lines. Only the first two fields
1581 (B<record_type> and B<invnum>) and the last five fields (B<pkg> through B<edate>)
1584 =item invnum - invoice number
1586 =item custnum - customer number
1588 =item _date - invoice date
1590 =item charged - total invoice amount
1592 =item first - customer first name
1594 =item last - customer first name
1596 =item company - company name
1598 =item address1 - address line 1
1600 =item address2 - address line 1
1610 =item pkg - line item description
1612 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
1614 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
1616 =item sdate - start date for recurring fee
1618 =item edate - end date for recurring fee
1622 If I<format> is "billco", the fields of the header CSV file are as follows:
1624 +-------------------------------------------------------------------+
1625 | FORMAT HEADER FILE |
1626 |-------------------------------------------------------------------|
1627 | Field | Description | Name | Type | Width |
1628 | 1 | N/A-Leave Empty | RC | CHAR | 2 |
1629 | 2 | N/A-Leave Empty | CUSTID | CHAR | 15 |
1630 | 3 | Transaction Account No | TRACCTNUM | CHAR | 15 |
1631 | 4 | Transaction Invoice No | TRINVOICE | CHAR | 15 |
1632 | 5 | Transaction Zip Code | TRZIP | CHAR | 5 |
1633 | 6 | Transaction Company Bill To | TRCOMPANY | CHAR | 30 |
1634 | 7 | Transaction Contact Bill To | TRNAME | CHAR | 30 |
1635 | 8 | Additional Address Unit Info | TRADDR1 | CHAR | 30 |
1636 | 9 | Bill To Street Address | TRADDR2 | CHAR | 30 |
1637 | 10 | Ancillary Billing Information | TRADDR3 | CHAR | 30 |
1638 | 11 | Transaction City Bill To | TRCITY | CHAR | 20 |
1639 | 12 | Transaction State Bill To | TRSTATE | CHAR | 2 |
1640 | 13 | Bill Cycle Close Date | CLOSEDATE | CHAR | 10 |
1641 | 14 | Bill Due Date | DUEDATE | CHAR | 10 |
1642 | 15 | Previous Balance | BALFWD | NUM* | 9 |
1643 | 16 | Pmt/CR Applied | CREDAPPLY | NUM* | 9 |
1644 | 17 | Total Current Charges | CURRENTCHG | NUM* | 9 |
1645 | 18 | Total Amt Due | TOTALDUE | NUM* | 9 |
1646 | 19 | Total Amt Due | AMTDUE | NUM* | 9 |
1647 | 20 | 30 Day Aging | AMT30 | NUM* | 9 |
1648 | 21 | 60 Day Aging | AMT60 | NUM* | 9 |
1649 | 22 | 90 Day Aging | AMT90 | NUM* | 9 |
1650 | 23 | Y/N | AGESWITCH | CHAR | 1 |
1651 | 24 | Remittance automation | SCANLINE | CHAR | 100 |
1652 | 25 | Total Taxes & Fees | TAXTOT | NUM* | 9 |
1653 | 26 | Customer Reference Number | CUSTREF | CHAR | 15 |
1654 | 27 | Federal Tax*** | FEDTAX | NUM* | 9 |
1655 | 28 | State Tax*** | STATETAX | NUM* | 9 |
1656 | 29 | Other Taxes & Fees*** | OTHERTAX | NUM* | 9 |
1657 +-------+-------------------------------+------------+------+-------+
1659 If I<format> is "billco", the fields of the detail CSV file are as follows:
1661 FORMAT FOR DETAIL FILE
1663 Field | Description | Name | Type | Width
1664 1 | N/A-Leave Empty | RC | CHAR | 2
1665 2 | N/A-Leave Empty | CUSTID | CHAR | 15
1666 3 | Account Number | TRACCTNUM | CHAR | 15
1667 4 | Invoice Number | TRINVOICE | CHAR | 15
1668 5 | Line Sequence (sort order) | LINESEQ | NUM | 6
1669 6 | Transaction Detail | DETAILS | CHAR | 100
1670 7 | Amount | AMT | NUM* | 9
1671 8 | Line Format Control** | LNCTRL | CHAR | 2
1672 9 | Grouping Code | GROUP | CHAR | 2
1673 10 | User Defined | ACCT CODE | CHAR | 15
1675 If format is 'oneline', there is no detail file. Each invoice has a
1676 header line only, with the fields:
1678 Agent number, agent name, customer number, first name, last name, address
1679 line 1, address line 2, city, state, zip, invoice date, invoice number,
1680 amount charged, amount due, previous balance, due date.
1682 and then, for each line item, three columns containing the package number,
1683 description, and amount.
1685 If format is 'bridgestone', there is no detail file. Each invoice has a
1686 header line with the following fields in a fixed-width format:
1688 Customer number (in display format), date, name (first last), company,
1689 address 1, address 2, city, state, zip.
1691 This is a mailing list format, and has no per-invoice fields. To avoid
1692 sending redundant notices, the spooling event should have a "once" or
1693 "once_percust_every" condition.
1698 my($self, %opt) = @_;
1700 eval "use Text::CSV_XS";
1703 my $cust_main = $self->cust_main;
1705 my $csv = Text::CSV_XS->new({'always_quote'=>1});
1706 my $format = lc($opt{'format'});
1708 my $time = $opt{'time'} || time;
1710 $self->set('_template', $opt{template})
1711 if exists $opt{template};
1713 my $tracctnum = ''; #leaking out from billco-specific sections :/
1714 if ( $format eq 'billco' ) {
1717 $self->conf->config('billco-account_num', $cust_main->agentnum);
1719 $tracctnum = $account_num eq 'display_custnum'
1720 ? $cust_main->display_custnum
1721 : $opt{'tracctnum'};
1724 $taxtotal += $_->{'amount'} foreach $self->_items_tax;
1726 my $duedate = $self->due_date2str('%m/%d/%Y'); # hardcoded, NOT date_format
1728 my( $previous_balance, @unused ) = $self->previous; #previous balance
1730 my $pmt_cr_applied = 0;
1731 $pmt_cr_applied += $_->{'amount'}
1732 foreach ( $self->_items_payments(%opt), $self->_items_credits(%opt) ) ;
1734 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1737 '', # 1 | N/A-Leave Empty CHAR 2
1738 '', # 2 | N/A-Leave Empty CHAR 15
1739 $tracctnum, # 3 | Transaction Account No CHAR 15
1740 $self->invnum, # 4 | Transaction Invoice No CHAR 15
1741 $cust_main->zip, # 5 | Transaction Zip Code CHAR 5
1742 $cust_main->company, # 6 | Transaction Company Bill To CHAR 30
1743 #$cust_main->payname, # 7 | Transaction Contact Bill To CHAR 30
1744 $cust_main->contact, # 7 | Transaction Contact Bill To CHAR 30
1745 $cust_main->address2, # 8 | Additional Address Unit Info CHAR 30
1746 $cust_main->address1, # 9 | Bill To Street Address CHAR 30
1747 '', # 10 | Ancillary Billing Information CHAR 30
1748 $cust_main->city, # 11 | Transaction City Bill To CHAR 20
1749 $cust_main->state, # 12 | Transaction State Bill To CHAR 2
1752 time2str("%m/%d/%Y", $self->_date), # 13 | Bill Cycle Close Date CHAR 10
1755 $duedate, # 14 | Bill Due Date CHAR 10
1757 $previous_balance, # 15 | Previous Balance NUM* 9
1758 $pmt_cr_applied, # 16 | Pmt/CR Applied NUM* 9
1759 sprintf("%.2f", $self->charged), # 17 | Total Current Charges NUM* 9
1760 $totaldue, # 18 | Total Amt Due NUM* 9
1761 $totaldue, # 19 | Total Amt Due NUM* 9
1762 '', # 20 | 30 Day Aging NUM* 9
1763 '', # 21 | 60 Day Aging NUM* 9
1764 '', # 22 | 90 Day Aging NUM* 9
1765 'N', # 23 | Y/N CHAR 1
1766 '', # 24 | Remittance automation CHAR 100
1767 $taxtotal, # 25 | Total Taxes & Fees NUM* 9
1768 $self->custnum, # 26 | Customer Reference Number CHAR 15
1769 '0', # 27 | Federal Tax*** NUM* 9
1770 sprintf("%.2f", $taxtotal), # 28 | State Tax*** NUM* 9
1771 '0', # 29 | Other Taxes & Fees*** NUM* 9
1774 } elsif ( $format eq 'oneline' ) { #name
1776 my ($previous_balance) = $self->previous;
1777 $previous_balance = sprintf('%.2f', $previous_balance);
1778 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1784 $self->_items_pkg, #_items_nontax? no sections or anything
1789 $cust_main->agentnum,
1790 $cust_main->agent->agent,
1794 $cust_main->company,
1795 $cust_main->address1,
1796 $cust_main->address2,
1802 time2str("%x", $self->_date),
1807 $self->due_date2str("%x"),
1812 } elsif ( $format eq 'bridgestone' ) {
1814 # bypass the CSV stuff and just return this
1815 my $longdate = time2str('%B %d, %Y', $time); #current time, right?
1816 my $zip = $cust_main->zip;
1818 my $prefix = $self->conf->config('bridgestone-prefix', $cust_main->agentnum)
1822 "%-5s%-15s%-20s%-30s%-30s%-30s%-30s%-20s%-2s%-9s\n",
1824 $cust_main->display_custnum,
1826 uc(substr($cust_main->contact_firstlast,0,30)),
1827 uc(substr($cust_main->company ,0,30)),
1828 uc(substr($cust_main->address1 ,0,30)),
1829 uc(substr($cust_main->address2 ,0,30)),
1830 uc(substr($cust_main->city ,0,20)),
1831 uc($cust_main->state),
1837 } elsif ( $format eq 'ics' ) {
1839 my $bill = $cust_main->bill_location;
1840 my $zip = $bill->zip;
1844 if ( $zip =~ /^(\d{5})(\d{4})$/ ) {
1849 # minor false laziness with print_generic
1850 my ($previous_balance) = $self->previous;
1851 my $balance_due = $self->owed + $previous_balance;
1852 my $payment_total = sum(0, map { $_->{'amount'} } $self->_items_payments);
1853 my $credit_total = sum(0, map { $_->{'amount'} } $self->_items_credits);
1856 if ( $self->due_date and $time >= $self->due_date ) {
1857 $past_due = sprintf('Past due:$%0.2f Due Immediately', $balance_due);
1861 my $header = sprintf(
1862 '%-10s%-30s%-48s%-2s%-50s%-30s%-30s%-25s%-2s%-5s%-4s%-8s%-8s%-10s%-10s%-10s%-10s%-10s%-10s%-480s%-35s',
1863 $cust_main->display_custnum, #BID
1864 uc($cust_main->first), #FNAME
1865 uc($cust_main->last), #LNAME
1866 '00', #BATCH, should this ever be anything else?
1867 uc($cust_main->company), #COMP
1868 uc($bill->address1), #STREET1
1869 uc($bill->address2), #STREET2
1870 uc($bill->city), #CITY
1871 uc($bill->state), #STATE
1874 time2str('%Y%m%d', $self->_date), #BILL_DATE
1875 $self->due_date2str('%Y%m%d'), #DUE_DATE,
1876 ( map {sprintf('%0.2f', $_)}
1877 $balance_due, #AMNT_DUE
1878 $previous_balance, #PREV_BAL
1879 $payment_total, #PYMT_RCVD
1880 $credit_total, #CREDITS
1881 $previous_balance, #BEG_BAL--is this correct?
1882 $self->charged, #NEW_CHRG
1885 $past_due, #PAST_MSG
1889 my %svc_class = ('' => ''); # maybe cache this more persistently?
1891 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1893 my $show_pkgnum = $cust_bill_pkg->pkgnum || '';
1894 my $cust_pkg = $cust_bill_pkg->cust_pkg if $show_pkgnum;
1898 my @dates = ( $self->_date, undef );
1899 if ( my $prev = $cust_bill_pkg->previous_cust_bill_pkg ) {
1900 $dates[1] = $prev->sdate; #questionable
1903 # generate an 01 detail for each service
1904 my @svcs = $cust_pkg->h_cust_svc(@dates, 'I');
1905 foreach my $cust_svc ( @svcs ) {
1906 $show_pkgnum = ''; # hide it if we're showing svcnums
1908 my $svcpart = $cust_svc->svcpart;
1909 if (!exists($svc_class{$svcpart})) {
1910 my $classnum = $cust_svc->part_svc->classnum;
1911 my $part_svc_class = FS::part_svc_class->by_key($classnum)
1913 $svc_class{$svcpart} = $part_svc_class ?
1914 $part_svc_class->classname :
1918 my @h_label = $cust_svc->label(@dates, 'I');
1919 push @details, sprintf('01%-9s%-20s%-47s',
1921 $svc_class{$svcpart},
1924 } #foreach $cust_svc
1927 my $desc = $cust_bill_pkg->desc; # itemdesc or part_pkg.pkg
1928 if ($cust_bill_pkg->recur > 0) {
1929 $desc .= ' '.time2str('%d-%b-%Y', $cust_bill_pkg->sdate).' to '.
1930 time2str('%d-%b-%Y', $cust_bill_pkg->edate - 86400);
1932 push @details, sprintf('02%-6s%-60s%-10s',
1935 sprintf('%0.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
1937 } #foreach $cust_bill_pkg
1939 # Tag this row so that we know whether this is one page (1), two pages
1940 # (2), # or "big" (B). The tag will be stripped off before uploading.
1941 if ( scalar(@details) < 12 ) {
1943 } elsif ( scalar(@details) < 58 ) {
1949 return join('', $header, @details, "\n");
1957 time2str("%x", $self->_date),
1958 sprintf("%.2f", $self->charged),
1959 ( map { $cust_main->getfield($_) }
1960 qw( first last company address1 address2 city state zip country ) ),
1962 ) or die "can't create csv";
1965 my $header = $csv->string. "\n";
1968 if ( lc($opt{'format'}) eq 'billco' ) {
1971 my %items_opt = ( format => 'template',
1972 escape_function => sub { shift } );
1973 # I don't know what characters billco actually tolerates in spool entries.
1974 # Text::CSV will take care of delimiters, though.
1976 my @items = ( $self->_items_pkg(%items_opt),
1977 $self->_items_fee(%items_opt) );
1978 foreach my $item (@items) {
1980 my $description = $item->{'description'};
1981 if ( $item->{'_is_discount'} and exists($item->{ext_description}[0]) ) {
1982 $description .= ': ' . $item->{ext_description}[0];
1986 '', # 1 | N/A-Leave Empty CHAR 2
1987 '', # 2 | N/A-Leave Empty CHAR 15
1988 $tracctnum, # 3 | Account Number CHAR 15
1989 $self->invnum, # 4 | Invoice Number CHAR 15
1990 $lineseq++, # 5 | Line Sequence (sort order) NUM 6
1991 $description, # 6 | Transaction Detail CHAR 100
1992 $item->{'amount'}, # 7 | Amount NUM* 9
1993 '', # 8 | Line Format Control** CHAR 2
1994 '', # 9 | Grouping Code CHAR 2
1995 '', # 10 | User Defined CHAR 15
1998 $detail .= $csv->string. "\n";
2002 } elsif ( lc($opt{'format'}) eq 'oneline' ) {
2008 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2010 my($pkg, $setup, $recur, $sdate, $edate);
2011 if ( $cust_bill_pkg->pkgnum ) {
2013 ($pkg, $setup, $recur, $sdate, $edate) = (
2014 $cust_bill_pkg->part_pkg->pkg,
2015 ( $cust_bill_pkg->setup != 0
2016 ? sprintf("%.2f", $cust_bill_pkg->setup )
2018 ( $cust_bill_pkg->recur != 0
2019 ? sprintf("%.2f", $cust_bill_pkg->recur )
2021 ( $cust_bill_pkg->sdate
2022 ? time2str("%x", $cust_bill_pkg->sdate)
2024 ($cust_bill_pkg->edate
2025 ? time2str("%x", $cust_bill_pkg->edate)
2029 } else { #pkgnum tax
2030 next unless $cust_bill_pkg->setup != 0;
2031 $pkg = $cust_bill_pkg->desc;
2032 $setup = sprintf('%10.2f', $cust_bill_pkg->setup );
2033 ( $sdate, $edate ) = ( '', '' );
2039 ( map { '' } (1..11) ),
2040 ($pkg, $setup, $recur, $sdate, $edate)
2041 ) or die "can't create csv";
2043 $detail .= $csv->string. "\n";
2049 ( $header, $detail );
2054 croak 'cust_bill->comp is deprecated (COMP payments are deprecated)';
2059 Attempts to pay this invoice with a credit card payment via a
2060 Business::OnlinePayment realtime gateway. See
2061 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2062 for supported processors.
2068 $self->realtime_bop( 'CC', @_ );
2073 Attempts to pay this invoice with an electronic check (ACH) payment via a
2074 Business::OnlinePayment realtime gateway. See
2075 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2076 for supported processors.
2082 $self->realtime_bop( 'ECHECK', @_ );
2087 Attempts to pay this invoice with phone bill (LEC) payment via a
2088 Business::OnlinePayment realtime gateway. See
2089 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2090 for supported processors.
2096 $self->realtime_bop( 'LEC', @_ );
2100 my( $self, $method ) = (shift,shift);
2101 my $conf = $self->conf;
2104 my $cust_main = $self->cust_main;
2105 my $balance = $cust_main->balance;
2106 my $amount = ( $balance < $self->owed ) ? $balance : $self->owed;
2107 $amount = sprintf("%.2f", $amount);
2108 return "not run (balance $balance)" unless $amount > 0;
2110 my $description = 'Internet Services';
2111 if ( $conf->exists('business-onlinepayment-description') ) {
2112 my $dtempl = $conf->config('business-onlinepayment-description');
2114 my $agent_obj = $cust_main->agent
2115 or die "can't retreive agent for $cust_main (agentnum ".
2116 $cust_main->agentnum. ")";
2117 my $agent = $agent_obj->agent;
2118 my $pkgs = join(', ',
2119 map { $_->part_pkg->pkg }
2120 grep { $_->pkgnum } $self->cust_bill_pkg
2122 $description = eval qq("$dtempl");
2125 $cust_main->realtime_bop($method, $amount,
2126 'description' => $description,
2127 'invnum' => $self->invnum,
2128 #this didn't do what we want, it just calls apply_payments_and_credits
2130 'apply_to_invoice' => 1,
2133 #this changes application behavior: auto payments
2134 #triggered against a specific invoice are now applied
2135 #to that invoice instead of oldest open.
2141 =item batch_card OPTION => VALUE...
2143 Adds a payment for this invoice to the pending credit card batch (see
2144 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2145 runs the payment using a realtime gateway.
2150 my ($self, %options) = @_;
2151 my $cust_main = $self->cust_main;
2153 $options{invnum} = $self->invnum;
2155 $cust_main->batch_card(%options);
2158 sub _agent_template {
2160 $self->cust_main->agent_template;
2163 sub _agent_invoice_from {
2165 $self->cust_main->agent_invoice_from;
2168 =item invoice_barcode DIR_OR_FALSE
2170 Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
2171 it is taken as the temp directory where the PNG file will be generated and the
2172 PNG file name is returned. Otherwise, the PNG image itself is returned.
2176 sub invoice_barcode {
2177 my ($self, $dir) = (shift,shift);
2179 my $gdbar = new GD::Barcode('Code39',$self->invnum);
2180 die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
2181 my $gd = $gdbar->plot(Height => 30);
2184 my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX',
2188 ) or die "can't open temp file: $!\n";
2189 print $bh $gd->png or die "cannot write barcode to file: $!\n";
2190 my $png_file = $bh->filename;
2197 =item invnum_date_pretty
2199 Returns a string with the invoice number and date, for example:
2200 "Invoice #54 (3/20/2008)".
2202 Intended for back-end context, with regard to translation and date formatting.
2206 #note: this uses _date_pretty_unlocalized because _date_pretty is too expensive
2207 # for backend use (and also does the wrong thing, localizing for end customer
2208 # instead of backoffice configured date format)
2209 sub invnum_date_pretty {
2211 #$self->mt('Invoice #').
2212 'Invoice #'. #XXX should be translated ala web UI user (not invoice customer)
2213 $self->invnum. ' ('. $self->_date_pretty_unlocalized. ')';
2216 #sub _items_extra_usage_sections {
2218 # my $escape = shift;
2220 # my %sections = ();
2222 # my %usage_class = map{ $_->classname, $_ } qsearch('usage_class', {});
2223 # foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
2225 # next unless $cust_bill_pkg->pkgnum > 0;
2227 # foreach my $section ( keys %usage_class ) {
2229 # my $usage = $cust_bill_pkg->usage($section);
2231 # next unless $usage && $usage > 0;
2233 # $sections{$section} ||= 0;
2234 # $sections{$section} += $usage;
2240 # map { { 'description' => &{$escape}($_),
2241 # 'subtotal' => $sections{$_},
2242 # 'summarized' => '',
2243 # 'tax_section' => '',
2246 # sort {$usage_class{$a}->weight <=> $usage_class{$b}->weight} keys %sections;
2250 sub _items_extra_usage_sections {
2252 my $conf = $self->conf;
2260 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2262 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2263 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2264 next unless $cust_bill_pkg->pkgnum > 0;
2266 foreach my $classnum ( keys %usage_class ) {
2267 my $section = $usage_class{$classnum}->classname;
2268 $classnums{$section} = $classnum;
2270 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail($classnum) ) {
2271 my $amount = $detail->amount;
2272 next unless $amount && $amount > 0;
2274 $sections{$section} ||= { 'subtotal'=>0, 'calls'=>0, 'duration'=>0 };
2275 $sections{$section}{amount} += $amount; #subtotal
2276 $sections{$section}{calls}++;
2277 $sections{$section}{duration} += $detail->duration;
2279 my $desc = $detail->regionname;
2280 my $description = $desc;
2281 $description = substr($desc, 0, $maxlength). '...'
2282 if $format eq 'latex' && length($desc) > $maxlength;
2284 $lines{$section}{$desc} ||= {
2285 description => &{$escape}($description),
2286 #pkgpart => $part_pkg->pkgpart,
2287 pkgnum => $cust_bill_pkg->pkgnum,
2292 #unit_amount => $cust_bill_pkg->unitrecur,
2293 quantity => $cust_bill_pkg->quantity,
2294 product_code => 'N/A',
2295 ext_description => [],
2298 $lines{$section}{$desc}{amount} += $amount;
2299 $lines{$section}{$desc}{calls}++;
2300 $lines{$section}{$desc}{duration} += $detail->duration;
2306 my %sectionmap = ();
2307 foreach (keys %sections) {
2308 my $usage_class = $usage_class{$classnums{$_}};
2309 $sectionmap{$_} = { 'description' => &{$escape}($_),
2310 'amount' => $sections{$_}{amount}, #subtotal
2311 'calls' => $sections{$_}{calls},
2312 'duration' => $sections{$_}{duration},
2314 'tax_section' => '',
2315 'sort_weight' => $usage_class->weight,
2316 ( $usage_class->format
2317 ? ( map { $_ => $usage_class->$_($format) }
2318 qw( description_generator header_generator total_generator total_line_generator )
2325 my @sections = sort { $a->{sort_weight} <=> $b->{sort_weight} }
2329 foreach my $section ( keys %lines ) {
2330 foreach my $line ( keys %{$lines{$section}} ) {
2331 my $l = $lines{$section}{$line};
2332 $l->{section} = $sectionmap{$section};
2333 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2334 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2339 return(\@sections, \@lines);
2345 my $end = $self->_date;
2347 # start at date of previous invoice + 1 second or 0 if no previous invoice
2348 my $start = $self->scalar_sql("SELECT max(_date) FROM cust_bill WHERE custnum = ? and invnum != ?",$self->custnum,$self->invnum);
2349 $start = 0 if !$start;
2352 my $cust_main = $self->cust_main;
2353 my @pkgs = $cust_main->all_pkgs;
2354 my($num_activated,$num_deactivated,$num_portedin,$num_portedout,$minutes)
2357 foreach my $pkg ( @pkgs ) {
2358 my @h_cust_svc = $pkg->h_cust_svc($end);
2359 foreach my $h_cust_svc ( @h_cust_svc ) {
2360 next if grep {$_ eq $h_cust_svc->svcnum} @seen;
2361 next unless $h_cust_svc->part_svc->svcdb eq 'svc_phone';
2363 my $inserted = $h_cust_svc->date_inserted;
2364 my $deleted = $h_cust_svc->date_deleted;
2365 my $phone_inserted = $h_cust_svc->h_svc_x($inserted+5);
2367 $phone_deleted = $h_cust_svc->h_svc_x($deleted) if $deleted;
2369 # DID either activated or ported in; cannot be both for same DID simultaneously
2370 if ($inserted >= $start && $inserted <= $end && $phone_inserted
2371 && (!$phone_inserted->lnp_status
2372 || $phone_inserted->lnp_status eq ''
2373 || $phone_inserted->lnp_status eq 'native')) {
2376 else { # this one not so clean, should probably move to (h_)svc_phone
2377 local($FS::Record::qsearch_qualify_columns) = 0;
2378 my $phone_portedin = qsearchs( 'h_svc_phone',
2379 { 'svcnum' => $h_cust_svc->svcnum,
2380 'lnp_status' => 'portedin' },
2381 FS::h_svc_phone->sql_h_searchs($end),
2383 $num_portedin++ if $phone_portedin;
2386 # DID either deactivated or ported out; cannot be both for same DID simultaneously
2387 if($deleted >= $start && $deleted <= $end && $phone_deleted
2388 && (!$phone_deleted->lnp_status
2389 || $phone_deleted->lnp_status ne 'portingout')) {
2392 elsif($deleted >= $start && $deleted <= $end && $phone_deleted
2393 && $phone_deleted->lnp_status
2394 && $phone_deleted->lnp_status eq 'portingout') {
2398 # increment usage minutes
2399 if ( $phone_inserted ) {
2400 my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end,'billsec_sum'=>1);
2401 $minutes = $cdrs[0]->billsec_sum if scalar(@cdrs) == 1;
2404 warn "WARNING: no matching h_svc_phone insert record for insert time $inserted, svcnum " . $h_cust_svc->svcnum;
2407 # don't look at this service again
2408 push @seen, $h_cust_svc->svcnum;
2412 $minutes = sprintf("%d", $minutes);
2413 ("Activated: $num_activated Ported-In: $num_portedin Deactivated: "
2414 . "$num_deactivated Ported-Out: $num_portedout ",
2415 "Total Minutes: $minutes");
2418 sub _items_accountcode_cdr {
2423 my $section = { 'amount' => 0,
2426 'sort_weight' => '',
2428 'description' => 'Usage by Account Code',
2434 my %accountcodes = ();
2436 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2437 next unless $cust_bill_pkg->pkgnum > 0;
2439 my @header = $cust_bill_pkg->details_header;
2440 next unless scalar(@header);
2441 $section->{'header'} = join(',',@header);
2443 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2445 $section->{'header'} = $detail->formatted('format' => $format)
2446 if($detail->detail eq $section->{'header'});
2448 my $accountcode = $detail->accountcode;
2449 next unless $accountcode;
2451 my $amount = $detail->amount;
2452 next unless $amount && $amount > 0;
2454 $accountcodes{$accountcode} ||= {
2455 description => $accountcode,
2462 product_code => 'N/A',
2463 section => $section,
2464 ext_description => [ $section->{'header'} ],
2468 $section->{'amount'} += $amount;
2469 $accountcodes{$accountcode}{'amount'} += $amount;
2470 $accountcodes{$accountcode}{calls}++;
2471 $accountcodes{$accountcode}{duration} += $detail->duration;
2472 push @{$accountcodes{$accountcode}{detail_temp}}, $detail;
2476 foreach my $l ( values %accountcodes ) {
2477 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2478 my @sorted_detail = sort { $a->startdate <=> $b->startdate } @{$l->{detail_temp}};
2479 foreach my $sorted_detail ( @sorted_detail ) {
2480 push @{$l->{ext_description}}, $sorted_detail->formatted('format'=>$format);
2482 delete $l->{detail_temp};
2486 my @sorted_lines = sort { $a->{'description'} <=> $b->{'description'} } @lines;
2488 return ($section,\@sorted_lines);
2491 sub _items_svc_phone_sections {
2493 my $conf = $self->conf;
2501 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40;
2503 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2504 $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
2506 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2507 next unless $cust_bill_pkg->pkgnum > 0;
2509 my @header = $cust_bill_pkg->details_header;
2510 next unless scalar(@header);
2512 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2514 my $phonenum = $detail->phonenum;
2515 next unless $phonenum;
2517 my $amount = $detail->amount;
2518 next unless $amount && $amount > 0;
2520 $sections{$phonenum} ||= { 'amount' => 0,
2523 'sort_weight' => -1,
2524 'phonenum' => $phonenum,
2526 $sections{$phonenum}{amount} += $amount; #subtotal
2527 $sections{$phonenum}{calls}++;
2528 $sections{$phonenum}{duration} += $detail->duration;
2530 my $desc = $detail->regionname;
2531 my $description = $desc;
2532 $description = substr($desc, 0, $maxlength). '...'
2533 if $format eq 'latex' && length($desc) > $maxlength;
2535 $lines{$phonenum}{$desc} ||= {
2536 description => &{$escape}($description),
2537 #pkgpart => $part_pkg->pkgpart,
2545 product_code => 'N/A',
2546 ext_description => [],
2549 $lines{$phonenum}{$desc}{amount} += $amount;
2550 $lines{$phonenum}{$desc}{calls}++;
2551 $lines{$phonenum}{$desc}{duration} += $detail->duration;
2553 my $line = $usage_class{$detail->classnum}->classname;
2554 $sections{"$phonenum $line"} ||=
2558 'sort_weight' => $usage_class{$detail->classnum}->weight,
2559 'phonenum' => $phonenum,
2560 'header' => [ @header ],
2562 $sections{"$phonenum $line"}{amount} += $amount; #subtotal
2563 $sections{"$phonenum $line"}{calls}++;
2564 $sections{"$phonenum $line"}{duration} += $detail->duration;
2566 $lines{"$phonenum $line"}{$desc} ||= {
2567 description => &{$escape}($description),
2568 #pkgpart => $part_pkg->pkgpart,
2576 product_code => 'N/A',
2577 ext_description => [],
2580 $lines{"$phonenum $line"}{$desc}{amount} += $amount;
2581 $lines{"$phonenum $line"}{$desc}{calls}++;
2582 $lines{"$phonenum $line"}{$desc}{duration} += $detail->duration;
2583 push @{$lines{"$phonenum $line"}{$desc}{ext_description}},
2584 $detail->formatted('format' => $format);
2589 my %sectionmap = ();
2590 my $simple = new FS::usage_class { format => 'simple' }; #bleh
2591 foreach ( keys %sections ) {
2592 my @header = @{ $sections{$_}{header} || [] };
2594 new FS::usage_class { format => 'usage_'. (scalar(@header) || 6). 'col' };
2595 my $summary = $sections{$_}{sort_weight} < 0 ? 1 : 0;
2596 my $usage_class = $summary ? $simple : $usage_simple;
2597 my $ending = $summary ? ' usage charges' : '';
2600 $gen_opt{label} = [ map{ &{$escape}($_) } @header ];
2602 $sectionmap{$_} = { 'description' => &{$escape}($_. $ending),
2603 'amount' => $sections{$_}{amount}, #subtotal
2604 'calls' => $sections{$_}{calls},
2605 'duration' => $sections{$_}{duration},
2607 'tax_section' => '',
2608 'phonenum' => $sections{$_}{phonenum},
2609 'sort_weight' => $sections{$_}{sort_weight},
2610 'post_total' => $summary, #inspire pagebreak
2612 ( map { $_ => $usage_class->$_($format, %gen_opt) }
2613 qw( description_generator
2616 total_line_generator
2623 my @sections = sort { $a->{phonenum} cmp $b->{phonenum} ||
2624 $a->{sort_weight} <=> $b->{sort_weight}
2629 foreach my $section ( keys %lines ) {
2630 foreach my $line ( keys %{$lines{$section}} ) {
2631 my $l = $lines{$section}{$line};
2632 $l->{section} = $sectionmap{$section};
2633 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2634 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2639 if($conf->exists('phone_usage_class_summary')) {
2640 # this only works with Latex
2644 # after this, we'll have only two sections per DID:
2645 # Calls Summary and Calls Detail
2646 foreach my $section ( @sections ) {
2647 if($section->{'post_total'}) {
2648 $section->{'description'} = 'Calls Summary: '.$section->{'phonenum'};
2649 $section->{'total_line_generator'} = sub { '' };
2650 $section->{'total_generator'} = sub { '' };
2651 $section->{'header_generator'} = sub { '' };
2652 $section->{'description_generator'} = '';
2653 push @newsections, $section;
2654 my %calls_detail = %$section;
2655 $calls_detail{'post_total'} = '';
2656 $calls_detail{'sort_weight'} = '';
2657 $calls_detail{'description_generator'} = sub { '' };
2658 $calls_detail{'header_generator'} = sub {
2659 return ' & Date/Time & Called Number & Duration & Price'
2660 if $format eq 'latex';
2663 $calls_detail{'description'} = 'Calls Detail: '
2664 . $section->{'phonenum'};
2665 push @newsections, \%calls_detail;
2669 # after this, each usage class is collapsed/summarized into a single
2670 # line under the Calls Summary section
2671 foreach my $newsection ( @newsections ) {
2672 if($newsection->{'post_total'}) { # this means Calls Summary
2673 foreach my $section ( @sections ) {
2674 next unless ($section->{'phonenum'} eq $newsection->{'phonenum'}
2675 && !$section->{'post_total'});
2676 my $newdesc = $section->{'description'};
2677 my $tn = $section->{'phonenum'};
2678 $newdesc =~ s/$tn//g;
2679 my $line = { ext_description => [],
2683 calls => $section->{'calls'},
2684 section => $newsection,
2685 duration => $section->{'duration'},
2686 description => $newdesc,
2687 amount => sprintf("%.2f",$section->{'amount'}),
2688 product_code => 'N/A',
2690 push @newlines, $line;
2695 # after this, Calls Details is populated with all CDRs
2696 foreach my $newsection ( @newsections ) {
2697 if(!$newsection->{'post_total'}) { # this means Calls Details
2698 foreach my $line ( @lines ) {
2699 next unless (scalar(@{$line->{'ext_description'}}) &&
2700 $line->{'section'}->{'phonenum'} eq $newsection->{'phonenum'}
2702 my @extdesc = @{$line->{'ext_description'}};
2704 foreach my $extdesc ( @extdesc ) {
2705 $extdesc =~ s/scriptsize/normalsize/g if $format eq 'latex';
2706 push @newextdesc, $extdesc;
2708 $line->{'ext_description'} = \@newextdesc;
2709 $line->{'section'} = $newsection;
2710 push @newlines, $line;
2715 return(\@newsections, \@newlines);
2718 return(\@sections, \@lines);
2722 =sub _items_usage_class_summary OPTIONS
2724 Returns a list of detail items summarizing the usage charges on this
2725 invoice. Each one will have 'amount', 'description' (the usage charge name),
2726 and 'usage_classnum'.
2728 OPTIONS can include 'escape' (a function to escape the descriptions).
2732 sub _items_usage_class_summary {
2736 my $escape = $opt{escape} || sub { $_[0] };
2737 my $money_char = $opt{money_char};
2738 my $invnum = $self->invnum;
2739 my @classes = qsearch({
2740 'table' => 'usage_class',
2741 'select' => 'classnum, classname, SUM(amount) AS amount,'.
2742 ' COUNT(*) AS calls, SUM(duration) AS duration',
2743 'addl_from' => ' LEFT JOIN cust_bill_pkg_detail USING (classnum)' .
2744 ' LEFT JOIN cust_bill_pkg USING (billpkgnum)',
2745 'extra_sql' => " WHERE cust_bill_pkg.invnum = $invnum".
2746 ' GROUP BY classnum, classname, weight'.
2747 ' HAVING (usage_class.disabled IS NULL OR SUM(amount) > 0)'.
2748 ' ORDER BY weight ASC',
2752 description => &{$escape}($self->mt('Usage Summary')),
2756 foreach my $class (@classes) {
2757 $section->{subtotal} += $class->get('amount');
2759 'description' => &{$escape}($class->classname),
2760 'amount' => $money_char.sprintf('%.2f', $class->get('amount')),
2761 'quantity' => $class->get('calls'),
2762 'duration' => $class->get('duration'),
2763 'usage_classnum' => $class->classnum,
2764 'section' => $section,
2767 $section->{subtotal} = $money_char.sprintf('%.2f', $section->{subtotal});
2771 =sub _items_previous()
2773 Returns an array of hashrefs, each hashref representing a line-item on
2774 the current bill for previous unpaid invoices.
2776 keys for each previous_item:
2777 - amount (see notes)
2783 Payments and credits shown on this invoice may vary based on configuraiton.
2785 when conf flag previous_balance-payments_since is set:
2786 This method works backwards to rebuild the invoice as a snapshot in time.
2787 The invoice displayed will have the balances owed, and payments made,
2788 reflecting the state of the account at the time of invoice generation.
2792 sub _items_previous {
2797 if ($self->get('_items_previous')) {
2798 return sort { $a->{_date} <=> $b->{_date} }
2799 values %{ $self->get('_items_previous') };
2802 # Gets the customer's current balance and outstanding invoices.
2803 my ($prev_balance, @open_invoices) = $self->previous;
2805 my %invoices = map {
2806 $_->invnum => $self->__items_previous_map_invoice($_)
2809 # Which credits and payments displayed on the bill will vary based on
2810 # conf flag previous_balance-payments_since.
2811 my @credits = $self->_items_credits();
2812 my @payments = $self->_items_payments();
2815 if ($self->conf->exists('previous_balance-payments_since')) {
2816 # For each credit or payment, determine which invoices it was applied to.
2817 # Manipulate data displayed so the invoice displayed appears as a
2818 # snapshot in time... with previous balances and balance owed displayed
2819 # as they were at the time of invoice creation.
2821 my @credits_postbill = $self->_items_credits_postbill();
2822 my @payments_postbill = $self->_items_payments_postbill();
2827 # Each section below follows this pattern on a payment/credit
2829 # - Dupe check, avoid adjusting for the same item twice
2830 # - If invoice being adjusted for isn't in our list, add it
2831 # - Adjust the invoice balance to refelct balnace without the
2832 # credit or payment applied
2835 # Working with payments displayed on this bill
2836 for my $pmt_hash (@payments) {
2837 my $pmt_obj = qsearchs('cust_pay', {paynum => $pmt_hash->{paynum}});
2838 for my $cust_bill_pay ($pmt_obj->cust_bill_pay) {
2839 next if exists $pmnt_dupechk{$cust_bill_pay->billpaynum};
2840 $pmnt_dupechk{$cust_bill_pay->billpaynum} = 1;
2842 my $invnum = $cust_bill_pay->invnum;
2844 $invoices{$invnum} = $self->__items_previous_get_invoice($invnum)
2845 unless exists $invoices{$invnum};
2847 $invoices{$invnum}->{amount} += $cust_bill_pay->amount;
2851 # Working with credits displayed on this bill
2852 for my $cred_hash (@credits) {
2853 my $cred_obj = qsearchs('cust_credit', {crednum => $cred_hash->{crednum}});
2854 for my $cust_credit_bill ($cred_obj->cust_credit_bill) {
2855 next if exists $cred_dupechk{$cust_credit_bill->creditbillnum};
2856 $cred_dupechk{$cust_credit_bill->creditbillnum} = 1;
2858 my $invnum = $cust_credit_bill->invnum;
2860 $invoices{$invnum} = $self->__items_previous_get_invoice($invnum)
2861 unless exists $invoices{$invnum};
2863 $invoices{$invnum}->{amount} += $cust_credit_bill->amount;
2867 # Working with both credits and payments which are not displayed
2868 # on this bill, but which have affected this bill's balances
2869 for my $postbill (@payments_postbill, @credits_postbill) {
2871 if ($postbill->{billpaynum}) {
2872 next if exists $pmnt_dupechk{$postbill->{billpaynum}};
2873 $pmnt_dupechk{$postbill->{billpaynum}} = 1;
2874 } elsif ($postbill->{creditbillnum}) {
2875 next if exists $cred_dupechk{$postbill->{creditbillnum}};
2876 $cred_dupechk{$postbill->{creditbillnum}} = 1;
2878 die "Missing creditbillnum or billpaynum";
2881 my $invnum = $postbill->{invnum};
2883 $invoices{$invnum} = $self->__items_previous_get_invoice($invnum)
2884 unless exists $invoices{$invnum};
2886 $invoices{$invnum}->{amount} += $postbill->{amount};
2889 # Make sure current invoice doesn't appear in previous items
2890 delete $invoices{$self->invnum}
2891 if exists $invoices{$self->invnum};
2895 # Make sure amount is formatted as a dollar string
2896 # (Formatting should happen on the template side, but is not?)
2897 $invoices{$_}->{amount} = sprintf('%.2f',$invoices{$_}->{amount})
2900 $self->set('_items_previous', \%invoices);
2901 return sort { $a->{_date} <=> $b->{_date} } values %invoices;
2905 =sub _items_previous_total
2907 Return sum of amounts from all items returned by _items_previous
2908 Results will vary based on invoicing conf flags
2912 sub _items_previous_total {
2915 $tot += $_->{amount} for $self->_items_previous();
2919 sub __items_previous_get_invoice {
2920 # Helper function for _items_previous
2922 # Read a record from cust_bill, return a hash of it's information
2923 my ($self, $invnum) = @_;
2924 die "Incorrect usage of __items_previous_get_invoice()" unless $invnum;
2926 my $cust_bill = qsearchs('cust_bill', {invnum => $invnum});
2927 return $self->__items_previous_map_invoice($cust_bill);
2930 sub __items_previous_map_invoice {
2931 # Helper function for _items_previous
2933 # Transform a cust_bill object into a simple hash reference of the type
2934 # required by _items_previous
2935 my ($self, $cust_bill) = @_;
2936 die "Incorrect usage of __items_previous_map_invoice" unless ref $cust_bill;
2938 my $date = $self->conf->exists('invoice_show_prior_due_date')
2939 ? 'due '.$cust_bill->due_date2str('short')
2940 : $self->time2str_local('short', $cust_bill->_date);
2943 invnum => $cust_bill->invnum,
2944 amount => $cust_bill->owed,
2946 _date => $cust_bill->_date,
2947 description => join(' ',
2948 $self->mt('Previous Balance, Invoice #'),
2955 =sub _items_credits()
2957 Return array of hashrefs containing credits to be shown as line-items
2958 when rendering this bill.
2960 keys for each credit item:
2961 - crednum: id of payment
2962 - amount: payment amount
2963 - description: line item to be displayed on the bill
2965 This method has three ways it selects which credits to display on
2968 1) Default Case: No Conf flag for 'previous_balance-payments_since'
2970 Returns credits that have been applied to this bill only
2973 Conf flag set for 'previous_balance-payments_since'
2975 List all credits that have been recorded during the time period
2976 between the timestamps of the last invoice and this invoice
2979 Conf flag set for 'previous_balance-payments_since'
2980 $opt{'template'} eq 'statement'
2982 List all payments that have been recorded between the timestamps
2983 of the previous invoice and the following invoice.
2985 This is used to give the customer a receipt for a payment
2986 in the form of their last bill with the payment amended.
2988 I am concerned with this implementation, but leaving in place as is
2989 If this option is selected, while viewing an older bill, the old bill
2990 will show ALL future credits for future bills, but no charges for
2991 future bills. Somebody could be misled into believing they have a
2992 large account credit when they don't. Also, interrupts the chain of
2993 invoices as an account history... the customer could have two invoices
2994 in their fileing cabinet, for two different dates, both with a line item
2995 for the same duplicate credit. The accounting is technically accurate,
2996 but somebody could easily become confused and think two credits were
2997 made, when really those two line items on two different bills represent
2998 only a single credit
3002 sub _items_credits {
3007 return @{$self->get('_items_credits')} if $self->get('_items_credits');
3010 my $template = $opt{template} || $self->get('_template');
3011 my $trim_len = $opt{template} || $self->get('trim_len') || 40;
3014 my @cust_credit_objs;
3016 if ($self->conf->exists('previous_balance-payments_since')) {
3017 if ($template eq 'statement') {
3018 # Case 3 (see above)
3019 # Return credits timestamped between the previous and following bills
3021 my $previous_bill = $self->previous_bill;
3022 my $following_bill = $self->following_bill;
3024 my $date_start = ref $previous_bill ? $previous_bill->_date : 0;
3025 my $date_end = ref $following_bill ? $following_bill->_date : undef;
3028 table => 'cust_credit',
3030 custnum => $self->custnum,
3031 _date => { op => '>=', value => $date_start },
3034 $query{extra_sql} = " AND _date <= $date_end " if $date_end;
3036 @cust_credit_objs = qsearch(\%query);
3039 # Case 2 (see above)
3040 # Return credits timestamps between this and the previous bills
3043 my $date_end = $self->_date;
3045 my $previous_bill = $self->previous_bill;
3046 if (ref $previous_bill) {
3047 $date_start = $previous_bill->_date;
3050 @cust_credit_objs = qsearch({
3051 table => 'cust_credit',
3053 custnum => $self->custnum,
3054 _date => {op => '>=', value => $date_start},
3056 extra_sql => " AND _date <= $date_end ",
3061 # Case 1 (see above)
3062 # Return only credits that have been applied to this bill
3064 @cust_credit_objs = $self->cust_credited;
3068 # Translate objects into hashrefs
3069 foreach my $obj ( @cust_credit_objs ) {
3070 my $cust_credit = $obj->isa('FS::cust_credit') ? $obj : $obj->cust_credit;
3072 amount => sprintf('%.2f',$cust_credit->amount),
3073 crednum => $cust_credit->crednum,
3074 _date => $cust_credit->_date,
3075 creditreason => $cust_credit->reason,
3078 my $reason = substr($cust_credit->reason, 0, $trim_len);
3079 $reason .= '...' if length($reason) < length($cust_credit->reason);
3080 $reason = "($reason)" if $reason;
3082 $r_obj{description} = join(' ',
3083 $self->mt('Credit applied'),
3084 $self->time2str_local('short', $cust_credit->_date),
3088 push @return, \%r_obj;
3090 $self->set('_items_credits',\@return);
3094 =sub _items_credits_total
3096 Return the total of al items from _items_credits
3097 Will vary based on invoice display conf flag
3101 sub _items_credits_total {
3104 $tot += $_->{amount} for $self->_items_credits();
3110 =sub _items_credits_postbill()
3112 Returns an array of hashrefs for credits where
3113 - Credit issued after this invoice
3114 - Credit applied to an invoice before this invoice
3116 Returned hashrefs are of the format returned by _items_credits()
3120 sub _items_credits_postbill {
3123 my @cust_credit_bill = qsearch({
3124 table => 'cust_credit_bill',
3125 select => join(', ',qw(
3126 cust_credit_bill.creditbillnum
3127 cust_credit_bill._date
3128 cust_credit_bill.invnum
3129 cust_credit_bill.amount
3131 addl_from => ' LEFT JOIN cust_credit'.
3132 ' ON (cust_credit_bill.crednum = cust_credit.crednum) ',
3133 extra_sql => ' WHERE cust_credit.custnum = '.$self->custnum.
3134 ' AND cust_credit_bill._date > '.$self->_date.
3135 ' AND cust_credit_bill.invnum < '.$self->invnum.' ',
3136 #! did not investigate why hashref doesn't work for this join query
3138 # 'cust_credit.custnum' => {op => '=', value => $self->custnum},
3139 # 'cust_credit_bill._date' => {op => '>', value => $self->_date},
3140 # 'cust_credit_bill.invnum' => {op => '<', value => $self->invnum},
3146 invnum => $_->invnum,
3147 amount => $_->amount,
3148 creditbillnum => $_->creditbillnum,
3149 }} @cust_credit_bill;
3152 =sub _items_payments_postbill()
3154 Returns an array of hashrefs for payments where
3155 - Payment occured after this invoice
3156 - Payment applied to an invoice before this invoice
3158 Returned hashrefs are of the format returned by _items_payments()
3162 sub _items_payments_postbill {
3165 my @cust_bill_pay = qsearch({
3166 table => 'cust_bill_pay',
3167 select => join(', ',qw(
3168 cust_bill_pay.billpaynum
3170 cust_bill_pay.invnum
3171 cust_bill_pay.amount
3173 addl_from => ' LEFT JOIN cust_bill'.
3174 ' ON (cust_bill_pay.invnum = cust_bill.invnum) ',
3175 extra_sql => ' WHERE cust_bill.custnum = '.$self->custnum.
3176 ' AND cust_bill_pay._date > '.$self->_date.
3177 ' AND cust_bill_pay.invnum < '.$self->invnum.' ',
3182 invnum => $_->invnum,
3183 amount => $_->amount,
3184 billpaynum => $_->billpaynum,
3188 =sub _items_payments()
3190 Return array of hashrefs containing payments to be shown as line-items
3191 when rendering this bill.
3193 keys for each payment item:
3194 - paynum: id of payment
3195 - amount: payment amount
3196 - description: line item to be displayed on the bill
3198 This method has three ways it selects which payments to display on
3201 1) Default Case: No Conf flag for 'previous_balance-payments_since'
3203 Returns payments that have been applied to this bill only
3206 Conf flag set for 'previous_balance-payments_since'
3208 List all payments that have been recorded between the timestamps
3209 of the previous invoice and this invoice
3212 Conf flag set for 'previous_balance-payments_since'
3213 $opt{'template'} eq 'statement'
3215 List all payments that have been recorded between the timestamps
3216 of the previous invoice and the following invoice.
3218 I am concerned with this implementation, but leaving in place as is
3219 If this option is selected, while viewing an older bill, the old bill
3220 will show ALL future payments for future bills, but no charges for
3221 future bills. Somebody could be misled into believing they have a
3222 large account credit when they don't. Also, interrupts the chain of
3223 invoices as an account history... the customer could have two invoices
3224 in their fileing cabinet, for two different dates, both with a line item
3225 for the same duplicate payment. The accounting is technically accurate,
3226 but somebody could easily become confused and think two payments were
3227 made, when really those two line items on two different bills represent
3228 only a single payment.
3232 sub _items_payments {
3237 return @{$self->get('_items_payments')} if $self->get('_items_payments');
3240 my $template = $opt{template} || $self->get('_template');
3245 my $c_invoice_payment_details = $self->conf->exists('invoice_payment_details');
3247 if ($self->conf->exists('previous_balance-payments_since')) {
3248 if ($template eq 'statement') {
3250 # Case 3 (see above)
3251 # Return payments timestamped between the previous and following bills
3253 my $previous_bill = $self->previous_bill;
3254 my $following_bill = $self->following_bill;
3256 my $date_start = ref $previous_bill ? $previous_bill->_date : 0;
3257 my $date_end = ref $following_bill ? $following_bill->_date : undef;
3260 table => 'cust_pay',
3262 custnum => $self->custnum,
3263 _date => { op => '>=', value => $date_start },
3266 $query{extra_sql} = " AND _date <= $date_end " if $date_end;
3268 @cust_pay_objs = qsearch(\%query);
3271 # Case 2 (see above)
3272 # Return payments timestamped between this and the previous bill
3275 my $date_end = $self->_date;
3277 my $previous_bill = $self->previous_bill;
3278 if (ref $previous_bill) {
3279 $date_start = $previous_bill->_date;
3282 @cust_pay_objs = qsearch({
3283 table => 'cust_pay',
3285 custnum => $self->custnum,
3286 _date => {op => '>=', value => $date_start},
3288 extra_sql => " AND _date <= $date_end ",
3293 # Case 1 (see above)
3294 # Return payments applied only to this bill
3296 @cust_pay_objs = $self->cust_bill_pay;
3302 [ $self->__items_payments_make_hashref(@cust_pay_objs) ]
3304 return @{ $self->get('_items_payments') };
3307 =sub _items_payments_total
3309 Return a total of all records returned by _items_payments
3310 Results vary based on invoicing conf flags
3314 sub _items_payments_total {
3317 $tot += $_->{amount} for $self->_items_payments();
3321 sub __items_payments_make_hashref {
3322 # Transform a FS::cust_pay object into a simple hashref for invoice
3323 my ($self, @cust_pay_objs) = @_;
3324 my $c_invoice_payment_details = $self->conf->exists('invoice_payment_details');
3327 for my $obj (@cust_pay_objs) {
3329 # In case we're passed FS::cust_bill_pay (or something else?)
3330 # Below, we use $obj to render amount rather than $cust_apy.
3331 # If we were passed cust_bill_pay objs, then:
3332 # $obj->amount represents the amount applied to THIS invoice
3333 # $cust_pay->amount represents the total payment, which may have
3334 # been applied accross several invoices.
3335 # If we were passed cust_bill_pay objects, then the conf flag
3336 # previous_balance-payments_since is NOT set, so we should not
3337 # present any payments not applied to this invoice.
3338 my $cust_pay = $obj->isa('FS::cust_pay') ? $obj : $obj->cust_pay;
3341 _date => $cust_pay->_date,
3342 amount => sprintf("%.2f", $obj->amount),
3343 paynum => $cust_pay->paynum,
3344 payinfo => $cust_pay->payby_payinfo_pretty(),
3345 description => join(' ',
3346 $self->mt('Payment received'),
3347 $self->time2str_local('short', $cust_pay->_date),
3351 if ($c_invoice_payment_details) {
3352 $r_obj{description} = join(' ',
3353 $r_obj{description},
3355 $cust_pay->payby_payinfo_pretty($self->cust_main->locale),
3359 push @return, \%r_obj;
3366 Generate the line-items to be shown on the bill in the "Totals" section
3368 Returns a list of hashrefs, each with the keys:
3369 - total_item: description field
3370 - total_amount: dollar-formatted number amount
3372 Information presented by this method varies based on Conf
3374 Conf previous_balance-payments_due
3375 - default, flag not set
3376 Only transactions that were applied to this bill bill be
3377 displayed and calculated intothe total. If items exist in
3378 the past-due section, those items will disappear from this
3379 invoice if they have been paid off.
3381 - previous_balance-payments_due flag is set
3382 Transactions occuring after the timestsamp of this
3383 invoice are not reflected on invoice line items
3385 Only payments/credits applied between the previous invoice
3386 and this one are displayed and calculated into the total
3388 - previous_balance-payments_due && $opt{template} eq 'statement'
3389 Same as above, except payments/credits occuring before the date
3390 of the following invoice are also displayed and calculated into
3393 Conf previous_balance-exclude_from_total
3394 - default, flag not set
3395 The "Totals" section contains a single line item.
3396 The dollar amount of this line items is a sum of old and new charges
3397 - previous_balance-exclude_from_total flag is set
3398 The "Totals" section contains two line items.
3399 One for previous balance, one for new charges
3400 !NOTE: Avent virtualization flag 'disable_previous_balance' can
3401 override the global conf flag previous_balance-exclude_from_total
3403 Conf invoice_show_prior_due_date
3404 - default, flag not set
3405 Total line item in the "Totals" section does not mention due date
3406 - invoice_show_prior_due_date flag is set
3407 Total line item in the "Totals" section includes either the due
3408 date of the invoice, or the specified invoice terms
3409 ? Not sure why this is called "Prior" due date, since we seem to be
3410 displaying THIS due date...
3415 my $conf = $self->conf;
3417 my $c_multi_line_total = 0;
3418 $c_multi_line_total = 1
3419 if $conf->exists('previous_balance-exclude_from_total')
3420 && $self->enable_previous();
3423 my $invoice_charges = $self->charged();
3425 # _items_previous() is aware of conf flags
3426 my $previous_balance = 0;
3427 $previous_balance += $_->{amount} for $self->_items_previous();
3432 if ( $previous_balance && $c_multi_line_total ) {
3433 # previous balance, new charges on separate lines
3436 total_amount => sprintf('%.2f',$previous_balance),
3437 total_item => $self->mt(
3438 $conf->config('previous_balance-text') || 'Previous Balance'
3442 $total_charges = $invoice_charges;
3443 $total_descr = $self->mt(
3444 $conf->config('previous_balance-text-total_new_charges')
3445 || 'Total New Charges'
3449 # previous balance and new charges combined into a single total line
3450 $total_charges = $invoice_charges + $previous_balance;
3451 $total_descr = $self->mt('Total Charges');
3454 if ( $conf->exists('invoice_show_prior_due_date') && !$conf->exists('invoice_omit_due_date') ) {
3455 # then the due date should be shown with Total New Charges,
3456 # and should NOT be shown with the Balance Due message.
3458 if ( $self->due_date ) {
3459 $total_descr .= $self->invoice_pay_by_msg;
3460 } elsif ( $self->terms ) {
3461 $total_descr = join(' ',
3464 $self->mt($self->terms)
3470 total_amount => sprintf('%.2f', $total_charges),
3471 total_item => $total_descr,
3477 =item _items_aging_balances
3479 Returns an array of aged balance amounts from a given epoch timestamp.
3481 The time of day is ignored for this calculation, so that slight differences
3482 on the generation time of an invoice doesn't determine which column an
3483 aged balance falls into.
3485 Will not include any balances dated after the given timestamp in
3486 the calculated totals
3489 @aged_balances = $b->_items_aging_balances( $b->_date )
3500 sub _items_aging_balances {
3501 my ($self, $basetime) = @_;
3502 die "Incorrect usage of _items_aging_balances()" unless ref $self;
3504 $basetime = $self->_date unless $basetime;
3505 my @aging_balances = (0, 0, 0, 0);
3506 my @open_invoices = $self->_items_previous();
3507 my $d30 = 2592000; # 60 * 60 * 24 * 30,
3508 my $d60 = 5184000; # 60 * 60 * 24 * 60,
3509 my $d90 = 7776000; # 60 * 60 * 24 * 90
3511 # Move the clock back on our given day to 12:00:01 AM
3512 my $dt_basetime = DateTime->from_epoch(epoch => $basetime);
3513 my $dt_12am = DateTime->new(
3514 year => $dt_basetime->year,
3515 month => $dt_basetime->month,
3516 day => $dt_basetime->day,
3522 # set our epoch breakpoints
3523 $_ = $dt_12am - $_ for $d30, $d60, $d90;
3525 # grep the aged balances
3526 for my $oinv (@open_invoices) {
3527 if ($oinv->{_date} <= $basetime && $oinv->{_date} > $d30) {
3528 # If post invoice dated less than 30days ago
3529 $aging_balances[0] += $oinv->{amount};
3530 } elsif ($oinv->{_date} <= $d30 && $oinv->{_date} > $d60) {
3531 # If past invoice dated between 30-60 days ago
3532 $aging_balances[1] += $oinv->{amount};
3533 } elsif ($oinv->{_date} <= $d60 && $oinv->{_date} > $d90) {
3534 # If past invoice dated between 60-90 days ago
3535 $aging_balances[2] += $oinv->{amount};
3537 # If past invoice dated 90+ days ago
3538 $aging_balances[3] += $oinv->{amount};
3542 return map{ sprintf('%.2f',$_) } @aging_balances;
3545 =item has_call_details
3547 Returns true if this invoice has call details.
3551 sub has_call_details {
3554 SELECT 1 FROM cust_bill_pkg_detail
3555 LEFT JOIN cust_bill_pkg USING (billpkgnum)
3556 WHERE cust_bill_pkg_detail.format = 'C'
3557 AND cust_bill_pkg.invnum = ?
3562 =item call_details [ OPTION => VALUE ... ]
3564 Returns an array of CSV strings representing the call details for this invoice
3565 The only option available is the boolean prepend_billed_number
3570 my ($self, %opt) = @_;
3572 my $format_function = sub { shift };
3574 if ($opt{prepend_billed_number}) {
3575 $format_function = sub {
3579 $row->amount ? $row->phonenum. ",". $detail : '"Billed number",'. $detail;
3584 my @details = map { $_->details( 'format_function' => $format_function,
3585 'escape_function' => sub{ return() },
3589 $self->cust_bill_pkg;
3590 my $header = $details[0];
3591 ( $header, grep { $_ ne $header } @details );
3594 =item cust_pay_batch
3596 Returns all L<FS::cust_pay_batch> records linked to this invoice. Deprecated,
3601 sub cust_pay_batch {
3602 carp "FS::cust_bill->cust_pay_batch is deprecated";
3604 qsearch('cust_pay_batch', { 'invnum' => $self->invnum });
3613 =item process_reprint
3617 sub process_reprint {
3618 process_re_X('print', @_);
3621 =item process_reemail
3625 sub process_reemail {
3626 process_re_X('email', @_);
3634 process_re_X('fax', @_);
3642 process_re_X('ftp', @_);
3649 sub process_respool {
3650 process_re_X('spool', @_);
3655 my( $method, $job ) = ( shift, shift );
3656 warn "$me process_re_X $method for job $job\n" if $DEBUG;
3659 warn Dumper($param) if $DEBUG;
3669 # this is called from search/cust_bill.html and given all its search
3670 # parameters, so it needs to perform the same search.
3673 # spool_invoice ftp_invoice fax_invoice print_invoice
3674 my($method, $job, %param ) = @_;
3676 warn "re_X $method for job $job with param:\n".
3677 join( '', map { " $_ => ". $param{$_}. "\n" } keys %param );
3680 #some false laziness w/search/cust_bill.html
3681 $param{'order_by'} = 'cust_bill._date';
3683 my $query = FS::cust_bill->search(\%param);
3684 delete $query->{'count_query'};
3685 delete $query->{'count_addl'};
3687 $query->{debug} = 1; # was in here before, is obviously useful
3689 my @cust_bill = qsearch( $query );
3691 $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
3693 warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
3696 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
3697 foreach my $cust_bill ( @cust_bill ) {
3698 $cust_bill->$method();
3700 if ( $job ) { #progressbar foo
3702 if ( time - $min_sec > $last ) {
3703 my $error = $job->update_statustext(
3704 int( 100 * $num / scalar(@cust_bill) )
3706 die $error if $error;
3717 +{ ( map { $_=>$self->$_ } $self->fields ),
3718 'owed' => $self->owed,
3719 #XXX last payment applied date
3725 =head1 CLASS METHODS
3731 Returns an SQL fragment to retreive the amount owed (charged minus credited and paid).
3736 my ($class, $start, $end) = @_;
3738 $class->paid_sql($start, $end). ' - '.
3739 $class->credited_sql($start, $end);
3744 Returns an SQL fragment to retreive the net amount (charged minus credited).
3749 my ($class, $start, $end) = @_;
3750 'charged - '. $class->credited_sql($start, $end);
3755 Returns an SQL fragment to retreive the amount paid against this invoice.
3760 my ($class, $start, $end) = @_;
3761 $start &&= "AND cust_bill_pay._date <= $start";
3762 $end &&= "AND cust_bill_pay._date > $end";
3763 $start = '' unless defined($start);
3764 $end = '' unless defined($end);
3765 "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
3766 WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end )";
3771 Returns an SQL fragment to retreive the amount credited against this invoice.
3776 my ($class, $start, $end) = @_;
3777 $start &&= "AND cust_credit_bill._date <= $start";
3778 $end &&= "AND cust_credit_bill._date > $end";
3779 $start = '' unless defined($start);
3780 $end = '' unless defined($end);
3781 "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
3782 WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end )";
3787 Returns an SQL fragment to retrieve the due date of an invoice.
3788 Currently only supported on PostgreSQL.
3793 die "don't use: doesn't account for agent-specific invoice_default_terms";
3795 #we're passed a $conf but not a specific customer (that's in the query), so
3796 # to make this work we'd need an agentnum-aware "condition_sql_conf" like
3797 # "condition_sql_option" that retreives a conf value with SQL in an agent-
3800 my $conf = new FS::Conf;
3804 cust_bill.invoice_terms,
3805 cust_main.invoice_terms,
3806 \''.($conf->config('invoice_default_terms') || '').'\'
3807 ), E\'Net (\\\\d+)\'
3809 ) * 86400 + cust_bill._date'
3820 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
3821 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base