option to bill partial period on cancel/suspend, #16066
[freeside.git] / FS / FS / cust_main / Billing.pm
1 package FS::cust_main::Billing;
2
3 use strict;
4 use vars qw( $conf $DEBUG $me );
5 use Carp;
6 use Data::Dumper;
7 use List::Util qw( min );
8 use FS::UID qw( dbh );
9 use FS::Record qw( qsearch qsearchs dbdef );
10 use FS::Misc::DateTime qw( day_end );
11 use FS::cust_bill;
12 use FS::cust_bill_pkg;
13 use FS::cust_bill_pkg_display;
14 use FS::cust_bill_pay;
15 use FS::cust_credit_bill;
16 use FS::cust_tax_adjustment;
17 use FS::tax_rate;
18 use FS::tax_rate_location;
19 use FS::cust_bill_pkg_tax_location;
20 use FS::cust_bill_pkg_tax_rate_location;
21 use FS::part_event;
22 use FS::part_event_condition;
23 use FS::pkg_category;
24
25 # 1 is mostly method/subroutine entry and options
26 # 2 traces progress of some operations
27 # 3 is even more information including possibly sensitive data
28 $DEBUG = 0;
29 $me = '[FS::cust_main::Billing]';
30
31 install_callback FS::UID sub { 
32   $conf = new FS::Conf;
33   #yes, need it for stuff below (prolly should be cached)
34 };
35
36 =head1 NAME
37
38 FS::cust_main::Billing - Billing mixin for cust_main
39
40 =head1 SYNOPSIS
41
42 =head1 DESCRIPTION
43
44 These methods are available on FS::cust_main objects.
45
46 =head1 METHODS
47
48 =over 4
49
50 =item bill_and_collect 
51
52 Cancels and suspends any packages due, generates bills, applies payments and
53 credits, and applies collection events to run cards, send bills and notices,
54 etc.
55
56 By default, warns on errors and continues with the next operation (but see the
57 "fatal" flag below).
58
59 Options are passed as name-value pairs.  Currently available options are:
60
61 =over 4
62
63 =item time
64
65 Bills the customer as if it were that time.  Specified as a UNIX timestamp; see L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion functions.  For example:
66
67  use Date::Parse;
68  ...
69  $cust_main->bill( 'time' => str2time('April 20th, 2001') );
70
71 =item invoice_time
72
73 Used in conjunction with the I<time> option, this option specifies the date of for the generated invoices.  Other calculations, such as whether or not to generate the invoice in the first place, are not affected.
74
75 =item check_freq
76
77 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
78
79 =item resetup
80
81 If set true, re-charges setup fees.
82
83 =item fatal
84
85 If set any errors prevent subsequent operations from continusing.  If set
86 specifically to "return", returns the error (or false, if there is no error).
87 Any other true value causes errors to die.
88
89 =item debug
90
91 Debugging level.  Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
92
93 =item job
94
95 Optional FS::queue entry to receive status updates.
96
97 =back
98
99 Options are passed to the B<bill> and B<collect> methods verbatim, so all
100 options of those methods are also available.
101
102 =cut
103
104 sub bill_and_collect {
105   my( $self, %options ) = @_;
106
107   my $error;
108
109   #$options{actual_time} not $options{time} because freeside-daily -d is for
110   #pre-printing invoices
111
112   $options{'actual_time'} ||= time;
113   my $job = $options{'job'};
114
115   $job->update_statustext('0,cleaning expired packages') if $job;
116   $error = $self->cancel_expired_pkgs( day_end( $options{actual_time} ) );
117   if ( $error ) {
118     $error = "Error expiring custnum ". $self->custnum. ": $error";
119     if    ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
120     elsif ( $options{fatal}                                ) { die    $error; }
121     else                                                     { warn   $error; }
122   }
123
124   $error = $self->suspend_adjourned_pkgs( day_end( $options{actual_time} ) );
125   if ( $error ) {
126     $error = "Error adjourning custnum ". $self->custnum. ": $error";
127     if    ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
128     elsif ( $options{fatal}                                ) { die    $error; }
129     else                                                     { warn   $error; }
130   }
131
132   $job->update_statustext('20,billing packages') if $job;
133   $error = $self->bill( %options );
134   if ( $error ) {
135     $error = "Error billing custnum ". $self->custnum. ": $error";
136     if    ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
137     elsif ( $options{fatal}                                ) { die    $error; }
138     else                                                     { warn   $error; }
139   }
140
141   $job->update_statustext('50,applying payments and credits') if $job;
142   $error = $self->apply_payments_and_credits;
143   if ( $error ) {
144     $error = "Error applying custnum ". $self->custnum. ": $error";
145     if    ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
146     elsif ( $options{fatal}                                ) { die    $error; }
147     else                                                     { warn   $error; }
148   }
149
150   $job->update_statustext('70,running collection events') if $job;
151   unless ( $conf->exists('cancelled_cust-noevents')
152            && ! $self->num_ncancelled_pkgs
153   ) {
154     $error = $self->collect( %options );
155     if ( $error ) {
156       $error = "Error collecting custnum ". $self->custnum. ": $error";
157       if    ($options{fatal} && $options{fatal} eq 'return') { return $error; }
158       elsif ($options{fatal}                               ) { die    $error; }
159       else                                                   { warn   $error; }
160     }
161   }
162   $job->update_statustext('100,finished') if $job;
163
164   '';
165
166 }
167
168 sub cancel_expired_pkgs {
169   my ( $self, $time, %options ) = @_;
170   
171   my @cancel_pkgs = $self->ncancelled_pkgs( { 
172     'extra_sql' => " AND expire IS NOT NULL AND expire > 0 AND expire <= $time "
173   } );
174
175   my @errors = ();
176
177   foreach my $cust_pkg ( @cancel_pkgs ) {
178     my $cpr = $cust_pkg->last_cust_pkg_reason('expire');
179     my $error = $cust_pkg->cancel($cpr ? ( 'reason'        => $cpr->reasonnum,
180                                            'reason_otaker' => $cpr->otaker,
181                                            'time'          => $time,
182                                          )
183                                        : ()
184                                  );
185     push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
186   }
187
188   scalar(@errors) ? join(' / ', @errors) : '';
189
190 }
191
192 sub suspend_adjourned_pkgs {
193   my ( $self, $time, %options ) = @_;
194   
195   my @susp_pkgs = $self->ncancelled_pkgs( {
196     'extra_sql' =>
197       " AND ( susp IS NULL OR susp = 0 )
198         AND (    ( bill    IS NOT NULL AND bill    != 0 AND bill    <  $time )
199               OR ( adjourn IS NOT NULL AND adjourn != 0 AND adjourn <= $time )
200             )
201       ",
202   } );
203
204   #only because there's no SQL test for is_prepaid :/
205   @susp_pkgs = 
206     grep {     (    $_->part_pkg->is_prepaid
207                  && $_->bill
208                  && $_->bill < $time
209                )
210             || (    $_->adjourn
211                  && $_->adjourn <= $time
212                )
213            
214          }
215          @susp_pkgs;
216
217   my @errors = ();
218
219   foreach my $cust_pkg ( @susp_pkgs ) {
220     my $cpr = $cust_pkg->last_cust_pkg_reason('adjourn')
221       if ($cust_pkg->adjourn && $cust_pkg->adjourn < $^T);
222     my $error = $cust_pkg->suspend($cpr ? ( 'reason' => $cpr->reasonnum,
223                                             'reason_otaker' => $cpr->otaker
224                                           )
225                                         : ()
226                                   );
227     push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
228   }
229
230   scalar(@errors) ? join(' / ', @errors) : '';
231
232 }
233
234 =item bill OPTIONS
235
236 Generates invoices (see L<FS::cust_bill>) for this customer.  Usually used in
237 conjunction with the collect method by calling B<bill_and_collect>.
238
239 If there is an error, returns the error, otherwise returns false.
240
241 Options are passed as name-value pairs.  Currently available options are:
242
243 =over 4
244
245 =item resetup
246
247 If set true, re-charges setup fees.
248
249 =item recurring_only
250
251 If set true then only bill recurring charges, not setup, usage, one time
252 charges, etc.
253
254 =item freq_override
255
256 If set, then override the normal frequency and look for a part_pkg_discount
257 to take at that frequency.  This is appropriate only when the normal 
258 frequency for all packages is monthly, and is an error otherwise.  Use
259 C<pkg_list> to limit the set of packages included in billing.
260
261 =item time
262
263 Bills the customer as if it were that time.  Specified as a UNIX timestamp; see L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion functions.  For example:
264
265  use Date::Parse;
266  ...
267  $cust_main->bill( 'time' => str2time('April 20th, 2001') );
268
269 =item pkg_list
270
271 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
272
273  $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
274
275 =item not_pkgpart
276
277 A hashref of pkgparts to exclude from this billing run (can also be specified as a comma-separated scalar).
278
279 =item invoice_time
280
281 Used in conjunction with the I<time> option, this option specifies the date of for the generated invoices.  Other calculations, such as whether or not to generate the invoice in the first place, are not affected.
282
283 =item cancel
284
285 This boolean value informs the us that the package is being cancelled.  This
286 typically might mean not charging the normal recurring fee but only usage
287 fees since the last billing. Setup charges may be charged.  Not all package
288 plans support this feature (they tend to charge 0).
289
290 =item no_usage_reset
291
292 Prevent the resetting of usage limits during this call.
293
294 =item no_commit
295
296 Do not save the generated bill in the database.  Useful with return_bill
297
298 =item return_bill
299
300 A list reference on which the generated bill(s) will be returned.
301
302 =item invoice_terms
303
304 Optional terms to be printed on this invoice.  Otherwise, customer-specific
305 terms or the default terms are used.
306
307 =back
308
309 =cut
310
311 sub bill {
312   my( $self, %options ) = @_;
313
314   return '' if $self->payby eq 'COMP';
315
316   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
317
318   warn "$me bill customer ". $self->custnum. "\n"
319     if $DEBUG;
320
321   my $time = $options{'time'} || time;
322   my $invoice_time = $options{'invoice_time'} || $time;
323
324   $options{'not_pkgpart'} ||= {};
325   $options{'not_pkgpart'} = { map { $_ => 1 }
326                                   split(/\s*,\s*/, $options{'not_pkgpart'})
327                             }
328     unless ref($options{'not_pkgpart'});
329
330   local $SIG{HUP} = 'IGNORE';
331   local $SIG{INT} = 'IGNORE';
332   local $SIG{QUIT} = 'IGNORE';
333   local $SIG{TERM} = 'IGNORE';
334   local $SIG{TSTP} = 'IGNORE';
335   local $SIG{PIPE} = 'IGNORE';
336
337   my $oldAutoCommit = $FS::UID::AutoCommit;
338   local $FS::UID::AutoCommit = 0;
339   my $dbh = dbh;
340
341   warn "$me acquiring lock on customer ". $self->custnum. "\n"
342     if $DEBUG;
343
344   $self->select_for_update; #mutex
345
346   warn "$me running pre-bill events for customer ". $self->custnum. "\n"
347     if $DEBUG;
348
349   my $error = $self->do_cust_event(
350     'debug'      => ( $options{'debug'} || 0 ),
351     'time'       => $invoice_time,
352     'check_freq' => $options{'check_freq'},
353     'stage'      => 'pre-bill',
354   )
355     unless $options{no_commit};
356   if ( $error ) {
357     $dbh->rollback if $oldAutoCommit && !$options{no_commit};
358     return $error;
359   }
360
361   warn "$me done running pre-bill events for customer ". $self->custnum. "\n"
362     if $DEBUG;
363
364   #keep auto-charge and non-auto-charge line items separate
365   my @passes = ( '', 'no_auto' );
366
367   my %cust_bill_pkg = map { $_ => [] } @passes;
368
369   ###
370   # find the packages which are due for billing, find out how much they are
371   # & generate invoice database.
372   ###
373
374   my %total_setup   = map { my $z = 0; $_ => \$z; } @passes;
375   my %total_recur   = map { my $z = 0; $_ => \$z; } @passes;
376
377   my %taxlisthash = map { $_ => {} } @passes;
378
379   my @precommit_hooks = ();
380
381   $options{'pkg_list'} ||= [ $self->ncancelled_pkgs ];  #param checks?
382   foreach my $cust_pkg ( @{ $options{'pkg_list'} } ) {
383
384     next if $options{'not_pkgpart'}->{$cust_pkg->pkgpart};
385
386     warn "  bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG;
387
388     #? to avoid use of uninitialized value errors... ?
389     $cust_pkg->setfield('bill', '')
390       unless defined($cust_pkg->bill);
391  
392     #my $part_pkg = $cust_pkg->part_pkg;
393
394     my $real_pkgpart = $cust_pkg->pkgpart;
395     my %hash = $cust_pkg->hash;
396
397     # we could implement this bit as FS::part_pkg::has_hidden, but we already
398     # suffer from performance issues
399     $options{has_hidden} = 0;
400     my @part_pkg = $cust_pkg->part_pkg->self_and_bill_linked;
401     $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
402  
403     foreach my $part_pkg ( @part_pkg ) {
404
405       $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
406
407       my $pass = ($cust_pkg->no_auto || $part_pkg->no_auto) ? 'no_auto' : '';
408
409       my $next_bill = $cust_pkg->getfield('bill') || 0;
410       my $error;
411       # let this run once if this is the last bill upon cancellation
412       while ( $next_bill <= $time or $options{cancel} ) {
413         $error =
414           $self->_make_lines( 'part_pkg'            => $part_pkg,
415                               'cust_pkg'            => $cust_pkg,
416                               'precommit_hooks'     => \@precommit_hooks,
417                               'line_items'          => $cust_bill_pkg{$pass},
418                               'setup'               => $total_setup{$pass},
419                               'recur'               => $total_recur{$pass},
420                               'tax_matrix'          => $taxlisthash{$pass},
421                               'time'                => $time,
422                               'real_pkgpart'        => $real_pkgpart,
423                               'options'             => \%options,
424                             );
425
426         # Stop if anything goes wrong
427         last if $error;
428
429         # or if we're not incrementing the bill date.
430         last if ($cust_pkg->getfield('bill') || 0) == $next_bill;
431
432         # or if we're letting it run only once
433         last if $options{cancel};
434
435         $next_bill = $cust_pkg->getfield('bill') || 0;
436
437         #stop if -o was passed to freeside-daily
438         last if $options{'one_recur'};
439       }
440       if ($error) {
441         $dbh->rollback if $oldAutoCommit && !$options{no_commit};
442         return $error;
443       }
444
445     } #foreach my $part_pkg
446
447   } #foreach my $cust_pkg
448
449   #if the customer isn't on an automatic payby, everything can go on a single
450   #invoice anyway?
451   #if ( $cust_main->payby !~ /^(CARD|CHEK)$/ ) {
452     #merge everything into one list
453   #}
454
455   foreach my $pass (@passes) { # keys %cust_bill_pkg ) {
456
457     my @cust_bill_pkg = _omit_zero_value_bundles(@{ $cust_bill_pkg{$pass} });
458
459     next unless @cust_bill_pkg; #don't create an invoice w/o line items
460
461     warn "$me billing pass $pass\n"
462            #.Dumper(\@cust_bill_pkg)."\n"
463       if $DEBUG > 2;
464
465     if ( scalar( grep { $_->recur && $_->recur > 0 } @cust_bill_pkg) ||
466            !$conf->exists('postal_invoice-recurring_only')
467        )
468     {
469
470       my $postal_pkg = $self->charge_postal_fee();
471       if ( $postal_pkg && !ref( $postal_pkg ) ) {
472
473         $dbh->rollback if $oldAutoCommit && !$options{no_commit};
474         return "can't charge postal invoice fee for customer ".
475           $self->custnum. ": $postal_pkg";
476
477       } elsif ( $postal_pkg ) {
478
479         my $real_pkgpart = $postal_pkg->pkgpart;
480         # we could implement this bit as FS::part_pkg::has_hidden, but we already
481         # suffer from performance issues
482         $options{has_hidden} = 0;
483         my @part_pkg = $postal_pkg->part_pkg->self_and_bill_linked;
484         $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
485
486         foreach my $part_pkg ( @part_pkg ) {
487           my %postal_options = %options;
488           delete $postal_options{cancel};
489           my $error =
490             $self->_make_lines( 'part_pkg'            => $part_pkg,
491                                 'cust_pkg'            => $postal_pkg,
492                                 'precommit_hooks'     => \@precommit_hooks,
493                                 'line_items'          => \@cust_bill_pkg,
494                                 'setup'               => $total_setup{$pass},
495                                 'recur'               => $total_recur{$pass},
496                                 'tax_matrix'          => $taxlisthash{$pass},
497                                 'time'                => $time,
498                                 'real_pkgpart'        => $real_pkgpart,
499                                 'options'             => \%postal_options,
500                               );
501           if ($error) {
502             $dbh->rollback if $oldAutoCommit && !$options{no_commit};
503             return $error;
504           }
505         }
506
507         # it's silly to have a zero value postal_pkg, but....
508         @cust_bill_pkg = _omit_zero_value_bundles(@cust_bill_pkg);
509
510       }
511
512     }
513
514     my $listref_or_error =
515       $self->calculate_taxes( \@cust_bill_pkg, $taxlisthash{$pass}, $invoice_time);
516
517     unless ( ref( $listref_or_error ) ) {
518       $dbh->rollback if $oldAutoCommit && !$options{no_commit};
519       return $listref_or_error;
520     }
521
522     foreach my $taxline ( @$listref_or_error ) {
523       ${ $total_setup{$pass} } =
524         sprintf('%.2f', ${ $total_setup{$pass} } + $taxline->setup );
525       push @cust_bill_pkg, $taxline;
526     }
527
528     #add tax adjustments
529     warn "adding tax adjustments...\n" if $DEBUG > 2;
530     foreach my $cust_tax_adjustment (
531       qsearch('cust_tax_adjustment', { 'custnum'    => $self->custnum,
532                                        'billpkgnum' => '',
533                                      }
534              )
535     ) {
536
537       my $tax = sprintf('%.2f', $cust_tax_adjustment->amount );
538
539       my $itemdesc = $cust_tax_adjustment->taxname;
540       $itemdesc = '' if $itemdesc eq 'Tax';
541
542       push @cust_bill_pkg, new FS::cust_bill_pkg {
543         'pkgnum'      => 0,
544         'setup'       => $tax,
545         'recur'       => 0,
546         'sdate'       => '',
547         'edate'       => '',
548         'itemdesc'    => $itemdesc,
549         'itemcomment' => $cust_tax_adjustment->comment,
550         'cust_tax_adjustment' => $cust_tax_adjustment,
551         #'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
552       };
553
554     }
555
556     my $charged = sprintf('%.2f', ${ $total_setup{$pass} } + ${ $total_recur{$pass} } );
557
558     my @cust_bill = $self->cust_bill;
559     my $balance = $self->balance;
560     my $previous_balance = scalar(@cust_bill)
561                              ? ( $cust_bill[$#cust_bill]->billing_balance || 0 )
562                              : 0;
563
564     $previous_balance += $cust_bill[$#cust_bill]->charged
565       if scalar(@cust_bill);
566     #my $balance_adjustments =
567     #  sprintf('%.2f', $balance - $prior_prior_balance - $prior_charged);
568
569     warn "creating the new invoice\n" if $DEBUG;
570     #create the new invoice
571     my $cust_bill = new FS::cust_bill ( {
572       'custnum'             => $self->custnum,
573       '_date'               => $invoice_time,
574       'charged'             => $charged,
575       'billing_balance'     => $balance,
576       'previous_balance'    => $previous_balance,
577       'invoice_terms'       => $options{'invoice_terms'},
578       'cust_bill_pkg'       => \@cust_bill_pkg,
579     } );
580     $error = $cust_bill->insert unless $options{no_commit};
581     if ( $error ) {
582       $dbh->rollback if $oldAutoCommit && !$options{no_commit};
583       return "can't create invoice for customer #". $self->custnum. ": $error";
584     }
585     push @{$options{return_bill}}, $cust_bill if $options{return_bill};
586
587   } #foreach my $pass ( keys %cust_bill_pkg )
588
589   foreach my $hook ( @precommit_hooks ) { 
590     eval {
591       &{$hook}; #($self) ?
592     } unless $options{no_commit};
593     if ( $@ ) {
594       $dbh->rollback if $oldAutoCommit && !$options{no_commit};
595       return "$@ running precommit hook $hook\n";
596     }
597   }
598   
599   $dbh->commit or die $dbh->errstr if $oldAutoCommit && !$options{no_commit};
600
601   ''; #no error
602 }
603
604 #discard bundled packages of 0 value
605 sub _omit_zero_value_bundles {
606   my @in = @_;
607
608   my @cust_bill_pkg = ();
609   my @cust_bill_pkg_bundle = ();
610   my $sum = 0;
611   my $discount_show_always = 0;
612
613   foreach my $cust_bill_pkg ( @in ) {
614
615     $discount_show_always = ($cust_bill_pkg->get('discounts')
616                                 && scalar(@{$cust_bill_pkg->get('discounts')})
617                                 && $conf->exists('discount-show-always'));
618
619     warn "  pkgnum ". $cust_bill_pkg->pkgnum. " sum $sum, ".
620          "setup_show_zero ". $cust_bill_pkg->setup_show_zero.
621          "recur_show_zero ". $cust_bill_pkg->recur_show_zero. "\n"
622       if $DEBUG > 0;
623
624     if (scalar(@cust_bill_pkg_bundle) && !$cust_bill_pkg->pkgpart_override) {
625       push @cust_bill_pkg, @cust_bill_pkg_bundle 
626         if $sum > 0
627         || ($sum == 0 && (    $discount_show_always
628                            || grep {$_->recur_show_zero || $_->setup_show_zero}
629                                    @cust_bill_pkg_bundle
630                          )
631            );
632       @cust_bill_pkg_bundle = ();
633       $sum = 0;
634     }
635
636     $sum += $cust_bill_pkg->setup + $cust_bill_pkg->recur;
637     push @cust_bill_pkg_bundle, $cust_bill_pkg;
638
639   }
640
641   push @cust_bill_pkg, @cust_bill_pkg_bundle
642     if $sum > 0
643     || ($sum == 0 && (    $discount_show_always
644                        || grep {$_->recur_show_zero || $_->setup_show_zero}
645                                @cust_bill_pkg_bundle
646                      )
647        );
648
649   warn "  _omit_zero_value_bundles: ". scalar(@in).
650        '->'. scalar(@cust_bill_pkg). "\n" #. Dumper(@cust_bill_pkg). "\n"
651     if $DEBUG > 2;
652
653   (@cust_bill_pkg);
654
655 }
656
657 =item calculate_taxes LINEITEMREF TAXHASHREF INVOICE_TIME
658
659 This is a weird one.  Perhaps it should not even be exposed.
660
661 Generates tax line items (see L<FS::cust_bill_pkg>) for this customer.
662 Usually used internally by bill method B<bill>.
663
664 If there is an error, returns the error, otherwise returns reference to a
665 list of line items suitable for insertion.
666
667 =over 4
668
669 =item LINEITEMREF
670
671 An array ref of the line items being billed.
672
673 =item TAXHASHREF
674
675 A strange beast.  The keys to this hash are internal identifiers consisting
676 of the name of the tax object type, a space, and its unique identifier ( e.g.
677  'cust_main_county 23' ).  The values of the hash are listrefs.  The first
678 item in the list is the tax object.  The remaining items are either line
679 items or floating point values (currency amounts).
680
681 The taxes are calculated on this entity.  Calculated exemption records are
682 transferred to the LINEITEMREF items on the assumption that they are related.
683
684 Read the source.
685
686 =item INVOICE_TIME
687
688 This specifies the date appearing on the associated invoice.  Some
689 jurisdictions (i.e. Texas) have tax exemptions which are date sensitive.
690
691 =back
692
693 =cut
694
695 sub calculate_taxes {
696   my ($self, $cust_bill_pkg, $taxlisthash, $invoice_time) = @_;
697
698   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
699
700   warn "$me calculate_taxes\n"
701        #.Dumper($self, $cust_bill_pkg, $taxlisthash, $invoice_time). "\n"
702     if $DEBUG > 2;
703
704   my @tax_line_items = ();
705
706   # keys are tax names (as printed on invoices / itemdesc )
707   # values are listrefs of taxlisthash keys (internal identifiers)
708   my %taxname = ();
709
710   # keys are taxlisthash keys (internal identifiers)
711   # values are (cumulative) amounts
712   my %tax = ();
713
714   # keys are taxlisthash keys (internal identifiers)
715   # values are listrefs of cust_bill_pkg_tax_location hashrefs
716   my %tax_location = ();
717
718   # keys are taxlisthash keys (internal identifiers)
719   # values are listrefs of cust_bill_pkg_tax_rate_location hashrefs
720   my %tax_rate_location = ();
721
722   foreach my $tax ( keys %$taxlisthash ) {
723     my $tax_object = shift @{ $taxlisthash->{$tax} };
724     warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
725     warn " ". join('/', @{ $taxlisthash->{$tax} } ). "\n" if $DEBUG > 2;
726     my $hashref_or_error =
727       $tax_object->taxline( $taxlisthash->{$tax},
728                             'custnum'      => $self->custnum,
729                             'invoice_time' => $invoice_time
730                           );
731     return $hashref_or_error unless ref($hashref_or_error);
732
733     unshift @{ $taxlisthash->{$tax} }, $tax_object;
734
735     my $name   = $hashref_or_error->{'name'};
736     my $amount = $hashref_or_error->{'amount'};
737
738     #warn "adding $amount as $name\n";
739     $taxname{ $name } ||= [];
740     push @{ $taxname{ $name } }, $tax;
741
742     $tax{ $tax } += $amount;
743
744     $tax_location{ $tax } ||= [];
745     if ( $tax_object->get('pkgnum') || $tax_object->get('locationnum') ) {
746       push @{ $tax_location{ $tax }  },
747         {
748           'taxnum'      => $tax_object->taxnum, 
749           'taxtype'     => ref($tax_object),
750           'pkgnum'      => $tax_object->get('pkgnum'),
751           'locationnum' => $tax_object->get('locationnum'),
752           'amount'      => sprintf('%.2f', $amount ),
753         };
754     }
755
756     $tax_rate_location{ $tax } ||= [];
757     if ( ref($tax_object) eq 'FS::tax_rate' ) {
758       my $taxratelocationnum =
759         $tax_object->tax_rate_location->taxratelocationnum;
760       push @{ $tax_rate_location{ $tax }  },
761         {
762           'taxnum'             => $tax_object->taxnum, 
763           'taxtype'            => ref($tax_object),
764           'amount'             => sprintf('%.2f', $amount ),
765           'locationtaxid'      => $tax_object->location,
766           'taxratelocationnum' => $taxratelocationnum,
767         };
768     }
769
770   }
771
772   #move the cust_tax_exempt_pkg records to the cust_bill_pkgs we will commit
773   my %packagemap = map { $_->pkgnum => $_ } @$cust_bill_pkg;
774   foreach my $tax ( keys %$taxlisthash ) {
775     foreach ( @{ $taxlisthash->{$tax} }[1 ... scalar(@{ $taxlisthash->{$tax} })] ) {
776       next unless ref($_) eq 'FS::cust_bill_pkg';
777      
778       my @cust_tax_exempt_pkg = splice( @{ $_->_cust_tax_exempt_pkg } );
779
780       next unless @cust_tax_exempt_pkg; #just avoiding the prob when irrelevant?
781       die "can't distribute tax exemptions: no line item for ".  Dumper($_).
782           " in packagemap ". join(',', sort {$a<=>$b} keys %packagemap). "\n"
783         unless $packagemap{$_->pkgnum};
784
785       push @{ $packagemap{$_->pkgnum}->_cust_tax_exempt_pkg },
786            @cust_tax_exempt_pkg;
787     }
788   }
789
790   #consolidate and create tax line items
791   warn "consolidating and generating...\n" if $DEBUG > 2;
792   foreach my $taxname ( keys %taxname ) {
793     my $tax = 0;
794     my %seen = ();
795     my @cust_bill_pkg_tax_location = ();
796     my @cust_bill_pkg_tax_rate_location = ();
797     warn "adding $taxname\n" if $DEBUG > 1;
798     foreach my $taxitem ( @{ $taxname{$taxname} } ) {
799       next if $seen{$taxitem}++;
800       warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
801       $tax += $tax{$taxitem};
802       push @cust_bill_pkg_tax_location,
803         map { new FS::cust_bill_pkg_tax_location $_ }
804             @{ $tax_location{ $taxitem } };
805       push @cust_bill_pkg_tax_rate_location,
806         map { new FS::cust_bill_pkg_tax_rate_location $_ }
807             @{ $tax_rate_location{ $taxitem } };
808     }
809     next unless $tax;
810
811     $tax = sprintf('%.2f', $tax );
812   
813     my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname,
814                                                    'disabled'     => '',
815                                                  },
816                                );
817
818     my @display = ();
819     if ( $pkg_category and
820          $conf->config('invoice_latexsummary') ||
821          $conf->config('invoice_htmlsummary')
822        )
823     {
824
825       my %hash = (  'section' => $pkg_category->categoryname );
826       push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
827
828     }
829
830     push @tax_line_items, new FS::cust_bill_pkg {
831       'pkgnum'   => 0,
832       'setup'    => $tax,
833       'recur'    => 0,
834       'sdate'    => '',
835       'edate'    => '',
836       'itemdesc' => $taxname,
837       'display'  => \@display,
838       'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
839       'cust_bill_pkg_tax_rate_location' => \@cust_bill_pkg_tax_rate_location,
840     };
841
842   }
843
844   \@tax_line_items;
845 }
846
847 sub _make_lines {
848   my ($self, %params) = @_;
849
850   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
851
852   my $part_pkg = $params{part_pkg} or die "no part_pkg specified";
853   my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
854   my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
855   my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
856   my $total_setup = $params{setup} or die "no setup accumulator specified";
857   my $total_recur = $params{recur} or die "no recur accumulator specified";
858   my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
859   my $time = $params{'time'} or die "no time specified";
860   my (%options) = %{$params{options}};
861
862   if ( $part_pkg->freq ne '1' and ($options{'freq_override'} || 0) > 0 ) {
863     # this should never happen
864     die 'freq_override billing attempted on non-monthly package '.
865       $cust_pkg->pkgnum;
866   }
867
868   my $dbh = dbh;
869   my $real_pkgpart = $params{real_pkgpart};
870   my %hash = $cust_pkg->hash;
871   my $old_cust_pkg = new FS::cust_pkg \%hash;
872
873   my @details = ();
874   my $lineitems = 0;
875
876   $cust_pkg->pkgpart($part_pkg->pkgpart);
877
878   ###
879   # bill setup
880   ###
881
882   my $setup = 0;
883   my $unitsetup = 0;
884   my @setup_discounts = ();
885   my %setup_param = ( 'discounts' => \@setup_discounts );
886   if (     ! $options{recurring_only}
887        and ! $options{cancel}
888        and ( $options{'resetup'}
889              || ( ! $cust_pkg->setup
890                   && ( ! $cust_pkg->start_date
891                        || $cust_pkg->start_date <= day_end($time)
892                      )
893                   && ( ! $conf->exists('disable_setup_suspended_pkgs')
894                        || ( $conf->exists('disable_setup_suspended_pkgs') &&
895                             ! $cust_pkg->getfield('susp')
896                           )
897                      )
898                 )
899            )
900      )
901   {
902     
903     warn "    bill setup\n" if $DEBUG > 1;
904
905     unless ( $cust_pkg->waive_setup ) {
906         $lineitems++;
907
908         $setup = eval { $cust_pkg->calc_setup( $time, \@details, \%setup_param ) };
909         return "$@ running calc_setup for $cust_pkg\n"
910           if $@;
911
912         $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
913     }
914
915     $cust_pkg->setfield('setup', $time)
916       unless $cust_pkg->setup;
917           #do need it, but it won't get written to the db
918           #|| $cust_pkg->pkgpart != $real_pkgpart;
919
920     $cust_pkg->setfield('start_date', '')
921       if $cust_pkg->start_date;
922
923   }
924
925   ###
926   # bill recurring fee
927   ### 
928
929   #XXX unit stuff here too
930   my $recur = 0;
931   my $unitrecur = 0;
932   my @recur_discounts = ();
933   my $sdate;
934   if (     ! $cust_pkg->start_date
935        and ( ! $cust_pkg->susp || $part_pkg->option('suspend_bill', 1) )
936        and
937             ( $part_pkg->freq ne '0' && ( $cust_pkg->bill || 0 ) <= day_end($time) )
938          || ( $part_pkg->plan eq 'voip_cdr'
939                && $part_pkg->option('bill_every_call')
940             )
941          || $options{cancel}
942   ) {
943
944     # XXX should this be a package event?  probably.  events are called
945     # at collection time at the moment, though...
946     $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
947       if $part_pkg->can('reset_usage') && !$options{'no_usage_reset'};
948       #don't want to reset usage just cause we want a line item??
949       #&& $part_pkg->pkgpart == $real_pkgpart;
950
951     warn "    bill recur\n" if $DEBUG > 1;
952     $lineitems++;
953
954     # XXX shared with $recur_prog
955     $sdate = ( $options{cancel} ? $cust_pkg->last_bill : $cust_pkg->bill )
956              || $cust_pkg->setup
957              || $time;
958
959     #over two params!  lets at least switch to a hashref for the rest...
960     my $increment_next_bill = ( $part_pkg->freq ne '0'
961                                 && ( $cust_pkg->getfield('bill') || 0 ) <= day_end($time)
962                                 && !$options{cancel}
963                               );
964     my %param = ( %setup_param,
965                   'precommit_hooks'     => $precommit_hooks,
966                   'increment_next_bill' => $increment_next_bill,
967                   'discounts'           => \@recur_discounts,
968                   'real_pkgpart'        => $real_pkgpart,
969                   'freq_override'       => $options{freq_override} || '',
970                   'setup_fee'           => 0,
971                 );
972
973     my $method = $options{cancel} ? 'calc_cancel' : 'calc_recur';
974
975     # There may be some part_pkg for which this is wrong.  Only those
976     # which can_discount are supported.
977     # (the UI should prevent adding discounts to these at the moment)
978
979     warn "calling $method on cust_pkg ". $cust_pkg->pkgnum.
980          " for pkgpart ". $cust_pkg->pkgpart.
981          " with params ". join(' / ', map "$_=>$param{$_}", keys %param). "\n"
982       if $DEBUG > 2;
983            
984     $recur = eval { $cust_pkg->$method( \$sdate, \@details, \%param ) };
985     return "$@ running $method for $cust_pkg\n"
986       if ( $@ );
987
988     if ( $increment_next_bill ) {
989
990       my $next_bill = $part_pkg->add_freq($sdate, $options{freq_override} || 0);
991       return "unparsable frequency: ". $part_pkg->freq
992         if $next_bill == -1;
993   
994       #pro-rating magic - if $recur_prog fiddled $sdate, want to use that
995       # only for figuring next bill date, nothing else, so, reset $sdate again
996       # here
997       $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
998       #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
999       $cust_pkg->last_bill($sdate);
1000
1001       $cust_pkg->setfield('bill', $next_bill );
1002
1003     }
1004
1005     if ( $param{'setup_fee'} ) {
1006       # Add an additional setup fee at the billing stage.
1007       # Used for prorate_defer_bill.
1008       $setup += $param{'setup_fee'};
1009       $unitsetup += $param{'setup_fee'};
1010       $lineitems++;
1011     }
1012
1013     if ( defined $param{'discount_left_setup'} ) {
1014         foreach my $discount_setup ( values %{$param{'discount_left_setup'}} ) {
1015             $setup -= $discount_setup;
1016         }
1017     }
1018
1019   }
1020
1021   warn "\$setup is undefined" unless defined($setup);
1022   warn "\$recur is undefined" unless defined($recur);
1023   warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1024   
1025   ###
1026   # If there's line items, create em cust_bill_pkg records
1027   # If $cust_pkg has been modified, update it (if we're a real pkgpart)
1028   ###
1029
1030   if ( $lineitems ) {
1031
1032     if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
1033       # hmm.. and if just the options are modified in some weird price plan?
1034   
1035       warn "  package ". $cust_pkg->pkgnum. " modified; updating\n"
1036         if $DEBUG >1;
1037   
1038       my $error = $cust_pkg->replace( $old_cust_pkg,
1039                                       'depend_jobnum'=>$options{depend_jobnum},
1040                                       'options' => { $cust_pkg->options },
1041                                     )
1042         unless $options{no_commit};
1043       return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error"
1044         if $error; #just in case
1045     }
1046   
1047     $setup = sprintf( "%.2f", $setup );
1048     $recur = sprintf( "%.2f", $recur );
1049     if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
1050       return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1051     }
1052     if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
1053       return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1054     }
1055
1056     my $discount_show_always = $conf->exists('discount-show-always')
1057                                && (    ($setup == 0 && scalar(@setup_discounts))
1058                                     || ($recur == 0 && scalar(@recur_discounts))
1059                                   );
1060
1061     if (    $setup != 0
1062          || $recur != 0
1063          || (!$part_pkg->hidden && $options{has_hidden}) #include some $0 lines
1064          || $discount_show_always
1065          || ($setup == 0 && $cust_pkg->_X_show_zero('setup'))
1066          || ($recur == 0 && $cust_pkg->_X_show_zero('recur'))
1067        ) 
1068     {
1069
1070       warn "    charges (setup=$setup, recur=$recur); adding line items\n"
1071         if $DEBUG > 1;
1072
1073       my @cust_pkg_detail = map { $_->detail } $cust_pkg->cust_pkg_detail('I');
1074       if ( $DEBUG > 1 ) {
1075         warn "      adding customer package invoice detail: $_\n"
1076           foreach @cust_pkg_detail;
1077       }
1078       push @details, @cust_pkg_detail;
1079
1080       my $cust_bill_pkg = new FS::cust_bill_pkg {
1081         'pkgnum'    => $cust_pkg->pkgnum,
1082         'setup'     => $setup,
1083         'unitsetup' => $unitsetup,
1084         'recur'     => $recur,
1085         'unitrecur' => $unitrecur,
1086         'quantity'  => $cust_pkg->quantity,
1087         'details'   => \@details,
1088         'discounts' => [ @setup_discounts, @recur_discounts ],
1089         'hidden'    => $part_pkg->hidden,
1090         'freq'      => $part_pkg->freq,
1091       };
1092
1093       if ( $part_pkg->recur_temporality eq 'preceding' ) {
1094         $cust_bill_pkg->sdate( $hash{last_bill} );
1095         $cust_bill_pkg->edate( $sdate - 86399   ); #60s*60m*24h-1
1096         $cust_bill_pkg->edate( $time ) if $options{cancel};
1097       } else { #if ( $part_pkg->recur_temporality eq 'upcoming' ) {
1098         $cust_bill_pkg->sdate( $sdate );
1099         $cust_bill_pkg->edate( $cust_pkg->bill );
1100         #$cust_bill_pkg->edate( $time ) if $options{cancel};
1101       }
1102
1103       $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
1104         unless $part_pkg->pkgpart == $real_pkgpart;
1105
1106       $$total_setup += $setup;
1107       $$total_recur += $recur;
1108
1109       ###
1110       # handle taxes
1111       ###
1112
1113       unless ( $discount_show_always ) {
1114           my $error = 
1115             $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart, \%options);
1116           return $error if $error;
1117       }
1118
1119       push @$cust_bill_pkgs, $cust_bill_pkg;
1120
1121     } #if $setup != 0 || $recur != 0
1122       
1123   } #if $line_items
1124
1125   '';
1126
1127 }
1128
1129 sub _handle_taxes {
1130   my $self = shift;
1131   my $part_pkg = shift;
1132   my $taxlisthash = shift;
1133   my $cust_bill_pkg = shift;
1134   my $cust_pkg = shift;
1135   my $invoice_time = shift;
1136   my $real_pkgpart = shift;
1137   my $options = shift;
1138
1139   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1140
1141   my %cust_bill_pkg = ();
1142   my %taxes = ();
1143     
1144   my @classes;
1145   #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
1146   push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
1147   push @classes, 'setup' if ($cust_bill_pkg->setup && !$options->{cancel});
1148   push @classes, 'recur' if ($cust_bill_pkg->recur && !$options->{cancel});
1149
1150   if ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
1151
1152     if ( $conf->exists('enable_taxproducts')
1153          && ( scalar($part_pkg->part_pkg_taxoverride)
1154               || $part_pkg->has_taxproduct
1155             )
1156        )
1157     {
1158
1159       foreach my $class (@classes) {
1160         my $err_or_ref = $self->_gather_taxes( $part_pkg, $class, $cust_pkg );
1161         return $err_or_ref unless ref($err_or_ref);
1162         $taxes{$class} = $err_or_ref;
1163       }
1164
1165       unless (exists $taxes{''}) {
1166         my $err_or_ref = $self->_gather_taxes( $part_pkg, '', $cust_pkg );
1167         return $err_or_ref unless ref($err_or_ref);
1168         $taxes{''} = $err_or_ref;
1169       }
1170
1171     } else {
1172
1173       my @loc_keys = qw( district city county state country );
1174       my %taxhash;
1175       if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1176         my $cust_location = $cust_pkg->cust_location;
1177         %taxhash = map { $_ => $cust_location->$_()    } @loc_keys;
1178       } else {
1179         my $prefix = 
1180           ( $conf->exists('tax-ship_address') && length($self->ship_last) )
1181           ? 'ship_'
1182           : '';
1183         %taxhash = map { $_ => $self->get("$prefix$_") } @loc_keys;
1184       }
1185
1186       $taxhash{'taxclass'} = $part_pkg->taxclass;
1187
1188       my @taxes = ();
1189       my %taxhash_elim = %taxhash;
1190       my @elim = qw( district city county state );
1191       do { 
1192
1193         #first try a match with taxclass
1194         @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
1195
1196         if ( !scalar(@taxes) && $taxhash_elim{'taxclass'} ) {
1197           #then try a match without taxclass
1198           my %no_taxclass = %taxhash_elim;
1199           $no_taxclass{ 'taxclass' } = '';
1200           @taxes = qsearch( 'cust_main_county', \%no_taxclass );
1201         }
1202
1203         $taxhash_elim{ shift(@elim) } = '';
1204
1205       } while ( !scalar(@taxes) && scalar(@elim) );
1206
1207       @taxes = grep { ! $_->taxname or ! $self->tax_exemption($_->taxname) }
1208                     @taxes
1209         if $self->cust_main_exemption; #just to be safe
1210
1211       if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1212         foreach (@taxes) {
1213           $_->set('pkgnum',      $cust_pkg->pkgnum );
1214           $_->set('locationnum', $cust_pkg->locationnum );
1215         }
1216       }
1217
1218       $taxes{''} = [ @taxes ];
1219       $taxes{'setup'} = [ @taxes ];
1220       $taxes{'recur'} = [ @taxes ];
1221       $taxes{$_} = [ @taxes ] foreach (@classes);
1222
1223       # # maybe eliminate this entirely, along with all the 0% records
1224       # unless ( @taxes ) {
1225       #   return
1226       #     "fatal: can't find tax rate for state/county/country/taxclass ".
1227       #     join('/', map $taxhash{$_}, qw(state county country taxclass) );
1228       # }
1229
1230     } #if $conf->exists('enable_taxproducts') ...
1231
1232   }
1233
1234   #what's this doing in the middle of _handle_taxes?  probably should split
1235   #this into three parts above in _make_lines
1236   $cust_bill_pkg->set_display(   part_pkg     => $part_pkg,
1237                                  real_pkgpart => $real_pkgpart,
1238                              );
1239
1240   my %tax_cust_bill_pkg = $cust_bill_pkg->disintegrate;
1241   foreach my $key (keys %tax_cust_bill_pkg) {
1242     my @taxes = @{ $taxes{$key} || [] };
1243     my $tax_cust_bill_pkg = $tax_cust_bill_pkg{$key};
1244
1245     my %localtaxlisthash = ();
1246     foreach my $tax ( @taxes ) {
1247
1248       my $taxname = ref( $tax ). ' '. $tax->taxnum;
1249 #      $taxname .= ' pkgnum'. $cust_pkg->pkgnum.
1250 #                  ' locationnum'. $cust_pkg->locationnum
1251 #        if $conf->exists('tax-pkg_address') && $cust_pkg->locationnum;
1252
1253       $taxlisthash->{ $taxname } ||= [ $tax ];
1254       push @{ $taxlisthash->{ $taxname  } }, $tax_cust_bill_pkg;
1255
1256       $localtaxlisthash{ $taxname } ||= [ $tax ];
1257       push @{ $localtaxlisthash{ $taxname  } }, $tax_cust_bill_pkg;
1258
1259     }
1260
1261     warn "finding taxed taxes...\n" if $DEBUG > 2;
1262     foreach my $tax ( keys %localtaxlisthash ) {
1263       my $tax_object = shift @{ $localtaxlisthash{$tax} };
1264       warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
1265         if $DEBUG > 2;
1266       next unless $tax_object->can('tax_on_tax');
1267
1268       foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
1269         my $totname = ref( $tot ). ' '. $tot->taxnum;
1270
1271         warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
1272           if $DEBUG > 2;
1273         next unless exists( $localtaxlisthash{ $totname } ); # only increase
1274                                                              # existing taxes
1275         warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
1276         my $hashref_or_error = 
1277           $tax_object->taxline( $localtaxlisthash{$tax},
1278                                 'custnum'      => $self->custnum,
1279                                 'invoice_time' => $invoice_time,
1280                               );
1281         return $hashref_or_error
1282           unless ref($hashref_or_error);
1283         
1284         $taxlisthash->{ $totname } ||= [ $tot ];
1285         push @{ $taxlisthash->{ $totname  } }, $hashref_or_error->{amount};
1286
1287       }
1288     }
1289
1290   }
1291
1292   '';
1293 }
1294
1295 sub _gather_taxes {
1296   my $self = shift;
1297   my $part_pkg = shift;
1298   my $class = shift;
1299   my $cust_pkg = shift;
1300
1301   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1302
1303   my $geocode;
1304   if ( $cust_pkg->locationnum && $conf->exists('tax-pkg_address') ) {
1305     $geocode = $cust_pkg->cust_location->geocode('cch');
1306   } else {
1307     $geocode = $self->geocode('cch');
1308   }
1309
1310   my @taxes = ();
1311
1312   my @taxclassnums = map { $_->taxclassnum }
1313                      $part_pkg->part_pkg_taxoverride($class);
1314
1315   unless (@taxclassnums) {
1316     @taxclassnums = map { $_->taxclassnum }
1317                     grep { $_->taxable eq 'Y' }
1318                     $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
1319   }
1320   warn "Found taxclassnum values of ". join(',', @taxclassnums)
1321     if $DEBUG;
1322
1323   my $extra_sql =
1324     "AND (".
1325     join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
1326
1327   @taxes = qsearch({ 'table' => 'tax_rate',
1328                      'hashref' => { 'geocode' => $geocode, },
1329                      'extra_sql' => $extra_sql,
1330                   })
1331     if scalar(@taxclassnums);
1332
1333   warn "Found taxes ".
1334        join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n" 
1335    if $DEBUG;
1336
1337   [ @taxes ];
1338
1339 }
1340
1341 =item collect [ HASHREF | OPTION => VALUE ... ]
1342
1343 (Attempt to) collect money for this customer's outstanding invoices (see
1344 L<FS::cust_bill>).  Usually used after the bill method.
1345
1346 Actions are now triggered by billing events; see L<FS::part_event> and the
1347 billing events web interface.  Old-style invoice events (see
1348 L<FS::part_bill_event>) have been deprecated.
1349
1350 If there is an error, returns the error, otherwise returns false.
1351
1352 Options are passed as name-value pairs.
1353
1354 Currently available options are:
1355
1356 =over 4
1357
1358 =item invoice_time
1359
1360 Use this time when deciding when to print invoices and late notices on those invoices.  The default is now.  It is specified as a UNIX timestamp; see L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion functions.
1361
1362 =item retry
1363
1364 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1365
1366 =item check_freq
1367
1368 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1369
1370 =item quiet
1371
1372 set true to surpress email card/ACH decline notices.
1373
1374 =item debug
1375
1376 Debugging level.  Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
1377
1378 =back
1379
1380 # =item payby
1381 #
1382 # allows for one time override of normal customer billing method
1383
1384 =cut
1385
1386 sub collect {
1387   my( $self, %options ) = @_;
1388
1389   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1390
1391   my $invoice_time = $options{'invoice_time'} || time;
1392
1393   #put below somehow?
1394   local $SIG{HUP} = 'IGNORE';
1395   local $SIG{INT} = 'IGNORE';
1396   local $SIG{QUIT} = 'IGNORE';
1397   local $SIG{TERM} = 'IGNORE';
1398   local $SIG{TSTP} = 'IGNORE';
1399   local $SIG{PIPE} = 'IGNORE';
1400
1401   my $oldAutoCommit = $FS::UID::AutoCommit;
1402   local $FS::UID::AutoCommit = 0;
1403   my $dbh = dbh;
1404
1405   $self->select_for_update; #mutex
1406
1407   if ( $DEBUG ) {
1408     my $balance = $self->balance;
1409     warn "$me collect customer ". $self->custnum. ": balance $balance\n"
1410   }
1411
1412   if ( exists($options{'retry_card'}) ) {
1413     carp 'retry_card option passed to collect is deprecated; use retry';
1414     $options{'retry'} ||= $options{'retry_card'};
1415   }
1416   if ( exists($options{'retry'}) && $options{'retry'} ) {
1417     my $error = $self->retry_realtime;
1418     if ( $error ) {
1419       $dbh->rollback if $oldAutoCommit;
1420       return $error;
1421     }
1422   }
1423
1424   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1425
1426   #never want to roll back an event just because it returned an error
1427   local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1428
1429   $self->do_cust_event(
1430     'debug'      => ( $options{'debug'} || 0 ),
1431     'time'       => $invoice_time,
1432     'check_freq' => $options{'check_freq'},
1433     'stage'      => 'collect',
1434   );
1435
1436 }
1437
1438 =item retry_realtime
1439
1440 Schedules realtime / batch  credit card / electronic check / LEC billing
1441 events for for retry.  Useful if card information has changed or manual
1442 retry is desired.  The 'collect' method must be called to actually retry
1443 the transaction.
1444
1445 Implementation details: For either this customer, or for each of this
1446 customer's open invoices, changes the status of the first "done" (with
1447 statustext error) realtime processing event to "failed".
1448
1449 =cut
1450
1451 sub retry_realtime {
1452   my $self = shift;
1453
1454   local $SIG{HUP} = 'IGNORE';
1455   local $SIG{INT} = 'IGNORE';
1456   local $SIG{QUIT} = 'IGNORE';
1457   local $SIG{TERM} = 'IGNORE';
1458   local $SIG{TSTP} = 'IGNORE';
1459   local $SIG{PIPE} = 'IGNORE';
1460
1461   my $oldAutoCommit = $FS::UID::AutoCommit;
1462   local $FS::UID::AutoCommit = 0;
1463   my $dbh = dbh;
1464
1465   #a little false laziness w/due_cust_event (not too bad, really)
1466
1467   my $join = FS::part_event_condition->join_conditions_sql;
1468   my $order = FS::part_event_condition->order_conditions_sql;
1469   my $mine = 
1470   '( '
1471    . join ( ' OR ' , map { 
1472     my $cust_join = FS::part_event->eventtables_cust_join->{$_} || '';
1473     my $custnum = FS::part_event->eventtables_custnum->{$_};
1474     "( part_event.eventtable = " . dbh->quote($_) 
1475     . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key 
1476     . " from $_ $cust_join"
1477     . " where $custnum = " . dbh->quote( $self->custnum ) . "))" ;
1478    } FS::part_event->eventtables)
1479    . ') ';
1480
1481   #here is the agent virtualization
1482   my $agent_virt = " (    part_event.agentnum IS NULL
1483                        OR part_event.agentnum = ". $self->agentnum. ' )';
1484
1485   #XXX this shouldn't be hardcoded, actions should declare it...
1486   my @realtime_events = qw(
1487     cust_bill_realtime_card
1488     cust_bill_realtime_check
1489     cust_bill_realtime_lec
1490     cust_bill_batch
1491   );
1492
1493   my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
1494                                                   @realtime_events
1495                                      ).
1496                           ' ) ';
1497
1498   my @cust_event = qsearchs({
1499     'table'     => 'cust_event',
1500     'select'    => 'cust_event.*',
1501     'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
1502     'hashref'   => { 'status' => 'done' },
1503     'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
1504                    " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
1505   });
1506
1507   my %seen_invnum = ();
1508   foreach my $cust_event (@cust_event) {
1509
1510     #max one for the customer, one for each open invoice
1511     my $cust_X = $cust_event->cust_X;
1512     next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
1513                           ? $cust_X->invnum
1514                           : 0
1515                         }++
1516          or $cust_event->part_event->eventtable eq 'cust_bill'
1517             && ! $cust_X->owed;
1518
1519     my $error = $cust_event->retry;
1520     if ( $error ) {
1521       $dbh->rollback if $oldAutoCommit;
1522       return "error scheduling event for retry: $error";
1523     }
1524
1525   }
1526
1527   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1528   '';
1529
1530 }
1531
1532 =item do_cust_event [ HASHREF | OPTION => VALUE ... ]
1533
1534 Runs billing events; see L<FS::part_event> and the billing events web
1535 interface.
1536
1537 If there is an error, returns the error, otherwise returns false.
1538
1539 Options are passed as name-value pairs.
1540
1541 Currently available options are:
1542
1543 =over 4
1544
1545 =item time
1546
1547 Use this time when deciding when to print invoices and late notices on those invoices.  The default is now.  It is specified as a UNIX timestamp; see L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion functions.
1548
1549 =item check_freq
1550
1551 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1552
1553 =item stage
1554
1555 "collect" (the default) or "pre-bill"
1556
1557 =item quiet
1558  
1559 set true to surpress email card/ACH decline notices.
1560
1561 =item debug
1562
1563 Debugging level.  Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
1564
1565 =back
1566 =cut
1567
1568 # =item payby
1569 #
1570 # allows for one time override of normal customer billing method
1571
1572 # =item retry
1573 #
1574 # Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1575
1576 sub do_cust_event {
1577   my( $self, %options ) = @_;
1578
1579   local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1580
1581   my $time = $options{'time'} || time;
1582
1583   #put below somehow?
1584   local $SIG{HUP} = 'IGNORE';
1585   local $SIG{INT} = 'IGNORE';
1586   local $SIG{QUIT} = 'IGNORE';
1587   local $SIG{TERM} = 'IGNORE';
1588   local $SIG{TSTP} = 'IGNORE';
1589   local $SIG{PIPE} = 'IGNORE';
1590
1591   my $oldAutoCommit = $FS::UID::AutoCommit;
1592   local $FS::UID::AutoCommit = 0;
1593   my $dbh = dbh;
1594
1595   $self->select_for_update; #mutex
1596
1597   if ( $DEBUG ) {
1598     my $balance = $self->balance;
1599     warn "$me do_cust_event customer ". $self->custnum. ": balance $balance\n"
1600   }
1601
1602 #  if ( exists($options{'retry_card'}) ) {
1603 #    carp 'retry_card option passed to collect is deprecated; use retry';
1604 #    $options{'retry'} ||= $options{'retry_card'};
1605 #  }
1606 #  if ( exists($options{'retry'}) && $options{'retry'} ) {
1607 #    my $error = $self->retry_realtime;
1608 #    if ( $error ) {
1609 #      $dbh->rollback if $oldAutoCommit;
1610 #      return $error;
1611 #    }
1612 #  }
1613
1614   # false laziness w/pay_batch::import_results
1615
1616   my $due_cust_event = $self->due_cust_event(
1617     'debug'      => ( $options{'debug'} || 0 ),
1618     'time'       => $time,
1619     'check_freq' => $options{'check_freq'},
1620     'stage'      => ( $options{'stage'} || 'collect' ),
1621   );
1622   unless( ref($due_cust_event) ) {
1623     $dbh->rollback if $oldAutoCommit;
1624     return $due_cust_event;
1625   }
1626
1627   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1628   #never want to roll back an event just because it or a different one
1629   # returned an error
1630   local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1631
1632   foreach my $cust_event ( @$due_cust_event ) {
1633
1634     #XXX lock event
1635     
1636     #re-eval event conditions (a previous event could have changed things)
1637     unless ( $cust_event->test_conditions ) {
1638       #don't leave stray "new/locked" records around
1639       my $error = $cust_event->delete;
1640       return $error if $error;
1641       next;
1642     }
1643
1644     {
1645       local $FS::cust_main::Billing_Realtime::realtime_bop_decline_quiet = 1
1646         if $options{'quiet'};
1647       warn "  running cust_event ". $cust_event->eventnum. "\n"
1648         if $DEBUG > 1;
1649
1650       #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
1651       if ( my $error = $cust_event->do_event( 'time' => $time ) ) {
1652         #XXX wtf is this?  figure out a proper dealio with return value
1653         #from do_event
1654         return $error;
1655       }
1656     }
1657
1658   }
1659
1660   '';
1661
1662 }
1663
1664 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
1665
1666 Inserts database records for and returns an ordered listref of new events due
1667 for this customer, as FS::cust_event objects (see L<FS::cust_event>).  If no
1668 events are due, an empty listref is returned.  If there is an error, returns a
1669 scalar error message.
1670
1671 To actually run the events, call each event's test_condition method, and if
1672 still true, call the event's do_event method.
1673
1674 Options are passed as a hashref or as a list of name-value pairs.  Available
1675 options are:
1676
1677 =over 4
1678
1679 =item check_freq
1680
1681 Search only for events of this check frequency (how often events of this type are checked); currently "1d" (daily, the default) and "1m" (monthly) are recognized.
1682
1683 =item stage
1684
1685 "collect" (the default) or "pre-bill"
1686
1687 =item time
1688
1689 "Current time" for the events.
1690
1691 =item debug
1692
1693 Debugging level.  Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
1694
1695 =item eventtable
1696
1697 Only return events for the specified eventtable (by default, events of all eventtables are returned)
1698
1699 =item objects
1700
1701 Explicitly pass the objects to be tested (typically used with eventtable).
1702
1703 =item testonly
1704
1705 Set to true to return the objects, but not actually insert them into the
1706 database.
1707
1708 =back
1709
1710 =cut
1711
1712 sub due_cust_event {
1713   my $self = shift;
1714   my %opt = ref($_[0]) ? %{ $_[0] } : @_;
1715
1716   #???
1717   #my $DEBUG = $opt{'debug'}
1718   local($DEBUG) = $opt{'debug'}
1719     if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
1720   $DEBUG = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1721
1722   warn "$me due_cust_event called with options ".
1723        join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
1724     if $DEBUG;
1725
1726   $opt{'time'} ||= time;
1727
1728   local $SIG{HUP} = 'IGNORE';
1729   local $SIG{INT} = 'IGNORE';
1730   local $SIG{QUIT} = 'IGNORE';
1731   local $SIG{TERM} = 'IGNORE';
1732   local $SIG{TSTP} = 'IGNORE';
1733   local $SIG{PIPE} = 'IGNORE';
1734
1735   my $oldAutoCommit = $FS::UID::AutoCommit;
1736   local $FS::UID::AutoCommit = 0;
1737   my $dbh = dbh;
1738
1739   $self->select_for_update #mutex
1740     unless $opt{testonly};
1741
1742   ###
1743   # find possible events (initial search)
1744   ###
1745   
1746   my @cust_event = ();
1747
1748   my @eventtable = $opt{'eventtable'}
1749                      ? ( $opt{'eventtable'} )
1750                      : FS::part_event->eventtables_runorder;
1751
1752   my $check_freq = $opt{'check_freq'} || '1d';
1753
1754   foreach my $eventtable ( @eventtable ) {
1755
1756     my @objects;
1757     if ( $opt{'objects'} ) {
1758
1759       @objects = @{ $opt{'objects'} };
1760
1761     } elsif ( $eventtable eq 'cust_main' ) {
1762
1763       @objects = ( $self );
1764
1765     } else {
1766
1767       my $cm_join = " LEFT JOIN cust_main USING ( custnum )";
1768       # linkage not needed here because FS::cust_main->$eventtable will 
1769       # already supply it
1770
1771       #some false laziness w/Cron::bill bill_where
1772
1773       my $join  = FS::part_event_condition->join_conditions_sql( $eventtable);
1774       my $where = FS::part_event_condition->where_conditions_sql($eventtable,
1775         'time'=>$opt{'time'},
1776       );
1777       $where = $where ? "AND $where" : '';
1778
1779       my $are_part_event = 
1780       "EXISTS ( SELECT 1 FROM part_event $join
1781         WHERE check_freq = '$check_freq'
1782         AND eventtable = '$eventtable'
1783         AND ( disabled = '' OR disabled IS NULL )
1784         $where
1785         )
1786       ";
1787       #eofalse
1788
1789       @objects = $self->$eventtable(
1790         'addl_from' => $cm_join,
1791         'extra_sql' => " AND $are_part_event",
1792       );
1793     } # if ( !$opt{objects} and $eventtable ne 'cust_main' )
1794
1795     my @e_cust_event = ();
1796
1797     my $linkage = FS::part_event->eventtables_cust_join->{$eventtable} || '';
1798
1799     my $cross = "CROSS JOIN $eventtable $linkage";
1800     $cross .= ' LEFT JOIN cust_main USING ( custnum )'
1801       unless $eventtable eq 'cust_main';
1802
1803     foreach my $object ( @objects ) {
1804
1805       #this first search uses the condition_sql magic for optimization.
1806       #the more possible events we can eliminate in this step the better
1807
1808       my $cross_where = '';
1809       my $pkey = $object->primary_key;
1810       $cross_where = "$eventtable.$pkey = ". $object->$pkey();
1811
1812       my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
1813       my $extra_sql =
1814         FS::part_event_condition->where_conditions_sql( $eventtable,
1815                                                         'time'=>$opt{'time'}
1816                                                       );
1817       my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
1818
1819       $extra_sql = "AND $extra_sql" if $extra_sql;
1820
1821       #here is the agent virtualization
1822       $extra_sql .= " AND (    part_event.agentnum IS NULL
1823                             OR part_event.agentnum = ". $self->agentnum. ' )';
1824
1825       $extra_sql .= " $order";
1826
1827       warn "searching for events for $eventtable ". $object->$pkey. "\n"
1828         if $opt{'debug'} > 2;
1829       my @part_event = qsearch( {
1830         'debug'     => ( $opt{'debug'} > 3 ? 1 : 0 ),
1831         'select'    => 'part_event.*',
1832         'table'     => 'part_event',
1833         'addl_from' => "$cross $join",
1834         'hashref'   => { 'check_freq' => $check_freq,
1835                          'eventtable' => $eventtable,
1836                          'disabled'   => '',
1837                        },
1838         'extra_sql' => "AND $cross_where $extra_sql",
1839       } );
1840
1841       if ( $DEBUG > 2 ) {
1842         my $pkey = $object->primary_key;
1843         warn "      ". scalar(@part_event).
1844              " possible events found for $eventtable ". $object->$pkey(). "\n";
1845       }
1846
1847       push @e_cust_event, map { 
1848         $_->new_cust_event($object, 'time' => $opt{'time'}) 
1849       } @part_event;
1850
1851     }
1852
1853     warn "    ". scalar(@e_cust_event).
1854          " subtotal possible cust events found for $eventtable\n"
1855       if $DEBUG > 1;
1856
1857     push @cust_event, @e_cust_event;
1858
1859   }
1860
1861   warn "  ". scalar(@cust_event).
1862        " total possible cust events found in initial search\n"
1863     if $DEBUG; # > 1;
1864
1865
1866   ##
1867   # test stage
1868   ##
1869
1870   $opt{stage} ||= 'collect';
1871   @cust_event =
1872     grep { my $stage = $_->part_event->event_stage;
1873            $opt{stage} eq $stage or ( ! $stage && $opt{stage} eq 'collect' )
1874          }
1875          @cust_event;
1876
1877   ##
1878   # test conditions
1879   ##
1880   
1881   my %unsat = ();
1882
1883   @cust_event = grep $_->test_conditions( 'stats_hashref' => \%unsat ),
1884                      @cust_event;
1885
1886   warn "  ". scalar(@cust_event). " cust events left satisfying conditions\n"
1887     if $DEBUG; # > 1;
1888
1889   warn "    invalid conditions not eliminated with condition_sql:\n".
1890        join('', map "      $_: ".$unsat{$_}."\n", keys %unsat )
1891     if keys %unsat && $DEBUG; # > 1;
1892
1893   ##
1894   # insert
1895   ##
1896
1897   unless( $opt{testonly} ) {
1898     foreach my $cust_event ( @cust_event ) {
1899
1900       my $error = $cust_event->insert();
1901       if ( $error ) {
1902         $dbh->rollback if $oldAutoCommit;
1903         return $error;
1904       }
1905                                        
1906     }
1907   }
1908
1909   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1910
1911   ##
1912   # return
1913   ##
1914
1915   warn "  returning events: ". Dumper(@cust_event). "\n"
1916     if $DEBUG > 2;
1917
1918   \@cust_event;
1919
1920 }
1921
1922 =item apply_payments_and_credits [ OPTION => VALUE ... ]
1923
1924 Applies unapplied payments and credits.
1925
1926 In most cases, this new method should be used in place of sequential
1927 apply_payments and apply_credits methods.
1928
1929 A hash of optional arguments may be passed.  Currently "manual" is supported.
1930 If true, a payment receipt is sent instead of a statement when
1931 'payment_receipt_email' configuration option is set.
1932
1933 If there is an error, returns the error, otherwise returns false.
1934
1935 =cut
1936
1937 sub apply_payments_and_credits {
1938   my( $self, %options ) = @_;
1939
1940   local $SIG{HUP} = 'IGNORE';
1941   local $SIG{INT} = 'IGNORE';
1942   local $SIG{QUIT} = 'IGNORE';
1943   local $SIG{TERM} = 'IGNORE';
1944   local $SIG{TSTP} = 'IGNORE';
1945   local $SIG{PIPE} = 'IGNORE';
1946
1947   my $oldAutoCommit = $FS::UID::AutoCommit;
1948   local $FS::UID::AutoCommit = 0;
1949   my $dbh = dbh;
1950
1951   $self->select_for_update; #mutex
1952
1953   foreach my $cust_bill ( $self->open_cust_bill ) {
1954     my $error = $cust_bill->apply_payments_and_credits(%options);
1955     if ( $error ) {
1956       $dbh->rollback if $oldAutoCommit;
1957       return "Error applying: $error";
1958     }
1959   }
1960
1961   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1962   ''; #no error
1963
1964 }
1965
1966 =item apply_credits OPTION => VALUE ...
1967
1968 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
1969 to outstanding invoice balances in chronological order (or reverse
1970 chronological order if the I<order> option is set to B<newest>) and returns the
1971 value of any remaining unapplied credits available for refund (see
1972 L<FS::cust_refund>).
1973
1974 Dies if there is an error.
1975
1976 =cut
1977
1978 sub apply_credits {
1979   my $self = shift;
1980   my %opt = @_;
1981
1982   local $SIG{HUP} = 'IGNORE';
1983   local $SIG{INT} = 'IGNORE';
1984   local $SIG{QUIT} = 'IGNORE';
1985   local $SIG{TERM} = 'IGNORE';
1986   local $SIG{TSTP} = 'IGNORE';
1987   local $SIG{PIPE} = 'IGNORE';
1988
1989   my $oldAutoCommit = $FS::UID::AutoCommit;
1990   local $FS::UID::AutoCommit = 0;
1991   my $dbh = dbh;
1992
1993   $self->select_for_update; #mutex
1994
1995   unless ( $self->total_unapplied_credits ) {
1996     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1997     return 0;
1998   }
1999
2000   my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
2001       qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
2002
2003   my @invoices = $self->open_cust_bill;
2004   @invoices = sort { $b->_date <=> $a->_date } @invoices
2005     if defined($opt{'order'}) && $opt{'order'} eq 'newest';
2006
2007   if ( $conf->exists('pkg-balances') ) {
2008     # limit @credits to those w/ a pkgnum grepped from $self
2009     my %pkgnums = ();
2010     foreach my $i (@invoices) {
2011       foreach my $li ( $i->cust_bill_pkg ) {
2012         $pkgnums{$li->pkgnum} = 1;
2013       }
2014     }
2015     @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
2016   }
2017
2018   my $credit;
2019
2020   foreach my $cust_bill ( @invoices ) {
2021
2022     if ( !defined($credit) || $credit->credited == 0) {
2023       $credit = pop @credits or last;
2024     }
2025
2026     my $owed;
2027     if ( $conf->exists('pkg-balances') && $credit->pkgnum ) {
2028       $owed = $cust_bill->owed_pkgnum($credit->pkgnum);
2029     } else {
2030       $owed = $cust_bill->owed;
2031     }
2032     unless ( $owed > 0 ) {
2033       push @credits, $credit;
2034       next;
2035     }
2036
2037     my $amount = min( $credit->credited, $owed );
2038     
2039     my $cust_credit_bill = new FS::cust_credit_bill ( {
2040       'crednum' => $credit->crednum,
2041       'invnum'  => $cust_bill->invnum,
2042       'amount'  => $amount,
2043     } );
2044     $cust_credit_bill->pkgnum( $credit->pkgnum )
2045       if $conf->exists('pkg-balances') && $credit->pkgnum;
2046     my $error = $cust_credit_bill->insert;
2047     if ( $error ) {
2048       $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2049       die $error;
2050     }
2051     
2052     redo if ($cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
2053
2054   }
2055
2056   my $total_unapplied_credits = $self->total_unapplied_credits;
2057
2058   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2059
2060   return $total_unapplied_credits;
2061 }
2062
2063 =item apply_payments  [ OPTION => VALUE ... ]
2064
2065 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
2066 to outstanding invoice balances in chronological order.
2067
2068  #and returns the value of any remaining unapplied payments.
2069
2070 A hash of optional arguments may be passed.  Currently "manual" is supported.
2071 If true, a payment receipt is sent instead of a statement when
2072 'payment_receipt_email' configuration option is set.
2073
2074 Dies if there is an error.
2075
2076 =cut
2077
2078 sub apply_payments {
2079   my( $self, %options ) = @_;
2080
2081   local $SIG{HUP} = 'IGNORE';
2082   local $SIG{INT} = 'IGNORE';
2083   local $SIG{QUIT} = 'IGNORE';
2084   local $SIG{TERM} = 'IGNORE';
2085   local $SIG{TSTP} = 'IGNORE';
2086   local $SIG{PIPE} = 'IGNORE';
2087
2088   my $oldAutoCommit = $FS::UID::AutoCommit;
2089   local $FS::UID::AutoCommit = 0;
2090   my $dbh = dbh;
2091
2092   $self->select_for_update; #mutex
2093
2094   #return 0 unless
2095
2096   my @payments = sort { $b->_date <=> $a->_date }
2097                  grep { $_->unapplied > 0 }
2098                  $self->cust_pay;
2099
2100   my @invoices = sort { $a->_date <=> $b->_date}
2101                  grep { $_->owed > 0 }
2102                  $self->cust_bill;
2103
2104   if ( $conf->exists('pkg-balances') ) {
2105     # limit @payments to those w/ a pkgnum grepped from $self
2106     my %pkgnums = ();
2107     foreach my $i (@invoices) {
2108       foreach my $li ( $i->cust_bill_pkg ) {
2109         $pkgnums{$li->pkgnum} = 1;
2110       }
2111     }
2112     @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
2113   }
2114
2115   my $payment;
2116
2117   foreach my $cust_bill ( @invoices ) {
2118
2119     if ( !defined($payment) || $payment->unapplied == 0 ) {
2120       $payment = pop @payments or last;
2121     }
2122
2123     my $owed;
2124     if ( $conf->exists('pkg-balances') && $payment->pkgnum ) {
2125       $owed = $cust_bill->owed_pkgnum($payment->pkgnum);
2126     } else {
2127       $owed = $cust_bill->owed;
2128     }
2129     unless ( $owed > 0 ) {
2130       push @payments, $payment;
2131       next;
2132     }
2133
2134     my $amount = min( $payment->unapplied, $owed );
2135
2136     my $cbp = {
2137       'paynum' => $payment->paynum,
2138       'invnum' => $cust_bill->invnum,
2139       'amount' => $amount,
2140     };
2141     $cbp->{_date} = $payment->_date 
2142         if $options{'manual'} && $options{'backdate_application'};
2143     my $cust_bill_pay = new FS::cust_bill_pay($cbp);
2144     $cust_bill_pay->pkgnum( $payment->pkgnum )
2145       if $conf->exists('pkg-balances') && $payment->pkgnum;
2146     my $error = $cust_bill_pay->insert(%options);
2147     if ( $error ) {
2148       $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2149       die $error;
2150     }
2151
2152     redo if ( $cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
2153
2154   }
2155
2156   my $total_unapplied_payments = $self->total_unapplied_payments;
2157
2158   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2159
2160   return $total_unapplied_payments;
2161 }
2162
2163 =back
2164
2165 =head1 FLOW
2166
2167   bill_and_collect
2168
2169     cancel_expired_pkgs
2170     suspend_adjourned_pkgs
2171
2172     bill
2173       (do_cust_event pre-bill)
2174       _make_lines
2175         _handle_taxes
2176           (vendor-only) _gather_taxes
2177       _omit_zero_value_bundles
2178       calculate_taxes
2179
2180     apply_payments_and_credits
2181     collect
2182       do_cust_event
2183         due_cust_event
2184
2185 =head1 BUGS
2186
2187 =head1 SEE ALSO
2188
2189 L<FS::cust_main>, L<FS::cust_main::Billing_Realtime>
2190
2191 =cut
2192
2193 1;