4 use vars qw( @ISA $conf $Debug $import );
8 eval "use Time::Local 1.05;" if $] < 5.006;
9 eval "use Time::Local qw(timelocal_nocheck);";
13 use Business::CreditCard;
14 use FS::UID qw( getotaker dbh );
15 use FS::Record qw( qsearchs qsearch dbdef );
16 use FS::Misc qw( send_email );
19 use FS::cust_bill_pkg;
22 use FS::part_referral;
23 use FS::cust_main_county;
25 use FS::cust_main_invoice;
26 use FS::cust_credit_bill;
27 use FS::cust_bill_pay;
28 use FS::prepay_credit;
31 use FS::part_bill_event;
32 use FS::cust_bill_event;
33 use FS::cust_tax_exempt;
35 use FS::Msgcat qw(gettext);
37 @ISA = qw( FS::Record );
44 #ask FS::UID to run this stuff for us later
45 #$FS::UID::callback{'FS::cust_main'} = sub {
46 install_callback FS::UID sub {
48 #yes, need it for stuff below (prolly should be cached)
53 my ( $hashref, $cache ) = @_;
54 if ( exists $hashref->{'pkgnum'} ) {
55 # #@{ $self->{'_pkgnum'} } = ();
56 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
57 $self->{'_pkgnum'} = $subcache;
58 #push @{ $self->{'_pkgnum'} },
59 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
65 FS::cust_main - Object methods for cust_main records
71 $record = new FS::cust_main \%hash;
72 $record = new FS::cust_main { 'column' => 'value' };
74 $error = $record->insert;
76 $error = $new_record->replace($old_record);
78 $error = $record->delete;
80 $error = $record->check;
82 @cust_pkg = $record->all_pkgs;
84 @cust_pkg = $record->ncancelled_pkgs;
86 @cust_pkg = $record->suspended_pkgs;
88 $error = $record->bill;
89 $error = $record->bill %options;
90 $error = $record->bill 'time' => $time;
92 $error = $record->collect;
93 $error = $record->collect %options;
94 $error = $record->collect 'invoice_time' => $time,
95 'batch_card' => 'yes',
96 'report_badcard' => 'yes',
101 An FS::cust_main object represents a customer. FS::cust_main inherits from
102 FS::Record. The following fields are currently supported:
106 =item custnum - primary key (assigned automatically for new customers)
108 =item agentnum - agent (see L<FS::agent>)
110 =item refnum - Advertising source (see L<FS::part_referral>)
116 =item ss - social security number (optional)
118 =item company - (optional)
122 =item address2 - (optional)
126 =item county - (optional, see L<FS::cust_main_county>)
128 =item state - (see L<FS::cust_main_county>)
132 =item country - (see L<FS::cust_main_county>)
134 =item daytime - phone (optional)
136 =item night - phone (optional)
138 =item fax - phone (optional)
140 =item ship_first - name
142 =item ship_last - name
144 =item ship_company - (optional)
148 =item ship_address2 - (optional)
152 =item ship_county - (optional, see L<FS::cust_main_county>)
154 =item ship_state - (see L<FS::cust_main_county>)
158 =item ship_country - (see L<FS::cust_main_county>)
160 =item ship_daytime - phone (optional)
162 =item ship_night - phone (optional)
164 =item ship_fax - phone (optional)
166 =item payby - I<CARD> (credit card - automatic), I<DCRD> (credit card - on-demand), I<CHEK> (electronic check - automatic), I<DCHK> (electronic check - on-demand), I<LECB> (Phone bill billing), I<BILL> (billing), I<COMP> (free), or I<PREPAY> (special billing type: applies a credit - see L<FS::prepay_credit> and sets billing type to I<BILL>)
168 =item payinfo - card number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
170 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
172 =item payname - name on card or billing name
174 =item tax - tax exempt, empty or `Y'
176 =item otaker - order taker (assigned automatically, see L<FS::UID>)
178 =item comments - comments (optional)
180 =item referral_custnum - referring customer number
190 Creates a new customer. To add the customer to the database, see L<"insert">.
192 Note that this stores the hash reference, not a distinct copy of the hash it
193 points to. You can ask the object for a copy with the I<hash> method.
197 sub table { 'cust_main'; }
199 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] ]
201 Adds this customer to the database. If there is an error, returns the error,
202 otherwise returns false.
204 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
205 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
206 are inserted atomicly, or the transaction is rolled back. Passing an empty
207 hash reference is equivalent to not supplying this parameter. There should be
208 a better explanation of this, but until then, here's an example:
211 tie %hash, 'Tie::RefHash'; #this part is important
213 $cust_pkg => [ $svc_acct ],
216 $cust_main->insert( \%hash );
218 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
219 be set as the invoicing list (see L<"invoicing_list">). Errors return as
220 expected and rollback the entire transaction; it is not necessary to call
221 check_invoicing_list first. The invoicing_list is set after the records in the
222 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
223 invoicing_list destination to the newly-created svc_acct. Here's an example:
225 $cust_main->insert( {}, [ $email, 'POST' ] );
231 my $cust_pkgs = @_ ? shift : {};
232 my $invoicing_list = @_ ? shift : '';
234 local $SIG{HUP} = 'IGNORE';
235 local $SIG{INT} = 'IGNORE';
236 local $SIG{QUIT} = 'IGNORE';
237 local $SIG{TERM} = 'IGNORE';
238 local $SIG{TSTP} = 'IGNORE';
239 local $SIG{PIPE} = 'IGNORE';
241 my $oldAutoCommit = $FS::UID::AutoCommit;
242 local $FS::UID::AutoCommit = 0;
247 if ( $self->payby eq 'PREPAY' ) {
248 $self->payby('BILL');
249 my $prepay_credit = qsearchs(
251 { 'identifier' => $self->payinfo },
255 warn "WARNING: can't find pre-found prepay_credit: ". $self->payinfo
256 unless $prepay_credit;
257 $amount = $prepay_credit->amount;
258 $seconds = $prepay_credit->seconds;
259 my $error = $prepay_credit->delete;
261 $dbh->rollback if $oldAutoCommit;
262 return "removing prepay_credit (transaction rolled back): $error";
266 my $error = $self->SUPER::insert;
268 $dbh->rollback if $oldAutoCommit;
269 #return "inserting cust_main record (transaction rolled back): $error";
274 if ( $invoicing_list ) {
275 $error = $self->check_invoicing_list( $invoicing_list );
277 $dbh->rollback if $oldAutoCommit;
278 return "checking invoicing_list (transaction rolled back): $error";
280 $self->invoicing_list( $invoicing_list );
284 $error = $self->order_pkgs($cust_pkgs, \$seconds);
286 $dbh->rollback if $oldAutoCommit;
291 $dbh->rollback if $oldAutoCommit;
292 return "No svc_acct record to apply pre-paid time";
296 my $cust_credit = new FS::cust_credit {
297 'custnum' => $self->custnum,
300 $error = $cust_credit->insert;
302 $dbh->rollback if $oldAutoCommit;
303 return "inserting credit (transaction rolled back): $error";
307 #false laziness with sub replace
308 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
309 $error = $queue->insert($self->getfield('last'), $self->company);
311 $dbh->rollback if $oldAutoCommit;
312 return "queueing job (transaction rolled back): $error";
315 if ( defined $self->dbdef_table->column('ship_last') && $self->ship_last ) {
316 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
317 $error = $queue->insert($self->getfield('last'), $self->company);
319 $dbh->rollback if $oldAutoCommit;
320 return "queueing job (transaction rolled back): $error";
325 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
332 document me. like ->insert(%cust_pkg) on an existing record
338 my $cust_pkgs = shift;
341 local $SIG{HUP} = 'IGNORE';
342 local $SIG{INT} = 'IGNORE';
343 local $SIG{QUIT} = 'IGNORE';
344 local $SIG{TERM} = 'IGNORE';
345 local $SIG{TSTP} = 'IGNORE';
346 local $SIG{PIPE} = 'IGNORE';
348 my $oldAutoCommit = $FS::UID::AutoCommit;
349 local $FS::UID::AutoCommit = 0;
352 foreach my $cust_pkg ( keys %$cust_pkgs ) {
353 $cust_pkg->custnum( $self->custnum );
354 my $error = $cust_pkg->insert;
356 $dbh->rollback if $oldAutoCommit;
357 return "inserting cust_pkg (transaction rolled back): $error";
359 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
360 $svc_something->pkgnum( $cust_pkg->pkgnum );
361 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
362 $svc_something->seconds( $svc_something->seconds + $$seconds );
365 $error = $svc_something->insert;
367 $dbh->rollback if $oldAutoCommit;
368 #return "inserting svc_ (transaction rolled back): $error";
374 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
378 =item delete NEW_CUSTNUM
380 This deletes the customer. If there is an error, returns the error, otherwise
383 This will completely remove all traces of the customer record. This is not
384 what you want when a customer cancels service; for that, cancel all of the
385 customer's packages (see L<FS::cust_pkg/cancel>).
387 If the customer has any uncancelled packages, you need to pass a new (valid)
388 customer number for those packages to be transferred to. Cancelled packages
389 will be deleted. Did I mention that this is NOT what you want when a customer
390 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
392 You can't delete a customer with invoices (see L<FS::cust_bill>),
393 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
394 refunds (see L<FS::cust_refund>).
401 local $SIG{HUP} = 'IGNORE';
402 local $SIG{INT} = 'IGNORE';
403 local $SIG{QUIT} = 'IGNORE';
404 local $SIG{TERM} = 'IGNORE';
405 local $SIG{TSTP} = 'IGNORE';
406 local $SIG{PIPE} = 'IGNORE';
408 my $oldAutoCommit = $FS::UID::AutoCommit;
409 local $FS::UID::AutoCommit = 0;
412 if ( qsearch( 'cust_bill', { 'custnum' => $self->custnum } ) ) {
413 $dbh->rollback if $oldAutoCommit;
414 return "Can't delete a customer with invoices";
416 if ( qsearch( 'cust_credit', { 'custnum' => $self->custnum } ) ) {
417 $dbh->rollback if $oldAutoCommit;
418 return "Can't delete a customer with credits";
420 if ( qsearch( 'cust_pay', { 'custnum' => $self->custnum } ) ) {
421 $dbh->rollback if $oldAutoCommit;
422 return "Can't delete a customer with payments";
424 if ( qsearch( 'cust_refund', { 'custnum' => $self->custnum } ) ) {
425 $dbh->rollback if $oldAutoCommit;
426 return "Can't delete a customer with refunds";
429 my @cust_pkg = $self->ncancelled_pkgs;
431 my $new_custnum = shift;
432 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
433 $dbh->rollback if $oldAutoCommit;
434 return "Invalid new customer number: $new_custnum";
436 foreach my $cust_pkg ( @cust_pkg ) {
437 my %hash = $cust_pkg->hash;
438 $hash{'custnum'} = $new_custnum;
439 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
440 my $error = $new_cust_pkg->replace($cust_pkg);
442 $dbh->rollback if $oldAutoCommit;
447 my @cancelled_cust_pkg = $self->all_pkgs;
448 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
449 my $error = $cust_pkg->delete;
451 $dbh->rollback if $oldAutoCommit;
456 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
457 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
459 my $error = $cust_main_invoice->delete;
461 $dbh->rollback if $oldAutoCommit;
466 my $error = $self->SUPER::delete;
468 $dbh->rollback if $oldAutoCommit;
472 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
477 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
479 Replaces the OLD_RECORD with this one in the database. If there is an error,
480 returns the error, otherwise returns false.
482 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
483 be set as the invoicing list (see L<"invoicing_list">). Errors return as
484 expected and rollback the entire transaction; it is not necessary to call
485 check_invoicing_list first. Here's an example:
487 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
496 local $SIG{HUP} = 'IGNORE';
497 local $SIG{INT} = 'IGNORE';
498 local $SIG{QUIT} = 'IGNORE';
499 local $SIG{TERM} = 'IGNORE';
500 local $SIG{TSTP} = 'IGNORE';
501 local $SIG{PIPE} = 'IGNORE';
503 my $oldAutoCommit = $FS::UID::AutoCommit;
504 local $FS::UID::AutoCommit = 0;
507 my $error = $self->SUPER::replace($old);
510 $dbh->rollback if $oldAutoCommit;
514 if ( @param ) { # INVOICING_LIST_ARYREF
515 my $invoicing_list = shift @param;
516 $error = $self->check_invoicing_list( $invoicing_list );
518 $dbh->rollback if $oldAutoCommit;
521 $self->invoicing_list( $invoicing_list );
524 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
525 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
526 # card/check info has changed, want to retry realtime_card invoice events
527 #false laziness w/collect
528 foreach my $cust_bill_event (
530 #$_->part_bill_event->plan eq 'realtime-card'
531 $_->part_bill_event->eventcode =~
532 /^\$cust_bill\->realtime_(card|ach|lec)\(\);$/
533 && $_->status eq 'done'
536 map { $_->cust_bill_event }
537 grep { $_->cust_bill_event }
538 $self->open_cust_bill
541 my $error = $cust_bill_event->retry;
543 $dbh->rollback if $oldAutoCommit;
544 return "error scheduling invoice events for retry: $error";
551 #false laziness with sub insert
552 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
553 $error = $queue->insert($self->getfield('last'), $self->company);
555 $dbh->rollback if $oldAutoCommit;
556 return "queueing job (transaction rolled back): $error";
559 if ( defined $self->dbdef_table->column('ship_last') && $self->ship_last ) {
560 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
561 $error = $queue->insert($self->getfield('last'), $self->company);
563 $dbh->rollback if $oldAutoCommit;
564 return "queueing job (transaction rolled back): $error";
569 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
576 Checks all fields to make sure this is a valid customer record. If there is
577 an error, returns the error, otherwise returns false. Called by the insert
585 #warn "BEFORE: \n". $self->_dump;
588 $self->ut_numbern('custnum')
589 || $self->ut_number('agentnum')
590 || $self->ut_number('refnum')
591 || $self->ut_name('last')
592 || $self->ut_name('first')
593 || $self->ut_textn('company')
594 || $self->ut_text('address1')
595 || $self->ut_textn('address2')
596 || $self->ut_text('city')
597 || $self->ut_textn('county')
598 || $self->ut_textn('state')
599 || $self->ut_country('country')
600 || $self->ut_anything('comments')
601 || $self->ut_numbern('referral_custnum')
603 #barf. need message catalogs. i18n. etc.
604 $error .= "Please select a advertising source."
605 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
606 return $error if $error;
608 return "Unknown agent"
609 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
611 return "Unknown refnum"
612 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
614 return "Unknown referring custnum ". $self->referral_custnum
615 unless ! $self->referral_custnum
616 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
618 if ( $self->ss eq '' ) {
623 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
624 or return "Illegal social security number: ". $self->ss;
625 $self->ss("$1-$2-$3");
629 # bad idea to disable, causes billing to fail because of no tax rates later
630 # unless ( $import ) {
631 unless ( qsearch('cust_main_county', {
632 'country' => $self->country,
635 return "Unknown state/county/country: ".
636 $self->state. "/". $self->county. "/". $self->country
637 unless qsearch('cust_main_county',{
638 'state' => $self->state,
639 'county' => $self->county,
640 'country' => $self->country,
646 $self->ut_phonen('daytime', $self->country)
647 || $self->ut_phonen('night', $self->country)
648 || $self->ut_phonen('fax', $self->country)
649 || $self->ut_zip('zip', $self->country)
651 return $error if $error;
654 last first company address1 address2 city county state zip
655 country daytime night fax
658 if ( defined $self->dbdef_table->column('ship_last') ) {
659 if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
661 && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
665 $self->ut_name('ship_last')
666 || $self->ut_name('ship_first')
667 || $self->ut_textn('ship_company')
668 || $self->ut_text('ship_address1')
669 || $self->ut_textn('ship_address2')
670 || $self->ut_text('ship_city')
671 || $self->ut_textn('ship_county')
672 || $self->ut_textn('ship_state')
673 || $self->ut_country('ship_country')
675 return $error if $error;
677 #false laziness with above
678 unless ( qsearchs('cust_main_county', {
679 'country' => $self->ship_country,
682 return "Unknown ship_state/ship_county/ship_country: ".
683 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
684 unless qsearchs('cust_main_county',{
685 'state' => $self->ship_state,
686 'county' => $self->ship_county,
687 'country' => $self->ship_country,
693 $self->ut_phonen('ship_daytime', $self->ship_country)
694 || $self->ut_phonen('ship_night', $self->ship_country)
695 || $self->ut_phonen('ship_fax', $self->ship_country)
696 || $self->ut_zip('ship_zip', $self->ship_country)
698 return $error if $error;
700 } else { # ship_ info eq billing info, so don't store dup info in database
701 $self->setfield("ship_$_", '')
702 foreach qw( last first company address1 address2 city county state zip
703 country daytime night fax );
707 $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY)$/
708 or return "Illegal payby: ". $self->payby;
711 if ( $self->payby eq 'CARD' || $self->payby eq 'DCRD' ) {
713 my $payinfo = $self->payinfo;
715 $payinfo =~ /^(\d{13,16})$/
716 or return gettext('invalid_card'); # . ": ". $self->payinfo;
718 $self->payinfo($payinfo);
720 or return gettext('invalid_card'); # . ": ". $self->payinfo;
721 return gettext('unknown_card_type')
722 if cardtype($self->payinfo) eq "Unknown";
724 } elsif ( $self->payby eq 'CHEK' || $self->payby eq 'DCHK' ) {
726 my $payinfo = $self->payinfo;
727 $payinfo =~ s/[^\d\@]//g;
728 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
730 $self->payinfo($payinfo);
732 } elsif ( $self->payby eq 'LECB' ) {
734 my $payinfo = $self->payinfo;
736 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
738 $self->payinfo($payinfo);
740 } elsif ( $self->payby eq 'BILL' ) {
742 $error = $self->ut_textn('payinfo');
743 return "Illegal P.O. number: ". $self->payinfo if $error;
745 } elsif ( $self->payby eq 'COMP' ) {
747 $error = $self->ut_textn('payinfo');
748 return "Illegal comp account issuer: ". $self->payinfo if $error;
750 } elsif ( $self->payby eq 'PREPAY' ) {
752 my $payinfo = $self->payinfo;
753 $payinfo =~ s/\W//g; #anything else would just confuse things
754 $self->payinfo($payinfo);
755 $error = $self->ut_alpha('payinfo');
756 return "Illegal prepayment identifier: ". $self->payinfo if $error;
757 return "Unknown prepayment identifier"
758 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
762 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
763 return "Expriation date required"
764 unless $self->payby =~ /^(BILL|PREPAY|CHEK|LECB)$/;
768 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
769 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
770 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{2})[\/\-]\d+$/ ) {
771 ( $m, $y ) = ( $3, "20$2" );
773 return "Illegal expiration date: ". $self->paydate;
775 $self->paydate("$y-$m-01");
776 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
777 return gettext('expired_card')
778 if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
781 if ( $self->payname eq '' && $self->payby ne 'CHEK' &&
782 ( ! $conf->exists('require_cardname')
783 || $self->payby !~ /^(CARD|DCRD)$/ )
785 $self->payname( $self->first. " ". $self->getfield('last') );
787 $self->payname =~ /^([\w \,\.\-\']+)$/
788 or return gettext('illegal_name'). " payname: ". $self->payname;
792 $self->tax =~ /^(Y?)$/ or return "Illegal tax: ". $self->tax;
795 $self->otaker(getotaker);
797 #warn "AFTER: \n". $self->_dump;
804 Returns all packages (see L<FS::cust_pkg>) for this customer.
810 if ( $self->{'_pkgnum'} ) {
811 values %{ $self->{'_pkgnum'}->cache };
813 qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
817 =item ncancelled_pkgs
819 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
823 sub ncancelled_pkgs {
825 if ( $self->{'_pkgnum'} ) {
826 grep { ! $_->getfield('cancel') } values %{ $self->{'_pkgnum'}->cache };
828 @{ [ # force list context
829 qsearch( 'cust_pkg', {
830 'custnum' => $self->custnum,
833 qsearch( 'cust_pkg', {
834 'custnum' => $self->custnum,
843 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
849 grep { $_->susp } $self->ncancelled_pkgs;
852 =item unflagged_suspended_pkgs
854 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
855 customer (thouse packages without the `manual_flag' set).
859 sub unflagged_suspended_pkgs {
861 return $self->suspended_pkgs
862 unless dbdef->table('cust_pkg')->column('manual_flag');
863 grep { ! $_->manual_flag } $self->suspended_pkgs;
866 =item unsuspended_pkgs
868 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
873 sub unsuspended_pkgs {
875 grep { ! $_->susp } $self->ncancelled_pkgs;
880 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
881 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
882 on success or a list of errors.
888 grep { $_->unsuspend } $self->suspended_pkgs;
893 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
894 Always returns a list: an empty list on success or a list of errors.
900 grep { $_->suspend } $self->unsuspended_pkgs;
905 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
906 Always returns a list: an empty list on success or a list of errors.
912 grep { $_->cancel } $self->ncancelled_pkgs;
917 Returns the agent (see L<FS::agent>) for this customer.
923 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
928 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
929 conjunction with the collect method.
931 Options are passed as name-value pairs.
933 The only currently available option is `time', which bills the customer as if
934 it were that time. It is specified as a UNIX timestamp; see
935 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
936 functions. For example:
940 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
942 If there is an error, returns the error, otherwise returns false.
947 my( $self, %options ) = @_;
948 my $time = $options{'time'} || time;
953 local $SIG{HUP} = 'IGNORE';
954 local $SIG{INT} = 'IGNORE';
955 local $SIG{QUIT} = 'IGNORE';
956 local $SIG{TERM} = 'IGNORE';
957 local $SIG{TSTP} = 'IGNORE';
958 local $SIG{PIPE} = 'IGNORE';
960 my $oldAutoCommit = $FS::UID::AutoCommit;
961 local $FS::UID::AutoCommit = 0;
964 # find the packages which are due for billing, find out how much they are
965 # & generate invoice database.
967 my( $total_setup, $total_recur ) = ( 0, 0 );
968 #my( $taxable_setup, $taxable_recur ) = ( 0, 0 );
969 my @cust_bill_pkg = ();
971 #my $taxable_charged = 0;##
976 foreach my $cust_pkg (
977 qsearch('cust_pkg', { 'custnum' => $self->custnum } )
980 #NO!! next if $cust_pkg->cancel;
981 next if $cust_pkg->getfield('cancel');
983 #? to avoid use of uninitialized value errors... ?
984 $cust_pkg->setfield('bill', '')
985 unless defined($cust_pkg->bill);
987 my $part_pkg = $cust_pkg->part_pkg;
989 #so we don't modify cust_pkg record unnecessarily
990 my $cust_pkg_mod_flag = 0;
991 my %hash = $cust_pkg->hash;
992 my $old_cust_pkg = new FS::cust_pkg \%hash;
998 unless ( $cust_pkg->setup ) {
999 my $setup_prog = $part_pkg->getfield('setup');
1000 $setup_prog =~ /^(.*)$/ or do {
1001 $dbh->rollback if $oldAutoCommit;
1002 return "Illegal setup for pkgpart ". $part_pkg->pkgpart.
1006 $setup_prog = '0' if $setup_prog =~ /^\s*$/;
1008 #my $cpt = new Safe;
1009 ##$cpt->permit(); #what is necessary?
1010 #$cpt->share(qw( $cust_pkg )); #can $cpt now use $cust_pkg methods?
1011 #$setup = $cpt->reval($setup_prog);
1012 $setup = eval $setup_prog;
1013 unless ( defined($setup) ) {
1014 $dbh->rollback if $oldAutoCommit;
1015 return "Error eval-ing part_pkg->setup pkgpart ". $part_pkg->pkgpart.
1016 "(expression $setup_prog): $@";
1018 $cust_pkg->setfield('setup',$time);
1019 $cust_pkg_mod_flag=1;
1025 if ( $part_pkg->getfield('freq') > 0 &&
1026 ! $cust_pkg->getfield('susp') &&
1027 ( $cust_pkg->getfield('bill') || 0 ) <= $time
1029 my $recur_prog = $part_pkg->getfield('recur');
1030 $recur_prog =~ /^(.*)$/ or do {
1031 $dbh->rollback if $oldAutoCommit;
1032 return "Illegal recur for pkgpart ". $part_pkg->pkgpart.
1036 $recur_prog = '0' if $recur_prog =~ /^\s*$/;
1038 # shared with $recur_prog
1039 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1041 #my $cpt = new Safe;
1042 ##$cpt->permit(); #what is necessary?
1043 #$cpt->share(qw( $cust_pkg )); #can $cpt now use $cust_pkg methods?
1044 #$recur = $cpt->reval($recur_prog);
1045 $recur = eval $recur_prog;
1046 unless ( defined($recur) ) {
1047 $dbh->rollback if $oldAutoCommit;
1048 return "Error eval-ing part_pkg->recur pkgpart ". $part_pkg->pkgpart.
1049 "(expression $recur_prog): $@";
1051 #change this bit to use Date::Manip? CAREFUL with timezones (see
1052 # mailing list archive)
1053 my ($sec,$min,$hour,$mday,$mon,$year) =
1054 (localtime($sdate) )[0,1,2,3,4,5];
1056 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
1057 # only for figuring next bill date, nothing else, so, reset $sdate again
1059 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1060 $cust_pkg->last_bill($sdate)
1061 if $cust_pkg->dbdef_table->column('last_bill');
1063 $mon += $part_pkg->freq;
1064 until ( $mon < 12 ) { $mon -= 12; $year++; }
1065 $cust_pkg->setfield('bill',
1066 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
1067 $cust_pkg_mod_flag = 1;
1070 warn "\$setup is undefined" unless defined($setup);
1071 warn "\$recur is undefined" unless defined($recur);
1072 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1074 my $taxable_charged = 0;
1075 if ( $cust_pkg_mod_flag ) {
1076 $error=$cust_pkg->replace($old_cust_pkg);
1077 if ( $error ) { #just in case
1078 $dbh->rollback if $oldAutoCommit;
1079 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
1081 $setup = sprintf( "%.2f", $setup );
1082 $recur = sprintf( "%.2f", $recur );
1084 $dbh->rollback if $oldAutoCommit;
1085 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1088 $dbh->rollback if $oldAutoCommit;
1089 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1091 if ( $setup > 0 || $recur > 0 ) {
1092 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1093 'pkgnum' => $cust_pkg->pkgnum,
1097 'edate' => $cust_pkg->bill,
1098 'details' => \@details,
1100 push @cust_bill_pkg, $cust_bill_pkg;
1101 $total_setup += $setup;
1102 $total_recur += $recur;
1103 $taxable_charged += $setup
1104 unless $part_pkg->setuptax =~ /^Y$/i;
1105 $taxable_charged += $recur
1106 unless $part_pkg->recurtax =~ /^Y$/i;
1108 unless ( $self->tax =~ /Y/i
1109 || $self->payby eq 'COMP'
1110 || $taxable_charged == 0 ) {
1112 my $cust_main_county = qsearchs('cust_main_county',{
1113 'state' => $self->state,
1114 'county' => $self->county,
1115 'country' => $self->country,
1116 'taxclass' => $part_pkg->taxclass,
1118 $cust_main_county ||= qsearchs('cust_main_county',{
1119 'state' => $self->state,
1120 'county' => $self->county,
1121 'country' => $self->country,
1124 unless ( $cust_main_county ) {
1125 $dbh->rollback if $oldAutoCommit;
1127 "fatal: can't find tax rate for state/county/country/taxclass ".
1128 join('/', ( map $self->$_(), qw(state county country) ),
1129 $part_pkg->taxclass ). "\n";
1132 if ( $cust_main_county->exempt_amount ) {
1133 my ($mon,$year) = (localtime($sdate) )[4,5];
1135 my $freq = $part_pkg->freq || 1;
1136 my $taxable_per_month = sprintf("%.2f", $taxable_charged / $freq );
1137 foreach my $which_month ( 1 .. $freq ) {
1139 'custnum' => $self->custnum,
1140 'taxnum' => $cust_main_county->taxnum,
1141 'year' => 1900+$year,
1144 #until ( $mon < 12 ) { $mon -= 12; $year++; }
1145 until ( $mon < 13 ) { $mon -= 12; $year++; }
1146 my $cust_tax_exempt =
1147 qsearchs('cust_tax_exempt', \%hash)
1148 || new FS::cust_tax_exempt( { %hash, 'amount' => 0 } );
1149 my $remaining_exemption = sprintf("%.2f",
1150 $cust_main_county->exempt_amount - $cust_tax_exempt->amount );
1151 if ( $remaining_exemption > 0 ) {
1152 my $addl = $remaining_exemption > $taxable_per_month
1153 ? $taxable_per_month
1154 : $remaining_exemption;
1155 $taxable_charged -= $addl;
1156 my $new_cust_tax_exempt = new FS::cust_tax_exempt ( {
1157 $cust_tax_exempt->hash,
1158 'amount' => sprintf("%.2f", $cust_tax_exempt->amount + $addl),
1160 $error = $new_cust_tax_exempt->exemptnum
1161 ? $new_cust_tax_exempt->replace($cust_tax_exempt)
1162 : $new_cust_tax_exempt->insert;
1164 $dbh->rollback if $oldAutoCommit;
1165 return "fatal: can't update cust_tax_exempt: $error";
1168 } # if $remaining_exemption > 0
1170 } #foreach $which_month
1172 } #if $cust_main_county->exempt_amount
1174 $taxable_charged = sprintf( "%.2f", $taxable_charged);
1176 #$tax += $taxable_charged * $cust_main_county->tax / 100
1177 $tax{ $cust_main_county->taxname || 'Tax' } +=
1178 $taxable_charged * $cust_main_county->tax / 100
1180 } #unless $self->tax =~ /Y/i
1181 # || $self->payby eq 'COMP'
1182 # || $taxable_charged == 0
1184 } #if $setup > 0 || $recur > 0
1186 } #if $cust_pkg_mod_flag
1188 } #foreach my $cust_pkg
1190 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
1191 # my $taxable_charged = sprintf( "%.2f", $taxable_setup + $taxable_recur );
1193 unless ( @cust_bill_pkg ) { #don't create invoices with no line items
1194 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1198 # unless ( $self->tax =~ /Y/i
1199 # || $self->payby eq 'COMP'
1200 # || $taxable_charged == 0 ) {
1201 # my $cust_main_county = qsearchs('cust_main_county',{
1202 # 'state' => $self->state,
1203 # 'county' => $self->county,
1204 # 'country' => $self->country,
1205 # } ) or die "fatal: can't find tax rate for state/county/country ".
1206 # $self->state. "/". $self->county. "/". $self->country. "\n";
1207 # my $tax = sprintf( "%.2f",
1208 # $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
1211 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
1212 my $tax = sprintf("%.2f", $tax{$taxname} );
1213 $charged = sprintf( "%.2f", $charged+$tax );
1215 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1221 'itemdesc' => $taxname,
1223 push @cust_bill_pkg, $cust_bill_pkg;
1227 my $cust_bill = new FS::cust_bill ( {
1228 'custnum' => $self->custnum,
1230 'charged' => $charged,
1232 $error = $cust_bill->insert;
1234 $dbh->rollback if $oldAutoCommit;
1235 return "can't create invoice for customer #". $self->custnum. ": $error";
1238 my $invnum = $cust_bill->invnum;
1240 foreach $cust_bill_pkg ( @cust_bill_pkg ) {
1242 $cust_bill_pkg->invnum($invnum);
1243 $error = $cust_bill_pkg->insert;
1245 $dbh->rollback if $oldAutoCommit;
1246 return "can't create invoice line item for customer #". $self->custnum.
1251 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1255 =item collect OPTIONS
1257 (Attempt to) collect money for this customer's outstanding invoices (see
1258 L<FS::cust_bill>). Usually used after the bill method.
1260 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
1261 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
1262 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
1264 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
1265 and the invoice events web interface.
1267 If there is an error, returns the error, otherwise returns false.
1269 Options are passed as name-value pairs.
1271 Currently available options are:
1273 invoice_time - Use this time when deciding when to print invoices and
1274 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>
1275 for conversion functions.
1277 retry_card - Retry cards even when not scheduled by invoice events.
1279 batch_card - This option is deprecated. See the invoice events web interface
1280 to control whether cards are batched or run against a realtime gateway.
1282 report_badcard - This option is deprecated.
1284 force_print - This option is deprecated; see the invoice events web interface.
1289 my( $self, %options ) = @_;
1290 my $invoice_time = $options{'invoice_time'} || time;
1293 local $SIG{HUP} = 'IGNORE';
1294 local $SIG{INT} = 'IGNORE';
1295 local $SIG{QUIT} = 'IGNORE';
1296 local $SIG{TERM} = 'IGNORE';
1297 local $SIG{TSTP} = 'IGNORE';
1298 local $SIG{PIPE} = 'IGNORE';
1300 my $oldAutoCommit = $FS::UID::AutoCommit;
1301 local $FS::UID::AutoCommit = 0;
1304 my $balance = $self->balance;
1305 warn "collect customer". $self->custnum. ": balance $balance" if $Debug;
1306 unless ( $balance > 0 ) { #redundant?????
1307 $dbh->rollback if $oldAutoCommit; #hmm
1311 if ( exists($options{'retry_card'}) && $options{'retry_card'} ) {
1312 #false laziness w/replace
1313 foreach my $cust_bill_event (
1315 #$_->part_bill_event->plan eq 'realtime-card'
1316 $_->part_bill_event->eventcode eq '$cust_bill->realtime_card();'
1317 && $_->status eq 'done'
1320 map { $_->cust_bill_event }
1321 grep { $_->cust_bill_event }
1322 $self->open_cust_bill
1324 my $error = $cust_bill_event->retry;
1326 $dbh->rollback if $oldAutoCommit;
1327 return "error scheduling invoice events for retry: $error";
1333 foreach my $cust_bill ( $self->cust_bill ) {
1335 #this has to be before next's
1336 my $amount = sprintf( "%.2f", $balance < $cust_bill->owed
1340 $balance = sprintf( "%.2f", $balance - $amount );
1342 next unless $cust_bill->owed > 0;
1344 # don't try to charge for the same invoice if it's already in a batch
1345 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
1347 warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ", amount $amount, balance $balance)" if $Debug;
1349 next unless $amount > 0;
1352 foreach my $part_bill_event (
1353 sort { $a->seconds <=> $b->seconds
1354 || $a->weight <=> $b->weight
1355 || $a->eventpart <=> $b->eventpart }
1356 grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
1357 && ! qsearchs( 'cust_bill_event', {
1358 'invnum' => $cust_bill->invnum,
1359 'eventpart' => $_->eventpart,
1363 qsearch('part_bill_event', { 'payby' => $self->payby,
1364 'disabled' => '', } )
1367 last unless $cust_bill->owed > 0; #don't run subsequent events if owed=0
1369 warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
1371 my $cust_main = $self; #for callback
1372 my $error = eval $part_bill_event->eventcode;
1375 my $statustext = '';
1379 } elsif ( $error ) {
1381 $statustext = $error;
1386 #add cust_bill_event
1387 my $cust_bill_event = new FS::cust_bill_event {
1388 'invnum' => $cust_bill->invnum,
1389 'eventpart' => $part_bill_event->eventpart,
1390 #'_date' => $invoice_time,
1392 'status' => $status,
1393 'statustext' => $statustext,
1395 $error = $cust_bill_event->insert;
1397 #$dbh->rollback if $oldAutoCommit;
1398 #return "error: $error";
1400 # gah, even with transactions.
1401 $dbh->commit if $oldAutoCommit; #well.
1402 my $e = 'WARNING: Event run but database not updated - '.
1403 'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
1404 ', eventpart '. $part_bill_event->eventpart.
1415 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1420 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
1422 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
1423 via a Business::OnlinePayment realtime gateway. See
1424 L<http://420.am/business-onlinepayment> for supported gateways.
1426 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1428 Available options are: I<description>, I<invnum>, I<quiet>
1430 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1431 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1432 if set, will override the value from the customer record.
1434 I<description> is a free-text field passed to the gateway. It defaults to
1435 "Internet services".
1437 If an I<invnum> is specified, this payment (if sucessful) is applied to the
1438 specified invoice. If you don't specify an I<invnum> you might want to
1439 call the B<apply_payments> method.
1441 I<quiet> can be set true to surpress email decline notices.
1443 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
1448 my( $self, $method, $amount, %options ) = @_;
1450 warn "$self $method $amount\n";
1451 warn " $_ => $options{$_}\n" foreach keys %options;
1454 $options{'description'} ||= 'Internet services';
1457 die "Real-time processing not enabled\n"
1458 unless $conf->exists('business-onlinepayment');
1459 eval "use Business::OnlinePayment";
1463 $self->set( $_ => $options{$_} )
1464 foreach grep { exists($options{$_}) }
1465 qw( payname address1 address2 city state zip payinfo paydate );
1468 my $bop_config = 'business-onlinepayment';
1469 $bop_config .= '-ach'
1470 if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
1471 my ( $processor, $login, $password, $action, @bop_options ) =
1472 $conf->config($bop_config);
1473 $action ||= 'normal authorization';
1474 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1478 my $address = $self->address1;
1479 $address .= ", ". $self->address2 if $self->address2;
1481 my($payname, $payfirst, $paylast);
1482 if ( $self->payname && $method ne 'ECHECK' ) {
1483 $payname = $self->payname;
1484 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1485 or return "Illegal payname $payname";
1486 ($payfirst, $paylast) = ($1, $2);
1488 $payfirst = $self->getfield('first');
1489 $paylast = $self->getfield('last');
1490 $payname = "$payfirst $paylast";
1493 my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
1494 if ( $conf->exists('emailinvoiceauto')
1495 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1496 push @invoicing_list, $self->all_emails;
1498 my $email = $invoicing_list[0];
1501 if ( $method eq 'CC' ) {
1502 $content{card_number} = $self->payinfo;
1503 $self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1504 $content{expiration} = "$2/$1";
1505 } elsif ( $method eq 'ECHECK' ) {
1506 my($account_number,$routing_code) = $self->payinfo;
1507 ( $content{account_number}, $content{routing_code} ) =
1508 split('@', $self->payinfo);
1509 $content{bank_name} = $self->payname;
1510 } elsif ( $method eq 'LEC' ) {
1511 $content{phone} = $self->payinfo;
1516 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
1519 new Business::OnlinePayment( $processor, @bop_options );
1520 $transaction->content(
1523 'password' => $password,
1524 'action' => $action1,
1525 'description' => $options{'description'},
1526 'amount' => $amount,
1527 'invoice_number' => $options{'invnum'},
1528 'customer_id' => $self->custnum,
1529 'last_name' => $paylast,
1530 'first_name' => $payfirst,
1532 'address' => $address,
1533 'city' => $self->city,
1534 'state' => $self->state,
1535 'zip' => $self->zip,
1536 'country' => $self->country,
1537 'referer' => 'http://cleanwhisker.420.am/',
1539 'phone' => $self->daytime || $self->night,
1542 $transaction->submit();
1544 if ( $transaction->is_success() && $action2 ) {
1545 my $auth = $transaction->authorization;
1546 my $ordernum = $transaction->can('order_number')
1547 ? $transaction->order_number
1551 new Business::OnlinePayment( $processor, @bop_options );
1558 password => $password,
1559 order_number => $ordernum,
1561 authorization => $auth,
1562 description => $options{'description'},
1565 foreach my $field (qw( authorization_source_code returned_ACI transaction_identifier validation_code
1566 transaction_sequence_num local_transaction_date
1567 local_transaction_time AVS_result_code )) {
1568 $capture{$field} = $transaction->$field() if $transaction->can($field);
1571 $capture->content( %capture );
1575 unless ( $capture->is_success ) {
1576 my $e = "Authorization sucessful but capture failed, custnum #".
1577 $self->custnum. ': '. $capture->result_code.
1578 ": ". $capture->error_message;
1586 if ( $transaction->is_success() ) {
1588 my %method2payby = (
1594 my $cust_pay = new FS::cust_pay ( {
1595 'custnum' => $self->custnum,
1596 'invnum' => $options{'invnum'},
1599 'payby' => $method2payby{$method},
1600 'payinfo' => $self->payinfo,
1601 'paybatch' => "$processor:". $transaction->authorization,
1603 my $error = $cust_pay->insert;
1605 # gah, even with transactions.
1606 my $e = 'WARNING: Card/ACH debited but database not updated - '.
1607 'error applying payment, invnum #' . $self->invnum.
1608 " ($processor): $error";
1617 my $perror = "$processor error: ". $transaction->error_message;
1619 if ( !$options{'quiet'} && $conf->exists('emaildecline')
1620 && grep { $_ ne 'POST' } $self->invoicing_list
1622 my @templ = $conf->config('declinetemplate');
1623 my $template = new Text::Template (
1625 SOURCE => [ map "$_\n", @templ ],
1626 ) or return "($perror) can't create template: $Text::Template::ERROR";
1627 $template->compile()
1628 or return "($perror) can't compile template: $Text::Template::ERROR";
1630 my $templ_hash = { error => $transaction->error_message };
1632 my $error = send_email(
1633 'from' => $conf->config('invoice_from'),
1634 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
1635 'subject' => 'Your payment could not be processed',
1636 'body' => [ $template->fill_in(HASH => $templ_hash) ],
1639 $perror .= " (also received error sending decline notification: $error)"
1651 Returns the total owed for this customer on all invoices
1652 (see L<FS::cust_bill/owed>).
1658 $self->total_owed_date(2145859200); #12/31/2037
1661 =item total_owed_date TIME
1663 Returns the total owed for this customer on all invoices with date earlier than
1664 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
1665 see L<Time::Local> and L<Date::Parse> for conversion functions.
1669 sub total_owed_date {
1673 foreach my $cust_bill (
1674 grep { $_->_date <= $time }
1675 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
1677 $total_bill += $cust_bill->owed;
1679 sprintf( "%.2f", $total_bill );
1684 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
1685 to outstanding invoice balances in chronological order and returns the value
1686 of any remaining unapplied credits available for refund
1687 (see L<FS::cust_refund>).
1694 return 0 unless $self->total_credited;
1696 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
1697 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
1699 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
1700 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
1704 foreach my $cust_bill ( @invoices ) {
1707 if ( !defined($credit) || $credit->credited == 0) {
1708 $credit = pop @credits or last;
1711 if ($cust_bill->owed >= $credit->credited) {
1712 $amount=$credit->credited;
1714 $amount=$cust_bill->owed;
1717 my $cust_credit_bill = new FS::cust_credit_bill ( {
1718 'crednum' => $credit->crednum,
1719 'invnum' => $cust_bill->invnum,
1720 'amount' => $amount,
1722 my $error = $cust_credit_bill->insert;
1723 die $error if $error;
1725 redo if ($cust_bill->owed > 0);
1729 return $self->total_credited;
1732 =item apply_payments
1734 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
1735 to outstanding invoice balances in chronological order.
1737 #and returns the value of any remaining unapplied payments.
1741 sub apply_payments {
1746 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
1747 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
1749 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
1750 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
1754 foreach my $cust_bill ( @invoices ) {
1757 if ( !defined($payment) || $payment->unapplied == 0 ) {
1758 $payment = pop @payments or last;
1761 if ( $cust_bill->owed >= $payment->unapplied ) {
1762 $amount = $payment->unapplied;
1764 $amount = $cust_bill->owed;
1767 my $cust_bill_pay = new FS::cust_bill_pay ( {
1768 'paynum' => $payment->paynum,
1769 'invnum' => $cust_bill->invnum,
1770 'amount' => $amount,
1772 my $error = $cust_bill_pay->insert;
1773 die $error if $error;
1775 redo if ( $cust_bill->owed > 0);
1779 return $self->total_unapplied_payments;
1782 =item total_credited
1784 Returns the total outstanding credit (see L<FS::cust_credit>) for this
1785 customer. See L<FS::cust_credit/credited>.
1789 sub total_credited {
1791 my $total_credit = 0;
1792 foreach my $cust_credit ( qsearch('cust_credit', {
1793 'custnum' => $self->custnum,
1795 $total_credit += $cust_credit->credited;
1797 sprintf( "%.2f", $total_credit );
1800 =item total_unapplied_payments
1802 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
1803 See L<FS::cust_pay/unapplied>.
1807 sub total_unapplied_payments {
1809 my $total_unapplied = 0;
1810 foreach my $cust_pay ( qsearch('cust_pay', {
1811 'custnum' => $self->custnum,
1813 $total_unapplied += $cust_pay->unapplied;
1815 sprintf( "%.2f", $total_unapplied );
1820 Returns the balance for this customer (total_owed minus total_credited
1821 minus total_unapplied_payments).
1828 $self->total_owed - $self->total_credited - $self->total_unapplied_payments
1832 =item balance_date TIME
1834 Returns the balance for this customer, only considering invoices with date
1835 earlier than TIME (total_owed_date minus total_credited minus
1836 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
1837 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
1846 $self->total_owed_date($time)
1847 - $self->total_credited
1848 - $self->total_unapplied_payments
1852 =item invoicing_list [ ARRAYREF ]
1854 If an arguement is given, sets these email addresses as invoice recipients
1855 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
1856 (except as warnings), so use check_invoicing_list first.
1858 Returns a list of email addresses (with svcnum entries expanded).
1860 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
1861 check it without disturbing anything by passing nothing.
1863 This interface may change in the future.
1867 sub invoicing_list {
1868 my( $self, $arrayref ) = @_;
1870 my @cust_main_invoice;
1871 if ( $self->custnum ) {
1872 @cust_main_invoice =
1873 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
1875 @cust_main_invoice = ();
1877 foreach my $cust_main_invoice ( @cust_main_invoice ) {
1878 #warn $cust_main_invoice->destnum;
1879 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
1880 #warn $cust_main_invoice->destnum;
1881 my $error = $cust_main_invoice->delete;
1882 warn $error if $error;
1885 if ( $self->custnum ) {
1886 @cust_main_invoice =
1887 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
1889 @cust_main_invoice = ();
1891 my %seen = map { $_->address => 1 } @cust_main_invoice;
1892 foreach my $address ( @{$arrayref} ) {
1893 next if exists $seen{$address} && $seen{$address};
1894 $seen{$address} = 1;
1895 my $cust_main_invoice = new FS::cust_main_invoice ( {
1896 'custnum' => $self->custnum,
1899 my $error = $cust_main_invoice->insert;
1900 warn $error if $error;
1903 if ( $self->custnum ) {
1905 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
1911 =item check_invoicing_list ARRAYREF
1913 Checks these arguements as valid input for the invoicing_list method. If there
1914 is an error, returns the error, otherwise returns false.
1918 sub check_invoicing_list {
1919 my( $self, $arrayref ) = @_;
1920 foreach my $address ( @{$arrayref} ) {
1921 my $cust_main_invoice = new FS::cust_main_invoice ( {
1922 'custnum' => $self->custnum,
1925 my $error = $self->custnum
1926 ? $cust_main_invoice->check
1927 : $cust_main_invoice->checkdest
1929 return $error if $error;
1934 =item set_default_invoicing_list
1936 Sets the invoicing list to all accounts associated with this customer,
1937 overwriting any previous invoicing list.
1941 sub set_default_invoicing_list {
1943 $self->invoicing_list($self->all_emails);
1948 Returns the email addresses of all accounts provisioned for this customer.
1955 foreach my $cust_pkg ( $self->all_pkgs ) {
1956 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
1958 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
1959 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
1961 $list{$_}=1 foreach map { $_->email } @svc_acct;
1966 =item invoicing_list_addpost
1968 Adds postal invoicing to this customer. If this customer is already configured
1969 to receive postal invoices, does nothing.
1973 sub invoicing_list_addpost {
1975 return if grep { $_ eq 'POST' } $self->invoicing_list;
1976 my @invoicing_list = $self->invoicing_list;
1977 push @invoicing_list, 'POST';
1978 $self->invoicing_list(\@invoicing_list);
1981 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
1983 Returns an array of customers referred by this customer (referral_custnum set
1984 to this custnum). If DEPTH is given, recurses up to the given depth, returning
1985 customers referred by customers referred by this customer and so on, inclusive.
1986 The default behavior is DEPTH 1 (no recursion).
1990 sub referral_cust_main {
1992 my $depth = @_ ? shift : 1;
1993 my $exclude = @_ ? shift : {};
1996 map { $exclude->{$_->custnum}++; $_; }
1997 grep { ! $exclude->{ $_->custnum } }
1998 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
2002 map { $_->referral_cust_main($depth-1, $exclude) }
2009 =item referral_cust_main_ncancelled
2011 Same as referral_cust_main, except only returns customers with uncancelled
2016 sub referral_cust_main_ncancelled {
2018 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
2021 =item referral_cust_pkg [ DEPTH ]
2023 Like referral_cust_main, except returns a flat list of all unsuspended (and
2024 uncancelled) packages for each customer. The number of items in this list may
2025 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
2029 sub referral_cust_pkg {
2031 my $depth = @_ ? shift : 1;
2033 map { $_->unsuspended_pkgs }
2034 grep { $_->unsuspended_pkgs }
2035 $self->referral_cust_main($depth);
2038 =item credit AMOUNT, REASON
2040 Applies a credit to this customer. If there is an error, returns the error,
2041 otherwise returns false.
2046 my( $self, $amount, $reason ) = @_;
2047 my $cust_credit = new FS::cust_credit {
2048 'custnum' => $self->custnum,
2049 'amount' => $amount,
2050 'reason' => $reason,
2052 $cust_credit->insert;
2055 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
2057 Creates a one-time charge for this customer. If there is an error, returns
2058 the error, otherwise returns false.
2063 my ( $self, $amount ) = ( shift, shift );
2064 my $pkg = @_ ? shift : 'One-time charge';
2065 my $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
2066 my $taxclass = @_ ? shift : '';
2068 local $SIG{HUP} = 'IGNORE';
2069 local $SIG{INT} = 'IGNORE';
2070 local $SIG{QUIT} = 'IGNORE';
2071 local $SIG{TERM} = 'IGNORE';
2072 local $SIG{TSTP} = 'IGNORE';
2073 local $SIG{PIPE} = 'IGNORE';
2075 my $oldAutoCommit = $FS::UID::AutoCommit;
2076 local $FS::UID::AutoCommit = 0;
2079 my $part_pkg = new FS::part_pkg ( {
2081 'comment' => $comment,
2086 'taxclass' => $taxclass,
2089 my $error = $part_pkg->insert;
2091 $dbh->rollback if $oldAutoCommit;
2095 my $pkgpart = $part_pkg->pkgpart;
2096 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
2097 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
2098 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
2099 $error = $type_pkgs->insert;
2101 $dbh->rollback if $oldAutoCommit;
2106 my $cust_pkg = new FS::cust_pkg ( {
2107 'custnum' => $self->custnum,
2108 'pkgpart' => $pkgpart,
2111 $error = $cust_pkg->insert;
2113 $dbh->rollback if $oldAutoCommit;
2117 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2124 Returns all the invoices (see L<FS::cust_bill>) for this customer.
2130 sort { $a->_date <=> $b->_date }
2131 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2134 =item open_cust_bill
2136 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
2141 sub open_cust_bill {
2143 grep { $_->owed > 0 } $self->cust_bill;
2152 =item check_and_rebuild_fuzzyfiles
2156 sub check_and_rebuild_fuzzyfiles {
2157 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2158 -e "$dir/cust_main.last" && -e "$dir/cust_main.company"
2159 or &rebuild_fuzzyfiles;
2162 =item rebuild_fuzzyfiles
2166 sub rebuild_fuzzyfiles {
2168 use Fcntl qw(:flock);
2170 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2174 open(LASTLOCK,">>$dir/cust_main.last")
2175 or die "can't open $dir/cust_main.last: $!";
2176 flock(LASTLOCK,LOCK_EX)
2177 or die "can't lock $dir/cust_main.last: $!";
2179 my @all_last = map $_->getfield('last'), qsearch('cust_main', {});
2181 grep $_, map $_->getfield('ship_last'), qsearch('cust_main',{})
2182 if defined dbdef->table('cust_main')->column('ship_last');
2184 open (LASTCACHE,">$dir/cust_main.last.tmp")
2185 or die "can't open $dir/cust_main.last.tmp: $!";
2186 print LASTCACHE join("\n", @all_last), "\n";
2187 close LASTCACHE or die "can't close $dir/cust_main.last.tmp: $!";
2189 rename "$dir/cust_main.last.tmp", "$dir/cust_main.last";
2194 open(COMPANYLOCK,">>$dir/cust_main.company")
2195 or die "can't open $dir/cust_main.company: $!";
2196 flock(COMPANYLOCK,LOCK_EX)
2197 or die "can't lock $dir/cust_main.company: $!";
2199 my @all_company = grep $_ ne '', map $_->company, qsearch('cust_main',{});
2201 grep $_ ne '', map $_->ship_company, qsearch('cust_main', {})
2202 if defined dbdef->table('cust_main')->column('ship_last');
2204 open (COMPANYCACHE,">$dir/cust_main.company.tmp")
2205 or die "can't open $dir/cust_main.company.tmp: $!";
2206 print COMPANYCACHE join("\n", @all_company), "\n";
2207 close COMPANYCACHE or die "can't close $dir/cust_main.company.tmp: $!";
2209 rename "$dir/cust_main.company.tmp", "$dir/cust_main.company";
2219 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2220 open(LASTCACHE,"<$dir/cust_main.last")
2221 or die "can't open $dir/cust_main.last: $!";
2222 my @array = map { chomp; $_; } <LASTCACHE>;
2232 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2233 open(COMPANYCACHE,"<$dir/cust_main.company")
2234 or die "can't open $dir/cust_main.last: $!";
2235 my @array = map { chomp; $_; } <COMPANYCACHE>;
2240 =item append_fuzzyfiles LASTNAME COMPANY
2244 sub append_fuzzyfiles {
2245 my( $last, $company ) = @_;
2247 &check_and_rebuild_fuzzyfiles;
2249 use Fcntl qw(:flock);
2251 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2255 open(LAST,">>$dir/cust_main.last")
2256 or die "can't open $dir/cust_main.last: $!";
2258 or die "can't lock $dir/cust_main.last: $!";
2260 print LAST "$last\n";
2263 or die "can't unlock $dir/cust_main.last: $!";
2269 open(COMPANY,">>$dir/cust_main.company")
2270 or die "can't open $dir/cust_main.company: $!";
2271 flock(COMPANY,LOCK_EX)
2272 or die "can't lock $dir/cust_main.company: $!";
2274 print COMPANY "$company\n";
2276 flock(COMPANY,LOCK_UN)
2277 or die "can't unlock $dir/cust_main.company: $!";
2291 #warn join('-',keys %$param);
2292 my $fh = $param->{filehandle};
2293 my $agentnum = $param->{agentnum};
2294 my $refnum = $param->{refnum};
2295 my $pkgpart = $param->{pkgpart};
2296 my @fields = @{$param->{fields}};
2298 eval "use Date::Parse;";
2300 eval "use Text::CSV_XS;";
2303 my $csv = new Text::CSV_XS;
2310 local $SIG{HUP} = 'IGNORE';
2311 local $SIG{INT} = 'IGNORE';
2312 local $SIG{QUIT} = 'IGNORE';
2313 local $SIG{TERM} = 'IGNORE';
2314 local $SIG{TSTP} = 'IGNORE';
2315 local $SIG{PIPE} = 'IGNORE';
2317 my $oldAutoCommit = $FS::UID::AutoCommit;
2318 local $FS::UID::AutoCommit = 0;
2321 #while ( $columns = $csv->getline($fh) ) {
2323 while ( defined($line=<$fh>) ) {
2325 $csv->parse($line) or do {
2326 $dbh->rollback if $oldAutoCommit;
2327 return "can't parse: ". $csv->error_input();
2330 my @columns = $csv->fields();
2331 #warn join('-',@columns);
2334 agentnum => $agentnum,
2336 country => 'US', #default
2337 payby => 'BILL', #default
2338 paydate => '12/2037', #default
2340 my $billtime = time;
2341 my %cust_pkg = ( pkgpart => $pkgpart );
2342 foreach my $field ( @fields ) {
2343 if ( $field =~ /^cust_pkg\.(setup|bill|susp|expire|cancel)$/ ) {
2344 #$cust_pkg{$1} = str2time( shift @$columns );
2345 if ( $1 eq 'setup' ) {
2346 $billtime = str2time(shift @columns);
2348 $cust_pkg{$1} = str2time( shift @columns );
2351 #$cust_main{$field} = shift @$columns;
2352 $cust_main{$field} = shift @columns;
2356 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg ) if $pkgpart;
2357 my $cust_main = new FS::cust_main ( \%cust_main );
2359 tie my %hash, 'Tie::RefHash'; #this part is important
2360 $hash{$cust_pkg} = [] if $pkgpart;
2361 my $error = $cust_main->insert( \%hash );
2364 $dbh->rollback if $oldAutoCommit;
2365 return "can't insert customer for $line: $error";
2368 #false laziness w/bill.cgi
2369 $error = $cust_main->bill( 'time' => $billtime );
2371 $dbh->rollback if $oldAutoCommit;
2372 return "can't bill customer for $line: $error";
2375 $cust_main->apply_payments;
2376 $cust_main->apply_credits;
2378 $error = $cust_main->collect();
2380 $dbh->rollback if $oldAutoCommit;
2381 return "can't collect customer for $line: $error";
2387 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2389 return "Empty file!" unless $imported;
2401 #warn join('-',keys %$param);
2402 my $fh = $param->{filehandle};
2403 my @fields = @{$param->{fields}};
2405 eval "use Date::Parse;";
2407 eval "use Text::CSV_XS;";
2410 my $csv = new Text::CSV_XS;
2417 local $SIG{HUP} = 'IGNORE';
2418 local $SIG{INT} = 'IGNORE';
2419 local $SIG{QUIT} = 'IGNORE';
2420 local $SIG{TERM} = 'IGNORE';
2421 local $SIG{TSTP} = 'IGNORE';
2422 local $SIG{PIPE} = 'IGNORE';
2424 my $oldAutoCommit = $FS::UID::AutoCommit;
2425 local $FS::UID::AutoCommit = 0;
2428 #while ( $columns = $csv->getline($fh) ) {
2430 while ( defined($line=<$fh>) ) {
2432 $csv->parse($line) or do {
2433 $dbh->rollback if $oldAutoCommit;
2434 return "can't parse: ". $csv->error_input();
2437 my @columns = $csv->fields();
2438 #warn join('-',@columns);
2441 foreach my $field ( @fields ) {
2442 $row{$field} = shift @columns;
2445 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
2446 unless ( $cust_main ) {
2447 $dbh->rollback if $oldAutoCommit;
2448 return "unknown custnum $row{'custnum'}";
2451 if ( $row{'amount'} > 0 ) {
2452 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
2454 $dbh->rollback if $oldAutoCommit;
2458 } elsif ( $row{'amount'} < 0 ) {
2459 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
2462 $dbh->rollback if $oldAutoCommit;
2472 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2474 return "Empty file!" unless $imported;
2486 The delete method should possibly take an FS::cust_main object reference
2487 instead of a scalar customer number.
2489 Bill and collect options should probably be passed as references instead of a
2492 There should probably be a configuration file with a list of allowed credit
2495 No multiple currency support (probably a larger project than just this module).
2499 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
2500 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
2501 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.