ba308bebac24aed3037948c9e93f4b678a08f08c
[freeside.git] / FS / FS / cust_bill.pm
1 package FS::cust_bill;
2
3 use strict;
4 use vars qw( @ISA $DEBUG $me $conf
5              $money_char $date_format $rdate_format $date_format_long );
6 use vars qw( $invoice_lines @buf ); #yuck
7 use Fcntl qw(:flock); #for spool_csv
8 use List::Util qw(min max);
9 use Date::Format;
10 use Text::Template 1.20;
11 use File::Temp 0.14;
12 use String::ShellQuote;
13 use HTML::Entities;
14 use Locale::Country;
15 use Storable qw( freeze thaw );
16 use GD::Barcode;
17 use FS::UID qw( datasrc );
18 use FS::Misc qw( send_email send_fax generate_ps generate_pdf do_print );
19 use FS::Record qw( qsearch qsearchs dbh );
20 use FS::cust_main_Mixin;
21 use FS::cust_main;
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;
26 use FS::cust_credit;
27 use FS::cust_pay;
28 use FS::cust_pkg;
29 use FS::cust_credit_bill;
30 use FS::pay_batch;
31 use FS::cust_pay_batch;
32 use FS::cust_bill_event;
33 use FS::cust_event;
34 use FS::part_pkg;
35 use FS::cust_bill_pay;
36 use FS::cust_bill_pay_batch;
37 use FS::part_bill_event;
38 use FS::payby;
39 use FS::bill_batch;
40 use FS::cust_bill_batch;
41 use Cwd;
42
43 @ISA = qw( FS::cust_main_Mixin FS::Record );
44
45 $DEBUG = 0;
46 $me = '[FS::cust_bill]';
47
48 #ask FS::UID to run this stuff for us later
49 FS::UID->install_callback( sub { 
50   $conf = new FS::Conf;
51   $money_char       = $conf->config('money_char')       || '$';  
52   $date_format      = $conf->config('date_format')      || '%x'; #/YY
53   $rdate_format     = $conf->config('date_format')      || '%m/%d/%Y';  #/YYYY
54   $date_format_long = $conf->config('date_format_long') || '%b %o, %Y';
55 } );
56
57 =head1 NAME
58
59 FS::cust_bill - Object methods for cust_bill records
60
61 =head1 SYNOPSIS
62
63   use FS::cust_bill;
64
65   $record = new FS::cust_bill \%hash;
66   $record = new FS::cust_bill { 'column' => 'value' };
67
68   $error = $record->insert;
69
70   $error = $new_record->replace($old_record);
71
72   $error = $record->delete;
73
74   $error = $record->check;
75
76   ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
77
78   @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
79
80   ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
81
82   @cust_pay_objects = $cust_bill->cust_pay;
83
84   $tax_amount = $record->tax;
85
86   @lines = $cust_bill->print_text;
87   @lines = $cust_bill->print_text $time;
88
89 =head1 DESCRIPTION
90
91 An FS::cust_bill object represents an invoice; a declaration that a customer
92 owes you money.  The specific charges are itemized as B<cust_bill_pkg> records
93 (see L<FS::cust_bill_pkg>).  FS::cust_bill inherits from FS::Record.  The
94 following fields are currently supported:
95
96 Regular fields
97
98 =over 4
99
100 =item invnum - primary key (assigned automatically for new invoices)
101
102 =item custnum - customer (see L<FS::cust_main>)
103
104 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
105 L<Time::Local> and L<Date::Parse> for conversion functions.
106
107 =item charged - amount of this invoice
108
109 =item invoice_terms - optional terms override for this specific invoice
110
111 =back
112
113 Customer info at invoice generation time
114
115 =over 4
116
117 =item previous_balance
118
119 =item billing_balance
120
121 =back
122
123 Deprecated
124
125 =over 4
126
127 =item printed - deprecated
128
129 =back
130
131 Specific use cases
132
133 =over 4
134
135 =item closed - books closed flag, empty or `Y'
136
137 =item statementnum - invoice aggregation (see L<FS::cust_statement>)
138
139 =item agent_invid - legacy invoice number
140
141 =back
142
143 =head1 METHODS
144
145 =over 4
146
147 =item new HASHREF
148
149 Creates a new invoice.  To add the invoice to the database, see L<"insert">.
150 Invoices are normally created by calling the bill method of a customer object
151 (see L<FS::cust_main>).
152
153 =cut
154
155 sub table { 'cust_bill'; }
156
157 sub cust_linked { $_[0]->cust_main_custnum; } 
158 sub cust_unlinked_msg {
159   my $self = shift;
160   "WARNING: can't find cust_main.custnum ". $self->custnum.
161   ' (cust_bill.invnum '. $self->invnum. ')';
162 }
163
164 =item insert
165
166 Adds this invoice to the database ("Posts" the invoice).  If there is an error,
167 returns the error, otherwise returns false.
168
169 =cut
170
171 sub insert {
172   my $self = shift;
173   warn "$me insert called\n" if $DEBUG;
174
175   local $SIG{HUP} = 'IGNORE';
176   local $SIG{INT} = 'IGNORE';
177   local $SIG{QUIT} = 'IGNORE';
178   local $SIG{TERM} = 'IGNORE';
179   local $SIG{TSTP} = 'IGNORE';
180   local $SIG{PIPE} = 'IGNORE';
181
182   my $oldAutoCommit = $FS::UID::AutoCommit;
183   local $FS::UID::AutoCommit = 0;
184   my $dbh = dbh;
185
186   my $error = $self->SUPER::insert;
187   if ( $error ) {
188     $dbh->rollback if $oldAutoCommit;
189     return $error;
190   }
191
192   if ( $self->get('cust_bill_pkg') ) {
193     foreach my $cust_bill_pkg ( @{$self->get('cust_bill_pkg')} ) {
194       $cust_bill_pkg->invnum($self->invnum);
195       my $error = $cust_bill_pkg->insert;
196       if ( $error ) {
197         $dbh->rollback if $oldAutoCommit;
198         return "can't create invoice line item: $error";
199       }
200     }
201   }
202
203   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
204   '';
205
206 }
207
208 =item delete
209
210 This method now works but you probably shouldn't use it.  Instead, apply a
211 credit against the invoice.
212
213 Using this method to delete invoices outright is really, really bad.  There
214 would be no record you ever posted this invoice, and there are no check to
215 make sure charged = 0 or that there are no associated cust_bill_pkg records.
216
217 Really, don't use it.
218
219 =cut
220
221 sub delete {
222   my $self = shift;
223   return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
224
225   local $SIG{HUP} = 'IGNORE';
226   local $SIG{INT} = 'IGNORE';
227   local $SIG{QUIT} = 'IGNORE';
228   local $SIG{TERM} = 'IGNORE';
229   local $SIG{TSTP} = 'IGNORE';
230   local $SIG{PIPE} = 'IGNORE';
231
232   my $oldAutoCommit = $FS::UID::AutoCommit;
233   local $FS::UID::AutoCommit = 0;
234   my $dbh = dbh;
235
236   foreach my $table (qw(
237     cust_bill_event
238     cust_event
239     cust_credit_bill
240     cust_bill_pay
241     cust_bill_pay
242     cust_credit_bill
243     cust_pay_batch
244     cust_bill_pay_batch
245     cust_bill_pkg
246   )) {
247
248     foreach my $linked ( $self->$table() ) {
249       my $error = $linked->delete;
250       if ( $error ) {
251         $dbh->rollback if $oldAutoCommit;
252         return $error;
253       }
254     }
255
256   }
257
258   my $error = $self->SUPER::delete(@_);
259   if ( $error ) {
260     $dbh->rollback if $oldAutoCommit;
261     return $error;
262   }
263
264   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
265
266   '';
267
268 }
269
270 =item replace [ OLD_RECORD ]
271
272 You can, but probably shouldn't modify invoices...
273
274 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
275 supplied, replaces this record.  If there is an error, returns the error,
276 otherwise returns false.
277
278 =cut
279
280 #replace can be inherited from Record.pm
281
282 # replace_check is now the preferred way to #implement replace data checks
283 # (so $object->replace() works without an argument)
284
285 sub replace_check {
286   my( $new, $old ) = ( shift, shift );
287   return "Can't modify closed invoice" if $old->closed =~ /^Y/i;
288   #return "Can't change _date!" unless $old->_date eq $new->_date;
289   return "Can't change _date" unless $old->_date == $new->_date;
290   return "Can't change charged" unless $old->charged == $new->charged
291                                     || $old->charged == 0
292                                     || $new->{'Hash'}{'cc_surcharge_replace_hack'};
293
294   '';
295 }
296
297
298 =item add_cc_surcharge
299
300 Giant hack
301
302 =cut
303
304 sub add_cc_surcharge {
305     my ($self, $pkgnum, $amount) = (shift, shift, shift);
306
307     my $error;
308     my $cust_bill_pkg = new FS::cust_bill_pkg({
309                                     'invnum' => $self->invnum,
310                                     'pkgnum' => $pkgnum,
311                                     'setup' => $amount,
312                         });
313     $error = $cust_bill_pkg->insert;
314     return $error if $error;
315
316     $self->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
317     $self->charged($self->charged+$amount);
318     $error = $self->replace;
319     return $error if $error;
320
321     $self->apply_payments_and_credits;
322 }
323
324
325 =item check
326
327 Checks all fields to make sure this is a valid invoice.  If there is an error,
328 returns the error, otherwise returns false.  Called by the insert and replace
329 methods.
330
331 =cut
332
333 sub check {
334   my $self = shift;
335
336   my $error =
337     $self->ut_numbern('invnum')
338     || $self->ut_foreign_key('custnum', 'cust_main', 'custnum' )
339     || $self->ut_numbern('_date')
340     || $self->ut_money('charged')
341     || $self->ut_numbern('printed')
342     || $self->ut_enum('closed', [ '', 'Y' ])
343     || $self->ut_foreign_keyn('statementnum', 'cust_statement', 'statementnum' )
344     || $self->ut_numbern('agent_invid') #varchar?
345   ;
346   return $error if $error;
347
348   $self->_date(time) unless $self->_date;
349
350   $self->printed(0) if $self->printed eq '';
351
352   $self->SUPER::check;
353 }
354
355 =item display_invnum
356
357 Returns the displayed invoice number for this invoice: agent_invid if
358 cust_bill-default_agent_invid is set and it has a value, invnum otherwise.
359
360 =cut
361
362 sub display_invnum {
363   my $self = shift;
364   if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){
365     return $self->agent_invid;
366   } else {
367     return $self->invnum;
368   }
369 }
370
371 =item previous
372
373 Returns a list consisting of the total previous balance for this customer, 
374 followed by the previous outstanding invoices (as FS::cust_bill objects also).
375
376 =cut
377
378 sub previous {
379   my $self = shift;
380   my $total = 0;
381   my @cust_bill = sort { $a->_date <=> $b->_date }
382     grep { $_->owed != 0 && $_->_date < $self->_date }
383       qsearch( 'cust_bill', { 'custnum' => $self->custnum } ) 
384   ;
385   foreach ( @cust_bill ) { $total += $_->owed; }
386   $total, @cust_bill;
387 }
388
389 =item cust_bill_pkg
390
391 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
392
393 =cut
394
395 sub cust_bill_pkg {
396   my $self = shift;
397   qsearch(
398     { 'table'    => 'cust_bill_pkg',
399       'hashref'  => { 'invnum' => $self->invnum },
400       'order_by' => 'ORDER BY billpkgnum',
401     }
402   );
403 }
404
405 =item cust_bill_pkg_pkgnum PKGNUM
406
407 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice and
408 specified pkgnum.
409
410 =cut
411
412 sub cust_bill_pkg_pkgnum {
413   my( $self, $pkgnum ) = @_;
414   qsearch(
415     { 'table'    => 'cust_bill_pkg',
416       'hashref'  => { 'invnum' => $self->invnum,
417                       'pkgnum' => $pkgnum,
418                     },
419       'order_by' => 'ORDER BY billpkgnum',
420     }
421   );
422 }
423
424 =item cust_pkg
425
426 Returns the packages (see L<FS::cust_pkg>) corresponding to the line items for
427 this invoice.
428
429 =cut
430
431 sub cust_pkg {
432   my $self = shift;
433   my @cust_pkg = map { $_->pkgnum > 0 ? $_->cust_pkg : () }
434                      $self->cust_bill_pkg;
435   my %saw = ();
436   grep { ! $saw{$_->pkgnum}++ } @cust_pkg;
437 }
438
439 =item no_auto
440
441 Returns true if any of the packages (or their definitions) corresponding to the
442 line items for this invoice have the no_auto flag set.
443
444 =cut
445
446 sub no_auto {
447   my $self = shift;
448   grep { $_->no_auto || $_->part_pkg->no_auto } $self->cust_pkg;
449 }
450
451 =item open_cust_bill_pkg
452
453 Returns the open line items for this invoice.
454
455 Note that cust_bill_pkg with both setup and recur fees are returned as two
456 separate line items, each with only one fee.
457
458 =cut
459
460 # modeled after cust_main::open_cust_bill
461 sub open_cust_bill_pkg {
462   my $self = shift;
463
464   # grep { $_->owed > 0 } $self->cust_bill_pkg
465
466   my %other = ( 'recur' => 'setup',
467                 'setup' => 'recur', );
468   my @open = ();
469   foreach my $field ( qw( recur setup )) {
470     push @open, map  { $_->set( $other{$field}, 0 ); $_; }
471                 grep { $_->owed($field) > 0 }
472                 $self->cust_bill_pkg;
473   }
474
475   @open;
476 }
477
478 =item cust_bill_event
479
480 Returns the completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
481
482 =cut
483
484 sub cust_bill_event {
485   my $self = shift;
486   qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
487 }
488
489 =item num_cust_bill_event
490
491 Returns the number of completed invoice events (deprecated, old-style events - see L<FS::cust_bill_event>) for this invoice.
492
493 =cut
494
495 sub num_cust_bill_event {
496   my $self = shift;
497   my $sql =
498     "SELECT COUNT(*) FROM cust_bill_event WHERE invnum = ?";
499   my $sth = dbh->prepare($sql) or die  dbh->errstr. " preparing $sql"; 
500   $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
501   $sth->fetchrow_arrayref->[0];
502 }
503
504 =item cust_event
505
506 Returns the new-style customer billing events (see L<FS::cust_event>) for this invoice.
507
508 =cut
509
510 #false laziness w/cust_pkg.pm
511 sub cust_event {
512   my $self = shift;
513   qsearch({
514     'table'     => 'cust_event',
515     'addl_from' => 'JOIN part_event USING ( eventpart )',
516     'hashref'   => { 'tablenum' => $self->invnum },
517     'extra_sql' => " AND eventtable = 'cust_bill' ",
518   });
519 }
520
521 =item num_cust_event
522
523 Returns the number of new-style customer billing events (see L<FS::cust_event>) for this invoice.
524
525 =cut
526
527 #false laziness w/cust_pkg.pm
528 sub num_cust_event {
529   my $self = shift;
530   my $sql =
531     "SELECT COUNT(*) FROM cust_event JOIN part_event USING ( eventpart ) ".
532     "  WHERE tablenum = ? AND eventtable = 'cust_bill'";
533   my $sth = dbh->prepare($sql) or die  dbh->errstr. " preparing $sql"; 
534   $sth->execute($self->invnum) or die $sth->errstr. " executing $sql";
535   $sth->fetchrow_arrayref->[0];
536 }
537
538 =item cust_main
539
540 Returns the customer (see L<FS::cust_main>) for this invoice.
541
542 =cut
543
544 sub cust_main {
545   my $self = shift;
546   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
547 }
548
549 =item cust_suspend_if_balance_over AMOUNT
550
551 Suspends the customer associated with this invoice if the total amount owed on
552 this invoice and all older invoices is greater than the specified amount.
553
554 Returns a list: an empty list on success or a list of errors.
555
556 =cut
557
558 sub cust_suspend_if_balance_over {
559   my( $self, $amount ) = ( shift, shift );
560   my $cust_main = $self->cust_main;
561   if ( $cust_main->total_owed_date($self->_date) < $amount ) {
562     return ();
563   } else {
564     $cust_main->suspend(@_);
565   }
566 }
567
568 =item cust_credit
569
570 Depreciated.  See the cust_credited method.
571
572  #Returns a list consisting of the total previous credited (see
573  #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
574  #outstanding credits (FS::cust_credit objects).
575
576 =cut
577
578 sub cust_credit {
579   use Carp;
580   croak "FS::cust_bill->cust_credit depreciated; see ".
581         "FS::cust_bill->cust_credit_bill";
582   #my $self = shift;
583   #my $total = 0;
584   #my @cust_credit = sort { $a->_date <=> $b->_date }
585   #  grep { $_->credited != 0 && $_->_date < $self->_date }
586   #    qsearch('cust_credit', { 'custnum' => $self->custnum } )
587   #;
588   #foreach (@cust_credit) { $total += $_->credited; }
589   #$total, @cust_credit;
590 }
591
592 =item cust_pay
593
594 Depreciated.  See the cust_bill_pay method.
595
596 #Returns all payments (see L<FS::cust_pay>) for this invoice.
597
598 =cut
599
600 sub cust_pay {
601   use Carp;
602   croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
603   #my $self = shift;
604   #sort { $a->_date <=> $b->_date }
605   #  qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
606   #;
607 }
608
609 sub cust_pay_batch {
610   my $self = shift;
611   qsearch('cust_pay_batch', { 'invnum' => $self->invnum } );
612 }
613
614 sub cust_bill_pay_batch {
615   my $self = shift;
616   qsearch('cust_bill_pay_batch', { 'invnum' => $self->invnum } );
617 }
618
619 =item cust_bill_pay
620
621 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
622
623 =cut
624
625 sub cust_bill_pay {
626   my $self = shift;
627   map { $_ } #return $self->num_cust_bill_pay unless wantarray;
628   sort { $a->_date <=> $b->_date }
629     qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
630 }
631
632 =item cust_credited
633
634 =item cust_credit_bill
635
636 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
637
638 =cut
639
640 sub cust_credited {
641   my $self = shift;
642   map { $_ } #return $self->num_cust_credit_bill unless wantarray;
643   sort { $a->_date <=> $b->_date }
644     qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
645   ;
646 }
647
648 sub cust_credit_bill {
649   shift->cust_credited(@_);
650 }
651
652 =item cust_bill_pay_pkgnum PKGNUM
653
654 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice
655 with matching pkgnum.
656
657 =cut
658
659 sub cust_bill_pay_pkgnum {
660   my( $self, $pkgnum ) = @_;
661   map { $_ } #return $self->num_cust_bill_pay_pkgnum($pkgnum) unless wantarray;
662   sort { $a->_date <=> $b->_date }
663     qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum,
664                                 'pkgnum' => $pkgnum,
665                               }
666            );
667 }
668
669 =item cust_credited_pkgnum PKGNUM
670
671 =item cust_credit_bill_pkgnum PKGNUM
672
673 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice
674 with matching pkgnum.
675
676 =cut
677
678 sub cust_credited_pkgnum {
679   my( $self, $pkgnum ) = @_;
680   map { $_ } #return $self->num_cust_credit_bill_pkgnum($pkgnum) unless wantarray;
681   sort { $a->_date <=> $b->_date }
682     qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum,
683                                    'pkgnum' => $pkgnum,
684                                  }
685            );
686 }
687
688 sub cust_credit_bill_pkgnum {
689   shift->cust_credited_pkgnum(@_);
690 }
691
692 =item tax
693
694 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
695
696 =cut
697
698 sub tax {
699   my $self = shift;
700   my $total = 0;
701   my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
702                                              'pkgnum' => 0 } );
703   foreach (@taxlines) { $total += $_->setup; }
704   $total;
705 }
706
707 =item owed
708
709 Returns the amount owed (still outstanding) on this invoice, which is charged
710 minus all payment applications (see L<FS::cust_bill_pay>) and credit
711 applications (see L<FS::cust_credit_bill>).
712
713 =cut
714
715 sub owed {
716   my $self = shift;
717   my $balance = $self->charged;
718   $balance -= $_->amount foreach ( $self->cust_bill_pay );
719   $balance -= $_->amount foreach ( $self->cust_credited );
720   $balance = sprintf( "%.2f", $balance);
721   $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
722   $balance;
723 }
724
725 sub owed_pkgnum {
726   my( $self, $pkgnum ) = @_;
727
728   #my $balance = $self->charged;
729   my $balance = 0;
730   $balance += $_->setup + $_->recur for $self->cust_bill_pkg_pkgnum($pkgnum);
731
732   $balance -= $_->amount            for $self->cust_bill_pay_pkgnum($pkgnum);
733   $balance -= $_->amount            for $self->cust_credited_pkgnum($pkgnum);
734
735   $balance = sprintf( "%.2f", $balance);
736   $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
737   $balance;
738 }
739
740 =item apply_payments_and_credits [ OPTION => VALUE ... ]
741
742 Applies unapplied payments and credits to this invoice.
743
744 A hash of optional arguments may be passed.  Currently "manual" is supported.
745 If true, a payment receipt is sent instead of a statement when
746 'payment_receipt_email' configuration option is set.
747
748 If there is an error, returns the error, otherwise returns false.
749
750 =cut
751
752 sub apply_payments_and_credits {
753   my( $self, %options ) = @_;
754
755   local $SIG{HUP} = 'IGNORE';
756   local $SIG{INT} = 'IGNORE';
757   local $SIG{QUIT} = 'IGNORE';
758   local $SIG{TERM} = 'IGNORE';
759   local $SIG{TSTP} = 'IGNORE';
760   local $SIG{PIPE} = 'IGNORE';
761
762   my $oldAutoCommit = $FS::UID::AutoCommit;
763   local $FS::UID::AutoCommit = 0;
764   my $dbh = dbh;
765
766   $self->select_for_update; #mutex
767
768   my @payments = grep { $_->unapplied > 0 } $self->cust_main->cust_pay;
769   my @credits  = grep { $_->credited > 0 } $self->cust_main->cust_credit;
770
771   if ( $conf->exists('pkg-balances') ) {
772     # limit @payments & @credits to those w/ a pkgnum grepped from $self
773     my %pkgnums = map { $_ => 1 } map $_->pkgnum, $self->cust_bill_pkg;
774     @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
775     @credits  = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
776   }
777
778   while ( $self->owed > 0 and ( @payments || @credits ) ) {
779
780     my $app = '';
781     if ( @payments && @credits ) {
782
783       #decide which goes first by weight of top (unapplied) line item
784
785       my @open_lineitems = $self->open_cust_bill_pkg;
786
787       my $max_pay_weight =
788         max( map  { $_->part_pkg->pay_weight || 0 }
789              grep { $_ }
790              map  { $_->cust_pkg }
791                   @open_lineitems
792            );
793       my $max_credit_weight =
794         max( map  { $_->part_pkg->credit_weight || 0 }
795              grep { $_ } 
796              map  { $_->cust_pkg }
797                   @open_lineitems
798            );
799
800       #if both are the same... payments first?  it has to be something
801       if ( $max_pay_weight >= $max_credit_weight ) {
802         $app = 'pay';
803       } else {
804         $app = 'credit';
805       }
806     
807     } elsif ( @payments ) {
808       $app = 'pay';
809     } elsif ( @credits ) {
810       $app = 'credit';
811     } else {
812       die "guru meditation #12 and 35";
813     }
814
815     my $unapp_amount;
816     if ( $app eq 'pay' ) {
817
818       my $payment = shift @payments;
819       $unapp_amount = $payment->unapplied;
820       $app = new FS::cust_bill_pay { 'paynum'  => $payment->paynum };
821       $app->pkgnum( $payment->pkgnum )
822         if $conf->exists('pkg-balances') && $payment->pkgnum;
823
824     } elsif ( $app eq 'credit' ) {
825
826       my $credit = shift @credits;
827       $unapp_amount = $credit->credited;
828       $app = new FS::cust_credit_bill { 'crednum' => $credit->crednum };
829       $app->pkgnum( $credit->pkgnum )
830         if $conf->exists('pkg-balances') && $credit->pkgnum;
831
832     } else {
833       die "guru meditation #12 and 35";
834     }
835
836     my $owed;
837     if ( $conf->exists('pkg-balances') && $app->pkgnum ) {
838       warn "owed_pkgnum ". $app->pkgnum;
839       $owed = $self->owed_pkgnum($app->pkgnum);
840     } else {
841       $owed = $self->owed;
842     }
843     next unless $owed > 0;
844
845     warn "min ( $unapp_amount, $owed )\n" if $DEBUG;
846     $app->amount( sprintf('%.2f', min( $unapp_amount, $owed ) ) );
847
848     $app->invnum( $self->invnum );
849
850     my $error = $app->insert(%options);
851     if ( $error ) {
852       $dbh->rollback if $oldAutoCommit;
853       return "Error inserting ". $app->table. " record: $error";
854     }
855     die $error if $error;
856
857   }
858
859   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
860   ''; #no error
861
862 }
863
864 =item generate_email OPTION => VALUE ...
865
866 Options:
867
868 =over 4
869
870 =item from
871
872 sender address, required
873
874 =item tempate
875
876 alternate template name, optional
877
878 =item print_text
879
880 text attachment arrayref, optional
881
882 =item subject
883
884 email subject, optional
885
886 =item notice_name
887
888 notice name instead of "Invoice", optional
889
890 =back
891
892 Returns an argument list to be passed to L<FS::Misc::send_email>.
893
894 =cut
895
896 use MIME::Entity;
897
898 sub generate_email {
899
900   my $self = shift;
901   my %args = @_;
902
903   my $me = '[FS::cust_bill::generate_email]';
904
905   my %return = (
906     'from'      => $args{'from'},
907     'subject'   => (($args{'subject'}) ? $args{'subject'} : 'Invoice'),
908   );
909
910   my %opt = (
911     'unsquelch_cdr' => $conf->exists('voip-cdr_email'),
912     'template'      => $args{'template'},
913     'notice_name'   => ( $args{'notice_name'} || 'Invoice' ),
914   );
915
916   my $cust_main = $self->cust_main;
917
918   if (ref($args{'to'}) eq 'ARRAY') {
919     $return{'to'} = $args{'to'};
920   } else {
921     $return{'to'} = [ grep { $_ !~ /^(POST|FAX)$/ }
922                            $cust_main->invoicing_list
923                     ];
924   }
925
926   if ( $conf->exists('invoice_html') ) {
927
928     warn "$me creating HTML/text multipart message"
929       if $DEBUG;
930
931     $return{'nobody'} = 1;
932
933     my $alternative = build MIME::Entity
934       'Type'        => 'multipart/alternative',
935       'Encoding'    => '7bit',
936       'Disposition' => 'inline'
937     ;
938
939     my $data;
940     if ( $conf->exists('invoice_email_pdf')
941          and scalar($conf->config('invoice_email_pdf_note')) ) {
942
943       warn "$me using 'invoice_email_pdf_note' in multipart message"
944         if $DEBUG;
945       $data = [ map { $_ . "\n" }
946                     $conf->config('invoice_email_pdf_note')
947               ];
948
949     } else {
950
951       warn "$me not using 'invoice_email_pdf_note' in multipart message"
952         if $DEBUG;
953       if ( ref($args{'print_text'}) eq 'ARRAY' ) {
954         $data = $args{'print_text'};
955       } else {
956         $data = [ $self->print_text(\%opt) ];
957       }
958
959     }
960
961     $alternative->attach(
962       'Type'        => 'text/plain',
963       #'Encoding'    => 'quoted-printable',
964       'Encoding'    => '7bit',
965       'Data'        => $data,
966       'Disposition' => 'inline',
967     );
968
969     $args{'from'} =~ /\@([\w\.\-]+)/;
970     my $from = $1 || 'example.com';
971     my $content_id = join('.', rand()*(2**32), $$, time). "\@$from";
972
973     my $logo;
974     my $agentnum = $cust_main->agentnum;
975     if ( defined($args{'template'}) && length($args{'template'})
976          && $conf->exists( 'logo_'. $args{'template'}. '.png', $agentnum )
977        )
978     {
979       $logo = 'logo_'. $args{'template'}. '.png';
980     } else {
981       $logo = "logo.png";
982     }
983     my $image_data = $conf->config_binary( $logo, $agentnum);
984
985     my $image = build MIME::Entity
986       'Type'       => 'image/png',
987       'Encoding'   => 'base64',
988       'Data'       => $image_data,
989       'Filename'   => 'logo.png',
990       'Content-ID' => "<$content_id>",
991     ;
992    
993     my $barcode;
994     if($conf->exists('invoice-barcode')){
995         my $barcode_content_id = join('.', rand()*(2**32), $$, time). "\@$from";
996         $barcode = build MIME::Entity
997           'Type'       => 'image/png',
998           'Encoding'   => 'base64',
999           'Data'       => $self->invoice_barcode(0),
1000           'Filename'   => 'barcode.png',
1001           'Content-ID' => "<$barcode_content_id>",
1002         ;
1003         $opt{'barcode_cid'} = $barcode_content_id;
1004     }
1005
1006     $alternative->attach(
1007       'Type'        => 'text/html',
1008       'Encoding'    => 'quoted-printable',
1009       'Data'        => [ '<html>',
1010                          '  <head>',
1011                          '    <title>',
1012                          '      '. encode_entities($return{'subject'}), 
1013                          '    </title>',
1014                          '  </head>',
1015                          '  <body bgcolor="#e8e8e8">',
1016                          $self->print_html({ 'cid'=>$content_id, %opt }),
1017                          '  </body>',
1018                          '</html>',
1019                        ],
1020       'Disposition' => 'inline',
1021       #'Filename'    => 'invoice.pdf',
1022     );
1023
1024     my @otherparts = ();
1025     if ( $cust_main->email_csv_cdr ) {
1026
1027       push @otherparts, build MIME::Entity
1028         'Type'        => 'text/csv',
1029         'Encoding'    => '7bit',
1030         'Data'        => [ map { "$_\n" }
1031                              $self->call_details('prepend_billed_number' => 1)
1032                          ],
1033         'Disposition' => 'attachment',
1034         'Filename'    => 'usage-'. $self->invnum. '.csv',
1035       ;
1036
1037     }
1038
1039     if ( $conf->exists('invoice_email_pdf') ) {
1040
1041       #attaching pdf too:
1042       # multipart/mixed
1043       #   multipart/related
1044       #     multipart/alternative
1045       #       text/plain
1046       #       text/html
1047       #     image/png
1048       #   application/pdf
1049
1050       my $related = build MIME::Entity 'Type'     => 'multipart/related',
1051                                        'Encoding' => '7bit';
1052
1053       #false laziness w/Misc::send_email
1054       $related->head->replace('Content-type',
1055         $related->mime_type.
1056         '; boundary="'. $related->head->multipart_boundary. '"'.
1057         '; type=multipart/alternative'
1058       );
1059
1060       $related->add_part($alternative);
1061
1062       $related->add_part($image);
1063
1064       my $pdf = build MIME::Entity $self->mimebuild_pdf(\%opt);
1065
1066       $return{'mimeparts'} = [ $related, $pdf, @otherparts ];
1067
1068     } else {
1069
1070       #no other attachment:
1071       # multipart/related
1072       #   multipart/alternative
1073       #     text/plain
1074       #     text/html
1075       #   image/png
1076
1077       $return{'content-type'} = 'multipart/related';
1078       if($conf->exists('invoice-barcode')){
1079           $return{'mimeparts'} = [ $alternative, $image, $barcode, @otherparts ];
1080       }
1081       else {
1082           $return{'mimeparts'} = [ $alternative, $image, @otherparts ];
1083       }
1084       $return{'type'} = 'multipart/alternative'; #Content-Type of first part...
1085       #$return{'disposition'} = 'inline';
1086
1087     }
1088   
1089   } else {
1090
1091     if ( $conf->exists('invoice_email_pdf') ) {
1092       warn "$me creating PDF attachment"
1093         if $DEBUG;
1094
1095       #mime parts arguments a la MIME::Entity->build().
1096       $return{'mimeparts'} = [
1097         { $self->mimebuild_pdf(\%opt) }
1098       ];
1099     }
1100   
1101     if ( $conf->exists('invoice_email_pdf')
1102          and scalar($conf->config('invoice_email_pdf_note')) ) {
1103
1104       warn "$me using 'invoice_email_pdf_note'"
1105         if $DEBUG;
1106       $return{'body'} = [ map { $_ . "\n" }
1107                               $conf->config('invoice_email_pdf_note')
1108                         ];
1109
1110     } else {
1111
1112       warn "$me not using 'invoice_email_pdf_note'"
1113         if $DEBUG;
1114       if ( ref($args{'print_text'}) eq 'ARRAY' ) {
1115         $return{'body'} = $args{'print_text'};
1116       } else {
1117         $return{'body'} = [ $self->print_text(\%opt) ];
1118       }
1119
1120     }
1121
1122   }
1123
1124   %return;
1125
1126 }
1127
1128 =item mimebuild_pdf
1129
1130 Returns a list suitable for passing to MIME::Entity->build(), representing
1131 this invoice as PDF attachment.
1132
1133 =cut
1134
1135 sub mimebuild_pdf {
1136   my $self = shift;
1137   (
1138     'Type'        => 'application/pdf',
1139     'Encoding'    => 'base64',
1140     'Data'        => [ $self->print_pdf(@_) ],
1141     'Disposition' => 'attachment',
1142     'Filename'    => 'invoice-'. $self->invnum. '.pdf',
1143   );
1144 }
1145
1146 =item send HASHREF | [ TEMPLATE [ , AGENTNUM [ , INVOICE_FROM [ , AMOUNT ] ] ] ]
1147
1148 Sends this invoice to the destinations configured for this customer: sends
1149 email, prints and/or faxes.  See L<FS::cust_main_invoice>.
1150
1151 Options can be passed as a hashref (recommended) or as a list of up to 
1152 four values for templatename, agentnum, invoice_from and amount.
1153
1154 I<template>, if specified, is the name of a suffix for alternate invoices.
1155
1156 I<agentnum>, if specified, means that this invoice will only be sent for customers
1157 of the specified agent or agent(s).  AGENTNUM can be a scalar agentnum (for a
1158 single agent) or an arrayref of agentnums.
1159
1160 I<invoice_from>, if specified, overrides the default email invoice From: address.
1161
1162 I<amount>, if specified, only sends the invoice if the total amount owed on this
1163 invoice and all older invoices is greater than the specified amount.
1164
1165 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1166
1167 =cut
1168
1169 sub queueable_send {
1170   my %opt = @_;
1171
1172   my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1173     or die "invalid invoice number: " . $opt{invnum};
1174
1175   my @args = ( $opt{template}, $opt{agentnum} );
1176   push @args, $opt{invoice_from}
1177     if exists($opt{invoice_from}) && $opt{invoice_from};
1178
1179   my $error = $self->send( @args );
1180   die $error if $error;
1181
1182 }
1183
1184 sub send {
1185   my $self = shift;
1186
1187   my( $template, $invoice_from, $notice_name );
1188   my $agentnums = '';
1189   my $balance_over = 0;
1190
1191   if ( ref($_[0]) ) {
1192     my $opt = shift;
1193     $template = $opt->{'template'} || '';
1194     if ( $agentnums = $opt->{'agentnum'} ) {
1195       $agentnums = [ $agentnums ] unless ref($agentnums);
1196     }
1197     $invoice_from = $opt->{'invoice_from'};
1198     $balance_over = $opt->{'balance_over'} if $opt->{'balance_over'};
1199     $notice_name = $opt->{'notice_name'};
1200   } else {
1201     $template = scalar(@_) ? shift : '';
1202     if ( scalar(@_) && $_[0]  ) {
1203       $agentnums = ref($_[0]) ? shift : [ shift ];
1204     }
1205     $invoice_from = shift if scalar(@_);
1206     $balance_over = shift if scalar(@_) && $_[0] !~ /^\s*$/;
1207   }
1208
1209   return 'N/A' unless ! $agentnums
1210                    or grep { $_ == $self->cust_main->agentnum } @$agentnums;
1211
1212   return ''
1213     unless $self->cust_main->total_owed_date($self->_date) > $balance_over;
1214
1215   $invoice_from ||= $self->_agent_invoice_from ||    #XXX should go away
1216                     $conf->config('invoice_from', $self->cust_main->agentnum );
1217
1218   my %opt = (
1219     'template'     => $template,
1220     'invoice_from' => $invoice_from,
1221     'notice_name'  => ( $notice_name || 'Invoice' ),
1222   );
1223
1224   my @invoicing_list = $self->cust_main->invoicing_list;
1225
1226   #$self->email_invoice(\%opt)
1227   $self->email(\%opt)
1228     if grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list;
1229
1230   #$self->print_invoice(\%opt)
1231   $self->print(\%opt)
1232     if grep { $_ eq 'POST' } @invoicing_list; #postal
1233
1234   $self->fax_invoice(\%opt)
1235     if grep { $_ eq 'FAX' } @invoicing_list; #fax
1236
1237   '';
1238
1239 }
1240
1241 =item email HASHREF | [ TEMPLATE [ , INVOICE_FROM ] ] 
1242
1243 Emails this invoice.
1244
1245 Options can be passed as a hashref (recommended) or as a list of up to 
1246 two values for templatename and invoice_from.
1247
1248 I<template>, if specified, is the name of a suffix for alternate invoices.
1249
1250 I<invoice_from>, if specified, overrides the default email invoice From: address.
1251
1252 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1253
1254 =cut
1255
1256 sub queueable_email {
1257   my %opt = @_;
1258
1259   my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } )
1260     or die "invalid invoice number: " . $opt{invnum};
1261
1262   my @args = ( $opt{template} );
1263   push @args, $opt{invoice_from}
1264     if exists($opt{invoice_from}) && $opt{invoice_from};
1265
1266   my $error = $self->email( @args );
1267   die $error if $error;
1268
1269 }
1270
1271 #sub email_invoice {
1272 sub email {
1273   my $self = shift;
1274
1275   my( $template, $invoice_from, $notice_name );
1276   if ( ref($_[0]) ) {
1277     my $opt = shift;
1278     $template = $opt->{'template'} || '';
1279     $invoice_from = $opt->{'invoice_from'};
1280     $notice_name = $opt->{'notice_name'} || 'Invoice';
1281   } else {
1282     $template = scalar(@_) ? shift : '';
1283     $invoice_from = shift if scalar(@_);
1284     $notice_name = 'Invoice';
1285   }
1286
1287   $invoice_from ||= $self->_agent_invoice_from ||    #XXX should go away
1288                     $conf->config('invoice_from', $self->cust_main->agentnum );
1289
1290   my @invoicing_list = grep { $_ !~ /^(POST|FAX)$/ } 
1291                             $self->cust_main->invoicing_list;
1292
1293   if ( ! @invoicing_list ) { #no recipients
1294     if ( $conf->exists('cust_bill-no_recipients-error') ) {
1295       die 'No recipients for customer #'. $self->custnum;
1296     } else {
1297       #default: better to notify this person than silence
1298       @invoicing_list = ($invoice_from);
1299     }
1300   }
1301
1302   my $subject = $self->email_subject($template);
1303
1304   my $error = send_email(
1305     $self->generate_email(
1306       'from'        => $invoice_from,
1307       'to'          => [ grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list ],
1308       'subject'     => $subject,
1309       'template'    => $template,
1310       'notice_name' => $notice_name,
1311     )
1312   );
1313   die "can't email invoice: $error\n" if $error;
1314   #die "$error\n" if $error;
1315
1316 }
1317
1318 sub email_subject {
1319   my $self = shift;
1320
1321   #my $template = scalar(@_) ? shift : '';
1322   #per-template?
1323
1324   my $subject = $conf->config('invoice_subject', $self->cust_main->agentnum)
1325                 || 'Invoice';
1326
1327   my $cust_main = $self->cust_main;
1328   my $name = $cust_main->name;
1329   my $name_short = $cust_main->name_short;
1330   my $invoice_number = $self->invnum;
1331   my $invoice_date = $self->_date_pretty;
1332
1333   eval qq("$subject");
1334 }
1335
1336 =item lpr_data HASHREF | [ TEMPLATE ]
1337
1338 Returns the postscript or plaintext for this invoice as an arrayref.
1339
1340 Options can be passed as a hashref (recommended) or as a single optional value
1341 for template.
1342
1343 I<template>, if specified, is the name of a suffix for alternate invoices.
1344
1345 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1346
1347 =cut
1348
1349 sub lpr_data {
1350   my $self = shift;
1351   my( $template, $notice_name );
1352   if ( ref($_[0]) ) {
1353     my $opt = shift;
1354     $template = $opt->{'template'} || '';
1355     $notice_name = $opt->{'notice_name'} || 'Invoice';
1356   } else {
1357     $template = scalar(@_) ? shift : '';
1358     $notice_name = 'Invoice';
1359   }
1360
1361   my %opt = (
1362     'template'    => $template,
1363     'notice_name' => $notice_name,
1364   );
1365
1366   my $method = $conf->exists('invoice_latex') ? 'print_ps' : 'print_text';
1367   [ $self->$method( \%opt ) ];
1368 }
1369
1370 =item print HASHREF | [ TEMPLATE ]
1371
1372 Prints this invoice.
1373
1374 Options can be passed as a hashref (recommended) or as a single optional
1375 value for template.
1376
1377 I<template>, if specified, is the name of a suffix for alternate invoices.
1378
1379 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1380
1381 =cut
1382
1383 #sub print_invoice {
1384 sub print {
1385   my $self = shift;
1386   my( $template, $notice_name );
1387   if ( ref($_[0]) ) {
1388     my $opt = shift;
1389     $template = $opt->{'template'} || '';
1390     $notice_name = $opt->{'notice_name'} || 'Invoice';
1391   } else {
1392     $template = scalar(@_) ? shift : '';
1393     $notice_name = 'Invoice';
1394   }
1395
1396   my %opt = (
1397     'template'    => $template,
1398     'notice_name' => $notice_name,
1399   );
1400
1401   if($conf->exists('invoice_print_pdf')) {
1402     # Add the invoice to the current batch.
1403     $self->batch_invoice(\%opt);
1404   }
1405   else {
1406     do_print $self->lpr_data(\%opt);
1407   }
1408 }
1409
1410 =item fax_invoice HASHREF | [ TEMPLATE ] 
1411
1412 Faxes this invoice.
1413
1414 Options can be passed as a hashref (recommended) or as a single optional
1415 value for template.
1416
1417 I<template>, if specified, is the name of a suffix for alternate invoices.
1418
1419 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1420
1421 =cut
1422
1423 sub fax_invoice {
1424   my $self = shift;
1425   my( $template, $notice_name );
1426   if ( ref($_[0]) ) {
1427     my $opt = shift;
1428     $template = $opt->{'template'} || '';
1429     $notice_name = $opt->{'notice_name'} || 'Invoice';
1430   } else {
1431     $template = scalar(@_) ? shift : '';
1432     $notice_name = 'Invoice';
1433   }
1434
1435   die 'FAX invoice destination not (yet?) supported with plain text invoices.'
1436     unless $conf->exists('invoice_latex');
1437
1438   my $dialstring = $self->cust_main->getfield('fax');
1439   #Check $dialstring?
1440
1441   my %opt = (
1442     'template'    => $template,
1443     'notice_name' => $notice_name,
1444   );
1445
1446   my $error = send_fax( 'docdata'    => $self->lpr_data(\%opt),
1447                         'dialstring' => $dialstring,
1448                       );
1449   die $error if $error;
1450
1451 }
1452
1453 =item batch_invoice [ HASHREF ]
1454
1455 Place this invoice into the open batch (see C<FS::bill_batch>).  If there 
1456 isn't an open batch, one will be created.
1457
1458 =cut
1459
1460 sub batch_invoice {
1461   my ($self, $opt) = @_;
1462   my $batch = FS::bill_batch->get_open_batch;
1463   my $cust_bill_batch = FS::cust_bill_batch->new({
1464       batchnum => $batch->batchnum,
1465       invnum   => $self->invnum,
1466   });
1467   return $cust_bill_batch->insert($opt);
1468 }
1469
1470 =item ftp_invoice [ TEMPLATENAME ] 
1471
1472 Sends this invoice data via FTP.
1473
1474 TEMPLATENAME is unused?
1475
1476 =cut
1477
1478 sub ftp_invoice {
1479   my $self = shift;
1480   my $template = scalar(@_) ? shift : '';
1481
1482   $self->send_csv(
1483     'protocol'   => 'ftp',
1484     'server'     => $conf->config('cust_bill-ftpserver'),
1485     'username'   => $conf->config('cust_bill-ftpusername'),
1486     'password'   => $conf->config('cust_bill-ftppassword'),
1487     'dir'        => $conf->config('cust_bill-ftpdir'),
1488     'format'     => $conf->config('cust_bill-ftpformat'),
1489   );
1490 }
1491
1492 =item spool_invoice [ TEMPLATENAME ] 
1493
1494 Spools this invoice data (see L<FS::spool_csv>)
1495
1496 TEMPLATENAME is unused?
1497
1498 =cut
1499
1500 sub spool_invoice {
1501   my $self = shift;
1502   my $template = scalar(@_) ? shift : '';
1503
1504   $self->spool_csv(
1505     'format'       => $conf->config('cust_bill-spoolformat'),
1506     'agent_spools' => $conf->exists('cust_bill-spoolagent'),
1507   );
1508 }
1509
1510 =item send_if_newest [ TEMPLATENAME [ , AGENTNUM [ , INVOICE_FROM ] ] ]
1511
1512 Like B<send>, but only sends the invoice if it is the newest open invoice for
1513 this customer.
1514
1515 =cut
1516
1517 sub send_if_newest {
1518   my $self = shift;
1519
1520   return ''
1521     if scalar(
1522                grep { $_->owed > 0 } 
1523                     qsearch('cust_bill', {
1524                       'custnum' => $self->custnum,
1525                       #'_date'   => { op=>'>', value=>$self->_date },
1526                       'invnum'  => { op=>'>', value=>$self->invnum },
1527                     } )
1528              );
1529     
1530   $self->send(@_);
1531 }
1532
1533 =item send_csv OPTION => VALUE, ...
1534
1535 Sends invoice as a CSV data-file to a remote host with the specified protocol.
1536
1537 Options are:
1538
1539 protocol - currently only "ftp"
1540 server
1541 username
1542 password
1543 dir
1544
1545 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
1546 and YYMMDDHHMMSS is a timestamp.
1547
1548 See L</print_csv> for a description of the output format.
1549
1550 =cut
1551
1552 sub send_csv {
1553   my($self, %opt) = @_;
1554
1555   #create file(s)
1556
1557   my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1558   mkdir $spooldir, 0700 unless -d $spooldir;
1559
1560   my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1561   my $file = "$spooldir/$tracctnum.csv";
1562   
1563   my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1564
1565   open(CSV, ">$file") or die "can't open $file: $!";
1566   print CSV $header;
1567
1568   print CSV $detail;
1569
1570   close CSV;
1571
1572   my $net;
1573   if ( $opt{protocol} eq 'ftp' ) {
1574     eval "use Net::FTP;";
1575     die $@ if $@;
1576     $net = Net::FTP->new($opt{server}) or die @$;
1577   } else {
1578     die "unknown protocol: $opt{protocol}";
1579   }
1580
1581   $net->login( $opt{username}, $opt{password} )
1582     or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
1583
1584   $net->binary or die "can't set binary mode";
1585
1586   $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
1587
1588   $net->put($file) or die "can't put $file: $!";
1589
1590   $net->quit;
1591
1592   unlink $file;
1593
1594 }
1595
1596 =item spool_csv
1597
1598 Spools CSV invoice data.
1599
1600 Options are:
1601
1602 =over 4
1603
1604 =item format - 'default' or 'billco'
1605
1606 =item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the customer has the corresponding invoice destinations set (see L<FS::cust_main_invoice>).
1607
1608 =item agent_spools - if set to a true value, will spool to per-agent files rather than a single global file
1609
1610 =item balanceover - if set, only spools the invoice if the total amount owed on this invoice and all older invoices is greater than the specified amount.
1611
1612 =back
1613
1614 =cut
1615
1616 sub spool_csv {
1617   my($self, %opt) = @_;
1618
1619   my $cust_main = $self->cust_main;
1620
1621   if ( $opt{'dest'} ) {
1622     my %invoicing_list = map { /^(POST|FAX)$/ or 'EMAIL' =~ /^(.*)$/; $1 => 1 }
1623                              $cust_main->invoicing_list;
1624     return 'N/A' unless $invoicing_list{$opt{'dest'}}
1625                      || ! keys %invoicing_list;
1626   }
1627
1628   if ( $opt{'balanceover'} ) {
1629     return 'N/A'
1630       if $cust_main->total_owed_date($self->_date) < $opt{'balanceover'};
1631   }
1632
1633   my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
1634   mkdir $spooldir, 0700 unless -d $spooldir;
1635
1636   my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time);
1637
1638   my $file =
1639     "$spooldir/".
1640     ( $opt{'agent_spools'} ? 'agentnum'.$cust_main->agentnum : 'spool' ).
1641     ( lc($opt{'format'}) eq 'billco' ? '-header' : '' ) .
1642     '.csv';
1643   
1644   my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum );
1645
1646   open(CSV, ">>$file") or die "can't open $file: $!";
1647   flock(CSV, LOCK_EX);
1648   seek(CSV, 0, 2);
1649
1650   print CSV $header;
1651
1652   if ( lc($opt{'format'}) eq 'billco' ) {
1653
1654     flock(CSV, LOCK_UN);
1655     close CSV;
1656
1657     $file =
1658       "$spooldir/".
1659       ( $opt{'agent_spools'} ? 'agentnum'.$cust_main->agentnum : 'spool' ).
1660       '-detail.csv';
1661
1662     open(CSV,">>$file") or die "can't open $file: $!";
1663     flock(CSV, LOCK_EX);
1664     seek(CSV, 0, 2);
1665   }
1666
1667   print CSV $detail;
1668
1669   flock(CSV, LOCK_UN);
1670   close CSV;
1671
1672   return '';
1673
1674 }
1675
1676 =item print_csv OPTION => VALUE, ...
1677
1678 Returns CSV data for this invoice.
1679
1680 Options are:
1681
1682 format - 'default' or 'billco'
1683
1684 Returns a list consisting of two scalars.  The first is a single line of CSV
1685 header information for this invoice.  The second is one or more lines of CSV
1686 detail information for this invoice.
1687
1688 If I<format> is not specified or "default", the fields of the CSV file are as
1689 follows:
1690
1691 record_type, invnum, custnum, _date, charged, first, last, company, address1, address2, city, state, zip, country, pkg, setup, recur, sdate, edate
1692
1693 =over 4
1694
1695 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
1696
1697 B<record_type> is C<cust_bill> for the initial header line only.  The
1698 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
1699 fields are filled in.
1700
1701 B<record_type> is C<cust_bill_pkg> for detail lines.  Only the first two fields
1702 (B<record_type> and B<invnum>) and the last five fields (B<pkg> through B<edate>)
1703 are filled in.
1704
1705 =item invnum - invoice number
1706
1707 =item custnum - customer number
1708
1709 =item _date - invoice date
1710
1711 =item charged - total invoice amount
1712
1713 =item first - customer first name
1714
1715 =item last - customer first name
1716
1717 =item company - company name
1718
1719 =item address1 - address line 1
1720
1721 =item address2 - address line 1
1722
1723 =item city
1724
1725 =item state
1726
1727 =item zip
1728
1729 =item country
1730
1731 =item pkg - line item description
1732
1733 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
1734
1735 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
1736
1737 =item sdate - start date for recurring fee
1738
1739 =item edate - end date for recurring fee
1740
1741 =back
1742
1743 If I<format> is "billco", the fields of the header CSV file are as follows:
1744
1745   +-------------------------------------------------------------------+
1746   |                        FORMAT HEADER FILE                         |
1747   |-------------------------------------------------------------------|
1748   | Field | Description                   | Name       | Type | Width |
1749   | 1     | N/A-Leave Empty               | RC         | CHAR |     2 |
1750   | 2     | N/A-Leave Empty               | CUSTID     | CHAR |    15 |
1751   | 3     | Transaction Account No        | TRACCTNUM  | CHAR |    15 |
1752   | 4     | Transaction Invoice No        | TRINVOICE  | CHAR |    15 |
1753   | 5     | Transaction Zip Code          | TRZIP      | CHAR |     5 |
1754   | 6     | Transaction Company Bill To   | TRCOMPANY  | CHAR |    30 |
1755   | 7     | Transaction Contact Bill To   | TRNAME     | CHAR |    30 |
1756   | 8     | Additional Address Unit Info  | TRADDR1    | CHAR |    30 |
1757   | 9     | Bill To Street Address        | TRADDR2    | CHAR |    30 |
1758   | 10    | Ancillary Billing Information | TRADDR3    | CHAR |    30 |
1759   | 11    | Transaction City Bill To      | TRCITY     | CHAR |    20 |
1760   | 12    | Transaction State Bill To     | TRSTATE    | CHAR |     2 |
1761   | 13    | Bill Cycle Close Date         | CLOSEDATE  | CHAR |    10 |
1762   | 14    | Bill Due Date                 | DUEDATE    | CHAR |    10 |
1763   | 15    | Previous Balance              | BALFWD     | NUM* |     9 |
1764   | 16    | Pmt/CR Applied                | CREDAPPLY  | NUM* |     9 |
1765   | 17    | Total Current Charges         | CURRENTCHG | NUM* |     9 |
1766   | 18    | Total Amt Due                 | TOTALDUE   | NUM* |     9 |
1767   | 19    | Total Amt Due                 | AMTDUE     | NUM* |     9 |
1768   | 20    | 30 Day Aging                  | AMT30      | NUM* |     9 |
1769   | 21    | 60 Day Aging                  | AMT60      | NUM* |     9 |
1770   | 22    | 90 Day Aging                  | AMT90      | NUM* |     9 |
1771   | 23    | Y/N                           | AGESWITCH  | CHAR |     1 |
1772   | 24    | Remittance automation         | SCANLINE   | CHAR |   100 |
1773   | 25    | Total Taxes & Fees            | TAXTOT     | NUM* |     9 |
1774   | 26    | Customer Reference Number     | CUSTREF    | CHAR |    15 |
1775   | 27    | Federal Tax***                | FEDTAX     | NUM* |     9 |
1776   | 28    | State Tax***                  | STATETAX   | NUM* |     9 |
1777   | 29    | Other Taxes & Fees***         | OTHERTAX   | NUM* |     9 |
1778   +-------+-------------------------------+------------+------+-------+
1779
1780 If I<format> is "billco", the fields of the detail CSV file are as follows:
1781
1782                                   FORMAT FOR DETAIL FILE
1783         |                            |           |      |
1784   Field | Description                | Name      | Type | Width
1785   1     | N/A-Leave Empty            | RC        | CHAR |     2
1786   2     | N/A-Leave Empty            | CUSTID    | CHAR |    15
1787   3     | Account Number             | TRACCTNUM | CHAR |    15
1788   4     | Invoice Number             | TRINVOICE | CHAR |    15
1789   5     | Line Sequence (sort order) | LINESEQ   | NUM  |     6
1790   6     | Transaction Detail         | DETAILS   | CHAR |   100
1791   7     | Amount                     | AMT       | NUM* |     9
1792   8     | Line Format Control**      | LNCTRL    | CHAR |     2
1793   9     | Grouping Code              | GROUP     | CHAR |     2
1794   10    | User Defined               | ACCT CODE | CHAR |    15
1795
1796 =cut
1797
1798 sub print_csv {
1799   my($self, %opt) = @_;
1800   
1801   eval "use Text::CSV_XS";
1802   die $@ if $@;
1803
1804   my $cust_main = $self->cust_main;
1805
1806   my $csv = Text::CSV_XS->new({'always_quote'=>1});
1807
1808   if ( lc($opt{'format'}) eq 'billco' ) {
1809
1810     my $taxtotal = 0;
1811     $taxtotal += $_->{'amount'} foreach $self->_items_tax;
1812
1813     my $duedate = $self->due_date2str('%m/%d/%Y'); #date_format?
1814
1815     my( $previous_balance, @unused ) = $self->previous; #previous balance
1816
1817     my $pmt_cr_applied = 0;
1818     $pmt_cr_applied += $_->{'amount'}
1819       foreach ( $self->_items_payments, $self->_items_credits ) ;
1820
1821     my $totaldue = sprintf('%.2f', $self->owed + $previous_balance);
1822
1823     $csv->combine(
1824       '',                         #  1 | N/A-Leave Empty               CHAR   2
1825       '',                         #  2 | N/A-Leave Empty               CHAR  15
1826       $opt{'tracctnum'},          #  3 | Transaction Account No        CHAR  15
1827       $self->invnum,              #  4 | Transaction Invoice No        CHAR  15
1828       $cust_main->zip,            #  5 | Transaction Zip Code          CHAR   5
1829       $cust_main->company,        #  6 | Transaction Company Bill To   CHAR  30
1830       #$cust_main->payname,        #  7 | Transaction Contact Bill To   CHAR  30
1831       $cust_main->contact,        #  7 | Transaction Contact Bill To   CHAR  30
1832       $cust_main->address2,       #  8 | Additional Address Unit Info  CHAR  30
1833       $cust_main->address1,       #  9 | Bill To Street Address        CHAR  30
1834       '',                         # 10 | Ancillary Billing Information CHAR  30
1835       $cust_main->city,           # 11 | Transaction City Bill To      CHAR  20
1836       $cust_main->state,          # 12 | Transaction State Bill To     CHAR   2
1837
1838       # XXX ?
1839       time2str("%m/%d/%Y", $self->_date), # 13 | Bill Cycle Close Date CHAR  10
1840
1841       # XXX ?
1842       $duedate,                   # 14 | Bill Due Date                 CHAR  10
1843
1844       $previous_balance,          # 15 | Previous Balance              NUM*   9
1845       $pmt_cr_applied,            # 16 | Pmt/CR Applied                NUM*   9
1846       sprintf("%.2f", $self->charged), # 17 | Total Current Charges    NUM*   9
1847       $totaldue,                  # 18 | Total Amt Due                 NUM*   9
1848       $totaldue,                  # 19 | Total Amt Due                 NUM*   9
1849       '',                         # 20 | 30 Day Aging                  NUM*   9
1850       '',                         # 21 | 60 Day Aging                  NUM*   9
1851       '',                         # 22 | 90 Day Aging                  NUM*   9
1852       'N',                        # 23 | Y/N                           CHAR   1
1853       '',                         # 24 | Remittance automation         CHAR 100
1854       $taxtotal,                  # 25 | Total Taxes & Fees            NUM*   9
1855       $self->custnum,             # 26 | Customer Reference Number     CHAR  15
1856       '0',                        # 27 | Federal Tax***                NUM*   9
1857       sprintf("%.2f", $taxtotal), # 28 | State Tax***                  NUM*   9
1858       '0',                        # 29 | Other Taxes & Fees***         NUM*   9
1859     );
1860
1861   } else {
1862   
1863     $csv->combine(
1864       'cust_bill',
1865       $self->invnum,
1866       $self->custnum,
1867       time2str("%x", $self->_date),
1868       sprintf("%.2f", $self->charged),
1869       ( map { $cust_main->getfield($_) }
1870           qw( first last company address1 address2 city state zip country ) ),
1871       map { '' } (1..5),
1872     ) or die "can't create csv";
1873   }
1874
1875   my $header = $csv->string. "\n";
1876
1877   my $detail = '';
1878   if ( lc($opt{'format'}) eq 'billco' ) {
1879
1880     my $lineseq = 0;
1881     foreach my $item ( $self->_items_pkg ) {
1882
1883       $csv->combine(
1884         '',                     #  1 | N/A-Leave Empty            CHAR   2
1885         '',                     #  2 | N/A-Leave Empty            CHAR  15
1886         $opt{'tracctnum'},      #  3 | Account Number             CHAR  15
1887         $self->invnum,          #  4 | Invoice Number             CHAR  15
1888         $lineseq++,             #  5 | Line Sequence (sort order) NUM    6
1889         $item->{'description'}, #  6 | Transaction Detail         CHAR 100
1890         $item->{'amount'},      #  7 | Amount                     NUM*   9
1891         '',                     #  8 | Line Format Control**      CHAR   2
1892         '',                     #  9 | Grouping Code              CHAR   2
1893         '',                     # 10 | User Defined               CHAR  15
1894       );
1895
1896       $detail .= $csv->string. "\n";
1897
1898     }
1899
1900   } else {
1901
1902     foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
1903
1904       my($pkg, $setup, $recur, $sdate, $edate);
1905       if ( $cust_bill_pkg->pkgnum ) {
1906       
1907         ($pkg, $setup, $recur, $sdate, $edate) = (
1908           $cust_bill_pkg->part_pkg->pkg,
1909           ( $cust_bill_pkg->setup != 0
1910             ? sprintf("%.2f", $cust_bill_pkg->setup )
1911             : '' ),
1912           ( $cust_bill_pkg->recur != 0
1913             ? sprintf("%.2f", $cust_bill_pkg->recur )
1914             : '' ),
1915           ( $cust_bill_pkg->sdate 
1916             ? time2str("%x", $cust_bill_pkg->sdate)
1917             : '' ),
1918           ($cust_bill_pkg->edate 
1919             ?time2str("%x", $cust_bill_pkg->edate)
1920             : '' ),
1921         );
1922   
1923       } else { #pkgnum tax
1924         next unless $cust_bill_pkg->setup != 0;
1925         $pkg = $cust_bill_pkg->desc;
1926         $setup = sprintf('%10.2f', $cust_bill_pkg->setup );
1927         ( $sdate, $edate ) = ( '', '' );
1928       }
1929   
1930       $csv->combine(
1931         'cust_bill_pkg',
1932         $self->invnum,
1933         ( map { '' } (1..11) ),
1934         ($pkg, $setup, $recur, $sdate, $edate)
1935       ) or die "can't create csv";
1936
1937       $detail .= $csv->string. "\n";
1938
1939     }
1940
1941   }
1942
1943   ( $header, $detail );
1944
1945 }
1946
1947 =item comp
1948
1949 Pays this invoice with a compliemntary payment.  If there is an error,
1950 returns the error, otherwise returns false.
1951
1952 =cut
1953
1954 sub comp {
1955   my $self = shift;
1956   my $cust_pay = new FS::cust_pay ( {
1957     'invnum'   => $self->invnum,
1958     'paid'     => $self->owed,
1959     '_date'    => '',
1960     'payby'    => 'COMP',
1961     'payinfo'  => $self->cust_main->payinfo,
1962     'paybatch' => '',
1963   } );
1964   $cust_pay->insert;
1965 }
1966
1967 =item realtime_card
1968
1969 Attempts to pay this invoice with a credit card payment via a
1970 Business::OnlinePayment realtime gateway.  See
1971 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
1972 for supported processors.
1973
1974 =cut
1975
1976 sub realtime_card {
1977   my $self = shift;
1978   $self->realtime_bop( 'CC', @_ );
1979 }
1980
1981 =item realtime_ach
1982
1983 Attempts to pay this invoice with an electronic check (ACH) payment via a
1984 Business::OnlinePayment realtime gateway.  See
1985 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
1986 for supported processors.
1987
1988 =cut
1989
1990 sub realtime_ach {
1991   my $self = shift;
1992   $self->realtime_bop( 'ECHECK', @_ );
1993 }
1994
1995 =item realtime_lec
1996
1997 Attempts to pay this invoice with phone bill (LEC) payment via a
1998 Business::OnlinePayment realtime gateway.  See
1999 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
2000 for supported processors.
2001
2002 =cut
2003
2004 sub realtime_lec {
2005   my $self = shift;
2006   $self->realtime_bop( 'LEC', @_ );
2007 }
2008
2009 sub realtime_bop {
2010   my( $self, $method ) = (shift,shift);
2011   my %opt = @_;
2012
2013   my $cust_main = $self->cust_main;
2014   my $balance = $cust_main->balance;
2015   my $amount = ( $balance < $self->owed ) ? $balance : $self->owed;
2016   $amount = sprintf("%.2f", $amount);
2017   return "not run (balance $balance)" unless $amount > 0;
2018
2019   my $description = 'Internet Services';
2020   if ( $conf->exists('business-onlinepayment-description') ) {
2021     my $dtempl = $conf->config('business-onlinepayment-description');
2022
2023     my $agent_obj = $cust_main->agent
2024       or die "can't retreive agent for $cust_main (agentnum ".
2025              $cust_main->agentnum. ")";
2026     my $agent = $agent_obj->agent;
2027     my $pkgs = join(', ',
2028       map { $_->part_pkg->pkg }
2029         grep { $_->pkgnum } $self->cust_bill_pkg
2030     );
2031     $description = eval qq("$dtempl");
2032   }
2033
2034   $cust_main->realtime_bop($method, $amount,
2035     'description' => $description,
2036     'invnum'      => $self->invnum,
2037 #this didn't do what we want, it just calls apply_payments_and_credits
2038 #    'apply'       => 1,
2039     'apply_to_invoice' => 1,
2040     %opt,
2041  #what we want:
2042  #this changes application behavior: auto payments
2043                         #triggered against a specific invoice are now applied
2044                         #to that invoice instead of oldest open.
2045                         #seem okay to me...
2046   );
2047
2048 }
2049
2050 =item batch_card OPTION => VALUE...
2051
2052 Adds a payment for this invoice to the pending credit card batch (see
2053 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2054 runs the payment using a realtime gateway.
2055
2056 =cut
2057
2058 sub batch_card {
2059   my ($self, %options) = @_;
2060   my $cust_main = $self->cust_main;
2061
2062   $options{invnum} = $self->invnum;
2063   
2064   $cust_main->batch_card(%options);
2065 }
2066
2067 sub _agent_template {
2068   my $self = shift;
2069   $self->cust_main->agent_template;
2070 }
2071
2072 sub _agent_invoice_from {
2073   my $self = shift;
2074   $self->cust_main->agent_invoice_from;
2075 }
2076
2077 =item print_text HASHREF | [ TIME [ , TEMPLATE [ , OPTION => VALUE ... ] ] ]
2078
2079 Returns an text invoice, as a list of lines.
2080
2081 Options can be passed as a hashref (recommended) or as a list of time, template
2082 and then any key/value pairs for any other options.
2083
2084 I<time>, if specified, is used to control the printing of overdue messages.  The
2085 default is now.  It isn't the date of the invoice; that's the `_date' field.
2086 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
2087 L<Time::Local> and L<Date::Parse> for conversion functions.
2088
2089 I<template>, if specified, is the name of a suffix for alternate invoices.
2090
2091 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
2092
2093 =cut
2094
2095 sub print_text {
2096   my $self = shift;
2097   my( $today, $template, %opt );
2098   if ( ref($_[0]) ) {
2099     %opt = %{ shift() };
2100     $today = delete($opt{'time'}) || '';
2101     $template = delete($opt{template}) || '';
2102   } else {
2103     ( $today, $template, %opt ) = @_;
2104   }
2105
2106   my %params = ( 'format' => 'template' );
2107   $params{'time'} = $today if $today;
2108   $params{'template'} = $template if $template;
2109   $params{$_} = $opt{$_} 
2110     foreach grep $opt{$_}, qw( unsquealch_cdr notice_name );
2111
2112   $self->print_generic( %params );
2113 }
2114
2115 =item print_latex HASHREF | [ TIME [ , TEMPLATE [ , OPTION => VALUE ... ] ] ]
2116
2117 Internal method - returns a filename of a filled-in LaTeX template for this
2118 invoice (Note: add ".tex" to get the actual filename), and a filename of
2119 an associated logo (with the .eps extension included).
2120
2121 See print_ps and print_pdf for methods that return PostScript and PDF output.
2122
2123 Options can be passed as a hashref (recommended) or as a list of time, template
2124 and then any key/value pairs for any other options.
2125
2126 I<time>, if specified, is used to control the printing of overdue messages.  The
2127 default is now.  It isn't the date of the invoice; that's the `_date' field.
2128 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
2129 L<Time::Local> and L<Date::Parse> for conversion functions.
2130
2131 I<template>, if specified, is the name of a suffix for alternate invoices.
2132
2133 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
2134
2135 =cut
2136
2137 sub print_latex {
2138   my $self = shift;
2139   my( $today, $template, %opt );
2140   if ( ref($_[0]) ) {
2141     %opt = %{ shift() };
2142     $today = delete($opt{'time'}) || '';
2143     $template = delete($opt{template}) || '';
2144   } else {
2145     ( $today, $template, %opt ) = @_;
2146   }
2147
2148   my %params = ( 'format' => 'latex' );
2149   $params{'time'} = $today if $today;
2150   $params{'template'} = $template if $template;
2151   $params{$_} = $opt{$_} 
2152     foreach grep $opt{$_}, qw( unsquealch_cdr notice_name );
2153
2154   $template ||= $self->_agent_template;
2155
2156   my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2157   my $lh = new File::Temp( TEMPLATE => 'invoice.'. $self->invnum. '.XXXXXXXX',
2158                            DIR      => $dir,
2159                            SUFFIX   => '.eps',
2160                            UNLINK   => 0,
2161                          ) or die "can't open temp file: $!\n";
2162
2163   my $agentnum = $self->cust_main->agentnum;
2164
2165   if ( $template && $conf->exists("logo_${template}.eps", $agentnum) ) {
2166     print $lh $conf->config_binary("logo_${template}.eps", $agentnum)
2167       or die "can't write temp file: $!\n";
2168   } else {
2169     print $lh $conf->config_binary('logo.eps', $agentnum)
2170       or die "can't write temp file: $!\n";
2171   }
2172   close $lh;
2173   $params{'logo_file'} = $lh->filename;
2174
2175   if($conf->exists('invoice-barcode')){
2176       my $png_file = $self->invoice_barcode($dir);
2177       my $eps_file = $png_file;
2178       $eps_file =~ s/\.png$/.eps/g;
2179       $png_file =~ /(barcode.*png)/;
2180       $png_file = $1;
2181       $eps_file =~ /(barcode.*eps)/;
2182       $eps_file = $1;
2183
2184       my $curr_dir = cwd();
2185       chdir($dir); 
2186       # after painfuly long experimentation, it was determined that sam2p won't
2187       # accept : and other chars in the path, no matter how hard I tried to
2188       # escape them, hence the chdir (and chdir back, just to be safe)
2189       system('sam2p', '-j:quiet', $png_file, 'EPS:', $eps_file ) == 0
2190         or die "sam2p failed: $!\n";
2191       unlink($png_file);
2192       chdir($curr_dir);
2193
2194       $params{'barcode_file'} = $eps_file;
2195   }
2196
2197   my @filled_in = $self->print_generic( %params );
2198   
2199   my $fh = new File::Temp( TEMPLATE => 'invoice.'. $self->invnum. '.XXXXXXXX',
2200                            DIR      => $dir,
2201                            SUFFIX   => '.tex',
2202                            UNLINK   => 0,
2203                          ) or die "can't open temp file: $!\n";
2204   print $fh join('', @filled_in );
2205   close $fh;
2206
2207   $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
2208   return ($1, $params{'logo_file'}, $params{'barcode_file'});
2209
2210 }
2211
2212 =item invoice_barcode DIR_OR_FALSE
2213
2214 Generates an invoice barcode PNG. If DIR_OR_FALSE is a true value,
2215 it is taken as the temp directory where the PNG file will be generated and the
2216 PNG file name is returned. Otherwise, the PNG image itself is returned.
2217
2218 =cut
2219
2220 sub invoice_barcode {
2221     my ($self, $dir) = (shift,shift);
2222     
2223     my $gdbar = new GD::Barcode('Code39',$self->invnum);
2224         die "can't create barcode: " . $GD::Barcode::errStr unless $gdbar;
2225     my $gd = $gdbar->plot(Height => 30);
2226
2227     if($dir) {
2228         my $bh = new File::Temp( TEMPLATE => 'barcode.'. $self->invnum. '.XXXXXXXX',
2229                            DIR      => $dir,
2230                            SUFFIX   => '.png',
2231                            UNLINK   => 0,
2232                          ) or die "can't open temp file: $!\n";
2233         print $bh $gd->png or die "cannot write barcode to file: $!\n";
2234         my $png_file = $bh->filename;
2235         close $bh;
2236         return $png_file;
2237     }
2238     return $gd->png;
2239 }
2240
2241 =item print_generic OPTION => VALUE ...
2242
2243 Internal method - returns a filled-in template for this invoice as a scalar.
2244
2245 See print_ps and print_pdf for methods that return PostScript and PDF output.
2246
2247 Non optional options include 
2248   format - latex, html, template
2249
2250 Optional options include
2251
2252 template - a value used as a suffix for a configuration template
2253
2254 time - a value used to control the printing of overdue messages.  The
2255 default is now.  It isn't the date of the invoice; that's the `_date' field.
2256 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
2257 L<Time::Local> and L<Date::Parse> for conversion functions.
2258
2259 cid - 
2260
2261 unsquelch_cdr - overrides any per customer cdr squelching when true
2262
2263 notice_name - overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
2264
2265 =cut
2266
2267 #what's with all the sprintf('%10.2f')'s in here?  will it cause any
2268 # (alignment in text invoice?) problems to change them all to '%.2f' ?
2269 # yes: fixed width (dot matrix) text printing will be borked
2270 sub print_generic {
2271
2272   my( $self, %params ) = @_;
2273   my $today = $params{today} ? $params{today} : time;
2274   warn "$me print_generic called on $self with suffix $params{template}\n"
2275     if $DEBUG;
2276
2277   my $format = $params{format};
2278   die "Unknown format: $format"
2279     unless $format =~ /^(latex|html|template)$/;
2280
2281   my $cust_main = $self->cust_main;
2282   $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
2283     unless $cust_main->payname
2284         && $cust_main->payby !~ /^(CARD|DCRD|CHEK|DCHK)$/;
2285
2286   my %delimiters = ( 'latex'    => [ '[@--', '--@]' ],
2287                      'html'     => [ '<%=', '%>' ],
2288                      'template' => [ '{', '}' ],
2289                    );
2290
2291   warn "$me print_generic creating template\n"
2292     if $DEBUG > 1;
2293
2294   #create the template
2295   my $template = $params{template} ? $params{template} : $self->_agent_template;
2296   my $templatefile = "invoice_$format";
2297   $templatefile .= "_$template"
2298     if length($template);
2299   my @invoice_template = map "$_\n", $conf->config($templatefile)
2300     or die "cannot load config data $templatefile";
2301
2302   my $old_latex = '';
2303   if ( $format eq 'latex' && grep { /^%%Detail/ } @invoice_template ) {
2304     #change this to a die when the old code is removed
2305     warn "old-style invoice template $templatefile; ".
2306          "patch with conf/invoice_latex.diff or use new conf/invoice_latex*\n";
2307     $old_latex = 'true';
2308     @invoice_template = _translate_old_latex_format(@invoice_template);
2309   } 
2310
2311   warn "$me print_generic creating T:T object\n"
2312     if $DEBUG > 1;
2313
2314   my $text_template = new Text::Template(
2315     TYPE => 'ARRAY',
2316     SOURCE => \@invoice_template,
2317     DELIMITERS => $delimiters{$format},
2318   );
2319
2320   warn "$me print_generic compiling T:T object\n"
2321     if $DEBUG > 1;
2322
2323   $text_template->compile()
2324     or die "Can't compile $templatefile: $Text::Template::ERROR\n";
2325
2326
2327   # additional substitution could possibly cause breakage in existing templates
2328   my %convert_maps = ( 
2329     'latex' => {
2330                  'notes'         => sub { map "$_", @_ },
2331                  'footer'        => sub { map "$_", @_ },
2332                  'smallfooter'   => sub { map "$_", @_ },
2333                  'returnaddress' => sub { map "$_", @_ },
2334                  'coupon'        => sub { map "$_", @_ },
2335                  'summary'       => sub { map "$_", @_ },
2336                },
2337     'html'  => {
2338                  'notes' =>
2339                    sub {
2340                      map { 
2341                        s/%%(.*)$/<!-- $1 -->/g;
2342                        s/\\section\*\{\\textsc\{(.)(.*)\}\}/<p><b><font size="+1">$1<\/font>\U$2<\/b>/g;
2343                        s/\\begin\{enumerate\}/<ol>/g;
2344                        s/\\item /  <li>/g;
2345                        s/\\end\{enumerate\}/<\/ol>/g;
2346                        s/\\textbf\{(.*)\}/<b>$1<\/b>/g;
2347                        s/\\\\\*/<br>/g;
2348                        s/\\dollar ?/\$/g;
2349                        s/\\#/#/g;
2350                        s/~/&nbsp;/g;
2351                        $_;
2352                      }  @_
2353                    },
2354                  'footer' =>
2355                    sub { map { s/~/&nbsp;/g; s/\\\\\*?\s*$/<BR>/; $_; } @_ },
2356                  'smallfooter' =>
2357                    sub { map { s/~/&nbsp;/g; s/\\\\\*?\s*$/<BR>/; $_; } @_ },
2358                  'returnaddress' =>
2359                    sub {
2360                      map { 
2361                        s/~/&nbsp;/g;
2362                        s/\\\\\*?\s*$/<BR>/;
2363                        s/\\hyphenation\{[\w\s\-]+}//;
2364                        s/\\([&])/$1/g;
2365                        $_;
2366                      }  @_
2367                    },
2368                  'coupon'        => sub { "" },
2369                  'summary'       => sub { "" },
2370                },
2371     'template' => {
2372                  'notes' =>
2373                    sub {
2374                      map { 
2375                        s/%%.*$//g;
2376                        s/\\section\*\{\\textsc\{(.*)\}\}/\U$1/g;
2377                        s/\\begin\{enumerate\}//g;
2378                        s/\\item /  * /g;
2379                        s/\\end\{enumerate\}//g;
2380                        s/\\textbf\{(.*)\}/$1/g;
2381                        s/\\\\\*/ /;
2382                        s/\\dollar ?/\$/g;
2383                        $_;
2384                      }  @_
2385                    },
2386                  'footer' =>
2387                    sub { map { s/~/ /g; s/\\\\\*?\s*$/\n/; $_; } @_ },
2388                  'smallfooter' =>
2389                    sub { map { s/~/ /g; s/\\\\\*?\s*$/\n/; $_; } @_ },
2390                  'returnaddress' =>
2391                    sub {
2392                      map { 
2393                        s/~/ /g;
2394                        s/\\\\\*?\s*$/\n/;             # dubious
2395                        s/\\hyphenation\{[\w\s\-]+}//;
2396                        $_;
2397                      }  @_
2398                    },
2399                  'coupon'        => sub { "" },
2400                  'summary'       => sub { "" },
2401                },
2402   );
2403
2404
2405   # hashes for differing output formats
2406   my %nbsps = ( 'latex'    => '~',
2407                 'html'     => '',    # '&nbps;' would be nice
2408                 'template' => '',    # not used
2409               );
2410   my $nbsp = $nbsps{$format};
2411
2412   my %escape_functions = ( 'latex'    => \&_latex_escape,
2413                            'html'     => \&_html_escape_nbsp,#\&encode_entities,
2414                            'template' => sub { shift },
2415                          );
2416   my $escape_function = $escape_functions{$format};
2417   my $escape_function_nonbsp = ($format eq 'html')
2418                                  ? \&_html_escape : $escape_function;
2419
2420   my %date_formats = ( 'latex'    => $date_format_long,
2421                        'html'     => $date_format_long,
2422                        'template' => '%s',
2423                      );
2424   $date_formats{'html'} =~ s/ /&nbsp;/g;
2425
2426   my $date_format = $date_formats{$format};
2427
2428   my %embolden_functions = ( 'latex'    => sub { return '\textbf{'. shift(). '}'
2429                                                },
2430                              'html'     => sub { return '<b>'. shift(). '</b>'
2431                                                },
2432                              'template' => sub { shift },
2433                            );
2434   my $embolden_function = $embolden_functions{$format};
2435
2436   my %newline_tokens = (  'latex'     => '\\\\',
2437                           'html'      => '<br>',
2438                           'template'  => "\n",
2439                         );
2440   my $newline_token = $newline_tokens{$format};
2441
2442   warn "$me generating template variables\n"
2443     if $DEBUG > 1;
2444
2445   # generate template variables
2446   my $returnaddress;
2447   if (
2448          defined( $conf->config_orbase( "invoice_${format}returnaddress",
2449                                         $template
2450                                       )
2451                 )
2452        && length( $conf->config_orbase( "invoice_${format}returnaddress",
2453                                         $template
2454                                       )
2455                 )
2456   ) {
2457
2458     $returnaddress = join("\n",
2459       $conf->config_orbase("invoice_${format}returnaddress", $template)
2460     );
2461
2462   } elsif ( grep /\S/,
2463             $conf->config_orbase('invoice_latexreturnaddress', $template) ) {
2464
2465     my $convert_map = $convert_maps{$format}{'returnaddress'};
2466     $returnaddress =
2467       join( "\n",
2468             &$convert_map( $conf->config_orbase( "invoice_latexreturnaddress",
2469                                                  $template
2470                                                )
2471                          )
2472           );
2473   } elsif ( grep /\S/, $conf->config('company_address', $self->cust_main->agentnum) ) {
2474
2475     my $convert_map = $convert_maps{$format}{'returnaddress'};
2476     $returnaddress = join( "\n", &$convert_map(
2477                                    map { s/( {2,})/'~' x length($1)/eg;
2478                                          s/$/\\\\\*/;
2479                                          $_
2480                                        }
2481                                      ( $conf->config('company_name', $self->cust_main->agentnum),
2482                                        $conf->config('company_address', $self->cust_main->agentnum),
2483                                      )
2484                                  )
2485                      );
2486
2487   } else {
2488
2489     my $warning = "Couldn't find a return address; ".
2490                   "do you need to set the company_address configuration value?";
2491     warn "$warning\n";
2492     $returnaddress = $nbsp;
2493     #$returnaddress = $warning;
2494
2495   }
2496
2497   warn "$me generating invoice data\n"
2498     if $DEBUG > 1;
2499
2500   my $agentnum = $self->cust_main->agentnum;
2501
2502   my %invoice_data = (
2503
2504     #invoice from info
2505     'company_name'    => scalar( $conf->config('company_name', $agentnum) ),
2506     'company_address' => join("\n", $conf->config('company_address', $agentnum) ). "\n",
2507     'returnaddress'   => $returnaddress,
2508     'agent'           => &$escape_function($cust_main->agent->agent),
2509
2510     #invoice info
2511     'invnum'          => $self->invnum,
2512     'date'            => time2str($date_format, $self->_date),
2513     'today'           => time2str($date_format_long, $today),
2514     'terms'           => $self->terms,
2515     'template'        => $template, #params{'template'},
2516     'notice_name'     => ($params{'notice_name'} || 'Invoice'),#escape_function?
2517     'current_charges' => sprintf("%.2f", $self->charged),
2518     'duedate'         => $self->due_date2str($rdate_format), #date_format?
2519
2520     #customer info
2521     'custnum'         => $cust_main->display_custnum,
2522     'agent_custid'    => &$escape_function($cust_main->agent_custid),
2523     ( map { $_ => &$escape_function($cust_main->$_()) } qw(
2524       payname company address1 address2 city state zip fax
2525     )),
2526
2527     #global config
2528     'ship_enable'     => $conf->exists('invoice-ship_address'),
2529     'unitprices'      => $conf->exists('invoice-unitprice'),
2530     'smallernotes'    => $conf->exists('invoice-smallernotes'),
2531     'smallerfooter'   => $conf->exists('invoice-smallerfooter'),
2532     'balance_due_below_line' => $conf->exists('balance_due_below_line'),
2533    
2534     #layout info -- would be fancy to calc some of this and bury the template
2535     #               here in the code
2536     'topmargin'             => scalar($conf->config('invoice_latextopmargin', $agentnum)),
2537     'headsep'               => scalar($conf->config('invoice_latexheadsep', $agentnum)),
2538     'textheight'            => scalar($conf->config('invoice_latextextheight', $agentnum)),
2539     'extracouponspace'      => scalar($conf->config('invoice_latexextracouponspace', $agentnum)),
2540     'couponfootsep'         => scalar($conf->config('invoice_latexcouponfootsep', $agentnum)),
2541     'verticalreturnaddress' => $conf->exists('invoice_latexverticalreturnaddress', $agentnum),
2542     'addresssep'            => scalar($conf->config('invoice_latexaddresssep', $agentnum)),
2543     'amountenclosedsep'     => scalar($conf->config('invoice_latexcouponamountenclosedsep', $agentnum)),
2544     'coupontoaddresssep'    => scalar($conf->config('invoice_latexcoupontoaddresssep', $agentnum)),
2545     'addcompanytoaddress'   => $conf->exists('invoice_latexcouponaddcompanytoaddress', $agentnum),
2546
2547     # better hang on to conf_dir for a while (for old templates)
2548     'conf_dir'        => "$FS::UID::conf_dir/conf.$FS::UID::datasrc",
2549
2550     #these are only used when doing paged plaintext
2551     'page'            => 1,
2552     'total_pages'     => 1,
2553
2554   );
2555   
2556   my $min_sdate = 999999999999;
2557   my $max_edate = 0;
2558   foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
2559     next unless $cust_bill_pkg->pkgnum > 0;
2560     $min_sdate = $cust_bill_pkg->sdate
2561       if length($cust_bill_pkg->sdate) && $cust_bill_pkg->sdate < $min_sdate;
2562     $max_edate = $cust_bill_pkg->edate
2563       if length($cust_bill_pkg->edate) && $cust_bill_pkg->edate > $max_edate;
2564   }
2565
2566   $invoice_data{'bill_period'} = '';
2567   $invoice_data{'bill_period'} = time2str('%e %h', $min_sdate) 
2568     . " to " . time2str('%e %h', $max_edate)
2569     if ($max_edate != 0 && $min_sdate != 999999999999);
2570
2571   $invoice_data{finance_section} = '';
2572   if ( $conf->config('finance_pkgclass') ) {
2573     my $pkg_class =
2574       qsearchs('pkg_class', { classnum => $conf->config('finance_pkgclass') });
2575     $invoice_data{finance_section} = $pkg_class->categoryname;
2576   } 
2577   $invoice_data{finance_amount} = '0.00';
2578   $invoice_data{finance_section} ||= 'Finance Charges'; #avoid config confusion
2579
2580   my $countrydefault = $conf->config('countrydefault') || 'US';
2581   my $prefix = $cust_main->has_ship_address ? 'ship_' : '';
2582   foreach ( qw( contact company address1 address2 city state zip country fax) ){
2583     my $method = $prefix.$_;
2584     $invoice_data{"ship_$_"} = _latex_escape($cust_main->$method);
2585   }
2586   $invoice_data{'ship_country'} = ''
2587     if ( $invoice_data{'ship_country'} eq $countrydefault );
2588   
2589   $invoice_data{'cid'} = $params{'cid'}
2590     if $params{'cid'};
2591
2592   if ( $cust_main->country eq $countrydefault ) {
2593     $invoice_data{'country'} = '';
2594   } else {
2595     $invoice_data{'country'} = &$escape_function(code2country($cust_main->country));
2596   }
2597
2598   my @address = ();
2599   $invoice_data{'address'} = \@address;
2600   push @address,
2601     $cust_main->payname.
2602       ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
2603         ? " (P.O. #". $cust_main->payinfo. ")"
2604         : ''
2605       )
2606   ;
2607   push @address, $cust_main->company
2608     if $cust_main->company;
2609   push @address, $cust_main->address1;
2610   push @address, $cust_main->address2
2611     if $cust_main->address2;
2612   push @address,
2613     $cust_main->city. ", ". $cust_main->state. "  ".  $cust_main->zip;
2614   push @address, $invoice_data{'country'}
2615     if $invoice_data{'country'};
2616   push @address, ''
2617     while (scalar(@address) < 5);
2618
2619   $invoice_data{'logo_file'} = $params{'logo_file'}
2620     if $params{'logo_file'};
2621   $invoice_data{'barcode_file'} = $params{'barcode_file'}
2622     if $params{'barcode_file'};
2623   $invoice_data{'barcode_img'} = $params{'barcode_img'}
2624     if $params{'barcode_img'};
2625   $invoice_data{'barcode_cid'} = $params{'barcode_cid'}
2626     if $params{'barcode_cid'};
2627
2628   my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
2629 #  my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
2630   #my $balance_due = $self->owed + $pr_total - $cr_total;
2631   my $balance_due = $self->owed + $pr_total;
2632   $invoice_data{'true_previous_balance'} = sprintf("%.2f", ($self->previous_balance || 0) );
2633   $invoice_data{'balance_adjustments'} = sprintf("%.2f", ($self->previous_balance || 0) - ($self->billing_balance || 0) );
2634   $invoice_data{'previous_balance'} = sprintf("%.2f", $pr_total);
2635   $invoice_data{'balance'} = sprintf("%.2f", $balance_due);
2636
2637   my $summarypage = '';
2638   if ( $conf->exists('invoice_usesummary', $agentnum) ) {
2639     $summarypage = 1;
2640   }
2641   $invoice_data{'summarypage'} = $summarypage;
2642
2643   warn "$me substituting variables in notes, footer, smallfooter\n"
2644     if $DEBUG > 1;
2645
2646   foreach my $include (qw( notes footer smallfooter coupon )) {
2647
2648     my $inc_file = $conf->key_orbase("invoice_${format}$include", $template);
2649     my @inc_src;
2650
2651     if ( $conf->exists($inc_file, $agentnum)
2652          && length( $conf->config($inc_file, $agentnum) ) ) {
2653
2654       @inc_src = $conf->config($inc_file, $agentnum);
2655
2656     } else {
2657
2658       $inc_file = $conf->key_orbase("invoice_latex$include", $template);
2659
2660       my $convert_map = $convert_maps{$format}{$include};
2661
2662       @inc_src = map { s/\[\@--/$delimiters{$format}[0]/g;
2663                        s/--\@\]/$delimiters{$format}[1]/g;
2664                        $_;
2665                      } 
2666                  &$convert_map( $conf->config($inc_file, $agentnum) );
2667
2668     }
2669
2670     my $inc_tt = new Text::Template (
2671       TYPE       => 'ARRAY',
2672       SOURCE     => [ map "$_\n", @inc_src ],
2673       DELIMITERS => $delimiters{$format},
2674     ) or die "Can't create new Text::Template object: $Text::Template::ERROR";
2675
2676     unless ( $inc_tt->compile() ) {
2677       my $error = "Can't compile $inc_file template: $Text::Template::ERROR\n";
2678       warn $error. "Template:\n". join('', map "$_\n", @inc_src);
2679       die $error;
2680     }
2681
2682     $invoice_data{$include} = $inc_tt->fill_in( HASH => \%invoice_data );
2683
2684     $invoice_data{$include} =~ s/\n+$//
2685       if ($format eq 'latex');
2686   }
2687
2688   $invoice_data{'po_line'} =
2689     (  $cust_main->payby eq 'BILL' && $cust_main->payinfo )
2690       ? &$escape_function("Purchase Order #". $cust_main->payinfo)
2691       : $nbsp;
2692
2693   my %money_chars = ( 'latex'    => '',
2694                       'html'     => $conf->config('money_char') || '$',
2695                       'template' => '',
2696                     );
2697   my $money_char = $money_chars{$format};
2698
2699   my %other_money_chars = ( 'latex'    => '\dollar ',#XXX should be a config too
2700                             'html'     => $conf->config('money_char') || '$',
2701                             'template' => '',
2702                           );
2703   my $other_money_char = $other_money_chars{$format};
2704   $invoice_data{'dollar'} = $other_money_char;
2705
2706   my @detail_items = ();
2707   my @total_items = ();
2708   my @buf = ();
2709   my @sections = ();
2710
2711   $invoice_data{'detail_items'} = \@detail_items;
2712   $invoice_data{'total_items'} = \@total_items;
2713   $invoice_data{'buf'} = \@buf;
2714   $invoice_data{'sections'} = \@sections;
2715
2716   warn "$me generating sections\n"
2717     if $DEBUG > 1;
2718
2719   my $previous_section = { 'description' => 'Previous Charges',
2720                            'subtotal'    => $other_money_char.
2721                                             sprintf('%.2f', $pr_total),
2722                            'summarized'  => $summarypage ? 'Y' : '',
2723                          };
2724   $previous_section->{posttotal} = '0 / 30 / 60/ 90 days overdue '. 
2725     join(' / ', map { $cust_main->balance_date_range(@$_) }
2726                 $self->_prior_month30s
2727         )
2728     if $conf->exists('invoice_include_aging');
2729
2730   my $taxtotal = 0;
2731   my $tax_section = { 'description' => 'Taxes, Surcharges, and Fees',
2732                       'subtotal'    => $taxtotal,   # adjusted below
2733                       'summarized'  => $summarypage ? 'Y' : '',
2734                     };
2735   my $tax_weight = _pkg_category($tax_section->{description})
2736                         ? _pkg_category($tax_section->{description})->weight
2737                         : 0;
2738   $tax_section->{'summarized'} = $summarypage && !$tax_weight ? 'Y' : '';
2739   $tax_section->{'sort_weight'} = $tax_weight;
2740
2741
2742   my $adjusttotal = 0;
2743   my $adjust_section = { 'description' => 'Credits, Payments, and Adjustments',
2744                          'subtotal'    => 0,   # adjusted below
2745                          'summarized'  => $summarypage ? 'Y' : '',
2746                        };
2747   my $adjust_weight = _pkg_category($adjust_section->{description})
2748                         ? _pkg_category($adjust_section->{description})->weight
2749                         : 0;
2750   $adjust_section->{'summarized'} = $summarypage && !$adjust_weight ? 'Y' : '';
2751   $adjust_section->{'sort_weight'} = $adjust_weight;
2752
2753   my $unsquelched = $params{unsquelch_cdr} || $cust_main->squelch_cdr ne 'Y';
2754   my $multisection = $conf->exists('invoice_sections', $cust_main->agentnum);
2755   $invoice_data{'multisection'} = $multisection;
2756   my $late_sections = [];
2757   my $extra_sections = [];
2758   my $extra_lines = ();
2759   if ( $multisection ) {
2760     ($extra_sections, $extra_lines) =
2761       $self->_items_extra_usage_sections($escape_function_nonbsp, $format)
2762       if $conf->exists('usage_class_as_a_section', $cust_main->agentnum);
2763
2764     push @$extra_sections, $adjust_section if $adjust_section->{sort_weight};
2765
2766     push @detail_items, @$extra_lines if $extra_lines;
2767     push @sections,
2768       $self->_items_sections( $late_sections,      # this could stand a refactor
2769                               $summarypage,
2770                               $escape_function_nonbsp,
2771                               $extra_sections,
2772                               $format,             #bah
2773                             );
2774     if ($conf->exists('svc_phone_sections')) {
2775       my ($phone_sections, $phone_lines) =
2776         $self->_items_svc_phone_sections($escape_function_nonbsp, $format);
2777       push @{$late_sections}, @$phone_sections;
2778       push @detail_items, @$phone_lines;
2779     }
2780   }else{
2781     push @sections, { 'description' => '', 'subtotal' => '' };
2782   }
2783
2784   unless (    $conf->exists('disable_previous_balance')
2785            || $conf->exists('previous_balance-summary_only')
2786          )
2787   {
2788
2789     warn "$me adding previous balances\n"
2790       if $DEBUG > 1;
2791
2792     foreach my $line_item ( $self->_items_previous ) {
2793
2794       my $detail = {
2795         ext_description => [],
2796       };
2797       $detail->{'ref'} = $line_item->{'pkgnum'};
2798       $detail->{'quantity'} = 1;
2799       $detail->{'section'} = $previous_section;
2800       $detail->{'description'} = &$escape_function($line_item->{'description'});
2801       if ( exists $line_item->{'ext_description'} ) {
2802         @{$detail->{'ext_description'}} = map {
2803           &$escape_function($_);
2804         } @{$line_item->{'ext_description'}};
2805       }
2806       $detail->{'amount'} = ( $old_latex ? '' : $money_char).
2807                             $line_item->{'amount'};
2808       $detail->{'product_code'} = $line_item->{'pkgpart'} || 'N/A';
2809
2810       push @detail_items, $detail;
2811       push @buf, [ $detail->{'description'},
2812                    $money_char. sprintf("%10.2f", $line_item->{'amount'}),
2813                  ];
2814     }
2815
2816   }
2817
2818   if ( @pr_cust_bill && !$conf->exists('disable_previous_balance') ) {
2819     push @buf, ['','-----------'];
2820     push @buf, [ 'Total Previous Balance',
2821                  $money_char. sprintf("%10.2f", $pr_total) ];
2822     push @buf, ['',''];
2823   }
2824  
2825   if ( $conf->exists('svc_phone-did-summary') ) {
2826       warn "$me adding DID summary\n"
2827         if $DEBUG > 1;
2828
2829       my ($didsummary,$minutes) = $self->_did_summary;
2830       my $didsummary_desc = 'DID Activity Summary (Past 30 days)';
2831       push @detail_items, 
2832         { 'description' => $didsummary_desc,
2833             'ext_description' => [ $didsummary, $minutes ],
2834         }
2835         if !$multisection;
2836   }
2837
2838   foreach my $section (@sections, @$late_sections) {
2839
2840     warn "$me adding section \n". Dumper($section)
2841       if $DEBUG > 1;
2842
2843     # begin some normalization
2844     $section->{'subtotal'} = $section->{'amount'}
2845       if $multisection
2846          && !exists($section->{subtotal})
2847          && exists($section->{amount});
2848
2849     $invoice_data{finance_amount} = sprintf('%.2f', $section->{'subtotal'} )
2850       if ( $invoice_data{finance_section} &&
2851            $section->{'description'} eq $invoice_data{finance_section} );
2852
2853     $section->{'subtotal'} = $other_money_char.
2854                              sprintf('%.2f', $section->{'subtotal'})
2855       if $multisection;
2856
2857     # continue some normalization
2858     $section->{'amount'}   = $section->{'subtotal'}
2859       if $multisection;
2860
2861
2862     if ( $section->{'description'} ) {
2863       push @buf, ( [ &$escape_function($section->{'description'}), '' ],
2864                    [ '', '' ],
2865                  );
2866     }
2867
2868     warn "$me   setting options\n"
2869       if $DEBUG > 1;
2870
2871     my $multilocation = scalar($cust_main->cust_location); #too expensive?
2872     my %options = ();
2873     $options{'section'} = $section if $multisection;
2874     $options{'format'} = $format;
2875     $options{'escape_function'} = $escape_function;
2876     $options{'format_function'} = sub { () } unless $unsquelched;
2877     $options{'unsquelched'} = $unsquelched;
2878     $options{'summary_page'} = $summarypage;
2879     $options{'skip_usage'} =
2880       scalar(@$extra_sections) && !grep{$section == $_} @$extra_sections;
2881     $options{'multilocation'} = $multilocation;
2882     $options{'multisection'} = $multisection;
2883
2884     warn "$me   searching for line items\n"
2885       if $DEBUG > 1;
2886
2887     foreach my $line_item ( $self->_items_pkg(%options) ) {
2888
2889       warn "$me     adding line item $line_item\n"
2890         if $DEBUG > 1;
2891
2892       my $detail = {
2893         ext_description => [],
2894       };
2895       $detail->{'ref'} = $line_item->{'pkgnum'};
2896       $detail->{'quantity'} = $line_item->{'quantity'};
2897       $detail->{'section'} = $section;
2898       $detail->{'description'} = &$escape_function($line_item->{'description'});
2899       if ( exists $line_item->{'ext_description'} ) {
2900         @{$detail->{'ext_description'}} = @{$line_item->{'ext_description'}};
2901       }
2902       $detail->{'amount'} = ( $old_latex ? '' : $money_char ).
2903                               $line_item->{'amount'};
2904       $detail->{'unit_amount'} = ( $old_latex ? '' : $money_char ).
2905                                  $line_item->{'unit_amount'};
2906       $detail->{'product_code'} = $line_item->{'pkgpart'} || 'N/A';
2907   
2908       push @detail_items, $detail;
2909       push @buf, ( [ $detail->{'description'},
2910                      $money_char. sprintf("%10.2f", $line_item->{'amount'}),
2911                    ],
2912                    map { [ " ". $_, '' ] } @{$detail->{'ext_description'}},
2913                  );
2914     }
2915
2916     if ( $section->{'description'} ) {
2917       push @buf, ( ['','-----------'],
2918                    [ $section->{'description'}. ' sub-total',
2919                       $money_char. sprintf("%10.2f", $section->{'subtotal'})
2920                    ],
2921                    [ '', '' ],
2922                    [ '', '' ],
2923                  );
2924     }
2925   
2926   }
2927   
2928   $invoice_data{current_less_finance} =
2929     sprintf('%.2f', $self->charged - $invoice_data{finance_amount} );
2930
2931   if ( $multisection && !$conf->exists('disable_previous_balance')
2932     || $conf->exists('previous_balance-summary_only') )
2933   {
2934     unshift @sections, $previous_section if $pr_total;
2935   }
2936
2937   warn "$me adding taxes\n"
2938     if $DEBUG > 1;
2939
2940   foreach my $tax ( $self->_items_tax ) {
2941
2942     $taxtotal += $tax->{'amount'};
2943
2944     my $description = &$escape_function( $tax->{'description'} );
2945     my $amount      = sprintf( '%.2f', $tax->{'amount'} );
2946
2947     if ( $multisection ) {
2948
2949       my $money = $old_latex ? '' : $money_char;
2950       push @detail_items, {
2951         ext_description => [],
2952         ref          => '',
2953         quantity     => '',
2954         description  => $description,
2955         amount       => $money. $amount,
2956         product_code => '',
2957         section      => $tax_section,
2958       };
2959
2960     } else {
2961
2962       push @total_items, {
2963         'total_item'   => $description,
2964         'total_amount' => $other_money_char. $amount,
2965       };
2966
2967     }
2968
2969     push @buf,[ $description,
2970                 $money_char. $amount,
2971               ];
2972
2973   }
2974   
2975   if ( $taxtotal ) {
2976     my $total = {};
2977     $total->{'total_item'} = 'Sub-total';
2978     $total->{'total_amount'} =
2979       $other_money_char. sprintf('%.2f', $self->charged - $taxtotal );
2980
2981     if ( $multisection ) {
2982       $tax_section->{'subtotal'} = $other_money_char.
2983                                    sprintf('%.2f', $taxtotal);
2984       $tax_section->{'pretotal'} = 'New charges sub-total '.
2985                                    $total->{'total_amount'};
2986       push @sections, $tax_section if $taxtotal;
2987     }else{
2988       unshift @total_items, $total;
2989     }
2990   }
2991   $invoice_data{'taxtotal'} = sprintf('%.2f', $taxtotal);
2992
2993   push @buf,['','-----------'];
2994   push @buf,[( $conf->exists('disable_previous_balance') 
2995                ? 'Total Charges'
2996                : 'Total New Charges'
2997              ),
2998              $money_char. sprintf("%10.2f",$self->charged) ];
2999   push @buf,['',''];
3000
3001   {
3002     my $total = {};
3003     my $item = 'Total';
3004     $item = $conf->config('previous_balance-exclude_from_total')
3005          || 'Total New Charges'
3006       if $conf->exists('previous_balance-exclude_from_total');
3007     my $amount = $self->charged +
3008                    ( $conf->exists('disable_previous_balance') ||
3009                      $conf->exists('previous_balance-exclude_from_total')
3010                      ? 0
3011                      : $pr_total
3012                    );
3013     $total->{'total_item'} = &$embolden_function($item);
3014     $total->{'total_amount'} =
3015       &$embolden_function( $other_money_char.  sprintf( '%.2f', $amount ) );
3016     if ( $multisection ) {
3017       if ( $adjust_section->{'sort_weight'} ) {
3018         $adjust_section->{'posttotal'} = 'Balance Forward '. $other_money_char.
3019           sprintf("%.2f", ($self->billing_balance || 0) );
3020       } else {
3021         $adjust_section->{'pretotal'} = 'New charges total '. $other_money_char.
3022                                         sprintf('%.2f', $self->charged );
3023       } 
3024     }else{
3025       push @total_items, $total;
3026     }
3027     push @buf,['','-----------'];
3028     push @buf,[$item,
3029                $money_char.
3030                sprintf( '%10.2f', $amount )
3031               ];
3032     push @buf,['',''];
3033   }
3034   
3035   unless ( $conf->exists('disable_previous_balance') ) {
3036     #foreach my $thing ( sort { $a->_date <=> $b->_date } $self->_items_credits, $self->_items_payments
3037   
3038     # credits
3039     my $credittotal = 0;
3040     foreach my $credit ( $self->_items_credits('trim_len'=>60) ) {
3041
3042       my $total;
3043       $total->{'total_item'} = &$escape_function($credit->{'description'});
3044       $credittotal += $credit->{'amount'};
3045       $total->{'total_amount'} = '-'. $other_money_char. $credit->{'amount'};
3046       $adjusttotal += $credit->{'amount'};
3047       if ( $multisection ) {
3048         my $money = $old_latex ? '' : $money_char;
3049         push @detail_items, {
3050           ext_description => [],
3051           ref          => '',
3052           quantity     => '',
3053           description  => &$escape_function($credit->{'description'}),
3054           amount       => $money. $credit->{'amount'},
3055           product_code => '',
3056           section      => $adjust_section,
3057         };
3058       } else {
3059         push @total_items, $total;
3060       }
3061
3062     }
3063     $invoice_data{'credittotal'} = sprintf('%.2f', $credittotal);
3064
3065     #credits (again)
3066     foreach my $credit ( $self->_items_credits('trim_len'=>32) ) {
3067       push @buf, [ $credit->{'description'}, $money_char.$credit->{'amount'} ];
3068     }
3069
3070     # payments
3071     my $paymenttotal = 0;
3072     foreach my $payment ( $self->_items_payments ) {
3073       my $total = {};
3074       $total->{'total_item'} = &$escape_function($payment->{'description'});
3075       $paymenttotal += $payment->{'amount'};
3076       $total->{'total_amount'} = '-'. $other_money_char. $payment->{'amount'};
3077       $adjusttotal += $payment->{'amount'};
3078       if ( $multisection ) {
3079         my $money = $old_latex ? '' : $money_char;
3080         push @detail_items, {
3081           ext_description => [],
3082           ref          => '',
3083           quantity     => '',
3084           description  => &$escape_function($payment->{'description'}),
3085           amount       => $money. $payment->{'amount'},
3086           product_code => '',
3087           section      => $adjust_section,
3088         };
3089       }else{
3090         push @total_items, $total;
3091       }
3092       push @buf, [ $payment->{'description'},
3093                    $money_char. sprintf("%10.2f", $payment->{'amount'}),
3094                  ];
3095     }
3096     $invoice_data{'paymenttotal'} = sprintf('%.2f', $paymenttotal);
3097   
3098     if ( $multisection ) {
3099       $adjust_section->{'subtotal'} = $other_money_char.
3100                                       sprintf('%.2f', $adjusttotal);
3101       push @sections, $adjust_section
3102         unless $adjust_section->{sort_weight};
3103     }
3104
3105     { 
3106       my $total;
3107       $total->{'total_item'} = &$embolden_function($self->balance_due_msg);
3108       $total->{'total_amount'} =
3109         &$embolden_function(
3110           $other_money_char. sprintf('%.2f', $summarypage 
3111                                                ? $self->charged +
3112                                                  $self->billing_balance
3113                                                : $self->owed + $pr_total
3114                                     )
3115         );
3116       if ( $multisection && !$adjust_section->{sort_weight} ) {
3117         $adjust_section->{'posttotal'} = $total->{'total_item'}. ' '.
3118                                          $total->{'total_amount'};
3119       }else{
3120         push @total_items, $total;
3121       }
3122       push @buf,['','-----------'];
3123       push @buf,[$self->balance_due_msg, $money_char. 
3124         sprintf("%10.2f", $balance_due ) ];
3125     }
3126
3127     if ( $conf->exists('previous_balance-show_credit')
3128         and $cust_main->balance < 0 ) {
3129       my $credit_total = {
3130         'total_item'    => &$embolden_function($self->credit_balance_msg),
3131         'total_amount'  => &$embolden_function(
3132           $other_money_char. sprintf('%.2f', -$cust_main->balance)
3133         ),
3134       };
3135       if ( $multisection ) {
3136         $adjust_section->{'posttotal'} .= $newline_token .
3137           $credit_total->{'total_item'} . ' ' . $credit_total->{'total_amount'};
3138       }
3139       else {
3140         push @total_items, $credit_total;
3141       }
3142       push @buf,['','-----------'];
3143       push @buf,[$self->credit_balance_msg, $money_char. 
3144         sprintf("%10.2f", -$cust_main->balance ) ];
3145     }
3146   }
3147
3148   if ( $multisection ) {
3149     if ($conf->exists('svc_phone_sections')) {
3150       my $total;
3151       $total->{'total_item'} = &$embolden_function($self->balance_due_msg);
3152       $total->{'total_amount'} =
3153         &$embolden_function(
3154           $other_money_char. sprintf('%.2f', $self->owed + $pr_total)
3155         );
3156       my $last_section = pop @sections;
3157       $last_section->{'posttotal'} = $total->{'total_item'}. ' '.
3158                                      $total->{'total_amount'};
3159       push @sections, $last_section;
3160     }
3161     push @sections, @$late_sections
3162       if $unsquelched;
3163   }
3164
3165   my @includelist = ();
3166   push @includelist, 'summary' if $summarypage;
3167   foreach my $include ( @includelist ) {
3168
3169     my $inc_file = $conf->key_orbase("invoice_${format}$include", $template);
3170     my @inc_src;
3171
3172     if ( length( $conf->config($inc_file, $agentnum) ) ) {
3173
3174       @inc_src = $conf->config($inc_file, $agentnum);
3175
3176     } else {
3177
3178       $inc_file = $conf->key_orbase("invoice_latex$include", $template);
3179
3180       my $convert_map = $convert_maps{$format}{$include};
3181
3182       @inc_src = map { s/\[\@--/$delimiters{$format}[0]/g;
3183                        s/--\@\]/$delimiters{$format}[1]/g;
3184                        $_;
3185                      } 
3186                  &$convert_map( $conf->config($inc_file, $agentnum) );
3187
3188     }
3189
3190     my $inc_tt = new Text::Template (
3191       TYPE       => 'ARRAY',
3192       SOURCE     => [ map "$_\n", @inc_src ],
3193       DELIMITERS => $delimiters{$format},
3194     ) or die "Can't create new Text::Template object: $Text::Template::ERROR";
3195
3196     unless ( $inc_tt->compile() ) {
3197       my $error = "Can't compile $inc_file template: $Text::Template::ERROR\n";
3198       warn $error. "Template:\n". join('', map "$_\n", @inc_src);
3199       die $error;
3200     }
3201
3202     $invoice_data{$include} = $inc_tt->fill_in( HASH => \%invoice_data );
3203
3204     $invoice_data{$include} =~ s/\n+$//
3205       if ($format eq 'latex');
3206   }
3207
3208   $invoice_lines = 0;
3209   my $wasfunc = 0;
3210   foreach ( grep /invoice_lines\(\d*\)/, @invoice_template ) { #kludgy
3211     /invoice_lines\((\d*)\)/;
3212     $invoice_lines += $1 || scalar(@buf);
3213     $wasfunc=1;
3214   }
3215   die "no invoice_lines() functions in template?"
3216     if ( $format eq 'template' && !$wasfunc );
3217
3218   if ($format eq 'template') {
3219
3220     if ( $invoice_lines ) {
3221       $invoice_data{'total_pages'} = int( scalar(@buf) / $invoice_lines );
3222       $invoice_data{'total_pages'}++
3223         if scalar(@buf) % $invoice_lines;
3224     }
3225
3226     #setup subroutine for the template
3227     sub FS::cust_bill::_template::invoice_lines {
3228       my $lines = shift || scalar(@FS::cust_bill::_template::buf);
3229       map { 
3230         scalar(@FS::cust_bill::_template::buf)
3231           ? shift @FS::cust_bill::_template::buf
3232           : [ '', '' ];
3233       }
3234       ( 1 .. $lines );
3235     }
3236
3237     my $lines;
3238     my @collect;
3239     while (@buf) {
3240       push @collect, split("\n",
3241         $text_template->fill_in( HASH => \%invoice_data,
3242                                  PACKAGE => 'FS::cust_bill::_template'
3243                                )
3244       );
3245       $FS::cust_bill::_template::page++;
3246     }
3247     map "$_\n", @collect;
3248   }else{
3249     warn "filling in template for invoice ". $self->invnum. "\n"
3250       if $DEBUG;
3251     warn join("\n", map " $_ => ". $invoice_data{$_}, keys %invoice_data). "\n"
3252       if $DEBUG > 1;
3253
3254     $text_template->fill_in(HASH => \%invoice_data);
3255   }
3256 }
3257
3258 # helper routine for generating date ranges
3259 sub _prior_month30s {
3260   my $self = shift;
3261   my @ranges = (
3262    [ 1,       2592000 ], # 0-30 days ago
3263    [ 2592000, 5184000 ], # 30-60 days ago
3264    [ 5184000, 7776000 ], # 60-90 days ago
3265    [ 7776000, 0       ], # 90+   days ago
3266   );
3267
3268   map { [ $_->[0] ? $self->_date - $_->[0] - 1 : '',
3269           $_->[1] ? $self->_date - $_->[1] - 1 : '',
3270       ] }
3271   @ranges;
3272 }
3273
3274 =item print_ps HASHREF | [ TIME [ , TEMPLATE ] ]
3275
3276 Returns an postscript invoice, as a scalar.
3277
3278 Options can be passed as a hashref (recommended) or as a list of time, template
3279 and then any key/value pairs for any other options.
3280
3281 I<time> an optional value used to control the printing of overdue messages.  The
3282 default is now.  It isn't the date of the invoice; that's the `_date' field.
3283 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
3284 L<Time::Local> and L<Date::Parse> for conversion functions.
3285
3286 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
3287
3288 =cut
3289
3290 sub print_ps {
3291   my $self = shift;
3292
3293   my ($file, $logofile, $barcodefile) = $self->print_latex(@_);
3294   my $ps = generate_ps($file);
3295   unlink($logofile);
3296   unlink($barcodefile);
3297
3298   $ps;
3299 }
3300
3301 =item print_pdf HASHREF | [ TIME [ , TEMPLATE ] ]
3302
3303 Returns an PDF invoice, as a scalar.
3304
3305 Options can be passed as a hashref (recommended) or as a list of time, template
3306 and then any key/value pairs for any other options.
3307
3308 I<time> an optional value used to control the printing of overdue messages.  The
3309 default is now.  It isn't the date of the invoice; that's the `_date' field.
3310 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
3311 L<Time::Local> and L<Date::Parse> for conversion functions.
3312
3313 I<template>, if specified, is the name of a suffix for alternate invoices.
3314
3315 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
3316
3317 =cut
3318
3319 sub print_pdf {
3320   my $self = shift;
3321
3322   my ($file, $logofile, $barcodefile) = $self->print_latex(@_);
3323   my $pdf = generate_pdf($file);
3324   unlink($logofile);
3325   unlink($barcodefile);
3326
3327   $pdf;
3328 }
3329
3330 =item print_html HASHREF | [ TIME [ , TEMPLATE [ , CID ] ] ]
3331
3332 Returns an HTML invoice, as a scalar.
3333
3334 I<time> an optional value used to control the printing of overdue messages.  The
3335 default is now.  It isn't the date of the invoice; that's the `_date' field.
3336 It is specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
3337 L<Time::Local> and L<Date::Parse> for conversion functions.
3338
3339 I<template>, if specified, is the name of a suffix for alternate invoices.
3340
3341 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
3342
3343 I<cid> is a MIME Content-ID used to create a "cid:" URL for the logo image, used
3344 when emailing the invoice as part of a multipart/related MIME email.
3345
3346 =cut
3347
3348 sub print_html {
3349   my $self = shift;
3350   my %params;
3351   if ( ref($_[0]) ) {
3352     %params = %{ shift() }; 
3353   }else{
3354     $params{'time'} = shift;
3355     $params{'template'} = shift;
3356     $params{'cid'} = shift;
3357   }
3358
3359   $params{'format'} = 'html';
3360   
3361   $self->print_generic( %params );
3362 }
3363
3364 # quick subroutine for print_latex
3365 #
3366 # There are ten characters that LaTeX treats as special characters, which
3367 # means that they do not simply typeset themselves: 
3368 #      # $ % & ~ _ ^ \ { }
3369 #
3370 # TeX ignores blanks following an escaped character; if you want a blank (as
3371 # in "10% of ..."), you have to "escape" the blank as well ("10\%\ of ..."). 
3372
3373 sub _latex_escape {
3374   my $value = shift;
3375   $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
3376   $value =~ s/([<>])/\$$1\$/g;
3377   $value;
3378 }
3379
3380 sub _html_escape {
3381   my $value = shift;
3382   encode_entities($value);
3383   $value;
3384 }
3385
3386 sub _html_escape_nbsp {
3387   my $value = _html_escape(shift);
3388   $value =~ s/ +/&nbsp;/g;
3389   $value;
3390 }
3391
3392 #utility methods for print_*
3393
3394 sub _translate_old_latex_format {
3395   warn "_translate_old_latex_format called\n"
3396     if $DEBUG; 
3397
3398   my @template = ();
3399   while ( @_ ) {
3400     my $line = shift;
3401   
3402     if ( $line =~ /^%%Detail\s*$/ ) {
3403   
3404       push @template, q![@--!,
3405                       q!  foreach my $_tr_line (@detail_items) {!,
3406                       q!    if ( scalar ($_tr_item->{'ext_description'} ) ) {!,
3407                       q!      $_tr_line->{'description'} .= !, 
3408                       q!        "\\tabularnewline\n~~".!,
3409                       q!        join( "\\tabularnewline\n~~",!,
3410                       q!          @{$_tr_line->{'ext_description'}}!,
3411                       q!        );!,
3412                       q!    }!;
3413
3414       while ( ( my $line_item_line = shift )
3415               !~ /^%%EndDetail\s*$/                            ) {
3416         $line_item_line =~ s/'/\\'/g;    # nice LTS
3417         $line_item_line =~ s/\\/\\\\/g;  # escape quotes and backslashes
3418         $line_item_line =~ s/\$(\w+)/'. \$_tr_line->{$1}. '/g;
3419         push @template, "    \$OUT .= '$line_item_line';";
3420       }
3421
3422       push @template, '}',
3423                       '--@]';
3424       #' doh, gvim
3425     } elsif ( $line =~ /^%%TotalDetails\s*$/ ) {
3426
3427       push @template, '[@--',
3428                       '  foreach my $_tr_line (@total_items) {';
3429
3430       while ( ( my $total_item_line = shift )
3431               !~ /^%%EndTotalDetails\s*$/                      ) {
3432         $total_item_line =~ s/'/\\'/g;    # nice LTS
3433         $total_item_line =~ s/\\/\\\\/g;  # escape quotes and backslashes
3434         $total_item_line =~ s/\$(\w+)/'. \$_tr_line->{$1}. '/g;
3435         push @template, "    \$OUT .= '$total_item_line';";
3436       }
3437
3438       push @template, '}',
3439                       '--@]';
3440
3441     } else {
3442       $line =~ s/\$(\w+)/[\@-- \$$1 --\@]/g;
3443       push @template, $line;  
3444     }
3445   
3446   }
3447
3448   if ($DEBUG) {
3449     warn "$_\n" foreach @template;
3450   }
3451
3452   (@template);
3453 }
3454
3455 sub terms {
3456   my $self = shift;
3457
3458   #check for an invoice-specific override
3459   return $self->invoice_terms if $self->invoice_terms;
3460   
3461   #check for a customer- specific override
3462   my $cust_main = $self->cust_main;
3463   return $cust_main->invoice_terms if $cust_main->invoice_terms;
3464
3465   #use configured default
3466   $conf->config('invoice_default_terms') || '';
3467 }
3468
3469 sub due_date {
3470   my $self = shift;
3471   my $duedate = '';
3472   if ( $self->terms =~ /^\s*Net\s*(\d+)\s*$/ ) {
3473     $duedate = $self->_date() + ( $1 * 86400 );
3474   }
3475   $duedate;
3476 }
3477
3478 sub due_date2str {
3479   my $self = shift;
3480   $self->due_date ? time2str(shift, $self->due_date) : '';
3481 }
3482
3483 sub balance_due_msg {
3484   my $self = shift;
3485   my $msg = 'Balance Due';
3486   return $msg unless $self->terms;
3487   if ( $self->due_date ) {
3488     $msg .= ' - Please pay by '. $self->due_date2str($date_format);
3489   } elsif ( $self->terms ) {
3490     $msg .= ' - '. $self->terms;
3491   }
3492   $msg;
3493 }
3494
3495 sub balance_due_date {
3496   my $self = shift;
3497   my $duedate = '';
3498   if (    $conf->exists('invoice_default_terms') 
3499        && $conf->config('invoice_default_terms')=~ /^\s*Net\s*(\d+)\s*$/ ) {
3500     $duedate = time2str($rdate_format, $self->_date + ($1*86400) );
3501   }
3502   $duedate;
3503 }
3504
3505 sub credit_balance_msg { 'Credit Balance Remaining' }
3506
3507 =item invnum_date_pretty
3508
3509 Returns a string with the invoice number and date, for example:
3510 "Invoice #54 (3/20/2008)"
3511
3512 =cut
3513
3514 sub invnum_date_pretty {
3515   my $self = shift;
3516   'Invoice #'. $self->invnum. ' ('. $self->_date_pretty. ')';
3517 }
3518
3519 =item _date_pretty
3520
3521 Returns a string with the date, for example: "3/20/2008"
3522
3523 =cut
3524
3525 sub _date_pretty {
3526   my $self = shift;
3527   time2str($date_format, $self->_date);
3528 }
3529
3530 use vars qw(%pkg_category_cache);
3531 sub _items_sections {
3532   my $self = shift;
3533   my $late = shift;
3534   my $summarypage = shift;
3535   my $escape = shift;
3536   my $extra_sections = shift;
3537   my $format = shift;
3538
3539   my %subtotal = ();
3540   my %late_subtotal = ();
3541   my %not_tax = ();
3542
3543   foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
3544   {
3545
3546       my $usage = $cust_bill_pkg->usage;
3547
3548       foreach my $display ($cust_bill_pkg->cust_bill_pkg_display) {
3549         next if ( $display->summary && $summarypage );
3550
3551         my $section = $display->section;
3552         my $type    = $display->type;
3553
3554         $not_tax{$section} = 1
3555           unless $cust_bill_pkg->pkgnum == 0;
3556
3557         if ( $display->post_total && !$summarypage ) {
3558           if (! $type || $type eq 'S') {
3559             $late_subtotal{$section} += $cust_bill_pkg->setup
3560               if $cust_bill_pkg->setup != 0;
3561           }
3562
3563           if (! $type) {
3564             $late_subtotal{$section} += $cust_bill_pkg->recur
3565               if $cust_bill_pkg->recur != 0;
3566           }
3567
3568           if ($type && $type eq 'R') {
3569             $late_subtotal{$section} += $cust_bill_pkg->recur - $usage
3570               if $cust_bill_pkg->recur != 0;
3571           }
3572           
3573           if ($type && $type eq 'U') {
3574             $late_subtotal{$section} += $usage
3575               unless scalar(@$extra_sections);
3576           }
3577
3578         } else {
3579
3580           next if $cust_bill_pkg->pkgnum == 0 && ! $section;
3581
3582           if (! $type || $type eq 'S') {
3583             $subtotal{$section} += $cust_bill_pkg->setup
3584               if $cust_bill_pkg->setup != 0;
3585           }
3586
3587           if (! $type) {
3588             $subtotal{$section} += $cust_bill_pkg->recur
3589               if $cust_bill_pkg->recur != 0;
3590           }
3591
3592           if ($type && $type eq 'R') {
3593             $subtotal{$section} += $cust_bill_pkg->recur - $usage
3594               if $cust_bill_pkg->recur != 0;
3595           }
3596           
3597           if ($type && $type eq 'U') {
3598             $subtotal{$section} += $usage
3599               unless scalar(@$extra_sections);
3600           }
3601
3602         }
3603
3604       }
3605
3606   }
3607
3608   %pkg_category_cache = ();
3609
3610   push @$late, map { { 'description' => &{$escape}($_),
3611                        'subtotal'    => $late_subtotal{$_},
3612                        'post_total'  => 1,
3613                        'sort_weight' => ( _pkg_category($_)
3614                                             ? _pkg_category($_)->weight
3615                                             : 0
3616                                        ),
3617                        ((_pkg_category($_) && _pkg_category($_)->condense)
3618                                            ? $self->_condense_section($format)
3619                                            : ()
3620                        ),
3621                    } }
3622                  sort _sectionsort keys %late_subtotal;
3623
3624   my @sections;
3625   if ( $summarypage ) {
3626     @sections = grep { exists($subtotal{$_}) || ! _pkg_category($_)->disabled }
3627                 map { $_->categoryname } qsearch('pkg_category', {});
3628     push @sections, '' if exists($subtotal{''});
3629   } else {
3630     @sections = keys %subtotal;
3631   }
3632
3633   my @early = map { { 'description' => &{$escape}($_),
3634                       'subtotal'    => $subtotal{$_},
3635                       'summarized'  => $not_tax{$_} ? '' : 'Y',
3636                       'tax_section' => $not_tax{$_} ? '' : 'Y',
3637                       'sort_weight' => ( _pkg_category($_)
3638                                            ? _pkg_category($_)->weight
3639                                            : 0
3640                                        ),
3641                        ((_pkg_category($_) && _pkg_category($_)->condense)
3642                                            ? $self->_condense_section($format)
3643                                            : ()
3644                        ),
3645                     }
3646                   } @sections;
3647   push @early, @$extra_sections if $extra_sections;
3648  
3649   sort { $a->{sort_weight} <=> $b->{sort_weight} } @early;
3650
3651 }
3652
3653 #helper subs for above
3654
3655 sub _sectionsort {
3656   _pkg_category($a)->weight <=> _pkg_category($b)->weight;
3657 }
3658
3659 sub _pkg_category {
3660   my $categoryname = shift;
3661   $pkg_category_cache{$categoryname} ||=
3662     qsearchs( 'pkg_category', { 'categoryname' => $categoryname } );
3663 }
3664
3665 my %condensed_format = (
3666   'label' => [ qw( Description Qty Amount ) ],
3667   'fields' => [
3668                 sub { shift->{description} },
3669                 sub { shift->{quantity} },
3670                 sub { my($href, %opt) = @_;
3671                       ($opt{dollar} || ''). $href->{amount};
3672                     },
3673               ],
3674   'align'  => [ qw( l r r ) ],
3675   'span'   => [ qw( 5 1 1 ) ],            # unitprices?
3676   'width'  => [ qw( 10.7cm 1.4cm 1.6cm ) ],   # don't like this
3677 );
3678
3679 sub _condense_section {
3680   my ( $self, $format ) = ( shift, shift );
3681   ( 'condensed' => 1,
3682     map { my $method = "_condensed_$_"; $_ => $self->$method($format) }
3683       qw( description_generator
3684           header_generator
3685           total_generator
3686           total_line_generator
3687         )
3688   );
3689 }
3690
3691 sub _condensed_generator_defaults {
3692   my ( $self, $format ) = ( shift, shift );
3693   return ( \%condensed_format, ' ', ' ', ' ', sub { shift } );
3694 }
3695
3696 my %html_align = (
3697   'c' => 'center',
3698   'l' => 'left',
3699   'r' => 'right',
3700 );
3701
3702 sub _condensed_header_generator {
3703   my ( $self, $format ) = ( shift, shift );
3704
3705   my ( $f, $prefix, $suffix, $separator, $column ) =
3706     _condensed_generator_defaults($format);
3707
3708   if ($format eq 'latex') {
3709     $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
3710     $suffix = "\\\\\n\\hline";
3711     $separator = "&\n";
3712     $column =
3713       sub { my ($d,$a,$s,$w) = @_;
3714             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
3715           };
3716   } elsif ( $format eq 'html' ) {
3717     $prefix = '<th></th>';
3718     $suffix = '';
3719     $separator = '';
3720     $column =
3721       sub { my ($d,$a,$s,$w) = @_;
3722             return qq!<th align="$html_align{$a}">$d</th>!;
3723       };
3724   }
3725
3726   sub {
3727     my @args = @_;
3728     my @result = ();
3729
3730     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
3731       push @result,
3732         &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
3733     }
3734
3735     $prefix. join($separator, @result). $suffix;
3736   };
3737
3738 }
3739
3740 sub _condensed_description_generator {
3741   my ( $self, $format ) = ( shift, shift );
3742
3743   my ( $f, $prefix, $suffix, $separator, $column ) =
3744     _condensed_generator_defaults($format);
3745
3746   my $money_char = '$';
3747   if ($format eq 'latex') {
3748     $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
3749     $suffix = '\\\\';
3750     $separator = " & \n";
3751     $column =
3752       sub { my ($d,$a,$s,$w) = @_;
3753             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
3754           };
3755     $money_char = '\\dollar';
3756   }elsif ( $format eq 'html' ) {
3757     $prefix = '"><td align="center"></td>';
3758     $suffix = '';
3759     $separator = '';
3760     $column =
3761       sub { my ($d,$a,$s,$w) = @_;
3762             return qq!<td align="$html_align{$a}">$d</td>!;
3763       };
3764     #$money_char = $conf->config('money_char') || '$';
3765     $money_char = '';  # this is madness
3766   }
3767
3768   sub {
3769     #my @args = @_;
3770     my $href = shift;
3771     my @result = ();
3772
3773     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
3774       my $dollar = '';
3775       $dollar = $money_char if $i == scalar(@{$f->{label}})-1;
3776       push @result,
3777         &{$column}( &{$f->{fields}->[$i]}($href, 'dollar' => $dollar),
3778                     map { $f->{$_}->[$i] } qw(align span width)
3779                   );
3780     }
3781
3782     $prefix. join( $separator, @result ). $suffix;
3783   };
3784
3785 }
3786
3787 sub _condensed_total_generator {
3788   my ( $self, $format ) = ( shift, shift );
3789
3790   my ( $f, $prefix, $suffix, $separator, $column ) =
3791     _condensed_generator_defaults($format);
3792   my $style = '';
3793
3794   if ($format eq 'latex') {
3795     $prefix = "& ";
3796     $suffix = "\\\\\n";
3797     $separator = " & \n";
3798     $column =
3799       sub { my ($d,$a,$s,$w) = @_;
3800             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
3801           };
3802   }elsif ( $format eq 'html' ) {
3803     $prefix = '';
3804     $suffix = '';
3805     $separator = '';
3806     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
3807     $column =
3808       sub { my ($d,$a,$s,$w) = @_;
3809             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
3810       };
3811   }
3812
3813
3814   sub {
3815     my @args = @_;
3816     my @result = ();
3817
3818     #  my $r = &{$f->{fields}->[$i]}(@args);
3819     #  $r .= ' Total' unless $i;
3820
3821     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
3822       push @result,
3823         &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
3824                     map { $f->{$_}->[$i] } qw(align span width)
3825                   );
3826     }
3827
3828     $prefix. join( $separator, @result ). $suffix;
3829   };
3830
3831 }
3832
3833 =item total_line_generator FORMAT
3834
3835 Returns a coderef used for generation of invoice total line items for this
3836 usage_class.  FORMAT is either html or latex
3837
3838 =cut
3839
3840 # should not be used: will have issues with hash element names (description vs
3841 # total_item and amount vs total_amount -- another array of functions?
3842
3843 sub _condensed_total_line_generator {
3844   my ( $self, $format ) = ( shift, shift );
3845
3846   my ( $f, $prefix, $suffix, $separator, $column ) =
3847     _condensed_generator_defaults($format);
3848   my $style = '';
3849
3850   if ($format eq 'latex') {
3851     $prefix = "& ";
3852     $suffix = "\\\\\n";
3853     $separator = " & \n";
3854     $column =
3855       sub { my ($d,$a,$s,$w) = @_;
3856             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
3857           };
3858   }elsif ( $format eq 'html' ) {
3859     $prefix = '';
3860     $suffix = '';
3861     $separator = '';
3862     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
3863     $column =
3864       sub { my ($d,$a,$s,$w) = @_;
3865             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
3866       };
3867   }
3868
3869
3870   sub {
3871     my @args = @_;
3872     my @result = ();
3873
3874     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
3875       push @result,
3876         &{$column}( &{$f->{fields}->[$i]}(@args),
3877                     map { $f->{$_}->[$i] } qw(align span width)
3878                   );
3879     }
3880
3881     $prefix. join( $separator, @result ). $suffix;
3882   };
3883
3884 }
3885
3886 #sub _items_extra_usage_sections {
3887 #  my $self = shift;
3888 #  my $escape = shift;
3889 #
3890 #  my %sections = ();
3891 #
3892 #  my %usage_class =  map{ $_->classname, $_ } qsearch('usage_class', {});
3893 #  foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
3894 #  {
3895 #    next unless $cust_bill_pkg->pkgnum > 0;
3896 #
3897 #    foreach my $section ( keys %usage_class ) {
3898 #
3899 #      my $usage = $cust_bill_pkg->usage($section);
3900 #
3901 #      next unless $usage && $usage > 0;
3902 #
3903 #      $sections{$section} ||= 0;
3904 #      $sections{$section} += $usage;
3905 #
3906 #    }
3907 #
3908 #  }
3909 #
3910 #  map { { 'description' => &{$escape}($_),
3911 #          'subtotal'    => $sections{$_},
3912 #          'summarized'  => '',
3913 #          'tax_section' => '',
3914 #        }
3915 #      }
3916 #    sort {$usage_class{$a}->weight <=> $usage_class{$b}->weight} keys %sections;
3917 #
3918 #}
3919
3920 sub _items_extra_usage_sections {
3921   my $self = shift;
3922   my $escape = shift;
3923   my $format = shift;
3924
3925   my %sections = ();
3926   my %classnums = ();
3927   my %lines = ();
3928
3929   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
3930   foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
3931     next unless $cust_bill_pkg->pkgnum > 0;
3932
3933     foreach my $classnum ( keys %usage_class ) {
3934       my $section = $usage_class{$classnum}->classname;
3935       $classnums{$section} = $classnum;
3936
3937       foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail($classnum) ) {
3938         my $amount = $detail->amount;
3939         next unless $amount && $amount > 0;
3940  
3941         $sections{$section} ||= { 'subtotal'=>0, 'calls'=>0, 'duration'=>0 };
3942         $sections{$section}{amount} += $amount;  #subtotal
3943         $sections{$section}{calls}++;
3944         $sections{$section}{duration} += $detail->duration;
3945
3946         my $desc = $detail->regionname; 
3947         my $description = $desc;
3948         $description = substr($desc, 0, 50). '...'
3949           if $format eq 'latex' && length($desc) > 50;
3950
3951         $lines{$section}{$desc} ||= {
3952           description     => &{$escape}($description),
3953           #pkgpart         => $part_pkg->pkgpart,
3954           pkgnum          => $cust_bill_pkg->pkgnum,
3955           ref             => '',
3956           amount          => 0,
3957           calls           => 0,
3958           duration        => 0,
3959           #unit_amount     => $cust_bill_pkg->unitrecur,
3960           quantity        => $cust_bill_pkg->quantity,
3961           product_code    => 'N/A',
3962           ext_description => [],
3963         };
3964
3965         $lines{$section}{$desc}{amount} += $amount;
3966         $lines{$section}{$desc}{calls}++;
3967         $lines{$section}{$desc}{duration} += $detail->duration;
3968
3969       }
3970     }
3971   }
3972
3973   my %sectionmap = ();
3974   foreach (keys %sections) {
3975     my $usage_class = $usage_class{$classnums{$_}};
3976     $sectionmap{$_} = { 'description' => &{$escape}($_),
3977                         'amount'    => $sections{$_}{amount},    #subtotal
3978                         'calls'       => $sections{$_}{calls},
3979                         'duration'    => $sections{$_}{duration},
3980                         'summarized'  => '',
3981                         'tax_section' => '',
3982                         'sort_weight' => $usage_class->weight,
3983                         ( $usage_class->format
3984                           ? ( map { $_ => $usage_class->$_($format) }
3985                               qw( description_generator header_generator total_generator total_line_generator )
3986                             )
3987                           : ()
3988                         ), 
3989                       };
3990   }
3991
3992   my @sections = sort { $a->{sort_weight} <=> $b->{sort_weight} }
3993                  values %sectionmap;
3994
3995   my @lines = ();
3996   foreach my $section ( keys %lines ) {
3997     foreach my $line ( keys %{$lines{$section}} ) {
3998       my $l = $lines{$section}{$line};
3999       $l->{section}     = $sectionmap{$section};
4000       $l->{amount}      = sprintf( "%.2f", $l->{amount} );
4001       #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
4002       push @lines, $l;
4003     }
4004   }
4005
4006   return(\@sections, \@lines);
4007
4008 }
4009
4010 sub _did_summary {
4011     my $self = shift;
4012     my $end = $self->_date;
4013     my $start = $end - 2592000; # 30 days
4014     my $cust_main = $self->cust_main;
4015     my @pkgs = $cust_main->all_pkgs;
4016     my($num_activated,$num_deactivated,$num_portedin,$num_portedout,$minutes)
4017         = (0,0,0,0,0);
4018     my @seen = ();
4019     foreach my $pkg ( @pkgs ) {
4020         my @h_cust_svc = $pkg->h_cust_svc($end);
4021         foreach my $h_cust_svc ( @h_cust_svc ) {
4022             next if grep {$_ eq $h_cust_svc->svcnum} @seen;
4023             next unless $h_cust_svc->part_svc->svcdb eq 'svc_phone';
4024
4025             my $inserted = $h_cust_svc->date_inserted;
4026             my $deleted = $h_cust_svc->date_deleted;
4027             my $phone_inserted = $h_cust_svc->h_svc_x($inserted);
4028             my $phone_deleted;
4029             $phone_deleted =  $h_cust_svc->h_svc_x($deleted) if $deleted;
4030             
4031 # DID either activated or ported in; cannot be both for same DID simultaneously
4032             if ($inserted >= $start && $inserted <= $end && $phone_inserted
4033                 && (!$phone_inserted->lnp_status 
4034                     || $phone_inserted->lnp_status eq ''
4035                     || $phone_inserted->lnp_status eq 'native')) {
4036                 $num_activated++;
4037             }
4038             else { # this one not so clean, should probably move to (h_)svc_phone
4039                  my $phone_portedin = qsearchs( 'h_svc_phone',
4040                       { 'svcnum' => $h_cust_svc->svcnum, 
4041                         'lnp_status' => 'portedin' },  
4042                       FS::h_svc_phone->sql_h_searchs($end),  
4043                     );
4044                  $num_portedin++ if $phone_portedin;
4045             }
4046
4047 # DID either deactivated or ported out; cannot be both for same DID simultaneously
4048             if($deleted >= $start && $deleted <= $end && $phone_deleted
4049                 && (!$phone_deleted->lnp_status 
4050                     || $phone_deleted->lnp_status ne 'portingout')) {
4051                 $num_deactivated++;
4052             } 
4053             elsif($deleted >= $start && $deleted <= $end && $phone_deleted 
4054                 && $phone_deleted->lnp_status 
4055                 && $phone_deleted->lnp_status eq 'portingout') {
4056                 $num_portedout++;
4057             }
4058
4059             # increment usage minutes
4060             my @cdrs = $phone_inserted->get_cdrs('begin'=>$start,'end'=>$end);
4061             foreach my $cdr ( @cdrs ) {
4062                 $minutes += $cdr->billsec/60;
4063             }
4064
4065             # don't look at this service again
4066             push @seen, $h_cust_svc->svcnum;
4067         }
4068     }
4069
4070     $minutes = sprintf("%d", $minutes);
4071     ("Activated: $num_activated  Ported-In: $num_portedin  Deactivated: "
4072         . "$num_deactivated  Ported-Out: $num_portedout ",
4073             "Total Minutes: $minutes");
4074 }
4075
4076 sub _items_svc_phone_sections {
4077   my $self = shift;
4078   my $escape = shift;
4079   my $format = shift;
4080
4081   my %sections = ();
4082   my %classnums = ();
4083   my %lines = ();
4084
4085   my %usage_class =  map { $_->classnum => $_ } qsearch( 'usage_class', {} );
4086   $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 };
4087
4088   foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
4089     next unless $cust_bill_pkg->pkgnum > 0;
4090
4091     my @header = $cust_bill_pkg->details_header;
4092     next unless scalar(@header);
4093
4094     foreach my $detail ( $cust_bill_pkg->cust_bill_pkg_detail ) {
4095
4096       my $phonenum = $detail->phonenum;
4097       next unless $phonenum;
4098
4099       my $amount = $detail->amount;
4100       next unless $amount && $amount > 0;
4101
4102       $sections{$phonenum} ||= { 'amount'      => 0,
4103                                  'calls'       => 0,
4104                                  'duration'    => 0,
4105                                  'sort_weight' => -1,
4106                                  'phonenum'    => $phonenum,
4107                                 };
4108       $sections{$phonenum}{amount} += $amount;  #subtotal
4109       $sections{$phonenum}{calls}++;
4110       $sections{$phonenum}{duration} += $detail->duration;
4111
4112       my $desc = $detail->regionname; 
4113       my $description = $desc;
4114       $description = substr($desc, 0, 50). '...'
4115         if $format eq 'latex' && length($desc) > 50;
4116
4117       $lines{$phonenum}{$desc} ||= {
4118         description     => &{$escape}($description),
4119         #pkgpart         => $part_pkg->pkgpart,
4120         pkgnum          => '',
4121         ref             => '',
4122         amount          => 0,
4123         calls           => 0,
4124         duration        => 0,
4125         #unit_amount     => '',
4126         quantity        => '',
4127         product_code    => 'N/A',
4128         ext_description => [],
4129       };
4130
4131       $lines{$phonenum}{$desc}{amount} += $amount;
4132       $lines{$phonenum}{$desc}{calls}++;
4133       $lines{$phonenum}{$desc}{duration} += $detail->duration;
4134
4135       my $line = $usage_class{$detail->classnum}->classname;
4136       $sections{"$phonenum $line"} ||=
4137         { 'amount' => 0,
4138           'calls' => 0,
4139           'duration' => 0,
4140           'sort_weight' => $usage_class{$detail->classnum}->weight,
4141           'phonenum' => $phonenum,
4142           'header'  => [ @header ],
4143         };
4144       $sections{"$phonenum $line"}{amount} += $amount;  #subtotal
4145       $sections{"$phonenum $line"}{calls}++;
4146       $sections{"$phonenum $line"}{duration} += $detail->duration;
4147
4148       $lines{"$phonenum $line"}{$desc} ||= {
4149         description     => &{$escape}($description),
4150         #pkgpart         => $part_pkg->pkgpart,
4151         pkgnum          => '',
4152         ref             => '',
4153         amount          => 0,
4154         calls           => 0,
4155         duration        => 0,
4156         #unit_amount     => '',
4157         quantity        => '',
4158         product_code    => 'N/A',
4159         ext_description => [],
4160       };
4161
4162       $lines{"$phonenum $line"}{$desc}{amount} += $amount;
4163       $lines{"$phonenum $line"}{$desc}{calls}++;
4164       $lines{"$phonenum $line"}{$desc}{duration} += $detail->duration;
4165       push @{$lines{"$phonenum $line"}{$desc}{ext_description}},
4166            $detail->formatted('format' => $format);
4167
4168     }
4169   }
4170
4171   my %sectionmap = ();
4172   my $simple = new FS::usage_class { format => 'simple' }; #bleh
4173   foreach ( keys %sections ) {
4174     my @header = @{ $sections{$_}{header} || [] };
4175     my $usage_simple =
4176       new FS::usage_class { format => 'usage_'. (scalar(@header) || 6). 'col' };
4177     my $summary = $sections{$_}{sort_weight} < 0 ? 1 : 0;
4178     my $usage_class = $summary ? $simple : $usage_simple;
4179     my $ending = $summary ? ' usage charges' : '';
4180     my %gen_opt = ();
4181     unless ($summary) {
4182       $gen_opt{label} = [ map{ &{$escape}($_) } @header ];
4183     }
4184     $sectionmap{$_} = { 'description' => &{$escape}($_. $ending),
4185                         'amount'    => $sections{$_}{amount},    #subtotal
4186                         'calls'       => $sections{$_}{calls},
4187                         'duration'    => $sections{$_}{duration},
4188                         'summarized'  => '',
4189                         'tax_section' => '',
4190                         'phonenum'    => $sections{$_}{phonenum},
4191                         'sort_weight' => $sections{$_}{sort_weight},
4192                         'post_total'  => $summary, #inspire pagebreak
4193                         (
4194                           ( map { $_ => $usage_class->$_($format, %gen_opt) }
4195                             qw( description_generator
4196                                 header_generator
4197                                 total_generator
4198                                 total_line_generator
4199                               )
4200                           )
4201                         ), 
4202                       };
4203   }
4204
4205   my @sections = sort { $a->{phonenum} cmp $b->{phonenum} ||
4206                         $a->{sort_weight} <=> $b->{sort_weight}
4207                       }
4208                  values %sectionmap;
4209
4210   my @lines = ();
4211   foreach my $section ( keys %lines ) {
4212     foreach my $line ( keys %{$lines{$section}} ) {
4213       my $l = $lines{$section}{$line};
4214       $l->{section}     = $sectionmap{$section};
4215       $l->{amount}      = sprintf( "%.2f", $l->{amount} );
4216       #$l->{unit_amount} = sprintf( "%.2f", $l->{unit_amount} );
4217       push @lines, $l;
4218     }
4219   }
4220   
4221   if($conf->exists('phone_usage_class_summary')) { 
4222       # this only works with Latex
4223       my @newlines;
4224       my @newsections;
4225
4226       # after this, we'll have only two sections per DID:
4227       # Calls Summary and Calls Detail
4228       foreach my $section ( @sections ) {
4229         if($section->{'post_total'}) {
4230             $section->{'description'} = 'Calls Summary: '.$section->{'phonenum'};
4231             $section->{'total_line_generator'} = sub { '' };
4232             $section->{'total_generator'} = sub { '' };
4233             $section->{'header_generator'} = sub { '' };
4234             $section->{'description_generator'} = '';
4235             push @newsections, $section;
4236             my %calls_detail = %$section;
4237             $calls_detail{'post_total'} = '';
4238             $calls_detail{'sort_weight'} = '';
4239             $calls_detail{'description_generator'} = sub { '' };
4240             $calls_detail{'header_generator'} = sub {
4241                 return ' & Date/Time & Called Number & Duration & Price'
4242                     if $format eq 'latex';
4243                 '';
4244             };
4245             $calls_detail{'description'} = 'Calls Detail: '
4246                                                     . $section->{'phonenum'};
4247             push @newsections, \%calls_detail;  
4248         }
4249       }
4250
4251       # after this, each usage class is collapsed/summarized into a single
4252       # line under the Calls Summary section
4253       foreach my $newsection ( @newsections ) {
4254         if($newsection->{'post_total'}) { # this means Calls Summary
4255             foreach my $section ( @sections ) {
4256                 next unless ($section->{'phonenum'} eq $newsection->{'phonenum'} 
4257                                 && !$section->{'post_total'});
4258                 my $newdesc = $section->{'description'};
4259                 my $tn = $section->{'phonenum'};
4260                 $newdesc =~ s/$tn//g;
4261                 my $line = {  ext_description => [],
4262                               pkgnum => '',
4263                               ref => '',
4264                               quantity => '',
4265                               calls => $section->{'calls'},
4266                               section => $newsection,
4267                               duration => $section->{'duration'},
4268                               description => $newdesc,
4269                               amount => sprintf("%.2f",$section->{'amount'}),
4270                               product_code => 'N/A',
4271                             };
4272                 push @newlines, $line;
4273             }
4274         }
4275       }
4276
4277       # after this, Calls Details is populated with all CDRs
4278       foreach my $newsection ( @newsections ) {
4279         if(!$newsection->{'post_total'}) { # this means Calls Details
4280             foreach my $line ( @lines ) {
4281                 next unless (scalar(@{$line->{'ext_description'}}) &&
4282                         $line->{'section'}->{'phonenum'} eq $newsection->{'phonenum'}
4283                             );
4284                 my @extdesc = @{$line->{'ext_description'}};
4285                 my @newextdesc;
4286                 foreach my $extdesc ( @extdesc ) {
4287                     $extdesc =~ s/scriptsize/normalsize/g if $format eq 'latex';
4288                     push @newextdesc, $extdesc;
4289                 }
4290                 $line->{'ext_description'} = \@newextdesc;
4291                 $line->{'section'} = $newsection;
4292                 push @newlines, $line;
4293             }
4294         }
4295       }
4296
4297       return(\@newsections, \@newlines);
4298   }
4299
4300   return(\@sections, \@lines);
4301
4302 }
4303
4304 sub _items {
4305   my $self = shift;
4306
4307   #my @display = scalar(@_)
4308   #              ? @_
4309   #              : qw( _items_previous _items_pkg );
4310   #              #: qw( _items_pkg );
4311   #              #: qw( _items_previous _items_pkg _items_tax _items_credits _items_payments );
4312   my @display = qw( _items_previous _items_pkg );
4313
4314   my @b = ();
4315   foreach my $display ( @display ) {
4316     push @b, $self->$display(@_);
4317   }
4318   @b;
4319 }
4320
4321 sub _items_previous {
4322   my $self = shift;
4323   my $cust_main = $self->cust_main;
4324   my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
4325   my @b = ();
4326   foreach ( @pr_cust_bill ) {
4327     my $date = $conf->exists('invoice_show_prior_due_date')
4328                ? 'due '. $_->due_date2str($date_format)
4329                : time2str($date_format, $_->_date);
4330     push @b, {
4331       'description' => 'Previous Balance, Invoice #'. $_->invnum. " ($date)",
4332       #'pkgpart'     => 'N/A',
4333       'pkgnum'      => 'N/A',
4334       'amount'      => sprintf("%.2f", $_->owed),
4335     };
4336   }
4337   @b;
4338
4339   #{
4340   #    'description'     => 'Previous Balance',
4341   #    #'pkgpart'         => 'N/A',
4342   #    'pkgnum'          => 'N/A',
4343   #    'amount'          => sprintf("%10.2f", $pr_total ),
4344   #    'ext_description' => [ map {
4345   #                                 "Invoice ". $_->invnum.
4346   #                                 " (". time2str("%x",$_->_date). ") ".
4347   #                                 sprintf("%10.2f", $_->owed)
4348   #                         } @pr_cust_bill ],
4349
4350   #};
4351 }
4352
4353 sub _items_pkg {
4354   my $self = shift;
4355   my %options = @_;
4356
4357   warn "$me _items_pkg searching for all package line items\n"
4358     if $DEBUG > 1;
4359
4360   my @cust_bill_pkg = grep { $_->pkgnum } $self->cust_bill_pkg;
4361
4362   warn "$me _items_pkg filtering line items\n"
4363     if $DEBUG > 1;
4364   my @items = $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_);
4365
4366   if ($options{section} && $options{section}->{condensed}) {
4367
4368     warn "$me _items_pkg condensing section\n"
4369       if $DEBUG > 1;
4370
4371     my %itemshash = ();
4372     local $Storable::canonical = 1;
4373     foreach ( @items ) {
4374       my $item = { %$_ };
4375       delete $item->{ref};
4376       delete $item->{ext_description};
4377       my $key = freeze($item);
4378       $itemshash{$key} ||= 0;
4379       $itemshash{$key} ++; # += $item->{quantity};
4380     }
4381     @items = sort { $a->{description} cmp $b->{description} }
4382              map { my $i = thaw($_);
4383                    $i->{quantity} = $itemshash{$_};
4384                    $i->{amount} =
4385                      sprintf( "%.2f", $i->{quantity} * $i->{amount} );#unit_amount
4386                    $i;
4387                  }
4388              keys %itemshash;
4389   }
4390
4391   warn "$me _items_pkg returning ". scalar(@items). " items\n"
4392     if $DEBUG > 1;
4393
4394   @items;
4395 }
4396
4397 sub _taxsort {
4398   return 0 unless $a->itemdesc cmp $b->itemdesc;
4399   return -1 if $b->itemdesc eq 'Tax';
4400   return 1 if $a->itemdesc eq 'Tax';
4401   return -1 if $b->itemdesc eq 'Other surcharges';
4402   return 1 if $a->itemdesc eq 'Other surcharges';
4403   $a->itemdesc cmp $b->itemdesc;
4404 }
4405
4406 sub _items_tax {
4407   my $self = shift;
4408   my @cust_bill_pkg = sort _taxsort grep { ! $_->pkgnum } $self->cust_bill_pkg;
4409   $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_);
4410 }
4411
4412 sub _items_cust_bill_pkg {
4413   my $self = shift;
4414   my $cust_bill_pkgs = shift;
4415   my %opt = @_;
4416
4417   my $format = $opt{format} || '';
4418   my $escape_function = $opt{escape_function} || sub { shift };
4419   my $format_function = $opt{format_function} || '';
4420   my $unsquelched = $opt{unsquelched} || '';
4421   my $section = $opt{section}->{description} if $opt{section};
4422   my $summary_page = $opt{summary_page} || '';
4423   my $multilocation = $opt{multilocation} || '';
4424   my $multisection = $opt{multisection} || '';
4425   my $discount_show_always = 0;
4426
4427   my @b = ();
4428   my ($s, $r, $u) = ( undef, undef, undef );
4429   foreach my $cust_bill_pkg ( @$cust_bill_pkgs )
4430   {
4431
4432     warn "$me _items_cust_bill_pkg considering cust_bill_pkg $cust_bill_pkg\n"
4433       if $DEBUG > 1;
4434
4435     $discount_show_always = ($cust_bill_pkg->cust_bill_pkg_discount
4436                                 && $conf->exists('discount-show-always'));
4437
4438     foreach ( $s, $r, ($opt{skip_usage} ? () : $u ) ) {
4439       if ( $_ && !$cust_bill_pkg->hidden ) {
4440         $_->{amount}      = sprintf( "%.2f", $_->{amount} ),
4441         $_->{amount}      =~ s/^\-0\.00$/0.00/;
4442         $_->{unit_amount} = sprintf( "%.2f", $_->{unit_amount} ),
4443         push @b, { %$_ }
4444           unless ( $_->{amount} == 0 && !$discount_show_always );
4445         $_ = undef;
4446       }
4447     }
4448
4449     foreach my $display ( grep { defined($section)
4450                                  ? $_->section eq $section
4451                                  : 1
4452                                }
4453                           #grep { !$_->summary || !$summary_page } # bunk!
4454                           grep { !$_->summary || $multisection }
4455                           $cust_bill_pkg->cust_bill_pkg_display
4456                         )
4457     {
4458
4459       warn "$me _items_cust_bill_pkg considering display item $display\n"
4460         if $DEBUG > 1;
4461
4462       my $type = $display->type;
4463
4464       my $desc = $cust_bill_pkg->desc;
4465       $desc = substr($desc, 0, 50). '...'
4466         if $format eq 'latex' && length($desc) > 50;
4467
4468       my %details_opt = ( 'format'          => $format,
4469                           'escape_function' => $escape_function,
4470                           'format_function' => $format_function,
4471                         );
4472
4473       if ( $cust_bill_pkg->pkgnum > 0 ) {
4474
4475         warn "$me _items_cust_bill_pkg cust_bill_pkg is non-tax\n"
4476           if $DEBUG > 1;
4477  
4478         my $cust_pkg = $cust_bill_pkg->cust_pkg;
4479
4480         if ( $cust_bill_pkg->setup != 0 && (!$type || $type eq 'S') ) {
4481
4482           warn "$me _items_cust_bill_pkg adding setup\n"
4483             if $DEBUG > 1;
4484
4485           my $description = $desc;
4486           $description .= ' Setup' if $cust_bill_pkg->recur != 0;
4487
4488           my @d = ();
4489           unless ( $cust_pkg->part_pkg->hide_svc_detail
4490                 || $cust_bill_pkg->hidden )
4491           {
4492
4493             push @d, map &{$escape_function}($_),
4494                          $cust_pkg->h_labels_short($self->_date, undef, 'I')
4495               unless $cust_bill_pkg->pkgpart_override; #don't redisplay services
4496
4497             if ( $multilocation ) {
4498               my $loc = $cust_pkg->location_label;
4499               $loc = substr($loc, 0, 50). '...'
4500                 if $format eq 'latex' && length($loc) > 50;
4501               push @d, &{$escape_function}($loc);
4502             }
4503
4504           }
4505
4506           push @d, $cust_bill_pkg->details(%details_opt)
4507             if $cust_bill_pkg->recur == 0;
4508
4509           if ( $cust_bill_pkg->hidden ) {
4510             $s->{amount}      += $cust_bill_pkg->setup;
4511             $s->{unit_amount} += $cust_bill_pkg->unitsetup;
4512             push @{ $s->{ext_description} }, @d;
4513           } else {
4514             $s = {
4515               description     => $description,
4516               #pkgpart         => $part_pkg->pkgpart,
4517               pkgnum          => $cust_bill_pkg->pkgnum,
4518               amount          => $cust_bill_pkg->setup,
4519               unit_amount     => $cust_bill_pkg->unitsetup,
4520               quantity        => $cust_bill_pkg->quantity,
4521               ext_description => \@d,
4522             };
4523           };
4524
4525         }
4526
4527         if ( ( $cust_bill_pkg->recur != 0  || $cust_bill_pkg->setup == 0 || 
4528                 ($discount_show_always && $cust_bill_pkg->recur == 0) ) &&
4529              ( !$type || $type eq 'R' || $type eq 'U' )
4530            )
4531         {
4532
4533           warn "$me _items_cust_bill_pkg adding recur/usage\n"
4534             if $DEBUG > 1;
4535
4536           my $is_summary = $display->summary;
4537           my $description = ($is_summary && $type && $type eq 'U')
4538                             ? "Usage charges" : $desc;
4539
4540           $description .= " (" . time2str($date_format, $cust_bill_pkg->sdate).
4541                           " - ". time2str($date_format, $cust_bill_pkg->edate).
4542                           ")"
4543             unless $conf->exists('disable_line_item_date_ranges');
4544
4545           my @d = ();
4546
4547           #at least until cust_bill_pkg has "past" ranges in addition to
4548           #the "future" sdate/edate ones... see #3032
4549           my @dates = ( $self->_date );
4550           my $prev = $cust_bill_pkg->previous_cust_bill_pkg;
4551           push @dates, $prev->sdate if $prev;
4552           push @dates, undef if !$prev;
4553
4554           unless ( $cust_pkg->part_pkg->hide_svc_detail
4555                 || $cust_bill_pkg->itemdesc
4556                 || $cust_bill_pkg->hidden
4557                 || $is_summary && $type && $type eq 'U' )
4558           {
4559
4560             warn "$me _items_cust_bill_pkg adding service details\n"
4561               if $DEBUG > 1;
4562
4563             push @d, map &{$escape_function}($_),
4564                          $cust_pkg->h_labels_short(@dates, 'I')
4565                                                    #$cust_bill_pkg->edate,
4566                                                    #$cust_bill_pkg->sdate)
4567               unless $cust_bill_pkg->pkgpart_override; #don't redisplay services
4568
4569             warn "$me _items_cust_bill_pkg done adding service details\n"
4570               if $DEBUG > 1;
4571
4572             if ( $multilocation ) {
4573               my $loc = $cust_pkg->location_label;
4574               $loc = substr($loc, 0, 50). '...'
4575                 if $format eq 'latex' && length($loc) > 50;
4576               push @d, &{$escape_function}($loc);
4577             }
4578
4579           }
4580
4581           warn "$me _items_cust_bill_pkg adding details\n"
4582             if $DEBUG > 1;
4583
4584           push @d, $cust_bill_pkg->details(%details_opt)
4585             unless $is_summary; # || ($type && $type eq 'R');
4586
4587           warn "$me _items_cust_bill_pkg calculating amount\n"
4588             if $DEBUG > 1;
4589   
4590           my $amount = 0;
4591           if (!$type) {
4592             $amount = $cust_bill_pkg->recur;
4593           }elsif($type eq 'R') {
4594             $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
4595           }elsif($type eq 'U') {
4596             $amount = $cust_bill_pkg->usage;
4597           }
4598   
4599           if ( !$type || $type eq 'R' ) {
4600
4601             warn "$me _items_cust_bill_pkg adding recur\n"
4602               if $DEBUG > 1;
4603
4604             if ( $cust_bill_pkg->hidden ) {
4605               $r->{amount}      += $amount;
4606               $r->{unit_amount} += $cust_bill_pkg->unitrecur;
4607               push @{ $r->{ext_description} }, @d;
4608             } else {
4609               $r = {
4610                 description     => $description,
4611                 #pkgpart         => $part_pkg->pkgpart,
4612                 pkgnum          => $cust_bill_pkg->pkgnum,
4613                 amount          => $amount,
4614                 unit_amount     => $cust_bill_pkg->unitrecur,
4615                 quantity        => $cust_bill_pkg->quantity,
4616                 ext_description => \@d,
4617               };
4618             }
4619
4620           } else {  # $type eq 'U'
4621
4622             warn "$me _items_cust_bill_pkg adding usage\n"
4623               if $DEBUG > 1;
4624
4625             if ( $cust_bill_pkg->hidden ) {
4626               $u->{amount}      += $amount;
4627               $u->{unit_amount} += $cust_bill_pkg->unitrecur;
4628               push @{ $u->{ext_description} }, @d;
4629             } else {
4630               $u = {
4631                 description     => $description,
4632                 #pkgpart         => $part_pkg->pkgpart,
4633                 pkgnum          => $cust_bill_pkg->pkgnum,
4634                 amount          => $amount,
4635                 unit_amount     => $cust_bill_pkg->unitrecur,
4636                 quantity        => $cust_bill_pkg->quantity,
4637                 ext_description => \@d,
4638               };
4639             }
4640
4641           }
4642
4643         } # recurring or usage with recurring charge
4644
4645       } else { #pkgnum tax or one-shot line item (??)
4646
4647         warn "$me _items_cust_bill_pkg cust_bill_pkg is tax\n"
4648           if $DEBUG > 1;
4649
4650         if ( $cust_bill_pkg->setup != 0 ) {
4651           push @b, {
4652             'description' => $desc,
4653             'amount'      => sprintf("%.2f", $cust_bill_pkg->setup),
4654           };
4655         }
4656         if ( $cust_bill_pkg->recur != 0 ) {
4657           push @b, {
4658             'description' => "$desc (".
4659                              time2str($date_format, $cust_bill_pkg->sdate). ' - '.
4660                              time2str($date_format, $cust_bill_pkg->edate). ')',
4661             'amount'      => sprintf("%.2f", $cust_bill_pkg->recur),
4662           };
4663         }
4664
4665       }
4666
4667     }
4668
4669   }
4670
4671   warn "$me _items_cust_bill_pkg done considering cust_bill_pkgs\n"
4672     if $DEBUG > 1;
4673
4674   foreach ( $s, $r, ($opt{skip_usage} ? () : $u ) ) {
4675     if ( $_  ) {
4676       $_->{amount}      = sprintf( "%.2f", $_->{amount} ),
4677       $_->{amount}      =~ s/^\-0\.00$/0.00/;
4678       $_->{unit_amount} = sprintf( "%.2f", $_->{unit_amount} ),
4679       push @b, { %$_ }
4680         unless ( $_->{amount} == 0 && !$discount_show_always );
4681     }
4682   }
4683
4684   @b;
4685
4686 }
4687
4688 sub _items_credits {
4689   my( $self, %opt ) = @_;
4690   my $trim_len = $opt{'trim_len'} || 60;
4691
4692   my @b;
4693   #credits
4694   foreach ( $self->cust_credited ) {
4695
4696     #something more elaborate if $_->amount ne $_->cust_credit->credited ?
4697
4698     my $reason = substr($_->cust_credit->reason, 0, $trim_len);
4699     $reason .= '...' if length($reason) < length($_->cust_credit->reason);
4700     $reason = " ($reason) " if $reason;
4701
4702     push @b, {
4703       #'description' => 'Credit ref\#'. $_->crednum.
4704       #                 " (". time2str("%x",$_->cust_credit->_date) .")".
4705       #                 $reason,
4706       'description' => 'Credit applied '.
4707                        time2str($date_format,$_->cust_credit->_date). $reason,
4708       'amount'      => sprintf("%.2f",$_->amount),
4709     };
4710   }
4711
4712   @b;
4713
4714 }
4715
4716 sub _items_payments {
4717   my $self = shift;
4718
4719   my @b;
4720   #get & print payments
4721   foreach ( $self->cust_bill_pay ) {
4722
4723     #something more elaborate if $_->amount ne ->cust_pay->paid ?
4724
4725     push @b, {
4726       'description' => "Payment received ".
4727                        time2str($date_format,$_->cust_pay->_date ),
4728       'amount'      => sprintf("%.2f", $_->amount )
4729     };
4730   }
4731
4732   @b;
4733
4734 }
4735
4736 =item call_details [ OPTION => VALUE ... ]
4737
4738 Returns an array of CSV strings representing the call details for this invoice
4739 The only option available is the boolean prepend_billed_number
4740
4741 =cut
4742
4743 sub call_details {
4744   my ($self, %opt) = @_;
4745
4746   my $format_function = sub { shift };
4747
4748   if ($opt{prepend_billed_number}) {
4749     $format_function = sub {
4750       my $detail = shift;
4751       my $row = shift;
4752
4753       $row->amount ? $row->phonenum. ",". $detail : '"Billed number",'. $detail;
4754       
4755     };
4756   }
4757
4758   my @details = map { $_->details( 'format_function' => $format_function,
4759                                    'escape_function' => sub{ return() },
4760                                  )
4761                     }
4762                   grep { $_->pkgnum }
4763                   $self->cust_bill_pkg;
4764   my $header = $details[0];
4765   ( $header, grep { $_ ne $header } @details );
4766 }
4767
4768
4769 =back
4770
4771 =head1 SUBROUTINES
4772
4773 =over 4
4774
4775 =item process_reprint
4776
4777 =cut
4778
4779 sub process_reprint {
4780   process_re_X('print', @_);
4781 }
4782
4783 =item process_reemail
4784
4785 =cut
4786
4787 sub process_reemail {
4788   process_re_X('email', @_);
4789 }
4790
4791 =item process_refax
4792
4793 =cut
4794
4795 sub process_refax {
4796   process_re_X('fax', @_);
4797 }
4798
4799 =item process_reftp
4800
4801 =cut
4802
4803 sub process_reftp {
4804   process_re_X('ftp', @_);
4805 }
4806
4807 =item respool
4808
4809 =cut
4810
4811 sub process_respool {
4812   process_re_X('spool', @_);
4813 }
4814
4815 use Storable qw(thaw);
4816 use Data::Dumper;
4817 use MIME::Base64;
4818 sub process_re_X {
4819   my( $method, $job ) = ( shift, shift );
4820   warn "$me process_re_X $method for job $job\n" if $DEBUG;
4821
4822   my $param = thaw(decode_base64(shift));
4823   warn Dumper($param) if $DEBUG;
4824
4825   re_X(
4826     $method,
4827     $job,
4828     %$param,
4829   );
4830
4831 }
4832
4833 sub re_X {
4834   my($method, $job, %param ) = @_;
4835   if ( $DEBUG ) {
4836     warn "re_X $method for job $job with param:\n".
4837          join( '', map { "  $_ => ". $param{$_}. "\n" } keys %param );
4838   }
4839
4840   #some false laziness w/search/cust_bill.html
4841   my $distinct = '';
4842   my $orderby = 'ORDER BY cust_bill._date';
4843
4844   my $extra_sql = ' WHERE '. FS::cust_bill->search_sql_where(\%param);
4845
4846   my $addl_from = 'LEFT JOIN cust_main USING ( custnum )';
4847      
4848   my @cust_bill = qsearch( {
4849     #'select'    => "cust_bill.*",
4850     'table'     => 'cust_bill',
4851     'addl_from' => $addl_from,
4852     'hashref'   => {},
4853     'extra_sql' => $extra_sql,
4854     'order_by'  => $orderby,
4855     'debug' => 1,
4856   } );
4857
4858   $method .= '_invoice' unless $method eq 'email' || $method eq 'print';
4859
4860   warn " $me re_X $method: ". scalar(@cust_bill). " invoices found\n"
4861     if $DEBUG;
4862
4863   my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
4864   foreach my $cust_bill ( @cust_bill ) {
4865     $cust_bill->$method();
4866
4867     if ( $job ) { #progressbar foo
4868       $num++;
4869       if ( time - $min_sec > $last ) {
4870         my $error = $job->update_statustext(
4871           int( 100 * $num / scalar(@cust_bill) )
4872         );
4873         die $error if $error;
4874         $last = time;
4875       }
4876     }
4877
4878   }
4879
4880 }
4881
4882 =back
4883
4884 =head1 CLASS METHODS
4885
4886 =over 4
4887
4888 =item owed_sql
4889
4890 Returns an SQL fragment to retreive the amount owed (charged minus credited and paid).
4891
4892 =cut
4893
4894 sub owed_sql {
4895   my ($class, $start, $end) = @_;
4896   'charged - '. 
4897     $class->paid_sql($start, $end). ' - '. 
4898     $class->credited_sql($start, $end);
4899 }
4900
4901 =item net_sql
4902
4903 Returns an SQL fragment to retreive the net amount (charged minus credited).
4904
4905 =cut
4906
4907 sub net_sql {
4908   my ($class, $start, $end) = @_;
4909   'charged - '. $class->credited_sql($start, $end);
4910 }
4911
4912 =item paid_sql
4913
4914 Returns an SQL fragment to retreive the amount paid against this invoice.
4915
4916 =cut
4917
4918 sub paid_sql {
4919   my ($class, $start, $end) = @_;
4920   $start &&= "AND cust_bill_pay._date <= $start";
4921   $end   &&= "AND cust_bill_pay._date > $end";
4922   $start = '' unless defined($start);
4923   $end   = '' unless defined($end);
4924   "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
4925        WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end  )";
4926 }
4927
4928 =item credited_sql
4929
4930 Returns an SQL fragment to retreive the amount credited against this invoice.
4931
4932 =cut
4933
4934 sub credited_sql {
4935   my ($class, $start, $end) = @_;
4936   $start &&= "AND cust_credit_bill._date <= $start";
4937   $end   &&= "AND cust_credit_bill._date >  $end";
4938   $start = '' unless defined($start);
4939   $end   = '' unless defined($end);
4940   "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
4941        WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end  )";
4942 }
4943
4944 =item due_date_sql
4945
4946 Returns an SQL fragment to retrieve the due date of an invoice.
4947 Currently only supported on PostgreSQL.
4948
4949 =cut
4950
4951 sub due_date_sql {
4952 'COALESCE(
4953   SUBSTRING(
4954     COALESCE(
4955       cust_bill.invoice_terms,
4956       cust_main.invoice_terms,
4957       \''.($conf->config('invoice_default_terms') || '').'\'
4958     ), E\'Net (\\\\d+)\'
4959   )::INTEGER, 0
4960 ) * 86400 + cust_bill._date'
4961 }
4962
4963 =item search_sql_where HASHREF
4964
4965 Class method which returns an SQL WHERE fragment to search for parameters
4966 specified in HASHREF.  Valid parameters are
4967
4968 =over 4
4969
4970 =item _date
4971
4972 List reference of start date, end date, as UNIX timestamps.
4973
4974 =item invnum_min
4975
4976 =item invnum_max
4977
4978 =item agentnum
4979
4980 =item charged
4981
4982 List reference of charged limits (exclusive).
4983
4984 =item owed
4985
4986 List reference of charged limits (exclusive).
4987
4988 =item open
4989
4990 flag, return open invoices only
4991
4992 =item net
4993
4994 flag, return net invoices only
4995
4996 =item days
4997
4998 =item newest_percust
4999
5000 =back
5001
5002 Note: validates all passed-in data; i.e. safe to use with unchecked CGI params.
5003
5004 =cut
5005
5006 sub search_sql_where {
5007   my($class, $param) = @_;
5008   if ( $DEBUG ) {
5009     warn "$me search_sql_where called with params: \n".
5010          join("\n", map { "  $_: ". $param->{$_} } keys %$param ). "\n";
5011   }
5012
5013   my @search = ();
5014
5015   #agentnum
5016   if ( $param->{'agentnum'} =~ /^(\d+)$/ ) {
5017     push @search, "cust_main.agentnum = $1";
5018   }
5019
5020   #_date
5021   if ( $param->{_date} ) {
5022     my($beginning, $ending) = @{$param->{_date}};
5023
5024     push @search, "cust_bill._date >= $beginning",
5025                   "cust_bill._date <  $ending";
5026   }
5027
5028   #invnum
5029   if ( $param->{'invnum_min'} =~ /^(\d+)$/ ) {
5030     push @search, "cust_bill.invnum >= $1";
5031   }
5032   if ( $param->{'invnum_max'} =~ /^(\d+)$/ ) {
5033     push @search, "cust_bill.invnum <= $1";
5034   }
5035
5036   #charged
5037   if ( $param->{charged} ) {
5038     my @charged = ref($param->{charged})
5039                     ? @{ $param->{charged} }
5040                     : ($param->{charged});
5041
5042     push @search, map { s/^charged/cust_bill.charged/; $_; }
5043                       @charged;
5044   }
5045
5046   my $owed_sql = FS::cust_bill->owed_sql;
5047
5048   #owed
5049   if ( $param->{owed} ) {
5050     my @owed = ref($param->{owed})
5051                  ? @{ $param->{owed} }
5052                  : ($param->{owed});
5053     push @search, map { s/^owed/$owed_sql/; $_; }
5054                       @owed;
5055   }
5056
5057   #open/net flags
5058   push @search, "0 != $owed_sql"
5059     if $param->{'open'};
5060   push @search, '0 != '. FS::cust_bill->net_sql
5061     if $param->{'net'};
5062
5063   #days
5064   push @search, "cust_bill._date < ". (time-86400*$param->{'days'})
5065     if $param->{'days'};
5066
5067   #newest_percust
5068   if ( $param->{'newest_percust'} ) {
5069
5070     #$distinct = 'DISTINCT ON ( cust_bill.custnum )';
5071     #$orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
5072
5073     my @newest_where = map { my $x = $_;
5074                              $x =~ s/\bcust_bill\./newest_cust_bill./g;
5075                              $x;
5076                            }
5077                            grep ! /^cust_main./, @search;
5078     my $newest_where = scalar(@newest_where)
5079                          ? ' AND '. join(' AND ', @newest_where)
5080                          : '';
5081
5082
5083     push @search, "cust_bill._date = (
5084       SELECT(MAX(newest_cust_bill._date)) FROM cust_bill AS newest_cust_bill
5085         WHERE newest_cust_bill.custnum = cust_bill.custnum
5086           $newest_where
5087     )";
5088
5089   }
5090
5091   #agent virtualization
5092   my $curuser = $FS::CurrentUser::CurrentUser;
5093   if ( $curuser->username eq 'fs_queue'
5094        && $param->{'CurrentUser'} =~ /^(\w+)$/ ) {
5095     my $username = $1;
5096     my $newuser = qsearchs('access_user', {
5097       'username' => $username,
5098       'disabled' => '',
5099     } );
5100     if ( $newuser ) {
5101       $curuser = $newuser;
5102     } else {
5103       warn "$me WARNING: (fs_queue) can't find CurrentUser $username\n";
5104     }
5105   }
5106   push @search, $curuser->agentnums_sql;
5107
5108   join(' AND ', @search );
5109
5110 }
5111
5112 =back
5113
5114 =head1 BUGS
5115
5116 The delete method.
5117
5118 =head1 SEE ALSO
5119
5120 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
5121 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base
5122 documentation.
5123
5124 =cut
5125
5126 1;
5127