5 use base qw( FS::cust_main::Packages FS::cust_main::Status
6 FS::cust_main::NationalID
7 FS::cust_main::Billing FS::cust_main::Billing_Realtime
8 FS::cust_main::Billing_Discount
9 FS::cust_main::Billing_ThirdParty
10 FS::cust_main::Location
11 FS::cust_main::Credit_Limit
12 FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
13 FS::geocode_Mixin FS::Quotable_Mixin FS::Sales_Mixin
17 use vars qw( $DEBUG $me $conf $default_agent_custid $custnum_display_length
20 $ignore_expired_card $ignore_banned_card $ignore_illegal_zip
25 use Scalar::Util qw( blessed );
26 use Time::Local qw(timelocal);
27 use Storable qw(thaw);
31 use Digest::MD5 qw(md5_base64);
34 use File::Temp; #qw( tempfile );
36 use Business::CreditCard 0.28;
37 use FS::UID qw( getotaker dbh driver_name );
38 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
39 use FS::Misc qw( generate_email send_email generate_ps do_print money_pretty card_types );
40 use FS::Msgcat qw(gettext);
47 use FS::cust_bill_void;
48 use FS::legacy_cust_bill;
50 use FS::cust_pay_pending;
51 use FS::cust_pay_void;
52 use FS::cust_pay_batch;
55 use FS::part_referral;
56 use FS::cust_main_county;
57 use FS::cust_location;
59 use FS::cust_main_exemption;
60 use FS::cust_tax_adjustment;
61 use FS::cust_tax_location;
63 use FS::cust_main_invoice;
65 use FS::prepay_credit;
71 use FS::payment_gateway;
72 use FS::agent_payment_gateway;
74 use FS::cust_main_note;
75 use FS::cust_attachment;
78 use FS::upgrade_journal;
81 # 1 is mostly method/subroutine entry and options
82 # 2 traces progress of some operations
83 # 3 is even more information including possibly sensitive data
85 $me = '[FS::cust_main]';
88 $ignore_expired_card = 0;
89 $ignore_banned_card = 0;
93 @encrypted_fields = ('payinfo', 'paycvv');
94 sub nohistory_fields { ('payinfo', 'paycvv'); }
96 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
98 #ask FS::UID to run this stuff for us later
99 #$FS::UID::callback{'FS::cust_main'} = sub {
100 install_callback FS::UID sub {
101 $conf = new FS::Conf;
102 $default_agent_custid = $conf->exists('cust_main-default_agent_custid');
103 $custnum_display_length = $conf->config('cust_main-custnum-display_length');
108 my ( $hashref, $cache ) = @_;
109 if ( exists $hashref->{'pkgnum'} ) {
110 #@{ $self->{'_pkgnum'} } = ();
111 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
112 $self->{'_pkgnum'} = $subcache;
113 #push @{ $self->{'_pkgnum'} },
114 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
120 FS::cust_main - Object methods for cust_main records
126 $record = new FS::cust_main \%hash;
127 $record = new FS::cust_main { 'column' => 'value' };
129 $error = $record->insert;
131 $error = $new_record->replace($old_record);
133 $error = $record->delete;
135 $error = $record->check;
137 @cust_pkg = $record->all_pkgs;
139 @cust_pkg = $record->ncancelled_pkgs;
141 @cust_pkg = $record->suspended_pkgs;
143 $error = $record->bill;
144 $error = $record->bill %options;
145 $error = $record->bill 'time' => $time;
147 $error = $record->collect;
148 $error = $record->collect %options;
149 $error = $record->collect 'invoice_time' => $time,
154 An FS::cust_main object represents a customer. FS::cust_main inherits from
155 FS::Record. The following fields are currently supported:
161 Primary key (assigned automatically for new customers)
165 Agent (see L<FS::agent>)
169 Advertising source (see L<FS::part_referral>)
181 Cocial security number (optional)
205 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
209 Payment Information (See L<FS::payinfo_Mixin> for data format)
213 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
217 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
221 Expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
225 Start date month (maestro/solo cards only)
229 Start date year (maestro/solo cards only)
233 Issue number (maestro/solo cards only)
237 Name on card or billing name
241 IP address from which payment information was received
245 The credit card type (deduced from the card number).
249 Tax exempt, empty or `Y'
253 Order taker (see L<FS::access_user>)
259 =item referral_custnum
261 Referring customer number
265 Enable individual CDR spooling, empty or `Y'
269 A suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
273 Discourage individual CDR printing, empty or `Y'
277 Allow self-service editing of ticket subjects, empty or 'Y'
279 =item calling_list_exempt
281 Do not call, empty or 'Y'
283 =item invoice_ship_address
285 Display ship_address ("Service address") on invoices for this customer, empty or 'Y'
295 Creates a new customer. To add the customer to the database, see L<"insert">.
297 Note that this stores the hash reference, not a distinct copy of the hash it
298 points to. You can ask the object for a copy with the I<hash> method.
302 sub table { 'cust_main'; }
304 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
306 Adds this customer to the database. If there is an error, returns the error,
307 otherwise returns false.
309 Usually the customer's location will not yet exist in the database, and
310 the C<bill_location> and C<ship_location> pseudo-fields must be set to
311 uninserted L<FS::cust_location> objects. These will be inserted and linked
312 (in both directions) to the new customer record. If they're references
313 to the same object, they will become the same location.
315 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
316 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
317 are inserted atomicly, or the transaction is rolled back. Passing an empty
318 hash reference is equivalent to not supplying this parameter. There should be
319 a better explanation of this, but until then, here's an example:
322 tie %hash, 'Tie::RefHash'; #this part is important
324 $cust_pkg => [ $svc_acct ],
327 $cust_main->insert( \%hash );
329 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
330 be set as the invoicing list (see L<"invoicing_list">). Errors return as
331 expected and rollback the entire transaction; it is not necessary to call
332 check_invoicing_list first. The invoicing_list is set after the records in the
333 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
334 invoicing_list destination to the newly-created svc_acct. Here's an example:
336 $cust_main->insert( {}, [ $email, 'POST' ] );
338 Currently available options are: I<depend_jobnum>, I<noexport>,
339 I<tax_exemption> and I<prospectnum>.
341 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
342 on the supplied jobnum (they will not run until the specific job completes).
343 This can be used to defer provisioning until some action completes (such
344 as running the customer's credit card successfully).
346 The I<noexport> option is deprecated. If I<noexport> is set true, no
347 provisioning jobs (exports) are scheduled. (You can schedule them later with
348 the B<reexport> method.)
350 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
351 of tax names and exemption numbers. FS::cust_main_exemption records will be
352 created and inserted.
354 If I<prospectnum> is set, moves contacts and locations from that prospect.
360 my $cust_pkgs = @_ ? shift : {};
361 my $invoicing_list = @_ ? shift : '';
363 warn "$me insert called with options ".
364 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
367 local $SIG{HUP} = 'IGNORE';
368 local $SIG{INT} = 'IGNORE';
369 local $SIG{QUIT} = 'IGNORE';
370 local $SIG{TERM} = 'IGNORE';
371 local $SIG{TSTP} = 'IGNORE';
372 local $SIG{PIPE} = 'IGNORE';
374 my $oldAutoCommit = $FS::UID::AutoCommit;
375 local $FS::UID::AutoCommit = 0;
378 my $prepay_identifier = '';
379 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
381 if ( $self->payby eq 'PREPAY' ) {
383 $self->payby('BILL');
384 $prepay_identifier = $self->payinfo;
387 warn " looking up prepaid card $prepay_identifier\n"
390 my $error = $self->get_prepay( $prepay_identifier,
391 'amount_ref' => \$amount,
392 'seconds_ref' => \$seconds,
393 'upbytes_ref' => \$upbytes,
394 'downbytes_ref' => \$downbytes,
395 'totalbytes_ref' => \$totalbytes,
398 $dbh->rollback if $oldAutoCommit;
399 #return "error applying prepaid card (transaction rolled back): $error";
403 $payby = 'PREP' if $amount;
405 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD|MCHK|PPAL)$/ ) {
408 $self->payby('BILL');
409 $amount = $self->paid;
414 foreach my $l (qw(bill_location ship_location)) {
416 my $loc = delete $self->hashref->{$l} or return "$l not set";
418 if ( !$loc->locationnum ) {
419 # warn the location that we're going to insert it with no custnum
420 $loc->set(custnum_pending => 1);
421 warn " inserting $l\n"
423 my $error = $loc->insert;
425 $dbh->rollback if $oldAutoCommit;
426 my $label = $l eq 'ship_location' ? 'service' : 'billing';
427 return "$error (in $label location)";
430 } elsif ( $loc->prospectnum ) {
432 $loc->prospectnum('');
433 $loc->set(custnum_pending => 1);
434 my $error = $loc->replace;
436 $dbh->rollback if $oldAutoCommit;
437 my $label = $l eq 'ship_location' ? 'service' : 'billing';
438 return "$error (moving $label location)";
441 } elsif ( ($loc->custnum || 0) > 0 ) {
442 # then it somehow belongs to another customer--shouldn't happen
443 $dbh->rollback if $oldAutoCommit;
444 return "$l belongs to customer ".$loc->custnum;
446 # else it already belongs to this customer
447 # (happens when ship_location is identical to bill_location)
449 $self->set($l.'num', $loc->locationnum);
451 if ( $self->get($l.'num') eq '' ) {
452 $dbh->rollback if $oldAutoCommit;
457 warn " inserting $self\n"
460 $self->signupdate(time) unless $self->signupdate;
462 $self->auto_agent_custid()
463 if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
465 my $error = $self->check_payinfo_cardtype
466 || $self->SUPER::insert;
468 $dbh->rollback if $oldAutoCommit;
469 #return "inserting cust_main record (transaction rolled back): $error";
473 # now set cust_location.custnum
474 foreach my $l (qw(bill_location ship_location)) {
475 warn " setting $l.custnum\n"
478 unless ( $loc->custnum ) {
479 $loc->set(custnum => $self->custnum);
480 $error ||= $loc->replace;
484 $dbh->rollback if $oldAutoCommit;
485 return "error setting $l custnum: $error";
489 warn " setting invoicing list\n"
492 if ( $invoicing_list ) {
493 $error = $self->check_invoicing_list( $invoicing_list );
495 $dbh->rollback if $oldAutoCommit;
496 #return "checking invoicing_list (transaction rolled back): $error";
499 $self->invoicing_list( $invoicing_list );
502 warn " setting customer tags\n"
505 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
506 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
507 'custnum' => $self->custnum };
508 my $error = $cust_tag->insert;
510 $dbh->rollback if $oldAutoCommit;
515 my $prospectnum = delete $options{'prospectnum'};
516 if ( $prospectnum ) {
518 warn " moving contacts and locations from prospect $prospectnum\n"
522 qsearchs('prospect_main', { 'prospectnum' => $prospectnum } );
523 unless ( $prospect_main ) {
524 $dbh->rollback if $oldAutoCommit;
525 return "Unknown prospectnum $prospectnum";
527 $prospect_main->custnum($self->custnum);
528 $prospect_main->disabled('Y');
529 my $error = $prospect_main->replace;
531 $dbh->rollback if $oldAutoCommit;
535 my @contact = $prospect_main->contact;
536 my @cust_location = $prospect_main->cust_location;
537 my @qual = $prospect_main->qual;
539 foreach my $r ( @contact, @cust_location, @qual ) {
541 $r->custnum($self->custnum);
542 my $error = $r->replace;
544 $dbh->rollback if $oldAutoCommit;
551 # validate card (needs custnum already set)
552 if ( $self->payby =~ /^(CARD|DCRD)$/
553 && $conf->exists('business-onlinepayment-verification') ) {
554 $error = $self->realtime_verify_bop({ 'method'=>'CC' });
556 $dbh->rollback if $oldAutoCommit;
561 warn " setting contacts\n"
564 if ( my $contact = delete $options{'contact'} ) {
566 foreach my $c ( @$contact ) {
567 $c->custnum($self->custnum);
568 my $error = $c->insert;
570 $dbh->rollback if $oldAutoCommit;
576 } elsif ( my $contact_params = delete $options{'contact_params'} ) {
578 my $error = $self->process_o2m( 'table' => 'contact',
579 'fields' => FS::contact->cgi_contact_fields,
580 'params' => $contact_params,
583 $dbh->rollback if $oldAutoCommit;
588 warn " setting cust_main_exemption\n"
591 my $tax_exemption = delete $options{'tax_exemption'};
592 if ( $tax_exemption ) {
594 $tax_exemption = { map { $_ => '' } @$tax_exemption }
595 if ref($tax_exemption) eq 'ARRAY';
597 foreach my $taxname ( keys %$tax_exemption ) {
598 my $cust_main_exemption = new FS::cust_main_exemption {
599 'custnum' => $self->custnum,
600 'taxname' => $taxname,
601 'exempt_number' => $tax_exemption->{$taxname},
603 my $error = $cust_main_exemption->insert;
605 $dbh->rollback if $oldAutoCommit;
606 return "inserting cust_main_exemption (transaction rolled back): $error";
611 warn " ordering packages\n"
614 $error = $self->order_pkgs( $cust_pkgs,
616 'seconds_ref' => \$seconds,
617 'upbytes_ref' => \$upbytes,
618 'downbytes_ref' => \$downbytes,
619 'totalbytes_ref' => \$totalbytes,
622 $dbh->rollback if $oldAutoCommit;
627 $dbh->rollback if $oldAutoCommit;
628 return "No svc_acct record to apply pre-paid time";
630 if ( $upbytes || $downbytes || $totalbytes ) {
631 $dbh->rollback if $oldAutoCommit;
632 return "No svc_acct record to apply pre-paid data";
636 warn " inserting initial $payby payment of $amount\n"
638 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
640 $dbh->rollback if $oldAutoCommit;
641 return "inserting payment (transaction rolled back): $error";
645 unless ( $import || $skip_fuzzyfiles ) {
646 warn " queueing fuzzyfiles update\n"
648 $error = $self->queue_fuzzyfiles_update;
650 $dbh->rollback if $oldAutoCommit;
651 return "updating fuzzy search cache: $error";
655 # FS::geocode_Mixin::after_insert or something?
656 if ( $conf->config('tax_district_method') and !$import ) {
657 # if anything non-empty, try to look it up
658 my $queue = new FS::queue {
659 'job' => 'FS::geocode_Mixin::process_district_update',
660 'custnum' => $self->custnum,
662 my $error = $queue->insert( ref($self), $self->custnum );
664 $dbh->rollback if $oldAutoCommit;
665 return "queueing tax district update: $error";
670 warn " exporting\n" if $DEBUG > 1;
672 my $export_args = $options{'export_args'} || [];
675 map qsearch( 'part_export', {exportnum=>$_} ),
676 $conf->config('cust_main-exports'); #, $agentnum
678 foreach my $part_export ( @part_export ) {
679 my $error = $part_export->export_insert($self, @$export_args);
681 $dbh->rollback if $oldAutoCommit;
682 return "exporting to ". $part_export->exporttype.
683 " (transaction rolled back): $error";
687 #foreach my $depend_jobnum ( @$depend_jobnums ) {
688 # warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
690 # foreach my $jobnum ( @jobnums ) {
691 # my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
692 # warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
694 # my $error = $queue->depend_insert($depend_jobnum);
696 # $dbh->rollback if $oldAutoCommit;
697 # return "error queuing job dependancy: $error";
704 #if ( exists $options{'jobnums'} ) {
705 # push @{ $options{'jobnums'} }, @jobnums;
708 warn " insert complete; committing transaction\n"
711 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
716 use File::CounterFile;
717 sub auto_agent_custid {
720 my $format = $conf->config('cust_main-auto_agent_custid');
722 if ( $format eq '1YMMXXXXXXXX' ) {
724 my $counter = new File::CounterFile 'cust_main.agent_custid';
727 my $ym = 100000000000 + time2str('%y%m00000000', time);
728 if ( $ym > $counter->value ) {
729 $counter->{'value'} = $agent_custid = $ym;
730 $counter->{'updated'} = 1;
732 $agent_custid = $counter->inc;
738 die "Unknown cust_main-auto_agent_custid format: $format";
741 $self->agent_custid($agent_custid);
745 =item PACKAGE METHODS
747 Documentation on customer package methods has been moved to
748 L<FS::cust_main::Packages>.
750 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
752 Recharges this (existing) customer with the specified prepaid card (see
753 L<FS::prepay_credit>), specified either by I<identifier> or as an
754 FS::prepay_credit object. If there is an error, returns the error, otherwise
757 Optionally, five scalar references can be passed as well. They will have their
758 values filled in with the amount, number of seconds, and number of upload,
759 download, and total bytes applied by this prepaid card.
763 #the ref bullshit here should be refactored like get_prepay. MyAccount.pm is
764 #the only place that uses these args
765 sub recharge_prepay {
766 my( $self, $prepay_credit, $amountref, $secondsref,
767 $upbytesref, $downbytesref, $totalbytesref ) = @_;
769 local $SIG{HUP} = 'IGNORE';
770 local $SIG{INT} = 'IGNORE';
771 local $SIG{QUIT} = 'IGNORE';
772 local $SIG{TERM} = 'IGNORE';
773 local $SIG{TSTP} = 'IGNORE';
774 local $SIG{PIPE} = 'IGNORE';
776 my $oldAutoCommit = $FS::UID::AutoCommit;
777 local $FS::UID::AutoCommit = 0;
780 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
782 my $error = $self->get_prepay( $prepay_credit,
783 'amount_ref' => \$amount,
784 'seconds_ref' => \$seconds,
785 'upbytes_ref' => \$upbytes,
786 'downbytes_ref' => \$downbytes,
787 'totalbytes_ref' => \$totalbytes,
789 || $self->increment_seconds($seconds)
790 || $self->increment_upbytes($upbytes)
791 || $self->increment_downbytes($downbytes)
792 || $self->increment_totalbytes($totalbytes)
793 || $self->insert_cust_pay_prepay( $amount,
795 ? $prepay_credit->identifier
800 $dbh->rollback if $oldAutoCommit;
804 if ( defined($amountref) ) { $$amountref = $amount; }
805 if ( defined($secondsref) ) { $$secondsref = $seconds; }
806 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
807 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
808 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
810 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
815 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
817 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
818 specified either by I<identifier> or as an FS::prepay_credit object.
820 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
821 incremented by the values of the prepaid card.
823 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
824 check or set this customer's I<agentnum>.
826 If there is an error, returns the error, otherwise returns false.
832 my( $self, $prepay_credit, %opt ) = @_;
834 local $SIG{HUP} = 'IGNORE';
835 local $SIG{INT} = 'IGNORE';
836 local $SIG{QUIT} = 'IGNORE';
837 local $SIG{TERM} = 'IGNORE';
838 local $SIG{TSTP} = 'IGNORE';
839 local $SIG{PIPE} = 'IGNORE';
841 my $oldAutoCommit = $FS::UID::AutoCommit;
842 local $FS::UID::AutoCommit = 0;
845 unless ( ref($prepay_credit) ) {
847 my $identifier = $prepay_credit;
849 $prepay_credit = qsearchs(
851 { 'identifier' => $identifier },
856 unless ( $prepay_credit ) {
857 $dbh->rollback if $oldAutoCommit;
858 return "Invalid prepaid card: ". $identifier;
863 if ( $prepay_credit->agentnum ) {
864 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
865 $dbh->rollback if $oldAutoCommit;
866 return "prepaid card not valid for agent ". $self->agentnum;
868 $self->agentnum($prepay_credit->agentnum);
871 my $error = $prepay_credit->delete;
873 $dbh->rollback if $oldAutoCommit;
874 return "removing prepay_credit (transaction rolled back): $error";
877 ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
878 for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
880 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
885 =item increment_upbytes SECONDS
887 Updates this customer's single or primary account (see L<FS::svc_acct>) by
888 the specified number of upbytes. If there is an error, returns the error,
889 otherwise returns false.
893 sub increment_upbytes {
894 _increment_column( shift, 'upbytes', @_);
897 =item increment_downbytes SECONDS
899 Updates this customer's single or primary account (see L<FS::svc_acct>) by
900 the specified number of downbytes. If there is an error, returns the error,
901 otherwise returns false.
905 sub increment_downbytes {
906 _increment_column( shift, 'downbytes', @_);
909 =item increment_totalbytes SECONDS
911 Updates this customer's single or primary account (see L<FS::svc_acct>) by
912 the specified number of totalbytes. If there is an error, returns the error,
913 otherwise returns false.
917 sub increment_totalbytes {
918 _increment_column( shift, 'totalbytes', @_);
921 =item increment_seconds SECONDS
923 Updates this customer's single or primary account (see L<FS::svc_acct>) by
924 the specified number of seconds. If there is an error, returns the error,
925 otherwise returns false.
929 sub increment_seconds {
930 _increment_column( shift, 'seconds', @_);
933 =item _increment_column AMOUNT
935 Updates this customer's single or primary account (see L<FS::svc_acct>) by
936 the specified number of seconds or bytes. If there is an error, returns
937 the error, otherwise returns false.
941 sub _increment_column {
942 my( $self, $column, $amount ) = @_;
943 warn "$me increment_column called: $column, $amount\n"
946 return '' unless $amount;
948 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
949 $self->ncancelled_pkgs;
952 return 'No packages with primary or single services found'.
953 ' to apply pre-paid time';
954 } elsif ( scalar(@cust_pkg) > 1 ) {
955 #maybe have a way to specify the package/account?
956 return 'Multiple packages found to apply pre-paid time';
959 my $cust_pkg = $cust_pkg[0];
960 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
964 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
967 return 'No account found to apply pre-paid time';
968 } elsif ( scalar(@cust_svc) > 1 ) {
969 return 'Multiple accounts found to apply pre-paid time';
972 my $svc_acct = $cust_svc[0]->svc_x;
973 warn " found service svcnum ". $svc_acct->pkgnum.
974 ' ('. $svc_acct->email. ")\n"
977 $column = "increment_$column";
978 $svc_acct->$column($amount);
982 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
984 Inserts a prepayment in the specified amount for this customer. An optional
985 second argument can specify the prepayment identifier for tracking purposes.
986 If there is an error, returns the error, otherwise returns false.
990 sub insert_cust_pay_prepay {
991 shift->insert_cust_pay('PREP', @_);
994 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
996 Inserts a cash payment in the specified amount for this customer. An optional
997 second argument can specify the payment identifier for tracking purposes.
998 If there is an error, returns the error, otherwise returns false.
1002 sub insert_cust_pay_cash {
1003 shift->insert_cust_pay('CASH', @_);
1006 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
1008 Inserts a Western Union payment in the specified amount for this customer. An
1009 optional second argument can specify the prepayment identifier for tracking
1010 purposes. If there is an error, returns the error, otherwise returns false.
1014 sub insert_cust_pay_west {
1015 shift->insert_cust_pay('WEST', @_);
1018 sub insert_cust_pay {
1019 my( $self, $payby, $amount ) = splice(@_, 0, 3);
1020 my $payinfo = scalar(@_) ? shift : '';
1022 my $cust_pay = new FS::cust_pay {
1023 'custnum' => $self->custnum,
1024 'paid' => sprintf('%.2f', $amount),
1025 #'_date' => #date the prepaid card was purchased???
1027 'payinfo' => $payinfo,
1035 This method is deprecated. See the I<depend_jobnum> option to the insert and
1036 order_pkgs methods for a better way to defer provisioning.
1038 Re-schedules all exports by calling the B<reexport> method of all associated
1039 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
1040 otherwise returns false.
1047 carp "WARNING: FS::cust_main::reexport is deprectated; ".
1048 "use the depend_jobnum option to insert or order_pkgs to delay export";
1050 local $SIG{HUP} = 'IGNORE';
1051 local $SIG{INT} = 'IGNORE';
1052 local $SIG{QUIT} = 'IGNORE';
1053 local $SIG{TERM} = 'IGNORE';
1054 local $SIG{TSTP} = 'IGNORE';
1055 local $SIG{PIPE} = 'IGNORE';
1057 my $oldAutoCommit = $FS::UID::AutoCommit;
1058 local $FS::UID::AutoCommit = 0;
1061 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
1062 my $error = $cust_pkg->reexport;
1064 $dbh->rollback if $oldAutoCommit;
1069 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1074 =item delete [ OPTION => VALUE ... ]
1076 This deletes the customer. If there is an error, returns the error, otherwise
1079 This will completely remove all traces of the customer record. This is not
1080 what you want when a customer cancels service; for that, cancel all of the
1081 customer's packages (see L</cancel>).
1083 If the customer has any uncancelled packages, you need to pass a new (valid)
1084 customer number for those packages to be transferred to, as the "new_customer"
1085 option. Cancelled packages will be deleted. Did I mention that this is NOT
1086 what you want when a customer cancels service and that you really should be
1087 looking at L<FS::cust_pkg/cancel>?
1089 You can't delete a customer with invoices (see L<FS::cust_bill>),
1090 statements (see L<FS::cust_statement>), credits (see L<FS::cust_credit>),
1091 payments (see L<FS::cust_pay>) or refunds (see L<FS::cust_refund>), unless you
1092 set the "delete_financials" option to a true value.
1097 my( $self, %opt ) = @_;
1099 local $SIG{HUP} = 'IGNORE';
1100 local $SIG{INT} = 'IGNORE';
1101 local $SIG{QUIT} = 'IGNORE';
1102 local $SIG{TERM} = 'IGNORE';
1103 local $SIG{TSTP} = 'IGNORE';
1104 local $SIG{PIPE} = 'IGNORE';
1106 my $oldAutoCommit = $FS::UID::AutoCommit;
1107 local $FS::UID::AutoCommit = 0;
1110 if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1111 $dbh->rollback if $oldAutoCommit;
1112 return "Can't delete a master agent customer";
1115 #use FS::access_user
1116 if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1117 $dbh->rollback if $oldAutoCommit;
1118 return "Can't delete a master employee customer";
1121 tie my %financial_tables, 'Tie::IxHash',
1122 'cust_bill' => 'invoices',
1123 'cust_statement' => 'statements',
1124 'cust_credit' => 'credits',
1125 'cust_pay' => 'payments',
1126 'cust_refund' => 'refunds',
1129 foreach my $table ( keys %financial_tables ) {
1131 my @records = $self->$table();
1133 if ( @records && ! $opt{'delete_financials'} ) {
1134 $dbh->rollback if $oldAutoCommit;
1135 return "Can't delete a customer with ". $financial_tables{$table};
1138 foreach my $record ( @records ) {
1139 my $error = $record->delete;
1141 $dbh->rollback if $oldAutoCommit;
1142 return "Error deleting ". $financial_tables{$table}. ": $error\n";
1148 my @cust_pkg = $self->ncancelled_pkgs;
1150 my $new_custnum = $opt{'new_custnum'};
1151 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1152 $dbh->rollback if $oldAutoCommit;
1153 return "Invalid new customer number: $new_custnum";
1155 foreach my $cust_pkg ( @cust_pkg ) {
1156 my %hash = $cust_pkg->hash;
1157 $hash{'custnum'} = $new_custnum;
1158 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1159 my $error = $new_cust_pkg->replace($cust_pkg,
1160 options => { $cust_pkg->options },
1163 $dbh->rollback if $oldAutoCommit;
1168 my @cancelled_cust_pkg = $self->all_pkgs;
1169 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1170 my $error = $cust_pkg->delete;
1172 $dbh->rollback if $oldAutoCommit;
1177 #cust_tax_adjustment in financials?
1178 #cust_pay_pending? ouch
1180 foreach my $table (qw(
1181 cust_main_invoice cust_main_exemption cust_tag cust_attachment contact
1182 cust_location cust_main_note cust_tax_adjustment
1183 cust_pay_void cust_pay_batch queue cust_tax_exempt
1185 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1186 my $error = $record->delete;
1188 $dbh->rollback if $oldAutoCommit;
1194 my $sth = $dbh->prepare(
1195 'UPDATE cust_main SET referral_custnum = NULL WHERE referral_custnum = ?'
1197 my $errstr = $dbh->errstr;
1198 $dbh->rollback if $oldAutoCommit;
1201 $sth->execute($self->custnum) or do {
1202 my $errstr = $sth->errstr;
1203 $dbh->rollback if $oldAutoCommit;
1209 my $ticket_dbh = '';
1210 if ($conf->config('ticket_system') eq 'RT_Internal') {
1212 } elsif ($conf->config('ticket_system') eq 'RT_External') {
1213 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1214 $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1215 #or die "RT_External DBI->connect error: $DBI::errstr\n";
1218 if ( $ticket_dbh ) {
1220 my $ticket_sth = $ticket_dbh->prepare(
1221 'DELETE FROM Links WHERE Target = ?'
1223 my $errstr = $ticket_dbh->errstr;
1224 $dbh->rollback if $oldAutoCommit;
1227 $ticket_sth->execute('freeside://freeside/cust_main/'.$self->custnum)
1229 my $errstr = $ticket_sth->errstr;
1230 $dbh->rollback if $oldAutoCommit;
1234 #check and see if the customer is the only link on the ticket, and
1235 #if so, set the ticket to deleted status in RT?
1236 #maybe someday, for now this will at least fix tickets not displaying
1240 #delete the customer record
1242 my $error = $self->SUPER::delete;
1244 $dbh->rollback if $oldAutoCommit;
1248 # cust_main exports!
1250 #my $export_args = $options{'export_args'} || [];
1253 map qsearch( 'part_export', {exportnum=>$_} ),
1254 $conf->config('cust_main-exports'); #, $agentnum
1256 foreach my $part_export ( @part_export ) {
1257 my $error = $part_export->export_delete( $self ); #, @$export_args);
1259 $dbh->rollback if $oldAutoCommit;
1260 return "exporting to ". $part_export->exporttype.
1261 " (transaction rolled back): $error";
1265 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1270 =item merge NEW_CUSTNUM [ , OPTION => VALUE ... ]
1272 This merges this customer into the provided new custnum, and then deletes the
1273 customer. If there is an error, returns the error, otherwise returns false.
1275 The source customer's name, company name, phone numbers, agent,
1276 referring customer, customer class, advertising source, order taker, and
1277 billing information (except balance) are discarded.
1279 All packages are moved to the target customer. Packages with package locations
1280 are preserved. Packages without package locations are moved to a new package
1281 location with the source customer's service/shipping address.
1283 All invoices, statements, payments, credits and refunds are moved to the target
1284 customer. The source customer's balance is added to the target customer.
1286 All notes, attachments, tickets and customer tags are moved to the target
1289 Change history is not currently moved.
1294 my( $self, $new_custnum, %opt ) = @_;
1296 return "Can't merge a customer into self" if $self->custnum == $new_custnum;
1298 my $new_cust_main = qsearchs( 'cust_main', { 'custnum' => $new_custnum } )
1299 or return "Invalid new customer number: $new_custnum";
1301 return 'Access denied: "Merge customer across agents" access right required to merge into a customer of a different agent'
1302 if $self->agentnum != $new_cust_main->agentnum
1303 && ! $FS::CurrentUser::CurrentUser->access_right('Merge customer across agents');
1305 local $SIG{HUP} = 'IGNORE';
1306 local $SIG{INT} = 'IGNORE';
1307 local $SIG{QUIT} = 'IGNORE';
1308 local $SIG{TERM} = 'IGNORE';
1309 local $SIG{TSTP} = 'IGNORE';
1310 local $SIG{PIPE} = 'IGNORE';
1312 my $oldAutoCommit = $FS::UID::AutoCommit;
1313 local $FS::UID::AutoCommit = 0;
1316 if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1317 $dbh->rollback if $oldAutoCommit;
1318 return "Can't merge a master agent customer";
1321 #use FS::access_user
1322 if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1323 $dbh->rollback if $oldAutoCommit;
1324 return "Can't merge a master employee customer";
1327 if ( qsearch('cust_pay_pending', { 'custnum' => $self->custnum,
1328 'status' => { op=>'!=', value=>'done' },
1332 $dbh->rollback if $oldAutoCommit;
1333 return "Can't merge a customer with pending payments";
1336 tie my %financial_tables, 'Tie::IxHash',
1337 'cust_bill' => 'invoices',
1338 'cust_bill_void' => 'voided invoices',
1339 'cust_statement' => 'statements',
1340 'cust_credit' => 'credits',
1341 'cust_credit_void' => 'voided credits',
1342 'cust_pay' => 'payments',
1343 'cust_pay_void' => 'voided payments',
1344 'cust_refund' => 'refunds',
1347 foreach my $table ( keys %financial_tables ) {
1349 my @records = $self->$table();
1351 foreach my $record ( @records ) {
1352 $record->custnum($new_custnum);
1353 my $error = $record->replace;
1355 $dbh->rollback if $oldAutoCommit;
1356 return "Error merging ". $financial_tables{$table}. ": $error\n";
1362 my $name = $self->ship_name; #?
1364 my $locationnum = '';
1365 foreach my $cust_pkg ( $self->all_pkgs ) {
1366 $cust_pkg->custnum($new_custnum);
1368 unless ( $cust_pkg->locationnum ) {
1369 unless ( $locationnum ) {
1370 my $cust_location = new FS::cust_location {
1371 $self->location_hash,
1372 'custnum' => $new_custnum,
1374 my $error = $cust_location->insert;
1376 $dbh->rollback if $oldAutoCommit;
1379 $locationnum = $cust_location->locationnum;
1381 $cust_pkg->locationnum($locationnum);
1384 my $error = $cust_pkg->replace;
1386 $dbh->rollback if $oldAutoCommit;
1390 # add customer (ship) name to svc_phone.phone_name if blank
1391 my @cust_svc = $cust_pkg->cust_svc;
1392 foreach my $cust_svc (@cust_svc) {
1393 my($label, $value, $svcdb) = $cust_svc->label;
1394 next unless $svcdb eq 'svc_phone';
1395 my $svc_phone = $cust_svc->svc_x;
1396 next if $svc_phone->phone_name;
1397 $svc_phone->phone_name($name);
1398 my $error = $svc_phone->replace;
1400 $dbh->rollback if $oldAutoCommit;
1408 # cust_tax_exempt (texas tax exemptions)
1409 # cust_recon (some sort of not-well understood thing for OnPac)
1411 #these are moved over
1412 foreach my $table (qw(
1413 cust_tag cust_location contact cust_attachment cust_main_note
1414 cust_tax_adjustment cust_pay_batch queue
1416 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1417 $record->custnum($new_custnum);
1418 my $error = $record->replace;
1420 $dbh->rollback if $oldAutoCommit;
1426 #these aren't preserved
1427 foreach my $table (qw(
1428 cust_main_exemption cust_main_invoice
1430 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1431 my $error = $record->delete;
1433 $dbh->rollback if $oldAutoCommit;
1440 my $sth = $dbh->prepare(
1441 'UPDATE cust_main SET referral_custnum = ? WHERE referral_custnum = ?'
1443 my $errstr = $dbh->errstr;
1444 $dbh->rollback if $oldAutoCommit;
1447 $sth->execute($new_custnum, $self->custnum) or do {
1448 my $errstr = $sth->errstr;
1449 $dbh->rollback if $oldAutoCommit;
1455 my $ticket_dbh = '';
1456 if ($conf->config('ticket_system') eq 'RT_Internal') {
1458 } elsif ($conf->config('ticket_system') eq 'RT_External') {
1459 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1460 $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1461 #or die "RT_External DBI->connect error: $DBI::errstr\n";
1464 if ( $ticket_dbh ) {
1466 my $ticket_sth = $ticket_dbh->prepare(
1467 'UPDATE Links SET Target = ? WHERE Target = ?'
1469 my $errstr = $ticket_dbh->errstr;
1470 $dbh->rollback if $oldAutoCommit;
1473 $ticket_sth->execute('freeside://freeside/cust_main/'.$new_custnum,
1474 'freeside://freeside/cust_main/'.$self->custnum)
1476 my $errstr = $ticket_sth->errstr;
1477 $dbh->rollback if $oldAutoCommit;
1483 #delete the customer record
1485 my $error = $self->delete;
1487 $dbh->rollback if $oldAutoCommit;
1491 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1496 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
1498 Replaces the OLD_RECORD with this one in the database. If there is an error,
1499 returns the error, otherwise returns false.
1501 To change the customer's address, set the pseudo-fields C<bill_location> and
1502 C<ship_location>. The address will still only change if at least one of the
1503 address fields differs from the existing values.
1505 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1506 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1507 expected and rollback the entire transaction; it is not necessary to call
1508 check_invoicing_list first. Here's an example:
1510 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1512 Currently available options are: I<tax_exemption>.
1514 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
1515 of tax names and exemption numbers. FS::cust_main_exemption records will be
1516 deleted and inserted as appropriate.
1523 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1525 : $self->replace_old;
1529 warn "$me replace called\n"
1532 my $curuser = $FS::CurrentUser::CurrentUser;
1533 if ( $self->payby eq 'COMP'
1534 && $self->payby ne $old->payby
1535 && ! $curuser->access_right('Complimentary customer')
1538 return "You are not permitted to create complimentary accounts.";
1541 local($ignore_expired_card) = 1
1542 if $old->payby =~ /^(CARD|DCRD)$/
1543 && $self->payby =~ /^(CARD|DCRD)$/
1544 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1546 local($ignore_banned_card) = 1
1547 if ( $old->payby =~ /^(CARD|DCRD)$/ && $self->payby =~ /^(CARD|DCRD)$/
1548 || $old->payby =~ /^(CHEK|DCHK)$/ && $self->payby =~ /^(CHEK|DCHK)$/ )
1549 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1551 if ( $self->payby =~ /^(CARD|DCRD)$/
1552 && $old->payinfo ne $self->payinfo
1553 && $old->paymask ne $self->paymask )
1555 my $error = $self->check_payinfo_cardtype;
1556 return $error if $error;
1558 if ( $conf->exists('business-onlinepayment-verification') ) {
1559 #need to standardize paydate for this, false laziness with check
1561 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1562 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1563 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1564 ( $m, $y ) = ( $2, "19$1" );
1565 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1566 ( $m, $y ) = ( $3, "20$2" );
1568 return "Illegal expiration date: ". $self->paydate;
1570 $m = sprintf('%02d',$m);
1571 $self->paydate("$y-$m-01");
1573 $error = $self->realtime_verify_bop({ 'method'=>'CC' });
1574 return $error if $error;
1578 return "Invoicing locale is required"
1581 && $conf->exists('cust_main-require_locale');
1583 local $SIG{HUP} = 'IGNORE';
1584 local $SIG{INT} = 'IGNORE';
1585 local $SIG{QUIT} = 'IGNORE';
1586 local $SIG{TERM} = 'IGNORE';
1587 local $SIG{TSTP} = 'IGNORE';
1588 local $SIG{PIPE} = 'IGNORE';
1590 my $oldAutoCommit = $FS::UID::AutoCommit;
1591 local $FS::UID::AutoCommit = 0;
1594 for my $l (qw(bill_location ship_location)) {
1595 my $old_loc = $old->$l;
1596 my $new_loc = $self->$l;
1598 # find the existing location if there is one
1599 $new_loc->set('custnum' => $self->custnum);
1600 my $error = $new_loc->find_or_insert;
1602 $dbh->rollback if $oldAutoCommit;
1605 $self->set($l.'num', $new_loc->locationnum);
1608 # replace the customer record
1609 my $error = $self->SUPER::replace($old);
1612 $dbh->rollback if $oldAutoCommit;
1616 # now move packages to the new service location
1617 $self->set('ship_location', ''); #flush cache
1618 if ( $old->ship_locationnum and # should only be null during upgrade...
1619 $old->ship_locationnum != $self->ship_locationnum ) {
1620 $error = $old->ship_location->move_to($self->ship_location);
1622 $dbh->rollback if $oldAutoCommit;
1626 # don't move packages based on the billing location, but
1627 # disable it if it's no longer in use
1628 if ( $old->bill_locationnum and
1629 $old->bill_locationnum != $self->bill_locationnum ) {
1630 $error = $old->bill_location->disable_if_unused;
1632 $dbh->rollback if $oldAutoCommit;
1637 if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
1638 my $invoicing_list = shift @param;
1639 $error = $self->check_invoicing_list( $invoicing_list );
1641 $dbh->rollback if $oldAutoCommit;
1644 $self->invoicing_list( $invoicing_list );
1647 if ( $self->exists('tagnum') ) { #so we don't delete these on edit by accident
1649 #this could be more efficient than deleting and re-inserting, if it matters
1650 foreach my $cust_tag (qsearch('cust_tag', {'custnum'=>$self->custnum} )) {
1651 my $error = $cust_tag->delete;
1653 $dbh->rollback if $oldAutoCommit;
1657 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
1658 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
1659 'custnum' => $self->custnum };
1660 my $error = $cust_tag->insert;
1662 $dbh->rollback if $oldAutoCommit;
1669 my %options = @param;
1671 my $tax_exemption = delete $options{'tax_exemption'};
1672 if ( $tax_exemption ) {
1674 $tax_exemption = { map { $_ => '' } @$tax_exemption }
1675 if ref($tax_exemption) eq 'ARRAY';
1677 my %cust_main_exemption =
1678 map { $_->taxname => $_ }
1679 qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
1681 foreach my $taxname ( keys %$tax_exemption ) {
1683 if ( $cust_main_exemption{$taxname} &&
1684 $cust_main_exemption{$taxname}->exempt_number eq $tax_exemption->{$taxname}
1687 delete $cust_main_exemption{$taxname};
1691 my $cust_main_exemption = new FS::cust_main_exemption {
1692 'custnum' => $self->custnum,
1693 'taxname' => $taxname,
1694 'exempt_number' => $tax_exemption->{$taxname},
1696 my $error = $cust_main_exemption->insert;
1698 $dbh->rollback if $oldAutoCommit;
1699 return "inserting cust_main_exemption (transaction rolled back): $error";
1703 foreach my $cust_main_exemption ( values %cust_main_exemption ) {
1704 my $error = $cust_main_exemption->delete;
1706 $dbh->rollback if $oldAutoCommit;
1707 return "deleting cust_main_exemption (transaction rolled back): $error";
1713 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/
1714 && ( ( $self->get('payinfo') ne $old->get('payinfo')
1715 && $self->get('payinfo') !~ /^99\d{14}$/
1717 || grep { $self->get($_) ne $old->get($_) } qw(paydate payname)
1722 # card/check/lec info has changed, want to retry realtime_ invoice events
1723 my $error = $self->retry_realtime;
1725 $dbh->rollback if $oldAutoCommit;
1730 unless ( $import || $skip_fuzzyfiles ) {
1731 $error = $self->queue_fuzzyfiles_update;
1733 $dbh->rollback if $oldAutoCommit;
1734 return "updating fuzzy search cache: $error";
1738 # tax district update in cust_location
1740 # cust_main exports!
1742 my $export_args = $options{'export_args'} || [];
1745 map qsearch( 'part_export', {exportnum=>$_} ),
1746 $conf->config('cust_main-exports'); #, $agentnum
1748 foreach my $part_export ( @part_export ) {
1749 my $error = $part_export->export_replace( $self, $old, @$export_args);
1751 $dbh->rollback if $oldAutoCommit;
1752 return "exporting to ". $part_export->exporttype.
1753 " (transaction rolled back): $error";
1757 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1762 =item queue_fuzzyfiles_update
1764 Used by insert & replace to update the fuzzy search cache
1768 use FS::cust_main::Search;
1769 sub queue_fuzzyfiles_update {
1772 local $SIG{HUP} = 'IGNORE';
1773 local $SIG{INT} = 'IGNORE';
1774 local $SIG{QUIT} = 'IGNORE';
1775 local $SIG{TERM} = 'IGNORE';
1776 local $SIG{TSTP} = 'IGNORE';
1777 local $SIG{PIPE} = 'IGNORE';
1779 my $oldAutoCommit = $FS::UID::AutoCommit;
1780 local $FS::UID::AutoCommit = 0;
1783 foreach my $field ( 'first', 'last', 'company', 'ship_company' ) {
1784 my $queue = new FS::queue {
1785 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1787 my @args = "cust_main.$field", $self->get($field);
1788 my $error = $queue->insert( @args );
1790 $dbh->rollback if $oldAutoCommit;
1791 return "queueing job (transaction rolled back): $error";
1795 my @locations = $self->bill_location;
1796 push @locations, $self->ship_location if $self->has_ship_address;
1797 foreach my $location (@locations) {
1798 my $queue = new FS::queue {
1799 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1801 my @args = 'cust_location.address1', $location->address1;
1802 my $error = $queue->insert( @args );
1804 $dbh->rollback if $oldAutoCommit;
1805 return "queueing job (transaction rolled back): $error";
1809 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1816 Checks all fields to make sure this is a valid customer record. If there is
1817 an error, returns the error, otherwise returns false. Called by the insert
1818 and replace methods.
1825 warn "$me check BEFORE: \n". $self->_dump
1829 $self->ut_numbern('custnum')
1830 || $self->ut_number('agentnum')
1831 || $self->ut_textn('agent_custid')
1832 || $self->ut_number('refnum')
1833 || $self->ut_foreign_key('bill_locationnum', 'cust_location','locationnum')
1834 || $self->ut_foreign_key('ship_locationnum', 'cust_location','locationnum')
1835 || $self->ut_foreign_keyn('classnum', 'cust_class', 'classnum')
1836 || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
1837 || $self->ut_textn('custbatch')
1838 || $self->ut_name('last')
1839 || $self->ut_name('first')
1840 || $self->ut_snumbern('signupdate')
1841 || $self->ut_snumbern('birthdate')
1842 || $self->ut_namen('spouse_last')
1843 || $self->ut_namen('spouse_first')
1844 || $self->ut_snumbern('spouse_birthdate')
1845 || $self->ut_snumbern('anniversary_date')
1846 || $self->ut_textn('company')
1847 || $self->ut_textn('ship_company')
1848 || $self->ut_anything('comments')
1849 || $self->ut_numbern('referral_custnum')
1850 || $self->ut_textn('stateid')
1851 || $self->ut_textn('stateid_state')
1852 || $self->ut_textn('invoice_terms')
1853 || $self->ut_floatn('cdr_termination_percentage')
1854 || $self->ut_floatn('credit_limit')
1855 || $self->ut_numbern('billday')
1856 || $self->ut_numbern('prorate_day')
1857 || $self->ut_flag('force_prorate_day')
1858 || $self->ut_flag('edit_subject')
1859 || $self->ut_flag('calling_list_exempt')
1860 || $self->ut_flag('invoice_noemail')
1861 || $self->ut_flag('message_noemail')
1862 || $self->ut_enum('locale', [ '', FS::Locales->locales ])
1863 || $self->ut_flag('invoice_ship_address')
1866 foreach (qw(company ship_company)) {
1867 my $company = $self->get($_);
1868 $company =~ s/^\s+//;
1869 $company =~ s/\s+$//;
1870 $company =~ s/\s+/ /g;
1871 $self->set($_, $company);
1874 #barf. need message catalogs. i18n. etc.
1875 $error .= "Please select an advertising source."
1876 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1877 return $error if $error;
1879 return "Unknown agent"
1880 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1882 return "Unknown refnum"
1883 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1885 return "Unknown referring custnum: ". $self->referral_custnum
1886 unless ! $self->referral_custnum
1887 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1889 if ( $self->ss eq '' ) {
1894 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1895 or return "Illegal social security number: ". $self->ss;
1896 $self->ss("$1-$2-$3");
1899 #turn off invoice_ship_address if ship & bill are the same
1900 if ($self->bill_locationnum eq $self->ship_locationnum) {
1901 $self->invoice_ship_address('');
1904 # cust_main_county verification now handled by cust_location check
1907 $self->ut_phonen('daytime', $self->country)
1908 || $self->ut_phonen('night', $self->country)
1909 || $self->ut_phonen('fax', $self->country)
1910 || $self->ut_phonen('mobile', $self->country)
1912 return $error if $error;
1914 if ( $conf->exists('cust_main-require_phone', $self->agentnum)
1916 && ! length($self->daytime) && ! length($self->night) && ! length($self->mobile)
1919 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1921 : FS::Msgcat::_gettext('daytime');
1922 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1924 : FS::Msgcat::_gettext('night');
1926 my $mobile_label = FS::Msgcat::_gettext('mobile') =~ /^(mobile)?$/
1928 : FS::Msgcat::_gettext('mobile');
1930 return "$daytime_label, $night_label or $mobile_label is required"
1934 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1935 # or return "Illegal payby: ". $self->payby;
1937 FS::payby->can_payby($self->table, $self->payby)
1938 or return "Illegal payby: ". $self->payby;
1940 $error = $self->ut_numbern('paystart_month')
1941 || $self->ut_numbern('paystart_year')
1942 || $self->ut_numbern('payissue')
1943 || $self->ut_textn('paytype')
1945 return $error if $error;
1947 if ( $self->payip eq '' ) {
1950 $error = $self->ut_ip('payip');
1951 return $error if $error;
1954 # If it is encrypted and the private key is not availaible then we can't
1955 # check the credit card.
1956 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
1958 # Need some kind of global flag to accept invalid cards, for testing
1960 if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1962 my $payinfo = $self->payinfo;
1963 $payinfo =~ s/\D//g;
1964 $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1965 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1967 $self->payinfo($payinfo);
1969 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1971 my $cardtype = $self->paycardtype;
1972 if ( $payinfo =~ /^99\d{14}$/ ) {
1973 $self->set('is_tokenized', 'Y'); #so we don't try to do it again
1974 if ( $self->paymask =~ /^\d+x/ ) {
1975 $cardtype = cardtype($self->paymask);
1977 #return "paycardtype required ".
1978 # "(can't derive from a token and no paymask w/prefix provided)"
1982 $cardtype = cardtype($self->payinfo);
1985 return gettext('unknown_card_type') if $cardtype eq 'Unknown';
1987 $self->set('paycardtype', $cardtype);
1989 unless ( $ignore_banned_card ) {
1990 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
1992 if ( $ban->bantype eq 'warn' ) {
1993 #or others depending on value of $ban->reason ?
1994 return '_duplicate_card'.
1995 ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
1996 ' until '. time2str('%a %h %o at %r', $ban->_end_date).
1997 ' (ban# '. $ban->bannum. ')'
1998 unless $self->override_ban_warn;
2000 return 'Banned credit card: banned on '.
2001 time2str('%a %h %o at %r', $ban->_date).
2002 ' by '. $ban->otaker.
2003 ' (ban# '. $ban->bannum. ')';
2008 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
2009 if ( $cardtype eq 'American Express card' ) {
2010 $self->paycvv =~ /^(\d{4})$/
2011 or return "CVV2 (CID) for American Express cards is four digits.";
2014 $self->paycvv =~ /^(\d{3})$/
2015 or return "CVV2 (CVC2/CID) is three digits.";
2022 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
2024 return "Start date or issue number is required for $cardtype cards"
2025 unless $self->paystart_month && $self->paystart_year or $self->payissue;
2027 return "Start month must be between 1 and 12"
2028 if $self->paystart_month
2029 and $self->paystart_month < 1 || $self->paystart_month > 12;
2031 return "Start year must be 1990 or later"
2032 if $self->paystart_year
2033 and $self->paystart_year < 1990;
2035 return "Issue number must be beween 1 and 99"
2037 and $self->payissue < 1 || $self->payissue > 99;
2040 $self->paystart_month('');
2041 $self->paystart_year('');
2042 $self->payissue('');
2045 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
2047 my $payinfo = $self->payinfo;
2048 $payinfo =~ s/[^\d\@\.]//g;
2049 if ( $conf->config('echeck-country') eq 'CA' ) {
2050 $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
2051 or return 'invalid echeck account@branch.bank';
2052 $payinfo = "$1\@$2.$3";
2053 } elsif ( $conf->config('echeck-country') eq 'US' ) {
2054 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
2055 $payinfo = "$1\@$2";
2057 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
2058 $payinfo = "$1\@$2";
2060 $self->payinfo($payinfo);
2063 unless ( $ignore_banned_card ) {
2064 my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
2066 if ( $ban->bantype eq 'warn' ) {
2067 #or others depending on value of $ban->reason ?
2068 return '_duplicate_ach' unless $self->override_ban_warn;
2070 return 'Banned ACH account: banned on '.
2071 time2str('%a %h %o at %r', $ban->_date).
2072 ' by '. $ban->otaker.
2073 ' (ban# '. $ban->bannum. ')';
2078 } elsif ( $self->payby eq 'LECB' ) {
2080 my $payinfo = $self->payinfo;
2081 $payinfo =~ s/\D//g;
2082 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
2084 $self->payinfo($payinfo);
2087 } elsif ( $self->payby eq 'BILL' ) {
2089 $error = $self->ut_textn('payinfo');
2090 return "Illegal P.O. number: ". $self->payinfo if $error;
2093 } elsif ( $self->payby eq 'COMP' ) {
2095 my $curuser = $FS::CurrentUser::CurrentUser;
2096 if ( ! $self->custnum
2097 && ! $curuser->access_right('Complimentary customer')
2100 return "You are not permitted to create complimentary accounts."
2103 $error = $self->ut_textn('payinfo');
2104 return "Illegal comp account issuer: ". $self->payinfo if $error;
2107 } elsif ( $self->payby eq 'PREPAY' ) {
2109 my $payinfo = $self->payinfo;
2110 $payinfo =~ s/\W//g; #anything else would just confuse things
2111 $self->payinfo($payinfo);
2112 $error = $self->ut_alpha('payinfo');
2113 return "Illegal prepayment identifier: ". $self->payinfo if $error;
2114 return "Unknown prepayment identifier"
2115 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
2118 } elsif ( $self->payby =~ /^CARD|DCRD$/ and $self->paymask ) {
2119 # either ignoring invalid cards, or we can't decrypt the payinfo, but
2120 # try to detect the card type anyway. this never returns failure, so
2121 # the contract of $ignore_invalid_cards is maintained.
2122 $self->set('paycardtype', cardtype($self->paymask));
2125 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
2126 return "Expiration date required"
2127 # shouldn't payinfo_check do this?
2128 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
2132 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
2133 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
2134 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
2135 ( $m, $y ) = ( $2, "19$1" );
2136 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
2137 ( $m, $y ) = ( $3, "20$2" );
2139 return "Illegal expiration date: ". $self->paydate;
2141 $m = sprintf('%02d',$m);
2142 $self->paydate("$y-$m-01");
2143 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
2144 return gettext('expired_card')
2146 && !$ignore_expired_card
2147 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
2149 if ( my $error = $self->ut_daten('paydate') ) {
2154 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
2155 ( ! $conf->exists('require_cardname')
2156 || $self->payby !~ /^(CARD|DCRD)$/ )
2158 $self->payname( $self->first. " ". $self->getfield('last') );
2161 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
2162 $self->payname =~ /^([\w \,\.\-\']*)$/
2163 or return gettext('illegal_name'). " payname: ". $self->payname;
2166 $self->payname =~ /^([\w \,\.\-\'\&]*)$/
2167 or return gettext('illegal_name'). " payname: ". $self->payname;
2173 return "Please select an invoicing locale"
2176 && $conf->exists('cust_main-require_locale');
2178 return "Please select a customer class"
2179 if ! $self->classnum
2180 && $conf->exists('cust_main-require_classnum');
2182 foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
2183 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
2187 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
2189 warn "$me check AFTER: \n". $self->_dump
2192 $self->SUPER::check;
2195 sub check_payinfo_cardtype {
2198 return '' unless $self->payby =~ /^(CARD|DCRD)$/;
2200 my $payinfo = $self->payinfo;
2201 $payinfo =~ s/\D//g;
2203 if ( $payinfo =~ /^99\d{14}$/ ) {
2207 my %bop_card_types = map { $_=>1 } values %{ card_types() };
2208 my $cardtype = cardtype($payinfo);
2209 $self->set('paycardtype', $cardtype);
2211 return "$cardtype not accepted" unless $bop_card_types{$cardtype};
2219 Additional checks for replace only.
2224 my ($new,$old) = @_;
2225 #preserve old value if global config is set
2226 if ($old && $conf->exists('invoice-ship_address')) {
2227 $new->invoice_ship_address($old->invoice_ship_address);
2234 Returns a list of fields which have ship_ duplicates.
2239 qw( last first company
2241 address1 address2 city county state zip country
2243 daytime night fax mobile
2247 =item has_ship_address
2249 Returns true if this customer record has a separate shipping address.
2253 sub has_ship_address {
2255 $self->bill_locationnum != $self->ship_locationnum;
2260 Returns a list of key/value pairs, with the following keys: address1,
2261 adddress2, city, county, state, zip, country, district, and geocode. The
2262 shipping address is used if present.
2268 $self->ship_location->location_hash;
2273 Returns all locations (see L<FS::cust_location>) for this customer.
2279 qsearch('cust_location', { 'custnum' => $self->custnum,
2280 'prospectnum' => '' } );
2285 Returns all contacts (see L<FS::contact>) for this customer.
2289 #already used :/ sub contact {
2292 qsearch('contact', { 'custnum' => $self->custnum } );
2297 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
2298 and L<FS::cust_pkg>) for this customer, except those on hold.
2300 Returns a list: an empty list on success or a list of errors.
2306 grep { ($_->get('setup')) && $_->unsuspend } $self->suspended_pkgs(@_);
2311 Unsuspends all suspended packages in the on-hold state (those without setup
2312 dates) for this customer.
2318 grep { (!$_->setup) && $_->unsuspend } $self->suspended_pkgs;
2323 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
2325 Returns a list: an empty list on success or a list of errors.
2331 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
2334 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2336 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
2337 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
2338 of a list of pkgparts; the hashref has the following keys:
2342 =item pkgparts - listref of pkgparts
2344 =item (other options are passed to the suspend method)
2349 Returns a list: an empty list on success or a list of errors.
2353 sub suspend_if_pkgpart {
2355 my (@pkgparts, %opt);
2356 if (ref($_[0]) eq 'HASH'){
2357 @pkgparts = @{$_[0]{pkgparts}};
2362 grep { $_->suspend(%opt) }
2363 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
2364 $self->unsuspended_pkgs;
2367 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2369 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
2370 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
2371 instead of a list of pkgparts; the hashref has the following keys:
2375 =item pkgparts - listref of pkgparts
2377 =item (other options are passed to the suspend method)
2381 Returns a list: an empty list on success or a list of errors.
2385 sub suspend_unless_pkgpart {
2387 my (@pkgparts, %opt);
2388 if (ref($_[0]) eq 'HASH'){
2389 @pkgparts = @{$_[0]{pkgparts}};
2394 grep { $_->suspend(%opt) }
2395 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
2396 $self->unsuspended_pkgs;
2399 =item cancel [ OPTION => VALUE ... ]
2401 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
2402 The cancellation time will be now.
2406 Always returns a list: an empty list on success or a list of errors.
2413 warn "$me cancel called on customer ". $self->custnum. " with options ".
2414 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
2416 my @pkgs = $self->ncancelled_pkgs;
2418 $self->cancel_pkgs( %opt, 'cust_pkg' => \@pkgs );
2421 =item cancel_pkgs OPTIONS
2423 Cancels a specified list of packages. OPTIONS can include:
2427 =item cust_pkg - an arrayref of the packages. Required.
2429 =item time - the cancellation time, used to calculate final bills and
2430 unused-time credits if any. Will be passed through to the bill() and
2431 FS::cust_pkg::cancel() methods.
2433 =item quiet - can be set true to supress email cancellation notices.
2435 =item reason - can be set to a cancellation reason (see L<FS:reason>), either a
2436 reasonnum of an existing reason, or passing a hashref will create a new reason.
2437 The hashref should have the following keys:
2438 typenum - Reason type (see L<FS::reason_type>)
2439 reason - Text of the new reason.
2441 =item cust_pkg_reason - can be an arrayref of L<FS::cust_pkg_reason> objects
2442 for the individual packages, parallel to the C<cust_pkg> argument. The
2443 reason and reason_otaker arguments will be taken from those objects.
2445 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
2447 =item nobill - can be set true to skip billing if it might otherwise be done.
2452 my( $self, %opt ) = @_;
2454 # we're going to cancel services, which is not reversible
2455 # but on 3.x, don't strictly enforce this
2456 warn "cancel_pkgs should not be run inside a transaction"
2457 if $FS::UID::AutoCommit == 0;
2459 local $FS::UID::AutoCommit = 0;
2461 return ( 'access denied' )
2462 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
2464 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
2466 #should try decryption (we might have the private key)
2467 # and if not maybe queue a job for the server that does?
2468 return ( "Can't (yet) ban encrypted credit cards" )
2469 if $self->is_encrypted($self->payinfo);
2471 my $ban = new FS::banned_pay $self->_new_banned_pay_hashref;
2472 my $error = $ban->insert;
2480 my @pkgs = @{ delete $opt{'cust_pkg'} };
2481 my $cancel_time = $opt{'time'} || time;
2483 # bill all packages first, so we don't lose usage, service counts for
2484 # bulk billing, etc.
2485 if ( !$opt{nobill} && $conf->exists('bill_usage_on_cancel') ) {
2487 my $error = $self->bill( 'pkg_list' => [ @pkgs ],
2489 'time' => $cancel_time );
2491 warn "Error billing during cancel, custnum ". $self->custnum. ": $error";
2493 return ( "Error billing during cancellation: $error" );
2498 $FS::UID::AutoCommit = 1;
2500 # now cancel all services, the same way we would for individual packages.
2501 # if any of them fail, cancel the rest anyway.
2502 my @cust_svc = map { $_->cust_svc } @pkgs;
2503 my @sorted_cust_svc =
2505 sort { $a->[1] <=> $b->[1] }
2506 map { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; } @cust_svc
2508 warn "$me removing ".scalar(@sorted_cust_svc)." service(s) for customer ".
2511 foreach my $cust_svc (@sorted_cust_svc) {
2512 my $part_svc = $cust_svc->part_svc;
2513 next if ( defined($part_svc) and $part_svc->preserve );
2514 my $error = $cust_svc->cancel; # immediate cancel, no date option
2515 push @errors, $error if $error;
2521 warn "$me cancelling ". scalar(@pkgs) ." package(s) for customer ".
2522 $self->custnum. "\n"
2526 if ($opt{'cust_pkg_reason'}) {
2527 @cprs = @{ delete $opt{'cust_pkg_reason'} };
2533 my $cpr = shift @cprs;
2535 $lopt{'reason'} = $cpr->reasonnum;
2536 $lopt{'reason_otaker'} = $cpr->otaker;
2538 warn "no reason found when canceling package ".$_->pkgnum."\n";
2539 $lopt{'reason'} = '';
2542 my $error = $_->cancel(%lopt);
2543 push @errors, 'pkgnum '.$_->pkgnum.': '.$error if $error;
2549 sub _banned_pay_hashref {
2560 'payby' => $payby2ban{$self->payby},
2561 'payinfo' => $self->payinfo,
2562 #don't ever *search* on reason! #'reason' =>
2566 sub _new_banned_pay_hashref {
2568 my $hr = $self->_banned_pay_hashref;
2569 $hr->{payinfo} = md5_base64($hr->{payinfo});
2575 Returns all notes (see L<FS::cust_main_note>) for this customer.
2580 my($self,$orderby_classnum) = (shift,shift);
2581 my $orderby = "sticky DESC, _date DESC";
2582 $orderby = "classnum ASC, $orderby" if $orderby_classnum;
2583 qsearch( 'cust_main_note',
2584 { 'custnum' => $self->custnum },
2586 "ORDER BY $orderby",
2592 Returns the agent (see L<FS::agent>) for this customer.
2598 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
2603 Returns the agent name (see L<FS::agent>) for this customer.
2609 $self->agent->agent;
2614 Returns any tags associated with this customer, as FS::cust_tag objects,
2615 or an empty list if there are no tags.
2621 qsearch('cust_tag', { 'custnum' => $self->custnum } );
2626 Returns any tags associated with this customer, as FS::part_tag objects,
2627 or an empty list if there are no tags.
2633 map $_->part_tag, $self->cust_tag;
2639 Returns the customer class, as an FS::cust_class object, or the empty string
2640 if there is no customer class.
2646 if ( $self->classnum ) {
2647 qsearchs('cust_class', { 'classnum' => $self->classnum } );
2655 Returns the customer category name, or the empty string if there is no customer
2662 my $cust_class = $self->cust_class;
2664 ? $cust_class->categoryname
2670 Returns the customer class name, or the empty string if there is no customer
2677 my $cust_class = $self->cust_class;
2679 ? $cust_class->classname
2683 =item BILLING METHODS
2685 Documentation on billing methods has been moved to
2686 L<FS::cust_main::Billing>.
2688 =item REALTIME BILLING METHODS
2690 Documentation on realtime billing methods has been moved to
2691 L<FS::cust_main::Billing_Realtime>.
2695 Removes the I<paycvv> field from the database directly.
2697 If there is an error, returns the error, otherwise returns false.
2703 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2704 or return dbh->errstr;
2705 $sth->execute($self->custnum)
2706 or return $sth->errstr;
2711 =item batch_card OPTION => VALUE...
2713 Adds a payment for this invoice to the pending credit card batch (see
2714 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2715 runs the payment using a realtime gateway.
2717 Options may include:
2719 B<amount>: the amount to be paid; defaults to the customer's balance minus
2720 any payments in transit.
2722 B<payby>: the payment method; defaults to cust_main.payby
2724 B<realtime>: runs this as a realtime payment instead of adding it to a
2727 B<invnum>: sets cust_pay_batch.invnum.
2729 B<address1>, B<address2>, B<city>, B<state>, B<zip>, B<country>: sets
2730 the billing address for the payment; defaults to the customer's billing
2733 B<payinfo>, B<paydate>, B<payname>: sets the payment account, expiration
2734 date, and name; defaults to those fields in cust_main.
2739 my ($self, %options) = @_;
2742 if (exists($options{amount})) {
2743 $amount = $options{amount};
2745 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
2748 warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
2750 $self->in_transit_payments
2755 my $invnum = delete $options{invnum};
2756 my $payby = $options{payby} || $self->payby; #still dubious
2758 if ($options{'realtime'}) {
2759 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
2765 my $oldAutoCommit = $FS::UID::AutoCommit;
2766 local $FS::UID::AutoCommit = 0;
2769 #this needs to handle mysql as well as Pg, like svc_acct.pm
2770 #(make it into a common function if folks need to do batching with mysql)
2771 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
2772 or return "Cannot lock pay_batch: " . $dbh->errstr;
2776 'payby' => FS::payby->payby2payment($payby),
2778 $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
2780 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
2782 unless ( $pay_batch ) {
2783 $pay_batch = new FS::pay_batch \%pay_batch;
2784 my $error = $pay_batch->insert;
2786 $dbh->rollback if $oldAutoCommit;
2787 die "error creating new batch: $error\n";
2791 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
2792 'batchnum' => $pay_batch->batchnum,
2793 'custnum' => $self->custnum,
2796 foreach (qw( address1 address2 city state zip country latitude longitude
2797 payby payinfo paydate payname paycode paytype ))
2799 $options{$_} = '' unless exists($options{$_});
2802 my $loc = $self->bill_location;
2804 my $cust_pay_batch = new FS::cust_pay_batch ( {
2805 'batchnum' => $pay_batch->batchnum,
2806 'invnum' => $invnum || 0, # is there a better value?
2807 # this field should be
2809 # cust_bill_pay_batch now
2810 'custnum' => $self->custnum,
2811 'last' => $self->getfield('last'),
2812 'first' => $self->getfield('first'),
2813 'address1' => $options{address1} || $loc->address1,
2814 'address2' => $options{address2} || $loc->address2,
2815 'city' => $options{city} || $loc->city,
2816 'state' => $options{state} || $loc->state,
2817 'zip' => $options{zip} || $loc->zip,
2818 'country' => $options{country} || $loc->country,
2819 'payby' => $options{payby} || $self->payby,
2820 'payinfo' => $options{payinfo} || $self->payinfo,
2821 'paymask' => ( $options{payinfo}
2822 ? FS::payinfo_Mixin->mask_payinfo( $options{payby},
2826 'exp' => $options{paydate} || $self->paydate,
2827 'payname' => $options{payname} || $self->payname,
2828 'amount' => $amount, # consolidating
2829 'paycode' => $options{paycode} || '',
2832 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
2833 if $old_cust_pay_batch;
2836 if ($old_cust_pay_batch) {
2837 $error = $cust_pay_batch->replace($old_cust_pay_batch)
2839 $error = $cust_pay_batch->insert;
2843 $dbh->rollback if $oldAutoCommit;
2847 if ($options{'processing-fee'} > 0) {
2849 my $processing_fee_text = 'Payment Processing Fee';
2850 my $pf_change_error = $self->charge({
2851 'amount' => $options{'processing-fee'},
2852 'pkg' => $processing_fee_text,
2854 'cust_pkg_ref' => \$pf_cust_pkg,
2857 if($pf_change_error) {
2858 warn 'Unable to add payment processing fee';
2862 $pf_cust_pkg->setup(time);
2863 my $pf_error = $pf_cust_pkg->replace;
2865 warn 'Unable to set setup time on cust_pkg for processing fee';
2869 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
2870 unless ( $cust_bill ) {
2871 warn "race condition + invoice deletion just happened";
2875 my $grand_pf_error =
2876 $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'});
2878 warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error"
2882 my $unapplied = $self->total_unapplied_credits
2883 + $self->total_unapplied_payments
2884 + $self->in_transit_payments;
2885 foreach my $cust_bill ($self->open_cust_bill) {
2886 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
2887 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
2888 'invnum' => $cust_bill->invnum,
2889 'paybatchnum' => $cust_pay_batch->paybatchnum,
2890 'amount' => $cust_bill->owed,
2893 if ($unapplied >= $cust_bill_pay_batch->amount){
2894 $unapplied -= $cust_bill_pay_batch->amount;
2897 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
2898 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
2900 $error = $cust_bill_pay_batch->insert;
2902 $dbh->rollback if $oldAutoCommit;
2907 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2913 Returns the total owed for this customer on all invoices
2914 (see L<FS::cust_bill/owed>).
2920 $self->total_owed_date(2145859200); #12/31/2037
2923 =item total_owed_date TIME
2925 Returns the total owed for this customer on all invoices with date earlier than
2926 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2927 see L<Time::Local> and L<Date::Parse> for conversion functions.
2931 sub total_owed_date {
2935 my $custnum = $self->custnum;
2937 my $owed_sql = FS::cust_bill->owed_sql;
2940 SELECT SUM($owed_sql) FROM cust_bill
2941 WHERE custnum = $custnum
2945 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2949 =item total_owed_pkgnum PKGNUM
2951 Returns the total owed on all invoices for this customer's specific package
2952 when using experimental package balances (see L<FS::cust_bill/owed_pkgnum>).
2956 sub total_owed_pkgnum {
2957 my( $self, $pkgnum ) = @_;
2958 $self->total_owed_date_pkgnum(2145859200, $pkgnum); #12/31/2037
2961 =item total_owed_date_pkgnum TIME PKGNUM
2963 Returns the total owed for this customer's specific package when using
2964 experimental package balances on all invoices with date earlier than
2965 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2966 see L<Time::Local> and L<Date::Parse> for conversion functions.
2970 sub total_owed_date_pkgnum {
2971 my( $self, $time, $pkgnum ) = @_;
2974 foreach my $cust_bill (
2975 grep { $_->_date <= $time }
2976 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2978 $total_bill += $cust_bill->owed_pkgnum($pkgnum);
2980 sprintf( "%.2f", $total_bill );
2986 Returns the total amount of all payments.
2993 $total += $_->paid foreach $self->cust_pay;
2994 sprintf( "%.2f", $total );
2997 =item total_unapplied_credits
2999 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3000 customer. See L<FS::cust_credit/credited>.
3002 =item total_credited
3004 Old name for total_unapplied_credits. Don't use.
3008 sub total_credited {
3009 #carp "total_credited deprecated, use total_unapplied_credits";
3010 shift->total_unapplied_credits(@_);
3013 sub total_unapplied_credits {
3016 my $custnum = $self->custnum;
3018 my $unapplied_sql = FS::cust_credit->unapplied_sql;
3021 SELECT SUM($unapplied_sql) FROM cust_credit
3022 WHERE custnum = $custnum
3025 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3029 =item total_unapplied_credits_pkgnum PKGNUM
3031 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3032 customer. See L<FS::cust_credit/credited>.
3036 sub total_unapplied_credits_pkgnum {
3037 my( $self, $pkgnum ) = @_;
3038 my $total_credit = 0;
3039 $total_credit += $_->credited foreach $self->cust_credit_pkgnum($pkgnum);
3040 sprintf( "%.2f", $total_credit );
3044 =item total_unapplied_payments
3046 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3047 See L<FS::cust_pay/unapplied>.
3051 sub total_unapplied_payments {
3054 my $custnum = $self->custnum;
3056 my $unapplied_sql = FS::cust_pay->unapplied_sql;
3059 SELECT SUM($unapplied_sql) FROM cust_pay
3060 WHERE custnum = $custnum
3063 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3067 =item total_unapplied_payments_pkgnum PKGNUM
3069 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer's
3070 specific package when using experimental package balances. See
3071 L<FS::cust_pay/unapplied>.
3075 sub total_unapplied_payments_pkgnum {
3076 my( $self, $pkgnum ) = @_;
3077 my $total_unapplied = 0;
3078 $total_unapplied += $_->unapplied foreach $self->cust_pay_pkgnum($pkgnum);
3079 sprintf( "%.2f", $total_unapplied );
3083 =item total_unapplied_refunds
3085 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
3086 customer. See L<FS::cust_refund/unapplied>.
3090 sub total_unapplied_refunds {
3092 my $custnum = $self->custnum;
3094 my $unapplied_sql = FS::cust_refund->unapplied_sql;
3097 SELECT SUM($unapplied_sql) FROM cust_refund
3098 WHERE custnum = $custnum
3101 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3107 Returns the balance for this customer (total_owed plus total_unrefunded, minus
3108 total_unapplied_credits minus total_unapplied_payments).
3114 $self->balance_date_range;
3117 =item balance_date TIME
3119 Returns the balance for this customer, only considering invoices with date
3120 earlier than TIME (total_owed_date minus total_credited minus
3121 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
3122 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
3129 $self->balance_date_range(shift);
3132 =item balance_date_range [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
3134 Returns the balance for this customer, optionally considering invoices with
3135 date earlier than START_TIME, and not later than END_TIME
3136 (total_owed_date minus total_unapplied_credits minus total_unapplied_payments).
3138 Times are specified as SQL fragments or numeric
3139 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
3140 L<Date::Parse> for conversion functions. The empty string can be passed
3141 to disable that time constraint completely.
3143 Accepts the same options as L<balance_date_sql>:
3147 =item unapplied_date
3149 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)
3153 An absolute cutoff time. Payments, credits, and refunds I<applied> after this
3154 time will be ignored. Note that START_TIME and END_TIME only limit the date
3155 range for invoices and I<unapplied> payments, credits, and refunds.
3161 sub balance_date_range {
3163 my $sql = 'SELECT SUM('. $self->balance_date_sql(@_).
3164 ') FROM cust_main WHERE custnum='. $self->custnum;
3165 sprintf( '%.2f', $self->scalar_sql($sql) || 0 );
3168 =item balance_pkgnum PKGNUM
3170 Returns the balance for this customer's specific package when using
3171 experimental package balances (total_owed plus total_unrefunded, minus
3172 total_unapplied_credits minus total_unapplied_payments)
3176 sub balance_pkgnum {
3177 my( $self, $pkgnum ) = @_;
3180 $self->total_owed_pkgnum($pkgnum)
3181 # n/a - refunds aren't part of pkg-balances since they don't apply to invoices
3182 # + $self->total_unapplied_refunds_pkgnum($pkgnum)
3183 - $self->total_unapplied_credits_pkgnum($pkgnum)
3184 - $self->total_unapplied_payments_pkgnum($pkgnum)
3188 =item in_transit_payments
3190 Returns the total of requests for payments for this customer pending in
3191 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
3195 sub in_transit_payments {
3197 my $in_transit_payments = 0;
3198 foreach my $pay_batch ( qsearch('pay_batch', {
3201 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
3202 'batchnum' => $pay_batch->batchnum,
3203 'custnum' => $self->custnum,
3206 $in_transit_payments += $cust_pay_batch->amount;
3209 sprintf( "%.2f", $in_transit_payments );
3214 Returns a hash of useful information for making a payment.
3224 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
3225 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
3226 'LECB' (Phone bill billing), 'BILL' (billing), or 'COMP' (free).
3230 For credit card transactions:
3242 For electronic check transactions:
3257 $return{balance} = $self->balance;
3259 $return{payname} = $self->payname
3260 || ( $self->first. ' '. $self->get('last') );
3262 $return{$_} = $self->bill_location->$_
3263 for qw(address1 address2 city state zip);
3265 $return{payby} = $self->payby;
3266 $return{stateid_state} = $self->stateid_state;
3268 if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
3269 $return{card_type} = cardtype($self->payinfo);
3270 $return{payinfo} = $self->paymask;
3272 @return{'month', 'year'} = $self->paydate_monthyear;
3276 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
3277 my ($payinfo1, $payinfo2) = split '@', $self->paymask;
3278 $return{payinfo1} = $payinfo1;
3279 $return{payinfo2} = $payinfo2;
3280 $return{paytype} = $self->paytype;
3281 $return{paystate} = $self->paystate;
3285 #doubleclick protection
3287 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
3293 =item paydate_monthyear
3295 Returns a two-element list consisting of the month and year of this customer's
3296 paydate (credit card expiration date for CARD customers)
3300 sub paydate_monthyear {
3302 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
3304 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
3313 Returns the exact time in seconds corresponding to the payment method
3314 expiration date. For CARD/DCRD customers this is the end of the month;
3315 for others (COMP is the only other payby that uses paydate) it's the start.
3316 Returns 0 if the paydate is empty or set to the far future.
3322 my ($month, $year) = $self->paydate_monthyear;
3323 return 0 if !$year or $year >= 2037;
3324 if ( $self->payby eq 'CARD' or $self->payby eq 'DCRD' ) {
3326 if ( $month == 13 ) {
3330 return timelocal(0,0,0,1,$month-1,$year) - 1;
3333 return timelocal(0,0,0,1,$month-1,$year);
3337 =item paydate_epoch_sql
3339 Class method. Returns an SQL expression to obtain the payment expiration date
3340 as a number of seconds.
3344 # Special expiration date behavior for non-CARD/DCRD customers has been
3345 # carefully preserved. Do we really use that?
3346 sub paydate_epoch_sql {
3348 my $table = shift || 'cust_main';
3349 my ($case1, $case2);
3350 if ( driver_name eq 'Pg' ) {
3351 $case1 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) + INTERVAL '1 month') - 1";
3352 $case2 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) )";
3354 elsif ( lc(driver_name) eq 'mysql' ) {
3355 $case1 = "UNIX_TIMESTAMP( DATE_ADD( CAST( $table.paydate AS DATETIME ), INTERVAL 1 month ) ) - 1";
3356 $case2 = "UNIX_TIMESTAMP( CAST( $table.paydate AS DATETIME ) )";
3359 return "CASE WHEN $table.payby IN('CARD','DCRD')
3365 =item tax_exemption TAXNAME
3370 my( $self, $taxname ) = @_;
3372 qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
3373 'taxname' => $taxname,
3378 =item cust_main_exemption
3382 sub cust_main_exemption {
3384 qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
3387 =item invoicing_list [ ARRAYREF ]
3389 If an arguement is given, sets these email addresses as invoice recipients
3390 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
3391 (except as warnings), so use check_invoicing_list first.
3393 Returns a list of email addresses (with svcnum entries expanded).
3395 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
3396 check it without disturbing anything by passing nothing.
3398 This interface may change in the future.
3402 sub invoicing_list {
3403 my( $self, $arrayref ) = @_;
3406 my @cust_main_invoice;
3407 if ( $self->custnum ) {
3408 @cust_main_invoice =
3409 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3411 @cust_main_invoice = ();
3413 foreach my $cust_main_invoice ( @cust_main_invoice ) {
3414 #warn $cust_main_invoice->destnum;
3415 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3416 #warn $cust_main_invoice->destnum;
3417 my $error = $cust_main_invoice->delete;
3418 warn $error if $error;
3421 if ( $self->custnum ) {
3422 @cust_main_invoice =
3423 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3425 @cust_main_invoice = ();
3427 my %seen = map { $_->address => 1 } @cust_main_invoice;
3428 foreach my $address ( @{$arrayref} ) {
3429 next if exists $seen{$address} && $seen{$address};
3430 $seen{$address} = 1;
3431 my $cust_main_invoice = new FS::cust_main_invoice ( {
3432 'custnum' => $self->custnum,
3435 my $error = $cust_main_invoice->insert;
3436 warn $error if $error;
3440 if ( $self->custnum ) {
3442 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3449 =item check_invoicing_list ARRAYREF
3451 Checks these arguements as valid input for the invoicing_list method. If there
3452 is an error, returns the error, otherwise returns false.
3456 sub check_invoicing_list {
3457 my( $self, $arrayref ) = @_;
3459 foreach my $address ( @$arrayref ) {
3461 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3462 return 'Can\'t add FAX invoice destination with a blank FAX number.';
3465 my $cust_main_invoice = new FS::cust_main_invoice ( {
3466 'custnum' => $self->custnum,
3469 my $error = $self->custnum
3470 ? $cust_main_invoice->check
3471 : $cust_main_invoice->checkdest
3473 return $error if $error;
3477 return "Email address required"
3478 if $conf->exists('cust_main-require_invoicing_list_email', $self->agentnum)
3479 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
3484 =item set_default_invoicing_list
3486 Sets the invoicing list to all accounts associated with this customer,
3487 overwriting any previous invoicing list.
3491 sub set_default_invoicing_list {
3493 $self->invoicing_list($self->all_emails);
3498 Returns the email addresses of all accounts provisioned for this customer.
3505 foreach my $cust_pkg ( $self->all_pkgs ) {
3506 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
3508 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3509 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3511 $list{$_}=1 foreach map { $_->email } @svc_acct;
3516 =item invoicing_list_addpost
3518 Adds postal invoicing to this customer. If this customer is already configured
3519 to receive postal invoices, does nothing.
3523 sub invoicing_list_addpost {
3525 return if grep { $_ eq 'POST' } $self->invoicing_list;
3526 my @invoicing_list = $self->invoicing_list;
3527 push @invoicing_list, 'POST';
3528 $self->invoicing_list(\@invoicing_list);
3531 =item invoicing_list_emailonly
3533 Returns the list of email invoice recipients (invoicing_list without non-email
3534 destinations such as POST and FAX).
3538 sub invoicing_list_emailonly {
3540 warn "$me invoicing_list_emailonly called"
3542 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
3545 =item invoicing_list_emailonly_scalar
3547 Returns the list of email invoice recipients (invoicing_list without non-email
3548 destinations such as POST and FAX) as a comma-separated scalar.
3552 sub invoicing_list_emailonly_scalar {
3554 warn "$me invoicing_list_emailonly_scalar called"
3556 join(', ', $self->invoicing_list_emailonly);
3559 =item contact_list [ CLASSNUM, ... ]
3561 Returns a list of contacts (L<FS::contact> objects) for the customer. If
3562 a list of contact classnums is given, returns only contacts in those
3563 classes. If '0' is given, also returns contacts with no class.
3565 If no arguments are given, returns all contacts for the customer.
3573 select => 'contact.*',
3574 extra_sql => ' WHERE contact.custnum = '.$self->custnum,
3581 push @orwhere, 'contact.classnum is null';
3582 } elsif ( /^\d+$/ ) {
3583 push @classnums, $_;
3585 die "bad classnum argument '$_'";
3590 push @orwhere, 'contact.classnum IN ('.join(',', @classnums).')';
3593 $search->{extra_sql} .= ' AND (' .
3594 join(' OR ', map "( $_ )", @orwhere) .
3601 =item contact_list_email [ CLASSNUM, ... ]
3603 Same as L</contact_list>, but returns email destinations instead of contact
3604 objects. Also accepts 'invoice' as an argument, in which case this will also
3605 return the invoice email address if any.
3609 sub contact_list_email {
3617 push @classnums, $_;
3621 # if the only argument passed was 'invoice' then no classnums are
3622 # intended, so skip this.
3624 my @contacts = $self->contact_list(@classnums);
3625 foreach my $contact (@contacts) {
3626 foreach my $contact_email ($contact->contact_email) {
3627 # unlike on 4.x, we have a separate list of invoice email
3629 # make sure they're not redundant with contact emails
3630 $emails{ $contact_email->emailaddress } =
3631 Email::Address->new( $contact->firstlast,
3632 $contact_email->emailaddress
3637 if ( $and_invoice ) {
3638 foreach my $email ($self->invoicing_list_emailonly) {
3639 $emails{ $email } ||=
3640 Email::Address->new( $self->name_short, $email )->format;
3646 =item referral_custnum_cust_main
3648 Returns the customer who referred this customer (or the empty string, if
3649 this customer was not referred).
3651 Note the difference with referral_cust_main method: This method,
3652 referral_custnum_cust_main returns the single customer (if any) who referred
3653 this customer, while referral_cust_main returns an array of customers referred
3658 sub referral_custnum_cust_main {
3660 return '' unless $self->referral_custnum;
3661 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3664 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
3666 Returns an array of customers referred by this customer (referral_custnum set
3667 to this custnum). If DEPTH is given, recurses up to the given depth, returning
3668 customers referred by customers referred by this customer and so on, inclusive.
3669 The default behavior is DEPTH 1 (no recursion).
3671 Note the difference with referral_custnum_cust_main method: This method,
3672 referral_cust_main, returns an array of customers referred BY this customer,
3673 while referral_custnum_cust_main returns the single customer (if any) who
3674 referred this customer.
3678 sub referral_cust_main {
3680 my $depth = @_ ? shift : 1;
3681 my $exclude = @_ ? shift : {};
3684 map { $exclude->{$_->custnum}++; $_; }
3685 grep { ! $exclude->{ $_->custnum } }
3686 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
3690 map { $_->referral_cust_main($depth-1, $exclude) }
3697 =item referral_cust_main_ncancelled
3699 Same as referral_cust_main, except only returns customers with uncancelled
3704 sub referral_cust_main_ncancelled {
3706 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
3709 =item referral_cust_pkg [ DEPTH ]
3711 Like referral_cust_main, except returns a flat list of all unsuspended (and
3712 uncancelled) packages for each customer. The number of items in this list may
3713 be useful for commission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
3717 sub referral_cust_pkg {
3719 my $depth = @_ ? shift : 1;
3721 map { $_->unsuspended_pkgs }
3722 grep { $_->unsuspended_pkgs }
3723 $self->referral_cust_main($depth);
3726 =item referring_cust_main
3728 Returns the single cust_main record for the customer who referred this customer
3729 (referral_custnum), or false.
3733 sub referring_cust_main {
3735 return '' unless $self->referral_custnum;
3736 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3739 =item credit AMOUNT, REASON [ , OPTION => VALUE ... ]
3741 Applies a credit to this customer. If there is an error, returns the error,
3742 otherwise returns false.
3744 REASON can be a text string, an FS::reason object, or a scalar reference to
3745 a reasonnum. If a text string, it will be automatically inserted as a new
3746 reason, and a 'reason_type' option must be passed to indicate the
3747 FS::reason_type for the new reason.
3749 An I<addlinfo> option may be passed to set the credit's I<addlinfo> field.
3750 Likewise for I<eventnum>, I<commission_agentnum>, I<commission_salesnum> and
3751 I<commission_pkgnum>.
3753 Any other options are passed to FS::cust_credit::insert.
3758 my( $self, $amount, $reason, %options ) = @_;
3760 my $cust_credit = new FS::cust_credit {
3761 'custnum' => $self->custnum,
3762 'amount' => $amount,
3765 if ( ref($reason) ) {
3767 if ( ref($reason) eq 'SCALAR' ) {
3768 $cust_credit->reasonnum( $$reason );
3770 $cust_credit->reasonnum( $reason->reasonnum );
3774 $cust_credit->set('reason', $reason)
3777 $cust_credit->$_( delete $options{$_} )
3778 foreach grep exists($options{$_}),
3779 qw( addlinfo eventnum ),
3780 map "commission_$_", qw( agentnum salesnum pkgnum );
3782 $cust_credit->insert(%options);
3786 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3788 Creates a one-time charge for this customer. If there is an error, returns
3789 the error, otherwise returns false.
3791 New-style, with a hashref of options:
3793 my $error = $cust_main->charge(
3797 'start_date' => str2time('7/4/2009'),
3798 'pkg' => 'Description',
3799 'comment' => 'Comment',
3800 'additional' => [], #extra invoice detail
3801 'classnum' => 1, #pkg_class
3803 'setuptax' => '', # or 'Y' for tax exempt
3805 'locationnum'=> 1234, # optional
3808 'taxclass' => 'Tax class',
3811 'taxproduct' => 2, #part_pkg_taxproduct
3812 'override' => {}, #XXX describe
3814 #will be filled in with the new object
3815 'cust_pkg_ref' => \$cust_pkg,
3817 #generate an invoice immediately
3819 'invoice_terms' => '', #with these terms
3825 my $error = $cust_main->charge( 54.32, 'Description', 'Comment', 'Tax class' );
3829 #super false laziness w/quotation::charge
3832 my ( $amount, $setup_cost, $quantity, $start_date, $classnum );
3833 my ( $pkg, $comment, $additional );
3834 my ( $setuptax, $taxclass ); #internal taxes
3835 my ( $taxproduct, $override ); #vendor (CCH) taxes
3837 my $separate_bill = '';
3838 my $cust_pkg_ref = '';
3839 my ( $bill_now, $invoice_terms ) = ( 0, '' );
3841 if ( ref( $_[0] ) ) {
3842 $amount = $_[0]->{amount};
3843 $setup_cost = $_[0]->{setup_cost};
3844 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
3845 $start_date = exists($_[0]->{start_date}) ? $_[0]->{start_date} : '';
3846 $no_auto = exists($_[0]->{no_auto}) ? $_[0]->{no_auto} : '';
3847 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
3848 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
3849 : '$'. sprintf("%.2f",$amount);
3850 $setuptax = exists($_[0]->{setuptax}) ? $_[0]->{setuptax} : '';
3851 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
3852 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
3853 $additional = $_[0]->{additional} || [];
3854 $taxproduct = $_[0]->{taxproductnum};
3855 $override = { '' => $_[0]->{tax_override} };
3856 $cust_pkg_ref = exists($_[0]->{cust_pkg_ref}) ? $_[0]->{cust_pkg_ref} : '';
3857 $bill_now = exists($_[0]->{bill_now}) ? $_[0]->{bill_now} : '';
3858 $invoice_terms = exists($_[0]->{invoice_terms}) ? $_[0]->{invoice_terms} : '';
3859 $locationnum = $_[0]->{locationnum} || $self->ship_locationnum;
3860 $separate_bill = $_[0]->{separate_bill} || '';
3866 $pkg = @_ ? shift : 'One-time charge';
3867 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
3869 $taxclass = @_ ? shift : '';
3873 local $SIG{HUP} = 'IGNORE';
3874 local $SIG{INT} = 'IGNORE';
3875 local $SIG{QUIT} = 'IGNORE';
3876 local $SIG{TERM} = 'IGNORE';
3877 local $SIG{TSTP} = 'IGNORE';
3878 local $SIG{PIPE} = 'IGNORE';
3880 my $oldAutoCommit = $FS::UID::AutoCommit;
3881 local $FS::UID::AutoCommit = 0;
3884 my $part_pkg = new FS::part_pkg ( {
3886 'comment' => $comment,
3890 'classnum' => ( $classnum ? $classnum : '' ),
3891 'setuptax' => $setuptax,
3892 'taxclass' => $taxclass,
3893 'taxproductnum' => $taxproduct,
3894 'setup_cost' => $setup_cost,
3897 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
3898 ( 0 .. @$additional - 1 )
3900 'additional_count' => scalar(@$additional),
3901 'setup_fee' => $amount,
3904 my $error = $part_pkg->insert( options => \%options,
3905 tax_overrides => $override,
3908 $dbh->rollback if $oldAutoCommit;
3912 my $pkgpart = $part_pkg->pkgpart;
3913 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3914 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3915 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3916 $error = $type_pkgs->insert;
3918 $dbh->rollback if $oldAutoCommit;
3923 my $cust_pkg = new FS::cust_pkg ( {
3924 'custnum' => $self->custnum,
3925 'pkgpart' => $pkgpart,
3926 'quantity' => $quantity,
3927 'start_date' => $start_date,
3928 'no_auto' => $no_auto,
3929 'separate_bill' => $separate_bill,
3930 'locationnum'=> $locationnum,
3933 $error = $cust_pkg->insert;
3935 $dbh->rollback if $oldAutoCommit;
3937 } elsif ( $cust_pkg_ref ) {
3938 ${$cust_pkg_ref} = $cust_pkg;
3942 my $error = $self->bill( 'invoice_terms' => $invoice_terms,
3943 'pkg_list' => [ $cust_pkg ],
3946 $dbh->rollback if $oldAutoCommit;
3951 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3956 #=item charge_postal_fee
3958 #Applies a one time charge this customer. If there is an error,
3959 #returns the error, returns the cust_pkg charge object or false
3960 #if there was no charge.
3964 # This should be a customer event. For that to work requires that bill
3965 # also be a customer event.
3967 sub charge_postal_fee {
3970 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart', $self->agentnum);
3971 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
3973 my $cust_pkg = new FS::cust_pkg ( {
3974 'custnum' => $self->custnum,
3975 'pkgpart' => $pkgpart,
3979 my $error = $cust_pkg->insert;
3980 $error ? $error : $cust_pkg;
3983 =item cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3985 Returns all the invoices (see L<FS::cust_bill>) for this customer.
3987 Optionally, a list or hashref of additional arguments to the qsearch call can
3994 my $opt = ref($_[0]) ? shift : { @_ };
3996 #return $self->num_cust_bill unless wantarray || keys %$opt;
3998 $opt->{'table'} = 'cust_bill';
3999 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4000 $opt->{'hashref'}{'custnum'} = $self->custnum;
4001 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
4003 map { $_ } #behavior of sort undefined in scalar context
4004 sort { $a->_date <=> $b->_date }
4008 =item open_cust_bill
4010 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4015 sub open_cust_bill {
4019 'extra_sql' => ' AND '. FS::cust_bill->owed_sql. ' > 0',
4025 =item legacy_cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4027 Returns all the legacy invoices (see L<FS::legacy_cust_bill>) for this customer.
4031 sub legacy_cust_bill {
4034 #return $self->num_legacy_cust_bill unless wantarray;
4036 map { $_ } #behavior of sort undefined in scalar context
4037 sort { $a->_date <=> $b->_date }
4038 qsearch({ 'table' => 'legacy_cust_bill',
4039 'hashref' => { 'custnum' => $self->custnum, },
4040 'order_by' => 'ORDER BY _date ASC',
4044 =item cust_statement [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4046 Returns all the statements (see L<FS::cust_statement>) for this customer.
4048 Optionally, a list or hashref of additional arguments to the qsearch call can
4053 =item cust_bill_void
4055 Returns all the voided invoices (see L<FS::cust_bill_void>) for this customer.
4059 sub cust_bill_void {
4062 map { $_ } #return $self->num_cust_bill_void unless wantarray;
4063 sort { $a->_date <=> $b->_date }
4064 qsearch( 'cust_bill_void', { 'custnum' => $self->custnum } )
4067 sub cust_statement {
4069 my $opt = ref($_[0]) ? shift : { @_ };
4071 #return $self->num_cust_statement unless wantarray || keys %$opt;
4073 $opt->{'table'} = 'cust_statement';
4074 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4075 $opt->{'hashref'}{'custnum'} = $self->custnum;
4076 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
4078 map { $_ } #behavior of sort undefined in scalar context
4079 sort { $a->_date <=> $b->_date }
4083 =item svc_x SVCDB [ OPTION => VALUE | EXTRA_QSEARCH_PARAMS_HASHREF ]
4085 Returns all services of type SVCDB (such as 'svc_acct') for this customer.
4087 Optionally, a list or hashref of additional arguments to the qsearch call can
4088 be passed following the SVCDB.
4095 if ( ! $svcdb =~ /^svc_\w+$/ ) {
4096 warn "$me svc_x requires a svcdb";
4099 my $opt = ref($_[0]) ? shift : { @_ };
4101 $opt->{'table'} = $svcdb;
4102 $opt->{'addl_from'} =
4103 'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum) '.
4104 ($opt->{'addl_from'} || '');
4106 my $custnum = $self->custnum;
4107 $custnum =~ /^\d+$/ or die "bad custnum '$custnum'";
4108 my $where = "cust_pkg.custnum = $custnum";
4110 my $extra_sql = $opt->{'extra_sql'} || '';
4111 if ( keys %{ $opt->{'hashref'} } ) {
4112 $extra_sql = " AND $where $extra_sql";
4115 if ( $opt->{'extra_sql'} =~ /^\s*where\s(.*)/si ) {
4116 $extra_sql = "WHERE $where AND $1";
4119 $extra_sql = "WHERE $where $extra_sql";
4122 $opt->{'extra_sql'} = $extra_sql;
4127 # required for use as an eventtable;
4130 $self->svc_x('svc_acct', @_);
4135 Returns all the credits (see L<FS::cust_credit>) for this customer.
4141 map { $_ } #return $self->num_cust_credit unless wantarray;
4142 sort { $a->_date <=> $b->_date }
4143 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4146 =item cust_credit_pkgnum
4148 Returns all the credits (see L<FS::cust_credit>) for this customer's specific
4149 package when using experimental package balances.
4153 sub cust_credit_pkgnum {
4154 my( $self, $pkgnum ) = @_;
4155 map { $_ } #return $self->num_cust_credit_pkgnum($pkgnum) unless wantarray;
4156 sort { $a->_date <=> $b->_date }
4157 qsearch( 'cust_credit', { 'custnum' => $self->custnum,
4158 'pkgnum' => $pkgnum,
4163 =item cust_credit_void
4165 Returns all voided credits (see L<FS::cust_credit_void>) for this customer.
4169 sub cust_credit_void {
4172 sort { $a->_date <=> $b->_date }
4173 qsearch( 'cust_credit_void', { 'custnum' => $self->custnum } )
4178 Returns all the payments (see L<FS::cust_pay>) for this customer.
4184 my $opt = ref($_[0]) ? shift : { @_ };
4186 return $self->num_cust_pay unless wantarray || keys %$opt;
4188 $opt->{'table'} = 'cust_pay';
4189 $opt->{'hashref'}{'custnum'} = $self->custnum;
4191 map { $_ } #behavior of sort undefined in scalar context
4192 sort { $a->_date <=> $b->_date }
4199 Returns the number of payments (see L<FS::cust_pay>) for this customer. Also
4200 called automatically when the cust_pay method is used in a scalar context.
4206 my $sql = "SELECT COUNT(*) FROM cust_pay WHERE custnum = ?";
4207 my $sth = dbh->prepare($sql) or die dbh->errstr;
4208 $sth->execute($self->custnum) or die $sth->errstr;
4209 $sth->fetchrow_arrayref->[0];
4212 =item unapplied_cust_pay
4214 Returns all the unapplied payments (see L<FS::cust_pay>) for this customer.
4218 sub unapplied_cust_pay {
4222 'extra_sql' => ' AND '. FS::cust_pay->unapplied_sql. ' > 0',
4228 =item cust_pay_pkgnum
4230 Returns all the payments (see L<FS::cust_pay>) for this customer's specific
4231 package when using experimental package balances.
4235 sub cust_pay_pkgnum {
4236 my( $self, $pkgnum ) = @_;
4237 map { $_ } #return $self->num_cust_pay_pkgnum($pkgnum) unless wantarray;
4238 sort { $a->_date <=> $b->_date }
4239 qsearch( 'cust_pay', { 'custnum' => $self->custnum,
4240 'pkgnum' => $pkgnum,
4247 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4253 map { $_ } #return $self->num_cust_pay_void unless wantarray;
4254 sort { $a->_date <=> $b->_date }
4255 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4258 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4260 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
4262 Optionally, a list or hashref of additional arguments to the qsearch call can
4267 sub cust_pay_batch {
4269 my $opt = ref($_[0]) ? shift : { @_ };
4271 #return $self->num_cust_statement unless wantarray || keys %$opt;
4273 $opt->{'table'} = 'cust_pay_batch';
4274 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4275 $opt->{'hashref'}{'custnum'} = $self->custnum;
4276 $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
4278 map { $_ } #behavior of sort undefined in scalar context
4279 sort { $a->paybatchnum <=> $b->paybatchnum }
4283 =item cust_pay_pending
4285 Returns all pending payments (see L<FS::cust_pay_pending>) for this customer
4286 (without status "done").
4290 sub cust_pay_pending {
4292 return $self->num_cust_pay_pending unless wantarray;
4293 sort { $a->_date <=> $b->_date }
4294 qsearch( 'cust_pay_pending', {
4295 'custnum' => $self->custnum,
4296 'status' => { op=>'!=', value=>'done' },
4301 =item cust_pay_pending_attempt
4303 Returns all payment attempts / declined payments for this customer, as pending
4304 payments objects (see L<FS::cust_pay_pending>), with status "done" but without
4305 a corresponding payment (see L<FS::cust_pay>).
4309 sub cust_pay_pending_attempt {
4311 return $self->num_cust_pay_pending_attempt unless wantarray;
4312 sort { $a->_date <=> $b->_date }
4313 qsearch( 'cust_pay_pending', {
4314 'custnum' => $self->custnum,
4321 =item num_cust_pay_pending
4323 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
4324 customer (without status "done"). Also called automatically when the
4325 cust_pay_pending method is used in a scalar context.
4329 sub num_cust_pay_pending {
4332 " SELECT COUNT(*) FROM cust_pay_pending ".
4333 " WHERE custnum = ? AND status != 'done' ",
4338 =item num_cust_pay_pending_attempt
4340 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
4341 customer, with status "done" but without a corresp. Also called automatically when the
4342 cust_pay_pending method is used in a scalar context.
4346 sub num_cust_pay_pending_attempt {
4349 " SELECT COUNT(*) FROM cust_pay_pending ".
4350 " WHERE custnum = ? AND status = 'done' AND paynum IS NULL",
4357 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4363 map { $_ } #return $self->num_cust_refund unless wantarray;
4364 sort { $a->_date <=> $b->_date }
4365 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4368 =item display_custnum
4370 Returns the displayed customer number for this customer: agent_custid if
4371 cust_main-default_agent_custid is set and it has a value, custnum otherwise.
4375 sub display_custnum {
4378 return $self->agent_custid
4379 if $default_agent_custid && $self->agent_custid;
4381 my $prefix = $conf->config('cust_main-custnum-display_prefix', $self->agentnum) || '';
4385 sprintf('%0'.($custnum_display_length||8).'d', $self->custnum)
4386 } elsif ( $custnum_display_length ) {
4387 return sprintf('%0'.$custnum_display_length.'d', $self->custnum);
4389 return $self->custnum;
4395 Returns a name string for this customer, either "Company (Last, First)" or
4402 my $name = $self->contact;
4403 $name = $self->company. " ($name)" if $self->company;
4407 =item service_contact
4409 Returns the L<FS::contact> object for this customer that has the 'Service'
4410 contact class, or undef if there is no such contact. Deprecated; don't use
4415 sub service_contact {
4417 if ( !exists($self->{service_contact}) ) {
4418 my $classnum = $self->scalar_sql(
4419 'SELECT classnum FROM contact_class WHERE classname = \'Service\''
4420 ) || 0; #if it's zero, qsearchs will return nothing
4421 $self->{service_contact} = qsearchs('contact', {
4422 'classnum' => $classnum, 'custnum' => $self->custnum
4425 $self->{service_contact};
4430 Returns a name string for this (service/shipping) contact, either
4431 "Company (Last, First)" or "Last, First".
4438 my $name = $self->ship_contact;
4439 $name = $self->company. " ($name)" if $self->company;
4445 Returns a name string for this customer, either "Company" or "First Last".
4451 $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
4454 =item ship_name_short
4456 Returns a name string for this (service/shipping) contact, either "Company"
4461 sub ship_name_short {
4463 $self->service_contact
4464 ? $self->ship_contact_firstlast
4470 Returns this customer's full (billing) contact name only, "Last, First"
4476 $self->get('last'). ', '. $self->first;
4481 Returns this customer's full (shipping) contact name only, "Last, First"
4487 my $contact = $self->service_contact || $self;
4488 $contact->get('last') . ', ' . $contact->get('first');
4491 =item contact_firstlast
4493 Returns this customers full (billing) contact name only, "First Last".
4497 sub contact_firstlast {
4499 $self->first. ' '. $self->get('last');
4502 =item ship_contact_firstlast
4504 Returns this customer's full (shipping) contact name only, "First Last".
4508 sub ship_contact_firstlast {
4510 my $contact = $self->service_contact || $self;
4511 $contact->get('first') . ' '. $contact->get('last');
4514 sub bill_country_full {
4516 $self->bill_location->country_full;
4519 sub ship_country_full {
4521 $self->ship_location->country_full;
4524 =item county_state_county [ PREFIX ]
4526 Returns a string consisting of just the county, state and country.
4530 sub county_state_country {
4533 if ( @_ && $_[0] && $self->has_ship_address ) {
4534 $locationnum = $self->ship_locationnum;
4536 $locationnum = $self->bill_locationnum;
4538 my $cust_location = qsearchs('cust_location', { locationnum=>$locationnum });
4539 $cust_location->county_state_country;
4542 =item geocode DATA_VENDOR
4544 Returns a value for the customer location as encoded by DATA_VENDOR.
4545 Currently this only makes sense for "CCH" as DATA_VENDOR.
4553 Returns a status string for this customer, currently:
4557 =item prospect - No packages have ever been ordered
4559 =item ordered - Recurring packages all are new (not yet billed).
4561 =item active - One or more recurring packages is active
4563 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
4565 =item suspended - All non-cancelled recurring packages are suspended
4567 =item cancelled - All recurring packages are cancelled
4571 Behavior of inactive vs. cancelled edge cases can be adjusted with the
4572 cust_main-status_module configuration option.
4576 sub status { shift->cust_status(@_); }
4580 return $self->hashref->{cust_status} if $self->hashref->{cust_status};
4581 for my $status ( FS::cust_main->statuses() ) {
4582 my $method = $status.'_sql';
4583 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
4584 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
4585 $sth->execute( ($self->custnum) x $numnum )
4586 or die "Error executing 'SELECT $sql': ". $sth->errstr;
4587 if ( $sth->fetchrow_arrayref->[0] ) {
4588 $self->hashref->{cust_status} = $status;
4594 =item is_status_delay_cancel
4596 Returns true if customer status is 'suspended'
4597 and all suspended cust_pkg return true for
4598 cust_pkg->is_status_delay_cancel.
4600 This is not a real status, this only meant for hacking display
4601 values, because otherwise treating the customer as suspended is
4602 really the whole point of the delay_cancel option.
4606 sub is_status_delay_cancel {
4608 return 0 unless $self->status eq 'suspended';
4609 foreach my $cust_pkg ($self->ncancelled_pkgs) {
4610 return 0 unless $cust_pkg->is_status_delay_cancel;
4615 =item ucfirst_cust_status
4617 =item ucfirst_status
4619 Returns the status with the first character capitalized.
4623 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
4625 sub ucfirst_cust_status {
4627 ucfirst($self->cust_status);
4632 Returns a hex triplet color string for this customer's status.
4636 sub statuscolor { shift->cust_statuscolor(@_); }
4638 sub cust_statuscolor {
4640 __PACKAGE__->statuscolors->{$self->cust_status};
4643 =item tickets [ STATUS ]
4645 Returns an array of hashes representing the customer's RT tickets.
4647 An optional status (or arrayref or hashref of statuses) may be specified.
4653 my $status = ( @_ && $_[0] ) ? shift : '';
4655 my $num = $conf->config('cust_main-max_tickets') || 10;
4658 if ( $conf->config('ticket_system') ) {
4659 unless ( $conf->config('ticket_system-custom_priority_field') ) {
4661 @tickets = @{ FS::TicketSystem->customer_tickets( $self->custnum,
4670 foreach my $priority (
4671 $conf->config('ticket_system-custom_priority_field-values'), ''
4673 last if scalar(@tickets) >= $num;
4675 @{ FS::TicketSystem->customer_tickets( $self->custnum,
4676 $num - scalar(@tickets),
4687 =item appointments [ STATUS ]
4689 Returns an array of hashes representing the customer's RT tickets which
4696 my $status = ( @_ && $_[0] ) ? shift : '';
4698 return () unless $conf->config('ticket_system');
4700 my $queueid = $conf->config('ticket_system-appointment-queueid');
4702 @{ FS::TicketSystem->customer_tickets( $self->custnum,
4711 # Return services representing svc_accts in customer support packages
4712 sub support_services {
4714 my %packages = map { $_ => 1 } $conf->config('support_packages');
4716 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
4717 grep { $_->part_svc->svcdb eq 'svc_acct' }
4718 map { $_->cust_svc }
4719 grep { exists $packages{ $_->pkgpart } }
4720 $self->ncancelled_pkgs;
4724 # Return a list of latitude/longitude for one of the services (if any)
4725 sub service_coordinates {
4729 grep { $_->latitude && $_->longitude }
4731 map { $_->cust_svc }
4732 $self->ncancelled_pkgs;
4734 scalar(@svc_X) ? ( $svc_X[0]->latitude, $svc_X[0]->longitude ) : ()
4739 Returns a masked version of the named field
4744 my ($self,$field) = @_;
4748 'x'x(length($self->getfield($field))-4).
4749 substr($self->getfield($field), (length($self->getfield($field))-4));
4753 =item payment_history
4755 Returns an array of hashrefs standardizing information from cust_bill, cust_pay,
4756 cust_credit and cust_refund objects. Each hashref has the following fields:
4758 I<type> - one of 'Line item', 'Invoice', 'Payment', 'Credit', 'Refund' or 'Previous'
4760 I<date> - value of _date field, unix timestamp
4762 I<date_pretty> - user-friendly date
4764 I<description> - user-friendly description of item
4766 I<amount> - impact of item on user's balance
4767 (positive for Invoice/Refund/Line item, negative for Payment/Credit.)
4768 Not to be confused with the native 'amount' field in cust_credit, see below.
4770 I<amount_pretty> - includes money char
4772 I<balance> - customer balance, chronologically as of this item
4774 I<balance_pretty> - includes money char
4776 I<charged> - amount charged for cust_bill (Invoice or Line item) records, undef for other types
4778 I<paid> - amount paid for cust_pay records, undef for other types
4780 I<credit> - amount credited for cust_credit records, undef for other types.
4781 Literally the 'amount' field from cust_credit, renamed here to avoid confusion.
4783 I<refund> - amount refunded for cust_refund records, undef for other types
4785 The four table-specific keys always have positive values, whether they reflect charges or payments.
4787 The following options may be passed to this method:
4789 I<line_items> - if true, returns charges ('Line item') rather than invoices
4791 I<start_date> - unix timestamp, only include records on or after.
4792 If specified, an item of type 'Previous' will also be included.
4793 It does not have table-specific fields.
4795 I<end_date> - unix timestamp, only include records before
4797 I<reverse_sort> - order from newest to oldest (default is oldest to newest)
4799 I<conf> - optional already-loaded FS::Conf object.
4803 # Caution: this gets used by FS::ClientAPI::MyAccount::billing_history,
4804 # and also for sending customer statements, which should both be kept customer-friendly.
4805 # If you add anything that shouldn't be passed on through the API or exposed
4806 # to customers, add a new option to include it, don't include it by default
4807 sub payment_history {
4809 my $opt = ref($_[0]) ? $_[0] : { @_ };
4811 my $conf = $$opt{'conf'} || new FS::Conf;
4812 my $money_char = $conf->config("money_char") || '$',
4814 #first load entire history,
4815 #need previous to calculate previous balance
4816 #loading after end_date shouldn't hurt too much?
4818 if ( $$opt{'line_items'} ) {
4820 foreach my $cust_bill ( $self->cust_bill ) {
4823 'type' => 'Line item',
4824 'description' => $_->desc( $self->locale ).
4825 ( $_->sdate && $_->edate
4826 ? ' '. time2str('%d-%b-%Y', $_->sdate).
4827 ' To '. time2str('%d-%b-%Y', $_->edate)
4830 'amount' => sprintf('%.2f', $_->setup + $_->recur ),
4831 'charged' => sprintf('%.2f', $_->setup + $_->recur ),
4832 'date' => $cust_bill->_date,
4833 'date_pretty' => $self->time2str_local('short', $cust_bill->_date ),
4835 foreach $cust_bill->cust_bill_pkg;
4842 'type' => 'Invoice',
4843 'description' => 'Invoice #'. $_->display_invnum,
4844 'amount' => sprintf('%.2f', $_->charged ),
4845 'charged' => sprintf('%.2f', $_->charged ),
4846 'date' => $_->_date,
4847 'date_pretty' => $self->time2str_local('short', $_->_date ),
4849 foreach $self->cust_bill;
4854 'type' => 'Payment',
4855 'description' => 'Payment', #XXX type
4856 'amount' => sprintf('%.2f', 0 - $_->paid ),
4857 'paid' => sprintf('%.2f', $_->paid ),
4858 'date' => $_->_date,
4859 'date_pretty' => $self->time2str_local('short', $_->_date ),
4861 foreach $self->cust_pay;
4865 'description' => 'Credit', #more info?
4866 'amount' => sprintf('%.2f', 0 -$_->amount ),
4867 'credit' => sprintf('%.2f', $_->amount ),
4868 'date' => $_->_date,
4869 'date_pretty' => $self->time2str_local('short', $_->_date ),
4871 foreach $self->cust_credit;
4875 'description' => 'Refund', #more info? type, like payment?
4876 'amount' => $_->refund,
4877 'refund' => $_->refund,
4878 'date' => $_->_date,
4879 'date_pretty' => $self->time2str_local('short', $_->_date ),
4881 foreach $self->cust_refund;
4883 #put it all in chronological order
4884 @history = sort { $a->{'date'} <=> $b->{'date'} } @history;
4886 #calculate balance, filter items outside date range
4890 foreach my $item (@history) {
4891 last if $$opt{'end_date'} && ($$item{'date'} >= $$opt{'end_date'});
4892 $balance += $$item{'amount'};
4893 if ($$opt{'start_date'} && ($$item{'date'} < $$opt{'start_date'})) {
4894 $previous += $$item{'amount'};
4897 $$item{'balance'} = sprintf("%.2f",$balance);
4898 foreach my $key ( qw(amount balance) ) {
4899 $$item{$key.'_pretty'} = money_pretty($$item{$key});
4904 # start with previous balance, if there was one
4907 'type' => 'Previous',
4908 'description' => 'Previous balance',
4909 'amount' => sprintf("%.2f",$previous),
4910 'balance' => sprintf("%.2f",$previous),
4911 'date' => $$opt{'start_date'},
4912 'date_pretty' => $self->time2str_local('short', $$opt{'start_date'} ),
4914 #false laziness with above
4915 foreach my $key ( qw(amount balance) ) {
4916 $$item{$key.'_pretty'} = $$item{$key};
4917 $$item{$key.'_pretty'} =~ s/^(-?)/$1$money_char/;
4919 unshift(@out,$item);
4922 @out = reverse @history if $$opt{'reverse_sort'};
4929 =head1 CLASS METHODS
4935 Class method that returns the list of possible status strings for customers
4936 (see L<the status method|/status>). For example:
4938 @statuses = FS::cust_main->statuses();
4944 keys %{ $self->statuscolors };
4947 =item cust_status_sql
4949 Returns an SQL fragment to determine the status of a cust_main record, as a
4954 sub cust_status_sql {
4956 for my $status ( FS::cust_main->statuses() ) {
4957 my $method = $status.'_sql';
4958 $sql .= ' WHEN ('.FS::cust_main->$method.") THEN '$status'";
4967 Returns an SQL expression identifying prospective cust_main records (customers
4968 with no packages ever ordered)
4972 use vars qw($select_count_pkgs);
4973 $select_count_pkgs =
4974 "SELECT COUNT(*) FROM cust_pkg
4975 WHERE cust_pkg.custnum = cust_main.custnum";
4977 sub select_count_pkgs_sql {
4982 " 0 = ( $select_count_pkgs ) ";
4987 Returns an SQL expression identifying ordered cust_main records (customers with
4988 no active packages, but recurring packages not yet setup or one time charges
4994 FS::cust_main->none_active_sql.
4995 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->not_yet_billed_sql. " ) ";
5000 Returns an SQL expression identifying active cust_main records (customers with
5001 active recurring packages).
5006 " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
5009 =item none_active_sql
5011 Returns an SQL expression identifying cust_main records with no active
5012 recurring packages. This includes customers of status prospect, ordered,
5013 inactive, and suspended.
5017 sub none_active_sql {
5018 " 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
5023 Returns an SQL expression identifying inactive cust_main records (customers with
5024 no active recurring packages, but otherwise unsuspended/uncancelled).
5029 FS::cust_main->none_active_sql.
5030 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " ) ";
5036 Returns an SQL expression identifying suspended cust_main records.
5041 sub suspended_sql { susp_sql(@_); }
5043 FS::cust_main->none_active_sql.
5044 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " ) ";
5050 Returns an SQL expression identifying cancelled cust_main records.
5054 sub cancel_sql { shift->cancelled_sql(@_); }
5057 =item uncancelled_sql
5059 Returns an SQL expression identifying un-cancelled cust_main records.
5063 sub uncancelled_sql { uncancel_sql(@_); }
5066 "( NOT (".$self->cancelled_sql.") )"; #sensitive to cust_main-status_module
5071 Returns an SQL fragment to retreive the balance.
5076 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
5077 WHERE cust_bill.custnum = cust_main.custnum )
5078 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
5079 WHERE cust_pay.custnum = cust_main.custnum )
5080 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
5081 WHERE cust_credit.custnum = cust_main.custnum )
5082 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
5083 WHERE cust_refund.custnum = cust_main.custnum )
5086 =item balance_date_sql [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
5088 Returns an SQL fragment to retreive the balance for this customer, optionally
5089 considering invoices with date earlier than START_TIME, and not
5090 later than END_TIME (total_owed_date minus total_unapplied_credits minus
5091 total_unapplied_payments).
5093 Times are specified as SQL fragments or numeric
5094 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
5095 L<Date::Parse> for conversion functions. The empty string can be passed
5096 to disable that time constraint completely.
5098 Available options are:
5102 =item unapplied_date
5104 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)
5109 set to true to remove all customer comparison clauses, for totals
5114 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
5119 JOIN clause (typically used with the total option)
5123 An absolute cutoff time. Payments, credits, and refunds I<applied> after this
5124 time will be ignored. Note that START_TIME and END_TIME only limit the date
5125 range for invoices and I<unapplied> payments, credits, and refunds.
5131 sub balance_date_sql {
5132 my( $class, $start, $end, %opt ) = @_;
5134 my $cutoff = $opt{'cutoff'};
5136 my $owed = FS::cust_bill->owed_sql($cutoff);
5137 my $unapp_refund = FS::cust_refund->unapplied_sql($cutoff);
5138 my $unapp_credit = FS::cust_credit->unapplied_sql($cutoff);
5139 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
5141 my $j = $opt{'join'} || '';
5143 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
5144 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
5145 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
5146 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
5148 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
5149 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
5150 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
5151 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
5156 =item unapplied_payments_date_sql START_TIME [ END_TIME ]
5158 Returns an SQL fragment to retreive the total unapplied payments for this
5159 customer, only considering payments with date earlier than START_TIME, and
5160 optionally not later than END_TIME.
5162 Times are specified as SQL fragments or numeric
5163 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
5164 L<Date::Parse> for conversion functions. The empty string can be passed
5165 to disable that time constraint completely.
5167 Available options are:
5171 sub unapplied_payments_date_sql {
5172 my( $class, $start, $end, %opt ) = @_;
5174 my $cutoff = $opt{'cutoff'};
5176 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
5178 my $pay_where = $class->_money_table_where( 'cust_pay', $start, $end,
5179 'unapplied_date'=>1 );
5181 " ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $pay_where ) ";
5184 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5186 Helper method for balance_date_sql; name (and usage) subject to change
5187 (suggestions welcome).
5189 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
5190 cust_refund, cust_credit or cust_pay).
5192 If TABLE is "cust_bill" or the unapplied_date option is true, only
5193 considers records with date earlier than START_TIME, and optionally not
5194 later than END_TIME .
5198 sub _money_table_where {
5199 my( $class, $table, $start, $end, %opt ) = @_;
5202 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
5203 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
5204 push @where, "$table._date <= $start" if defined($start) && length($start);
5205 push @where, "$table._date > $end" if defined($end) && length($end);
5207 push @where, @{$opt{'where'}} if $opt{'where'};
5208 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
5214 #for dyanmic FS::$table->search in httemplate/misc/email_customers.html
5215 use FS::cust_main::Search;
5218 FS::cust_main::Search->search(@_);
5227 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5229 Deprecated. Use event notification and message templates
5230 (L<FS::msg_template>) instead.
5232 Sends a templated email notification to the customer (see L<Text::Template>).
5234 OPTIONS is a hash and may include
5236 I<from> - the email sender (default is invoice_from)
5238 I<to> - comma-separated scalar or arrayref of recipients
5239 (default is invoicing_list)
5241 I<subject> - The subject line of the sent email notification
5242 (default is "Notice from company_name")
5244 I<extra_fields> - a hashref of name/value pairs which will be substituted
5247 The following variables are vavailable in the template.
5249 I<$first> - the customer first name
5250 I<$last> - the customer last name
5251 I<$company> - the customer company
5252 I<$payby> - a description of the method of payment for the customer
5253 # would be nice to use FS::payby::shortname
5254 I<$payinfo> - the account information used to collect for this customer
5255 I<$expdate> - the expiration of the customer payment in seconds from epoch
5260 my ($self, $template, %options) = @_;
5262 return unless $conf->exists($template);
5264 my $from = $conf->invoice_from_full($self->agentnum)
5265 if $conf->exists('invoice_from', $self->agentnum);
5266 $from = $options{from} if exists($options{from});
5268 my $to = join(',', $self->invoicing_list_emailonly);
5269 $to = $options{to} if exists($options{to});
5271 my $subject = "Notice from " . $conf->config('company_name', $self->agentnum)
5272 if $conf->exists('company_name', $self->agentnum);
5273 $subject = $options{subject} if exists($options{subject});
5275 my $notify_template = new Text::Template (TYPE => 'ARRAY',
5276 SOURCE => [ map "$_\n",
5277 $conf->config($template)]
5279 or die "can't create new Text::Template object: Text::Template::ERROR";
5280 $notify_template->compile()
5281 or die "can't compile template: Text::Template::ERROR";
5283 $FS::notify_template::_template::company_name =
5284 $conf->config('company_name', $self->agentnum);
5285 $FS::notify_template::_template::company_address =
5286 join("\n", $conf->config('company_address', $self->agentnum) ). "\n";
5288 my $paydate = $self->paydate || '2037-12-31';
5289 $FS::notify_template::_template::first = $self->first;
5290 $FS::notify_template::_template::last = $self->last;
5291 $FS::notify_template::_template::company = $self->company;
5292 $FS::notify_template::_template::payinfo = $self->mask_payinfo;
5293 my $payby = $self->payby;
5294 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5295 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5297 #credit cards expire at the end of the month/year of their exp date
5298 if ($payby eq 'CARD' || $payby eq 'DCRD') {
5299 $FS::notify_template::_template::payby = 'credit card';
5300 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5301 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5303 }elsif ($payby eq 'COMP') {
5304 $FS::notify_template::_template::payby = 'complimentary account';
5306 $FS::notify_template::_template::payby = 'current method';
5308 $FS::notify_template::_template::expdate = $expire_time;
5310 for (keys %{$options{extra_fields}}){
5312 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
5315 send_email(from => $from,
5317 subject => $subject,
5318 body => $notify_template->fill_in( PACKAGE =>
5319 'FS::notify_template::_template' ),
5324 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5326 Generates a templated notification to the customer (see L<Text::Template>).
5328 OPTIONS is a hash and may include
5330 I<extra_fields> - a hashref of name/value pairs which will be substituted
5331 into the template. These values may override values mentioned below
5332 and those from the customer record.
5334 I<template_text> - if present, ignores TEMPLATE_NAME and uses the provided text
5336 The following variables are available in the template instead of or in addition
5337 to the fields of the customer record.
5339 I<$payby> - a description of the method of payment for the customer
5340 # would be nice to use FS::payby::shortname
5341 I<$payinfo> - the masked account information used to collect for this customer
5342 I<$expdate> - the expiration of the customer payment method in seconds from epoch
5343 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
5347 # a lot like cust_bill::print_latex
5348 sub generate_letter {
5349 my ($self, $template, %options) = @_;
5351 warn "Template $template does not exist" && return
5352 unless $conf->exists($template) || $options{'template_text'};
5354 my $template_source = $options{'template_text'}
5355 ? [ $options{'template_text'} ]
5356 : [ map "$_\n", $conf->config($template) ];
5358 my $letter_template = new Text::Template
5360 SOURCE => $template_source,
5361 DELIMITERS => [ '[@--', '--@]' ],
5363 or die "can't create new Text::Template object: Text::Template::ERROR";
5365 $letter_template->compile()
5366 or die "can't compile template: Text::Template::ERROR";
5368 my %letter_data = map { $_ => $self->$_ } $self->fields;
5369 $letter_data{payinfo} = $self->mask_payinfo;
5371 #my $paydate = $self->paydate || '2037-12-31';
5372 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
5374 my $payby = $self->payby;
5375 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5376 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5378 #credit cards expire at the end of the month/year of their exp date
5379 if ($payby eq 'CARD' || $payby eq 'DCRD') {
5380 $letter_data{payby} = 'credit card';
5381 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5382 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5384 }elsif ($payby eq 'COMP') {
5385 $letter_data{payby} = 'complimentary account';
5387 $letter_data{payby} = 'current method';
5389 $letter_data{expdate} = $expire_time;
5391 for (keys %{$options{extra_fields}}){
5392 $letter_data{$_} = $options{extra_fields}->{$_};
5395 unless(exists($letter_data{returnaddress})){
5396 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
5397 $self->agent_template)
5399 if ( length($retadd) ) {
5400 $letter_data{returnaddress} = $retadd;
5401 } elsif ( grep /\S/, $conf->config('company_address', $self->agentnum) ) {
5402 $letter_data{returnaddress} =
5403 join( "\n", map { s/( {2,})/'~' x length($1)/eg;
5407 ( $conf->config('company_name', $self->agentnum),
5408 $conf->config('company_address', $self->agentnum),
5412 $letter_data{returnaddress} = '~';
5416 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
5418 $letter_data{company_name} = $conf->config('company_name', $self->agentnum);
5420 my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
5422 my $lh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
5426 ) or die "can't open temp file: $!\n";
5427 print $lh $conf->config_binary('logo.eps', $self->agentnum)
5428 or die "can't write temp file: $!\n";
5430 $letter_data{'logo_file'} = $lh->filename;
5432 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
5436 ) or die "can't open temp file: $!\n";
5438 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
5440 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
5441 return ($1, $letter_data{'logo_file'});
5445 =item print_ps TEMPLATE
5447 Returns an postscript letter filled in from TEMPLATE, as a scalar.
5453 my($file, $lfile) = $self->generate_letter(@_);
5454 my $ps = FS::Misc::generate_ps($file);
5455 unlink($file.'.tex');
5461 =item print TEMPLATE
5463 Prints the filled in template.
5465 TEMPLATE is the name of a L<Text::Template> to fill in and print.
5469 sub queueable_print {
5472 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
5473 or die "invalid customer number: " . $opt{custnum};
5475 my $error = $self->print( { 'template' => $opt{template} } );
5476 die $error if $error;
5480 my ($self, $template) = (shift, shift);
5482 [ $self->print_ps($template) ],
5483 'agentnum' => $self->agentnum,
5487 #these three subs should just go away once agent stuff is all config overrides
5489 sub agent_template {
5491 $self->_agent_plandata('agent_templatename');
5494 sub agent_invoice_from {
5496 $self->_agent_plandata('agent_invoice_from');
5499 sub _agent_plandata {
5500 my( $self, $option ) = @_;
5502 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
5503 #agent-specific Conf
5505 use FS::part_event::Condition;
5507 my $agentnum = $self->agentnum;
5509 my $regexp = regexp_sql();
5511 my $part_event_option =
5513 'select' => 'part_event_option.*',
5514 'table' => 'part_event_option',
5516 LEFT JOIN part_event USING ( eventpart )
5517 LEFT JOIN part_event_option AS peo_agentnum
5518 ON ( part_event.eventpart = peo_agentnum.eventpart
5519 AND peo_agentnum.optionname = 'agentnum'
5520 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
5522 LEFT JOIN part_event_condition
5523 ON ( part_event.eventpart = part_event_condition.eventpart
5524 AND part_event_condition.conditionname = 'cust_bill_age'
5526 LEFT JOIN part_event_condition_option
5527 ON ( part_event_condition.eventconditionnum = part_event_condition_option.eventconditionnum
5528 AND part_event_condition_option.optionname = 'age'
5531 #'hashref' => { 'optionname' => $option },
5532 #'hashref' => { 'part_event_option.optionname' => $option },
5534 " WHERE part_event_option.optionname = ". dbh->quote($option).
5535 " AND action = 'cust_bill_send_agent' ".
5536 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
5537 " AND peo_agentnum.optionname = 'agentnum' ".
5538 " AND ( agentnum IS NULL OR agentnum = $agentnum ) ".
5540 CASE WHEN part_event_condition_option.optionname IS NULL
5542 ELSE ". FS::part_event::Condition->age2seconds_sql('part_event_condition_option.optionvalue').
5544 , part_event.weight".
5548 unless ( $part_event_option ) {
5549 return $self->agent->invoice_template || ''
5550 if $option eq 'agent_templatename';
5554 $part_event_option->optionvalue;
5558 =item queued_bill 'custnum' => CUSTNUM [ , OPTION => VALUE ... ]
5560 Subroutine (not a method), designed to be called from the queue.
5562 Takes a list of options and values.
5564 Pulls up the customer record via the custnum option and calls bill_and_collect.
5569 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
5571 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
5572 warn 'bill_and_collect custnum#'. $cust_main->custnum. "\n";#log custnum w/pid
5574 #without this errors don't get rolled back
5575 $args{'fatal'} = 1; # runs from job queue, will be caught
5577 $cust_main->bill_and_collect( %args );
5580 sub process_bill_and_collect {
5582 my $param = thaw(decode_base64(shift));
5583 my $cust_main = qsearchs( 'cust_main', { custnum => $param->{'custnum'} } )
5584 or die "custnum '$param->{custnum}' not found!\n";
5585 $param->{'job'} = $job;
5586 $param->{'fatal'} = 1; # runs from job queue, will be caught
5587 $param->{'retry'} = 1;
5589 $cust_main->bill_and_collect( %$param );
5592 #starting to take quite a while for big dbs
5593 # (JRNL: journaled so it only happens once per database)
5594 # - seq scan of h_cust_main (yuck), but not going to index paycvv, so
5595 # JRNL seq scan of cust_main on signupdate... index signupdate? will that help?
5596 # JRNL seq scan of cust_main on paydate... index on substrings? maybe set an
5597 # JRNL seq scan of cust_main on payinfo.. certainly not going toi ndex that...
5598 # JRNL leading/trailing spaces in first, last, company
5599 # - otaker upgrade? journal and call it good? (double check to make sure
5600 # we're not still setting otaker here)
5602 #only going to get worse with new location stuff...
5604 sub _upgrade_data { #class method
5605 my ($class, %opts) = @_;
5608 'UPDATE h_cust_main SET paycvv = NULL WHERE paycvv IS NOT NULL',
5611 #this seems to be the only expensive one.. why does it take so long?
5612 unless ( FS::upgrade_journal->is_done('cust_main__signupdate') ) {
5614 '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';
5615 FS::upgrade_journal->set_done('cust_main__signupdate');
5618 unless ( FS::upgrade_journal->is_done('cust_main__paydate') ) {
5620 # fix yyyy-m-dd formatted paydates
5621 if ( driver_name =~ /^mysql/i ) {
5623 "UPDATE cust_main SET paydate = CONCAT( SUBSTRING(paydate FROM 1 FOR 5), '0', SUBSTRING(paydate FROM 6) ) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
5624 } else { # the SQL standard
5626 "UPDATE cust_main SET paydate = SUBSTRING(paydate FROM 1 FOR 5) || '0' || SUBSTRING(paydate FROM 6) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
5628 FS::upgrade_journal->set_done('cust_main__paydate');
5631 unless ( FS::upgrade_journal->is_done('cust_main__payinfo') ) {
5633 push @statements, #fix the weird BILL with a cc# in payinfo problem
5635 "UPDATE cust_main SET payby = 'DCRD' WHERE payby = 'BILL' and length(payinfo) = 16 and payinfo ". regexp_sql. q( '^[0-9]*$' );
5637 FS::upgrade_journal->set_done('cust_main__payinfo');
5642 foreach my $sql ( @statements ) {
5643 my $sth = dbh->prepare($sql) or die dbh->errstr;
5644 $sth->execute or die $sth->errstr;
5645 #warn ( (time - $t). " seconds\n" );
5649 local($ignore_expired_card) = 1;
5650 local($ignore_banned_card) = 1;
5651 local($skip_fuzzyfiles) = 1;
5652 local($import) = 1; #prevent automatic geocoding (need its own variable?)
5654 FS::cust_main::Location->_upgrade_data(%opts);
5656 unless ( FS::upgrade_journal->is_done('cust_main__trimspaces') ) {
5658 foreach my $cust_main ( qsearch({
5659 'table' => 'cust_main',
5661 'extra_sql' => 'WHERE '.
5663 map "$_ LIKE ' %' OR $_ LIKE '% ' OR $_ LIKE '% %'",
5664 qw( first last company )
5667 my $error = $cust_main->replace;
5668 die $error if $error;
5671 FS::upgrade_journal->set_done('cust_main__trimspaces');
5675 $class->_upgrade_otaker(%opts);
5677 # turn on encryption as part of regular upgrade, so all new records are immediately encrypted
5678 # existing records will be encrypted in queueable_upgrade (below)
5679 unless ($conf->exists('encryptionpublickey') || $conf->exists('encryptionprivatekey')) {
5680 eval "use FS::Setup";
5682 FS::Setup::enable_encryption();
5685 $class->_upgrade_data_paydate_edgebug;
5688 =item _upgrade_data_paydate_edgebug
5690 Correct bad data injected into payment expire date column by Edge browser bug
5692 The month and year values may have an extra character injected into form POST
5693 data by Edge browser. It was possible for some bad month values to slip
5694 past data validation.
5696 If the stored value was out of range, it was causing payments screen to crash.
5697 We can detect and fix this by dropping the second digit.
5699 If the stored value is is 11 or 12, it's possible the user inputted a 1. In
5700 this case, the payment method will fail to authorize, but the record will
5701 not cause crashdumps for being out of range.
5703 In short, check for any expiration month > 12, and drop the extra digit
5707 sub _upgrade_data_paydate_edgebug {
5708 my $journal_label = 'cust_main_paydate_edgebug';
5709 return if FS::upgrade_journal->is_done( $journal_label );
5711 my $oldAutoCommit = $FS::UID::AutoCommit;
5712 local $FS::UID::AutoCommit = 0;
5715 FS::Record::qsearch(
5716 cust_main => { paydate => { op => '!=', value => '' }}
5719 next unless $row->ut_daten('paydate');
5721 # paydate column stored in database has failed date validation
5722 my $bad_paydate = $row->paydate;
5724 my @date = split /[\-\/]/, $bad_paydate;
5725 @date = @date[2,0,1] if $date[2] > 1900;
5727 # Only autocorrecting when month > 12 - notify operator
5728 unless ( $date[1] > 12 ) {
5730 'Unable to correct bad paydate stored in cust_main row '.
5731 'custnum(%s) paydate(%s)',
5737 $date[1] = substr( $date[1], 0, 1 );
5738 $row->paydate( join('-', @date ));
5740 if ( my $error = $row->replace ) {
5742 'Failed to autocorrect bad paydate stored in cust_main row '.
5743 'custnum(%s) paydate(%s) - error: %s',
5751 'Autocorrected bad paydate stored in cust_main row '.
5752 "custnum(%s) old-paydate(%s) new-paydate(%s)\n",
5760 FS::upgrade_journal->set_done( $journal_label );
5761 dbh->commit unless $oldAutoCommit;
5765 sub queueable_upgrade {
5768 ### encryption gets turned on in _upgrade_data, above
5770 eval "use FS::upgrade_journal";
5773 # prior to 2013 (commit f16665c9) payinfo was stored in history if not encrypted,
5774 # clear that out before encrypting/tokenizing anything else
5775 if (!FS::upgrade_journal->is_done('clear_payinfo_history')) {
5776 foreach my $table ('cust_main','cust_pay_pending','cust_pay','cust_pay_void','cust_refund') {
5777 my $sql = 'UPDATE h_'.$table.' SET payinfo = NULL WHERE payinfo IS NOT NULL';
5778 my $sth = dbh->prepare($sql) or die dbh->errstr;
5779 $sth->execute or die $sth->errstr;
5781 FS::upgrade_journal->set_done('clear_payinfo_history');
5784 # fix Tokenized paycardtype and encrypt old records
5785 if ( ! FS::upgrade_journal->is_done('paycardtype_Tokenized')
5786 || ! FS::upgrade_journal->is_done('encryption_check')
5790 # allow replacement of closed cust_pay/cust_refund records
5791 local $FS::payinfo_Mixin::allow_closed_replace = 1;
5793 # because it looks like nothing's changing
5794 local $FS::Record::no_update_diff = 1;
5796 # commit everything immediately
5797 local $FS::UID::AutoCommit = 1;
5799 # encrypt what's there
5800 foreach my $table ('cust_main','cust_pay_pending','cust_pay','cust_pay_void','cust_refund') {
5801 my $tclass = 'FS::'.$table;
5804 while (my $recnum = _upgrade_next_recnum(dbh,$table,\$lastrecnum,\@recnums)) {
5805 my $record = $tclass->by_key($recnum);
5806 next unless $record; # small chance it's been deleted, that's ok
5807 next unless grep { $record->payby eq $_ } @FS::Record::encrypt_payby;
5808 # window for possible conflict is practically nonexistant,
5809 # but just in case...
5810 $record = $record->select_for_update;
5811 if (!$record->custnum && $table eq 'cust_pay_pending') {
5812 $record->set('custnum_pending',1);
5814 $record->paycardtype('') if $record->paycardtype eq 'Tokenized';
5816 local($ignore_expired_card) = 1;
5817 local($ignore_banned_card) = 1;
5818 local($skip_fuzzyfiles) = 1;
5819 local($import) = 1;#prevent automatic geocoding (need its own variable?)
5821 my $error = $record->replace;
5822 die "Error replacing $table ".$record->get($record->primary_key).": $error" if $error;
5826 FS::upgrade_journal->set_done('paycardtype_Tokenized');
5827 FS::upgrade_journal->set_done('encryption_check') if $conf->exists('encryption');
5832 # not entirely false laziness w/ Billing_Realtime::_token_check_next_recnum
5833 # cust_payby might get deleted while this runs
5835 sub _upgrade_next_recnum {
5836 my ($dbh,$table,$lastrecnum,$recnums) = @_;
5837 my $recnum = shift @$recnums;
5838 return $recnum if $recnum;
5839 my $tclass = 'FS::'.$table;
5840 my $paycardtypecheck = ($table ne 'cust_pay_pending') ? q( OR paycardtype = 'Tokenized') : '';
5841 my $sql = 'SELECT '.$tclass->primary_key.
5843 ' WHERE '.$tclass->primary_key.' > '.$$lastrecnum.
5844 " AND payby IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
5845 " AND ( length(payinfo) < 80$paycardtypecheck ) ".
5846 ' ORDER BY '.$tclass->primary_key.' LIMIT 500';
5847 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
5848 $sth->execute() or die $sth->errstr;
5850 while (my $rec = $sth->fetchrow_hashref) {
5851 push @$recnums, $rec->{$tclass->primary_key};
5854 $$lastrecnum = $$recnums[-1];
5855 return shift @$recnums;
5864 The delete method should possibly take an FS::cust_main object reference
5865 instead of a scalar customer number.
5867 Bill and collect options should probably be passed as references instead of a
5870 There should probably be a configuration file with a list of allowed credit
5873 No multiple currency support (probably a larger project than just this module).
5875 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
5877 Birthdates rely on negative epoch values.
5879 The payby for card/check batches is broken. With mixed batching, bad
5882 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
5886 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
5887 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
5888 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.