4 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
5 $import $skip_fuzzyfiles $ignore_expired_card );
6 use vars qw( $realtime_bop_decline_quiet ); #ugh
11 eval "use Time::Local;";
12 die "Time::Local minimum version 1.05 required with Perl versions before 5.6"
13 if $] < 5.006 && !defined($Time::Local::VERSION);
14 #eval "use Time::Local qw(timelocal timelocal_nocheck);";
15 eval "use Time::Local qw(timelocal_nocheck);";
17 use Digest::MD5 qw(md5_base64);
21 use String::Approx qw(amatch);
22 use Business::CreditCard 0.28;
24 use FS::UID qw( getotaker dbh );
25 use FS::Record qw( qsearchs qsearch dbdef );
26 use FS::Misc qw( send_email );
27 use FS::Msgcat qw(gettext);
31 use FS::cust_bill_pkg;
33 use FS::cust_pay_void;
36 use FS::part_referral;
37 use FS::cust_main_county;
39 use FS::cust_main_invoice;
40 use FS::cust_credit_bill;
41 use FS::cust_bill_pay;
42 use FS::prepay_credit;
45 use FS::part_bill_event;
46 use FS::cust_bill_event;
47 use FS::cust_tax_exempt;
48 use FS::cust_tax_exempt_pkg;
50 use FS::payment_gateway;
51 use FS::agent_payment_gateway;
54 @ISA = qw( FS::Record );
56 @EXPORT_OK = qw( smart_search );
58 $realtime_bop_decline_quiet = 0;
60 # 1 is mostly method/subroutine entry and options
61 # 2 traces progress of some operations
62 # 3 is even more information including possibly sensitive data
64 $me = '[FS::cust_main]';
68 $ignore_expired_card = 0;
70 @encrypted_fields = ('payinfo', 'paycvv');
72 #ask FS::UID to run this stuff for us later
73 #$FS::UID::callback{'FS::cust_main'} = sub {
74 install_callback FS::UID sub {
76 #yes, need it for stuff below (prolly should be cached)
81 my ( $hashref, $cache ) = @_;
82 if ( exists $hashref->{'pkgnum'} ) {
83 #@{ $self->{'_pkgnum'} } = ();
84 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
85 $self->{'_pkgnum'} = $subcache;
86 #push @{ $self->{'_pkgnum'} },
87 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
93 FS::cust_main - Object methods for cust_main records
99 $record = new FS::cust_main \%hash;
100 $record = new FS::cust_main { 'column' => 'value' };
102 $error = $record->insert;
104 $error = $new_record->replace($old_record);
106 $error = $record->delete;
108 $error = $record->check;
110 @cust_pkg = $record->all_pkgs;
112 @cust_pkg = $record->ncancelled_pkgs;
114 @cust_pkg = $record->suspended_pkgs;
116 $error = $record->bill;
117 $error = $record->bill %options;
118 $error = $record->bill 'time' => $time;
120 $error = $record->collect;
121 $error = $record->collect %options;
122 $error = $record->collect 'invoice_time' => $time,
127 An FS::cust_main object represents a customer. FS::cust_main inherits from
128 FS::Record. The following fields are currently supported:
132 =item custnum - primary key (assigned automatically for new customers)
134 =item agentnum - agent (see L<FS::agent>)
136 =item refnum - Advertising source (see L<FS::part_referral>)
142 =item ss - social security number (optional)
144 =item company - (optional)
148 =item address2 - (optional)
152 =item county - (optional, see L<FS::cust_main_county>)
154 =item state - (see L<FS::cust_main_county>)
158 =item country - (see L<FS::cust_main_county>)
160 =item daytime - phone (optional)
162 =item night - phone (optional)
164 =item fax - phone (optional)
166 =item ship_first - name
168 =item ship_last - name
170 =item ship_company - (optional)
174 =item ship_address2 - (optional)
178 =item ship_county - (optional, see L<FS::cust_main_county>)
180 =item ship_state - (see L<FS::cust_main_county>)
184 =item ship_country - (see L<FS::cust_main_county>)
186 =item ship_daytime - phone (optional)
188 =item ship_night - phone (optional)
190 =item ship_fax - phone (optional)
194 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>)
198 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
203 my($self,$payinfo) = @_;
204 if ( defined($payinfo) ) {
205 $self->paymask($payinfo);
206 $self->setfield('payinfo', $payinfo); # This is okay since we are the 'setter'
208 $payinfo = $self->getfield('payinfo'); # This is okay since we are the 'getter'
216 Card Verification Value, "CVV2" (also known as CVC2 or CID), the 3 or 4 digit number on the back (or front, for American Express) of the credit card
220 =item paymask - Masked payment type
226 Mask all but the last four characters.
230 Mask all but last 2 of account number and bank routing number.
234 Do nothing, return the unmasked string.
243 # If it doesn't exist then generate it
244 my $paymask=$self->getfield('paymask');
245 if (!defined($value) && (!defined($paymask) || $paymask eq '')) {
246 $value = $self->payinfo;
249 if ( defined($value) && !$self->is_encrypted($value)) {
250 my $payinfo = $value;
251 my $payby = $self->payby;
252 if ($payby eq 'CARD' || $payby eq 'DCRD') { # Credit Cards (Show last four)
253 $paymask = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
254 } elsif ($payby eq 'CHEK' ||
255 $payby eq 'DCHK' ) { # Checks (Show last 2 @ bank)
256 my( $account, $aba ) = split('@', $payinfo );
257 $paymask = 'x'x(length($account)-2). substr($account,(length($account)-2))."@".$aba;
258 } else { # Tie up loose ends
261 $self->setfield('paymask', $paymask); # This is okay since we are the 'setter'
262 } elsif (defined($value) && $self->is_encrypted($value)) {
268 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
270 =item paystart_month - start date month (maestro/solo cards only)
272 =item paystart_year - start date year (maestro/solo cards only)
274 =item payissue - issue number (maestro/solo cards only)
276 =item payname - name on card or billing name
278 =item payip - IP address from which payment information was received
280 =item tax - tax exempt, empty or `Y'
282 =item otaker - order taker (assigned automatically, see L<FS::UID>)
284 =item comments - comments (optional)
286 =item referral_custnum - referring customer number
288 =item spool_cdr - Enable individual CDR spooling, empty or `Y'
298 Creates a new customer. To add the customer to the database, see L<"insert">.
300 Note that this stores the hash reference, not a distinct copy of the hash it
301 points to. You can ask the object for a copy with the I<hash> method.
305 sub table { 'cust_main'; }
307 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
309 Adds this customer to the database. If there is an error, returns the error,
310 otherwise returns false.
312 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
313 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
314 are inserted atomicly, or the transaction is rolled back. Passing an empty
315 hash reference is equivalent to not supplying this parameter. There should be
316 a better explanation of this, but until then, here's an example:
319 tie %hash, 'Tie::RefHash'; #this part is important
321 $cust_pkg => [ $svc_acct ],
324 $cust_main->insert( \%hash );
326 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
327 be set as the invoicing list (see L<"invoicing_list">). Errors return as
328 expected and rollback the entire transaction; it is not necessary to call
329 check_invoicing_list first. The invoicing_list is set after the records in the
330 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
331 invoicing_list destination to the newly-created svc_acct. Here's an example:
333 $cust_main->insert( {}, [ $email, 'POST' ] );
335 Currently available options are: I<depend_jobnum> and I<noexport>.
337 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
338 on the supplied jobnum (they will not run until the specific job completes).
339 This can be used to defer provisioning until some action completes (such
340 as running the customer's credit card successfully).
342 The I<noexport> option is deprecated. If I<noexport> is set true, no
343 provisioning jobs (exports) are scheduled. (You can schedule them later with
344 the B<reexport> method.)
350 my $cust_pkgs = @_ ? shift : {};
351 my $invoicing_list = @_ ? shift : '';
353 warn "$me insert called with options ".
354 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
357 local $SIG{HUP} = 'IGNORE';
358 local $SIG{INT} = 'IGNORE';
359 local $SIG{QUIT} = 'IGNORE';
360 local $SIG{TERM} = 'IGNORE';
361 local $SIG{TSTP} = 'IGNORE';
362 local $SIG{PIPE} = 'IGNORE';
364 my $oldAutoCommit = $FS::UID::AutoCommit;
365 local $FS::UID::AutoCommit = 0;
368 my $prepay_identifier = '';
369 my( $amount, $seconds ) = ( 0, 0 );
371 if ( $self->payby eq 'PREPAY' ) {
373 $self->payby('BILL');
374 $prepay_identifier = $self->payinfo;
377 warn " looking up prepaid card $prepay_identifier\n"
380 my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
382 $dbh->rollback if $oldAutoCommit;
383 #return "error applying prepaid card (transaction rolled back): $error";
387 $payby = 'PREP' if $amount;
389 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
392 $self->payby('BILL');
393 $amount = $self->paid;
397 warn " inserting $self\n"
400 my $error = $self->SUPER::insert;
402 $dbh->rollback if $oldAutoCommit;
403 #return "inserting cust_main record (transaction rolled back): $error";
407 warn " setting invoicing list\n"
410 if ( $invoicing_list ) {
411 $error = $self->check_invoicing_list( $invoicing_list );
413 $dbh->rollback if $oldAutoCommit;
414 return "checking invoicing_list (transaction rolled back): $error";
416 $self->invoicing_list( $invoicing_list );
419 if ( $conf->config('cust_main-skeleton_tables')
420 && $conf->config('cust_main-skeleton_custnum') ) {
422 warn " inserting skeleton records\n"
425 my $error = $self->start_copy_skel;
427 $dbh->rollback if $oldAutoCommit;
433 warn " ordering packages\n"
436 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
438 $dbh->rollback if $oldAutoCommit;
443 $dbh->rollback if $oldAutoCommit;
444 return "No svc_acct record to apply pre-paid time";
448 warn " inserting initial $payby payment of $amount\n"
450 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
452 $dbh->rollback if $oldAutoCommit;
453 return "inserting payment (transaction rolled back): $error";
457 unless ( $import || $skip_fuzzyfiles ) {
458 warn " queueing fuzzyfiles update\n"
460 $error = $self->queue_fuzzyfiles_update;
462 $dbh->rollback if $oldAutoCommit;
463 return "updating fuzzy search cache: $error";
467 warn " insert complete; committing transaction\n"
470 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
475 sub start_copy_skel {
478 #'mg_user_preference' => {},
479 #'mg_user_indicator_profile' => { 'mg_profile_indicator' => { 'mg_profile_details' }, },
480 #'mg_watchlist_header' => { 'mg_watchlist_details' },
481 #'mg_user_grid_header' => { 'mg_user_grid_details' },
482 #'mg_portfolio_header' => { 'mg_portfolio_trades' => { 'mg_portfolio_trades_positions' } },
483 my @tables = eval($conf->config('cust_main-skeleton_tables'));
486 _copy_skel( 'cust_main', #tablename
487 $conf->config('cust_main-skeleton_custnum'), #sourceid
488 $self->custnum, #destid
489 @tables, #child tables
493 #recursive subroutine, not a method
495 my( $table, $sourceid, $destid, %child_tables ) = @_;
497 my $dbdef_table = dbdef->table($table);
498 my $primary_key = $dbdef_table->primary_key
499 or return "$table has no primary key".
500 " (or do you need to run dbdef-create?)";
502 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
503 join (', ', keys %child_tables). "\n"
506 foreach my $child_table ( keys %child_tables ) {
508 my $child_pkey = dbdef->table($child_table)->primary_key;
509 # or return "$table has no primary key".
510 # " (or do you need to run dbdef-create?)\n";
512 if ( keys %{ $child_tables{$child_table} } ) {
514 return "$child_table has no primary key\n" unless $child_pkey;
516 #false laziness w/Record::insert and only works on Pg
517 #refactor the proper last-inserted-id stuff out of Record::insert if this
518 # ever gets use for anything besides a quick kludge for one customer
519 my $default = dbdef->table($child_table)->column($child_pkey)->default;
520 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
521 or return "can't parse $child_table.$child_pkey default value ".
522 " for sequence name: $default";
527 my @sel_columns = grep { $_ ne $primary_key }
528 dbdef->table($child_table)->columns;
529 my $sel_columns = ' ( '. join(', ', @sel_columns ). ' ) ';
531 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
532 my $ins_columns = ' ( ', join(', ', $primary_key, @ins_columns ). ' ) ',
533 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
535 my $sel_sth = dbh->prepare( "SELECT $sel_columns FROM $child_table".
536 " WHERE $primary_key = $sourceid")
537 or return dbh->errstr;
539 $sel_sth->execute or return $sel_sth->errstr;
541 while ( my $row = $sel_sth->fetchrow_hashref ) {
544 dbh->prepare("INSERT INTO $child_table $ins_columns".
545 " VALUES $placeholders")
546 or return dbh->errstr;
547 $ins_sth->execute( $destid, map $row->{$_}, @ins_columns )
548 or return $ins_sth->errstr;
550 #next unless keys %{ $child_tables{$child_table} };
551 next unless $sequence;
553 #another section of that laziness
554 my $seq_sql = "SELECT currval('$sequence')";
555 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
556 $seq_sth->execute or return $seq_sth->errstr;
557 my $insertid = $seq_sth->fetchrow_arrayref->[0];
559 # don't drink soap! recurse! recurse! okay!
561 _copy_skel( $child_table,
562 $row->{$child_pkey}, #sourceid
564 %{ $child_tables{$child_table} },
566 return $error if $error;
576 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
578 Like the insert method on an existing record, this method orders a package
579 and included services atomicaly. Pass a Tie::RefHash data structure to this
580 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
581 be a better explanation of this, but until then, here's an example:
584 tie %hash, 'Tie::RefHash'; #this part is important
586 $cust_pkg => [ $svc_acct ],
589 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
591 Services can be new, in which case they are inserted, or existing unaudited
592 services, in which case they are linked to the newly-created package.
594 Currently available options are: I<depend_jobnum> and I<noexport>.
596 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
597 on the supplied jobnum (they will not run until the specific job completes).
598 This can be used to defer provisioning until some action completes (such
599 as running the customer's credit card successfully).
601 The I<noexport> option is deprecated. If I<noexport> is set true, no
602 provisioning jobs (exports) are scheduled. (You can schedule them later with
603 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
604 on the cust_main object is not recommended, as existing services will also be
611 my $cust_pkgs = shift;
614 my %svc_options = ();
615 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
616 if exists $options{'depend_jobnum'};
617 warn "$me order_pkgs called with options ".
618 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
621 local $SIG{HUP} = 'IGNORE';
622 local $SIG{INT} = 'IGNORE';
623 local $SIG{QUIT} = 'IGNORE';
624 local $SIG{TERM} = 'IGNORE';
625 local $SIG{TSTP} = 'IGNORE';
626 local $SIG{PIPE} = 'IGNORE';
628 my $oldAutoCommit = $FS::UID::AutoCommit;
629 local $FS::UID::AutoCommit = 0;
632 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
634 foreach my $cust_pkg ( keys %$cust_pkgs ) {
635 $cust_pkg->custnum( $self->custnum );
636 my $error = $cust_pkg->insert;
638 $dbh->rollback if $oldAutoCommit;
639 return "inserting cust_pkg (transaction rolled back): $error";
641 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
642 if ( $svc_something->svcnum ) {
643 my $old_cust_svc = $svc_something->cust_svc;
644 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
645 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
646 $error = $new_cust_svc->replace($old_cust_svc);
648 $svc_something->pkgnum( $cust_pkg->pkgnum );
649 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
650 $svc_something->seconds( $svc_something->seconds + $$seconds );
653 $error = $svc_something->insert(%svc_options);
656 $dbh->rollback if $oldAutoCommit;
657 #return "inserting svc_ (transaction rolled back): $error";
663 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
667 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF ]
669 Recharges this (existing) customer with the specified prepaid card (see
670 L<FS::prepay_credit>), specified either by I<identifier> or as an
671 FS::prepay_credit object. If there is an error, returns the error, otherwise
674 Optionally, two scalar references can be passed as well. They will have their
675 values filled in with the amount and number of seconds applied by this prepaid
680 sub recharge_prepay {
681 my( $self, $prepay_credit, $amountref, $secondsref ) = @_;
683 local $SIG{HUP} = 'IGNORE';
684 local $SIG{INT} = 'IGNORE';
685 local $SIG{QUIT} = 'IGNORE';
686 local $SIG{TERM} = 'IGNORE';
687 local $SIG{TSTP} = 'IGNORE';
688 local $SIG{PIPE} = 'IGNORE';
690 my $oldAutoCommit = $FS::UID::AutoCommit;
691 local $FS::UID::AutoCommit = 0;
694 my( $amount, $seconds ) = ( 0, 0 );
696 my $error = $self->get_prepay($prepay_credit, \$amount, \$seconds)
697 || $self->increment_seconds($seconds)
698 || $self->insert_cust_pay_prepay( $amount,
700 ? $prepay_credit->identifier
705 $dbh->rollback if $oldAutoCommit;
709 if ( defined($amountref) ) { $$amountref = $amount; }
710 if ( defined($secondsref) ) { $$secondsref = $seconds; }
712 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
717 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
719 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
720 specified either by I<identifier> or as an FS::prepay_credit object.
722 References to I<amount> and I<seconds> scalars should be passed as arguments
723 and will be incremented by the values of the prepaid card.
725 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
726 check or set this customer's I<agentnum>.
728 If there is an error, returns the error, otherwise returns false.
734 my( $self, $prepay_credit, $amountref, $secondsref ) = @_;
736 local $SIG{HUP} = 'IGNORE';
737 local $SIG{INT} = 'IGNORE';
738 local $SIG{QUIT} = 'IGNORE';
739 local $SIG{TERM} = 'IGNORE';
740 local $SIG{TSTP} = 'IGNORE';
741 local $SIG{PIPE} = 'IGNORE';
743 my $oldAutoCommit = $FS::UID::AutoCommit;
744 local $FS::UID::AutoCommit = 0;
747 unless ( ref($prepay_credit) ) {
749 my $identifier = $prepay_credit;
751 $prepay_credit = qsearchs(
753 { 'identifier' => $prepay_credit },
758 unless ( $prepay_credit ) {
759 $dbh->rollback if $oldAutoCommit;
760 return "Invalid prepaid card: ". $identifier;
765 if ( $prepay_credit->agentnum ) {
766 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
767 $dbh->rollback if $oldAutoCommit;
768 return "prepaid card not valid for agent ". $self->agentnum;
770 $self->agentnum($prepay_credit->agentnum);
773 my $error = $prepay_credit->delete;
775 $dbh->rollback if $oldAutoCommit;
776 return "removing prepay_credit (transaction rolled back): $error";
779 $$amountref += $prepay_credit->amount;
780 $$secondsref += $prepay_credit->seconds;
782 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
787 =item increment_seconds SECONDS
789 Updates this customer's single or primary account (see L<FS::svc_acct>) by
790 the specified number of seconds. If there is an error, returns the error,
791 otherwise returns false.
795 sub increment_seconds {
796 my( $self, $seconds ) = @_;
797 warn "$me increment_seconds called: $seconds seconds\n"
800 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
801 $self->ncancelled_pkgs;
804 return 'No packages with primary or single services found'.
805 ' to apply pre-paid time';
806 } elsif ( scalar(@cust_pkg) > 1 ) {
807 #maybe have a way to specify the package/account?
808 return 'Multiple packages found to apply pre-paid time';
811 my $cust_pkg = $cust_pkg[0];
812 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
816 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
819 return 'No account found to apply pre-paid time';
820 } elsif ( scalar(@cust_svc) > 1 ) {
821 return 'Multiple accounts found to apply pre-paid time';
824 my $svc_acct = $cust_svc[0]->svc_x;
825 warn " found service svcnum ". $svc_acct->pkgnum.
826 ' ('. $svc_acct->email. ")\n"
829 $svc_acct->increment_seconds($seconds);
833 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
835 Inserts a prepayment in the specified amount for this customer. An optional
836 second argument can specify the prepayment identifier for tracking purposes.
837 If there is an error, returns the error, otherwise returns false.
841 sub insert_cust_pay_prepay {
842 shift->insert_cust_pay('PREP', @_);
845 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
847 Inserts a cash payment in the specified amount for this customer. An optional
848 second argument can specify the payment identifier for tracking purposes.
849 If there is an error, returns the error, otherwise returns false.
853 sub insert_cust_pay_cash {
854 shift->insert_cust_pay('CASH', @_);
857 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
859 Inserts a Western Union payment in the specified amount for this customer. An
860 optional second argument can specify the prepayment identifier for tracking
861 purposes. If there is an error, returns the error, otherwise returns false.
865 sub insert_cust_pay_west {
866 shift->insert_cust_pay('WEST', @_);
869 sub insert_cust_pay {
870 my( $self, $payby, $amount ) = splice(@_, 0, 3);
871 my $payinfo = scalar(@_) ? shift : '';
873 my $cust_pay = new FS::cust_pay {
874 'custnum' => $self->custnum,
875 'paid' => sprintf('%.2f', $amount),
876 #'_date' => #date the prepaid card was purchased???
878 'payinfo' => $payinfo,
886 This method is deprecated. See the I<depend_jobnum> option to the insert and
887 order_pkgs methods for a better way to defer provisioning.
889 Re-schedules all exports by calling the B<reexport> method of all associated
890 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
891 otherwise returns false.
898 carp "WARNING: FS::cust_main::reexport is deprectated; ".
899 "use the depend_jobnum option to insert or order_pkgs to delay export";
901 local $SIG{HUP} = 'IGNORE';
902 local $SIG{INT} = 'IGNORE';
903 local $SIG{QUIT} = 'IGNORE';
904 local $SIG{TERM} = 'IGNORE';
905 local $SIG{TSTP} = 'IGNORE';
906 local $SIG{PIPE} = 'IGNORE';
908 my $oldAutoCommit = $FS::UID::AutoCommit;
909 local $FS::UID::AutoCommit = 0;
912 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
913 my $error = $cust_pkg->reexport;
915 $dbh->rollback if $oldAutoCommit;
920 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
925 =item delete NEW_CUSTNUM
927 This deletes the customer. If there is an error, returns the error, otherwise
930 This will completely remove all traces of the customer record. This is not
931 what you want when a customer cancels service; for that, cancel all of the
932 customer's packages (see L</cancel>).
934 If the customer has any uncancelled packages, you need to pass a new (valid)
935 customer number for those packages to be transferred to. Cancelled packages
936 will be deleted. Did I mention that this is NOT what you want when a customer
937 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
939 You can't delete a customer with invoices (see L<FS::cust_bill>),
940 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
941 refunds (see L<FS::cust_refund>).
948 local $SIG{HUP} = 'IGNORE';
949 local $SIG{INT} = 'IGNORE';
950 local $SIG{QUIT} = 'IGNORE';
951 local $SIG{TERM} = 'IGNORE';
952 local $SIG{TSTP} = 'IGNORE';
953 local $SIG{PIPE} = 'IGNORE';
955 my $oldAutoCommit = $FS::UID::AutoCommit;
956 local $FS::UID::AutoCommit = 0;
959 if ( $self->cust_bill ) {
960 $dbh->rollback if $oldAutoCommit;
961 return "Can't delete a customer with invoices";
963 if ( $self->cust_credit ) {
964 $dbh->rollback if $oldAutoCommit;
965 return "Can't delete a customer with credits";
967 if ( $self->cust_pay ) {
968 $dbh->rollback if $oldAutoCommit;
969 return "Can't delete a customer with payments";
971 if ( $self->cust_refund ) {
972 $dbh->rollback if $oldAutoCommit;
973 return "Can't delete a customer with refunds";
976 my @cust_pkg = $self->ncancelled_pkgs;
978 my $new_custnum = shift;
979 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
980 $dbh->rollback if $oldAutoCommit;
981 return "Invalid new customer number: $new_custnum";
983 foreach my $cust_pkg ( @cust_pkg ) {
984 my %hash = $cust_pkg->hash;
985 $hash{'custnum'} = $new_custnum;
986 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
987 my $error = $new_cust_pkg->replace($cust_pkg);
989 $dbh->rollback if $oldAutoCommit;
994 my @cancelled_cust_pkg = $self->all_pkgs;
995 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
996 my $error = $cust_pkg->delete;
998 $dbh->rollback if $oldAutoCommit;
1003 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1004 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1006 my $error = $cust_main_invoice->delete;
1008 $dbh->rollback if $oldAutoCommit;
1013 my $error = $self->SUPER::delete;
1015 $dbh->rollback if $oldAutoCommit;
1019 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1024 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
1026 Replaces the OLD_RECORD with this one in the database. If there is an error,
1027 returns the error, otherwise returns false.
1029 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1030 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1031 expected and rollback the entire transaction; it is not necessary to call
1032 check_invoicing_list first. Here's an example:
1034 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1042 warn "$me replace called\n"
1045 local $SIG{HUP} = 'IGNORE';
1046 local $SIG{INT} = 'IGNORE';
1047 local $SIG{QUIT} = 'IGNORE';
1048 local $SIG{TERM} = 'IGNORE';
1049 local $SIG{TSTP} = 'IGNORE';
1050 local $SIG{PIPE} = 'IGNORE';
1052 # If the mask is blank then try to set it - if we can...
1053 if (!defined($self->getfield('paymask')) || $self->getfield('paymask') eq '') {
1054 $self->paymask($self->payinfo);
1057 # We absolutely have to have an old vs. new record to make this work.
1058 if (!defined($old)) {
1059 $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
1062 my $curuser = $FS::CurrentUser::CurrentUser;
1063 if ( $self->payby eq 'COMP'
1064 && $self->payby ne $old->payby
1065 && ! $curuser->access_right('Complimentary customer')
1068 return "You are not permitted to create complimentary accounts.";
1071 local($ignore_expired_card) = 1
1072 if $old->payby =~ /^(CARD|DCRD)$/
1073 && $self->payby =~ /^(CARD|DCRD)$/
1074 && $old->payinfo eq $self->payinfo;
1076 my $oldAutoCommit = $FS::UID::AutoCommit;
1077 local $FS::UID::AutoCommit = 0;
1080 my $error = $self->SUPER::replace($old);
1083 $dbh->rollback if $oldAutoCommit;
1087 if ( @param ) { # INVOICING_LIST_ARYREF
1088 my $invoicing_list = shift @param;
1089 $error = $self->check_invoicing_list( $invoicing_list );
1091 $dbh->rollback if $oldAutoCommit;
1094 $self->invoicing_list( $invoicing_list );
1097 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1098 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1099 # card/check/lec info has changed, want to retry realtime_ invoice events
1100 my $error = $self->retry_realtime;
1102 $dbh->rollback if $oldAutoCommit;
1107 unless ( $import || $skip_fuzzyfiles ) {
1108 $error = $self->queue_fuzzyfiles_update;
1110 $dbh->rollback if $oldAutoCommit;
1111 return "updating fuzzy search cache: $error";
1115 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1120 =item queue_fuzzyfiles_update
1122 Used by insert & replace to update the fuzzy search cache
1126 sub queue_fuzzyfiles_update {
1129 local $SIG{HUP} = 'IGNORE';
1130 local $SIG{INT} = 'IGNORE';
1131 local $SIG{QUIT} = 'IGNORE';
1132 local $SIG{TERM} = 'IGNORE';
1133 local $SIG{TSTP} = 'IGNORE';
1134 local $SIG{PIPE} = 'IGNORE';
1136 my $oldAutoCommit = $FS::UID::AutoCommit;
1137 local $FS::UID::AutoCommit = 0;
1140 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1141 my $error = $queue->insert( map $self->getfield($_),
1142 qw(first last company)
1145 $dbh->rollback if $oldAutoCommit;
1146 return "queueing job (transaction rolled back): $error";
1149 if ( $self->ship_last ) {
1150 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1151 $error = $queue->insert( map $self->getfield("ship_$_"),
1152 qw(first last company)
1155 $dbh->rollback if $oldAutoCommit;
1156 return "queueing job (transaction rolled back): $error";
1160 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1167 Checks all fields to make sure this is a valid customer record. If there is
1168 an error, returns the error, otherwise returns false. Called by the insert
1169 and replace methods.
1176 warn "$me check BEFORE: \n". $self->_dump
1180 $self->ut_numbern('custnum')
1181 || $self->ut_number('agentnum')
1182 || $self->ut_textn('agent_custid')
1183 || $self->ut_number('refnum')
1184 || $self->ut_name('last')
1185 || $self->ut_name('first')
1186 || $self->ut_textn('company')
1187 || $self->ut_text('address1')
1188 || $self->ut_textn('address2')
1189 || $self->ut_text('city')
1190 || $self->ut_textn('county')
1191 || $self->ut_textn('state')
1192 || $self->ut_country('country')
1193 || $self->ut_anything('comments')
1194 || $self->ut_numbern('referral_custnum')
1196 #barf. need message catalogs. i18n. etc.
1197 $error .= "Please select an advertising source."
1198 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1199 return $error if $error;
1201 return "Unknown agent"
1202 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1204 return "Unknown refnum"
1205 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1207 return "Unknown referring custnum: ". $self->referral_custnum
1208 unless ! $self->referral_custnum
1209 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1211 if ( $self->ss eq '' ) {
1216 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1217 or return "Illegal social security number: ". $self->ss;
1218 $self->ss("$1-$2-$3");
1222 # bad idea to disable, causes billing to fail because of no tax rates later
1223 # unless ( $import ) {
1224 unless ( qsearch('cust_main_county', {
1225 'country' => $self->country,
1228 return "Unknown state/county/country: ".
1229 $self->state. "/". $self->county. "/". $self->country
1230 unless qsearch('cust_main_county',{
1231 'state' => $self->state,
1232 'county' => $self->county,
1233 'country' => $self->country,
1239 $self->ut_phonen('daytime', $self->country)
1240 || $self->ut_phonen('night', $self->country)
1241 || $self->ut_phonen('fax', $self->country)
1242 || $self->ut_zip('zip', $self->country)
1244 return $error if $error;
1247 last first company address1 address2 city county state zip
1248 country daytime night fax
1251 if ( defined $self->dbdef_table->column('ship_last') ) {
1252 if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1254 && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
1258 $self->ut_name('ship_last')
1259 || $self->ut_name('ship_first')
1260 || $self->ut_textn('ship_company')
1261 || $self->ut_text('ship_address1')
1262 || $self->ut_textn('ship_address2')
1263 || $self->ut_text('ship_city')
1264 || $self->ut_textn('ship_county')
1265 || $self->ut_textn('ship_state')
1266 || $self->ut_country('ship_country')
1268 return $error if $error;
1270 #false laziness with above
1271 unless ( qsearchs('cust_main_county', {
1272 'country' => $self->ship_country,
1275 return "Unknown ship_state/ship_county/ship_country: ".
1276 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1277 unless qsearch('cust_main_county',{
1278 'state' => $self->ship_state,
1279 'county' => $self->ship_county,
1280 'country' => $self->ship_country,
1286 $self->ut_phonen('ship_daytime', $self->ship_country)
1287 || $self->ut_phonen('ship_night', $self->ship_country)
1288 || $self->ut_phonen('ship_fax', $self->ship_country)
1289 || $self->ut_zip('ship_zip', $self->ship_country)
1291 return $error if $error;
1293 } else { # ship_ info eq billing info, so don't store dup info in database
1294 $self->setfield("ship_$_", '')
1295 foreach qw( last first company address1 address2 city county state zip
1296 country daytime night fax );
1300 $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1301 or return "Illegal payby: ". $self->payby;
1303 $error = $self->ut_numbern('paystart_month')
1304 || $self->ut_numbern('paystart_year')
1305 || $self->ut_numbern('payissue')
1307 return $error if $error;
1309 if ( $self->payip eq '' ) {
1312 $error = $self->ut_ip('payip');
1313 return $error if $error;
1316 # If it is encrypted and the private key is not availaible then we can't
1317 # check the credit card.
1319 my $check_payinfo = 1;
1321 if ($self->is_encrypted($self->payinfo)) {
1327 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1329 my $payinfo = $self->payinfo;
1330 $payinfo =~ s/\D//g;
1331 $payinfo =~ /^(\d{13,16})$/
1332 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1334 $self->payinfo($payinfo);
1336 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1338 return gettext('unknown_card_type')
1339 if cardtype($self->payinfo) eq "Unknown";
1341 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1343 return 'Banned credit card: banned on '.
1344 time2str('%a %h %o at %r', $ban->_date).
1345 ' by '. $ban->otaker.
1346 ' (ban# '. $ban->bannum. ')';
1349 if ( defined $self->dbdef_table->column('paycvv') ) {
1350 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1351 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1352 $self->paycvv =~ /^(\d{4})$/
1353 or return "CVV2 (CID) for American Express cards is four digits.";
1356 $self->paycvv =~ /^(\d{3})$/
1357 or return "CVV2 (CVC2/CID) is three digits.";
1365 my $cardtype = cardtype($payinfo);
1366 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1368 return "Start date or issue number is required for $cardtype cards"
1369 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1371 return "Start month must be between 1 and 12"
1372 if $self->paystart_month
1373 and $self->paystart_month < 1 || $self->paystart_month > 12;
1375 return "Start year must be 1990 or later"
1376 if $self->paystart_year
1377 and $self->paystart_year < 1990;
1379 return "Issue number must be beween 1 and 99"
1381 and $self->payissue < 1 || $self->payissue > 99;
1384 $self->paystart_month('');
1385 $self->paystart_year('');
1386 $self->payissue('');
1389 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1391 my $payinfo = $self->payinfo;
1392 $payinfo =~ s/[^\d\@]//g;
1393 if ( $conf->exists('echeck-nonus') ) {
1394 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1395 $payinfo = "$1\@$2";
1397 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1398 $payinfo = "$1\@$2";
1400 $self->payinfo($payinfo);
1401 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1403 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1405 return 'Banned ACH account: banned on '.
1406 time2str('%a %h %o at %r', $ban->_date).
1407 ' by '. $ban->otaker.
1408 ' (ban# '. $ban->bannum. ')';
1411 } elsif ( $self->payby eq 'LECB' ) {
1413 my $payinfo = $self->payinfo;
1414 $payinfo =~ s/\D//g;
1415 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1417 $self->payinfo($payinfo);
1418 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1420 } elsif ( $self->payby eq 'BILL' ) {
1422 $error = $self->ut_textn('payinfo');
1423 return "Illegal P.O. number: ". $self->payinfo if $error;
1424 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1426 } elsif ( $self->payby eq 'COMP' ) {
1428 my $curuser = $FS::CurrentUser::CurrentUser;
1429 if ( ! $self->custnum
1430 && ! $curuser->access_right('Complimentary customer')
1433 return "You are not permitted to create complimentary accounts."
1436 $error = $self->ut_textn('payinfo');
1437 return "Illegal comp account issuer: ". $self->payinfo if $error;
1438 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1440 } elsif ( $self->payby eq 'PREPAY' ) {
1442 my $payinfo = $self->payinfo;
1443 $payinfo =~ s/\W//g; #anything else would just confuse things
1444 $self->payinfo($payinfo);
1445 $error = $self->ut_alpha('payinfo');
1446 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1447 return "Unknown prepayment identifier"
1448 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1449 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1453 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1454 return "Expiration date required"
1455 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1459 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1460 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1461 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1462 ( $m, $y ) = ( $3, "20$2" );
1464 return "Illegal expiration date: ". $self->paydate;
1466 $self->paydate("$y-$m-01");
1467 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1468 return gettext('expired_card')
1470 && !$ignore_expired_card
1471 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1474 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1475 ( ! $conf->exists('require_cardname')
1476 || $self->payby !~ /^(CARD|DCRD)$/ )
1478 $self->payname( $self->first. " ". $self->getfield('last') );
1480 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1481 or return gettext('illegal_name'). " payname: ". $self->payname;
1485 foreach my $flag (qw( tax spool_cdr )) {
1486 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1490 $self->otaker(getotaker) unless $self->otaker;
1492 warn "$me check AFTER: \n". $self->_dump
1495 $self->SUPER::check;
1500 Returns all packages (see L<FS::cust_pkg>) for this customer.
1506 if ( $self->{'_pkgnum'} ) {
1507 values %{ $self->{'_pkgnum'}->cache };
1509 qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1513 =item ncancelled_pkgs
1515 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1519 sub ncancelled_pkgs {
1521 if ( $self->{'_pkgnum'} ) {
1522 grep { ! $_->getfield('cancel') } values %{ $self->{'_pkgnum'}->cache };
1524 @{ [ # force list context
1525 qsearch( 'cust_pkg', {
1526 'custnum' => $self->custnum,
1529 qsearch( 'cust_pkg', {
1530 'custnum' => $self->custnum,
1537 =item suspended_pkgs
1539 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1543 sub suspended_pkgs {
1545 grep { $_->susp } $self->ncancelled_pkgs;
1548 =item unflagged_suspended_pkgs
1550 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1551 customer (thouse packages without the `manual_flag' set).
1555 sub unflagged_suspended_pkgs {
1557 return $self->suspended_pkgs
1558 unless dbdef->table('cust_pkg')->column('manual_flag');
1559 grep { ! $_->manual_flag } $self->suspended_pkgs;
1562 =item unsuspended_pkgs
1564 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1569 sub unsuspended_pkgs {
1571 grep { ! $_->susp } $self->ncancelled_pkgs;
1574 =item num_cancelled_pkgs
1576 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1581 sub num_cancelled_pkgs {
1583 $self->num_pkgs("cancel IS NOT NULL AND cust_pkg.cancel != 0");
1587 my( $self, $sql ) = @_;
1588 my $sth = dbh->prepare(
1589 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? AND $sql"
1590 ) or die dbh->errstr;
1591 $sth->execute($self->custnum) or die $sth->errstr;
1592 $sth->fetchrow_arrayref->[0];
1597 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1598 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1599 on success or a list of errors.
1605 grep { $_->unsuspend } $self->suspended_pkgs;
1610 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1612 Returns a list: an empty list on success or a list of errors.
1618 grep { $_->suspend } $self->unsuspended_pkgs;
1621 =item suspend_if_pkgpart PKGPART [ , PKGPART ... ]
1623 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1624 PKGPARTs (see L<FS::part_pkg>).
1626 Returns a list: an empty list on success or a list of errors.
1630 sub suspend_if_pkgpart {
1633 grep { $_->suspend }
1634 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1635 $self->unsuspended_pkgs;
1638 =item suspend_unless_pkgpart PKGPART [ , PKGPART ... ]
1640 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1641 listed PKGPARTs (see L<FS::part_pkg>).
1643 Returns a list: an empty list on success or a list of errors.
1647 sub suspend_unless_pkgpart {
1650 grep { $_->suspend }
1651 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1652 $self->unsuspended_pkgs;
1655 =item cancel [ OPTION => VALUE ... ]
1657 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1659 Available options are: I<quiet>, I<reasonnum>, and I<ban>
1661 I<quiet> can be set true to supress email cancellation notices.
1663 # I<reasonnum> can be set to a cancellation reason (see L<FS::cancel_reason>)
1665 I<ban> can be set true to ban this customer's credit card or ACH information,
1668 Always returns a list: an empty list on success or a list of errors.
1676 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1678 #should try decryption (we might have the private key)
1679 # and if not maybe queue a job for the server that does?
1680 return ( "Can't (yet) ban encrypted credit cards" )
1681 if $self->is_encrypted($self->payinfo);
1683 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1684 my $error = $ban->insert;
1685 return ( $error ) if $error;
1689 grep { $_ } map { $_->cancel(@_) } $self->ncancelled_pkgs;
1692 sub _banned_pay_hashref {
1703 'payby' => $payby2ban{$self->payby},
1704 'payinfo' => md5_base64($self->payinfo),
1711 Returns the agent (see L<FS::agent>) for this customer.
1717 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1722 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
1723 conjunction with the collect method.
1725 Options are passed as name-value pairs.
1727 Currently available options are:
1729 resetup - if set true, re-charges setup fees.
1731 time - bills the customer as if it were that time. Specified as a UNIX
1732 timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and
1733 L<Date::Parse> for conversion functions. For example:
1737 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1740 If there is an error, returns the error, otherwise returns false.
1745 my( $self, %options ) = @_;
1746 return '' if $self->payby eq 'COMP';
1747 warn "$me bill customer ". $self->custnum. "\n"
1750 my $time = $options{'time'} || time;
1755 local $SIG{HUP} = 'IGNORE';
1756 local $SIG{INT} = 'IGNORE';
1757 local $SIG{QUIT} = 'IGNORE';
1758 local $SIG{TERM} = 'IGNORE';
1759 local $SIG{TSTP} = 'IGNORE';
1760 local $SIG{PIPE} = 'IGNORE';
1762 my $oldAutoCommit = $FS::UID::AutoCommit;
1763 local $FS::UID::AutoCommit = 0;
1766 $self->select_for_update; #mutex
1768 #create a new invoice
1769 #(we'll remove it later if it doesn't actually need to be generated [contains
1770 # no line items] and we're inside a transaciton so nothing else will see it)
1771 my $cust_bill = new FS::cust_bill ( {
1772 'custnum' => $self->custnum,
1774 #'charged' => $charged,
1777 $error = $cust_bill->insert;
1779 $dbh->rollback if $oldAutoCommit;
1780 return "can't create invoice for customer #". $self->custnum. ": $error";
1782 my $invnum = $cust_bill->invnum;
1785 # find the packages which are due for billing, find out how much they are
1786 # & generate invoice database.
1789 my( $total_setup, $total_recur ) = ( 0, 0 );
1791 my @precommit_hooks = ();
1793 foreach my $cust_pkg (
1794 qsearch('cust_pkg', { 'custnum' => $self->custnum } )
1797 #NO!! next if $cust_pkg->cancel;
1798 next if $cust_pkg->getfield('cancel');
1800 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
1802 #? to avoid use of uninitialized value errors... ?
1803 $cust_pkg->setfield('bill', '')
1804 unless defined($cust_pkg->bill);
1806 my $part_pkg = $cust_pkg->part_pkg;
1808 my %hash = $cust_pkg->hash;
1809 my $old_cust_pkg = new FS::cust_pkg \%hash;
1818 if ( !$cust_pkg->setup || $options{'resetup'} ) {
1820 warn " bill setup\n" if $DEBUG > 1;
1822 $setup = eval { $cust_pkg->calc_setup( $time ) };
1824 $dbh->rollback if $oldAutoCommit;
1825 return "$@ running calc_setup for $cust_pkg\n";
1828 $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
1832 # bill recurring fee
1837 if ( $part_pkg->getfield('freq') ne '0' &&
1838 ! $cust_pkg->getfield('susp') &&
1839 ( $cust_pkg->getfield('bill') || 0 ) <= $time
1842 warn " bill recur\n" if $DEBUG > 1;
1844 # XXX shared with $recur_prog
1845 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1847 #over two params! lets at least switch to a hashref for the rest...
1848 my %param = ( 'precommit_hooks' => \@precommit_hooks, );
1850 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
1852 $dbh->rollback if $oldAutoCommit;
1853 return "$@ running calc_recur for $cust_pkg\n";
1856 #change this bit to use Date::Manip? CAREFUL with timezones (see
1857 # mailing list archive)
1858 my ($sec,$min,$hour,$mday,$mon,$year) =
1859 (localtime($sdate) )[0,1,2,3,4,5];
1861 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
1862 # only for figuring next bill date, nothing else, so, reset $sdate again
1864 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1865 $cust_pkg->last_bill($sdate)
1866 if $cust_pkg->dbdef_table->column('last_bill');
1868 if ( $part_pkg->freq =~ /^\d+$/ ) {
1869 $mon += $part_pkg->freq;
1870 until ( $mon < 12 ) { $mon -= 12; $year++; }
1871 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
1873 $mday += $weeks * 7;
1874 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
1877 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
1881 $dbh->rollback if $oldAutoCommit;
1882 return "unparsable frequency: ". $part_pkg->freq;
1884 $cust_pkg->setfield('bill',
1885 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
1888 warn "\$setup is undefined" unless defined($setup);
1889 warn "\$recur is undefined" unless defined($recur);
1890 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1893 # If $cust_pkg has been modified, update it and create cust_bill_pkg records
1896 if ( $cust_pkg->modified ) {
1898 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
1901 $error=$cust_pkg->replace($old_cust_pkg);
1902 if ( $error ) { #just in case
1903 $dbh->rollback if $oldAutoCommit;
1904 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
1907 $setup = sprintf( "%.2f", $setup );
1908 $recur = sprintf( "%.2f", $recur );
1909 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
1910 $dbh->rollback if $oldAutoCommit;
1911 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1913 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
1914 $dbh->rollback if $oldAutoCommit;
1915 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1918 if ( $setup != 0 || $recur != 0 ) {
1920 warn " charges (setup=$setup, recur=$recur); adding line items\n"
1922 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1923 'invnum' => $invnum,
1924 'pkgnum' => $cust_pkg->pkgnum,
1928 'edate' => $cust_pkg->bill,
1929 'details' => \@details,
1931 $error = $cust_bill_pkg->insert;
1933 $dbh->rollback if $oldAutoCommit;
1934 return "can't create invoice line item for invoice #$invnum: $error";
1936 $total_setup += $setup;
1937 $total_recur += $recur;
1943 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
1946 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
1949 my %taxhash = map { $_ => $self->get("$prefix$_") }
1950 qw( state county country );
1952 $taxhash{'taxclass'} = $part_pkg->taxclass;
1954 my @taxes = qsearch( 'cust_main_county', \%taxhash );
1957 $taxhash{'taxclass'} = '';
1958 @taxes = qsearch( 'cust_main_county', \%taxhash );
1961 #one more try at a whole-country tax rate
1963 $taxhash{$_} = '' foreach qw( state county );
1964 @taxes = qsearch( 'cust_main_county', \%taxhash );
1967 # maybe eliminate this entirely, along with all the 0% records
1969 $dbh->rollback if $oldAutoCommit;
1971 "fatal: can't find tax rate for state/county/country/taxclass ".
1972 join('/', ( map $self->get("$prefix$_"),
1973 qw(state county country)
1975 $part_pkg->taxclass ). "\n";
1978 foreach my $tax ( @taxes ) {
1980 my $taxable_charged = 0;
1981 $taxable_charged += $setup
1982 unless $part_pkg->setuptax =~ /^Y$/i
1983 || $tax->setuptax =~ /^Y$/i;
1984 $taxable_charged += $recur
1985 unless $part_pkg->recurtax =~ /^Y$/i
1986 || $tax->recurtax =~ /^Y$/i;
1987 next unless $taxable_charged;
1989 if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
1990 #my ($mon,$year) = (localtime($sdate) )[4,5];
1991 my ($mon,$year) = (localtime( $sdate || $cust_bill->_date ) )[4,5];
1993 my $freq = $part_pkg->freq || 1;
1994 if ( $freq !~ /(\d+)$/ ) {
1995 $dbh->rollback if $oldAutoCommit;
1996 return "daily/weekly package definitions not (yet?)".
1997 " compatible with monthly tax exemptions";
1999 my $taxable_per_month =
2000 sprintf("%.2f", $taxable_charged / $freq );
2002 #call the whole thing off if this customer has any old
2003 #exemption records...
2004 my @cust_tax_exempt =
2005 qsearch( 'cust_tax_exempt' => { custnum=> $self->custnum } );
2006 if ( @cust_tax_exempt ) {
2007 $dbh->rollback if $oldAutoCommit;
2009 'this customer still has old-style tax exemption records; '.
2010 'run bin/fs-migrate-cust_tax_exempt?';
2013 foreach my $which_month ( 1 .. $freq ) {
2015 #maintain the new exemption table now
2018 FROM cust_tax_exempt_pkg
2019 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
2020 LEFT JOIN cust_bill USING ( invnum )
2026 my $sth = dbh->prepare($sql) or do {
2027 $dbh->rollback if $oldAutoCommit;
2028 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2036 $dbh->rollback if $oldAutoCommit;
2037 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2039 my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0;
2041 my $remaining_exemption =
2042 $tax->exempt_amount - $existing_exemption;
2043 if ( $remaining_exemption > 0 ) {
2044 my $addl = $remaining_exemption > $taxable_per_month
2045 ? $taxable_per_month
2046 : $remaining_exemption;
2047 $taxable_charged -= $addl;
2049 my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( {
2050 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2051 'taxnum' => $tax->taxnum,
2052 'year' => 1900+$year,
2054 'amount' => sprintf("%.2f", $addl ),
2056 $error = $cust_tax_exempt_pkg->insert;
2058 $dbh->rollback if $oldAutoCommit;
2059 return "fatal: can't insert cust_tax_exempt_pkg: $error";
2061 } # if $remaining_exemption > 0
2065 #until ( $mon < 12 ) { $mon -= 12; $year++; }
2066 until ( $mon < 13 ) { $mon -= 12; $year++; }
2068 } #foreach $which_month
2070 } #if $tax->exempt_amount
2072 $taxable_charged = sprintf( "%.2f", $taxable_charged);
2074 #$tax += $taxable_charged * $cust_main_county->tax / 100
2075 $tax{ $tax->taxname || 'Tax' } +=
2076 $taxable_charged * $tax->tax / 100
2078 } #foreach my $tax ( @taxes )
2080 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2082 } #if $setup != 0 || $recur != 0
2084 } #if $cust_pkg->modified
2086 } #foreach my $cust_pkg
2088 unless ( $cust_bill->cust_bill_pkg ) {
2089 $cust_bill->delete; #don't create an invoice w/o line items
2090 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2094 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
2096 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
2097 my $tax = sprintf("%.2f", $tax{$taxname} );
2098 $charged = sprintf( "%.2f", $charged+$tax );
2100 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2101 'invnum' => $invnum,
2107 'itemdesc' => $taxname,
2109 $error = $cust_bill_pkg->insert;
2111 $dbh->rollback if $oldAutoCommit;
2112 return "can't create invoice line item for invoice #$invnum: $error";
2114 $total_setup += $tax;
2118 $cust_bill->charged( sprintf( "%.2f", $total_setup + $total_recur ) );
2119 $error = $cust_bill->replace;
2121 $dbh->rollback if $oldAutoCommit;
2122 return "can't update charged for invoice #$invnum: $error";
2125 foreach my $hook ( @precommit_hooks ) {
2127 &{$hook}; #($self) ?
2130 $dbh->rollback if $oldAutoCommit;
2131 return "$@ running precommit hook $hook\n";
2135 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2139 =item collect OPTIONS
2141 (Attempt to) collect money for this customer's outstanding invoices (see
2142 L<FS::cust_bill>). Usually used after the bill method.
2144 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
2145 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
2146 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
2148 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
2149 and the invoice events web interface.
2151 If there is an error, returns the error, otherwise returns false.
2153 Options are passed as name-value pairs.
2155 Currently available options are:
2157 invoice_time - Use this time when deciding when to print invoices and
2158 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>
2159 for conversion functions.
2161 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
2164 quiet - set true to surpress email card/ACH decline notices.
2166 freq - "1d" for the traditional, daily events (the default), or "1m" for the
2169 payby - allows for one time override of normal customer billing method
2174 my( $self, %options ) = @_;
2175 my $invoice_time = $options{'invoice_time'} || time;
2178 local $SIG{HUP} = 'IGNORE';
2179 local $SIG{INT} = 'IGNORE';
2180 local $SIG{QUIT} = 'IGNORE';
2181 local $SIG{TERM} = 'IGNORE';
2182 local $SIG{TSTP} = 'IGNORE';
2183 local $SIG{PIPE} = 'IGNORE';
2185 my $oldAutoCommit = $FS::UID::AutoCommit;
2186 local $FS::UID::AutoCommit = 0;
2189 $self->select_for_update; #mutex
2191 my $balance = $self->balance;
2192 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2194 unless ( $balance > 0 ) { #redundant?????
2195 $dbh->rollback if $oldAutoCommit; #hmm
2199 if ( exists($options{'retry_card'}) ) {
2200 carp 'retry_card option passed to collect is deprecated; use retry';
2201 $options{'retry'} ||= $options{'retry_card'};
2203 if ( exists($options{'retry'}) && $options{'retry'} ) {
2204 my $error = $self->retry_realtime;
2206 $dbh->rollback if $oldAutoCommit;
2212 if ( defined $options{'freq'} && $options{'freq'} eq '1m' ) {
2213 $extra_sql = " AND freq = '1m' ";
2215 $extra_sql = " AND ( freq = '1d' OR freq IS NULL OR freq = '' ) ";
2218 foreach my $cust_bill ( $self->open_cust_bill ) {
2220 # don't try to charge for the same invoice if it's already in a batch
2221 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
2223 last if $self->balance <= 0;
2225 warn " invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
2228 foreach my $part_bill_event (
2229 sort { $a->seconds <=> $b->seconds
2230 || $a->weight <=> $b->weight
2231 || $a->eventpart <=> $b->eventpart }
2232 grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
2233 && ! qsearch( 'cust_bill_event', {
2234 'invnum' => $cust_bill->invnum,
2235 'eventpart' => $_->eventpart,
2240 'table' => 'part_bill_event',
2241 'hashref' => { 'payby' => (exists($options{'payby'})
2245 'disabled' => '', },
2246 'extra_sql' => $extra_sql,
2250 last if $cust_bill->owed <= 0 # don't run subsequent events if owed<=0
2251 || $self->balance <= 0; # or if balance<=0
2253 warn " calling invoice event (". $part_bill_event->eventcode. ")\n"
2255 my $cust_main = $self; #for callback
2259 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2260 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
2261 $error = eval $part_bill_event->eventcode;
2265 my $statustext = '';
2269 } elsif ( $error ) {
2271 $statustext = $error;
2276 #add cust_bill_event
2277 my $cust_bill_event = new FS::cust_bill_event {
2278 'invnum' => $cust_bill->invnum,
2279 'eventpart' => $part_bill_event->eventpart,
2280 #'_date' => $invoice_time,
2282 'status' => $status,
2283 'statustext' => $statustext,
2285 $error = $cust_bill_event->insert;
2287 #$dbh->rollback if $oldAutoCommit;
2288 #return "error: $error";
2290 # gah, even with transactions.
2291 $dbh->commit if $oldAutoCommit; #well.
2292 my $e = 'WARNING: Event run but database not updated - '.
2293 'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
2294 ', eventpart '. $part_bill_event->eventpart.
2305 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2310 =item retry_realtime
2312 Schedules realtime credit card / electronic check / LEC billing events for
2313 for retry. Useful if card information has changed or manual retry is desired.
2314 The 'collect' method must be called to actually retry the transaction.
2316 Implementation details: For each of this customer's open invoices, changes
2317 the status of the first "done" (with statustext error) realtime processing
2322 sub retry_realtime {
2325 local $SIG{HUP} = 'IGNORE';
2326 local $SIG{INT} = 'IGNORE';
2327 local $SIG{QUIT} = 'IGNORE';
2328 local $SIG{TERM} = 'IGNORE';
2329 local $SIG{TSTP} = 'IGNORE';
2330 local $SIG{PIPE} = 'IGNORE';
2332 my $oldAutoCommit = $FS::UID::AutoCommit;
2333 local $FS::UID::AutoCommit = 0;
2336 foreach my $cust_bill (
2337 grep { $_->cust_bill_event }
2338 $self->open_cust_bill
2340 my @cust_bill_event =
2341 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
2343 #$_->part_bill_event->plan eq 'realtime-card'
2344 $_->part_bill_event->eventcode =~
2345 /\$cust_bill\->realtime_(card|ach|lec)/
2346 && $_->status eq 'done'
2349 $cust_bill->cust_bill_event;
2350 next unless @cust_bill_event;
2351 my $error = $cust_bill_event[0]->retry;
2353 $dbh->rollback if $oldAutoCommit;
2354 return "error scheduling invoice event for retry: $error";
2359 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2364 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2366 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2367 via a Business::OnlinePayment realtime gateway. See
2368 L<http://420.am/business-onlinepayment> for supported gateways.
2370 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2372 Available options are: I<description>, I<invnum>, I<quiet>
2374 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2375 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2376 if set, will override the value from the customer record.
2378 I<description> is a free-text field passed to the gateway. It defaults to
2379 "Internet services".
2381 If an I<invnum> is specified, this payment (if successful) is applied to the
2382 specified invoice. If you don't specify an I<invnum> you might want to
2383 call the B<apply_payments> method.
2385 I<quiet> can be set true to surpress email decline notices.
2387 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2392 my( $self, $method, $amount, %options ) = @_;
2394 warn "$me realtime_bop: $method $amount\n";
2395 warn " $_ => $options{$_}\n" foreach keys %options;
2398 $options{'description'} ||= 'Internet services';
2400 eval "use Business::OnlinePayment";
2403 my $payinfo = exists($options{'payinfo'})
2404 ? $options{'payinfo'}
2412 if ( $options{'invnum'} ) {
2413 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
2414 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
2416 map { $_->part_pkg->taxclass }
2418 map { $_->cust_pkg }
2419 $cust_bill->cust_bill_pkg;
2420 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
2421 #different taxclasses
2422 $taxclass = $taxclasses[0];
2426 #look for an agent gateway override first
2428 if ( $method eq 'CC' ) {
2429 $cardtype = cardtype($payinfo);
2430 } elsif ( $method eq 'ECHECK' ) {
2433 $cardtype = $method;
2437 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2438 cardtype => $cardtype,
2439 taxclass => $taxclass, } )
2440 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2442 taxclass => $taxclass, } )
2443 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2444 cardtype => $cardtype,
2446 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2448 taxclass => '', } );
2450 my $payment_gateway = '';
2451 my( $processor, $login, $password, $action, @bop_options );
2452 if ( $override ) { #use a payment gateway override
2454 $payment_gateway = $override->payment_gateway;
2456 $processor = $payment_gateway->gateway_module;
2457 $login = $payment_gateway->gateway_username;
2458 $password = $payment_gateway->gateway_password;
2459 $action = $payment_gateway->gateway_action;
2460 @bop_options = $payment_gateway->options;
2462 } else { #use the standard settings from the config
2464 ( $processor, $login, $password, $action, @bop_options ) =
2465 $self->default_payment_gateway($method);
2473 my $address = exists($options{'address1'})
2474 ? $options{'address1'}
2476 my $address2 = exists($options{'address2'})
2477 ? $options{'address2'}
2479 $address .= ", ". $address2 if length($address2);
2481 my $o_payname = exists($options{'payname'})
2482 ? $options{'payname'}
2484 my($payname, $payfirst, $paylast);
2485 if ( $o_payname && $method ne 'ECHECK' ) {
2486 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2487 or return "Illegal payname $payname";
2488 ($payfirst, $paylast) = ($1, $2);
2490 $payfirst = $self->getfield('first');
2491 $paylast = $self->getfield('last');
2492 $payname = "$payfirst $paylast";
2495 my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
2496 if ( $conf->exists('emailinvoiceauto')
2497 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2498 push @invoicing_list, $self->all_emails;
2501 my $email = ($conf->exists('business-onlinepayment-email-override'))
2502 ? $conf->config('business-onlinepayment-email-override')
2503 : $invoicing_list[0];
2507 my $payip = exists($options{'payip'})
2510 $content{customer_ip} = $payip
2513 if ( $method eq 'CC' ) {
2515 $content{card_number} = $payinfo;
2516 my $paydate = exists($options{'paydate'})
2517 ? $options{'paydate'}
2519 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2520 $content{expiration} = "$2/$1";
2522 my $paycvv = exists($options{'paycvv'})
2523 ? $options{'paycvv'}
2525 $content{cvv2} = $self->paycvv
2528 my $paystart_month = exists($options{'paystart_month'})
2529 ? $options{'paystart_month'}
2530 : $self->paystart_month;
2532 my $paystart_year = exists($options{'paystart_year'})
2533 ? $options{'paystart_year'}
2534 : $self->paystart_year;
2536 $content{card_start} = "$paystart_month/$paystart_year"
2537 if $paystart_month && $paystart_year;
2539 my $payissue = exists($options{'payissue'})
2540 ? $options{'payissue'}
2542 $content{issue_number} = $payissue if $payissue;
2544 $content{recurring_billing} = 'YES'
2545 if qsearch('cust_pay', { 'custnum' => $self->custnum,
2547 'payinfo' => $payinfo,
2550 } elsif ( $method eq 'ECHECK' ) {
2551 ( $content{account_number}, $content{routing_code} ) =
2552 split('@', $payinfo);
2553 $content{bank_name} = $o_payname;
2554 $content{account_type} = 'CHECKING';
2555 $content{account_name} = $payname;
2556 $content{customer_org} = $self->company ? 'B' : 'I';
2557 $content{customer_ssn} = exists($options{'ss'})
2560 } elsif ( $method eq 'LEC' ) {
2561 $content{phone} = $payinfo;
2565 # run transaction(s)
2568 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
2570 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
2571 $transaction->content(
2574 'password' => $password,
2575 'action' => $action1,
2576 'description' => $options{'description'},
2577 'amount' => $amount,
2578 'invoice_number' => $options{'invnum'},
2579 'customer_id' => $self->custnum,
2580 'last_name' => $paylast,
2581 'first_name' => $payfirst,
2583 'address' => $address,
2584 'city' => ( exists($options{'city'})
2587 'state' => ( exists($options{'state'})
2590 'zip' => ( exists($options{'zip'})
2593 'country' => ( exists($options{'country'})
2594 ? $options{'country'}
2596 'referer' => 'http://cleanwhisker.420.am/',
2598 'phone' => $self->daytime || $self->night,
2601 $transaction->submit();
2603 if ( $transaction->is_success() && $action2 ) {
2604 my $auth = $transaction->authorization;
2605 my $ordernum = $transaction->can('order_number')
2606 ? $transaction->order_number
2610 new Business::OnlinePayment( $processor, @bop_options );
2617 password => $password,
2618 order_number => $ordernum,
2620 authorization => $auth,
2621 description => $options{'description'},
2624 foreach my $field (qw( authorization_source_code returned_ACI transaction_identifier validation_code
2625 transaction_sequence_num local_transaction_date
2626 local_transaction_time AVS_result_code )) {
2627 $capture{$field} = $transaction->$field() if $transaction->can($field);
2630 $capture->content( %capture );
2634 unless ( $capture->is_success ) {
2635 my $e = "Authorization successful but capture failed, custnum #".
2636 $self->custnum. ': '. $capture->result_code.
2637 ": ". $capture->error_message;
2645 # remove paycvv after initial transaction
2648 #false laziness w/misc/process/payment.cgi - check both to make sure working
2650 if ( defined $self->dbdef_table->column('paycvv')
2651 && length($self->paycvv)
2652 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
2654 my $error = $self->remove_cvv;
2656 warn "WARNING: error removing cvv: $error\n";
2664 if ( $transaction->is_success() ) {
2666 my %method2payby = (
2673 if ( $payment_gateway ) { # agent override
2674 $paybatch = $payment_gateway->gatewaynum. '-';
2677 $paybatch .= "$processor:". $transaction->authorization;
2679 $paybatch .= ':'. $transaction->order_number
2680 if $transaction->can('order_number')
2681 && length($transaction->order_number);
2683 my $cust_pay = new FS::cust_pay ( {
2684 'custnum' => $self->custnum,
2685 'invnum' => $options{'invnum'},
2688 'payby' => $method2payby{$method},
2689 'payinfo' => $payinfo,
2690 'paybatch' => $paybatch,
2692 my $error = $cust_pay->insert;
2694 $cust_pay->invnum(''); #try again with no specific invnum
2695 my $error2 = $cust_pay->insert;
2697 # gah, even with transactions.
2698 my $e = 'WARNING: Card/ACH debited but database not updated - '.
2699 "error inserting payment ($processor): $error2".
2700 " (previously tried insert with invnum #$options{'invnum'}" .
2706 return ''; #no error
2710 my $perror = "$processor error: ". $transaction->error_message;
2712 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
2713 && $conf->exists('emaildecline')
2714 && grep { $_ ne 'POST' } $self->invoicing_list
2715 && ! grep { $transaction->error_message =~ /$_/ }
2716 $conf->config('emaildecline-exclude')
2718 my @templ = $conf->config('declinetemplate');
2719 my $template = new Text::Template (
2721 SOURCE => [ map "$_\n", @templ ],
2722 ) or return "($perror) can't create template: $Text::Template::ERROR";
2723 $template->compile()
2724 or return "($perror) can't compile template: $Text::Template::ERROR";
2726 my $templ_hash = { error => $transaction->error_message };
2728 my $error = send_email(
2729 'from' => $conf->config('invoice_from'),
2730 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
2731 'subject' => 'Your payment could not be processed',
2732 'body' => [ $template->fill_in(HASH => $templ_hash) ],
2735 $perror .= " (also received error sending decline notification: $error)"
2745 =item default_payment_gateway
2749 sub default_payment_gateway {
2750 my( $self, $method ) = @_;
2752 die "Real-time processing not enabled\n"
2753 unless $conf->exists('business-onlinepayment');
2756 my $bop_config = 'business-onlinepayment';
2757 $bop_config .= '-ach'
2758 if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
2759 my ( $processor, $login, $password, $action, @bop_options ) =
2760 $conf->config($bop_config);
2761 $action ||= 'normal authorization';
2762 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
2763 die "No real-time processor is enabled - ".
2764 "did you set the business-onlinepayment configuration value?\n"
2767 ( $processor, $login, $password, $action, @bop_options )
2772 Removes the I<paycvv> field from the database directly.
2774 If there is an error, returns the error, otherwise returns false.
2780 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2781 or return dbh->errstr;
2782 $sth->execute($self->custnum)
2783 or return $sth->errstr;
2788 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
2790 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
2791 via a Business::OnlinePayment realtime gateway. See
2792 L<http://420.am/business-onlinepayment> for supported gateways.
2794 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2796 Available options are: I<amount>, I<reason>, I<paynum>
2798 Most gateways require a reference to an original payment transaction to refund,
2799 so you probably need to specify a I<paynum>.
2801 I<amount> defaults to the original amount of the payment if not specified.
2803 I<reason> specifies a reason for the refund.
2805 Implementation note: If I<amount> is unspecified or equal to the amount of the
2806 orignal payment, first an attempt is made to "void" the transaction via
2807 the gateway (to cancel a not-yet settled transaction) and then if that fails,
2808 the normal attempt is made to "refund" ("credit") the transaction via the
2809 gateway is attempted.
2811 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2812 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2813 #if set, will override the value from the customer record.
2815 #If an I<invnum> is specified, this payment (if successful) is applied to the
2816 #specified invoice. If you don't specify an I<invnum> you might want to
2817 #call the B<apply_payments> method.
2821 #some false laziness w/realtime_bop, not enough to make it worth merging
2822 #but some useful small subs should be pulled out
2823 sub realtime_refund_bop {
2824 my( $self, $method, %options ) = @_;
2826 warn "$me realtime_refund_bop: $method refund\n";
2827 warn " $_ => $options{$_}\n" foreach keys %options;
2830 eval "use Business::OnlinePayment";
2834 # look up the original payment and optionally a gateway for that payment
2838 my $amount = $options{'amount'};
2840 my( $processor, $login, $password, @bop_options ) ;
2841 my( $auth, $order_number ) = ( '', '', '' );
2843 if ( $options{'paynum'} ) {
2845 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
2846 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
2847 or return "Unknown paynum $options{'paynum'}";
2848 $amount ||= $cust_pay->paid;
2850 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-]*)(:([\w\-]+))?$/
2851 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
2852 $cust_pay->paybatch;
2853 my $gatewaynum = '';
2854 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
2856 if ( $gatewaynum ) { #gateway for the payment to be refunded
2858 my $payment_gateway =
2859 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
2860 die "payment gateway $gatewaynum not found"
2861 unless $payment_gateway;
2863 $processor = $payment_gateway->gateway_module;
2864 $login = $payment_gateway->gateway_username;
2865 $password = $payment_gateway->gateway_password;
2866 @bop_options = $payment_gateway->options;
2868 } else { #try the default gateway
2870 my( $conf_processor, $unused_action );
2871 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
2872 $self->default_payment_gateway($method);
2874 return "processor of payment $options{'paynum'} $processor does not".
2875 " match default processor $conf_processor"
2876 unless $processor eq $conf_processor;
2881 } else { # didn't specify a paynum, so look for agent gateway overrides
2882 # like a normal transaction
2885 if ( $method eq 'CC' ) {
2886 $cardtype = cardtype($self->payinfo);
2887 } elsif ( $method eq 'ECHECK' ) {
2890 $cardtype = $method;
2893 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2894 cardtype => $cardtype,
2896 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2898 taxclass => '', } );
2900 if ( $override ) { #use a payment gateway override
2902 my $payment_gateway = $override->payment_gateway;
2904 $processor = $payment_gateway->gateway_module;
2905 $login = $payment_gateway->gateway_username;
2906 $password = $payment_gateway->gateway_password;
2907 #$action = $payment_gateway->gateway_action;
2908 @bop_options = $payment_gateway->options;
2910 } else { #use the standard settings from the config
2913 ( $processor, $login, $password, $unused_action, @bop_options ) =
2914 $self->default_payment_gateway($method);
2919 return "neither amount nor paynum specified" unless $amount;
2924 'password' => $password,
2925 'order_number' => $order_number,
2926 'amount' => $amount,
2927 'referer' => 'http://cleanwhisker.420.am/',
2929 $content{authorization} = $auth
2930 if length($auth); #echeck/ACH transactions have an order # but no auth
2931 #(at least with authorize.net)
2933 #first try void if applicable
2934 if ( $cust_pay && $cust_pay->paid == $amount ) { #and check dates?
2935 warn " attempting void\n" if $DEBUG > 1;
2936 my $void = new Business::OnlinePayment( $processor, @bop_options );
2937 $void->content( 'action' => 'void', %content );
2939 if ( $void->is_success ) {
2940 my $error = $cust_pay->void($options{'reason'});
2942 # gah, even with transactions.
2943 my $e = 'WARNING: Card/ACH voided but database not updated - '.
2944 "error voiding payment: $error";
2948 warn " void successful\n" if $DEBUG > 1;
2953 warn " void unsuccessful, trying refund\n"
2957 my $address = $self->address1;
2958 $address .= ", ". $self->address2 if $self->address2;
2960 my($payname, $payfirst, $paylast);
2961 if ( $self->payname && $method ne 'ECHECK' ) {
2962 $payname = $self->payname;
2963 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2964 or return "Illegal payname $payname";
2965 ($payfirst, $paylast) = ($1, $2);
2967 $payfirst = $self->getfield('first');
2968 $paylast = $self->getfield('last');
2969 $payname = "$payfirst $paylast";
2972 my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
2973 if ( $conf->exists('emailinvoiceauto')
2974 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2975 push @invoicing_list, $self->all_emails;
2978 my $email = ($conf->exists('business-onlinepayment-email-override'))
2979 ? $conf->config('business-onlinepayment-email-override')
2980 : $invoicing_list[0];
2982 my $payip = exists($options{'payip'})
2985 $content{customer_ip} = $payip
2989 if ( $method eq 'CC' ) {
2992 $content{card_number} = $payinfo = $cust_pay->payinfo;
2993 #$self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2994 #$content{expiration} = "$2/$1";
2996 $content{card_number} = $payinfo = $self->payinfo;
2997 $self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2998 $content{expiration} = "$2/$1";
3001 } elsif ( $method eq 'ECHECK' ) {
3002 ( $content{account_number}, $content{routing_code} ) =
3003 split('@', $payinfo = $self->payinfo);
3004 $content{bank_name} = $self->payname;
3005 $content{account_type} = 'CHECKING';
3006 $content{account_name} = $payname;
3007 $content{customer_org} = $self->company ? 'B' : 'I';
3008 $content{customer_ssn} = $self->ss;
3009 } elsif ( $method eq 'LEC' ) {
3010 $content{phone} = $payinfo = $self->payinfo;
3014 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3015 my %sub_content = $refund->content(
3016 'action' => 'credit',
3017 'customer_id' => $self->custnum,
3018 'last_name' => $paylast,
3019 'first_name' => $payfirst,
3021 'address' => $address,
3022 'city' => $self->city,
3023 'state' => $self->state,
3024 'zip' => $self->zip,
3025 'country' => $self->country,
3027 'phone' => $self->daytime || $self->night,
3030 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3034 return "$processor error: ". $refund->error_message
3035 unless $refund->is_success();
3037 my %method2payby = (
3043 my $paybatch = "$processor:". $refund->authorization;
3044 $paybatch .= ':'. $refund->order_number
3045 if $refund->can('order_number') && $refund->order_number;
3047 while ( $cust_pay && $cust_pay->unappled < $amount ) {
3048 my @cust_bill_pay = $cust_pay->cust_bill_pay;
3049 last unless @cust_bill_pay;
3050 my $cust_bill_pay = pop @cust_bill_pay;
3051 my $error = $cust_bill_pay->delete;
3055 my $cust_refund = new FS::cust_refund ( {
3056 'custnum' => $self->custnum,
3057 'paynum' => $options{'paynum'},
3058 'refund' => $amount,
3060 'payby' => $method2payby{$method},
3061 'payinfo' => $payinfo,
3062 'paybatch' => $paybatch,
3063 'reason' => $options{'reason'} || 'card or ACH refund',
3065 my $error = $cust_refund->insert;
3067 $cust_refund->paynum(''); #try again with no specific paynum
3068 my $error2 = $cust_refund->insert;
3070 # gah, even with transactions.
3071 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
3072 "error inserting refund ($processor): $error2".
3073 " (previously tried insert with paynum #$options{'paynum'}" .
3086 Returns the total owed for this customer on all invoices
3087 (see L<FS::cust_bill/owed>).
3093 $self->total_owed_date(2145859200); #12/31/2037
3096 =item total_owed_date TIME
3098 Returns the total owed for this customer on all invoices with date earlier than
3099 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
3100 see L<Time::Local> and L<Date::Parse> for conversion functions.
3104 sub total_owed_date {
3108 foreach my $cust_bill (
3109 grep { $_->_date <= $time }
3110 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3112 $total_bill += $cust_bill->owed;
3114 sprintf( "%.2f", $total_bill );
3117 =item apply_credits OPTION => VALUE ...
3119 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
3120 to outstanding invoice balances in chronological order (or reverse
3121 chronological order if the I<order> option is set to B<newest>) and returns the
3122 value of any remaining unapplied credits available for refund (see
3123 L<FS::cust_refund>).
3131 return 0 unless $self->total_credited;
3133 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
3134 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
3136 my @invoices = $self->open_cust_bill;
3137 @invoices = sort { $b->_date <=> $a->_date } @invoices
3138 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
3141 foreach my $cust_bill ( @invoices ) {
3144 if ( !defined($credit) || $credit->credited == 0) {
3145 $credit = pop @credits or last;
3148 if ($cust_bill->owed >= $credit->credited) {
3149 $amount=$credit->credited;
3151 $amount=$cust_bill->owed;
3154 my $cust_credit_bill = new FS::cust_credit_bill ( {
3155 'crednum' => $credit->crednum,
3156 'invnum' => $cust_bill->invnum,
3157 'amount' => $amount,
3159 my $error = $cust_credit_bill->insert;
3160 die $error if $error;
3162 redo if ($cust_bill->owed > 0);
3166 return $self->total_credited;
3169 =item apply_payments
3171 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
3172 to outstanding invoice balances in chronological order.
3174 #and returns the value of any remaining unapplied payments.
3178 sub apply_payments {
3183 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
3184 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
3186 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
3187 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
3191 foreach my $cust_bill ( @invoices ) {
3194 if ( !defined($payment) || $payment->unapplied == 0 ) {
3195 $payment = pop @payments or last;
3198 if ( $cust_bill->owed >= $payment->unapplied ) {
3199 $amount = $payment->unapplied;
3201 $amount = $cust_bill->owed;
3204 my $cust_bill_pay = new FS::cust_bill_pay ( {
3205 'paynum' => $payment->paynum,
3206 'invnum' => $cust_bill->invnum,
3207 'amount' => $amount,
3209 my $error = $cust_bill_pay->insert;
3210 die $error if $error;
3212 redo if ( $cust_bill->owed > 0);
3216 return $self->total_unapplied_payments;
3219 =item total_credited
3221 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3222 customer. See L<FS::cust_credit/credited>.
3226 sub total_credited {
3228 my $total_credit = 0;
3229 foreach my $cust_credit ( qsearch('cust_credit', {
3230 'custnum' => $self->custnum,
3232 $total_credit += $cust_credit->credited;
3234 sprintf( "%.2f", $total_credit );
3237 =item total_unapplied_payments
3239 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3240 See L<FS::cust_pay/unapplied>.
3244 sub total_unapplied_payments {
3246 my $total_unapplied = 0;
3247 foreach my $cust_pay ( qsearch('cust_pay', {
3248 'custnum' => $self->custnum,
3250 $total_unapplied += $cust_pay->unapplied;
3252 sprintf( "%.2f", $total_unapplied );
3257 Returns the balance for this customer (total_owed minus total_credited
3258 minus total_unapplied_payments).
3265 $self->total_owed - $self->total_credited - $self->total_unapplied_payments
3269 =item balance_date TIME
3271 Returns the balance for this customer, only considering invoices with date
3272 earlier than TIME (total_owed_date minus total_credited minus
3273 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
3274 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
3283 $self->total_owed_date($time)
3284 - $self->total_credited
3285 - $self->total_unapplied_payments
3289 =item in_transit_payments
3291 Returns the total of requests for payments for this customer pending in
3292 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
3296 sub in_transit_payments {
3298 my $in_transit_payments = 0;
3299 foreach my $pay_batch ( qsearch('pay_batch', {
3302 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
3303 'batchnum' => $pay_batch->batchnum,
3304 'custnum' => $self->custnum,
3306 $in_transit_payments += $cust_pay_batch->amount;
3309 sprintf( "%.2f", $in_transit_payments );
3312 =item paydate_monthyear
3314 Returns a two-element list consisting of the month and year of this customer's
3315 paydate (credit card expiration date for CARD customers)
3319 sub paydate_monthyear {
3321 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
3323 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
3330 =item payinfo_masked
3332 Returns a "masked" payinfo field appropriate to the payment type. Masked characters are replaced by 'x'es. Use this to display publicly accessable account Information.
3334 Credit Cards - Mask all but the last four characters.
3335 Checks - Mask all but last 2 of account number and bank routing number.
3336 Others - Do nothing, return the unmasked string.
3340 sub payinfo_masked {
3342 return $self->paymask;
3345 =item invoicing_list [ ARRAYREF ]
3347 If an arguement is given, sets these email addresses as invoice recipients
3348 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
3349 (except as warnings), so use check_invoicing_list first.
3351 Returns a list of email addresses (with svcnum entries expanded).
3353 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
3354 check it without disturbing anything by passing nothing.
3356 This interface may change in the future.
3360 sub invoicing_list {
3361 my( $self, $arrayref ) = @_;
3364 my @cust_main_invoice;
3365 if ( $self->custnum ) {
3366 @cust_main_invoice =
3367 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3369 @cust_main_invoice = ();
3371 foreach my $cust_main_invoice ( @cust_main_invoice ) {
3372 #warn $cust_main_invoice->destnum;
3373 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3374 #warn $cust_main_invoice->destnum;
3375 my $error = $cust_main_invoice->delete;
3376 warn $error if $error;
3379 if ( $self->custnum ) {
3380 @cust_main_invoice =
3381 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3383 @cust_main_invoice = ();
3385 my %seen = map { $_->address => 1 } @cust_main_invoice;
3386 foreach my $address ( @{$arrayref} ) {
3387 next if exists $seen{$address} && $seen{$address};
3388 $seen{$address} = 1;
3389 my $cust_main_invoice = new FS::cust_main_invoice ( {
3390 'custnum' => $self->custnum,
3393 my $error = $cust_main_invoice->insert;
3394 warn $error if $error;
3398 if ( $self->custnum ) {
3400 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3407 =item check_invoicing_list ARRAYREF
3409 Checks these arguements as valid input for the invoicing_list method. If there
3410 is an error, returns the error, otherwise returns false.
3414 sub check_invoicing_list {
3415 my( $self, $arrayref ) = @_;
3416 foreach my $address ( @{$arrayref} ) {
3418 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3419 return 'Can\'t add FAX invoice destination with a blank FAX number.';
3422 my $cust_main_invoice = new FS::cust_main_invoice ( {
3423 'custnum' => $self->custnum,
3426 my $error = $self->custnum
3427 ? $cust_main_invoice->check
3428 : $cust_main_invoice->checkdest
3430 return $error if $error;
3435 =item set_default_invoicing_list
3437 Sets the invoicing list to all accounts associated with this customer,
3438 overwriting any previous invoicing list.
3442 sub set_default_invoicing_list {
3444 $self->invoicing_list($self->all_emails);
3449 Returns the email addresses of all accounts provisioned for this customer.
3456 foreach my $cust_pkg ( $self->all_pkgs ) {
3457 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
3459 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3460 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3462 $list{$_}=1 foreach map { $_->email } @svc_acct;
3467 =item invoicing_list_addpost
3469 Adds postal invoicing to this customer. If this customer is already configured
3470 to receive postal invoices, does nothing.
3474 sub invoicing_list_addpost {
3476 return if grep { $_ eq 'POST' } $self->invoicing_list;
3477 my @invoicing_list = $self->invoicing_list;
3478 push @invoicing_list, 'POST';
3479 $self->invoicing_list(\@invoicing_list);
3482 =item invoicing_list_emailonly
3484 Returns the list of email invoice recipients (invoicing_list without non-email
3485 destinations such as POST and FAX).
3489 sub invoicing_list_emailonly {
3491 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
3494 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
3496 Returns an array of customers referred by this customer (referral_custnum set
3497 to this custnum). If DEPTH is given, recurses up to the given depth, returning
3498 customers referred by customers referred by this customer and so on, inclusive.
3499 The default behavior is DEPTH 1 (no recursion).
3503 sub referral_cust_main {
3505 my $depth = @_ ? shift : 1;
3506 my $exclude = @_ ? shift : {};
3509 map { $exclude->{$_->custnum}++; $_; }
3510 grep { ! $exclude->{ $_->custnum } }
3511 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
3515 map { $_->referral_cust_main($depth-1, $exclude) }
3522 =item referral_cust_main_ncancelled
3524 Same as referral_cust_main, except only returns customers with uncancelled
3529 sub referral_cust_main_ncancelled {
3531 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
3534 =item referral_cust_pkg [ DEPTH ]
3536 Like referral_cust_main, except returns a flat list of all unsuspended (and
3537 uncancelled) packages for each customer. The number of items in this list may
3538 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
3542 sub referral_cust_pkg {
3544 my $depth = @_ ? shift : 1;
3546 map { $_->unsuspended_pkgs }
3547 grep { $_->unsuspended_pkgs }
3548 $self->referral_cust_main($depth);
3551 =item referring_cust_main
3553 Returns the single cust_main record for the customer who referred this customer
3554 (referral_custnum), or false.
3558 sub referring_cust_main {
3560 return '' unless $self->referral_custnum;
3561 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3564 =item credit AMOUNT, REASON
3566 Applies a credit to this customer. If there is an error, returns the error,
3567 otherwise returns false.
3572 my( $self, $amount, $reason ) = @_;
3573 my $cust_credit = new FS::cust_credit {
3574 'custnum' => $self->custnum,
3575 'amount' => $amount,
3576 'reason' => $reason,
3578 $cust_credit->insert;
3581 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3583 Creates a one-time charge for this customer. If there is an error, returns
3584 the error, otherwise returns false.
3589 my ( $self, $amount ) = ( shift, shift );
3590 my $pkg = @_ ? shift : 'One-time charge';
3591 my $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
3592 my $taxclass = @_ ? shift : '';
3594 local $SIG{HUP} = 'IGNORE';
3595 local $SIG{INT} = 'IGNORE';
3596 local $SIG{QUIT} = 'IGNORE';
3597 local $SIG{TERM} = 'IGNORE';
3598 local $SIG{TSTP} = 'IGNORE';
3599 local $SIG{PIPE} = 'IGNORE';
3601 my $oldAutoCommit = $FS::UID::AutoCommit;
3602 local $FS::UID::AutoCommit = 0;
3605 my $part_pkg = new FS::part_pkg ( {
3607 'comment' => $comment,
3608 #'setup' => $amount,
3611 'plandata' => "setup_fee=$amount",
3614 'taxclass' => $taxclass,
3617 my $error = $part_pkg->insert;
3619 $dbh->rollback if $oldAutoCommit;
3623 my $pkgpart = $part_pkg->pkgpart;
3624 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3625 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3626 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3627 $error = $type_pkgs->insert;
3629 $dbh->rollback if $oldAutoCommit;
3634 my $cust_pkg = new FS::cust_pkg ( {
3635 'custnum' => $self->custnum,
3636 'pkgpart' => $pkgpart,
3639 $error = $cust_pkg->insert;
3641 $dbh->rollback if $oldAutoCommit;
3645 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3652 Returns all the invoices (see L<FS::cust_bill>) for this customer.
3658 sort { $a->_date <=> $b->_date }
3659 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3662 =item open_cust_bill
3664 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
3669 sub open_cust_bill {
3671 grep { $_->owed > 0 } $self->cust_bill;
3676 Returns all the credits (see L<FS::cust_credit>) for this customer.
3682 sort { $a->_date <=> $b->_date }
3683 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
3688 Returns all the payments (see L<FS::cust_pay>) for this customer.
3694 sort { $a->_date <=> $b->_date }
3695 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
3700 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
3706 sort { $a->_date <=> $b->_date }
3707 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
3713 Returns all the refunds (see L<FS::cust_refund>) for this customer.
3719 sort { $a->_date <=> $b->_date }
3720 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
3723 =item select_for_update
3725 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
3730 sub select_for_update {
3732 qsearch('cust_main', { 'custnum' => $self->custnum }, '*', 'FOR UPDATE' );
3737 Returns a name string for this customer, either "Company (Last, First)" or
3744 my $name = $self->contact;
3745 $name = $self->company. " ($name)" if $self->company;
3751 Returns a name string for this (service/shipping) contact, either
3752 "Company (Last, First)" or "Last, First".
3758 if ( $self->get('ship_last') ) {
3759 my $name = $self->ship_contact;
3760 $name = $self->ship_company. " ($name)" if $self->ship_company;
3769 Returns this customer's full (billing) contact name only, "Last, First"
3775 $self->get('last'). ', '. $self->first;
3780 Returns this customer's full (shipping) contact name only, "Last, First"
3786 $self->get('ship_last')
3787 ? $self->get('ship_last'). ', '. $self->ship_first
3793 Returns this customer's full country name
3799 code2country($self->country);
3804 Returns a status string for this customer, currently:
3808 =item prospect - No packages have ever been ordered
3810 =item active - One or more recurring packages is active
3812 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
3814 =item suspended - All non-cancelled recurring packages are suspended
3816 =item cancelled - All recurring packages are cancelled
3824 for my $status (qw( prospect active inactive suspended cancelled )) {
3825 my $method = $status.'_sql';
3826 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
3827 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
3828 $sth->execute( ($self->custnum) x $numnum ) or die $sth->errstr;
3829 return $status if $sth->fetchrow_arrayref->[0];
3835 Returns a hex triplet color string for this customer's status.
3839 use vars qw(%statuscolor);
3841 'prospect' => '7e0079', #'000000', #black? naw, purple
3842 'active' => '00CC00', #green
3843 'inactive' => '0000CC', #blue
3844 'suspended' => 'FF9900', #yellow
3845 'cancelled' => 'FF0000', #red
3850 $statuscolor{$self->status};
3855 =head1 CLASS METHODS
3861 Returns an SQL expression identifying prospective cust_main records (customers
3862 with no packages ever ordered)
3866 use vars qw($select_count_pkgs);
3867 $select_count_pkgs =
3868 "SELECT COUNT(*) FROM cust_pkg
3869 WHERE cust_pkg.custnum = cust_main.custnum";
3871 sub select_count_pkgs_sql {
3875 sub prospect_sql { "
3876 0 = ( $select_count_pkgs )
3881 Returns an SQL expression identifying active cust_main records (customers with
3882 no active recurring packages, but otherwise unsuspended/uncancelled).
3887 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
3893 Returns an SQL expression identifying inactive cust_main records (customers with
3894 active recurring packages).
3898 sub inactive_sql { "
3899 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
3901 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
3907 Returns an SQL expression identifying suspended cust_main records.
3912 sub suspended_sql { susp_sql(@_); }
3914 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
3916 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
3922 Returns an SQL expression identifying cancelled cust_main records.
3926 sub cancelled_sql { cancel_sql(@_); }
3929 my $recurring_sql = FS::cust_pkg->recurring_sql;
3930 #my $recurring_sql = "
3931 # '0' != ( select freq from part_pkg
3932 # where cust_pkg.pkgpart = part_pkg.pkgpart )
3936 0 < ( $select_count_pkgs )
3937 AND 0 = ( $select_count_pkgs AND $recurring_sql
3938 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3944 =item uncancelled_sql
3946 Returns an SQL expression identifying un-cancelled cust_main records.
3950 sub uncancelled_sql { uncancel_sql(@_); }
3951 sub uncancel_sql { "
3952 ( 0 < ( $select_count_pkgs
3953 AND ( cust_pkg.cancel IS NULL
3954 OR cust_pkg.cancel = 0
3957 OR 0 = ( $select_count_pkgs )
3961 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
3963 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
3964 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
3965 appropriate ship_ field is also searched).
3967 Additional options are the same as FS::Record::qsearch
3972 my( $self, $fuzzy, $hash, @opt) = @_;
3977 check_and_rebuild_fuzzyfiles();
3978 foreach my $field ( keys %$fuzzy ) {
3980 $match{$_}=1 foreach ( amatch( $fuzzy->{$field},
3982 @{ $self->all_X($field) }
3987 foreach ( keys %match ) {
3988 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
3989 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
3992 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
3995 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
3997 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
4009 =item smart_search OPTION => VALUE ...
4011 Accepts the following options: I<search>, the string to search for. The string
4012 will be searched for as a customer number, phone number, name or company name,
4013 first searching for an exact match then fuzzy and substring matches (in some
4014 cases - see the source code for the exact heuristics used).
4016 Any additional options treated as an additional qualifier on the search
4019 Returns a (possibly empty) array of FS::cust_main objects.
4026 #here is the agent virtualization
4027 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
4031 my $search = delete $options{'search'};
4032 ( my $alphanum_search = $search ) =~ s/\W//g;
4034 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
4036 #false laziness w/Record::ut_phone
4037 my $phonen = "$1-$2-$3";
4038 $phonen .= " x$4" if $4;
4040 push @cust_main, qsearch( {
4041 'table' => 'cust_main',
4042 'hashref' => { %options },
4043 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
4045 join(' OR ', map "$_ = '$phonen'",
4046 qw( daytime night fax
4047 ship_daytime ship_night ship_fax )
4050 " AND $agentnums_sql", #agent virtualization
4053 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
4054 #try looking for matches with extensions unless one was specified
4056 push @cust_main, qsearch( {
4057 'table' => 'cust_main',
4058 'hashref' => { %options },
4059 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
4061 join(' OR ', map "$_ LIKE '$phonen\%'",
4063 ship_daytime ship_night )
4066 " AND $agentnums_sql", #agent virtualization
4071 } elsif ( $search =~ /^\s*(\d+)\s*$/ ) { # customer # search
4073 push @cust_main, qsearch( {
4074 'table' => 'cust_main',
4075 'hashref' => { 'custnum' => $1, %options },
4076 'extra_sql' => " AND $agentnums_sql", #agent virtualization
4079 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
4081 my($company, $last, $first) = ( $1, $2, $3 );
4083 # "Company (Last, First)"
4084 #this is probably something a browser remembered,
4085 #so just do an exact search
4087 foreach my $prefix ( '', 'ship_' ) {
4088 push @cust_main, qsearch( {
4089 'table' => 'cust_main',
4090 'hashref' => { $prefix.'first' => $first,
4091 $prefix.'last' => $last,
4092 $prefix.'company' => $company,
4095 'extra_sql' => " AND $agentnums_sql",
4099 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
4100 # try (ship_){last,company}
4104 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
4105 # # full strings the browser remembers won't work
4106 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
4108 use Lingua::EN::NameParse;
4109 my $NameParse = new Lingua::EN::NameParse(
4111 allow_reversed => 1,
4114 my($last, $first) = ( '', '' );
4115 #maybe disable this too and just rely on NameParse?
4116 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
4118 ($last, $first) = ( $1, $2 );
4120 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
4121 } elsif ( ! $NameParse->parse($value) ) {
4123 my %name = $NameParse->components;
4124 $first = $name{'given_name_1'};
4125 $last = $name{'surname_1'};
4129 if ( $first && $last ) {
4131 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
4134 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
4136 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
4137 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
4140 push @cust_main, qsearch( {
4141 'table' => 'cust_main',
4142 'hashref' => \%options,
4143 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
4146 # or it just be something that was typed in... (try that in a sec)
4150 my $q_value = dbh->quote($value);
4153 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
4154 $sql .= " ( LOWER(last) = $q_value
4155 OR LOWER(company) = $q_value
4156 OR LOWER(ship_last) = $q_value
4157 OR LOWER(ship_company) = $q_value
4160 push @cust_main, qsearch( {
4161 'table' => 'cust_main',
4162 'hashref' => \%options,
4163 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
4166 unless ( @cust_main ) { #no exact match, trying substring/fuzzy
4168 #still some false laziness w/ search/cust_main.cgi
4173 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
4174 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
4177 if ( $first && $last ) {
4180 { 'first' => { op=>'ILIKE', value=>"%$first%" },
4181 'last' => { op=>'ILIKE', value=>"%$last%" },
4183 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
4184 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
4191 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
4192 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
4196 foreach my $hashref ( @hashrefs ) {
4198 push @cust_main, qsearch( {
4199 'table' => 'cust_main',
4200 'hashref' => { %$hashref,
4203 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
4212 " AND $agentnums_sql", #extra_sql #agent virtualization
4215 if ( $first && $last ) {
4216 push @cust_main, FS::cust_main->fuzzy_search(
4217 { 'last' => $last, #fuzzy hashref
4218 'first' => $first }, #
4222 foreach my $field ( 'last', 'company' ) {
4224 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
4229 #eliminate duplicates
4231 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
4239 =item check_and_rebuild_fuzzyfiles
4243 use vars qw(@fuzzyfields);
4244 @fuzzyfields = ( 'last', 'first', 'company' );
4246 sub check_and_rebuild_fuzzyfiles {
4247 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
4248 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
4251 =item rebuild_fuzzyfiles
4255 sub rebuild_fuzzyfiles {
4257 use Fcntl qw(:flock);
4259 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
4260 mkdir $dir, 0700 unless -d $dir;
4262 foreach my $fuzzy ( @fuzzyfields ) {
4264 open(LOCK,">>$dir/cust_main.$fuzzy")
4265 or die "can't open $dir/cust_main.$fuzzy: $!";
4267 or die "can't lock $dir/cust_main.$fuzzy: $!";
4269 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
4270 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
4272 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
4273 my $sth = dbh->prepare("SELECT $field FROM cust_main".
4274 " WHERE $field != '' AND $field IS NOT NULL");
4275 $sth->execute or die $sth->errstr;
4277 while ( my $row = $sth->fetchrow_arrayref ) {
4278 print CACHE $row->[0]. "\n";
4283 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
4285 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
4296 my( $self, $field ) = @_;
4297 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
4298 open(CACHE,"<$dir/cust_main.$field")
4299 or die "can't open $dir/cust_main.$field: $!";
4300 my @array = map { chomp; $_; } <CACHE>;
4305 =item append_fuzzyfiles LASTNAME COMPANY
4309 sub append_fuzzyfiles {
4310 #my( $first, $last, $company ) = @_;
4312 &check_and_rebuild_fuzzyfiles;
4314 use Fcntl qw(:flock);
4316 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
4318 foreach my $field (qw( first last company )) {
4323 open(CACHE,">>$dir/cust_main.$field")
4324 or die "can't open $dir/cust_main.$field: $!";
4325 flock(CACHE,LOCK_EX)
4326 or die "can't lock $dir/cust_main.$field: $!";
4328 print CACHE "$value\n";
4330 flock(CACHE,LOCK_UN)
4331 or die "can't unlock $dir/cust_main.$field: $!";
4346 #warn join('-',keys %$param);
4347 my $fh = $param->{filehandle};
4348 my $agentnum = $param->{agentnum};
4350 my $refnum = $param->{refnum};
4351 my $pkgpart = $param->{pkgpart};
4353 #my @fields = @{$param->{fields}};
4354 my $format = $param->{'format'};
4357 if ( $format eq 'simple' ) {
4358 @fields = qw( cust_pkg.setup dayphone first last
4359 address1 address2 city state zip comments );
4361 } elsif ( $format eq 'extended' ) {
4362 @fields = qw( agent_custid refnum
4363 last first address1 address2 city state zip country
4365 ship_last ship_first ship_address1 ship_address2
4366 ship_city ship_state ship_zip ship_country
4367 payinfo paycvv paydate
4370 svc_acct.username svc_acct._password
4374 die "unknown format $format";
4377 eval "use Text::CSV_XS;";
4380 my $csv = new Text::CSV_XS;
4387 local $SIG{HUP} = 'IGNORE';
4388 local $SIG{INT} = 'IGNORE';
4389 local $SIG{QUIT} = 'IGNORE';
4390 local $SIG{TERM} = 'IGNORE';
4391 local $SIG{TSTP} = 'IGNORE';
4392 local $SIG{PIPE} = 'IGNORE';
4394 my $oldAutoCommit = $FS::UID::AutoCommit;
4395 local $FS::UID::AutoCommit = 0;
4398 #while ( $columns = $csv->getline($fh) ) {
4400 while ( defined($line=<$fh>) ) {
4402 $csv->parse($line) or do {
4403 $dbh->rollback if $oldAutoCommit;
4404 return "can't parse: ". $csv->error_input();
4407 my @columns = $csv->fields();
4408 #warn join('-',@columns);
4411 agentnum => $agentnum,
4413 country => $conf->config('countrydefault') || 'US',
4414 payby => $payby, #default
4415 paydate => '12/2037', #default
4417 my $billtime = time;
4418 my %cust_pkg = ( pkgpart => $pkgpart );
4420 foreach my $field ( @fields ) {
4422 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|expire|cancel)$/ ) {
4424 #$cust_pkg{$1} = str2time( shift @$columns );
4425 if ( $1 eq 'pkgpart' ) {
4426 $cust_pkg{$1} = shift @columns;
4427 } elsif ( $1 eq 'setup' ) {
4428 $billtime = str2time(shift @columns);
4430 $cust_pkg{$1} = str2time( shift @columns );
4433 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
4435 $svc_acct{$1} = shift @columns;
4439 #refnum interception
4440 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
4442 my $referral = $columns[0];
4443 my $part_referral = new FS::part_referral {
4444 'referral' => $referral,
4445 'agentnum' => $agentnum,
4448 my $error = $part_referral->insert;
4450 $dbh->rollback if $oldAutoCommit;
4451 return "can't auto-insert advertising source: $referral: $error";
4453 $columns[0] = $part_referral->refnum;
4456 #$cust_main{$field} = shift @$columns;
4457 $cust_main{$field} = shift @columns;
4461 my $invoicing_list = $cust_main{'invoicing_list'}
4462 ? [ delete $cust_main{'invoicing_list'} ]
4465 my $cust_main = new FS::cust_main ( \%cust_main );
4468 tie my %hash, 'Tie::RefHash'; #this part is important
4470 if ( $cust_pkg{'pkgpart'} ) {
4471 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
4474 if ( $svc_acct{'username'} ) {
4475 $svc_acct{svcpart} = $cust_pkg->part_pkg->svcpart( 'svc_acct' );
4476 push @svc_acct, new FS::svc_acct ( \%svc_acct )
4479 $hash{$cust_pkg} = \@svc_acct;
4482 my $error = $cust_main->insert( \%hash, $invoicing_list );
4485 $dbh->rollback if $oldAutoCommit;
4486 return "can't insert customer for $line: $error";
4489 if ( $format eq 'simple' ) {
4491 #false laziness w/bill.cgi
4492 $error = $cust_main->bill( 'time' => $billtime );
4494 $dbh->rollback if $oldAutoCommit;
4495 return "can't bill customer for $line: $error";
4498 $cust_main->apply_payments;
4499 $cust_main->apply_credits;
4501 $error = $cust_main->collect();
4503 $dbh->rollback if $oldAutoCommit;
4504 return "can't collect customer for $line: $error";
4512 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4514 return "Empty file!" unless $imported;
4526 #warn join('-',keys %$param);
4527 my $fh = $param->{filehandle};
4528 my @fields = @{$param->{fields}};
4530 eval "use Text::CSV_XS;";
4533 my $csv = new Text::CSV_XS;
4540 local $SIG{HUP} = 'IGNORE';
4541 local $SIG{INT} = 'IGNORE';
4542 local $SIG{QUIT} = 'IGNORE';
4543 local $SIG{TERM} = 'IGNORE';
4544 local $SIG{TSTP} = 'IGNORE';
4545 local $SIG{PIPE} = 'IGNORE';
4547 my $oldAutoCommit = $FS::UID::AutoCommit;
4548 local $FS::UID::AutoCommit = 0;
4551 #while ( $columns = $csv->getline($fh) ) {
4553 while ( defined($line=<$fh>) ) {
4555 $csv->parse($line) or do {
4556 $dbh->rollback if $oldAutoCommit;
4557 return "can't parse: ". $csv->error_input();
4560 my @columns = $csv->fields();
4561 #warn join('-',@columns);
4564 foreach my $field ( @fields ) {
4565 $row{$field} = shift @columns;
4568 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
4569 unless ( $cust_main ) {
4570 $dbh->rollback if $oldAutoCommit;
4571 return "unknown custnum $row{'custnum'}";
4574 if ( $row{'amount'} > 0 ) {
4575 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
4577 $dbh->rollback if $oldAutoCommit;
4581 } elsif ( $row{'amount'} < 0 ) {
4582 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
4585 $dbh->rollback if $oldAutoCommit;
4595 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4597 return "Empty file!" unless $imported;
4609 The delete method should possibly take an FS::cust_main object reference
4610 instead of a scalar customer number.
4612 Bill and collect options should probably be passed as references instead of a
4615 There should probably be a configuration file with a list of allowed credit
4618 No multiple currency support (probably a larger project than just this module).
4620 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
4624 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
4625 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
4626 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.