2 use base qw( FS::cust_main::Packages
4 FS::cust_main::NationalID
6 FS::cust_main::Billing_Realtime
7 FS::cust_main::Billing_Batch
8 FS::cust_main::Billing_Discount
9 FS::cust_main::Billing_ThirdParty
10 FS::cust_main::Location
11 FS::cust_main::Credit_Limit
14 FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
15 FS::geocode_Mixin FS::Quotable_Mixin FS::Sales_Mixin
23 use Scalar::Util qw( blessed );
24 use Time::Local qw(timelocal);
27 use Digest::MD5 qw(md5_base64);
30 use File::Temp; #qw( tempfile );
31 use Business::CreditCard 0.28;
33 use FS::UID qw( dbh driver_name );
34 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
36 use FS::Misc qw( generate_email send_email generate_ps do_print );
37 use FS::Msgcat qw(gettext);
44 use FS::cust_bill_void;
45 use FS::legacy_cust_bill;
47 use FS::cust_pay_pending;
48 use FS::cust_pay_void;
49 use FS::cust_pay_batch;
52 use FS::part_referral;
53 use FS::cust_main_county;
54 use FS::cust_location;
57 use FS::cust_main_exemption;
58 use FS::cust_tax_adjustment;
59 use FS::cust_tax_location;
60 use FS::agent_currency;
61 use FS::cust_main_invoice;
63 use FS::prepay_credit;
69 use FS::payment_gateway;
70 use FS::agent_payment_gateway;
72 use FS::cust_main_note;
73 use FS::cust_attachment;
76 use FS::upgrade_journal;
80 # 1 is mostly method/subroutine entry and options
81 # 2 traces progress of some operations
82 # 3 is even more information including possibly sensitive data
84 our $me = '[FS::cust_main]';
87 our $ignore_expired_card = 0;
88 our $ignore_banned_card = 0;
89 our $ignore_invalid_card = 0;
91 our $skip_fuzzyfiles = 0;
93 our $ucfirst_nowarn = 0;
95 our @encrypted_fields = ('payinfo', 'paycvv');
96 sub nohistory_fields { ('payinfo', 'paycvv'); }
98 our @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
101 #ask FS::UID to run this stuff for us later
102 #$FS::UID::callback{'FS::cust_main'} = sub {
103 install_callback FS::UID sub {
104 $conf = new FS::Conf;
105 #yes, need it for stuff below (prolly should be cached)
106 $ignore_invalid_card = $conf->exists('allow_invalid_cards');
111 my ( $hashref, $cache ) = @_;
112 if ( exists $hashref->{'pkgnum'} ) {
113 #@{ $self->{'_pkgnum'} } = ();
114 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
115 $self->{'_pkgnum'} = $subcache;
116 #push @{ $self->{'_pkgnum'} },
117 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
123 FS::cust_main - Object methods for cust_main records
129 $record = new FS::cust_main \%hash;
130 $record = new FS::cust_main { 'column' => 'value' };
132 $error = $record->insert;
134 $error = $new_record->replace($old_record);
136 $error = $record->delete;
138 $error = $record->check;
140 @cust_pkg = $record->all_pkgs;
142 @cust_pkg = $record->ncancelled_pkgs;
144 @cust_pkg = $record->suspended_pkgs;
146 $error = $record->bill;
147 $error = $record->bill %options;
148 $error = $record->bill 'time' => $time;
150 $error = $record->collect;
151 $error = $record->collect %options;
152 $error = $record->collect 'invoice_time' => $time,
157 An FS::cust_main object represents a customer. FS::cust_main inherits from
158 FS::Record. The following fields are currently supported:
164 Primary key (assigned automatically for new customers)
168 Agent (see L<FS::agent>)
172 Advertising source (see L<FS::part_referral>)
184 Cocial security number (optional)
208 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
212 Payment Information (See L<FS::payinfo_Mixin> for data format)
216 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
220 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
224 Expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
228 Start date month (maestro/solo cards only)
232 Start date year (maestro/solo cards only)
236 Issue number (maestro/solo cards only)
240 Name on card or billing name
244 IP address from which payment information was received
248 Tax exempt, empty or `Y'
252 Order taker (see L<FS::access_user>)
258 =item referral_custnum
260 Referring customer number
264 Enable individual CDR spooling, empty or `Y'
268 A suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
272 Discourage individual CDR printing, empty or `Y'
276 Allow self-service editing of ticket subjects, empty or 'Y'
278 =item calling_list_exempt
280 Do not call, empty or 'Y'
290 Creates a new customer. To add the customer to the database, see L<"insert">.
292 Note that this stores the hash reference, not a distinct copy of the hash it
293 points to. You can ask the object for a copy with the I<hash> method.
297 sub table { 'cust_main'; }
299 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
301 Adds this customer to the database. If there is an error, returns the error,
302 otherwise returns false.
304 Usually the customer's location will not yet exist in the database, and
305 the C<bill_location> and C<ship_location> pseudo-fields must be set to
306 uninserted L<FS::cust_location> objects. These will be inserted and linked
307 (in both directions) to the new customer record. If they're references
308 to the same object, they will become the same location.
310 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
311 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
312 are inserted atomicly, or the transaction is rolled back. Passing an empty
313 hash reference is equivalent to not supplying this parameter. There should be
314 a better explanation of this, but until then, here's an example:
317 tie %hash, 'Tie::RefHash'; #this part is important
319 $cust_pkg => [ $svc_acct ],
322 $cust_main->insert( \%hash );
324 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
325 be set as the invoicing list (see L<"invoicing_list">). Errors return as
326 expected and rollback the entire transaction; it is not necessary to call
327 check_invoicing_list first. The invoicing_list is set after the records in the
328 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
329 invoicing_list destination to the newly-created svc_acct. Here's an example:
331 $cust_main->insert( {}, [ $email, 'POST' ] );
333 Currently available options are: I<depend_jobnum>, I<noexport>,
334 I<tax_exemption> and I<prospectnum>.
336 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
337 on the supplied jobnum (they will not run until the specific job completes).
338 This can be used to defer provisioning until some action completes (such
339 as running the customer's credit card successfully).
341 The I<noexport> option is deprecated. If I<noexport> is set true, no
342 provisioning jobs (exports) are scheduled. (You can schedule them later with
343 the B<reexport> method.)
345 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
346 of tax names and exemption numbers. FS::cust_main_exemption records will be
347 created and inserted.
349 If I<prospectnum> is set, moves contacts and locations from that prospect.
351 If I<contact> is set to an arrayref of FS::contact objects, inserts those
352 new contacts with this new customer.
358 my $cust_pkgs = @_ ? shift : {};
359 my $invoicing_list = @_ ? shift : '';
361 warn "$me insert called with options ".
362 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
365 local $SIG{HUP} = 'IGNORE';
366 local $SIG{INT} = 'IGNORE';
367 local $SIG{QUIT} = 'IGNORE';
368 local $SIG{TERM} = 'IGNORE';
369 local $SIG{TSTP} = 'IGNORE';
370 local $SIG{PIPE} = 'IGNORE';
372 my $oldAutoCommit = $FS::UID::AutoCommit;
373 local $FS::UID::AutoCommit = 0;
376 my $prepay_identifier = '';
377 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
379 if ( $self->payby eq 'PREPAY' ) {
381 $self->payby('BILL');
382 $prepay_identifier = $self->payinfo;
385 warn " looking up prepaid card $prepay_identifier\n"
388 my $error = $self->get_prepay( $prepay_identifier,
389 'amount_ref' => \$amount,
390 'seconds_ref' => \$seconds,
391 'upbytes_ref' => \$upbytes,
392 'downbytes_ref' => \$downbytes,
393 'totalbytes_ref' => \$totalbytes,
396 $dbh->rollback if $oldAutoCommit;
397 #return "error applying prepaid card (transaction rolled back): $error";
401 $payby = 'PREP' if $amount;
403 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD|PPAL)$/ ) {
406 $self->payby('BILL');
407 $amount = $self->paid;
412 foreach my $l (qw(bill_location ship_location)) {
414 my $loc = delete $self->hashref->{$l} or next;
416 if ( !$loc->locationnum ) {
417 # warn the location that we're going to insert it with no custnum
418 $loc->set(custnum_pending => 1);
419 warn " inserting $l\n"
421 my $error = $loc->insert;
423 $dbh->rollback if $oldAutoCommit;
424 my $label = $l eq 'ship_location' ? 'service' : 'billing';
425 return "$error (in $label location)";
428 } elsif ( $loc->prospectnum ) {
430 $loc->prospectnum('');
431 $loc->set(custnum_pending => 1);
432 my $error = $loc->replace;
434 $dbh->rollback if $oldAutoCommit;
435 my $label = $l eq 'ship_location' ? 'service' : 'billing';
436 return "$error (moving $label location)";
439 } elsif ( ($loc->custnum || 0) > 0 ) {
440 # then it somehow belongs to another customer--shouldn't happen
441 $dbh->rollback if $oldAutoCommit;
442 return "$l belongs to customer ".$loc->custnum;
444 # else it already belongs to this customer
445 # (happens when ship_location is identical to bill_location)
447 $self->set($l.'num', $loc->locationnum);
449 if ( $self->get($l.'num') eq '' ) {
450 $dbh->rollback if $oldAutoCommit;
455 warn " inserting $self\n"
458 $self->signupdate(time) unless $self->signupdate;
460 $self->auto_agent_custid()
461 if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
463 my $error = $self->SUPER::insert;
465 $dbh->rollback if $oldAutoCommit;
466 #return "inserting cust_main record (transaction rolled back): $error";
470 # now set cust_location.custnum
471 foreach my $l (qw(bill_location ship_location)) {
472 warn " setting $l.custnum\n"
474 my $loc = $self->$l or next;
475 unless ( $loc->custnum ) {
476 $loc->set(custnum => $self->custnum);
477 $error ||= $loc->replace;
481 $dbh->rollback if $oldAutoCommit;
482 return "error setting $l custnum: $error";
486 warn " setting invoicing list\n"
489 if ( $invoicing_list ) {
490 $error = $self->check_invoicing_list( $invoicing_list );
492 $dbh->rollback if $oldAutoCommit;
493 #return "checking invoicing_list (transaction rolled back): $error";
496 $self->invoicing_list( $invoicing_list );
499 warn " setting customer tags\n"
502 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
503 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
504 'custnum' => $self->custnum };
505 my $error = $cust_tag->insert;
507 $dbh->rollback if $oldAutoCommit;
512 my $prospectnum = delete $options{'prospectnum'};
513 if ( $prospectnum ) {
515 warn " moving contacts and locations from prospect $prospectnum\n"
519 qsearchs('prospect_main', { 'prospectnum' => $prospectnum } );
520 unless ( $prospect_main ) {
521 $dbh->rollback if $oldAutoCommit;
522 return "Unknown prospectnum $prospectnum";
524 $prospect_main->custnum($self->custnum);
525 $prospect_main->disabled('Y');
526 my $error = $prospect_main->replace;
528 $dbh->rollback if $oldAutoCommit;
532 my @contact = $prospect_main->contact;
533 my @cust_location = $prospect_main->cust_location;
534 my @qual = $prospect_main->qual;
536 foreach my $r ( @contact, @cust_location, @qual ) {
538 $r->custnum($self->custnum);
539 my $error = $r->replace;
541 $dbh->rollback if $oldAutoCommit;
548 my $contact = delete $options{'contact'};
551 foreach my $c ( @$contact ) {
552 $c->custnum($self->custnum);
553 my $error = $c->insert;
555 $dbh->rollback if $oldAutoCommit;
563 warn " setting cust_main_exemption\n"
566 my $tax_exemption = delete $options{'tax_exemption'};
567 if ( $tax_exemption ) {
569 $tax_exemption = { map { $_ => '' } @$tax_exemption }
570 if ref($tax_exemption) eq 'ARRAY';
572 foreach my $taxname ( keys %$tax_exemption ) {
573 my $cust_main_exemption = new FS::cust_main_exemption {
574 'custnum' => $self->custnum,
575 'taxname' => $taxname,
576 'exempt_number' => $tax_exemption->{$taxname},
578 my $error = $cust_main_exemption->insert;
580 $dbh->rollback if $oldAutoCommit;
581 return "inserting cust_main_exemption (transaction rolled back): $error";
586 warn " ordering packages\n"
589 $error = $self->order_pkgs( $cust_pkgs,
591 'seconds_ref' => \$seconds,
592 'upbytes_ref' => \$upbytes,
593 'downbytes_ref' => \$downbytes,
594 'totalbytes_ref' => \$totalbytes,
597 $dbh->rollback if $oldAutoCommit;
602 $dbh->rollback if $oldAutoCommit;
603 return "No svc_acct record to apply pre-paid time";
605 if ( $upbytes || $downbytes || $totalbytes ) {
606 $dbh->rollback if $oldAutoCommit;
607 return "No svc_acct record to apply pre-paid data";
611 warn " inserting initial $payby payment of $amount\n"
613 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
615 $dbh->rollback if $oldAutoCommit;
616 return "inserting payment (transaction rolled back): $error";
620 unless ( $import || $skip_fuzzyfiles ) {
621 warn " queueing fuzzyfiles update\n"
623 $error = $self->queue_fuzzyfiles_update;
625 $dbh->rollback if $oldAutoCommit;
626 return "updating fuzzy search cache: $error";
630 # FS::geocode_Mixin::after_insert or something?
631 if ( $conf->config('tax_district_method') and !$import ) {
632 # if anything non-empty, try to look it up
633 my $queue = new FS::queue {
634 'job' => 'FS::geocode_Mixin::process_district_update',
635 'custnum' => $self->custnum,
637 my $error = $queue->insert( ref($self), $self->custnum );
639 $dbh->rollback if $oldAutoCommit;
640 return "queueing tax district update: $error";
645 warn " exporting\n" if $DEBUG > 1;
647 my $export_args = $options{'export_args'} || [];
650 map qsearch( 'part_export', {exportnum=>$_} ),
651 $conf->config('cust_main-exports'); #, $agentnum
653 foreach my $part_export ( @part_export ) {
654 my $error = $part_export->export_insert($self, @$export_args);
656 $dbh->rollback if $oldAutoCommit;
657 return "exporting to ". $part_export->exporttype.
658 " (transaction rolled back): $error";
662 #foreach my $depend_jobnum ( @$depend_jobnums ) {
663 # warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
665 # foreach my $jobnum ( @jobnums ) {
666 # my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
667 # warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
669 # my $error = $queue->depend_insert($depend_jobnum);
671 # $dbh->rollback if $oldAutoCommit;
672 # return "error queuing job dependancy: $error";
679 #if ( exists $options{'jobnums'} ) {
680 # push @{ $options{'jobnums'} }, @jobnums;
683 warn " insert complete; committing transaction\n"
686 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
691 use File::CounterFile;
692 sub auto_agent_custid {
695 my $format = $conf->config('cust_main-auto_agent_custid');
697 if ( $format eq '1YMMXXXXXXXX' ) {
699 my $counter = new File::CounterFile 'cust_main.agent_custid';
702 my $ym = 100000000000 + time2str('%y%m00000000', time);
703 if ( $ym > $counter->value ) {
704 $counter->{'value'} = $agent_custid = $ym;
705 $counter->{'updated'} = 1;
707 $agent_custid = $counter->inc;
713 die "Unknown cust_main-auto_agent_custid format: $format";
716 $self->agent_custid($agent_custid);
720 =item PACKAGE METHODS
722 Documentation on customer package methods has been moved to
723 L<FS::cust_main::Packages>.
725 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
727 Recharges this (existing) customer with the specified prepaid card (see
728 L<FS::prepay_credit>), specified either by I<identifier> or as an
729 FS::prepay_credit object. If there is an error, returns the error, otherwise
732 Optionally, five scalar references can be passed as well. They will have their
733 values filled in with the amount, number of seconds, and number of upload,
734 download, and total bytes applied by this prepaid card.
738 #the ref bullshit here should be refactored like get_prepay. MyAccount.pm is
739 #the only place that uses these args
740 sub recharge_prepay {
741 my( $self, $prepay_credit, $amountref, $secondsref,
742 $upbytesref, $downbytesref, $totalbytesref ) = @_;
744 local $SIG{HUP} = 'IGNORE';
745 local $SIG{INT} = 'IGNORE';
746 local $SIG{QUIT} = 'IGNORE';
747 local $SIG{TERM} = 'IGNORE';
748 local $SIG{TSTP} = 'IGNORE';
749 local $SIG{PIPE} = 'IGNORE';
751 my $oldAutoCommit = $FS::UID::AutoCommit;
752 local $FS::UID::AutoCommit = 0;
755 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
757 my $error = $self->get_prepay( $prepay_credit,
758 'amount_ref' => \$amount,
759 'seconds_ref' => \$seconds,
760 'upbytes_ref' => \$upbytes,
761 'downbytes_ref' => \$downbytes,
762 'totalbytes_ref' => \$totalbytes,
764 || $self->increment_seconds($seconds)
765 || $self->increment_upbytes($upbytes)
766 || $self->increment_downbytes($downbytes)
767 || $self->increment_totalbytes($totalbytes)
768 || $self->insert_cust_pay_prepay( $amount,
770 ? $prepay_credit->identifier
775 $dbh->rollback if $oldAutoCommit;
779 if ( defined($amountref) ) { $$amountref = $amount; }
780 if ( defined($secondsref) ) { $$secondsref = $seconds; }
781 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
782 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
783 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
785 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
790 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
792 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
793 specified either by I<identifier> or as an FS::prepay_credit object.
795 Available options are: I<amount_ref>, I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>. The scalars (provided by references) will be
796 incremented by the values of the prepaid card.
798 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
799 check or set this customer's I<agentnum>.
801 If there is an error, returns the error, otherwise returns false.
807 my( $self, $prepay_credit, %opt ) = @_;
809 local $SIG{HUP} = 'IGNORE';
810 local $SIG{INT} = 'IGNORE';
811 local $SIG{QUIT} = 'IGNORE';
812 local $SIG{TERM} = 'IGNORE';
813 local $SIG{TSTP} = 'IGNORE';
814 local $SIG{PIPE} = 'IGNORE';
816 my $oldAutoCommit = $FS::UID::AutoCommit;
817 local $FS::UID::AutoCommit = 0;
820 unless ( ref($prepay_credit) ) {
822 my $identifier = $prepay_credit;
824 $prepay_credit = qsearchs(
826 { 'identifier' => $identifier },
831 unless ( $prepay_credit ) {
832 $dbh->rollback if $oldAutoCommit;
833 return "Invalid prepaid card: ". $identifier;
838 if ( $prepay_credit->agentnum ) {
839 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
840 $dbh->rollback if $oldAutoCommit;
841 return "prepaid card not valid for agent ". $self->agentnum;
843 $self->agentnum($prepay_credit->agentnum);
846 my $error = $prepay_credit->delete;
848 $dbh->rollback if $oldAutoCommit;
849 return "removing prepay_credit (transaction rolled back): $error";
852 ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
853 for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
855 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
860 =item increment_upbytes SECONDS
862 Updates this customer's single or primary account (see L<FS::svc_acct>) by
863 the specified number of upbytes. If there is an error, returns the error,
864 otherwise returns false.
868 sub increment_upbytes {
869 _increment_column( shift, 'upbytes', @_);
872 =item increment_downbytes SECONDS
874 Updates this customer's single or primary account (see L<FS::svc_acct>) by
875 the specified number of downbytes. If there is an error, returns the error,
876 otherwise returns false.
880 sub increment_downbytes {
881 _increment_column( shift, 'downbytes', @_);
884 =item increment_totalbytes SECONDS
886 Updates this customer's single or primary account (see L<FS::svc_acct>) by
887 the specified number of totalbytes. If there is an error, returns the error,
888 otherwise returns false.
892 sub increment_totalbytes {
893 _increment_column( shift, 'totalbytes', @_);
896 =item increment_seconds SECONDS
898 Updates this customer's single or primary account (see L<FS::svc_acct>) by
899 the specified number of seconds. If there is an error, returns the error,
900 otherwise returns false.
904 sub increment_seconds {
905 _increment_column( shift, 'seconds', @_);
908 =item _increment_column AMOUNT
910 Updates this customer's single or primary account (see L<FS::svc_acct>) by
911 the specified number of seconds or bytes. If there is an error, returns
912 the error, otherwise returns false.
916 sub _increment_column {
917 my( $self, $column, $amount ) = @_;
918 warn "$me increment_column called: $column, $amount\n"
921 return '' unless $amount;
923 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
924 $self->ncancelled_pkgs;
927 return 'No packages with primary or single services found'.
928 ' to apply pre-paid time';
929 } elsif ( scalar(@cust_pkg) > 1 ) {
930 #maybe have a way to specify the package/account?
931 return 'Multiple packages found to apply pre-paid time';
934 my $cust_pkg = $cust_pkg[0];
935 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
939 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
942 return 'No account found to apply pre-paid time';
943 } elsif ( scalar(@cust_svc) > 1 ) {
944 return 'Multiple accounts found to apply pre-paid time';
947 my $svc_acct = $cust_svc[0]->svc_x;
948 warn " found service svcnum ". $svc_acct->pkgnum.
949 ' ('. $svc_acct->email. ")\n"
952 $column = "increment_$column";
953 $svc_acct->$column($amount);
957 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
959 Inserts a prepayment in the specified amount for this customer. An optional
960 second argument can specify the prepayment identifier for tracking purposes.
961 If there is an error, returns the error, otherwise returns false.
965 sub insert_cust_pay_prepay {
966 shift->insert_cust_pay('PREP', @_);
969 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
971 Inserts a cash payment in the specified amount for this customer. An optional
972 second argument can specify the payment identifier for tracking purposes.
973 If there is an error, returns the error, otherwise returns false.
977 sub insert_cust_pay_cash {
978 shift->insert_cust_pay('CASH', @_);
981 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
983 Inserts a Western Union payment in the specified amount for this customer. An
984 optional second argument can specify the prepayment identifier for tracking
985 purposes. If there is an error, returns the error, otherwise returns false.
989 sub insert_cust_pay_west {
990 shift->insert_cust_pay('WEST', @_);
993 sub insert_cust_pay {
994 my( $self, $payby, $amount ) = splice(@_, 0, 3);
995 my $payinfo = scalar(@_) ? shift : '';
997 my $cust_pay = new FS::cust_pay {
998 'custnum' => $self->custnum,
999 'paid' => sprintf('%.2f', $amount),
1000 #'_date' => #date the prepaid card was purchased???
1002 'payinfo' => $payinfo,
1008 =item delete [ OPTION => VALUE ... ]
1010 This deletes the customer. If there is an error, returns the error, otherwise
1013 This will completely remove all traces of the customer record. This is not
1014 what you want when a customer cancels service; for that, cancel all of the
1015 customer's packages (see L</cancel>).
1017 If the customer has any uncancelled packages, you need to pass a new (valid)
1018 customer number for those packages to be transferred to, as the "new_customer"
1019 option. Cancelled packages will be deleted. Did I mention that this is NOT
1020 what you want when a customer cancels service and that you really should be
1021 looking at L<FS::cust_pkg/cancel>?
1023 You can't delete a customer with invoices (see L<FS::cust_bill>),
1024 statements (see L<FS::cust_statement>), credits (see L<FS::cust_credit>),
1025 payments (see L<FS::cust_pay>) or refunds (see L<FS::cust_refund>), unless you
1026 set the "delete_financials" option to a true value.
1031 my( $self, %opt ) = @_;
1033 local $SIG{HUP} = 'IGNORE';
1034 local $SIG{INT} = 'IGNORE';
1035 local $SIG{QUIT} = 'IGNORE';
1036 local $SIG{TERM} = 'IGNORE';
1037 local $SIG{TSTP} = 'IGNORE';
1038 local $SIG{PIPE} = 'IGNORE';
1040 my $oldAutoCommit = $FS::UID::AutoCommit;
1041 local $FS::UID::AutoCommit = 0;
1044 if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1045 $dbh->rollback if $oldAutoCommit;
1046 return "Can't delete a master agent customer";
1049 #use FS::access_user
1050 if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1051 $dbh->rollback if $oldAutoCommit;
1052 return "Can't delete a master employee customer";
1055 tie my %financial_tables, 'Tie::IxHash',
1056 'cust_bill' => 'invoices',
1057 'cust_statement' => 'statements',
1058 'cust_credit' => 'credits',
1059 'cust_pay' => 'payments',
1060 'cust_refund' => 'refunds',
1063 foreach my $table ( keys %financial_tables ) {
1065 my @records = $self->$table();
1067 if ( @records && ! $opt{'delete_financials'} ) {
1068 $dbh->rollback if $oldAutoCommit;
1069 return "Can't delete a customer with ". $financial_tables{$table};
1072 foreach my $record ( @records ) {
1073 my $error = $record->delete;
1075 $dbh->rollback if $oldAutoCommit;
1076 return "Error deleting ". $financial_tables{$table}. ": $error\n";
1082 my @cust_pkg = $self->ncancelled_pkgs;
1084 my $new_custnum = $opt{'new_custnum'};
1085 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1086 $dbh->rollback if $oldAutoCommit;
1087 return "Invalid new customer number: $new_custnum";
1089 foreach my $cust_pkg ( @cust_pkg ) {
1090 my %hash = $cust_pkg->hash;
1091 $hash{'custnum'} = $new_custnum;
1092 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1093 my $error = $new_cust_pkg->replace($cust_pkg,
1094 options => { $cust_pkg->options },
1097 $dbh->rollback if $oldAutoCommit;
1102 my @cancelled_cust_pkg = $self->all_pkgs;
1103 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1104 my $error = $cust_pkg->delete;
1106 $dbh->rollback if $oldAutoCommit;
1111 #cust_tax_adjustment in financials?
1112 #cust_pay_pending? ouch
1114 foreach my $table (qw(
1115 cust_main_invoice cust_main_exemption cust_tag cust_attachment contact
1116 cust_location cust_main_note cust_tax_adjustment
1117 cust_pay_void cust_pay_batch queue cust_tax_exempt
1119 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1120 my $error = $record->delete;
1122 $dbh->rollback if $oldAutoCommit;
1128 my $sth = $dbh->prepare(
1129 'UPDATE cust_main SET referral_custnum = NULL WHERE referral_custnum = ?'
1131 my $errstr = $dbh->errstr;
1132 $dbh->rollback if $oldAutoCommit;
1135 $sth->execute($self->custnum) or do {
1136 my $errstr = $sth->errstr;
1137 $dbh->rollback if $oldAutoCommit;
1143 my $ticket_dbh = '';
1144 if ($conf->config('ticket_system') eq 'RT_Internal') {
1146 } elsif ($conf->config('ticket_system') eq 'RT_External') {
1147 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1148 $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1149 #or die "RT_External DBI->connect error: $DBI::errstr\n";
1152 if ( $ticket_dbh ) {
1154 my $ticket_sth = $ticket_dbh->prepare(
1155 'DELETE FROM Links WHERE Target = ?'
1157 my $errstr = $ticket_dbh->errstr;
1158 $dbh->rollback if $oldAutoCommit;
1161 $ticket_sth->execute('freeside://freeside/cust_main/'.$self->custnum)
1163 my $errstr = $ticket_sth->errstr;
1164 $dbh->rollback if $oldAutoCommit;
1168 #check and see if the customer is the only link on the ticket, and
1169 #if so, set the ticket to deleted status in RT?
1170 #maybe someday, for now this will at least fix tickets not displaying
1174 #delete the customer record
1176 my $error = $self->SUPER::delete;
1178 $dbh->rollback if $oldAutoCommit;
1182 # cust_main exports!
1184 #my $export_args = $options{'export_args'} || [];
1187 map qsearch( 'part_export', {exportnum=>$_} ),
1188 $conf->config('cust_main-exports'); #, $agentnum
1190 foreach my $part_export ( @part_export ) {
1191 my $error = $part_export->export_delete( $self ); #, @$export_args);
1193 $dbh->rollback if $oldAutoCommit;
1194 return "exporting to ". $part_export->exporttype.
1195 " (transaction rolled back): $error";
1199 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1204 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
1206 Replaces the OLD_RECORD with this one in the database. If there is an error,
1207 returns the error, otherwise returns false.
1209 To change the customer's address, set the pseudo-fields C<bill_location> and
1210 C<ship_location>. The address will still only change if at least one of the
1211 address fields differs from the existing values.
1213 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1214 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1215 expected and rollback the entire transaction; it is not necessary to call
1216 check_invoicing_list first. Here's an example:
1218 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1220 Currently available options are: I<tax_exemption>.
1222 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
1223 of tax names and exemption numbers. FS::cust_main_exemption records will be
1224 deleted and inserted as appropriate.
1231 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1233 : $self->replace_old;
1237 warn "$me replace called\n"
1240 my $curuser = $FS::CurrentUser::CurrentUser;
1241 if ( $self->payby eq 'COMP'
1242 && $self->payby ne $old->payby
1243 && ! $curuser->access_right('Complimentary customer')
1246 return "You are not permitted to create complimentary accounts.";
1249 local($ignore_expired_card) = 1
1250 if $old->payby =~ /^(CARD|DCRD)$/
1251 && $self->payby =~ /^(CARD|DCRD)$/
1252 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1254 local($ignore_banned_card) = 1
1255 if ( $old->payby =~ /^(CARD|DCRD)$/ && $self->payby =~ /^(CARD|DCRD)$/
1256 || $old->payby =~ /^(CHEK|DCHK)$/ && $self->payby =~ /^(CHEK|DCHK)$/ )
1257 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1259 return "Invoicing locale is required"
1262 && $conf->exists('cust_main-require_locale');
1264 local $SIG{HUP} = 'IGNORE';
1265 local $SIG{INT} = 'IGNORE';
1266 local $SIG{QUIT} = 'IGNORE';
1267 local $SIG{TERM} = 'IGNORE';
1268 local $SIG{TSTP} = 'IGNORE';
1269 local $SIG{PIPE} = 'IGNORE';
1271 my $oldAutoCommit = $FS::UID::AutoCommit;
1272 local $FS::UID::AutoCommit = 0;
1275 for my $l (qw(bill_location ship_location)) {
1276 my $old_loc = $old->$l;
1277 my $new_loc = $self->$l;
1279 # find the existing location if there is one
1280 $new_loc->set('custnum' => $self->custnum);
1281 my $error = $new_loc->find_or_insert;
1283 $dbh->rollback if $oldAutoCommit;
1286 $self->set($l.'num', $new_loc->locationnum);
1289 # replace the customer record
1290 my $error = $self->SUPER::replace($old);
1293 $dbh->rollback if $oldAutoCommit;
1297 # now move packages to the new service location
1298 $self->set('ship_location', ''); #flush cache
1299 if ( $old->ship_locationnum and # should only be null during upgrade...
1300 $old->ship_locationnum != $self->ship_locationnum ) {
1301 $error = $old->ship_location->move_to($self->ship_location);
1303 $dbh->rollback if $oldAutoCommit;
1307 # don't move packages based on the billing location, but
1308 # disable it if it's no longer in use
1309 if ( $old->bill_locationnum and
1310 $old->bill_locationnum != $self->bill_locationnum ) {
1311 $error = $old->bill_location->disable_if_unused;
1313 $dbh->rollback if $oldAutoCommit;
1318 if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
1319 my $invoicing_list = shift @param;
1320 $error = $self->check_invoicing_list( $invoicing_list );
1322 $dbh->rollback if $oldAutoCommit;
1325 $self->invoicing_list( $invoicing_list );
1328 if ( $self->exists('tagnum') ) { #so we don't delete these on edit by accident
1330 #this could be more efficient than deleting and re-inserting, if it matters
1331 foreach my $cust_tag (qsearch('cust_tag', {'custnum'=>$self->custnum} )) {
1332 my $error = $cust_tag->delete;
1334 $dbh->rollback if $oldAutoCommit;
1338 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
1339 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
1340 'custnum' => $self->custnum };
1341 my $error = $cust_tag->insert;
1343 $dbh->rollback if $oldAutoCommit;
1350 my %options = @param;
1352 my $tax_exemption = delete $options{'tax_exemption'};
1353 if ( $tax_exemption ) {
1355 $tax_exemption = { map { $_ => '' } @$tax_exemption }
1356 if ref($tax_exemption) eq 'ARRAY';
1358 my %cust_main_exemption =
1359 map { $_->taxname => $_ }
1360 qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
1362 foreach my $taxname ( keys %$tax_exemption ) {
1364 if ( $cust_main_exemption{$taxname} &&
1365 $cust_main_exemption{$taxname}->exempt_number eq $tax_exemption->{$taxname}
1368 delete $cust_main_exemption{$taxname};
1372 my $cust_main_exemption = new FS::cust_main_exemption {
1373 'custnum' => $self->custnum,
1374 'taxname' => $taxname,
1375 'exempt_number' => $tax_exemption->{$taxname},
1377 my $error = $cust_main_exemption->insert;
1379 $dbh->rollback if $oldAutoCommit;
1380 return "inserting cust_main_exemption (transaction rolled back): $error";
1384 foreach my $cust_main_exemption ( values %cust_main_exemption ) {
1385 my $error = $cust_main_exemption->delete;
1387 $dbh->rollback if $oldAutoCommit;
1388 return "deleting cust_main_exemption (transaction rolled back): $error";
1394 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/
1395 && ( ( $self->get('payinfo') ne $old->get('payinfo')
1396 && $self->get('payinfo') !~ /^99\d{14}$/
1398 || grep { $self->get($_) ne $old->get($_) } qw(paydate payname)
1403 # card/check/lec info has changed, want to retry realtime_ invoice events
1404 my $error = $self->retry_realtime;
1406 $dbh->rollback if $oldAutoCommit;
1411 unless ( $import || $skip_fuzzyfiles ) {
1412 $error = $self->queue_fuzzyfiles_update;
1414 $dbh->rollback if $oldAutoCommit;
1415 return "updating fuzzy search cache: $error";
1419 # tax district update in cust_location
1421 # cust_main exports!
1423 my $export_args = $options{'export_args'} || [];
1426 map qsearch( 'part_export', {exportnum=>$_} ),
1427 $conf->config('cust_main-exports'); #, $agentnum
1429 foreach my $part_export ( @part_export ) {
1430 my $error = $part_export->export_replace( $self, $old, @$export_args);
1432 $dbh->rollback if $oldAutoCommit;
1433 return "exporting to ". $part_export->exporttype.
1434 " (transaction rolled back): $error";
1438 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1443 =item queue_fuzzyfiles_update
1445 Used by insert & replace to update the fuzzy search cache
1449 use FS::cust_main::Search;
1450 sub queue_fuzzyfiles_update {
1453 local $SIG{HUP} = 'IGNORE';
1454 local $SIG{INT} = 'IGNORE';
1455 local $SIG{QUIT} = 'IGNORE';
1456 local $SIG{TERM} = 'IGNORE';
1457 local $SIG{TSTP} = 'IGNORE';
1458 local $SIG{PIPE} = 'IGNORE';
1460 my $oldAutoCommit = $FS::UID::AutoCommit;
1461 local $FS::UID::AutoCommit = 0;
1464 foreach my $field ( 'first', 'last', 'company', 'ship_company' ) {
1465 my $queue = new FS::queue {
1466 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1468 my @args = "cust_main.$field", $self->get($field);
1469 my $error = $queue->insert( @args );
1471 $dbh->rollback if $oldAutoCommit;
1472 return "queueing job (transaction rolled back): $error";
1477 push @locations, $self->bill_location if $self->bill_locationnum;
1478 push @locations, $self->ship_location if @locations && $self->has_ship_address;
1479 foreach my $location (@locations) {
1480 my $queue = new FS::queue {
1481 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1483 my @args = 'cust_location.address1', $location->address1;
1484 my $error = $queue->insert( @args );
1486 $dbh->rollback if $oldAutoCommit;
1487 return "queueing job (transaction rolled back): $error";
1491 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1498 Checks all fields to make sure this is a valid customer record. If there is
1499 an error, returns the error, otherwise returns false. Called by the insert
1500 and replace methods.
1507 warn "$me check BEFORE: \n". $self->_dump
1511 $self->ut_numbern('custnum')
1512 || $self->ut_number('agentnum')
1513 || $self->ut_textn('agent_custid')
1514 || $self->ut_number('refnum')
1515 || $self->ut_foreign_keyn('bill_locationnum', 'cust_location','locationnum')
1516 || $self->ut_foreign_keyn('ship_locationnum', 'cust_location','locationnum')
1517 || $self->ut_foreign_keyn('classnum', 'cust_class', 'classnum')
1518 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
1519 || $self->ut_foreign_keyn('taxstatusnum', 'tax_status', 'taxstatusnum')
1520 || $self->ut_textn('custbatch')
1521 || $self->ut_name('last')
1522 || $self->ut_name('first')
1523 || $self->ut_snumbern('signupdate')
1524 || $self->ut_snumbern('birthdate')
1525 || $self->ut_namen('spouse_last')
1526 || $self->ut_namen('spouse_first')
1527 || $self->ut_snumbern('spouse_birthdate')
1528 || $self->ut_snumbern('anniversary_date')
1529 || $self->ut_textn('company')
1530 || $self->ut_textn('ship_company')
1531 || $self->ut_anything('comments')
1532 || $self->ut_numbern('referral_custnum')
1533 || $self->ut_textn('stateid')
1534 || $self->ut_textn('stateid_state')
1535 || $self->ut_textn('invoice_terms')
1536 || $self->ut_floatn('cdr_termination_percentage')
1537 || $self->ut_floatn('credit_limit')
1538 || $self->ut_numbern('billday')
1539 || $self->ut_numbern('prorate_day')
1540 || $self->ut_flag('edit_subject')
1541 || $self->ut_flag('calling_list_exempt')
1542 || $self->ut_flag('invoice_noemail')
1543 || $self->ut_flag('message_noemail')
1544 || $self->ut_enum('locale', [ '', FS::Locales->locales ])
1545 || $self->ut_currencyn('currency')
1548 foreach (qw(company ship_company)) {
1549 my $company = $self->get($_);
1550 $company =~ s/^\s+//;
1551 $company =~ s/\s+$//;
1552 $company =~ s/\s+/ /g;
1553 $self->set($_, $company);
1556 #barf. need message catalogs. i18n. etc.
1557 $error .= "Please select an advertising source."
1558 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1559 return $error if $error;
1561 my $agent = qsearchs( 'agent', { 'agentnum' => $self->agentnum } )
1562 or return "Unknown agent";
1564 if ( $self->currency ) {
1565 my $agent_currency = qsearchs( 'agent_currency', {
1566 'agentnum' => $agent->agentnum,
1567 'currency' => $self->currency,
1569 or return "Agent ". $agent->agent.
1570 " not permitted to offer ". $self->currency. " invoicing";
1573 return "Unknown refnum"
1574 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1576 return "Unknown referring custnum: ". $self->referral_custnum
1577 unless ! $self->referral_custnum
1578 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1580 if ( $self->ss eq '' ) {
1585 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1586 or return "Illegal social security number: ". $self->ss;
1587 $self->ss("$1-$2-$3");
1590 # cust_main_county verification now handled by cust_location check
1593 $self->ut_phonen('daytime', $self->country)
1594 || $self->ut_phonen('night', $self->country)
1595 || $self->ut_phonen('fax', $self->country)
1596 || $self->ut_phonen('mobile', $self->country)
1598 return $error if $error;
1600 if ( $conf->exists('cust_main-require_phone', $self->agentnum)
1602 && ! length($self->daytime) && ! length($self->night) && ! length($self->mobile)
1605 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1607 : FS::Msgcat::_gettext('daytime');
1608 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1610 : FS::Msgcat::_gettext('night');
1612 my $mobile_label = FS::Msgcat::_gettext('mobile') =~ /^(mobile)?$/
1614 : FS::Msgcat::_gettext('mobile');
1616 return "$daytime_label, $night_label or $mobile_label is required"
1620 ### start of stuff moved to cust_payby
1621 # then mostly kept here to support upgrades (can remove in 5.x)
1622 # but modified to allow everything to be empty
1624 if ( $self->payby ) {
1625 FS::payby->can_payby($self->table, $self->payby)
1626 or return "Illegal payby: ". $self->payby;
1631 $error = $self->ut_numbern('paystart_month')
1632 || $self->ut_numbern('paystart_year')
1633 || $self->ut_numbern('payissue')
1634 || $self->ut_textn('paytype')
1636 return $error if $error;
1638 if ( $self->payip eq '' ) {
1641 $error = $self->ut_ip('payip');
1642 return $error if $error;
1645 # If it is encrypted and the private key is not availaible then we can't
1646 # check the credit card.
1647 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
1649 # Need some kind of global flag to accept invalid cards, for testing
1651 if ( !$import && !$ignore_invalid_card && $check_payinfo &&
1652 $self->payby =~ /^(CARD|DCRD)$/ ) {
1654 my $payinfo = $self->payinfo;
1655 $payinfo =~ s/\D//g;
1656 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1657 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1659 $self->payinfo($payinfo);
1661 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1663 return gettext('unknown_card_type')
1664 if $self->payinfo !~ /^99\d{14}$/ #token
1665 && cardtype($self->payinfo) eq "Unknown";
1667 unless ( $ignore_banned_card ) {
1668 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
1670 if ( $ban->bantype eq 'warn' ) {
1671 #or others depending on value of $ban->reason ?
1672 return '_duplicate_card'.
1673 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
1674 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
1675 ' (ban# '. $ban->bannum. ')'
1676 unless $self->override_ban_warn;
1678 return 'Banned credit card: banned on '.
1679 time2str('%a %h %o at %r', $ban->_date).
1680 ' by '. $ban->otaker.
1681 ' (ban# '. $ban->bannum. ')';
1686 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1687 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1688 $self->paycvv =~ /^(\d{4})$/
1689 or return "CVV2 (CID) for American Express cards is four digits.";
1692 $self->paycvv =~ /^(\d{3})$/
1693 or return "CVV2 (CVC2/CID) is three digits.";
1700 my $cardtype = cardtype($payinfo);
1701 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1703 return "Start date or issue number is required for $cardtype cards"
1704 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1706 return "Start month must be between 1 and 12"
1707 if $self->paystart_month
1708 and $self->paystart_month < 1 || $self->paystart_month > 12;
1710 return "Start year must be 1990 or later"
1711 if $self->paystart_year
1712 and $self->paystart_year < 1990;
1714 return "Issue number must be beween 1 and 99"
1716 and $self->payissue < 1 || $self->payissue > 99;
1719 $self->paystart_month('');
1720 $self->paystart_year('');
1721 $self->payissue('');
1724 } elsif ( !$ignore_invalid_card && $check_payinfo &&
1725 $self->payby =~ /^(CHEK|DCHK)$/ ) {
1727 my $payinfo = $self->payinfo;
1728 $payinfo =~ s/[^\d\@\.]//g;
1729 if ( $conf->config('echeck-country') eq 'CA' ) {
1730 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
1731 or return 'invalid echeck account@branch.bank';
1732 $payinfo = "$1\@$2.$3";
1733 } elsif ( $conf->config('echeck-country') eq 'US' ) {
1734 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1735 $payinfo = "$1\@$2";
1737 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
1738 $payinfo = "$1\@$2";
1740 $self->payinfo($payinfo);
1743 unless ( $ignore_banned_card ) {
1744 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
1746 if ( $ban->bantype eq 'warn' ) {
1747 #or others depending on value of $ban->reason ?
1748 return '_duplicate_ach' unless $self->override_ban_warn;
1750 return 'Banned ACH account: banned on '.
1751 time2str('%a %h %o at %r', $ban->_date).
1752 ' by '. $ban->otaker.
1753 ' (ban# '. $ban->bannum. ')';
1758 } elsif ( $self->payby eq 'LECB' ) {
1760 my $payinfo = $self->payinfo;
1761 $payinfo =~ s/\D//g;
1762 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1764 $self->payinfo($payinfo);
1767 } elsif ( $self->payby eq 'BILL' ) {
1769 $error = $self->ut_textn('payinfo');
1770 return "Illegal P.O. number: ". $self->payinfo if $error;
1773 } elsif ( $self->payby eq 'COMP' ) {
1775 my $curuser = $FS::CurrentUser::CurrentUser;
1776 if ( ! $self->custnum
1777 && ! $curuser->access_right('Complimentary customer')
1780 return "You are not permitted to create complimentary accounts."
1783 $error = $self->ut_textn('payinfo');
1784 return "Illegal comp account issuer: ". $self->payinfo if $error;
1787 } elsif ( $self->payby eq 'PREPAY' ) {
1789 my $payinfo = $self->payinfo;
1790 $payinfo =~ s/\W//g; #anything else would just confuse things
1791 $self->payinfo($payinfo);
1792 $error = $self->ut_alpha('payinfo');
1793 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1794 return "Unknown prepayment identifier"
1795 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1800 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1801 return "Expiration date required"
1802 # shouldn't payinfo_check do this?
1803 unless ! $self->payby
1804 || $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
1808 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1809 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1810 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1811 ( $m, $y ) = ( $2, "19$1" );
1812 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1813 ( $m, $y ) = ( $3, "20$2" );
1815 return "Illegal expiration date: ". $self->paydate;
1817 $m = sprintf('%02d',$m);
1818 $self->paydate("$y-$m-01");
1819 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1820 return gettext('expired_card')
1822 && !$ignore_expired_card
1823 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1826 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1827 ( ! $conf->exists('require_cardname')
1828 || $self->payby !~ /^(CARD|DCRD)$/ )
1830 $self->payname( $self->first. " ". $self->getfield('last') );
1833 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
1834 $self->payname =~ /^([\w \,\.\-\']*)$/
1835 or return gettext('illegal_name'). " payname: ". $self->payname;
1838 $self->payname =~ /^([\w \,\.\-\'\&]*)$/
1839 or return gettext('illegal_name'). " payname: ". $self->payname;
1845 ### end of stuff moved to cust_payby
1847 return "Please select an invoicing locale"
1850 && $conf->exists('cust_main-require_locale');
1852 foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
1853 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1857 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
1859 warn "$me check AFTER: \n". $self->_dump
1862 $self->SUPER::check;
1867 Returns a list of fields which have ship_ duplicates.
1872 qw( last first company
1874 address1 address2 city county state zip country
1876 daytime night fax mobile
1880 =item has_ship_address
1882 Returns true if this customer record has a separate shipping address.
1886 sub has_ship_address {
1888 $self->bill_locationnum != $self->ship_locationnum;
1893 Returns a list of key/value pairs, with the following keys: address1,
1894 adddress2, city, county, state, zip, country, district, and geocode. The
1895 shipping address is used if present.
1901 $self->ship_location->location_hash;
1906 Returns all locations (see L<FS::cust_location>) for this customer.
1912 qsearch('cust_location', { 'custnum' => $self->custnum,
1913 'prospectnum' => '' } );
1918 Returns all contacts (see L<FS::contact>) for this customer.
1922 #already used :/ sub contact {
1925 qsearch('contact', { 'custnum' => $self->custnum } );
1930 Returns all payment methods (see L<FS::cust_payby>) for this customer.
1937 'table' => 'cust_payby',
1938 'hashref' => { 'custnum' => $self->custnum },
1939 'order_by' => 'ORDER BY weight ASC',
1945 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1946 and L<FS::cust_pkg>) for this customer, except those on hold.
1948 Returns a list: an empty list on success or a list of errors.
1954 grep { ($_->get('setup')) && $_->unsuspend } $self->suspended_pkgs;
1959 Unsuspends all suspended packages in the on-hold state (those without setup
1960 dates) for this customer.
1966 grep { (!$_->setup) && $_->unsuspend } $self->suspended_pkgs;
1971 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1973 Returns a list: an empty list on success or a list of errors.
1979 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1982 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1984 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1985 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
1986 of a list of pkgparts; the hashref has the following keys:
1990 =item pkgparts - listref of pkgparts
1992 =item (other options are passed to the suspend method)
1997 Returns a list: an empty list on success or a list of errors.
2001 sub suspend_if_pkgpart {
2003 my (@pkgparts, %opt);
2004 if (ref($_[0]) eq 'HASH'){
2005 @pkgparts = @{$_[0]{pkgparts}};
2010 grep { $_->suspend(%opt) }
2011 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
2012 $self->unsuspended_pkgs;
2015 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2017 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
2018 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
2019 instead of a list of pkgparts; the hashref has the following keys:
2023 =item pkgparts - listref of pkgparts
2025 =item (other options are passed to the suspend method)
2029 Returns a list: an empty list on success or a list of errors.
2033 sub suspend_unless_pkgpart {
2035 my (@pkgparts, %opt);
2036 if (ref($_[0]) eq 'HASH'){
2037 @pkgparts = @{$_[0]{pkgparts}};
2042 grep { $_->suspend(%opt) }
2043 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
2044 $self->unsuspended_pkgs;
2047 =item cancel [ OPTION => VALUE ... ]
2049 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
2051 Available options are:
2055 =item quiet - can be set true to supress email cancellation notices.
2057 =item reason - can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
2059 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
2061 =item nobill - can be set true to skip billing if it might otherwise be done.
2065 Always returns a list: an empty list on success or a list of errors.
2069 # nb that dates are not specified as valid options to this method
2072 my( $self, %opt ) = @_;
2074 warn "$me cancel called on customer ". $self->custnum. " with options ".
2075 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
2078 return ( 'access denied' )
2079 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
2081 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
2083 #should try decryption (we might have the private key)
2084 # and if not maybe queue a job for the server that does?
2085 return ( "Can't (yet) ban encrypted credit cards" )
2086 if $self->is_encrypted($self->payinfo);
2088 my $ban = new FS::banned_pay $self->_new_banned_pay_hashref;
2089 my $error = $ban->insert;
2090 return ( $error ) if $error;
2094 my @pkgs = $self->ncancelled_pkgs;
2096 if ( !$opt{nobill} && $conf->exists('bill_usage_on_cancel') ) {
2098 my $error = $self->bill( pkg_list => [ @pkgs ], cancel => 1 );
2099 warn "Error billing during cancel, custnum ". $self->custnum. ": $error"
2103 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
2104 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
2107 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
2110 sub _banned_pay_hashref {
2121 'payby' => $payby2ban{$self->payby},
2122 'payinfo' => $self->payinfo,
2123 #don't ever *search* on reason! #'reason' =>
2127 sub _new_banned_pay_hashref {
2129 my $hr = $self->_banned_pay_hashref;
2130 $hr->{payinfo} = md5_base64($hr->{payinfo});
2136 Returns all notes (see L<FS::cust_main_note>) for this customer.
2141 my($self,$orderby_classnum) = (shift,shift);
2142 my $orderby = "_DATE DESC";
2143 $orderby = "CLASSNUM ASC, $orderby" if $orderby_classnum;
2144 qsearch( 'cust_main_note',
2145 { 'custnum' => $self->custnum },
2147 "ORDER BY $orderby",
2153 Returns the agent (see L<FS::agent>) for this customer.
2157 Returns the agent name (see L<FS::agent>) for this customer.
2163 $self->agent->agent;
2168 Returns any tags associated with this customer, as FS::cust_tag objects,
2169 or an empty list if there are no tags.
2173 Returns any tags associated with this customer, as FS::part_tag objects,
2174 or an empty list if there are no tags.
2180 map $_->part_tag, $self->cust_tag;
2186 Returns the customer class, as an FS::cust_class object, or the empty string
2187 if there is no customer class.
2191 Returns the customer category name, or the empty string if there is no customer
2198 my $cust_class = $self->cust_class;
2200 ? $cust_class->categoryname
2206 Returns the customer class name, or the empty string if there is no customer
2213 my $cust_class = $self->cust_class;
2215 ? $cust_class->classname
2221 Returns the external tax status, as an FS::tax_status object, or the empty
2222 string if there is no tax status.
2228 if ( $self->taxstatusnum ) {
2229 qsearchs('tax_status', { 'taxstatusnum' => $self->taxstatusnum } );
2237 Returns the tax status code if there is one.
2243 my $tax_status = $self->tax_status;
2245 ? $tax_status->taxstatus
2249 =item BILLING METHODS
2251 Documentation on billing methods has been moved to
2252 L<FS::cust_main::Billing>.
2254 =item REALTIME BILLING METHODS
2256 Documentation on realtime billing methods has been moved to
2257 L<FS::cust_main::Billing_Realtime>.
2261 Removes the I<paycvv> field from the database directly.
2263 If there is an error, returns the error, otherwise returns false.
2269 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2270 or return dbh->errstr;
2271 $sth->execute($self->custnum)
2272 or return $sth->errstr;
2279 Returns the total owed for this customer on all invoices
2280 (see L<FS::cust_bill/owed>).
2286 $self->total_owed_date(2145859200); #12/31/2037
2289 =item total_owed_date TIME
2291 Returns the total owed for this customer on all invoices with date earlier than
2292 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2293 see L<Time::Local> and L<Date::Parse> for conversion functions.
2297 sub total_owed_date {
2301 my $custnum = $self->custnum;
2303 my $owed_sql = FS::cust_bill->owed_sql;
2306 SELECT SUM($owed_sql) FROM cust_bill
2307 WHERE custnum = $custnum
2311 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2315 =item total_owed_pkgnum PKGNUM
2317 Returns the total owed on all invoices for this customer's specific package
2318 when using experimental package balances (see L<FS::cust_bill/owed_pkgnum>).
2322 sub total_owed_pkgnum {
2323 my( $self, $pkgnum ) = @_;
2324 $self->total_owed_date_pkgnum(2145859200, $pkgnum); #12/31/2037
2327 =item total_owed_date_pkgnum TIME PKGNUM
2329 Returns the total owed for this customer's specific package when using
2330 experimental package balances on all invoices with date earlier than
2331 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2332 see L<Time::Local> and L<Date::Parse> for conversion functions.
2336 sub total_owed_date_pkgnum {
2337 my( $self, $time, $pkgnum ) = @_;
2340 foreach my $cust_bill (
2341 grep { $_->_date <= $time }
2342 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2344 $total_bill += $cust_bill->owed_pkgnum($pkgnum);
2346 sprintf( "%.2f", $total_bill );
2352 Returns the total amount of all payments.
2359 $total += $_->paid foreach $self->cust_pay;
2360 sprintf( "%.2f", $total );
2363 =item total_unapplied_credits
2365 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2366 customer. See L<FS::cust_credit/credited>.
2368 =item total_credited
2370 Old name for total_unapplied_credits. Don't use.
2374 sub total_credited {
2375 #carp "total_credited deprecated, use total_unapplied_credits";
2376 shift->total_unapplied_credits(@_);
2379 sub total_unapplied_credits {
2382 my $custnum = $self->custnum;
2384 my $unapplied_sql = FS::cust_credit->unapplied_sql;
2387 SELECT SUM($unapplied_sql) FROM cust_credit
2388 WHERE custnum = $custnum
2391 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2395 =item total_unapplied_credits_pkgnum PKGNUM
2397 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2398 customer. See L<FS::cust_credit/credited>.
2402 sub total_unapplied_credits_pkgnum {
2403 my( $self, $pkgnum ) = @_;
2404 my $total_credit = 0;
2405 $total_credit += $_->credited foreach $self->cust_credit_pkgnum($pkgnum);
2406 sprintf( "%.2f", $total_credit );
2410 =item total_unapplied_payments
2412 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
2413 See L<FS::cust_pay/unapplied>.
2417 sub total_unapplied_payments {
2420 my $custnum = $self->custnum;
2422 my $unapplied_sql = FS::cust_pay->unapplied_sql;
2425 SELECT SUM($unapplied_sql) FROM cust_pay
2426 WHERE custnum = $custnum
2429 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2433 =item total_unapplied_payments_pkgnum PKGNUM
2435 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer's
2436 specific package when using experimental package balances. See
2437 L<FS::cust_pay/unapplied>.
2441 sub total_unapplied_payments_pkgnum {
2442 my( $self, $pkgnum ) = @_;
2443 my $total_unapplied = 0;
2444 $total_unapplied += $_->unapplied foreach $self->cust_pay_pkgnum($pkgnum);
2445 sprintf( "%.2f", $total_unapplied );
2449 =item total_unapplied_refunds
2451 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
2452 customer. See L<FS::cust_refund/unapplied>.
2456 sub total_unapplied_refunds {
2458 my $custnum = $self->custnum;
2460 my $unapplied_sql = FS::cust_refund->unapplied_sql;
2463 SELECT SUM($unapplied_sql) FROM cust_refund
2464 WHERE custnum = $custnum
2467 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2473 Returns the balance for this customer (total_owed plus total_unrefunded, minus
2474 total_unapplied_credits minus total_unapplied_payments).
2480 $self->balance_date_range;
2483 =item balance_date TIME
2485 Returns the balance for this customer, only considering invoices with date
2486 earlier than TIME (total_owed_date minus total_credited minus
2487 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
2488 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
2495 $self->balance_date_range(shift);
2498 =item balance_date_range [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
2500 Returns the balance for this customer, optionally considering invoices with
2501 date earlier than START_TIME, and not later than END_TIME
2502 (total_owed_date minus total_unapplied_credits minus total_unapplied_payments).
2504 Times are specified as SQL fragments or numeric
2505 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
2506 L<Date::Parse> for conversion functions. The empty string can be passed
2507 to disable that time constraint completely.
2509 Accepts the same options as L<balance_date_sql>:
2513 =item unapplied_date
2515 set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
2519 An absolute cutoff time. Payments, credits, and refunds I<applied> after this
2520 time will be ignored. Note that START_TIME and END_TIME only limit the date
2521 range for invoices and I<unapplied> payments, credits, and refunds.
2527 sub balance_date_range {
2529 my $sql = 'SELECT SUM('. $self->balance_date_sql(@_).
2530 ') FROM cust_main WHERE custnum='. $self->custnum;
2531 sprintf( '%.2f', $self->scalar_sql($sql) || 0 );
2534 =item balance_pkgnum PKGNUM
2536 Returns the balance for this customer's specific package when using
2537 experimental package balances (total_owed plus total_unrefunded, minus
2538 total_unapplied_credits minus total_unapplied_payments)
2542 sub balance_pkgnum {
2543 my( $self, $pkgnum ) = @_;
2546 $self->total_owed_pkgnum($pkgnum)
2547 # n/a - refunds aren't part of pkg-balances since they don't apply to invoices
2548 # + $self->total_unapplied_refunds_pkgnum($pkgnum)
2549 - $self->total_unapplied_credits_pkgnum($pkgnum)
2550 - $self->total_unapplied_payments_pkgnum($pkgnum)
2556 Returns a hash of useful information for making a payment.
2566 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
2567 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
2568 'LECB' (Phone bill billing), 'BILL' (billing), or 'COMP' (free).
2572 For credit card transactions:
2584 For electronic check transactions:
2599 $return{balance} = $self->balance;
2601 $return{payname} = $self->payname
2602 || ( $self->first. ' '. $self->get('last') );
2604 $return{$_} = $self->bill_location->$_
2605 for qw(address1 address2 city state zip);
2607 $return{payby} = $self->payby;
2608 $return{stateid_state} = $self->stateid_state;
2610 if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
2611 $return{card_type} = cardtype($self->payinfo);
2612 $return{payinfo} = $self->paymask;
2614 @return{'month', 'year'} = $self->paydate_monthyear;
2618 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
2619 my ($payinfo1, $payinfo2) = split '@', $self->paymask;
2620 $return{payinfo1} = $payinfo1;
2621 $return{payinfo2} = $payinfo2;
2622 $return{paytype} = $self->paytype;
2623 $return{paystate} = $self->paystate;
2627 #doubleclick protection
2629 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
2635 =item paydate_monthyear
2637 Returns a two-element list consisting of the month and year of this customer's
2638 paydate (credit card expiration date for CARD customers)
2642 sub paydate_monthyear {
2644 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
2646 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
2655 Returns the exact time in seconds corresponding to the payment method
2656 expiration date. For CARD/DCRD customers this is the end of the month;
2657 for others (COMP is the only other payby that uses paydate) it's the start.
2658 Returns 0 if the paydate is empty or set to the far future.
2664 my ($month, $year) = $self->paydate_monthyear;
2665 return 0 if !$year or $year >= 2037;
2666 if ( $self->payby eq 'CARD' or $self->payby eq 'DCRD' ) {
2668 if ( $month == 13 ) {
2672 return timelocal(0,0,0,1,$month-1,$year) - 1;
2675 return timelocal(0,0,0,1,$month-1,$year);
2679 =item paydate_epoch_sql
2681 Class method. Returns an SQL expression to obtain the payment expiration date
2682 as a number of seconds.
2686 # Special expiration date behavior for non-CARD/DCRD customers has been
2687 # carefully preserved. Do we really use that?
2688 sub paydate_epoch_sql {
2690 my $table = shift || 'cust_main';
2691 my ($case1, $case2);
2692 if ( driver_name eq 'Pg' ) {
2693 $case1 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) + INTERVAL '1 month') - 1";
2694 $case2 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) )";
2696 elsif ( lc(driver_name) eq 'mysql' ) {
2697 $case1 = "UNIX_TIMESTAMP( DATE_ADD( CAST( $table.paydate AS DATETIME ), INTERVAL 1 month ) ) - 1";
2698 $case2 = "UNIX_TIMESTAMP( CAST( $table.paydate AS DATETIME ) )";
2701 return "CASE WHEN $table.payby IN('CARD','DCRD')
2707 =item tax_exemption TAXNAME
2712 my( $self, $taxname ) = @_;
2714 qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
2715 'taxname' => $taxname,
2720 =item cust_main_exemption
2722 =item invoicing_list [ ARRAYREF ]
2724 If an arguement is given, sets these email addresses as invoice recipients
2725 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
2726 (except as warnings), so use check_invoicing_list first.
2728 Returns a list of email addresses (with svcnum entries expanded).
2730 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
2731 check it without disturbing anything by passing nothing.
2733 This interface may change in the future.
2737 sub invoicing_list {
2738 my( $self, $arrayref ) = @_;
2741 my @cust_main_invoice;
2742 if ( $self->custnum ) {
2743 @cust_main_invoice =
2744 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2746 @cust_main_invoice = ();
2748 foreach my $cust_main_invoice ( @cust_main_invoice ) {
2749 #warn $cust_main_invoice->destnum;
2750 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
2751 #warn $cust_main_invoice->destnum;
2752 my $error = $cust_main_invoice->delete;
2753 warn $error if $error;
2756 if ( $self->custnum ) {
2757 @cust_main_invoice =
2758 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2760 @cust_main_invoice = ();
2762 my %seen = map { $_->address => 1 } @cust_main_invoice;
2763 foreach my $address ( @{$arrayref} ) {
2764 next if exists $seen{$address} && $seen{$address};
2765 $seen{$address} = 1;
2766 my $cust_main_invoice = new FS::cust_main_invoice ( {
2767 'custnum' => $self->custnum,
2770 my $error = $cust_main_invoice->insert;
2771 warn $error if $error;
2775 if ( $self->custnum ) {
2777 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2784 =item check_invoicing_list ARRAYREF
2786 Checks these arguements as valid input for the invoicing_list method. If there
2787 is an error, returns the error, otherwise returns false.
2791 sub check_invoicing_list {
2792 my( $self, $arrayref ) = @_;
2794 foreach my $address ( @$arrayref ) {
2796 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
2797 return 'Can\'t add FAX invoice destination with a blank FAX number.';
2800 my $cust_main_invoice = new FS::cust_main_invoice ( {
2801 'custnum' => $self->custnum,
2804 my $error = $self->custnum
2805 ? $cust_main_invoice->check
2806 : $cust_main_invoice->checkdest
2808 return $error if $error;
2812 return "Email address required"
2813 if $conf->exists('cust_main-require_invoicing_list_email', $self->agentnum)
2814 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
2819 =item set_default_invoicing_list
2821 Sets the invoicing list to all accounts associated with this customer,
2822 overwriting any previous invoicing list.
2826 sub set_default_invoicing_list {
2828 $self->invoicing_list($self->all_emails);
2833 Returns the email addresses of all accounts provisioned for this customer.
2840 foreach my $cust_pkg ( $self->all_pkgs ) {
2841 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
2843 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2844 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2846 $list{$_}=1 foreach map { $_->email } @svc_acct;
2851 =item invoicing_list_addpost
2853 Adds postal invoicing to this customer. If this customer is already configured
2854 to receive postal invoices, does nothing.
2858 sub invoicing_list_addpost {
2860 return if grep { $_ eq 'POST' } $self->invoicing_list;
2861 my @invoicing_list = $self->invoicing_list;
2862 push @invoicing_list, 'POST';
2863 $self->invoicing_list(\@invoicing_list);
2866 =item invoicing_list_emailonly
2868 Returns the list of email invoice recipients (invoicing_list without non-email
2869 destinations such as POST and FAX).
2873 sub invoicing_list_emailonly {
2875 warn "$me invoicing_list_emailonly called"
2877 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
2880 =item invoicing_list_emailonly_scalar
2882 Returns the list of email invoice recipients (invoicing_list without non-email
2883 destinations such as POST and FAX) as a comma-separated scalar.
2887 sub invoicing_list_emailonly_scalar {
2889 warn "$me invoicing_list_emailonly_scalar called"
2891 join(', ', $self->invoicing_list_emailonly);
2894 =item referral_custnum_cust_main
2896 Returns the customer who referred this customer (or the empty string, if
2897 this customer was not referred).
2899 Note the difference with referral_cust_main method: This method,
2900 referral_custnum_cust_main returns the single customer (if any) who referred
2901 this customer, while referral_cust_main returns an array of customers referred
2906 sub referral_custnum_cust_main {
2908 return '' unless $self->referral_custnum;
2909 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
2912 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
2914 Returns an array of customers referred by this customer (referral_custnum set
2915 to this custnum). If DEPTH is given, recurses up to the given depth, returning
2916 customers referred by customers referred by this customer and so on, inclusive.
2917 The default behavior is DEPTH 1 (no recursion).
2919 Note the difference with referral_custnum_cust_main method: This method,
2920 referral_cust_main, returns an array of customers referred BY this customer,
2921 while referral_custnum_cust_main returns the single customer (if any) who
2922 referred this customer.
2926 sub referral_cust_main {
2928 my $depth = @_ ? shift : 1;
2929 my $exclude = @_ ? shift : {};
2932 map { $exclude->{$_->custnum}++; $_; }
2933 grep { ! $exclude->{ $_->custnum } }
2934 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
2938 map { $_->referral_cust_main($depth-1, $exclude) }
2945 =item referral_cust_main_ncancelled
2947 Same as referral_cust_main, except only returns customers with uncancelled
2952 sub referral_cust_main_ncancelled {
2954 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
2957 =item referral_cust_pkg [ DEPTH ]
2959 Like referral_cust_main, except returns a flat list of all unsuspended (and
2960 uncancelled) packages for each customer. The number of items in this list may
2961 be useful for commission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
2965 sub referral_cust_pkg {
2967 my $depth = @_ ? shift : 1;
2969 map { $_->unsuspended_pkgs }
2970 grep { $_->unsuspended_pkgs }
2971 $self->referral_cust_main($depth);
2974 =item referring_cust_main
2976 Returns the single cust_main record for the customer who referred this customer
2977 (referral_custnum), or false.
2981 sub referring_cust_main {
2983 return '' unless $self->referral_custnum;
2984 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
2987 =item credit AMOUNT, REASON [ , OPTION => VALUE ... ]
2989 Applies a credit to this customer. If there is an error, returns the error,
2990 otherwise returns false.
2992 REASON can be a text string, an FS::reason object, or a scalar reference to
2993 a reasonnum. If a text string, it will be automatically inserted as a new
2994 reason, and a 'reason_type' option must be passed to indicate the
2995 FS::reason_type for the new reason.
2997 An I<addlinfo> option may be passed to set the credit's I<addlinfo> field.
2998 Likewise for I<eventnum>, I<commission_agentnum>, I<commission_salesnum> and
2999 I<commission_pkgnum>.
3001 Any other options are passed to FS::cust_credit::insert.
3006 my( $self, $amount, $reason, %options ) = @_;
3008 my $cust_credit = new FS::cust_credit {
3009 'custnum' => $self->custnum,
3010 'amount' => $amount,
3013 if ( ref($reason) ) {
3015 if ( ref($reason) eq 'SCALAR' ) {
3016 $cust_credit->reasonnum( $$reason );
3018 $cust_credit->reasonnum( $reason->reasonnum );
3022 $cust_credit->set('reason', $reason)
3025 $cust_credit->$_( delete $options{$_} )
3026 foreach grep exists($options{$_}),
3027 qw( addlinfo eventnum ),
3028 map "commission_$_", qw( agentnum salesnum pkgnum );
3030 $cust_credit->insert(%options);
3034 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3036 Creates a one-time charge for this customer. If there is an error, returns
3037 the error, otherwise returns false.
3039 New-style, with a hashref of options:
3041 my $error = $cust_main->charge(
3045 'start_date' => str2time('7/4/2009'),
3046 'pkg' => 'Description',
3047 'comment' => 'Comment',
3048 'additional' => [], #extra invoice detail
3049 'classnum' => 1, #pkg_class
3051 'setuptax' => '', # or 'Y' for tax exempt
3053 'locationnum'=> 1234, # optional
3056 'taxclass' => 'Tax class',
3059 'taxproduct' => 2, #part_pkg_taxproduct
3060 'override' => {}, #XXX describe
3062 #will be filled in with the new object
3063 'cust_pkg_ref' => \$cust_pkg,
3065 #generate an invoice immediately
3067 'invoice_terms' => '', #with these terms
3073 my $error = $cust_main->charge( 54.32, 'Description', 'Comment', 'Tax class' );
3079 my ( $amount, $setup_cost, $quantity, $start_date, $classnum );
3080 my ( $pkg, $comment, $additional );
3081 my ( $setuptax, $taxclass ); #internal taxes
3082 my ( $taxproduct, $override ); #vendor (CCH) taxes
3084 my $cust_pkg_ref = '';
3085 my ( $bill_now, $invoice_terms ) = ( 0, '' );
3087 if ( ref( $_[0] ) ) {
3088 $amount = $_[0]->{amount};
3089 $setup_cost = $_[0]->{setup_cost};
3090 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
3091 $start_date = exists($_[0]->{start_date}) ? $_[0]->{start_date} : '';
3092 $no_auto = exists($_[0]->{no_auto}) ? $_[0]->{no_auto} : '';
3093 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
3094 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
3095 : '$'. sprintf("%.2f",$amount);
3096 $setuptax = exists($_[0]->{setuptax}) ? $_[0]->{setuptax} : '';
3097 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
3098 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
3099 $additional = $_[0]->{additional} || [];
3100 $taxproduct = $_[0]->{taxproductnum};
3101 $override = { '' => $_[0]->{tax_override} };
3102 $cust_pkg_ref = exists($_[0]->{cust_pkg_ref}) ? $_[0]->{cust_pkg_ref} : '';
3103 $bill_now = exists($_[0]->{bill_now}) ? $_[0]->{bill_now} : '';
3104 $invoice_terms = exists($_[0]->{invoice_terms}) ? $_[0]->{invoice_terms} : '';
3105 $locationnum = $_[0]->{locationnum} || $self->ship_locationnum;
3111 $pkg = @_ ? shift : 'One-time charge';
3112 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
3114 $taxclass = @_ ? shift : '';
3118 local $SIG{HUP} = 'IGNORE';
3119 local $SIG{INT} = 'IGNORE';
3120 local $SIG{QUIT} = 'IGNORE';
3121 local $SIG{TERM} = 'IGNORE';
3122 local $SIG{TSTP} = 'IGNORE';
3123 local $SIG{PIPE} = 'IGNORE';
3125 my $oldAutoCommit = $FS::UID::AutoCommit;
3126 local $FS::UID::AutoCommit = 0;
3129 my $part_pkg = new FS::part_pkg ( {
3131 'comment' => $comment,
3135 'classnum' => ( $classnum ? $classnum : '' ),
3136 'setuptax' => $setuptax,
3137 'taxclass' => $taxclass,
3138 'taxproductnum' => $taxproduct,
3139 'setup_cost' => $setup_cost,
3142 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
3143 ( 0 .. @$additional - 1 )
3145 'additional_count' => scalar(@$additional),
3146 'setup_fee' => $amount,
3149 my $error = $part_pkg->insert( options => \%options,
3150 tax_overrides => $override,
3153 $dbh->rollback if $oldAutoCommit;
3157 my $pkgpart = $part_pkg->pkgpart;
3158 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3159 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3160 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3161 $error = $type_pkgs->insert;
3163 $dbh->rollback if $oldAutoCommit;
3168 my $cust_pkg = new FS::cust_pkg ( {
3169 'custnum' => $self->custnum,
3170 'pkgpart' => $pkgpart,
3171 'quantity' => $quantity,
3172 'start_date' => $start_date,
3173 'no_auto' => $no_auto,
3174 'locationnum'=> $locationnum,
3177 $error = $cust_pkg->insert;
3179 $dbh->rollback if $oldAutoCommit;
3181 } elsif ( $cust_pkg_ref ) {
3182 ${$cust_pkg_ref} = $cust_pkg;
3186 my $error = $self->bill( 'invoice_terms' => $invoice_terms,
3187 'pkg_list' => [ $cust_pkg ],
3190 $dbh->rollback if $oldAutoCommit;
3195 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3200 #=item charge_postal_fee
3202 #Applies a one time charge this customer. If there is an error,
3203 #returns the error, returns the cust_pkg charge object or false
3204 #if there was no charge.
3208 # This should be a customer event. For that to work requires that bill
3209 # also be a customer event.
3211 sub charge_postal_fee {
3214 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart', $self->agentnum);
3215 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
3217 my $cust_pkg = new FS::cust_pkg ( {
3218 'custnum' => $self->custnum,
3219 'pkgpart' => $pkgpart,
3223 my $error = $cust_pkg->insert;
3224 $error ? $error : $cust_pkg;
3227 =item cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3229 Returns all the invoices (see L<FS::cust_bill>) for this customer.
3231 Optionally, a list or hashref of additional arguments to the qsearch call can
3238 my $opt = ref($_[0]) ? shift : { @_ };
3240 #return $self->num_cust_bill unless wantarray || keys %$opt;
3242 $opt->{'table'} = 'cust_bill';
3243 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
3244 $opt->{'hashref'}{'custnum'} = $self->custnum;
3245 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
3247 map { $_ } #behavior of sort undefined in scalar context
3248 sort { $a->_date <=> $b->_date }
3252 =item open_cust_bill
3254 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
3259 sub open_cust_bill {
3263 'extra_sql' => ' AND '. FS::cust_bill->owed_sql. ' > 0',
3269 =item legacy_cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3271 Returns all the legacy invoices (see L<FS::legacy_cust_bill>) for this customer.
3275 sub legacy_cust_bill {
3278 #return $self->num_legacy_cust_bill unless wantarray;
3280 map { $_ } #behavior of sort undefined in scalar context
3281 sort { $a->_date <=> $b->_date }
3282 qsearch({ 'table' => 'legacy_cust_bill',
3283 'hashref' => { 'custnum' => $self->custnum, },
3284 'order_by' => 'ORDER BY _date ASC',
3288 =item cust_statement [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3290 Returns all the statements (see L<FS::cust_statement>) for this customer.
3292 Optionally, a list or hashref of additional arguments to the qsearch call can
3297 =item cust_bill_void
3299 Returns all the voided invoices (see L<FS::cust_bill_void>) for this customer.
3303 sub cust_bill_void {
3306 map { $_ } #return $self->num_cust_bill_void unless wantarray;
3307 sort { $a->_date <=> $b->_date }
3308 qsearch( 'cust_bill_void', { 'custnum' => $self->custnum } )
3311 sub cust_statement {
3313 my $opt = ref($_[0]) ? shift : { @_ };
3315 #return $self->num_cust_statement unless wantarray || keys %$opt;
3317 $opt->{'table'} = 'cust_statement';
3318 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
3319 $opt->{'hashref'}{'custnum'} = $self->custnum;
3320 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
3322 map { $_ } #behavior of sort undefined in scalar context
3323 sort { $a->_date <=> $b->_date }
3327 =item svc_x SVCDB [ OPTION => VALUE | EXTRA_QSEARCH_PARAMS_HASHREF ]
3329 Returns all services of type SVCDB (such as 'svc_acct') for this customer.
3331 Optionally, a list or hashref of additional arguments to the qsearch call can
3332 be passed following the SVCDB.
3339 if ( ! $svcdb =~ /^svc_\w+$/ ) {
3340 warn "$me svc_x requires a svcdb";
3343 my $opt = ref($_[0]) ? shift : { @_ };
3345 $opt->{'table'} = $svcdb;
3346 $opt->{'addl_from'} =
3347 'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum) '.
3348 ($opt->{'addl_from'} || '');
3350 my $custnum = $self->custnum;
3351 $custnum =~ /^\d+$/ or die "bad custnum '$custnum'";
3352 my $where = "cust_pkg.custnum = $custnum";
3354 my $extra_sql = $opt->{'extra_sql'} || '';
3355 if ( keys %{ $opt->{'hashref'} } ) {
3356 $extra_sql = " AND $where $extra_sql";
3359 if ( $opt->{'extra_sql'} =~ /^\s*where\s(.*)/si ) {
3360 $extra_sql = "WHERE $where AND $1";
3363 $extra_sql = "WHERE $where $extra_sql";
3366 $opt->{'extra_sql'} = $extra_sql;
3371 # required for use as an eventtable;
3374 $self->svc_x('svc_acct', @_);
3379 Returns all the credits (see L<FS::cust_credit>) for this customer.
3385 map { $_ } #return $self->num_cust_credit unless wantarray;
3386 sort { $a->_date <=> $b->_date }
3387 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
3390 =item cust_credit_pkgnum
3392 Returns all the credits (see L<FS::cust_credit>) for this customer's specific
3393 package when using experimental package balances.
3397 sub cust_credit_pkgnum {
3398 my( $self, $pkgnum ) = @_;
3399 map { $_ } #return $self->num_cust_credit_pkgnum($pkgnum) unless wantarray;
3400 sort { $a->_date <=> $b->_date }
3401 qsearch( 'cust_credit', { 'custnum' => $self->custnum,
3402 'pkgnum' => $pkgnum,
3407 =item cust_credit_void
3409 Returns all voided credits (see L<FS::cust_credit_void>) for this customer.
3413 sub cust_credit_void {
3416 sort { $a->_date <=> $b->_date }
3417 qsearch( 'cust_credit_void', { 'custnum' => $self->custnum } )
3422 Returns all the payments (see L<FS::cust_pay>) for this customer.
3428 my $opt = ref($_[0]) ? shift : { @_ };
3430 return $self->num_cust_pay unless wantarray || keys %$opt;
3432 $opt->{'table'} = 'cust_pay';
3433 $opt->{'hashref'}{'custnum'} = $self->custnum;
3435 map { $_ } #behavior of sort undefined in scalar context
3436 sort { $a->_date <=> $b->_date }
3443 Returns the number of payments (see L<FS::cust_pay>) for this customer. Also
3444 called automatically when the cust_pay method is used in a scalar context.
3450 my $sql = "SELECT COUNT(*) FROM cust_pay WHERE custnum = ?";
3451 my $sth = dbh->prepare($sql) or die dbh->errstr;
3452 $sth->execute($self->custnum) or die $sth->errstr;
3453 $sth->fetchrow_arrayref->[0];
3456 =item unapplied_cust_pay
3458 Returns all the unapplied payments (see L<FS::cust_pay>) for this customer.
3462 sub unapplied_cust_pay {
3466 'extra_sql' => ' AND '. FS::cust_pay->unapplied_sql. ' > 0',
3472 =item cust_pay_pkgnum
3474 Returns all the payments (see L<FS::cust_pay>) for this customer's specific
3475 package when using experimental package balances.
3479 sub cust_pay_pkgnum {
3480 my( $self, $pkgnum ) = @_;
3481 map { $_ } #return $self->num_cust_pay_pkgnum($pkgnum) unless wantarray;
3482 sort { $a->_date <=> $b->_date }
3483 qsearch( 'cust_pay', { 'custnum' => $self->custnum,
3484 'pkgnum' => $pkgnum,
3491 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
3497 map { $_ } #return $self->num_cust_pay_void unless wantarray;
3498 sort { $a->_date <=> $b->_date }
3499 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
3502 =item cust_pay_pending
3504 Returns all pending payments (see L<FS::cust_pay_pending>) for this customer
3505 (without status "done").
3509 sub cust_pay_pending {
3511 return $self->num_cust_pay_pending unless wantarray;
3512 sort { $a->_date <=> $b->_date }
3513 qsearch( 'cust_pay_pending', {
3514 'custnum' => $self->custnum,
3515 'status' => { op=>'!=', value=>'done' },
3520 =item cust_pay_pending_attempt
3522 Returns all payment attempts / declined payments for this customer, as pending
3523 payments objects (see L<FS::cust_pay_pending>), with status "done" but without
3524 a corresponding payment (see L<FS::cust_pay>).
3528 sub cust_pay_pending_attempt {
3530 return $self->num_cust_pay_pending_attempt unless wantarray;
3531 sort { $a->_date <=> $b->_date }
3532 qsearch( 'cust_pay_pending', {
3533 'custnum' => $self->custnum,
3540 =item num_cust_pay_pending
3542 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
3543 customer (without status "done"). Also called automatically when the
3544 cust_pay_pending method is used in a scalar context.
3548 sub num_cust_pay_pending {
3551 " SELECT COUNT(*) FROM cust_pay_pending ".
3552 " WHERE custnum = ? AND status != 'done' ",
3557 =item num_cust_pay_pending_attempt
3559 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
3560 customer, with status "done" but without a corresp. Also called automatically when the
3561 cust_pay_pending method is used in a scalar context.
3565 sub num_cust_pay_pending_attempt {
3568 " SELECT COUNT(*) FROM cust_pay_pending ".
3569 " WHERE custnum = ? AND status = 'done' AND paynum IS NULL",
3576 Returns all the refunds (see L<FS::cust_refund>) for this customer.
3582 map { $_ } #return $self->num_cust_refund unless wantarray;
3583 sort { $a->_date <=> $b->_date }
3584 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
3587 =item display_custnum
3589 Returns the displayed customer number for this customer: agent_custid if
3590 cust_main-default_agent_custid is set and it has a value, custnum otherwise.
3594 sub display_custnum {
3597 my $prefix = $conf->config('cust_main-custnum-display_prefix', $self->agentnum) || '';
3598 if ( my $special = $conf->config('cust_main-custnum-display_special') ) {
3599 if ( $special eq 'CoStAg' ) {
3600 $prefix = uc( join('',
3602 ($self->state =~ /^(..)/),
3603 $prefix || ($self->agent->agent =~ /^(..)/)
3606 elsif ( $special eq 'CoStCl' ) {
3607 $prefix = uc( join('',
3609 ($self->state =~ /^(..)/),
3610 ($self->classnum ? $self->cust_class->classname =~ /^(..)/ : '__')
3613 # add any others here if needed
3616 my $length = $conf->config('cust_main-custnum-display_length');
3617 if ( $conf->exists('cust_main-default_agent_custid') && $self->agent_custid ){
3618 return $self->agent_custid;
3619 } elsif ( $prefix ) {
3620 $length = 8 if !defined($length);
3622 sprintf('%0'.$length.'d', $self->custnum)
3623 } elsif ( $length ) {
3624 return sprintf('%0'.$length.'d', $self->custnum);
3626 return $self->custnum;
3632 Returns a name string for this customer, either "Company (Last, First)" or
3639 my $name = $self->contact;
3640 $name = $self->company. " ($name)" if $self->company;
3644 =item service_contact
3646 Returns the L<FS::contact> object for this customer that has the 'Service'
3647 contact class, or undef if there is no such contact. Deprecated; don't use
3652 sub service_contact {
3654 if ( !exists($self->{service_contact}) ) {
3655 my $classnum = $self->scalar_sql(
3656 'SELECT classnum FROM contact_class WHERE classname = \'Service\''
3657 ) || 0; #if it's zero, qsearchs will return nothing
3658 $self->{service_contact} = qsearchs('contact', {
3659 'classnum' => $classnum, 'custnum' => $self->custnum
3662 $self->{service_contact};
3667 Returns a name string for this (service/shipping) contact, either
3668 "Company (Last, First)" or "Last, First".
3675 my $name = $self->ship_contact;
3676 $name = $self->company. " ($name)" if $self->company;
3682 Returns a name string for this customer, either "Company" or "First Last".
3688 $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
3691 =item ship_name_short
3693 Returns a name string for this (service/shipping) contact, either "Company"
3698 sub ship_name_short {
3700 $self->service_contact
3701 ? $self->ship_contact_firstlast
3707 Returns this customer's full (billing) contact name only, "Last, First"
3713 $self->get('last'). ', '. $self->first;
3718 Returns this customer's full (shipping) contact name only, "Last, First"
3724 my $contact = $self->service_contact || $self;
3725 $contact->get('last') . ', ' . $contact->get('first');
3728 =item contact_firstlast
3730 Returns this customers full (billing) contact name only, "First Last".
3734 sub contact_firstlast {
3736 $self->first. ' '. $self->get('last');
3739 =item ship_contact_firstlast
3741 Returns this customer's full (shipping) contact name only, "First Last".
3745 sub ship_contact_firstlast {
3747 my $contact = $self->service_contact || $self;
3748 $contact->get('first') . ' '. $contact->get('last');
3751 #XXX this doesn't work in 3.x+
3754 #Returns this customer's full country name
3760 # code2country($self->country);
3763 sub bill_country_full {
3765 code2country($self->bill_location->country);
3768 sub ship_country_full {
3770 code2country($self->ship_location->country);
3773 =item county_state_county [ PREFIX ]
3775 Returns a string consisting of just the county, state and country.
3779 sub county_state_country {
3782 if ( @_ && $_[0] && $self->has_ship_address ) {
3783 $locationnum = $self->ship_locationnum;
3785 $locationnum = $self->bill_locationnum;
3787 my $cust_location = qsearchs('cust_location', { locationnum=>$locationnum });
3788 $cust_location->county_state_country;
3791 =item geocode DATA_VENDOR
3793 Returns a value for the customer location as encoded by DATA_VENDOR.
3794 Currently this only makes sense for "CCH" as DATA_VENDOR.
3802 Returns a status string for this customer, currently:
3808 No packages have ever been ordered. Displayed as "No packages".
3812 Recurring packages all are new (not yet billed).
3816 One or more recurring packages is active.
3820 No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled).
3824 All non-cancelled recurring packages are suspended.
3828 All recurring packages are cancelled.
3832 Behavior of inactive vs. cancelled edge cases can be adjusted with the
3833 cust_main-status_module configuration option.
3837 sub status { shift->cust_status(@_); }
3841 for my $status ( FS::cust_main->statuses() ) {
3842 my $method = $status.'_sql';
3843 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
3844 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
3845 $sth->execute( ($self->custnum) x $numnum )
3846 or die "Error executing 'SELECT $sql': ". $sth->errstr;
3847 return $status if $sth->fetchrow_arrayref->[0];
3851 =item ucfirst_cust_status
3853 =item ucfirst_status
3855 Deprecated, use the cust_status_label method instead.
3857 Returns the status with the first character capitalized.
3861 sub ucfirst_status {
3862 carp "ucfirst_status deprecated, use cust_status_label" unless $ucfirst_nowarn;
3863 local($ucfirst_nowarn) = 1;
3864 shift->ucfirst_cust_status(@_);
3867 sub ucfirst_cust_status {
3868 carp "ucfirst_cust_status deprecated, use cust_status_label" unless $ucfirst_nowarn;
3870 ucfirst($self->cust_status);
3873 =item cust_status_label
3877 Returns the display label for this status.
3881 sub status_label { shift->cust_status_label(@_); }
3883 sub cust_status_label {
3885 __PACKAGE__->statuslabels->{$self->cust_status};
3890 Returns a hex triplet color string for this customer's status.
3894 sub statuscolor { shift->cust_statuscolor(@_); }
3896 sub cust_statuscolor {
3898 __PACKAGE__->statuscolors->{$self->cust_status};
3901 =item tickets [ STATUS ]
3903 Returns an array of hashes representing the customer's RT tickets.
3905 An optional status (or arrayref or hashref of statuses) may be specified.
3911 my $status = ( @_ && $_[0] ) ? shift : '';
3913 my $num = $conf->config('cust_main-max_tickets') || 10;
3916 if ( $conf->config('ticket_system') ) {
3917 unless ( $conf->config('ticket_system-custom_priority_field') ) {
3919 @tickets = @{ FS::TicketSystem->customer_tickets( $self->custnum,
3928 foreach my $priority (
3929 $conf->config('ticket_system-custom_priority_field-values'), ''
3931 last if scalar(@tickets) >= $num;
3933 @{ FS::TicketSystem->customer_tickets( $self->custnum,
3934 $num - scalar(@tickets),
3945 # Return services representing svc_accts in customer support packages
3946 sub support_services {
3948 my %packages = map { $_ => 1 } $conf->config('support_packages');
3950 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
3951 grep { $_->part_svc->svcdb eq 'svc_acct' }
3952 map { $_->cust_svc }
3953 grep { exists $packages{ $_->pkgpart } }
3954 $self->ncancelled_pkgs;
3958 # Return a list of latitude/longitude for one of the services (if any)
3959 sub service_coordinates {
3963 grep { $_->latitude && $_->longitude }
3965 map { $_->cust_svc }
3966 $self->ncancelled_pkgs;
3968 scalar(@svc_X) ? ( $svc_X[0]->latitude, $svc_X[0]->longitude ) : ()
3973 Returns a masked version of the named field
3978 my ($self,$field) = @_;
3982 'x'x(length($self->getfield($field))-4).
3983 substr($self->getfield($field), (length($self->getfield($field))-4));
3989 =head1 CLASS METHODS
3995 Class method that returns the list of possible status strings for customers
3996 (see L<the status method|/status>). For example:
3998 @statuses = FS::cust_main->statuses();
4004 keys %{ $self->statuscolors };
4007 =item cust_status_sql
4009 Returns an SQL fragment to determine the status of a cust_main record, as a
4014 sub cust_status_sql {
4016 for my $status ( FS::cust_main->statuses() ) {
4017 my $method = $status.'_sql';
4018 $sql .= ' WHEN ('.FS::cust_main->$method.") THEN '$status'";
4027 Returns an SQL expression identifying prospective cust_main records (customers
4028 with no packages ever ordered)
4032 use vars qw($select_count_pkgs);
4033 $select_count_pkgs =
4034 "SELECT COUNT(*) FROM cust_pkg
4035 WHERE cust_pkg.custnum = cust_main.custnum";
4037 sub select_count_pkgs_sql {
4042 " 0 = ( $select_count_pkgs ) ";
4047 Returns an SQL expression identifying ordered cust_main records (customers with
4048 no active packages, but recurring packages not yet setup or one time charges
4054 FS::cust_main->none_active_sql.
4055 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->not_yet_billed_sql. " ) ";
4060 Returns an SQL expression identifying active cust_main records (customers with
4061 active recurring packages).
4066 " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
4069 =item none_active_sql
4071 Returns an SQL expression identifying cust_main records with no active
4072 recurring packages. This includes customers of status prospect, ordered,
4073 inactive, and suspended.
4077 sub none_active_sql {
4078 " 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
4083 Returns an SQL expression identifying inactive cust_main records (customers with
4084 no active recurring packages, but otherwise unsuspended/uncancelled).
4089 FS::cust_main->none_active_sql.
4090 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " ) ";
4096 Returns an SQL expression identifying suspended cust_main records.
4101 sub suspended_sql { susp_sql(@_); }
4103 FS::cust_main->none_active_sql.
4104 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " ) ";
4110 Returns an SQL expression identifying cancelled cust_main records.
4114 sub cancel_sql { shift->cancelled_sql(@_); }
4117 =item uncancelled_sql
4119 Returns an SQL expression identifying un-cancelled cust_main records.
4123 sub uncancelled_sql { uncancel_sql(@_); }
4124 sub uncancel_sql { "
4125 ( 0 < ( $select_count_pkgs
4126 AND ( cust_pkg.cancel IS NULL
4127 OR cust_pkg.cancel = 0
4130 OR 0 = ( $select_count_pkgs )
4136 Returns an SQL fragment to retreive the balance.
4141 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
4142 WHERE cust_bill.custnum = cust_main.custnum )
4143 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
4144 WHERE cust_pay.custnum = cust_main.custnum )
4145 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
4146 WHERE cust_credit.custnum = cust_main.custnum )
4147 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
4148 WHERE cust_refund.custnum = cust_main.custnum )
4151 =item balance_date_sql [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
4153 Returns an SQL fragment to retreive the balance for this customer, optionally
4154 considering invoices with date earlier than START_TIME, and not
4155 later than END_TIME (total_owed_date minus total_unapplied_credits minus
4156 total_unapplied_payments).
4158 Times are specified as SQL fragments or numeric
4159 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4160 L<Date::Parse> for conversion functions. The empty string can be passed
4161 to disable that time constraint completely.
4163 Available options are:
4167 =item unapplied_date
4169 set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
4174 set to true to remove all customer comparison clauses, for totals
4179 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
4184 JOIN clause (typically used with the total option)
4188 An absolute cutoff time. Payments, credits, and refunds I<applied> after this
4189 time will be ignored. Note that START_TIME and END_TIME only limit the date
4190 range for invoices and I<unapplied> payments, credits, and refunds.
4196 sub balance_date_sql {
4197 my( $class, $start, $end, %opt ) = @_;
4199 my $cutoff = $opt{'cutoff'};
4201 my $owed = FS::cust_bill->owed_sql($cutoff);
4202 my $unapp_refund = FS::cust_refund->unapplied_sql($cutoff);
4203 my $unapp_credit = FS::cust_credit->unapplied_sql($cutoff);
4204 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
4206 my $j = $opt{'join'} || '';
4208 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
4209 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
4210 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
4211 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
4213 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
4214 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
4215 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
4216 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
4221 =item unapplied_payments_date_sql START_TIME [ END_TIME ]
4223 Returns an SQL fragment to retreive the total unapplied payments for this
4224 customer, only considering payments with date earlier than START_TIME, and
4225 optionally not later than END_TIME.
4227 Times are specified as SQL fragments or numeric
4228 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4229 L<Date::Parse> for conversion functions. The empty string can be passed
4230 to disable that time constraint completely.
4232 Available options are:
4236 sub unapplied_payments_date_sql {
4237 my( $class, $start, $end, %opt ) = @_;
4239 my $cutoff = $opt{'cutoff'};
4241 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
4243 my $pay_where = $class->_money_table_where( 'cust_pay', $start, $end,
4244 'unapplied_date'=>1 );
4246 " ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $pay_where ) ";
4249 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4251 Helper method for balance_date_sql; name (and usage) subject to change
4252 (suggestions welcome).
4254 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
4255 cust_refund, cust_credit or cust_pay).
4257 If TABLE is "cust_bill" or the unapplied_date option is true, only
4258 considers records with date earlier than START_TIME, and optionally not
4259 later than END_TIME .
4263 sub _money_table_where {
4264 my( $class, $table, $start, $end, %opt ) = @_;
4267 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
4268 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
4269 push @where, "$table._date <= $start" if defined($start) && length($start);
4270 push @where, "$table._date > $end" if defined($end) && length($end);
4272 push @where, @{$opt{'where'}} if $opt{'where'};
4273 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
4279 #for dyanmic FS::$table->search in httemplate/misc/email_customers.html
4280 use FS::cust_main::Search;
4283 FS::cust_main::Search->search(@_);
4292 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
4294 Deprecated. Use event notification and message templates
4295 (L<FS::msg_template>) instead.
4297 Sends a templated email notification to the customer (see L<Text::Template>).
4299 OPTIONS is a hash and may include
4301 I<from> - the email sender (default is invoice_from)
4303 I<to> - comma-separated scalar or arrayref of recipients
4304 (default is invoicing_list)
4306 I<subject> - The subject line of the sent email notification
4307 (default is "Notice from company_name")
4309 I<extra_fields> - a hashref of name/value pairs which will be substituted
4312 The following variables are vavailable in the template.
4314 I<$first> - the customer first name
4315 I<$last> - the customer last name
4316 I<$company> - the customer company
4317 I<$payby> - a description of the method of payment for the customer
4318 # would be nice to use FS::payby::shortname
4319 I<$payinfo> - the account information used to collect for this customer
4320 I<$expdate> - the expiration of the customer payment in seconds from epoch
4325 my ($self, $template, %options) = @_;
4327 return unless $conf->exists($template);
4329 my $from = $conf->config('invoice_from', $self->agentnum)
4330 if $conf->exists('invoice_from', $self->agentnum);
4331 $from = $options{from} if exists($options{from});
4333 my $to = join(',', $self->invoicing_list_emailonly);
4334 $to = $options{to} if exists($options{to});
4336 my $subject = "Notice from " . $conf->config('company_name', $self->agentnum)
4337 if $conf->exists('company_name', $self->agentnum);
4338 $subject = $options{subject} if exists($options{subject});
4340 my $notify_template = new Text::Template (TYPE => 'ARRAY',
4341 SOURCE => [ map "$_\n",
4342 $conf->config($template)]
4344 or die "can't create new Text::Template object: Text::Template::ERROR";
4345 $notify_template->compile()
4346 or die "can't compile template: Text::Template::ERROR";
4348 $FS::notify_template::_template::company_name =
4349 $conf->config('company_name', $self->agentnum);
4350 $FS::notify_template::_template::company_address =
4351 join("\n", $conf->config('company_address', $self->agentnum) ). "\n";
4353 my $paydate = $self->paydate || '2037-12-31';
4354 $FS::notify_template::_template::first = $self->first;
4355 $FS::notify_template::_template::last = $self->last;
4356 $FS::notify_template::_template::company = $self->company;
4357 $FS::notify_template::_template::payinfo = $self->mask_payinfo;
4358 my $payby = $self->payby;
4359 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
4360 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
4362 #credit cards expire at the end of the month/year of their exp date
4363 if ($payby eq 'CARD' || $payby eq 'DCRD') {
4364 $FS::notify_template::_template::payby = 'credit card';
4365 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
4366 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
4368 }elsif ($payby eq 'COMP') {
4369 $FS::notify_template::_template::payby = 'complimentary account';
4371 $FS::notify_template::_template::payby = 'current method';
4373 $FS::notify_template::_template::expdate = $expire_time;
4375 for (keys %{$options{extra_fields}}){
4377 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
4380 send_email(from => $from,
4382 subject => $subject,
4383 body => $notify_template->fill_in( PACKAGE =>
4384 'FS::notify_template::_template' ),
4389 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
4391 Generates a templated notification to the customer (see L<Text::Template>).
4393 OPTIONS is a hash and may include
4395 I<extra_fields> - a hashref of name/value pairs which will be substituted
4396 into the template. These values may override values mentioned below
4397 and those from the customer record.
4399 The following variables are available in the template instead of or in addition
4400 to the fields of the customer record.
4402 I<$payby> - a description of the method of payment for the customer
4403 # would be nice to use FS::payby::shortname
4404 I<$payinfo> - the masked account information used to collect for this customer
4405 I<$expdate> - the expiration of the customer payment method in seconds from epoch
4406 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
4410 # a lot like cust_bill::print_latex
4411 sub generate_letter {
4412 my ($self, $template, %options) = @_;
4414 return unless $conf->exists($template);
4416 my $letter_template = new Text::Template
4418 SOURCE => [ map "$_\n", $conf->config($template)],
4419 DELIMITERS => [ '[@--', '--@]' ],
4421 or die "can't create new Text::Template object: Text::Template::ERROR";
4423 $letter_template->compile()
4424 or die "can't compile template: Text::Template::ERROR";
4426 my %letter_data = map { $_ => $self->$_ } $self->fields;
4427 $letter_data{payinfo} = $self->mask_payinfo;
4429 #my $paydate = $self->paydate || '2037-12-31';
4430 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
4432 my $payby = $self->payby;
4433 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
4434 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
4436 #credit cards expire at the end of the month/year of their exp date
4437 if ($payby eq 'CARD' || $payby eq 'DCRD') {
4438 $letter_data{payby} = 'credit card';
4439 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
4440 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
4442 }elsif ($payby eq 'COMP') {
4443 $letter_data{payby} = 'complimentary account';
4445 $letter_data{payby} = 'current method';
4447 $letter_data{expdate} = $expire_time;
4449 for (keys %{$options{extra_fields}}){
4450 $letter_data{$_} = $options{extra_fields}->{$_};
4453 unless(exists($letter_data{returnaddress})){
4454 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
4455 $self->agent_template)
4457 if ( length($retadd) ) {
4458 $letter_data{returnaddress} = $retadd;
4459 } elsif ( grep /\S/, $conf->config('company_address', $self->agentnum) ) {
4460 $letter_data{returnaddress} =
4461 join( "\n", map { s/( {2,})/'~' x length($1)/eg;
4465 ( $conf->config('company_name', $self->agentnum),
4466 $conf->config('company_address', $self->agentnum),
4470 $letter_data{returnaddress} = '~';
4474 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
4476 $letter_data{company_name} = $conf->config('company_name', $self->agentnum);
4478 my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
4480 my $lh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
4484 ) or die "can't open temp file: $!\n";
4485 print $lh $conf->config_binary('logo.eps', $self->agentnum)
4486 or die "can't write temp file: $!\n";
4488 $letter_data{'logo_file'} = $lh->filename;
4490 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
4494 ) or die "can't open temp file: $!\n";
4496 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
4498 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
4499 return ($1, $letter_data{'logo_file'});
4503 =item print_ps TEMPLATE
4505 Returns an postscript letter filled in from TEMPLATE, as a scalar.
4511 my($file, $lfile) = $self->generate_letter(@_);
4512 my $ps = FS::Misc::generate_ps($file);
4513 unlink($file.'.tex');
4519 =item print TEMPLATE
4521 Prints the filled in template.
4523 TEMPLATE is the name of a L<Text::Template> to fill in and print.
4527 sub queueable_print {
4530 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
4531 or die "invalid customer number: " . $opt{custnum};
4533 my $error = $self->print( { 'template' => $opt{template} } );
4534 die $error if $error;
4538 my ($self, $template) = (shift, shift);
4540 [ $self->print_ps($template) ],
4541 'agentnum' => $self->agentnum,
4545 #these three subs should just go away once agent stuff is all config overrides
4547 sub agent_template {
4549 $self->_agent_plandata('agent_templatename');
4552 sub agent_invoice_from {
4554 $self->_agent_plandata('agent_invoice_from');
4557 sub _agent_plandata {
4558 my( $self, $option ) = @_;
4560 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
4561 #agent-specific Conf
4563 use FS::part_event::Condition;
4565 my $agentnum = $self->agentnum;
4567 my $regexp = regexp_sql();
4569 my $part_event_option =
4571 'select' => 'part_event_option.*',
4572 'table' => 'part_event_option',
4574 LEFT JOIN part_event USING ( eventpart )
4575 LEFT JOIN part_event_option AS peo_agentnum
4576 ON ( part_event.eventpart = peo_agentnum.eventpart
4577 AND peo_agentnum.optionname = 'agentnum'
4578 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
4580 LEFT JOIN part_event_condition
4581 ON ( part_event.eventpart = part_event_condition.eventpart
4582 AND part_event_condition.conditionname = 'cust_bill_age'
4584 LEFT JOIN part_event_condition_option
4585 ON ( part_event_condition.eventconditionnum = part_event_condition_option.eventconditionnum
4586 AND part_event_condition_option.optionname = 'age'
4589 #'hashref' => { 'optionname' => $option },
4590 #'hashref' => { 'part_event_option.optionname' => $option },
4592 " WHERE part_event_option.optionname = ". dbh->quote($option).
4593 " AND action = 'cust_bill_send_agent' ".
4594 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
4595 " AND peo_agentnum.optionname = 'agentnum' ".
4596 " AND ( agentnum IS NULL OR agentnum = $agentnum ) ".
4598 CASE WHEN part_event_condition_option.optionname IS NULL
4600 ELSE ". FS::part_event::Condition->age2seconds_sql('part_event_condition_option.optionvalue').
4602 , part_event.weight".
4606 unless ( $part_event_option ) {
4607 return $self->agent->invoice_template || ''
4608 if $option eq 'agent_templatename';
4612 $part_event_option->optionvalue;
4616 =item queued_bill 'custnum' => CUSTNUM [ , OPTION => VALUE ... ]
4618 Subroutine (not a method), designed to be called from the queue.
4620 Takes a list of options and values.
4622 Pulls up the customer record via the custnum option and calls bill_and_collect.
4627 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
4629 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
4630 warn 'bill_and_collect custnum#'. $cust_main->custnum. "\n";#log custnum w/pid
4632 #without this errors don't get rolled back
4633 $args{'fatal'} = 1; # runs from job queue, will be caught
4635 $cust_main->bill_and_collect( %args );
4638 =item queued_collect 'custnum' => CUSTNUM [ , OPTION => VALUE ... ]
4640 Like queued_bill, but instead of C<bill_and_collect>, just runs the
4641 C<collect> part. This is used in batch tax calculation, where invoice
4642 generation and collection events have to be completely separated.
4646 sub queued_collect {
4648 my $cust_main = FS::cust_main->by_key($args{'custnum'});
4650 $cust_main->collect(%args);
4653 sub process_bill_and_collect {
4656 my $cust_main = qsearchs( 'cust_main', { custnum => $param->{'custnum'} } )
4657 or die "custnum '$param->{custnum}' not found!\n";
4658 $param->{'job'} = $job;
4659 $param->{'fatal'} = 1; # runs from job queue, will be caught
4660 $param->{'retry'} = 1;
4662 $cust_main->bill_and_collect( %$param );
4665 #starting to take quite a while for big dbs
4666 # (JRNL: journaled so it only happens once per database)
4667 # - seq scan of h_cust_main (yuck), but not going to index paycvv, so
4668 # JRNL seq scan of cust_main on signupdate... index signupdate? will that help?
4669 # JRNL seq scan of cust_main on paydate... index on substrings? maybe set an
4670 # JRNL seq scan of cust_main on payinfo.. certainly not going toi ndex that...
4671 # JRNL leading/trailing spaces in first, last, company
4672 # JRNL migrate to cust_payby
4673 # - otaker upgrade? journal and call it good? (double check to make sure
4674 # we're not still setting otaker here)
4676 #only going to get worse with new location stuff...
4678 sub _upgrade_data { #class method
4679 my ($class, %opts) = @_;
4682 'UPDATE h_cust_main SET paycvv = NULL WHERE paycvv IS NOT NULL',
4685 #this seems to be the only expensive one.. why does it take so long?
4686 unless ( FS::upgrade_journal->is_done('cust_main__signupdate') ) {
4688 'UPDATE cust_main SET signupdate = (SELECT signupdate FROM h_cust_main WHERE signupdate IS NOT NULL AND h_cust_main.custnum = cust_main.custnum ORDER BY historynum DESC LIMIT 1) WHERE signupdate IS NULL';
4689 FS::upgrade_journal->set_done('cust_main__signupdate');
4692 unless ( FS::upgrade_journal->is_done('cust_main__paydate') ) {
4694 # fix yyyy-m-dd formatted paydates
4695 if ( driver_name =~ /^mysql/i ) {
4697 "UPDATE cust_main SET paydate = CONCAT( SUBSTRING(paydate FROM 1 FOR 5), '0', SUBSTRING(paydate FROM 6) ) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
4698 } else { # the SQL standard
4700 "UPDATE cust_main SET paydate = SUBSTRING(paydate FROM 1 FOR 5) || '0' || SUBSTRING(paydate FROM 6) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
4702 FS::upgrade_journal->set_done('cust_main__paydate');
4705 unless ( FS::upgrade_journal->is_done('cust_main__payinfo') ) {
4707 push @statements, #fix the weird BILL with a cc# in payinfo problem
4709 "UPDATE cust_main SET payby = 'DCRD' WHERE payby = 'BILL' and length(payinfo) = 16 and payinfo ". regexp_sql. q( '^[0-9]*$' );
4711 FS::upgrade_journal->set_done('cust_main__payinfo');
4716 foreach my $sql ( @statements ) {
4717 my $sth = dbh->prepare($sql) or die dbh->errstr;
4718 $sth->execute or die $sth->errstr;
4719 #warn ( (time - $t). " seconds\n" );
4723 local($ignore_expired_card) = 1;
4724 local($ignore_banned_card) = 1;
4725 local($skip_fuzzyfiles) = 1;
4726 local($import) = 1; #prevent automatic geocoding (need its own variable?)
4728 FS::cust_main::Location->_upgrade_data(%opts);
4730 unless ( FS::upgrade_journal->is_done('cust_main__trimspaces') ) {
4732 foreach my $cust_main ( qsearch({
4733 'table' => 'cust_main',
4735 'extra_sql' => 'WHERE '.
4737 map "$_ LIKE ' %' OR $_ LIKE '% ' OR $_ LIKE '% %'",
4738 qw( first last company )
4741 my $error = $cust_main->replace;
4742 die $error if $error;
4745 FS::upgrade_journal->set_done('cust_main__trimspaces');
4749 unless ( FS::upgrade_journal->is_done('cust_main__cust_payby') ) {
4751 #we don't want to decrypt them, just stuff them as-is into cust_payby
4752 local(@encrypted_fields) = ();
4754 local($FS::cust_payby::ignore_expired_card) = 1;
4755 local($FS::cust_payby::ignore_banned_card) = 1;
4757 my @payfields = qw( payby payinfo paycvv paymask
4758 paydate paystart_month paystart_year payissue
4759 payname paystate paytype payip
4762 my $search = new FS::Cursor {
4763 'table' => 'cust_main',
4764 'extra_sql' => " WHERE ( payby IS NOT NULL AND payby != '' ) ",
4767 while (my $cust_main = $search->fetch) {
4769 my $cust_payby = new FS::cust_payby {
4770 'custnum' => $cust_main->custnum,
4772 map { $_ => $cust_main->$_(); } @payfields
4775 my $error = $cust_payby->insert;
4776 die $error if $error;
4778 $cust_main->setfield($_, '') foreach @payfields;
4779 $error = $cust_main->replace;
4780 die $error if $error;
4784 FS::upgrade_journal->set_done('cust_main__cust_payby');
4787 $class->_upgrade_otaker(%opts);
4797 The delete method should possibly take an FS::cust_main object reference
4798 instead of a scalar customer number.
4800 Bill and collect options should probably be passed as references instead of a
4803 There should probably be a configuration file with a list of allowed credit
4806 No multiple currency support (probably a larger project than just this module).
4808 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
4810 Birthdates rely on negative epoch values.
4812 The payby for card/check batches is broken. With mixed batching, bad
4815 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
4819 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
4820 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
4821 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.