5 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
6 $import $skip_fuzzyfiles $ignore_expired_card @paytypes);
7 use vars qw( $realtime_bop_decline_quiet ); #ugh
11 use Scalar::Util qw( blessed );
12 use List::Util qw( min );
13 use Time::Local qw(timelocal);
16 use Digest::MD5 qw(md5_base64);
19 use File::Temp qw( tempfile );
20 use String::Approx qw(amatch);
21 use Business::CreditCard 0.28;
23 use FS::UID qw( getotaker dbh driver_name );
24 use FS::Record qw( qsearchs qsearch dbdef );
25 use FS::Misc qw( generate_email send_email generate_ps do_print );
26 use FS::Msgcat qw(gettext);
31 use FS::cust_bill_pkg;
32 use FS::cust_bill_pkg_display;
33 use FS::cust_bill_pkg_tax_location;
34 use FS::cust_bill_pkg_tax_rate_location;
36 use FS::cust_pay_pending;
37 use FS::cust_pay_void;
38 use FS::cust_pay_batch;
41 use FS::part_referral;
42 use FS::cust_main_county;
43 use FS::cust_location;
44 use FS::cust_main_exemption;
45 use FS::cust_tax_adjustment;
47 use FS::tax_rate_location;
48 use FS::cust_tax_location;
49 use FS::part_pkg_taxrate;
51 use FS::cust_main_invoice;
52 use FS::cust_credit_bill;
53 use FS::cust_bill_pay;
54 use FS::prepay_credit;
58 use FS::part_event_condition;
61 use FS::payment_gateway;
62 use FS::agent_payment_gateway;
64 use FS::payinfo_Mixin;
67 @ISA = qw( FS::payinfo_Mixin FS::Record );
69 @EXPORT_OK = qw( smart_search );
71 $realtime_bop_decline_quiet = 0;
73 # 1 is mostly method/subroutine entry and options
74 # 2 traces progress of some operations
75 # 3 is even more information including possibly sensitive data
77 $me = '[FS::cust_main]';
81 $ignore_expired_card = 0;
83 @encrypted_fields = ('payinfo', 'paycvv');
84 sub nohistory_fields { ('paycvv'); }
86 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
88 #ask FS::UID to run this stuff for us later
89 #$FS::UID::callback{'FS::cust_main'} = sub {
90 install_callback FS::UID sub {
92 #yes, need it for stuff below (prolly should be cached)
97 my ( $hashref, $cache ) = @_;
98 if ( exists $hashref->{'pkgnum'} ) {
99 #@{ $self->{'_pkgnum'} } = ();
100 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
101 $self->{'_pkgnum'} = $subcache;
102 #push @{ $self->{'_pkgnum'} },
103 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
109 FS::cust_main - Object methods for cust_main records
115 $record = new FS::cust_main \%hash;
116 $record = new FS::cust_main { 'column' => 'value' };
118 $error = $record->insert;
120 $error = $new_record->replace($old_record);
122 $error = $record->delete;
124 $error = $record->check;
126 @cust_pkg = $record->all_pkgs;
128 @cust_pkg = $record->ncancelled_pkgs;
130 @cust_pkg = $record->suspended_pkgs;
132 $error = $record->bill;
133 $error = $record->bill %options;
134 $error = $record->bill 'time' => $time;
136 $error = $record->collect;
137 $error = $record->collect %options;
138 $error = $record->collect 'invoice_time' => $time,
143 An FS::cust_main object represents a customer. FS::cust_main inherits from
144 FS::Record. The following fields are currently supported:
150 Primary key (assigned automatically for new customers)
154 Agent (see L<FS::agent>)
158 Advertising source (see L<FS::part_referral>)
170 Cocial security number (optional)
186 (optional, see L<FS::cust_main_county>)
190 (see L<FS::cust_main_county>)
196 (see L<FS::cust_main_county>)
232 (optional, see L<FS::cust_main_county>)
236 (see L<FS::cust_main_county>)
242 (see L<FS::cust_main_county>)
258 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
262 Payment Information (See L<FS::payinfo_Mixin> for data format)
266 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
270 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
274 Expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
278 Start date month (maestro/solo cards only)
282 Start date year (maestro/solo cards only)
286 Issue number (maestro/solo cards only)
290 Name on card or billing name
294 IP address from which payment information was received
298 Tax exempt, empty or `Y'
302 Order taker (assigned automatically, see L<FS::UID>)
308 =item referral_custnum
310 Referring customer number
314 Enable individual CDR spooling, empty or `Y'
318 A suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
322 Discourage individual CDR printing, empty or `Y'
332 Creates a new customer. To add the customer to the database, see L<"insert">.
334 Note that this stores the hash reference, not a distinct copy of the hash it
335 points to. You can ask the object for a copy with the I<hash> method.
339 sub table { 'cust_main'; }
341 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
343 Adds this customer to the database. If there is an error, returns the error,
344 otherwise returns false.
346 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
347 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
348 are inserted atomicly, or the transaction is rolled back. Passing an empty
349 hash reference is equivalent to not supplying this parameter. There should be
350 a better explanation of this, but until then, here's an example:
353 tie %hash, 'Tie::RefHash'; #this part is important
355 $cust_pkg => [ $svc_acct ],
358 $cust_main->insert( \%hash );
360 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
361 be set as the invoicing list (see L<"invoicing_list">). Errors return as
362 expected and rollback the entire transaction; it is not necessary to call
363 check_invoicing_list first. The invoicing_list is set after the records in the
364 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
365 invoicing_list destination to the newly-created svc_acct. Here's an example:
367 $cust_main->insert( {}, [ $email, 'POST' ] );
369 Currently available options are: I<depend_jobnum>, I<noexport> and I<tax_exemption>.
371 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
372 on the supplied jobnum (they will not run until the specific job completes).
373 This can be used to defer provisioning until some action completes (such
374 as running the customer's credit card successfully).
376 The I<noexport> option is deprecated. If I<noexport> is set true, no
377 provisioning jobs (exports) are scheduled. (You can schedule them later with
378 the B<reexport> method.)
380 The I<tax_exemption> option can be set to an arrayref of tax names.
381 FS::cust_main_exemption records will be created and inserted.
387 my $cust_pkgs = @_ ? shift : {};
388 my $invoicing_list = @_ ? shift : '';
390 warn "$me insert called with options ".
391 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
394 local $SIG{HUP} = 'IGNORE';
395 local $SIG{INT} = 'IGNORE';
396 local $SIG{QUIT} = 'IGNORE';
397 local $SIG{TERM} = 'IGNORE';
398 local $SIG{TSTP} = 'IGNORE';
399 local $SIG{PIPE} = 'IGNORE';
401 my $oldAutoCommit = $FS::UID::AutoCommit;
402 local $FS::UID::AutoCommit = 0;
405 my $prepay_identifier = '';
406 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
408 if ( $self->payby eq 'PREPAY' ) {
410 $self->payby('BILL');
411 $prepay_identifier = $self->payinfo;
414 warn " looking up prepaid card $prepay_identifier\n"
417 my $error = $self->get_prepay( $prepay_identifier,
418 'amount_ref' => \$amount,
419 'seconds_ref' => \$seconds,
420 'upbytes_ref' => \$upbytes,
421 'downbytes_ref' => \$downbytes,
422 'totalbytes_ref' => \$totalbytes,
425 $dbh->rollback if $oldAutoCommit;
426 #return "error applying prepaid card (transaction rolled back): $error";
430 $payby = 'PREP' if $amount;
432 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
435 $self->payby('BILL');
436 $amount = $self->paid;
440 warn " inserting $self\n"
443 $self->signupdate(time) unless $self->signupdate;
445 $self->auto_agent_custid()
446 if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
448 my $error = $self->SUPER::insert;
450 $dbh->rollback if $oldAutoCommit;
451 #return "inserting cust_main record (transaction rolled back): $error";
455 warn " setting invoicing list\n"
458 if ( $invoicing_list ) {
459 $error = $self->check_invoicing_list( $invoicing_list );
461 $dbh->rollback if $oldAutoCommit;
462 #return "checking invoicing_list (transaction rolled back): $error";
465 $self->invoicing_list( $invoicing_list );
468 warn " setting cust_main_exemption\n"
471 my $tax_exemption = delete $options{'tax_exemption'};
472 if ( $tax_exemption ) {
473 foreach my $taxname ( @$tax_exemption ) {
474 my $cust_main_exemption = new FS::cust_main_exemption {
475 'custnum' => $self->custnum,
476 'taxname' => $taxname,
478 my $error = $cust_main_exemption->insert;
480 $dbh->rollback if $oldAutoCommit;
481 return "inserting cust_main_exemption (transaction rolled back): $error";
486 if ( $conf->config('cust_main-skeleton_tables')
487 && $conf->config('cust_main-skeleton_custnum') ) {
489 warn " inserting skeleton records\n"
492 my $error = $self->start_copy_skel;
494 $dbh->rollback if $oldAutoCommit;
500 warn " ordering packages\n"
503 $error = $self->order_pkgs( $cust_pkgs,
505 'seconds_ref' => \$seconds,
506 'upbytes_ref' => \$upbytes,
507 'downbytes_ref' => \$downbytes,
508 'totalbytes_ref' => \$totalbytes,
511 $dbh->rollback if $oldAutoCommit;
516 $dbh->rollback if $oldAutoCommit;
517 return "No svc_acct record to apply pre-paid time";
519 if ( $upbytes || $downbytes || $totalbytes ) {
520 $dbh->rollback if $oldAutoCommit;
521 return "No svc_acct record to apply pre-paid data";
525 warn " inserting initial $payby payment of $amount\n"
527 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
529 $dbh->rollback if $oldAutoCommit;
530 return "inserting payment (transaction rolled back): $error";
534 unless ( $import || $skip_fuzzyfiles ) {
535 warn " queueing fuzzyfiles update\n"
537 $error = $self->queue_fuzzyfiles_update;
539 $dbh->rollback if $oldAutoCommit;
540 return "updating fuzzy search cache: $error";
544 warn " insert complete; committing transaction\n"
547 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
552 use File::CounterFile;
553 sub auto_agent_custid {
556 my $format = $conf->config('cust_main-auto_agent_custid');
558 if ( $format eq '1YMMXXXXXXXX' ) {
560 my $counter = new File::CounterFile 'cust_main.agent_custid';
563 my $ym = 100000000000 + time2str('%y%m00000000', time);
564 if ( $ym > $counter->value ) {
565 $counter->{'value'} = $agent_custid = $ym;
566 $counter->{'updated'} = 1;
568 $agent_custid = $counter->inc;
574 die "Unknown cust_main-auto_agent_custid format: $format";
577 $self->agent_custid($agent_custid);
581 sub start_copy_skel {
584 #'mg_user_preference' => {},
585 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
586 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
587 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
588 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
589 my @tables = eval(join('\n',$conf->config('cust_main-skeleton_tables')));
592 _copy_skel( 'cust_main', #tablename
593 $conf->config('cust_main-skeleton_custnum'), #sourceid
594 $self->custnum, #destid
595 @tables, #child tables
599 #recursive subroutine, not a method
601 my( $table, $sourceid, $destid, %child_tables ) = @_;
604 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
605 ( $table, $primary_key ) = ( $1, $2 );
607 my $dbdef_table = dbdef->table($table);
608 $primary_key = $dbdef_table->primary_key
609 or return "$table has no primary key".
610 " (or do you need to run dbdef-create?)";
613 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
614 join (', ', keys %child_tables). "\n"
617 foreach my $child_table_def ( keys %child_tables ) {
621 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
622 ( $child_table, $child_pkey ) = ( $1, $2 );
624 $child_table = $child_table_def;
626 $child_pkey = dbdef->table($child_table)->primary_key;
627 # or return "$table has no primary key".
628 # " (or do you need to run dbdef-create?)\n";
632 if ( keys %{ $child_tables{$child_table_def} } ) {
634 return "$child_table has no primary key".
635 " (run dbdef-create or try specifying it?)\n"
638 #false laziness w/Record::insert and only works on Pg
639 #refactor the proper last-inserted-id stuff out of Record::insert if this
640 # ever gets use for anything besides a quick kludge for one customer
641 my $default = dbdef->table($child_table)->column($child_pkey)->default;
642 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
643 or return "can't parse $child_table.$child_pkey default value ".
644 " for sequence name: $default";
649 my @sel_columns = grep { $_ ne $primary_key }
650 dbdef->table($child_table)->columns;
651 my $sel_columns = join(', ', @sel_columns );
653 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
654 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
655 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
657 my $sel_st = "SELECT $sel_columns FROM $child_table".
658 " WHERE $primary_key = $sourceid";
661 my $sel_sth = dbh->prepare( $sel_st )
662 or return dbh->errstr;
664 $sel_sth->execute or return $sel_sth->errstr;
666 while ( my $row = $sel_sth->fetchrow_hashref ) {
668 warn " selected row: ".
669 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
673 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
674 my $ins_sth =dbh->prepare($statement)
675 or return dbh->errstr;
676 my @param = ( $destid, map $row->{$_}, @ins_columns );
677 warn " $statement: [ ". join(', ', @param). " ]\n"
679 $ins_sth->execute( @param )
680 or return $ins_sth->errstr;
682 #next unless keys %{ $child_tables{$child_table} };
683 next unless $sequence;
685 #another section of that laziness
686 my $seq_sql = "SELECT currval('$sequence')";
687 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
688 $seq_sth->execute or return $seq_sth->errstr;
689 my $insertid = $seq_sth->fetchrow_arrayref->[0];
691 # don't drink soap! recurse! recurse! okay!
693 _copy_skel( $child_table_def,
694 $row->{$child_pkey}, #sourceid
696 %{ $child_tables{$child_table_def} },
698 return $error if $error;
708 =item order_pkg HASHREF | OPTION => VALUE ...
710 Orders a single package.
712 Options may be passed as a list of key/value pairs or as a hash reference.
723 Optional FS::cust_location object
727 Optional arryaref of FS::svc_* service objects.
731 If this option is set to a job queue jobnum (see L<FS::queue>), all provisioning
732 jobs will have a dependancy on the supplied job (they will not run until the
733 specific job completes). This can be used to defer provisioning until some
734 action completes (such as running the customer's credit card successfully).
738 Optional subject for a ticket created and attached to this customer
742 Optional queue name for ticket additions
750 my $opt = ref($_[0]) ? shift : { @_ };
752 warn "$me order_pkg called with options ".
753 join(', ', map { "$_: $opt->{$_}" } keys %$opt ). "\n"
756 my $cust_pkg = $opt->{'cust_pkg'};
757 my $svcs = $opt->{'svcs'} || [];
759 my %svc_options = ();
760 $svc_options{'depend_jobnum'} = $opt->{'depend_jobnum'}
761 if exists($opt->{'depend_jobnum'}) && $opt->{'depend_jobnum'};
763 my %insert_params = map { $opt->{$_} ? ( $_ => $opt->{$_} ) : () }
764 qw( ticket_subject ticket_queue );
766 local $SIG{HUP} = 'IGNORE';
767 local $SIG{INT} = 'IGNORE';
768 local $SIG{QUIT} = 'IGNORE';
769 local $SIG{TERM} = 'IGNORE';
770 local $SIG{TSTP} = 'IGNORE';
771 local $SIG{PIPE} = 'IGNORE';
773 my $oldAutoCommit = $FS::UID::AutoCommit;
774 local $FS::UID::AutoCommit = 0;
777 if ( $opt->{'cust_location'} &&
778 ( ! $cust_pkg->locationnum || $cust_pkg->locationnum == -1 ) ) {
779 my $error = $opt->{'cust_location'}->insert;
781 $dbh->rollback if $oldAutoCommit;
782 return "inserting cust_location (transaction rolled back): $error";
784 $cust_pkg->locationnum($opt->{'cust_location'}->locationnum);
787 $cust_pkg->custnum( $self->custnum );
789 my $error = $cust_pkg->insert( %insert_params );
791 $dbh->rollback if $oldAutoCommit;
792 return "inserting cust_pkg (transaction rolled back): $error";
795 foreach my $svc_something ( @{ $opt->{'svcs'} } ) {
796 if ( $svc_something->svcnum ) {
797 my $old_cust_svc = $svc_something->cust_svc;
798 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
799 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
800 $error = $new_cust_svc->replace($old_cust_svc);
802 $svc_something->pkgnum( $cust_pkg->pkgnum );
803 if ( $svc_something->isa('FS::svc_acct') ) {
804 foreach ( grep { $opt->{$_.'_ref'} && ${ $opt->{$_.'_ref'} } }
805 qw( seconds upbytes downbytes totalbytes ) ) {
806 $svc_something->$_( $svc_something->$_() + ${ $opt->{$_.'_ref'} } );
807 ${ $opt->{$_.'_ref'} } = 0;
810 $error = $svc_something->insert(%svc_options);
813 $dbh->rollback if $oldAutoCommit;
814 return "inserting svc_ (transaction rolled back): $error";
818 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
823 #deprecated #=item order_pkgs HASHREF [ , SECONDSREF ] [ , OPTION => VALUE ... ]
824 =item order_pkgs HASHREF [ , OPTION => VALUE ... ]
826 Like the insert method on an existing record, this method orders multiple
827 packages and included services atomicaly. Pass a Tie::RefHash data structure
828 to this method containing FS::cust_pkg and FS::svc_I<tablename> objects.
829 There should be a better explanation of this, but until then, here's an
833 tie %hash, 'Tie::RefHash'; #this part is important
835 $cust_pkg => [ $svc_acct ],
838 $cust_main->order_pkgs( \%hash, 'noexport'=>1 );
840 Services can be new, in which case they are inserted, or existing unaudited
841 services, in which case they are linked to the newly-created package.
843 Currently available options are: I<depend_jobnum>, I<noexport>, I<seconds_ref>,
844 I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>.
846 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
847 on the supplied jobnum (they will not run until the specific job completes).
848 This can be used to defer provisioning until some action completes (such
849 as running the customer's credit card successfully).
851 The I<noexport> option is deprecated. If I<noexport> is set true, no
852 provisioning jobs (exports) are scheduled. (You can schedule them later with
853 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
854 on the cust_main object is not recommended, as existing services will also be
857 If I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, or I<totalbytes_ref> is
858 provided, the scalars (provided by references) will be incremented by the
859 values of the prepaid card.`
865 my $cust_pkgs = shift;
866 my $seconds_ref = ref($_[0]) ? shift : ''; #deprecated
868 $seconds_ref ||= $options{'seconds_ref'};
870 warn "$me order_pkgs called with options ".
871 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
874 local $SIG{HUP} = 'IGNORE';
875 local $SIG{INT} = 'IGNORE';
876 local $SIG{QUIT} = 'IGNORE';
877 local $SIG{TERM} = 'IGNORE';
878 local $SIG{TSTP} = 'IGNORE';
879 local $SIG{PIPE} = 'IGNORE';
881 my $oldAutoCommit = $FS::UID::AutoCommit;
882 local $FS::UID::AutoCommit = 0;
885 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
887 foreach my $cust_pkg ( keys %$cust_pkgs ) {
889 my $error = $self->order_pkg(
890 'cust_pkg' => $cust_pkg,
891 'svcs' => $cust_pkgs->{$cust_pkg},
892 'seconds_ref' => $seconds_ref,
893 map { $_ => $options{$_} } qw( upbytes_ref downbytes_ref totalbytes_ref
898 $dbh->rollback if $oldAutoCommit;
904 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
908 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
910 Recharges this (existing) customer with the specified prepaid card (see
911 L<FS::prepay_credit>), specified either by I<identifier> or as an
912 FS::prepay_credit object. If there is an error, returns the error, otherwise
915 Optionally, five scalar references can be passed as well. They will have their
916 values filled in with the amount, number of seconds, and number of upload,
917 download, and total bytes applied by this prepaid card.
921 #the ref bullshit here should be refactored like get_prepay. MyAccount.pm is
922 #the only place that uses these args
923 sub recharge_prepay {
924 my( $self, $prepay_credit, $amountref, $secondsref,
925 $upbytesref, $downbytesref, $totalbytesref ) = @_;
927 local $SIG{HUP} = 'IGNORE';
928 local $SIG{INT} = 'IGNORE';
929 local $SIG{QUIT} = 'IGNORE';
930 local $SIG{TERM} = 'IGNORE';
931 local $SIG{TSTP} = 'IGNORE';
932 local $SIG{PIPE} = 'IGNORE';
934 my $oldAutoCommit = $FS::UID::AutoCommit;
935 local $FS::UID::AutoCommit = 0;
938 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
940 my $error = $self->get_prepay( $prepay_credit,
941 'amount_ref' => \$amount,
942 'seconds_ref' => \$seconds,
943 'upbytes_ref' => \$upbytes,
944 'downbytes_ref' => \$downbytes,
945 'totalbytes_ref' => \$totalbytes,
947 || $self->increment_seconds($seconds)
948 || $self->increment_upbytes($upbytes)
949 || $self->increment_downbytes($downbytes)
950 || $self->increment_totalbytes($totalbytes)
951 || $self->insert_cust_pay_prepay( $amount,
953 ? $prepay_credit->identifier
958 $dbh->rollback if $oldAutoCommit;
962 if ( defined($amountref) ) { $$amountref = $amount; }
963 if ( defined($secondsref) ) { $$secondsref = $seconds; }
964 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
965 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
966 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
968 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
973 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
975 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
976 specified either by I<identifier> or as an FS::prepay_credit object.
978 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
979 incremented by the values of the prepaid card.
981 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
982 check or set this customer's I<agentnum>.
984 If there is an error, returns the error, otherwise returns false.
990 my( $self, $prepay_credit, %opt ) = @_;
992 local $SIG{HUP} = 'IGNORE';
993 local $SIG{INT} = 'IGNORE';
994 local $SIG{QUIT} = 'IGNORE';
995 local $SIG{TERM} = 'IGNORE';
996 local $SIG{TSTP} = 'IGNORE';
997 local $SIG{PIPE} = 'IGNORE';
999 my $oldAutoCommit = $FS::UID::AutoCommit;
1000 local $FS::UID::AutoCommit = 0;
1003 unless ( ref($prepay_credit) ) {
1005 my $identifier = $prepay_credit;
1007 $prepay_credit = qsearchs(
1009 { 'identifier' => $prepay_credit },
1014 unless ( $prepay_credit ) {
1015 $dbh->rollback if $oldAutoCommit;
1016 return "Invalid prepaid card: ". $identifier;
1021 if ( $prepay_credit->agentnum ) {
1022 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
1023 $dbh->rollback if $oldAutoCommit;
1024 return "prepaid card not valid for agent ". $self->agentnum;
1026 $self->agentnum($prepay_credit->agentnum);
1029 my $error = $prepay_credit->delete;
1031 $dbh->rollback if $oldAutoCommit;
1032 return "removing prepay_credit (transaction rolled back): $error";
1035 ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
1036 for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
1038 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1043 =item increment_upbytes SECONDS
1045 Updates this customer's single or primary account (see L<FS::svc_acct>) by
1046 the specified number of upbytes. If there is an error, returns the error,
1047 otherwise returns false.
1051 sub increment_upbytes {
1052 _increment_column( shift, 'upbytes', @_);
1055 =item increment_downbytes SECONDS
1057 Updates this customer's single or primary account (see L<FS::svc_acct>) by
1058 the specified number of downbytes. If there is an error, returns the error,
1059 otherwise returns false.
1063 sub increment_downbytes {
1064 _increment_column( shift, 'downbytes', @_);
1067 =item increment_totalbytes SECONDS
1069 Updates this customer's single or primary account (see L<FS::svc_acct>) by
1070 the specified number of totalbytes. If there is an error, returns the error,
1071 otherwise returns false.
1075 sub increment_totalbytes {
1076 _increment_column( shift, 'totalbytes', @_);
1079 =item increment_seconds SECONDS
1081 Updates this customer's single or primary account (see L<FS::svc_acct>) by
1082 the specified number of seconds. If there is an error, returns the error,
1083 otherwise returns false.
1087 sub increment_seconds {
1088 _increment_column( shift, 'seconds', @_);
1091 =item _increment_column AMOUNT
1093 Updates this customer's single or primary account (see L<FS::svc_acct>) by
1094 the specified number of seconds or bytes. If there is an error, returns
1095 the error, otherwise returns false.
1099 sub _increment_column {
1100 my( $self, $column, $amount ) = @_;
1101 warn "$me increment_column called: $column, $amount\n"
1104 return '' unless $amount;
1106 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
1107 $self->ncancelled_pkgs;
1109 if ( ! @cust_pkg ) {
1110 return 'No packages with primary or single services found'.
1111 ' to apply pre-paid time';
1112 } elsif ( scalar(@cust_pkg) > 1 ) {
1113 #maybe have a way to specify the package/account?
1114 return 'Multiple packages found to apply pre-paid time';
1117 my $cust_pkg = $cust_pkg[0];
1118 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
1122 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
1124 if ( ! @cust_svc ) {
1125 return 'No account found to apply pre-paid time';
1126 } elsif ( scalar(@cust_svc) > 1 ) {
1127 return 'Multiple accounts found to apply pre-paid time';
1130 my $svc_acct = $cust_svc[0]->svc_x;
1131 warn " found service svcnum ". $svc_acct->pkgnum.
1132 ' ('. $svc_acct->email. ")\n"
1135 $column = "increment_$column";
1136 $svc_acct->$column($amount);
1140 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
1142 Inserts a prepayment in the specified amount for this customer. An optional
1143 second argument can specify the prepayment identifier for tracking purposes.
1144 If there is an error, returns the error, otherwise returns false.
1148 sub insert_cust_pay_prepay {
1149 shift->insert_cust_pay('PREP', @_);
1152 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
1154 Inserts a cash payment in the specified amount for this customer. An optional
1155 second argument can specify the payment identifier for tracking purposes.
1156 If there is an error, returns the error, otherwise returns false.
1160 sub insert_cust_pay_cash {
1161 shift->insert_cust_pay('CASH', @_);
1164 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
1166 Inserts a Western Union payment in the specified amount for this customer. An
1167 optional second argument can specify the prepayment identifier for tracking
1168 purposes. If there is an error, returns the error, otherwise returns false.
1172 sub insert_cust_pay_west {
1173 shift->insert_cust_pay('WEST', @_);
1176 sub insert_cust_pay {
1177 my( $self, $payby, $amount ) = splice(@_, 0, 3);
1178 my $payinfo = scalar(@_) ? shift : '';
1180 my $cust_pay = new FS::cust_pay {
1181 'custnum' => $self->custnum,
1182 'paid' => sprintf('%.2f', $amount),
1183 #'_date' => #date the prepaid card was purchased???
1185 'payinfo' => $payinfo,
1193 This method is deprecated. See the I<depend_jobnum> option to the insert and
1194 order_pkgs methods for a better way to defer provisioning.
1196 Re-schedules all exports by calling the B<reexport> method of all associated
1197 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
1198 otherwise returns false.
1205 carp "WARNING: FS::cust_main::reexport is deprectated; ".
1206 "use the depend_jobnum option to insert or order_pkgs to delay export";
1208 local $SIG{HUP} = 'IGNORE';
1209 local $SIG{INT} = 'IGNORE';
1210 local $SIG{QUIT} = 'IGNORE';
1211 local $SIG{TERM} = 'IGNORE';
1212 local $SIG{TSTP} = 'IGNORE';
1213 local $SIG{PIPE} = 'IGNORE';
1215 my $oldAutoCommit = $FS::UID::AutoCommit;
1216 local $FS::UID::AutoCommit = 0;
1219 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
1220 my $error = $cust_pkg->reexport;
1222 $dbh->rollback if $oldAutoCommit;
1227 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1232 =item delete NEW_CUSTNUM
1234 This deletes the customer. If there is an error, returns the error, otherwise
1237 This will completely remove all traces of the customer record. This is not
1238 what you want when a customer cancels service; for that, cancel all of the
1239 customer's packages (see L</cancel>).
1241 If the customer has any uncancelled packages, you need to pass a new (valid)
1242 customer number for those packages to be transferred to. Cancelled packages
1243 will be deleted. Did I mention that this is NOT what you want when a customer
1244 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
1246 You can't delete a customer with invoices (see L<FS::cust_bill>),
1247 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
1248 refunds (see L<FS::cust_refund>).
1255 local $SIG{HUP} = 'IGNORE';
1256 local $SIG{INT} = 'IGNORE';
1257 local $SIG{QUIT} = 'IGNORE';
1258 local $SIG{TERM} = 'IGNORE';
1259 local $SIG{TSTP} = 'IGNORE';
1260 local $SIG{PIPE} = 'IGNORE';
1262 my $oldAutoCommit = $FS::UID::AutoCommit;
1263 local $FS::UID::AutoCommit = 0;
1266 if ( $self->cust_bill ) {
1267 $dbh->rollback if $oldAutoCommit;
1268 return "Can't delete a customer with invoices";
1270 if ( $self->cust_credit ) {
1271 $dbh->rollback if $oldAutoCommit;
1272 return "Can't delete a customer with credits";
1274 if ( $self->cust_pay ) {
1275 $dbh->rollback if $oldAutoCommit;
1276 return "Can't delete a customer with payments";
1278 if ( $self->cust_refund ) {
1279 $dbh->rollback if $oldAutoCommit;
1280 return "Can't delete a customer with refunds";
1283 my @cust_pkg = $self->ncancelled_pkgs;
1285 my $new_custnum = shift;
1286 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1287 $dbh->rollback if $oldAutoCommit;
1288 return "Invalid new customer number: $new_custnum";
1290 foreach my $cust_pkg ( @cust_pkg ) {
1291 my %hash = $cust_pkg->hash;
1292 $hash{'custnum'} = $new_custnum;
1293 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1294 my $error = $new_cust_pkg->replace($cust_pkg,
1295 options => { $cust_pkg->options },
1298 $dbh->rollback if $oldAutoCommit;
1303 my @cancelled_cust_pkg = $self->all_pkgs;
1304 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1305 my $error = $cust_pkg->delete;
1307 $dbh->rollback if $oldAutoCommit;
1312 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1313 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1315 my $error = $cust_main_invoice->delete;
1317 $dbh->rollback if $oldAutoCommit;
1322 foreach my $cust_main_exemption (
1323 qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } )
1325 my $error = $cust_main_exemption->delete;
1327 $dbh->rollback if $oldAutoCommit;
1332 my $error = $self->SUPER::delete;
1334 $dbh->rollback if $oldAutoCommit;
1338 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1343 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
1346 Replaces the OLD_RECORD with this one in the database. If there is an error,
1347 returns the error, otherwise returns false.
1349 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1350 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1351 expected and rollback the entire transaction; it is not necessary to call
1352 check_invoicing_list first. Here's an example:
1354 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1356 Currently available options are: I<tax_exemption>.
1358 The I<tax_exemption> option can be set to an arrayref of tax names.
1359 FS::cust_main_exemption records will be deleted and inserted as appropriate.
1366 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1368 : $self->replace_old;
1372 warn "$me replace called\n"
1375 my $curuser = $FS::CurrentUser::CurrentUser;
1376 if ( $self->payby eq 'COMP'
1377 && $self->payby ne $old->payby
1378 && ! $curuser->access_right('Complimentary customer')
1381 return "You are not permitted to create complimentary accounts.";
1384 local($ignore_expired_card) = 1
1385 if $old->payby =~ /^(CARD|DCRD)$/
1386 && $self->payby =~ /^(CARD|DCRD)$/
1387 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1389 local $SIG{HUP} = 'IGNORE';
1390 local $SIG{INT} = 'IGNORE';
1391 local $SIG{QUIT} = 'IGNORE';
1392 local $SIG{TERM} = 'IGNORE';
1393 local $SIG{TSTP} = 'IGNORE';
1394 local $SIG{PIPE} = 'IGNORE';
1396 my $oldAutoCommit = $FS::UID::AutoCommit;
1397 local $FS::UID::AutoCommit = 0;
1400 my $error = $self->SUPER::replace($old);
1403 $dbh->rollback if $oldAutoCommit;
1407 if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
1408 my $invoicing_list = shift @param;
1409 $error = $self->check_invoicing_list( $invoicing_list );
1411 $dbh->rollback if $oldAutoCommit;
1414 $self->invoicing_list( $invoicing_list );
1417 my %options = @param;
1419 my $tax_exemption = delete $options{'tax_exemption'};
1420 if ( $tax_exemption ) {
1422 my %cust_main_exemption =
1423 map { $_->taxname => $_ }
1424 qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
1426 foreach my $taxname ( @$tax_exemption ) {
1428 next if delete $cust_main_exemption{$taxname};
1430 my $cust_main_exemption = new FS::cust_main_exemption {
1431 'custnum' => $self->custnum,
1432 'taxname' => $taxname,
1434 my $error = $cust_main_exemption->insert;
1436 $dbh->rollback if $oldAutoCommit;
1437 return "inserting cust_main_exemption (transaction rolled back): $error";
1441 foreach my $cust_main_exemption ( values %cust_main_exemption ) {
1442 my $error = $cust_main_exemption->delete;
1444 $dbh->rollback if $oldAutoCommit;
1445 return "deleting cust_main_exemption (transaction rolled back): $error";
1451 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1452 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1453 # card/check/lec info has changed, want to retry realtime_ invoice events
1454 my $error = $self->retry_realtime;
1456 $dbh->rollback if $oldAutoCommit;
1461 unless ( $import || $skip_fuzzyfiles ) {
1462 $error = $self->queue_fuzzyfiles_update;
1464 $dbh->rollback if $oldAutoCommit;
1465 return "updating fuzzy search cache: $error";
1469 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1474 =item queue_fuzzyfiles_update
1476 Used by insert & replace to update the fuzzy search cache
1480 sub queue_fuzzyfiles_update {
1483 local $SIG{HUP} = 'IGNORE';
1484 local $SIG{INT} = 'IGNORE';
1485 local $SIG{QUIT} = 'IGNORE';
1486 local $SIG{TERM} = 'IGNORE';
1487 local $SIG{TSTP} = 'IGNORE';
1488 local $SIG{PIPE} = 'IGNORE';
1490 my $oldAutoCommit = $FS::UID::AutoCommit;
1491 local $FS::UID::AutoCommit = 0;
1494 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1495 my $error = $queue->insert( map $self->getfield($_),
1496 qw(first last company)
1499 $dbh->rollback if $oldAutoCommit;
1500 return "queueing job (transaction rolled back): $error";
1503 if ( $self->ship_last ) {
1504 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1505 $error = $queue->insert( map $self->getfield("ship_$_"),
1506 qw(first last company)
1509 $dbh->rollback if $oldAutoCommit;
1510 return "queueing job (transaction rolled back): $error";
1514 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1521 Checks all fields to make sure this is a valid customer record. If there is
1522 an error, returns the error, otherwise returns false. Called by the insert
1523 and replace methods.
1530 warn "$me check BEFORE: \n". $self->_dump
1534 $self->ut_numbern('custnum')
1535 || $self->ut_number('agentnum')
1536 || $self->ut_textn('agent_custid')
1537 || $self->ut_number('refnum')
1538 || $self->ut_textn('custbatch')
1539 || $self->ut_name('last')
1540 || $self->ut_name('first')
1541 || $self->ut_snumbern('birthdate')
1542 || $self->ut_snumbern('signupdate')
1543 || $self->ut_textn('company')
1544 || $self->ut_text('address1')
1545 || $self->ut_textn('address2')
1546 || $self->ut_text('city')
1547 || $self->ut_textn('county')
1548 || $self->ut_textn('state')
1549 || $self->ut_country('country')
1550 || $self->ut_anything('comments')
1551 || $self->ut_numbern('referral_custnum')
1552 || $self->ut_textn('stateid')
1553 || $self->ut_textn('stateid_state')
1554 || $self->ut_textn('invoice_terms')
1555 || $self->ut_alphan('geocode')
1556 || $self->ut_floatn('cdr_termination_percentage')
1559 #barf. need message catalogs. i18n. etc.
1560 $error .= "Please select an advertising source."
1561 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1562 return $error if $error;
1564 return "Unknown agent"
1565 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1567 return "Unknown refnum"
1568 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1570 return "Unknown referring custnum: ". $self->referral_custnum
1571 unless ! $self->referral_custnum
1572 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1574 if ( $self->censustract ne '' ) {
1575 $self->censustract =~ /^\s*(\d{9})\.?(\d{2})\s*$/
1576 or return "Illegal census tract: ". $self->censustract;
1578 $self->censustract("$1.$2");
1581 if ( $self->ss eq '' ) {
1586 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1587 or return "Illegal social security number: ". $self->ss;
1588 $self->ss("$1-$2-$3");
1592 # bad idea to disable, causes billing to fail because of no tax rates later
1593 # unless ( $import ) {
1594 unless ( qsearch('cust_main_county', {
1595 'country' => $self->country,
1598 return "Unknown state/county/country: ".
1599 $self->state. "/". $self->county. "/". $self->country
1600 unless qsearch('cust_main_county',{
1601 'state' => $self->state,
1602 'county' => $self->county,
1603 'country' => $self->country,
1609 $self->ut_phonen('daytime', $self->country)
1610 || $self->ut_phonen('night', $self->country)
1611 || $self->ut_phonen('fax', $self->country)
1612 || $self->ut_zip('zip', $self->country)
1614 return $error if $error;
1616 if ( $conf->exists('cust_main-require_phone')
1617 && ! length($self->daytime) && ! length($self->night)
1620 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1622 : FS::Msgcat::_gettext('daytime');
1623 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1625 : FS::Msgcat::_gettext('night');
1627 return "$daytime_label or $night_label is required"
1631 if ( $self->has_ship_address
1632 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1633 $self->addr_fields )
1637 $self->ut_name('ship_last')
1638 || $self->ut_name('ship_first')
1639 || $self->ut_textn('ship_company')
1640 || $self->ut_text('ship_address1')
1641 || $self->ut_textn('ship_address2')
1642 || $self->ut_text('ship_city')
1643 || $self->ut_textn('ship_county')
1644 || $self->ut_textn('ship_state')
1645 || $self->ut_country('ship_country')
1647 return $error if $error;
1649 #false laziness with above
1650 unless ( qsearchs('cust_main_county', {
1651 'country' => $self->ship_country,
1654 return "Unknown ship_state/ship_county/ship_country: ".
1655 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1656 unless qsearch('cust_main_county',{
1657 'state' => $self->ship_state,
1658 'county' => $self->ship_county,
1659 'country' => $self->ship_country,
1665 $self->ut_phonen('ship_daytime', $self->ship_country)
1666 || $self->ut_phonen('ship_night', $self->ship_country)
1667 || $self->ut_phonen('ship_fax', $self->ship_country)
1668 || $self->ut_zip('ship_zip', $self->ship_country)
1670 return $error if $error;
1672 return "Unit # is required."
1673 if $self->ship_address2 =~ /^\s*$/
1674 && $conf->exists('cust_main-require_address2');
1676 } else { # ship_ info eq billing info, so don't store dup info in database
1678 $self->setfield("ship_$_", '')
1679 foreach $self->addr_fields;
1681 return "Unit # is required."
1682 if $self->address2 =~ /^\s*$/
1683 && $conf->exists('cust_main-require_address2');
1687 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1688 # or return "Illegal payby: ". $self->payby;
1690 FS::payby->can_payby($self->table, $self->payby)
1691 or return "Illegal payby: ". $self->payby;
1693 $error = $self->ut_numbern('paystart_month')
1694 || $self->ut_numbern('paystart_year')
1695 || $self->ut_numbern('payissue')
1696 || $self->ut_textn('paytype')
1698 return $error if $error;
1700 if ( $self->payip eq '' ) {
1703 $error = $self->ut_ip('payip');
1704 return $error if $error;
1707 # If it is encrypted and the private key is not availaible then we can't
1708 # check the credit card.
1710 my $check_payinfo = 1;
1712 if ($self->is_encrypted($self->payinfo)) {
1716 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1718 my $payinfo = $self->payinfo;
1719 $payinfo =~ s/\D//g;
1720 $payinfo =~ /^(\d{13,16})$/
1721 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1723 $self->payinfo($payinfo);
1725 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1727 return gettext('unknown_card_type')
1728 if cardtype($self->payinfo) eq "Unknown";
1730 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1732 return 'Banned credit card: banned on '.
1733 time2str('%a %h %o at %r', $ban->_date).
1734 ' by '. $ban->otaker.
1735 ' (ban# '. $ban->bannum. ')';
1738 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1739 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1740 $self->paycvv =~ /^(\d{4})$/
1741 or return "CVV2 (CID) for American Express cards is four digits.";
1744 $self->paycvv =~ /^(\d{3})$/
1745 or return "CVV2 (CVC2/CID) is three digits.";
1752 my $cardtype = cardtype($payinfo);
1753 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1755 return "Start date or issue number is required for $cardtype cards"
1756 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1758 return "Start month must be between 1 and 12"
1759 if $self->paystart_month
1760 and $self->paystart_month < 1 || $self->paystart_month > 12;
1762 return "Start year must be 1990 or later"
1763 if $self->paystart_year
1764 and $self->paystart_year < 1990;
1766 return "Issue number must be beween 1 and 99"
1768 and $self->payissue < 1 || $self->payissue > 99;
1771 $self->paystart_month('');
1772 $self->paystart_year('');
1773 $self->payissue('');
1776 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1778 my $payinfo = $self->payinfo;
1779 $payinfo =~ s/[^\d\@]//g;
1780 if ( $conf->exists('echeck-nonus') ) {
1781 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1782 $payinfo = "$1\@$2";
1784 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1785 $payinfo = "$1\@$2";
1787 $self->payinfo($payinfo);
1790 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1792 return 'Banned ACH account: banned on '.
1793 time2str('%a %h %o at %r', $ban->_date).
1794 ' by '. $ban->otaker.
1795 ' (ban# '. $ban->bannum. ')';
1798 } elsif ( $self->payby eq 'LECB' ) {
1800 my $payinfo = $self->payinfo;
1801 $payinfo =~ s/\D//g;
1802 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1804 $self->payinfo($payinfo);
1807 } elsif ( $self->payby eq 'BILL' ) {
1809 $error = $self->ut_textn('payinfo');
1810 return "Illegal P.O. number: ". $self->payinfo if $error;
1813 } elsif ( $self->payby eq 'COMP' ) {
1815 my $curuser = $FS::CurrentUser::CurrentUser;
1816 if ( ! $self->custnum
1817 && ! $curuser->access_right('Complimentary customer')
1820 return "You are not permitted to create complimentary accounts."
1823 $error = $self->ut_textn('payinfo');
1824 return "Illegal comp account issuer: ". $self->payinfo if $error;
1827 } elsif ( $self->payby eq 'PREPAY' ) {
1829 my $payinfo = $self->payinfo;
1830 $payinfo =~ s/\W//g; #anything else would just confuse things
1831 $self->payinfo($payinfo);
1832 $error = $self->ut_alpha('payinfo');
1833 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1834 return "Unknown prepayment identifier"
1835 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1840 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1841 return "Expiration date required"
1842 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1846 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1847 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1848 } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1849 ( $m, $y ) = ( $2, "19$1" );
1850 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1851 ( $m, $y ) = ( $3, "20$2" );
1853 return "Illegal expiration date: ". $self->paydate;
1855 $self->paydate("$y-$m-01");
1856 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1857 return gettext('expired_card')
1859 && !$ignore_expired_card
1860 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1863 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1864 ( ! $conf->exists('require_cardname')
1865 || $self->payby !~ /^(CARD|DCRD)$/ )
1867 $self->payname( $self->first. " ". $self->getfield('last') );
1869 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1870 or return gettext('illegal_name'). " payname: ". $self->payname;
1874 foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
1875 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1879 $self->otaker(getotaker) unless $self->otaker;
1881 warn "$me check AFTER: \n". $self->_dump
1884 $self->SUPER::check;
1889 Returns a list of fields which have ship_ duplicates.
1894 qw( last first company
1895 address1 address2 city county state zip country
1900 =item has_ship_address
1902 Returns true if this customer record has a separate shipping address.
1906 sub has_ship_address {
1908 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
1911 =item all_pkgs [ EXTRA_QSEARCH_PARAMS_HASHREF ]
1913 Returns all packages (see L<FS::cust_pkg>) for this customer.
1919 my $extra_qsearch = ref($_[0]) ? shift : {};
1921 return $self->num_pkgs unless wantarray || keys(%$extra_qsearch);
1924 if ( $self->{'_pkgnum'} ) {
1925 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1927 @cust_pkg = $self->_cust_pkg($extra_qsearch);
1930 sort sort_packages @cust_pkg;
1935 Synonym for B<all_pkgs>.
1940 shift->all_pkgs(@_);
1945 Returns all locations (see L<FS::cust_location>) for this customer.
1951 qsearch('cust_location', { 'custnum' => $self->custnum } );
1954 =item ncancelled_pkgs [ EXTRA_QSEARCH_PARAMS_HASHREF ]
1956 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1960 sub ncancelled_pkgs {
1962 my $extra_qsearch = ref($_[0]) ? shift : {};
1964 return $self->num_ncancelled_pkgs unless wantarray;
1967 if ( $self->{'_pkgnum'} ) {
1969 warn "$me ncancelled_pkgs: returning cached objects"
1972 @cust_pkg = grep { ! $_->getfield('cancel') }
1973 values %{ $self->{'_pkgnum'}->cache };
1977 warn "$me ncancelled_pkgs: searching for packages with custnum ".
1978 $self->custnum. "\n"
1981 $extra_qsearch->{'extra_sql'} .= ' AND ( cancel IS NULL OR cancel = 0 ) ';
1983 @cust_pkg = $self->_cust_pkg($extra_qsearch);
1987 sort sort_packages @cust_pkg;
1993 my $extra_qsearch = ref($_[0]) ? shift : {};
1995 $extra_qsearch->{'select'} ||= '*';
1996 $extra_qsearch->{'select'} .=
1997 ',( SELECT COUNT(*) FROM cust_svc WHERE cust_pkg.pkgnum = cust_svc.pkgnum )
2001 $_->{'_num_cust_svc'} = $_->get('_num_cust_svc');
2006 'table' => 'cust_pkg',
2007 'hashref' => { 'custnum' => $self->custnum },
2012 # This should be generalized to use config options to determine order.
2015 if ( $a->get('cancel') xor $b->get('cancel') ) {
2016 return -1 if $b->get('cancel');
2017 return 1 if $a->get('cancel');
2018 #shouldn't get here...
2021 my $a_num_cust_svc = $a->num_cust_svc;
2022 my $b_num_cust_svc = $b->num_cust_svc;
2023 return 0 if !$a_num_cust_svc && !$b_num_cust_svc;
2024 return -1 if $a_num_cust_svc && !$b_num_cust_svc;
2025 return 1 if !$a_num_cust_svc && $b_num_cust_svc;
2026 my @a_cust_svc = $a->cust_svc;
2027 my @b_cust_svc = $b->cust_svc;
2028 $a_cust_svc[0]->svc_x->label cmp $b_cust_svc[0]->svc_x->label;
2033 =item suspended_pkgs
2035 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
2039 sub suspended_pkgs {
2041 grep { $_->susp } $self->ncancelled_pkgs;
2044 =item unflagged_suspended_pkgs
2046 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
2047 customer (thouse packages without the `manual_flag' set).
2051 sub unflagged_suspended_pkgs {
2053 return $self->suspended_pkgs
2054 unless dbdef->table('cust_pkg')->column('manual_flag');
2055 grep { ! $_->manual_flag } $self->suspended_pkgs;
2058 =item unsuspended_pkgs
2060 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
2065 sub unsuspended_pkgs {
2067 grep { ! $_->susp } $self->ncancelled_pkgs;
2070 =item next_bill_date
2072 Returns the next date this customer will be billed, as a UNIX timestamp, or
2073 undef if no active package has a next bill date.
2077 sub next_bill_date {
2079 min( map $_->get('bill'), grep $_->get('bill'), $self->unsuspended_pkgs );
2082 =item num_cancelled_pkgs
2084 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
2089 sub num_cancelled_pkgs {
2090 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
2093 sub num_ncancelled_pkgs {
2094 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
2098 my( $self ) = shift;
2099 my $sql = scalar(@_) ? shift : '';
2100 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
2101 my $sth = dbh->prepare(
2102 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
2103 ) or die dbh->errstr;
2104 $sth->execute($self->custnum) or die $sth->errstr;
2105 $sth->fetchrow_arrayref->[0];
2110 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
2111 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
2112 on success or a list of errors.
2118 grep { $_->unsuspend } $self->suspended_pkgs;
2123 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
2125 Returns a list: an empty list on success or a list of errors.
2131 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
2134 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2136 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
2137 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
2138 of a list of pkgparts; the hashref has the following keys:
2142 =item pkgparts - listref of pkgparts
2144 =item (other options are passed to the suspend method)
2149 Returns a list: an empty list on success or a list of errors.
2153 sub suspend_if_pkgpart {
2155 my (@pkgparts, %opt);
2156 if (ref($_[0]) eq 'HASH'){
2157 @pkgparts = @{$_[0]{pkgparts}};
2162 grep { $_->suspend(%opt) }
2163 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
2164 $self->unsuspended_pkgs;
2167 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2169 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
2170 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
2171 instead of a list of pkgparts; the hashref has the following keys:
2175 =item pkgparts - listref of pkgparts
2177 =item (other options are passed to the suspend method)
2181 Returns a list: an empty list on success or a list of errors.
2185 sub suspend_unless_pkgpart {
2187 my (@pkgparts, %opt);
2188 if (ref($_[0]) eq 'HASH'){
2189 @pkgparts = @{$_[0]{pkgparts}};
2194 grep { $_->suspend(%opt) }
2195 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
2196 $self->unsuspended_pkgs;
2199 =item cancel [ OPTION => VALUE ... ]
2201 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
2203 Available options are:
2207 =item quiet - can be set true to supress email cancellation notices.
2209 =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.
2211 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
2213 =item nobill - can be set true to skip billing if it might otherwise be done.
2217 Always returns a list: an empty list on success or a list of errors.
2221 # nb that dates are not specified as valid options to this method
2224 my( $self, %opt ) = @_;
2226 warn "$me cancel called on customer ". $self->custnum. " with options ".
2227 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
2230 return ( 'access denied' )
2231 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
2233 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
2235 #should try decryption (we might have the private key)
2236 # and if not maybe queue a job for the server that does?
2237 return ( "Can't (yet) ban encrypted credit cards" )
2238 if $self->is_encrypted($self->payinfo);
2240 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
2241 my $error = $ban->insert;
2242 return ( $error ) if $error;
2246 my @pkgs = $self->ncancelled_pkgs;
2248 if ( !$opt{nobill} && $conf->exists('bill_usage_on_cancel') ) {
2250 my $error = $self->bill( pkg_list => [ @pkgs ], cancel => 1 );
2251 warn "Error billing during cancel, custnum ". $self->custnum. ": $error"
2255 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
2256 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
2259 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
2262 sub _banned_pay_hashref {
2273 'payby' => $payby2ban{$self->payby},
2274 'payinfo' => md5_base64($self->payinfo),
2275 #don't ever *search* on reason! #'reason' =>
2281 Returns all notes (see L<FS::cust_main_note>) for this customer.
2288 qsearch( 'cust_main_note',
2289 { 'custnum' => $self->custnum },
2291 'ORDER BY _DATE DESC'
2297 Returns the agent (see L<FS::agent>) for this customer.
2303 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
2306 =item bill_and_collect
2308 Cancels and suspends any packages due, generates bills, applies payments and
2311 Warns on errors (Does not currently: If there is an error, returns the error, otherwise returns false.)
2313 Options are passed as name-value pairs. Currently available options are:
2319 Bills the customer as if it were that time. Specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion functions. For example:
2323 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
2327 Used in conjunction with the I<time> option, this option specifies the date of for the generated invoices. Other calculations, such as whether or not to generate the invoice in the first place, are not affected.
2331 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
2335 If set true, re-charges setup fees.
2339 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
2343 Options are passed to the B<bill> and B<collect> methods verbatim, so all
2344 options of those methods are also available.
2348 sub bill_and_collect {
2349 my( $self, %options ) = @_;
2351 #$options{actual_time} not $options{time} because freeside-daily -d is for
2352 #pre-printing invoices
2353 $self->cancel_expired_pkgs( $options{actual_time} );
2354 $self->suspend_adjourned_pkgs( $options{actual_time} );
2356 my $error = $self->bill( %options );
2357 warn "Error billing, custnum ". $self->custnum. ": $error" if $error;
2359 $self->apply_payments_and_credits;
2361 unless ( $conf->exists('cancelled_cust-noevents')
2362 && ! $self->num_ncancelled_pkgs
2365 $error = $self->collect( %options );
2366 warn "Error collecting, custnum". $self->custnum. ": $error" if $error;
2372 sub cancel_expired_pkgs {
2373 my ( $self, $time ) = @_;
2375 my @cancel_pkgs = $self->ncancelled_pkgs( {
2376 'extra_sql' => " AND expire IS NOT NULL AND expire > 0 AND expire <= $time "
2379 foreach my $cust_pkg ( @cancel_pkgs ) {
2380 my $cpr = $cust_pkg->last_cust_pkg_reason('expire');
2381 my $error = $cust_pkg->cancel($cpr ? ( 'reason' => $cpr->reasonnum,
2382 'reason_otaker' => $cpr->otaker
2386 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum.
2387 " for custnum ". $self->custnum. ": $error"
2393 sub suspend_adjourned_pkgs {
2394 my ( $self, $time ) = @_;
2396 my @susp_pkgs = $self->ncancelled_pkgs( {
2398 " AND ( susp IS NULL OR susp = 0 )
2399 AND ( ( bill IS NOT NULL AND bill != 0 AND bill < $time )
2400 OR ( adjourn IS NOT NULL AND adjourn != 0 AND adjourn <= $time )
2405 #only because there's no SQL test for is_prepaid :/
2407 grep { ( $_->part_pkg->is_prepaid
2412 && $_->adjourn <= $time
2418 foreach my $cust_pkg ( @susp_pkgs ) {
2419 my $cpr = $cust_pkg->last_cust_pkg_reason('adjourn')
2420 if ($cust_pkg->adjourn && $cust_pkg->adjourn < $^T);
2421 my $error = $cust_pkg->suspend($cpr ? ( 'reason' => $cpr->reasonnum,
2422 'reason_otaker' => $cpr->otaker
2427 warn "Error suspending package ". $cust_pkg->pkgnum.
2428 " for custnum ". $self->custnum. ": $error"
2436 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
2437 conjunction with the collect method by calling B<bill_and_collect>.
2439 If there is an error, returns the error, otherwise returns false.
2441 Options are passed as name-value pairs. Currently available options are:
2447 If set true, re-charges setup fees.
2451 Bills the customer as if it were that time. Specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion functions. For example:
2455 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
2459 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
2461 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
2465 A hashref of pkgparts to exclude from this billing run.
2469 Used in conjunction with the I<time> option, this option specifies the date of for the generated invoices. Other calculations, such as whether or not to generate the invoice in the first place, are not affected.
2473 This boolean value informs the us that the package is being cancelled. This
2474 typically might mean not charging the normal recurring fee but only usage
2475 fees since the last billing. Setup charges may be charged. Not all package
2476 plans support this feature (they tend to charge 0).
2483 my( $self, %options ) = @_;
2484 return '' if $self->payby eq 'COMP';
2485 warn "$me bill customer ". $self->custnum. "\n"
2488 my $time = $options{'time'} || time;
2489 my $invoice_time = $options{'invoice_time'} || $time;
2491 $options{'not_pkgpart'} ||= {};
2493 local $SIG{HUP} = 'IGNORE';
2494 local $SIG{INT} = 'IGNORE';
2495 local $SIG{QUIT} = 'IGNORE';
2496 local $SIG{TERM} = 'IGNORE';
2497 local $SIG{TSTP} = 'IGNORE';
2498 local $SIG{PIPE} = 'IGNORE';
2500 my $oldAutoCommit = $FS::UID::AutoCommit;
2501 local $FS::UID::AutoCommit = 0;
2504 $self->select_for_update; #mutex
2506 my $error = $self->do_cust_event(
2507 'debug' => ( $options{'debug'} || 0 ),
2508 'time' => $invoice_time,
2509 'check_freq' => $options{'check_freq'},
2510 'stage' => 'pre-bill',
2513 $dbh->rollback if $oldAutoCommit;
2517 my @cust_bill_pkg = ();
2520 # find the packages which are due for billing, find out how much they are
2521 # & generate invoice database.
2524 my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
2526 my @precommit_hooks = ();
2528 $options{'pkg_list'} ||= [ $self->ncancelled_pkgs ]; #param checks?
2529 foreach my $cust_pkg ( @{ $options{'pkg_list'} } ) {
2531 next if $options{'not_pkgpart'}->{$cust_pkg->pkgpart};
2533 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
2535 #? to avoid use of uninitialized value errors... ?
2536 $cust_pkg->setfield('bill', '')
2537 unless defined($cust_pkg->bill);
2539 #my $part_pkg = $cust_pkg->part_pkg;
2541 my $real_pkgpart = $cust_pkg->pkgpart;
2542 my %hash = $cust_pkg->hash;
2544 foreach my $part_pkg ( $cust_pkg->part_pkg->self_and_bill_linked ) {
2546 $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
2549 $self->_make_lines( 'part_pkg' => $part_pkg,
2550 'cust_pkg' => $cust_pkg,
2551 'precommit_hooks' => \@precommit_hooks,
2552 'line_items' => \@cust_bill_pkg,
2553 'setup' => \$total_setup,
2554 'recur' => \$total_recur,
2555 'tax_matrix' => \%taxlisthash,
2557 'real_pkgpart' => $real_pkgpart,
2558 'options' => \%options,
2561 $dbh->rollback if $oldAutoCommit;
2565 } #foreach my $part_pkg
2567 } #foreach my $cust_pkg
2569 unless ( @cust_bill_pkg ) { #don't create an invoice w/o line items
2570 #but do commit any package date cycling that happened
2571 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2575 if ( scalar( grep { $_->recur && $_->recur > 0 } @cust_bill_pkg) ||
2576 !$conf->exists('postal_invoice-recurring_only')
2580 my $postal_pkg = $self->charge_postal_fee();
2581 if ( $postal_pkg && !ref( $postal_pkg ) ) {
2583 $dbh->rollback if $oldAutoCommit;
2584 return "can't charge postal invoice fee for customer ".
2585 $self->custnum. ": $postal_pkg";
2587 } elsif ( $postal_pkg ) {
2589 my $real_pkgpart = $postal_pkg->pkgpart;
2590 foreach my $part_pkg ( $postal_pkg->part_pkg->self_and_bill_linked ) {
2591 my %postal_options = %options;
2592 delete $postal_options{cancel};
2594 $self->_make_lines( 'part_pkg' => $part_pkg,
2595 'cust_pkg' => $postal_pkg,
2596 'precommit_hooks' => \@precommit_hooks,
2597 'line_items' => \@cust_bill_pkg,
2598 'setup' => \$total_setup,
2599 'recur' => \$total_recur,
2600 'tax_matrix' => \%taxlisthash,
2602 'real_pkgpart' => $real_pkgpart,
2603 'options' => \%postal_options,
2606 $dbh->rollback if $oldAutoCommit;
2615 warn "having a look at the taxes we found...\n" if $DEBUG > 2;
2617 # keys are tax names (as printed on invoices / itemdesc )
2618 # values are listrefs of taxlisthash keys (internal identifiers)
2621 # keys are taxlisthash keys (internal identifiers)
2622 # values are (cumulative) amounts
2625 # keys are taxlisthash keys (internal identifiers)
2626 # values are listrefs of cust_bill_pkg_tax_location hashrefs
2627 my %tax_location = ();
2629 # keys are taxlisthash keys (internal identifiers)
2630 # values are listrefs of cust_bill_pkg_tax_rate_location hashrefs
2631 my %tax_rate_location = ();
2633 foreach my $tax ( keys %taxlisthash ) {
2634 my $tax_object = shift @{ $taxlisthash{$tax} };
2635 warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
2636 warn " ". join('/', @{ $taxlisthash{$tax} } ). "\n" if $DEBUG > 2;
2637 my $hashref_or_error =
2638 $tax_object->taxline( $taxlisthash{$tax},
2639 'custnum' => $self->custnum,
2640 'invoice_time' => $invoice_time
2642 unless ( ref($hashref_or_error) ) {
2643 $dbh->rollback if $oldAutoCommit;
2644 return $hashref_or_error;
2646 unshift @{ $taxlisthash{$tax} }, $tax_object;
2648 my $name = $hashref_or_error->{'name'};
2649 my $amount = $hashref_or_error->{'amount'};
2651 #warn "adding $amount as $name\n";
2652 $taxname{ $name } ||= [];
2653 push @{ $taxname{ $name } }, $tax;
2655 $tax{ $tax } += $amount;
2657 $tax_location{ $tax } ||= [];
2658 if ( $tax_object->get('pkgnum') || $tax_object->get('locationnum') ) {
2659 push @{ $tax_location{ $tax } },
2661 'taxnum' => $tax_object->taxnum,
2662 'taxtype' => ref($tax_object),
2663 'pkgnum' => $tax_object->get('pkgnum'),
2664 'locationnum' => $tax_object->get('locationnum'),
2665 'amount' => sprintf('%.2f', $amount ),
2669 $tax_rate_location{ $tax } ||= [];
2670 if ( ref($tax_object) eq 'FS::tax_rate' ) {
2671 my $taxratelocationnum =
2672 $tax_object->tax_rate_location->taxratelocationnum;
2673 push @{ $tax_rate_location{ $tax } },
2675 'taxnum' => $tax_object->taxnum,
2676 'taxtype' => ref($tax_object),
2677 'amount' => sprintf('%.2f', $amount ),
2678 'locationtaxid' => $tax_object->location,
2679 'taxratelocationnum' => $taxratelocationnum,
2685 #move the cust_tax_exempt_pkg records to the cust_bill_pkgs we will commit
2686 my %packagemap = map { $_->pkgnum => $_ } @cust_bill_pkg;
2687 foreach my $tax ( keys %taxlisthash ) {
2688 foreach ( @{ $taxlisthash{$tax} }[1 ... scalar(@{ $taxlisthash{$tax} })] ) {
2689 next unless ref($_) eq 'FS::cust_bill_pkg';
2691 push @{ $packagemap{$_->pkgnum}->_cust_tax_exempt_pkg },
2692 splice( @{ $_->_cust_tax_exempt_pkg } );
2696 #consolidate and create tax line items
2697 warn "consolidating and generating...\n" if $DEBUG > 2;
2698 foreach my $taxname ( keys %taxname ) {
2701 my @cust_bill_pkg_tax_location = ();
2702 my @cust_bill_pkg_tax_rate_location = ();
2703 warn "adding $taxname\n" if $DEBUG > 1;
2704 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
2705 next if $seen{$taxitem}++;
2706 warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
2707 $tax += $tax{$taxitem};
2708 push @cust_bill_pkg_tax_location,
2709 map { new FS::cust_bill_pkg_tax_location $_ }
2710 @{ $tax_location{ $taxitem } };
2711 push @cust_bill_pkg_tax_rate_location,
2712 map { new FS::cust_bill_pkg_tax_rate_location $_ }
2713 @{ $tax_rate_location{ $taxitem } };
2717 $tax = sprintf('%.2f', $tax );
2718 $total_setup = sprintf('%.2f', $total_setup+$tax );
2720 my $pkg_category = qsearchs( 'pkg_category', { 'categoryname' => $taxname,
2726 if ( $pkg_category and
2727 $conf->config('invoice_latexsummary') ||
2728 $conf->config('invoice_htmlsummary')
2732 my %hash = ( 'section' => $pkg_category->categoryname );
2733 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
2737 push @cust_bill_pkg, new FS::cust_bill_pkg {
2743 'itemdesc' => $taxname,
2744 'display' => \@display,
2745 'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
2746 'cust_bill_pkg_tax_rate_location' => \@cust_bill_pkg_tax_rate_location,
2751 #add tax adjustments
2752 warn "adding tax adjustments...\n" if $DEBUG > 2;
2753 foreach my $cust_tax_adjustment (
2754 qsearch('cust_tax_adjustment', { 'custnum' => $self->custnum,
2760 my $tax = sprintf('%.2f', $cust_tax_adjustment->amount );
2761 $total_setup = sprintf('%.2f', $total_setup+$tax );
2763 my $itemdesc = $cust_tax_adjustment->taxname;
2764 $itemdesc = '' if $itemdesc eq 'Tax';
2766 push @cust_bill_pkg, new FS::cust_bill_pkg {
2772 'itemdesc' => $itemdesc,
2773 'itemcomment' => $cust_tax_adjustment->comment,
2774 'cust_tax_adjustment' => $cust_tax_adjustment,
2775 #'cust_bill_pkg_tax_location' => \@cust_bill_pkg_tax_location,
2780 my $charged = sprintf('%.2f', $total_setup + $total_recur );
2782 my @cust_bill = $self->cust_bill;
2783 my $balance = $self->balance;
2784 my $previous_balance = scalar(@cust_bill)
2785 ? $cust_bill[$#cust_bill]->billing_balance
2788 $previous_balance += $cust_bill[$#cust_bill]->charged
2789 if scalar(@cust_bill);
2790 #my $balance_adjustments =
2791 # sprintf('%.2f', $balance - $prior_prior_balance - $prior_charged);
2793 #create the new invoice
2794 my $cust_bill = new FS::cust_bill ( {
2795 'custnum' => $self->custnum,
2796 '_date' => ( $invoice_time ),
2797 'charged' => $charged,
2798 'billing_balance' => $balance,
2799 'previous_balance' => $previous_balance,
2801 $error = $cust_bill->insert;
2803 $dbh->rollback if $oldAutoCommit;
2804 return "can't create invoice for customer #". $self->custnum. ": $error";
2807 foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
2808 $cust_bill_pkg->invnum($cust_bill->invnum);
2809 my $error = $cust_bill_pkg->insert;
2811 $dbh->rollback if $oldAutoCommit;
2812 return "can't create invoice line item: $error";
2817 foreach my $hook ( @precommit_hooks ) {
2819 &{$hook}; #($self) ?
2822 $dbh->rollback if $oldAutoCommit;
2823 return "$@ running precommit hook $hook\n";
2827 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2833 my ($self, %params) = @_;
2835 my $part_pkg = $params{part_pkg} or die "no part_pkg specified";
2836 my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
2837 my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
2838 my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
2839 my $total_setup = $params{setup} or die "no setup accumulator specified";
2840 my $total_recur = $params{recur} or die "no recur accumulator specified";
2841 my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
2842 my $time = $params{'time'} or die "no time specified";
2843 my (%options) = %{$params{options}};
2846 my $real_pkgpart = $params{real_pkgpart};
2847 my %hash = $cust_pkg->hash;
2848 my $old_cust_pkg = new FS::cust_pkg \%hash;
2854 $cust_pkg->pkgpart($part_pkg->pkgpart);
2862 if ( $options{'resetup'}
2863 || ( ! $cust_pkg->setup
2864 && ( ! $cust_pkg->start_date
2865 || $cust_pkg->start_date <= $time
2867 && ( ! $conf->exists('disable_setup_suspended_pkgs')
2868 || ( $conf->exists('disable_setup_suspended_pkgs') &&
2869 ! $cust_pkg->getfield('susp')
2876 warn " bill setup\n" if $DEBUG > 1;
2879 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
2880 return "$@ running calc_setup for $cust_pkg\n"
2883 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
2885 $cust_pkg->setfield('setup', $time)
2886 unless $cust_pkg->setup;
2887 #do need it, but it won't get written to the db
2888 #|| $cust_pkg->pkgpart != $real_pkgpart;
2890 $cust_pkg->setfield('start_date', '')
2891 if $cust_pkg->start_date;
2896 # bill recurring fee
2899 #XXX unit stuff here too
2903 if ( ! $cust_pkg->get('susp')
2904 and ! $cust_pkg->get('start_date')
2905 and ( $part_pkg->getfield('freq') ne '0'
2906 && ( $cust_pkg->getfield('bill') || 0 ) <= $time
2908 || ( $part_pkg->plan eq 'voip_cdr'
2909 && $part_pkg->option('bill_every_call')
2911 || ( $options{cancel} )
2914 # XXX should this be a package event? probably. events are called
2915 # at collection time at the moment, though...
2916 $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
2917 if $part_pkg->can('reset_usage');
2918 #don't want to reset usage just cause we want a line item??
2919 #&& $part_pkg->pkgpart == $real_pkgpart;
2921 warn " bill recur\n" if $DEBUG > 1;
2924 # XXX shared with $recur_prog
2925 $sdate = ( $options{cancel} ? $cust_pkg->last_bill : $cust_pkg->bill )
2929 #over two params! lets at least switch to a hashref for the rest...
2930 my $increment_next_bill = ( $part_pkg->freq ne '0'
2931 && ( $cust_pkg->getfield('bill') || 0 ) <= $time
2932 && !$options{cancel}
2934 my %param = ( 'precommit_hooks' => $precommit_hooks,
2935 'increment_next_bill' => $increment_next_bill,
2938 my $method = $options{cancel} ? 'calc_cancel' : 'calc_recur';
2939 $recur = eval { $cust_pkg->$method( \$sdate, \@details, \%param ) };
2940 return "$@ running $method for $cust_pkg\n"
2943 if ( $increment_next_bill ) {
2945 my $next_bill = $part_pkg->add_freq($sdate);
2946 return "unparsable frequency: ". $part_pkg->freq
2947 if $next_bill == -1;
2949 #pro-rating magic - if $recur_prog fiddled $sdate, want to use that
2950 # only for figuring next bill date, nothing else, so, reset $sdate again
2952 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2953 #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
2954 $cust_pkg->last_bill($sdate);
2956 $cust_pkg->setfield('bill', $next_bill );
2962 warn "\$setup is undefined" unless defined($setup);
2963 warn "\$recur is undefined" unless defined($recur);
2964 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2967 # If there's line items, create em cust_bill_pkg records
2968 # If $cust_pkg has been modified, update it (if we're a real pkgpart)
2973 if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
2974 # hmm.. and if just the options are modified in some weird price plan?
2976 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2979 my $error = $cust_pkg->replace( $old_cust_pkg,
2980 'options' => { $cust_pkg->options },
2982 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error"
2983 if $error; #just in case
2986 $setup = sprintf( "%.2f", $setup );
2987 $recur = sprintf( "%.2f", $recur );
2988 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2989 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2991 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2992 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2995 if ( $setup != 0 || $recur != 0 ) {
2997 warn " charges (setup=$setup, recur=$recur); adding line items\n"
3000 my @cust_pkg_detail = map { $_->detail } $cust_pkg->cust_pkg_detail('I');
3002 warn " adding customer package invoice detail: $_\n"
3003 foreach @cust_pkg_detail;
3005 push @details, @cust_pkg_detail;
3007 my $cust_bill_pkg = new FS::cust_bill_pkg {
3008 'pkgnum' => $cust_pkg->pkgnum,
3010 'unitsetup' => $unitsetup,
3012 'unitrecur' => $unitrecur,
3013 'quantity' => $cust_pkg->quantity,
3014 'details' => \@details,
3015 'hidden' => $part_pkg->hidden,
3018 if ( $part_pkg->option('recur_temporality', 1) eq 'preceding' ) {
3019 $cust_bill_pkg->sdate( $hash{last_bill} );
3020 $cust_bill_pkg->edate( $sdate - 86399 ); #60s*60m*24h-1
3021 $cust_bill_pkg->edate( $time ) if $options{cancel};
3022 } else { #if ( $part_pkg->option('recur_temporality', 1) eq 'upcoming' ) {
3023 $cust_bill_pkg->sdate( $sdate );
3024 $cust_bill_pkg->edate( $cust_pkg->bill );
3025 #$cust_bill_pkg->edate( $time ) if $options{cancel};
3028 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
3029 unless $part_pkg->pkgpart == $real_pkgpart;
3031 $$total_setup += $setup;
3032 $$total_recur += $recur;
3039 $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg, $options{invoice_time}, $real_pkgpart);
3040 return $error if $error;
3042 push @$cust_bill_pkgs, $cust_bill_pkg;
3044 } #if $setup != 0 || $recur != 0
3054 my $part_pkg = shift;
3055 my $taxlisthash = shift;
3056 my $cust_bill_pkg = shift;
3057 my $cust_pkg = shift;
3058 my $invoice_time = shift;
3059 my $real_pkgpart = shift;
3061 my %cust_bill_pkg = ();
3065 #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
3066 push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
3067 push @classes, 'setup' if $cust_bill_pkg->setup;
3068 push @classes, 'recur' if $cust_bill_pkg->recur;
3070 if ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
3072 if ( $conf->exists('enable_taxproducts')
3073 && ( scalar($part_pkg->part_pkg_taxoverride)
3074 || $part_pkg->has_taxproduct
3079 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
3080 return "fatal: Can't (yet) use tax-pkg_address with taxproducts";
3083 foreach my $class (@classes) {
3084 my $err_or_ref = $self->_gather_taxes( $part_pkg, $class );
3085 return $err_or_ref unless ref($err_or_ref);
3086 $taxes{$class} = $err_or_ref;
3089 unless (exists $taxes{''}) {
3090 my $err_or_ref = $self->_gather_taxes( $part_pkg, '' );
3091 return $err_or_ref unless ref($err_or_ref);
3092 $taxes{''} = $err_or_ref;
3097 my @loc_keys = qw( state county country );
3099 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
3100 my $cust_location = $cust_pkg->cust_location;
3101 %taxhash = map { $_ => $cust_location->$_() } @loc_keys;
3104 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
3107 %taxhash = map { $_ => $self->get("$prefix$_") } @loc_keys;
3110 $taxhash{'taxclass'} = $part_pkg->taxclass;
3112 my @taxes = qsearch( 'cust_main_county', \%taxhash );
3114 my %taxhash_elim = %taxhash;
3116 my @elim = qw( taxclass county state );
3117 while ( !scalar(@taxes) && scalar(@elim) ) {
3118 $taxhash_elim{ shift(@elim) } = '';
3119 @taxes = qsearch( 'cust_main_county', \%taxhash_elim );
3122 @taxes = grep { ! $_->taxname or ! $self->tax_exemption($_->taxname) }
3124 if $self->cust_main_exemption; #just to be safe
3126 if ( $conf->exists('tax-pkg_address') && $cust_pkg->locationnum ) {
3128 $_->set('pkgnum', $cust_pkg->pkgnum );
3129 $_->set('locationnum', $cust_pkg->locationnum );
3133 $taxes{''} = [ @taxes ];
3134 $taxes{'setup'} = [ @taxes ];
3135 $taxes{'recur'} = [ @taxes ];
3136 $taxes{$_} = [ @taxes ] foreach (@classes);
3138 # # maybe eliminate this entirely, along with all the 0% records
3139 # unless ( @taxes ) {
3141 # "fatal: can't find tax rate for state/county/country/taxclass ".
3142 # join('/', map $taxhash{$_}, qw(state county country taxclass) );
3145 } #if $conf->exists('enable_taxproducts') ...
3150 my $separate = $conf->exists('separate_usage');
3151 my $usage_mandate = $cust_pkg->part_pkg->option('usage_mandate', 'Hush!');
3152 if ( $separate || $cust_bill_pkg->hidden || $usage_mandate ) {
3154 my $temp_pkg = new FS::cust_pkg { pkgpart => $real_pkgpart };
3155 my %hash = $cust_bill_pkg->hidden # maybe for all bill linked?
3156 ? ( 'section' => $temp_pkg->part_pkg->categoryname )
3159 my $section = $cust_pkg->part_pkg->option('usage_section', 'Hush!');
3160 my $summary = $cust_pkg->part_pkg->option('summarize_usage', 'Hush!');
3162 push @display, new FS::cust_bill_pkg_display { type => 'S', %hash };
3163 push @display, new FS::cust_bill_pkg_display { type => 'R', %hash };
3165 push @display, new FS::cust_bill_pkg_display
3168 ( ( $usage_mandate ) ? ( 'summary' => 'Y' ) : () ),
3172 if ($separate && $section && $summary) {
3173 push @display, new FS::cust_bill_pkg_display { type => 'U',
3178 if ($usage_mandate || $section && $summary) {
3179 $hash{post_total} = 'Y';
3182 $hash{section} = $section if ($separate || $usage_mandate);
3183 push @display, new FS::cust_bill_pkg_display { type => 'U', %hash };
3186 $cust_bill_pkg->set('display', \@display);
3188 my %tax_cust_bill_pkg = $cust_bill_pkg->disintegrate;
3189 foreach my $key (keys %tax_cust_bill_pkg) {
3190 my @taxes = @{ $taxes{$key} || [] };
3191 my $tax_cust_bill_pkg = $tax_cust_bill_pkg{$key};
3193 my %localtaxlisthash = ();
3194 foreach my $tax ( @taxes ) {
3196 my $taxname = ref( $tax ). ' '. $tax->taxnum;
3197 # $taxname .= ' pkgnum'. $cust_pkg->pkgnum.
3198 # ' locationnum'. $cust_pkg->locationnum
3199 # if $conf->exists('tax-pkg_address') && $cust_pkg->locationnum;
3201 $taxlisthash->{ $taxname } ||= [ $tax ];
3202 push @{ $taxlisthash->{ $taxname } }, $tax_cust_bill_pkg;
3204 $localtaxlisthash{ $taxname } ||= [ $tax ];
3205 push @{ $localtaxlisthash{ $taxname } }, $tax_cust_bill_pkg;
3209 warn "finding taxed taxes...\n" if $DEBUG > 2;
3210 foreach my $tax ( keys %localtaxlisthash ) {
3211 my $tax_object = shift @{ $localtaxlisthash{$tax} };
3212 warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
3214 next unless $tax_object->can('tax_on_tax');
3216 foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
3217 my $totname = ref( $tot ). ' '. $tot->taxnum;
3219 warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
3221 next unless exists( $localtaxlisthash{ $totname } ); # only increase
3223 warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
3224 my $hashref_or_error =
3225 $tax_object->taxline( $localtaxlisthash{$tax},
3226 'custnum' => $self->custnum,
3227 'invoice_time' => $invoice_time,
3229 return $hashref_or_error
3230 unless ref($hashref_or_error);
3232 $taxlisthash->{ $totname } ||= [ $tot ];
3233 push @{ $taxlisthash->{ $totname } }, $hashref_or_error->{amount};
3245 my $part_pkg = shift;
3249 my $geocode = $self->geocode('cch');
3251 my @taxclassnums = map { $_->taxclassnum }
3252 $part_pkg->part_pkg_taxoverride($class);
3254 unless (@taxclassnums) {
3255 @taxclassnums = map { $_->taxclassnum }
3256 grep { $_->taxable eq 'Y' }
3257 $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
3259 warn "Found taxclassnum values of ". join(',', @taxclassnums)
3264 join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
3266 @taxes = qsearch({ 'table' => 'tax_rate',
3267 'hashref' => { 'geocode' => $geocode, },
3268 'extra_sql' => $extra_sql,
3270 if scalar(@taxclassnums);
3272 warn "Found taxes ".
3273 join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n"
3280 =item collect [ HASHREF | OPTION => VALUE ... ]
3282 (Attempt to) collect money for this customer's outstanding invoices (see
3283 L<FS::cust_bill>). Usually used after the bill method.
3285 Actions are now triggered by billing events; see L<FS::part_event> and the
3286 billing events web interface. Old-style invoice events (see
3287 L<FS::part_bill_event>) have been deprecated.
3289 If there is an error, returns the error, otherwise returns false.
3291 Options are passed as name-value pairs.
3293 Currently available options are:
3299 Use this time when deciding when to print invoices and late notices on those invoices. The default is now. It is specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion functions.
3303 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
3307 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
3311 set true to surpress email card/ACH decline notices.
3315 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
3321 # allows for one time override of normal customer billing method
3326 my( $self, %options ) = @_;
3327 my $invoice_time = $options{'invoice_time'} || time;
3330 local $SIG{HUP} = 'IGNORE';
3331 local $SIG{INT} = 'IGNORE';
3332 local $SIG{QUIT} = 'IGNORE';
3333 local $SIG{TERM} = 'IGNORE';
3334 local $SIG{TSTP} = 'IGNORE';
3335 local $SIG{PIPE} = 'IGNORE';
3337 my $oldAutoCommit = $FS::UID::AutoCommit;
3338 local $FS::UID::AutoCommit = 0;
3341 $self->select_for_update; #mutex
3344 my $balance = $self->balance;
3345 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
3348 if ( exists($options{'retry_card'}) ) {
3349 carp 'retry_card option passed to collect is deprecated; use retry';
3350 $options{'retry'} ||= $options{'retry_card'};
3352 if ( exists($options{'retry'}) && $options{'retry'} ) {
3353 my $error = $self->retry_realtime;
3355 $dbh->rollback if $oldAutoCommit;
3360 my $error = $self->do_cust_event(
3361 'debug' => ( $options{'debug'} || 0 ),
3362 'time' => $invoice_time,
3363 'check_freq' => $options{'check_freq'},
3364 'stage' => 'collect',
3367 $dbh->rollback if $oldAutoCommit;
3371 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3376 =item do_cust_event [ HASHREF | OPTION => VALUE ... ]
3378 Runs billing events; see L<FS::part_event> and the billing events web
3381 If there is an error, returns the error, otherwise returns false.
3383 Options are passed as name-value pairs.
3385 Currently available options are:
3391 Use this time when deciding when to print invoices and late notices on those invoices. The default is now. It is specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion functions.
3395 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
3399 "collect" (the default) or "pre-bill"
3403 set true to surpress email card/ACH decline notices.
3407 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
3413 # allows for one time override of normal customer billing method
3417 # Retry card/echeck/LEC transactions even when not scheduled by invoice events.
3420 my( $self, %options ) = @_;
3421 my $time = $options{'time'} || time;
3424 local $SIG{HUP} = 'IGNORE';
3425 local $SIG{INT} = 'IGNORE';
3426 local $SIG{QUIT} = 'IGNORE';
3427 local $SIG{TERM} = 'IGNORE';
3428 local $SIG{TSTP} = 'IGNORE';
3429 local $SIG{PIPE} = 'IGNORE';
3431 my $oldAutoCommit = $FS::UID::AutoCommit;
3432 local $FS::UID::AutoCommit = 0;
3435 $self->select_for_update; #mutex
3438 my $balance = $self->balance;
3439 warn "$me do_cust_event customer ". $self->custnum. ": balance $balance\n"
3442 # if ( exists($options{'retry_card'}) ) {
3443 # carp 'retry_card option passed to collect is deprecated; use retry';
3444 # $options{'retry'} ||= $options{'retry_card'};
3446 # if ( exists($options{'retry'}) && $options{'retry'} ) {
3447 # my $error = $self->retry_realtime;
3449 # $dbh->rollback if $oldAutoCommit;
3454 # false laziness w/pay_batch::import_results
3456 my $due_cust_event = $self->due_cust_event(
3457 'debug' => ( $options{'debug'} || 0 ),
3459 'check_freq' => $options{'check_freq'},
3460 'stage' => ( $options{'stage'} || 'collect' ),
3462 unless( ref($due_cust_event) ) {
3463 $dbh->rollback if $oldAutoCommit;
3464 return $due_cust_event;
3467 foreach my $cust_event ( @$due_cust_event ) {
3471 #re-eval event conditions (a previous event could have changed things)
3472 unless ( $cust_event->test_conditions( 'time' => $time ) ) {
3473 #don't leave stray "new/locked" records around
3474 my $error = $cust_event->delete;
3476 #gah, even with transactions
3477 $dbh->commit if $oldAutoCommit; #well.
3484 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
3485 warn " running cust_event ". $cust_event->eventnum. "\n"
3489 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
3490 if ( my $error = $cust_event->do_event() ) {
3491 #XXX wtf is this? figure out a proper dealio with return value
3493 # gah, even with transactions.
3494 $dbh->commit if $oldAutoCommit; #well.
3501 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3506 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
3508 Inserts database records for and returns an ordered listref of new events due
3509 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
3510 events are due, an empty listref is returned. If there is an error, returns a
3511 scalar error message.
3513 To actually run the events, call each event's test_condition method, and if
3514 still true, call the event's do_event method.
3516 Options are passed as a hashref or as a list of name-value pairs. Available
3523 Search only for events of this check frequency (how often events of this type are checked); currently "1d" (daily, the default) and "1m" (monthly) are recognized.
3527 "collect" (the default) or "pre-bill"
3531 "Current time" for the events.
3535 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
3539 Only return events for the specified eventtable (by default, events of all eventtables are returned)
3543 Explicitly pass the objects to be tested (typically used with eventtable).
3547 Set to true to return the objects, but not actually insert them into the
3554 sub due_cust_event {
3556 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
3559 #my $DEBUG = $opt{'debug'}
3560 local($DEBUG) = $opt{'debug'}
3561 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
3563 warn "$me due_cust_event called with options ".
3564 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
3567 $opt{'time'} ||= time;
3569 local $SIG{HUP} = 'IGNORE';
3570 local $SIG{INT} = 'IGNORE';
3571 local $SIG{QUIT} = 'IGNORE';
3572 local $SIG{TERM} = 'IGNORE';
3573 local $SIG{TSTP} = 'IGNORE';
3574 local $SIG{PIPE} = 'IGNORE';
3576 my $oldAutoCommit = $FS::UID::AutoCommit;
3577 local $FS::UID::AutoCommit = 0;
3580 $self->select_for_update #mutex
3581 unless $opt{testonly};
3584 # find possible events (initial search)
3587 my @cust_event = ();
3589 my @eventtable = $opt{'eventtable'}
3590 ? ( $opt{'eventtable'} )
3591 : FS::part_event->eventtables_runorder;
3593 foreach my $eventtable ( @eventtable ) {
3596 if ( $opt{'objects'} ) {
3598 @objects = @{ $opt{'objects'} };
3602 #my @objects = $self->eventtable(); # sub cust_main { @{ [ $self ] }; }
3603 @objects = ( $eventtable eq 'cust_main' )
3605 : ( $self->$eventtable() );
3609 my @e_cust_event = ();
3611 my $cross = "CROSS JOIN $eventtable";
3612 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
3613 unless $eventtable eq 'cust_main';
3615 foreach my $object ( @objects ) {
3617 #this first search uses the condition_sql magic for optimization.
3618 #the more possible events we can eliminate in this step the better
3620 my $cross_where = '';
3621 my $pkey = $object->primary_key;
3622 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
3624 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
3626 FS::part_event_condition->where_conditions_sql( $eventtable,
3627 'time'=>$opt{'time'}
3629 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
3631 $extra_sql = "AND $extra_sql" if $extra_sql;
3633 #here is the agent virtualization
3634 $extra_sql .= " AND ( part_event.agentnum IS NULL
3635 OR part_event.agentnum = ". $self->agentnum. ' )';
3637 $extra_sql .= " $order";
3639 warn "searching for events for $eventtable ". $object->$pkey. "\n"
3640 if $opt{'debug'} > 2;
3641 my @part_event = qsearch( {
3642 'debug' => ( $opt{'debug'} > 3 ? 1 : 0 ),
3643 'select' => 'part_event.*',
3644 'table' => 'part_event',
3645 'addl_from' => "$cross $join",
3646 'hashref' => { 'check_freq' => ( $opt{'check_freq'} || '1d' ),
3647 'eventtable' => $eventtable,
3650 'extra_sql' => "AND $cross_where $extra_sql",
3654 my $pkey = $object->primary_key;
3655 warn " ". scalar(@part_event).
3656 " possible events found for $eventtable ". $object->$pkey(). "\n";
3659 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
3663 warn " ". scalar(@e_cust_event).
3664 " subtotal possible cust events found for $eventtable\n"
3667 push @cust_event, @e_cust_event;
3671 warn " ". scalar(@cust_event).
3672 " total possible cust events found in initial search\n"
3680 $opt{stage} ||= 'collect';
3682 grep { my $stage = $_->part_event->event_stage;
3683 $opt{stage} eq $stage or ( ! $stage && $opt{stage} eq 'collect' )
3693 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
3694 'stats_hashref' => \%unsat ),
3697 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
3700 warn " invalid conditions not eliminated with condition_sql:\n".
3701 join('', map " $_: ".$unsat{$_}."\n", keys %unsat )
3708 unless( $opt{testonly} ) {
3709 foreach my $cust_event ( @cust_event ) {
3711 my $error = $cust_event->insert();
3713 $dbh->rollback if $oldAutoCommit;
3720 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3726 warn " returning events: ". Dumper(@cust_event). "\n"
3733 =item retry_realtime
3735 Schedules realtime / batch credit card / electronic check / LEC billing
3736 events for for retry. Useful if card information has changed or manual
3737 retry is desired. The 'collect' method must be called to actually retry
3740 Implementation details: For either this customer, or for each of this
3741 customer's open invoices, changes the status of the first "done" (with
3742 statustext error) realtime processing event to "failed".
3746 sub retry_realtime {
3749 local $SIG{HUP} = 'IGNORE';
3750 local $SIG{INT} = 'IGNORE';
3751 local $SIG{QUIT} = 'IGNORE';
3752 local $SIG{TERM} = 'IGNORE';
3753 local $SIG{TSTP} = 'IGNORE';
3754 local $SIG{PIPE} = 'IGNORE';
3756 my $oldAutoCommit = $FS::UID::AutoCommit;
3757 local $FS::UID::AutoCommit = 0;
3760 #a little false laziness w/due_cust_event (not too bad, really)
3762 my $join = FS::part_event_condition->join_conditions_sql;
3763 my $order = FS::part_event_condition->order_conditions_sql;
3766 . join ( ' OR ' , map {
3767 "( part_event.eventtable = " . dbh->quote($_)
3768 . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
3769 } FS::part_event->eventtables)
3772 #here is the agent virtualization
3773 my $agent_virt = " ( part_event.agentnum IS NULL
3774 OR part_event.agentnum = ". $self->agentnum. ' )';
3776 #XXX this shouldn't be hardcoded, actions should declare it...
3777 my @realtime_events = qw(
3778 cust_bill_realtime_card
3779 cust_bill_realtime_check
3780 cust_bill_realtime_lec
3784 my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
3789 my @cust_event = qsearchs({
3790 'table' => 'cust_event',
3791 'select' => 'cust_event.*',
3792 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
3793 'hashref' => { 'status' => 'done' },
3794 'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
3795 " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
3798 my %seen_invnum = ();
3799 foreach my $cust_event (@cust_event) {
3801 #max one for the customer, one for each open invoice
3802 my $cust_X = $cust_event->cust_X;
3803 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
3807 or $cust_event->part_event->eventtable eq 'cust_bill'
3810 my $error = $cust_event->retry;
3812 $dbh->rollback if $oldAutoCommit;
3813 return "error scheduling event for retry: $error";
3818 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3823 # some horrid false laziness here to avoid refactor fallout
3824 # eventually realtime realtime_bop and realtime_refund_bop should go
3825 # away and be replaced by _new_realtime_bop and _new_realtime_refund_bop
3827 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
3829 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
3830 via a Business::OnlinePayment realtime gateway. See
3831 L<http://420.am/business-onlinepayment> for supported gateways.
3833 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3835 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
3837 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3838 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3839 if set, will override the value from the customer record.
3841 I<description> is a free-text field passed to the gateway. It defaults to
3842 "Internet services".
3844 If an I<invnum> is specified, this payment (if successful) is applied to the
3845 specified invoice. If you don't specify an I<invnum> you might want to
3846 call the B<apply_payments> method.
3848 I<quiet> can be set true to surpress email decline notices.
3850 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
3851 resulting paynum, if any.
3853 I<payunique> is a unique identifier for this payment.
3855 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
3862 return $self->_new_realtime_bop(@_)
3863 if $self->_new_bop_required();
3865 my( $method, $amount, %options ) = @_;
3867 warn "$me realtime_bop: $method $amount\n";
3868 warn " $_ => $options{$_}\n" foreach keys %options;
3871 $options{'description'} ||= 'Internet services';
3873 return $self->fake_bop($method, $amount, %options) if $options{'fake'};
3875 eval "use Business::OnlinePayment";
3878 my $payinfo = exists($options{'payinfo'})
3879 ? $options{'payinfo'}
3882 my %method2payby = (
3889 # check for banned credit card/ACH
3892 my $ban = qsearchs('banned_pay', {
3893 'payby' => $method2payby{$method},
3894 'payinfo' => md5_base64($payinfo),
3896 return "Banned credit card" if $ban;
3899 # set taxclass and trans_is_recur based on invnum if there is one
3903 my $trans_is_recur = 0;
3904 if ( $options{'invnum'} ) {
3906 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
3907 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
3910 map { $_->part_pkg }
3912 map { $_->cust_pkg }
3913 $cust_bill->cust_bill_pkg;
3915 my @taxclasses = map $_->taxclass, @part_pkg;
3916 $taxclass = $taxclasses[0]
3917 unless grep { $taxclasses[0] ne $_ } @taxclasses; #unless there are
3918 #different taxclasses
3920 if grep { $_->freq ne '0' } @part_pkg;
3928 #look for an agent gateway override first
3930 if ( $method eq 'CC' ) {
3931 $cardtype = cardtype($payinfo);
3932 } elsif ( $method eq 'ECHECK' ) {
3935 $cardtype = $method;
3939 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3940 cardtype => $cardtype,
3941 taxclass => $taxclass, } )
3942 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3944 taxclass => $taxclass, } )
3945 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3946 cardtype => $cardtype,
3948 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3950 taxclass => '', } );
3952 my $payment_gateway = '';
3953 my( $processor, $login, $password, $action, @bop_options );
3954 if ( $override ) { #use a payment gateway override
3956 $payment_gateway = $override->payment_gateway;
3958 $processor = $payment_gateway->gateway_module;
3959 $login = $payment_gateway->gateway_username;
3960 $password = $payment_gateway->gateway_password;
3961 $action = $payment_gateway->gateway_action;
3962 @bop_options = $payment_gateway->options;
3964 } else { #use the standard settings from the config
3966 ( $processor, $login, $password, $action, @bop_options ) =
3967 $self->default_payment_gateway($method);
3975 my $address = exists($options{'address1'})
3976 ? $options{'address1'}
3978 my $address2 = exists($options{'address2'})
3979 ? $options{'address2'}
3981 $address .= ", ". $address2 if length($address2);
3983 my $o_payname = exists($options{'payname'})
3984 ? $options{'payname'}
3986 my($payname, $payfirst, $paylast);
3987 if ( $o_payname && $method ne 'ECHECK' ) {
3988 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3989 or return "Illegal payname $payname";
3990 ($payfirst, $paylast) = ($1, $2);
3992 $payfirst = $self->getfield('first');
3993 $paylast = $self->getfield('last');
3994 $payname = "$payfirst $paylast";
3997 my @invoicing_list = $self->invoicing_list_emailonly;
3998 if ( $conf->exists('emailinvoiceautoalways')
3999 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
4000 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
4001 push @invoicing_list, $self->all_emails;
4004 my $email = ($conf->exists('business-onlinepayment-email-override'))
4005 ? $conf->config('business-onlinepayment-email-override')
4006 : $invoicing_list[0];
4010 my $payip = exists($options{'payip'})
4013 $content{customer_ip} = $payip
4016 $content{invoice_number} = $options{'invnum'}
4017 if exists($options{'invnum'}) && length($options{'invnum'});
4019 $content{email_customer} =
4020 ( $conf->exists('business-onlinepayment-email_customer')
4021 || $conf->exists('business-onlinepayment-email-override') );
4024 if ( $method eq 'CC' ) {
4026 $content{card_number} = $payinfo;
4027 $paydate = exists($options{'paydate'})
4028 ? $options{'paydate'}
4030 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
4031 $content{expiration} = "$2/$1";
4033 my $paycvv = exists($options{'paycvv'})
4034 ? $options{'paycvv'}
4036 $content{cvv2} = $paycvv
4039 my $paystart_month = exists($options{'paystart_month'})
4040 ? $options{'paystart_month'}
4041 : $self->paystart_month;
4043 my $paystart_year = exists($options{'paystart_year'})
4044 ? $options{'paystart_year'}
4045 : $self->paystart_year;
4047 $content{card_start} = "$paystart_month/$paystart_year"
4048 if $paystart_month && $paystart_year;
4050 my $payissue = exists($options{'payissue'})
4051 ? $options{'payissue'}
4053 $content{issue_number} = $payissue if $payissue;
4055 if ( $self->_bop_recurring_billing( 'payinfo' => $payinfo,
4056 'trans_is_recur' => $trans_is_recur,
4060 $content{recurring_billing} = 'YES';
4061 $content{acct_code} = 'rebill'
4062 if $conf->exists('credit_card-recurring_billing_acct_code');
4065 } elsif ( $method eq 'ECHECK' ) {
4066 ( $content{account_number}, $content{routing_code} ) =
4067 split('@', $payinfo);
4068 $content{bank_name} = $o_payname;
4069 $content{bank_state} = exists($options{'paystate'})
4070 ? $options{'paystate'}
4071 : $self->getfield('paystate');
4072 $content{account_type} = exists($options{'paytype'})
4073 ? uc($options{'paytype'}) || 'CHECKING'
4074 : uc($self->getfield('paytype')) || 'CHECKING';
4075 $content{account_name} = $payname;
4076 $content{customer_org} = $self->company ? 'B' : 'I';
4077 $content{state_id} = exists($options{'stateid'})
4078 ? $options{'stateid'}
4079 : $self->getfield('stateid');
4080 $content{state_id_state} = exists($options{'stateid_state'})
4081 ? $options{'stateid_state'}
4082 : $self->getfield('stateid_state');
4083 $content{customer_ssn} = exists($options{'ss'})
4086 } elsif ( $method eq 'LEC' ) {
4087 $content{phone} = $payinfo;
4091 # run transaction(s)
4094 my $balance = exists( $options{'balance'} )
4095 ? $options{'balance'}
4098 $self->select_for_update; #mutex ... just until we get our pending record in
4100 #the checks here are intended to catch concurrent payments
4101 #double-form-submission prevention is taken care of in cust_pay_pending::check
4104 return "The customer's balance has changed; $method transaction aborted."
4105 if $self->balance < $balance;
4106 #&& $self->balance < $amount; #might as well anyway?
4108 #also check and make sure there aren't *other* pending payments for this cust
4110 my @pending = qsearch('cust_pay_pending', {
4111 'custnum' => $self->custnum,
4112 'status' => { op=>'!=', value=>'done' }
4114 return "A payment is already being processed for this customer (".
4115 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
4116 "); $method transaction aborted."
4117 if scalar(@pending);
4119 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
4121 my $cust_pay_pending = new FS::cust_pay_pending {
4122 'custnum' => $self->custnum,
4123 #'invnum' => $options{'invnum'},
4126 'payby' => $method2payby{$method},
4127 'payinfo' => $payinfo,
4128 'paydate' => $paydate,
4129 'recurring_billing' => $content{recurring_billing},
4130 'pkgnum' => $options{'pkgnum'},
4132 'gatewaynum' => ( $payment_gateway ? $payment_gateway->gatewaynum : '' ),
4134 $cust_pay_pending->payunique( $options{payunique} )
4135 if defined($options{payunique}) && length($options{payunique});
4136 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
4137 return $cpp_new_err if $cpp_new_err;
4139 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
4141 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
4142 $transaction->content(
4145 'password' => $password,
4146 'action' => $action1,
4147 'description' => $options{'description'},
4148 'amount' => $amount,
4149 #'invoice_number' => $options{'invnum'},
4150 'customer_id' => $self->custnum,
4151 'last_name' => $paylast,
4152 'first_name' => $payfirst,
4154 'address' => $address,
4155 'city' => ( exists($options{'city'})
4158 'state' => ( exists($options{'state'})
4161 'zip' => ( exists($options{'zip'})
4164 'country' => ( exists($options{'country'})
4165 ? $options{'country'}
4167 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
4169 'phone' => $self->daytime || $self->night,
4173 $cust_pay_pending->status('pending');
4174 my $cpp_pending_err = $cust_pay_pending->replace;
4175 return $cpp_pending_err if $cpp_pending_err;
4178 my $BOP_TESTING = 0;
4179 my $BOP_TESTING_SUCCESS = 1;
4181 unless ( $BOP_TESTING ) {
4182 $transaction->submit();
4184 if ( $BOP_TESTING_SUCCESS ) {
4185 $transaction->is_success(1);
4186 $transaction->authorization('fake auth');
4188 $transaction->is_success(0);
4189 $transaction->error_message('fake failure');
4193 if ( $transaction->is_success() && $action2 ) {
4195 $cust_pay_pending->status('authorized');
4196 my $cpp_authorized_err = $cust_pay_pending->replace;
4197 return $cpp_authorized_err if $cpp_authorized_err;
4199 my $auth = $transaction->authorization;
4200 my $ordernum = $transaction->can('order_number')
4201 ? $transaction->order_number
4205 new Business::OnlinePayment( $processor, @bop_options );
4212 password => $password,
4213 order_number => $ordernum,
4215 authorization => $auth,
4216 description => $options{'description'},
4219 foreach my $field (qw( authorization_source_code returned_ACI
4220 transaction_identifier validation_code
4221 transaction_sequence_num local_transaction_date
4222 local_transaction_time AVS_result_code )) {
4223 $capture{$field} = $transaction->$field() if $transaction->can($field);
4226 $capture->content( %capture );
4230 unless ( $capture->is_success ) {
4231 my $e = "Authorization successful but capture failed, custnum #".
4232 $self->custnum. ': '. $capture->result_code.
4233 ": ". $capture->error_message;
4240 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
4241 my $cpp_captured_err = $cust_pay_pending->replace;
4242 return $cpp_captured_err if $cpp_captured_err;
4245 # remove paycvv after initial transaction
4248 #false laziness w/misc/process/payment.cgi - check both to make sure working
4250 if ( defined $self->dbdef_table->column('paycvv')
4251 && length($self->paycvv)
4252 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
4254 my $error = $self->remove_cvv;
4256 warn "WARNING: error removing cvv: $error\n";
4264 if ( $transaction->is_success() ) {
4267 if ( $payment_gateway ) { # agent override
4268 $paybatch = $payment_gateway->gatewaynum. '-';
4271 $paybatch .= "$processor:". $transaction->authorization;
4273 $paybatch .= ':'. $transaction->order_number
4274 if $transaction->can('order_number')
4275 && length($transaction->order_number);
4277 my $cust_pay = new FS::cust_pay ( {
4278 'custnum' => $self->custnum,
4279 'invnum' => $options{'invnum'},
4282 'payby' => $method2payby{$method},
4283 'payinfo' => $payinfo,
4284 'paybatch' => $paybatch,
4285 'paydate' => $paydate,
4286 'pkgnum' => $options{'pkgnum'},
4288 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
4289 $cust_pay->payunique( $options{payunique} )
4290 if defined($options{payunique}) && length($options{payunique});
4292 my $oldAutoCommit = $FS::UID::AutoCommit;
4293 local $FS::UID::AutoCommit = 0;
4296 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
4298 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
4301 $cust_pay->invnum(''); #try again with no specific invnum
4302 my $error2 = $cust_pay->insert( $options{'manual'} ?
4303 ( 'manual' => 1 ) : ()
4306 # gah. but at least we have a record of the state we had to abort in
4307 # from cust_pay_pending now.
4308 my $e = "WARNING: $method captured but payment not recorded - ".
4309 "error inserting payment ($processor): $error2".
4310 " (previously tried insert with invnum #$options{'invnum'}" .
4311 ": $error ) - pending payment saved as paypendingnum ".
4312 $cust_pay_pending->paypendingnum. "\n";
4318 if ( $options{'paynum_ref'} ) {
4319 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
4322 $cust_pay_pending->status('done');
4323 $cust_pay_pending->statustext('captured');
4324 $cust_pay_pending->paynum($cust_pay->paynum);
4325 my $cpp_done_err = $cust_pay_pending->replace;
4327 if ( $cpp_done_err ) {
4329 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
4330 my $e = "WARNING: $method captured but payment not recorded - ".
4331 "error updating status for paypendingnum ".
4332 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
4338 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4339 return ''; #no error
4345 my $perror = "$processor error: ". $transaction->error_message;
4347 unless ( $transaction->error_message ) {
4350 if ( $transaction->can('response_page') ) {
4352 'page' => ( $transaction->can('response_page')
4353 ? $transaction->response_page
4356 'code' => ( $transaction->can('response_code')
4357 ? $transaction->response_code
4360 'headers' => ( $transaction->can('response_headers')
4361 ? $transaction->response_headers
4367 "No additional debugging information available for $processor";
4370 $perror .= "No error_message returned from $processor -- ".
4371 ( ref($t_response) ? Dumper($t_response) : $t_response );
4375 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
4376 && $conf->exists('emaildecline')
4377 && grep { $_ ne 'POST' } $self->invoicing_list
4378 && ! grep { $transaction->error_message =~ /$_/ }
4379 $conf->config('emaildecline-exclude')
4381 my @templ = $conf->config('declinetemplate');
4382 my $template = new Text::Template (
4384 SOURCE => [ map "$_\n", @templ ],
4385 ) or return "($perror) can't create template: $Text::Template::ERROR";
4386 $template->compile()
4387 or return "($perror) can't compile template: $Text::Template::ERROR";
4391 scalar( $conf->config('company_name', $self->agentnum ) ),
4392 'company_address' =>
4393 join("\n", $conf->config('company_address', $self->agentnum ) ),
4394 'error' => $transaction->error_message,
4397 my $error = send_email(
4398 'from' => $conf->config('invoice_from', $self->agentnum ),
4399 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
4400 'subject' => 'Your payment could not be processed',
4401 'body' => [ $template->fill_in(HASH => $templ_hash) ],
4404 $perror .= " (also received error sending decline notification: $error)"
4409 $cust_pay_pending->status('done');
4410 $cust_pay_pending->statustext("declined: $perror");
4411 my $cpp_done_err = $cust_pay_pending->replace;
4412 if ( $cpp_done_err ) {
4413 my $e = "WARNING: $method declined but pending payment not resolved - ".
4414 "error updating status for paypendingnum ".
4415 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
4417 $perror = "$e ($perror)";
4425 sub _bop_recurring_billing {
4426 my( $self, %opt ) = @_;
4428 my $method = $conf->config('credit_card-recurring_billing_flag');
4430 if ( $method eq 'transaction_is_recur' ) {
4432 return 1 if $opt{'trans_is_recur'};
4436 my %hash = ( 'custnum' => $self->custnum,
4441 if qsearch('cust_pay', { %hash, 'payinfo' => $opt{'payinfo'} } )
4442 || qsearch('cust_pay', { %hash, 'paymask' => $self->mask_payinfo('CARD',
4453 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
4455 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
4456 via a Business::OnlinePayment realtime gateway. See
4457 L<http://420.am/business-onlinepayment> for supported gateways.
4459 Available methods are: I<CC>, I<ECHECK> and I<LEC>
4461 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
4463 Most gateways require a reference to an original payment transaction to refund,
4464 so you probably need to specify a I<paynum>.
4466 I<amount> defaults to the original amount of the payment if not specified.
4468 I<reason> specifies a reason for the refund.
4470 I<paydate> specifies the expiration date for a credit card overriding the
4471 value from the customer record or the payment record. Specified as yyyy-mm-dd
4473 Implementation note: If I<amount> is unspecified or equal to the amount of the
4474 orignal payment, first an attempt is made to "void" the transaction via
4475 the gateway (to cancel a not-yet settled transaction) and then if that fails,
4476 the normal attempt is made to "refund" ("credit") the transaction via the
4477 gateway is attempted.
4479 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
4480 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
4481 #if set, will override the value from the customer record.
4483 #If an I<invnum> is specified, this payment (if successful) is applied to the
4484 #specified invoice. If you don't specify an I<invnum> you might want to
4485 #call the B<apply_payments> method.
4489 #some false laziness w/realtime_bop, not enough to make it worth merging
4490 #but some useful small subs should be pulled out
4491 sub realtime_refund_bop {
4494 return $self->_new_realtime_refund_bop(@_)
4495 if $self->_new_bop_required();
4497 my( $method, %options ) = @_;
4499 warn "$me realtime_refund_bop: $method refund\n";
4500 warn " $_ => $options{$_}\n" foreach keys %options;
4503 eval "use Business::OnlinePayment";
4507 # look up the original payment and optionally a gateway for that payment
4511 my $amount = $options{'amount'};
4513 my( $processor, $login, $password, @bop_options ) ;
4514 my( $auth, $order_number ) = ( '', '', '' );
4516 if ( $options{'paynum'} ) {
4518 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
4519 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
4520 or return "Unknown paynum $options{'paynum'}";
4521 $amount ||= $cust_pay->paid;
4523 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
4524 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
4525 $cust_pay->paybatch;
4526 my $gatewaynum = '';
4527 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
4529 if ( $gatewaynum ) { #gateway for the payment to be refunded
4531 my $payment_gateway =
4532 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
4533 die "payment gateway $gatewaynum not found"
4534 unless $payment_gateway;
4536 $processor = $payment_gateway->gateway_module;
4537 $login = $payment_gateway->gateway_username;
4538 $password = $payment_gateway->gateway_password;
4539 @bop_options = $payment_gateway->options;
4541 } else { #try the default gateway
4543 my( $conf_processor, $unused_action );
4544 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
4545 $self->default_payment_gateway($method);
4547 return "processor of payment $options{'paynum'} $processor does not".
4548 " match default processor $conf_processor"
4549 unless $processor eq $conf_processor;
4554 } else { # didn't specify a paynum, so look for agent gateway overrides
4555 # like a normal transaction
4558 if ( $method eq 'CC' ) {
4559 $cardtype = cardtype($self->payinfo);
4560 } elsif ( $method eq 'ECHECK' ) {
4563 $cardtype = $method;
4566 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
4567 cardtype => $cardtype,
4569 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
4571 taxclass => '', } );
4573 if ( $override ) { #use a payment gateway override
4575 my $payment_gateway = $override->payment_gateway;
4577 $processor = $payment_gateway->gateway_module;
4578 $login = $payment_gateway->gateway_username;
4579 $password = $payment_gateway->gateway_password;
4580 #$action = $payment_gateway->gateway_action;
4581 @bop_options = $payment_gateway->options;
4583 } else { #use the standard settings from the config
4586 ( $processor, $login, $password, $unused_action, @bop_options ) =
4587 $self->default_payment_gateway($method);
4592 return "neither amount nor paynum specified" unless $amount;
4597 'password' => $password,
4598 'order_number' => $order_number,
4599 'amount' => $amount,
4600 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
4602 $content{authorization} = $auth
4603 if length($auth); #echeck/ACH transactions have an order # but no auth
4604 #(at least with authorize.net)
4606 my $disable_void_after;
4607 if ($conf->exists('disable_void_after')
4608 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
4609 $disable_void_after = $1;
4612 #first try void if applicable
4613 if ( $cust_pay && $cust_pay->paid == $amount
4615 ( not defined($disable_void_after) )
4616 || ( time < ($cust_pay->_date + $disable_void_after ) )
4619 warn " attempting void\n" if $DEBUG > 1;
4620 my $void = new Business::OnlinePayment( $processor, @bop_options );
4621 $void->content( 'action' => 'void', %content );
4623 if ( $void->is_success ) {
4624 my $error = $cust_pay->void($options{'reason'});
4626 # gah, even with transactions.
4627 my $e = 'WARNING: Card/ACH voided but database not updated - '.
4628 "error voiding payment: $error";
4632 warn " void successful\n" if $DEBUG > 1;
4637 warn " void unsuccessful, trying refund\n"
4641 my $address = $self->address1;
4642 $address .= ", ". $self->address2 if $self->address2;
4644 my($payname, $payfirst, $paylast);
4645 if ( $self->payname && $method ne 'ECHECK' ) {
4646 $payname = $self->payname;
4647 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
4648 or return "Illegal payname $payname";
4649 ($payfirst, $paylast) = ($1, $2);
4651 $payfirst = $self->getfield('first');
4652 $paylast = $self->getfield('last');
4653 $payname = "$payfirst $paylast";
4656 my @invoicing_list = $self->invoicing_list_emailonly;
4657 if ( $conf->exists('emailinvoiceautoalways')
4658 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
4659 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
4660 push @invoicing_list, $self->all_emails;
4663 my $email = ($conf->exists('business-onlinepayment-email-override'))
4664 ? $conf->config('business-onlinepayment-email-override')
4665 : $invoicing_list[0];
4667 my $payip = exists($options{'payip'})
4670 $content{customer_ip} = $payip
4674 if ( $method eq 'CC' ) {
4677 $content{card_number} = $payinfo = $cust_pay->payinfo;
4678 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
4679 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
4680 ($content{expiration} = "$2/$1"); # where available
4682 $content{card_number} = $payinfo = $self->payinfo;
4683 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
4684 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
4685 $content{expiration} = "$2/$1";
4688 } elsif ( $method eq 'ECHECK' ) {
4691 $payinfo = $cust_pay->payinfo;
4693 $payinfo = $self->payinfo;
4695 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
4696 $content{bank_name} = $self->payname;
4697 $content{account_type} = 'CHECKING';
4698 $content{account_name} = $payname;
4699 $content{customer_org} = $self->company ? 'B' : 'I';
4700 $content{customer_ssn} = $self->ss;
4701 } elsif ( $method eq 'LEC' ) {
4702 $content{phone} = $payinfo = $self->payinfo;
4706 my $refund = new Business::OnlinePayment( $processor, @bop_options );
4707 my %sub_content = $refund->content(
4708 'action' => 'credit',
4709 'customer_id' => $self->custnum,
4710 'last_name' => $paylast,
4711 'first_name' => $payfirst,
4713 'address' => $address,
4714 'city' => $self->city,
4715 'state' => $self->state,
4716 'zip' => $self->zip,
4717 'country' => $self->country,
4719 'phone' => $self->daytime || $self->night,
4722 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
4726 return "$processor error: ". $refund->error_message
4727 unless $refund->is_success();
4729 my %method2payby = (
4735 my $paybatch = "$processor:". $refund->authorization;
4736 $paybatch .= ':'. $refund->order_number
4737 if $refund->can('order_number') && $refund->order_number;
4739 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
4740 my @cust_bill_pay = $cust_pay->cust_bill_pay;
4741 last unless @cust_bill_pay;
4742 my $cust_bill_pay = pop @cust_bill_pay;
4743 my $error = $cust_bill_pay->delete;
4747 my $cust_refund = new FS::cust_refund ( {
4748 'custnum' => $self->custnum,
4749 'paynum' => $options{'paynum'},
4750 'refund' => $amount,
4752 'payby' => $method2payby{$method},
4753 'payinfo' => $payinfo,
4754 'paybatch' => $paybatch,
4755 'reason' => $options{'reason'} || 'card or ACH refund',
4757 my $error = $cust_refund->insert;
4759 $cust_refund->paynum(''); #try again with no specific paynum
4760 my $error2 = $cust_refund->insert;
4762 # gah, even with transactions.
4763 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
4764 "error inserting refund ($processor): $error2".
4765 " (previously tried insert with paynum #$options{'paynum'}" .
4776 # does the configuration indicate the new bop routines are required?
4778 sub _new_bop_required {
4781 my $botpp = 'Business::OnlineThirdPartyPayment';
4784 if ( $conf->config('business-onlinepayment-namespace') eq $botpp ||
4785 scalar( grep { $_->gateway_namespace eq $botpp }
4786 qsearch( 'payment_gateway', { 'disabled' => '' } )
4795 =item realtime_collect [ OPTION => VALUE ... ]
4797 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
4798 via a Business::OnlinePayment or Business::OnlineThirdPartyPayment realtime
4799 gateway. See L<http://420.am/business-onlinepayment> and
4800 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
4802 On failure returns an error message.
4804 Returns false or a hashref upon success. The hashref contains keys popup_url reference, and collectitems. The first is a URL to which a browser should be redirected for completion of collection. The second is a reference id for the transaction suitable for the end user. The collectitems is a reference to a list of name value pairs suitable for assigning to a html form and posted to popup_url.
4806 Available options are: I<method>, I<amount>, I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>, I<pkgnum>
4808 I<method> is one of: I<CC>, I<ECHECK> and I<LEC>. If none is specified
4809 then it is deduced from the customer record.
4811 If no I<amount> is specified, then the customer balance is used.
4813 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
4814 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
4815 if set, will override the value from the customer record.
4817 I<description> is a free-text field passed to the gateway. It defaults to
4818 "Internet services".
4820 If an I<invnum> is specified, this payment (if successful) is applied to the
4821 specified invoice. If you don't specify an I<invnum> you might want to
4822 call the B<apply_payments> method.
4824 I<quiet> can be set true to surpress email decline notices.
4826 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
4827 resulting paynum, if any.
4829 I<payunique> is a unique identifier for this payment.
4831 I<session_id> is a session identifier associated with this payment.
4833 I<depend_jobnum> allows payment capture to unlock export jobs
4837 sub realtime_collect {
4838 my( $self, %options ) = @_;
4841 warn "$me realtime_collect:\n";
4842 warn " $_ => $options{$_}\n" foreach keys %options;
4845 $options{amount} = $self->balance unless exists( $options{amount} );
4846 $options{method} = FS::payby->payby2bop($self->payby)
4847 unless exists( $options{method} );
4849 return $self->realtime_bop({%options});
4853 =item _realtime_bop { [ ARG => VALUE ... ] }
4855 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
4856 via a Business::OnlinePayment realtime gateway. See
4857 L<http://420.am/business-onlinepayment> for supported gateways.
4859 Required arguments in the hashref are I<method>, and I<amount>
4861 Available methods are: I<CC>, I<ECHECK> and I<LEC>
4863 Available optional arguments are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>, I<session_id>
4865 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
4866 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
4867 if set, will override the value from the customer record.
4869 I<description> is a free-text field passed to the gateway. It defaults to
4870 "Internet services".
4872 If an I<invnum> is specified, this payment (if successful) is applied to the
4873 specified invoice. If you don't specify an I<invnum> you might want to
4874 call the B<apply_payments> method.
4876 I<quiet> can be set true to surpress email decline notices.
4878 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
4879 resulting paynum, if any.
4881 I<payunique> is a unique identifier for this payment.
4883 I<session_id> is a session identifier associated with this payment.
4885 I<depend_jobnum> allows payment capture to unlock export jobs
4887 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
4891 # some helper routines
4892 sub _payment_gateway {
4893 my ($self, $options) = @_;
4895 $options->{payment_gateway} = $self->agent->payment_gateway( %$options )
4896 unless exists($options->{payment_gateway});
4898 $options->{payment_gateway};
4902 my ($self, $options) = @_;
4905 'login' => $options->{payment_gateway}->gateway_username,
4906 'password' => $options->{payment_gateway}->gateway_password,
4911 my ($self, $options) = @_;
4913 $options->{payment_gateway}->gatewaynum
4914 ? $options->{payment_gateway}->options
4915 : @{ $options->{payment_gateway}->get('options') };
4919 my ($self, $options) = @_;
4921 $options->{description} ||= 'Internet services';
4922 $options->{payinfo} = $self->payinfo unless exists( $options->{payinfo} );
4923 $options->{invnum} ||= '';
4924 $options->{payname} = $self->payname unless exists( $options->{payname} );
4928 my ($self, $options) = @_;
4931 $content{address} = exists($options->{'address1'})
4932 ? $options->{'address1'}
4934 my $address2 = exists($options->{'address2'})
4935 ? $options->{'address2'}
4937 $content{address} .= ", ". $address2 if length($address2);
4939 my $payip = exists($options->{'payip'}) ? $options->{'payip'} : $self->payip;
4940 $content{customer_ip} = $payip if length($payip);
4942 $content{invoice_number} = $options->{'invnum'}
4943 if exists($options->{'invnum'}) && length($options->{'invnum'});
4945 $content{email_customer} =
4946 ( $conf->exists('business-onlinepayment-email_customer')
4947 || $conf->exists('business-onlinepayment-email-override') );
4949 $content{payfirst} = $self->getfield('first');
4950 $content{paylast} = $self->getfield('last');
4952 $content{account_name} = "$content{payfirst} $content{paylast}"
4953 if $options->{method} eq 'ECHECK';
4955 $content{name} = $options->{payname};
4956 $content{name} = $content{account_name} if exists($content{account_name});
4958 $content{city} = exists($options->{city})
4961 $content{state} = exists($options->{state})
4964 $content{zip} = exists($options->{zip})
4967 $content{country} = exists($options->{country})
4968 ? $options->{country}
4970 $content{referer} = 'http://cleanwhisker.420.am/'; #XXX fix referer :/
4971 $content{phone} = $self->daytime || $self->night;
4976 my %bop_method2payby = (
4982 sub _new_realtime_bop {
4986 if (ref($_[0]) eq 'HASH') {
4987 %options = %{$_[0]};
4989 my ( $method, $amount ) = ( shift, shift );
4991 $options{method} = $method;
4992 $options{amount} = $amount;
4996 warn "$me realtime_bop (new): $options{method} $options{amount}\n";
4997 warn " $_ => $options{$_}\n" foreach keys %options;
5000 return $self->fake_bop(%options) if $options{'fake'};
5002 $self->_bop_defaults(\%options);
5005 # set trans_is_recur based on invnum if there is one
5008 my $trans_is_recur = 0;
5009 if ( $options{'invnum'} ) {
5011 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
5012 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
5015 map { $_->part_pkg }
5017 map { $_->cust_pkg }
5018 $cust_bill->cust_bill_pkg;
5021 if grep { $_->freq ne '0' } @part_pkg;
5029 my $payment_gateway = $self->_payment_gateway( \%options );
5030 my $namespace = $payment_gateway->gateway_namespace;
5032 eval "use $namespace";
5036 # check for banned credit card/ACH
5039 my $ban = qsearchs('banned_pay', {
5040 'payby' => $bop_method2payby{$options{method}},
5041 'payinfo' => md5_base64($options{payinfo}),
5043 return "Banned credit card" if $ban;
5049 my (%bop_content) = $self->_bop_content(\%options);
5051 if ( $options{method} ne 'ECHECK' ) {
5052 $options{payname} =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
5053 or return "Illegal payname $options{payname}";
5054 ($bop_content{payfirst}, $bop_content{paylast}) = ($1, $2);
5057 my @invoicing_list = $self->invoicing_list_emailonly;
5058 if ( $conf->exists('emailinvoiceautoalways')
5059 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
5060 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
5061 push @invoicing_list, $self->all_emails;
5064 my $email = ($conf->exists('business-onlinepayment-email-override'))
5065 ? $conf->config('business-onlinepayment-email-override')
5066 : $invoicing_list[0];
5070 if ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'CC' ) {
5072 $content{card_number} = $options{payinfo};
5073 $paydate = exists($options{'paydate'})
5074 ? $options{'paydate'}
5076 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
5077 $content{expiration} = "$2/$1";
5079 my $paycvv = exists($options{'paycvv'})
5080 ? $options{'paycvv'}
5082 $content{cvv2} = $paycvv
5085 my $paystart_month = exists($options{'paystart_month'})
5086 ? $options{'paystart_month'}
5087 : $self->paystart_month;
5089 my $paystart_year = exists($options{'paystart_year'})
5090 ? $options{'paystart_year'}
5091 : $self->paystart_year;
5093 $content{card_start} = "$paystart_month/$paystart_year"
5094 if $paystart_month && $paystart_year;
5096 my $payissue = exists($options{'payissue'})
5097 ? $options{'payissue'}
5099 $content{issue_number} = $payissue if $payissue;
5101 if ( $self->_bop_recurring_billing( 'payinfo' => $options{'payinfo'},
5102 'trans_is_recur' => $trans_is_recur,
5106 $content{recurring_billing} = 'YES';
5107 $content{acct_code} = 'rebill'
5108 if $conf->exists('credit_card-recurring_billing_acct_code');
5111 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'ECHECK' ){
5112 ( $content{account_number}, $content{routing_code} ) =
5113 split('@', $options{payinfo});
5114 $content{bank_name} = $options{payname};
5115 $content{bank_state} = exists($options{'paystate'})
5116 ? $options{'paystate'}
5117 : $self->getfield('paystate');
5118 $content{account_type} = exists($options{'paytype'})
5119 ? uc($options{'paytype'}) || 'CHECKING'
5120 : uc($self->getfield('paytype')) || 'CHECKING';
5121 $content{customer_org} = $self->company ? 'B' : 'I';
5122 $content{state_id} = exists($options{'stateid'})
5123 ? $options{'stateid'}
5124 : $self->getfield('stateid');
5125 $content{state_id_state} = exists($options{'stateid_state'})
5126 ? $options{'stateid_state'}
5127 : $self->getfield('stateid_state');
5128 $content{customer_ssn} = exists($options{'ss'})
5131 } elsif ( $namespace eq 'Business::OnlinePayment' && $options{method} eq 'LEC' ) {
5132 $content{phone} = $options{payinfo};
5133 } elsif ( $namespace eq 'Business::OnlineThirdPartyPayment' ) {
5140 # run transaction(s)
5143 my $balance = exists( $options{'balance'} )
5144 ? $options{'balance'}
5147 $self->select_for_update; #mutex ... just until we get our pending record in
5149 #the checks here are intended to catch concurrent payments
5150 #double-form-submission prevention is taken care of in cust_pay_pending::check
5153 return "The customer's balance has changed; $options{method} transaction aborted."
5154 if $self->balance < $balance;
5155 #&& $self->balance < $options{amount}; #might as well anyway?
5157 #also check and make sure there aren't *other* pending payments for this cust
5159 my @pending = qsearch('cust_pay_pending', {
5160 'custnum' => $self->custnum,
5161 'status' => { op=>'!=', value=>'done' }
5163 return "A payment is already being processed for this customer (".
5164 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
5165 "); $options{method} transaction aborted."
5166 if scalar(@pending);
5168 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
5170 my $cust_pay_pending = new FS::cust_pay_pending {
5171 'custnum' => $self->custnum,
5172 #'invnum' => $options{'invnum'},
5173 'paid' => $options{amount},
5175 'payby' => $bop_method2payby{$options{method}},
5176 'payinfo' => $options{payinfo},
5177 'paydate' => $paydate,
5178 'recurring_billing' => $content{recurring_billing},
5179 'pkgnum' => $options{'pkgnum'},
5181 'gatewaynum' => $payment_gateway->gatewaynum || '',
5182 'session_id' => $options{session_id} || '',
5183 'jobnum' => $options{depend_jobnum} || '',
5185 $cust_pay_pending->payunique( $options{payunique} )
5186 if defined($options{payunique}) && length($options{payunique});
5187 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
5188 return $cpp_new_err if $cpp_new_err;
5190 my( $action1, $action2 ) =
5191 split( /\s*\,\s*/, $payment_gateway->gateway_action );
5193 my $transaction = new $namespace( $payment_gateway->gateway_module,
5194 $self->_bop_options(\%options),
5197 $transaction->content(
5198 'type' => $options{method},
5199 $self->_bop_auth(\%options),
5200 'action' => $action1,
5201 'description' => $options{'description'},
5202 'amount' => $options{amount},
5203 #'invoice_number' => $options{'invnum'},
5204 'customer_id' => $self->custnum,
5206 'reference' => $cust_pay_pending->paypendingnum, #for now
5211 $cust_pay_pending->status('pending');
5212 my $cpp_pending_err = $cust_pay_pending->replace;
5213 return $cpp_pending_err if $cpp_pending_err;
5216 my $BOP_TESTING = 0;
5217 my $BOP_TESTING_SUCCESS = 1;
5219 unless ( $BOP_TESTING ) {
5220 $transaction->submit();
5222 if ( $BOP_TESTING_SUCCESS ) {
5223 $transaction->is_success(1);
5224 $transaction->authorization('fake auth');
5226 $transaction->is_success(0);
5227 $transaction->error_message('fake failure');
5231 if ( $transaction->is_success() && $namespace eq 'Business::OnlineThirdPartyPayment' ) {
5233 return { reference => $cust_pay_pending->paypendingnum,
5234 map { $_ => $transaction->$_ } qw ( popup_url collectitems ) };
5236 } elsif ( $transaction->is_success() && $action2 ) {
5238 $cust_pay_pending->status('authorized');
5239 my $cpp_authorized_err = $cust_pay_pending->replace;
5240 return $cpp_authorized_err if $cpp_authorized_err;
5242 my $auth = $transaction->authorization;
5243 my $ordernum = $transaction->can('order_number')
5244 ? $transaction->order_number
5248 new Business::OnlinePayment( $payment_gateway->gateway_module,
5249 $self->_bop_options(\%options),
5254 type => $options{method},
5256 $self->_bop_auth(\%options),
5257 order_number => $ordernum,
5258 amount => $options{amount},
5259 authorization => $auth,
5260 description => $options{'description'},
5263 foreach my $field (qw( authorization_source_code returned_ACI
5264 transaction_identifier validation_code
5265 transaction_sequence_num local_transaction_date
5266 local_transaction_time AVS_result_code )) {
5267 $capture{$field} = $transaction->$field() if $transaction->can($field);
5270 $capture->content( %capture );
5274 unless ( $capture->is_success ) {
5275 my $e = "Authorization successful but capture failed, custnum #".
5276 $self->custnum. ': '. $capture->result_code.
5277 ": ". $capture->error_message;
5285 # remove paycvv after initial transaction
5288 #false laziness w/misc/process/payment.cgi - check both to make sure working
5290 if ( defined $self->dbdef_table->column('paycvv')
5291 && length($self->paycvv)
5292 && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
5294 my $error = $self->remove_cvv;
5296 warn "WARNING: error removing cvv: $error\n";
5304 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
5316 if (ref($_[0]) eq 'HASH') {
5317 %options = %{$_[0]};
5319 my ( $method, $amount ) = ( shift, shift );
5321 $options{method} = $method;
5322 $options{amount} = $amount;
5325 if ( $options{'fake_failure'} ) {
5326 return "Error: No error; test failure requested with fake_failure";
5330 #if ( $payment_gateway->gatewaynum ) { # agent override
5331 # $paybatch = $payment_gateway->gatewaynum. '-';
5334 #$paybatch .= "$processor:". $transaction->authorization;
5336 #$paybatch .= ':'. $transaction->order_number
5337 # if $transaction->can('order_number')
5338 # && length($transaction->order_number);
5340 my $paybatch = 'FakeProcessor:54:32';
5342 my $cust_pay = new FS::cust_pay ( {
5343 'custnum' => $self->custnum,
5344 'invnum' => $options{'invnum'},
5345 'paid' => $options{amount},
5347 'payby' => $bop_method2payby{$options{method}},
5348 #'payinfo' => $payinfo,
5349 'payinfo' => '4111111111111111',
5350 'paybatch' => $paybatch,
5351 #'paydate' => $paydate,
5352 'paydate' => '2012-05-01',
5354 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
5356 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
5359 $cust_pay->invnum(''); #try again with no specific invnum
5360 my $error2 = $cust_pay->insert( $options{'manual'} ?
5361 ( 'manual' => 1 ) : ()
5364 # gah, even with transactions.
5365 my $e = 'WARNING: Card/ACH debited but database not updated - '.
5366 "error inserting (fake!) payment: $error2".
5367 " (previously tried insert with invnum #$options{'invnum'}" .
5374 if ( $options{'paynum_ref'} ) {
5375 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
5378 return ''; #no error
5383 # item _realtime_bop_result CUST_PAY_PENDING, BOP_OBJECT [ OPTION => VALUE ... ]
5385 # Wraps up processing of a realtime credit card, ACH (electronic check) or
5386 # phone bill transaction.
5388 sub _realtime_bop_result {
5389 my( $self, $cust_pay_pending, $transaction, %options ) = @_;
5391 warn "$me _realtime_bop_result: pending transaction ".
5392 $cust_pay_pending->paypendingnum. "\n";
5393 warn " $_ => $options{$_}\n" foreach keys %options;
5396 my $payment_gateway = $options{payment_gateway}
5397 or return "no payment gateway in arguments to _realtime_bop_result";
5399 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
5400 my $cpp_captured_err = $cust_pay_pending->replace;
5401 return $cpp_captured_err if $cpp_captured_err;
5403 if ( $transaction->is_success() ) {
5406 if ( $payment_gateway->gatewaynum ) { # agent override
5407 $paybatch = $payment_gateway->gatewaynum. '-';
5410 $paybatch .= $payment_gateway->gateway_module. ":".
5411 $transaction->authorization;
5413 $paybatch .= ':'. $transaction->order_number
5414 if $transaction->can('order_number')
5415 && length($transaction->order_number);
5417 my $cust_pay = new FS::cust_pay ( {
5418 'custnum' => $self->custnum,
5419 'invnum' => $options{'invnum'},
5420 'paid' => $cust_pay_pending->paid,
5422 'payby' => $cust_pay_pending->payby,
5423 #'payinfo' => $payinfo,
5424 'paybatch' => $paybatch,
5425 'paydate' => $cust_pay_pending->paydate,
5426 'pkgnum' => $cust_pay_pending->pkgnum,
5428 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
5429 $cust_pay->payunique( $options{payunique} )
5430 if defined($options{payunique}) && length($options{payunique});
5432 my $oldAutoCommit = $FS::UID::AutoCommit;
5433 local $FS::UID::AutoCommit = 0;
5436 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
5438 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
5441 $cust_pay->invnum(''); #try again with no specific invnum
5442 my $error2 = $cust_pay->insert( $options{'manual'} ?
5443 ( 'manual' => 1 ) : ()
5446 # gah. but at least we have a record of the state we had to abort in
5447 # from cust_pay_pending now.
5448 my $e = "WARNING: $options{method} captured but payment not recorded -".
5449 " error inserting payment (". $payment_gateway->gateway_module.
5451 " (previously tried insert with invnum #$options{'invnum'}" .
5452 ": $error ) - pending payment saved as paypendingnum ".
5453 $cust_pay_pending->paypendingnum. "\n";
5459 my $jobnum = $cust_pay_pending->jobnum;
5461 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
5463 unless ( $placeholder ) {
5464 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
5465 my $e = "WARNING: $options{method} captured but job $jobnum not ".
5466 "found for paypendingnum ". $cust_pay_pending->paypendingnum. "\n";
5471 $error = $placeholder->delete;
5474 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
5475 my $e = "WARNING: $options{method} captured but could not delete ".
5476 "job $jobnum for paypendingnum ".
5477 $cust_pay_pending->paypendingnum. ": $error\n";
5484 if ( $options{'paynum_ref'} ) {
5485 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
5488 $cust_pay_pending->status('done');
5489 $cust_pay_pending->statustext('captured');
5490 $cust_pay_pending->paynum($cust_pay->paynum);
5491 my $cpp_done_err = $cust_pay_pending->replace;
5493 if ( $cpp_done_err ) {
5495 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
5496 my $e = "WARNING: $options{method} captured but payment not recorded - ".
5497 "error updating status for paypendingnum ".
5498 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
5504 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5505 return ''; #no error
5511 my $perror = $payment_gateway->gateway_module. " error: ".
5512 $transaction->error_message;
5514 my $jobnum = $cust_pay_pending->jobnum;
5516 my $placeholder = qsearchs( 'queue', { 'jobnum' => $jobnum } );
5518 if ( $placeholder ) {
5519 my $error = $placeholder->depended_delete;
5520 $error ||= $placeholder->delete;
5521 warn "error removing provisioning jobs after declined paypendingnum ".
5522 $cust_pay_pending->paypendingnum. "\n";
5524 my $e = "error finding job $jobnum for declined paypendingnum ".
5525 $cust_pay_pending->paypendingnum. "\n";
5531 unless ( $transaction->error_message ) {
5534 if ( $transaction->can('response_page') ) {
5536 'page' => ( $transaction->can('response_page')
5537 ? $transaction->response_page
5540 'code' => ( $transaction->can('response_code')
5541 ? $transaction->response_code
5544 'headers' => ( $transaction->can('response_headers')
5545 ? $transaction->response_headers
5551 "No additional debugging information available for ".
5552 $payment_gateway->gateway_module;
5555 $perror .= "No error_message returned from ".
5556 $payment_gateway->gateway_module. " -- ".
5557 ( ref($t_response) ? Dumper($t_response) : $t_response );
5561 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
5562 && $conf->exists('emaildecline')
5563 && grep { $_ ne 'POST' } $self->invoicing_list
5564 && ! grep { $transaction->error_message =~ /$_/ }
5565 $conf->config('emaildecline-exclude')
5567 my @templ = $conf->config('declinetemplate');
5568 my $template = new Text::Template (
5570 SOURCE => [ map "$_\n", @templ ],
5571 ) or return "($perror) can't create template: $Text::Template::ERROR";
5572 $template->compile()
5573 or return "($perror) can't compile template: $Text::Template::ERROR";
5577 scalar( $conf->config('company_name', $self->agentnum ) ),
5578 'company_address' =>
5579 join("\n", $conf->config('company_address', $self->agentnum ) ),
5580 'error' => $transaction->error_message,
5583 my $error = send_email(
5584 'from' => $conf->config('invoice_from', $self->agentnum ),
5585 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
5586 'subject' => 'Your payment could not be processed',
5587 'body' => [ $template->fill_in(HASH => $templ_hash) ],
5590 $perror .= " (also received error sending decline notification: $error)"
5595 $cust_pay_pending->status('done');
5596 $cust_pay_pending->statustext("declined: $perror");
5597 my $cpp_done_err = $cust_pay_pending->replace;
5598 if ( $cpp_done_err ) {
5599 my $e = "WARNING: $options{method} declined but pending payment not ".
5600 "resolved - error updating status for paypendingnum ".
5601 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
5603 $perror = "$e ($perror)";
5611 =item realtime_botpp_capture CUST_PAY_PENDING [ OPTION => VALUE ... ]
5613 Verifies successful third party processing of a realtime credit card,
5614 ACH (electronic check) or phone bill transaction via a
5615 Business::OnlineThirdPartyPayment realtime gateway. See
5616 L<http://420.am/business-onlinethirdpartypayment> for supported gateways.
5618 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
5620 The additional options I<payname>, I<city>, I<state>,
5621 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
5622 if set, will override the value from the customer record.
5624 I<description> is a free-text field passed to the gateway. It defaults to
5625 "Internet services".
5627 If an I<invnum> is specified, this payment (if successful) is applied to the
5628 specified invoice. If you don't specify an I<invnum> you might want to
5629 call the B<apply_payments> method.
5631 I<quiet> can be set true to surpress email decline notices.
5633 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
5634 resulting paynum, if any.
5636 I<payunique> is a unique identifier for this payment.
5638 Returns a hashref containing elements bill_error (which will be undefined
5639 upon success) and session_id of any associated session.
5643 sub realtime_botpp_capture {
5644 my( $self, $cust_pay_pending, %options ) = @_;
5646 warn "$me realtime_botpp_capture: pending transaction $cust_pay_pending\n";
5647 warn " $_ => $options{$_}\n" foreach keys %options;
5650 eval "use Business::OnlineThirdPartyPayment";
5654 # select the gateway
5657 my $method = FS::payby->payby2bop($cust_pay_pending->payby);
5659 my $payment_gateway = $cust_pay_pending->gatewaynum
5660 ? qsearchs( 'payment_gateway',
5661 { gatewaynum => $cust_pay_pending->gatewaynum }
5663 : $self->agent->payment_gateway( 'method' => $method,
5664 # 'invnum' => $cust_pay_pending->invnum,
5665 # 'payinfo' => $cust_pay_pending->payinfo,
5668 $options{payment_gateway} = $payment_gateway; # for the helper subs
5674 my @invoicing_list = $self->invoicing_list_emailonly;
5675 if ( $conf->exists('emailinvoiceautoalways')
5676 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
5677 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
5678 push @invoicing_list, $self->all_emails;
5681 my $email = ($conf->exists('business-onlinepayment-email-override'))
5682 ? $conf->config('business-onlinepayment-email-override')
5683 : $invoicing_list[0];
5687 $content{email_customer} =
5688 ( $conf->exists('business-onlinepayment-email_customer')
5689 || $conf->exists('business-onlinepayment-email-override') );
5692 # run transaction(s)
5696 new Business::OnlineThirdPartyPayment( $payment_gateway->gateway_module,
5697 $self->_bop_options(\%options),
5700 $transaction->reference({ %options });
5702 $transaction->content(
5704 $self->_bop_auth(\%options),
5705 'action' => 'Post Authorization',
5706 'description' => $options{'description'},
5707 'amount' => $cust_pay_pending->paid,
5708 #'invoice_number' => $options{'invnum'},
5709 'customer_id' => $self->custnum,
5710 'referer' => 'http://cleanwhisker.420.am/',
5711 'reference' => $cust_pay_pending->paypendingnum,
5713 'phone' => $self->daytime || $self->night,
5715 # plus whatever is required for bogus capture avoidance
5718 $transaction->submit();
5721 $self->_realtime_bop_result( $cust_pay_pending, $transaction, %options );
5724 bill_error => $error,
5725 session_id => $cust_pay_pending->session_id,
5730 =item default_payment_gateway DEPRECATED -- use agent->payment_gateway
5734 sub default_payment_gateway {
5735 my( $self, $method ) = @_;
5737 die "Real-time processing not enabled\n"
5738 unless $conf->exists('business-onlinepayment');
5740 #warn "default_payment_gateway deprecated -- use agent->payment_gateway\n";
5743 my $bop_config = 'business-onlinepayment';
5744 $bop_config .= '-ach'
5745 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
5746 my ( $processor, $login, $password, $action, @bop_options ) =
5747 $conf->config($bop_config);
5748 $action ||= 'normal authorization';
5749 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
5750 die "No real-time processor is enabled - ".
5751 "did you set the business-onlinepayment configuration value?\n"
5754 ( $processor, $login, $password, $action, @bop_options )
5759 Removes the I<paycvv> field from the database directly.
5761 If there is an error, returns the error, otherwise returns false.
5767 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
5768 or return dbh->errstr;
5769 $sth->execute($self->custnum)
5770 or return $sth->errstr;
5775 =item _new_realtime_refund_bop METHOD [ OPTION => VALUE ... ]
5777 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
5778 via a Business::OnlinePayment realtime gateway. See
5779 L<http://420.am/business-onlinepayment> for supported gateways.
5781 Available methods are: I<CC>, I<ECHECK> and I<LEC>
5783 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
5785 Most gateways require a reference to an original payment transaction to refund,
5786 so you probably need to specify a I<paynum>.
5788 I<amount> defaults to the original amount of the payment if not specified.
5790 I<reason> specifies a reason for the refund.
5792 I<paydate> specifies the expiration date for a credit card overriding the
5793 value from the customer record or the payment record. Specified as yyyy-mm-dd
5795 Implementation note: If I<amount> is unspecified or equal to the amount of the
5796 orignal payment, first an attempt is made to "void" the transaction via
5797 the gateway (to cancel a not-yet settled transaction) and then if that fails,
5798 the normal attempt is made to "refund" ("credit") the transaction via the
5799 gateway is attempted.
5801 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
5802 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
5803 #if set, will override the value from the customer record.
5805 #If an I<invnum> is specified, this payment (if successful) is applied to the
5806 #specified invoice. If you don't specify an I<invnum> you might want to
5807 #call the B<apply_payments> method.
5811 #some false laziness w/realtime_bop, not enough to make it worth merging
5812 #but some useful small subs should be pulled out
5813 sub _new_realtime_refund_bop {
5817 if (ref($_[0]) ne 'HASH') {
5818 %options = %{$_[0]};
5822 $options{method} = $method;
5826 warn "$me realtime_refund_bop (new): $options{method} refund\n";
5827 warn " $_ => $options{$_}\n" foreach keys %options;
5831 # look up the original payment and optionally a gateway for that payment
5835 my $amount = $options{'amount'};
5837 my( $processor, $login, $password, @bop_options, $namespace ) ;
5838 my( $auth, $order_number ) = ( '', '', '' );
5840 if ( $options{'paynum'} ) {
5842 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
5843 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
5844 or return "Unknown paynum $options{'paynum'}";
5845 $amount ||= $cust_pay->paid;
5847 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
5848 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
5849 $cust_pay->paybatch;
5850 my $gatewaynum = '';
5851 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
5853 if ( $gatewaynum ) { #gateway for the payment to be refunded
5855 my $payment_gateway =
5856 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
5857 die "payment gateway $gatewaynum not found"
5858 unless $payment_gateway;
5860 $processor = $payment_gateway->gateway_module;
5861 $login = $payment_gateway->gateway_username;
5862 $password = $payment_gateway->gateway_password;
5863 $namespace = $payment_gateway->gateway_namespace;
5864 @bop_options = $payment_gateway->options;
5866 } else { #try the default gateway
5869 my $payment_gateway =
5870 $self->agent->payment_gateway('method' => $options{method});
5872 ( $conf_processor, $login, $password, $namespace ) =
5873 map { my $method = "gateway_$_"; $payment_gateway->$method }
5874 qw( module username password namespace );
5876 @bop_options = $payment_gateway->gatewaynum
5877 ? $payment_gateway->options
5878 : @{ $payment_gateway->get('options') };
5880 return "processor of payment $options{'paynum'} $processor does not".
5881 " match default processor $conf_processor"
5882 unless $processor eq $conf_processor;
5887 } else { # didn't specify a paynum, so look for agent gateway overrides
5888 # like a normal transaction
5890 my $payment_gateway =
5891 $self->agent->payment_gateway( 'method' => $options{method},
5892 #'payinfo' => $payinfo,
5894 my( $processor, $login, $password, $namespace ) =
5895 map { my $method = "gateway_$_"; $payment_gateway->$method }
5896 qw( module username password namespace );
5898 my @bop_options = $payment_gateway->gatewaynum
5899 ? $payment_gateway->options
5900 : @{ $payment_gateway->get('options') };
5903 return "neither amount nor paynum specified" unless $amount;
5905 eval "use $namespace";
5909 'type' => $options{method},
5911 'password' => $password,
5912 'order_number' => $order_number,
5913 'amount' => $amount,
5914 'referer' => 'http://cleanwhisker.420.am/', #XXX fix referer :/
5916 $content{authorization} = $auth
5917 if length($auth); #echeck/ACH transactions have an order # but no auth
5918 #(at least with authorize.net)
5920 my $disable_void_after;
5921 if ($conf->exists('disable_void_after')
5922 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
5923 $disable_void_after = $1;
5926 #first try void if applicable
5927 if ( $cust_pay && $cust_pay->paid == $amount
5929 ( not defined($disable_void_after) )
5930 || ( time < ($cust_pay->_date + $disable_void_after ) )
5933 warn " attempting void\n" if $DEBUG > 1;
5934 my $void = new Business::OnlinePayment( $processor, @bop_options );
5935 $void->content( 'action' => 'void', %content );
5937 if ( $void->is_success ) {
5938 my $error = $cust_pay->void($options{'reason'});
5940 # gah, even with transactions.
5941 my $e = 'WARNING: Card/ACH voided but database not updated - '.
5942 "error voiding payment: $error";
5946 warn " void successful\n" if $DEBUG > 1;
5951 warn " void unsuccessful, trying refund\n"
5955 my $address = $self->address1;
5956 $address .= ", ". $self->address2 if $self->address2;
5958 my($payname, $payfirst, $paylast);
5959 if ( $self->payname && $options{method} ne 'ECHECK' ) {
5960 $payname = $self->payname;
5961 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
5962 or return "Illegal payname $payname";
5963 ($payfirst, $paylast) = ($1, $2);
5965 $payfirst = $self->getfield('first');
5966 $paylast = $self->getfield('last');
5967 $payname = "$payfirst $paylast";
5970 my @invoicing_list = $self->invoicing_list_emailonly;
5971 if ( $conf->exists('emailinvoiceautoalways')
5972 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
5973 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
5974 push @invoicing_list, $self->all_emails;
5977 my $email = ($conf->exists('business-onlinepayment-email-override'))
5978 ? $conf->config('business-onlinepayment-email-override')
5979 : $invoicing_list[0];
5981 my $payip = exists($options{'payip'})
5984 $content{customer_ip} = $payip
5988 if ( $options{method} eq 'CC' ) {
5991 $content{card_number} = $payinfo = $cust_pay->payinfo;
5992 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
5993 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
5994 ($content{expiration} = "$2/$1"); # where available
5996 $content{card_number} = $payinfo = $self->payinfo;
5997 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
5998 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
5999 $content{expiration} = "$2/$1";
6002 } elsif ( $options{method} eq 'ECHECK' ) {
6005 $payinfo = $cust_pay->payinfo;
6007 $payinfo = $self->payinfo;
6009 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
6010 $content{bank_name} = $self->payname;
6011 $content{account_type} = 'CHECKING';
6012 $content{account_name} = $payname;
6013 $content{customer_org} = $self->company ? 'B' : 'I';
6014 $content{customer_ssn} = $self->ss;
6015 } elsif ( $options{method} eq 'LEC' ) {
6016 $content{phone} = $payinfo = $self->payinfo;
6020 my $refund = new Business::OnlinePayment( $processor, @bop_options );
6021 my %sub_content = $refund->content(
6022 'action' => 'credit',
6023 'customer_id' => $self->custnum,
6024 'last_name' => $paylast,
6025 'first_name' => $payfirst,
6027 'address' => $address,
6028 'city' => $self->city,
6029 'state' => $self->state,
6030 'zip' => $self->zip,
6031 'country' => $self->country,
6033 'phone' => $self->daytime || $self->night,
6036 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
6040 return "$processor error: ". $refund->error_message
6041 unless $refund->is_success();
6043 my $paybatch = "$processor:". $refund->authorization;
6044 $paybatch .= ':'. $refund->order_number
6045 if $refund->can('order_number') && $refund->order_number;
6047 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
6048 my @cust_bill_pay = $cust_pay->cust_bill_pay;
6049 last unless @cust_bill_pay;
6050 my $cust_bill_pay = pop @cust_bill_pay;
6051 my $error = $cust_bill_pay->delete;
6055 my $cust_refund = new FS::cust_refund ( {
6056 'custnum' => $self->custnum,
6057 'paynum' => $options{'paynum'},
6058 'refund' => $amount,
6060 'payby' => $bop_method2payby{$options{method}},
6061 'payinfo' => $payinfo,
6062 'paybatch' => $paybatch,
6063 'reason' => $options{'reason'} || 'card or ACH refund',
6065 my $error = $cust_refund->insert;
6067 $cust_refund->paynum(''); #try again with no specific paynum
6068 my $error2 = $cust_refund->insert;
6070 # gah, even with transactions.
6071 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
6072 "error inserting refund ($processor): $error2".
6073 " (previously tried insert with paynum #$options{'paynum'}" .
6084 =item batch_card OPTION => VALUE...
6086 Adds a payment for this invoice to the pending credit card batch (see
6087 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
6088 runs the payment using a realtime gateway.
6093 my ($self, %options) = @_;
6096 if (exists($options{amount})) {
6097 $amount = $options{amount};
6099 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
6101 return '' unless $amount > 0;
6103 my $invnum = delete $options{invnum};
6104 my $payby = $options{invnum} || $self->payby; #dubious
6106 if ($options{'realtime'}) {
6107 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
6113 my $oldAutoCommit = $FS::UID::AutoCommit;
6114 local $FS::UID::AutoCommit = 0;
6117 #this needs to handle mysql as well as Pg, like svc_acct.pm
6118 #(make it into a common function if folks need to do batching with mysql)
6119 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
6120 or return "Cannot lock pay_batch: " . $dbh->errstr;
6124 'payby' => FS::payby->payby2payment($payby),
6127 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
6129 unless ( $pay_batch ) {
6130 $pay_batch = new FS::pay_batch \%pay_batch;
6131 my $error = $pay_batch->insert;
6133 $dbh->rollback if $oldAutoCommit;
6134 die "error creating new batch: $error\n";
6138 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
6139 'batchnum' => $pay_batch->batchnum,
6140 'custnum' => $self->custnum,
6143 foreach (qw( address1 address2 city state zip country payby payinfo paydate
6145 $options{$_} = '' unless exists($options{$_});
6148 my $cust_pay_batch = new FS::cust_pay_batch ( {
6149 'batchnum' => $pay_batch->batchnum,
6150 'invnum' => $invnum || 0, # is there a better value?
6151 # this field should be
6153 # cust_bill_pay_batch now
6154 'custnum' => $self->custnum,
6155 'last' => $self->getfield('last'),
6156 'first' => $self->getfield('first'),
6157 'address1' => $options{address1} || $self->address1,
6158 'address2' => $options{address2} || $self->address2,
6159 'city' => $options{city} || $self->city,
6160 'state' => $options{state} || $self->state,
6161 'zip' => $options{zip} || $self->zip,
6162 'country' => $options{country} || $self->country,
6163 'payby' => $options{payby} || $self->payby,
6164 'payinfo' => $options{payinfo} || $self->payinfo,
6165 'exp' => $options{paydate} || $self->paydate,
6166 'payname' => $options{payname} || $self->payname,
6167 'amount' => $amount, # consolidating
6170 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
6171 if $old_cust_pay_batch;
6174 if ($old_cust_pay_batch) {
6175 $error = $cust_pay_batch->replace($old_cust_pay_batch)
6177 $error = $cust_pay_batch->insert;
6181 $dbh->rollback if $oldAutoCommit;
6185 my $unapplied = $self->total_unapplied_credits
6186 + $self->total_unapplied_payments
6187 + $self->in_transit_payments;
6188 foreach my $cust_bill ($self->open_cust_bill) {
6189 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
6190 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
6191 'invnum' => $cust_bill->invnum,
6192 'paybatchnum' => $cust_pay_batch->paybatchnum,
6193 'amount' => $cust_bill->owed,
6196 if ($unapplied >= $cust_bill_pay_batch->amount){
6197 $unapplied -= $cust_bill_pay_batch->amount;
6200 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
6201 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
6203 $error = $cust_bill_pay_batch->insert;
6205 $dbh->rollback if $oldAutoCommit;
6210 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6214 =item apply_payments_and_credits [ OPTION => VALUE ... ]
6216 Applies unapplied payments and credits.
6218 In most cases, this new method should be used in place of sequential
6219 apply_payments and apply_credits methods.
6221 A hash of optional arguments may be passed. Currently "manual" is supported.
6222 If true, a payment receipt is sent instead of a statement when
6223 'payment_receipt_email' configuration option is set.
6225 If there is an error, returns the error, otherwise returns false.
6229 sub apply_payments_and_credits {
6230 my( $self, %options ) = @_;
6232 local $SIG{HUP} = 'IGNORE';
6233 local $SIG{INT} = 'IGNORE';
6234 local $SIG{QUIT} = 'IGNORE';
6235 local $SIG{TERM} = 'IGNORE';
6236 local $SIG{TSTP} = 'IGNORE';
6237 local $SIG{PIPE} = 'IGNORE';
6239 my $oldAutoCommit = $FS::UID::AutoCommit;
6240 local $FS::UID::AutoCommit = 0;
6243 $self->select_for_update; #mutex
6245 foreach my $cust_bill ( $self->open_cust_bill ) {
6246 my $error = $cust_bill->apply_payments_and_credits(%options);
6248 $dbh->rollback if $oldAutoCommit;
6249 return "Error applying: $error";
6253 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6258 =item apply_credits OPTION => VALUE ...
6260 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
6261 to outstanding invoice balances in chronological order (or reverse
6262 chronological order if the I<order> option is set to B<newest>) and returns the
6263 value of any remaining unapplied credits available for refund (see
6264 L<FS::cust_refund>).
6266 Dies if there is an error.
6274 local $SIG{HUP} = 'IGNORE';
6275 local $SIG{INT} = 'IGNORE';
6276 local $SIG{QUIT} = 'IGNORE';
6277 local $SIG{TERM} = 'IGNORE';
6278 local $SIG{TSTP} = 'IGNORE';
6279 local $SIG{PIPE} = 'IGNORE';
6281 my $oldAutoCommit = $FS::UID::AutoCommit;
6282 local $FS::UID::AutoCommit = 0;
6285 $self->select_for_update; #mutex
6287 unless ( $self->total_unapplied_credits ) {
6288 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6292 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
6293 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
6295 my @invoices = $self->open_cust_bill;
6296 @invoices = sort { $b->_date <=> $a->_date } @invoices
6297 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
6299 if ( $conf->exists('pkg-balances') ) {
6300 # limit @credits to those w/ a pkgnum grepped from $self
6302 foreach my $i (@invoices) {
6303 foreach my $li ( $i->cust_bill_pkg ) {
6304 $pkgnums{$li->pkgnum} = 1;
6307 @credits = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @credits;
6312 foreach my $cust_bill ( @invoices ) {
6314 if ( !defined($credit) || $credit->credited == 0) {
6315 $credit = pop @credits or last;
6319 if ( $conf->exists('pkg-balances') && $credit->pkgnum ) {
6320 $owed = $cust_bill->owed_pkgnum($credit->pkgnum);
6322 $owed = $cust_bill->owed;
6324 unless ( $owed > 0 ) {
6325 push @credits, $credit;
6329 my $amount = min( $credit->credited, $owed );
6331 my $cust_credit_bill = new FS::cust_credit_bill ( {
6332 'crednum' => $credit->crednum,
6333 'invnum' => $cust_bill->invnum,
6334 'amount' => $amount,
6336 $cust_credit_bill->pkgnum( $credit->pkgnum )
6337 if $conf->exists('pkg-balances') && $credit->pkgnum;
6338 my $error = $cust_credit_bill->insert;
6340 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
6344 redo if ($cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
6348 my $total_unapplied_credits = $self->total_unapplied_credits;
6350 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6352 return $total_unapplied_credits;
6355 =item apply_payments [ OPTION => VALUE ... ]
6357 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
6358 to outstanding invoice balances in chronological order.
6360 #and returns the value of any remaining unapplied payments.
6362 A hash of optional arguments may be passed. Currently "manual" is supported.
6363 If true, a payment receipt is sent instead of a statement when
6364 'payment_receipt_email' configuration option is set.
6367 Dies if there is an error.
6371 sub apply_payments {
6372 my( $self, %options ) = @_;
6374 local $SIG{HUP} = 'IGNORE';
6375 local $SIG{INT} = 'IGNORE';
6376 local $SIG{QUIT} = 'IGNORE';
6377 local $SIG{TERM} = 'IGNORE';
6378 local $SIG{TSTP} = 'IGNORE';
6379 local $SIG{PIPE} = 'IGNORE';
6381 my $oldAutoCommit = $FS::UID::AutoCommit;
6382 local $FS::UID::AutoCommit = 0;
6385 $self->select_for_update; #mutex
6389 my @payments = sort { $b->_date <=> $a->_date }
6390 grep { $_->unapplied > 0 }
6393 my @invoices = sort { $a->_date <=> $b->_date}
6394 grep { $_->owed > 0 }
6397 if ( $conf->exists('pkg-balances') ) {
6398 # limit @payments to those w/ a pkgnum grepped from $self
6400 foreach my $i (@invoices) {
6401 foreach my $li ( $i->cust_bill_pkg ) {
6402 $pkgnums{$li->pkgnum} = 1;
6405 @payments = grep { ! $_->pkgnum || $pkgnums{$_->pkgnum} } @payments;
6410 foreach my $cust_bill ( @invoices ) {
6412 if ( !defined($payment) || $payment->unapplied == 0 ) {
6413 $payment = pop @payments or last;
6417 if ( $conf->exists('pkg-balances') && $payment->pkgnum ) {
6418 $owed = $cust_bill->owed_pkgnum($payment->pkgnum);
6420 $owed = $cust_bill->owed;
6422 unless ( $owed > 0 ) {
6423 push @payments, $payment;
6427 my $amount = min( $payment->unapplied, $owed );
6429 my $cust_bill_pay = new FS::cust_bill_pay ( {
6430 'paynum' => $payment->paynum,
6431 'invnum' => $cust_bill->invnum,
6432 'amount' => $amount,
6434 $cust_bill_pay->pkgnum( $payment->pkgnum )
6435 if $conf->exists('pkg-balances') && $payment->pkgnum;
6436 my $error = $cust_bill_pay->insert(%options);
6438 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
6442 redo if ( $cust_bill->owed > 0) && ! $conf->exists('pkg-balances');
6446 my $total_unapplied_payments = $self->total_unapplied_payments;
6448 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6450 return $total_unapplied_payments;
6455 Returns the total owed for this customer on all invoices
6456 (see L<FS::cust_bill/owed>).
6462 $self->total_owed_date(2145859200); #12/31/2037
6465 =item total_owed_date TIME
6467 Returns the total owed for this customer on all invoices with date earlier than
6468 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
6469 see L<Time::Local> and L<Date::Parse> for conversion functions.
6473 sub total_owed_date {
6477 # my $custnum = $self->custnum;
6479 # my $owed_sql = FS::cust_bill->owed_sql;
6482 # SELECT SUM($owed_sql) FROM cust_bill
6483 # WHERE custnum = $custnum
6484 # AND _date <= $time
6487 # my $sth = dbh->prepare($sql) or die dbh->errstr;
6488 # $sth->execute() or die $sth->errstr;
6490 # return sprintf( '%.2f', $sth->fetchrow_arrayref->[0] );
6493 foreach my $cust_bill (
6494 grep { $_->_date <= $time }
6495 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
6497 $total_bill += $cust_bill->owed;
6499 sprintf( "%.2f", $total_bill );
6503 =item total_owed_pkgnum PKGNUM
6505 Returns the total owed on all invoices for this customer's specific package
6506 when using experimental package balances (see L<FS::cust_bill/owed_pkgnum>).
6510 sub total_owed_pkgnum {
6511 my( $self, $pkgnum ) = @_;
6512 $self->total_owed_date_pkgnum(2145859200, $pkgnum); #12/31/2037
6515 =item total_owed_date_pkgnum TIME PKGNUM
6517 Returns the total owed for this customer's specific package when using
6518 experimental package balances on all invoices with date earlier than
6519 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
6520 see L<Time::Local> and L<Date::Parse> for conversion functions.
6524 sub total_owed_date_pkgnum {
6525 my( $self, $time, $pkgnum ) = @_;
6528 foreach my $cust_bill (
6529 grep { $_->_date <= $time }
6530 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
6532 $total_bill += $cust_bill->owed_pkgnum($pkgnum);
6534 sprintf( "%.2f", $total_bill );
6540 Returns the total amount of all payments.
6547 $total += $_->paid foreach $self->cust_pay;
6548 sprintf( "%.2f", $total );
6551 =item total_unapplied_credits
6553 Returns the total outstanding credit (see L<FS::cust_credit>) for this
6554 customer. See L<FS::cust_credit/credited>.
6556 =item total_credited
6558 Old name for total_unapplied_credits. Don't use.
6562 sub total_credited {
6563 #carp "total_credited deprecated, use total_unapplied_credits";
6564 shift->total_unapplied_credits(@_);
6567 sub total_unapplied_credits {
6569 my $total_credit = 0;
6570 $total_credit += $_->credited foreach $self->cust_credit;
6571 sprintf( "%.2f", $total_credit );
6574 =item total_unapplied_credits_pkgnum PKGNUM
6576 Returns the total outstanding credit (see L<FS::cust_credit>) for this
6577 customer. See L<FS::cust_credit/credited>.
6581 sub total_unapplied_credits_pkgnum {
6582 my( $self, $pkgnum ) = @_;
6583 my $total_credit = 0;
6584 $total_credit += $_->credited foreach $self->cust_credit_pkgnum($pkgnum);
6585 sprintf( "%.2f", $total_credit );
6589 =item total_unapplied_payments
6591 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
6592 See L<FS::cust_pay/unapplied>.
6596 sub total_unapplied_payments {
6598 my $total_unapplied = 0;
6599 $total_unapplied += $_->unapplied foreach $self->cust_pay;
6600 sprintf( "%.2f", $total_unapplied );
6603 =item total_unapplied_payments_pkgnum PKGNUM
6605 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer's
6606 specific package when using experimental package balances. See
6607 L<FS::cust_pay/unapplied>.
6611 sub total_unapplied_payments_pkgnum {
6612 my( $self, $pkgnum ) = @_;
6613 my $total_unapplied = 0;
6614 $total_unapplied += $_->unapplied foreach $self->cust_pay_pkgnum($pkgnum);
6615 sprintf( "%.2f", $total_unapplied );
6619 =item total_unapplied_refunds
6621 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
6622 customer. See L<FS::cust_refund/unapplied>.
6626 sub total_unapplied_refunds {
6628 my $total_unapplied = 0;
6629 $total_unapplied += $_->unapplied foreach $self->cust_refund;
6630 sprintf( "%.2f", $total_unapplied );
6635 Returns the balance for this customer (total_owed plus total_unrefunded, minus
6636 total_unapplied_credits minus total_unapplied_payments).
6644 + $self->total_unapplied_refunds
6645 - $self->total_unapplied_credits
6646 - $self->total_unapplied_payments
6650 =item balance_date TIME
6652 Returns the balance for this customer, only considering invoices with date
6653 earlier than TIME (total_owed_date minus total_credited minus
6654 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
6655 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
6664 $self->total_owed_date($time)
6665 + $self->total_unapplied_refunds
6666 - $self->total_unapplied_credits
6667 - $self->total_unapplied_payments
6671 =item balance_pkgnum PKGNUM
6673 Returns the balance for this customer's specific package when using
6674 experimental package balances (total_owed plus total_unrefunded, minus
6675 total_unapplied_credits minus total_unapplied_payments)
6679 sub balance_pkgnum {
6680 my( $self, $pkgnum ) = @_;
6683 $self->total_owed_pkgnum($pkgnum)
6684 # n/a - refunds aren't part of pkg-balances since they don't apply to invoices
6685 # + $self->total_unapplied_refunds_pkgnum($pkgnum)
6686 - $self->total_unapplied_credits_pkgnum($pkgnum)
6687 - $self->total_unapplied_payments_pkgnum($pkgnum)
6691 =item in_transit_payments
6693 Returns the total of requests for payments for this customer pending in
6694 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
6698 sub in_transit_payments {
6700 my $in_transit_payments = 0;
6701 foreach my $pay_batch ( qsearch('pay_batch', {
6704 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
6705 'batchnum' => $pay_batch->batchnum,
6706 'custnum' => $self->custnum,
6708 $in_transit_payments += $cust_pay_batch->amount;
6711 sprintf( "%.2f", $in_transit_payments );
6716 Returns a hash of useful information for making a payment.
6726 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
6727 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
6728 'LECB' (Phone bill billing), 'BILL' (billing), or 'COMP' (free).
6732 For credit card transactions:
6744 For electronic check transactions:
6759 $return{balance} = $self->balance;
6761 $return{payname} = $self->payname
6762 || ( $self->first. ' '. $self->get('last') );
6764 $return{$_} = $self->get($_) for qw(address1 address2 city state zip);
6766 $return{payby} = $self->payby;
6767 $return{stateid_state} = $self->stateid_state;
6769 if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
6770 $return{card_type} = cardtype($self->payinfo);
6771 $return{payinfo} = $self->paymask;
6773 @return{'month', 'year'} = $self->paydate_monthyear;
6777 if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
6778 my ($payinfo1, $payinfo2) = split '@', $self->paymask;
6779 $return{payinfo1} = $payinfo1;
6780 $return{payinfo2} = $payinfo2;
6781 $return{paytype} = $self->paytype;
6782 $return{paystate} = $self->paystate;
6786 #doubleclick protection
6788 $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
6794 =item paydate_monthyear
6796 Returns a two-element list consisting of the month and year of this customer's
6797 paydate (credit card expiration date for CARD customers)
6801 sub paydate_monthyear {
6803 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
6805 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
6812 =item tax_exemption TAXNAME
6817 my( $self, $taxname ) = @_;
6819 qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
6820 'taxname' => $taxname,
6825 =item cust_main_exemption
6829 sub cust_main_exemption {
6831 qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
6834 =item invoicing_list [ ARRAYREF ]
6836 If an arguement is given, sets these email addresses as invoice recipients
6837 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
6838 (except as warnings), so use check_invoicing_list first.
6840 Returns a list of email addresses (with svcnum entries expanded).
6842 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
6843 check it without disturbing anything by passing nothing.
6845 This interface may change in the future.
6849 sub invoicing_list {
6850 my( $self, $arrayref ) = @_;
6853 my @cust_main_invoice;
6854 if ( $self->custnum ) {
6855 @cust_main_invoice =
6856 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
6858 @cust_main_invoice = ();
6860 foreach my $cust_main_invoice ( @cust_main_invoice ) {
6861 #warn $cust_main_invoice->destnum;
6862 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
6863 #warn $cust_main_invoice->destnum;
6864 my $error = $cust_main_invoice->delete;
6865 warn $error if $error;
6868 if ( $self->custnum ) {
6869 @cust_main_invoice =
6870 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
6872 @cust_main_invoice = ();
6874 my %seen = map { $_->address => 1 } @cust_main_invoice;
6875 foreach my $address ( @{$arrayref} ) {
6876 next if exists $seen{$address} && $seen{$address};
6877 $seen{$address} = 1;
6878 my $cust_main_invoice = new FS::cust_main_invoice ( {
6879 'custnum' => $self->custnum,
6882 my $error = $cust_main_invoice->insert;
6883 warn $error if $error;
6887 if ( $self->custnum ) {
6889 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
6896 =item check_invoicing_list ARRAYREF
6898 Checks these arguements as valid input for the invoicing_list method. If there
6899 is an error, returns the error, otherwise returns false.
6903 sub check_invoicing_list {
6904 my( $self, $arrayref ) = @_;
6906 foreach my $address ( @$arrayref ) {
6908 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
6909 return 'Can\'t add FAX invoice destination with a blank FAX number.';
6912 my $cust_main_invoice = new FS::cust_main_invoice ( {
6913 'custnum' => $self->custnum,
6916 my $error = $self->custnum
6917 ? $cust_main_invoice->check
6918 : $cust_main_invoice->checkdest
6920 return $error if $error;
6924 return "Email address required"
6925 if $conf->exists('cust_main-require_invoicing_list_email')
6926 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
6931 =item set_default_invoicing_list
6933 Sets the invoicing list to all accounts associated with this customer,
6934 overwriting any previous invoicing list.
6938 sub set_default_invoicing_list {
6940 $self->invoicing_list($self->all_emails);
6945 Returns the email addresses of all accounts provisioned for this customer.
6952 foreach my $cust_pkg ( $self->all_pkgs ) {
6953 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
6955 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
6956 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
6958 $list{$_}=1 foreach map { $_->email } @svc_acct;
6963 =item invoicing_list_addpost
6965 Adds postal invoicing to this customer. If this customer is already configured
6966 to receive postal invoices, does nothing.
6970 sub invoicing_list_addpost {
6972 return if grep { $_ eq 'POST' } $self->invoicing_list;
6973 my @invoicing_list = $self->invoicing_list;
6974 push @invoicing_list, 'POST';
6975 $self->invoicing_list(\@invoicing_list);
6978 =item invoicing_list_emailonly
6980 Returns the list of email invoice recipients (invoicing_list without non-email
6981 destinations such as POST and FAX).
6985 sub invoicing_list_emailonly {
6987 warn "$me invoicing_list_emailonly called"
6989 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
6992 =item invoicing_list_emailonly_scalar
6994 Returns the list of email invoice recipients (invoicing_list without non-email
6995 destinations such as POST and FAX) as a comma-separated scalar.
6999 sub invoicing_list_emailonly_scalar {
7001 warn "$me invoicing_list_emailonly_scalar called"
7003 join(', ', $self->invoicing_list_emailonly);
7006 =item referral_custnum_cust_main
7008 Returns the customer who referred this customer (or the empty string, if
7009 this customer was not referred).
7011 Note the difference with referral_cust_main method: This method,
7012 referral_custnum_cust_main returns the single customer (if any) who referred
7013 this customer, while referral_cust_main returns an array of customers referred
7018 sub referral_custnum_cust_main {
7020 return '' unless $self->referral_custnum;
7021 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
7024 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
7026 Returns an array of customers referred by this customer (referral_custnum set
7027 to this custnum). If DEPTH is given, recurses up to the given depth, returning
7028 customers referred by customers referred by this customer and so on, inclusive.
7029 The default behavior is DEPTH 1 (no recursion).
7031 Note the difference with referral_custnum_cust_main method: This method,
7032 referral_cust_main, returns an array of customers referred BY this customer,
7033 while referral_custnum_cust_main returns the single customer (if any) who
7034 referred this customer.
7038 sub referral_cust_main {
7040 my $depth = @_ ? shift : 1;
7041 my $exclude = @_ ? shift : {};
7044 map { $exclude->{$_->custnum}++; $_; }
7045 grep { ! $exclude->{ $_->custnum } }
7046 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
7050 map { $_->referral_cust_main($depth-1, $exclude) }
7057 =item referral_cust_main_ncancelled
7059 Same as referral_cust_main, except only returns customers with uncancelled
7064 sub referral_cust_main_ncancelled {
7066 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
7069 =item referral_cust_pkg [ DEPTH ]
7071 Like referral_cust_main, except returns a flat list of all unsuspended (and
7072 uncancelled) packages for each customer. The number of items in this list may
7073 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
7077 sub referral_cust_pkg {
7079 my $depth = @_ ? shift : 1;
7081 map { $_->unsuspended_pkgs }
7082 grep { $_->unsuspended_pkgs }
7083 $self->referral_cust_main($depth);
7086 =item referring_cust_main
7088 Returns the single cust_main record for the customer who referred this customer
7089 (referral_custnum), or false.
7093 sub referring_cust_main {
7095 return '' unless $self->referral_custnum;
7096 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
7099 =item credit AMOUNT, REASON [ , OPTION => VALUE ... ]
7101 Applies a credit to this customer. If there is an error, returns the error,
7102 otherwise returns false.
7104 REASON can be a text string, an FS::reason object, or a scalar reference to
7105 a reasonnum. If a text string, it will be automatically inserted as a new
7106 reason, and a 'reason_type' option must be passed to indicate the
7107 FS::reason_type for the new reason.
7109 An I<addlinfo> option may be passed to set the credit's I<addlinfo> field.
7111 Any other options are passed to FS::cust_credit::insert.
7116 my( $self, $amount, $reason, %options ) = @_;
7118 my $cust_credit = new FS::cust_credit {
7119 'custnum' => $self->custnum,
7120 'amount' => $amount,
7123 if ( ref($reason) ) {
7125 if ( ref($reason) eq 'SCALAR' ) {
7126 $cust_credit->reasonnum( $$reason );
7128 $cust_credit->reasonnum( $reason->reasonnum );
7132 $cust_credit->set('reason', $reason)
7135 $cust_credit->addlinfo( delete $options{'addlinfo'} )
7136 if exists($options{'addlinfo'});
7138 $cust_credit->insert(%options);
7142 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
7144 Creates a one-time charge for this customer. If there is an error, returns
7145 the error, otherwise returns false.
7147 New-style, with a hashref of options:
7149 my $error = $cust_main->charge(
7153 'start_date' => str2time('7/4/2009'),
7154 'pkg' => 'Description',
7155 'comment' => 'Comment',
7156 'additional' => [], #extra invoice detail
7157 'classnum' => 1, #pkg_class
7159 'setuptax' => '', # or 'Y' for tax exempt
7162 'taxclass' => 'Tax class',
7165 'taxproduct' => 2, #part_pkg_taxproduct
7166 'override' => {}, #XXX describe
7172 my $error = $cust_main->charge( 54.32, 'Description', 'Comment', 'Tax class' );
7178 my ( $amount, $quantity, $start_date, $classnum );
7179 my ( $pkg, $comment, $additional );
7180 my ( $setuptax, $taxclass ); #internal taxes
7181 my ( $taxproduct, $override ); #vendor (CCH) taxes
7182 if ( ref( $_[0] ) ) {
7183 $amount = $_[0]->{amount};
7184 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
7185 $start_date = exists($_[0]->{start_date}) ? $_[0]->{start_date} : '';
7186 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
7187 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
7188 : '$'. sprintf("%.2f",$amount);
7189 $setuptax = exists($_[0]->{setuptax}) ? $_[0]->{setuptax} : '';
7190 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
7191 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
7192 $additional = $_[0]->{additional} || [];
7193 $taxproduct = $_[0]->{taxproductnum};
7194 $override = { '' => $_[0]->{tax_override} };
7199 $pkg = @_ ? shift : 'One-time charge';
7200 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
7202 $taxclass = @_ ? shift : '';
7206 local $SIG{HUP} = 'IGNORE';
7207 local $SIG{INT} = 'IGNORE';
7208 local $SIG{QUIT} = 'IGNORE';
7209 local $SIG{TERM} = 'IGNORE';
7210 local $SIG{TSTP} = 'IGNORE';
7211 local $SIG{PIPE} = 'IGNORE';
7213 my $oldAutoCommit = $FS::UID::AutoCommit;
7214 local $FS::UID::AutoCommit = 0;
7217 my $part_pkg = new FS::part_pkg ( {
7219 'comment' => $comment,
7223 'classnum' => $classnum ? $classnum : '',
7224 'setuptax' => $setuptax,
7225 'taxclass' => $taxclass,
7226 'taxproductnum' => $taxproduct,
7229 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
7230 ( 0 .. @$additional - 1 )
7232 'additional_count' => scalar(@$additional),
7233 'setup_fee' => $amount,
7236 my $error = $part_pkg->insert( options => \%options,
7237 tax_overrides => $override,
7240 $dbh->rollback if $oldAutoCommit;
7244 my $pkgpart = $part_pkg->pkgpart;
7245 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
7246 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
7247 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
7248 $error = $type_pkgs->insert;
7250 $dbh->rollback if $oldAutoCommit;
7255 my $cust_pkg = new FS::cust_pkg ( {
7256 'custnum' => $self->custnum,
7257 'pkgpart' => $pkgpart,
7258 'quantity' => $quantity,
7259 'start_date' => $start_date,
7262 $error = $cust_pkg->insert;
7264 $dbh->rollback if $oldAutoCommit;
7268 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
7273 #=item charge_postal_fee
7275 #Applies a one time charge this customer. If there is an error,
7276 #returns the error, returns the cust_pkg charge object or false
7277 #if there was no charge.
7281 # This should be a customer event. For that to work requires that bill
7282 # also be a customer event.
7284 sub charge_postal_fee {
7287 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
7288 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
7290 my $cust_pkg = new FS::cust_pkg ( {
7291 'custnum' => $self->custnum,
7292 'pkgpart' => $pkgpart,
7296 my $error = $cust_pkg->insert;
7297 $error ? $error : $cust_pkg;
7302 Returns all the invoices (see L<FS::cust_bill>) for this customer.
7308 sort { $a->_date <=> $b->_date }
7309 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
7312 =item open_cust_bill
7314 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
7319 sub open_cust_bill {
7323 'table' => 'cust_bill',
7324 'hashref' => { 'custnum' => $self->custnum, },
7325 'extra_sql' => ' AND '. FS::cust_bill->owed_sql. ' > 0',
7326 'order_by' => 'ORDER BY _date ASC',
7331 =item cust_statements
7333 Returns all the statements (see L<FS::cust_statement>) for this customer.
7337 sub cust_statement {
7339 sort { $a->_date <=> $b->_date }
7340 qsearch('cust_statement', { 'custnum' => $self->custnum, } )
7345 Returns all the credits (see L<FS::cust_credit>) for this customer.
7351 sort { $a->_date <=> $b->_date }
7352 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
7355 =item cust_credit_pkgnum
7357 Returns all the credits (see L<FS::cust_credit>) for this customer's specific
7358 package when using experimental package balances.
7362 sub cust_credit_pkgnum {
7363 my( $self, $pkgnum ) = @_;
7364 sort { $a->_date <=> $b->_date }
7365 qsearch( 'cust_credit', { 'custnum' => $self->custnum,
7366 'pkgnum' => $pkgnum,
7373 Returns all the payments (see L<FS::cust_pay>) for this customer.
7379 sort { $a->_date <=> $b->_date }
7380 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
7383 =item cust_pay_pkgnum
7385 Returns all the payments (see L<FS::cust_pay>) for this customer's specific
7386 package when using experimental package balances.
7390 sub cust_pay_pkgnum {
7391 my( $self, $pkgnum ) = @_;
7392 sort { $a->_date <=> $b->_date }
7393 qsearch( 'cust_pay', { 'custnum' => $self->custnum,
7394 'pkgnum' => $pkgnum,
7401 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
7407 sort { $a->_date <=> $b->_date }
7408 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
7411 =item cust_pay_batch
7413 Returns all batched payments (see L<FS::cust_pay_void>) for this customer.
7417 sub cust_pay_batch {
7419 sort { $a->paybatchnum <=> $b->paybatchnum }
7420 qsearch( 'cust_pay_batch', { 'custnum' => $self->custnum } )
7423 =item cust_pay_pending
7425 Returns all pending payments (see L<FS::cust_pay_pending>) for this customer
7426 (without status "done").
7430 sub cust_pay_pending {
7432 return $self->num_cust_pay_pending unless wantarray;
7433 sort { $a->_date <=> $b->_date }
7434 qsearch( 'cust_pay_pending', {
7435 'custnum' => $self->custnum,
7436 'status' => { op=>'!=', value=>'done' },
7441 =item num_cust_pay_pending
7443 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
7444 customer (without status "done"). Also called automatically when the
7445 cust_pay_pending method is used in a scalar context.
7449 sub num_cust_pay_pending {
7451 my $sql = " SELECT COUNT(*) FROM cust_pay_pending ".
7452 " WHERE custnum = ? AND status != 'done' ";
7453 my $sth = dbh->prepare($sql) or die dbh->errstr;
7454 $sth->execute($self->custnum) or die $sth->errstr;
7455 $sth->fetchrow_arrayref->[0];
7460 Returns all the refunds (see L<FS::cust_refund>) for this customer.
7466 sort { $a->_date <=> $b->_date }
7467 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
7470 =item display_custnum
7472 Returns the displayed customer number for this customer: agent_custid if
7473 cust_main-default_agent_custid is set and it has a value, custnum otherwise.
7477 sub display_custnum {
7479 if ( $conf->exists('cust_main-default_agent_custid') && $self->agent_custid ){
7480 return $self->agent_custid;
7482 return $self->custnum;
7488 Returns a name string for this customer, either "Company (Last, First)" or
7495 my $name = $self->contact;
7496 $name = $self->company. " ($name)" if $self->company;
7502 Returns a name string for this (service/shipping) contact, either
7503 "Company (Last, First)" or "Last, First".
7509 if ( $self->get('ship_last') ) {
7510 my $name = $self->ship_contact;
7511 $name = $self->ship_company. " ($name)" if $self->ship_company;
7520 Returns a name string for this customer, either "Company" or "First Last".
7526 $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
7529 =item ship_name_short
7531 Returns a name string for this (service/shipping) contact, either "Company"
7536 sub ship_name_short {
7538 if ( $self->get('ship_last') ) {
7539 $self->ship_company !~ /^\s*$/
7540 ? $self->ship_company
7541 : $self->ship_contact_firstlast;
7543 $self->name_company_or_firstlast;
7549 Returns this customer's full (billing) contact name only, "Last, First"
7555 $self->get('last'). ', '. $self->first;
7560 Returns this customer's full (shipping) contact name only, "Last, First"
7566 $self->get('ship_last')
7567 ? $self->get('ship_last'). ', '. $self->ship_first
7571 =item contact_firstlast
7573 Returns this customers full (billing) contact name only, "First Last".
7577 sub contact_firstlast {
7579 $self->first. ' '. $self->get('last');
7582 =item ship_contact_firstlast
7584 Returns this customer's full (shipping) contact name only, "First Last".
7588 sub ship_contact_firstlast {
7590 $self->get('ship_last')
7591 ? $self->first. ' '. $self->get('ship_last')
7592 : $self->contact_firstlast;
7597 Returns this customer's full country name
7603 code2country($self->country);
7606 =item geocode DATA_VENDOR
7608 Returns a value for the customer location as encoded by DATA_VENDOR.
7609 Currently this only makes sense for "CCH" as DATA_VENDOR.
7614 my ($self, $data_vendor) = (shift, shift); #always cch for now
7616 my $geocode = $self->get('geocode'); #XXX only one data_vendor for geocode
7617 return $geocode if $geocode;
7619 my $prefix = ( $conf->exists('tax-ship_address') && length($self->ship_last) )
7623 my ($zip,$plus4) = split /-/, $self->get("${prefix}zip")
7624 if $self->country eq 'US';
7626 #CCH specific location stuff
7627 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
7629 my @cust_tax_location =
7631 'table' => 'cust_tax_location',
7632 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
7633 'extra_sql' => $extra_sql,
7634 'order_by' => 'ORDER BY plus4hi',#overlapping with distinct ends
7637 $geocode = $cust_tax_location[0]->geocode
7638 if scalar(@cust_tax_location);
7647 Returns a status string for this customer, currently:
7651 =item prospect - No packages have ever been ordered
7653 =item active - One or more recurring packages is active
7655 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
7657 =item suspended - All non-cancelled recurring packages are suspended
7659 =item cancelled - All recurring packages are cancelled
7665 sub status { shift->cust_status(@_); }
7669 for my $status (qw( prospect active inactive suspended cancelled )) {
7670 my $method = $status.'_sql';
7671 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
7672 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
7673 $sth->execute( ($self->custnum) x $numnum )
7674 or die "Error executing 'SELECT $sql': ". $sth->errstr;
7675 return $status if $sth->fetchrow_arrayref->[0];
7679 =item ucfirst_cust_status
7681 =item ucfirst_status
7683 Returns the status with the first character capitalized.
7687 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
7689 sub ucfirst_cust_status {
7691 ucfirst($self->cust_status);
7696 Returns a hex triplet color string for this customer's status.
7700 use vars qw(%statuscolor);
7701 tie %statuscolor, 'Tie::IxHash',
7702 'prospect' => '7e0079', #'000000', #black? naw, purple
7703 'active' => '00CC00', #green
7704 'inactive' => '0000CC', #blue
7705 'suspended' => 'FF9900', #yellow
7706 'cancelled' => 'FF0000', #red
7709 sub statuscolor { shift->cust_statuscolor(@_); }
7711 sub cust_statuscolor {
7713 $statuscolor{$self->cust_status};
7718 Returns an array of hashes representing the customer's RT tickets.
7725 my $num = $conf->config('cust_main-max_tickets') || 10;
7728 if ( $conf->config('ticket_system') ) {
7729 unless ( $conf->config('ticket_system-custom_priority_field') ) {
7731 @tickets = @{ FS::TicketSystem->customer_tickets($self->custnum, $num) };
7735 foreach my $priority (
7736 $conf->config('ticket_system-custom_priority_field-values'), ''
7738 last if scalar(@tickets) >= $num;
7740 @{ FS::TicketSystem->customer_tickets( $self->custnum,
7741 $num - scalar(@tickets),
7751 # Return services representing svc_accts in customer support packages
7752 sub support_services {
7754 my %packages = map { $_ => 1 } $conf->config('support_packages');
7756 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
7757 grep { $_->part_svc->svcdb eq 'svc_acct' }
7758 map { $_->cust_svc }
7759 grep { exists $packages{ $_->pkgpart } }
7760 $self->ncancelled_pkgs;
7764 # Return a list of latitude/longitude for one of the services (if any)
7765 sub service_coordinates {
7769 grep { $_->latitude && $_->longitude }
7771 map { $_->cust_svc }
7772 $self->ncancelled_pkgs;
7774 scalar(@svc_X) ? ( $svc_X[0]->latitude, $svc_X[0]->longitude ) : ()
7779 =head1 CLASS METHODS
7785 Class method that returns the list of possible status strings for customers
7786 (see L<the status method|/status>). For example:
7788 @statuses = FS::cust_main->statuses();
7793 #my $self = shift; #could be class...
7799 Returns an SQL expression identifying prospective cust_main records (customers
7800 with no packages ever ordered)
7804 use vars qw($select_count_pkgs);
7805 $select_count_pkgs =
7806 "SELECT COUNT(*) FROM cust_pkg
7807 WHERE cust_pkg.custnum = cust_main.custnum";
7809 sub select_count_pkgs_sql {
7813 sub prospect_sql { "
7814 0 = ( $select_count_pkgs )
7819 Returns an SQL expression identifying active cust_main records (customers with
7820 active recurring packages).
7825 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
7831 Returns an SQL expression identifying inactive cust_main records (customers with
7832 no active recurring packages, but otherwise unsuspended/uncancelled).
7836 sub inactive_sql { "
7837 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
7839 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
7845 Returns an SQL expression identifying suspended cust_main records.
7850 sub suspended_sql { susp_sql(@_); }
7852 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
7854 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
7860 Returns an SQL expression identifying cancelled cust_main records.
7864 sub cancelled_sql { cancel_sql(@_); }
7867 my $recurring_sql = FS::cust_pkg->recurring_sql;
7868 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
7871 0 < ( $select_count_pkgs )
7872 AND 0 < ( $select_count_pkgs AND $recurring_sql AND $cancelled_sql )
7873 AND 0 = ( $select_count_pkgs AND $recurring_sql
7874 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
7876 AND 0 = ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
7882 =item uncancelled_sql
7884 Returns an SQL expression identifying un-cancelled cust_main records.
7888 sub uncancelled_sql { uncancel_sql(@_); }
7889 sub uncancel_sql { "
7890 ( 0 < ( $select_count_pkgs
7891 AND ( cust_pkg.cancel IS NULL
7892 OR cust_pkg.cancel = 0
7895 OR 0 = ( $select_count_pkgs )
7901 Returns an SQL fragment to retreive the balance.
7906 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
7907 WHERE cust_bill.custnum = cust_main.custnum )
7908 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
7909 WHERE cust_pay.custnum = cust_main.custnum )
7910 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
7911 WHERE cust_credit.custnum = cust_main.custnum )
7912 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
7913 WHERE cust_refund.custnum = cust_main.custnum )
7916 =item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
7918 Returns an SQL fragment to retreive the balance for this customer, only
7919 considering invoices with date earlier than START_TIME, and optionally not
7920 later than END_TIME (total_owed_date minus total_unapplied_credits minus
7921 total_unapplied_payments).
7923 Times are specified as SQL fragments or numeric
7924 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
7925 L<Date::Parse> for conversion functions. The empty string can be passed
7926 to disable that time constraint completely.
7928 Available options are:
7932 =item unapplied_date
7934 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)
7939 set to true to remove all customer comparison clauses, for totals
7944 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
7949 JOIN clause (typically used with the total option)
7955 sub balance_date_sql {
7956 my( $class, $start, $end, %opt ) = @_;
7958 my $owed = FS::cust_bill->owed_sql;
7959 my $unapp_refund = FS::cust_refund->unapplied_sql;
7960 my $unapp_credit = FS::cust_credit->unapplied_sql;
7961 my $unapp_pay = FS::cust_pay->unapplied_sql;
7963 my $j = $opt{'join'} || '';
7965 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
7966 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
7967 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
7968 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
7970 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
7971 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
7972 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
7973 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
7978 =item unapplied_payments_date_sql START_TIME [ END_TIME ]
7980 Returns an SQL fragment to retreive the total unapplied payments for this
7981 customer, only considering invoices with date earlier than START_TIME, and
7982 optionally not later than END_TIME.
7984 Times are specified as SQL fragments or numeric
7985 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
7986 L<Date::Parse> for conversion functions. The empty string can be passed
7987 to disable that time constraint completely.
7989 Available options are:
7993 sub unapplied_payments_date_sql {
7994 my( $class, $start, $end, ) = @_;
7996 my $unapp_pay = FS::cust_pay->unapplied_sql;
7998 my $pay_where = $class->_money_table_where( 'cust_pay', $start, $end,
7999 'unapplied_date'=>1 );
8001 " ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $pay_where ) ";
8004 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
8006 Helper method for balance_date_sql; name (and usage) subject to change
8007 (suggestions welcome).
8009 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
8010 cust_refund, cust_credit or cust_pay).
8012 If TABLE is "cust_bill" or the unapplied_date option is true, only
8013 considers records with date earlier than START_TIME, and optionally not
8014 later than END_TIME .
8018 sub _money_table_where {
8019 my( $class, $table, $start, $end, %opt ) = @_;
8022 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
8023 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
8024 push @where, "$table._date <= $start" if defined($start) && length($start);
8025 push @where, "$table._date > $end" if defined($end) && length($end);
8027 push @where, @{$opt{'where'}} if $opt{'where'};
8028 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
8034 =item search_sql HASHREF
8038 Returns a qsearch hash expression to search for parameters specified in HREF.
8039 Valid parameters are
8047 =item cancelled_pkgs
8053 listref of start date, end date
8059 =item current_balance
8061 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
8065 =item flattened_pkgs
8074 my ($class, $params) = @_;
8085 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
8087 "cust_main.agentnum = $1";
8094 #prospect active inactive suspended cancelled
8095 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
8096 my $method = $params->{'status'}. '_sql';
8097 #push @where, $class->$method();
8098 push @where, FS::cust_main->$method();
8102 # parse cancelled package checkbox
8107 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
8108 unless $params->{'cancelled_pkgs'};
8111 # parse without census tract checkbox
8114 push @where, "(censustract = '' or censustract is null)"
8115 if $params->{'no_censustract'};
8121 foreach my $field (qw( signupdate )) {
8123 next unless exists($params->{$field});
8125 my($beginning, $ending) = @{$params->{$field}};
8128 "cust_main.$field IS NOT NULL",
8129 "cust_main.$field >= $beginning",
8130 "cust_main.$field <= $ending";
8132 $orderby ||= "ORDER BY cust_main.$field";
8140 my @payby = grep /^([A-Z]{4})$/, @{ $params->{'payby'} };
8142 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
8149 #my $balance_sql = $class->balance_sql();
8150 my $balance_sql = FS::cust_main->balance_sql();
8152 push @where, map { s/current_balance/$balance_sql/; $_ }
8153 @{ $params->{'current_balance'} };
8159 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
8161 "cust_main.custbatch = '$1'";
8165 # setup queries, subs, etc. for the search
8168 $orderby ||= 'ORDER BY custnum';
8170 # here is the agent virtualization
8171 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
8173 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
8175 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
8177 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
8179 my $select = join(', ',
8180 'cust_main.custnum',
8181 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
8184 my(@extra_headers) = ();
8185 my(@extra_fields) = ();
8187 if ($params->{'flattened_pkgs'}) {
8189 if ($dbh->{Driver}->{Name} eq 'Pg') {
8191 $select .= ", array_to_string(array(select pkg from cust_pkg left join part_pkg using ( pkgpart ) where cust_main.custnum = cust_pkg.custnum $pkgwhere),'|') as magic";
8193 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
8194 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
8195 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
8197 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
8198 "omitting packing information from report.";
8201 my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $addl_from $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
8203 my $sth = dbh->prepare($header_query) or die dbh->errstr;
8204 $sth->execute() or die $sth->errstr;
8205 my $headerrow = $sth->fetchrow_arrayref;
8206 my $headercount = $headerrow ? $headerrow->[0] : 0;
8207 while($headercount) {
8208 unshift @extra_headers, "Package ". $headercount;
8209 unshift @extra_fields, eval q!sub {my $c = shift;
8210 my @a = split '\|', $c->magic;
8211 my $p = $a[!.--$headercount. q!];
8219 'table' => 'cust_main',
8220 'select' => $select,
8222 'extra_sql' => $extra_sql,
8223 'order_by' => $orderby,
8224 'count_query' => $count_query,
8225 'extra_headers' => \@extra_headers,
8226 'extra_fields' => \@extra_fields,
8231 =item email_search_sql HASHREF
8235 Emails a notice to the specified customers.
8237 Valid parameters are those of the L<search_sql> method, plus the following:
8259 Optional job queue job for status updates.
8263 Returns an error message, or false for success.
8265 If an error occurs during any email, stops the enture send and returns that
8266 error. Presumably if you're getting SMTP errors aborting is better than
8267 retrying everything.
8271 sub email_search_sql {
8272 my($class, $params) = @_;
8274 my $from = delete $params->{from};
8275 my $subject = delete $params->{subject};
8276 my $html_body = delete $params->{html_body};
8277 my $text_body = delete $params->{text_body};
8279 my $job = delete $params->{'job'};
8281 $params->{'payby'} = [ split(/\0/, $params->{'payby'}) ]
8282 unless ref($params->{'payby'});
8284 my $sql_query = $class->search_sql($params);
8286 my $count_query = delete($sql_query->{'count_query'});
8287 my $count_sth = dbh->prepare($count_query)
8288 or die "Error preparing $count_query: ". dbh->errstr;
8290 or die "Error executing $count_query: ". $count_sth->errstr;
8291 my $count_arrayref = $count_sth->fetchrow_arrayref;
8292 my $num_cust = $count_arrayref->[0];
8294 #my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
8295 #my @extra_fields = @{ delete($sql_query->{'extra_fields'}) };
8298 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
8300 #eventually order+limit magic to reduce memory use?
8301 foreach my $cust_main ( qsearch($sql_query) ) {
8303 my $to = $cust_main->invoicing_list_emailonly_scalar;
8306 my $error = send_email(
8310 'subject' => $subject,
8311 'html_body' => $html_body,
8312 'text_body' => $text_body,
8315 return $error if $error;
8317 if ( $job ) { #progressbar foo
8319 if ( time - $min_sec > $last ) {
8320 my $error = $job->update_statustext(
8321 int( 100 * $num / $num_cust )
8323 die $error if $error;
8333 use Storable qw(thaw);
8336 sub process_email_search_sql {
8338 #warn "$me process_re_X $method for job $job\n" if $DEBUG;
8340 my $param = thaw(decode_base64(shift));
8341 warn Dumper($param) if $DEBUG;
8343 $param->{'job'} = $job;
8345 $param->{'payby'} = [ split(/\0/, $param->{'payby'}) ]
8346 unless ref($param->{'payby'});
8348 my $error = FS::cust_main->email_search_sql( $param );
8349 die $error if $error;
8353 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
8355 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
8356 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
8357 appropriate ship_ field is also searched).
8359 Additional options are the same as FS::Record::qsearch
8364 my( $self, $fuzzy, $hash, @opt) = @_;
8369 check_and_rebuild_fuzzyfiles();
8370 foreach my $field ( keys %$fuzzy ) {
8372 my $all = $self->all_X($field);
8373 next unless scalar(@$all);
8376 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
8379 foreach ( keys %match ) {
8380 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
8381 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
8384 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
8387 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
8389 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
8397 Returns a masked version of the named field
8402 my ($self,$field) = @_;
8406 'x'x(length($self->getfield($field))-4).
8407 substr($self->getfield($field), (length($self->getfield($field))-4));
8417 =item smart_search OPTION => VALUE ...
8419 Accepts the following options: I<search>, the string to search for. The string
8420 will be searched for as a customer number, phone number, name or company name,
8421 as an exact, or, in some cases, a substring or fuzzy match (see the source code
8422 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
8423 skip fuzzy matching when an exact match is found.
8425 Any additional options are treated as an additional qualifier on the search
8428 Returns a (possibly empty) array of FS::cust_main objects.
8435 #here is the agent virtualization
8436 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
8440 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
8441 my $search = delete $options{'search'};
8442 ( my $alphanum_search = $search ) =~ s/\W//g;
8444 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
8446 #false laziness w/Record::ut_phone
8447 my $phonen = "$1-$2-$3";
8448 $phonen .= " x$4" if $4;
8450 push @cust_main, qsearch( {
8451 'table' => 'cust_main',
8452 'hashref' => { %options },
8453 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
8455 join(' OR ', map "$_ = '$phonen'",
8456 qw( daytime night fax
8457 ship_daytime ship_night ship_fax )
8460 " AND $agentnums_sql", #agent virtualization
8463 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
8464 #try looking for matches with extensions unless one was specified
8466 push @cust_main, qsearch( {
8467 'table' => 'cust_main',
8468 'hashref' => { %options },
8469 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
8471 join(' OR ', map "$_ LIKE '$phonen\%'",
8473 ship_daytime ship_night )
8476 " AND $agentnums_sql", #agent virtualization
8481 # custnum search (also try agent_custid), with some tweaking options if your
8482 # legacy cust "numbers" have letters
8485 if ( $search =~ /^\s*(\d+)\s*$/
8486 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
8487 && $search =~ /^\s*(\w\w?\d+)\s*$/
8494 if ( $num <= 2147483647 ) { #need a bigint custnum? wow.
8495 push @cust_main, qsearch( {
8496 'table' => 'cust_main',
8497 'hashref' => { 'custnum' => $num, %options },
8498 'extra_sql' => " AND $agentnums_sql", #agent virtualization
8502 push @cust_main, qsearch( {
8503 'table' => 'cust_main',
8504 'hashref' => { 'agent_custid' => $num, %options },
8505 'extra_sql' => " AND $agentnums_sql", #agent virtualization
8508 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
8510 my($company, $last, $first) = ( $1, $2, $3 );
8512 # "Company (Last, First)"
8513 #this is probably something a browser remembered,
8514 #so just do an exact search
8516 foreach my $prefix ( '', 'ship_' ) {
8517 push @cust_main, qsearch( {
8518 'table' => 'cust_main',
8519 'hashref' => { $prefix.'first' => $first,
8520 $prefix.'last' => $last,
8521 $prefix.'company' => $company,
8524 'extra_sql' => " AND $agentnums_sql",
8528 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
8529 # try (ship_){last,company}
8533 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
8534 # # full strings the browser remembers won't work
8535 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
8537 use Lingua::EN::NameParse;
8538 my $NameParse = new Lingua::EN::NameParse(
8540 allow_reversed => 1,
8543 my($last, $first) = ( '', '' );
8544 #maybe disable this too and just rely on NameParse?
8545 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
8547 ($last, $first) = ( $1, $2 );
8549 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
8550 } elsif ( ! $NameParse->parse($value) ) {
8552 my %name = $NameParse->components;
8553 $first = $name{'given_name_1'};
8554 $last = $name{'surname_1'};
8558 if ( $first && $last ) {
8560 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
8563 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
8565 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
8566 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
8569 push @cust_main, qsearch( {
8570 'table' => 'cust_main',
8571 'hashref' => \%options,
8572 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
8575 # or it just be something that was typed in... (try that in a sec)
8579 my $q_value = dbh->quote($value);
8582 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
8583 $sql .= " ( LOWER(last) = $q_value
8584 OR LOWER(company) = $q_value
8585 OR LOWER(ship_last) = $q_value
8586 OR LOWER(ship_company) = $q_value
8589 push @cust_main, qsearch( {
8590 'table' => 'cust_main',
8591 'hashref' => \%options,
8592 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
8595 #no exact match, trying substring/fuzzy
8596 #always do substring & fuzzy (unless they're explicity config'ed off)
8597 #getting complaints searches are not returning enough
8598 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
8600 #still some false laziness w/search_sql (was search/cust_main.cgi)
8605 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
8606 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
8609 if ( $first && $last ) {
8612 { 'first' => { op=>'ILIKE', value=>"%$first%" },
8613 'last' => { op=>'ILIKE', value=>"%$last%" },
8615 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
8616 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
8623 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
8624 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
8628 foreach my $hashref ( @hashrefs ) {
8630 push @cust_main, qsearch( {
8631 'table' => 'cust_main',
8632 'hashref' => { %$hashref,
8635 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
8644 " AND $agentnums_sql", #extra_sql #agent virtualization
8647 if ( $first && $last ) {
8648 push @cust_main, FS::cust_main->fuzzy_search(
8649 { 'last' => $last, #fuzzy hashref
8650 'first' => $first }, #
8654 foreach my $field ( 'last', 'company' ) {
8656 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
8663 #eliminate duplicates
8665 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
8673 Accepts the following options: I<email>, the email address to search for. The
8674 email address will be searched for as an email invoice destination and as an
8677 #Any additional options are treated as an additional qualifier on the search
8678 #(i.e. I<agentnum>).
8680 Returns a (possibly empty) array of FS::cust_main objects (but usually just
8690 my $email = delete $options{'email'};
8692 #we're only being used by RT at the moment... no agent virtualization yet
8693 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
8697 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
8699 my ( $user, $domain ) = ( $1, $2 );
8701 warn "$me smart_search: searching for $user in domain $domain"
8707 'table' => 'cust_main_invoice',
8708 'hashref' => { 'dest' => $email },
8715 map $_->cust_svc->cust_pkg,
8717 'table' => 'svc_acct',
8718 'hashref' => { 'username' => $user, },
8720 'AND ( SELECT domain FROM svc_domain
8721 WHERE svc_acct.domsvc = svc_domain.svcnum
8722 ) = '. dbh->quote($domain),
8728 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
8730 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
8737 =item check_and_rebuild_fuzzyfiles
8741 use vars qw(@fuzzyfields);
8742 @fuzzyfields = ( 'last', 'first', 'company' );
8744 sub check_and_rebuild_fuzzyfiles {
8745 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
8746 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
8749 =item rebuild_fuzzyfiles
8753 sub rebuild_fuzzyfiles {
8755 use Fcntl qw(:flock);
8757 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
8758 mkdir $dir, 0700 unless -d $dir;
8760 foreach my $fuzzy ( @fuzzyfields ) {
8762 open(LOCK,">>$dir/cust_main.$fuzzy")
8763 or die "can't open $dir/cust_main.$fuzzy: $!";
8765 or die "can't lock $dir/cust_main.$fuzzy: $!";
8767 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
8768 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
8770 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
8771 my $sth = dbh->prepare("SELECT $field FROM cust_main".
8772 " WHERE $field != '' AND $field IS NOT NULL");
8773 $sth->execute or die $sth->errstr;
8775 while ( my $row = $sth->fetchrow_arrayref ) {
8776 print CACHE $row->[0]. "\n";
8781 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
8783 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
8794 my( $self, $field ) = @_;
8795 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
8796 open(CACHE,"<$dir/cust_main.$field")
8797 or die "can't open $dir/cust_main.$field: $!";
8798 my @array = map { chomp; $_; } <CACHE>;
8803 =item append_fuzzyfiles LASTNAME COMPANY
8807 sub append_fuzzyfiles {
8808 #my( $first, $last, $company ) = @_;
8810 &check_and_rebuild_fuzzyfiles;
8812 use Fcntl qw(:flock);
8814 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
8816 foreach my $field (qw( first last company )) {
8821 open(CACHE,">>$dir/cust_main.$field")
8822 or die "can't open $dir/cust_main.$field: $!";
8823 flock(CACHE,LOCK_EX)
8824 or die "can't lock $dir/cust_main.$field: $!";
8826 print CACHE "$value\n";
8828 flock(CACHE,LOCK_UN)
8829 or die "can't unlock $dir/cust_main.$field: $!";
8844 #warn join('-',keys %$param);
8845 my $fh = $param->{filehandle};
8846 my @fields = @{$param->{fields}};
8848 eval "use Text::CSV_XS;";
8851 my $csv = new Text::CSV_XS;
8858 local $SIG{HUP} = 'IGNORE';
8859 local $SIG{INT} = 'IGNORE';
8860 local $SIG{QUIT} = 'IGNORE';
8861 local $SIG{TERM} = 'IGNORE';
8862 local $SIG{TSTP} = 'IGNORE';
8863 local $SIG{PIPE} = 'IGNORE';
8865 my $oldAutoCommit = $FS::UID::AutoCommit;
8866 local $FS::UID::AutoCommit = 0;
8869 #while ( $columns = $csv->getline($fh) ) {
8871 while ( defined($line=<$fh>) ) {
8873 $csv->parse($line) or do {
8874 $dbh->rollback if $oldAutoCommit;
8875 return "can't parse: ". $csv->error_input();
8878 my @columns = $csv->fields();
8879 #warn join('-',@columns);
8882 foreach my $field ( @fields ) {
8883 $row{$field} = shift @columns;
8886 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
8887 unless ( $cust_main ) {
8888 $dbh->rollback if $oldAutoCommit;
8889 return "unknown custnum $row{'custnum'}";
8892 if ( $row{'amount'} > 0 ) {
8893 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
8895 $dbh->rollback if $oldAutoCommit;
8899 } elsif ( $row{'amount'} < 0 ) {
8900 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
8903 $dbh->rollback if $oldAutoCommit;
8913 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
8915 return "Empty file!" unless $imported;
8921 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
8923 Sends a templated email notification to the customer (see L<Text::Template>).
8925 OPTIONS is a hash and may include
8927 I<from> - the email sender (default is invoice_from)
8929 I<to> - comma-separated scalar or arrayref of recipients
8930 (default is invoicing_list)
8932 I<subject> - The subject line of the sent email notification
8933 (default is "Notice from company_name")
8935 I<extra_fields> - a hashref of name/value pairs which will be substituted
8938 The following variables are vavailable in the template.
8940 I<$first> - the customer first name
8941 I<$last> - the customer last name
8942 I<$company> - the customer company
8943 I<$payby> - a description of the method of payment for the customer
8944 # would be nice to use FS::payby::shortname
8945 I<$payinfo> - the account information used to collect for this customer
8946 I<$expdate> - the expiration of the customer payment in seconds from epoch
8951 my ($self, $template, %options) = @_;
8953 return unless $conf->exists($template);
8955 my $from = $conf->config('invoice_from', $self->agentnum)
8956 if $conf->exists('invoice_from', $self->agentnum);
8957 $from = $options{from} if exists($options{from});
8959 my $to = join(',', $self->invoicing_list_emailonly);
8960 $to = $options{to} if exists($options{to});
8962 my $subject = "Notice from " . $conf->config('company_name', $self->agentnum)
8963 if $conf->exists('company_name', $self->agentnum);
8964 $subject = $options{subject} if exists($options{subject});
8966 my $notify_template = new Text::Template (TYPE => 'ARRAY',
8967 SOURCE => [ map "$_\n",
8968 $conf->config($template)]
8970 or die "can't create new Text::Template object: Text::Template::ERROR";
8971 $notify_template->compile()
8972 or die "can't compile template: Text::Template::ERROR";
8974 $FS::notify_template::_template::company_name =
8975 $conf->config('company_name', $self->agentnum);
8976 $FS::notify_template::_template::company_address =
8977 join("\n", $conf->config('company_address', $self->agentnum) ). "\n";
8979 my $paydate = $self->paydate || '2037-12-31';
8980 $FS::notify_template::_template::first = $self->first;
8981 $FS::notify_template::_template::last = $self->last;
8982 $FS::notify_template::_template::company = $self->company;
8983 $FS::notify_template::_template::payinfo = $self->mask_payinfo;
8984 my $payby = $self->payby;
8985 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
8986 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
8988 #credit cards expire at the end of the month/year of their exp date
8989 if ($payby eq 'CARD' || $payby eq 'DCRD') {
8990 $FS::notify_template::_template::payby = 'credit card';
8991 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
8992 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
8994 }elsif ($payby eq 'COMP') {
8995 $FS::notify_template::_template::payby = 'complimentary account';
8997 $FS::notify_template::_template::payby = 'current method';
8999 $FS::notify_template::_template::expdate = $expire_time;
9001 for (keys %{$options{extra_fields}}){
9003 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
9006 send_email(from => $from,
9008 subject => $subject,
9009 body => $notify_template->fill_in( PACKAGE =>
9010 'FS::notify_template::_template' ),
9015 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
9017 Generates a templated notification to the customer (see L<Text::Template>).
9019 OPTIONS is a hash and may include
9021 I<extra_fields> - a hashref of name/value pairs which will be substituted
9022 into the template. These values may override values mentioned below
9023 and those from the customer record.
9025 The following variables are available in the template instead of or in addition
9026 to the fields of the customer record.
9028 I<$payby> - a description of the method of payment for the customer
9029 # would be nice to use FS::payby::shortname
9030 I<$payinfo> - the masked account information used to collect for this customer
9031 I<$expdate> - the expiration of the customer payment method in seconds from epoch
9032 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
9036 sub generate_letter {
9037 my ($self, $template, %options) = @_;
9039 return unless $conf->exists($template);
9041 my $letter_template = new Text::Template
9043 SOURCE => [ map "$_\n", $conf->config($template)],
9044 DELIMITERS => [ '[@--', '--@]' ],
9046 or die "can't create new Text::Template object: Text::Template::ERROR";
9048 $letter_template->compile()
9049 or die "can't compile template: Text::Template::ERROR";
9051 my %letter_data = map { $_ => $self->$_ } $self->fields;
9052 $letter_data{payinfo} = $self->mask_payinfo;
9054 #my $paydate = $self->paydate || '2037-12-31';
9055 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
9057 my $payby = $self->payby;
9058 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
9059 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
9061 #credit cards expire at the end of the month/year of their exp date
9062 if ($payby eq 'CARD' || $payby eq 'DCRD') {
9063 $letter_data{payby} = 'credit card';
9064 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
9065 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
9067 }elsif ($payby eq 'COMP') {
9068 $letter_data{payby} = 'complimentary account';
9070 $letter_data{payby} = 'current method';
9072 $letter_data{expdate} = $expire_time;
9074 for (keys %{$options{extra_fields}}){
9075 $letter_data{$_} = $options{extra_fields}->{$_};
9078 unless(exists($letter_data{returnaddress})){
9079 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
9080 $self->agent_template)
9082 if ( length($retadd) ) {
9083 $letter_data{returnaddress} = $retadd;
9084 } elsif ( grep /\S/, $conf->config('company_address', $self->agentnum) ) {
9085 $letter_data{returnaddress} =
9086 join( '\\*'."\n", map s/( {2,})/'~' x length($1)/eg,
9087 $conf->config('company_address', $self->agentnum)
9090 $letter_data{returnaddress} = '~';
9094 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
9096 $letter_data{company_name} = $conf->config('company_name', $self->agentnum);
9098 my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
9099 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
9103 ) or die "can't open temp file: $!\n";
9105 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
9107 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
9111 =item print_ps TEMPLATE
9113 Returns an postscript letter filled in from TEMPLATE, as a scalar.
9119 my $file = $self->generate_letter(@_);
9120 FS::Misc::generate_ps($file);
9123 =item print TEMPLATE
9125 Prints the filled in template.
9127 TEMPLATE is the name of a L<Text::Template> to fill in and print.
9131 sub queueable_print {
9134 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
9135 or die "invalid customer number: " . $opt{custvnum};
9137 my $error = $self->print( $opt{template} );
9138 die $error if $error;
9142 my ($self, $template) = (shift, shift);
9143 do_print [ $self->print_ps($template) ];
9146 #these three subs should just go away once agent stuff is all config overrides
9148 sub agent_template {
9150 $self->_agent_plandata('agent_templatename');
9153 sub agent_invoice_from {
9155 $self->_agent_plandata('agent_invoice_from');
9158 sub _agent_plandata {
9159 my( $self, $option ) = @_;
9161 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
9162 #agent-specific Conf
9164 use FS::part_event::Condition;
9166 my $agentnum = $self->agentnum;
9169 if ( driver_name =~ /^Pg/i ) {
9171 } elsif ( driver_name =~ /^mysql/i ) {
9174 die "don't know how to use regular expressions in ". driver_name. " databases";
9177 my $part_event_option =
9179 'select' => 'part_event_option.*',
9180 'table' => 'part_event_option',
9182 LEFT JOIN part_event USING ( eventpart )
9183 LEFT JOIN part_event_option AS peo_agentnum
9184 ON ( part_event.eventpart = peo_agentnum.eventpart
9185 AND peo_agentnum.optionname = 'agentnum'
9186 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
9188 LEFT JOIN part_event_condition
9189 ON ( part_event.eventpart = part_event_condition.eventpart
9190 AND part_event_condition.conditionname = 'cust_bill_age'
9192 LEFT JOIN part_event_condition_option
9193 ON ( part_event_condition.eventconditionnum = part_event_condition_option.eventconditionnum
9194 AND part_event_condition_option.optionname = 'age'
9197 #'hashref' => { 'optionname' => $option },
9198 #'hashref' => { 'part_event_option.optionname' => $option },
9200 " WHERE part_event_option.optionname = ". dbh->quote($option).
9201 " AND action = 'cust_bill_send_agent' ".
9202 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
9203 " AND peo_agentnum.optionname = 'agentnum' ".
9204 " AND ( agentnum IS NULL OR agentnum = $agentnum ) ".
9206 CASE WHEN part_event_condition_option.optionname IS NULL
9208 ELSE ". FS::part_event::Condition->age2seconds_sql('part_event_condition_option.optionvalue').
9210 , part_event.weight".
9214 unless ( $part_event_option ) {
9215 return $self->agent->invoice_template || ''
9216 if $option eq 'agent_templatename';
9220 $part_event_option->optionvalue;
9225 ## actual sub, not a method, designed to be called from the queue.
9226 ## sets up the customer, and calls the bill_and_collect
9227 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
9228 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
9229 $cust_main->bill_and_collect(
9234 sub _upgrade_data { #class method
9235 my ($class, %opts) = @_;
9237 my $sql = 'UPDATE h_cust_main SET paycvv = NULL WHERE paycvv IS NOT NULL';
9238 my $sth = dbh->prepare($sql) or die dbh->errstr;
9239 $sth->execute or die $sth->errstr;
9249 The delete method should possibly take an FS::cust_main object reference
9250 instead of a scalar customer number.
9252 Bill and collect options should probably be passed as references instead of a
9255 There should probably be a configuration file with a list of allowed credit
9258 No multiple currency support (probably a larger project than just this module).
9260 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
9262 Birthdates rely on negative epoch values.
9264 The payby for card/check batches is broken. With mixed batching, bad
9267 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
9271 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
9272 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
9273 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.