1 package FS::cust_main::Billing;
4 use vars qw( $conf $DEBUG $me );
7 use List::Util qw( min );
9 use FS::Record qw( qsearch qsearchs dbdef );
10 use FS::Misc::DateTime qw( day_end );
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;
18 use FS::tax_rate_location;
19 use FS::cust_bill_pkg_tax_location;
20 use FS::cust_bill_pkg_tax_rate_location;
22 use FS::part_event_condition;
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
29 $me = '[FS::cust_main::Billing]';
31 install_callback FS::UID sub {
33 #yes, need it for stuff below (prolly should be cached)
38 FS::cust_main::Billing - Billing mixin for cust_main
44 These methods are available on FS::cust_main objects.
50 =item bill_and_collect
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,
56 By default, warns on errors and continues with the next operation (but see the
59 Options are passed as name-value pairs. Currently available options are:
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:
69 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
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.
77 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
81 If set true, re-charges setup fees.
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.
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)
95 Optional FS::queue entry to receive status updates.
99 Options are passed to the B<bill> and B<collect> methods verbatim, so all
100 options of those methods are also available.
104 sub bill_and_collect {
105 my( $self, %options ) = @_;
109 #$options{actual_time} not $options{time} because freeside-daily -d is for
110 #pre-printing invoices
112 $options{'actual_time'} ||= time;
113 my $job = $options{'job'};
115 $job->update_statustext('0,cleaning expired packages') if $job;
116 $error = $self->cancel_expired_pkgs( day_end( $options{actual_time} ) );
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; }
124 $error = $self->suspend_adjourned_pkgs( day_end( $options{actual_time} ) );
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; }
132 $error = $self->unsuspend_resumed_pkgs( day_end( $options{actual_time} ) );
134 $error = "Error resuming custnum ".$self->custnum. ": $error";
135 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
136 elsif ( $options{fatal} ) { die $error; }
137 else { warn $error; }
140 $job->update_statustext('20,billing packages') if $job;
141 $error = $self->bill( %options );
143 $error = "Error billing custnum ". $self->custnum. ": $error";
144 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
145 elsif ( $options{fatal} ) { die $error; }
146 else { warn $error; }
149 $job->update_statustext('50,applying payments and credits') if $job;
150 $error = $self->apply_payments_and_credits;
152 $error = "Error applying custnum ". $self->custnum. ": $error";
153 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
154 elsif ( $options{fatal} ) { die $error; }
155 else { warn $error; }
158 $job->update_statustext('70,running collection events') if $job;
159 unless ( $conf->exists('cancelled_cust-noevents')
160 && ! $self->num_ncancelled_pkgs
162 $error = $self->collect( %options );
164 $error = "Error collecting custnum ". $self->custnum. ": $error";
165 if ($options{fatal} && $options{fatal} eq 'return') { return $error; }
166 elsif ($options{fatal} ) { die $error; }
167 else { warn $error; }
170 $job->update_statustext('100,finished') if $job;
176 sub cancel_expired_pkgs {
177 my ( $self, $time, %options ) = @_;
179 my @cancel_pkgs = $self->ncancelled_pkgs( {
180 'extra_sql' => " AND expire IS NOT NULL AND expire > 0 AND expire <= $time "
185 foreach my $cust_pkg ( @cancel_pkgs ) {
186 my $cpr = $cust_pkg->last_cust_pkg_reason('expire');
187 my $error = $cust_pkg->cancel($cpr ? ( 'reason' => $cpr->reasonnum,
188 'reason_otaker' => $cpr->otaker
192 push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
195 join(' / ', @errors);
199 sub suspend_adjourned_pkgs {
200 my ( $self, $time, %options ) = @_;
202 my @susp_pkgs = $self->ncancelled_pkgs( {
204 " AND ( susp IS NULL OR susp = 0 )
205 AND ( ( bill IS NOT NULL AND bill != 0 AND bill < $time )
206 OR ( adjourn IS NOT NULL AND adjourn != 0 AND adjourn <= $time )
211 #only because there's no SQL test for is_prepaid :/
213 grep { ( $_->part_pkg->is_prepaid
218 && $_->adjourn <= $time
226 foreach my $cust_pkg ( @susp_pkgs ) {
227 my $cpr = $cust_pkg->last_cust_pkg_reason('adjourn')
228 if ($cust_pkg->adjourn && $cust_pkg->adjourn < $^T);
229 my $error = $cust_pkg->suspend($cpr ? ( 'reason' => $cpr->reasonnum,
230 'reason_otaker' => $cpr->otaker
234 push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
237 join(' / ', @errors);
241 sub unsuspend_resumed_pkgs {
242 my ( $self, $time, %options ) = @_;
244 my @unsusp_pkgs = $self->ncancelled_pkgs( {
245 'extra_sql' => " AND resume IS NOT NULL AND resume > 0 AND resume <= $time "
250 foreach my $cust_pkg ( @unsusp_pkgs ) {
251 my $error = $cust_pkg->unsuspend( 'time' => $time );
252 push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
255 join(' / ', @errors);
261 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
262 conjunction with the collect method by calling B<bill_and_collect>.
264 If there is an error, returns the error, otherwise returns false.
266 Options are passed as name-value pairs. Currently available options are:
272 If set true, re-charges setup fees.
276 If set true then only bill recurring charges, not setup, usage, one time
281 If set, then override the normal frequency and look for a part_pkg_discount
282 to take at that frequency. This is appropriate only when the normal
283 frequency for all packages is monthly, and is an error otherwise. Use
284 C<pkg_list> to limit the set of packages included in billing.
288 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:
292 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
296 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
298 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
302 A hashref of pkgparts to exclude from this billing run (can also be specified as a comma-separated scalar).
306 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.
310 This boolean value informs the us that the package is being cancelled. This
311 typically might mean not charging the normal recurring fee but only usage
312 fees since the last billing. Setup charges may be charged. Not all package
313 plans support this feature (they tend to charge 0).
317 Prevent the resetting of usage limits during this call.
321 Do not save the generated bill in the database. Useful with return_bill
325 A list reference on which the generated bill(s) will be returned.
329 Optional terms to be printed on this invoice. Otherwise, customer-specific
330 terms or the default terms are used.
337 my( $self, %options ) = @_;
339 return '' if $self->payby eq 'COMP';
341 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
343 warn "$me bill customer ". $self->custnum. "\n"
346 my $time = $options{'time'} || time;
347 my $invoice_time = $options{'invoice_time'} || $time;
349 $options{'not_pkgpart'} ||= {};
350 $options{'not_pkgpart'} = { map { $_ => 1 }
351 split(/\s*,\s*/, $options{'not_pkgpart'})
353 unless ref($options{'not_pkgpart'});
355 local $SIG{HUP} = 'IGNORE';
356 local $SIG{INT} = 'IGNORE';
357 local $SIG{QUIT} = 'IGNORE';
358 local $SIG{TERM} = 'IGNORE';
359 local $SIG{TSTP} = 'IGNORE';
360 local $SIG{PIPE} = 'IGNORE';
362 my $oldAutoCommit = $FS::UID::AutoCommit;
363 local $FS::UID::AutoCommit = 0;
366 warn "$me acquiring lock on customer ". $self->custnum. "\n"
369 $self->select_for_update; #mutex
371 warn "$me running pre-bill events for customer ". $self->custnum. "\n"
374 my $error = $self->do_cust_event(
375 'debug' => ( $options{'debug'} || 0 ),
376 'time' => $invoice_time,
377 'check_freq' => $options{'check_freq'},
378 'stage' => 'pre-bill',
380 unless $options{no_commit};
382 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
386 warn "$me done running pre-bill events for customer ". $self->custnum. "\n"
389 #keep auto-charge and non-auto-charge line items separate
390 my @passes = ( '', 'no_auto' );
392 my %cust_bill_pkg = map { $_ => [] } @passes;
395 # find the packages which are due for billing, find out how much they are
396 # & generate invoice database.
399 my %total_setup = map { my $z = 0; $_ => \$z; } @passes;
400 my %total_recur = map { my $z = 0; $_ => \$z; } @passes;
402 my %taxlisthash = map { $_ => {} } @passes;
404 my @precommit_hooks = ();
406 $options{'pkg_list'} ||= [ $self->ncancelled_pkgs ]; #param checks?
407 foreach my $cust_pkg ( @{ $options{'pkg_list'} } ) {
409 next if $options{'not_pkgpart'}->{$cust_pkg->pkgpart};
411 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG;
413 #? to avoid use of uninitialized value errors... ?
414 $cust_pkg->setfield('bill', '')
415 unless defined($cust_pkg->bill);
417 #my $part_pkg = $cust_pkg->part_pkg;
419 my $real_pkgpart = $cust_pkg->pkgpart;
420 my %hash = $cust_pkg->hash;
422 # we could implement this bit as FS::part_pkg::has_hidden, but we already
423 # suffer from performance issues
424 $options{has_hidden} = 0;
425 my @part_pkg = $cust_pkg->part_pkg->self_and_bill_linked;
426 $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
428 foreach my $part_pkg ( @part_pkg ) {
430 $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
432 my $pass = ($cust_pkg->no_auto || $part_pkg->no_auto) ? 'no_auto' : '';
434 my $next_bill = $cust_pkg->getfield('bill') || 0;
436 while ( $next_bill <= $time ) {
438 $self->_make_lines( 'part_pkg' => $part_pkg,
439 'cust_pkg' => $cust_pkg,
440 'precommit_hooks' => \@precommit_hooks,
441 'line_items' => $cust_bill_pkg{$pass},
442 'setup' => $total_setup{$pass},
443 'recur' => $total_recur{$pass},
444 'tax_matrix' => $taxlisthash{$pass},
446 'real_pkgpart' => $real_pkgpart,
447 'options' => \%options,
450 # Stop if anything goes wrong
453 # or if we're not incrementing the bill date.
454 last if ($cust_pkg->getfield('bill') || 0) == $next_bill;
456 $next_bill = $cust_pkg->getfield('bill') || 0;
458 #stop if -o was passed to freeside-daily
459 last if $options{'one_recur'};
462 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
466 } #foreach my $part_pkg
468 } #foreach my $cust_pkg
470 #if the customer isn't on an automatic payby, everything can go on a single
472 #if ( $cust_main->payby !~ /^(CARD|CHEK)$/ ) {
473 #merge everything into one list
476 foreach my $pass (@passes) { # keys %cust_bill_pkg ) {
478 my @cust_bill_pkg = _omit_zero_value_bundles(@{ $cust_bill_pkg{$pass} });
480 next unless @cust_bill_pkg; #don't create an invoice w/o line items
482 warn "$me billing pass $pass\n"
483 #.Dumper(\@cust_bill_pkg)."\n"
486 if ( scalar( grep { $_->recur && $_->recur > 0 } @cust_bill_pkg) ||
487 !$conf->exists('postal_invoice-recurring_only')
491 my $postal_pkg = $self->charge_postal_fee();
492 if ( $postal_pkg && !ref( $postal_pkg ) ) {
494 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
495 return "can't charge postal invoice fee for customer ".
496 $self->custnum. ": $postal_pkg";
498 } elsif ( $postal_pkg ) {
500 my $real_pkgpart = $postal_pkg->pkgpart;
501 # we could implement this bit as FS::part_pkg::has_hidden, but we already
502 # suffer from performance issues
503 $options{has_hidden} = 0;
504 my @part_pkg = $postal_pkg->part_pkg->self_and_bill_linked;
505 $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
507 foreach my $part_pkg ( @part_pkg ) {
508 my %postal_options = %options;
509 delete $postal_options{cancel};
511 $self->_make_lines( 'part_pkg' => $part_pkg,
512 'cust_pkg' => $postal_pkg,
513 'precommit_hooks' => \@precommit_hooks,
514 'line_items' => \@cust_bill_pkg,
515 'setup' => $total_setup{$pass},
516 'recur' => $total_recur{$pass},
517 'tax_matrix' => $taxlisthash{$pass},
519 'real_pkgpart' => $real_pkgpart,
520 'options' => \%postal_options,
523 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
528 # it's silly to have a zero value postal_pkg, but....
529 @cust_bill_pkg = _omit_zero_value_bundles(@cust_bill_pkg);
535 my $listref_or_error =
536 $self->calculate_taxes( \@cust_bill_pkg, $taxlisthash{$pass}, $invoice_time);
538 unless ( ref( $listref_or_error ) ) {
539 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
540 return $listref_or_error;
543 foreach my $taxline ( @$listref_or_error ) {
544 ${ $total_setup{$pass} } =
545 sprintf('%.2f', ${ $total_setup{$pass} } + $taxline->setup );
546 push @cust_bill_pkg, $taxline;
550 warn "adding tax adjustments...\n" if $DEBUG > 2;
551 foreach my $cust_tax_adjustment (
552 qsearch('cust_tax_adjustment', { 'custnum' => $self->custnum,
558 my $tax = sprintf('%.2f', $cust_tax_adjustment->amount );
560 my $itemdesc = $cust_tax_adjustment->taxname;
561 $itemdesc = '' if $itemdesc eq 'Tax';
563 push @cust_bill_pkg, new FS::cust_bill_pkg {
569 'itemdesc' => $itemdesc,
570 'itemcomment' => $cust_tax_adjustment->comment,
571 'cust_tax_adjustment' => $cust_tax_adjustment,
572 #'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
577 my $charged = sprintf('%.2f', ${ $total_setup{$pass} } + ${ $total_recur{$pass} } );
579 my @cust_bill = $self->cust_bill;
580 my $balance = $self->balance;
581 my $previous_balance = scalar(@cust_bill)
582 ? ( $cust_bill[$#cust_bill]->billing_balance || 0 )
585 $previous_balance += $cust_bill[$#cust_bill]->charged
586 if scalar(@cust_bill);
587 #my $balance_adjustments =
588 # sprintf('%.2f', $balance - $prior_prior_balance - $prior_charged);
590 warn "creating the new invoice\n" if $DEBUG;
591 #create the new invoice
592 my $cust_bill = new FS::cust_bill ( {
593 'custnum' => $self->custnum,
594 '_date' => $invoice_time,
595 'charged' => $charged,
596 'billing_balance' => $balance,
597 'previous_balance' => $previous_balance,
598 'invoice_terms' => $options{'invoice_terms'},
599 'cust_bill_pkg' => \@cust_bill_pkg,
601 $error = $cust_bill->insert unless $options{no_commit};
603 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
604 return "can't create invoice for customer #". $self->custnum. ": $error";
606 push @{$options{return_bill}}, $cust_bill if $options{return_bill};
608 } #foreach my $pass ( keys %cust_bill_pkg )
610 foreach my $hook ( @precommit_hooks ) {
613 } unless $options{no_commit};
615 $dbh->rollback if $oldAutoCommit && !$options{no_commit};
616 return "$@ running precommit hook $hook\n";
620 $dbh->commit or die $dbh->errstr if $oldAutoCommit && !$options{no_commit};
625 #discard bundled packages of 0 value
626 sub _omit_zero_value_bundles {
629 my @cust_bill_pkg = ();
630 my @cust_bill_pkg_bundle = ();
632 my $discount_show_always = 0;
634 foreach my $cust_bill_pkg ( @in ) {
636 $discount_show_always = ($cust_bill_pkg->get('discounts')
637 && scalar(@{$cust_bill_pkg->get('discounts')})
638 && $conf->exists('discount-show-always'));
640 warn " pkgnum ". $cust_bill_pkg->pkgnum. " sum $sum, ".
641 "setup_show_zero ". $cust_bill_pkg->setup_show_zero.
642 "recur_show_zero ". $cust_bill_pkg->recur_show_zero. "\n"
645 if (scalar(@cust_bill_pkg_bundle) && !$cust_bill_pkg->pkgpart_override) {
646 push @cust_bill_pkg, @cust_bill_pkg_bundle
648 || ($sum == 0 && ( $discount_show_always
649 || grep {$_->recur_show_zero || $_->setup_show_zero}
650 @cust_bill_pkg_bundle
653 @cust_bill_pkg_bundle = ();
657 $sum += $cust_bill_pkg->setup + $cust_bill_pkg->recur;
658 push @cust_bill_pkg_bundle, $cust_bill_pkg;
662 push @cust_bill_pkg, @cust_bill_pkg_bundle
664 || ($sum == 0 && ( $discount_show_always
665 || grep {$_->recur_show_zero || $_->setup_show_zero}
666 @cust_bill_pkg_bundle
670 warn " _omit_zero_value_bundles: ". scalar(@in).
671 '->'. scalar(@cust_bill_pkg). "\n" #. Dumper(@cust_bill_pkg). "\n"
678 =item calculate_taxes LINEITEMREF TAXHASHREF INVOICE_TIME
680 This is a weird one. Perhaps it should not even be exposed.
682 Generates tax line items (see L<FS::cust_bill_pkg>) for this customer.
683 Usually used internally by bill method B<bill>.
685 If there is an error, returns the error, otherwise returns reference to a
686 list of line items suitable for insertion.
692 An array ref of the line items being billed.
696 A strange beast. The keys to this hash are internal identifiers consisting
697 of the name of the tax object type, a space, and its unique identifier ( e.g.
698 'cust_main_county 23' ). The values of the hash are listrefs. The first
699 item in the list is the tax object. The remaining items are either line
700 items or floating point values (currency amounts).
702 The taxes are calculated on this entity. Calculated exemption records are
703 transferred to the LINEITEMREF items on the assumption that they are related.
709 This specifies the date appearing on the associated invoice. Some
710 jurisdictions (i.e. Texas) have tax exemptions which are date sensitive.
716 sub calculate_taxes {
717 my ($self, $cust_bill_pkg, $taxlisthash, $invoice_time) = @_;
719 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
721 warn "$me calculate_taxes\n"
722 #.Dumper($self, $cust_bill_pkg, $taxlisthash, $invoice_time). "\n"
725 my @tax_line_items = ();
727 # keys are tax names (as printed on invoices / itemdesc )
728 # values are listrefs of taxlisthash keys (internal identifiers)
731 # keys are taxlisthash keys (internal identifiers)
732 # values are (cumulative) amounts
735 # keys are taxlisthash keys (internal identifiers)
736 # values are listrefs of cust_bill_pkg_tax_location hashrefs
737 my %tax_location = ();
739 # keys are taxlisthash keys (internal identifiers)
740 # values are listrefs of cust_bill_pkg_tax_rate_location hashrefs
741 my %tax_rate_location = ();
743 foreach my $tax ( keys %$taxlisthash ) {
744 my $tax_object = shift @{ $taxlisthash->{$tax} };
745 warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
746 warn " ". join('/', @{ $taxlisthash->{$tax} } ). "\n" if $DEBUG > 2;
747 my $hashref_or_error =
748 $tax_object->taxline( $taxlisthash->{$tax},
749 'custnum' => $self->custnum,
750 'invoice_time' => $invoice_time
752 return $hashref_or_error unless ref($hashref_or_error);
754 unshift @{ $taxlisthash->{$tax} }, $tax_object;
756 my $name = $hashref_or_error->{'name'};
757 my $amount = $hashref_or_error->{'amount'};
759 #warn "adding $amount as $name\n";
760 $taxname{ $name } ||= [];
761 push @{ $taxname{ $name } }, $tax;
763 $tax{ $tax } += $amount;
765 $tax_location{ $tax } ||= [];
766 if ( $tax_object->get('pkgnum') || $tax_object->get('locationnum') ) {
767 push @{ $tax_location{ $tax } },
769 'taxnum' => $tax_object->taxnum,
770 'taxtype' => ref($tax_object),
771 'pkgnum' => $tax_object->get('pkgnum'),
772 'locationnum' => $tax_object->get('locationnum'),
773 'amount' => sprintf('%.2f', $amount ),
777 $tax_rate_location{ $tax } ||= [];
778 if ( ref($tax_object) eq 'FS::tax_rate' ) {
779 my $taxratelocationnum =
780 $tax_object->tax_rate_location->taxratelocationnum;
781 push @{ $tax_rate_location{ $tax } },
783 'taxnum' => $tax_object->taxnum,
784 'taxtype' => ref($tax_object),
785 'amount' => sprintf('%.2f', $amount ),
786 'locationtaxid' => $tax_object->location,
787 'taxratelocationnum' => $taxratelocationnum,
793 #move the cust_tax_exempt_pkg records to the cust_bill_pkgs we will commit
794 my %packagemap = map { $_->pkgnum => $_ } @$cust_bill_pkg;
795 foreach my $tax ( keys %$taxlisthash ) {
796 foreach ( @{ $taxlisthash->{$tax} }[1 ... scalar(@{ $taxlisthash->{$tax} })] ) {
797 next unless ref($_) eq 'FS::cust_bill_pkg';
799 my @cust_tax_exempt_pkg = splice( @{ $_->_cust_tax_exempt_pkg } );
801 next unless @cust_tax_exempt_pkg; #just avoiding the prob when irrelevant?
802 die "can't distribute tax exemptions: no line item for ". Dumper($_).
803 " in packagemap ". join(',', sort {$a<=>$b} keys %packagemap). "\n"
804 unless $packagemap{$_->pkgnum};
806 push @{ $packagemap{$_->pkgnum}->_cust_tax_exempt_pkg },
807 @cust_tax_exempt_pkg;
811 #consolidate and create tax line items
812 warn "consolidating and generating...\n" if $DEBUG > 2;
813 foreach my $taxname ( keys %taxname ) {
816 my @cust_bill_pkg_tax_location = ();
817 my @cust_bill_pkg_tax_rate_location = ();
818 warn "adding $taxname\n" if $DEBUG > 1;
819 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
820 next if $seen{$taxitem}++;
821 warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
822 $tax += $tax{$taxitem};
823 push @cust_bill_pkg_tax_location,
824 map { new FS::cust_bill_pkg_tax_location $_ }
825 @{ $tax_location{ $taxitem } };
826 push @cust_bill_pkg_tax_rate_location,
827 map { new FS::cust_bill_pkg_tax_rate_location $_ }
828 @{ $tax_rate_location{ $taxitem } };
832 $tax = sprintf('%.2f', $tax );
834 my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname,
840 if ( $pkg_category and
841 $conf->config('invoice_latexsummary') ||
842 $conf->config('invoice_htmlsummary')
846 my %hash = ( 'section' => $pkg_category->categoryname );
847 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
851 push @tax_line_items, new FS::cust_bill_pkg {
857 'itemdesc' => $taxname,
858 'display' => \@display,
859 'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
860 'cust_bill_pkg_tax_rate_location' => \@cust_bill_pkg_tax_rate_location,
869 my ($self, %params) = @_;
871 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
873 my $part_pkg = $params{part_pkg} or die "no part_pkg specified";
874 my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
875 my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
876 my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
877 my $total_setup = $params{setup} or die "no setup accumulator specified";
878 my $total_recur = $params{recur} or die "no recur accumulator specified";
879 my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
880 my $time = $params{'time'} or die "no time specified";
881 my (%options) = %{$params{options}};
883 if ( $part_pkg->freq ne '1' and ($options{'freq_override'} || 0) > 0 ) {
884 # this should never happen
885 die 'freq_override billing attempted on non-monthly package '.
890 my $real_pkgpart = $params{real_pkgpart};
891 my %hash = $cust_pkg->hash;
892 my $old_cust_pkg = new FS::cust_pkg \%hash;
897 $cust_pkg->pkgpart($part_pkg->pkgpart);
905 my @setup_discounts = ();
906 my %setup_param = ( 'discounts' => \@setup_discounts );
907 if ( ! $options{recurring_only}
908 and ! $options{cancel}
909 and ( $options{'resetup'}
910 || ( ! $cust_pkg->setup
911 && ( ! $cust_pkg->start_date
912 || $cust_pkg->start_date <= day_end($time)
914 && ( ! $conf->exists('disable_setup_suspended_pkgs')
915 || ( $conf->exists('disable_setup_suspended_pkgs') &&
916 ! $cust_pkg->getfield('susp')
924 warn " bill setup\n" if $DEBUG > 1;
926 unless ( $cust_pkg->waive_setup ) {
929 $setup = eval { $cust_pkg->calc_setup( $time, \@details, \%setup_param ) };
930 return "$@ running calc_setup for $cust_pkg\n"
933 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
936 $cust_pkg->setfield('setup', $time)
937 unless $cust_pkg->setup;
938 #do need it, but it won't get written to the db
939 #|| $cust_pkg->pkgpart != $real_pkgpart;
941 $cust_pkg->setfield('start_date', '')
942 if $cust_pkg->start_date;
950 #XXX unit stuff here too
953 my @recur_discounts = ();
955 if ( ! $cust_pkg->start_date
956 and ( ! $cust_pkg->susp || $cust_pkg->option('suspend_bill',1)
957 || ( $part_pkg->option('suspend_bill', 1) )
958 && ! $cust_pkg->option('no_suspend_bill',1)
961 ( $part_pkg->freq ne '0' && ( $cust_pkg->bill || 0 ) <= day_end($time) )
962 || ( $part_pkg->plan eq 'voip_cdr'
963 && $part_pkg->option('bill_every_call')
968 # XXX should this be a package event? probably. events are called
969 # at collection time at the moment, though...
970 $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
971 if $part_pkg->can('reset_usage') && !$options{'no_usage_reset'};
972 #don't want to reset usage just cause we want a line item??
973 #&& $part_pkg->pkgpart == $real_pkgpart;
975 warn " bill recur\n" if $DEBUG > 1;
978 # XXX shared with $recur_prog
979 $sdate = ( $options{cancel} ? $cust_pkg->last_bill : $cust_pkg->bill )
983 #over two params! lets at least switch to a hashref for the rest...
984 my $increment_next_bill = ( $part_pkg->freq ne '0'
985 && ( $cust_pkg->getfield('bill') || 0 ) <= day_end($time)
988 my %param = ( %setup_param,
989 'precommit_hooks' => $precommit_hooks,
990 'increment_next_bill' => $increment_next_bill,
991 'discounts' => \@recur_discounts,
992 'real_pkgpart' => $real_pkgpart,
993 'freq_override' => $options{freq_override} || '',
997 my $method = $options{cancel} ? 'calc_cancel' : 'calc_recur';
999 # There may be some part_pkg for which this is wrong. Only those
1000 # which can_discount are supported.
1001 # (the UI should prevent adding discounts to these at the moment)
1003 warn "calling $method on cust_pkg ". $cust_pkg->pkgnum.
1004 " for pkgpart ". $cust_pkg->pkgpart.
1005 " with params ". join(' / ', map "$_=>$param{$_}", keys %param). "\n"
1008 $recur = eval { $cust_pkg->$method( \$sdate, \@details, \%param ) };
1009 return "$@ running $method for $cust_pkg\n"
1012 if ( $increment_next_bill ) {
1014 my $next_bill = $part_pkg->add_freq($sdate, $options{freq_override} || 0);
1015 return "unparsable frequency: ". $part_pkg->freq
1016 if $next_bill == -1;
1018 #pro-rating magic - if $recur_prog fiddled $sdate, want to use that
1019 # only for figuring next bill date, nothing else, so, reset $sdate again
1021 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1022 #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
1023 $cust_pkg->last_bill($sdate);
1025 $cust_pkg->setfield('bill', $next_bill );
1029 if ( $param{'setup_fee'} ) {
1030 # Add an additional setup fee at the billing stage.
1031 # Used for prorate_defer_bill.
1032 $setup += $param{'setup_fee'};
1033 $unitsetup += $param{'setup_fee'};
1037 if ( defined $param{'discount_left_setup'} ) {
1038 foreach my $discount_setup ( values %{$param{'discount_left_setup'}} ) {
1039 $setup -= $discount_setup;
1045 warn "\$setup is undefined" unless defined($setup);
1046 warn "\$recur is undefined" unless defined($recur);
1047 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1050 # If there's line items, create em cust_bill_pkg records
1051 # If $cust_pkg has been modified, update it (if we're a real pkgpart)
1056 if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
1057 # hmm.. and if just the options are modified in some weird price plan?
1059 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
1062 my $error = $cust_pkg->replace( $old_cust_pkg,
1063 'depend_jobnum'=>$options{depend_jobnum},
1064 'options' => { $cust_pkg->options },
1066 unless $options{no_commit};
1067 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error"
1068 if $error; #just in case
1071 $setup = sprintf( "%.2f", $setup );
1072 $recur = sprintf( "%.2f", $recur );
1073 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
1074 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1076 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
1077 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1080 my $discount_show_always = $conf->exists('discount-show-always')
1081 && ( ($setup == 0 && scalar(@setup_discounts))
1082 || ($recur == 0 && scalar(@recur_discounts))
1087 || (!$part_pkg->hidden && $options{has_hidden}) #include some $0 lines
1088 || $discount_show_always
1089 || ($setup == 0 && $cust_pkg->_X_show_zero('setup'))
1090 || ($recur == 0 && $cust_pkg->_X_show_zero('recur'))
1094 warn " charges (setup=$setup, recur=$recur); adding line items\n"
1097 my @cust_pkg_detail = map { $_->detail } $cust_pkg->cust_pkg_detail('I');
1099 warn " adding customer package invoice detail: $_\n"
1100 foreach @cust_pkg_detail;
1102 push @details, @cust_pkg_detail;
1104 my $cust_bill_pkg = new FS::cust_bill_pkg {
1105 'pkgnum' => $cust_pkg->pkgnum,
1107 'unitsetup' => $unitsetup,
1109 'unitrecur' => $unitrecur,
1110 'quantity' => $cust_pkg->quantity,
1111 'details' => \@details,
1112 'discounts' => [ @setup_discounts, @recur_discounts ],
1113 'hidden' => $part_pkg->hidden,
1114 'freq' => $part_pkg->freq,
1117 if ( $part_pkg->option('prorate_defer_bill',1)
1118 and !$hash{last_bill} ) {
1119 # both preceding and upcoming, technically
1120 $cust_bill_pkg->sdate( $cust_pkg->setup );
1121 $cust_bill_pkg->edate( $cust_pkg->bill );
1122 } elsif ( $part_pkg->recur_temporality eq 'preceding' ) {
1123 $cust_bill_pkg->sdate( $hash{last_bill} );
1124 $cust_bill_pkg->edate( $sdate - 86399 ); #60s*60m*24h-1
1125 $cust_bill_pkg->edate( $time ) if $options{cancel};
1126 } else { #if ( $part_pkg->recur_temporality eq 'upcoming' ) {
1127 $cust_bill_pkg->sdate( $sdate );
1128 $cust_bill_pkg->edate( $cust_pkg->bill );
1129 #$cust_bill_pkg->edate( $time ) if $options{cancel};
1132 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
1133 unless $part_pkg->pkgpart == $real_pkgpart;
1135 $$total_setup += $setup;
1136 $$total_recur += $recur;
1142 unless ( $discount_show_always ) {
1144 $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart, \%options);
1145 return $error if $error;
1148 push @$cust_bill_pkgs, $cust_bill_pkg;
1150 } #if $setup != 0 || $recur != 0
1160 my $part_pkg = shift;
1161 my $taxlisthash = shift;
1162 my $cust_bill_pkg = shift;
1163 my $cust_pkg = shift;
1164 my $invoice_time = shift;
1165 my $real_pkgpart = shift;
1166 my $options = shift;
1168 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1170 my %cust_bill_pkg = ();
1174 #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
1175 push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
1176 push @classes, 'setup' if ($cust_bill_pkg->setup && !$options->{cancel});
1177 push @classes, 'recur' if ($cust_bill_pkg->recur && !$options->{cancel});
1179 if ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
1181 if ( $conf->exists('enable_taxproducts')
1182 && ( scalar($part_pkg->part_pkg_taxoverride)
1183 || $part_pkg->has_taxproduct
1188 foreach my $class (@classes) {
1189 my $err_or_ref = $self->_gather_taxes( $part_pkg, $class, $cust_pkg );
1190 return $err_or_ref unless ref($err_or_ref);
1191 $taxes{$class} = $err_or_ref;
1194 unless (exists $taxes{''}) {
1195 my $err_or_ref = $self->_gather_taxes( $part_pkg, '', $cust_pkg );
1196 return $err_or_ref unless ref($err_or_ref);
1197 $taxes{''} = $err_or_ref;
1202 my @loc_keys = qw( district city county state country );
1204 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1205 my $cust_location = $cust_pkg->cust_location;
1206 %taxhash = map { $_ => $cust_location->$_() } @loc_keys;
1209 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
1212 %taxhash = map { $_ => $self->get("$prefix$_") } @loc_keys;
1215 $taxhash{'taxclass'} = $part_pkg->taxclass;
1218 my %taxhash_elim = %taxhash;
1219 my @elim = qw( district city county state );
1222 #first try a match with taxclass
1223 @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
1225 if ( !scalar(@taxes) && $taxhash_elim{'taxclass'} ) {
1226 #then try a match without taxclass
1227 my %no_taxclass = %taxhash_elim;
1228 $no_taxclass{ 'taxclass' } = '';
1229 @taxes = qsearch( 'cust_main_county', \%no_taxclass );
1232 $taxhash_elim{ shift(@elim) } = '';
1234 } while ( !scalar(@taxes) && scalar(@elim) );
1236 @taxes = grep { ! $_->taxname or ! $self->tax_exemption($_->taxname) }
1238 if $self->cust_main_exemption; #just to be safe
1240 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1242 $_->set('pkgnum', $cust_pkg->pkgnum );
1243 $_->set('locationnum', $cust_pkg->locationnum );
1247 $taxes{''} = [ @taxes ];
1248 $taxes{'setup'} = [ @taxes ];
1249 $taxes{'recur'} = [ @taxes ];
1250 $taxes{$_} = [ @taxes ] foreach (@classes);
1252 # # maybe eliminate this entirely, along with all the 0% records
1253 # unless ( @taxes ) {
1255 # "fatal: can't find tax rate for state/county/country/taxclass ".
1256 # join('/', map $taxhash{$_}, qw(state county country taxclass) );
1259 } #if $conf->exists('enable_taxproducts') ...
1263 #what's this doing in the middle of _handle_taxes? probably should split
1264 #this into three parts above in _make_lines
1265 $cust_bill_pkg->set_display( part_pkg => $part_pkg,
1266 real_pkgpart => $real_pkgpart,
1269 my %tax_cust_bill_pkg = $cust_bill_pkg->disintegrate;
1270 foreach my $key (keys %tax_cust_bill_pkg) {
1271 my @taxes = @{ $taxes{$key} || [] };
1272 my $tax_cust_bill_pkg = $tax_cust_bill_pkg{$key};
1274 my %localtaxlisthash = ();
1275 foreach my $tax ( @taxes ) {
1277 my $taxname = ref( $tax ). ' '. $tax->taxnum;
1278 # $taxname .= ' pkgnum'. $cust_pkg->pkgnum.
1279 # ' locationnum'. $cust_pkg->locationnum
1280 # if $conf->exists('tax-pkg_address') && $cust_pkg->locationnum;
1282 $taxlisthash->{ $taxname } ||= [ $tax ];
1283 push @{ $taxlisthash->{ $taxname } }, $tax_cust_bill_pkg;
1285 $localtaxlisthash{ $taxname } ||= [ $tax ];
1286 push @{ $localtaxlisthash{ $taxname } }, $tax_cust_bill_pkg;
1290 warn "finding taxed taxes...\n" if $DEBUG > 2;
1291 foreach my $tax ( keys %localtaxlisthash ) {
1292 my $tax_object = shift @{ $localtaxlisthash{$tax} };
1293 warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
1295 next unless $tax_object->can('tax_on_tax');
1297 foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
1298 my $totname = ref( $tot ). ' '. $tot->taxnum;
1300 warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
1302 next unless exists( $localtaxlisthash{ $totname } ); # only increase
1304 warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
1305 my $hashref_or_error =
1306 $tax_object->taxline( $localtaxlisthash{$tax},
1307 'custnum' => $self->custnum,
1308 'invoice_time' => $invoice_time,
1310 return $hashref_or_error
1311 unless ref($hashref_or_error);
1313 $taxlisthash->{ $totname } ||= [ $tot ];
1314 push @{ $taxlisthash->{ $totname } }, $hashref_or_error->{amount};
1326 my $part_pkg = shift;
1328 my $cust_pkg = shift;
1330 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1333 if ( $cust_pkg->locationnum && $conf->exists('tax-pkg_address') ) {
1334 $geocode = $cust_pkg->cust_location->geocode('cch');
1336 $geocode = $self->geocode('cch');
1341 my @taxclassnums = map { $_->taxclassnum }
1342 $part_pkg->part_pkg_taxoverride($class);
1344 unless (@taxclassnums) {
1345 @taxclassnums = map { $_->taxclassnum }
1346 grep { $_->taxable eq 'Y' }
1347 $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
1349 warn "Found taxclassnum values of ". join(',', @taxclassnums)
1354 join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
1356 @taxes = qsearch({ 'table' => 'tax_rate',
1357 'hashref' => { 'geocode' => $geocode, },
1358 'extra_sql' => $extra_sql,
1360 if scalar(@taxclassnums);
1362 warn "Found taxes ".
1363 join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n"
1370 =item collect [ HASHREF | OPTION => VALUE ... ]
1372 (Attempt to) collect money for this customer's outstanding invoices (see
1373 L<FS::cust_bill>). Usually used after the bill method.
1375 Actions are now triggered by billing events; see L<FS::part_event> and the
1376 billing events web interface. Old-style invoice events (see
1377 L<FS::part_bill_event>) have been deprecated.
1379 If there is an error, returns the error, otherwise returns false.
1381 Options are passed as name-value pairs.
1383 Currently available options are:
1389 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.
1393 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1397 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1401 set true to surpress email card/ACH decline notices.
1405 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)
1411 # allows for one time override of normal customer billing method
1416 my( $self, %options ) = @_;
1418 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1420 my $invoice_time = $options{'invoice_time'} || time;
1423 local $SIG{HUP} = 'IGNORE';
1424 local $SIG{INT} = 'IGNORE';
1425 local $SIG{QUIT} = 'IGNORE';
1426 local $SIG{TERM} = 'IGNORE';
1427 local $SIG{TSTP} = 'IGNORE';
1428 local $SIG{PIPE} = 'IGNORE';
1430 my $oldAutoCommit = $FS::UID::AutoCommit;
1431 local $FS::UID::AutoCommit = 0;
1434 $self->select_for_update; #mutex
1437 my $balance = $self->balance;
1438 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
1441 if ( exists($options{'retry_card'}) ) {
1442 carp 'retry_card option passed to collect is deprecated; use retry';
1443 $options{'retry'} ||= $options{'retry_card'};
1445 if ( exists($options{'retry'}) && $options{'retry'} ) {
1446 my $error = $self->retry_realtime;
1448 $dbh->rollback if $oldAutoCommit;
1453 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1455 #never want to roll back an event just because it returned an error
1456 local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1458 $self->do_cust_event(
1459 'debug' => ( $options{'debug'} || 0 ),
1460 'time' => $invoice_time,
1461 'check_freq' => $options{'check_freq'},
1462 'stage' => 'collect',
1467 =item retry_realtime
1469 Schedules realtime / batch credit card / electronic check / LEC billing
1470 events for for retry. Useful if card information has changed or manual
1471 retry is desired. The 'collect' method must be called to actually retry
1474 Implementation details: For either this customer, or for each of this
1475 customer's open invoices, changes the status of the first "done" (with
1476 statustext error) realtime processing event to "failed".
1480 sub retry_realtime {
1483 local $SIG{HUP} = 'IGNORE';
1484 local $SIG{INT} = 'IGNORE';
1485 local $SIG{QUIT} = 'IGNORE';
1486 local $SIG{TERM} = 'IGNORE';
1487 local $SIG{TSTP} = 'IGNORE';
1488 local $SIG{PIPE} = 'IGNORE';
1490 my $oldAutoCommit = $FS::UID::AutoCommit;
1491 local $FS::UID::AutoCommit = 0;
1494 #a little false laziness w/due_cust_event (not too bad, really)
1496 my $join = FS::part_event_condition->join_conditions_sql;
1497 my $order = FS::part_event_condition->order_conditions_sql;
1500 . join ( ' OR ' , map {
1501 "( part_event.eventtable = " . dbh->quote($_)
1502 . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
1503 } FS::part_event->eventtables)
1506 #here is the agent virtualization
1507 my $agent_virt = " ( part_event.agentnum IS NULL
1508 OR part_event.agentnum = ". $self->agentnum. ' )';
1510 #XXX this shouldn't be hardcoded, actions should declare it...
1511 my @realtime_events = qw(
1512 cust_bill_realtime_card
1513 cust_bill_realtime_check
1514 cust_bill_realtime_lec
1518 my $is_realtime_event =
1519 ' part_event.action IN ( '.
1520 join(',', map "'$_'", @realtime_events ).
1523 my $batch_or_statustext =
1524 "( part_event.action = 'cust_bill_batch'
1525 OR ( statustext IS NOT NULL AND statustext != '' )
1529 my @cust_event = qsearch({
1530 'table' => 'cust_event',
1531 'select' => 'cust_event.*',
1532 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
1533 'hashref' => { 'status' => 'done' },
1534 'extra_sql' => " AND $batch_or_statustext ".
1535 " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
1538 my %seen_invnum = ();
1539 foreach my $cust_event (@cust_event) {
1541 #max one for the customer, one for each open invoice
1542 my $cust_X = $cust_event->cust_X;
1543 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
1547 or $cust_event->part_event->eventtable eq 'cust_bill'
1550 my $error = $cust_event->retry;
1552 $dbh->rollback if $oldAutoCommit;
1553 return "error scheduling event for retry: $error";
1558 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1563 =item do_cust_event [ HASHREF | OPTION => VALUE ... ]
1565 Runs billing events; see L<FS::part_event> and the billing events web
1568 If there is an error, returns the error, otherwise returns false.
1570 Options are passed as name-value pairs.
1572 Currently available options are:
1578 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.
1582 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1586 "collect" (the default) or "pre-bill"
1590 set true to surpress email card/ACH decline notices.
1594 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)
1601 # allows for one time override of normal customer billing method
1605 # Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1608 my( $self, %options ) = @_;
1610 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1612 my $time = $options{'time'} || time;
1615 local $SIG{HUP} = 'IGNORE';
1616 local $SIG{INT} = 'IGNORE';
1617 local $SIG{QUIT} = 'IGNORE';
1618 local $SIG{TERM} = 'IGNORE';
1619 local $SIG{TSTP} = 'IGNORE';
1620 local $SIG{PIPE} = 'IGNORE';
1622 my $oldAutoCommit = $FS::UID::AutoCommit;
1623 local $FS::UID::AutoCommit = 0;
1626 $self->select_for_update; #mutex
1629 my $balance = $self->balance;
1630 warn "$me do_cust_event customer ". $self->custnum. ": balance $balance\n"
1633 # if ( exists($options{'retry_card'}) ) {
1634 # carp 'retry_card option passed to collect is deprecated; use retry';
1635 # $options{'retry'} ||= $options{'retry_card'};
1637 # if ( exists($options{'retry'}) && $options{'retry'} ) {
1638 # my $error = $self->retry_realtime;
1640 # $dbh->rollback if $oldAutoCommit;
1645 # false laziness w/pay_batch::import_results
1647 my $due_cust_event = $self->due_cust_event(
1648 'debug' => ( $options{'debug'} || 0 ),
1650 'check_freq' => $options{'check_freq'},
1651 'stage' => ( $options{'stage'} || 'collect' ),
1653 unless( ref($due_cust_event) ) {
1654 $dbh->rollback if $oldAutoCommit;
1655 return $due_cust_event;
1658 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1659 #never want to roll back an event just because it or a different one
1661 local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1663 foreach my $cust_event ( @$due_cust_event ) {
1667 #re-eval event conditions (a previous event could have changed things)
1668 unless ( $cust_event->test_conditions( 'time' => $time ) ) {
1669 #don't leave stray "new/locked" records around
1670 my $error = $cust_event->delete;
1671 return $error if $error;
1676 local $FS::cust_main::Billing_Realtime::realtime_bop_decline_quiet = 1
1677 if $options{'quiet'};
1678 warn " running cust_event ". $cust_event->eventnum. "\n"
1681 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
1682 if ( my $error = $cust_event->do_event( 'time' => $time ) ) {
1683 #XXX wtf is this? figure out a proper dealio with return value
1695 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
1697 Inserts database records for and returns an ordered listref of new events due
1698 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
1699 events are due, an empty listref is returned. If there is an error, returns a
1700 scalar error message.
1702 To actually run the events, call each event's test_condition method, and if
1703 still true, call the event's do_event method.
1705 Options are passed as a hashref or as a list of name-value pairs. Available
1712 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.
1716 "collect" (the default) or "pre-bill"
1720 "Current time" for the events.
1724 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)
1728 Only return events for the specified eventtable (by default, events of all eventtables are returned)
1732 Explicitly pass the objects to be tested (typically used with eventtable).
1736 Set to true to return the objects, but not actually insert them into the
1743 sub due_cust_event {
1745 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
1748 #my $DEBUG = $opt{'debug'}
1749 local($DEBUG) = $opt{'debug'}
1750 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
1751 $DEBUG = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
1753 warn "$me due_cust_event called with options ".
1754 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
1757 $opt{'time'} ||= time;
1759 local $SIG{HUP} = 'IGNORE';
1760 local $SIG{INT} = 'IGNORE';
1761 local $SIG{QUIT} = 'IGNORE';
1762 local $SIG{TERM} = 'IGNORE';
1763 local $SIG{TSTP} = 'IGNORE';
1764 local $SIG{PIPE} = 'IGNORE';
1766 my $oldAutoCommit = $FS::UID::AutoCommit;
1767 local $FS::UID::AutoCommit = 0;
1770 $self->select_for_update #mutex
1771 unless $opt{testonly};
1774 # find possible events (initial search)
1777 my @cust_event = ();
1779 my @eventtable = $opt{'eventtable'}
1780 ? ( $opt{'eventtable'} )
1781 : FS::part_event->eventtables_runorder;
1783 my $check_freq = $opt{'check_freq'} || '1d';
1785 foreach my $eventtable ( @eventtable ) {
1788 if ( $opt{'objects'} ) {
1790 @objects = @{ $opt{'objects'} };
1794 #my @objects = $self->$eventtable(); # sub cust_main { @{ [ $self ] }; }
1795 if ( $eventtable eq 'cust_main' ) {
1796 @objects = ( $self );
1800 "LEFT JOIN cust_main USING ( custnum )";
1802 #some false laziness w/Cron::bill bill_where
1804 my $join = FS::part_event_condition->join_conditions_sql( $eventtable);
1805 my $where = FS::part_event_condition->where_conditions_sql($eventtable,
1806 'time'=>$opt{'time'},
1808 $where = $where ? "AND $where" : '';
1810 my $are_part_event =
1811 "EXISTS ( SELECT 1 FROM part_event $join
1812 WHERE check_freq = '$check_freq'
1813 AND eventtable = '$eventtable'
1814 AND ( disabled = '' OR disabled IS NULL )
1820 @objects = $self->$eventtable(
1821 'addl_from' => $cm_join,
1822 'extra_sql' => " AND $are_part_event",
1828 my @e_cust_event = ();
1830 my $cross = "CROSS JOIN $eventtable";
1831 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
1832 unless $eventtable eq 'cust_main';
1834 foreach my $object ( @objects ) {
1836 #this first search uses the condition_sql magic for optimization.
1837 #the more possible events we can eliminate in this step the better
1839 my $cross_where = '';
1840 my $pkey = $object->primary_key;
1841 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
1843 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
1845 FS::part_event_condition->where_conditions_sql( $eventtable,
1846 'time'=>$opt{'time'}
1848 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
1850 $extra_sql = "AND $extra_sql" if $extra_sql;
1852 #here is the agent virtualization
1853 $extra_sql .= " AND ( part_event.agentnum IS NULL
1854 OR part_event.agentnum = ". $self->agentnum. ' )';
1856 $extra_sql .= " $order";
1858 warn "searching for events for $eventtable ". $object->$pkey. "\n"
1859 if $opt{'debug'} > 2;
1860 my @part_event = qsearch( {
1861 'debug' => ( $opt{'debug'} > 3 ? 1 : 0 ),
1862 'select' => 'part_event.*',
1863 'table' => 'part_event',
1864 'addl_from' => "$cross $join",
1865 'hashref' => { 'check_freq' => $check_freq,
1866 'eventtable' => $eventtable,
1869 'extra_sql' => "AND $cross_where $extra_sql",
1873 my $pkey = $object->primary_key;
1874 warn " ". scalar(@part_event).
1875 " possible events found for $eventtable ". $object->$pkey(). "\n";
1878 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
1882 warn " ". scalar(@e_cust_event).
1883 " subtotal possible cust events found for $eventtable\n"
1886 push @cust_event, @e_cust_event;
1890 warn " ". scalar(@cust_event).
1891 " total possible cust events found in initial search\n"
1899 $opt{stage} ||= 'collect';
1901 grep { my $stage = $_->part_event->event_stage;
1902 $opt{stage} eq $stage or ( ! $stage && $opt{stage} eq 'collect' )
1912 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
1913 'stats_hashref' => \%unsat ),
1916 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
1919 warn " invalid conditions not eliminated with condition_sql:\n".
1920 join('', map " $_: ".$unsat{$_}."\n", keys %unsat )
1921 if keys %unsat && $DEBUG; # > 1;
1927 unless( $opt{testonly} ) {
1928 foreach my $cust_event ( @cust_event ) {
1930 my $error = $cust_event->insert();
1932 $dbh->rollback if $oldAutoCommit;
1939 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1945 warn " returning events: ". Dumper(@cust_event). "\n"
1952 =item apply_payments_and_credits [ OPTION => VALUE ... ]
1954 Applies unapplied payments and credits.
1956 In most cases, this new method should be used in place of sequential
1957 apply_payments and apply_credits methods.
1959 A hash of optional arguments may be passed. Currently "manual" is supported.
1960 If true, a payment receipt is sent instead of a statement when
1961 'payment_receipt_email' configuration option is set.
1963 If there is an error, returns the error, otherwise returns false.
1967 sub apply_payments_and_credits {
1968 my( $self, %options ) = @_;
1970 local $SIG{HUP} = 'IGNORE';
1971 local $SIG{INT} = 'IGNORE';
1972 local $SIG{QUIT} = 'IGNORE';
1973 local $SIG{TERM} = 'IGNORE';
1974 local $SIG{TSTP} = 'IGNORE';
1975 local $SIG{PIPE} = 'IGNORE';
1977 my $oldAutoCommit = $FS::UID::AutoCommit;
1978 local $FS::UID::AutoCommit = 0;
1981 $self->select_for_update; #mutex
1983 foreach my $cust_bill ( $self->open_cust_bill ) {
1984 my $error = $cust_bill->apply_payments_and_credits(%options);
1986 $dbh->rollback if $oldAutoCommit;
1987 return "Error applying: $error";
1991 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1996 =item apply_credits OPTION => VALUE ...
1998 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
1999 to outstanding invoice balances in chronological order (or reverse
2000 chronological order if the I<order> option is set to B<newest>) and returns the
2001 value of any remaining unapplied credits available for refund (see
2002 L<FS::cust_refund>).
2004 Dies if there is an error.
2012 local $SIG{HUP} = 'IGNORE';
2013 local $SIG{INT} = 'IGNORE';
2014 local $SIG{QUIT} = 'IGNORE';
2015 local $SIG{TERM} = 'IGNORE';
2016 local $SIG{TSTP} = 'IGNORE';
2017 local $SIG{PIPE} = 'IGNORE';
2019 my $oldAutoCommit = $FS::UID::AutoCommit;
2020 local $FS::UID::AutoCommit = 0;
2023 $self->select_for_update; #mutex
2025 unless ( $self->total_unapplied_credits ) {
2026 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2030 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
2031 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
2033 my @invoices = $self->open_cust_bill;
2034 @invoices = sort { $b->_date <=> $a->_date } @invoices
2035 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
2037 if ( $conf->exists('pkg-balances') ) {
2038 # limit @credits to those w/ a pkgnum grepped from $self
2040 foreach my $i (@invoices) {
2041 foreach my $li ( $i->cust_bill_pkg ) {
2042 $pkgnums{$li->pkgnum} = 1;
2045 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
2050 foreach my $cust_bill ( @invoices ) {
2052 if ( !defined($credit) || $credit->credited == 0) {
2053 $credit = pop @credits or last;
2057 if ( $conf->exists('pkg-balances') && $credit->pkgnum ) {
2058 $owed = $cust_bill->owed_pkgnum($credit->pkgnum);
2060 $owed = $cust_bill->owed;
2062 unless ( $owed > 0 ) {
2063 push @credits, $credit;
2067 my $amount = min( $credit->credited, $owed );
2069 my $cust_credit_bill = new FS::cust_credit_bill ( {
2070 'crednum' => $credit->crednum,
2071 'invnum' => $cust_bill->invnum,
2072 'amount' => $amount,
2074 $cust_credit_bill->pkgnum( $credit->pkgnum )
2075 if $conf->exists('pkg-balances') && $credit->pkgnum;
2076 my $error = $cust_credit_bill->insert;
2078 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2082 redo if ($cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
2086 my $total_unapplied_credits = $self->total_unapplied_credits;
2088 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2090 return $total_unapplied_credits;
2093 =item apply_payments [ OPTION => VALUE ... ]
2095 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
2096 to outstanding invoice balances in chronological order.
2098 #and returns the value of any remaining unapplied payments.
2100 A hash of optional arguments may be passed. Currently "manual" is supported.
2101 If true, a payment receipt is sent instead of a statement when
2102 'payment_receipt_email' configuration option is set.
2104 Dies if there is an error.
2108 sub apply_payments {
2109 my( $self, %options ) = @_;
2111 local $SIG{HUP} = 'IGNORE';
2112 local $SIG{INT} = 'IGNORE';
2113 local $SIG{QUIT} = 'IGNORE';
2114 local $SIG{TERM} = 'IGNORE';
2115 local $SIG{TSTP} = 'IGNORE';
2116 local $SIG{PIPE} = 'IGNORE';
2118 my $oldAutoCommit = $FS::UID::AutoCommit;
2119 local $FS::UID::AutoCommit = 0;
2122 $self->select_for_update; #mutex
2126 my @payments = sort { $b->_date <=> $a->_date }
2127 grep { $_->unapplied > 0 }
2130 my @invoices = sort { $a->_date <=> $b->_date}
2131 grep { $_->owed > 0 }
2134 if ( $conf->exists('pkg-balances') ) {
2135 # limit @payments to those w/ a pkgnum grepped from $self
2137 foreach my $i (@invoices) {
2138 foreach my $li ( $i->cust_bill_pkg ) {
2139 $pkgnums{$li->pkgnum} = 1;
2142 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
2147 foreach my $cust_bill ( @invoices ) {
2149 if ( !defined($payment) || $payment->unapplied == 0 ) {
2150 $payment = pop @payments or last;
2154 if ( $conf->exists('pkg-balances') && $payment->pkgnum ) {
2155 $owed = $cust_bill->owed_pkgnum($payment->pkgnum);
2157 $owed = $cust_bill->owed;
2159 unless ( $owed > 0 ) {
2160 push @payments, $payment;
2164 my $amount = min( $payment->unapplied, $owed );
2167 'paynum' => $payment->paynum,
2168 'invnum' => $cust_bill->invnum,
2169 'amount' => $amount,
2171 $cbp->{_date} = $payment->_date
2172 if $options{'manual'} && $options{'backdate_application'};
2173 my $cust_bill_pay = new FS::cust_bill_pay($cbp);
2174 $cust_bill_pay->pkgnum( $payment->pkgnum )
2175 if $conf->exists('pkg-balances') && $payment->pkgnum;
2176 my $error = $cust_bill_pay->insert(%options);
2178 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2182 redo if ( $cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
2186 my $total_unapplied_payments = $self->total_unapplied_payments;
2188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2190 return $total_unapplied_payments;
2200 suspend_adjourned_pkgs
2201 unsuspend_resumed_pkgs
2204 (do_cust_event pre-bill)
2207 (vendor-only) _gather_taxes
2208 _omit_zero_value_bundles
2211 apply_payments_and_credits
2220 L<FS::cust_main>, L<FS::cust_main::Billing_Realtime>