1 package FS::cust_main::Billing;
4 use vars qw( $conf $DEBUG $me );
6 use List::Util qw( min );
8 use FS::Record qw( qsearch qsearchs dbdef );
10 use FS::cust_bill_pkg;
11 use FS::cust_bill_pkg_display;
12 use FS::cust_bill_pay;
13 use FS::cust_credit_bill;
15 use FS::cust_tax_adjustment;
17 use FS::tax_rate_location;
18 use FS::cust_bill_pkg_tax_location;
19 use FS::cust_bill_pkg_tax_rate_location;
21 use FS::part_event_condition;
23 # 1 is mostly method/subroutine entry and options
24 # 2 traces progress of some operations
25 # 3 is even more information including possibly sensitive data
27 $me = '[FS::cust_main::Billing]';
29 install_callback FS::UID sub {
31 #yes, need it for stuff below (prolly should be cached)
36 FS::cust_main::Billing - Billing mixin for cust_main
42 These methods are available on FS::cust_main objects.
48 =item bill_and_collect
50 Cancels and suspends any packages due, generates bills, applies payments and
51 credits, and applies collection events to run cards, send bills and notices,
54 By default, warns on errors and continues with the next operation (but see the
57 Options are passed as name-value pairs. Currently available options are:
63 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:
67 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
71 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.
75 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
79 If set true, re-charges setup fees.
83 If set any errors prevent subsequent operations from continusing. If set
84 specifically to "return", returns the error (or false, if there is no error).
85 Any other true value causes errors to die.
89 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)
93 Optional FS::queue entry to receive status updates.
97 Options are passed to the B<bill> and B<collect> methods verbatim, so all
98 options of those methods are also available.
102 sub bill_and_collect {
103 my( $self, %options ) = @_;
107 #$options{actual_time} not $options{time} because freeside-daily -d is for
108 #pre-printing invoices
110 $options{'actual_time'} ||= time;
111 my $job = $options{'job'};
113 $job->update_statustext('0,cleaning expired packages') if $job;
114 $error = $self->cancel_expired_pkgs( $options{actual_time} );
116 $error = "Error expiring custnum ". $self->custnum. ": $error";
117 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
118 elsif ( $options{fatal} ) { die $error; }
119 else { warn $error; }
122 $error = $self->suspend_adjourned_pkgs( $options{actual_time} );
124 $error = "Error adjourning custnum ". $self->custnum. ": $error";
125 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
126 elsif ( $options{fatal} ) { die $error; }
127 else { warn $error; }
130 $job->update_statustext('20,billing packages') if $job;
131 $error = $self->bill( %options );
133 $error = "Error billing custnum ". $self->custnum. ": $error";
134 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
135 elsif ( $options{fatal} ) { die $error; }
136 else { warn $error; }
139 $job->update_statustext('50,applying payments and credits') if $job;
140 $error = $self->apply_payments_and_credits;
142 $error = "Error applying custnum ". $self->custnum. ": $error";
143 if ( $options{fatal} && $options{fatal} eq 'return' ) { return $error; }
144 elsif ( $options{fatal} ) { die $error; }
145 else { warn $error; }
148 $job->update_statustext('70,running collection events') if $job;
149 unless ( $conf->exists('cancelled_cust-noevents')
150 && ! $self->num_ncancelled_pkgs
152 $error = $self->collect( %options );
154 $error = "Error collecting custnum ". $self->custnum. ": $error";
155 if ($options{fatal} && $options{fatal} eq 'return') { return $error; }
156 elsif ($options{fatal} ) { die $error; }
157 else { warn $error; }
160 $job->update_statustext('100,finished') if $job;
166 sub cancel_expired_pkgs {
167 my ( $self, $time, %options ) = @_;
169 my @cancel_pkgs = $self->ncancelled_pkgs( {
170 'extra_sql' => " AND expire IS NOT NULL AND expire > 0 AND expire <= $time "
175 foreach my $cust_pkg ( @cancel_pkgs ) {
176 my $cpr = $cust_pkg->last_cust_pkg_reason('expire');
177 my $error = $cust_pkg->cancel($cpr ? ( 'reason' => $cpr->reasonnum,
178 'reason_otaker' => $cpr->otaker
182 push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
185 scalar(@errors) ? join(' / ', @errors) : '';
189 sub suspend_adjourned_pkgs {
190 my ( $self, $time, %options ) = @_;
192 my @susp_pkgs = $self->ncancelled_pkgs( {
194 " AND ( susp IS NULL OR susp = 0 )
195 AND ( ( bill IS NOT NULL AND bill != 0 AND bill < $time )
196 OR ( adjourn IS NOT NULL AND adjourn != 0 AND adjourn <= $time )
201 #only because there's no SQL test for is_prepaid :/
203 grep { ( $_->part_pkg->is_prepaid
208 && $_->adjourn <= $time
216 foreach my $cust_pkg ( @susp_pkgs ) {
217 my $cpr = $cust_pkg->last_cust_pkg_reason('adjourn')
218 if ($cust_pkg->adjourn && $cust_pkg->adjourn < $^T);
219 my $error = $cust_pkg->suspend($cpr ? ( 'reason' => $cpr->reasonnum,
220 'reason_otaker' => $cpr->otaker
224 push @errors, 'pkgnum '.$cust_pkg->pkgnum.": $error" if $error;
227 scalar(@errors) ? join(' / ', @errors) : '';
233 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
234 conjunction with the collect method by calling B<bill_and_collect>.
236 If there is an error, returns the error, otherwise returns false.
238 Options are passed as name-value pairs. Currently available options are:
244 If set true, re-charges setup fees.
248 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:
252 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
256 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
258 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
262 A hashref of pkgparts to exclude from this billing run (can also be specified as a comma-separated scalar).
266 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.
270 This boolean value informs the us that the package is being cancelled. This
271 typically might mean not charging the normal recurring fee but only usage
272 fees since the last billing. Setup charges may be charged. Not all package
273 plans support this feature (they tend to charge 0).
277 Optional terms to be printed on this invoice. Otherwise, customer-specific
278 terms or the default terms are used.
285 my( $self, %options ) = @_;
286 return '' if $self->payby eq 'COMP';
287 warn "$me bill customer ". $self->custnum. "\n"
290 my $time = $options{'time'} || time;
291 my $invoice_time = $options{'invoice_time'} || $time;
293 $options{'not_pkgpart'} ||= {};
294 $options{'not_pkgpart'} = { map { $_ => 1 }
295 split(/\s*,\s*/, $options{'not_pkgpart'})
297 unless ref($options{'not_pkgpart'});
299 local $SIG{HUP} = 'IGNORE';
300 local $SIG{INT} = 'IGNORE';
301 local $SIG{QUIT} = 'IGNORE';
302 local $SIG{TERM} = 'IGNORE';
303 local $SIG{TSTP} = 'IGNORE';
304 local $SIG{PIPE} = 'IGNORE';
306 my $oldAutoCommit = $FS::UID::AutoCommit;
307 local $FS::UID::AutoCommit = 0;
310 warn "$me acquiring lock on customer ". $self->custnum. "\n"
313 $self->select_for_update; #mutex
315 warn "$me running pre-bill events for customer ". $self->custnum. "\n"
318 my $error = $self->do_cust_event(
319 'debug' => ( $options{'debug'} || 0 ),
320 'time' => $invoice_time,
321 'check_freq' => $options{'check_freq'},
322 'stage' => 'pre-bill',
325 $dbh->rollback if $oldAutoCommit;
329 warn "$me done running pre-bill events for customer ". $self->custnum. "\n"
332 #keep auto-charge and non-auto-charge line items separate
333 my @passes = ( '', 'no_auto' );
335 my %cust_bill_pkg = map { $_ => [] } @passes;
338 # find the packages which are due for billing, find out how much they are
339 # & generate invoice database.
342 my %total_setup = map { my $z = 0; $_ => \$z; } @passes;
343 my %total_recur = map { my $z = 0; $_ => \$z; } @passes;
345 my %taxlisthash = map { $_ => {} } @passes;
347 my @precommit_hooks = ();
349 $options{'pkg_list'} ||= [ $self->ncancelled_pkgs ]; #param checks?
350 foreach my $cust_pkg ( @{ $options{'pkg_list'} } ) {
352 next if $options{'not_pkgpart'}->{$cust_pkg->pkgpart};
354 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
356 #? to avoid use of uninitialized value errors... ?
357 $cust_pkg->setfield('bill', '')
358 unless defined($cust_pkg->bill);
360 #my $part_pkg = $cust_pkg->part_pkg;
362 my $real_pkgpart = $cust_pkg->pkgpart;
363 my %hash = $cust_pkg->hash;
365 # we could implement this bit as FS::part_pkg::has_hidden, but we already
366 # suffer from performance issues
367 $options{has_hidden} = 0;
368 my @part_pkg = $cust_pkg->part_pkg->self_and_bill_linked;
369 $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
371 foreach my $part_pkg ( @part_pkg ) {
373 $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
375 my $pass = ($cust_pkg->no_auto || $part_pkg->no_auto) ? 'no_auto' : '';
378 $self->_make_lines( 'part_pkg' => $part_pkg,
379 'cust_pkg' => $cust_pkg,
380 'precommit_hooks' => \@precommit_hooks,
381 'line_items' => $cust_bill_pkg{$pass},
382 'setup' => $total_setup{$pass},
383 'recur' => $total_recur{$pass},
384 'tax_matrix' => $taxlisthash{$pass},
386 'real_pkgpart' => $real_pkgpart,
387 'options' => \%options,
390 $dbh->rollback if $oldAutoCommit;
394 } #foreach my $part_pkg
396 } #foreach my $cust_pkg
398 #if the customer isn't on an automatic payby, everything can go on a single
400 #if ( $cust_main->payby !~ /^(CARD|CHEK)$/ ) {
401 #merge everything into one list
404 foreach my $pass (@passes) { # keys %cust_bill_pkg ) {
406 my @cust_bill_pkg = _omit_zero_value_bundles(@{ $cust_bill_pkg{$pass} });
408 next unless @cust_bill_pkg; #don't create an invoice w/o line items
410 if ( scalar( grep { $_->recur && $_->recur > 0 } @cust_bill_pkg) ||
411 !$conf->exists('postal_invoice-recurring_only')
415 my $postal_pkg = $self->charge_postal_fee();
416 if ( $postal_pkg && !ref( $postal_pkg ) ) {
418 $dbh->rollback if $oldAutoCommit;
419 return "can't charge postal invoice fee for customer ".
420 $self->custnum. ": $postal_pkg";
422 } elsif ( $postal_pkg ) {
424 my $real_pkgpart = $postal_pkg->pkgpart;
425 # we could implement this bit as FS::part_pkg::has_hidden, but we already
426 # suffer from performance issues
427 $options{has_hidden} = 0;
428 my @part_pkg = $postal_pkg->part_pkg->self_and_bill_linked;
429 $options{has_hidden} = 1 if ($part_pkg[1] && $part_pkg[1]->hidden);
431 foreach my $part_pkg ( @part_pkg ) {
432 my %postal_options = %options;
433 delete $postal_options{cancel};
435 $self->_make_lines( 'part_pkg' => $part_pkg,
436 'cust_pkg' => $postal_pkg,
437 'precommit_hooks' => \@precommit_hooks,
438 'line_items' => \@cust_bill_pkg,
439 'setup' => $total_setup{$pass},
440 'recur' => $total_recur{$pass},
441 'tax_matrix' => $taxlisthash{$pass},
443 'real_pkgpart' => $real_pkgpart,
444 'options' => \%postal_options,
447 $dbh->rollback if $oldAutoCommit;
452 # it's silly to have a zero value postal_pkg, but....
453 @cust_bill_pkg = _omit_zero_value_bundles(@cust_bill_pkg);
459 my $listref_or_error =
460 $self->calculate_taxes( \@cust_bill_pkg, $taxlisthash{$pass}, $invoice_time);
462 unless ( ref( $listref_or_error ) ) {
463 $dbh->rollback if $oldAutoCommit;
464 return $listref_or_error;
467 foreach my $taxline ( @$listref_or_error ) {
468 ${ $total_setup{$pass} } =
469 sprintf('%.2f', ${ $total_setup{$pass} } + $taxline->setup );
470 push @cust_bill_pkg, $taxline;
474 warn "adding tax adjustments...\n" if $DEBUG > 2;
475 foreach my $cust_tax_adjustment (
476 qsearch('cust_tax_adjustment', { 'custnum' => $self->custnum,
482 my $tax = sprintf('%.2f', $cust_tax_adjustment->amount );
484 my $itemdesc = $cust_tax_adjustment->taxname;
485 $itemdesc = '' if $itemdesc eq 'Tax';
487 push @cust_bill_pkg, new FS::cust_bill_pkg {
493 'itemdesc' => $itemdesc,
494 'itemcomment' => $cust_tax_adjustment->comment,
495 'cust_tax_adjustment' => $cust_tax_adjustment,
496 #'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
501 my $charged = sprintf('%.2f', ${ $total_setup{$pass} } + ${ $total_recur{$pass} } );
503 my @cust_bill = $self->cust_bill;
504 my $balance = $self->balance;
505 my $previous_balance = scalar(@cust_bill)
506 ? ( $cust_bill[$#cust_bill]->billing_balance || 0 )
509 $previous_balance += $cust_bill[$#cust_bill]->charged
510 if scalar(@cust_bill);
511 #my $balance_adjustments =
512 # sprintf('%.2f', $balance - $prior_prior_balance - $prior_charged);
514 #create the new invoice
515 my $cust_bill = new FS::cust_bill ( {
516 'custnum' => $self->custnum,
517 '_date' => ( $invoice_time ),
518 'charged' => $charged,
519 'billing_balance' => $balance,
520 'previous_balance' => $previous_balance,
521 'invoice_terms' => $options{'invoice_terms'},
523 $error = $cust_bill->insert;
525 $dbh->rollback if $oldAutoCommit;
526 return "can't create invoice for customer #". $self->custnum. ": $error";
529 foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
530 $cust_bill_pkg->invnum($cust_bill->invnum);
531 my $error = $cust_bill_pkg->insert;
533 $dbh->rollback if $oldAutoCommit;
534 return "can't create invoice line item: $error";
538 } #foreach my $pass ( keys %cust_bill_pkg )
540 foreach my $hook ( @precommit_hooks ) {
545 $dbh->rollback if $oldAutoCommit;
546 return "$@ running precommit hook $hook\n";
550 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
554 #discard bundled packages of 0 value
555 sub _omit_zero_value_bundles {
557 my @cust_bill_pkg = ();
558 my @cust_bill_pkg_bundle = ();
561 foreach my $cust_bill_pkg ( @_ ) {
562 if (scalar(@cust_bill_pkg_bundle) && !$cust_bill_pkg->pkgpart_override) {
563 push @cust_bill_pkg, @cust_bill_pkg_bundle if $sum > 0;
564 @cust_bill_pkg_bundle = ();
567 $sum += $cust_bill_pkg->setup + $cust_bill_pkg->recur;
568 push @cust_bill_pkg_bundle, $cust_bill_pkg;
570 push @cust_bill_pkg, @cust_bill_pkg_bundle if $sum > 0;
576 =item calculate_taxes LINEITEMREF TAXHASHREF INVOICE_TIME
578 This is a weird one. Perhaps it should not even be exposed.
580 Generates tax line items (see L<FS::cust_bill_pkg>) for this customer.
581 Usually used internally by bill method B<bill>.
583 If there is an error, returns the error, otherwise returns reference to a
584 list of line items suitable for insertion.
590 An array ref of the line items being billed.
594 A strange beast. The keys to this hash are internal identifiers consisting
595 of the name of the tax object type, a space, and its unique identifier ( e.g.
596 'cust_main_county 23' ). The values of the hash are listrefs. The first
597 item in the list is the tax object. The remaining items are either line
598 items or floating point values (currency amounts).
600 The taxes are calculated on this entity. Calculated exemption records are
601 transferred to the LINEITEMREF items on the assumption that they are related.
607 This specifies the date appearing on the associated invoice. Some
608 jurisdictions (i.e. Texas) have tax exemptions which are date sensitive.
613 sub calculate_taxes {
614 my ($self, $cust_bill_pkg, $taxlisthash, $invoice_time) = @_;
616 my @tax_line_items = ();
618 warn "having a look at the taxes we found...\n" if $DEBUG > 2;
620 # keys are tax names (as printed on invoices / itemdesc )
621 # values are listrefs of taxlisthash keys (internal identifiers)
624 # keys are taxlisthash keys (internal identifiers)
625 # values are (cumulative) amounts
628 # keys are taxlisthash keys (internal identifiers)
629 # values are listrefs of cust_bill_pkg_tax_location hashrefs
630 my %tax_location = ();
632 # keys are taxlisthash keys (internal identifiers)
633 # values are listrefs of cust_bill_pkg_tax_rate_location hashrefs
634 my %tax_rate_location = ();
636 foreach my $tax ( keys %$taxlisthash ) {
637 my $tax_object = shift @{ $taxlisthash->{$tax} };
638 warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
639 warn " ". join('/', @{ $taxlisthash->{$tax} } ). "\n" if $DEBUG > 2;
640 my $hashref_or_error =
641 $tax_object->taxline( $taxlisthash->{$tax},
642 'custnum' => $self->custnum,
643 'invoice_time' => $invoice_time
645 return $hashref_or_error unless ref($hashref_or_error);
647 unshift @{ $taxlisthash->{$tax} }, $tax_object;
649 my $name = $hashref_or_error->{'name'};
650 my $amount = $hashref_or_error->{'amount'};
652 #warn "adding $amount as $name\n";
653 $taxname{ $name } ||= [];
654 push @{ $taxname{ $name } }, $tax;
656 $tax{ $tax } += $amount;
658 $tax_location{ $tax } ||= [];
659 if ( $tax_object->get('pkgnum') || $tax_object->get('locationnum') ) {
660 push @{ $tax_location{ $tax } },
662 'taxnum' => $tax_object->taxnum,
663 'taxtype' => ref($tax_object),
664 'pkgnum' => $tax_object->get('pkgnum'),
665 'locationnum' => $tax_object->get('locationnum'),
666 'amount' => sprintf('%.2f', $amount ),
670 $tax_rate_location{ $tax } ||= [];
671 if ( ref($tax_object) eq 'FS::tax_rate' ) {
672 my $taxratelocationnum =
673 $tax_object->tax_rate_location->taxratelocationnum;
674 push @{ $tax_rate_location{ $tax } },
676 'taxnum' => $tax_object->taxnum,
677 'taxtype' => ref($tax_object),
678 'amount' => sprintf('%.2f', $amount ),
679 'locationtaxid' => $tax_object->location,
680 'taxratelocationnum' => $taxratelocationnum,
686 #move the cust_tax_exempt_pkg records to the cust_bill_pkgs we will commit
687 my %packagemap = map { $_->pkgnum => $_ } @$cust_bill_pkg;
688 foreach my $tax ( keys %$taxlisthash ) {
689 foreach ( @{ $taxlisthash->{$tax} }[1 ... scalar(@{ $taxlisthash->{$tax} })] ) {
690 next unless ref($_) eq 'FS::cust_bill_pkg';
692 push @{ $packagemap{$_->pkgnum}->_cust_tax_exempt_pkg },
693 splice( @{ $_->_cust_tax_exempt_pkg } );
697 #consolidate and create tax line items
698 warn "consolidating and generating...\n" if $DEBUG > 2;
699 foreach my $taxname ( keys %taxname ) {
702 my @cust_bill_pkg_tax_location = ();
703 my @cust_bill_pkg_tax_rate_location = ();
704 warn "adding $taxname\n" if $DEBUG > 1;
705 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
706 next if $seen{$taxitem}++;
707 warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
708 $tax += $tax{$taxitem};
709 push @cust_bill_pkg_tax_location,
710 map { new FS::cust_bill_pkg_tax_location $_ }
711 @{ $tax_location{ $taxitem } };
712 push @cust_bill_pkg_tax_rate_location,
713 map { new FS::cust_bill_pkg_tax_rate_location $_ }
714 @{ $tax_rate_location{ $taxitem } };
718 $tax = sprintf('%.2f', $tax );
720 my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname,
726 if ( $pkg_category and
727 $conf->config('invoice_latexsummary') ||
728 $conf->config('invoice_htmlsummary')
732 my %hash = ( 'section' => $pkg_category->categoryname );
733 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
737 push @tax_line_items, new FS::cust_bill_pkg {
743 'itemdesc' => $taxname,
744 'display' => \@display,
745 'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
746 'cust_bill_pkg_tax_rate_location' => \@cust_bill_pkg_tax_rate_location,
755 my ($self, %params) = @_;
757 my $part_pkg = $params{part_pkg} or die "no part_pkg specified";
758 my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
759 my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
760 my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
761 my $total_setup = $params{setup} or die "no setup accumulator specified";
762 my $total_recur = $params{recur} or die "no recur accumulator specified";
763 my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
764 my $time = $params{'time'} or die "no time specified";
765 my (%options) = %{$params{options}};
768 my $real_pkgpart = $params{real_pkgpart};
769 my %hash = $cust_pkg->hash;
770 my $old_cust_pkg = new FS::cust_pkg \%hash;
776 $cust_pkg->pkgpart($part_pkg->pkgpart);
784 if ( $options{'resetup'}
785 || ( ! $cust_pkg->setup
786 && ( ! $cust_pkg->start_date
787 || $cust_pkg->start_date <= $time
789 && ( ! $conf->exists('disable_setup_suspended_pkgs')
790 || ( $conf->exists('disable_setup_suspended_pkgs') &&
791 ! $cust_pkg->getfield('susp')
798 warn " bill setup\n" if $DEBUG > 1;
801 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
802 return "$@ running calc_setup for $cust_pkg\n"
805 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
807 $cust_pkg->setfield('setup', $time)
808 unless $cust_pkg->setup;
809 #do need it, but it won't get written to the db
810 #|| $cust_pkg->pkgpart != $real_pkgpart;
812 $cust_pkg->setfield('start_date', '')
813 if $cust_pkg->start_date;
821 #XXX unit stuff here too
825 if ( ! $cust_pkg->get('susp')
826 and ! $cust_pkg->get('start_date')
827 and ( $part_pkg->getfield('freq') ne '0'
828 && ( $cust_pkg->getfield('bill') || 0 ) <= $time
830 || ( $part_pkg->plan eq 'voip_cdr'
831 && $part_pkg->option('bill_every_call')
833 || ( $options{cancel} )
836 # XXX should this be a package event? probably. events are called
837 # at collection time at the moment, though...
838 $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
839 if $part_pkg->can('reset_usage');
840 #don't want to reset usage just cause we want a line item??
841 #&& $part_pkg->pkgpart == $real_pkgpart;
843 warn " bill recur\n" if $DEBUG > 1;
846 # XXX shared with $recur_prog
847 $sdate = ( $options{cancel} ? $cust_pkg->last_bill : $cust_pkg->bill )
851 #over two params! lets at least switch to a hashref for the rest...
852 my $increment_next_bill = ( $part_pkg->freq ne '0'
853 && ( $cust_pkg->getfield('bill') || 0 ) <= $time
856 my %param = ( 'precommit_hooks' => $precommit_hooks,
857 'increment_next_bill' => $increment_next_bill,
858 'discounts' => \@discounts,
859 'real_pkgpart' => $real_pkgpart,
862 my $method = $options{cancel} ? 'calc_cancel' : 'calc_recur';
863 $recur = eval { $cust_pkg->$method( \$sdate, \@details, \%param ) };
864 return "$@ running $method for $cust_pkg\n"
867 if ( $increment_next_bill ) {
869 my $next_bill = $part_pkg->add_freq($sdate);
870 return "unparsable frequency: ". $part_pkg->freq
873 #pro-rating magic - if $recur_prog fiddled $sdate, want to use that
874 # only for figuring next bill date, nothing else, so, reset $sdate again
876 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
877 #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
878 $cust_pkg->last_bill($sdate);
880 $cust_pkg->setfield('bill', $next_bill );
886 warn "\$setup is undefined" unless defined($setup);
887 warn "\$recur is undefined" unless defined($recur);
888 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
891 # If there's line items, create em cust_bill_pkg records
892 # If $cust_pkg has been modified, update it (if we're a real pkgpart)
895 if ( $lineitems || $options{has_hidden} ) {
897 if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
898 # hmm.. and if just the options are modified in some weird price plan?
900 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
903 my $error = $cust_pkg->replace( $old_cust_pkg,
904 'options' => { $cust_pkg->options },
906 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error"
907 if $error; #just in case
910 $setup = sprintf( "%.2f", $setup );
911 $recur = sprintf( "%.2f", $recur );
912 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
913 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
915 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
916 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
921 !$part_pkg->hidden && $options{has_hidden} ) #include some $0 lines
924 warn " charges (setup=$setup, recur=$recur); adding line items\n"
927 my @cust_pkg_detail = map { $_->detail } $cust_pkg->cust_pkg_detail('I');
929 warn " adding customer package invoice detail: $_\n"
930 foreach @cust_pkg_detail;
932 push @details, @cust_pkg_detail;
934 my $cust_bill_pkg = new FS::cust_bill_pkg {
935 'pkgnum' => $cust_pkg->pkgnum,
937 'unitsetup' => $unitsetup,
939 'unitrecur' => $unitrecur,
940 'quantity' => $cust_pkg->quantity,
941 'details' => \@details,
942 'discounts' => \@discounts,
943 'hidden' => $part_pkg->hidden,
946 if ( $part_pkg->option('recur_temporality', 1) eq 'preceding' ) {
947 $cust_bill_pkg->sdate( $hash{last_bill} );
948 $cust_bill_pkg->edate( $sdate - 86399 ); #60s*60m*24h-1
949 $cust_bill_pkg->edate( $time ) if $options{cancel};
950 } else { #if ( $part_pkg->option('recur_temporality', 1) eq 'upcoming' ) {
951 $cust_bill_pkg->sdate( $sdate );
952 $cust_bill_pkg->edate( $cust_pkg->bill );
953 #$cust_bill_pkg->edate( $time ) if $options{cancel};
956 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
957 unless $part_pkg->pkgpart == $real_pkgpart;
959 $$total_setup += $setup;
960 $$total_recur += $recur;
967 $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart, \%options);
968 return $error if $error;
970 push @$cust_bill_pkgs, $cust_bill_pkg;
972 } #if $setup != 0 || $recur != 0
982 my $part_pkg = shift;
983 my $taxlisthash = shift;
984 my $cust_bill_pkg = shift;
985 my $cust_pkg = shift;
986 my $invoice_time = shift;
987 my $real_pkgpart = shift;
990 my %cust_bill_pkg = ();
994 #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
995 push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
996 push @classes, 'setup' if ($cust_bill_pkg->setup && !$options->{cancel});
997 push @classes, 'recur' if ($cust_bill_pkg->recur && !$options->{cancel});
999 if ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
1001 if ( $conf->exists('enable_taxproducts')
1002 && ( scalar($part_pkg->part_pkg_taxoverride)
1003 || $part_pkg->has_taxproduct
1008 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1009 return "fatal: Can't (yet) use tax-pkg_address with taxproducts";
1012 foreach my $class (@classes) {
1013 my $err_or_ref = $self->_gather_taxes( $part_pkg, $class );
1014 return $err_or_ref unless ref($err_or_ref);
1015 $taxes{$class} = $err_or_ref;
1018 unless (exists $taxes{''}) {
1019 my $err_or_ref = $self->_gather_taxes( $part_pkg, '' );
1020 return $err_or_ref unless ref($err_or_ref);
1021 $taxes{''} = $err_or_ref;
1026 my @loc_keys = qw( city county state country );
1028 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1029 my $cust_location = $cust_pkg->cust_location;
1030 %taxhash = map { $_ => $cust_location->$_() } @loc_keys;
1033 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
1036 %taxhash = map { $_ => $self->get("$prefix$_") } @loc_keys;
1039 $taxhash{'taxclass'} = $part_pkg->taxclass;
1042 my %taxhash_elim = %taxhash;
1043 my @elim = qw( city county state );
1046 #first try a match with taxclass
1047 @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
1049 if ( !scalar(@taxes) && $taxhash_elim{'taxclass'} ) {
1050 #then try a match without taxclass
1051 my %no_taxclass = %taxhash_elim;
1052 $no_taxclass{ 'taxclass' } = '';
1053 @taxes = qsearch( 'cust_main_county', \%no_taxclass );
1056 $taxhash_elim{ shift(@elim) } = '';
1058 } while ( !scalar(@taxes) && scalar(@elim) );
1060 @taxes = grep { ! $_->taxname or ! $self->tax_exemption($_->taxname) }
1062 if $self->cust_main_exemption; #just to be safe
1064 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
1066 $_->set('pkgnum', $cust_pkg->pkgnum );
1067 $_->set('locationnum', $cust_pkg->locationnum );
1071 $taxes{''} = [ @taxes ];
1072 $taxes{'setup'} = [ @taxes ];
1073 $taxes{'recur'} = [ @taxes ];
1074 $taxes{$_} = [ @taxes ] foreach (@classes);
1076 # # maybe eliminate this entirely, along with all the 0% records
1077 # unless ( @taxes ) {
1079 # "fatal: can't find tax rate for state/county/country/taxclass ".
1080 # join('/', map $taxhash{$_}, qw(state county country taxclass) );
1083 } #if $conf->exists('enable_taxproducts') ...
1088 my $separate = $conf->exists('separate_usage');
1089 my $temp_pkg = new FS::cust_pkg { pkgpart => $real_pkgpart };
1090 my $usage_mandate = $temp_pkg->part_pkg->option('usage_mandate', 'Hush!');
1091 my $section = $temp_pkg->part_pkg->categoryname;
1092 if ( $separate || $section || $usage_mandate ) {
1094 my %hash = ( 'section' => $section );
1096 $section = $temp_pkg->part_pkg->option('usage_section', 'Hush!');
1097 my $summary = $temp_pkg->part_pkg->option('summarize_usage', 'Hush!');
1099 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
1100 push @display, new FS::cust_bill_pkg_display { type => 'R', %hash };
1102 push @display, new FS::cust_bill_pkg_display
1105 ( ( $usage_mandate ) ? ( 'summary' => 'Y' ) : () ),
1109 if ($separate && $section && $summary) {
1110 push @display, new FS::cust_bill_pkg_display { type => 'U',
1115 if ($usage_mandate || $section && $summary) {
1116 $hash{post_total} = 'Y';
1119 if ($separate || $usage_mandate) {
1120 $hash{section} = $section if ($separate || $usage_mandate);
1121 push @display, new FS::cust_bill_pkg_display { type => 'U', %hash };
1125 $cust_bill_pkg->set('display', \@display);
1127 my %tax_cust_bill_pkg = $cust_bill_pkg->disintegrate;
1128 foreach my $key (keys %tax_cust_bill_pkg) {
1129 my @taxes = @{ $taxes{$key} || [] };
1130 my $tax_cust_bill_pkg = $tax_cust_bill_pkg{$key};
1132 my %localtaxlisthash = ();
1133 foreach my $tax ( @taxes ) {
1135 my $taxname = ref( $tax ). ' '. $tax->taxnum;
1136 # $taxname .= ' pkgnum'. $cust_pkg->pkgnum.
1137 # ' locationnum'. $cust_pkg->locationnum
1138 # if $conf->exists('tax-pkg_address') && $cust_pkg->locationnum;
1140 $taxlisthash->{ $taxname } ||= [ $tax ];
1141 push @{ $taxlisthash->{ $taxname } }, $tax_cust_bill_pkg;
1143 $localtaxlisthash{ $taxname } ||= [ $tax ];
1144 push @{ $localtaxlisthash{ $taxname } }, $tax_cust_bill_pkg;
1148 warn "finding taxed taxes...\n" if $DEBUG > 2;
1149 foreach my $tax ( keys %localtaxlisthash ) {
1150 my $tax_object = shift @{ $localtaxlisthash{$tax} };
1151 warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
1153 next unless $tax_object->can('tax_on_tax');
1155 foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
1156 my $totname = ref( $tot ). ' '. $tot->taxnum;
1158 warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
1160 next unless exists( $localtaxlisthash{ $totname } ); # only increase
1162 warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
1163 my $hashref_or_error =
1164 $tax_object->taxline( $localtaxlisthash{$tax},
1165 'custnum' => $self->custnum,
1166 'invoice_time' => $invoice_time,
1168 return $hashref_or_error
1169 unless ref($hashref_or_error);
1171 $taxlisthash->{ $totname } ||= [ $tot ];
1172 push @{ $taxlisthash->{ $totname } }, $hashref_or_error->{amount};
1184 my $part_pkg = shift;
1188 my $geocode = $self->geocode('cch');
1190 my @taxclassnums = map { $_->taxclassnum }
1191 $part_pkg->part_pkg_taxoverride($class);
1193 unless (@taxclassnums) {
1194 @taxclassnums = map { $_->taxclassnum }
1195 grep { $_->taxable eq 'Y' }
1196 $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
1198 warn "Found taxclassnum values of ". join(',', @taxclassnums)
1203 join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
1205 @taxes = qsearch({ 'table' => 'tax_rate',
1206 'hashref' => { 'geocode' => $geocode, },
1207 'extra_sql' => $extra_sql,
1209 if scalar(@taxclassnums);
1211 warn "Found taxes ".
1212 join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n"
1219 =item collect [ HASHREF | OPTION => VALUE ... ]
1221 (Attempt to) collect money for this customer's outstanding invoices (see
1222 L<FS::cust_bill>). Usually used after the bill method.
1224 Actions are now triggered by billing events; see L<FS::part_event> and the
1225 billing events web interface. Old-style invoice events (see
1226 L<FS::part_bill_event>) have been deprecated.
1228 If there is an error, returns the error, otherwise returns false.
1230 Options are passed as name-value pairs.
1232 Currently available options are:
1238 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.
1242 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1246 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1250 set true to surpress email card/ACH decline notices.
1254 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)
1260 # allows for one time override of normal customer billing method
1265 my( $self, %options ) = @_;
1266 my $invoice_time = $options{'invoice_time'} || time;
1269 local $SIG{HUP} = 'IGNORE';
1270 local $SIG{INT} = 'IGNORE';
1271 local $SIG{QUIT} = 'IGNORE';
1272 local $SIG{TERM} = 'IGNORE';
1273 local $SIG{TSTP} = 'IGNORE';
1274 local $SIG{PIPE} = 'IGNORE';
1276 my $oldAutoCommit = $FS::UID::AutoCommit;
1277 local $FS::UID::AutoCommit = 0;
1280 $self->select_for_update; #mutex
1283 my $balance = $self->balance;
1284 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
1287 if ( exists($options{'retry_card'}) ) {
1288 carp 'retry_card option passed to collect is deprecated; use retry';
1289 $options{'retry'} ||= $options{'retry_card'};
1291 if ( exists($options{'retry'}) && $options{'retry'} ) {
1292 my $error = $self->retry_realtime;
1294 $dbh->rollback if $oldAutoCommit;
1299 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1301 #never want to roll back an event just because it returned an error
1302 local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1304 $self->do_cust_event(
1305 'debug' => ( $options{'debug'} || 0 ),
1306 'time' => $invoice_time,
1307 'check_freq' => $options{'check_freq'},
1308 'stage' => 'collect',
1313 =item retry_realtime
1315 Schedules realtime / batch credit card / electronic check / LEC billing
1316 events for for retry. Useful if card information has changed or manual
1317 retry is desired. The 'collect' method must be called to actually retry
1320 Implementation details: For either this customer, or for each of this
1321 customer's open invoices, changes the status of the first "done" (with
1322 statustext error) realtime processing event to "failed".
1326 sub retry_realtime {
1329 local $SIG{HUP} = 'IGNORE';
1330 local $SIG{INT} = 'IGNORE';
1331 local $SIG{QUIT} = 'IGNORE';
1332 local $SIG{TERM} = 'IGNORE';
1333 local $SIG{TSTP} = 'IGNORE';
1334 local $SIG{PIPE} = 'IGNORE';
1336 my $oldAutoCommit = $FS::UID::AutoCommit;
1337 local $FS::UID::AutoCommit = 0;
1340 #a little false laziness w/due_cust_event (not too bad, really)
1342 my $join = FS::part_event_condition->join_conditions_sql;
1343 my $order = FS::part_event_condition->order_conditions_sql;
1346 . join ( ' OR ' , map {
1347 "( part_event.eventtable = " . dbh->quote($_)
1348 . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
1349 } FS::part_event->eventtables)
1352 #here is the agent virtualization
1353 my $agent_virt = " ( part_event.agentnum IS NULL
1354 OR part_event.agentnum = ". $self->agentnum. ' )';
1356 #XXX this shouldn't be hardcoded, actions should declare it...
1357 my @realtime_events = qw(
1358 cust_bill_realtime_card
1359 cust_bill_realtime_check
1360 cust_bill_realtime_lec
1364 my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
1369 my @cust_event = qsearchs({
1370 'table' => 'cust_event',
1371 'select' => 'cust_event.*',
1372 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
1373 'hashref' => { 'status' => 'done' },
1374 'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
1375 " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
1378 my %seen_invnum = ();
1379 foreach my $cust_event (@cust_event) {
1381 #max one for the customer, one for each open invoice
1382 my $cust_X = $cust_event->cust_X;
1383 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
1387 or $cust_event->part_event->eventtable eq 'cust_bill'
1390 my $error = $cust_event->retry;
1392 $dbh->rollback if $oldAutoCommit;
1393 return "error scheduling event for retry: $error";
1398 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1403 =item do_cust_event [ HASHREF | OPTION => VALUE ... ]
1405 Runs billing events; see L<FS::part_event> and the billing events web
1408 If there is an error, returns the error, otherwise returns false.
1410 Options are passed as name-value pairs.
1412 Currently available options are:
1418 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.
1422 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1426 "collect" (the default) or "pre-bill"
1430 set true to surpress email card/ACH decline notices.
1434 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)
1440 # allows for one time override of normal customer billing method
1444 # Retry card/echeck/LEC transactions even when not scheduled by invoice events.
1447 my( $self, %options ) = @_;
1448 my $time = $options{'time'} || time;
1451 local $SIG{HUP} = 'IGNORE';
1452 local $SIG{INT} = 'IGNORE';
1453 local $SIG{QUIT} = 'IGNORE';
1454 local $SIG{TERM} = 'IGNORE';
1455 local $SIG{TSTP} = 'IGNORE';
1456 local $SIG{PIPE} = 'IGNORE';
1458 my $oldAutoCommit = $FS::UID::AutoCommit;
1459 local $FS::UID::AutoCommit = 0;
1462 $self->select_for_update; #mutex
1465 my $balance = $self->balance;
1466 warn "$me do_cust_event customer ". $self->custnum. ": balance $balance\n"
1469 # if ( exists($options{'retry_card'}) ) {
1470 # carp 'retry_card option passed to collect is deprecated; use retry';
1471 # $options{'retry'} ||= $options{'retry_card'};
1473 # if ( exists($options{'retry'}) && $options{'retry'} ) {
1474 # my $error = $self->retry_realtime;
1476 # $dbh->rollback if $oldAutoCommit;
1481 # false laziness w/pay_batch::import_results
1483 my $due_cust_event = $self->due_cust_event(
1484 'debug' => ( $options{'debug'} || 0 ),
1486 'check_freq' => $options{'check_freq'},
1487 'stage' => ( $options{'stage'} || 'collect' ),
1489 unless( ref($due_cust_event) ) {
1490 $dbh->rollback if $oldAutoCommit;
1491 return $due_cust_event;
1494 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1495 #never want to roll back an event just because it or a different one
1497 local $FS::UID::AutoCommit = 1; #$oldAutoCommit;
1499 foreach my $cust_event ( @$due_cust_event ) {
1503 #re-eval event conditions (a previous event could have changed things)
1504 unless ( $cust_event->test_conditions( 'time' => $time ) ) {
1505 #don't leave stray "new/locked" records around
1506 my $error = $cust_event->delete;
1507 return $error if $error;
1512 local $FS::cust_main::Billing_Realtime::realtime_bop_decline_quiet = 1
1513 if $options{'quiet'};
1514 warn " running cust_event ". $cust_event->eventnum. "\n"
1517 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
1518 if ( my $error = $cust_event->do_event() ) {
1519 #XXX wtf is this? figure out a proper dealio with return value
1531 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
1533 Inserts database records for and returns an ordered listref of new events due
1534 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
1535 events are due, an empty listref is returned. If there is an error, returns a
1536 scalar error message.
1538 To actually run the events, call each event's test_condition method, and if
1539 still true, call the event's do_event method.
1541 Options are passed as a hashref or as a list of name-value pairs. Available
1548 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.
1552 "collect" (the default) or "pre-bill"
1556 "Current time" for the events.
1560 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 Only return events for the specified eventtable (by default, events of all eventtables are returned)
1568 Explicitly pass the objects to be tested (typically used with eventtable).
1572 Set to true to return the objects, but not actually insert them into the
1579 sub due_cust_event {
1581 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
1584 #my $DEBUG = $opt{'debug'}
1585 local($DEBUG) = $opt{'debug'}
1586 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
1588 warn "$me due_cust_event called with options ".
1589 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
1592 $opt{'time'} ||= time;
1594 local $SIG{HUP} = 'IGNORE';
1595 local $SIG{INT} = 'IGNORE';
1596 local $SIG{QUIT} = 'IGNORE';
1597 local $SIG{TERM} = 'IGNORE';
1598 local $SIG{TSTP} = 'IGNORE';
1599 local $SIG{PIPE} = 'IGNORE';
1601 my $oldAutoCommit = $FS::UID::AutoCommit;
1602 local $FS::UID::AutoCommit = 0;
1605 $self->select_for_update #mutex
1606 unless $opt{testonly};
1609 # find possible events (initial search)
1612 my @cust_event = ();
1614 my @eventtable = $opt{'eventtable'}
1615 ? ( $opt{'eventtable'} )
1616 : FS::part_event->eventtables_runorder;
1618 my $check_freq = $opt{'check_freq'} || '1d';
1620 foreach my $eventtable ( @eventtable ) {
1623 if ( $opt{'objects'} ) {
1625 @objects = @{ $opt{'objects'} };
1629 #my @objects = $self->$eventtable(); # sub cust_main { @{ [ $self ] }; }
1630 if ( $eventtable eq 'cust_main' ) {
1631 @objects = ( $self );
1635 "LEFT JOIN cust_main USING ( custnum )";
1637 #some false laziness w/Cron::bill bill_where
1639 my $join = FS::part_event_condition->join_conditions_sql( $eventtable);
1640 my $where = FS::part_event_condition->where_conditions_sql($eventtable,
1641 'time'=>$opt{'time'},
1643 $where = $where ? "AND $where" : '';
1645 my $are_part_event =
1646 "EXISTS ( SELECT 1 FROM part_event $join
1647 WHERE check_freq = '$check_freq'
1648 AND eventtable = '$eventtable'
1649 AND ( disabled = '' OR disabled IS NULL )
1655 @objects = $self->$eventtable(
1656 'addl_from' => $cm_join,
1657 'extra_sql' => " AND $are_part_event",
1663 my @e_cust_event = ();
1665 my $cross = "CROSS JOIN $eventtable";
1666 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
1667 unless $eventtable eq 'cust_main';
1669 foreach my $object ( @objects ) {
1671 #this first search uses the condition_sql magic for optimization.
1672 #the more possible events we can eliminate in this step the better
1674 my $cross_where = '';
1675 my $pkey = $object->primary_key;
1676 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
1678 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
1680 FS::part_event_condition->where_conditions_sql( $eventtable,
1681 'time'=>$opt{'time'}
1683 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
1685 $extra_sql = "AND $extra_sql" if $extra_sql;
1687 #here is the agent virtualization
1688 $extra_sql .= " AND ( part_event.agentnum IS NULL
1689 OR part_event.agentnum = ". $self->agentnum. ' )';
1691 $extra_sql .= " $order";
1693 warn "searching for events for $eventtable ". $object->$pkey. "\n"
1694 if $opt{'debug'} > 2;
1695 my @part_event = qsearch( {
1696 'debug' => ( $opt{'debug'} > 3 ? 1 : 0 ),
1697 'select' => 'part_event.*',
1698 'table' => 'part_event',
1699 'addl_from' => "$cross $join",
1700 'hashref' => { 'check_freq' => $check_freq,
1701 'eventtable' => $eventtable,
1704 'extra_sql' => "AND $cross_where $extra_sql",
1708 my $pkey = $object->primary_key;
1709 warn " ". scalar(@part_event).
1710 " possible events found for $eventtable ". $object->$pkey(). "\n";
1713 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
1717 warn " ". scalar(@e_cust_event).
1718 " subtotal possible cust events found for $eventtable\n"
1721 push @cust_event, @e_cust_event;
1725 warn " ". scalar(@cust_event).
1726 " total possible cust events found in initial search\n"
1734 $opt{stage} ||= 'collect';
1736 grep { my $stage = $_->part_event->event_stage;
1737 $opt{stage} eq $stage or ( ! $stage && $opt{stage} eq 'collect' )
1747 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
1748 'stats_hashref' => \%unsat ),
1751 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
1754 warn " invalid conditions not eliminated with condition_sql:\n".
1755 join('', map " $_: ".$unsat{$_}."\n", keys %unsat )
1756 if keys %unsat && $DEBUG; # > 1;
1762 unless( $opt{testonly} ) {
1763 foreach my $cust_event ( @cust_event ) {
1765 my $error = $cust_event->insert();
1767 $dbh->rollback if $oldAutoCommit;
1774 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1780 warn " returning events: ". Dumper(@cust_event). "\n"
1787 =item apply_payments_and_credits [ OPTION => VALUE ... ]
1789 Applies unapplied payments and credits.
1791 In most cases, this new method should be used in place of sequential
1792 apply_payments and apply_credits methods.
1794 A hash of optional arguments may be passed. Currently "manual" is supported.
1795 If true, a payment receipt is sent instead of a statement when
1796 'payment_receipt_email' configuration option is set.
1798 If there is an error, returns the error, otherwise returns false.
1802 sub apply_payments_and_credits {
1803 my( $self, %options ) = @_;
1805 local $SIG{HUP} = 'IGNORE';
1806 local $SIG{INT} = 'IGNORE';
1807 local $SIG{QUIT} = 'IGNORE';
1808 local $SIG{TERM} = 'IGNORE';
1809 local $SIG{TSTP} = 'IGNORE';
1810 local $SIG{PIPE} = 'IGNORE';
1812 my $oldAutoCommit = $FS::UID::AutoCommit;
1813 local $FS::UID::AutoCommit = 0;
1816 $self->select_for_update; #mutex
1818 foreach my $cust_bill ( $self->open_cust_bill ) {
1819 my $error = $cust_bill->apply_payments_and_credits(%options);
1821 $dbh->rollback if $oldAutoCommit;
1822 return "Error applying: $error";
1826 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1831 =item apply_credits OPTION => VALUE ...
1833 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
1834 to outstanding invoice balances in chronological order (or reverse
1835 chronological order if the I<order> option is set to B<newest>) and returns the
1836 value of any remaining unapplied credits available for refund (see
1837 L<FS::cust_refund>).
1839 Dies if there is an error.
1847 local $SIG{HUP} = 'IGNORE';
1848 local $SIG{INT} = 'IGNORE';
1849 local $SIG{QUIT} = 'IGNORE';
1850 local $SIG{TERM} = 'IGNORE';
1851 local $SIG{TSTP} = 'IGNORE';
1852 local $SIG{PIPE} = 'IGNORE';
1854 my $oldAutoCommit = $FS::UID::AutoCommit;
1855 local $FS::UID::AutoCommit = 0;
1858 $self->select_for_update; #mutex
1860 unless ( $self->total_unapplied_credits ) {
1861 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1865 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
1866 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
1868 my @invoices = $self->open_cust_bill;
1869 @invoices = sort { $b->_date <=> $a->_date } @invoices
1870 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
1872 if ( $conf->exists('pkg-balances') ) {
1873 # limit @credits to those w/ a pkgnum grepped from $self
1875 foreach my $i (@invoices) {
1876 foreach my $li ( $i->cust_bill_pkg ) {
1877 $pkgnums{$li->pkgnum} = 1;
1880 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
1885 foreach my $cust_bill ( @invoices ) {
1887 if ( !defined($credit) || $credit->credited == 0) {
1888 $credit = pop @credits or last;
1892 if ( $conf->exists('pkg-balances') && $credit->pkgnum ) {
1893 $owed = $cust_bill->owed_pkgnum($credit->pkgnum);
1895 $owed = $cust_bill->owed;
1897 unless ( $owed > 0 ) {
1898 push @credits, $credit;
1902 my $amount = min( $credit->credited, $owed );
1904 my $cust_credit_bill = new FS::cust_credit_bill ( {
1905 'crednum' => $credit->crednum,
1906 'invnum' => $cust_bill->invnum,
1907 'amount' => $amount,
1909 $cust_credit_bill->pkgnum( $credit->pkgnum )
1910 if $conf->exists('pkg-balances') && $credit->pkgnum;
1911 my $error = $cust_credit_bill->insert;
1913 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
1917 redo if ($cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
1921 my $total_unapplied_credits = $self->total_unapplied_credits;
1923 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1925 return $total_unapplied_credits;
1928 =item apply_payments [ OPTION => VALUE ... ]
1930 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
1931 to outstanding invoice balances in chronological order.
1933 #and returns the value of any remaining unapplied payments.
1935 A hash of optional arguments may be passed. Currently "manual" is supported.
1936 If true, a payment receipt is sent instead of a statement when
1937 'payment_receipt_email' configuration option is set.
1939 Dies if there is an error.
1943 sub apply_payments {
1944 my( $self, %options ) = @_;
1946 local $SIG{HUP} = 'IGNORE';
1947 local $SIG{INT} = 'IGNORE';
1948 local $SIG{QUIT} = 'IGNORE';
1949 local $SIG{TERM} = 'IGNORE';
1950 local $SIG{TSTP} = 'IGNORE';
1951 local $SIG{PIPE} = 'IGNORE';
1953 my $oldAutoCommit = $FS::UID::AutoCommit;
1954 local $FS::UID::AutoCommit = 0;
1957 $self->select_for_update; #mutex
1961 my @payments = sort { $b->_date <=> $a->_date }
1962 grep { $_->unapplied > 0 }
1965 my @invoices = sort { $a->_date <=> $b->_date}
1966 grep { $_->owed > 0 }
1969 if ( $conf->exists('pkg-balances') ) {
1970 # limit @payments to those w/ a pkgnum grepped from $self
1972 foreach my $i (@invoices) {
1973 foreach my $li ( $i->cust_bill_pkg ) {
1974 $pkgnums{$li->pkgnum} = 1;
1977 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
1982 foreach my $cust_bill ( @invoices ) {
1984 if ( !defined($payment) || $payment->unapplied == 0 ) {
1985 $payment = pop @payments or last;
1989 if ( $conf->exists('pkg-balances') && $payment->pkgnum ) {
1990 $owed = $cust_bill->owed_pkgnum($payment->pkgnum);
1992 $owed = $cust_bill->owed;
1994 unless ( $owed > 0 ) {
1995 push @payments, $payment;
1999 my $amount = min( $payment->unapplied, $owed );
2001 my $cust_bill_pay = new FS::cust_bill_pay ( {
2002 'paynum' => $payment->paynum,
2003 'invnum' => $cust_bill->invnum,
2004 'amount' => $amount,
2006 $cust_bill_pay->pkgnum( $payment->pkgnum )
2007 if $conf->exists('pkg-balances') && $payment->pkgnum;
2008 my $error = $cust_bill_pay->insert(%options);
2010 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2014 redo if ( $cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
2018 my $total_unapplied_payments = $self->total_unapplied_payments;
2020 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2022 return $total_unapplied_payments;