4 use vars qw( @ISA $conf $Debug $import );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
9 eval "use Time::Local;";
10 die "Time::Local minimum version 1.05 required with Perl versions before 5.6"
11 if $] < 5.006 && !defined($Time::Local::VERSION);
12 eval "use Time::Local qw(timelocal timelocal_nocheck);";
16 use Business::CreditCard;
17 use FS::UID qw( getotaker dbh );
18 use FS::Record qw( qsearchs qsearch dbdef );
19 use FS::Misc qw( send_email );
22 use FS::cust_bill_pkg;
25 use FS::part_referral;
26 use FS::cust_main_county;
28 use FS::cust_main_invoice;
29 use FS::cust_credit_bill;
30 use FS::cust_bill_pay;
31 use FS::prepay_credit;
34 use FS::part_bill_event;
35 use FS::cust_bill_event;
36 use FS::cust_tax_exempt;
38 use FS::Msgcat qw(gettext);
40 @ISA = qw( FS::Record );
42 $realtime_bop_decline_quiet = 0;
49 #ask FS::UID to run this stuff for us later
50 #$FS::UID::callback{'FS::cust_main'} = sub {
51 install_callback FS::UID sub {
53 #yes, need it for stuff below (prolly should be cached)
58 my ( $hashref, $cache ) = @_;
59 if ( exists $hashref->{'pkgnum'} ) {
60 # #@{ $self->{'_pkgnum'} } = ();
61 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
62 $self->{'_pkgnum'} = $subcache;
63 #push @{ $self->{'_pkgnum'} },
64 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
70 FS::cust_main - Object methods for cust_main records
76 $record = new FS::cust_main \%hash;
77 $record = new FS::cust_main { 'column' => 'value' };
79 $error = $record->insert;
81 $error = $new_record->replace($old_record);
83 $error = $record->delete;
85 $error = $record->check;
87 @cust_pkg = $record->all_pkgs;
89 @cust_pkg = $record->ncancelled_pkgs;
91 @cust_pkg = $record->suspended_pkgs;
93 $error = $record->bill;
94 $error = $record->bill %options;
95 $error = $record->bill 'time' => $time;
97 $error = $record->collect;
98 $error = $record->collect %options;
99 $error = $record->collect 'invoice_time' => $time,
100 'batch_card' => 'yes',
101 'report_badcard' => 'yes',
106 An FS::cust_main object represents a customer. FS::cust_main inherits from
107 FS::Record. The following fields are currently supported:
111 =item custnum - primary key (assigned automatically for new customers)
113 =item agentnum - agent (see L<FS::agent>)
115 =item refnum - Advertising source (see L<FS::part_referral>)
121 =item ss - social security number (optional)
123 =item company - (optional)
127 =item address2 - (optional)
131 =item county - (optional, see L<FS::cust_main_county>)
133 =item state - (see L<FS::cust_main_county>)
137 =item country - (see L<FS::cust_main_county>)
139 =item daytime - phone (optional)
141 =item night - phone (optional)
143 =item fax - phone (optional)
145 =item ship_first - name
147 =item ship_last - name
149 =item ship_company - (optional)
153 =item ship_address2 - (optional)
157 =item ship_county - (optional, see L<FS::cust_main_county>)
159 =item ship_state - (see L<FS::cust_main_county>)
163 =item ship_country - (see L<FS::cust_main_county>)
165 =item ship_daytime - phone (optional)
167 =item ship_night - phone (optional)
169 =item ship_fax - phone (optional)
171 =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>)
173 =item payinfo - card number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
175 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
177 =item payname - name on card or billing name
179 =item tax - tax exempt, empty or `Y'
181 =item otaker - order taker (assigned automatically, see L<FS::UID>)
183 =item comments - comments (optional)
185 =item referral_custnum - referring customer number
195 Creates a new customer. To add the customer to the database, see L<"insert">.
197 Note that this stores the hash reference, not a distinct copy of the hash it
198 points to. You can ask the object for a copy with the I<hash> method.
202 sub table { 'cust_main'; }
204 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
206 Adds this customer to the database. If there is an error, returns the error,
207 otherwise returns false.
209 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
210 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
211 are inserted atomicly, or the transaction is rolled back. Passing an empty
212 hash reference is equivalent to not supplying this parameter. There should be
213 a better explanation of this, but until then, here's an example:
216 tie %hash, 'Tie::RefHash'; #this part is important
218 $cust_pkg => [ $svc_acct ],
221 $cust_main->insert( \%hash );
223 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
224 be set as the invoicing list (see L<"invoicing_list">). Errors return as
225 expected and rollback the entire transaction; it is not necessary to call
226 check_invoicing_list first. The invoicing_list is set after the records in the
227 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
228 invoicing_list destination to the newly-created svc_acct. Here's an example:
230 $cust_main->insert( {}, [ $email, 'POST' ] );
232 Currently available options are: I<noexport>
234 If I<noexport> is set true, no provisioning jobs (exports) are scheduled.
235 (You can schedule them later with the B<reexport> method.)
241 my $cust_pkgs = @_ ? shift : {};
242 my $invoicing_list = @_ ? shift : '';
245 local $SIG{HUP} = 'IGNORE';
246 local $SIG{INT} = 'IGNORE';
247 local $SIG{QUIT} = 'IGNORE';
248 local $SIG{TERM} = 'IGNORE';
249 local $SIG{TSTP} = 'IGNORE';
250 local $SIG{PIPE} = 'IGNORE';
252 my $oldAutoCommit = $FS::UID::AutoCommit;
253 local $FS::UID::AutoCommit = 0;
258 if ( $self->payby eq 'PREPAY' ) {
259 $self->payby('BILL');
260 my $prepay_credit = qsearchs(
262 { 'identifier' => $self->payinfo },
266 warn "WARNING: can't find pre-found prepay_credit: ". $self->payinfo
267 unless $prepay_credit;
268 $amount = $prepay_credit->amount;
269 $seconds = $prepay_credit->seconds;
270 my $error = $prepay_credit->delete;
272 $dbh->rollback if $oldAutoCommit;
273 return "removing prepay_credit (transaction rolled back): $error";
277 my $error = $self->SUPER::insert;
279 $dbh->rollback if $oldAutoCommit;
280 #return "inserting cust_main record (transaction rolled back): $error";
285 if ( $invoicing_list ) {
286 $error = $self->check_invoicing_list( $invoicing_list );
288 $dbh->rollback if $oldAutoCommit;
289 return "checking invoicing_list (transaction rolled back): $error";
291 $self->invoicing_list( $invoicing_list );
295 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
296 $error = $self->order_pkgs($cust_pkgs, \$seconds);
298 $dbh->rollback if $oldAutoCommit;
303 $dbh->rollback if $oldAutoCommit;
304 return "No svc_acct record to apply pre-paid time";
308 my $cust_credit = new FS::cust_credit {
309 'custnum' => $self->custnum,
312 $error = $cust_credit->insert;
314 $dbh->rollback if $oldAutoCommit;
315 return "inserting credit (transaction rolled back): $error";
319 $error = $self->queue_fuzzyfiles_update;
321 $dbh->rollback if $oldAutoCommit;
322 return "updating fuzzy search cache: $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;
380 document me. Re-schedules all exports by calling the B<reexport> method
381 of all associated packages (see L<FS::cust_pkg>). If there is an error,
382 returns the error; otherwise returns false.
389 local $SIG{HUP} = 'IGNORE';
390 local $SIG{INT} = 'IGNORE';
391 local $SIG{QUIT} = 'IGNORE';
392 local $SIG{TERM} = 'IGNORE';
393 local $SIG{TSTP} = 'IGNORE';
394 local $SIG{PIPE} = 'IGNORE';
396 my $oldAutoCommit = $FS::UID::AutoCommit;
397 local $FS::UID::AutoCommit = 0;
400 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
401 my $error = $cust_pkg->reexport;
403 $dbh->rollback if $oldAutoCommit;
408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
413 =item delete NEW_CUSTNUM
415 This deletes the customer. If there is an error, returns the error, otherwise
418 This will completely remove all traces of the customer record. This is not
419 what you want when a customer cancels service; for that, cancel all of the
420 customer's packages (see L</cancel>).
422 If the customer has any uncancelled packages, you need to pass a new (valid)
423 customer number for those packages to be transferred to. Cancelled packages
424 will be deleted. Did I mention that this is NOT what you want when a customer
425 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
427 You can't delete a customer with invoices (see L<FS::cust_bill>),
428 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
429 refunds (see L<FS::cust_refund>).
436 local $SIG{HUP} = 'IGNORE';
437 local $SIG{INT} = 'IGNORE';
438 local $SIG{QUIT} = 'IGNORE';
439 local $SIG{TERM} = 'IGNORE';
440 local $SIG{TSTP} = 'IGNORE';
441 local $SIG{PIPE} = 'IGNORE';
443 my $oldAutoCommit = $FS::UID::AutoCommit;
444 local $FS::UID::AutoCommit = 0;
447 if ( qsearch( 'cust_bill', { 'custnum' => $self->custnum } ) ) {
448 $dbh->rollback if $oldAutoCommit;
449 return "Can't delete a customer with invoices";
451 if ( qsearch( 'cust_credit', { 'custnum' => $self->custnum } ) ) {
452 $dbh->rollback if $oldAutoCommit;
453 return "Can't delete a customer with credits";
455 if ( qsearch( 'cust_pay', { 'custnum' => $self->custnum } ) ) {
456 $dbh->rollback if $oldAutoCommit;
457 return "Can't delete a customer with payments";
459 if ( qsearch( 'cust_refund', { 'custnum' => $self->custnum } ) ) {
460 $dbh->rollback if $oldAutoCommit;
461 return "Can't delete a customer with refunds";
464 my @cust_pkg = $self->ncancelled_pkgs;
466 my $new_custnum = shift;
467 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
468 $dbh->rollback if $oldAutoCommit;
469 return "Invalid new customer number: $new_custnum";
471 foreach my $cust_pkg ( @cust_pkg ) {
472 my %hash = $cust_pkg->hash;
473 $hash{'custnum'} = $new_custnum;
474 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
475 my $error = $new_cust_pkg->replace($cust_pkg);
477 $dbh->rollback if $oldAutoCommit;
482 my @cancelled_cust_pkg = $self->all_pkgs;
483 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
484 my $error = $cust_pkg->delete;
486 $dbh->rollback if $oldAutoCommit;
491 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
492 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
494 my $error = $cust_main_invoice->delete;
496 $dbh->rollback if $oldAutoCommit;
501 my $error = $self->SUPER::delete;
503 $dbh->rollback if $oldAutoCommit;
507 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
512 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
514 Replaces the OLD_RECORD with this one in the database. If there is an error,
515 returns the error, otherwise returns false.
517 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
518 be set as the invoicing list (see L<"invoicing_list">). Errors return as
519 expected and rollback the entire transaction; it is not necessary to call
520 check_invoicing_list first. Here's an example:
522 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
531 local $SIG{HUP} = 'IGNORE';
532 local $SIG{INT} = 'IGNORE';
533 local $SIG{QUIT} = 'IGNORE';
534 local $SIG{TERM} = 'IGNORE';
535 local $SIG{TSTP} = 'IGNORE';
536 local $SIG{PIPE} = 'IGNORE';
538 if ( $self->payby eq 'COMP' && $self->payby ne $old->payby
539 && $conf->config('users-allow_comp') ) {
540 return "You are not permitted to create complimentary accounts."
541 unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
544 my $oldAutoCommit = $FS::UID::AutoCommit;
545 local $FS::UID::AutoCommit = 0;
548 my $error = $self->SUPER::replace($old);
551 $dbh->rollback if $oldAutoCommit;
555 if ( @param ) { # INVOICING_LIST_ARYREF
556 my $invoicing_list = shift @param;
557 $error = $self->check_invoicing_list( $invoicing_list );
559 $dbh->rollback if $oldAutoCommit;
562 $self->invoicing_list( $invoicing_list );
565 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
566 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
567 # card/check/lec info has changed, want to retry realtime_ invoice events
568 my $error = $self->retry_realtime;
570 $dbh->rollback if $oldAutoCommit;
575 $error = $self->queue_fuzzyfiles_update;
577 $dbh->rollback if $oldAutoCommit;
578 return "updating fuzzy search cache: $error";
581 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
586 =item queue_fuzzyfiles_update
588 Used by insert & replace to update the fuzzy search cache
592 sub queue_fuzzyfiles_update {
595 local $SIG{HUP} = 'IGNORE';
596 local $SIG{INT} = 'IGNORE';
597 local $SIG{QUIT} = 'IGNORE';
598 local $SIG{TERM} = 'IGNORE';
599 local $SIG{TSTP} = 'IGNORE';
600 local $SIG{PIPE} = 'IGNORE';
602 my $oldAutoCommit = $FS::UID::AutoCommit;
603 local $FS::UID::AutoCommit = 0;
606 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
607 my $error = $queue->insert($self->getfield('last'), $self->company);
609 $dbh->rollback if $oldAutoCommit;
610 return "queueing job (transaction rolled back): $error";
613 if ( defined $self->dbdef_table->column('ship_last') && $self->ship_last ) {
614 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
615 $error = $queue->insert($self->getfield('ship_last'), $self->ship_company);
617 $dbh->rollback if $oldAutoCommit;
618 return "queueing job (transaction rolled back): $error";
622 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
629 Checks all fields to make sure this is a valid customer record. If there is
630 an error, returns the error, otherwise returns false. Called by the insert
638 #warn "BEFORE: \n". $self->_dump;
641 $self->ut_numbern('custnum')
642 || $self->ut_number('agentnum')
643 || $self->ut_number('refnum')
644 || $self->ut_name('last')
645 || $self->ut_name('first')
646 || $self->ut_textn('company')
647 || $self->ut_text('address1')
648 || $self->ut_textn('address2')
649 || $self->ut_text('city')
650 || $self->ut_textn('county')
651 || $self->ut_textn('state')
652 || $self->ut_country('country')
653 || $self->ut_anything('comments')
654 || $self->ut_numbern('referral_custnum')
656 #barf. need message catalogs. i18n. etc.
657 $error .= "Please select an advertising source."
658 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
659 return $error if $error;
661 return "Unknown agent"
662 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
664 return "Unknown refnum"
665 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
667 return "Unknown referring custnum ". $self->referral_custnum
668 unless ! $self->referral_custnum
669 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
671 if ( $self->ss eq '' ) {
676 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
677 or return "Illegal social security number: ". $self->ss;
678 $self->ss("$1-$2-$3");
682 # bad idea to disable, causes billing to fail because of no tax rates later
683 # unless ( $import ) {
684 unless ( qsearch('cust_main_county', {
685 'country' => $self->country,
688 return "Unknown state/county/country: ".
689 $self->state. "/". $self->county. "/". $self->country
690 unless qsearch('cust_main_county',{
691 'state' => $self->state,
692 'county' => $self->county,
693 'country' => $self->country,
699 $self->ut_phonen('daytime', $self->country)
700 || $self->ut_phonen('night', $self->country)
701 || $self->ut_phonen('fax', $self->country)
702 || $self->ut_zip('zip', $self->country)
704 return $error if $error;
707 last first company address1 address2 city county state zip
708 country daytime night fax
711 if ( defined $self->dbdef_table->column('ship_last') ) {
712 if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
714 && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
718 $self->ut_name('ship_last')
719 || $self->ut_name('ship_first')
720 || $self->ut_textn('ship_company')
721 || $self->ut_text('ship_address1')
722 || $self->ut_textn('ship_address2')
723 || $self->ut_text('ship_city')
724 || $self->ut_textn('ship_county')
725 || $self->ut_textn('ship_state')
726 || $self->ut_country('ship_country')
728 return $error if $error;
730 #false laziness with above
731 unless ( qsearchs('cust_main_county', {
732 'country' => $self->ship_country,
735 return "Unknown ship_state/ship_county/ship_country: ".
736 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
737 unless qsearchs('cust_main_county',{
738 'state' => $self->ship_state,
739 'county' => $self->ship_county,
740 'country' => $self->ship_country,
746 $self->ut_phonen('ship_daytime', $self->ship_country)
747 || $self->ut_phonen('ship_night', $self->ship_country)
748 || $self->ut_phonen('ship_fax', $self->ship_country)
749 || $self->ut_zip('ship_zip', $self->ship_country)
751 return $error if $error;
753 } else { # ship_ info eq billing info, so don't store dup info in database
754 $self->setfield("ship_$_", '')
755 foreach qw( last first company address1 address2 city county state zip
756 country daytime night fax );
760 $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY)$/
761 or return "Illegal payby: ". $self->payby;
764 if ( $self->payby eq 'CARD' || $self->payby eq 'DCRD' ) {
766 my $payinfo = $self->payinfo;
768 $payinfo =~ /^(\d{13,16})$/
769 or return gettext('invalid_card'); # . ": ". $self->payinfo;
771 $self->payinfo($payinfo);
773 or return gettext('invalid_card'); # . ": ". $self->payinfo;
774 return gettext('unknown_card_type')
775 if cardtype($self->payinfo) eq "Unknown";
777 } elsif ( $self->payby eq 'CHEK' || $self->payby eq 'DCHK' ) {
779 my $payinfo = $self->payinfo;
780 $payinfo =~ s/[^\d\@]//g;
781 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
783 $self->payinfo($payinfo);
785 } elsif ( $self->payby eq 'LECB' ) {
787 my $payinfo = $self->payinfo;
789 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
791 $self->payinfo($payinfo);
793 } elsif ( $self->payby eq 'BILL' ) {
795 $error = $self->ut_textn('payinfo');
796 return "Illegal P.O. number: ". $self->payinfo if $error;
798 } elsif ( $self->payby eq 'COMP' ) {
800 if ( !$self->custnum && $conf->config('users-allow_comp') ) {
801 return "You are not permitted to create complimentary accounts."
802 unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
805 $error = $self->ut_textn('payinfo');
806 return "Illegal comp account issuer: ". $self->payinfo if $error;
808 } elsif ( $self->payby eq 'PREPAY' ) {
810 my $payinfo = $self->payinfo;
811 $payinfo =~ s/\W//g; #anything else would just confuse things
812 $self->payinfo($payinfo);
813 $error = $self->ut_alpha('payinfo');
814 return "Illegal prepayment identifier: ". $self->payinfo if $error;
815 return "Unknown prepayment identifier"
816 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
820 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
821 return "Expriation date required"
822 unless $self->payby =~ /^(BILL|PREPAY|CHEK|LECB)$/;
826 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
827 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
828 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{2})[\/\-]\d+$/ ) {
829 ( $m, $y ) = ( $3, "20$2" );
831 return "Illegal expiration date: ". $self->paydate;
833 $self->paydate("$y-$m-01");
834 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
835 return gettext('expired_card')
836 if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
839 if ( $self->payname eq '' && $self->payby ne 'CHEK' &&
840 ( ! $conf->exists('require_cardname')
841 || $self->payby !~ /^(CARD|DCRD)$/ )
843 $self->payname( $self->first. " ". $self->getfield('last') );
845 $self->payname =~ /^([\w \,\.\-\']+)$/
846 or return gettext('illegal_name'). " payname: ". $self->payname;
850 $self->tax =~ /^(Y?)$/ or return "Illegal tax: ". $self->tax;
853 $self->otaker(getotaker);
855 #warn "AFTER: \n". $self->_dump;
862 Returns all packages (see L<FS::cust_pkg>) for this customer.
868 if ( $self->{'_pkgnum'} ) {
869 values %{ $self->{'_pkgnum'}->cache };
871 qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
875 =item ncancelled_pkgs
877 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
881 sub ncancelled_pkgs {
883 if ( $self->{'_pkgnum'} ) {
884 grep { ! $_->getfield('cancel') } values %{ $self->{'_pkgnum'}->cache };
886 @{ [ # force list context
887 qsearch( 'cust_pkg', {
888 'custnum' => $self->custnum,
891 qsearch( 'cust_pkg', {
892 'custnum' => $self->custnum,
901 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
907 grep { $_->susp } $self->ncancelled_pkgs;
910 =item unflagged_suspended_pkgs
912 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
913 customer (thouse packages without the `manual_flag' set).
917 sub unflagged_suspended_pkgs {
919 return $self->suspended_pkgs
920 unless dbdef->table('cust_pkg')->column('manual_flag');
921 grep { ! $_->manual_flag } $self->suspended_pkgs;
924 =item unsuspended_pkgs
926 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
931 sub unsuspended_pkgs {
933 grep { ! $_->susp } $self->ncancelled_pkgs;
938 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
939 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
940 on success or a list of errors.
946 grep { $_->unsuspend } $self->suspended_pkgs;
951 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
952 Always returns a list: an empty list on success or a list of errors.
958 grep { $_->suspend } $self->unsuspended_pkgs;
961 =item cancel [ OPTION => VALUE ... ]
963 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
965 Available options are: I<quiet>
967 I<quiet> can be set true to supress email cancellation notices.
969 Always returns a list: an empty list on success or a list of errors.
975 grep { $_->cancel(@_) } $self->ncancelled_pkgs;
980 Returns the agent (see L<FS::agent>) for this customer.
986 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
991 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
992 conjunction with the collect method.
994 Options are passed as name-value pairs.
996 The only currently available option is `time', which bills the customer as if
997 it were that time. It is specified as a UNIX timestamp; see
998 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
999 functions. For example:
1003 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1005 If there is an error, returns the error, otherwise returns false.
1010 my( $self, %options ) = @_;
1011 my $time = $options{'time'} || time;
1016 local $SIG{HUP} = 'IGNORE';
1017 local $SIG{INT} = 'IGNORE';
1018 local $SIG{QUIT} = 'IGNORE';
1019 local $SIG{TERM} = 'IGNORE';
1020 local $SIG{TSTP} = 'IGNORE';
1021 local $SIG{PIPE} = 'IGNORE';
1023 my $oldAutoCommit = $FS::UID::AutoCommit;
1024 local $FS::UID::AutoCommit = 0;
1027 # find the packages which are due for billing, find out how much they are
1028 # & generate invoice database.
1030 my( $total_setup, $total_recur ) = ( 0, 0 );
1031 #my( $taxable_setup, $taxable_recur ) = ( 0, 0 );
1032 my @cust_bill_pkg = ();
1034 #my $taxable_charged = 0;##
1039 foreach my $cust_pkg (
1040 qsearch('cust_pkg', { 'custnum' => $self->custnum } )
1043 #NO!! next if $cust_pkg->cancel;
1044 next if $cust_pkg->getfield('cancel');
1046 #? to avoid use of uninitialized value errors... ?
1047 $cust_pkg->setfield('bill', '')
1048 unless defined($cust_pkg->bill);
1050 my $part_pkg = $cust_pkg->part_pkg;
1052 #so we don't modify cust_pkg record unnecessarily
1053 my $cust_pkg_mod_flag = 0;
1054 my %hash = $cust_pkg->hash;
1055 my $old_cust_pkg = new FS::cust_pkg \%hash;
1061 unless ( $cust_pkg->setup ) {
1062 my $setup_prog = $part_pkg->getfield('setup');
1063 $setup_prog =~ /^(.*)$/ or do {
1064 $dbh->rollback if $oldAutoCommit;
1065 return "Illegal setup for pkgpart ". $part_pkg->pkgpart.
1069 $setup_prog = '0' if $setup_prog =~ /^\s*$/;
1071 #my $cpt = new Safe;
1072 ##$cpt->permit(); #what is necessary?
1073 #$cpt->share(qw( $cust_pkg )); #can $cpt now use $cust_pkg methods?
1074 #$setup = $cpt->reval($setup_prog);
1075 $setup = eval $setup_prog;
1076 unless ( defined($setup) ) {
1077 $dbh->rollback if $oldAutoCommit;
1078 return "Error eval-ing part_pkg->setup pkgpart ". $part_pkg->pkgpart.
1079 "(expression $setup_prog): $@";
1081 $cust_pkg->setfield('setup',$time);
1082 $cust_pkg_mod_flag=1;
1088 if ( $part_pkg->getfield('freq') > 0 &&
1089 ! $cust_pkg->getfield('susp') &&
1090 ( $cust_pkg->getfield('bill') || 0 ) <= $time
1092 my $recur_prog = $part_pkg->getfield('recur');
1093 $recur_prog =~ /^(.*)$/ or do {
1094 $dbh->rollback if $oldAutoCommit;
1095 return "Illegal recur for pkgpart ". $part_pkg->pkgpart.
1099 $recur_prog = '0' if $recur_prog =~ /^\s*$/;
1101 # shared with $recur_prog
1102 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1104 #my $cpt = new Safe;
1105 ##$cpt->permit(); #what is necessary?
1106 #$cpt->share(qw( $cust_pkg )); #can $cpt now use $cust_pkg methods?
1107 #$recur = $cpt->reval($recur_prog);
1108 $recur = eval $recur_prog;
1109 unless ( defined($recur) ) {
1110 $dbh->rollback if $oldAutoCommit;
1111 return "Error eval-ing part_pkg->recur pkgpart ". $part_pkg->pkgpart.
1112 "(expression $recur_prog): $@";
1114 #change this bit to use Date::Manip? CAREFUL with timezones (see
1115 # mailing list archive)
1116 my ($sec,$min,$hour,$mday,$mon,$year) =
1117 (localtime($sdate) )[0,1,2,3,4,5];
1119 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
1120 # only for figuring next bill date, nothing else, so, reset $sdate again
1122 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1123 $cust_pkg->last_bill($sdate)
1124 if $cust_pkg->dbdef_table->column('last_bill');
1126 $mon += $part_pkg->freq;
1127 until ( $mon < 12 ) { $mon -= 12; $year++; }
1128 $cust_pkg->setfield('bill',
1129 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
1130 $cust_pkg_mod_flag = 1;
1133 warn "\$setup is undefined" unless defined($setup);
1134 warn "\$recur is undefined" unless defined($recur);
1135 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1137 if ( $cust_pkg_mod_flag ) {
1138 $error=$cust_pkg->replace($old_cust_pkg);
1139 if ( $error ) { #just in case
1140 $dbh->rollback if $oldAutoCommit;
1141 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
1143 $setup = sprintf( "%.2f", $setup );
1144 $recur = sprintf( "%.2f", $recur );
1146 $dbh->rollback if $oldAutoCommit;
1147 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1150 $dbh->rollback if $oldAutoCommit;
1151 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1153 if ( $setup > 0 || $recur > 0 ) {
1154 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1155 'pkgnum' => $cust_pkg->pkgnum,
1159 'edate' => $cust_pkg->bill,
1160 'details' => \@details,
1162 push @cust_bill_pkg, $cust_bill_pkg;
1163 $total_setup += $setup;
1164 $total_recur += $recur;
1166 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
1168 my @taxes = qsearch( 'cust_main_county', {
1169 'state' => $self->state,
1170 'county' => $self->county,
1171 'country' => $self->country,
1172 'taxclass' => $part_pkg->taxclass,
1174 || qsearch( 'cust_main_county', {
1175 'state' => $self->state,
1176 'county' => $self->county,
1177 'country' => $self->country,
1181 # maybe eliminate this entirely, along with all the 0% records
1183 $dbh->rollback if $oldAutoCommit;
1185 "fatal: can't find tax rate for state/county/country/taxclass ".
1186 join('/', ( map $self->$_(), qw(state county country) ),
1187 $part_pkg->taxclass ). "\n";
1190 foreach my $tax ( @taxes ) {
1192 my $taxable_charged = 0;
1193 $taxable_charged += $setup
1194 unless $part_pkg->setuptax =~ /^Y$/i
1195 || $tax->setuptax =~ /^Y$/i;
1196 $taxable_charged += $recur
1197 unless $part_pkg->recurtax =~ /^Y$/i
1198 || $tax->recurtax =~ /^Y$/i;
1199 next unless $taxable_charged;
1201 if ( $tax->exempt_amount ) {
1202 my ($mon,$year) = (localtime($sdate) )[4,5];
1204 my $freq = $part_pkg->freq || 1;
1205 my $taxable_per_month = sprintf("%.2f", $taxable_charged / $freq );
1206 foreach my $which_month ( 1 .. $freq ) {
1208 'custnum' => $self->custnum,
1209 'taxnum' => $tax->taxnum,
1210 'year' => 1900+$year,
1213 #until ( $mon < 12 ) { $mon -= 12; $year++; }
1214 until ( $mon < 13 ) { $mon -= 12; $year++; }
1215 my $cust_tax_exempt =
1216 qsearchs('cust_tax_exempt', \%hash)
1217 || new FS::cust_tax_exempt( { %hash, 'amount' => 0 } );
1218 my $remaining_exemption = sprintf("%.2f",
1219 $tax->exempt_amount - $cust_tax_exempt->amount );
1220 if ( $remaining_exemption > 0 ) {
1221 my $addl = $remaining_exemption > $taxable_per_month
1222 ? $taxable_per_month
1223 : $remaining_exemption;
1224 $taxable_charged -= $addl;
1225 my $new_cust_tax_exempt = new FS::cust_tax_exempt ( {
1226 $cust_tax_exempt->hash,
1228 sprintf("%.2f", $cust_tax_exempt->amount + $addl),
1230 $error = $new_cust_tax_exempt->exemptnum
1231 ? $new_cust_tax_exempt->replace($cust_tax_exempt)
1232 : $new_cust_tax_exempt->insert;
1234 $dbh->rollback if $oldAutoCommit;
1235 return "fatal: can't update cust_tax_exempt: $error";
1238 } # if $remaining_exemption > 0
1240 } #foreach $which_month
1242 } #if $tax->exempt_amount
1244 $taxable_charged = sprintf( "%.2f", $taxable_charged);
1246 #$tax += $taxable_charged * $cust_main_county->tax / 100
1247 $tax{ $tax->taxname || 'Tax' } +=
1248 $taxable_charged * $tax->tax / 100
1250 } #foreach my $tax ( @taxes )
1252 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
1254 } #if $setup > 0 || $recur > 0
1256 } #if $cust_pkg_mod_flag
1258 } #foreach my $cust_pkg
1260 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
1261 # my $taxable_charged = sprintf( "%.2f", $taxable_setup + $taxable_recur );
1263 unless ( @cust_bill_pkg ) { #don't create invoices with no line items
1264 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1268 # unless ( $self->tax =~ /Y/i
1269 # || $self->payby eq 'COMP'
1270 # || $taxable_charged == 0 ) {
1271 # my $cust_main_county = qsearchs('cust_main_county',{
1272 # 'state' => $self->state,
1273 # 'county' => $self->county,
1274 # 'country' => $self->country,
1275 # } ) or die "fatal: can't find tax rate for state/county/country ".
1276 # $self->state. "/". $self->county. "/". $self->country. "\n";
1277 # my $tax = sprintf( "%.2f",
1278 # $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
1281 if ( dbdef->table('cust_bill_pkg')->column('itemdesc') ) { #1.5 schema
1283 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
1284 my $tax = sprintf("%.2f", $tax{$taxname} );
1285 $charged = sprintf( "%.2f", $charged+$tax );
1287 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1293 'itemdesc' => $taxname,
1295 push @cust_bill_pkg, $cust_bill_pkg;
1298 } else { #1.4 schema
1301 foreach ( values %tax ) { $tax += $_ };
1302 $tax = sprintf("%.2f", $tax);
1304 $charged = sprintf( "%.2f", $charged+$tax );
1306 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1313 push @cust_bill_pkg, $cust_bill_pkg;
1318 my $cust_bill = new FS::cust_bill ( {
1319 'custnum' => $self->custnum,
1321 'charged' => $charged,
1323 $error = $cust_bill->insert;
1325 $dbh->rollback if $oldAutoCommit;
1326 return "can't create invoice for customer #". $self->custnum. ": $error";
1329 my $invnum = $cust_bill->invnum;
1331 foreach $cust_bill_pkg ( @cust_bill_pkg ) {
1333 $cust_bill_pkg->invnum($invnum);
1334 $error = $cust_bill_pkg->insert;
1336 $dbh->rollback if $oldAutoCommit;
1337 return "can't create invoice line item for customer #". $self->custnum.
1342 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1346 =item collect OPTIONS
1348 (Attempt to) collect money for this customer's outstanding invoices (see
1349 L<FS::cust_bill>). Usually used after the bill method.
1351 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
1352 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
1353 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
1355 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
1356 and the invoice events web interface.
1358 If there is an error, returns the error, otherwise returns false.
1360 Options are passed as name-value pairs.
1362 Currently available options are:
1364 invoice_time - Use this time when deciding when to print invoices and
1365 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>
1366 for conversion functions.
1368 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
1371 retry_card - Deprecated alias for 'retry'
1373 batch_card - This option is deprecated. See the invoice events web interface
1374 to control whether cards are batched or run against a realtime gateway.
1376 report_badcard - This option is deprecated.
1378 force_print - This option is deprecated; see the invoice events web interface.
1380 quiet - set true to surpress email card/ACH decline notices.
1385 my( $self, %options ) = @_;
1386 my $invoice_time = $options{'invoice_time'} || time;
1389 local $SIG{HUP} = 'IGNORE';
1390 local $SIG{INT} = 'IGNORE';
1391 local $SIG{QUIT} = 'IGNORE';
1392 local $SIG{TERM} = 'IGNORE';
1393 local $SIG{TSTP} = 'IGNORE';
1394 local $SIG{PIPE} = 'IGNORE';
1396 my $oldAutoCommit = $FS::UID::AutoCommit;
1397 local $FS::UID::AutoCommit = 0;
1400 my $balance = $self->balance;
1401 warn "collect customer". $self->custnum. ": balance $balance" if $Debug;
1402 unless ( $balance > 0 ) { #redundant?????
1403 $dbh->rollback if $oldAutoCommit; #hmm
1407 if ( exists($options{'retry_card'}) ) {
1408 carp 'retry_card option passed to collect is deprecated; use retry';
1409 $options{'retry'} ||= $options{'retry_card'};
1411 if ( exists($options{'retry'}) && $options{'retry'} ) {
1412 my $error = $self->retry_realtime;
1414 $dbh->rollback if $oldAutoCommit;
1419 foreach my $cust_bill ( $self->cust_bill ) {
1421 #this has to be before next's
1422 my $amount = sprintf( "%.2f", $balance < $cust_bill->owed
1426 $balance = sprintf( "%.2f", $balance - $amount );
1428 next unless $cust_bill->owed > 0;
1430 # don't try to charge for the same invoice if it's already in a batch
1431 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
1433 warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ", amount $amount, balance $balance)" if $Debug;
1435 next unless $amount > 0;
1438 foreach my $part_bill_event (
1439 sort { $a->seconds <=> $b->seconds
1440 || $a->weight <=> $b->weight
1441 || $a->eventpart <=> $b->eventpart }
1442 grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
1443 && ! qsearchs( 'cust_bill_event', {
1444 'invnum' => $cust_bill->invnum,
1445 'eventpart' => $_->eventpart,
1449 qsearch('part_bill_event', { 'payby' => $self->payby,
1450 'disabled' => '', } )
1453 last unless $cust_bill->owed > 0; #don't run subsequent events if owed=0
1455 warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
1457 my $cust_main = $self; #for callback
1461 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
1462 $error = eval $part_bill_event->eventcode;
1466 my $statustext = '';
1470 } elsif ( $error ) {
1472 $statustext = $error;
1477 #add cust_bill_event
1478 my $cust_bill_event = new FS::cust_bill_event {
1479 'invnum' => $cust_bill->invnum,
1480 'eventpart' => $part_bill_event->eventpart,
1481 #'_date' => $invoice_time,
1483 'status' => $status,
1484 'statustext' => $statustext,
1486 $error = $cust_bill_event->insert;
1488 #$dbh->rollback if $oldAutoCommit;
1489 #return "error: $error";
1491 # gah, even with transactions.
1492 $dbh->commit if $oldAutoCommit; #well.
1493 my $e = 'WARNING: Event run but database not updated - '.
1494 'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
1495 ', eventpart '. $part_bill_event->eventpart.
1506 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1511 =item retry_realtime
1513 Schedules realtime credit card / electronic check / LEC billing events for
1514 for retry. Useful if card information has changed or manual retry is desired.
1515 The 'collect' method must be called to actually retry the transaction.
1517 Implementation details: For each of this customer's open invoices, changes
1518 the status of the first "done" (with statustext error) realtime processing
1523 sub retry_realtime {
1526 local $SIG{HUP} = 'IGNORE';
1527 local $SIG{INT} = 'IGNORE';
1528 local $SIG{QUIT} = 'IGNORE';
1529 local $SIG{TERM} = 'IGNORE';
1530 local $SIG{TSTP} = 'IGNORE';
1531 local $SIG{PIPE} = 'IGNORE';
1533 my $oldAutoCommit = $FS::UID::AutoCommit;
1534 local $FS::UID::AutoCommit = 0;
1537 foreach my $cust_bill (
1538 grep { $_->cust_bill_event }
1539 $self->open_cust_bill
1541 my @cust_bill_event =
1542 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
1544 #$_->part_bill_event->plan eq 'realtime-card'
1545 $_->part_bill_event->eventcode =~
1546 /\$cust_bill\->realtime_(card|ach|lec)/
1547 && $_->status eq 'done'
1550 $cust_bill->cust_bill_event;
1551 next unless @cust_bill_event;
1552 my $error = $cust_bill_event[0]->retry;
1554 $dbh->rollback if $oldAutoCommit;
1555 return "error scheduling invoice event for retry: $error";
1560 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1565 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
1567 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
1568 via a Business::OnlinePayment realtime gateway. See
1569 L<http://420.am/business-onlinepayment> for supported gateways.
1571 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1573 Available options are: I<description>, I<invnum>, I<quiet>
1575 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1576 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
1577 if set, will override the value from the customer record.
1579 I<description> is a free-text field passed to the gateway. It defaults to
1580 "Internet services".
1582 If an I<invnum> is specified, this payment (if sucessful) is applied to the
1583 specified invoice. If you don't specify an I<invnum> you might want to
1584 call the B<apply_payments> method.
1586 I<quiet> can be set true to surpress email decline notices.
1588 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
1593 my( $self, $method, $amount, %options ) = @_;
1595 warn "$self $method $amount\n";
1596 warn " $_ => $options{$_}\n" foreach keys %options;
1599 $options{'description'} ||= 'Internet services';
1602 die "Real-time processing not enabled\n"
1603 unless $conf->exists('business-onlinepayment');
1604 eval "use Business::OnlinePayment";
1608 $self->set( $_ => $options{$_} )
1609 foreach grep { exists($options{$_}) }
1610 qw( payname address1 address2 city state zip payinfo paydate );
1613 my $bop_config = 'business-onlinepayment';
1614 $bop_config .= '-ach'
1615 if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
1616 my ( $processor, $login, $password, $action, @bop_options ) =
1617 $conf->config($bop_config);
1618 $action ||= 'normal authorization';
1619 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1623 my $address = $self->address1;
1624 $address .= ", ". $self->address2 if $self->address2;
1626 my($payname, $payfirst, $paylast);
1627 if ( $self->payname && $method ne 'ECHECK' ) {
1628 $payname = $self->payname;
1629 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1630 or return "Illegal payname $payname";
1631 ($payfirst, $paylast) = ($1, $2);
1633 $payfirst = $self->getfield('first');
1634 $paylast = $self->getfield('last');
1635 $payname = "$payfirst $paylast";
1638 my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
1639 if ( $conf->exists('emailinvoiceauto')
1640 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1641 push @invoicing_list, $self->all_emails;
1643 my $email = $invoicing_list[0];
1646 if ( $method eq 'CC' ) {
1647 $content{card_number} = $self->payinfo;
1648 $self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1649 $content{expiration} = "$2/$1";
1650 } elsif ( $method eq 'ECHECK' ) {
1651 my($account_number,$routing_code) = $self->payinfo;
1652 ( $content{account_number}, $content{routing_code} ) =
1653 split('@', $self->payinfo);
1654 $content{bank_name} = $self->payname;
1655 $content{account_type} = 'CHECKING';
1656 $content{account_name} = $payname;
1657 $content{customer_org} = $self->company ? 'B' : 'I';
1658 $content{customer_ssn} = $self->ss;
1659 } elsif ( $method eq 'LEC' ) {
1660 $content{phone} = $self->payinfo;
1665 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
1668 new Business::OnlinePayment( $processor, @bop_options );
1669 $transaction->content(
1672 'password' => $password,
1673 'action' => $action1,
1674 'description' => $options{'description'},
1675 'amount' => $amount,
1676 'invoice_number' => $options{'invnum'},
1677 'customer_id' => $self->custnum,
1678 'last_name' => $paylast,
1679 'first_name' => $payfirst,
1681 'address' => $address,
1682 'city' => $self->city,
1683 'state' => $self->state,
1684 'zip' => $self->zip,
1685 'country' => $self->country,
1686 'referer' => 'http://cleanwhisker.420.am/',
1688 'phone' => $self->daytime || $self->night,
1691 $transaction->submit();
1693 if ( $transaction->is_success() && $action2 ) {
1694 my $auth = $transaction->authorization;
1695 my $ordernum = $transaction->can('order_number')
1696 ? $transaction->order_number
1700 new Business::OnlinePayment( $processor, @bop_options );
1707 password => $password,
1708 order_number => $ordernum,
1710 authorization => $auth,
1711 description => $options{'description'},
1714 foreach my $field (qw( authorization_source_code returned_ACI transaction_identifier validation_code
1715 transaction_sequence_num local_transaction_date
1716 local_transaction_time AVS_result_code )) {
1717 $capture{$field} = $transaction->$field() if $transaction->can($field);
1720 $capture->content( %capture );
1724 unless ( $capture->is_success ) {
1725 my $e = "Authorization sucessful but capture failed, custnum #".
1726 $self->custnum. ': '. $capture->result_code.
1727 ": ". $capture->error_message;
1735 if ( $transaction->is_success() ) {
1737 my %method2payby = (
1743 my $cust_pay = new FS::cust_pay ( {
1744 'custnum' => $self->custnum,
1745 'invnum' => $options{'invnum'},
1748 'payby' => $method2payby{$method},
1749 'payinfo' => $self->payinfo,
1750 'paybatch' => "$processor:". $transaction->authorization,
1752 my $error = $cust_pay->insert;
1754 # gah, even with transactions.
1755 my $e = 'WARNING: Card/ACH debited but database not updated - '.
1756 'error applying payment, invnum #' . $self->invnum.
1757 " ($processor): $error";
1766 my $perror = "$processor error: ". $transaction->error_message;
1768 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
1769 && $conf->exists('emaildecline')
1770 && grep { $_ ne 'POST' } $self->invoicing_list
1772 my @templ = $conf->config('declinetemplate');
1773 my $template = new Text::Template (
1775 SOURCE => [ map "$_\n", @templ ],
1776 ) or return "($perror) can't create template: $Text::Template::ERROR";
1777 $template->compile()
1778 or return "($perror) can't compile template: $Text::Template::ERROR";
1780 my $templ_hash = { error => $transaction->error_message };
1782 my $error = send_email(
1783 'from' => $conf->config('invoice_from'),
1784 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
1785 'subject' => 'Your payment could not be processed',
1786 'body' => [ $template->fill_in(HASH => $templ_hash) ],
1789 $perror .= " (also received error sending decline notification: $error)"
1801 Returns the total owed for this customer on all invoices
1802 (see L<FS::cust_bill/owed>).
1808 $self->total_owed_date(2145859200); #12/31/2037
1811 =item total_owed_date TIME
1813 Returns the total owed for this customer on all invoices with date earlier than
1814 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
1815 see L<Time::Local> and L<Date::Parse> for conversion functions.
1819 sub total_owed_date {
1823 foreach my $cust_bill (
1824 grep { $_->_date <= $time }
1825 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
1827 $total_bill += $cust_bill->owed;
1829 sprintf( "%.2f", $total_bill );
1834 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
1835 to outstanding invoice balances in chronological order and returns the value
1836 of any remaining unapplied credits available for refund
1837 (see L<FS::cust_refund>).
1844 return 0 unless $self->total_credited;
1846 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
1847 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
1849 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
1850 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
1854 foreach my $cust_bill ( @invoices ) {
1857 if ( !defined($credit) || $credit->credited == 0) {
1858 $credit = pop @credits or last;
1861 if ($cust_bill->owed >= $credit->credited) {
1862 $amount=$credit->credited;
1864 $amount=$cust_bill->owed;
1867 my $cust_credit_bill = new FS::cust_credit_bill ( {
1868 'crednum' => $credit->crednum,
1869 'invnum' => $cust_bill->invnum,
1870 'amount' => $amount,
1872 my $error = $cust_credit_bill->insert;
1873 die $error if $error;
1875 redo if ($cust_bill->owed > 0);
1879 return $self->total_credited;
1882 =item apply_payments
1884 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
1885 to outstanding invoice balances in chronological order.
1887 #and returns the value of any remaining unapplied payments.
1891 sub apply_payments {
1896 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
1897 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
1899 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
1900 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
1904 foreach my $cust_bill ( @invoices ) {
1907 if ( !defined($payment) || $payment->unapplied == 0 ) {
1908 $payment = pop @payments or last;
1911 if ( $cust_bill->owed >= $payment->unapplied ) {
1912 $amount = $payment->unapplied;
1914 $amount = $cust_bill->owed;
1917 my $cust_bill_pay = new FS::cust_bill_pay ( {
1918 'paynum' => $payment->paynum,
1919 'invnum' => $cust_bill->invnum,
1920 'amount' => $amount,
1922 my $error = $cust_bill_pay->insert;
1923 die $error if $error;
1925 redo if ( $cust_bill->owed > 0);
1929 return $self->total_unapplied_payments;
1932 =item total_credited
1934 Returns the total outstanding credit (see L<FS::cust_credit>) for this
1935 customer. See L<FS::cust_credit/credited>.
1939 sub total_credited {
1941 my $total_credit = 0;
1942 foreach my $cust_credit ( qsearch('cust_credit', {
1943 'custnum' => $self->custnum,
1945 $total_credit += $cust_credit->credited;
1947 sprintf( "%.2f", $total_credit );
1950 =item total_unapplied_payments
1952 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
1953 See L<FS::cust_pay/unapplied>.
1957 sub total_unapplied_payments {
1959 my $total_unapplied = 0;
1960 foreach my $cust_pay ( qsearch('cust_pay', {
1961 'custnum' => $self->custnum,
1963 $total_unapplied += $cust_pay->unapplied;
1965 sprintf( "%.2f", $total_unapplied );
1970 Returns the balance for this customer (total_owed minus total_credited
1971 minus total_unapplied_payments).
1978 $self->total_owed - $self->total_credited - $self->total_unapplied_payments
1982 =item balance_date TIME
1984 Returns the balance for this customer, only considering invoices with date
1985 earlier than TIME (total_owed_date minus total_credited minus
1986 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
1987 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
1996 $self->total_owed_date($time)
1997 - $self->total_credited
1998 - $self->total_unapplied_payments
2002 =item invoicing_list [ ARRAYREF ]
2004 If an arguement is given, sets these email addresses as invoice recipients
2005 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
2006 (except as warnings), so use check_invoicing_list first.
2008 Returns a list of email addresses (with svcnum entries expanded).
2010 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
2011 check it without disturbing anything by passing nothing.
2013 This interface may change in the future.
2017 sub invoicing_list {
2018 my( $self, $arrayref ) = @_;
2020 my @cust_main_invoice;
2021 if ( $self->custnum ) {
2022 @cust_main_invoice =
2023 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2025 @cust_main_invoice = ();
2027 foreach my $cust_main_invoice ( @cust_main_invoice ) {
2028 #warn $cust_main_invoice->destnum;
2029 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
2030 #warn $cust_main_invoice->destnum;
2031 my $error = $cust_main_invoice->delete;
2032 warn $error if $error;
2035 if ( $self->custnum ) {
2036 @cust_main_invoice =
2037 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2039 @cust_main_invoice = ();
2041 my %seen = map { $_->address => 1 } @cust_main_invoice;
2042 foreach my $address ( @{$arrayref} ) {
2043 next if exists $seen{$address} && $seen{$address};
2044 $seen{$address} = 1;
2045 my $cust_main_invoice = new FS::cust_main_invoice ( {
2046 'custnum' => $self->custnum,
2049 my $error = $cust_main_invoice->insert;
2050 warn $error if $error;
2053 if ( $self->custnum ) {
2055 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2061 =item check_invoicing_list ARRAYREF
2063 Checks these arguements as valid input for the invoicing_list method. If there
2064 is an error, returns the error, otherwise returns false.
2068 sub check_invoicing_list {
2069 my( $self, $arrayref ) = @_;
2070 foreach my $address ( @{$arrayref} ) {
2071 my $cust_main_invoice = new FS::cust_main_invoice ( {
2072 'custnum' => $self->custnum,
2075 my $error = $self->custnum
2076 ? $cust_main_invoice->check
2077 : $cust_main_invoice->checkdest
2079 return $error if $error;
2084 =item set_default_invoicing_list
2086 Sets the invoicing list to all accounts associated with this customer,
2087 overwriting any previous invoicing list.
2091 sub set_default_invoicing_list {
2093 $self->invoicing_list($self->all_emails);
2098 Returns the email addresses of all accounts provisioned for this customer.
2105 foreach my $cust_pkg ( $self->all_pkgs ) {
2106 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
2108 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2109 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2111 $list{$_}=1 foreach map { $_->email } @svc_acct;
2116 =item invoicing_list_addpost
2118 Adds postal invoicing to this customer. If this customer is already configured
2119 to receive postal invoices, does nothing.
2123 sub invoicing_list_addpost {
2125 return if grep { $_ eq 'POST' } $self->invoicing_list;
2126 my @invoicing_list = $self->invoicing_list;
2127 push @invoicing_list, 'POST';
2128 $self->invoicing_list(\@invoicing_list);
2131 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
2133 Returns an array of customers referred by this customer (referral_custnum set
2134 to this custnum). If DEPTH is given, recurses up to the given depth, returning
2135 customers referred by customers referred by this customer and so on, inclusive.
2136 The default behavior is DEPTH 1 (no recursion).
2140 sub referral_cust_main {
2142 my $depth = @_ ? shift : 1;
2143 my $exclude = @_ ? shift : {};
2146 map { $exclude->{$_->custnum}++; $_; }
2147 grep { ! $exclude->{ $_->custnum } }
2148 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
2152 map { $_->referral_cust_main($depth-1, $exclude) }
2159 =item referral_cust_main_ncancelled
2161 Same as referral_cust_main, except only returns customers with uncancelled
2166 sub referral_cust_main_ncancelled {
2168 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
2171 =item referral_cust_pkg [ DEPTH ]
2173 Like referral_cust_main, except returns a flat list of all unsuspended (and
2174 uncancelled) packages for each customer. The number of items in this list may
2175 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
2179 sub referral_cust_pkg {
2181 my $depth = @_ ? shift : 1;
2183 map { $_->unsuspended_pkgs }
2184 grep { $_->unsuspended_pkgs }
2185 $self->referral_cust_main($depth);
2188 =item credit AMOUNT, REASON
2190 Applies a credit to this customer. If there is an error, returns the error,
2191 otherwise returns false.
2196 my( $self, $amount, $reason ) = @_;
2197 my $cust_credit = new FS::cust_credit {
2198 'custnum' => $self->custnum,
2199 'amount' => $amount,
2200 'reason' => $reason,
2202 $cust_credit->insert;
2205 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
2207 Creates a one-time charge for this customer. If there is an error, returns
2208 the error, otherwise returns false.
2213 my ( $self, $amount ) = ( shift, shift );
2214 my $pkg = @_ ? shift : 'One-time charge';
2215 my $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
2216 my $taxclass = @_ ? shift : '';
2218 local $SIG{HUP} = 'IGNORE';
2219 local $SIG{INT} = 'IGNORE';
2220 local $SIG{QUIT} = 'IGNORE';
2221 local $SIG{TERM} = 'IGNORE';
2222 local $SIG{TSTP} = 'IGNORE';
2223 local $SIG{PIPE} = 'IGNORE';
2225 my $oldAutoCommit = $FS::UID::AutoCommit;
2226 local $FS::UID::AutoCommit = 0;
2229 my $part_pkg = new FS::part_pkg ( {
2231 'comment' => $comment,
2236 'taxclass' => $taxclass,
2239 my $error = $part_pkg->insert;
2241 $dbh->rollback if $oldAutoCommit;
2245 my $pkgpart = $part_pkg->pkgpart;
2246 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
2247 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
2248 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
2249 $error = $type_pkgs->insert;
2251 $dbh->rollback if $oldAutoCommit;
2256 my $cust_pkg = new FS::cust_pkg ( {
2257 'custnum' => $self->custnum,
2258 'pkgpart' => $pkgpart,
2261 $error = $cust_pkg->insert;
2263 $dbh->rollback if $oldAutoCommit;
2267 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2274 Returns all the invoices (see L<FS::cust_bill>) for this customer.
2280 sort { $a->_date <=> $b->_date }
2281 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2284 =item open_cust_bill
2286 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
2291 sub open_cust_bill {
2293 grep { $_->owed > 0 } $self->cust_bill;
2302 =item check_and_rebuild_fuzzyfiles
2306 sub check_and_rebuild_fuzzyfiles {
2307 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2308 -e "$dir/cust_main.last" && -e "$dir/cust_main.company"
2309 or &rebuild_fuzzyfiles;
2312 =item rebuild_fuzzyfiles
2316 sub rebuild_fuzzyfiles {
2318 use Fcntl qw(:flock);
2320 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2324 open(LASTLOCK,">>$dir/cust_main.last")
2325 or die "can't open $dir/cust_main.last: $!";
2326 flock(LASTLOCK,LOCK_EX)
2327 or die "can't lock $dir/cust_main.last: $!";
2329 my @all_last = map $_->getfield('last'), qsearch('cust_main', {});
2331 grep $_, map $_->getfield('ship_last'), qsearch('cust_main',{})
2332 if defined dbdef->table('cust_main')->column('ship_last');
2334 open (LASTCACHE,">$dir/cust_main.last.tmp")
2335 or die "can't open $dir/cust_main.last.tmp: $!";
2336 print LASTCACHE join("\n", @all_last), "\n";
2337 close LASTCACHE or die "can't close $dir/cust_main.last.tmp: $!";
2339 rename "$dir/cust_main.last.tmp", "$dir/cust_main.last";
2344 open(COMPANYLOCK,">>$dir/cust_main.company")
2345 or die "can't open $dir/cust_main.company: $!";
2346 flock(COMPANYLOCK,LOCK_EX)
2347 or die "can't lock $dir/cust_main.company: $!";
2349 my @all_company = grep $_ ne '', map $_->company, qsearch('cust_main',{});
2351 grep $_ ne '', map $_->ship_company, qsearch('cust_main', {})
2352 if defined dbdef->table('cust_main')->column('ship_last');
2354 open (COMPANYCACHE,">$dir/cust_main.company.tmp")
2355 or die "can't open $dir/cust_main.company.tmp: $!";
2356 print COMPANYCACHE join("\n", @all_company), "\n";
2357 close COMPANYCACHE or die "can't close $dir/cust_main.company.tmp: $!";
2359 rename "$dir/cust_main.company.tmp", "$dir/cust_main.company";
2369 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2370 open(LASTCACHE,"<$dir/cust_main.last")
2371 or die "can't open $dir/cust_main.last: $!";
2372 my @array = map { chomp; $_; } <LASTCACHE>;
2382 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2383 open(COMPANYCACHE,"<$dir/cust_main.company")
2384 or die "can't open $dir/cust_main.last: $!";
2385 my @array = map { chomp; $_; } <COMPANYCACHE>;
2390 =item append_fuzzyfiles LASTNAME COMPANY
2394 sub append_fuzzyfiles {
2395 my( $last, $company ) = @_;
2397 &check_and_rebuild_fuzzyfiles;
2399 use Fcntl qw(:flock);
2401 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2405 open(LAST,">>$dir/cust_main.last")
2406 or die "can't open $dir/cust_main.last: $!";
2408 or die "can't lock $dir/cust_main.last: $!";
2410 print LAST "$last\n";
2413 or die "can't unlock $dir/cust_main.last: $!";
2419 open(COMPANY,">>$dir/cust_main.company")
2420 or die "can't open $dir/cust_main.company: $!";
2421 flock(COMPANY,LOCK_EX)
2422 or die "can't lock $dir/cust_main.company: $!";
2424 print COMPANY "$company\n";
2426 flock(COMPANY,LOCK_UN)
2427 or die "can't unlock $dir/cust_main.company: $!";
2441 #warn join('-',keys %$param);
2442 my $fh = $param->{filehandle};
2443 my $agentnum = $param->{agentnum};
2444 my $refnum = $param->{refnum};
2445 my $pkgpart = $param->{pkgpart};
2446 my @fields = @{$param->{fields}};
2448 eval "use Date::Parse;";
2450 eval "use Text::CSV_XS;";
2453 my $csv = new Text::CSV_XS;
2460 local $SIG{HUP} = 'IGNORE';
2461 local $SIG{INT} = 'IGNORE';
2462 local $SIG{QUIT} = 'IGNORE';
2463 local $SIG{TERM} = 'IGNORE';
2464 local $SIG{TSTP} = 'IGNORE';
2465 local $SIG{PIPE} = 'IGNORE';
2467 my $oldAutoCommit = $FS::UID::AutoCommit;
2468 local $FS::UID::AutoCommit = 0;
2471 #while ( $columns = $csv->getline($fh) ) {
2473 while ( defined($line=<$fh>) ) {
2475 $csv->parse($line) or do {
2476 $dbh->rollback if $oldAutoCommit;
2477 return "can't parse: ". $csv->error_input();
2480 my @columns = $csv->fields();
2481 #warn join('-',@columns);
2484 agentnum => $agentnum,
2486 country => 'US', #default
2487 payby => 'BILL', #default
2488 paydate => '12/2037', #default
2490 my $billtime = time;
2491 my %cust_pkg = ( pkgpart => $pkgpart );
2492 foreach my $field ( @fields ) {
2493 if ( $field =~ /^cust_pkg\.(setup|bill|susp|expire|cancel)$/ ) {
2494 #$cust_pkg{$1} = str2time( shift @$columns );
2495 if ( $1 eq 'setup' ) {
2496 $billtime = str2time(shift @columns);
2498 $cust_pkg{$1} = str2time( shift @columns );
2501 #$cust_main{$field} = shift @$columns;
2502 $cust_main{$field} = shift @columns;
2506 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg ) if $pkgpart;
2507 my $cust_main = new FS::cust_main ( \%cust_main );
2509 tie my %hash, 'Tie::RefHash'; #this part is important
2510 $hash{$cust_pkg} = [] if $pkgpart;
2511 my $error = $cust_main->insert( \%hash );
2514 $dbh->rollback if $oldAutoCommit;
2515 return "can't insert customer for $line: $error";
2518 #false laziness w/bill.cgi
2519 $error = $cust_main->bill( 'time' => $billtime );
2521 $dbh->rollback if $oldAutoCommit;
2522 return "can't bill customer for $line: $error";
2525 $cust_main->apply_payments;
2526 $cust_main->apply_credits;
2528 $error = $cust_main->collect();
2530 $dbh->rollback if $oldAutoCommit;
2531 return "can't collect customer for $line: $error";
2537 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2539 return "Empty file!" unless $imported;
2551 #warn join('-',keys %$param);
2552 my $fh = $param->{filehandle};
2553 my @fields = @{$param->{fields}};
2555 eval "use Date::Parse;";
2557 eval "use Text::CSV_XS;";
2560 my $csv = new Text::CSV_XS;
2567 local $SIG{HUP} = 'IGNORE';
2568 local $SIG{INT} = 'IGNORE';
2569 local $SIG{QUIT} = 'IGNORE';
2570 local $SIG{TERM} = 'IGNORE';
2571 local $SIG{TSTP} = 'IGNORE';
2572 local $SIG{PIPE} = 'IGNORE';
2574 my $oldAutoCommit = $FS::UID::AutoCommit;
2575 local $FS::UID::AutoCommit = 0;
2578 #while ( $columns = $csv->getline($fh) ) {
2580 while ( defined($line=<$fh>) ) {
2582 $csv->parse($line) or do {
2583 $dbh->rollback if $oldAutoCommit;
2584 return "can't parse: ". $csv->error_input();
2587 my @columns = $csv->fields();
2588 #warn join('-',@columns);
2591 foreach my $field ( @fields ) {
2592 $row{$field} = shift @columns;
2595 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
2596 unless ( $cust_main ) {
2597 $dbh->rollback if $oldAutoCommit;
2598 return "unknown custnum $row{'custnum'}";
2601 if ( $row{'amount'} > 0 ) {
2602 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
2604 $dbh->rollback if $oldAutoCommit;
2608 } elsif ( $row{'amount'} < 0 ) {
2609 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
2612 $dbh->rollback if $oldAutoCommit;
2622 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2624 return "Empty file!" unless $imported;
2636 The delete method should possibly take an FS::cust_main object reference
2637 instead of a scalar customer number.
2639 Bill and collect options should probably be passed as references instead of a
2642 There should probably be a configuration file with a list of allowed credit
2645 No multiple currency support (probably a larger project than just this module).
2649 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
2650 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
2651 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.