2 use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::Record );
5 use vars qw( $DEBUG $me $date_format );
7 use Fcntl qw(:flock); #for spool_csv
9 use List::Util qw(min max sum);
13 use Storable qw( freeze thaw );
15 use FS::UID qw( datasrc );
16 use FS::Misc qw( send_email send_fax do_print );
17 use FS::Record qw( qsearch qsearchs dbh );
18 use FS::cust_statement;
19 use FS::cust_bill_pkg;
20 use FS::cust_bill_pkg_display;
21 use FS::cust_bill_pkg_detail;
25 use FS::cust_credit_bill;
27 use FS::cust_bill_event;
30 use FS::cust_bill_pay;
31 use FS::part_bill_event;
34 use FS::cust_bill_batch;
35 use FS::cust_bill_pay_pkg;
36 use FS::cust_credit_bill_pkg;
37 use FS::discount_plan;
38 use FS::cust_bill_void;
42 $me = '[FS::cust_bill]';
44 #ask FS::UID to run this stuff for us later
45 FS::UID->install_callback( sub {
46 my $conf = new FS::Conf; #global
47 $date_format = $conf->config('date_format') || '%x'; #/YY
52 FS::cust_bill - Object methods for cust_bill records
58 $record = new FS::cust_bill \%hash;
59 $record = new FS::cust_bill { 'column' => 'value' };
61 $error = $record->insert;
63 $error = $new_record->replace($old_record);
65 $error = $record->delete;
67 $error = $record->check;
69 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
71 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
73 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
75 @cust_pay_objects = $cust_bill->cust_pay;
77 $tax_amount = $record->tax;
79 @lines = $cust_bill->print_text;
80 @lines = $cust_bill->print_text('time' => $time);
84 An FS::cust_bill object represents an invoice; a declaration that a customer
85 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
86 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
87 following fields are currently supported:
93 =item invnum - primary key (assigned automatically for new invoices)
95 =item custnum - customer (see L<FS::cust_main>)
97 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
98 L<Time::Local> and L<Date::Parse> for conversion functions.
100 =item charged - amount of this invoice
102 =item invoice_terms - optional terms override for this specific invoice
106 Customer info at invoice generation time
110 =item billing_balance - the customer's balance at the time the invoice was
111 generated (not including charges on this invoice)
113 =item previous_balance - the billing_balance of this customer's previous
114 invoice plus the charges on that invoice
122 =item printed - deprecated
130 =item closed - books closed flag, empty or `Y'
132 =item statementnum - invoice aggregation (see L<FS::cust_statement>)
134 =item agent_invid - legacy invoice number
136 =item promised_date - customer promised payment date, for collection
146 Creates a new invoice. To add the invoice to the database, see L<"insert">.
147 Invoices are normally created by calling the bill method of a customer object
148 (see L<FS::cust_main>).
152 sub table { 'cust_bill'; }
154 # should be the ONLY occurrence of "Invoice" in invoice rendering code.
155 # (except email_subject and invnum_date_pretty)
158 $self->conf->config('notice_name') || 'Invoice'
161 sub cust_linked { $_[0]->cust_main_custnum || $_[0]->custnum }
162 sub cust_unlinked_msg {
164 "WARNING: can't find cust_main.custnum ". $self->custnum.
165 ' (cust_bill.invnum '. $self->invnum. ')';
170 Adds this invoice to the database ("Posts" the invoice). If there is an error,
171 returns the error, otherwise returns false.
177 warn "$me insert called\n" if $DEBUG;
179 local $SIG{HUP} = 'IGNORE';
180 local $SIG{INT} = 'IGNORE';
181 local $SIG{QUIT} = 'IGNORE';
182 local $SIG{TERM} = 'IGNORE';
183 local $SIG{TSTP} = 'IGNORE';
184 local $SIG{PIPE} = 'IGNORE';
186 my $oldAutoCommit = $FS::UID::AutoCommit;
187 local $FS::UID::AutoCommit = 0;
190 my $error = $self->SUPER::insert;
192 $dbh->rollback if $oldAutoCommit;
196 if ( $self->get('cust_bill_pkg') ) {
197 foreach my $cust_bill_pkg ( @{$self->get('cust_bill_pkg')} ) {
198 $cust_bill_pkg->invnum($self->invnum);
199 my $error = $cust_bill_pkg->insert;
201 $dbh->rollback if $oldAutoCommit;
202 return "can't create invoice line item: $error";
207 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
214 Voids this invoice: deletes the invoice and adds a record of the voided invoice
215 to the FS::cust_bill_void table (and related tables starting from
216 FS::cust_bill_pkg_void).
222 my $reason = scalar(@_) ? shift : '';
224 local $SIG{HUP} = 'IGNORE';
225 local $SIG{INT} = 'IGNORE';
226 local $SIG{QUIT} = 'IGNORE';
227 local $SIG{TERM} = 'IGNORE';
228 local $SIG{TSTP} = 'IGNORE';
229 local $SIG{PIPE} = 'IGNORE';
231 my $oldAutoCommit = $FS::UID::AutoCommit;
232 local $FS::UID::AutoCommit = 0;
235 my $cust_bill_void = new FS::cust_bill_void ( {
236 map { $_ => $self->get($_) } $self->fields
238 $cust_bill_void->reason($reason);
239 my $error = $cust_bill_void->insert;
241 $dbh->rollback if $oldAutoCommit;
245 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
246 my $error = $cust_bill_pkg->void($reason);
248 $dbh->rollback if $oldAutoCommit;
253 $error = $self->delete;
255 $dbh->rollback if $oldAutoCommit;
259 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
267 This method now works but you probably shouldn't use it. Instead, apply a
268 credit against the invoice, or use the new void method.
270 Using this method to delete invoices outright is really, really bad. There
271 would be no record you ever posted this invoice, and there are no check to
272 make sure charged = 0 or that there are no associated cust_bill_pkg records.
274 Really, don't use it.
280 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
282 local $SIG{HUP} = 'IGNORE';
283 local $SIG{INT} = 'IGNORE';
284 local $SIG{QUIT} = 'IGNORE';
285 local $SIG{TERM} = 'IGNORE';
286 local $SIG{TSTP} = 'IGNORE';
287 local $SIG{PIPE} = 'IGNORE';
289 my $oldAutoCommit = $FS::UID::AutoCommit;
290 local $FS::UID::AutoCommit = 0;
293 foreach my $table (qw(
304 foreach my $linked ( $self->$table() ) {
305 my $error = $linked->delete;
307 $dbh->rollback if $oldAutoCommit;
314 my $error = $self->SUPER::delete(@_);
316 $dbh->rollback if $oldAutoCommit;
320 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
326 =item replace [ OLD_RECORD ]
328 You can, but probably shouldn't modify invoices...
330 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
331 supplied, replaces this record. If there is an error, returns the error,
332 otherwise returns false.
336 #replace can be inherited from Record.pm
338 # replace_check is now the preferred way to #implement replace data checks
339 # (so $object->replace() works without an argument)
342 my( $new, $old ) = ( shift, shift );
343 return "Can't modify closed invoice" if $old->closed =~ /^Y/i;
344 #return "Can't change _date!" unless $old->_date eq $new->_date;
345 return "Can't change _date" unless $old->_date == $new->_date;
346 return "Can't change charged" unless $old->charged == $new->charged
347 || $old->charged == 0
348 || $new->{'Hash'}{'cc_surcharge_replace_hack'};
354 =item add_cc_surcharge
360 sub add_cc_surcharge {
361 my ($self, $pkgnum, $amount) = (shift, shift, shift);
364 my $cust_bill_pkg = new FS::cust_bill_pkg({
365 'invnum' => $self->invnum,
369 $error = $cust_bill_pkg->insert;
370 return $error if $error;
372 $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
373 $self->charged($self->charged+$amount);
374 $error = $self->replace;
375 return $error if $error;
377 $self->apply_payments_and_credits;
383 Checks all fields to make sure this is a valid invoice. If there is an error,
384 returns the error, otherwise returns false. Called by the insert and replace
393 $self->ut_numbern('invnum')
394 || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
395 || $self->ut_numbern('_date')
396 || $self->ut_money('charged')
397 || $self->ut_numbern('printed')
398 || $self->ut_enum('closed', [ '', 'Y' ])
399 || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum' )
400 || $self->ut_numbern('agent_invid') #varchar?
402 return $error if $error;
404 $self->_date(time) unless $self->_date;
406 $self->printed(0) if $self->printed eq '';
413 Returns the displayed invoice number for this invoice: agent_invid if
414 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
420 my $conf = $self->conf;
421 if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
422 return $self->agent_invid;
424 return $self->invnum;
430 Returns the customer's last invoice before this one.
436 if ( !$self->get('previous_bill') ) {
437 $self->set('previous_bill', qsearchs({
438 'table' => 'cust_bill',
439 'hashref' => { 'custnum' => $self->custnum,
440 '_date' => { op=>'<', value=>$self->_date } },
441 'order_by' => 'ORDER BY _date DESC LIMIT 1',
444 $self->get('previous_bill');
449 Returns a list consisting of the total previous balance for this customer,
450 followed by the previous outstanding invoices (as FS::cust_bill objects also).
457 my @cust_bill = sort { $a->_date <=> $b->_date }
458 grep { $_->owed != 0 }
459 qsearch( 'cust_bill', { 'custnum' => $self->custnum,
460 #'_date' => { op=>'<', value=>$self->_date },
461 'invnum' => { op=>'<', value=>$self->invnum },
464 foreach ( @cust_bill ) { $total += $_->owed; }
468 =item enable_previous
470 Whether to show the 'Previous Charges' section when printing this invoice.
471 The negation of the 'disable_previous_balance' config setting.
475 sub enable_previous {
477 my $agentnum = $self->cust_main->agentnum;
478 !$self->conf->exists('disable_previous_balance', $agentnum);
483 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
490 { 'table' => 'cust_bill_pkg',
491 'hashref' => { 'invnum' => $self->invnum },
492 'order_by' => 'ORDER BY billpkgnum', #important? otherwise we could use
493 # the AUTLOADED FK search. or should
494 # that default to ORDER by the pkey?
499 =item cust_bill_pkg_pkgnum PKGNUM
501 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice and
506 sub cust_bill_pkg_pkgnum {
507 my( $self, $pkgnum ) = @_;
509 { 'table' => 'cust_bill_pkg',
510 'hashref' => { 'invnum' => $self->invnum,
513 'order_by' => 'ORDER BY billpkgnum',
520 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
527 my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
528 $self->cust_bill_pkg;
530 grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
535 Returns true if any of the packages (or their definitions) corresponding to the
536 line items for this invoice have the no_auto flag set.
542 grep { $_->no_auto || $_->part_pkg->no_auto } $self->cust_pkg;
545 =item open_cust_bill_pkg
547 Returns the open line items for this invoice.
549 Note that cust_bill_pkg with both setup and recur fees are returned as two
550 separate line items, each with only one fee.
554 # modeled after cust_main::open_cust_bill
555 sub open_cust_bill_pkg {
558 # grep { $_->owed > 0 } $self->cust_bill_pkg
560 my %other = ( 'recur' => 'setup',
561 'setup' => 'recur', );
563 foreach my $field ( qw( recur setup )) {
564 push @open, map { $_->set( $other{$field}, 0 ); $_; }
565 grep { $_->owed($field) > 0 }
566 $self->cust_bill_pkg;
572 =item cust_bill_event
574 Returns the completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
578 sub cust_bill_event {
580 qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
583 =item num_cust_bill_event
585 Returns the number of completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
589 sub num_cust_bill_event {
592 "SELECT COUNT(*) FROM cust_bill_event WHERE invnum = ?";
593 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
594 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
595 $sth->fetchrow_arrayref->[0];
600 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
604 #false laziness w/cust_pkg.pm
608 'table' => 'cust_event',
609 'addl_from' => 'JOIN part_event USING ( eventpart )',
610 'hashref' => { 'tablenum' => $self->invnum },
611 'extra_sql' => " AND eventtable = 'cust_bill' ",
617 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
621 #false laziness w/cust_pkg.pm
625 "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
626 " WHERE tablenum = ? AND eventtable = 'cust_bill'";
627 my $sth = dbh->prepare($sql) or die dbh->errstr. " preparing $sql";
628 $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
629 $sth->fetchrow_arrayref->[0];
634 Returns the customer (see L<FS::cust_main>) for this invoice.
636 =item cust_suspend_if_balance_over AMOUNT
638 Suspends the customer associated with this invoice if the total amount owed on
639 this invoice and all older invoices is greater than the specified amount.
641 Returns a list: an empty list on success or a list of errors.
645 sub cust_suspend_if_balance_over {
646 my( $self, $amount ) = ( shift, shift );
647 my $cust_main = $self->cust_main;
648 if ( $cust_main->total_owed_date($self->_date) < $amount ) {
651 $cust_main->suspend(@_);
657 Depreciated. See the cust_credited method.
659 #Returns a list consisting of the total previous credited (see
660 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
661 #outstanding credits (FS::cust_credit objects).
667 croak "FS::cust_bill->cust_credit depreciated; see ".
668 "FS::cust_bill->cust_credit_bill";
671 #my @cust_credit = sort { $a->_date <=> $b->_date }
672 # grep { $_->credited != 0 && $_->_date < $self->_date }
673 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
675 #foreach (@cust_credit) { $total += $_->credited; }
676 #$total, @cust_credit;
681 Depreciated. See the cust_bill_pay method.
683 #Returns all payments (see L<FS::cust_pay>) for this invoice.
689 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
691 #sort { $a->_date <=> $b->_date }
692 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
698 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
704 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
705 sort { $a->_date <=> $b->_date }
706 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
711 =item cust_credit_bill
713 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
719 map { $_ } #return $self->num_cust_credit_bill unless wantarray;
720 sort { $a->_date <=> $b->_date }
721 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
725 sub cust_credit_bill {
726 shift->cust_credited(@_);
729 #=item cust_bill_pay_pkgnum PKGNUM
731 #Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
732 #with matching pkgnum.
736 #sub cust_bill_pay_pkgnum {
737 # my( $self, $pkgnum ) = @_;
738 # map { $_ } #return $self->num_cust_bill_pay_pkgnum($pkgnum) unless wantarray;
739 # sort { $a->_date <=> $b->_date }
740 # qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum,
741 # 'pkgnum' => $pkgnum,
746 =item cust_bill_pay_pkg PKGNUM
748 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
749 applied against the matching pkgnum.
753 sub cust_bill_pay_pkg {
754 my( $self, $pkgnum ) = @_;
757 'select' => 'cust_bill_pay_pkg.*',
758 'table' => 'cust_bill_pay_pkg',
759 'addl_from' => ' LEFT JOIN cust_bill_pay USING ( billpaynum ) '.
760 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
761 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
762 " AND cust_bill_pkg.pkgnum = $pkgnum",
767 #=item cust_credited_pkgnum PKGNUM
769 #=item cust_credit_bill_pkgnum PKGNUM
771 #Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice
772 #with matching pkgnum.
776 #sub cust_credited_pkgnum {
777 # my( $self, $pkgnum ) = @_;
778 # map { $_ } #return $self->num_cust_credit_bill_pkgnum($pkgnum) unless wantarray;
779 # sort { $a->_date <=> $b->_date }
780 # qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum,
781 # 'pkgnum' => $pkgnum,
786 #sub cust_credit_bill_pkgnum {
787 # shift->cust_credited_pkgnum(@_);
790 =item cust_credit_bill_pkg PKGNUM
792 Returns all credit applications (see L<FS::cust_credit_bill>) for this invoice
793 applied against the matching pkgnum.
797 sub cust_credit_bill_pkg {
798 my( $self, $pkgnum ) = @_;
801 'select' => 'cust_credit_bill_pkg.*',
802 'table' => 'cust_credit_bill_pkg',
803 'addl_from' => ' LEFT JOIN cust_credit_bill USING ( creditbillnum ) '.
804 ' LEFT JOIN cust_bill_pkg USING ( billpkgnum ) ',
805 'extra_sql' => ' WHERE cust_bill_pkg.invnum = '. $self->invnum.
806 " AND cust_bill_pkg.pkgnum = $pkgnum",
811 =item cust_bill_batch
813 Returns all invoice batch records (L<FS::cust_bill_batch>) for this invoice.
817 sub cust_bill_batch {
819 qsearch('cust_bill_batch', { 'invnum' => $self->invnum });
824 Returns all discount plans (L<FS::discount_plan>) for this invoice, as a
825 hash keyed by term length.
831 FS::discount_plan->all($self);
836 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
843 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
845 foreach (@taxlines) { $total += $_->setup; }
851 Returns the amount owed (still outstanding) on this invoice, which is charged
852 minus all payment applications (see L<FS::cust_bill_pay>) and credit
853 applications (see L<FS::cust_credit_bill>).
859 my $balance = $self->charged;
860 $balance -= $_->amount foreach ( $self->cust_bill_pay );
861 $balance -= $_->amount foreach ( $self->cust_credited );
862 $balance = sprintf( "%.2f", $balance);
863 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
868 my( $self, $pkgnum ) = @_;
870 #my $balance = $self->charged;
872 $balance += $_->setup + $_->recur for $self->cust_bill_pkg_pkgnum($pkgnum);
874 $balance -= $_->amount for $self->cust_bill_pay_pkg($pkgnum);
875 $balance -= $_->amount for $self->cust_credit_bill_pkg($pkgnum);
877 $balance = sprintf( "%.2f", $balance);
878 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
884 Returns true if this invoice should be hidden. See the
885 selfservice-hide_invoices-taxclass configuraiton setting.
891 my $conf = $self->conf;
892 my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass')
894 my @cust_bill_pkg = $self->cust_bill_pkg;
895 my @part_pkg = grep $_, map $_->part_pkg, @cust_bill_pkg;
896 ! grep { $_->taxclass ne $hide_taxclass } @part_pkg;
899 =item apply_payments_and_credits [ OPTION => VALUE ... ]
901 Applies unapplied payments and credits to this invoice.
903 A hash of optional arguments may be passed. Currently "manual" is supported.
904 If true, a payment receipt is sent instead of a statement when
905 'payment_receipt_email' configuration option is set.
907 If there is an error, returns the error, otherwise returns false.
911 sub apply_payments_and_credits {
912 my( $self, %options ) = @_;
913 my $conf = $self->conf;
915 local $SIG{HUP} = 'IGNORE';
916 local $SIG{INT} = 'IGNORE';
917 local $SIG{QUIT} = 'IGNORE';
918 local $SIG{TERM} = 'IGNORE';
919 local $SIG{TSTP} = 'IGNORE';
920 local $SIG{PIPE} = 'IGNORE';
922 my $oldAutoCommit = $FS::UID::AutoCommit;
923 local $FS::UID::AutoCommit = 0;
926 $self->select_for_update; #mutex
928 my @payments = grep { $_->unapplied > 0 } $self->cust_main->cust_pay;
929 my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit;
931 if ( $conf->exists('pkg-balances') ) {
932 # limit @payments & @credits to those w/ a pkgnum grepped from $self
933 my %pkgnums = map { $_ => 1 } map $_->pkgnum, $self->cust_bill_pkg;
934 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
935 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
938 while ( $self->owed > 0 and ( @payments || @credits ) ) {
941 if ( @payments && @credits ) {
943 #decide which goes first by weight of top (unapplied) line item
945 my @open_lineitems = $self->open_cust_bill_pkg;
948 max( map { $_->part_pkg->pay_weight || 0 }
953 my $max_credit_weight =
954 max( map { $_->part_pkg->credit_weight || 0 }
960 #if both are the same... payments first? it has to be something
961 if ( $max_pay_weight >= $max_credit_weight ) {
967 } elsif ( @payments ) {
969 } elsif ( @credits ) {
972 die "guru meditation #12 and 35";
976 if ( $app eq 'pay' ) {
978 my $payment = shift @payments;
979 $unapp_amount = $payment->unapplied;
980 $app = new FS::cust_bill_pay { 'paynum' => $payment->paynum };
981 $app->pkgnum( $payment->pkgnum )
982 if $conf->exists('pkg-balances') && $payment->pkgnum;
984 } elsif ( $app eq 'credit' ) {
986 my $credit = shift @credits;
987 $unapp_amount = $credit->credited;
988 $app = new FS::cust_credit_bill { 'crednum' => $credit->crednum };
989 $app->pkgnum( $credit->pkgnum )
990 if $conf->exists('pkg-balances') && $credit->pkgnum;
993 die "guru meditation #12 and 35";
997 if ( $conf->exists('pkg-balances') && $app->pkgnum ) {
998 warn "owed_pkgnum ". $app->pkgnum;
999 $owed = $self->owed_pkgnum($app->pkgnum);
1001 $owed = $self->owed;
1003 next unless $owed > 0;
1005 warn "min ( $unapp_amount, $owed )\n" if $DEBUG;
1006 $app->amount( sprintf('%.2f', min( $unapp_amount, $owed ) ) );
1008 $app->invnum( $self->invnum );
1010 my $error = $app->insert(%options);
1012 $dbh->rollback if $oldAutoCommit;
1013 return "Error inserting ". $app->table. " record: $error";
1015 die $error if $error;
1019 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1024 =item generate_email OPTION => VALUE ...
1032 sender address, required
1036 alternate template name, optional
1040 text attachment arrayref, optional
1044 email subject, optional
1048 notice name instead of "Invoice", optional
1052 Returns an argument list to be passed to L<FS::Misc::send_email>.
1058 sub generate_email {
1062 my $conf = $self->conf;
1064 my $me = '[FS::cust_bill::generate_email]';
1067 'from' => $args{'from'},
1068 'subject' => ($args{'subject'} || $self->email_subject),
1071 $args{'unsquelch_cdr'} = $conf->exists('voip-cdr_email');
1073 my $cust_main = $self->cust_main;
1075 if (ref($args{'to'}) eq 'ARRAY') {
1076 $return{'to'} = $args{'to'};
1078 $return{'to'} = [ grep { $_ !~ /^(POST|FAX)$/ }
1079 $cust_main->invoicing_list
1083 if ( $conf->exists('invoice_html') ) {
1085 warn "$me creating HTML/text multipart message"
1088 $return{'nobody'} = 1;
1090 my $alternative = build MIME::Entity
1091 'Type' => 'multipart/alternative',
1092 #'Encoding' => '7bit',
1093 'Disposition' => 'inline'
1097 if ( $conf->exists('invoice_email_pdf')
1098 and scalar($conf->config('invoice_email_pdf_note')) ) {
1100 warn "$me using 'invoice_email_pdf_note' in multipart message"
1102 $data = [ map { $_ . "\n" }
1103 $conf->config('invoice_email_pdf_note')
1108 warn "$me not using 'invoice_email_pdf_note' in multipart message"
1110 if ( ref($args{'print_text'}) eq 'ARRAY' ) {
1111 $data = $args{'print_text'};
1113 $data = [ $self->print_text(\%args) ];
1118 $alternative->attach(
1119 'Type' => 'text/plain',
1120 'Encoding' => 'quoted-printable',
1121 #'Encoding' => '7bit',
1123 'Disposition' => 'inline',
1130 if ( $conf->exists('invoice_email_pdf')
1131 and scalar($conf->config('invoice_email_pdf_note')) ) {
1133 $htmldata = join('<BR>', $conf->config('invoice_email_pdf_note') );
1137 $args{'from'} =~ /\@([\w\.\-]+)/;
1138 my $from = $1 || 'example.com';
1139 my $content_id = join('.', rand()*(2**32), $$, time). "\@$from";
1142 my $agentnum = $cust_main->agentnum;
1143 if ( defined($args{'template'}) && length($args{'template'})
1144 && $conf->exists( 'logo_'. $args{'template'}. '.png', $agentnum )
1147 $logo = 'logo_'. $args{'template'}. '.png';
1151 my $image_data = $conf->config_binary( $logo, $agentnum);
1153 $image = build MIME::Entity
1154 'Type' => 'image/png',
1155 'Encoding' => 'base64',
1156 'Data' => $image_data,
1157 'Filename' => 'logo.png',
1158 'Content-ID' => "<$content_id>",
1161 if ($conf->exists('invoice-barcode')) {
1162 my $barcode_content_id = join('.', rand()*(2**32), $$, time). "\@$from";
1163 $barcode = build MIME::Entity
1164 'Type' => 'image/png',
1165 'Encoding' => 'base64',
1166 'Data' => $self->invoice_barcode(0),
1167 'Filename' => 'barcode.png',
1168 'Content-ID' => "<$barcode_content_id>",
1170 $args{'barcode_cid'} = $barcode_content_id;
1173 $htmldata = $self->print_html({ 'cid'=>$content_id, %args });
1176 $alternative->attach(
1177 'Type' => 'text/html',
1178 'Encoding' => 'quoted-printable',
1179 'Data' => [ '<html>',
1182 ' '. encode_entities($return{'subject'}),
1185 ' <body bgcolor="#e8e8e8">',
1190 'Disposition' => 'inline',
1191 #'Filename' => 'invoice.pdf',
1195 my @otherparts = ();
1196 if ( $cust_main->email_csv_cdr ) {
1198 push @otherparts, build MIME::Entity
1199 'Type' => 'text/csv',
1200 'Encoding' => '7bit',
1201 'Data' => [ map { "$_\n" }
1202 $self->call_details('prepend_billed_number' => 1)
1204 'Disposition' => 'attachment',
1205 'Filename' => 'usage-'. $self->invnum. '.csv',
1210 if ( $conf->exists('invoice_email_pdf') ) {
1215 # multipart/alternative
1221 my $related = build MIME::Entity 'Type' => 'multipart/related',
1222 'Encoding' => '7bit';
1224 #false laziness w/Misc::send_email
1225 $related->head->replace('Content-type',
1226 $related->mime_type.
1227 '; boundary="'. $related->head->multipart_boundary. '"'.
1228 '; type=multipart/alternative'
1231 $related->add_part($alternative);
1233 $related->add_part($image) if $image;
1235 my $pdf = build MIME::Entity $self->mimebuild_pdf(\%args);
1237 $return{'mimeparts'} = [ $related, $pdf, @otherparts ];
1241 #no other attachment:
1243 # multipart/alternative
1248 $return{'content-type'} = 'multipart/related';
1249 if ($conf->exists('invoice-barcode') && $barcode) {
1250 $return{'mimeparts'} = [ $alternative, $image, $barcode, @otherparts ];
1252 $return{'mimeparts'} = [ $alternative, $image, @otherparts ];
1254 $return{'type'} = 'multipart/alternative'; #Content-Type of first part...
1255 #$return{'disposition'} = 'inline';
1261 if ( $conf->exists('invoice_email_pdf') ) {
1262 warn "$me creating PDF attachment"
1265 #mime parts arguments a la MIME::Entity->build().
1266 $return{'mimeparts'} = [
1267 { $self->mimebuild_pdf(\%args) }
1271 if ( $conf->exists('invoice_email_pdf')
1272 and scalar($conf->config('invoice_email_pdf_note')) ) {
1274 warn "$me using 'invoice_email_pdf_note'"
1276 $return{'body'} = [ map { $_ . "\n" }
1277 $conf->config('invoice_email_pdf_note')
1282 warn "$me not using 'invoice_email_pdf_note'"
1284 if ( ref($args{'print_text'}) eq 'ARRAY' ) {
1285 $return{'body'} = $args{'print_text'};
1287 $return{'body'} = [ $self->print_text(\%args) ];
1300 Returns a list suitable for passing to MIME::Entity->build(), representing
1301 this invoice as PDF attachment.
1308 'Type' => 'application/pdf',
1309 'Encoding' => 'base64',
1310 'Data' => [ $self->print_pdf(@_) ],
1311 'Disposition' => 'attachment',
1312 'Filename' => 'invoice-'. $self->invnum. '.pdf',
1318 Sends this invoice to the destinations configured for this customer: sends
1319 email, prints and/or faxes. See L<FS::cust_main_invoice>.
1321 Options can be passed as a hashref. Positional parameters are no longer
1324 I<template>: a suffix for alternate invoices
1326 I<agentnum>: obsolete, now does nothing.
1328 I<invoice_from> overrides the default email invoice From: address.
1330 I<amount>: obsolete, does nothing
1332 I<notice_name> overrides "Invoice" as the name of the sent document
1333 (templates from 10/2009 or newer required).
1335 I<lpr> overrides the system 'lpr' option as the command to print a document
1336 from standard input.
1342 my $opt = ref($_[0]) ? $_[0] : +{ @_ };
1343 my $conf = $self->conf;
1345 my $cust_main = $self->cust_main;
1347 my @invoicing_list = $cust_main->invoicing_list;
1350 if ( grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list )
1351 && ! $self->invoice_noemail;
1354 if grep { $_ eq 'POST' } @invoicing_list; #postal
1356 #this has never been used post-$ORIGINAL_ISP afaik
1357 $self->fax_invoice($opt)
1358 if grep { $_ eq 'FAX' } @invoicing_list; #fax
1364 =item email HASHREF | [ TEMPLATE [ , INVOICE_FROM ] ]
1366 Sends this invoice to the customer's email destination(s).
1368 Options must be passed as a hashref. Positional parameters are no longer
1371 I<template>, if specified, is the name of a suffix for alternate invoices.
1373 I<invoice_from>, if specified, overrides the default email invoice From:
1376 I<notice_name> is the name of the sent document.
1380 sub queueable_email {
1383 my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1384 or die "invalid invoice number: " . $opt{invnum};
1386 my %args = map {$_ => $opt{$_}}
1388 qw( invoice_from notice_name no_coupon template );
1390 my $error = $self->email( \%args );
1391 die $error if $error;
1397 return if $self->hide;
1398 my $conf = $self->conf;
1399 my $opt = shift || {};
1400 if ($opt and !ref($opt)) {
1401 die "FS::cust_bill::email called with positional parameters";
1404 my $template = $opt->{template};
1405 my $from = delete $opt->{invoice_from};
1407 # this is where we set the From: address
1408 $from ||= $self->_agent_invoice_from || #XXX should go away
1409 $conf->config('invoice_from', $self->cust_main->agentnum );
1411 my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ }
1412 $self->cust_main->invoicing_list;
1414 if ( ! @invoicing_list ) { #no recipients
1415 if ( $conf->exists('cust_bill-no_recipients-error') ) {
1416 die 'No recipients for customer #'. $self->custnum;
1418 #default: better to notify this person than silence
1419 @invoicing_list = ($from);
1423 # this is where we set the Subject:
1424 my $subject = $self->email_subject($template);
1426 my $error = send_email(
1427 $self->generate_email(
1429 'to' => [ grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ],
1430 'subject' => $subject,
1431 %$opt, # template, etc.
1434 die "can't email invoice: $error\n" if $error;
1435 #die "$error\n" if $error;
1441 my $conf = $self->conf;
1443 #my $template = scalar(@_) ? shift : '';
1446 my $subject = $conf->config('invoice_subject', $self->cust_main->agentnum)
1449 my $cust_main = $self->cust_main;
1450 my $name = $cust_main->name;
1451 my $name_short = $cust_main->name_short;
1452 my $invoice_number = $self->invnum;
1453 my $invoice_date = $self->_date_pretty;
1455 eval qq("$subject");
1458 =item lpr_data HASHREF
1460 Returns the postscript or plaintext for this invoice as an arrayref.
1462 Options must be passed as a hashref. Positional parameters are no longer
1465 I<template>, if specified, is the name of a suffix for alternate invoices.
1467 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1473 my $conf = $self->conf;
1474 my $opt = shift || {};
1475 if ($opt and !ref($opt)) {
1476 # nobody does this anyway
1477 die "FS::cust_bill::lpr_data called with positional parameters";
1480 my $method = $conf->exists('invoice_latex') ? 'print_ps' : 'print_text';
1481 [ $self->$method( $opt ) ];
1486 Prints this invoice.
1488 Options must be passed as a hashref.
1490 I<template>, if specified, is the name of a suffix for alternate invoices.
1492 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1498 return if $self->hide;
1499 my $conf = $self->conf;
1500 my $opt = shift || {};
1501 if ($opt and !ref($opt)) {
1502 die "FS::cust_bill::print called with positional parameters";
1505 my $lpr = delete $opt->{lpr};
1506 if($conf->exists('invoice_print_pdf')) {
1507 # Add the invoice to the current batch.
1508 $self->batch_invoice($opt);
1512 $self->lpr_data($opt),
1513 'agentnum' => $self->cust_main->agentnum,
1519 =item fax_invoice HASHREF
1523 Options must be passed as a hashref.
1525 I<template>, if specified, is the name of a suffix for alternate invoices.
1527 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1533 return if $self->hide;
1534 my $conf = $self->conf;
1535 my $opt = shift || {};
1536 if ($opt and !ref($opt)) {
1537 die "FS::cust_bill::fax_invoice called with positional parameters";
1540 die 'FAX invoice destination not (yet?) supported with plain text invoices.'
1541 unless $conf->exists('invoice_latex');
1543 my $dialstring = $self->cust_main->getfield('fax');
1546 my $error = send_fax( 'docdata' => $self->lpr_data($opt),
1547 'dialstring' => $dialstring,
1549 die $error if $error;
1553 =item batch_invoice [ HASHREF ]
1555 Place this invoice into the open batch (see C<FS::bill_batch>). If there
1556 isn't an open batch, one will be created.
1561 my ($self, $opt) = @_;
1562 my $bill_batch = $self->get_open_bill_batch;
1563 my $cust_bill_batch = FS::cust_bill_batch->new({
1564 batchnum => $bill_batch->batchnum,
1565 invnum => $self->invnum,
1567 return $cust_bill_batch->insert($opt);
1570 =item get_open_batch
1572 Returns the currently open batch as an FS::bill_batch object, creating a new
1573 one if necessary. (A per-agent batch if invoice_print_pdf-spoolagent is
1578 sub get_open_bill_batch {
1580 my $conf = $self->conf;
1581 my $hashref = { status => 'O' };
1582 $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent')
1583 ? $self->cust_main->agentnum
1585 my $batch = qsearchs('bill_batch', $hashref);
1586 return $batch if $batch;
1587 $batch = FS::bill_batch->new($hashref);
1588 my $error = $batch->insert;
1589 die $error if $error;
1593 =item ftp_invoice [ TEMPLATENAME ]
1595 Sends this invoice data via FTP.
1597 TEMPLATENAME is unused?
1603 my $conf = $self->conf;
1604 my $template = scalar(@_) ? shift : '';
1607 'protocol' => 'ftp',
1608 'server' => $conf->config('cust_bill-ftpserver'),
1609 'username' => $conf->config('cust_bill-ftpusername'),
1610 'password' => $conf->config('cust_bill-ftppassword'),
1611 'dir' => $conf->config('cust_bill-ftpdir'),
1612 'format' => $conf->config('cust_bill-ftpformat'),
1616 =item spool_invoice [ TEMPLATENAME ]
1618 Spools this invoice data (see L<FS::spool_csv>)
1620 TEMPLATENAME is unused?
1626 my $conf = $self->conf;
1627 my $template = scalar(@_) ? shift : '';
1630 'format' => $conf->config('cust_bill-spoolformat'),
1631 'agent_spools' => $conf->exists('cust_bill-spoolagent'),
1635 =item send_csv OPTION => VALUE, ...
1637 Sends invoice as a CSV data-file to a remote host with the specified protocol.
1641 protocol - currently only "ftp"
1647 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
1648 and YYMMDDHHMMSS is a timestamp.
1650 See L</print_csv> for a description of the output format.
1655 my($self, %opt) = @_;
1659 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1660 mkdir $spooldir, 0700 unless -d $spooldir;
1662 # don't localize dates here, they're a defined format
1663 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1664 my $file = "$spooldir/$tracctnum.csv";
1666 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1668 open(CSV, ">$file") or die "can't open $file: $!";
1676 if ( $opt{protocol} eq 'ftp' ) {
1677 eval "use Net::FTP;";
1679 $net = Net::FTP->new($opt{server}) or die @$;
1681 die "unknown protocol: $opt{protocol}";
1684 $net->login( $opt{username}, $opt{password} )
1685 or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
1687 $net->binary or die "can't set binary mode";
1689 $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
1691 $net->put($file) or die "can't put $file: $!";
1701 Spools CSV invoice data.
1707 =item format - any of FS::Misc::::Invoicing::spool_formats
1709 =item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the
1710 customer has the corresponding invoice destinations set (see
1711 L<FS::cust_main_invoice>).
1713 =item agent_spools - if set to a true value, will spool to per-agent files
1714 rather than a single global file
1716 =item upload_targetnum - if set to a target (see L<FS::upload_target>), will
1717 append to that spool. L<FS::Cron::upload> will then send the spool file to
1720 =item balanceover - if set, only spools the invoice if the total amount owed on
1721 this invoice and all older invoices is greater than the specified amount.
1723 =item time - the "current time". Controls the printing of past due messages
1731 my($self, %opt) = @_;
1733 my $time = $opt{'time'} || time;
1734 my $cust_main = $self->cust_main;
1736 if ( $opt{'dest'} ) {
1737 my %invoicing_list = map { /^(POST|FAX)$/ or 'EMAIL' =~ /^(.*)$/; $1 => 1 }
1738 $cust_main->invoicing_list;
1739 return 'N/A' unless $invoicing_list{$opt{'dest'}}
1740 || ! keys %invoicing_list;
1743 if ( $opt{'balanceover'} ) {
1745 if $cust_main->total_owed_date($self->_date) < $opt{'balanceover'};
1748 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1749 mkdir $spooldir, 0700 unless -d $spooldir;
1751 my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', $time);
1754 if ( $opt{'agent_spools'} ) {
1755 $file = 'agentnum'.$cust_main->agentnum;
1760 if ( $opt{'upload_targetnum'} ) {
1761 $spooldir .= '/target'.$opt{'upload_targetnum'};
1762 mkdir $spooldir, 0700 unless -d $spooldir;
1763 } # otherwise it just goes into export.xxx/cust_bill
1765 if ( lc($opt{'format'}) eq 'billco' ) {
1769 $file = "$spooldir/$file.csv";
1771 my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum);
1773 open(CSV, ">>$file") or die "can't open $file: $!";
1774 flock(CSV, LOCK_EX);
1779 if ( lc($opt{'format'}) eq 'billco' ) {
1781 flock(CSV, LOCK_UN);
1784 $file =~ s/-header.csv$/-detail.csv/;
1786 open(CSV,">>$file") or die "can't open $file: $!";
1787 flock(CSV, LOCK_EX);
1791 print CSV $detail if defined($detail);
1793 flock(CSV, LOCK_UN);
1800 =item print_csv OPTION => VALUE, ...
1802 Returns CSV data for this invoice.
1806 format - 'default', 'billco', 'oneline', 'bridgestone'
1808 Returns a list consisting of two scalars. The first is a single line of CSV
1809 header information for this invoice. The second is one or more lines of CSV
1810 detail information for this invoice.
1812 If I<format> is not specified or "default", the fields of the CSV file are as
1815 record_type, invnum, custnum, _date, charged, first, last, company, address1,
1816 address2, city, state, zip, country, pkg, setup, recur, sdate, edate
1820 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
1822 B<record_type> is C<cust_bill> for the initial header line only. The
1823 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
1824 fields are filled in.
1826 B<record_type> is C<cust_bill_pkg> for detail lines. Only the first two fields
1827 (B<record_type> and B<invnum>) and the last five fields (B<pkg> through B<edate>)
1830 =item invnum - invoice number
1832 =item custnum - customer number
1834 =item _date - invoice date
1836 =item charged - total invoice amount
1838 =item first - customer first name
1840 =item last - customer first name
1842 =item company - company name
1844 =item address1 - address line 1
1846 =item address2 - address line 1
1856 =item pkg - line item description
1858 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
1860 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
1862 =item sdate - start date for recurring fee
1864 =item edate - end date for recurring fee
1868 If I<format> is "billco", the fields of the header CSV file are as follows:
1870 +-------------------------------------------------------------------+
1871 | FORMAT HEADER FILE |
1872 |-------------------------------------------------------------------|
1873 | Field | Description | Name | Type | Width |
1874 | 1 | N/A-Leave Empty | RC | CHAR | 2 |
1875 | 2 | N/A-Leave Empty | CUSTID | CHAR | 15 |
1876 | 3 | Transaction Account No | TRACCTNUM | CHAR | 15 |
1877 | 4 | Transaction Invoice No | TRINVOICE | CHAR | 15 |
1878 | 5 | Transaction Zip Code | TRZIP | CHAR | 5 |
1879 | 6 | Transaction Company Bill To | TRCOMPANY | CHAR | 30 |
1880 | 7 | Transaction Contact Bill To | TRNAME | CHAR | 30 |
1881 | 8 | Additional Address Unit Info | TRADDR1 | CHAR | 30 |
1882 | 9 | Bill To Street Address | TRADDR2 | CHAR | 30 |
1883 | 10 | Ancillary Billing Information | TRADDR3 | CHAR | 30 |
1884 | 11 | Transaction City Bill To | TRCITY | CHAR | 20 |
1885 | 12 | Transaction State Bill To | TRSTATE | CHAR | 2 |
1886 | 13 | Bill Cycle Close Date | CLOSEDATE | CHAR | 10 |
1887 | 14 | Bill Due Date | DUEDATE | CHAR | 10 |
1888 | 15 | Previous Balance | BALFWD | NUM* | 9 |
1889 | 16 | Pmt/CR Applied | CREDAPPLY | NUM* | 9 |
1890 | 17 | Total Current Charges | CURRENTCHG | NUM* | 9 |
1891 | 18 | Total Amt Due | TOTALDUE | NUM* | 9 |
1892 | 19 | Total Amt Due | AMTDUE | NUM* | 9 |
1893 | 20 | 30 Day Aging | AMT30 | NUM* | 9 |
1894 | 21 | 60 Day Aging | AMT60 | NUM* | 9 |
1895 | 22 | 90 Day Aging | AMT90 | NUM* | 9 |
1896 | 23 | Y/N | AGESWITCH | CHAR | 1 |
1897 | 24 | Remittance automation | SCANLINE | CHAR | 100 |
1898 | 25 | Total Taxes & Fees | TAXTOT | NUM* | 9 |
1899 | 26 | Customer Reference Number | CUSTREF | CHAR | 15 |
1900 | 27 | Federal Tax*** | FEDTAX | NUM* | 9 |
1901 | 28 | State Tax*** | STATETAX | NUM* | 9 |
1902 | 29 | Other Taxes & Fees*** | OTHERTAX | NUM* | 9 |
1903 +-------+-------------------------------+------------+------+-------+
1905 If I<format> is "billco", the fields of the detail CSV file are as follows:
1907 FORMAT FOR DETAIL FILE
1909 Field | Description | Name | Type | Width
1910 1 | N/A-Leave Empty | RC | CHAR | 2
1911 2 | N/A-Leave Empty | CUSTID | CHAR | 15
1912 3 | Account Number | TRACCTNUM | CHAR | 15
1913 4 | Invoice Number | TRINVOICE | CHAR | 15
1914 5 | Line Sequence (sort order) | LINESEQ | NUM | 6
1915 6 | Transaction Detail | DETAILS | CHAR | 100
1916 7 | Amount | AMT | NUM* | 9
1917 8 | Line Format Control** | LNCTRL | CHAR | 2
1918 9 | Grouping Code | GROUP | CHAR | 2
1919 10 | User Defined | ACCT CODE | CHAR | 15
1921 If format is 'oneline', there is no detail file. Each invoice has a
1922 header line only, with the fields:
1924 Agent number, agent name, customer number, first name, last name, address
1925 line 1, address line 2, city, state, zip, invoice date, invoice number,
1926 amount charged, amount due, previous balance, due date.
1928 and then, for each line item, three columns containing the package number,
1929 description, and amount.
1931 If format is 'bridgestone', there is no detail file. Each invoice has a
1932 header line with the following fields in a fixed-width format:
1934 Customer number (in display format), date, name (first last), company,
1935 address 1, address 2, city, state, zip.
1937 This is a mailing list format, and has no per-invoice fields. To avoid
1938 sending redundant notices, the spooling event should have a "once" or
1939 "once_percust_every" condition.
1944 my($self, %opt) = @_;
1946 eval "use Text::CSV_XS";
1949 my $cust_main = $self->cust_main;
1951 my $csv = Text::CSV_XS->new({'always_quote'=>1});
1952 my $format = lc($opt{'format'});
1954 my $time = $opt{'time'} || time;
1956 my $tracctnum = ''; #leaking out from billco-specific sections :/
1957 if ( $format eq 'billco' ) {
1960 $self->conf->config('billco-account_num', $cust_main->agentnum);
1962 $tracctnum = $account_num eq 'display_custnum'
1963 ? $cust_main->display_custnum
1964 : $opt{'tracctnum'};
1967 $taxtotal += $_->{'amount'} foreach $self->_items_tax;
1969 my $duedate = $self->due_date2str('%m/%d/%Y'); #date_format?
1971 my( $previous_balance, @unused ) = $self->previous; #previous balance
1973 my $pmt_cr_applied = 0;
1974 $pmt_cr_applied += $_->{'amount'}
1975 foreach ( $self->_items_payments(%opt), $self->_items_credits(%opt) ) ;
1977 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1980 '', # 1 | N/A-Leave Empty CHAR 2
1981 '', # 2 | N/A-Leave Empty CHAR 15
1982 $tracctnum, # 3 | Transaction Account No CHAR 15
1983 $self->invnum, # 4 | Transaction Invoice No CHAR 15
1984 $cust_main->zip, # 5 | Transaction Zip Code CHAR 5
1985 $cust_main->company, # 6 | Transaction Company Bill To CHAR 30
1986 #$cust_main->payname, # 7 | Transaction Contact Bill To CHAR 30
1987 $cust_main->contact, # 7 | Transaction Contact Bill To CHAR 30
1988 $cust_main->address2, # 8 | Additional Address Unit Info CHAR 30
1989 $cust_main->address1, # 9 | Bill To Street Address CHAR 30
1990 '', # 10 | Ancillary Billing Information CHAR 30
1991 $cust_main->city, # 11 | Transaction City Bill To CHAR 20
1992 $cust_main->state, # 12 | Transaction State Bill To CHAR 2
1995 time2str("%m/%d/%Y", $self->_date), # 13 | Bill Cycle Close Date CHAR 10
1998 $duedate, # 14 | Bill Due Date CHAR 10
2000 $previous_balance, # 15 | Previous Balance NUM* 9
2001 $pmt_cr_applied, # 16 | Pmt/CR Applied NUM* 9
2002 sprintf("%.2f", $self->charged), # 17 | Total Current Charges NUM* 9
2003 $totaldue, # 18 | Total Amt Due NUM* 9
2004 $totaldue, # 19 | Total Amt Due NUM* 9
2005 '', # 20 | 30 Day Aging NUM* 9
2006 '', # 21 | 60 Day Aging NUM* 9
2007 '', # 22 | 90 Day Aging NUM* 9
2008 'N', # 23 | Y/N CHAR 1
2009 '', # 24 | Remittance automation CHAR 100
2010 $taxtotal, # 25 | Total Taxes & Fees NUM* 9
2011 $self->custnum, # 26 | Customer Reference Number CHAR 15
2012 '0', # 27 | Federal Tax*** NUM* 9
2013 sprintf("%.2f", $taxtotal), # 28 | State Tax*** NUM* 9
2014 '0', # 29 | Other Taxes & Fees*** NUM* 9
2017 } elsif ( $format eq 'oneline' ) { #name
2019 my ($previous_balance) = $self->previous;
2020 $previous_balance = sprintf('%.2f', $previous_balance);
2021 my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
2027 $self->_items_pkg, #_items_nontax? no sections or anything
2032 $cust_main->agentnum,
2033 $cust_main->agent->agent,
2037 $cust_main->company,
2038 $cust_main->address1,
2039 $cust_main->address2,
2045 time2str("%x", $self->_date),
2050 $self->due_date2str("%x"),
2055 } elsif ( $format eq 'bridgestone' ) {
2057 # bypass the CSV stuff and just return this
2058 my $longdate = time2str('%B %d, %Y', $time); #current time, right?
2059 my $zip = $cust_main->zip;
2061 my $prefix = $self->conf->config('bridgestone-prefix', $cust_main->agentnum)
2065 "%-5s%-15s%-20s%-30s%-30s%-30s%-30s%-20s%-2s%-9s\n",
2067 $cust_main->display_custnum,
2069 uc(substr($cust_main->contact_firstlast,0,30)),
2070 uc(substr($cust_main->company ,0,30)),
2071 uc(substr($cust_main->address1 ,0,30)),
2072 uc(substr($cust_main->address2 ,0,30)),
2073 uc(substr($cust_main->city ,0,20)),
2074 uc($cust_main->state),
2080 } elsif ( $format eq 'ics' ) {
2082 my $bill = $cust_main->bill_location;
2083 my $zip = $bill->zip;
2087 if ( $zip =~ /^(\d{5})(\d{4})$/ ) {
2092 # minor false laziness with print_generic
2093 my ($previous_balance) = $self->previous;
2094 my $balance_due = $self->owed + $previous_balance;
2095 my $payment_total = sum(0, map { $_->{'amount'} } $self->_items_payments);
2096 my $credit_total = sum(0, map { $_->{'amount'} } $self->_items_credits);
2099 if ( $self->due_date and $time >= $self->due_date ) {
2100 $past_due = sprintf('Past due:$%0.2f Due Immediately', $balance_due);
2104 my $header = sprintf(
2105 '%-10s%-30s%-48s%-2s%-50s%-30s%-30s%-25s%-2s%-5s%-4s%-8s%-8s%-10s%-10s%-10s%-10s%-10s%-10s%-480s%-35s',
2106 $cust_main->display_custnum, #BID
2107 uc($cust_main->first), #FNAME
2108 uc($cust_main->last), #LNAME
2109 '00', #BATCH, should this ever be anything else?
2110 uc($cust_main->company), #COMP
2111 uc($bill->address1), #STREET1
2112 uc($bill->address2), #STREET2
2113 uc($bill->city), #CITY
2114 uc($bill->state), #STATE
2117 time2str('%Y%m%d', $self->_date), #BILL_DATE
2118 $self->due_date2str('%Y%m%d'), #DUE_DATE,
2119 ( map {sprintf('%0.2f', $_)}
2120 $balance_due, #AMNT_DUE
2121 $previous_balance, #PREV_BAL
2122 $payment_total, #PYMT_RCVD
2123 $credit_total, #CREDITS
2124 $previous_balance, #BEG_BAL--is this correct?
2125 $self->charged, #NEW_CHRG
2128 $past_due, #PAST_MSG
2132 my %svc_class = ('' => ''); # maybe cache this more persistently?
2134 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2136 my $show_pkgnum = $cust_bill_pkg->pkgnum || '';
2137 my $cust_pkg = $cust_bill_pkg->cust_pkg if $show_pkgnum;
2141 my @dates = ( $self->_date, undef );
2142 if ( my $prev = $cust_bill_pkg->previous_cust_bill_pkg ) {
2143 $dates[1] = $prev->sdate; #questionable
2146 # generate an 01 detail for each service
2147 my @svcs = $cust_pkg->h_cust_svc(@dates, 'I');
2148 foreach my $cust_svc ( @svcs ) {
2149 $show_pkgnum = ''; # hide it if we're showing svcnums
2151 my $svcpart = $cust_svc->svcpart;
2152 if (!exists($svc_class{$svcpart})) {
2153 my $classnum = $cust_svc->part_svc->classnum;
2154 my $part_svc_class = FS::part_svc_class->by_key($classnum)
2156 $svc_class{$svcpart} = $part_svc_class ?
2157 $part_svc_class->classname :
2161 my @h_label = $cust_svc->label(@dates, 'I');
2162 push @details, sprintf('01%-9s%-20s%-47s',
2164 $svc_class{$svcpart},
2167 } #foreach $cust_svc
2170 my $desc = $cust_bill_pkg->desc; # itemdesc or part_pkg.pkg
2171 if ($cust_bill_pkg->recur > 0) {
2172 $desc .= ' '.time2str('%d-%b-%Y', $cust_bill_pkg->sdate).' to '.
2173 time2str('%d-%b-%Y', $cust_bill_pkg->edate - 86400);
2175 push @details, sprintf('02%-6s%-60s%-10s',
2178 sprintf('%0.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
2180 } #foreach $cust_bill_pkg
2182 # Tag this row so that we know whether this is one page (1), two pages
2183 # (2), # or "big" (B). The tag will be stripped off before uploading.
2184 if ( scalar(@details) < 12 ) {
2186 } elsif ( scalar(@details) < 58 ) {
2192 return join('', $header, @details, "\n");
2200 time2str("%x", $self->_date),
2201 sprintf("%.2f", $self->charged),
2202 ( map { $cust_main->getfield($_) }
2203 qw( first last company address1 address2 city state zip country ) ),
2205 ) or die "can't create csv";
2208 my $header = $csv->string. "\n";
2211 if ( lc($opt{'format'}) eq 'billco' ) {
2214 foreach my $item ( $self->_items_pkg ) {
2217 '', # 1 | N/A-Leave Empty CHAR 2
2218 '', # 2 | N/A-Leave Empty CHAR 15
2219 $tracctnum, # 3 | Account Number CHAR 15
2220 $self->invnum, # 4 | Invoice Number CHAR 15
2221 $lineseq++, # 5 | Line Sequence (sort order) NUM 6
2222 $item->{'description'}, # 6 | Transaction Detail CHAR 100
2223 $item->{'amount'}, # 7 | Amount NUM* 9
2224 '', # 8 | Line Format Control** CHAR 2
2225 '', # 9 | Grouping Code CHAR 2
2226 '', # 10 | User Defined CHAR 15
2229 $detail .= $csv->string. "\n";
2233 } elsif ( lc($opt{'format'}) eq 'oneline' ) {
2239 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2241 my($pkg, $setup, $recur, $sdate, $edate);
2242 if ( $cust_bill_pkg->pkgnum ) {
2244 ($pkg, $setup, $recur, $sdate, $edate) = (
2245 $cust_bill_pkg->part_pkg->pkg,
2246 ( $cust_bill_pkg->setup != 0
2247 ? sprintf("%.2f", $cust_bill_pkg->setup )
2249 ( $cust_bill_pkg->recur != 0
2250 ? sprintf("%.2f", $cust_bill_pkg->recur )
2252 ( $cust_bill_pkg->sdate
2253 ? time2str("%x", $cust_bill_pkg->sdate)
2255 ($cust_bill_pkg->edate
2256 ? time2str("%x", $cust_bill_pkg->edate)
2260 } else { #pkgnum tax
2261 next unless $cust_bill_pkg->setup != 0;
2262 $pkg = $cust_bill_pkg->desc;
2263 $setup = sprintf('%10.2f', $cust_bill_pkg->setup );
2264 ( $sdate, $edate ) = ( '', '' );
2270 ( map { '' } (1..11) ),
2271 ($pkg, $setup, $recur, $sdate, $edate)
2272 ) or die "can't create csv";
2274 $detail .= $csv->string. "\n";
2280 ( $header, $detail );
2286 Pays this invoice with a compliemntary payment. If there is an error,
2287 returns the error, otherwise returns false.
2293 my $cust_pay = new FS::cust_pay ( {
2294 'invnum' => $self->invnum,
2295 'paid' => $self->owed,
2298 'payinfo' => $self->cust_main->payinfo,
2306 Attempts to pay this invoice with a credit card payment via a
2307 Business::OnlinePayment realtime gateway. See
2308 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2309 for supported processors.
2315 $self->realtime_bop( 'CC', @_ );
2320 Attempts to pay this invoice with an electronic check (ACH) payment via a
2321 Business::OnlinePayment realtime gateway. See
2322 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2323 for supported processors.
2329 $self->realtime_bop( 'ECHECK', @_ );
2334 Attempts to pay this invoice with phone bill (LEC) payment via a
2335 Business::OnlinePayment realtime gateway. See
2336 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2337 for supported processors.
2343 $self->realtime_bop( 'LEC', @_ );
2347 my( $self, $method ) = (shift,shift);
2348 my $conf = $self->conf;
2351 my $cust_main = $self->cust_main;
2352 my $balance = $cust_main->balance;
2353 my $amount = ( $balance < $self->owed ) ? $balance : $self->owed;
2354 $amount = sprintf("%.2f", $amount);
2355 return "not run (balance $balance)" unless $amount > 0;
2357 my $description = 'Internet Services';
2358 if ( $conf->exists('business-onlinepayment-description') ) {
2359 my $dtempl = $conf->config('business-onlinepayment-description');
2361 my $agent_obj = $cust_main->agent
2362 or die "can't retreive agent for $cust_main (agentnum ".
2363 $cust_main->agentnum. ")";
2364 my $agent = $agent_obj->agent;
2365 my $pkgs = join(', ',
2366 map { $_->part_pkg->pkg }
2367 grep { $_->pkgnum } $self->cust_bill_pkg
2369 $description = eval qq("$dtempl");
2372 $cust_main->realtime_bop($method, $amount,
2373 'description' => $description,
2374 'invnum' => $self->invnum,
2375 #this didn't do what we want, it just calls apply_payments_and_credits
2377 'apply_to_invoice' => 1,
2380 #this changes application behavior: auto payments
2381 #triggered against a specific invoice are now applied
2382 #to that invoice instead of oldest open.
2388 =item batch_card OPTION => VALUE...
2390 Adds a payment for this invoice to the pending credit card batch (see
2391 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2392 runs the payment using a realtime gateway.
2397 my ($self, %options) = @_;
2398 my $cust_main = $self->cust_main;
2400 $options{invnum} = $self->invnum;
2402 $cust_main->batch_card(%options);
2405 sub _agent_template {
2407 $self->cust_main->agent_template;
2410 sub _agent_invoice_from {
2412 $self->cust_main->agent_invoice_from;
2415 =item invoice_barcode DIR_OR_FALSE
2417 Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
2418 it is taken as the temp directory where the PNG file will be generated and the
2419 PNG file name is returned. Otherwise, the PNG image itself is returned.
2423 sub invoice_barcode {
2424 my ($self, $dir) = (shift,shift);
2426 my $gdbar = new GD::Barcode('Code39',$self->invnum);
2427 die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
2428 my $gd = $gdbar->plot(Height => 30);
2431 my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX',
2435 ) or die "can't open temp file: $!\n";
2436 print $bh $gd->png or die "cannot write barcode to file: $!\n";
2437 my $png_file = $bh->filename;
2444 =item invnum_date_pretty
2446 Returns a string with the invoice number and date, for example:
2447 "Invoice #54 (3/20/2008)"
2451 sub invnum_date_pretty {
2453 $self->mt('Invoice #'). $self->invnum. ' ('. $self->_date_pretty. ')';
2456 #sub _items_extra_usage_sections {
2458 # my $escape = shift;
2460 # my %sections = ();
2462 # my %usage_class = map{ $_->classname, $_ } qsearch('usage_class', {});
2463 # foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
2465 # next unless $cust_bill_pkg->pkgnum > 0;
2467 # foreach my $section ( keys %usage_class ) {
2469 # my $usage = $cust_bill_pkg->usage($section);
2471 # next unless $usage && $usage > 0;
2473 # $sections{$section} ||= 0;
2474 # $sections{$section} += $usage;
2480 # map { { 'description' => &{$escape}($_),
2481 # 'subtotal' => $sections{$_},
2482 # 'summarized' => '',
2483 # 'tax_section' => '',
2486 # sort {$usage_class{$a}->weight <=> $usage_class{$b}->weight} keys %sections;
2490 sub _items_extra_usage_sections {
2492 my $conf = $self->conf;
2500 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
2502 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2503 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2504 next unless $cust_bill_pkg->pkgnum > 0;
2506 foreach my $classnum ( keys %usage_class ) {
2507 my $section = $usage_class{$classnum}->classname;
2508 $classnums{$section} = $classnum;
2510 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail($classnum) ) {
2511 my $amount = $detail->amount;
2512 next unless $amount && $amount > 0;
2514 $sections{$section} ||= { 'subtotal'=>0, 'calls'=>0, 'duration'=>0 };
2515 $sections{$section}{amount} += $amount; #subtotal
2516 $sections{$section}{calls}++;
2517 $sections{$section}{duration} += $detail->duration;
2519 my $desc = $detail->regionname;
2520 my $description = $desc;
2521 $description = substr($desc, 0, $maxlength). '...'
2522 if $format eq 'latex' && length($desc) > $maxlength;
2524 $lines{$section}{$desc} ||= {
2525 description => &{$escape}($description),
2526 #pkgpart => $part_pkg->pkgpart,
2527 pkgnum => $cust_bill_pkg->pkgnum,
2532 #unit_amount => $cust_bill_pkg->unitrecur,
2533 quantity => $cust_bill_pkg->quantity,
2534 product_code => 'N/A',
2535 ext_description => [],
2538 $lines{$section}{$desc}{amount} += $amount;
2539 $lines{$section}{$desc}{calls}++;
2540 $lines{$section}{$desc}{duration} += $detail->duration;
2546 my %sectionmap = ();
2547 foreach (keys %sections) {
2548 my $usage_class = $usage_class{$classnums{$_}};
2549 $sectionmap{$_} = { 'description' => &{$escape}($_),
2550 'amount' => $sections{$_}{amount}, #subtotal
2551 'calls' => $sections{$_}{calls},
2552 'duration' => $sections{$_}{duration},
2554 'tax_section' => '',
2555 'sort_weight' => $usage_class->weight,
2556 ( $usage_class->format
2557 ? ( map { $_ => $usage_class->$_($format) }
2558 qw( description_generator header_generator total_generator total_line_generator )
2565 my @sections = sort { $a->{sort_weight} <=> $b->{sort_weight} }
2569 foreach my $section ( keys %lines ) {
2570 foreach my $line ( keys %{$lines{$section}} ) {
2571 my $l = $lines{$section}{$line};
2572 $l->{section} = $sectionmap{$section};
2573 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2574 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2579 return(\@sections, \@lines);
2585 my $end = $self->_date;
2587 # start at date of previous invoice + 1 second or 0 if no previous invoice
2588 my $start = $self->scalar_sql("SELECT max(_date) FROM cust_bill WHERE custnum = ? and invnum != ?",$self->custnum,$self->invnum);
2589 $start = 0 if !$start;
2592 my $cust_main = $self->cust_main;
2593 my @pkgs = $cust_main->all_pkgs;
2594 my($num_activated,$num_deactivated,$num_portedin,$num_portedout,$minutes)
2597 foreach my $pkg ( @pkgs ) {
2598 my @h_cust_svc = $pkg->h_cust_svc($end);
2599 foreach my $h_cust_svc ( @h_cust_svc ) {
2600 next if grep {$_ eq $h_cust_svc->svcnum} @seen;
2601 next unless $h_cust_svc->part_svc->svcdb eq 'svc_phone';
2603 my $inserted = $h_cust_svc->date_inserted;
2604 my $deleted = $h_cust_svc->date_deleted;
2605 my $phone_inserted = $h_cust_svc->h_svc_x($inserted+5);
2607 $phone_deleted = $h_cust_svc->h_svc_x($deleted) if $deleted;
2609 # DID either activated or ported in; cannot be both for same DID simultaneously
2610 if ($inserted >= $start && $inserted <= $end && $phone_inserted
2611 && (!$phone_inserted->lnp_status
2612 || $phone_inserted->lnp_status eq ''
2613 || $phone_inserted->lnp_status eq 'native')) {
2616 else { # this one not so clean, should probably move to (h_)svc_phone
2617 my $phone_portedin = qsearchs( 'h_svc_phone',
2618 { 'svcnum' => $h_cust_svc->svcnum,
2619 'lnp_status' => 'portedin' },
2620 FS::h_svc_phone->sql_h_searchs($end),
2622 $num_portedin++ if $phone_portedin;
2625 # DID either deactivated or ported out; cannot be both for same DID simultaneously
2626 if($deleted >= $start && $deleted <= $end && $phone_deleted
2627 && (!$phone_deleted->lnp_status
2628 || $phone_deleted->lnp_status ne 'portingout')) {
2631 elsif($deleted >= $start && $deleted <= $end && $phone_deleted
2632 && $phone_deleted->lnp_status
2633 && $phone_deleted->lnp_status eq 'portingout') {
2637 # increment usage minutes
2638 if ( $phone_inserted ) {
2639 my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end,'billsec_sum'=>1);
2640 $minutes = $cdrs[0]->billsec_sum if scalar(@cdrs) == 1;
2643 warn "WARNING: no matching h_svc_phone insert record for insert time $inserted, svcnum " . $h_cust_svc->svcnum;
2646 # don't look at this service again
2647 push @seen, $h_cust_svc->svcnum;
2651 $minutes = sprintf("%d", $minutes);
2652 ("Activated: $num_activated Ported-In: $num_portedin Deactivated: "
2653 . "$num_deactivated Ported-Out: $num_portedout ",
2654 "Total Minutes: $minutes");
2657 sub _items_accountcode_cdr {
2662 my $section = { 'amount' => 0,
2665 'sort_weight' => '',
2667 'description' => 'Usage by Account Code',
2673 my %accountcodes = ();
2675 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2676 next unless $cust_bill_pkg->pkgnum > 0;
2678 my @header = $cust_bill_pkg->details_header;
2679 next unless scalar(@header);
2680 $section->{'header'} = join(',',@header);
2682 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2684 $section->{'header'} = $detail->formatted('format' => $format)
2685 if($detail->detail eq $section->{'header'});
2687 my $accountcode = $detail->accountcode;
2688 next unless $accountcode;
2690 my $amount = $detail->amount;
2691 next unless $amount && $amount > 0;
2693 $accountcodes{$accountcode} ||= {
2694 description => $accountcode,
2701 product_code => 'N/A',
2702 section => $section,
2703 ext_description => [ $section->{'header'} ],
2707 $section->{'amount'} += $amount;
2708 $accountcodes{$accountcode}{'amount'} += $amount;
2709 $accountcodes{$accountcode}{calls}++;
2710 $accountcodes{$accountcode}{duration} += $detail->duration;
2711 push @{$accountcodes{$accountcode}{detail_temp}}, $detail;
2715 foreach my $l ( values %accountcodes ) {
2716 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2717 my @sorted_detail = sort { $a->startdate <=> $b->startdate } @{$l->{detail_temp}};
2718 foreach my $sorted_detail ( @sorted_detail ) {
2719 push @{$l->{ext_description}}, $sorted_detail->formatted('format'=>$format);
2721 delete $l->{detail_temp};
2725 my @sorted_lines = sort { $a->{'description'} <=> $b->{'description'} } @lines;
2727 return ($section,\@sorted_lines);
2730 sub _items_svc_phone_sections {
2732 my $conf = $self->conf;
2740 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
2742 my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} );
2743 $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
2745 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2746 next unless $cust_bill_pkg->pkgnum > 0;
2748 my @header = $cust_bill_pkg->details_header;
2749 next unless scalar(@header);
2751 foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
2753 my $phonenum = $detail->phonenum;
2754 next unless $phonenum;
2756 my $amount = $detail->amount;
2757 next unless $amount && $amount > 0;
2759 $sections{$phonenum} ||= { 'amount' => 0,
2762 'sort_weight' => -1,
2763 'phonenum' => $phonenum,
2765 $sections{$phonenum}{amount} += $amount; #subtotal
2766 $sections{$phonenum}{calls}++;
2767 $sections{$phonenum}{duration} += $detail->duration;
2769 my $desc = $detail->regionname;
2770 my $description = $desc;
2771 $description = substr($desc, 0, $maxlength). '...'
2772 if $format eq 'latex' && length($desc) > $maxlength;
2774 $lines{$phonenum}{$desc} ||= {
2775 description => &{$escape}($description),
2776 #pkgpart => $part_pkg->pkgpart,
2784 product_code => 'N/A',
2785 ext_description => [],
2788 $lines{$phonenum}{$desc}{amount} += $amount;
2789 $lines{$phonenum}{$desc}{calls}++;
2790 $lines{$phonenum}{$desc}{duration} += $detail->duration;
2792 my $line = $usage_class{$detail->classnum}->classname;
2793 $sections{"$phonenum $line"} ||=
2797 'sort_weight' => $usage_class{$detail->classnum}->weight,
2798 'phonenum' => $phonenum,
2799 'header' => [ @header ],
2801 $sections{"$phonenum $line"}{amount} += $amount; #subtotal
2802 $sections{"$phonenum $line"}{calls}++;
2803 $sections{"$phonenum $line"}{duration} += $detail->duration;
2805 $lines{"$phonenum $line"}{$desc} ||= {
2806 description => &{$escape}($description),
2807 #pkgpart => $part_pkg->pkgpart,
2815 product_code => 'N/A',
2816 ext_description => [],
2819 $lines{"$phonenum $line"}{$desc}{amount} += $amount;
2820 $lines{"$phonenum $line"}{$desc}{calls}++;
2821 $lines{"$phonenum $line"}{$desc}{duration} += $detail->duration;
2822 push @{$lines{"$phonenum $line"}{$desc}{ext_description}},
2823 $detail->formatted('format' => $format);
2828 my %sectionmap = ();
2829 my $simple = new FS::usage_class { format => 'simple' }; #bleh
2830 foreach ( keys %sections ) {
2831 my @header = @{ $sections{$_}{header} || [] };
2833 new FS::usage_class { format => 'usage_'. (scalar(@header) || 6). 'col' };
2834 my $summary = $sections{$_}{sort_weight} < 0 ? 1 : 0;
2835 my $usage_class = $summary ? $simple : $usage_simple;
2836 my $ending = $summary ? ' usage charges' : '';
2839 $gen_opt{label} = [ map{ &{$escape}($_) } @header ];
2841 $sectionmap{$_} = { 'description' => &{$escape}($_. $ending),
2842 'amount' => $sections{$_}{amount}, #subtotal
2843 'calls' => $sections{$_}{calls},
2844 'duration' => $sections{$_}{duration},
2846 'tax_section' => '',
2847 'phonenum' => $sections{$_}{phonenum},
2848 'sort_weight' => $sections{$_}{sort_weight},
2849 'post_total' => $summary, #inspire pagebreak
2851 ( map { $_ => $usage_class->$_($format, %gen_opt) }
2852 qw( description_generator
2855 total_line_generator
2862 my @sections = sort { $a->{phonenum} cmp $b->{phonenum} ||
2863 $a->{sort_weight} <=> $b->{sort_weight}
2868 foreach my $section ( keys %lines ) {
2869 foreach my $line ( keys %{$lines{$section}} ) {
2870 my $l = $lines{$section}{$line};
2871 $l->{section} = $sectionmap{$section};
2872 $l->{amount} = sprintf( "%.2f", $l->{amount} );
2873 #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
2878 if($conf->exists('phone_usage_class_summary')) {
2879 # this only works with Latex
2883 # after this, we'll have only two sections per DID:
2884 # Calls Summary and Calls Detail
2885 foreach my $section ( @sections ) {
2886 if($section->{'post_total'}) {
2887 $section->{'description'} = 'Calls Summary: '.$section->{'phonenum'};
2888 $section->{'total_line_generator'} = sub { '' };
2889 $section->{'total_generator'} = sub { '' };
2890 $section->{'header_generator'} = sub { '' };
2891 $section->{'description_generator'} = '';
2892 push @newsections, $section;
2893 my %calls_detail = %$section;
2894 $calls_detail{'post_total'} = '';
2895 $calls_detail{'sort_weight'} = '';
2896 $calls_detail{'description_generator'} = sub { '' };
2897 $calls_detail{'header_generator'} = sub {
2898 return ' & Date/Time & Called Number & Duration & Price'
2899 if $format eq 'latex';
2902 $calls_detail{'description'} = 'Calls Detail: '
2903 . $section->{'phonenum'};
2904 push @newsections, \%calls_detail;
2908 # after this, each usage class is collapsed/summarized into a single
2909 # line under the Calls Summary section
2910 foreach my $newsection ( @newsections ) {
2911 if($newsection->{'post_total'}) { # this means Calls Summary
2912 foreach my $section ( @sections ) {
2913 next unless ($section->{'phonenum'} eq $newsection->{'phonenum'}
2914 && !$section->{'post_total'});
2915 my $newdesc = $section->{'description'};
2916 my $tn = $section->{'phonenum'};
2917 $newdesc =~ s/$tn//g;
2918 my $line = { ext_description => [],
2922 calls => $section->{'calls'},
2923 section => $newsection,
2924 duration => $section->{'duration'},
2925 description => $newdesc,
2926 amount => sprintf("%.2f",$section->{'amount'}),
2927 product_code => 'N/A',
2929 push @newlines, $line;
2934 # after this, Calls Details is populated with all CDRs
2935 foreach my $newsection ( @newsections ) {
2936 if(!$newsection->{'post_total'}) { # this means Calls Details
2937 foreach my $line ( @lines ) {
2938 next unless (scalar(@{$line->{'ext_description'}}) &&
2939 $line->{'section'}->{'phonenum'} eq $newsection->{'phonenum'}
2941 my @extdesc = @{$line->{'ext_description'}};
2943 foreach my $extdesc ( @extdesc ) {
2944 $extdesc =~ s/scriptsize/normalsize/g if $format eq 'latex';
2945 push @newextdesc, $extdesc;
2947 $line->{'ext_description'} = \@newextdesc;
2948 $line->{'section'} = $newsection;
2949 push @newlines, $line;
2954 return(\@newsections, \@newlines);
2957 return(\@sections, \@lines);
2961 sub _items_previous {
2963 my $conf = $self->conf;
2964 my $cust_main = $self->cust_main;
2965 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
2967 foreach ( @pr_cust_bill ) {
2968 my $date = $conf->exists('invoice_show_prior_due_date')
2969 ? 'due '. $_->due_date2str($date_format)
2970 : $self->time2str_local($date_format, $_->_date);
2972 'description' => $self->mt('Previous Balance, Invoice #'). $_->invnum. " ($date)",
2973 #'pkgpart' => 'N/A',
2975 'amount' => sprintf("%.2f", $_->owed),
2981 # 'description' => 'Previous Balance',
2982 # #'pkgpart' => 'N/A',
2983 # 'pkgnum' => 'N/A',
2984 # 'amount' => sprintf("%10.2f", $pr_total ),
2985 # 'ext_description' => [ map {
2986 # "Invoice ". $_->invnum.
2987 # " (". time2str("%x",$_->_date). ") ".
2988 # sprintf("%10.2f", $_->owed)
2989 # } @pr_cust_bill ],
2994 sub _items_credits {
2995 my( $self, %opt ) = @_;
2996 my $trim_len = $opt{'trim_len'} || 60;
3001 if ( $self->conf->exists('previous_balance-payments_since') ) {
3002 if ( $opt{'template'} eq 'statement' ) {
3003 # then the current bill is a "statement" (i.e. an invoice sent as
3004 # a payment receipt)
3005 # and in that case we want to see payments on or after THIS invoice
3006 @objects = qsearch('cust_credit', {
3007 'custnum' => $self->custnum,
3008 '_date' => {op => '>=', value => $self->_date},
3012 $date = $self->previous_bill->_date if $self->previous_bill;
3013 @objects = qsearch('cust_credit', {
3014 'custnum' => $self->custnum,
3015 '_date' => {op => '>=', value => $date},
3019 @objects = $self->cust_credited;
3022 foreach my $obj ( @objects ) {
3023 my $cust_credit = $obj->isa('FS::cust_credit') ? $obj : $obj->cust_credit;
3025 my $reason = substr($cust_credit->reason, 0, $trim_len);
3026 $reason .= '...' if length($reason) < length($cust_credit->reason);
3027 $reason = " ($reason) " if $reason;
3030 #'description' => 'Credit ref\#'. $_->crednum.
3031 # " (". time2str("%x",$_->cust_credit->_date) .")".
3033 'description' => $self->mt('Credit applied').' '.
3034 $self->time2str_local($date_format,$obj->_date). $reason,
3035 'amount' => sprintf("%.2f",$obj->amount),
3043 sub _items_payments {
3048 my $detailed = $self->conf->exists('invoice_payment_details');
3050 if ( $self->conf->exists('previous_balance-payments_since') ) {
3051 # then show payments dated on/after the previous bill...
3052 if ( $opt{'template'} eq 'statement' ) {
3053 # then the current bill is a "statement" (i.e. an invoice sent as
3054 # a payment receipt)
3055 # and in that case we want to see payments on or after THIS invoice
3056 @objects = qsearch('cust_pay', {
3057 'custnum' => $self->custnum,
3058 '_date' => {op => '>=', value => $self->_date},
3061 # the normal case: payments on or after the previous invoice
3063 $date = $self->previous_bill->_date if $self->previous_bill;
3064 @objects = qsearch('cust_pay', {
3065 'custnum' => $self->custnum,
3066 '_date' => {op => '>=', value => $date},
3068 # and before the current bill...
3069 @objects = grep { $_->_date < $self->_date } @objects;
3072 @objects = $self->cust_bill_pay;
3075 foreach my $obj (@objects) {
3076 my $cust_pay = $obj->isa('FS::cust_pay') ? $obj : $obj->cust_pay;
3077 my $desc = $self->mt('Payment received').' '.
3078 $self->time2str_local($date_format, $cust_pay->_date );
3079 $desc .= $self->mt(' via ') .
3080 $cust_pay->payby_payinfo_pretty( $self->cust_main->locale )
3084 'description' => $desc,
3085 'amount' => sprintf("%.2f", $obj->amount )
3093 =item call_details [ OPTION => VALUE ... ]
3095 Returns an array of CSV strings representing the call details for this invoice
3096 The only option available is the boolean prepend_billed_number
3101 my ($self, %opt) = @_;
3103 my $format_function = sub { shift };
3105 if ($opt{prepend_billed_number}) {
3106 $format_function = sub {
3110 $row->amount ? $row->phonenum. ",". $detail : '"Billed number",'. $detail;
3115 my @details = map { $_->details( 'format_function' => $format_function,
3116 'escape_function' => sub{ return() },
3120 $self->cust_bill_pkg;
3121 my $header = $details[0];
3122 ( $header, grep { $_ ne $header } @details );
3132 =item process_reprint
3136 sub process_reprint {
3137 process_re_X('print', @_);
3140 =item process_reemail
3144 sub process_reemail {
3145 process_re_X('email', @_);
3153 process_re_X('fax', @_);
3161 process_re_X('ftp', @_);
3168 sub process_respool {
3169 process_re_X('spool', @_);
3172 use Storable qw(thaw);
3176 my( $method, $job ) = ( shift, shift );
3177 warn "$me process_re_X $method for job $job\n" if $DEBUG;
3179 my $param = thaw(decode_base64(shift));
3180 warn Dumper($param) if $DEBUG;
3191 # spool_invoice ftp_invoice fax_invoice print_invoice
3192 my($method, $job, %param ) = @_;
3194 warn "re_X $method for job $job with param:\n".
3195 join( '', map { " $_ => ". $param{$_}. "\n" } keys %param );
3198 #some false laziness w/search/cust_bill.html
3200 my $orderby = 'ORDER BY cust_bill._date';
3202 my $extra_sql = ' WHERE '. FS::cust_bill->search_sql_where(\%param);
3204 my $addl_from = 'LEFT JOIN cust_main USING ( custnum )';
3206 my @cust_bill = qsearch( {
3207 #'select' => "cust_bill.*",
3208 'table' => 'cust_bill',
3209 'addl_from' => $addl_from,
3211 'extra_sql' => $extra_sql,
3212 'order_by' => $orderby,
3216 $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
3218 warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
3221 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
3222 foreach my $cust_bill ( @cust_bill ) {
3223 $cust_bill->$method();
3225 if ( $job ) { #progressbar foo
3227 if ( time - $min_sec > $last ) {
3228 my $error = $job->update_statustext(
3229 int( 100 * $num / scalar(@cust_bill) )
3231 die $error if $error;
3242 =head1 CLASS METHODS
3248 Returns an SQL fragment to retreive the amount owed (charged minus credited and paid).
3253 my ($class, $start, $end) = @_;
3255 $class->paid_sql($start, $end). ' - '.
3256 $class->credited_sql($start, $end);
3261 Returns an SQL fragment to retreive the net amount (charged minus credited).
3266 my ($class, $start, $end) = @_;
3267 'charged - '. $class->credited_sql($start, $end);
3272 Returns an SQL fragment to retreive the amount paid against this invoice.
3277 my ($class, $start, $end) = @_;
3278 $start &&= "AND cust_bill_pay._date <= $start";
3279 $end &&= "AND cust_bill_pay._date > $end";
3280 $start = '' unless defined($start);
3281 $end = '' unless defined($end);
3282 "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
3283 WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end )";
3288 Returns an SQL fragment to retreive the amount credited against this invoice.
3293 my ($class, $start, $end) = @_;
3294 $start &&= "AND cust_credit_bill._date <= $start";
3295 $end &&= "AND cust_credit_bill._date > $end";
3296 $start = '' unless defined($start);
3297 $end = '' unless defined($end);
3298 "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
3299 WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end )";
3304 Returns an SQL fragment to retrieve the due date of an invoice.
3305 Currently only supported on PostgreSQL.
3310 my $conf = new FS::Conf;
3314 cust_bill.invoice_terms,
3315 cust_main.invoice_terms,
3316 \''.($conf->config('invoice_default_terms') || '').'\'
3317 ), E\'Net (\\\\d+)\'
3319 ) * 86400 + cust_bill._date'
3322 =item search_sql_where HASHREF
3324 Class method which returns an SQL WHERE fragment to search for parameters
3325 specified in HASHREF. Valid parameters are
3331 List reference of start date, end date, as UNIX timestamps.
3341 List reference of charged limits (exclusive).
3345 List reference of charged limits (exclusive).
3349 flag, return open invoices only
3353 flag, return net invoices only
3357 =item newest_percust
3361 Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
3365 sub search_sql_where {
3366 my($class, $param) = @_;
3368 warn "$me search_sql_where called with params: \n".
3369 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
3375 if ( $param->{'agentnum'} =~ /^(\d+)$/ ) {
3376 push @search, "cust_main.agentnum = $1";
3380 if ( $param->{'refnum'} =~ /^(\d+)$/ ) {
3381 push @search, "cust_main.refnum = $1";
3385 if ( $param->{'custnum'} =~ /^(\d+)$/ ) {
3386 push @search, "cust_bill.custnum = $1";
3389 #customer classnum (false laziness w/ cust_main/Search.pm)
3390 if ( $param->{'cust_classnum'} ) {
3392 my @classnum = ref( $param->{'cust_classnum'} )
3393 ? @{ $param->{'cust_classnum'} }
3394 : ( $param->{'cust_classnum'} );
3396 @classnum = grep /^(\d*)$/, @classnum;
3399 push @search, '( '. join(' OR ', map {
3400 $_ ? "cust_main.classnum = $_"
3401 : "cust_main.classnum IS NULL"
3411 if ( $param->{_date} ) {
3412 my($beginning, $ending) = @{$param->{_date}};
3414 push @search, "cust_bill._date >= $beginning",
3415 "cust_bill._date < $ending";
3419 if ( $param->{'invnum_min'} =~ /^(\d+)$/ ) {
3420 push @search, "cust_bill.invnum >= $1";
3422 if ( $param->{'invnum_max'} =~ /^(\d+)$/ ) {
3423 push @search, "cust_bill.invnum <= $1";
3427 if ( $param->{charged} ) {
3428 my @charged = ref($param->{charged})
3429 ? @{ $param->{charged} }
3430 : ($param->{charged});
3432 push @search, map { s/^charged/cust_bill.charged/; $_; }
3436 my $owed_sql = FS::cust_bill->owed_sql;
3439 if ( $param->{owed} ) {
3440 my @owed = ref($param->{owed})
3441 ? @{ $param->{owed} }
3443 push @search, map { s/^owed/$owed_sql/; $_; }
3448 push @search, "0 != $owed_sql"
3449 if $param->{'open'};
3450 push @search, '0 != '. FS::cust_bill->net_sql
3454 push @search, "cust_bill._date < ". (time-86400*$param->{'days'})
3455 if $param->{'days'};
3458 if ( $param->{'newest_percust'} ) {
3460 #$distinct = 'DISTINCT ON ( cust_bill.custnum )';
3461 #$orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
3463 my @newest_where = map { my $x = $_;
3464 $x =~ s/\bcust_bill\./newest_cust_bill./g;
3467 grep ! /^cust_main./, @search;
3468 my $newest_where = scalar(@newest_where)
3469 ? ' AND '. join(' AND ', @newest_where)
3473 push @search, "cust_bill._date = (
3474 SELECT(MAX(newest_cust_bill._date)) FROM cust_bill AS newest_cust_bill
3475 WHERE newest_cust_bill.custnum = cust_bill.custnum
3481 #promised_date - also has an option to accept nulls
3482 if ( $param->{promised_date} ) {
3483 my($beginning, $ending, $null) = @{$param->{promised_date}};
3485 push @search, "(( cust_bill.promised_date >= $beginning AND ".
3486 "cust_bill.promised_date < $ending )" .
3487 ($null ? ' OR cust_bill.promised_date IS NULL ) ' : ')');
3490 #agent virtualization
3491 my $curuser = $FS::CurrentUser::CurrentUser;
3492 if ( $curuser->username eq 'fs_queue'
3493 && $param->{'CurrentUser'} =~ /^(\w+)$/ ) {
3495 my $newuser = qsearchs('access_user', {
3496 'username' => $username,
3500 $curuser = $newuser;
3502 warn "$me WARNING: (fs_queue) can't find CurrentUser $username\n";
3505 push @search, $curuser->agentnums_sql;
3507 join(' AND ', @search );
3519 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
3520 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base