5 #FS::cust_main:_Marketgear when they're ready to move to 2.1
6 use base qw( FS::cust_main::Packages FS::cust_main::Status
7 FS::cust_main::Billing FS::cust_main::Billing_Realtime
8 FS::cust_main::Billing_Discount
9 FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
13 use vars qw( $DEBUG $me $conf
16 $ignore_expired_card $ignore_illegal_zip $ignore_banned_card
17 $skip_fuzzyfiles @fuzzyfields
21 use Scalar::Util qw( blessed );
22 use Time::Local qw(timelocal);
23 use Storable qw(thaw);
27 use Digest::MD5 qw(md5_base64);
30 use File::Temp; #qw( tempfile );
31 use Business::CreditCard 0.28;
33 use FS::UID qw( getotaker dbh driver_name );
34 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
35 use FS::Misc qw( generate_email send_email generate_ps do_print );
36 use FS::Msgcat qw(gettext);
44 use FS::cust_pay_pending;
45 use FS::cust_pay_void;
46 use FS::cust_pay_batch;
49 use FS::part_referral;
50 use FS::cust_main_county;
51 use FS::cust_location;
53 use FS::cust_main_exemption;
54 use FS::cust_tax_adjustment;
55 use FS::cust_tax_location;
57 use FS::cust_main_invoice;
59 use FS::prepay_credit;
65 use FS::payment_gateway;
66 use FS::agent_payment_gateway;
68 use FS::cust_main_note;
69 use FS::cust_attachment;
72 # 1 is mostly method/subroutine entry and options
73 # 2 traces progress of some operations
74 # 3 is even more information including possibly sensitive data
76 $me = '[FS::cust_main]';
79 $ignore_expired_card = 0;
80 $ignore_illegal_zip = 0;
81 $ignore_banned_card = 0;
84 @fuzzyfields = ( 'first', 'last', 'company', 'address1' );
86 @encrypted_fields = ('payinfo', 'paycvv');
87 sub nohistory_fields { ('payinfo', 'paycvv'); }
89 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
91 #ask FS::UID to run this stuff for us later
92 #$FS::UID::callback{'FS::cust_main'} = sub {
93 install_callback FS::UID sub {
95 #yes, need it for stuff below (prolly should be cached)
100 my ( $hashref, $cache ) = @_;
101 if ( exists $hashref->{'pkgnum'} ) {
102 #@{ $self->{'_pkgnum'} } = ();
103 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
104 $self->{'_pkgnum'} = $subcache;
105 #push @{ $self->{'_pkgnum'} },
106 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
112 FS::cust_main - Object methods for cust_main records
118 $record = new FS::cust_main \%hash;
119 $record = new FS::cust_main { 'column' => 'value' };
121 $error = $record->insert;
123 $error = $new_record->replace($old_record);
125 $error = $record->delete;
127 $error = $record->check;
129 @cust_pkg = $record->all_pkgs;
131 @cust_pkg = $record->ncancelled_pkgs;
133 @cust_pkg = $record->suspended_pkgs;
135 $error = $record->bill;
136 $error = $record->bill %options;
137 $error = $record->bill 'time' => $time;
139 $error = $record->collect;
140 $error = $record->collect %options;
141 $error = $record->collect 'invoice_time' => $time,
146 An FS::cust_main object represents a customer. FS::cust_main inherits from
147 FS::Record. The following fields are currently supported:
153 Primary key (assigned automatically for new customers)
157 Agent (see L<FS::agent>)
161 Advertising source (see L<FS::part_referral>)
173 Cocial security number (optional)
189 (optional, see L<FS::cust_main_county>)
193 (see L<FS::cust_main_county>)
199 (see L<FS::cust_main_county>)
235 (optional, see L<FS::cust_main_county>)
239 (see L<FS::cust_main_county>)
245 (see L<FS::cust_main_county>)
261 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
265 Payment Information (See L<FS::payinfo_Mixin> for data format)
269 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
273 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
277 Expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
281 Start date month (maestro/solo cards only)
285 Start date year (maestro/solo cards only)
289 Issue number (maestro/solo cards only)
293 Name on card or billing name
297 IP address from which payment information was received
301 Tax exempt, empty or `Y'
305 Order taker (see L<FS::access_user>)
311 =item referral_custnum
313 Referring customer number
317 Enable individual CDR spooling, empty or `Y'
321 A suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
325 Discourage individual CDR printing, empty or `Y'
335 Creates a new customer. To add the customer to the database, see L<"insert">.
337 Note that this stores the hash reference, not a distinct copy of the hash it
338 points to. You can ask the object for a copy with the I<hash> method.
342 sub table { 'cust_main'; }
344 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
346 Adds this customer to the database. If there is an error, returns the error,
347 otherwise returns false.
349 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
350 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
351 are inserted atomicly, or the transaction is rolled back. Passing an empty
352 hash reference is equivalent to not supplying this parameter. There should be
353 a better explanation of this, but until then, here's an example:
356 tie %hash, 'Tie::RefHash'; #this part is important
358 $cust_pkg => [ $svc_acct ],
361 $cust_main->insert( \%hash );
363 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
364 be set as the invoicing list (see L<"invoicing_list">). Errors return as
365 expected and rollback the entire transaction; it is not necessary to call
366 check_invoicing_list first. The invoicing_list is set after the records in the
367 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
368 invoicing_list destination to the newly-created svc_acct. Here's an example:
370 $cust_main->insert( {}, [ $email, 'POST' ] );
372 Currently available options are: I<depend_jobnum>, I<noexport>,
373 I<tax_exemption> and I<prospectnum>.
375 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
376 on the supplied jobnum (they will not run until the specific job completes).
377 This can be used to defer provisioning until some action completes (such
378 as running the customer's credit card successfully).
380 The I<noexport> option is deprecated. If I<noexport> is set true, no
381 provisioning jobs (exports) are scheduled. (You can schedule them later with
382 the B<reexport> method.)
384 The I<tax_exemption> option can be set to an arrayref of tax names.
385 FS::cust_main_exemption records will be created and inserted.
387 If I<prospectnum> is set, moves contacts and locations from that prospect.
393 my $cust_pkgs = @_ ? shift : {};
394 my $invoicing_list = @_ ? shift : '';
396 warn "$me insert called with options ".
397 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
400 local $SIG{HUP} = 'IGNORE';
401 local $SIG{INT} = 'IGNORE';
402 local $SIG{QUIT} = 'IGNORE';
403 local $SIG{TERM} = 'IGNORE';
404 local $SIG{TSTP} = 'IGNORE';
405 local $SIG{PIPE} = 'IGNORE';
407 my $oldAutoCommit = $FS::UID::AutoCommit;
408 local $FS::UID::AutoCommit = 0;
411 my $prepay_identifier = '';
412 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
414 if ( $self->payby eq 'PREPAY' ) {
416 $self->payby('BILL');
417 $prepay_identifier = $self->payinfo;
420 warn " looking up prepaid card $prepay_identifier\n"
423 my $error = $self->get_prepay( $prepay_identifier,
424 'amount_ref' => \$amount,
425 'seconds_ref' => \$seconds,
426 'upbytes_ref' => \$upbytes,
427 'downbytes_ref' => \$downbytes,
428 'totalbytes_ref' => \$totalbytes,
431 $dbh->rollback if $oldAutoCommit;
432 #return "error applying prepaid card (transaction rolled back): $error";
436 $payby = 'PREP' if $amount;
438 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
441 $self->payby('BILL');
442 $amount = $self->paid;
446 warn " inserting $self\n"
449 $self->signupdate(time) unless $self->signupdate;
451 $self->auto_agent_custid()
452 if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
454 my $error = $self->SUPER::insert;
456 $dbh->rollback if $oldAutoCommit;
457 #return "inserting cust_main record (transaction rolled back): $error";
461 warn " setting invoicing list\n"
464 if ( $invoicing_list ) {
465 $error = $self->check_invoicing_list( $invoicing_list );
467 $dbh->rollback if $oldAutoCommit;
468 #return "checking invoicing_list (transaction rolled back): $error";
471 $self->invoicing_list( $invoicing_list );
474 warn " setting customer tags\n"
477 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
478 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
479 'custnum' => $self->custnum };
480 my $error = $cust_tag->insert;
482 $dbh->rollback if $oldAutoCommit;
487 my $prospectnum = delete $options{'prospectnum'};
488 if ( $prospectnum ) {
490 warn " moving contacts and locations from prospect $prospectnum\n"
494 qsearchs('prospect_main', { 'prospectnum' => $prospectnum } );
495 unless ( $prospect_main ) {
496 $dbh->rollback if $oldAutoCommit;
497 return "Unknown prospectnum $prospectnum";
499 $prospect_main->custnum($self->custnum);
500 $prospect_main->disabled('Y');
501 my $error = $prospect_main->replace;
503 $dbh->rollback if $oldAutoCommit;
507 my @contact = $prospect_main->contact;
508 my @cust_location = $prospect_main->cust_location;
509 my @qual = $prospect_main->qual;
511 foreach my $r ( @contact, @cust_location, @qual ) {
513 $r->custnum($self->custnum);
514 my $error = $r->replace;
516 $dbh->rollback if $oldAutoCommit;
523 warn " setting cust_main_exemption\n"
526 my $tax_exemption = delete $options{'tax_exemption'};
527 if ( $tax_exemption ) {
528 foreach my $taxname ( @$tax_exemption ) {
529 my $cust_main_exemption = new FS::cust_main_exemption {
530 'custnum' => $self->custnum,
531 'taxname' => $taxname,
533 my $error = $cust_main_exemption->insert;
535 $dbh->rollback if $oldAutoCommit;
536 return "inserting cust_main_exemption (transaction rolled back): $error";
541 if ( $self->can('start_copy_skel') ) {
542 my $error = $self->start_copy_skel;
544 $dbh->rollback if $oldAutoCommit;
549 warn " ordering packages\n"
552 $error = $self->order_pkgs( $cust_pkgs,
554 'seconds_ref' => \$seconds,
555 'upbytes_ref' => \$upbytes,
556 'downbytes_ref' => \$downbytes,
557 'totalbytes_ref' => \$totalbytes,
560 $dbh->rollback if $oldAutoCommit;
565 $dbh->rollback if $oldAutoCommit;
566 return "No svc_acct record to apply pre-paid time";
568 if ( $upbytes || $downbytes || $totalbytes ) {
569 $dbh->rollback if $oldAutoCommit;
570 return "No svc_acct record to apply pre-paid data";
574 warn " inserting initial $payby payment of $amount\n"
576 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
578 $dbh->rollback if $oldAutoCommit;
579 return "inserting payment (transaction rolled back): $error";
583 unless ( $import || $skip_fuzzyfiles ) {
584 warn " queueing fuzzyfiles update\n"
586 $error = $self->queue_fuzzyfiles_update;
588 $dbh->rollback if $oldAutoCommit;
589 return "updating fuzzy search cache: $error";
594 warn " exporting\n" if $DEBUG > 1;
596 my $export_args = $options{'export_args'} || [];
599 map qsearch( 'part_export', {exportnum=>$_} ),
600 $conf->config('cust_main-exports'); #, $agentnum
602 foreach my $part_export ( @part_export ) {
603 my $error = $part_export->export_insert($self, @$export_args);
605 $dbh->rollback if $oldAutoCommit;
606 return "exporting to ". $part_export->exporttype.
607 " (transaction rolled back): $error";
611 #foreach my $depend_jobnum ( @$depend_jobnums ) {
612 # warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
614 # foreach my $jobnum ( @jobnums ) {
615 # my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
616 # warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
618 # my $error = $queue->depend_insert($depend_jobnum);
620 # $dbh->rollback if $oldAutoCommit;
621 # return "error queuing job dependancy: $error";
628 #if ( exists $options{'jobnums'} ) {
629 # push @{ $options{'jobnums'} }, @jobnums;
632 warn " insert complete; committing transaction\n"
635 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
640 use File::CounterFile;
641 sub auto_agent_custid {
644 my $format = $conf->config('cust_main-auto_agent_custid');
646 if ( $format eq '1YMMXXXXXXXX' ) {
648 my $counter = new File::CounterFile 'cust_main.agent_custid';
651 my $ym = 100000000000 + time2str('%y%m00000000', time);
652 if ( $ym > $counter->value ) {
653 $counter->{'value'} = $agent_custid = $ym;
654 $counter->{'updated'} = 1;
656 $agent_custid = $counter->inc;
662 die "Unknown cust_main-auto_agent_custid format: $format";
665 $self->agent_custid($agent_custid);
669 =item PACKAGE METHODS
671 Documentation on customer package methods has been moved to
672 L<FS::cust_main::Packages>.
674 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
676 Recharges this (existing) customer with the specified prepaid card (see
677 L<FS::prepay_credit>), specified either by I<identifier> or as an
678 FS::prepay_credit object. If there is an error, returns the error, otherwise
681 Optionally, five scalar references can be passed as well. They will have their
682 values filled in with the amount, number of seconds, and number of upload,
683 download, and total bytes applied by this prepaid card.
687 #the ref bullshit here should be refactored like get_prepay. MyAccount.pm is
688 #the only place that uses these args
689 sub recharge_prepay {
690 my( $self, $prepay_credit, $amountref, $secondsref,
691 $upbytesref, $downbytesref, $totalbytesref ) = @_;
693 local $SIG{HUP} = 'IGNORE';
694 local $SIG{INT} = 'IGNORE';
695 local $SIG{QUIT} = 'IGNORE';
696 local $SIG{TERM} = 'IGNORE';
697 local $SIG{TSTP} = 'IGNORE';
698 local $SIG{PIPE} = 'IGNORE';
700 my $oldAutoCommit = $FS::UID::AutoCommit;
701 local $FS::UID::AutoCommit = 0;
704 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
706 my $error = $self->get_prepay( $prepay_credit,
707 'amount_ref' => \$amount,
708 'seconds_ref' => \$seconds,
709 'upbytes_ref' => \$upbytes,
710 'downbytes_ref' => \$downbytes,
711 'totalbytes_ref' => \$totalbytes,
713 || $self->increment_seconds($seconds)
714 || $self->increment_upbytes($upbytes)
715 || $self->increment_downbytes($downbytes)
716 || $self->increment_totalbytes($totalbytes)
717 || $self->insert_cust_pay_prepay( $amount,
719 ? $prepay_credit->identifier
724 $dbh->rollback if $oldAutoCommit;
728 if ( defined($amountref) ) { $$amountref = $amount; }
729 if ( defined($secondsref) ) { $$secondsref = $seconds; }
730 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
731 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
732 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
734 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
739 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
741 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
742 specified either by I<identifier> or as an FS::prepay_credit object.
744 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
745 incremented by the values of the prepaid card.
747 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
748 check or set this customer's I<agentnum>.
750 If there is an error, returns the error, otherwise returns false.
756 my( $self, $prepay_credit, %opt ) = @_;
758 local $SIG{HUP} = 'IGNORE';
759 local $SIG{INT} = 'IGNORE';
760 local $SIG{QUIT} = 'IGNORE';
761 local $SIG{TERM} = 'IGNORE';
762 local $SIG{TSTP} = 'IGNORE';
763 local $SIG{PIPE} = 'IGNORE';
765 my $oldAutoCommit = $FS::UID::AutoCommit;
766 local $FS::UID::AutoCommit = 0;
769 unless ( ref($prepay_credit) ) {
771 my $identifier = $prepay_credit;
773 $prepay_credit = qsearchs(
775 { 'identifier' => $prepay_credit },
780 unless ( $prepay_credit ) {
781 $dbh->rollback if $oldAutoCommit;
782 return "Invalid prepaid card: ". $identifier;
787 if ( $prepay_credit->agentnum ) {
788 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
789 $dbh->rollback if $oldAutoCommit;
790 return "prepaid card not valid for agent ". $self->agentnum;
792 $self->agentnum($prepay_credit->agentnum);
795 my $error = $prepay_credit->delete;
797 $dbh->rollback if $oldAutoCommit;
798 return "removing prepay_credit (transaction rolled back): $error";
801 ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
802 for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
804 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
809 =item increment_upbytes SECONDS
811 Updates this customer's single or primary account (see L<FS::svc_acct>) by
812 the specified number of upbytes. If there is an error, returns the error,
813 otherwise returns false.
817 sub increment_upbytes {
818 _increment_column( shift, 'upbytes', @_);
821 =item increment_downbytes SECONDS
823 Updates this customer's single or primary account (see L<FS::svc_acct>) by
824 the specified number of downbytes. If there is an error, returns the error,
825 otherwise returns false.
829 sub increment_downbytes {
830 _increment_column( shift, 'downbytes', @_);
833 =item increment_totalbytes SECONDS
835 Updates this customer's single or primary account (see L<FS::svc_acct>) by
836 the specified number of totalbytes. If there is an error, returns the error,
837 otherwise returns false.
841 sub increment_totalbytes {
842 _increment_column( shift, 'totalbytes', @_);
845 =item increment_seconds SECONDS
847 Updates this customer's single or primary account (see L<FS::svc_acct>) by
848 the specified number of seconds. If there is an error, returns the error,
849 otherwise returns false.
853 sub increment_seconds {
854 _increment_column( shift, 'seconds', @_);
857 =item _increment_column AMOUNT
859 Updates this customer's single or primary account (see L<FS::svc_acct>) by
860 the specified number of seconds or bytes. If there is an error, returns
861 the error, otherwise returns false.
865 sub _increment_column {
866 my( $self, $column, $amount ) = @_;
867 warn "$me increment_column called: $column, $amount\n"
870 return '' unless $amount;
872 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
873 $self->ncancelled_pkgs;
876 return 'No packages with primary or single services found'.
877 ' to apply pre-paid time';
878 } elsif ( scalar(@cust_pkg) > 1 ) {
879 #maybe have a way to specify the package/account?
880 return 'Multiple packages found to apply pre-paid time';
883 my $cust_pkg = $cust_pkg[0];
884 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
888 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
891 return 'No account found to apply pre-paid time';
892 } elsif ( scalar(@cust_svc) > 1 ) {
893 return 'Multiple accounts found to apply pre-paid time';
896 my $svc_acct = $cust_svc[0]->svc_x;
897 warn " found service svcnum ". $svc_acct->pkgnum.
898 ' ('. $svc_acct->email. ")\n"
901 $column = "increment_$column";
902 $svc_acct->$column($amount);
906 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
908 Inserts a prepayment in the specified amount for this customer. An optional
909 second argument can specify the prepayment identifier for tracking purposes.
910 If there is an error, returns the error, otherwise returns false.
914 sub insert_cust_pay_prepay {
915 shift->insert_cust_pay('PREP', @_);
918 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
920 Inserts a cash payment in the specified amount for this customer. An optional
921 second argument can specify the payment identifier for tracking purposes.
922 If there is an error, returns the error, otherwise returns false.
926 sub insert_cust_pay_cash {
927 shift->insert_cust_pay('CASH', @_);
930 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
932 Inserts a Western Union payment in the specified amount for this customer. An
933 optional second argument can specify the prepayment identifier for tracking
934 purposes. If there is an error, returns the error, otherwise returns false.
938 sub insert_cust_pay_west {
939 shift->insert_cust_pay('WEST', @_);
942 sub insert_cust_pay {
943 my( $self, $payby, $amount ) = splice(@_, 0, 3);
944 my $payinfo = scalar(@_) ? shift : '';
946 my $cust_pay = new FS::cust_pay {
947 'custnum' => $self->custnum,
948 'paid' => sprintf('%.2f', $amount),
949 #'_date' => #date the prepaid card was purchased???
951 'payinfo' => $payinfo,
959 This method is deprecated. See the I<depend_jobnum> option to the insert and
960 order_pkgs methods for a better way to defer provisioning.
962 Re-schedules all exports by calling the B<reexport> method of all associated
963 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
964 otherwise returns false.
971 carp "WARNING: FS::cust_main::reexport is deprectated; ".
972 "use the depend_jobnum option to insert or order_pkgs to delay export";
974 local $SIG{HUP} = 'IGNORE';
975 local $SIG{INT} = 'IGNORE';
976 local $SIG{QUIT} = 'IGNORE';
977 local $SIG{TERM} = 'IGNORE';
978 local $SIG{TSTP} = 'IGNORE';
979 local $SIG{PIPE} = 'IGNORE';
981 my $oldAutoCommit = $FS::UID::AutoCommit;
982 local $FS::UID::AutoCommit = 0;
985 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
986 my $error = $cust_pkg->reexport;
988 $dbh->rollback if $oldAutoCommit;
993 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
998 =item delete [ OPTION => VALUE ... ]
1000 This deletes the customer. If there is an error, returns the error, otherwise
1003 This will completely remove all traces of the customer record. This is not
1004 what you want when a customer cancels service; for that, cancel all of the
1005 customer's packages (see L</cancel>).
1007 If the customer has any uncancelled packages, you need to pass a new (valid)
1008 customer number for those packages to be transferred to, as the "new_customer"
1009 option. Cancelled packages will be deleted. Did I mention that this is NOT
1010 what you want when a customer cancels service and that you really should be
1011 looking at L<FS::cust_pkg/cancel>?
1013 You can't delete a customer with invoices (see L<FS::cust_bill>),
1014 statements (see L<FS::cust_statement>), credits (see L<FS::cust_credit>),
1015 payments (see L<FS::cust_pay>) or refunds (see L<FS::cust_refund>), unless you
1016 set the "delete_financials" option to a true value.
1021 my( $self, %opt ) = @_;
1023 local $SIG{HUP} = 'IGNORE';
1024 local $SIG{INT} = 'IGNORE';
1025 local $SIG{QUIT} = 'IGNORE';
1026 local $SIG{TERM} = 'IGNORE';
1027 local $SIG{TSTP} = 'IGNORE';
1028 local $SIG{PIPE} = 'IGNORE';
1030 my $oldAutoCommit = $FS::UID::AutoCommit;
1031 local $FS::UID::AutoCommit = 0;
1034 if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1035 $dbh->rollback if $oldAutoCommit;
1036 return "Can't delete a master agent customer";
1039 #use FS::access_user
1040 if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1041 $dbh->rollback if $oldAutoCommit;
1042 return "Can't delete a master employee customer";
1045 tie my %financial_tables, 'Tie::IxHash',
1046 'cust_bill' => 'invoices',
1047 'cust_statement' => 'statements',
1048 'cust_credit' => 'credits',
1049 'cust_pay' => 'payments',
1050 'cust_refund' => 'refunds',
1053 foreach my $table ( keys %financial_tables ) {
1055 my @records = $self->$table();
1057 if ( @records && ! $opt{'delete_financials'} ) {
1058 $dbh->rollback if $oldAutoCommit;
1059 return "Can't delete a customer with ". $financial_tables{$table};
1062 foreach my $record ( @records ) {
1063 my $error = $record->delete;
1065 $dbh->rollback if $oldAutoCommit;
1066 return "Error deleting ". $financial_tables{$table}. ": $error\n";
1072 my @cust_pkg = $self->ncancelled_pkgs;
1074 my $new_custnum = $opt{'new_custnum'};
1075 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1076 $dbh->rollback if $oldAutoCommit;
1077 return "Invalid new customer number: $new_custnum";
1079 foreach my $cust_pkg ( @cust_pkg ) {
1080 my %hash = $cust_pkg->hash;
1081 $hash{'custnum'} = $new_custnum;
1082 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1083 my $error = $new_cust_pkg->replace($cust_pkg,
1084 options => { $cust_pkg->options },
1087 $dbh->rollback if $oldAutoCommit;
1092 my @cancelled_cust_pkg = $self->all_pkgs;
1093 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1094 my $error = $cust_pkg->delete;
1096 $dbh->rollback if $oldAutoCommit;
1101 #cust_tax_adjustment in financials?
1102 #cust_pay_pending? ouch
1104 foreach my $table (qw(
1105 cust_main_invoice cust_main_exemption cust_tag cust_attachment contact
1106 cust_location cust_main_note cust_tax_adjustment
1107 cust_pay_void cust_pay_batch queue cust_tax_exempt
1109 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1110 my $error = $record->delete;
1112 $dbh->rollback if $oldAutoCommit;
1118 my $sth = $dbh->prepare(
1119 'UPDATE cust_main SET referral_custnum = NULL WHERE referral_custnum = ?'
1121 my $errstr = $dbh->errstr;
1122 $dbh->rollback if $oldAutoCommit;
1125 $sth->execute($self->custnum) or do {
1126 my $errstr = $sth->errstr;
1127 $dbh->rollback if $oldAutoCommit;
1133 my $ticket_dbh = '';
1134 if ($conf->config('ticket_system') eq 'RT_Internal') {
1136 } elsif ($conf->config('ticket_system') eq 'RT_External') {
1137 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1138 $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1139 #or die "RT_External DBI->connect error: $DBI::errstr\n";
1142 if ( $ticket_dbh ) {
1144 my $ticket_sth = $ticket_dbh->prepare(
1145 'DELETE FROM Links WHERE Target = ?'
1147 my $errstr = $ticket_dbh->errstr;
1148 $dbh->rollback if $oldAutoCommit;
1151 $ticket_sth->execute('freeside://freeside/cust_main/'.$self->custnum)
1153 my $errstr = $ticket_sth->errstr;
1154 $dbh->rollback if $oldAutoCommit;
1158 #check and see if the customer is the only link on the ticket, and
1159 #if so, set the ticket to deleted status in RT?
1160 #maybe someday, for now this will at least fix tickets not displaying
1164 #delete the customer record
1166 my $error = $self->SUPER::delete;
1168 $dbh->rollback if $oldAutoCommit;
1172 # cust_main exports!
1174 #my $export_args = $options{'export_args'} || [];
1177 map qsearch( 'part_export', {exportnum=>$_} ),
1178 $conf->config('cust_main-exports'); #, $agentnum
1180 foreach my $part_export ( @part_export ) {
1181 my $error = $part_export->export_delete( $self ); #, @$export_args);
1183 $dbh->rollback if $oldAutoCommit;
1184 return "exporting to ". $part_export->exporttype.
1185 " (transaction rolled back): $error";
1189 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1194 =item merge NEW_CUSTNUM [ , OPTION => VALUE ... ]
1196 This merges this customer into the provided new custnum, and then deletes the
1197 customer. If there is an error, returns the error, otherwise returns false.
1199 The source customer's name, company name, phone numbers, agent,
1200 referring customer, customer class, advertising source, order taker, and
1201 billing information (except balance) are discarded.
1203 All packages are moved to the target customer. Packages with package locations
1204 are preserved. Packages without package locations are moved to a new package
1205 location with the source customer's service/shipping address.
1207 All invoices, statements, payments, credits and refunds are moved to the target
1208 customer. The source customer's balance is added to the target customer.
1210 All notes, attachments, tickets and customer tags are moved to the target
1213 Change history is not currently moved.
1218 my( $self, $new_custnum, %opt ) = @_;
1220 return "Can't merge a customer into self" if $self->custnum == $new_custnum;
1222 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1223 return "Invalid new customer number: $new_custnum";
1226 local $SIG{HUP} = 'IGNORE';
1227 local $SIG{INT} = 'IGNORE';
1228 local $SIG{QUIT} = 'IGNORE';
1229 local $SIG{TERM} = 'IGNORE';
1230 local $SIG{TSTP} = 'IGNORE';
1231 local $SIG{PIPE} = 'IGNORE';
1233 my $oldAutoCommit = $FS::UID::AutoCommit;
1234 local $FS::UID::AutoCommit = 0;
1237 if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1238 $dbh->rollback if $oldAutoCommit;
1239 return "Can't merge a master agent customer";
1242 #use FS::access_user
1243 if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1244 $dbh->rollback if $oldAutoCommit;
1245 return "Can't merge a master employee customer";
1248 if ( qsearch('cust_pay_pending', { 'custnum' => $self->custnum,
1249 'status' => { op=>'!=', value=>'done' },
1253 $dbh->rollback if $oldAutoCommit;
1254 return "Can't merge a customer with pending payments";
1257 tie my %financial_tables, 'Tie::IxHash',
1258 'cust_bill' => 'invoices',
1259 'cust_statement' => 'statements',
1260 'cust_credit' => 'credits',
1261 'cust_pay' => 'payments',
1262 'cust_pay_void' => 'voided payments',
1263 'cust_refund' => 'refunds',
1266 foreach my $table ( keys %financial_tables ) {
1268 my @records = $self->$table();
1270 foreach my $record ( @records ) {
1271 $record->custnum($new_custnum);
1272 my $error = $record->replace;
1274 $dbh->rollback if $oldAutoCommit;
1275 return "Error merging ". $financial_tables{$table}. ": $error\n";
1281 my $name = $self->ship_name;
1283 my $locationnum = '';
1284 foreach my $cust_pkg ( $self->all_pkgs ) {
1285 $cust_pkg->custnum($new_custnum);
1287 unless ( $cust_pkg->locationnum ) {
1288 unless ( $locationnum ) {
1289 my $cust_location = new FS::cust_location {
1290 $self->location_hash,
1291 'custnum' => $new_custnum,
1293 my $error = $cust_location->insert;
1295 $dbh->rollback if $oldAutoCommit;
1298 $locationnum = $cust_location->locationnum;
1300 $cust_pkg->locationnum($locationnum);
1303 my $error = $cust_pkg->replace;
1305 $dbh->rollback if $oldAutoCommit;
1309 # add customer (ship) name to svc_phone.phone_name if blank
1310 my @cust_svc = $cust_pkg->cust_svc;
1311 foreach my $cust_svc (@cust_svc) {
1312 my($label, $value, $svcdb) = $cust_svc->label;
1313 next unless $svcdb eq 'svc_phone';
1314 my $svc_phone = $cust_svc->svc_x;
1315 next if $svc_phone->phone_name;
1316 $svc_phone->phone_name($name);
1317 my $error = $svc_phone->replace;
1319 $dbh->rollback if $oldAutoCommit;
1327 # cust_tax_exempt (texas tax exemptions)
1328 # cust_recon (some sort of not-well understood thing for OnPac)
1330 #these are moved over
1331 foreach my $table (qw(
1332 cust_tag cust_location contact cust_attachment cust_main_note
1333 cust_tax_adjustment cust_pay_batch queue
1335 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1336 $record->custnum($new_custnum);
1337 my $error = $record->replace;
1339 $dbh->rollback if $oldAutoCommit;
1345 #these aren't preserved
1346 foreach my $table (qw(
1347 cust_main_exemption cust_main_invoice
1349 foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1350 my $error = $record->delete;
1352 $dbh->rollback if $oldAutoCommit;
1359 my $sth = $dbh->prepare(
1360 'UPDATE cust_main SET referral_custnum = ? WHERE referral_custnum = ?'
1362 my $errstr = $dbh->errstr;
1363 $dbh->rollback if $oldAutoCommit;
1366 $sth->execute($new_custnum, $self->custnum) or do {
1367 my $errstr = $sth->errstr;
1368 $dbh->rollback if $oldAutoCommit;
1374 my $ticket_dbh = '';
1375 if ($conf->config('ticket_system') eq 'RT_Internal') {
1377 } elsif ($conf->config('ticket_system') eq 'RT_External') {
1378 my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1379 $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1380 #or die "RT_External DBI->connect error: $DBI::errstr\n";
1383 if ( $ticket_dbh ) {
1385 my $ticket_sth = $ticket_dbh->prepare(
1386 'UPDATE Links SET Target = ? WHERE Target = ?'
1388 my $errstr = $ticket_dbh->errstr;
1389 $dbh->rollback if $oldAutoCommit;
1392 $ticket_sth->execute('freeside://freeside/cust_main/'.$new_custnum,
1393 'freeside://freeside/cust_main/'.$self->custnum)
1395 my $errstr = $ticket_sth->errstr;
1396 $dbh->rollback if $oldAutoCommit;
1402 #delete the customer record
1404 my $error = $self->delete;
1406 $dbh->rollback if $oldAutoCommit;
1410 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1415 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
1418 Replaces the OLD_RECORD with this one in the database. If there is an error,
1419 returns the error, otherwise returns false.
1421 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1422 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1423 expected and rollback the entire transaction; it is not necessary to call
1424 check_invoicing_list first. Here's an example:
1426 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1428 Currently available options are: I<tax_exemption>.
1430 The I<tax_exemption> option can be set to an arrayref of tax names.
1431 FS::cust_main_exemption records will be deleted and inserted as appropriate.
1438 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1440 : $self->replace_old;
1444 warn "$me replace called\n"
1447 my $curuser = $FS::CurrentUser::CurrentUser;
1448 if ( $self->payby eq 'COMP'
1449 && $self->payby ne $old->payby
1450 && ! $curuser->access_right('Complimentary customer')
1453 return "You are not permitted to create complimentary accounts.";
1456 if ( $old->get('geocode') && $old->get('geocode') eq $self->get('geocode')
1457 && $conf->exists('enable_taxproducts')
1460 my $pre = ($conf->exists('tax-ship_address') && $self->ship_zip)
1462 $self->set('geocode', '')
1463 if $old->get($pre.'zip') ne $self->get($pre.'zip')
1464 && length($self->get($pre.'zip')) >= 10;
1467 local($ignore_expired_card) = 1
1468 if $old->payby =~ /^(CARD|DCRD)$/
1469 && $self->payby =~ /^(CARD|DCRD)$/
1470 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1472 local $SIG{HUP} = 'IGNORE';
1473 local $SIG{INT} = 'IGNORE';
1474 local $SIG{QUIT} = 'IGNORE';
1475 local $SIG{TERM} = 'IGNORE';
1476 local $SIG{TSTP} = 'IGNORE';
1477 local $SIG{PIPE} = 'IGNORE';
1479 my $oldAutoCommit = $FS::UID::AutoCommit;
1480 local $FS::UID::AutoCommit = 0;
1483 my $error = $self->SUPER::replace($old);
1486 $dbh->rollback if $oldAutoCommit;
1490 if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
1491 my $invoicing_list = shift @param;
1492 $error = $self->check_invoicing_list( $invoicing_list );
1494 $dbh->rollback if $oldAutoCommit;
1497 $self->invoicing_list( $invoicing_list );
1500 if ( $self->exists('tagnum') ) { #so we don't delete these on edit by accident
1502 #this could be more efficient than deleting and re-inserting, if it matters
1503 foreach my $cust_tag (qsearch('cust_tag', {'custnum'=>$self->custnum} )) {
1504 my $error = $cust_tag->delete;
1506 $dbh->rollback if $oldAutoCommit;
1510 foreach my $tagnum ( @{ $self->tagnum || [] } ) {
1511 my $cust_tag = new FS::cust_tag { 'tagnum' => $tagnum,
1512 'custnum' => $self->custnum };
1513 my $error = $cust_tag->insert;
1515 $dbh->rollback if $oldAutoCommit;
1522 my %options = @param;
1524 my $tax_exemption = delete $options{'tax_exemption'};
1525 if ( $tax_exemption ) {
1527 my %cust_main_exemption =
1528 map { $_->taxname => $_ }
1529 qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
1531 foreach my $taxname ( @$tax_exemption ) {
1533 next if delete $cust_main_exemption{$taxname};
1535 my $cust_main_exemption = new FS::cust_main_exemption {
1536 'custnum' => $self->custnum,
1537 'taxname' => $taxname,
1539 my $error = $cust_main_exemption->insert;
1541 $dbh->rollback if $oldAutoCommit;
1542 return "inserting cust_main_exemption (transaction rolled back): $error";
1546 foreach my $cust_main_exemption ( values %cust_main_exemption ) {
1547 my $error = $cust_main_exemption->delete;
1549 $dbh->rollback if $oldAutoCommit;
1550 return "deleting cust_main_exemption (transaction rolled back): $error";
1556 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/
1557 && ( ( $self->get('payinfo') ne $old->get('payinfo')
1558 && $self->get('payinfo') !~ /^99\d{14}$/
1560 || grep { $self->get($_) ne $old->get($_) } qw(paydate payname)
1565 # card/check/lec info has changed, want to retry realtime_ invoice events
1566 my $error = $self->retry_realtime;
1568 $dbh->rollback if $oldAutoCommit;
1573 unless ( $import || $skip_fuzzyfiles ) {
1574 $error = $self->queue_fuzzyfiles_update;
1576 $dbh->rollback if $oldAutoCommit;
1577 return "updating fuzzy search cache: $error";
1581 # cust_main exports!
1583 my $export_args = $options{'export_args'} || [];
1586 map qsearch( 'part_export', {exportnum=>$_} ),
1587 $conf->config('cust_main-exports'); #, $agentnum
1589 foreach my $part_export ( @part_export ) {
1590 my $error = $part_export->export_replace( $self, $old, @$export_args);
1592 $dbh->rollback if $oldAutoCommit;
1593 return "exporting to ". $part_export->exporttype.
1594 " (transaction rolled back): $error";
1598 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1603 =item queue_fuzzyfiles_update
1605 Used by insert & replace to update the fuzzy search cache
1609 sub queue_fuzzyfiles_update {
1612 local $SIG{HUP} = 'IGNORE';
1613 local $SIG{INT} = 'IGNORE';
1614 local $SIG{QUIT} = 'IGNORE';
1615 local $SIG{TERM} = 'IGNORE';
1616 local $SIG{TSTP} = 'IGNORE';
1617 local $SIG{PIPE} = 'IGNORE';
1619 my $oldAutoCommit = $FS::UID::AutoCommit;
1620 local $FS::UID::AutoCommit = 0;
1623 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1624 my $error = $queue->insert( map $self->getfield($_), @fuzzyfields );
1626 $dbh->rollback if $oldAutoCommit;
1627 return "queueing job (transaction rolled back): $error";
1630 if ( $self->ship_last ) {
1631 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1632 $error = $queue->insert( map $self->getfield("ship_$_"), @fuzzyfields );
1634 $dbh->rollback if $oldAutoCommit;
1635 return "queueing job (transaction rolled back): $error";
1639 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1646 Checks all fields to make sure this is a valid customer record. If there is
1647 an error, returns the error, otherwise returns false. Called by the insert
1648 and replace methods.
1655 warn "$me check BEFORE: \n". $self->_dump
1659 $self->ut_numbern('custnum')
1660 || $self->ut_number('agentnum')
1661 || $self->ut_textn('agent_custid')
1662 || $self->ut_number('refnum')
1663 || $self->ut_foreign_keyn('classnum', 'cust_class', 'classnum')
1664 || $self->ut_textn('custbatch')
1665 || $self->ut_name('last')
1666 || $self->ut_name('first')
1667 || $self->ut_snumbern('birthdate')
1668 || $self->ut_snumbern('signupdate')
1669 || $self->ut_textn('company')
1670 || $self->ut_text('address1')
1671 || $self->ut_textn('address2')
1672 || $self->ut_text('city')
1673 || $self->ut_textn('county')
1674 || $self->ut_textn('state')
1675 || $self->ut_country('country')
1676 || $self->ut_anything('comments')
1677 || $self->ut_numbern('referral_custnum')
1678 || $self->ut_textn('stateid')
1679 || $self->ut_textn('stateid_state')
1680 || $self->ut_textn('invoice_terms')
1681 || $self->ut_alphan('geocode')
1682 || $self->ut_floatn('cdr_termination_percentage')
1683 || $self->ut_floatn('credit_limit')
1686 #barf. need message catalogs. i18n. etc.
1687 $error .= "Please select an advertising source."
1688 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1689 return $error if $error;
1691 return "Unknown agent"
1692 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1694 return "Unknown refnum"
1695 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1697 return "Unknown referring custnum: ". $self->referral_custnum
1698 unless ! $self->referral_custnum
1699 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1701 if ( $self->censustract ne '' ) {
1702 $self->censustract =~ /^\s*(\d{9})\.?(\d{2})\s*$/
1703 or return "Illegal census tract: ". $self->censustract;
1705 $self->censustract("$1.$2");
1708 if ( $self->ss eq '' ) {
1713 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1714 or return "Illegal social security number: ". $self->ss;
1715 $self->ss("$1-$2-$3");
1719 # bad idea to disable, causes billing to fail because of no tax rates later
1720 # except we don't fail any more
1721 unless ( $import ) {
1722 unless ( qsearch('cust_main_county', {
1723 'country' => $self->country,
1726 return "Unknown state/county/country: ".
1727 $self->state. "/". $self->county. "/". $self->country
1728 unless qsearch('cust_main_county',{
1729 'state' => $self->state,
1730 'county' => $self->county,
1731 'country' => $self->country,
1737 $self->ut_phonen('daytime', $self->country)
1738 || $self->ut_phonen('night', $self->country)
1739 || $self->ut_phonen('fax', $self->country)
1741 return $error if $error;
1743 unless ( $ignore_illegal_zip ) {
1744 $error = $self->ut_zip('zip', $self->country);
1745 return $error if $error;
1748 if ( $conf->exists('cust_main-require_phone')
1749 && ! length($self->daytime) && ! length($self->night)
1752 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1754 : FS::Msgcat::_gettext('daytime');
1755 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1757 : FS::Msgcat::_gettext('night');
1759 return "$daytime_label or $night_label is required"
1763 if ( $self->has_ship_address
1764 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1765 $self->addr_fields )
1769 $self->ut_name('ship_last')
1770 || $self->ut_name('ship_first')
1771 || $self->ut_textn('ship_company')
1772 || $self->ut_text('ship_address1')
1773 || $self->ut_textn('ship_address2')
1774 || $self->ut_text('ship_city')
1775 || $self->ut_textn('ship_county')
1776 || $self->ut_textn('ship_state')
1777 || $self->ut_country('ship_country')
1779 return $error if $error;
1781 #false laziness with above
1782 unless ( qsearchs('cust_main_county', {
1783 'country' => $self->ship_country,
1786 return "Unknown ship_state/ship_county/ship_country: ".
1787 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1788 unless qsearch('cust_main_county',{
1789 'state' => $self->ship_state,
1790 'county' => $self->ship_county,
1791 'country' => $self->ship_country,
1797 $self->ut_phonen('ship_daytime', $self->ship_country)
1798 || $self->ut_phonen('ship_night', $self->ship_country)
1799 || $self->ut_phonen('ship_fax', $self->ship_country)
1801 return $error if $error;
1803 unless ( $ignore_illegal_zip ) {
1804 $error = $self->ut_zip('ship_zip', $self->ship_country);
1805 return $error if $error;
1807 return "Unit # is required."
1808 if $self->ship_address2 =~ /^\s*$/
1809 && $conf->exists('cust_main-require_address2');
1811 } else { # ship_ info eq billing info, so don't store dup info in database
1813 $self->setfield("ship_$_", '')
1814 foreach $self->addr_fields;
1816 return "Unit # is required."
1817 if $self->address2 =~ /^\s*$/
1818 && $conf->exists('cust_main-require_address2');
1822 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1823 # or return "Illegal payby: ". $self->payby;
1825 FS::payby->can_payby($self->table, $self->payby)
1826 or return "Illegal payby: ". $self->payby;
1828 $error = $self->ut_numbern('paystart_month')
1829 || $self->ut_numbern('paystart_year')
1830 || $self->ut_numbern('payissue')
1831 || $self->ut_textn('paytype')
1833 return $error if $error;
1835 if ( $self->payip eq '' ) {
1838 $error = $self->ut_ip('payip');
1839 return $error if $error;
1842 # If it is encrypted and the private key is not availaible then we can't
1843 # check the credit card.
1844 my $check_payinfo = ! $self->is_encrypted($self->payinfo);
1846 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1848 my $payinfo = $self->payinfo;
1849 $payinfo =~ s/\D//g;
1850 $payinfo =~ /^(\d{13,16})$/
1851 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1853 $self->payinfo($payinfo);
1855 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1857 return gettext('unknown_card_type')
1858 if $self->payinfo !~ /^99\d{14}$/ #token
1859 && cardtype($self->payinfo) eq "Unknown";
1861 unless ( $ignore_banned_card ) {
1862 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1864 return 'Banned credit card: banned on '.
1865 time2str('%a %h %o at %r', $ban->_date).
1866 ' by '. $ban->otaker.
1867 ' (ban# '. $ban->bannum. ')';
1871 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1872 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1873 $self->paycvv =~ /^(\d{4})$/
1874 or return "CVV2 (CID) for American Express cards is four digits.";
1877 $self->paycvv =~ /^(\d{3})$/
1878 or return "CVV2 (CVC2/CID) is three digits.";
1885 my $cardtype = cardtype($payinfo);
1886 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1888 return "Start date or issue number is required for $cardtype cards"
1889 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1891 return "Start month must be between 1 and 12"
1892 if $self->paystart_month
1893 and $self->paystart_month < 1 || $self->paystart_month > 12;
1895 return "Start year must be 1990 or later"
1896 if $self->paystart_year
1897 and $self->paystart_year < 1990;
1899 return "Issue number must be beween 1 and 99"
1901 and $self->payissue < 1 || $self->payissue > 99;
1904 $self->paystart_month('');
1905 $self->paystart_year('');
1906 $self->payissue('');
1909 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1911 my $payinfo = $self->payinfo;
1912 $payinfo =~ s/[^\d\@]//g;
1913 if ( $conf->exists('echeck-nonus') ) {
1914 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1915 $payinfo = "$1\@$2";
1917 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1918 $payinfo = "$1\@$2";
1920 $self->payinfo($payinfo);
1923 unless ( $ignore_banned_card ) {
1924 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1926 return 'Banned ACH account: banned on '.
1927 time2str('%a %h %o at %r', $ban->_date).
1928 ' by '. $ban->otaker.
1929 ' (ban# '. $ban->bannum. ')';
1933 } elsif ( $self->payby eq 'LECB' ) {
1935 my $payinfo = $self->payinfo;
1936 $payinfo =~ s/\D//g;
1937 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1939 $self->payinfo($payinfo);
1942 } elsif ( $self->payby eq 'BILL' ) {
1944 $error = $self->ut_textn('payinfo');
1945 return "Illegal P.O. number: ". $self->payinfo if $error;
1948 } elsif ( $self->payby eq 'COMP' ) {
1950 my $curuser = $FS::CurrentUser::CurrentUser;
1951 if ( ! $self->custnum
1952 && ! $curuser->access_right('Complimentary customer')
1955 return "You are not permitted to create complimentary accounts."
1958 $error = $self->ut_textn('payinfo');
1959 return "Illegal comp account issuer: ". $self->payinfo if $error;
1962 } elsif ( $self->payby eq 'PREPAY' ) {
1964 my $payinfo = $self->payinfo;
1965 $payinfo =~ s/\W//g; #anything else would just confuse things
1966 $self->payinfo($payinfo);
1967 $error = $self->ut_alpha('payinfo');
1968 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1969 return "Unknown prepayment identifier"
1970 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1975 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1976 return "Expiration date required"
1977 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1981 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1982 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1983 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1984 ( $m, $y ) = ( $2, "19$1" );
1985 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1986 ( $m, $y ) = ( $3, "20$2" );
1988 return "Illegal expiration date: ". $self->paydate;
1990 $m = sprintf('%02d',$m);
1991 $self->paydate("$y-$m-01");
1992 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1993 return gettext('expired_card')
1995 && !$ignore_expired_card
1996 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1999 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
2000 ( ! $conf->exists('require_cardname')
2001 || $self->payby !~ /^(CARD|DCRD)$/ )
2003 $self->payname( $self->first. " ". $self->getfield('last') );
2005 $self->payname =~ /^([µ_0123456789aAáÁàÀâÂåÅäÄãêæÆbBcCçÇdDðÐeEéÉèÈêÊëËfFgGhHiIíÍìÌîÎïÏjJkKlLmMnNñÑoOóÓòÒôÔöÖõÕøغpPqQrRsSßtTuUúÚùÙûÛüÜvVwWxXyYýÝÿzZþÞ \,\.\-\'\&]+)$/
2006 or return gettext('illegal_name'). " payname: ". $self->payname;
2010 foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
2011 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
2015 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
2017 warn "$me check AFTER: \n". $self->_dump
2020 $self->SUPER::check;
2025 Returns a list of fields which have ship_ duplicates.
2030 qw( last first company
2031 address1 address2 city county state zip country
2036 =item has_ship_address
2038 Returns true if this customer record has a separate shipping address.
2042 sub has_ship_address {
2044 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
2049 Returns a list of key/value pairs, with the following keys: address1, adddress2,
2050 city, county, state, zip, country, and geocode. The shipping address is used if present.
2056 Returns all locations (see L<FS::cust_location>) for this customer.
2062 qsearch('cust_location', { 'custnum' => $self->custnum } );
2067 Returns all contacts (see L<FS::contact>) for this customer.
2071 #already used :/ sub contact {
2074 qsearch('contact', { 'custnum' => $self->custnum } );
2079 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
2080 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
2081 on success or a list of errors.
2087 grep { $_->unsuspend } $self->suspended_pkgs;
2092 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
2094 Returns a list: an empty list on success or a list of errors.
2100 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
2103 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2105 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
2106 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
2107 of a list of pkgparts; the hashref has the following keys:
2111 =item pkgparts - listref of pkgparts
2113 =item (other options are passed to the suspend method)
2118 Returns a list: an empty list on success or a list of errors.
2122 sub suspend_if_pkgpart {
2124 my (@pkgparts, %opt);
2125 if (ref($_[0]) eq 'HASH'){
2126 @pkgparts = @{$_[0]{pkgparts}};
2131 grep { $_->suspend(%opt) }
2132 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
2133 $self->unsuspended_pkgs;
2136 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2138 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
2139 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
2140 instead of a list of pkgparts; the hashref has the following keys:
2144 =item pkgparts - listref of pkgparts
2146 =item (other options are passed to the suspend method)
2150 Returns a list: an empty list on success or a list of errors.
2154 sub suspend_unless_pkgpart {
2156 my (@pkgparts, %opt);
2157 if (ref($_[0]) eq 'HASH'){
2158 @pkgparts = @{$_[0]{pkgparts}};
2163 grep { $_->suspend(%opt) }
2164 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
2165 $self->unsuspended_pkgs;
2168 =item cancel [ OPTION => VALUE ... ]
2170 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
2172 Available options are:
2176 =item quiet - can be set true to supress email cancellation notices.
2178 =item reason - can be set to a cancellation reason (see L<FS:reason>), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see L<FS::reason_type>, reason - Text of the new reason.
2180 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
2182 =item nobill - can be set true to skip billing if it might otherwise be done.
2186 Always returns a list: an empty list on success or a list of errors.
2190 # nb that dates are not specified as valid options to this method
2193 my( $self, %opt ) = @_;
2195 warn "$me cancel called on customer ". $self->custnum. " with options ".
2196 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
2199 return ( 'access denied' )
2200 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
2202 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
2204 #should try decryption (we might have the private key)
2205 # and if not maybe queue a job for the server that does?
2206 return ( "Can't (yet) ban encrypted credit cards" )
2207 if $self->is_encrypted($self->payinfo);
2209 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
2210 my $error = $ban->insert;
2211 return ( $error ) if $error;
2215 my @pkgs = $self->ncancelled_pkgs;
2217 if ( !$opt{nobill} && $conf->exists('bill_usage_on_cancel') ) {
2219 my $error = $self->bill( pkg_list => [ @pkgs ], cancel => 1 );
2220 warn "Error billing during cancel, custnum ". $self->custnum. ": $error"
2224 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
2225 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
2228 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
2231 sub _banned_pay_hashref {
2242 'payby' => $payby2ban{$self->payby},
2243 'payinfo' => md5_base64($self->payinfo),
2244 #don't ever *search* on reason! #'reason' =>
2250 Returns all notes (see L<FS::cust_main_note>) for this customer.
2255 my($self,$orderby_classnum) = (shift,shift);
2256 my $orderby = "_DATE DESC";
2257 $orderby = "CLASSNUM ASC, $orderby" if $orderby_classnum;
2258 qsearch( 'cust_main_note',
2259 { 'custnum' => $self->custnum },
2261 "ORDER BY $orderby",
2267 Returns the agent (see L<FS::agent>) for this customer.
2273 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
2278 Returns the agent name (see L<FS::agent>) for this customer.
2284 $self->agent->agent;
2289 Returns any tags associated with this customer, as FS::cust_tag objects,
2290 or an empty list if there are no tags.
2296 qsearch('cust_tag', { 'custnum' => $self->custnum } );
2301 Returns any tags associated with this customer, as FS::part_tag objects,
2302 or an empty list if there are no tags.
2308 map $_->part_tag, $self->cust_tag;
2314 Returns the customer class, as an FS::cust_class object, or the empty string
2315 if there is no customer class.
2321 if ( $self->classnum ) {
2322 qsearchs('cust_class', { 'classnum' => $self->classnum } );
2330 Returns the customer category name, or the empty string if there is no customer
2337 my $cust_class = $self->cust_class;
2339 ? $cust_class->categoryname
2345 Returns the customer class name, or the empty string if there is no customer
2352 my $cust_class = $self->cust_class;
2354 ? $cust_class->classname
2358 =item BILLING METHODS
2360 Documentation on billing methods has been moved to
2361 L<FS::cust_main::Billing>.
2363 =item REALTIME BILLING METHODS
2365 Documentation on realtime billing methods has been moved to
2366 L<FS::cust_main::Billing_Realtime>.
2370 Removes the I<paycvv> field from the database directly.
2372 If there is an error, returns the error, otherwise returns false.
2378 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2379 or return dbh->errstr;
2380 $sth->execute($self->custnum)
2381 or return $sth->errstr;
2386 =item batch_card OPTION => VALUE...
2388 Adds a payment for this invoice to the pending credit card batch (see
2389 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2390 runs the payment using a realtime gateway.
2395 my ($self, %options) = @_;
2398 if (exists($options{amount})) {
2399 $amount = $options{amount};
2401 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
2403 return '' unless $amount > 0;
2405 my $invnum = delete $options{invnum};
2406 my $payby = $options{payby} || $self->payby; #still dubious
2408 if ($options{'realtime'}) {
2409 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
2415 my $oldAutoCommit = $FS::UID::AutoCommit;
2416 local $FS::UID::AutoCommit = 0;
2419 #this needs to handle mysql as well as Pg, like svc_acct.pm
2420 #(make it into a common function if folks need to do batching with mysql)
2421 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
2422 or return "Cannot lock pay_batch: " . $dbh->errstr;
2426 'payby' => FS::payby->payby2payment($payby),
2429 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
2431 unless ( $pay_batch ) {
2432 $pay_batch = new FS::pay_batch \%pay_batch;
2433 my $error = $pay_batch->insert;
2435 $dbh->rollback if $oldAutoCommit;
2436 die "error creating new batch: $error\n";
2440 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
2441 'batchnum' => $pay_batch->batchnum,
2442 'custnum' => $self->custnum,
2445 foreach (qw( address1 address2 city state zip country payby payinfo paydate
2447 $options{$_} = '' unless exists($options{$_});
2450 my $cust_pay_batch = new FS::cust_pay_batch ( {
2451 'batchnum' => $pay_batch->batchnum,
2452 'invnum' => $invnum || 0, # is there a better value?
2453 # this field should be
2455 # cust_bill_pay_batch now
2456 'custnum' => $self->custnum,
2457 'last' => $self->getfield('last'),
2458 'first' => $self->getfield('first'),
2459 'address1' => $options{address1} || $self->address1,
2460 'address2' => $options{address2} || $self->address2,
2461 'city' => $options{city} || $self->city,
2462 'state' => $options{state} || $self->state,
2463 'zip' => $options{zip} || $self->zip,
2464 'country' => $options{country} || $self->country,
2465 'payby' => $options{payby} || $self->payby,
2466 'payinfo' => $options{payinfo} || $self->payinfo,
2467 'exp' => $options{paydate} || $self->paydate,
2468 'payname' => $options{payname} || $self->payname,
2469 'amount' => $amount, # consolidating
2472 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
2473 if $old_cust_pay_batch;
2476 if ($old_cust_pay_batch) {
2477 $error = $cust_pay_batch->replace($old_cust_pay_batch)
2479 $error = $cust_pay_batch->insert;
2483 $dbh->rollback if $oldAutoCommit;
2487 my $unapplied = $self->total_unapplied_credits
2488 + $self->total_unapplied_payments
2489 + $self->in_transit_payments;
2490 foreach my $cust_bill ($self->open_cust_bill) {
2491 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
2492 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
2493 'invnum' => $cust_bill->invnum,
2494 'paybatchnum' => $cust_pay_batch->paybatchnum,
2495 'amount' => $cust_bill->owed,
2498 if ($unapplied >= $cust_bill_pay_batch->amount){
2499 $unapplied -= $cust_bill_pay_batch->amount;
2502 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
2503 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
2505 $error = $cust_bill_pay_batch->insert;
2507 $dbh->rollback if $oldAutoCommit;
2512 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2518 Returns the total owed for this customer on all invoices
2519 (see L<FS::cust_bill/owed>).
2525 $self->total_owed_date(2145859200); #12/31/2037
2528 =item total_owed_date TIME
2530 Returns the total owed for this customer on all invoices with date earlier than
2531 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2532 see L<Time::Local> and L<Date::Parse> for conversion functions.
2536 sub total_owed_date {
2540 my $custnum = $self->custnum;
2542 my $owed_sql = FS::cust_bill->owed_sql;
2545 SELECT SUM($owed_sql) FROM cust_bill
2546 WHERE custnum = $custnum
2550 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2554 =item total_owed_pkgnum PKGNUM
2556 Returns the total owed on all invoices for this customer's specific package
2557 when using experimental package balances (see L<FS::cust_bill/owed_pkgnum>).
2561 sub total_owed_pkgnum {
2562 my( $self, $pkgnum ) = @_;
2563 $self->total_owed_date_pkgnum(2145859200, $pkgnum); #12/31/2037
2566 =item total_owed_date_pkgnum TIME PKGNUM
2568 Returns the total owed for this customer's specific package when using
2569 experimental package balances on all invoices with date earlier than
2570 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
2571 see L<Time::Local> and L<Date::Parse> for conversion functions.
2575 sub total_owed_date_pkgnum {
2576 my( $self, $time, $pkgnum ) = @_;
2579 foreach my $cust_bill (
2580 grep { $_->_date <= $time }
2581 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2583 $total_bill += $cust_bill->owed_pkgnum($pkgnum);
2585 sprintf( "%.2f", $total_bill );
2591 Returns the total amount of all payments.
2598 $total += $_->paid foreach $self->cust_pay;
2599 sprintf( "%.2f", $total );
2602 =item total_unapplied_credits
2604 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2605 customer. See L<FS::cust_credit/credited>.
2607 =item total_credited
2609 Old name for total_unapplied_credits. Don't use.
2613 sub total_credited {
2614 #carp "total_credited deprecated, use total_unapplied_credits";
2615 shift->total_unapplied_credits(@_);
2618 sub total_unapplied_credits {
2621 my $custnum = $self->custnum;
2623 my $unapplied_sql = FS::cust_credit->unapplied_sql;
2626 SELECT SUM($unapplied_sql) FROM cust_credit
2627 WHERE custnum = $custnum
2630 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2634 =item total_unapplied_credits_pkgnum PKGNUM
2636 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2637 customer. See L<FS::cust_credit/credited>.
2641 sub total_unapplied_credits_pkgnum {
2642 my( $self, $pkgnum ) = @_;
2643 my $total_credit = 0;
2644 $total_credit += $_->credited foreach $self->cust_credit_pkgnum($pkgnum);
2645 sprintf( "%.2f", $total_credit );
2649 =item total_unapplied_payments
2651 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
2652 See L<FS::cust_pay/unapplied>.
2656 sub total_unapplied_payments {
2659 my $custnum = $self->custnum;
2661 my $unapplied_sql = FS::cust_pay->unapplied_sql;
2664 SELECT SUM($unapplied_sql) FROM cust_pay
2665 WHERE custnum = $custnum
2668 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2672 =item total_unapplied_payments_pkgnum PKGNUM
2674 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer's
2675 specific package when using experimental package balances. See
2676 L<FS::cust_pay/unapplied>.
2680 sub total_unapplied_payments_pkgnum {
2681 my( $self, $pkgnum ) = @_;
2682 my $total_unapplied = 0;
2683 $total_unapplied += $_->unapplied foreach $self->cust_pay_pkgnum($pkgnum);
2684 sprintf( "%.2f", $total_unapplied );
2688 =item total_unapplied_refunds
2690 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
2691 customer. See L<FS::cust_refund/unapplied>.
2695 sub total_unapplied_refunds {
2697 my $custnum = $self->custnum;
2699 my $unapplied_sql = FS::cust_refund->unapplied_sql;
2702 SELECT SUM($unapplied_sql) FROM cust_refund
2703 WHERE custnum = $custnum
2706 sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2712 Returns the balance for this customer (total_owed plus total_unrefunded, minus
2713 total_unapplied_credits minus total_unapplied_payments).
2719 $self->balance_date_range;
2722 =item balance_date TIME
2724 Returns the balance for this customer, only considering invoices with date
2725 earlier than TIME (total_owed_date minus total_credited minus
2726 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
2727 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
2734 $self->balance_date_range(shift);
2737 =item balance_date_range [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
2739 Returns the balance for this customer, optionally considering invoices with
2740 date earlier than START_TIME, and not later than END_TIME
2741 (total_owed_date minus total_unapplied_credits minus total_unapplied_payments).
2743 Times are specified as SQL fragments or numeric
2744 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
2745 L<Date::Parse> for conversion functions. The empty string can be passed
2746 to disable that time constraint completely.
2748 Available options are:
2752 =item unapplied_date
2754 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)
2760 sub balance_date_range {
2762 my $sql = 'SELECT SUM('. $self->balance_date_sql(@_).
2763 ') FROM cust_main WHERE custnum='. $self->custnum;
2764 sprintf( '%.2f', $self->scalar_sql($sql) || 0 );
2767 =item balance_pkgnum PKGNUM
2769 Returns the balance for this customer's specific package when using
2770 experimental package balances (total_owed plus total_unrefunded, minus
2771 total_unapplied_credits minus total_unapplied_payments)
2775 sub balance_pkgnum {
2776 my( $self, $pkgnum ) = @_;
2779 $self->total_owed_pkgnum($pkgnum)
2780 # n/a - refunds aren't part of pkg-balances since they don't apply to invoices
2781 # + $self->total_unapplied_refunds_pkgnum($pkgnum)
2782 - $self->total_unapplied_credits_pkgnum($pkgnum)
2783 - $self->total_unapplied_payments_pkgnum($pkgnum)
2787 =item in_transit_payments
2789 Returns the total of requests for payments for this customer pending in
2790 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
2794 sub in_transit_payments {
2796 my $in_transit_payments = 0;
2797 foreach my $pay_batch ( qsearch('pay_batch', {
2800 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
2801 'batchnum' => $pay_batch->batchnum,
2802 'custnum' => $self->custnum,
2804 $in_transit_payments += $cust_pay_batch->amount;
2807 sprintf( "%.2f", $in_transit_payments );
2812 Returns a hash of useful information for making a payment.
2822 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
2823 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
2824 'LECB' (Phone bill billing), 'BILL' (billing), or 'COMP' (free).
2828 For credit card transactions:
2840 For electronic check transactions:
2855 $return{balance} = $self->balance;
2857 $return{payname} = $self->payname
2858 || ( $self->first. ' '. $self->get('last') );
2860 $return{$_} = $self->get($_) for qw(address1 address2 city state zip);
2862 $return{payby} = $self->payby;
2863 $return{stateid_state} = $self->stateid_state;
2865 if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
2866 $return{card_type} = cardtype($self->payinfo);
2867 $return{payinfo} = $self->paymask;
2869 @return{'month', 'year'} = $self->paydate_monthyear;
2873 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
2874 my ($payinfo1, $payinfo2) = split '@', $self->paymask;
2875 $return{payinfo1} = $payinfo1;
2876 $return{payinfo2} = $payinfo2;
2877 $return{paytype} = $self->paytype;
2878 $return{paystate} = $self->paystate;
2882 #doubleclick protection
2884 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
2890 =item paydate_monthyear
2892 Returns a two-element list consisting of the month and year of this customer's
2893 paydate (credit card expiration date for CARD customers)
2897 sub paydate_monthyear {
2899 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
2901 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
2908 =item tax_exemption TAXNAME
2913 my( $self, $taxname ) = @_;
2915 qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
2916 'taxname' => $taxname,
2921 =item cust_main_exemption
2925 sub cust_main_exemption {
2927 qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
2930 =item invoicing_list [ ARRAYREF ]
2932 If an arguement is given, sets these email addresses as invoice recipients
2933 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
2934 (except as warnings), so use check_invoicing_list first.
2936 Returns a list of email addresses (with svcnum entries expanded).
2938 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
2939 check it without disturbing anything by passing nothing.
2941 This interface may change in the future.
2945 sub invoicing_list {
2946 my( $self, $arrayref ) = @_;
2949 my @cust_main_invoice;
2950 if ( $self->custnum ) {
2951 @cust_main_invoice =
2952 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2954 @cust_main_invoice = ();
2956 foreach my $cust_main_invoice ( @cust_main_invoice ) {
2957 #warn $cust_main_invoice->destnum;
2958 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
2959 #warn $cust_main_invoice->destnum;
2960 my $error = $cust_main_invoice->delete;
2961 warn $error if $error;
2964 if ( $self->custnum ) {
2965 @cust_main_invoice =
2966 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2968 @cust_main_invoice = ();
2970 my %seen = map { $_->address => 1 } @cust_main_invoice;
2971 foreach my $address ( @{$arrayref} ) {
2972 next if exists $seen{$address} && $seen{$address};
2973 $seen{$address} = 1;
2974 my $cust_main_invoice = new FS::cust_main_invoice ( {
2975 'custnum' => $self->custnum,
2978 my $error = $cust_main_invoice->insert;
2979 warn $error if $error;
2983 if ( $self->custnum ) {
2985 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2992 =item check_invoicing_list ARRAYREF
2994 Checks these arguements as valid input for the invoicing_list method. If there
2995 is an error, returns the error, otherwise returns false.
2999 sub check_invoicing_list {
3000 my( $self, $arrayref ) = @_;
3002 foreach my $address ( @$arrayref ) {
3004 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3005 return 'Can\'t add FAX invoice destination with a blank FAX number.';
3008 my $cust_main_invoice = new FS::cust_main_invoice ( {
3009 'custnum' => $self->custnum,
3012 my $error = $self->custnum
3013 ? $cust_main_invoice->check
3014 : $cust_main_invoice->checkdest
3016 return $error if $error;
3020 return "Email address required"
3021 if $conf->exists('cust_main-require_invoicing_list_email')
3022 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
3027 =item set_default_invoicing_list
3029 Sets the invoicing list to all accounts associated with this customer,
3030 overwriting any previous invoicing list.
3034 sub set_default_invoicing_list {
3036 $self->invoicing_list($self->all_emails);
3041 Returns the email addresses of all accounts provisioned for this customer.
3048 foreach my $cust_pkg ( $self->all_pkgs ) {
3049 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
3051 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3052 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3054 $list{$_}=1 foreach map { $_->email } @svc_acct;
3059 =item invoicing_list_addpost
3061 Adds postal invoicing to this customer. If this customer is already configured
3062 to receive postal invoices, does nothing.
3066 sub invoicing_list_addpost {
3068 return if grep { $_ eq 'POST' } $self->invoicing_list;
3069 my @invoicing_list = $self->invoicing_list;
3070 push @invoicing_list, 'POST';
3071 $self->invoicing_list(\@invoicing_list);
3074 =item invoicing_list_emailonly
3076 Returns the list of email invoice recipients (invoicing_list without non-email
3077 destinations such as POST and FAX).
3081 sub invoicing_list_emailonly {
3083 warn "$me invoicing_list_emailonly called"
3085 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
3088 =item invoicing_list_emailonly_scalar
3090 Returns the list of email invoice recipients (invoicing_list without non-email
3091 destinations such as POST and FAX) as a comma-separated scalar.
3095 sub invoicing_list_emailonly_scalar {
3097 warn "$me invoicing_list_emailonly_scalar called"
3099 join(', ', $self->invoicing_list_emailonly);
3102 =item referral_custnum_cust_main
3104 Returns the customer who referred this customer (or the empty string, if
3105 this customer was not referred).
3107 Note the difference with referral_cust_main method: This method,
3108 referral_custnum_cust_main returns the single customer (if any) who referred
3109 this customer, while referral_cust_main returns an array of customers referred
3114 sub referral_custnum_cust_main {
3116 return '' unless $self->referral_custnum;
3117 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3120 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
3122 Returns an array of customers referred by this customer (referral_custnum set
3123 to this custnum). If DEPTH is given, recurses up to the given depth, returning
3124 customers referred by customers referred by this customer and so on, inclusive.
3125 The default behavior is DEPTH 1 (no recursion).
3127 Note the difference with referral_custnum_cust_main method: This method,
3128 referral_cust_main, returns an array of customers referred BY this customer,
3129 while referral_custnum_cust_main returns the single customer (if any) who
3130 referred this customer.
3134 sub referral_cust_main {
3136 my $depth = @_ ? shift : 1;
3137 my $exclude = @_ ? shift : {};
3140 map { $exclude->{$_->custnum}++; $_; }
3141 grep { ! $exclude->{ $_->custnum } }
3142 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
3146 map { $_->referral_cust_main($depth-1, $exclude) }
3153 =item referral_cust_main_ncancelled
3155 Same as referral_cust_main, except only returns customers with uncancelled
3160 sub referral_cust_main_ncancelled {
3162 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
3165 =item referral_cust_pkg [ DEPTH ]
3167 Like referral_cust_main, except returns a flat list of all unsuspended (and
3168 uncancelled) packages for each customer. The number of items in this list may
3169 be useful for commission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
3173 sub referral_cust_pkg {
3175 my $depth = @_ ? shift : 1;
3177 map { $_->unsuspended_pkgs }
3178 grep { $_->unsuspended_pkgs }
3179 $self->referral_cust_main($depth);
3182 =item referring_cust_main
3184 Returns the single cust_main record for the customer who referred this customer
3185 (referral_custnum), or false.
3189 sub referring_cust_main {
3191 return '' unless $self->referral_custnum;
3192 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3195 =item credit AMOUNT, REASON [ , OPTION => VALUE ... ]
3197 Applies a credit to this customer. If there is an error, returns the error,
3198 otherwise returns false.
3200 REASON can be a text string, an FS::reason object, or a scalar reference to
3201 a reasonnum. If a text string, it will be automatically inserted as a new
3202 reason, and a 'reason_type' option must be passed to indicate the
3203 FS::reason_type for the new reason.
3205 An I<addlinfo> option may be passed to set the credit's I<addlinfo> field.
3207 Any other options are passed to FS::cust_credit::insert.
3212 my( $self, $amount, $reason, %options ) = @_;
3214 my $cust_credit = new FS::cust_credit {
3215 'custnum' => $self->custnum,
3216 'amount' => $amount,
3219 if ( ref($reason) ) {
3221 if ( ref($reason) eq 'SCALAR' ) {
3222 $cust_credit->reasonnum( $$reason );
3224 $cust_credit->reasonnum( $reason->reasonnum );
3228 $cust_credit->set('reason', $reason)
3231 for (qw( addlinfo eventnum )) {
3232 $cust_credit->$_( delete $options{$_} )
3233 if exists($options{$_});
3236 $cust_credit->insert(%options);
3240 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3242 Creates a one-time charge for this customer. If there is an error, returns
3243 the error, otherwise returns false.
3245 New-style, with a hashref of options:
3247 my $error = $cust_main->charge(
3251 'start_date' => str2time('7/4/2009'),
3252 'pkg' => 'Description',
3253 'comment' => 'Comment',
3254 'additional' => [], #extra invoice detail
3255 'classnum' => 1, #pkg_class
3257 'setuptax' => '', # or 'Y' for tax exempt
3260 'taxclass' => 'Tax class',
3263 'taxproduct' => 2, #part_pkg_taxproduct
3264 'override' => {}, #XXX describe
3266 #will be filled in with the new object
3267 'cust_pkg_ref' => \$cust_pkg,
3269 #generate an invoice immediately
3271 'invoice_terms' => '', #with these terms
3277 my $error = $cust_main->charge( 54.32, 'Description', 'Comment', 'Tax class' );
3283 my ( $amount, $quantity, $start_date, $classnum );
3284 my ( $pkg, $comment, $additional );
3285 my ( $setuptax, $taxclass ); #internal taxes
3286 my ( $taxproduct, $override ); #vendor (CCH) taxes
3288 my $cust_pkg_ref = '';
3289 my ( $bill_now, $invoice_terms ) = ( 0, '' );
3290 if ( ref( $_[0] ) ) {
3291 $amount = $_[0]->{amount};
3292 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
3293 $start_date = exists($_[0]->{start_date}) ? $_[0]->{start_date} : '';
3294 $no_auto = exists($_[0]->{no_auto}) ? $_[0]->{no_auto} : '';
3295 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
3296 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
3297 : '$'. sprintf("%.2f",$amount);
3298 $setuptax = exists($_[0]->{setuptax}) ? $_[0]->{setuptax} : '';
3299 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
3300 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
3301 $additional = $_[0]->{additional} || [];
3302 $taxproduct = $_[0]->{taxproductnum};
3303 $override = { '' => $_[0]->{tax_override} };
3304 $cust_pkg_ref = exists($_[0]->{cust_pkg_ref}) ? $_[0]->{cust_pkg_ref} : '';
3305 $bill_now = exists($_[0]->{bill_now}) ? $_[0]->{bill_now} : '';
3306 $invoice_terms = exists($_[0]->{invoice_terms}) ? $_[0]->{invoice_terms} : '';
3311 $pkg = @_ ? shift : 'One-time charge';
3312 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
3314 $taxclass = @_ ? shift : '';
3318 local $SIG{HUP} = 'IGNORE';
3319 local $SIG{INT} = 'IGNORE';
3320 local $SIG{QUIT} = 'IGNORE';
3321 local $SIG{TERM} = 'IGNORE';
3322 local $SIG{TSTP} = 'IGNORE';
3323 local $SIG{PIPE} = 'IGNORE';
3325 my $oldAutoCommit = $FS::UID::AutoCommit;
3326 local $FS::UID::AutoCommit = 0;
3329 my $part_pkg = new FS::part_pkg ( {
3331 'comment' => $comment,
3335 'classnum' => ( $classnum ? $classnum : '' ),
3336 'setuptax' => $setuptax,
3337 'taxclass' => $taxclass,
3338 'taxproductnum' => $taxproduct,
3341 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
3342 ( 0 .. @$additional - 1 )
3344 'additional_count' => scalar(@$additional),
3345 'setup_fee' => $amount,
3348 my $error = $part_pkg->insert( options => \%options,
3349 tax_overrides => $override,
3352 $dbh->rollback if $oldAutoCommit;
3356 my $pkgpart = $part_pkg->pkgpart;
3357 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3358 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3359 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3360 $error = $type_pkgs->insert;
3362 $dbh->rollback if $oldAutoCommit;
3367 my $cust_pkg = new FS::cust_pkg ( {
3368 'custnum' => $self->custnum,
3369 'pkgpart' => $pkgpart,
3370 'quantity' => $quantity,
3371 'start_date' => $start_date,
3372 'no_auto' => $no_auto,
3375 $error = $cust_pkg->insert;
3377 $dbh->rollback if $oldAutoCommit;
3379 } elsif ( $cust_pkg_ref ) {
3380 ${$cust_pkg_ref} = $cust_pkg;
3384 my $error = $self->bill( 'invoice_terms' => $invoice_terms,
3385 'pkg_list' => [ $cust_pkg ],
3388 $dbh->rollback if $oldAutoCommit;
3393 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3398 #=item charge_postal_fee
3400 #Applies a one time charge this customer. If there is an error,
3401 #returns the error, returns the cust_pkg charge object or false
3402 #if there was no charge.
3406 # This should be a customer event. For that to work requires that bill
3407 # also be a customer event.
3409 sub charge_postal_fee {
3412 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
3413 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
3415 my $cust_pkg = new FS::cust_pkg ( {
3416 'custnum' => $self->custnum,
3417 'pkgpart' => $pkgpart,
3421 my $error = $cust_pkg->insert;
3422 $error ? $error : $cust_pkg;
3425 =item cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3427 Returns all the invoices (see L<FS::cust_bill>) for this customer.
3429 Optionally, a list or hashref of additional arguments to the qsearch call can
3436 my $opt = ref($_[0]) ? shift : { @_ };
3438 #return $self->num_cust_bill unless wantarray || keys %$opt;
3440 $opt->{'table'} = 'cust_bill';
3441 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
3442 $opt->{'hashref'}{'custnum'} = $self->custnum;
3443 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
3445 map { $_ } #behavior of sort undefined in scalar context
3446 sort { $a->_date <=> $b->_date }
3450 =item open_cust_bill
3452 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
3457 sub open_cust_bill {
3461 'extra_sql' => ' AND '. FS::cust_bill->owed_sql. ' > 0',
3467 =item cust_statement [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3469 Returns all the statements (see L<FS::cust_statement>) for this customer.
3471 Optionally, a list or hashref of additional arguments to the qsearch call can
3476 sub cust_statement {
3478 my $opt = ref($_[0]) ? shift : { @_ };
3480 #return $self->num_cust_statement unless wantarray || keys %$opt;
3482 $opt->{'table'} = 'cust_statement';
3483 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
3484 $opt->{'hashref'}{'custnum'} = $self->custnum;
3485 $opt->{'order_by'} ||= 'ORDER BY _date ASC';
3487 map { $_ } #behavior of sort undefined in scalar context
3488 sort { $a->_date <=> $b->_date }
3494 Returns all the credits (see L<FS::cust_credit>) for this customer.
3500 map { $_ } #return $self->num_cust_credit unless wantarray;
3501 sort { $a->_date <=> $b->_date }
3502 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
3505 =item cust_credit_pkgnum
3507 Returns all the credits (see L<FS::cust_credit>) for this customer's specific
3508 package when using experimental package balances.
3512 sub cust_credit_pkgnum {
3513 my( $self, $pkgnum ) = @_;
3514 map { $_ } #return $self->num_cust_credit_pkgnum($pkgnum) unless wantarray;
3515 sort { $a->_date <=> $b->_date }
3516 qsearch( 'cust_credit', { 'custnum' => $self->custnum,
3517 'pkgnum' => $pkgnum,
3524 Returns all the payments (see L<FS::cust_pay>) for this customer.
3530 return $self->num_cust_pay unless wantarray;
3531 sort { $a->_date <=> $b->_date }
3532 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
3537 Returns the number of payments (see L<FS::cust_pay>) for this customer. Also
3538 called automatically when the cust_pay method is used in a scalar context.
3544 my $sql = "SELECT COUNT(*) FROM cust_pay WHERE custnum = ?";
3545 my $sth = dbh->prepare($sql) or die dbh->errstr;
3546 $sth->execute($self->custnum) or die $sth->errstr;
3547 $sth->fetchrow_arrayref->[0];
3550 =item cust_pay_pkgnum
3552 Returns all the payments (see L<FS::cust_pay>) for this customer's specific
3553 package when using experimental package balances.
3557 sub cust_pay_pkgnum {
3558 my( $self, $pkgnum ) = @_;
3559 map { $_ } #return $self->num_cust_pay_pkgnum($pkgnum) unless wantarray;
3560 sort { $a->_date <=> $b->_date }
3561 qsearch( 'cust_pay', { 'custnum' => $self->custnum,
3562 'pkgnum' => $pkgnum,
3569 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
3575 map { $_ } #return $self->num_cust_pay_void unless wantarray;
3576 sort { $a->_date <=> $b->_date }
3577 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
3580 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
3582 Returns all batched payments (see L<FS::cust_pay_void>) for this customer.
3584 Optionally, a list or hashref of additional arguments to the qsearch call can
3589 sub cust_pay_batch {
3591 my $opt = ref($_[0]) ? shift : { @_ };
3593 #return $self->num_cust_statement unless wantarray || keys %$opt;
3595 $opt->{'table'} = 'cust_pay_batch';
3596 $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
3597 $opt->{'hashref'}{'custnum'} = $self->custnum;
3598 $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
3600 map { $_ } #behavior of sort undefined in scalar context
3601 sort { $a->paybatchnum <=> $b->paybatchnum }
3605 =item cust_pay_pending
3607 Returns all pending payments (see L<FS::cust_pay_pending>) for this customer
3608 (without status "done").
3612 sub cust_pay_pending {
3614 return $self->num_cust_pay_pending unless wantarray;
3615 sort { $a->_date <=> $b->_date }
3616 qsearch( 'cust_pay_pending', {
3617 'custnum' => $self->custnum,
3618 'status' => { op=>'!=', value=>'done' },
3623 =item cust_pay_pending_attempt
3625 Returns all payment attempts / declined payments for this customer, as pending
3626 payments objects (see L<FS::cust_pay_pending>), with status "done" but without
3627 a corresponding payment (see L<FS::cust_pay>).
3631 sub cust_pay_pending_attempt {
3633 return $self->num_cust_pay_pending_attempt unless wantarray;
3634 sort { $a->_date <=> $b->_date }
3635 qsearch( 'cust_pay_pending', {
3636 'custnum' => $self->custnum,
3643 =item num_cust_pay_pending
3645 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
3646 customer (without status "done"). Also called automatically when the
3647 cust_pay_pending method is used in a scalar context.
3651 sub num_cust_pay_pending {
3654 " SELECT COUNT(*) FROM cust_pay_pending ".
3655 " WHERE custnum = ? AND status != 'done' ",
3660 =item num_cust_pay_pending_attempt
3662 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
3663 customer, with status "done" but without a corresp. Also called automatically when the
3664 cust_pay_pending method is used in a scalar context.
3668 sub num_cust_pay_pending_attempt {
3671 " SELECT COUNT(*) FROM cust_pay_pending ".
3672 " WHERE custnum = ? AND status = 'done' AND paynum IS NULL",
3679 Returns all the refunds (see L<FS::cust_refund>) for this customer.
3685 map { $_ } #return $self->num_cust_refund unless wantarray;
3686 sort { $a->_date <=> $b->_date }
3687 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
3690 =item display_custnum
3692 Returns the displayed customer number for this customer: agent_custid if
3693 cust_main-default_agent_custid is set and it has a value, custnum otherwise.
3697 sub display_custnum {
3699 if ( $conf->exists('cust_main-default_agent_custid') && $self->agent_custid ){
3700 return $self->agent_custid;
3702 return $self->custnum;
3708 Returns a name string for this customer, either "Company (Last, First)" or
3715 my $name = $self->contact;
3716 $name = $self->company. " ($name)" if $self->company;
3722 Returns a name string for this (service/shipping) contact, either
3723 "Company (Last, First)" or "Last, First".
3729 if ( $self->get('ship_last') ) {
3730 my $name = $self->ship_contact;
3731 $name = $self->ship_company. " ($name)" if $self->ship_company;
3740 Returns a name string for this customer, either "Company" or "First Last".
3746 $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
3749 =item ship_name_short
3751 Returns a name string for this (service/shipping) contact, either "Company"
3756 sub ship_name_short {
3758 if ( $self->get('ship_last') ) {
3759 $self->ship_company !~ /^\s*$/
3760 ? $self->ship_company
3761 : $self->ship_contact_firstlast;
3763 $self->name_company_or_firstlast;
3769 Returns this customer's full (billing) contact name only, "Last, First"
3775 $self->get('last'). ', '. $self->first;
3780 Returns this customer's full (shipping) contact name only, "Last, First"
3786 $self->get('ship_last')
3787 ? $self->get('ship_last'). ', '. $self->ship_first
3791 =item contact_firstlast
3793 Returns this customers full (billing) contact name only, "First Last".
3797 sub contact_firstlast {
3799 $self->first. ' '. $self->get('last');
3802 =item ship_contact_firstlast
3804 Returns this customer's full (shipping) contact name only, "First Last".
3808 sub ship_contact_firstlast {
3810 $self->get('ship_last')
3811 ? $self->first. ' '. $self->get('ship_last')
3812 : $self->contact_firstlast;
3817 Returns this customer's full country name
3823 code2country($self->country);
3826 =item geocode DATA_VENDOR
3828 Returns a value for the customer location as encoded by DATA_VENDOR.
3829 Currently this only makes sense for "CCH" as DATA_VENDOR.
3837 Returns a status string for this customer, currently:
3841 =item prospect - No packages have ever been ordered
3843 =item ordered - Recurring packages all are new (not yet billed).
3845 =item active - One or more recurring packages is active
3847 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
3849 =item suspended - All non-cancelled recurring packages are suspended
3851 =item cancelled - All recurring packages are cancelled
3855 Behavior of inactive vs. cancelled edge cases can be adjusted with the
3856 cust_main-status_module configuration option.
3860 sub status { shift->cust_status(@_); }
3864 for my $status ( FS::cust_main->statuses() ) {
3865 my $method = $status.'_sql';
3866 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
3867 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
3868 $sth->execute( ($self->custnum) x $numnum )
3869 or die "Error executing 'SELECT $sql': ". $sth->errstr;
3870 return $status if $sth->fetchrow_arrayref->[0];
3874 =item ucfirst_cust_status
3876 =item ucfirst_status
3878 Returns the status with the first character capitalized.
3882 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
3884 sub ucfirst_cust_status {
3886 ucfirst($self->cust_status);
3891 Returns a hex triplet color string for this customer's status.
3895 sub statuscolor { shift->cust_statuscolor(@_); }
3897 sub cust_statuscolor {
3899 __PACKAGE__->statuscolors->{$self->cust_status};
3904 Returns an array of hashes representing the customer's RT tickets.
3911 my $num = $conf->config('cust_main-max_tickets') || 10;
3914 if ( $conf->config('ticket_system') ) {
3915 unless ( $conf->config('ticket_system-custom_priority_field') ) {
3917 @tickets = @{ FS::TicketSystem->customer_tickets($self->custnum, $num) };
3921 foreach my $priority (
3922 $conf->config('ticket_system-custom_priority_field-values'), ''
3924 last if scalar(@tickets) >= $num;
3926 @{ FS::TicketSystem->customer_tickets( $self->custnum,
3927 $num - scalar(@tickets),
3937 # Return services representing svc_accts in customer support packages
3938 sub support_services {
3940 my %packages = map { $_ => 1 } $conf->config('support_packages');
3942 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
3943 grep { $_->part_svc->svcdb eq 'svc_acct' }
3944 map { $_->cust_svc }
3945 grep { exists $packages{ $_->pkgpart } }
3946 $self->ncancelled_pkgs;
3950 # Return a list of latitude/longitude for one of the services (if any)
3951 sub service_coordinates {
3955 grep { $_->latitude && $_->longitude }
3957 map { $_->cust_svc }
3958 $self->ncancelled_pkgs;
3960 scalar(@svc_X) ? ( $svc_X[0]->latitude, $svc_X[0]->longitude ) : ()
3965 Returns a masked version of the named field
3970 my ($self,$field) = @_;
3974 'x'x(length($self->getfield($field))-4).
3975 substr($self->getfield($field), (length($self->getfield($field))-4));
3981 =head1 CLASS METHODS
3987 Class method that returns the list of possible status strings for customers
3988 (see L<the status method|/status>). For example:
3990 @statuses = FS::cust_main->statuses();
3996 keys %{ $self->statuscolors };
3999 =item cust_status_sql
4001 Returns an SQL fragment to determine the status of a cust_main record, as a
4006 sub cust_status_sql {
4008 for my $status ( FS::cust_main->statuses() ) {
4009 my $method = $status.'_sql';
4010 $sql .= ' WHEN ('.FS::cust_main->$method.") THEN '$status'";
4019 Returns an SQL expression identifying prospective cust_main records (customers
4020 with no packages ever ordered)
4024 use vars qw($select_count_pkgs);
4025 $select_count_pkgs =
4026 "SELECT COUNT(*) FROM cust_pkg
4027 WHERE cust_pkg.custnum = cust_main.custnum";
4029 sub select_count_pkgs_sql {
4034 " 0 = ( $select_count_pkgs ) ";
4039 Returns an SQL expression identifying ordered cust_main records (customers with
4040 no active packages, but recurring packages not yet setup or one time charges
4046 FS::cust_main->none_active_sql.
4047 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->not_yet_billed_sql. " ) ";
4052 Returns an SQL expression identifying active cust_main records (customers with
4053 active recurring packages).
4058 " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
4061 =item none_active_sql
4063 Returns an SQL expression identifying cust_main records with no active
4064 recurring packages. This includes customers of status prospect, ordered,
4065 inactive, and suspended.
4069 sub none_active_sql {
4070 " 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
4075 Returns an SQL expression identifying inactive cust_main records (customers with
4076 no active recurring packages, but otherwise unsuspended/uncancelled).
4081 FS::cust_main->none_active_sql.
4082 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " ) ";
4088 Returns an SQL expression identifying suspended cust_main records.
4093 sub suspended_sql { susp_sql(@_); }
4095 FS::cust_main->none_active_sql.
4096 " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " ) ";
4102 Returns an SQL expression identifying cancelled cust_main records.
4106 sub cancel_sql { shift->cancelled_sql(@_); }
4109 =item uncancelled_sql
4111 Returns an SQL expression identifying un-cancelled cust_main records.
4115 sub uncancelled_sql { uncancel_sql(@_); }
4116 sub uncancel_sql { "
4117 ( 0 < ( $select_count_pkgs
4118 AND ( cust_pkg.cancel IS NULL
4119 OR cust_pkg.cancel = 0
4122 OR 0 = ( $select_count_pkgs )
4128 Returns an SQL fragment to retreive the balance.
4133 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
4134 WHERE cust_bill.custnum = cust_main.custnum )
4135 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
4136 WHERE cust_pay.custnum = cust_main.custnum )
4137 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
4138 WHERE cust_credit.custnum = cust_main.custnum )
4139 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
4140 WHERE cust_refund.custnum = cust_main.custnum )
4143 =item balance_date_sql [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
4145 Returns an SQL fragment to retreive the balance for this customer, optionally
4146 considering invoices with date earlier than START_TIME, and not
4147 later than END_TIME (total_owed_date minus total_unapplied_credits minus
4148 total_unapplied_payments).
4150 Times are specified as SQL fragments or numeric
4151 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4152 L<Date::Parse> for conversion functions. The empty string can be passed
4153 to disable that time constraint completely.
4155 Available options are:
4159 =item unapplied_date
4161 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)
4166 set to true to remove all customer comparison clauses, for totals
4171 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
4176 JOIN clause (typically used with the total option)
4180 An absolute cutoff time. Payments, credits, and refunds I<applied> after this
4181 time will be ignored. Note that START_TIME and END_TIME only limit the date
4182 range for invoices and I<unapplied> payments, credits, and refunds.
4188 sub balance_date_sql {
4189 my( $class, $start, $end, %opt ) = @_;
4191 my $cutoff = $opt{'cutoff'};
4193 my $owed = FS::cust_bill->owed_sql($cutoff);
4194 my $unapp_refund = FS::cust_refund->unapplied_sql($cutoff);
4195 my $unapp_credit = FS::cust_credit->unapplied_sql($cutoff);
4196 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
4198 my $j = $opt{'join'} || '';
4200 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
4201 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
4202 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
4203 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
4205 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
4206 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
4207 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
4208 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
4213 =item unapplied_payments_date_sql START_TIME [ END_TIME ]
4215 Returns an SQL fragment to retreive the total unapplied payments for this
4216 customer, only considering invoices with date earlier than START_TIME, and
4217 optionally not later than END_TIME.
4219 Times are specified as SQL fragments or numeric
4220 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4221 L<Date::Parse> for conversion functions. The empty string can be passed
4222 to disable that time constraint completely.
4224 Available options are:
4228 sub unapplied_payments_date_sql {
4229 my( $class, $start, $end, %opt ) = @_;
4231 my $cutoff = $opt{'cutoff'};
4233 my $unapp_pay = FS::cust_pay->unapplied_sql($cutoff);
4235 my $pay_where = $class->_money_table_where( 'cust_pay', $start, $end,
4236 'unapplied_date'=>1 );
4238 " ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $pay_where ) ";
4241 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4243 Helper method for balance_date_sql; name (and usage) subject to change
4244 (suggestions welcome).
4246 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
4247 cust_refund, cust_credit or cust_pay).
4249 If TABLE is "cust_bill" or the unapplied_date option is true, only
4250 considers records with date earlier than START_TIME, and optionally not
4251 later than END_TIME .
4255 sub _money_table_where {
4256 my( $class, $table, $start, $end, %opt ) = @_;
4259 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
4260 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
4261 push @where, "$table._date <= $start" if defined($start) && length($start);
4262 push @where, "$table._date > $end" if defined($end) && length($end);
4264 push @where, @{$opt{'where'}} if $opt{'where'};
4265 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
4271 #for dyanmic FS::$table->search in httemplate/misc/email_customers.html
4272 use FS::cust_main::Search;
4275 FS::cust_main::Search->search(@_);
4284 =item append_fuzzyfiles FIRSTNAME LASTNAME COMPANY ADDRESS1
4288 use FS::cust_main::Search;
4289 sub append_fuzzyfiles {
4290 #my( $first, $last, $company ) = @_;
4292 FS::cust_main::Search::check_and_rebuild_fuzzyfiles();
4294 use Fcntl qw(:flock);
4296 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
4298 foreach my $field (@fuzzyfields) {
4303 open(CACHE,">>$dir/cust_main.$field")
4304 or die "can't open $dir/cust_main.$field: $!";
4305 flock(CACHE,LOCK_EX)
4306 or die "can't lock $dir/cust_main.$field: $!";
4308 print CACHE "$value\n";
4310 flock(CACHE,LOCK_UN)
4311 or die "can't unlock $dir/cust_main.$field: $!";
4326 #warn join('-',keys %$param);
4327 my $fh = $param->{filehandle};
4328 my $agentnum = $param->{agentnum};
4329 my $format = $param->{format};
4331 my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
4334 if ( $format eq 'simple' ) {
4335 @fields = qw( custnum agent_custid amount pkg );
4337 die "unknown format $format";
4340 eval "use Text::CSV_XS;";
4343 my $csv = new Text::CSV_XS;
4350 local $SIG{HUP} = 'IGNORE';
4351 local $SIG{INT} = 'IGNORE';
4352 local $SIG{QUIT} = 'IGNORE';
4353 local $SIG{TERM} = 'IGNORE';
4354 local $SIG{TSTP} = 'IGNORE';
4355 local $SIG{PIPE} = 'IGNORE';
4357 my $oldAutoCommit = $FS::UID::AutoCommit;
4358 local $FS::UID::AutoCommit = 0;
4361 #while ( $columns = $csv->getline($fh) ) {
4363 while ( defined($line=<$fh>) ) {
4365 $csv->parse($line) or do {
4366 $dbh->rollback if $oldAutoCommit;
4367 return "can't parse: ". $csv->error_input();
4370 my @columns = $csv->fields();
4371 #warn join('-',@columns);
4374 foreach my $field ( @fields ) {
4375 $row{$field} = shift @columns;
4378 if ( $row{custnum} && $row{agent_custid} ) {
4379 dbh->rollback if $oldAutoCommit;
4380 return "can't specify custnum with agent_custid $row{agent_custid}";
4384 if ( $row{agent_custid} && $agentnum ) {
4385 %hash = ( 'agent_custid' => $row{agent_custid},
4386 'agentnum' => $agentnum,
4390 if ( $row{custnum} ) {
4391 %hash = ( 'custnum' => $row{custnum} );
4394 unless ( scalar(keys %hash) ) {
4395 $dbh->rollback if $oldAutoCommit;
4396 return "can't find customer without custnum or agent_custid and agentnum";
4399 my $cust_main = qsearchs('cust_main', { %hash } );
4400 unless ( $cust_main ) {
4401 $dbh->rollback if $oldAutoCommit;
4402 my $custnum = $row{custnum} || $row{agent_custid};
4403 return "unknown custnum $custnum";
4406 if ( $row{'amount'} > 0 ) {
4407 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
4409 $dbh->rollback if $oldAutoCommit;
4413 } elsif ( $row{'amount'} < 0 ) {
4414 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
4417 $dbh->rollback if $oldAutoCommit;
4427 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4429 return "Empty file!" unless $imported;
4435 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
4437 Deprecated. Use event notification and message templates
4438 (L<FS::msg_template>) instead.
4440 Sends a templated email notification to the customer (see L<Text::Template>).
4442 OPTIONS is a hash and may include
4444 I<from> - the email sender (default is invoice_from)
4446 I<to> - comma-separated scalar or arrayref of recipients
4447 (default is invoicing_list)
4449 I<subject> - The subject line of the sent email notification
4450 (default is "Notice from company_name")
4452 I<extra_fields> - a hashref of name/value pairs which will be substituted
4455 The following variables are vavailable in the template.
4457 I<$first> - the customer first name
4458 I<$last> - the customer last name
4459 I<$company> - the customer company
4460 I<$payby> - a description of the method of payment for the customer
4461 # would be nice to use FS::payby::shortname
4462 I<$payinfo> - the account information used to collect for this customer
4463 I<$expdate> - the expiration of the customer payment in seconds from epoch
4468 my ($self, $template, %options) = @_;
4470 return unless $conf->exists($template);
4472 my $from = $conf->config('invoice_from', $self->agentnum)
4473 if $conf->exists('invoice_from', $self->agentnum);
4474 $from = $options{from} if exists($options{from});
4476 my $to = join(',', $self->invoicing_list_emailonly);
4477 $to = $options{to} if exists($options{to});
4479 my $subject = "Notice from " . $conf->config('company_name', $self->agentnum)
4480 if $conf->exists('company_name', $self->agentnum);
4481 $subject = $options{subject} if exists($options{subject});
4483 my $notify_template = new Text::Template (TYPE => 'ARRAY',
4484 SOURCE => [ map "$_\n",
4485 $conf->config($template)]
4487 or die "can't create new Text::Template object: Text::Template::ERROR";
4488 $notify_template->compile()
4489 or die "can't compile template: Text::Template::ERROR";
4491 $FS::notify_template::_template::company_name =
4492 $conf->config('company_name', $self->agentnum);
4493 $FS::notify_template::_template::company_address =
4494 join("\n", $conf->config('company_address', $self->agentnum) ). "\n";
4496 my $paydate = $self->paydate || '2037-12-31';
4497 $FS::notify_template::_template::first = $self->first;
4498 $FS::notify_template::_template::last = $self->last;
4499 $FS::notify_template::_template::company = $self->company;
4500 $FS::notify_template::_template::payinfo = $self->mask_payinfo;
4501 my $payby = $self->payby;
4502 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
4503 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
4505 #credit cards expire at the end of the month/year of their exp date
4506 if ($payby eq 'CARD' || $payby eq 'DCRD') {
4507 $FS::notify_template::_template::payby = 'credit card';
4508 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
4509 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
4511 }elsif ($payby eq 'COMP') {
4512 $FS::notify_template::_template::payby = 'complimentary account';
4514 $FS::notify_template::_template::payby = 'current method';
4516 $FS::notify_template::_template::expdate = $expire_time;
4518 for (keys %{$options{extra_fields}}){
4520 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
4523 send_email(from => $from,
4525 subject => $subject,
4526 body => $notify_template->fill_in( PACKAGE =>
4527 'FS::notify_template::_template' ),
4532 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
4534 Generates a templated notification to the customer (see L<Text::Template>).
4536 OPTIONS is a hash and may include
4538 I<extra_fields> - a hashref of name/value pairs which will be substituted
4539 into the template. These values may override values mentioned below
4540 and those from the customer record.
4542 The following variables are available in the template instead of or in addition
4543 to the fields of the customer record.
4545 I<$payby> - a description of the method of payment for the customer
4546 # would be nice to use FS::payby::shortname
4547 I<$payinfo> - the masked account information used to collect for this customer
4548 I<$expdate> - the expiration of the customer payment method in seconds from epoch
4549 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
4553 # a lot like cust_bill::print_latex
4554 sub generate_letter {
4555 my ($self, $template, %options) = @_;
4557 return unless $conf->exists($template);
4559 my $letter_template = new Text::Template
4561 SOURCE => [ map "$_\n", $conf->config($template)],
4562 DELIMITERS => [ '[@--', '--@]' ],
4564 or die "can't create new Text::Template object: Text::Template::ERROR";
4566 $letter_template->compile()
4567 or die "can't compile template: Text::Template::ERROR";
4569 my %letter_data = map { $_ => $self->$_ } $self->fields;
4570 $letter_data{payinfo} = $self->mask_payinfo;
4572 #my $paydate = $self->paydate || '2037-12-31';
4573 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
4575 my $payby = $self->payby;
4576 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
4577 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
4579 #credit cards expire at the end of the month/year of their exp date
4580 if ($payby eq 'CARD' || $payby eq 'DCRD') {
4581 $letter_data{payby} = 'credit card';
4582 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
4583 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
4585 }elsif ($payby eq 'COMP') {
4586 $letter_data{payby} = 'complimentary account';
4588 $letter_data{payby} = 'current method';
4590 $letter_data{expdate} = $expire_time;
4592 for (keys %{$options{extra_fields}}){
4593 $letter_data{$_} = $options{extra_fields}->{$_};
4596 unless(exists($letter_data{returnaddress})){
4597 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
4598 $self->agent_template)
4600 if ( length($retadd) ) {
4601 $letter_data{returnaddress} = $retadd;
4602 } elsif ( grep /\S/, $conf->config('company_address', $self->agentnum) ) {
4603 $letter_data{returnaddress} =
4604 join( "\n", map { s/( {2,})/'~' x length($1)/eg;
4608 ( $conf->config('company_name', $self->agentnum),
4609 $conf->config('company_address', $self->agentnum),
4613 $letter_data{returnaddress} = '~';
4617 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
4619 $letter_data{company_name} = $conf->config('company_name', $self->agentnum);
4621 my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
4623 my $lh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
4627 ) or die "can't open temp file: $!\n";
4628 print $lh $conf->config_binary('logo.eps', $self->agentnum)
4629 or die "can't write temp file: $!\n";
4631 $letter_data{'logo_file'} = $lh->filename;
4633 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
4637 ) or die "can't open temp file: $!\n";
4639 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
4641 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
4642 return ($1, $letter_data{'logo_file'});
4646 =item print_ps TEMPLATE
4648 Returns an postscript letter filled in from TEMPLATE, as a scalar.
4654 my($file, $lfile) = $self->generate_letter(@_);
4655 my $ps = FS::Misc::generate_ps($file);
4656 unlink($file.'.tex');
4662 =item print TEMPLATE
4664 Prints the filled in template.
4666 TEMPLATE is the name of a L<Text::Template> to fill in and print.
4670 sub queueable_print {
4673 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
4674 or die "invalid customer number: " . $opt{custvnum};
4676 my $error = $self->print( $opt{template} );
4677 die $error if $error;
4681 my ($self, $template) = (shift, shift);
4682 do_print [ $self->print_ps($template) ];
4685 #these three subs should just go away once agent stuff is all config overrides
4687 sub agent_template {
4689 $self->_agent_plandata('agent_templatename');
4692 sub agent_invoice_from {
4694 $self->_agent_plandata('agent_invoice_from');
4697 sub _agent_plandata {
4698 my( $self, $option ) = @_;
4700 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
4701 #agent-specific Conf
4703 use FS::part_event::Condition;
4705 my $agentnum = $self->agentnum;
4707 my $regexp = regexp_sql();
4709 my $part_event_option =
4711 'select' => 'part_event_option.*',
4712 'table' => 'part_event_option',
4714 LEFT JOIN part_event USING ( eventpart )
4715 LEFT JOIN part_event_option AS peo_agentnum
4716 ON ( part_event.eventpart = peo_agentnum.eventpart
4717 AND peo_agentnum.optionname = 'agentnum'
4718 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
4720 LEFT JOIN part_event_condition
4721 ON ( part_event.eventpart = part_event_condition.eventpart
4722 AND part_event_condition.conditionname = 'cust_bill_age'
4724 LEFT JOIN part_event_condition_option
4725 ON ( part_event_condition.eventconditionnum = part_event_condition_option.eventconditionnum
4726 AND part_event_condition_option.optionname = 'age'
4729 #'hashref' => { 'optionname' => $option },
4730 #'hashref' => { 'part_event_option.optionname' => $option },
4732 " WHERE part_event_option.optionname = ". dbh->quote($option).
4733 " AND action = 'cust_bill_send_agent' ".
4734 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
4735 " AND peo_agentnum.optionname = 'agentnum' ".
4736 " AND ( agentnum IS NULL OR agentnum = $agentnum ) ".
4738 CASE WHEN part_event_condition_option.optionname IS NULL
4740 ELSE ". FS::part_event::Condition->age2seconds_sql('part_event_condition_option.optionvalue').
4742 , part_event.weight".
4746 unless ( $part_event_option ) {
4747 return $self->agent->invoice_template || ''
4748 if $option eq 'agent_templatename';
4752 $part_event_option->optionvalue;
4756 =item queued_bill 'custnum' => CUSTNUM [ , OPTION => VALUE ... ]
4758 Subroutine (not a method), designed to be called from the queue.
4760 Takes a list of options and values.
4762 Pulls up the customer record via the custnum option and calls bill_and_collect.
4767 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
4769 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
4770 warn 'bill_and_collect custnum#'. $cust_main->custnum. "\n";#log custnum w/pid
4772 $cust_main->bill_and_collect( %args );
4775 sub process_bill_and_collect {
4777 my $param = thaw(decode_base64(shift));
4778 my $cust_main = qsearchs( 'cust_main', { custnum => $param->{'custnum'} } )
4779 or die "custnum '$param->{custnum}' not found!\n";
4780 $param->{'job'} = $job;
4781 $param->{'fatal'} = 1; # runs from job queue, will be caught
4782 $param->{'retry'} = 1;
4784 $cust_main->bill_and_collect( %$param );
4787 sub _upgrade_data { #class method
4788 my ($class, %opts) = @_;
4791 'UPDATE h_cust_main SET paycvv = NULL WHERE paycvv IS NOT NULL',
4792 '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',
4794 # fix yyyy-m-dd formatted paydates
4795 if ( driver_name =~ /^mysql$/i ) {
4797 "UPDATE cust_main SET paydate = CONCAT( SUBSTRING(paydate FROM 1 FOR 5), '0', SUBSTRING(paydate FROM 6) ) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
4799 else { # the SQL standard
4801 "UPDATE cust_main SET paydate = SUBSTRING(paydate FROM 1 FOR 5) || '0' || SUBSTRING(paydate FROM 6) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
4804 foreach my $sql ( @statements ) {
4805 my $sth = dbh->prepare($sql) or die dbh->errstr;
4806 $sth->execute or die $sth->errstr;
4809 local($ignore_expired_card) = 1;
4810 local($ignore_illegal_zip) = 1;
4811 local($ignore_banned_card) = 1;
4812 local($skip_fuzzyfiles) = 1;
4813 $class->_upgrade_otaker(%opts);
4823 The delete method should possibly take an FS::cust_main object reference
4824 instead of a scalar customer number.
4826 Bill and collect options should probably be passed as references instead of a
4829 There should probably be a configuration file with a list of allowed credit
4832 No multiple currency support (probably a larger project than just this module).
4834 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
4836 Birthdates rely on negative epoch values.
4838 The payby for card/check batches is broken. With mixed batching, bad
4841 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
4845 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
4846 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
4847 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.