4 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
5 $import $skip_fuzzyfiles );
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);";
19 use String::Approx qw(amatch);
20 use Business::CreditCard 0.28;
21 use FS::UID qw( getotaker dbh );
22 use FS::Record qw( qsearchs qsearch dbdef );
23 use FS::Misc qw( send_email );
27 use FS::cust_bill_pkg;
29 use FS::cust_pay_void;
32 use FS::part_referral;
33 use FS::cust_main_county;
35 use FS::cust_main_invoice;
36 use FS::cust_credit_bill;
37 use FS::cust_bill_pay;
38 use FS::prepay_credit;
41 use FS::part_bill_event;
42 use FS::cust_bill_event;
43 use FS::cust_tax_exempt;
45 use FS::payment_gateway;
46 use FS::agent_payment_gateway;
47 use FS::Msgcat qw(gettext);
49 @ISA = qw( FS::Record );
51 @EXPORT_OK = qw( smart_search );
53 $realtime_bop_decline_quiet = 0;
56 $me = '[FS::cust_main]';
61 @encrypted_fields = ('payinfo', 'paycvv');
63 #ask FS::UID to run this stuff for us later
64 #$FS::UID::callback{'FS::cust_main'} = sub {
65 install_callback FS::UID sub {
67 #yes, need it for stuff below (prolly should be cached)
72 my ( $hashref, $cache ) = @_;
73 if ( exists $hashref->{'pkgnum'} ) {
74 # #@{ $self->{'_pkgnum'} } = ();
75 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
76 $self->{'_pkgnum'} = $subcache;
77 #push @{ $self->{'_pkgnum'} },
78 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
84 FS::cust_main - Object methods for cust_main records
90 $record = new FS::cust_main \%hash;
91 $record = new FS::cust_main { 'column' => 'value' };
93 $error = $record->insert;
95 $error = $new_record->replace($old_record);
97 $error = $record->delete;
99 $error = $record->check;
101 @cust_pkg = $record->all_pkgs;
103 @cust_pkg = $record->ncancelled_pkgs;
105 @cust_pkg = $record->suspended_pkgs;
107 $error = $record->bill;
108 $error = $record->bill %options;
109 $error = $record->bill 'time' => $time;
111 $error = $record->collect;
112 $error = $record->collect %options;
113 $error = $record->collect 'invoice_time' => $time,
114 'batch_card' => 'yes',
115 'report_badcard' => 'yes',
120 An FS::cust_main object represents a customer. FS::cust_main inherits from
121 FS::Record. The following fields are currently supported:
125 =item custnum - primary key (assigned automatically for new customers)
127 =item agentnum - agent (see L<FS::agent>)
129 =item refnum - Advertising source (see L<FS::part_referral>)
135 =item ss - social security number (optional)
137 =item company - (optional)
141 =item address2 - (optional)
145 =item county - (optional, see L<FS::cust_main_county>)
147 =item state - (see L<FS::cust_main_county>)
151 =item country - (see L<FS::cust_main_county>)
153 =item daytime - phone (optional)
155 =item night - phone (optional)
157 =item fax - phone (optional)
159 =item ship_first - name
161 =item ship_last - name
163 =item ship_company - (optional)
167 =item ship_address2 - (optional)
171 =item ship_county - (optional, see L<FS::cust_main_county>)
173 =item ship_state - (see L<FS::cust_main_county>)
177 =item ship_country - (see L<FS::cust_main_county>)
179 =item ship_daytime - phone (optional)
181 =item ship_night - phone (optional)
183 =item ship_fax - phone (optional)
187 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>)
191 Card Number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
196 my($self,$payinfo) = @_;
197 if ( defined($payinfo) ) {
198 $self->paymask($payinfo);
199 $self->setfield('payinfo', $payinfo); # This is okay since we are the 'setter'
201 $payinfo = $self->getfield('payinfo'); # This is okay since we are the 'getter'
209 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
213 =item paymask - Masked payment type
219 Mask all but the last four characters.
223 Mask all but last 2 of account number and bank routing number.
227 Do nothing, return the unmasked string.
236 # If it doesn't exist then generate it
237 my $paymask=$self->getfield('paymask');
238 if (!defined($value) && (!defined($paymask) || $paymask eq '')) {
239 $value = $self->payinfo;
242 if ( defined($value) && !$self->is_encrypted($value)) {
243 my $payinfo = $value;
244 my $payby = $self->payby;
245 if ($payby eq 'CARD' || $payby eq 'DCARD') { # Credit Cards (Show last four)
246 $paymask = 'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
247 } elsif ($payby eq 'CHEK' ||
248 $payby eq 'DCHK' ) { # Checks (Show last 2 @ bank)
249 my( $account, $aba ) = split('@', $payinfo );
250 $paymask = 'x'x(length($account)-2). substr($account,(length($account)-2))."@".$aba;
251 } else { # Tie up loose ends
254 $self->setfield('paymask', $paymask); # This is okay since we are the 'setter'
255 } elsif (defined($value) && $self->is_encrypted($value)) {
261 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
263 =item paystart_month - start date month (maestro/solo cards only)
265 =item paystart_year - start date year (maestro/solo cards only)
267 =item payissue - issue number (maestro/solo cards only)
269 =item payname - name on card or billing name
271 =item payip - IP address from which payment information was received
273 =item tax - tax exempt, empty or `Y'
275 =item otaker - order taker (assigned automatically, see L<FS::UID>)
277 =item comments - comments (optional)
279 =item referral_custnum - referring customer number
289 Creates a new customer. To add the customer to the database, see L<"insert">.
291 Note that this stores the hash reference, not a distinct copy of the hash it
292 points to. You can ask the object for a copy with the I<hash> method.
296 sub table { 'cust_main'; }
298 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
300 Adds this customer to the database. If there is an error, returns the error,
301 otherwise returns false.
303 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
304 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
305 are inserted atomicly, or the transaction is rolled back. Passing an empty
306 hash reference is equivalent to not supplying this parameter. There should be
307 a better explanation of this, but until then, here's an example:
310 tie %hash, 'Tie::RefHash'; #this part is important
312 $cust_pkg => [ $svc_acct ],
315 $cust_main->insert( \%hash );
317 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
318 be set as the invoicing list (see L<"invoicing_list">). Errors return as
319 expected and rollback the entire transaction; it is not necessary to call
320 check_invoicing_list first. The invoicing_list is set after the records in the
321 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
322 invoicing_list destination to the newly-created svc_acct. Here's an example:
324 $cust_main->insert( {}, [ $email, 'POST' ] );
326 Currently available options are: I<depend_jobnum> and I<noexport>.
328 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
329 on the supplied jobnum (they will not run until the specific job completes).
330 This can be used to defer provisioning until some action completes (such
331 as running the customer's credit card sucessfully).
333 The I<noexport> option is deprecated. If I<noexport> is set true, no
334 provisioning jobs (exports) are scheduled. (You can schedule them later with
335 the B<reexport> method.)
341 my $cust_pkgs = @_ ? shift : {};
342 my $invoicing_list = @_ ? shift : '';
344 warn "FS::cust_main::insert called with options ".
345 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
348 local $SIG{HUP} = 'IGNORE';
349 local $SIG{INT} = 'IGNORE';
350 local $SIG{QUIT} = 'IGNORE';
351 local $SIG{TERM} = 'IGNORE';
352 local $SIG{TSTP} = 'IGNORE';
353 local $SIG{PIPE} = 'IGNORE';
355 my $oldAutoCommit = $FS::UID::AutoCommit;
356 local $FS::UID::AutoCommit = 0;
359 my $prepay_identifier = '';
360 my( $amount, $seconds ) = ( 0, 0 );
361 if ( $self->payby eq 'PREPAY' ) {
363 $self->payby('BILL');
364 $prepay_identifier = $self->payinfo;
367 my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
369 $dbh->rollback if $oldAutoCommit;
370 #return "error applying prepaid card (transaction rolled back): $error";
376 my $error = $self->SUPER::insert;
378 $dbh->rollback if $oldAutoCommit;
379 #return "inserting cust_main record (transaction rolled back): $error";
384 if ( $invoicing_list ) {
385 $error = $self->check_invoicing_list( $invoicing_list );
387 $dbh->rollback if $oldAutoCommit;
388 return "checking invoicing_list (transaction rolled back): $error";
390 $self->invoicing_list( $invoicing_list );
394 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
396 $dbh->rollback if $oldAutoCommit;
401 $dbh->rollback if $oldAutoCommit;
402 return "No svc_acct record to apply pre-paid time";
406 $error = $self->insert_cust_pay_prepay($amount, $prepay_identifier);
408 $dbh->rollback if $oldAutoCommit;
409 return "inserting prepayment (transaction rolled back): $error";
413 unless ( $import || $skip_fuzzyfiles ) {
414 $error = $self->queue_fuzzyfiles_update;
416 $dbh->rollback if $oldAutoCommit;
417 return "updating fuzzy search cache: $error";
421 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
426 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
428 Like the insert method on an existing record, this method orders a package
429 and included services atomicaly. Pass a Tie::RefHash data structure to this
430 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
431 be a better explanation of this, but until then, here's an example:
434 tie %hash, 'Tie::RefHash'; #this part is important
436 $cust_pkg => [ $svc_acct ],
439 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
441 Services can be new, in which case they are inserted, or existing unaudited
442 services, in which case they are linked to the newly-created package.
444 Currently available options are: I<depend_jobnum> and I<noexport>.
446 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
447 on the supplied jobnum (they will not run until the specific job completes).
448 This can be used to defer provisioning until some action completes (such
449 as running the customer's credit card sucessfully).
451 The I<noexport> option is deprecated. If I<noexport> is set true, no
452 provisioning jobs (exports) are scheduled. (You can schedule them later with
453 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
454 on the cust_main object is not recommended, as existing services will also be
461 my $cust_pkgs = shift;
464 my %svc_options = ();
465 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
466 if exists $options{'depend_jobnum'};
467 warn "FS::cust_main::order_pkgs called with options ".
468 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
471 local $SIG{HUP} = 'IGNORE';
472 local $SIG{INT} = 'IGNORE';
473 local $SIG{QUIT} = 'IGNORE';
474 local $SIG{TERM} = 'IGNORE';
475 local $SIG{TSTP} = 'IGNORE';
476 local $SIG{PIPE} = 'IGNORE';
478 my $oldAutoCommit = $FS::UID::AutoCommit;
479 local $FS::UID::AutoCommit = 0;
482 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
484 foreach my $cust_pkg ( keys %$cust_pkgs ) {
485 $cust_pkg->custnum( $self->custnum );
486 my $error = $cust_pkg->insert;
488 $dbh->rollback if $oldAutoCommit;
489 return "inserting cust_pkg (transaction rolled back): $error";
491 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
492 if ( $svc_something->svcnum ) {
493 my $old_cust_svc = $svc_something->cust_svc;
494 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
495 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
496 $error = $new_cust_svc->replace($old_cust_svc);
498 $svc_something->pkgnum( $cust_pkg->pkgnum );
499 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
500 $svc_something->seconds( $svc_something->seconds + $$seconds );
503 $error = $svc_something->insert(%svc_options);
506 $dbh->rollback if $oldAutoCommit;
507 #return "inserting svc_ (transaction rolled back): $error";
513 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
517 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF ]
519 Recharges this (existing) customer with the specified prepaid card (see
520 L<FS::prepay_credit>), specified either by I<identifier> or as an
521 FS::prepay_credit object. If there is an error, returns the error, otherwise
524 Optionally, two scalar references can be passed as well. They will have their
525 values filled in with the amount and number of seconds applied by this prepaid
530 sub recharge_prepay {
531 my( $self, $prepay_credit, $amountref, $secondsref ) = @_;
533 local $SIG{HUP} = 'IGNORE';
534 local $SIG{INT} = 'IGNORE';
535 local $SIG{QUIT} = 'IGNORE';
536 local $SIG{TERM} = 'IGNORE';
537 local $SIG{TSTP} = 'IGNORE';
538 local $SIG{PIPE} = 'IGNORE';
540 my $oldAutoCommit = $FS::UID::AutoCommit;
541 local $FS::UID::AutoCommit = 0;
544 my( $amount, $seconds ) = ( 0, 0 );
546 my $error = $self->get_prepay($prepay_credit, \$amount, \$seconds)
547 || $self->increment_seconds($seconds)
548 || $self->insert_cust_pay_prepay( $amount,
550 ? $prepay_credit->identifier
555 $dbh->rollback if $oldAutoCommit;
559 if ( defined($amountref) ) { $$amountref = $amount; }
560 if ( defined($secondsref) ) { $$secondsref = $seconds; }
562 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
567 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
569 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
570 specified either by I<identifier> or as an FS::prepay_credit object.
572 References to I<amount> and I<seconds> scalars should be passed as arguments
573 and will be incremented by the values of the prepaid card.
575 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
576 check or set this customer's I<agentnum>.
578 If there is an error, returns the error, otherwise returns false.
584 my( $self, $prepay_credit, $amountref, $secondsref ) = @_;
586 local $SIG{HUP} = 'IGNORE';
587 local $SIG{INT} = 'IGNORE';
588 local $SIG{QUIT} = 'IGNORE';
589 local $SIG{TERM} = 'IGNORE';
590 local $SIG{TSTP} = 'IGNORE';
591 local $SIG{PIPE} = 'IGNORE';
593 my $oldAutoCommit = $FS::UID::AutoCommit;
594 local $FS::UID::AutoCommit = 0;
597 unless ( ref($prepay_credit) ) {
599 my $identifier = $prepay_credit;
601 $prepay_credit = qsearchs(
603 { 'identifier' => $prepay_credit },
608 unless ( $prepay_credit ) {
609 $dbh->rollback if $oldAutoCommit;
610 return "Invalid prepaid card: ". $identifier;
615 if ( $prepay_credit->agentnum ) {
616 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
617 $dbh->rollback if $oldAutoCommit;
618 return "prepaid card not valid for agent ". $self->agentnum;
620 $self->agentnum($prepay_credit->agentnum);
623 my $error = $prepay_credit->delete;
625 $dbh->rollback if $oldAutoCommit;
626 return "removing prepay_credit (transaction rolled back): $error";
629 $$amountref += $prepay_credit->amount;
630 $$secondsref += $prepay_credit->seconds;
632 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
637 =item increment_seconds SECONDS
639 Updates this customer's single or primary account (see L<FS::svc_acct>) by
640 the specified number of seconds. If there is an error, returns the error,
641 otherwise returns false.
645 sub increment_seconds {
646 my( $self, $seconds ) = @_;
647 warn "$me increment_seconds called: $seconds seconds\n"
650 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
651 $self->ncancelled_pkgs;
654 return 'No packages with primary or single services found'.
655 ' to apply pre-paid time';
656 } elsif ( scalar(@cust_pkg) > 1 ) {
657 #maybe have a way to specify the package/account?
658 return 'Multiple packages found to apply pre-paid time';
661 my $cust_pkg = $cust_pkg[0];
662 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
666 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
669 return 'No account found to apply pre-paid time';
670 } elsif ( scalar(@cust_svc) > 1 ) {
671 return 'Multiple accounts found to apply pre-paid time';
674 my $svc_acct = $cust_svc[0]->svc_x;
675 warn " found service svcnum ". $svc_acct->pkgnum.
676 ' ('. $svc_acct->email. ")\n"
679 $svc_acct->increment_seconds($seconds);
683 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
685 Inserts a prepayment in the specified amount for this customer. An optional
686 second argument can specify the prepayment identifier for tracking purposes.
687 If there is an error, returns the error, otherwise returns false.
691 sub insert_cust_pay_prepay {
692 my( $self, $amount ) = splice(@_, 0, 2);
693 my $payinfo = scalar(@_) ? shift : '';
695 my $cust_pay = new FS::cust_pay {
696 'custnum' => $self->custnum,
697 'paid' => sprintf('%.2f', $amount),
698 #'_date' => #date the prepaid card was purchased???
700 'payinfo' => $payinfo,
708 This method is deprecated. See the I<depend_jobnum> option to the insert and
709 order_pkgs methods for a better way to defer provisioning.
711 Re-schedules all exports by calling the B<reexport> method of all associated
712 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
713 otherwise returns false.
720 carp "warning: FS::cust_main::reexport is deprectated; ".
721 "use the depend_jobnum option to insert or order_pkgs to delay export";
723 local $SIG{HUP} = 'IGNORE';
724 local $SIG{INT} = 'IGNORE';
725 local $SIG{QUIT} = 'IGNORE';
726 local $SIG{TERM} = 'IGNORE';
727 local $SIG{TSTP} = 'IGNORE';
728 local $SIG{PIPE} = 'IGNORE';
730 my $oldAutoCommit = $FS::UID::AutoCommit;
731 local $FS::UID::AutoCommit = 0;
734 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
735 my $error = $cust_pkg->reexport;
737 $dbh->rollback if $oldAutoCommit;
742 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
747 =item delete NEW_CUSTNUM
749 This deletes the customer. If there is an error, returns the error, otherwise
752 This will completely remove all traces of the customer record. This is not
753 what you want when a customer cancels service; for that, cancel all of the
754 customer's packages (see L</cancel>).
756 If the customer has any uncancelled packages, you need to pass a new (valid)
757 customer number for those packages to be transferred to. Cancelled packages
758 will be deleted. Did I mention that this is NOT what you want when a customer
759 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
761 You can't delete a customer with invoices (see L<FS::cust_bill>),
762 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
763 refunds (see L<FS::cust_refund>).
770 local $SIG{HUP} = 'IGNORE';
771 local $SIG{INT} = 'IGNORE';
772 local $SIG{QUIT} = 'IGNORE';
773 local $SIG{TERM} = 'IGNORE';
774 local $SIG{TSTP} = 'IGNORE';
775 local $SIG{PIPE} = 'IGNORE';
777 my $oldAutoCommit = $FS::UID::AutoCommit;
778 local $FS::UID::AutoCommit = 0;
781 if ( $self->cust_bill ) {
782 $dbh->rollback if $oldAutoCommit;
783 return "Can't delete a customer with invoices";
785 if ( $self->cust_credit ) {
786 $dbh->rollback if $oldAutoCommit;
787 return "Can't delete a customer with credits";
789 if ( $self->cust_pay ) {
790 $dbh->rollback if $oldAutoCommit;
791 return "Can't delete a customer with payments";
793 if ( $self->cust_refund ) {
794 $dbh->rollback if $oldAutoCommit;
795 return "Can't delete a customer with refunds";
798 my @cust_pkg = $self->ncancelled_pkgs;
800 my $new_custnum = shift;
801 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
802 $dbh->rollback if $oldAutoCommit;
803 return "Invalid new customer number: $new_custnum";
805 foreach my $cust_pkg ( @cust_pkg ) {
806 my %hash = $cust_pkg->hash;
807 $hash{'custnum'} = $new_custnum;
808 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
809 my $error = $new_cust_pkg->replace($cust_pkg);
811 $dbh->rollback if $oldAutoCommit;
816 my @cancelled_cust_pkg = $self->all_pkgs;
817 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
818 my $error = $cust_pkg->delete;
820 $dbh->rollback if $oldAutoCommit;
825 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
826 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
828 my $error = $cust_main_invoice->delete;
830 $dbh->rollback if $oldAutoCommit;
835 my $error = $self->SUPER::delete;
837 $dbh->rollback if $oldAutoCommit;
841 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
846 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
848 Replaces the OLD_RECORD with this one in the database. If there is an error,
849 returns the error, otherwise returns false.
851 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
852 be set as the invoicing list (see L<"invoicing_list">). Errors return as
853 expected and rollback the entire transaction; it is not necessary to call
854 check_invoicing_list first. Here's an example:
856 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
865 local $SIG{HUP} = 'IGNORE';
866 local $SIG{INT} = 'IGNORE';
867 local $SIG{QUIT} = 'IGNORE';
868 local $SIG{TERM} = 'IGNORE';
869 local $SIG{TSTP} = 'IGNORE';
870 local $SIG{PIPE} = 'IGNORE';
872 # If the mask is blank then try to set it - if we can...
873 if (!defined($self->getfield('paymask')) || $self->getfield('paymask') eq '') {
874 $self->paymask($self->payinfo);
877 # We absolutely have to have an old vs. new record to make this work.
878 if (!defined($old)) {
879 $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
882 if ( $self->payby eq 'COMP' && $self->payby ne $old->payby
883 && $conf->config('users-allow_comp') ) {
884 return "You are not permitted to create complimentary accounts."
885 unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
888 my $oldAutoCommit = $FS::UID::AutoCommit;
889 local $FS::UID::AutoCommit = 0;
892 my $error = $self->SUPER::replace($old);
895 $dbh->rollback if $oldAutoCommit;
899 if ( @param ) { # INVOICING_LIST_ARYREF
900 my $invoicing_list = shift @param;
901 $error = $self->check_invoicing_list( $invoicing_list );
903 $dbh->rollback if $oldAutoCommit;
906 $self->invoicing_list( $invoicing_list );
909 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
910 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
911 # card/check/lec info has changed, want to retry realtime_ invoice events
912 my $error = $self->retry_realtime;
914 $dbh->rollback if $oldAutoCommit;
919 unless ( $import || $skip_fuzzyfiles ) {
920 $error = $self->queue_fuzzyfiles_update;
922 $dbh->rollback if $oldAutoCommit;
923 return "updating fuzzy search cache: $error";
927 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
932 =item queue_fuzzyfiles_update
934 Used by insert & replace to update the fuzzy search cache
938 sub queue_fuzzyfiles_update {
941 local $SIG{HUP} = 'IGNORE';
942 local $SIG{INT} = 'IGNORE';
943 local $SIG{QUIT} = 'IGNORE';
944 local $SIG{TERM} = 'IGNORE';
945 local $SIG{TSTP} = 'IGNORE';
946 local $SIG{PIPE} = 'IGNORE';
948 my $oldAutoCommit = $FS::UID::AutoCommit;
949 local $FS::UID::AutoCommit = 0;
952 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
953 my $error = $queue->insert($self->getfield('last'), $self->company);
955 $dbh->rollback if $oldAutoCommit;
956 return "queueing job (transaction rolled back): $error";
959 if ( defined $self->dbdef_table->column('ship_last') && $self->ship_last ) {
960 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
961 $error = $queue->insert($self->getfield('ship_last'), $self->ship_company);
963 $dbh->rollback if $oldAutoCommit;
964 return "queueing job (transaction rolled back): $error";
968 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
975 Checks all fields to make sure this is a valid customer record. If there is
976 an error, returns the error, otherwise returns false. Called by the insert
984 #warn "BEFORE: \n". $self->_dump;
987 $self->ut_numbern('custnum')
988 || $self->ut_number('agentnum')
989 || $self->ut_number('refnum')
990 || $self->ut_name('last')
991 || $self->ut_name('first')
992 || $self->ut_textn('company')
993 || $self->ut_text('address1')
994 || $self->ut_textn('address2')
995 || $self->ut_text('city')
996 || $self->ut_textn('county')
997 || $self->ut_textn('state')
998 || $self->ut_country('country')
999 || $self->ut_anything('comments')
1000 || $self->ut_numbern('referral_custnum')
1002 #barf. need message catalogs. i18n. etc.
1003 $error .= "Please select an advertising source."
1004 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1005 return $error if $error;
1007 return "Unknown agent"
1008 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1010 return "Unknown refnum"
1011 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1013 return "Unknown referring custnum: ". $self->referral_custnum
1014 unless ! $self->referral_custnum
1015 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1017 if ( $self->ss eq '' ) {
1022 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1023 or return "Illegal social security number: ". $self->ss;
1024 $self->ss("$1-$2-$3");
1028 # bad idea to disable, causes billing to fail because of no tax rates later
1029 # unless ( $import ) {
1030 unless ( qsearch('cust_main_county', {
1031 'country' => $self->country,
1034 return "Unknown state/county/country: ".
1035 $self->state. "/". $self->county. "/". $self->country
1036 unless qsearch('cust_main_county',{
1037 'state' => $self->state,
1038 'county' => $self->county,
1039 'country' => $self->country,
1045 $self->ut_phonen('daytime', $self->country)
1046 || $self->ut_phonen('night', $self->country)
1047 || $self->ut_phonen('fax', $self->country)
1048 || $self->ut_zip('zip', $self->country)
1050 return $error if $error;
1053 last first company address1 address2 city county state zip
1054 country daytime night fax
1057 if ( defined $self->dbdef_table->column('ship_last') ) {
1058 if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1060 && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
1064 $self->ut_name('ship_last')
1065 || $self->ut_name('ship_first')
1066 || $self->ut_textn('ship_company')
1067 || $self->ut_text('ship_address1')
1068 || $self->ut_textn('ship_address2')
1069 || $self->ut_text('ship_city')
1070 || $self->ut_textn('ship_county')
1071 || $self->ut_textn('ship_state')
1072 || $self->ut_country('ship_country')
1074 return $error if $error;
1076 #false laziness with above
1077 unless ( qsearchs('cust_main_county', {
1078 'country' => $self->ship_country,
1081 return "Unknown ship_state/ship_county/ship_country: ".
1082 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1083 unless qsearchs('cust_main_county',{
1084 'state' => $self->ship_state,
1085 'county' => $self->ship_county,
1086 'country' => $self->ship_country,
1092 $self->ut_phonen('ship_daytime', $self->ship_country)
1093 || $self->ut_phonen('ship_night', $self->ship_country)
1094 || $self->ut_phonen('ship_fax', $self->ship_country)
1095 || $self->ut_zip('ship_zip', $self->ship_country)
1097 return $error if $error;
1099 } else { # ship_ info eq billing info, so don't store dup info in database
1100 $self->setfield("ship_$_", '')
1101 foreach qw( last first company address1 address2 city county state zip
1102 country daytime night fax );
1106 $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY)$/
1107 or return "Illegal payby: ". $self->payby;
1109 $error = $self->ut_numbern('paystart_month')
1110 || $self->ut_numbern('paystart_year')
1111 || $self->ut_numbern('payissue')
1113 return $error if $error;
1115 if ( $self->payip eq '' ) {
1118 $error = $self->ut_ip('payip');
1119 return $error if $error;
1122 # If it is encrypted and the private key is not availaible then we can't
1123 # check the credit card.
1125 my $check_payinfo = 1;
1127 if ($self->is_encrypted($self->payinfo)) {
1133 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1135 my $payinfo = $self->payinfo;
1136 $payinfo =~ s/\D//g;
1137 $payinfo =~ /^(\d{13,16})$/
1138 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1140 $self->payinfo($payinfo);
1142 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1143 return gettext('unknown_card_type')
1144 if cardtype($self->payinfo) eq "Unknown";
1145 if ( defined $self->dbdef_table->column('paycvv') ) {
1146 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1147 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1148 $self->paycvv =~ /^(\d{4})$/
1149 or return "CVV2 (CID) for American Express cards is four digits.";
1152 $self->paycvv =~ /^(\d{3})$/
1153 or return "CVV2 (CVC2/CID) is three digits.";
1161 my $cardtype = cardtype($payinfo);
1162 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1164 return "Start date or issue number is required for $cardtype cards"
1165 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1167 return "Start month must be between 1 and 12"
1168 if $self->paystart_month
1169 and $self->paystart_month < 1 || $self->paystart_month > 12;
1171 return "Start year must be 1990 or later"
1172 if $self->paystart_year
1173 and $self->paystart_year < 1990;
1175 return "Issue number must be beween 1 and 99"
1177 and $self->payissue < 1 || $self->payissue > 99;
1180 $self->paystart_month('');
1181 $self->paystart_year('');
1182 $self->payissue('');
1185 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1187 my $payinfo = $self->payinfo;
1188 $payinfo =~ s/[^\d\@]//g;
1189 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1190 $payinfo = "$1\@$2";
1191 $self->payinfo($payinfo);
1192 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1194 } elsif ( $self->payby eq 'LECB' ) {
1196 my $payinfo = $self->payinfo;
1197 $payinfo =~ s/\D//g;
1198 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1200 $self->payinfo($payinfo);
1201 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1203 } elsif ( $self->payby eq 'BILL' ) {
1205 $error = $self->ut_textn('payinfo');
1206 return "Illegal P.O. number: ". $self->payinfo if $error;
1207 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1209 } elsif ( $self->payby eq 'COMP' ) {
1211 if ( !$self->custnum && $conf->config('users-allow_comp') ) {
1212 return "You are not permitted to create complimentary accounts."
1213 unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
1216 $error = $self->ut_textn('payinfo');
1217 return "Illegal comp account issuer: ". $self->payinfo if $error;
1218 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1220 } elsif ( $self->payby eq 'PREPAY' ) {
1222 my $payinfo = $self->payinfo;
1223 $payinfo =~ s/\W//g; #anything else would just confuse things
1224 $self->payinfo($payinfo);
1225 $error = $self->ut_alpha('payinfo');
1226 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1227 return "Unknown prepayment identifier"
1228 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1229 $self->paycvv('') if $self->dbdef_table->column('paycvv');
1233 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1234 return "Expriation date required"
1235 unless $self->payby =~ /^(BILL|PREPAY|CHEK|LECB)$/;
1239 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1240 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1241 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1242 ( $m, $y ) = ( $3, "20$2" );
1244 return "Illegal expiration date: ". $self->paydate;
1246 $self->paydate("$y-$m-01");
1247 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1248 return gettext('expired_card')
1249 if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1252 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1253 ( ! $conf->exists('require_cardname')
1254 || $self->payby !~ /^(CARD|DCRD)$/ )
1256 $self->payname( $self->first. " ". $self->getfield('last') );
1258 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1259 or return gettext('illegal_name'). " payname: ". $self->payname;
1263 $self->tax =~ /^(Y?)$/ or return "Illegal tax: ". $self->tax;
1266 $self->otaker(getotaker) unless $self->otaker;
1268 #warn "AFTER: \n". $self->_dump;
1270 $self->SUPER::check;
1275 Returns all packages (see L<FS::cust_pkg>) for this customer.
1281 if ( $self->{'_pkgnum'} ) {
1282 values %{ $self->{'_pkgnum'}->cache };
1284 qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1288 =item ncancelled_pkgs
1290 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1294 sub ncancelled_pkgs {
1296 if ( $self->{'_pkgnum'} ) {
1297 grep { ! $_->getfield('cancel') } values %{ $self->{'_pkgnum'}->cache };
1299 @{ [ # force list context
1300 qsearch( 'cust_pkg', {
1301 'custnum' => $self->custnum,
1304 qsearch( 'cust_pkg', {
1305 'custnum' => $self->custnum,
1312 =item suspended_pkgs
1314 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1318 sub suspended_pkgs {
1320 grep { $_->susp } $self->ncancelled_pkgs;
1323 =item unflagged_suspended_pkgs
1325 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1326 customer (thouse packages without the `manual_flag' set).
1330 sub unflagged_suspended_pkgs {
1332 return $self->suspended_pkgs
1333 unless dbdef->table('cust_pkg')->column('manual_flag');
1334 grep { ! $_->manual_flag } $self->suspended_pkgs;
1337 =item unsuspended_pkgs
1339 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1344 sub unsuspended_pkgs {
1346 grep { ! $_->susp } $self->ncancelled_pkgs;
1349 =item num_cancelled_pkgs
1351 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1356 sub num_cancelled_pkgs {
1358 $self->num_pkgs("cancel IS NOT NULL AND cust_pkg.cancel != 0");
1362 my( $self, $sql ) = @_;
1363 my $sth = dbh->prepare(
1364 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? AND $sql"
1365 ) or die dbh->errstr;
1366 $sth->execute($self->custnum) or die $sth->errstr;
1367 $sth->fetchrow_arrayref->[0];
1372 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1373 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1374 on success or a list of errors.
1380 grep { $_->unsuspend } $self->suspended_pkgs;
1385 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1386 Always returns a list: an empty list on success or a list of errors.
1392 grep { $_->suspend } $self->unsuspended_pkgs;
1395 =item suspend_if_pkgpart PKGPART [ , PKGPART ... ]
1397 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1398 PKGPARTs (see L<FS::part_pkg>). Always returns a list: an empty list on
1399 success or a list of errors.
1403 sub suspend_if_pkgpart {
1406 grep { $_->suspend }
1407 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1408 $self->unsuspended_pkgs;
1411 =item suspend_unless_pkgpart PKGPART [ , PKGPART ... ]
1413 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1414 listed PKGPARTs (see L<FS::part_pkg>). Always returns a list: an empty list
1415 on success or a list of errors.
1419 sub suspend_unless_pkgpart {
1422 grep { $_->suspend }
1423 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1424 $self->unsuspended_pkgs;
1427 =item cancel [ OPTION => VALUE ... ]
1429 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1431 Available options are: I<quiet>
1433 I<quiet> can be set true to supress email cancellation notices.
1435 Always returns a list: an empty list on success or a list of errors.
1441 grep { $_ } map { $_->cancel(@_) } $self->ncancelled_pkgs;
1446 Returns the agent (see L<FS::agent>) for this customer.
1452 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1457 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
1458 conjunction with the collect method.
1460 Options are passed as name-value pairs.
1462 Currently available options are:
1464 resetup - if set true, re-charges setup fees.
1466 time - bills the customer as if it were that time. Specified as a UNIX
1467 timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and
1468 L<Date::Parse> for conversion functions. For example:
1472 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1475 If there is an error, returns the error, otherwise returns false.
1480 my( $self, %options ) = @_;
1481 return '' if $self->payby eq 'COMP';
1482 warn "bill customer ". $self->custnum. "\n" if $DEBUG;
1484 my $time = $options{'time'} || time;
1489 local $SIG{HUP} = 'IGNORE';
1490 local $SIG{INT} = 'IGNORE';
1491 local $SIG{QUIT} = 'IGNORE';
1492 local $SIG{TERM} = 'IGNORE';
1493 local $SIG{TSTP} = 'IGNORE';
1494 local $SIG{PIPE} = 'IGNORE';
1496 my $oldAutoCommit = $FS::UID::AutoCommit;
1497 local $FS::UID::AutoCommit = 0;
1500 $self->select_for_update; #mutex
1502 # find the packages which are due for billing, find out how much they are
1503 # & generate invoice database.
1505 my( $total_setup, $total_recur ) = ( 0, 0 );
1506 #my( $taxable_setup, $taxable_recur ) = ( 0, 0 );
1507 my @cust_bill_pkg = ();
1509 #my $taxable_charged = 0;##
1514 foreach my $cust_pkg (
1515 qsearch('cust_pkg', { 'custnum' => $self->custnum } )
1518 #NO!! next if $cust_pkg->cancel;
1519 next if $cust_pkg->getfield('cancel');
1521 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG;
1523 #? to avoid use of uninitialized value errors... ?
1524 $cust_pkg->setfield('bill', '')
1525 unless defined($cust_pkg->bill);
1527 my $part_pkg = $cust_pkg->part_pkg;
1529 my %hash = $cust_pkg->hash;
1530 my $old_cust_pkg = new FS::cust_pkg \%hash;
1536 if ( !$cust_pkg->setup || $options{'resetup'} ) {
1538 warn " bill setup\n" if $DEBUG;
1540 $setup = eval { $cust_pkg->calc_setup( $time ) };
1542 $dbh->rollback if $oldAutoCommit;
1546 $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
1552 if ( $part_pkg->getfield('freq') ne '0' &&
1553 ! $cust_pkg->getfield('susp') &&
1554 ( $cust_pkg->getfield('bill') || 0 ) <= $time
1557 warn " bill recur\n" if $DEBUG;
1559 # XXX shared with $recur_prog
1560 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1562 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details ) };
1564 $dbh->rollback if $oldAutoCommit;
1568 #change this bit to use Date::Manip? CAREFUL with timezones (see
1569 # mailing list archive)
1570 my ($sec,$min,$hour,$mday,$mon,$year) =
1571 (localtime($sdate) )[0,1,2,3,4,5];
1573 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
1574 # only for figuring next bill date, nothing else, so, reset $sdate again
1576 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1577 $cust_pkg->last_bill($sdate)
1578 if $cust_pkg->dbdef_table->column('last_bill');
1580 if ( $part_pkg->freq =~ /^\d+$/ ) {
1581 $mon += $part_pkg->freq;
1582 until ( $mon < 12 ) { $mon -= 12; $year++; }
1583 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
1585 $mday += $weeks * 7;
1586 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
1590 $dbh->rollback if $oldAutoCommit;
1591 return "unparsable frequency: ". $part_pkg->freq;
1593 $cust_pkg->setfield('bill',
1594 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
1597 warn "\$setup is undefined" unless defined($setup);
1598 warn "\$recur is undefined" unless defined($recur);
1599 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1601 if ( $cust_pkg->modified ) {
1603 warn " package ". $cust_pkg->pkgnum. " modified; updating\n" if $DEBUG;
1605 $error=$cust_pkg->replace($old_cust_pkg);
1606 if ( $error ) { #just in case
1607 $dbh->rollback if $oldAutoCommit;
1608 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
1611 $setup = sprintf( "%.2f", $setup );
1612 $recur = sprintf( "%.2f", $recur );
1613 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
1614 $dbh->rollback if $oldAutoCommit;
1615 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1617 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
1618 $dbh->rollback if $oldAutoCommit;
1619 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1621 if ( $setup != 0 || $recur != 0 ) {
1622 warn " charges (setup=$setup, recur=$recur); queueing line items\n"
1624 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1625 'pkgnum' => $cust_pkg->pkgnum,
1629 'edate' => $cust_pkg->bill,
1630 'details' => \@details,
1632 push @cust_bill_pkg, $cust_bill_pkg;
1633 $total_setup += $setup;
1634 $total_recur += $recur;
1636 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
1638 my @taxes = qsearch( 'cust_main_county', {
1639 'state' => $self->state,
1640 'county' => $self->county,
1641 'country' => $self->country,
1642 'taxclass' => $part_pkg->taxclass,
1645 @taxes = qsearch( 'cust_main_county', {
1646 'state' => $self->state,
1647 'county' => $self->county,
1648 'country' => $self->country,
1653 #one more try at a whole-country tax rate
1655 @taxes = qsearch( 'cust_main_county', {
1658 'country' => $self->country,
1663 # maybe eliminate this entirely, along with all the 0% records
1665 $dbh->rollback if $oldAutoCommit;
1667 "fatal: can't find tax rate for state/county/country/taxclass ".
1668 join('/', ( map $self->$_(), qw(state county country) ),
1669 $part_pkg->taxclass ). "\n";
1672 foreach my $tax ( @taxes ) {
1674 my $taxable_charged = 0;
1675 $taxable_charged += $setup
1676 unless $part_pkg->setuptax =~ /^Y$/i
1677 || $tax->setuptax =~ /^Y$/i;
1678 $taxable_charged += $recur
1679 unless $part_pkg->recurtax =~ /^Y$/i
1680 || $tax->recurtax =~ /^Y$/i;
1681 next unless $taxable_charged;
1683 if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
1684 my ($mon,$year) = (localtime($sdate) )[4,5];
1686 my $freq = $part_pkg->freq || 1;
1687 if ( $freq !~ /(\d+)$/ ) {
1688 $dbh->rollback if $oldAutoCommit;
1689 return "daily/weekly package definitions not (yet?)".
1690 " compatible with monthly tax exemptions";
1692 my $taxable_per_month = sprintf("%.2f", $taxable_charged / $freq );
1693 foreach my $which_month ( 1 .. $freq ) {
1695 'custnum' => $self->custnum,
1696 'taxnum' => $tax->taxnum,
1697 'year' => 1900+$year,
1700 #until ( $mon < 12 ) { $mon -= 12; $year++; }
1701 until ( $mon < 13 ) { $mon -= 12; $year++; }
1702 my $cust_tax_exempt =
1703 qsearchs('cust_tax_exempt', \%hash)
1704 || new FS::cust_tax_exempt( { %hash, 'amount' => 0 } );
1705 my $remaining_exemption = sprintf("%.2f",
1706 $tax->exempt_amount - $cust_tax_exempt->amount );
1707 if ( $remaining_exemption > 0 ) {
1708 my $addl = $remaining_exemption > $taxable_per_month
1709 ? $taxable_per_month
1710 : $remaining_exemption;
1711 $taxable_charged -= $addl;
1712 my $new_cust_tax_exempt = new FS::cust_tax_exempt ( {
1713 $cust_tax_exempt->hash,
1715 sprintf("%.2f", $cust_tax_exempt->amount + $addl),
1717 $error = $new_cust_tax_exempt->exemptnum
1718 ? $new_cust_tax_exempt->replace($cust_tax_exempt)
1719 : $new_cust_tax_exempt->insert;
1721 $dbh->rollback if $oldAutoCommit;
1722 return "fatal: can't update cust_tax_exempt: $error";
1725 } # if $remaining_exemption > 0
1727 } #foreach $which_month
1729 } #if $tax->exempt_amount
1731 $taxable_charged = sprintf( "%.2f", $taxable_charged);
1733 #$tax += $taxable_charged * $cust_main_county->tax / 100
1734 $tax{ $tax->taxname || 'Tax' } +=
1735 $taxable_charged * $tax->tax / 100
1737 } #foreach my $tax ( @taxes )
1739 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
1741 } #if $setup != 0 || $recur != 0
1743 } #if $cust_pkg->modified
1745 } #foreach my $cust_pkg
1747 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
1748 # my $taxable_charged = sprintf( "%.2f", $taxable_setup + $taxable_recur );
1750 unless ( @cust_bill_pkg ) { #don't create invoices with no line items
1751 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1755 # unless ( $self->tax =~ /Y/i
1756 # || $self->payby eq 'COMP'
1757 # || $taxable_charged == 0 ) {
1758 # my $cust_main_county = qsearchs('cust_main_county',{
1759 # 'state' => $self->state,
1760 # 'county' => $self->county,
1761 # 'country' => $self->country,
1762 # } ) or die "fatal: can't find tax rate for state/county/country ".
1763 # $self->state. "/". $self->county. "/". $self->country. "\n";
1764 # my $tax = sprintf( "%.2f",
1765 # $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
1768 if ( dbdef->table('cust_bill_pkg')->column('itemdesc') ) { #1.5 schema
1770 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
1771 my $tax = sprintf("%.2f", $tax{$taxname} );
1772 $charged = sprintf( "%.2f", $charged+$tax );
1774 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1780 'itemdesc' => $taxname,
1782 push @cust_bill_pkg, $cust_bill_pkg;
1785 } else { #1.4 schema
1788 foreach ( values %tax ) { $tax += $_ };
1789 $tax = sprintf("%.2f", $tax);
1791 $charged = sprintf( "%.2f", $charged+$tax );
1793 my $cust_bill_pkg = new FS::cust_bill_pkg ({
1800 push @cust_bill_pkg, $cust_bill_pkg;
1805 my $cust_bill = new FS::cust_bill ( {
1806 'custnum' => $self->custnum,
1808 'charged' => $charged,
1810 $error = $cust_bill->insert;
1812 $dbh->rollback if $oldAutoCommit;
1813 return "can't create invoice for customer #". $self->custnum. ": $error";
1816 my $invnum = $cust_bill->invnum;
1818 foreach $cust_bill_pkg ( @cust_bill_pkg ) {
1820 $cust_bill_pkg->invnum($invnum);
1821 $error = $cust_bill_pkg->insert;
1823 $dbh->rollback if $oldAutoCommit;
1824 return "can't create invoice line item for customer #". $self->custnum.
1829 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1833 =item collect OPTIONS
1835 (Attempt to) collect money for this customer's outstanding invoices (see
1836 L<FS::cust_bill>). Usually used after the bill method.
1838 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
1839 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
1840 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
1842 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
1843 and the invoice events web interface.
1845 If there is an error, returns the error, otherwise returns false.
1847 Options are passed as name-value pairs.
1849 Currently available options are:
1851 invoice_time - Use this time when deciding when to print invoices and
1852 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>
1853 for conversion functions.
1855 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
1858 retry_card - Deprecated alias for 'retry'
1860 batch_card - This option is deprecated. See the invoice events web interface
1861 to control whether cards are batched or run against a realtime gateway.
1863 report_badcard - This option is deprecated.
1865 force_print - This option is deprecated; see the invoice events web interface.
1867 quiet - set true to surpress email card/ACH decline notices.
1872 my( $self, %options ) = @_;
1873 my $invoice_time = $options{'invoice_time'} || time;
1876 local $SIG{HUP} = 'IGNORE';
1877 local $SIG{INT} = 'IGNORE';
1878 local $SIG{QUIT} = 'IGNORE';
1879 local $SIG{TERM} = 'IGNORE';
1880 local $SIG{TSTP} = 'IGNORE';
1881 local $SIG{PIPE} = 'IGNORE';
1883 my $oldAutoCommit = $FS::UID::AutoCommit;
1884 local $FS::UID::AutoCommit = 0;
1887 $self->select_for_update; #mutex
1889 my $balance = $self->balance;
1890 warn "collect customer ". $self->custnum. ": balance $balance\n" if $DEBUG;
1891 unless ( $balance > 0 ) { #redundant?????
1892 $dbh->rollback if $oldAutoCommit; #hmm
1896 if ( exists($options{'retry_card'}) ) {
1897 carp 'retry_card option passed to collect is deprecated; use retry';
1898 $options{'retry'} ||= $options{'retry_card'};
1900 if ( exists($options{'retry'}) && $options{'retry'} ) {
1901 my $error = $self->retry_realtime;
1903 $dbh->rollback if $oldAutoCommit;
1908 foreach my $cust_bill ( $self->open_cust_bill ) {
1910 # don't try to charge for the same invoice if it's already in a batch
1911 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
1913 last if $self->balance <= 0;
1915 warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
1918 foreach my $part_bill_event (
1919 sort { $a->seconds <=> $b->seconds
1920 || $a->weight <=> $b->weight
1921 || $a->eventpart <=> $b->eventpart }
1922 grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
1923 && ! qsearch( 'cust_bill_event', {
1924 'invnum' => $cust_bill->invnum,
1925 'eventpart' => $_->eventpart,
1929 qsearch('part_bill_event', { 'payby' => $self->payby,
1930 'disabled' => '', } )
1933 last if $cust_bill->owed <= 0 # don't run subsequent events if owed<=0
1934 || $self->balance <= 0; # or if balance<=0
1936 warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
1938 my $cust_main = $self; #for callback
1942 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
1943 local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
1944 $error = eval $part_bill_event->eventcode;
1948 my $statustext = '';
1952 } elsif ( $error ) {
1954 $statustext = $error;
1959 #add cust_bill_event
1960 my $cust_bill_event = new FS::cust_bill_event {
1961 'invnum' => $cust_bill->invnum,
1962 'eventpart' => $part_bill_event->eventpart,
1963 #'_date' => $invoice_time,
1965 'status' => $status,
1966 'statustext' => $statustext,
1968 $error = $cust_bill_event->insert;
1970 #$dbh->rollback if $oldAutoCommit;
1971 #return "error: $error";
1973 # gah, even with transactions.
1974 $dbh->commit if $oldAutoCommit; #well.
1975 my $e = 'WARNING: Event run but database not updated - '.
1976 'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
1977 ', eventpart '. $part_bill_event->eventpart.
1988 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1993 =item retry_realtime
1995 Schedules realtime credit card / electronic check / LEC billing events for
1996 for retry. Useful if card information has changed or manual retry is desired.
1997 The 'collect' method must be called to actually retry the transaction.
1999 Implementation details: For each of this customer's open invoices, changes
2000 the status of the first "done" (with statustext error) realtime processing
2005 sub retry_realtime {
2008 local $SIG{HUP} = 'IGNORE';
2009 local $SIG{INT} = 'IGNORE';
2010 local $SIG{QUIT} = 'IGNORE';
2011 local $SIG{TERM} = 'IGNORE';
2012 local $SIG{TSTP} = 'IGNORE';
2013 local $SIG{PIPE} = 'IGNORE';
2015 my $oldAutoCommit = $FS::UID::AutoCommit;
2016 local $FS::UID::AutoCommit = 0;
2019 foreach my $cust_bill (
2020 grep { $_->cust_bill_event }
2021 $self->open_cust_bill
2023 my @cust_bill_event =
2024 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
2026 #$_->part_bill_event->plan eq 'realtime-card'
2027 $_->part_bill_event->eventcode =~
2028 /\$cust_bill\->realtime_(card|ach|lec)/
2029 && $_->status eq 'done'
2032 $cust_bill->cust_bill_event;
2033 next unless @cust_bill_event;
2034 my $error = $cust_bill_event[0]->retry;
2036 $dbh->rollback if $oldAutoCommit;
2037 return "error scheduling invoice event for retry: $error";
2042 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2047 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2049 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2050 via a Business::OnlinePayment realtime gateway. See
2051 L<http://420.am/business-onlinepayment> for supported gateways.
2053 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2055 Available options are: I<description>, I<invnum>, I<quiet>
2057 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2058 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2059 if set, will override the value from the customer record.
2061 I<description> is a free-text field passed to the gateway. It defaults to
2062 "Internet services".
2064 If an I<invnum> is specified, this payment (if sucessful) is applied to the
2065 specified invoice. If you don't specify an I<invnum> you might want to
2066 call the B<apply_payments> method.
2068 I<quiet> can be set true to surpress email decline notices.
2070 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2075 my( $self, $method, $amount, %options ) = @_;
2077 warn "$self $method $amount\n";
2078 warn " $_ => $options{$_}\n" foreach keys %options;
2081 $options{'description'} ||= 'Internet services';
2083 eval "use Business::OnlinePayment";
2086 my $payinfo = exists($options{'payinfo'})
2087 ? $options{'payinfo'}
2095 if ( $options{'invnum'} ) {
2096 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
2097 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
2099 map { $_->part_pkg->taxclass }
2101 map { $_->cust_pkg }
2102 $cust_bill->cust_bill_pkg;
2103 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
2104 #different taxclasses
2105 $taxclass = $taxclasses[0];
2109 #look for an agent gateway override first
2111 if ( $method eq 'CC' ) {
2112 $cardtype = cardtype($payinfo);
2113 } elsif ( $method eq 'ECHECK' ) {
2116 $cardtype = $method;
2120 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2121 cardtype => $cardtype,
2122 taxclass => $taxclass, } )
2123 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2125 taxclass => $taxclass, } )
2126 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2127 cardtype => $cardtype,
2129 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2131 taxclass => '', } );
2133 my $payment_gateway = '';
2134 my( $processor, $login, $password, $action, @bop_options );
2135 if ( $override ) { #use a payment gateway override
2137 $payment_gateway = $override->payment_gateway;
2139 $processor = $payment_gateway->gateway_module;
2140 $login = $payment_gateway->gateway_username;
2141 $password = $payment_gateway->gateway_password;
2142 $action = $payment_gateway->gateway_action;
2143 @bop_options = $payment_gateway->options;
2145 } else { #use the standard settings from the config
2147 ( $processor, $login, $password, $action, @bop_options ) =
2148 $self->default_payment_gateway($method);
2156 my $address = exists($options{'address1'})
2157 ? $options{'address1'}
2159 my $address2 = exists($options{'address2'})
2160 ? $options{'address2'}
2162 $address .= ", ". $address2 if length($address2);
2164 my $o_payname = exists($options{'payname'})
2165 ? $options{'payname'}
2167 my($payname, $payfirst, $paylast);
2168 if ( $o_payname && $method ne 'ECHECK' ) {
2169 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2170 or return "Illegal payname $payname";
2171 ($payfirst, $paylast) = ($1, $2);
2173 $payfirst = $self->getfield('first');
2174 $paylast = $self->getfield('last');
2175 $payname = "$payfirst $paylast";
2178 my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
2179 if ( $conf->exists('emailinvoiceauto')
2180 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2181 push @invoicing_list, $self->all_emails;
2184 my $email = ($conf->exists('business-onlinepayment-email-override'))
2185 ? $conf->config('business-onlinepayment-email-override')
2186 : $invoicing_list[0];
2189 if ( $method eq 'CC' ) {
2191 $content{card_number} = $payinfo;
2192 my $paydate = exists($options{'paydate'})
2193 ? $options{'paydate'}
2195 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2196 $content{expiration} = "$2/$1";
2198 my $paycvv = exists($options{'paycvv'})
2199 ? $options{'paycvv'}
2201 $content{cvv2} = $self->paycvv
2204 my $paystart_month = exists($options{'paystart_month'})
2205 ? $options{'paystart_month'}
2206 : $self->paystart_month;
2208 my $paystart_year = exists($options{'paystart_year'})
2209 ? $options{'paystart_year'}
2210 : $self->paystart_year;
2212 $content{card_start} = "$paystart_month/$paystart_year"
2213 if $paystart_month && $paystart_year;
2215 my $payissue = exists($options{'payissue'})
2216 ? $options{'payissue'}
2218 $content{issue_number} = $payissue if $payissue;
2220 my $payip = exists($options{'payip'})
2223 $content{customer_ip} = $payip
2226 $content{recurring_billing} = 'YES'
2227 if qsearch('cust_pay', { 'custnum' => $self->custnum,
2229 'payinfo' => $payinfo,
2232 } elsif ( $method eq 'ECHECK' ) {
2233 ( $content{account_number}, $content{routing_code} ) =
2234 split('@', $payinfo);
2235 $content{bank_name} = $o_payname;
2236 $content{account_type} = 'CHECKING';
2237 $content{account_name} = $payname;
2238 $content{customer_org} = $self->company ? 'B' : 'I';
2239 $content{customer_ssn} = exists($options{'ss'})
2242 } elsif ( $method eq 'LEC' ) {
2243 $content{phone} = $payinfo;
2247 # run transaction(s)
2250 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
2252 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
2253 $transaction->content(
2256 'password' => $password,
2257 'action' => $action1,
2258 'description' => $options{'description'},
2259 'amount' => $amount,
2260 'invoice_number' => $options{'invnum'},
2261 'customer_id' => $self->custnum,
2262 'last_name' => $paylast,
2263 'first_name' => $payfirst,
2265 'address' => $address,
2266 'city' => ( exists($options{'city'})
2269 'state' => ( exists($options{'state'})
2272 'zip' => ( exists($options{'zip'})
2275 'country' => ( exists($options{'country'})
2276 ? $options{'country'}
2278 'referer' => 'http://cleanwhisker.420.am/',
2280 'phone' => $self->daytime || $self->night,
2283 $transaction->submit();
2285 if ( $transaction->is_success() && $action2 ) {
2286 my $auth = $transaction->authorization;
2287 my $ordernum = $transaction->can('order_number')
2288 ? $transaction->order_number
2292 new Business::OnlinePayment( $processor, @bop_options );
2299 password => $password,
2300 order_number => $ordernum,
2302 authorization => $auth,
2303 description => $options{'description'},
2306 foreach my $field (qw( authorization_source_code returned_ACI transaction_identifier validation_code
2307 transaction_sequence_num local_transaction_date
2308 local_transaction_time AVS_result_code )) {
2309 $capture{$field} = $transaction->$field() if $transaction->can($field);
2312 $capture->content( %capture );
2316 unless ( $capture->is_success ) {
2317 my $e = "Authorization sucessful but capture failed, custnum #".
2318 $self->custnum. ': '. $capture->result_code.
2319 ": ". $capture->error_message;
2327 # remove paycvv after initial transaction
2330 #false laziness w/misc/process/payment.cgi - check both to make sure working
2332 if ( defined $self->dbdef_table->column('paycvv')
2333 && length($self->paycvv)
2334 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
2336 my $error = $self->remove_cvv;
2338 warn "error removing cvv: $error\n";
2346 if ( $transaction->is_success() ) {
2348 my %method2payby = (
2355 if ( $payment_gateway ) { # agent override
2356 $paybatch = $payment_gateway->gatewaynum. '-';
2359 $paybatch .= "$processor:". $transaction->authorization;
2361 $paybatch .= ':'. $transaction->order_number
2362 if $transaction->can('order_number')
2363 && length($transaction->order_number);
2365 my $cust_pay = new FS::cust_pay ( {
2366 'custnum' => $self->custnum,
2367 'invnum' => $options{'invnum'},
2370 'payby' => $method2payby{$method},
2371 'payinfo' => $payinfo,
2372 'paybatch' => $paybatch,
2374 my $error = $cust_pay->insert;
2376 $cust_pay->invnum(''); #try again with no specific invnum
2377 my $error2 = $cust_pay->insert;
2379 # gah, even with transactions.
2380 my $e = 'WARNING: Card/ACH debited but database not updated - '.
2381 "error inserting payment ($processor): $error2".
2382 " (previously tried insert with invnum #$options{'invnum'}" .
2388 return ''; #no error
2392 my $perror = "$processor error: ". $transaction->error_message;
2394 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
2395 && $conf->exists('emaildecline')
2396 && grep { $_ ne 'POST' } $self->invoicing_list
2397 && ! grep { $transaction->error_message =~ /$_/ }
2398 $conf->config('emaildecline-exclude')
2400 my @templ = $conf->config('declinetemplate');
2401 my $template = new Text::Template (
2403 SOURCE => [ map "$_\n", @templ ],
2404 ) or return "($perror) can't create template: $Text::Template::ERROR";
2405 $template->compile()
2406 or return "($perror) can't compile template: $Text::Template::ERROR";
2408 my $templ_hash = { error => $transaction->error_message };
2410 my $error = send_email(
2411 'from' => $conf->config('invoice_from'),
2412 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
2413 'subject' => 'Your payment could not be processed',
2414 'body' => [ $template->fill_in(HASH => $templ_hash) ],
2417 $perror .= " (also received error sending decline notification: $error)"
2427 =item default_payment_gateway
2431 sub default_payment_gateway {
2432 my( $self, $method ) = @_;
2434 die "Real-time processing not enabled\n"
2435 unless $conf->exists('business-onlinepayment');
2438 my $bop_config = 'business-onlinepayment';
2439 $bop_config .= '-ach'
2440 if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
2441 my ( $processor, $login, $password, $action, @bop_options ) =
2442 $conf->config($bop_config);
2443 $action ||= 'normal authorization';
2444 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
2445 die "No real-time processor is enabled - ".
2446 "did you set the business-onlinepayment configuration value?\n"
2449 ( $processor, $login, $password, $action, @bop_options )
2454 Removes the I<paycvv> field from the database directly.
2456 If there is an error, returns the error, otherwise returns false.
2462 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2463 or return dbh->errstr;
2464 $sth->execute($self->custnum)
2465 or return $sth->errstr;
2470 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
2472 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
2473 via a Business::OnlinePayment realtime gateway. See
2474 L<http://420.am/business-onlinepayment> for supported gateways.
2476 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2478 Available options are: I<amount>, I<reason>, I<paynum>
2480 Most gateways require a reference to an original payment transaction to refund,
2481 so you probably need to specify a I<paynum>.
2483 I<amount> defaults to the original amount of the payment if not specified.
2485 I<reason> specifies a reason for the refund.
2487 Implementation note: If I<amount> is unspecified or equal to the amount of the
2488 orignal payment, first an attempt is made to "void" the transaction via
2489 the gateway (to cancel a not-yet settled transaction) and then if that fails,
2490 the normal attempt is made to "refund" ("credit") the transaction via the
2491 gateway is attempted.
2493 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2494 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2495 #if set, will override the value from the customer record.
2497 #If an I<invnum> is specified, this payment (if sucessful) is applied to the
2498 #specified invoice. If you don't specify an I<invnum> you might want to
2499 #call the B<apply_payments> method.
2503 #some false laziness w/realtime_bop, not enough to make it worth merging
2504 #but some useful small subs should be pulled out
2505 sub realtime_refund_bop {
2506 my( $self, $method, %options ) = @_;
2508 warn "$self $method refund\n";
2509 warn " $_ => $options{$_}\n" foreach keys %options;
2512 eval "use Business::OnlinePayment";
2516 # look up the original payment and optionally a gateway for that payment
2520 my $amount = $options{'amount'};
2522 my( $processor, $login, $password, @bop_options ) ;
2523 my( $auth, $order_number ) = ( '', '', '' );
2525 if ( $options{'paynum'} ) {
2527 warn "FS::cust_main::realtime_bop: paynum: $options{paynum}\n" if $DEBUG;
2528 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
2529 or return "Unknown paynum $options{'paynum'}";
2530 $amount ||= $cust_pay->paid;
2532 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):([\w-]*)(:(\w+))?$/
2533 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
2534 $cust_pay->paybatch;
2535 my $gatewaynum = '';
2536 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
2538 if ( $gatewaynum ) { #gateway for the payment to be refunded
2540 my $payment_gateway =
2541 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
2542 die "payment gateway $gatewaynum not found"
2543 unless $payment_gateway;
2545 $processor = $payment_gateway->gateway_module;
2546 $login = $payment_gateway->gateway_username;
2547 $password = $payment_gateway->gateway_password;
2548 @bop_options = $payment_gateway->options;
2550 } else { #try the default gateway
2552 my( $conf_processor, $unused_action );
2553 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
2554 $self->default_payment_gateway($method);
2556 return "processor of payment $options{'paynum'} $processor does not".
2557 " match default processor $conf_processor"
2558 unless $processor eq $conf_processor;
2563 } else { # didn't specify a paynum, so look for agent gateway overrides
2564 # like a normal transaction
2567 if ( $method eq 'CC' ) {
2568 $cardtype = cardtype($self->payinfo);
2569 } elsif ( $method eq 'ECHECK' ) {
2572 $cardtype = $method;
2575 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2576 cardtype => $cardtype,
2578 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2580 taxclass => '', } );
2582 if ( $override ) { #use a payment gateway override
2584 my $payment_gateway = $override->payment_gateway;
2586 $processor = $payment_gateway->gateway_module;
2587 $login = $payment_gateway->gateway_username;
2588 $password = $payment_gateway->gateway_password;
2589 #$action = $payment_gateway->gateway_action;
2590 @bop_options = $payment_gateway->options;
2592 } else { #use the standard settings from the config
2595 ( $processor, $login, $password, $unused_action, @bop_options ) =
2596 $self->default_payment_gateway($method);
2601 return "neither amount nor paynum specified" unless $amount;
2606 'password' => $password,
2607 'order_number' => $order_number,
2608 'amount' => $amount,
2609 'referer' => 'http://cleanwhisker.420.am/',
2611 $content{authorization} = $auth
2612 if length($auth); #echeck/ACH transactions have an order # but no auth
2613 #(at least with authorize.net)
2615 #first try void if applicable
2616 if ( $cust_pay && $cust_pay->paid == $amount ) { #and check dates?
2617 warn "FS::cust_main::realtime_bop: attempting void\n" if $DEBUG;
2618 my $void = new Business::OnlinePayment( $processor, @bop_options );
2619 $void->content( 'action' => 'void', %content );
2621 if ( $void->is_success ) {
2622 my $error = $cust_pay->void($options{'reason'});
2624 # gah, even with transactions.
2625 my $e = 'WARNING: Card/ACH voided but database not updated - '.
2626 "error voiding payment: $error";
2630 warn "FS::cust_main::realtime_bop: void successful\n" if $DEBUG;
2635 warn "FS::cust_main::realtime_bop: void unsuccessful, trying refund\n"
2639 my $address = $self->address1;
2640 $address .= ", ". $self->address2 if $self->address2;
2642 my($payname, $payfirst, $paylast);
2643 if ( $self->payname && $method ne 'ECHECK' ) {
2644 $payname = $self->payname;
2645 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2646 or return "Illegal payname $payname";
2647 ($payfirst, $paylast) = ($1, $2);
2649 $payfirst = $self->getfield('first');
2650 $paylast = $self->getfield('last');
2651 $payname = "$payfirst $paylast";
2655 if ( $method eq 'CC' ) {
2658 $content{card_number} = $payinfo = $cust_pay->payinfo;
2659 #$self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2660 #$content{expiration} = "$2/$1";
2662 $content{card_number} = $payinfo = $self->payinfo;
2663 $self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2664 $content{expiration} = "$2/$1";
2667 } elsif ( $method eq 'ECHECK' ) {
2668 ( $content{account_number}, $content{routing_code} ) =
2669 split('@', $payinfo = $self->payinfo);
2670 $content{bank_name} = $self->payname;
2671 $content{account_type} = 'CHECKING';
2672 $content{account_name} = $payname;
2673 $content{customer_org} = $self->company ? 'B' : 'I';
2674 $content{customer_ssn} = $self->ss;
2675 } elsif ( $method eq 'LEC' ) {
2676 $content{phone} = $payinfo = $self->payinfo;
2680 my $refund = new Business::OnlinePayment( $processor, @bop_options );
2681 my %sub_content = $refund->content(
2682 'action' => 'credit',
2683 'customer_id' => $self->custnum,
2684 'last_name' => $paylast,
2685 'first_name' => $payfirst,
2687 'address' => $address,
2688 'city' => $self->city,
2689 'state' => $self->state,
2690 'zip' => $self->zip,
2691 'country' => $self->country,
2694 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
2698 return "$processor error: ". $refund->error_message
2699 unless $refund->is_success();
2701 my %method2payby = (
2707 my $paybatch = "$processor:". $refund->authorization;
2708 $paybatch .= ':'. $refund->order_number
2709 if $refund->can('order_number') && $refund->order_number;
2711 while ( $cust_pay && $cust_pay->unappled < $amount ) {
2712 my @cust_bill_pay = $cust_pay->cust_bill_pay;
2713 last unless @cust_bill_pay;
2714 my $cust_bill_pay = pop @cust_bill_pay;
2715 my $error = $cust_bill_pay->delete;
2719 my $cust_refund = new FS::cust_refund ( {
2720 'custnum' => $self->custnum,
2721 'paynum' => $options{'paynum'},
2722 'refund' => $amount,
2724 'payby' => $method2payby{$method},
2725 'payinfo' => $payinfo,
2726 'paybatch' => $paybatch,
2727 'reason' => $options{'reason'} || 'card or ACH refund',
2729 my $error = $cust_refund->insert;
2731 $cust_refund->paynum(''); #try again with no specific paynum
2732 my $error2 = $cust_refund->insert;
2734 # gah, even with transactions.
2735 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
2736 "error inserting refund ($processor): $error2".
2737 " (previously tried insert with paynum #$options{'paynum'}" .
2750 Returns the total owed for this customer on all invoices
2751 (see L<FS::cust_bill/owed>).
2757 $self->total_owed_date(2145859200); #12/31/2037
2760 =item total_owed_date TIME
2762 Returns the total owed for this customer on all invoices with date earlier than
2763 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2764 see L<Time::Local> and L<Date::Parse> for conversion functions.
2768 sub total_owed_date {
2772 foreach my $cust_bill (
2773 grep { $_->_date <= $time }
2774 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2776 $total_bill += $cust_bill->owed;
2778 sprintf( "%.2f", $total_bill );
2781 =item apply_credits OPTION => VALUE ...
2783 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
2784 to outstanding invoice balances in chronological order (or reverse
2785 chronological order if the I<order> option is set to B<newest>) and returns the
2786 value of any remaining unapplied credits available for refund (see
2787 L<FS::cust_refund>).
2795 return 0 unless $self->total_credited;
2797 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
2798 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
2800 my @invoices = $self->open_cust_bill;
2801 @invoices = sort { $b->_date <=> $a->_date } @invoices
2802 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
2805 foreach my $cust_bill ( @invoices ) {
2808 if ( !defined($credit) || $credit->credited == 0) {
2809 $credit = pop @credits or last;
2812 if ($cust_bill->owed >= $credit->credited) {
2813 $amount=$credit->credited;
2815 $amount=$cust_bill->owed;
2818 my $cust_credit_bill = new FS::cust_credit_bill ( {
2819 'crednum' => $credit->crednum,
2820 'invnum' => $cust_bill->invnum,
2821 'amount' => $amount,
2823 my $error = $cust_credit_bill->insert;
2824 die $error if $error;
2826 redo if ($cust_bill->owed > 0);
2830 return $self->total_credited;
2833 =item apply_payments
2835 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
2836 to outstanding invoice balances in chronological order.
2838 #and returns the value of any remaining unapplied payments.
2842 sub apply_payments {
2847 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
2848 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
2850 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
2851 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
2855 foreach my $cust_bill ( @invoices ) {
2858 if ( !defined($payment) || $payment->unapplied == 0 ) {
2859 $payment = pop @payments or last;
2862 if ( $cust_bill->owed >= $payment->unapplied ) {
2863 $amount = $payment->unapplied;
2865 $amount = $cust_bill->owed;
2868 my $cust_bill_pay = new FS::cust_bill_pay ( {
2869 'paynum' => $payment->paynum,
2870 'invnum' => $cust_bill->invnum,
2871 'amount' => $amount,
2873 my $error = $cust_bill_pay->insert;
2874 die $error if $error;
2876 redo if ( $cust_bill->owed > 0);
2880 return $self->total_unapplied_payments;
2883 =item total_credited
2885 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2886 customer. See L<FS::cust_credit/credited>.
2890 sub total_credited {
2892 my $total_credit = 0;
2893 foreach my $cust_credit ( qsearch('cust_credit', {
2894 'custnum' => $self->custnum,
2896 $total_credit += $cust_credit->credited;
2898 sprintf( "%.2f", $total_credit );
2901 =item total_unapplied_payments
2903 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
2904 See L<FS::cust_pay/unapplied>.
2908 sub total_unapplied_payments {
2910 my $total_unapplied = 0;
2911 foreach my $cust_pay ( qsearch('cust_pay', {
2912 'custnum' => $self->custnum,
2914 $total_unapplied += $cust_pay->unapplied;
2916 sprintf( "%.2f", $total_unapplied );
2921 Returns the balance for this customer (total_owed minus total_credited
2922 minus total_unapplied_payments).
2929 $self->total_owed - $self->total_credited - $self->total_unapplied_payments
2933 =item balance_date TIME
2935 Returns the balance for this customer, only considering invoices with date
2936 earlier than TIME (total_owed_date minus total_credited minus
2937 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
2938 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
2947 $self->total_owed_date($time)
2948 - $self->total_credited
2949 - $self->total_unapplied_payments
2953 =item paydate_monthyear
2955 Returns a two-element list consisting of the month and year of this customer's
2956 paydate (credit card expiration date for CARD customers)
2960 sub paydate_monthyear {
2962 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
2964 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
2971 =item payinfo_masked
2973 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.
2975 Credit Cards - Mask all but the last four characters.
2976 Checks - Mask all but last 2 of account number and bank routing number.
2977 Others - Do nothing, return the unmasked string.
2981 sub payinfo_masked {
2983 return $self->paymask;
2986 =item invoicing_list [ ARRAYREF ]
2988 If an arguement is given, sets these email addresses as invoice recipients
2989 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
2990 (except as warnings), so use check_invoicing_list first.
2992 Returns a list of email addresses (with svcnum entries expanded).
2994 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
2995 check it without disturbing anything by passing nothing.
2997 This interface may change in the future.
3001 sub invoicing_list {
3002 my( $self, $arrayref ) = @_;
3004 my @cust_main_invoice;
3005 if ( $self->custnum ) {
3006 @cust_main_invoice =
3007 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3009 @cust_main_invoice = ();
3011 foreach my $cust_main_invoice ( @cust_main_invoice ) {
3012 #warn $cust_main_invoice->destnum;
3013 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3014 #warn $cust_main_invoice->destnum;
3015 my $error = $cust_main_invoice->delete;
3016 warn $error if $error;
3019 if ( $self->custnum ) {
3020 @cust_main_invoice =
3021 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3023 @cust_main_invoice = ();
3025 my %seen = map { $_->address => 1 } @cust_main_invoice;
3026 foreach my $address ( @{$arrayref} ) {
3027 next if exists $seen{$address} && $seen{$address};
3028 $seen{$address} = 1;
3029 my $cust_main_invoice = new FS::cust_main_invoice ( {
3030 'custnum' => $self->custnum,
3033 my $error = $cust_main_invoice->insert;
3034 warn $error if $error;
3037 if ( $self->custnum ) {
3039 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3045 =item check_invoicing_list ARRAYREF
3047 Checks these arguements as valid input for the invoicing_list method. If there
3048 is an error, returns the error, otherwise returns false.
3052 sub check_invoicing_list {
3053 my( $self, $arrayref ) = @_;
3054 foreach my $address ( @{$arrayref} ) {
3056 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3057 return 'Can\'t add FAX invoice destination with a blank FAX number.';
3060 my $cust_main_invoice = new FS::cust_main_invoice ( {
3061 'custnum' => $self->custnum,
3064 my $error = $self->custnum
3065 ? $cust_main_invoice->check
3066 : $cust_main_invoice->checkdest
3068 return $error if $error;
3073 =item set_default_invoicing_list
3075 Sets the invoicing list to all accounts associated with this customer,
3076 overwriting any previous invoicing list.
3080 sub set_default_invoicing_list {
3082 $self->invoicing_list($self->all_emails);
3087 Returns the email addresses of all accounts provisioned for this customer.
3094 foreach my $cust_pkg ( $self->all_pkgs ) {
3095 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
3097 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3098 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3100 $list{$_}=1 foreach map { $_->email } @svc_acct;
3105 =item invoicing_list_addpost
3107 Adds postal invoicing to this customer. If this customer is already configured
3108 to receive postal invoices, does nothing.
3112 sub invoicing_list_addpost {
3114 return if grep { $_ eq 'POST' } $self->invoicing_list;
3115 my @invoicing_list = $self->invoicing_list;
3116 push @invoicing_list, 'POST';
3117 $self->invoicing_list(\@invoicing_list);
3120 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
3122 Returns an array of customers referred by this customer (referral_custnum set
3123 to this custnum). If DEPTH is given, recurses up to the given depth, returning
3124 customers referred by customers referred by this customer and so on, inclusive.
3125 The default behavior is DEPTH 1 (no recursion).
3129 sub referral_cust_main {
3131 my $depth = @_ ? shift : 1;
3132 my $exclude = @_ ? shift : {};
3135 map { $exclude->{$_->custnum}++; $_; }
3136 grep { ! $exclude->{ $_->custnum } }
3137 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
3141 map { $_->referral_cust_main($depth-1, $exclude) }
3148 =item referral_cust_main_ncancelled
3150 Same as referral_cust_main, except only returns customers with uncancelled
3155 sub referral_cust_main_ncancelled {
3157 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
3160 =item referral_cust_pkg [ DEPTH ]
3162 Like referral_cust_main, except returns a flat list of all unsuspended (and
3163 uncancelled) packages for each customer. The number of items in this list may
3164 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
3168 sub referral_cust_pkg {
3170 my $depth = @_ ? shift : 1;
3172 map { $_->unsuspended_pkgs }
3173 grep { $_->unsuspended_pkgs }
3174 $self->referral_cust_main($depth);
3177 =item referring_cust_main
3179 Returns the single cust_main record for the customer who referred this customer
3180 (referral_custnum), or false.
3184 sub referring_cust_main {
3186 return '' unless $self->referral_custnum;
3187 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3190 =item credit AMOUNT, REASON
3192 Applies a credit to this customer. If there is an error, returns the error,
3193 otherwise returns false.
3198 my( $self, $amount, $reason ) = @_;
3199 my $cust_credit = new FS::cust_credit {
3200 'custnum' => $self->custnum,
3201 'amount' => $amount,
3202 'reason' => $reason,
3204 $cust_credit->insert;
3207 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3209 Creates a one-time charge for this customer. If there is an error, returns
3210 the error, otherwise returns false.
3215 my ( $self, $amount ) = ( shift, shift );
3216 my $pkg = @_ ? shift : 'One-time charge';
3217 my $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
3218 my $taxclass = @_ ? shift : '';
3220 local $SIG{HUP} = 'IGNORE';
3221 local $SIG{INT} = 'IGNORE';
3222 local $SIG{QUIT} = 'IGNORE';
3223 local $SIG{TERM} = 'IGNORE';
3224 local $SIG{TSTP} = 'IGNORE';
3225 local $SIG{PIPE} = 'IGNORE';
3227 my $oldAutoCommit = $FS::UID::AutoCommit;
3228 local $FS::UID::AutoCommit = 0;
3231 my $part_pkg = new FS::part_pkg ( {
3233 'comment' => $comment,
3234 #'setup' => $amount,
3237 'plandata' => "setup_fee=$amount",
3240 'taxclass' => $taxclass,
3243 my $error = $part_pkg->insert;
3245 $dbh->rollback if $oldAutoCommit;
3249 my $pkgpart = $part_pkg->pkgpart;
3250 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3251 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3252 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3253 $error = $type_pkgs->insert;
3255 $dbh->rollback if $oldAutoCommit;
3260 my $cust_pkg = new FS::cust_pkg ( {
3261 'custnum' => $self->custnum,
3262 'pkgpart' => $pkgpart,
3265 $error = $cust_pkg->insert;
3267 $dbh->rollback if $oldAutoCommit;
3271 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3278 Returns all the invoices (see L<FS::cust_bill>) for this customer.
3284 sort { $a->_date <=> $b->_date }
3285 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3288 =item open_cust_bill
3290 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
3295 sub open_cust_bill {
3297 grep { $_->owed > 0 } $self->cust_bill;
3302 Returns all the credits (see L<FS::cust_credit>) for this customer.
3308 sort { $a->_date <=> $b->_date }
3309 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
3314 Returns all the payments (see L<FS::cust_pay>) for this customer.
3320 sort { $a->_date <=> $b->_date }
3321 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
3326 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
3332 sort { $a->_date <=> $b->_date }
3333 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
3339 Returns all the refunds (see L<FS::cust_refund>) for this customer.
3345 sort { $a->_date <=> $b->_date }
3346 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
3349 =item select_for_update
3351 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
3356 sub select_for_update {
3358 qsearch('cust_main', { 'custnum' => $self->custnum }, '*', 'FOR UPDATE' );
3363 Returns a name string for this customer, either "Company (Last, First)" or
3370 my $name = $self->contact;
3371 $name = $self->company. " ($name)" if $self->company;
3377 Returns a name string for this (service/shipping) contact, either
3378 "Company (Last, First)" or "Last, First".
3384 if ( $self->get('ship_last') ) {
3385 my $name = $self->ship_contact;
3386 $name = $self->ship_company. " ($name)" if $self->ship_company;
3395 Returns this customer's full (billing) contact name only, "Last, First"
3401 $self->get('last'). ', '. $self->first;
3406 Returns this customer's full (shipping) contact name only, "Last, First"
3412 $self->get('ship_last')
3413 ? $self->get('ship_last'). ', '. $self->ship_first
3419 Returns a status string for this customer, currently:
3423 =item prospect - No packages have ever been ordered
3425 =item active - One or more recurring packages is active
3427 =item suspended - All non-cancelled recurring packages are suspended
3429 =item cancelled - All recurring packages are cancelled
3437 for my $status (qw( prospect active suspended cancelled )) {
3438 my $method = $status.'_sql';
3439 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
3440 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
3441 $sth->execute( ($self->custnum) x $numnum ) or die $sth->errstr;
3442 return $status if $sth->fetchrow_arrayref->[0];
3448 Returns a hex triplet color string for this customer's status.
3453 'prospect' => '000000',
3454 'active' => '00CC00',
3455 'suspended' => 'FF9900',
3456 'cancelled' => 'FF0000',
3460 $statuscolor{$self->status};
3465 =head1 CLASS METHODS
3471 Returns an SQL expression identifying prospective cust_main records (customers
3472 with no packages ever ordered)
3476 sub prospect_sql { "
3477 0 = ( SELECT COUNT(*) FROM cust_pkg
3478 WHERE cust_pkg.custnum = cust_main.custnum
3484 Returns an SQL expression identifying active cust_main records.
3489 0 < ( SELECT COUNT(*) FROM cust_pkg
3490 WHERE cust_pkg.custnum = cust_main.custnum
3491 AND ". FS::cust_pkg->active_sql. "
3498 Returns an SQL expression identifying suspended cust_main records.
3502 #my $recurring_sql = FS::cust_pkg->recurring_sql;
3503 my $recurring_sql = "
3504 '0' != ( select freq from part_pkg
3505 where cust_pkg.pkgpart = part_pkg.pkgpart )
3508 sub suspended_sql { susp_sql(@_); }
3510 0 < ( SELECT COUNT(*) FROM cust_pkg
3511 WHERE cust_pkg.custnum = cust_main.custnum
3513 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3515 AND 0 = ( SELECT COUNT(*) FROM cust_pkg
3516 WHERE cust_pkg.custnum = cust_main.custnum
3517 AND ". FS::cust_pkg->active_sql. "
3524 Returns an SQL expression identifying cancelled cust_main records.
3528 sub cancelled_sql { cancel_sql(@_); }
3530 0 < ( SELECT COUNT(*) FROM cust_pkg
3531 WHERE cust_pkg.custnum = cust_main.custnum
3533 AND 0 = ( SELECT COUNT(*) FROM cust_pkg
3534 WHERE cust_pkg.custnum = cust_main.custnum
3536 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
3540 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
3542 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
3543 records. Currently, only I<last> or I<company> may be specified (the
3544 appropriate ship_ field is also searched if applicable).
3546 Additional options are the same as FS::Record::qsearch
3551 my( $self, $fuzzy, $hash, @opt) = @_;
3556 check_and_rebuild_fuzzyfiles();
3557 foreach my $field ( keys %$fuzzy ) {
3558 my $sub = \&{"all_$field"};
3560 $match{$_}=1 foreach ( amatch($fuzzy->{$field}, ['i'], @{ &$sub() } ) );
3562 foreach ( keys %match ) {
3563 push @cust_main, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
3564 push @cust_main, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt)
3565 if defined dbdef->table('cust_main')->column('ship_last');
3570 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
3582 =item smart_search OPTION => VALUE ...
3584 Accepts the following options: I<search>, the string to search for. The string
3585 will be searched for as a customer number, last name or company name, first
3586 searching for an exact match then fuzzy and substring matches.
3588 Any additional options treated as an additional qualifier on the search
3591 Returns a (possibly empty) array of FS::cust_main objects.
3597 my $search = delete $options{'search'};
3600 if ( $search =~ /^\s*(\d+)\s*$/ ) { # customer # search
3602 push @cust_main, qsearch('cust_main', { 'custnum' => $1, %options } );
3604 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { #value search
3607 my $q_value = dbh->quote($value);
3610 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
3611 $sql .= " ( LOWER(last) = $q_value OR LOWER(company) = $q_value";
3612 $sql .= " OR LOWER(ship_last) = $q_value OR LOWER(ship_company) = $q_value"
3613 if defined dbdef->table('cust_main')->column('ship_last');
3616 push @cust_main, qsearch( 'cust_main', \%options, '', $sql );
3618 unless ( @cust_main ) { #no exact match, trying substring/fuzzy
3620 #still some false laziness w/ search/cust_main.cgi
3623 push @cust_main, qsearch( 'cust_main',
3624 { 'last' => { 'op' => 'ILIKE',
3625 'value' => "%$q_value%" },
3629 push @cust_main, qsearch( 'cust_main',
3630 { 'ship_last' => { 'op' => 'ILIKE',
3631 'value' => "%$q_value%" },
3636 if defined dbdef->table('cust_main')->column('ship_last');
3638 push @cust_main, qsearch( 'cust_main',
3639 { 'company' => { 'op' => 'ILIKE',
3640 'value' => "%$q_value%" },
3644 push @cust_main, qsearch( 'cust_main',
3645 { 'ship_company' => { 'op' => 'ILIKE',
3646 'value' => "%$q_value%" },
3650 if defined dbdef->table('cust_main')->column('ship_last');
3653 push @cust_main, FS::cust_main->fuzzy_search(
3654 { 'last' => $value },
3657 push @cust_main, FS::cust_main->fuzzy_search(
3658 { 'company' => $value },
3670 =item check_and_rebuild_fuzzyfiles
3674 sub check_and_rebuild_fuzzyfiles {
3675 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3676 -e "$dir/cust_main.last" && -e "$dir/cust_main.company"
3677 or &rebuild_fuzzyfiles;
3680 =item rebuild_fuzzyfiles
3684 sub rebuild_fuzzyfiles {
3686 use Fcntl qw(:flock);
3688 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3692 open(LASTLOCK,">>$dir/cust_main.last")
3693 or die "can't open $dir/cust_main.last: $!";
3694 flock(LASTLOCK,LOCK_EX)
3695 or die "can't lock $dir/cust_main.last: $!";
3697 my @all_last = map $_->getfield('last'), qsearch('cust_main', {});
3699 grep $_, map $_->getfield('ship_last'), qsearch('cust_main',{})
3700 if defined dbdef->table('cust_main')->column('ship_last');
3702 open (LASTCACHE,">$dir/cust_main.last.tmp")
3703 or die "can't open $dir/cust_main.last.tmp: $!";
3704 print LASTCACHE join("\n", @all_last), "\n";
3705 close LASTCACHE or die "can't close $dir/cust_main.last.tmp: $!";
3707 rename "$dir/cust_main.last.tmp", "$dir/cust_main.last";
3712 open(COMPANYLOCK,">>$dir/cust_main.company")
3713 or die "can't open $dir/cust_main.company: $!";
3714 flock(COMPANYLOCK,LOCK_EX)
3715 or die "can't lock $dir/cust_main.company: $!";
3717 my @all_company = grep $_ ne '', map $_->company, qsearch('cust_main',{});
3719 grep $_ ne '', map $_->ship_company, qsearch('cust_main', {})
3720 if defined dbdef->table('cust_main')->column('ship_last');
3722 open (COMPANYCACHE,">$dir/cust_main.company.tmp")
3723 or die "can't open $dir/cust_main.company.tmp: $!";
3724 print COMPANYCACHE join("\n", @all_company), "\n";
3725 close COMPANYCACHE or die "can't close $dir/cust_main.company.tmp: $!";
3727 rename "$dir/cust_main.company.tmp", "$dir/cust_main.company";
3737 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3738 open(LASTCACHE,"<$dir/cust_main.last")
3739 or die "can't open $dir/cust_main.last: $!";
3740 my @array = map { chomp; $_; } <LASTCACHE>;
3750 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3751 open(COMPANYCACHE,"<$dir/cust_main.company")
3752 or die "can't open $dir/cust_main.last: $!";
3753 my @array = map { chomp; $_; } <COMPANYCACHE>;
3758 =item append_fuzzyfiles LASTNAME COMPANY
3762 sub append_fuzzyfiles {
3763 my( $last, $company ) = @_;
3765 &check_and_rebuild_fuzzyfiles;
3767 use Fcntl qw(:flock);
3769 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3773 open(LAST,">>$dir/cust_main.last")
3774 or die "can't open $dir/cust_main.last: $!";
3776 or die "can't lock $dir/cust_main.last: $!";
3778 print LAST "$last\n";
3781 or die "can't unlock $dir/cust_main.last: $!";
3787 open(COMPANY,">>$dir/cust_main.company")
3788 or die "can't open $dir/cust_main.company: $!";
3789 flock(COMPANY,LOCK_EX)
3790 or die "can't lock $dir/cust_main.company: $!";
3792 print COMPANY "$company\n";
3794 flock(COMPANY,LOCK_UN)
3795 or die "can't unlock $dir/cust_main.company: $!";
3809 #warn join('-',keys %$param);
3810 my $fh = $param->{filehandle};
3811 my $agentnum = $param->{agentnum};
3812 my $refnum = $param->{refnum};
3813 my $pkgpart = $param->{pkgpart};
3814 my @fields = @{$param->{fields}};
3816 eval "use Date::Parse;";
3818 eval "use Text::CSV_XS;";
3821 my $csv = new Text::CSV_XS;
3828 local $SIG{HUP} = 'IGNORE';
3829 local $SIG{INT} = 'IGNORE';
3830 local $SIG{QUIT} = 'IGNORE';
3831 local $SIG{TERM} = 'IGNORE';
3832 local $SIG{TSTP} = 'IGNORE';
3833 local $SIG{PIPE} = 'IGNORE';
3835 my $oldAutoCommit = $FS::UID::AutoCommit;
3836 local $FS::UID::AutoCommit = 0;
3839 #while ( $columns = $csv->getline($fh) ) {
3841 while ( defined($line=<$fh>) ) {
3843 $csv->parse($line) or do {
3844 $dbh->rollback if $oldAutoCommit;
3845 return "can't parse: ". $csv->error_input();
3848 my @columns = $csv->fields();
3849 #warn join('-',@columns);
3852 agentnum => $agentnum,
3854 country => $conf->config('countrydefault') || 'US',
3855 payby => 'BILL', #default
3856 paydate => '12/2037', #default
3858 my $billtime = time;
3859 my %cust_pkg = ( pkgpart => $pkgpart );
3860 foreach my $field ( @fields ) {
3861 if ( $field =~ /^cust_pkg\.(setup|bill|susp|expire|cancel)$/ ) {
3862 #$cust_pkg{$1} = str2time( shift @$columns );
3863 if ( $1 eq 'setup' ) {
3864 $billtime = str2time(shift @columns);
3866 $cust_pkg{$1} = str2time( shift @columns );
3869 #$cust_main{$field} = shift @$columns;
3870 $cust_main{$field} = shift @columns;
3874 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg ) if $pkgpart;
3875 my $cust_main = new FS::cust_main ( \%cust_main );
3877 tie my %hash, 'Tie::RefHash'; #this part is important
3878 $hash{$cust_pkg} = [] if $pkgpart;
3879 my $error = $cust_main->insert( \%hash );
3882 $dbh->rollback if $oldAutoCommit;
3883 return "can't insert customer for $line: $error";
3886 #false laziness w/bill.cgi
3887 $error = $cust_main->bill( 'time' => $billtime );
3889 $dbh->rollback if $oldAutoCommit;
3890 return "can't bill customer for $line: $error";
3893 $cust_main->apply_payments;
3894 $cust_main->apply_credits;
3896 $error = $cust_main->collect();
3898 $dbh->rollback if $oldAutoCommit;
3899 return "can't collect customer for $line: $error";
3905 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3907 return "Empty file!" unless $imported;
3919 #warn join('-',keys %$param);
3920 my $fh = $param->{filehandle};
3921 my @fields = @{$param->{fields}};
3923 eval "use Date::Parse;";
3925 eval "use Text::CSV_XS;";
3928 my $csv = new Text::CSV_XS;
3935 local $SIG{HUP} = 'IGNORE';
3936 local $SIG{INT} = 'IGNORE';
3937 local $SIG{QUIT} = 'IGNORE';
3938 local $SIG{TERM} = 'IGNORE';
3939 local $SIG{TSTP} = 'IGNORE';
3940 local $SIG{PIPE} = 'IGNORE';
3942 my $oldAutoCommit = $FS::UID::AutoCommit;
3943 local $FS::UID::AutoCommit = 0;
3946 #while ( $columns = $csv->getline($fh) ) {
3948 while ( defined($line=<$fh>) ) {
3950 $csv->parse($line) or do {
3951 $dbh->rollback if $oldAutoCommit;
3952 return "can't parse: ". $csv->error_input();
3955 my @columns = $csv->fields();
3956 #warn join('-',@columns);
3959 foreach my $field ( @fields ) {
3960 $row{$field} = shift @columns;
3963 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
3964 unless ( $cust_main ) {
3965 $dbh->rollback if $oldAutoCommit;
3966 return "unknown custnum $row{'custnum'}";
3969 if ( $row{'amount'} > 0 ) {
3970 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
3972 $dbh->rollback if $oldAutoCommit;
3976 } elsif ( $row{'amount'} < 0 ) {
3977 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
3980 $dbh->rollback if $oldAutoCommit;
3990 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3992 return "Empty file!" unless $imported;
4004 The delete method should possibly take an FS::cust_main object reference
4005 instead of a scalar customer number.
4007 Bill and collect options should probably be passed as references instead of a
4010 There should probably be a configuration file with a list of allowed credit
4013 No multiple currency support (probably a larger project than just this module).
4015 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
4019 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
4020 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
4021 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.