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 Time::Local qw(timelocal_nocheck);
15 use Digest::MD5 qw(md5_base64);
19 use String::Approx qw(amatch);
20 use Business::CreditCard 0.28;
22 use FS::UID qw( getotaker dbh driver_name );
23 use FS::Record qw( qsearchs qsearch dbdef );
24 use FS::Misc qw( generate_email send_email generate_ps do_print );
25 use FS::Msgcat qw(gettext);
29 use FS::cust_bill_pkg;
31 use FS::cust_pay_pending;
32 use FS::cust_pay_void;
33 use FS::cust_pay_batch;
36 use FS::part_referral;
37 use FS::cust_main_county;
39 use FS::cust_main_invoice;
40 use FS::cust_credit_bill;
41 use FS::cust_bill_pay;
42 use FS::prepay_credit;
46 use FS::part_event_condition;
49 use FS::payment_gateway;
50 use FS::agent_payment_gateway;
52 use FS::payinfo_Mixin;
55 @ISA = qw( FS::payinfo_Mixin FS::Record );
57 @EXPORT_OK = qw( smart_search );
59 $realtime_bop_decline_quiet = 0;
61 # 1 is mostly method/subroutine entry and options
62 # 2 traces progress of some operations
63 # 3 is even more information including possibly sensitive data
65 $me = '[FS::cust_main]';
69 $ignore_expired_card = 0;
71 @encrypted_fields = ('payinfo', 'paycvv');
72 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
74 #ask FS::UID to run this stuff for us later
75 #$FS::UID::callback{'FS::cust_main'} = sub {
76 install_callback FS::UID sub {
78 #yes, need it for stuff below (prolly should be cached)
83 my ( $hashref, $cache ) = @_;
84 if ( exists $hashref->{'pkgnum'} ) {
85 #@{ $self->{'_pkgnum'} } = ();
86 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
87 $self->{'_pkgnum'} = $subcache;
88 #push @{ $self->{'_pkgnum'} },
89 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
95 FS::cust_main - Object methods for cust_main records
101 $record = new FS::cust_main \%hash;
102 $record = new FS::cust_main { 'column' => 'value' };
104 $error = $record->insert;
106 $error = $new_record->replace($old_record);
108 $error = $record->delete;
110 $error = $record->check;
112 @cust_pkg = $record->all_pkgs;
114 @cust_pkg = $record->ncancelled_pkgs;
116 @cust_pkg = $record->suspended_pkgs;
118 $error = $record->bill;
119 $error = $record->bill %options;
120 $error = $record->bill 'time' => $time;
122 $error = $record->collect;
123 $error = $record->collect %options;
124 $error = $record->collect 'invoice_time' => $time,
129 An FS::cust_main object represents a customer. FS::cust_main inherits from
130 FS::Record. The following fields are currently supported:
134 =item custnum - primary key (assigned automatically for new customers)
136 =item agentnum - agent (see L<FS::agent>)
138 =item refnum - Advertising source (see L<FS::part_referral>)
144 =item ss - social security number (optional)
146 =item company - (optional)
150 =item address2 - (optional)
154 =item county - (optional, see L<FS::cust_main_county>)
156 =item state - (see L<FS::cust_main_county>)
160 =item country - (see L<FS::cust_main_county>)
162 =item daytime - phone (optional)
164 =item night - phone (optional)
166 =item fax - phone (optional)
168 =item ship_first - name
170 =item ship_last - name
172 =item ship_company - (optional)
176 =item ship_address2 - (optional)
180 =item ship_county - (optional, see L<FS::cust_main_county>)
182 =item ship_state - (see L<FS::cust_main_county>)
186 =item ship_country - (see L<FS::cust_main_county>)
188 =item ship_daytime - phone (optional)
190 =item ship_night - phone (optional)
192 =item ship_fax - phone (optional)
194 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
196 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
198 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
202 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
204 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
206 =item paystart_month - start date month (maestro/solo cards only)
208 =item paystart_year - start date year (maestro/solo cards only)
210 =item payissue - issue number (maestro/solo cards only)
212 =item payname - name on card or billing name
214 =item payip - IP address from which payment information was received
216 =item tax - tax exempt, empty or `Y'
218 =item otaker - order taker (assigned automatically, see L<FS::UID>)
220 =item comments - comments (optional)
222 =item referral_custnum - referring customer number
224 =item spool_cdr - Enable individual CDR spooling, empty or `Y'
234 Creates a new customer. To add the customer to the database, see L<"insert">.
236 Note that this stores the hash reference, not a distinct copy of the hash it
237 points to. You can ask the object for a copy with the I<hash> method.
241 sub table { 'cust_main'; }
243 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
245 Adds this customer to the database. If there is an error, returns the error,
246 otherwise returns false.
248 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
249 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
250 are inserted atomicly, or the transaction is rolled back. Passing an empty
251 hash reference is equivalent to not supplying this parameter. There should be
252 a better explanation of this, but until then, here's an example:
255 tie %hash, 'Tie::RefHash'; #this part is important
257 $cust_pkg => [ $svc_acct ],
260 $cust_main->insert( \%hash );
262 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
263 be set as the invoicing list (see L<"invoicing_list">). Errors return as
264 expected and rollback the entire transaction; it is not necessary to call
265 check_invoicing_list first. The invoicing_list is set after the records in the
266 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
267 invoicing_list destination to the newly-created svc_acct. Here's an example:
269 $cust_main->insert( {}, [ $email, 'POST' ] );
271 Currently available options are: I<depend_jobnum> and I<noexport>.
273 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
274 on the supplied jobnum (they will not run until the specific job completes).
275 This can be used to defer provisioning until some action completes (such
276 as running the customer's credit card successfully).
278 The I<noexport> option is deprecated. If I<noexport> is set true, no
279 provisioning jobs (exports) are scheduled. (You can schedule them later with
280 the B<reexport> method.)
286 my $cust_pkgs = @_ ? shift : {};
287 my $invoicing_list = @_ ? shift : '';
289 warn "$me insert called with options ".
290 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
293 local $SIG{HUP} = 'IGNORE';
294 local $SIG{INT} = 'IGNORE';
295 local $SIG{QUIT} = 'IGNORE';
296 local $SIG{TERM} = 'IGNORE';
297 local $SIG{TSTP} = 'IGNORE';
298 local $SIG{PIPE} = 'IGNORE';
300 my $oldAutoCommit = $FS::UID::AutoCommit;
301 local $FS::UID::AutoCommit = 0;
304 my $prepay_identifier = '';
305 my( $amount, $seconds ) = ( 0, 0 );
307 if ( $self->payby eq 'PREPAY' ) {
309 $self->payby('BILL');
310 $prepay_identifier = $self->payinfo;
313 warn " looking up prepaid card $prepay_identifier\n"
316 my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
318 $dbh->rollback if $oldAutoCommit;
319 #return "error applying prepaid card (transaction rolled back): $error";
323 $payby = 'PREP' if $amount;
325 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
328 $self->payby('BILL');
329 $amount = $self->paid;
333 warn " inserting $self\n"
336 $self->signupdate(time) unless $self->signupdate;
338 my $error = $self->SUPER::insert;
340 $dbh->rollback if $oldAutoCommit;
341 #return "inserting cust_main record (transaction rolled back): $error";
345 warn " setting invoicing list\n"
348 if ( $invoicing_list ) {
349 $error = $self->check_invoicing_list( $invoicing_list );
351 $dbh->rollback if $oldAutoCommit;
352 #return "checking invoicing_list (transaction rolled back): $error";
355 $self->invoicing_list( $invoicing_list );
358 if ( $conf->config('cust_main-skeleton_tables')
359 && $conf->config('cust_main-skeleton_custnum') ) {
361 warn " inserting skeleton records\n"
364 my $error = $self->start_copy_skel;
366 $dbh->rollback if $oldAutoCommit;
372 warn " ordering packages\n"
375 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
377 $dbh->rollback if $oldAutoCommit;
382 $dbh->rollback if $oldAutoCommit;
383 return "No svc_acct record to apply pre-paid time";
387 warn " inserting initial $payby payment of $amount\n"
389 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
391 $dbh->rollback if $oldAutoCommit;
392 return "inserting payment (transaction rolled back): $error";
396 unless ( $import || $skip_fuzzyfiles ) {
397 warn " queueing fuzzyfiles update\n"
399 $error = $self->queue_fuzzyfiles_update;
401 $dbh->rollback if $oldAutoCommit;
402 return "updating fuzzy search cache: $error";
406 warn " insert complete; committing transaction\n"
409 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
414 sub start_copy_skel {
417 #'mg_user_preference' => {},
418 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
419 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
420 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
421 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
422 my @tables = eval(join('\n',$conf->config('cust_main-skeleton_tables')));
425 _copy_skel( 'cust_main', #tablename
426 $conf->config('cust_main-skeleton_custnum'), #sourceid
427 $self->custnum, #destid
428 @tables, #child tables
432 #recursive subroutine, not a method
434 my( $table, $sourceid, $destid, %child_tables ) = @_;
437 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
438 ( $table, $primary_key ) = ( $1, $2 );
440 my $dbdef_table = dbdef->table($table);
441 $primary_key = $dbdef_table->primary_key
442 or return "$table has no primary key".
443 " (or do you need to run dbdef-create?)";
446 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
447 join (', ', keys %child_tables). "\n"
450 foreach my $child_table_def ( keys %child_tables ) {
454 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
455 ( $child_table, $child_pkey ) = ( $1, $2 );
457 $child_table = $child_table_def;
459 $child_pkey = dbdef->table($child_table)->primary_key;
460 # or return "$table has no primary key".
461 # " (or do you need to run dbdef-create?)\n";
465 if ( keys %{ $child_tables{$child_table_def} } ) {
467 return "$child_table has no primary key".
468 " (run dbdef-create or try specifying it?)\n"
471 #false laziness w/Record::insert and only works on Pg
472 #refactor the proper last-inserted-id stuff out of Record::insert if this
473 # ever gets use for anything besides a quick kludge for one customer
474 my $default = dbdef->table($child_table)->column($child_pkey)->default;
475 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
476 or return "can't parse $child_table.$child_pkey default value ".
477 " for sequence name: $default";
482 my @sel_columns = grep { $_ ne $primary_key }
483 dbdef->table($child_table)->columns;
484 my $sel_columns = join(', ', @sel_columns );
486 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
487 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
488 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
490 my $sel_st = "SELECT $sel_columns FROM $child_table".
491 " WHERE $primary_key = $sourceid";
494 my $sel_sth = dbh->prepare( $sel_st )
495 or return dbh->errstr;
497 $sel_sth->execute or return $sel_sth->errstr;
499 while ( my $row = $sel_sth->fetchrow_hashref ) {
501 warn " selected row: ".
502 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
506 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
507 my $ins_sth =dbh->prepare($statement)
508 or return dbh->errstr;
509 my @param = ( $destid, map $row->{$_}, @ins_columns );
510 warn " $statement: [ ". join(', ', @param). " ]\n"
512 $ins_sth->execute( @param )
513 or return $ins_sth->errstr;
515 #next unless keys %{ $child_tables{$child_table} };
516 next unless $sequence;
518 #another section of that laziness
519 my $seq_sql = "SELECT currval('$sequence')";
520 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
521 $seq_sth->execute or return $seq_sth->errstr;
522 my $insertid = $seq_sth->fetchrow_arrayref->[0];
524 # don't drink soap! recurse! recurse! okay!
526 _copy_skel( $child_table_def,
527 $row->{$child_pkey}, #sourceid
529 %{ $child_tables{$child_table_def} },
531 return $error if $error;
541 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
543 Like the insert method on an existing record, this method orders a package
544 and included services atomicaly. Pass a Tie::RefHash data structure to this
545 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
546 be a better explanation of this, but until then, here's an example:
549 tie %hash, 'Tie::RefHash'; #this part is important
551 $cust_pkg => [ $svc_acct ],
554 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
556 Services can be new, in which case they are inserted, or existing unaudited
557 services, in which case they are linked to the newly-created package.
559 Currently available options are: I<depend_jobnum> and I<noexport>.
561 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
562 on the supplied jobnum (they will not run until the specific job completes).
563 This can be used to defer provisioning until some action completes (such
564 as running the customer's credit card successfully).
566 The I<noexport> option is deprecated. If I<noexport> is set true, no
567 provisioning jobs (exports) are scheduled. (You can schedule them later with
568 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
569 on the cust_main object is not recommended, as existing services will also be
576 my $cust_pkgs = shift;
579 my %svc_options = ();
580 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
581 if exists $options{'depend_jobnum'};
582 warn "$me order_pkgs called with options ".
583 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
586 local $SIG{HUP} = 'IGNORE';
587 local $SIG{INT} = 'IGNORE';
588 local $SIG{QUIT} = 'IGNORE';
589 local $SIG{TERM} = 'IGNORE';
590 local $SIG{TSTP} = 'IGNORE';
591 local $SIG{PIPE} = 'IGNORE';
593 my $oldAutoCommit = $FS::UID::AutoCommit;
594 local $FS::UID::AutoCommit = 0;
597 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
599 foreach my $cust_pkg ( keys %$cust_pkgs ) {
600 $cust_pkg->custnum( $self->custnum );
601 my $error = $cust_pkg->insert;
603 $dbh->rollback if $oldAutoCommit;
604 return "inserting cust_pkg (transaction rolled back): $error";
606 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
607 if ( $svc_something->svcnum ) {
608 my $old_cust_svc = $svc_something->cust_svc;
609 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
610 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
611 $error = $new_cust_svc->replace($old_cust_svc);
613 $svc_something->pkgnum( $cust_pkg->pkgnum );
614 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
615 $svc_something->seconds( $svc_something->seconds + $$seconds );
618 $error = $svc_something->insert(%svc_options);
621 $dbh->rollback if $oldAutoCommit;
622 #return "inserting svc_ (transaction rolled back): $error";
628 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
632 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
634 Recharges this (existing) customer with the specified prepaid card (see
635 L<FS::prepay_credit>), specified either by I<identifier> or as an
636 FS::prepay_credit object. If there is an error, returns the error, otherwise
639 Optionally, four scalar references can be passed as well. They will have their
640 values filled in with the amount, number of seconds, and number of upload and
641 download bytes applied by this prepaid
646 sub recharge_prepay {
647 my( $self, $prepay_credit, $amountref, $secondsref,
648 $upbytesref, $downbytesref, $totalbytesref ) = @_;
650 local $SIG{HUP} = 'IGNORE';
651 local $SIG{INT} = 'IGNORE';
652 local $SIG{QUIT} = 'IGNORE';
653 local $SIG{TERM} = 'IGNORE';
654 local $SIG{TSTP} = 'IGNORE';
655 local $SIG{PIPE} = 'IGNORE';
657 my $oldAutoCommit = $FS::UID::AutoCommit;
658 local $FS::UID::AutoCommit = 0;
661 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
663 my $error = $self->get_prepay($prepay_credit, \$amount,
664 \$seconds, \$upbytes, \$downbytes, \$totalbytes)
665 || $self->increment_seconds($seconds)
666 || $self->increment_upbytes($upbytes)
667 || $self->increment_downbytes($downbytes)
668 || $self->increment_totalbytes($totalbytes)
669 || $self->insert_cust_pay_prepay( $amount,
671 ? $prepay_credit->identifier
676 $dbh->rollback if $oldAutoCommit;
680 if ( defined($amountref) ) { $$amountref = $amount; }
681 if ( defined($secondsref) ) { $$secondsref = $seconds; }
682 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
683 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
684 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
686 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
691 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
693 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
694 specified either by I<identifier> or as an FS::prepay_credit object.
696 References to I<amount> and I<seconds> scalars should be passed as arguments
697 and will be incremented by the values of the prepaid card.
699 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
700 check or set this customer's I<agentnum>.
702 If there is an error, returns the error, otherwise returns false.
708 my( $self, $prepay_credit, $amountref, $secondsref,
709 $upref, $downref, $totalref) = @_;
711 local $SIG{HUP} = 'IGNORE';
712 local $SIG{INT} = 'IGNORE';
713 local $SIG{QUIT} = 'IGNORE';
714 local $SIG{TERM} = 'IGNORE';
715 local $SIG{TSTP} = 'IGNORE';
716 local $SIG{PIPE} = 'IGNORE';
718 my $oldAutoCommit = $FS::UID::AutoCommit;
719 local $FS::UID::AutoCommit = 0;
722 unless ( ref($prepay_credit) ) {
724 my $identifier = $prepay_credit;
726 $prepay_credit = qsearchs(
728 { 'identifier' => $prepay_credit },
733 unless ( $prepay_credit ) {
734 $dbh->rollback if $oldAutoCommit;
735 return "Invalid prepaid card: ". $identifier;
740 if ( $prepay_credit->agentnum ) {
741 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
742 $dbh->rollback if $oldAutoCommit;
743 return "prepaid card not valid for agent ". $self->agentnum;
745 $self->agentnum($prepay_credit->agentnum);
748 my $error = $prepay_credit->delete;
750 $dbh->rollback if $oldAutoCommit;
751 return "removing prepay_credit (transaction rolled back): $error";
754 $$amountref += $prepay_credit->amount;
755 $$secondsref += $prepay_credit->seconds;
756 $$upref += $prepay_credit->upbytes;
757 $$downref += $prepay_credit->downbytes;
758 $$totalref += $prepay_credit->totalbytes;
760 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
765 =item increment_upbytes SECONDS
767 Updates this customer's single or primary account (see L<FS::svc_acct>) by
768 the specified number of upbytes. If there is an error, returns the error,
769 otherwise returns false.
773 sub increment_upbytes {
774 _increment_column( shift, 'upbytes', @_);
777 =item increment_downbytes SECONDS
779 Updates this customer's single or primary account (see L<FS::svc_acct>) by
780 the specified number of downbytes. If there is an error, returns the error,
781 otherwise returns false.
785 sub increment_downbytes {
786 _increment_column( shift, 'downbytes', @_);
789 =item increment_totalbytes SECONDS
791 Updates this customer's single or primary account (see L<FS::svc_acct>) by
792 the specified number of totalbytes. If there is an error, returns the error,
793 otherwise returns false.
797 sub increment_totalbytes {
798 _increment_column( shift, 'totalbytes', @_);
801 =item increment_seconds SECONDS
803 Updates this customer's single or primary account (see L<FS::svc_acct>) by
804 the specified number of seconds. If there is an error, returns the error,
805 otherwise returns false.
809 sub increment_seconds {
810 _increment_column( shift, 'seconds', @_);
813 =item _increment_column AMOUNT
815 Updates this customer's single or primary account (see L<FS::svc_acct>) by
816 the specified number of seconds or bytes. If there is an error, returns
817 the error, otherwise returns false.
821 sub _increment_column {
822 my( $self, $column, $amount ) = @_;
823 warn "$me increment_column called: $column, $amount\n"
826 return '' unless $amount;
828 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
829 $self->ncancelled_pkgs;
832 return 'No packages with primary or single services found'.
833 ' to apply pre-paid time';
834 } elsif ( scalar(@cust_pkg) > 1 ) {
835 #maybe have a way to specify the package/account?
836 return 'Multiple packages found to apply pre-paid time';
839 my $cust_pkg = $cust_pkg[0];
840 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
844 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
847 return 'No account found to apply pre-paid time';
848 } elsif ( scalar(@cust_svc) > 1 ) {
849 return 'Multiple accounts found to apply pre-paid time';
852 my $svc_acct = $cust_svc[0]->svc_x;
853 warn " found service svcnum ". $svc_acct->pkgnum.
854 ' ('. $svc_acct->email. ")\n"
857 $column = "increment_$column";
858 $svc_acct->$column($amount);
862 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
864 Inserts a prepayment in the specified amount for this customer. An optional
865 second argument can specify the prepayment identifier for tracking purposes.
866 If there is an error, returns the error, otherwise returns false.
870 sub insert_cust_pay_prepay {
871 shift->insert_cust_pay('PREP', @_);
874 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
876 Inserts a cash payment in the specified amount for this customer. An optional
877 second argument can specify the payment identifier for tracking purposes.
878 If there is an error, returns the error, otherwise returns false.
882 sub insert_cust_pay_cash {
883 shift->insert_cust_pay('CASH', @_);
886 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
888 Inserts a Western Union payment in the specified amount for this customer. An
889 optional second argument can specify the prepayment identifier for tracking
890 purposes. If there is an error, returns the error, otherwise returns false.
894 sub insert_cust_pay_west {
895 shift->insert_cust_pay('WEST', @_);
898 sub insert_cust_pay {
899 my( $self, $payby, $amount ) = splice(@_, 0, 3);
900 my $payinfo = scalar(@_) ? shift : '';
902 my $cust_pay = new FS::cust_pay {
903 'custnum' => $self->custnum,
904 'paid' => sprintf('%.2f', $amount),
905 #'_date' => #date the prepaid card was purchased???
907 'payinfo' => $payinfo,
915 This method is deprecated. See the I<depend_jobnum> option to the insert and
916 order_pkgs methods for a better way to defer provisioning.
918 Re-schedules all exports by calling the B<reexport> method of all associated
919 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
920 otherwise returns false.
927 carp "WARNING: FS::cust_main::reexport is deprectated; ".
928 "use the depend_jobnum option to insert or order_pkgs to delay export";
930 local $SIG{HUP} = 'IGNORE';
931 local $SIG{INT} = 'IGNORE';
932 local $SIG{QUIT} = 'IGNORE';
933 local $SIG{TERM} = 'IGNORE';
934 local $SIG{TSTP} = 'IGNORE';
935 local $SIG{PIPE} = 'IGNORE';
937 my $oldAutoCommit = $FS::UID::AutoCommit;
938 local $FS::UID::AutoCommit = 0;
941 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
942 my $error = $cust_pkg->reexport;
944 $dbh->rollback if $oldAutoCommit;
949 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
954 =item delete NEW_CUSTNUM
956 This deletes the customer. If there is an error, returns the error, otherwise
959 This will completely remove all traces of the customer record. This is not
960 what you want when a customer cancels service; for that, cancel all of the
961 customer's packages (see L</cancel>).
963 If the customer has any uncancelled packages, you need to pass a new (valid)
964 customer number for those packages to be transferred to. Cancelled packages
965 will be deleted. Did I mention that this is NOT what you want when a customer
966 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
968 You can't delete a customer with invoices (see L<FS::cust_bill>),
969 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
970 refunds (see L<FS::cust_refund>).
977 local $SIG{HUP} = 'IGNORE';
978 local $SIG{INT} = 'IGNORE';
979 local $SIG{QUIT} = 'IGNORE';
980 local $SIG{TERM} = 'IGNORE';
981 local $SIG{TSTP} = 'IGNORE';
982 local $SIG{PIPE} = 'IGNORE';
984 my $oldAutoCommit = $FS::UID::AutoCommit;
985 local $FS::UID::AutoCommit = 0;
988 if ( $self->cust_bill ) {
989 $dbh->rollback if $oldAutoCommit;
990 return "Can't delete a customer with invoices";
992 if ( $self->cust_credit ) {
993 $dbh->rollback if $oldAutoCommit;
994 return "Can't delete a customer with credits";
996 if ( $self->cust_pay ) {
997 $dbh->rollback if $oldAutoCommit;
998 return "Can't delete a customer with payments";
1000 if ( $self->cust_refund ) {
1001 $dbh->rollback if $oldAutoCommit;
1002 return "Can't delete a customer with refunds";
1005 my @cust_pkg = $self->ncancelled_pkgs;
1007 my $new_custnum = shift;
1008 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1009 $dbh->rollback if $oldAutoCommit;
1010 return "Invalid new customer number: $new_custnum";
1012 foreach my $cust_pkg ( @cust_pkg ) {
1013 my %hash = $cust_pkg->hash;
1014 $hash{'custnum'} = $new_custnum;
1015 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1016 my $error = $new_cust_pkg->replace($cust_pkg,
1017 options => { $cust_pkg->options },
1020 $dbh->rollback if $oldAutoCommit;
1025 my @cancelled_cust_pkg = $self->all_pkgs;
1026 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1027 my $error = $cust_pkg->delete;
1029 $dbh->rollback if $oldAutoCommit;
1034 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1035 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1037 my $error = $cust_main_invoice->delete;
1039 $dbh->rollback if $oldAutoCommit;
1044 my $error = $self->SUPER::delete;
1046 $dbh->rollback if $oldAutoCommit;
1050 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1055 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ]
1057 Replaces the OLD_RECORD with this one in the database. If there is an error,
1058 returns the error, otherwise returns false.
1060 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1061 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1062 expected and rollback the entire transaction; it is not necessary to call
1063 check_invoicing_list first. Here's an example:
1065 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1072 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1074 : $self->replace_old;
1078 warn "$me replace called\n"
1081 my $curuser = $FS::CurrentUser::CurrentUser;
1082 if ( $self->payby eq 'COMP'
1083 && $self->payby ne $old->payby
1084 && ! $curuser->access_right('Complimentary customer')
1087 return "You are not permitted to create complimentary accounts.";
1090 local($ignore_expired_card) = 1
1091 if $old->payby =~ /^(CARD|DCRD)$/
1092 && $self->payby =~ /^(CARD|DCRD)$/
1093 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1095 local $SIG{HUP} = 'IGNORE';
1096 local $SIG{INT} = 'IGNORE';
1097 local $SIG{QUIT} = 'IGNORE';
1098 local $SIG{TERM} = 'IGNORE';
1099 local $SIG{TSTP} = 'IGNORE';
1100 local $SIG{PIPE} = 'IGNORE';
1102 my $oldAutoCommit = $FS::UID::AutoCommit;
1103 local $FS::UID::AutoCommit = 0;
1106 my $error = $self->SUPER::replace($old);
1109 $dbh->rollback if $oldAutoCommit;
1113 if ( @param ) { # INVOICING_LIST_ARYREF
1114 my $invoicing_list = shift @param;
1115 $error = $self->check_invoicing_list( $invoicing_list );
1117 $dbh->rollback if $oldAutoCommit;
1120 $self->invoicing_list( $invoicing_list );
1123 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1124 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1125 # card/check/lec info has changed, want to retry realtime_ invoice events
1126 my $error = $self->retry_realtime;
1128 $dbh->rollback if $oldAutoCommit;
1133 unless ( $import || $skip_fuzzyfiles ) {
1134 $error = $self->queue_fuzzyfiles_update;
1136 $dbh->rollback if $oldAutoCommit;
1137 return "updating fuzzy search cache: $error";
1141 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1146 =item queue_fuzzyfiles_update
1148 Used by insert & replace to update the fuzzy search cache
1152 sub queue_fuzzyfiles_update {
1155 local $SIG{HUP} = 'IGNORE';
1156 local $SIG{INT} = 'IGNORE';
1157 local $SIG{QUIT} = 'IGNORE';
1158 local $SIG{TERM} = 'IGNORE';
1159 local $SIG{TSTP} = 'IGNORE';
1160 local $SIG{PIPE} = 'IGNORE';
1162 my $oldAutoCommit = $FS::UID::AutoCommit;
1163 local $FS::UID::AutoCommit = 0;
1166 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1167 my $error = $queue->insert( map $self->getfield($_),
1168 qw(first last company)
1171 $dbh->rollback if $oldAutoCommit;
1172 return "queueing job (transaction rolled back): $error";
1175 if ( $self->ship_last ) {
1176 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1177 $error = $queue->insert( map $self->getfield("ship_$_"),
1178 qw(first last company)
1181 $dbh->rollback if $oldAutoCommit;
1182 return "queueing job (transaction rolled back): $error";
1186 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1193 Checks all fields to make sure this is a valid customer record. If there is
1194 an error, returns the error, otherwise returns false. Called by the insert
1195 and replace methods.
1202 warn "$me check BEFORE: \n". $self->_dump
1206 $self->ut_numbern('custnum')
1207 || $self->ut_number('agentnum')
1208 || $self->ut_textn('agent_custid')
1209 || $self->ut_number('refnum')
1210 || $self->ut_name('last')
1211 || $self->ut_name('first')
1212 || $self->ut_snumbern('birthdate')
1213 || $self->ut_snumbern('signupdate')
1214 || $self->ut_textn('company')
1215 || $self->ut_text('address1')
1216 || $self->ut_textn('address2')
1217 || $self->ut_text('city')
1218 || $self->ut_textn('county')
1219 || $self->ut_textn('state')
1220 || $self->ut_country('country')
1221 || $self->ut_anything('comments')
1222 || $self->ut_numbern('referral_custnum')
1223 || $self->ut_textn('stateid')
1224 || $self->ut_textn('stateid_state')
1225 || $self->ut_textn('invoice_terms')
1227 #barf. need message catalogs. i18n. etc.
1228 $error .= "Please select an advertising source."
1229 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1230 return $error if $error;
1232 return "Unknown agent"
1233 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1235 return "Unknown refnum"
1236 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1238 return "Unknown referring custnum: ". $self->referral_custnum
1239 unless ! $self->referral_custnum
1240 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1242 if ( $self->ss eq '' ) {
1247 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1248 or return "Illegal social security number: ". $self->ss;
1249 $self->ss("$1-$2-$3");
1253 # bad idea to disable, causes billing to fail because of no tax rates later
1254 # unless ( $import ) {
1255 unless ( qsearch('cust_main_county', {
1256 'country' => $self->country,
1259 return "Unknown state/county/country: ".
1260 $self->state. "/". $self->county. "/". $self->country
1261 unless qsearch('cust_main_county',{
1262 'state' => $self->state,
1263 'county' => $self->county,
1264 'country' => $self->country,
1270 $self->ut_phonen('daytime', $self->country)
1271 || $self->ut_phonen('night', $self->country)
1272 || $self->ut_phonen('fax', $self->country)
1273 || $self->ut_zip('zip', $self->country)
1275 return $error if $error;
1277 if ( $conf->exists('cust_main-require_phone')
1278 && ! length($self->daytime) && ! length($self->night)
1281 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1283 : FS::Msgcat::_gettext('daytime');
1284 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1286 : FS::Msgcat::_gettext('night');
1288 return "$daytime_label or $night_label is required"
1292 if ( $self->has_ship_address
1293 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1294 $self->addr_fields )
1298 $self->ut_name('ship_last')
1299 || $self->ut_name('ship_first')
1300 || $self->ut_textn('ship_company')
1301 || $self->ut_text('ship_address1')
1302 || $self->ut_textn('ship_address2')
1303 || $self->ut_text('ship_city')
1304 || $self->ut_textn('ship_county')
1305 || $self->ut_textn('ship_state')
1306 || $self->ut_country('ship_country')
1308 return $error if $error;
1310 #false laziness with above
1311 unless ( qsearchs('cust_main_county', {
1312 'country' => $self->ship_country,
1315 return "Unknown ship_state/ship_county/ship_country: ".
1316 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1317 unless qsearch('cust_main_county',{
1318 'state' => $self->ship_state,
1319 'county' => $self->ship_county,
1320 'country' => $self->ship_country,
1326 $self->ut_phonen('ship_daytime', $self->ship_country)
1327 || $self->ut_phonen('ship_night', $self->ship_country)
1328 || $self->ut_phonen('ship_fax', $self->ship_country)
1329 || $self->ut_zip('ship_zip', $self->ship_country)
1331 return $error if $error;
1333 return "Unit # is required."
1334 if $self->ship_address2 =~ /^\s*$/
1335 && $conf->exists('cust_main-require_address2');
1337 } else { # ship_ info eq billing info, so don't store dup info in database
1339 $self->setfield("ship_$_", '')
1340 foreach $self->addr_fields;
1342 return "Unit # is required."
1343 if $self->address2 =~ /^\s*$/
1344 && $conf->exists('cust_main-require_address2');
1348 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1349 # or return "Illegal payby: ". $self->payby;
1351 FS::payby->can_payby($self->table, $self->payby)
1352 or return "Illegal payby: ". $self->payby;
1354 $error = $self->ut_numbern('paystart_month')
1355 || $self->ut_numbern('paystart_year')
1356 || $self->ut_numbern('payissue')
1357 || $self->ut_textn('paytype')
1359 return $error if $error;
1361 if ( $self->payip eq '' ) {
1364 $error = $self->ut_ip('payip');
1365 return $error if $error;
1368 # If it is encrypted and the private key is not availaible then we can't
1369 # check the credit card.
1371 my $check_payinfo = 1;
1373 if ($self->is_encrypted($self->payinfo)) {
1377 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1379 my $payinfo = $self->payinfo;
1380 $payinfo =~ s/\D//g;
1381 $payinfo =~ /^(\d{13,16})$/
1382 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1384 $self->payinfo($payinfo);
1386 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1388 return gettext('unknown_card_type')
1389 if cardtype($self->payinfo) eq "Unknown";
1391 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1393 return 'Banned credit card: banned on '.
1394 time2str('%a %h %o at %r', $ban->_date).
1395 ' by '. $ban->otaker.
1396 ' (ban# '. $ban->bannum. ')';
1399 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1400 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1401 $self->paycvv =~ /^(\d{4})$/
1402 or return "CVV2 (CID) for American Express cards is four digits.";
1405 $self->paycvv =~ /^(\d{3})$/
1406 or return "CVV2 (CVC2/CID) is three digits.";
1413 my $cardtype = cardtype($payinfo);
1414 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1416 return "Start date or issue number is required for $cardtype cards"
1417 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1419 return "Start month must be between 1 and 12"
1420 if $self->paystart_month
1421 and $self->paystart_month < 1 || $self->paystart_month > 12;
1423 return "Start year must be 1990 or later"
1424 if $self->paystart_year
1425 and $self->paystart_year < 1990;
1427 return "Issue number must be beween 1 and 99"
1429 and $self->payissue < 1 || $self->payissue > 99;
1432 $self->paystart_month('');
1433 $self->paystart_year('');
1434 $self->payissue('');
1437 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1439 my $payinfo = $self->payinfo;
1440 $payinfo =~ s/[^\d\@]//g;
1441 if ( $conf->exists('echeck-nonus') ) {
1442 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1443 $payinfo = "$1\@$2";
1445 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1446 $payinfo = "$1\@$2";
1448 $self->payinfo($payinfo);
1451 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1453 return 'Banned ACH account: banned on '.
1454 time2str('%a %h %o at %r', $ban->_date).
1455 ' by '. $ban->otaker.
1456 ' (ban# '. $ban->bannum. ')';
1459 } elsif ( $self->payby eq 'LECB' ) {
1461 my $payinfo = $self->payinfo;
1462 $payinfo =~ s/\D//g;
1463 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1465 $self->payinfo($payinfo);
1468 } elsif ( $self->payby eq 'BILL' ) {
1470 $error = $self->ut_textn('payinfo');
1471 return "Illegal P.O. number: ". $self->payinfo if $error;
1474 } elsif ( $self->payby eq 'COMP' ) {
1476 my $curuser = $FS::CurrentUser::CurrentUser;
1477 if ( ! $self->custnum
1478 && ! $curuser->access_right('Complimentary customer')
1481 return "You are not permitted to create complimentary accounts."
1484 $error = $self->ut_textn('payinfo');
1485 return "Illegal comp account issuer: ". $self->payinfo if $error;
1488 } elsif ( $self->payby eq 'PREPAY' ) {
1490 my $payinfo = $self->payinfo;
1491 $payinfo =~ s/\W//g; #anything else would just confuse things
1492 $self->payinfo($payinfo);
1493 $error = $self->ut_alpha('payinfo');
1494 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1495 return "Unknown prepayment identifier"
1496 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1501 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1502 return "Expiration date required"
1503 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1507 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1508 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1509 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1510 ( $m, $y ) = ( $3, "20$2" );
1512 return "Illegal expiration date: ". $self->paydate;
1514 $self->paydate("$y-$m-01");
1515 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1516 return gettext('expired_card')
1518 && !$ignore_expired_card
1519 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1522 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1523 ( ! $conf->exists('require_cardname')
1524 || $self->payby !~ /^(CARD|DCRD)$/ )
1526 $self->payname( $self->first. " ". $self->getfield('last') );
1528 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1529 or return gettext('illegal_name'). " payname: ". $self->payname;
1533 foreach my $flag (qw( tax spool_cdr )) {
1534 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1538 $self->otaker(getotaker) unless $self->otaker;
1540 warn "$me check AFTER: \n". $self->_dump
1543 $self->SUPER::check;
1548 Returns a list of fields which have ship_ duplicates.
1553 qw( last first company
1554 address1 address2 city county state zip country
1559 =item has_ship_address
1561 Returns true if this customer record has a separate shipping address.
1565 sub has_ship_address {
1567 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
1572 Returns all packages (see L<FS::cust_pkg>) for this customer.
1579 return $self->num_pkgs unless wantarray;
1582 if ( $self->{'_pkgnum'} ) {
1583 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1585 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1588 sort sort_packages @cust_pkg;
1593 Synonym for B<all_pkgs>.
1598 shift->all_pkgs(@_);
1601 =item ncancelled_pkgs
1603 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1607 sub ncancelled_pkgs {
1610 return $self->num_ncancelled_pkgs unless wantarray;
1613 if ( $self->{'_pkgnum'} ) {
1615 warn "$me ncancelled_pkgs: returning cached objects"
1618 @cust_pkg = grep { ! $_->getfield('cancel') }
1619 values %{ $self->{'_pkgnum'}->cache };
1623 warn "$me ncancelled_pkgs: searching for packages with custnum ".
1624 $self->custnum. "\n"
1628 qsearch( 'cust_pkg', {
1629 'custnum' => $self->custnum,
1633 qsearch( 'cust_pkg', {
1634 'custnum' => $self->custnum,
1639 sort sort_packages @cust_pkg;
1643 # This should be generalized to use config options to determine order.
1645 if ( $a->get('cancel') and $b->get('cancel') ) {
1646 $a->pkgnum <=> $b->pkgnum;
1647 } elsif ( $a->get('cancel') or $b->get('cancel') ) {
1648 return -1 if $b->get('cancel');
1649 return 1 if $a->get('cancel');
1652 $a->pkgnum <=> $b->pkgnum;
1656 =item suspended_pkgs
1658 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1662 sub suspended_pkgs {
1664 grep { $_->susp } $self->ncancelled_pkgs;
1667 =item unflagged_suspended_pkgs
1669 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1670 customer (thouse packages without the `manual_flag' set).
1674 sub unflagged_suspended_pkgs {
1676 return $self->suspended_pkgs
1677 unless dbdef->table('cust_pkg')->column('manual_flag');
1678 grep { ! $_->manual_flag } $self->suspended_pkgs;
1681 =item unsuspended_pkgs
1683 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1688 sub unsuspended_pkgs {
1690 grep { ! $_->susp } $self->ncancelled_pkgs;
1693 =item num_cancelled_pkgs
1695 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1700 sub num_cancelled_pkgs {
1701 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
1704 sub num_ncancelled_pkgs {
1705 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
1709 my( $self ) = shift;
1710 my $sql = scalar(@_) ? shift : '';
1711 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
1712 my $sth = dbh->prepare(
1713 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
1714 ) or die dbh->errstr;
1715 $sth->execute($self->custnum) or die $sth->errstr;
1716 $sth->fetchrow_arrayref->[0];
1721 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1722 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1723 on success or a list of errors.
1729 grep { $_->unsuspend } $self->suspended_pkgs;
1734 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1736 Returns a list: an empty list on success or a list of errors.
1742 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1745 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1747 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1748 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
1749 of a list of pkgparts; the hashref has the following keys:
1753 =item pkgparts - listref of pkgparts
1755 =item (other options are passed to the suspend method)
1760 Returns a list: an empty list on success or a list of errors.
1764 sub suspend_if_pkgpart {
1766 my (@pkgparts, %opt);
1767 if (ref($_[0]) eq 'HASH'){
1768 @pkgparts = @{$_[0]{pkgparts}};
1773 grep { $_->suspend(%opt) }
1774 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1775 $self->unsuspended_pkgs;
1778 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1780 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1781 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
1782 instead of a list of pkgparts; the hashref has the following keys:
1786 =item pkgparts - listref of pkgparts
1788 =item (other options are passed to the suspend method)
1792 Returns a list: an empty list on success or a list of errors.
1796 sub suspend_unless_pkgpart {
1798 my (@pkgparts, %opt);
1799 if (ref($_[0]) eq 'HASH'){
1800 @pkgparts = @{$_[0]{pkgparts}};
1805 grep { $_->suspend(%opt) }
1806 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1807 $self->unsuspended_pkgs;
1810 =item cancel [ OPTION => VALUE ... ]
1812 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1814 Available options are:
1818 =item quiet - can be set true to supress email cancellation notices.
1820 =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.
1822 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
1826 Always returns a list: an empty list on success or a list of errors.
1831 my( $self, %opt ) = @_;
1833 warn "$me cancel called on customer ". $self->custnum. " with options ".
1834 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
1837 return ( 'access denied' )
1838 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
1840 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1842 #should try decryption (we might have the private key)
1843 # and if not maybe queue a job for the server that does?
1844 return ( "Can't (yet) ban encrypted credit cards" )
1845 if $self->is_encrypted($self->payinfo);
1847 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1848 my $error = $ban->insert;
1849 return ( $error ) if $error;
1853 my @pkgs = $self->ncancelled_pkgs;
1855 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
1856 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
1859 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
1862 sub _banned_pay_hashref {
1873 'payby' => $payby2ban{$self->payby},
1874 'payinfo' => md5_base64($self->payinfo),
1875 #don't ever *search* on reason! #'reason' =>
1881 Returns all notes (see L<FS::cust_main_note>) for this customer.
1888 qsearch( 'cust_main_note',
1889 { 'custnum' => $self->custnum },
1891 'ORDER BY _DATE DESC'
1897 Returns the agent (see L<FS::agent>) for this customer.
1903 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1906 =item bill_and_collect
1908 Cancels and suspends any packages due, generates bills, applies payments and
1911 Warns on errors (Does not currently: If there is an error, returns the error, otherwise returns false.)
1913 Options are passed as name-value pairs. Currently available options are:
1919 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:
1923 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1927 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.
1931 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1935 If set true, re-charges setup fees.
1939 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)
1945 sub bill_and_collect {
1946 my( $self, %options ) = @_;
1952 #$options{actual_time} not $options{time} because freeside-daily -d is for
1953 #pre-printing invoices
1954 my @cancel_pkgs = grep { $_->expire && $_->expire <= $options{actual_time} }
1955 $self->ncancelled_pkgs;
1957 foreach my $cust_pkg ( @cancel_pkgs ) {
1958 my $error = $cust_pkg->cancel;
1959 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum.
1960 " for custnum ". $self->custnum. ": $error"
1968 #$options{actual_time} not $options{time} because freeside-daily -d is for
1969 #pre-printing invoices
1972 && ( ( $_->part_pkg->is_prepaid
1974 && $_->bill < $options{actual_time}
1977 && $_->adjourn <= $options{actual_time}
1981 $self->ncancelled_pkgs;
1983 foreach my $cust_pkg ( @susp_pkgs ) {
1984 my $error = $cust_pkg->suspend;
1985 warn "Error suspending package ". $cust_pkg->pkgnum.
1986 " for custnum ". $self->custnum. ": $error"
1994 my $error = $self->bill( %options );
1995 warn "Error billing, custnum ". $self->custnum. ": $error" if $error;
1997 $self->apply_payments_and_credits;
1999 $error = $self->collect( %options );
2000 warn "Error collecting, custnum". $self->custnum. ": $error" if $error;
2006 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
2007 conjunction with the collect method by calling B<bill_and_collect>.
2009 If there is an error, returns the error, otherwise returns false.
2011 Options are passed as name-value pairs. Currently available options are:
2017 If set true, re-charges setup fees.
2021 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:
2025 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
2029 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
2031 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
2035 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.
2042 my( $self, %options ) = @_;
2043 return '' if $self->payby eq 'COMP';
2044 warn "$me bill customer ". $self->custnum. "\n"
2047 my $time = $options{'time'} || time;
2050 local $SIG{HUP} = 'IGNORE';
2051 local $SIG{INT} = 'IGNORE';
2052 local $SIG{QUIT} = 'IGNORE';
2053 local $SIG{TERM} = 'IGNORE';
2054 local $SIG{TSTP} = 'IGNORE';
2055 local $SIG{PIPE} = 'IGNORE';
2057 my $oldAutoCommit = $FS::UID::AutoCommit;
2058 local $FS::UID::AutoCommit = 0;
2061 $self->select_for_update; #mutex
2063 my @cust_bill_pkg = ();
2066 # find the packages which are due for billing, find out how much they are
2067 # & generate invoice database.
2070 my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
2074 my @precommit_hooks = ();
2076 my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } );
2077 foreach my $cust_pkg (@cust_pkgs) {
2079 #NO!! next if $cust_pkg->cancel;
2080 next if $cust_pkg->getfield('cancel');
2082 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
2084 #? to avoid use of uninitialized value errors... ?
2085 $cust_pkg->setfield('bill', '')
2086 unless defined($cust_pkg->bill);
2088 #my $part_pkg = $cust_pkg->part_pkg;
2090 my $real_pkgpart = $cust_pkg->pkgpart;
2091 my %hash = $cust_pkg->hash;
2092 my $old_cust_pkg = new FS::cust_pkg \%hash;
2094 foreach my $part_pkg ( $cust_pkg->part_pkg->self_and_bill_linked ) {
2096 $cust_pkg->pkgpart($part_pkg->pkgpart);
2097 $cust_pkg->set($_, $hash{$_}) foreach qw( setup last_bill bill );
2109 if ( ! $cust_pkg->setup &&
2111 ( $conf->exists('disable_setup_suspended_pkgs') &&
2112 ! $cust_pkg->getfield('susp')
2113 ) || ! $conf->exists('disable_setup_suspended_pkgs')
2115 || $options{'resetup'}
2118 warn " bill setup\n" if $DEBUG > 1;
2121 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
2123 $dbh->rollback if $oldAutoCommit;
2124 return "$@ running calc_setup for $cust_pkg\n";
2127 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
2129 $cust_pkg->setfield('setup', $time)
2130 unless $cust_pkg->setup;
2131 #do need it, but it won't get written to the db
2132 #|| $cust_pkg->pkgpart != $real_pkgpart;
2137 # bill recurring fee
2140 #XXX unit stuff here too
2144 if ( $part_pkg->getfield('freq') ne '0' &&
2145 ! $cust_pkg->getfield('susp') &&
2146 ( $cust_pkg->getfield('bill') || 0 ) <= $time
2149 # XXX should this be a package event? probably. events are called
2150 # at collection time at the moment, though...
2151 $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
2152 if $part_pkg->can('reset_usage');
2153 #don't want to reset usage just cause we want a line item??
2154 #&& $part_pkg->pkgpart == $real_pkgpart;
2156 warn " bill recur\n" if $DEBUG > 1;
2159 # XXX shared with $recur_prog
2160 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2162 #over two params! lets at least switch to a hashref for the rest...
2163 my %param = ( 'precommit_hooks' => \@precommit_hooks, );
2165 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
2167 $dbh->rollback if $oldAutoCommit;
2168 return "$@ running calc_recur for $cust_pkg\n";
2172 #change this bit to use Date::Manip? CAREFUL with timezones (see
2173 # mailing list archive)
2174 my ($sec,$min,$hour,$mday,$mon,$year) =
2175 (localtime($sdate) )[0,1,2,3,4,5];
2177 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
2178 # only for figuring next bill date, nothing else, so, reset $sdate again
2180 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2181 $cust_pkg->last_bill($sdate);
2183 if ( $part_pkg->freq =~ /^\d+$/ ) {
2184 $mon += $part_pkg->freq;
2185 until ( $mon < 12 ) { $mon -= 12; $year++; }
2186 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
2188 $mday += $weeks * 7;
2189 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
2192 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
2196 $dbh->rollback if $oldAutoCommit;
2197 return "unparsable frequency: ". $part_pkg->freq;
2199 $cust_pkg->setfield('bill',
2200 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
2204 warn "\$setup is undefined" unless defined($setup);
2205 warn "\$recur is undefined" unless defined($recur);
2206 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2209 # If there's line items, create em cust_bill_pkg records
2210 # If $cust_pkg has been modified, update it (if we're a real pkgpart)
2215 if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
2216 # hmm.. and if just the options are modified in some weird price plan?
2218 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2221 my $error = $cust_pkg->replace( $old_cust_pkg,
2222 'options' => { $cust_pkg->options },
2224 if ( $error ) { #just in case
2225 $dbh->rollback if $oldAutoCommit;
2226 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
2230 $setup = sprintf( "%.2f", $setup );
2231 $recur = sprintf( "%.2f", $recur );
2232 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2233 $dbh->rollback if $oldAutoCommit;
2234 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2236 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2237 $dbh->rollback if $oldAutoCommit;
2238 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2241 if ( $setup != 0 || $recur != 0 ) {
2243 unless ($postal_charge) {
2244 $postal_charge = 1; # try only once
2245 my $postal_pkg = $self->charge_postal_fee();
2246 if ( $postal_pkg && !ref( $postal_pkg ) ) {
2247 $dbh->rollback if $oldAutoCommit;
2248 return "can't charge postal invoice fee for customer ".
2249 $self->custnum. ": $postal_pkg";
2251 push @cust_pkgs, $postal_pkg if $postal_pkg;
2254 warn " charges (setup=$setup, recur=$recur); adding line items\n"
2256 my $cust_bill_pkg = new FS::cust_bill_pkg {
2257 'pkgnum' => $cust_pkg->pkgnum,
2259 'unitsetup' => $unitsetup,
2261 'unitrecur' => $unitrecur,
2262 'quantity' => $cust_pkg->quantity,
2264 'edate' => $cust_pkg->bill,
2265 'details' => \@details,
2267 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
2268 unless $part_pkg->pkgpart == $real_pkgpart;
2269 push @cust_bill_pkg, $cust_bill_pkg;
2271 $total_setup += $setup;
2272 $total_recur += $recur;
2278 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2281 my @taxoverrides = $part_pkg->part_pkg_taxoverride;
2284 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
2288 if ( $conf->exists('enable_taxproducts')
2289 && (scalar(@taxoverrides) || $part_pkg->taxproductnum )
2293 my @taxclassnums = ();
2294 my $geocode = $self->geocode('cch');
2296 if ( scalar( @taxoverrides ) ) {
2297 @taxclassnums = map { $_->taxclassnum } @taxoverrides;
2298 }elsif ( $part_pkg->taxproductnum ) {
2299 @taxclassnums = map { $_->taxclassnum }
2300 $part_pkg->part_pkg_taxrate('cch', $geocode);
2305 join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
2307 @taxes = qsearch({ 'table' => 'tax_rate',
2308 'hashref' => { 'geocode' => $geocode, },
2309 'extra_sql' => $extra_sql,
2311 if scalar(@taxclassnums);
2316 my %taxhash = map { $_ => $self->get("$prefix$_") }
2317 qw( state county country );
2319 $taxhash{'taxclass'} = $part_pkg->taxclass;
2321 @taxes = qsearch( 'cust_main_county', \%taxhash );
2324 $taxhash{'taxclass'} = '';
2325 @taxes = qsearch( 'cust_main_county', \%taxhash );
2328 #one more try at a whole-country tax rate
2330 $taxhash{$_} = '' foreach qw( state county );
2331 @taxes = qsearch( 'cust_main_county', \%taxhash );
2334 } #if $conf->exists('enable_taxproducts')
2336 # maybe eliminate this entirely, along with all the 0% records
2338 $dbh->rollback if $oldAutoCommit;
2340 if ( $conf->exists('enable_taxproducts') ) {
2342 "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
2343 join('/', ( map $self->get("$prefix$_"),
2346 $part_pkg->taxproduct_description,
2347 $part_pkg->pkgpart ). "\n";
2350 "fatal: can't find tax rate for state/county/country/taxclass ".
2351 join('/', ( map $self->get("$prefix$_"),
2352 qw(state county country)
2354 $part_pkg->taxclass ). "\n";
2359 foreach my $tax ( @taxes ) {
2360 my $taxname = ref( $tax ). ' '. $tax->taxnum;
2361 if ( exists( $taxlisthash{ $taxname } ) ) {
2362 push @{ $taxlisthash{ $taxname } }, $cust_bill_pkg;
2364 $taxlisthash{ $taxname } = [ $tax, $cust_bill_pkg ];
2369 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2371 } #if $setup != 0 || $recur != 0
2373 } #if $cust_pkg->modified
2375 } #foreach my $part_pkg
2377 } #foreach my $cust_pkg
2379 unless ( @cust_bill_pkg ) { #don't create an invoice w/o line items
2380 #but do commit any package date cycling that happened
2381 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2385 warn "having a look at the taxes we found...\n" if $DEBUG > 2;
2386 foreach my $tax ( keys %taxlisthash ) {
2387 my $tax_object = shift @{ $taxlisthash{$tax} };
2388 warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
2389 my $listref_or_error = $tax_object->taxline( @{ $taxlisthash{$tax} } );
2390 unless (ref($listref_or_error)) {
2391 $dbh->rollback if $oldAutoCommit;
2392 return $listref_or_error;
2394 unshift @{ $taxlisthash{$tax} }, $tax_object;
2396 warn "adding ". $listref_or_error->[1].
2397 " as ". $listref_or_error->[0]. "\n"
2399 $tax{ $tax_object->taxname } += $listref_or_error->[1];
2400 if ( $taxname{ $listref_or_error->[0] } ) {
2401 push @{ $taxname{ $listref_or_error->[0] } }, $tax_object->taxname;
2403 $taxname{ $listref_or_error->[0] } = [ $tax_object->taxname ];
2408 #some taxes are taxed
2411 warn "finding taxed taxes...\n" if $DEBUG > 2;
2412 foreach my $tax ( keys %taxlisthash ) {
2413 my $tax_object = shift @{ $taxlisthash{$tax} };
2414 warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
2416 next unless $tax_object->can('tax_on_tax');
2418 foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
2419 my $totname = ref( $tot ). ' '. $tot->taxnum;
2421 warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
2423 next unless exists( $taxlisthash{ $totname } ); # only increase
2425 warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
2426 if ( exists( $totlisthash{ $totname } ) ) {
2427 push @{ $totlisthash{ $totname } }, $tax{ $tax_object->taxname };
2429 $totlisthash{ $totname } = [ $tot, $tax{ $tax_object->taxname } ];
2434 warn "having a look at taxed taxes...\n" if $DEBUG > 2;
2435 foreach my $tax ( keys %totlisthash ) {
2436 my $tax_object = shift @{ $totlisthash{$tax} };
2437 warn "found previously found taxed tax ". $tax_object->taxname. "\n"
2439 my $listref_or_error = $tax_object->taxline( @{ $totlisthash{$tax} } );
2440 unless (ref($listref_or_error)) {
2441 $dbh->rollback if $oldAutoCommit;
2442 return $listref_or_error;
2445 warn "adding taxed tax amount ". $listref_or_error->[1].
2446 " as ". $tax_object->taxname. "\n"
2448 $tax{ $tax_object->taxname } += $listref_or_error->[1];
2451 #consolidate and create tax line items
2452 warn "consolidating and generating...\n" if $DEBUG > 2;
2453 foreach my $taxname ( keys %taxname ) {
2456 warn "adding $taxname\n" if $DEBUG > 1;
2457 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
2458 $tax += $tax{$taxitem} unless $seen{$taxitem};
2459 warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
2463 $tax = sprintf('%.2f', $tax );
2464 $total_setup = sprintf('%.2f', $total_setup+$tax );
2466 push @cust_bill_pkg, new FS::cust_bill_pkg {
2472 'itemdesc' => $taxname,
2477 my $charged = sprintf('%.2f', $total_setup + $total_recur );
2479 #create the new invoice
2480 my $cust_bill = new FS::cust_bill ( {
2481 'custnum' => $self->custnum,
2482 '_date' => ( $options{'invoice_time'} || $time ),
2483 'charged' => $charged,
2485 my $error = $cust_bill->insert;
2487 $dbh->rollback if $oldAutoCommit;
2488 return "can't create invoice for customer #". $self->custnum. ": $error";
2491 foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
2492 $cust_bill_pkg->invnum($cust_bill->invnum);
2493 my $error = $cust_bill_pkg->insert;
2495 $dbh->rollback if $oldAutoCommit;
2496 return "can't create invoice line item: $error";
2501 foreach my $hook ( @precommit_hooks ) {
2503 &{$hook}; #($self) ?
2506 $dbh->rollback if $oldAutoCommit;
2507 return "$@ running precommit hook $hook\n";
2511 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2515 =item collect OPTIONS
2517 (Attempt to) collect money for this customer's outstanding invoices (see
2518 L<FS::cust_bill>). Usually used after the bill method.
2520 Actions are now triggered by billing events; see L<FS::part_event> and the
2521 billing events web interface. Old-style invoice events (see
2522 L<FS::part_bill_event>) have been deprecated.
2524 If there is an error, returns the error, otherwise returns false.
2526 Options are passed as name-value pairs.
2528 Currently available options are:
2534 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.
2538 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
2542 set true to surpress email card/ACH decline notices.
2546 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
2550 allows for one time override of normal customer billing method
2554 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)
2562 my( $self, %options ) = @_;
2563 my $invoice_time = $options{'invoice_time'} || time;
2566 local $SIG{HUP} = 'IGNORE';
2567 local $SIG{INT} = 'IGNORE';
2568 local $SIG{QUIT} = 'IGNORE';
2569 local $SIG{TERM} = 'IGNORE';
2570 local $SIG{TSTP} = 'IGNORE';
2571 local $SIG{PIPE} = 'IGNORE';
2573 my $oldAutoCommit = $FS::UID::AutoCommit;
2574 local $FS::UID::AutoCommit = 0;
2577 $self->select_for_update; #mutex
2580 my $balance = $self->balance;
2581 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2584 if ( exists($options{'retry_card'}) ) {
2585 carp 'retry_card option passed to collect is deprecated; use retry';
2586 $options{'retry'} ||= $options{'retry_card'};
2588 if ( exists($options{'retry'}) && $options{'retry'} ) {
2589 my $error = $self->retry_realtime;
2591 $dbh->rollback if $oldAutoCommit;
2596 # false laziness w/pay_batch::import_results
2598 my $due_cust_event = $self->due_cust_event(
2599 'debug' => ( $options{'debug'} || 0 ),
2600 'time' => $invoice_time,
2601 'check_freq' => $options{'check_freq'},
2603 unless( ref($due_cust_event) ) {
2604 $dbh->rollback if $oldAutoCommit;
2605 return $due_cust_event;
2608 foreach my $cust_event ( @$due_cust_event ) {
2612 #re-eval event conditions (a previous event could have changed things)
2613 unless ( $cust_event->test_conditions( 'time' => $invoice_time ) ) {
2614 #don't leave stray "new/locked" records around
2615 my $error = $cust_event->delete;
2617 #gah, even with transactions
2618 $dbh->commit if $oldAutoCommit; #well.
2625 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2626 warn " running cust_event ". $cust_event->eventnum. "\n"
2630 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
2631 if ( my $error = $cust_event->do_event() ) {
2632 #XXX wtf is this? figure out a proper dealio with return value
2634 # gah, even with transactions.
2635 $dbh->commit if $oldAutoCommit; #well.
2642 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2647 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
2649 Inserts database records for and returns an ordered listref of new events due
2650 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
2651 events are due, an empty listref is returned. If there is an error, returns a
2652 scalar error message.
2654 To actually run the events, call each event's test_condition method, and if
2655 still true, call the event's do_event method.
2657 Options are passed as a hashref or as a list of name-value pairs. Available
2664 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.
2668 "Current time" for the events.
2672 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)
2676 Only return events for the specified eventtable (by default, events of all eventtables are returned)
2680 Explicitly pass the objects to be tested (typically used with eventtable).
2686 sub due_cust_event {
2688 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
2691 #my $DEBUG = $opt{'debug'}
2692 local($DEBUG) = $opt{'debug'}
2693 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
2695 warn "$me due_cust_event called with options ".
2696 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
2699 $opt{'time'} ||= time;
2701 local $SIG{HUP} = 'IGNORE';
2702 local $SIG{INT} = 'IGNORE';
2703 local $SIG{QUIT} = 'IGNORE';
2704 local $SIG{TERM} = 'IGNORE';
2705 local $SIG{TSTP} = 'IGNORE';
2706 local $SIG{PIPE} = 'IGNORE';
2708 my $oldAutoCommit = $FS::UID::AutoCommit;
2709 local $FS::UID::AutoCommit = 0;
2712 $self->select_for_update; #mutex
2715 # 1: find possible events (initial search)
2718 my @cust_event = ();
2720 my @eventtable = $opt{'eventtable'}
2721 ? ( $opt{'eventtable'} )
2722 : FS::part_event->eventtables_runorder;
2724 foreach my $eventtable ( @eventtable ) {
2727 if ( $opt{'objects'} ) {
2729 @objects = @{ $opt{'objects'} };
2733 #my @objects = $self->eventtable(); # sub cust_main { @{ [ $self ] }; }
2734 @objects = ( $eventtable eq 'cust_main' )
2736 : ( $self->$eventtable() );
2740 my @e_cust_event = ();
2742 my $cross = "CROSS JOIN $eventtable";
2743 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
2744 unless $eventtable eq 'cust_main';
2746 foreach my $object ( @objects ) {
2748 #this first search uses the condition_sql magic for optimization.
2749 #the more possible events we can eliminate in this step the better
2751 my $cross_where = '';
2752 my $pkey = $object->primary_key;
2753 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
2755 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
2757 FS::part_event_condition->where_conditions_sql( $eventtable,
2758 'time'=>$opt{'time'}
2760 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
2762 $extra_sql = "AND $extra_sql" if $extra_sql;
2764 #here is the agent virtualization
2765 $extra_sql .= " AND ( part_event.agentnum IS NULL
2766 OR part_event.agentnum = ". $self->agentnum. ' )';
2768 $extra_sql .= " $order";
2770 warn "searching for events for $eventtable ". $object->$pkey. "\n"
2771 if $opt{'debug'} > 2;
2772 my @part_event = qsearch( {
2773 'debug' => ( $opt{'debug'} > 3 ? 1 : 0 ),
2774 'select' => 'part_event.*',
2775 'table' => 'part_event',
2776 'addl_from' => "$cross $join",
2777 'hashref' => { 'check_freq' => ( $opt{'check_freq'} || '1d' ),
2778 'eventtable' => $eventtable,
2781 'extra_sql' => "AND $cross_where $extra_sql",
2785 my $pkey = $object->primary_key;
2786 warn " ". scalar(@part_event).
2787 " possible events found for $eventtable ". $object->$pkey(). "\n";
2790 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
2794 warn " ". scalar(@e_cust_event).
2795 " subtotal possible cust events found for $eventtable\n"
2798 push @cust_event, @e_cust_event;
2802 warn " ". scalar(@cust_event).
2803 " total possible cust events found in initial search\n"
2807 # 2: test conditions
2812 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
2813 'stats_hashref' => \%unsat ),
2816 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
2819 warn " invalid conditions not eliminated with condition_sql:\n".
2820 join('', map " $_: ".$unsat{$_}."\n", keys %unsat )
2827 foreach my $cust_event ( @cust_event ) {
2829 my $error = $cust_event->insert();
2831 $dbh->rollback if $oldAutoCommit;
2837 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2843 warn " returning events: ". Dumper(@cust_event). "\n"
2850 =item retry_realtime
2852 Schedules realtime / batch credit card / electronic check / LEC billing
2853 events for for retry. Useful if card information has changed or manual
2854 retry is desired. The 'collect' method must be called to actually retry
2857 Implementation details: For either this customer, or for each of this
2858 customer's open invoices, changes the status of the first "done" (with
2859 statustext error) realtime processing event to "failed".
2863 sub retry_realtime {
2866 local $SIG{HUP} = 'IGNORE';
2867 local $SIG{INT} = 'IGNORE';
2868 local $SIG{QUIT} = 'IGNORE';
2869 local $SIG{TERM} = 'IGNORE';
2870 local $SIG{TSTP} = 'IGNORE';
2871 local $SIG{PIPE} = 'IGNORE';
2873 my $oldAutoCommit = $FS::UID::AutoCommit;
2874 local $FS::UID::AutoCommit = 0;
2877 #a little false laziness w/due_cust_event (not too bad, really)
2879 my $join = FS::part_event_condition->join_conditions_sql;
2880 my $order = FS::part_event_condition->order_conditions_sql;
2883 . join ( ' OR ' , map {
2884 "( part_event.eventtable = " . dbh->quote($_)
2885 . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
2886 } FS::part_event->eventtables)
2889 #here is the agent virtualization
2890 my $agent_virt = " ( part_event.agentnum IS NULL
2891 OR part_event.agentnum = ". $self->agentnum. ' )';
2893 #XXX this shouldn't be hardcoded, actions should declare it...
2894 my @realtime_events = qw(
2895 cust_bill_realtime_card
2896 cust_bill_realtime_check
2897 cust_bill_realtime_lec
2901 my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
2906 my @cust_event = qsearchs({
2907 'table' => 'cust_event',
2908 'select' => 'cust_event.*',
2909 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
2910 'hashref' => { 'status' => 'done' },
2911 'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
2912 " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
2915 my %seen_invnum = ();
2916 foreach my $cust_event (@cust_event) {
2918 #max one for the customer, one for each open invoice
2919 my $cust_X = $cust_event->cust_X;
2920 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
2924 or $cust_event->part_event->eventtable eq 'cust_bill'
2927 my $error = $cust_event->retry;
2929 $dbh->rollback if $oldAutoCommit;
2930 return "error scheduling event for retry: $error";
2935 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2940 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2942 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2943 via a Business::OnlinePayment realtime gateway. See
2944 L<http://420.am/business-onlinepayment> for supported gateways.
2946 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2948 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
2950 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2951 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2952 if set, will override the value from the customer record.
2954 I<description> is a free-text field passed to the gateway. It defaults to
2955 "Internet services".
2957 If an I<invnum> is specified, this payment (if successful) is applied to the
2958 specified invoice. If you don't specify an I<invnum> you might want to
2959 call the B<apply_payments> method.
2961 I<quiet> can be set true to surpress email decline notices.
2963 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
2964 resulting paynum, if any.
2966 I<payunique> is a unique identifier for this payment.
2968 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2973 my( $self, $method, $amount, %options ) = @_;
2975 warn "$me realtime_bop: $method $amount\n";
2976 warn " $_ => $options{$_}\n" foreach keys %options;
2979 $options{'description'} ||= 'Internet services';
2981 return $self->fake_bop($method, $amount, %options) if $options{'fake'};
2983 eval "use Business::OnlinePayment";
2986 my $payinfo = exists($options{'payinfo'})
2987 ? $options{'payinfo'}
2990 my %method2payby = (
2997 # check for banned credit card/ACH
3000 my $ban = qsearchs('banned_pay', {
3001 'payby' => $method2payby{$method},
3002 'payinfo' => md5_base64($payinfo),
3004 return "Banned credit card" if $ban;
3011 if ( $options{'invnum'} ) {
3012 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
3013 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
3015 map { $_->part_pkg->taxclass }
3017 map { $_->cust_pkg }
3018 $cust_bill->cust_bill_pkg;
3019 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
3020 #different taxclasses
3021 $taxclass = $taxclasses[0];
3025 #look for an agent gateway override first
3027 if ( $method eq 'CC' ) {
3028 $cardtype = cardtype($payinfo);
3029 } elsif ( $method eq 'ECHECK' ) {
3032 $cardtype = $method;
3036 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3037 cardtype => $cardtype,
3038 taxclass => $taxclass, } )
3039 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3041 taxclass => $taxclass, } )
3042 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3043 cardtype => $cardtype,
3045 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3047 taxclass => '', } );
3049 my $payment_gateway = '';
3050 my( $processor, $login, $password, $action, @bop_options );
3051 if ( $override ) { #use a payment gateway override
3053 $payment_gateway = $override->payment_gateway;
3055 $processor = $payment_gateway->gateway_module;
3056 $login = $payment_gateway->gateway_username;
3057 $password = $payment_gateway->gateway_password;
3058 $action = $payment_gateway->gateway_action;
3059 @bop_options = $payment_gateway->options;
3061 } else { #use the standard settings from the config
3063 ( $processor, $login, $password, $action, @bop_options ) =
3064 $self->default_payment_gateway($method);
3072 my $address = exists($options{'address1'})
3073 ? $options{'address1'}
3075 my $address2 = exists($options{'address2'})
3076 ? $options{'address2'}
3078 $address .= ", ". $address2 if length($address2);
3080 my $o_payname = exists($options{'payname'})
3081 ? $options{'payname'}
3083 my($payname, $payfirst, $paylast);
3084 if ( $o_payname && $method ne 'ECHECK' ) {
3085 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3086 or return "Illegal payname $payname";
3087 ($payfirst, $paylast) = ($1, $2);
3089 $payfirst = $self->getfield('first');
3090 $paylast = $self->getfield('last');
3091 $payname = "$payfirst $paylast";
3094 my @invoicing_list = $self->invoicing_list_emailonly;
3095 if ( $conf->exists('emailinvoiceautoalways')
3096 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3097 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3098 push @invoicing_list, $self->all_emails;
3101 my $email = ($conf->exists('business-onlinepayment-email-override'))
3102 ? $conf->config('business-onlinepayment-email-override')
3103 : $invoicing_list[0];
3107 my $payip = exists($options{'payip'})
3110 $content{customer_ip} = $payip
3113 $content{invoice_number} = $options{'invnum'}
3114 if exists($options{'invnum'}) && length($options{'invnum'});
3116 $content{email_customer} =
3117 ( $conf->exists('business-onlinepayment-email_customer')
3118 || $conf->exists('business-onlinepayment-email-override') );
3121 if ( $method eq 'CC' ) {
3123 $content{card_number} = $payinfo;
3124 $paydate = exists($options{'paydate'})
3125 ? $options{'paydate'}
3127 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3128 $content{expiration} = "$2/$1";
3130 my $paycvv = exists($options{'paycvv'})
3131 ? $options{'paycvv'}
3133 $content{cvv2} = $paycvv
3136 my $paystart_month = exists($options{'paystart_month'})
3137 ? $options{'paystart_month'}
3138 : $self->paystart_month;
3140 my $paystart_year = exists($options{'paystart_year'})
3141 ? $options{'paystart_year'}
3142 : $self->paystart_year;
3144 $content{card_start} = "$paystart_month/$paystart_year"
3145 if $paystart_month && $paystart_year;
3147 my $payissue = exists($options{'payissue'})
3148 ? $options{'payissue'}
3150 $content{issue_number} = $payissue if $payissue;
3152 $content{recurring_billing} = 'YES'
3153 if qsearch('cust_pay', { 'custnum' => $self->custnum,
3155 'payinfo' => $payinfo,
3157 || qsearch('cust_pay', { 'custnum' => $self->custnum,
3159 'paymask' => $self->mask_payinfo('CARD', $payinfo),
3163 } elsif ( $method eq 'ECHECK' ) {
3164 ( $content{account_number}, $content{routing_code} ) =
3165 split('@', $payinfo);
3166 $content{bank_name} = $o_payname;
3167 $content{bank_state} = exists($options{'paystate'})
3168 ? $options{'paystate'}
3169 : $self->getfield('paystate');
3170 $content{account_type} = exists($options{'paytype'})
3171 ? uc($options{'paytype'}) || 'CHECKING'
3172 : uc($self->getfield('paytype')) || 'CHECKING';
3173 $content{account_name} = $payname;
3174 $content{customer_org} = $self->company ? 'B' : 'I';
3175 $content{state_id} = exists($options{'stateid'})
3176 ? $options{'stateid'}
3177 : $self->getfield('stateid');
3178 $content{state_id_state} = exists($options{'stateid_state'})
3179 ? $options{'stateid_state'}
3180 : $self->getfield('stateid_state');
3181 $content{customer_ssn} = exists($options{'ss'})
3184 } elsif ( $method eq 'LEC' ) {
3185 $content{phone} = $payinfo;
3189 # run transaction(s)
3192 my $balance = exists( $options{'balance'} )
3193 ? $options{'balance'}
3196 $self->select_for_update; #mutex ... just until we get our pending record in
3198 #the checks here are intended to catch concurrent payments
3199 #double-form-submission prevention is taken care of in cust_pay_pending::check
3202 return "The customer's balance has changed; $method transaction aborted."
3203 if $self->balance < $balance;
3204 #&& $self->balance < $amount; #might as well anyway?
3206 #also check and make sure there aren't *other* pending payments for this cust
3208 my @pending = qsearch('cust_pay_pending', {
3209 'custnum' => $self->custnum,
3210 'status' => { op=>'!=', value=>'done' }
3212 return "A payment is already being processed for this customer (".
3213 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
3214 "); $method transaction aborted."
3215 if scalar(@pending);
3217 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
3219 my $cust_pay_pending = new FS::cust_pay_pending {
3220 'custnum' => $self->custnum,
3221 #'invnum' => $options{'invnum'},
3224 'payby' => $method2payby{$method},
3225 'payinfo' => $payinfo,
3226 'paydate' => $paydate,
3228 'gatewaynum' => ( $payment_gateway ? $payment_gateway->gatewaynum : '' ),
3230 $cust_pay_pending->payunique( $options{payunique} )
3231 if defined($options{payunique}) && length($options{payunique});
3232 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
3233 return $cpp_new_err if $cpp_new_err;
3235 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
3237 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
3238 $transaction->content(
3241 'password' => $password,
3242 'action' => $action1,
3243 'description' => $options{'description'},
3244 'amount' => $amount,
3245 #'invoice_number' => $options{'invnum'},
3246 'customer_id' => $self->custnum,
3247 'last_name' => $paylast,
3248 'first_name' => $payfirst,
3250 'address' => $address,
3251 'city' => ( exists($options{'city'})
3254 'state' => ( exists($options{'state'})
3257 'zip' => ( exists($options{'zip'})
3260 'country' => ( exists($options{'country'})
3261 ? $options{'country'}
3263 'referer' => 'http://cleanwhisker.420.am/',
3265 'phone' => $self->daytime || $self->night,
3269 $cust_pay_pending->status('pending');
3270 my $cpp_pending_err = $cust_pay_pending->replace;
3271 return $cpp_pending_err if $cpp_pending_err;
3274 my $BOP_TESTING = 0;
3275 my $BOP_TESTING_SUCCESS = 1;
3277 unless ( $BOP_TESTING ) {
3278 $transaction->submit();
3280 if ( $BOP_TESTING_SUCCESS ) {
3281 $transaction->is_success(1);
3282 $transaction->authorization('fake auth');
3284 $transaction->is_success(0);
3285 $transaction->error_message('fake failure');
3289 if ( $transaction->is_success() && $action2 ) {
3291 $cust_pay_pending->status('authorized');
3292 my $cpp_authorized_err = $cust_pay_pending->replace;
3293 return $cpp_authorized_err if $cpp_authorized_err;
3295 my $auth = $transaction->authorization;
3296 my $ordernum = $transaction->can('order_number')
3297 ? $transaction->order_number
3301 new Business::OnlinePayment( $processor, @bop_options );
3308 password => $password,
3309 order_number => $ordernum,
3311 authorization => $auth,
3312 description => $options{'description'},
3315 foreach my $field (qw( authorization_source_code returned_ACI
3316 transaction_identifier validation_code
3317 transaction_sequence_num local_transaction_date
3318 local_transaction_time AVS_result_code )) {
3319 $capture{$field} = $transaction->$field() if $transaction->can($field);
3322 $capture->content( %capture );
3326 unless ( $capture->is_success ) {
3327 my $e = "Authorization successful but capture failed, custnum #".
3328 $self->custnum. ': '. $capture->result_code.
3329 ": ". $capture->error_message;
3336 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
3337 my $cpp_captured_err = $cust_pay_pending->replace;
3338 return $cpp_captured_err if $cpp_captured_err;
3341 # remove paycvv after initial transaction
3344 #false laziness w/misc/process/payment.cgi - check both to make sure working
3346 if ( defined $self->dbdef_table->column('paycvv')
3347 && length($self->paycvv)
3348 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
3350 my $error = $self->remove_cvv;
3352 warn "WARNING: error removing cvv: $error\n";
3360 if ( $transaction->is_success() ) {
3363 if ( $payment_gateway ) { # agent override
3364 $paybatch = $payment_gateway->gatewaynum. '-';
3367 $paybatch .= "$processor:". $transaction->authorization;
3369 $paybatch .= ':'. $transaction->order_number
3370 if $transaction->can('order_number')
3371 && length($transaction->order_number);
3373 my $cust_pay = new FS::cust_pay ( {
3374 'custnum' => $self->custnum,
3375 'invnum' => $options{'invnum'},
3378 'payby' => $method2payby{$method},
3379 'payinfo' => $payinfo,
3380 'paybatch' => $paybatch,
3381 'paydate' => $paydate,
3383 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
3384 $cust_pay->payunique( $options{payunique} )
3385 if defined($options{payunique}) && length($options{payunique});
3387 my $oldAutoCommit = $FS::UID::AutoCommit;
3388 local $FS::UID::AutoCommit = 0;
3391 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
3393 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
3396 $cust_pay->invnum(''); #try again with no specific invnum
3397 my $error2 = $cust_pay->insert( $options{'manual'} ?
3398 ( 'manual' => 1 ) : ()
3401 # gah. but at least we have a record of the state we had to abort in
3402 # from cust_pay_pending now.
3403 my $e = "WARNING: $method captured but payment not recorded - ".
3404 "error inserting payment ($processor): $error2".
3405 " (previously tried insert with invnum #$options{'invnum'}" .
3406 ": $error ) - pending payment saved as paypendingnum ".
3407 $cust_pay_pending->paypendingnum. "\n";
3413 if ( $options{'paynum_ref'} ) {
3414 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
3417 $cust_pay_pending->status('done');
3418 $cust_pay_pending->statustext('captured');
3419 my $cpp_done_err = $cust_pay_pending->replace;
3421 if ( $cpp_done_err ) {
3423 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3424 my $e = "WARNING: $method captured but payment not recorded - ".
3425 "error updating status for paypendingnum ".
3426 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3432 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3433 return ''; #no error
3439 my $perror = "$processor error: ". $transaction->error_message;
3441 unless ( $transaction->error_message ) {
3444 if ( $transaction->can('response_page') ) {
3446 'page' => ( $transaction->can('response_page')
3447 ? $transaction->response_page
3450 'code' => ( $transaction->can('response_code')
3451 ? $transaction->response_code
3454 'headers' => ( $transaction->can('response_headers')
3455 ? $transaction->response_headers
3461 "No additional debugging information available for $processor";
3464 $perror .= "No error_message returned from $processor -- ".
3465 ( ref($t_response) ? Dumper($t_response) : $t_response );
3469 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
3470 && $conf->exists('emaildecline')
3471 && grep { $_ ne 'POST' } $self->invoicing_list
3472 && ! grep { $transaction->error_message =~ /$_/ }
3473 $conf->config('emaildecline-exclude')
3475 my @templ = $conf->config('declinetemplate');
3476 my $template = new Text::Template (
3478 SOURCE => [ map "$_\n", @templ ],
3479 ) or return "($perror) can't create template: $Text::Template::ERROR";
3480 $template->compile()
3481 or return "($perror) can't compile template: $Text::Template::ERROR";
3483 my $templ_hash = { error => $transaction->error_message };
3485 my $error = send_email(
3486 'from' => $conf->config('invoice_from'),
3487 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
3488 'subject' => 'Your payment could not be processed',
3489 'body' => [ $template->fill_in(HASH => $templ_hash) ],
3492 $perror .= " (also received error sending decline notification: $error)"
3497 $cust_pay_pending->status('done');
3498 $cust_pay_pending->statustext("declined: $perror");
3499 my $cpp_done_err = $cust_pay_pending->replace;
3500 if ( $cpp_done_err ) {
3501 my $e = "WARNING: $method declined but pending payment not resolved - ".
3502 "error updating status for paypendingnum ".
3503 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3505 $perror = "$e ($perror)";
3518 my( $self, $method, $amount, %options ) = @_;
3520 if ( $options{'fake_failure'} ) {
3521 return "Error: No error; test failure requested with fake_failure";
3524 my %method2payby = (
3531 #if ( $payment_gateway ) { # agent override
3532 # $paybatch = $payment_gateway->gatewaynum. '-';
3535 #$paybatch .= "$processor:". $transaction->authorization;
3537 #$paybatch .= ':'. $transaction->order_number
3538 # if $transaction->can('order_number')
3539 # && length($transaction->order_number);
3541 my $paybatch = 'FakeProcessor:54:32';
3543 my $cust_pay = new FS::cust_pay ( {
3544 'custnum' => $self->custnum,
3545 'invnum' => $options{'invnum'},
3548 'payby' => $method2payby{$method},
3549 #'payinfo' => $payinfo,
3550 'payinfo' => '4111111111111111',
3551 'paybatch' => $paybatch,
3552 #'paydate' => $paydate,
3553 'paydate' => '2012-05-01',
3555 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
3557 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
3560 $cust_pay->invnum(''); #try again with no specific invnum
3561 my $error2 = $cust_pay->insert( $options{'manual'} ?
3562 ( 'manual' => 1 ) : ()
3565 # gah, even with transactions.
3566 my $e = 'WARNING: Card/ACH debited but database not updated - '.
3567 "error inserting (fake!) payment: $error2".
3568 " (previously tried insert with invnum #$options{'invnum'}" .
3575 if ( $options{'paynum_ref'} ) {
3576 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
3579 return ''; #no error
3583 =item default_payment_gateway
3587 sub default_payment_gateway {
3588 my( $self, $method ) = @_;
3590 die "Real-time processing not enabled\n"
3591 unless $conf->exists('business-onlinepayment');
3594 my $bop_config = 'business-onlinepayment';
3595 $bop_config .= '-ach'
3596 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
3597 my ( $processor, $login, $password, $action, @bop_options ) =
3598 $conf->config($bop_config);
3599 $action ||= 'normal authorization';
3600 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
3601 die "No real-time processor is enabled - ".
3602 "did you set the business-onlinepayment configuration value?\n"
3605 ( $processor, $login, $password, $action, @bop_options )
3610 Removes the I<paycvv> field from the database directly.
3612 If there is an error, returns the error, otherwise returns false.
3618 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
3619 or return dbh->errstr;
3620 $sth->execute($self->custnum)
3621 or return $sth->errstr;
3626 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
3628 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
3629 via a Business::OnlinePayment realtime gateway. See
3630 L<http://420.am/business-onlinepayment> for supported gateways.
3632 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3634 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
3636 Most gateways require a reference to an original payment transaction to refund,
3637 so you probably need to specify a I<paynum>.
3639 I<amount> defaults to the original amount of the payment if not specified.
3641 I<reason> specifies a reason for the refund.
3643 I<paydate> specifies the expiration date for a credit card overriding the
3644 value from the customer record or the payment record. Specified as yyyy-mm-dd
3646 Implementation note: If I<amount> is unspecified or equal to the amount of the
3647 orignal payment, first an attempt is made to "void" the transaction via
3648 the gateway (to cancel a not-yet settled transaction) and then if that fails,
3649 the normal attempt is made to "refund" ("credit") the transaction via the
3650 gateway is attempted.
3652 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3653 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3654 #if set, will override the value from the customer record.
3656 #If an I<invnum> is specified, this payment (if successful) is applied to the
3657 #specified invoice. If you don't specify an I<invnum> you might want to
3658 #call the B<apply_payments> method.
3662 #some false laziness w/realtime_bop, not enough to make it worth merging
3663 #but some useful small subs should be pulled out
3664 sub realtime_refund_bop {
3665 my( $self, $method, %options ) = @_;
3667 warn "$me realtime_refund_bop: $method refund\n";
3668 warn " $_ => $options{$_}\n" foreach keys %options;
3671 eval "use Business::OnlinePayment";
3675 # look up the original payment and optionally a gateway for that payment
3679 my $amount = $options{'amount'};
3681 my( $processor, $login, $password, @bop_options ) ;
3682 my( $auth, $order_number ) = ( '', '', '' );
3684 if ( $options{'paynum'} ) {
3686 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
3687 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
3688 or return "Unknown paynum $options{'paynum'}";
3689 $amount ||= $cust_pay->paid;
3691 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
3692 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
3693 $cust_pay->paybatch;
3694 my $gatewaynum = '';
3695 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
3697 if ( $gatewaynum ) { #gateway for the payment to be refunded
3699 my $payment_gateway =
3700 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
3701 die "payment gateway $gatewaynum not found"
3702 unless $payment_gateway;
3704 $processor = $payment_gateway->gateway_module;
3705 $login = $payment_gateway->gateway_username;
3706 $password = $payment_gateway->gateway_password;
3707 @bop_options = $payment_gateway->options;
3709 } else { #try the default gateway
3711 my( $conf_processor, $unused_action );
3712 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
3713 $self->default_payment_gateway($method);
3715 return "processor of payment $options{'paynum'} $processor does not".
3716 " match default processor $conf_processor"
3717 unless $processor eq $conf_processor;
3722 } else { # didn't specify a paynum, so look for agent gateway overrides
3723 # like a normal transaction
3726 if ( $method eq 'CC' ) {
3727 $cardtype = cardtype($self->payinfo);
3728 } elsif ( $method eq 'ECHECK' ) {
3731 $cardtype = $method;
3734 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3735 cardtype => $cardtype,
3737 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3739 taxclass => '', } );
3741 if ( $override ) { #use a payment gateway override
3743 my $payment_gateway = $override->payment_gateway;
3745 $processor = $payment_gateway->gateway_module;
3746 $login = $payment_gateway->gateway_username;
3747 $password = $payment_gateway->gateway_password;
3748 #$action = $payment_gateway->gateway_action;
3749 @bop_options = $payment_gateway->options;
3751 } else { #use the standard settings from the config
3754 ( $processor, $login, $password, $unused_action, @bop_options ) =
3755 $self->default_payment_gateway($method);
3760 return "neither amount nor paynum specified" unless $amount;
3765 'password' => $password,
3766 'order_number' => $order_number,
3767 'amount' => $amount,
3768 'referer' => 'http://cleanwhisker.420.am/',
3770 $content{authorization} = $auth
3771 if length($auth); #echeck/ACH transactions have an order # but no auth
3772 #(at least with authorize.net)
3774 my $disable_void_after;
3775 if ($conf->exists('disable_void_after')
3776 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
3777 $disable_void_after = $1;
3780 #first try void if applicable
3781 if ( $cust_pay && $cust_pay->paid == $amount
3783 ( not defined($disable_void_after) )
3784 || ( time < ($cust_pay->_date + $disable_void_after ) )
3787 warn " attempting void\n" if $DEBUG > 1;
3788 my $void = new Business::OnlinePayment( $processor, @bop_options );
3789 $void->content( 'action' => 'void', %content );
3791 if ( $void->is_success ) {
3792 my $error = $cust_pay->void($options{'reason'});
3794 # gah, even with transactions.
3795 my $e = 'WARNING: Card/ACH voided but database not updated - '.
3796 "error voiding payment: $error";
3800 warn " void successful\n" if $DEBUG > 1;
3805 warn " void unsuccessful, trying refund\n"
3809 my $address = $self->address1;
3810 $address .= ", ". $self->address2 if $self->address2;
3812 my($payname, $payfirst, $paylast);
3813 if ( $self->payname && $method ne 'ECHECK' ) {
3814 $payname = $self->payname;
3815 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3816 or return "Illegal payname $payname";
3817 ($payfirst, $paylast) = ($1, $2);
3819 $payfirst = $self->getfield('first');
3820 $paylast = $self->getfield('last');
3821 $payname = "$payfirst $paylast";
3824 my @invoicing_list = $self->invoicing_list_emailonly;
3825 if ( $conf->exists('emailinvoiceautoalways')
3826 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3827 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3828 push @invoicing_list, $self->all_emails;
3831 my $email = ($conf->exists('business-onlinepayment-email-override'))
3832 ? $conf->config('business-onlinepayment-email-override')
3833 : $invoicing_list[0];
3835 my $payip = exists($options{'payip'})
3838 $content{customer_ip} = $payip
3842 if ( $method eq 'CC' ) {
3845 $content{card_number} = $payinfo = $cust_pay->payinfo;
3846 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
3847 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
3848 ($content{expiration} = "$2/$1"); # where available
3850 $content{card_number} = $payinfo = $self->payinfo;
3851 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
3852 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3853 $content{expiration} = "$2/$1";
3856 } elsif ( $method eq 'ECHECK' ) {
3859 $payinfo = $cust_pay->payinfo;
3861 $payinfo = $self->payinfo;
3863 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
3864 $content{bank_name} = $self->payname;
3865 $content{account_type} = 'CHECKING';
3866 $content{account_name} = $payname;
3867 $content{customer_org} = $self->company ? 'B' : 'I';
3868 $content{customer_ssn} = $self->ss;
3869 } elsif ( $method eq 'LEC' ) {
3870 $content{phone} = $payinfo = $self->payinfo;
3874 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3875 my %sub_content = $refund->content(
3876 'action' => 'credit',
3877 'customer_id' => $self->custnum,
3878 'last_name' => $paylast,
3879 'first_name' => $payfirst,
3881 'address' => $address,
3882 'city' => $self->city,
3883 'state' => $self->state,
3884 'zip' => $self->zip,
3885 'country' => $self->country,
3887 'phone' => $self->daytime || $self->night,
3890 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3894 return "$processor error: ". $refund->error_message
3895 unless $refund->is_success();
3897 my %method2payby = (
3903 my $paybatch = "$processor:". $refund->authorization;
3904 $paybatch .= ':'. $refund->order_number
3905 if $refund->can('order_number') && $refund->order_number;
3907 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
3908 my @cust_bill_pay = $cust_pay->cust_bill_pay;
3909 last unless @cust_bill_pay;
3910 my $cust_bill_pay = pop @cust_bill_pay;
3911 my $error = $cust_bill_pay->delete;
3915 my $cust_refund = new FS::cust_refund ( {
3916 'custnum' => $self->custnum,
3917 'paynum' => $options{'paynum'},
3918 'refund' => $amount,
3920 'payby' => $method2payby{$method},
3921 'payinfo' => $payinfo,
3922 'paybatch' => $paybatch,
3923 'reason' => $options{'reason'} || 'card or ACH refund',
3925 my $error = $cust_refund->insert;
3927 $cust_refund->paynum(''); #try again with no specific paynum
3928 my $error2 = $cust_refund->insert;
3930 # gah, even with transactions.
3931 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
3932 "error inserting refund ($processor): $error2".
3933 " (previously tried insert with paynum #$options{'paynum'}" .
3944 =item batch_card OPTION => VALUE...
3946 Adds a payment for this invoice to the pending credit card batch (see
3947 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
3948 runs the payment using a realtime gateway.
3953 my ($self, %options) = @_;
3956 if (exists($options{amount})) {
3957 $amount = $options{amount};
3959 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
3961 return '' unless $amount > 0;
3963 my $invnum = delete $options{invnum};
3964 my $payby = $options{invnum} || $self->payby; #dubious
3966 if ($options{'realtime'}) {
3967 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
3973 my $oldAutoCommit = $FS::UID::AutoCommit;
3974 local $FS::UID::AutoCommit = 0;
3977 #this needs to handle mysql as well as Pg, like svc_acct.pm
3978 #(make it into a common function if folks need to do batching with mysql)
3979 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
3980 or return "Cannot lock pay_batch: " . $dbh->errstr;
3984 'payby' => FS::payby->payby2payment($payby),
3987 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
3989 unless ( $pay_batch ) {
3990 $pay_batch = new FS::pay_batch \%pay_batch;
3991 my $error = $pay_batch->insert;
3993 $dbh->rollback if $oldAutoCommit;
3994 die "error creating new batch: $error\n";
3998 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
3999 'batchnum' => $pay_batch->batchnum,
4000 'custnum' => $self->custnum,
4003 foreach (qw( address1 address2 city state zip country payby payinfo paydate
4005 $options{$_} = '' unless exists($options{$_});
4008 my $cust_pay_batch = new FS::cust_pay_batch ( {
4009 'batchnum' => $pay_batch->batchnum,
4010 'invnum' => $invnum || 0, # is there a better value?
4011 # this field should be
4013 # cust_bill_pay_batch now
4014 'custnum' => $self->custnum,
4015 'last' => $self->getfield('last'),
4016 'first' => $self->getfield('first'),
4017 'address1' => $options{address1} || $self->address1,
4018 'address2' => $options{address2} || $self->address2,
4019 'city' => $options{city} || $self->city,
4020 'state' => $options{state} || $self->state,
4021 'zip' => $options{zip} || $self->zip,
4022 'country' => $options{country} || $self->country,
4023 'payby' => $options{payby} || $self->payby,
4024 'payinfo' => $options{payinfo} || $self->payinfo,
4025 'exp' => $options{paydate} || $self->paydate,
4026 'payname' => $options{payname} || $self->payname,
4027 'amount' => $amount, # consolidating
4030 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
4031 if $old_cust_pay_batch;
4034 if ($old_cust_pay_batch) {
4035 $error = $cust_pay_batch->replace($old_cust_pay_batch)
4037 $error = $cust_pay_batch->insert;
4041 $dbh->rollback if $oldAutoCommit;
4045 my $unapplied = $self->total_credited + $self->total_unapplied_payments + $self->in_transit_payments;
4046 foreach my $cust_bill ($self->open_cust_bill) {
4047 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
4048 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
4049 'invnum' => $cust_bill->invnum,
4050 'paybatchnum' => $cust_pay_batch->paybatchnum,
4051 'amount' => $cust_bill->owed,
4054 if ($unapplied >= $cust_bill_pay_batch->amount){
4055 $unapplied -= $cust_bill_pay_batch->amount;
4058 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
4059 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
4061 $error = $cust_bill_pay_batch->insert;
4063 $dbh->rollback if $oldAutoCommit;
4068 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4074 Returns the total owed for this customer on all invoices
4075 (see L<FS::cust_bill/owed>).
4081 $self->total_owed_date(2145859200); #12/31/2037
4084 =item total_owed_date TIME
4086 Returns the total owed for this customer on all invoices with date earlier than
4087 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
4088 see L<Time::Local> and L<Date::Parse> for conversion functions.
4092 sub total_owed_date {
4096 foreach my $cust_bill (
4097 grep { $_->_date <= $time }
4098 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4100 $total_bill += $cust_bill->owed;
4102 sprintf( "%.2f", $total_bill );
4105 =item apply_payments_and_credits
4107 Applies unapplied payments and credits.
4109 In most cases, this new method should be used in place of sequential
4110 apply_payments and apply_credits methods.
4112 If there is an error, returns the error, otherwise returns false.
4116 sub apply_payments_and_credits {
4119 local $SIG{HUP} = 'IGNORE';
4120 local $SIG{INT} = 'IGNORE';
4121 local $SIG{QUIT} = 'IGNORE';
4122 local $SIG{TERM} = 'IGNORE';
4123 local $SIG{TSTP} = 'IGNORE';
4124 local $SIG{PIPE} = 'IGNORE';
4126 my $oldAutoCommit = $FS::UID::AutoCommit;
4127 local $FS::UID::AutoCommit = 0;
4130 $self->select_for_update; #mutex
4132 foreach my $cust_bill ( $self->open_cust_bill ) {
4133 my $error = $cust_bill->apply_payments_and_credits;
4135 $dbh->rollback if $oldAutoCommit;
4136 return "Error applying: $error";
4140 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4145 =item apply_credits OPTION => VALUE ...
4147 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
4148 to outstanding invoice balances in chronological order (or reverse
4149 chronological order if the I<order> option is set to B<newest>) and returns the
4150 value of any remaining unapplied credits available for refund (see
4151 L<FS::cust_refund>).
4153 Dies if there is an error.
4161 local $SIG{HUP} = 'IGNORE';
4162 local $SIG{INT} = 'IGNORE';
4163 local $SIG{QUIT} = 'IGNORE';
4164 local $SIG{TERM} = 'IGNORE';
4165 local $SIG{TSTP} = 'IGNORE';
4166 local $SIG{PIPE} = 'IGNORE';
4168 my $oldAutoCommit = $FS::UID::AutoCommit;
4169 local $FS::UID::AutoCommit = 0;
4172 $self->select_for_update; #mutex
4174 unless ( $self->total_credited ) {
4175 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4179 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
4180 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
4182 my @invoices = $self->open_cust_bill;
4183 @invoices = sort { $b->_date <=> $a->_date } @invoices
4184 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
4187 foreach my $cust_bill ( @invoices ) {
4190 if ( !defined($credit) || $credit->credited == 0) {
4191 $credit = pop @credits or last;
4194 if ($cust_bill->owed >= $credit->credited) {
4195 $amount=$credit->credited;
4197 $amount=$cust_bill->owed;
4200 my $cust_credit_bill = new FS::cust_credit_bill ( {
4201 'crednum' => $credit->crednum,
4202 'invnum' => $cust_bill->invnum,
4203 'amount' => $amount,
4205 my $error = $cust_credit_bill->insert;
4207 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
4211 redo if ($cust_bill->owed > 0);
4215 my $total_credited = $self->total_credited;
4217 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4219 return $total_credited;
4222 =item apply_payments
4224 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
4225 to outstanding invoice balances in chronological order.
4227 #and returns the value of any remaining unapplied payments.
4229 Dies if there is an error.
4233 sub apply_payments {
4236 local $SIG{HUP} = 'IGNORE';
4237 local $SIG{INT} = 'IGNORE';
4238 local $SIG{QUIT} = 'IGNORE';
4239 local $SIG{TERM} = 'IGNORE';
4240 local $SIG{TSTP} = 'IGNORE';
4241 local $SIG{PIPE} = 'IGNORE';
4243 my $oldAutoCommit = $FS::UID::AutoCommit;
4244 local $FS::UID::AutoCommit = 0;
4247 $self->select_for_update; #mutex
4251 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
4252 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
4254 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
4255 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
4259 foreach my $cust_bill ( @invoices ) {
4262 if ( !defined($payment) || $payment->unapplied == 0 ) {
4263 $payment = pop @payments or last;
4266 if ( $cust_bill->owed >= $payment->unapplied ) {
4267 $amount = $payment->unapplied;
4269 $amount = $cust_bill->owed;
4272 my $cust_bill_pay = new FS::cust_bill_pay ( {
4273 'paynum' => $payment->paynum,
4274 'invnum' => $cust_bill->invnum,
4275 'amount' => $amount,
4277 my $error = $cust_bill_pay->insert;
4279 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
4283 redo if ( $cust_bill->owed > 0);
4287 my $total_unapplied_payments = $self->total_unapplied_payments;
4289 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4291 return $total_unapplied_payments;
4294 =item total_credited
4296 Returns the total outstanding credit (see L<FS::cust_credit>) for this
4297 customer. See L<FS::cust_credit/credited>.
4301 sub total_credited {
4303 my $total_credit = 0;
4304 foreach my $cust_credit ( qsearch('cust_credit', {
4305 'custnum' => $self->custnum,
4307 $total_credit += $cust_credit->credited;
4309 sprintf( "%.2f", $total_credit );
4312 =item total_unapplied_payments
4314 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
4315 See L<FS::cust_pay/unapplied>.
4319 sub total_unapplied_payments {
4321 my $total_unapplied = 0;
4322 foreach my $cust_pay ( qsearch('cust_pay', {
4323 'custnum' => $self->custnum,
4325 $total_unapplied += $cust_pay->unapplied;
4327 sprintf( "%.2f", $total_unapplied );
4330 =item total_unapplied_refunds
4332 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
4333 customer. See L<FS::cust_refund/unapplied>.
4337 sub total_unapplied_refunds {
4339 my $total_unapplied = 0;
4340 foreach my $cust_refund ( qsearch('cust_refund', {
4341 'custnum' => $self->custnum,
4343 $total_unapplied += $cust_refund->unapplied;
4345 sprintf( "%.2f", $total_unapplied );
4350 Returns the balance for this customer (total_owed plus total_unrefunded, minus
4351 total_credited minus total_unapplied_payments).
4359 + $self->total_unapplied_refunds
4360 - $self->total_credited
4361 - $self->total_unapplied_payments
4365 =item balance_date TIME
4367 Returns the balance for this customer, only considering invoices with date
4368 earlier than TIME (total_owed_date minus total_credited minus
4369 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
4370 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
4379 $self->total_owed_date($time)
4380 + $self->total_unapplied_refunds
4381 - $self->total_credited
4382 - $self->total_unapplied_payments
4386 =item in_transit_payments
4388 Returns the total of requests for payments for this customer pending in
4389 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
4393 sub in_transit_payments {
4395 my $in_transit_payments = 0;
4396 foreach my $pay_batch ( qsearch('pay_batch', {
4399 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
4400 'batchnum' => $pay_batch->batchnum,
4401 'custnum' => $self->custnum,
4403 $in_transit_payments += $cust_pay_batch->amount;
4406 sprintf( "%.2f", $in_transit_payments );
4409 =item paydate_monthyear
4411 Returns a two-element list consisting of the month and year of this customer's
4412 paydate (credit card expiration date for CARD customers)
4416 sub paydate_monthyear {
4418 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
4420 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
4427 =item invoicing_list [ ARRAYREF ]
4429 If an arguement is given, sets these email addresses as invoice recipients
4430 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
4431 (except as warnings), so use check_invoicing_list first.
4433 Returns a list of email addresses (with svcnum entries expanded).
4435 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
4436 check it without disturbing anything by passing nothing.
4438 This interface may change in the future.
4442 sub invoicing_list {
4443 my( $self, $arrayref ) = @_;
4446 my @cust_main_invoice;
4447 if ( $self->custnum ) {
4448 @cust_main_invoice =
4449 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4451 @cust_main_invoice = ();
4453 foreach my $cust_main_invoice ( @cust_main_invoice ) {
4454 #warn $cust_main_invoice->destnum;
4455 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
4456 #warn $cust_main_invoice->destnum;
4457 my $error = $cust_main_invoice->delete;
4458 warn $error if $error;
4461 if ( $self->custnum ) {
4462 @cust_main_invoice =
4463 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4465 @cust_main_invoice = ();
4467 my %seen = map { $_->address => 1 } @cust_main_invoice;
4468 foreach my $address ( @{$arrayref} ) {
4469 next if exists $seen{$address} && $seen{$address};
4470 $seen{$address} = 1;
4471 my $cust_main_invoice = new FS::cust_main_invoice ( {
4472 'custnum' => $self->custnum,
4475 my $error = $cust_main_invoice->insert;
4476 warn $error if $error;
4480 if ( $self->custnum ) {
4482 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4489 =item check_invoicing_list ARRAYREF
4491 Checks these arguements as valid input for the invoicing_list method. If there
4492 is an error, returns the error, otherwise returns false.
4496 sub check_invoicing_list {
4497 my( $self, $arrayref ) = @_;
4499 foreach my $address ( @$arrayref ) {
4501 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
4502 return 'Can\'t add FAX invoice destination with a blank FAX number.';
4505 my $cust_main_invoice = new FS::cust_main_invoice ( {
4506 'custnum' => $self->custnum,
4509 my $error = $self->custnum
4510 ? $cust_main_invoice->check
4511 : $cust_main_invoice->checkdest
4513 return $error if $error;
4517 return "Email address required"
4518 if $conf->exists('cust_main-require_invoicing_list_email')
4519 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
4524 =item set_default_invoicing_list
4526 Sets the invoicing list to all accounts associated with this customer,
4527 overwriting any previous invoicing list.
4531 sub set_default_invoicing_list {
4533 $self->invoicing_list($self->all_emails);
4538 Returns the email addresses of all accounts provisioned for this customer.
4545 foreach my $cust_pkg ( $self->all_pkgs ) {
4546 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
4548 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4549 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4551 $list{$_}=1 foreach map { $_->email } @svc_acct;
4556 =item invoicing_list_addpost
4558 Adds postal invoicing to this customer. If this customer is already configured
4559 to receive postal invoices, does nothing.
4563 sub invoicing_list_addpost {
4565 return if grep { $_ eq 'POST' } $self->invoicing_list;
4566 my @invoicing_list = $self->invoicing_list;
4567 push @invoicing_list, 'POST';
4568 $self->invoicing_list(\@invoicing_list);
4571 =item invoicing_list_emailonly
4573 Returns the list of email invoice recipients (invoicing_list without non-email
4574 destinations such as POST and FAX).
4578 sub invoicing_list_emailonly {
4580 warn "$me invoicing_list_emailonly called"
4582 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
4585 =item invoicing_list_emailonly_scalar
4587 Returns the list of email invoice recipients (invoicing_list without non-email
4588 destinations such as POST and FAX) as a comma-separated scalar.
4592 sub invoicing_list_emailonly_scalar {
4594 warn "$me invoicing_list_emailonly_scalar called"
4596 join(', ', $self->invoicing_list_emailonly);
4599 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
4601 Returns an array of customers referred by this customer (referral_custnum set
4602 to this custnum). If DEPTH is given, recurses up to the given depth, returning
4603 customers referred by customers referred by this customer and so on, inclusive.
4604 The default behavior is DEPTH 1 (no recursion).
4608 sub referral_cust_main {
4610 my $depth = @_ ? shift : 1;
4611 my $exclude = @_ ? shift : {};
4614 map { $exclude->{$_->custnum}++; $_; }
4615 grep { ! $exclude->{ $_->custnum } }
4616 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
4620 map { $_->referral_cust_main($depth-1, $exclude) }
4627 =item referral_cust_main_ncancelled
4629 Same as referral_cust_main, except only returns customers with uncancelled
4634 sub referral_cust_main_ncancelled {
4636 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
4639 =item referral_cust_pkg [ DEPTH ]
4641 Like referral_cust_main, except returns a flat list of all unsuspended (and
4642 uncancelled) packages for each customer. The number of items in this list may
4643 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
4647 sub referral_cust_pkg {
4649 my $depth = @_ ? shift : 1;
4651 map { $_->unsuspended_pkgs }
4652 grep { $_->unsuspended_pkgs }
4653 $self->referral_cust_main($depth);
4656 =item referring_cust_main
4658 Returns the single cust_main record for the customer who referred this customer
4659 (referral_custnum), or false.
4663 sub referring_cust_main {
4665 return '' unless $self->referral_custnum;
4666 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
4669 =item credit AMOUNT, REASON
4671 Applies a credit to this customer. If there is an error, returns the error,
4672 otherwise returns false.
4677 my( $self, $amount, $reason, %options ) = @_;
4678 my $cust_credit = new FS::cust_credit {
4679 'custnum' => $self->custnum,
4680 'amount' => $amount,
4681 'reason' => $reason,
4683 $cust_credit->insert(%options);
4686 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
4688 Creates a one-time charge for this customer. If there is an error, returns
4689 the error, otherwise returns false.
4695 my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
4696 if ( ref( $_[0] ) ) {
4697 $amount = $_[0]->{amount};
4698 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
4699 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
4700 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
4701 : '$'. sprintf("%.2f",$amount);
4702 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
4703 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
4704 $additional = $_[0]->{additional};
4708 $pkg = @_ ? shift : 'One-time charge';
4709 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
4710 $taxclass = @_ ? shift : '';
4714 local $SIG{HUP} = 'IGNORE';
4715 local $SIG{INT} = 'IGNORE';
4716 local $SIG{QUIT} = 'IGNORE';
4717 local $SIG{TERM} = 'IGNORE';
4718 local $SIG{TSTP} = 'IGNORE';
4719 local $SIG{PIPE} = 'IGNORE';
4721 my $oldAutoCommit = $FS::UID::AutoCommit;
4722 local $FS::UID::AutoCommit = 0;
4725 my $part_pkg = new FS::part_pkg ( {
4727 'comment' => $comment,
4731 'classnum' => $classnum ? $classnum : '',
4732 'taxclass' => $taxclass,
4735 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
4736 ( 0 .. @$additional - 1 )
4738 'additional_count' => scalar(@$additional),
4739 'setup_fee' => $amount,
4742 my $error = $part_pkg->insert( options => \%options );
4744 $dbh->rollback if $oldAutoCommit;
4748 my $pkgpart = $part_pkg->pkgpart;
4749 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
4750 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
4751 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
4752 $error = $type_pkgs->insert;
4754 $dbh->rollback if $oldAutoCommit;
4759 my $cust_pkg = new FS::cust_pkg ( {
4760 'custnum' => $self->custnum,
4761 'pkgpart' => $pkgpart,
4762 'quantity' => $quantity,
4765 $error = $cust_pkg->insert;
4767 $dbh->rollback if $oldAutoCommit;
4771 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4776 #=item charge_postal_fee
4778 #Applies a one time charge this customer. If there is an error,
4779 #returns the error, returns the cust_pkg charge object or false
4780 #if there was no charge.
4784 # This should be a customer event. For that to work requires that bill
4785 # also be a customer event.
4787 sub charge_postal_fee {
4790 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
4791 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
4793 my $cust_pkg = new FS::cust_pkg ( {
4794 'custnum' => $self->custnum,
4795 'pkgpart' => $pkgpart,
4799 my $error = $cust_pkg->insert;
4800 $error ? $error : $cust_pkg;
4805 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4811 sort { $a->_date <=> $b->_date }
4812 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4815 =item open_cust_bill
4817 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4822 sub open_cust_bill {
4824 grep { $_->owed > 0 } $self->cust_bill;
4829 Returns all the credits (see L<FS::cust_credit>) for this customer.
4835 sort { $a->_date <=> $b->_date }
4836 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4841 Returns all the payments (see L<FS::cust_pay>) for this customer.
4847 sort { $a->_date <=> $b->_date }
4848 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
4853 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4859 sort { $a->_date <=> $b->_date }
4860 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4863 =item cust_pay_batch
4865 Returns all batched payments (see L<FS::cust_pay_void>) for this customer.
4869 sub cust_pay_batch {
4871 sort { $a->_date <=> $b->_date }
4872 qsearch( 'cust_pay_batch', { 'custnum' => $self->custnum } )
4877 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4883 sort { $a->_date <=> $b->_date }
4884 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4889 Returns a name string for this customer, either "Company (Last, First)" or
4896 my $name = $self->contact;
4897 $name = $self->company. " ($name)" if $self->company;
4903 Returns a name string for this (service/shipping) contact, either
4904 "Company (Last, First)" or "Last, First".
4910 if ( $self->get('ship_last') ) {
4911 my $name = $self->ship_contact;
4912 $name = $self->ship_company. " ($name)" if $self->ship_company;
4921 Returns this customer's full (billing) contact name only, "Last, First"
4927 $self->get('last'). ', '. $self->first;
4932 Returns this customer's full (shipping) contact name only, "Last, First"
4938 $self->get('ship_last')
4939 ? $self->get('ship_last'). ', '. $self->ship_first
4945 Returns this customer's full country name
4951 code2country($self->country);
4954 =item geocode DATA_VENDOR
4956 Returns a value for the customer location as encoded by DATA_VENDOR.
4957 Currently this only makes sense for "CCH" as DATA_VENDOR.
4962 my ($self, $data_vendor) = (shift, shift); #always cch for now
4964 my $prefix = ( $conf->exists('tax-ship_address') && length($self->ship_last) )
4968 my ($zip,$plus4) = split /-/, $self->get("${prefix}zip")
4969 if $self->country eq 'US';
4971 #CCH specific location stuff
4972 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
4975 my $cust_tax_location =
4977 'table' => 'cust_tax_location',
4978 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
4979 'extra_sql' => $extra_sql,
4982 $geocode = $cust_tax_location->geocode
4983 if $cust_tax_location;
4992 Returns a status string for this customer, currently:
4996 =item prospect - No packages have ever been ordered
4998 =item active - One or more recurring packages is active
5000 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
5002 =item suspended - All non-cancelled recurring packages are suspended
5004 =item cancelled - All recurring packages are cancelled
5010 sub status { shift->cust_status(@_); }
5014 for my $status (qw( prospect active inactive suspended cancelled )) {
5015 my $method = $status.'_sql';
5016 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
5017 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
5018 $sth->execute( ($self->custnum) x $numnum )
5019 or die "Error executing 'SELECT $sql': ". $sth->errstr;
5020 return $status if $sth->fetchrow_arrayref->[0];
5024 =item ucfirst_cust_status
5026 =item ucfirst_status
5028 Returns the status with the first character capitalized.
5032 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
5034 sub ucfirst_cust_status {
5036 ucfirst($self->cust_status);
5041 Returns a hex triplet color string for this customer's status.
5045 use vars qw(%statuscolor);
5046 tie %statuscolor, 'Tie::IxHash',
5047 'prospect' => '7e0079', #'000000', #black? naw, purple
5048 'active' => '00CC00', #green
5049 'inactive' => '0000CC', #blue
5050 'suspended' => 'FF9900', #yellow
5051 'cancelled' => 'FF0000', #red
5054 sub statuscolor { shift->cust_statuscolor(@_); }
5056 sub cust_statuscolor {
5058 $statuscolor{$self->cust_status};
5063 Returns an array of hashes representing the customer's RT tickets.
5070 my $num = $conf->config('cust_main-max_tickets') || 10;
5073 unless ( $conf->config('ticket_system-custom_priority_field') ) {
5075 @tickets = @{ FS::TicketSystem->customer_tickets($self->custnum, $num) };
5079 foreach my $priority (
5080 $conf->config('ticket_system-custom_priority_field-values'), ''
5082 last if scalar(@tickets) >= $num;
5084 @{ FS::TicketSystem->customer_tickets( $self->custnum,
5085 $num - scalar(@tickets),
5094 # Return services representing svc_accts in customer support packages
5095 sub support_services {
5097 my %packages = map { $_ => 1 } $conf->config('support_packages');
5099 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
5100 grep { $_->part_svc->svcdb eq 'svc_acct' }
5101 map { $_->cust_svc }
5102 grep { exists $packages{ $_->pkgpart } }
5103 $self->ncancelled_pkgs;
5109 =head1 CLASS METHODS
5115 Class method that returns the list of possible status strings for customers
5116 (see L<the status method|/status>). For example:
5118 @statuses = FS::cust_main->statuses();
5123 #my $self = shift; #could be class...
5129 Returns an SQL expression identifying prospective cust_main records (customers
5130 with no packages ever ordered)
5134 use vars qw($select_count_pkgs);
5135 $select_count_pkgs =
5136 "SELECT COUNT(*) FROM cust_pkg
5137 WHERE cust_pkg.custnum = cust_main.custnum";
5139 sub select_count_pkgs_sql {
5143 sub prospect_sql { "
5144 0 = ( $select_count_pkgs )
5149 Returns an SQL expression identifying active cust_main records (customers with
5150 active recurring packages).
5155 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
5161 Returns an SQL expression identifying inactive cust_main records (customers with
5162 no active recurring packages, but otherwise unsuspended/uncancelled).
5166 sub inactive_sql { "
5167 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
5169 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
5175 Returns an SQL expression identifying suspended cust_main records.
5180 sub suspended_sql { susp_sql(@_); }
5182 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
5184 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
5190 Returns an SQL expression identifying cancelled cust_main records.
5194 sub cancelled_sql { cancel_sql(@_); }
5197 my $recurring_sql = FS::cust_pkg->recurring_sql;
5198 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
5201 0 < ( $select_count_pkgs )
5202 AND 0 < ( $select_count_pkgs AND $recurring_sql AND $cancelled_sql )
5203 AND 0 = ( $select_count_pkgs AND $recurring_sql
5204 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5206 AND 0 = ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
5212 =item uncancelled_sql
5214 Returns an SQL expression identifying un-cancelled cust_main records.
5218 sub uncancelled_sql { uncancel_sql(@_); }
5219 sub uncancel_sql { "
5220 ( 0 < ( $select_count_pkgs
5221 AND ( cust_pkg.cancel IS NULL
5222 OR cust_pkg.cancel = 0
5225 OR 0 = ( $select_count_pkgs )
5231 Returns an SQL fragment to retreive the balance.
5236 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
5237 WHERE cust_bill.custnum = cust_main.custnum )
5238 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
5239 WHERE cust_pay.custnum = cust_main.custnum )
5240 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
5241 WHERE cust_credit.custnum = cust_main.custnum )
5242 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
5243 WHERE cust_refund.custnum = cust_main.custnum )
5246 =item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5248 Returns an SQL fragment to retreive the balance for this customer, only
5249 considering invoices with date earlier than START_TIME, and optionally not
5250 later than END_TIME (total_owed_date minus total_credited minus
5251 total_unapplied_payments).
5253 Times are specified as SQL fragments or numeric
5254 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
5255 L<Date::Parse> for conversion functions. The empty string can be passed
5256 to disable that time constraint completely.
5258 Available options are:
5262 =item unapplied_date
5264 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)
5269 set to true to remove all customer comparison clauses, for totals
5274 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
5279 JOIN clause (typically used with the total option)
5285 sub balance_date_sql {
5286 my( $class, $start, $end, %opt ) = @_;
5288 my $owed = FS::cust_bill->owed_sql;
5289 my $unapp_refund = FS::cust_refund->unapplied_sql;
5290 my $unapp_credit = FS::cust_credit->unapplied_sql;
5291 my $unapp_pay = FS::cust_pay->unapplied_sql;
5293 my $j = $opt{'join'} || '';
5295 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
5296 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
5297 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
5298 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
5300 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
5301 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
5302 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
5303 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
5308 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5310 Helper method for balance_date_sql; name (and usage) subject to change
5311 (suggestions welcome).
5313 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
5314 cust_refund, cust_credit or cust_pay).
5316 If TABLE is "cust_bill" or the unapplied_date option is true, only
5317 considers records with date earlier than START_TIME, and optionally not
5318 later than END_TIME .
5322 sub _money_table_where {
5323 my( $class, $table, $start, $end, %opt ) = @_;
5326 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
5327 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
5328 push @where, "$table._date <= $start" if defined($start) && length($start);
5329 push @where, "$table._date > $end" if defined($end) && length($end);
5331 push @where, @{$opt{'where'}} if $opt{'where'};
5332 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
5338 =item search_sql HASHREF
5342 Returns a qsearch hash expression to search for parameters specified in HREF.
5343 Valid parameters are
5351 =item cancelled_pkgs
5357 listref of start date, end date
5363 =item current_balance
5365 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
5369 =item flattened_pkgs
5378 my ($class, $params) = @_;
5389 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
5391 "cust_main.agentnum = $1";
5398 #prospect active inactive suspended cancelled
5399 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
5400 my $method = $params->{'status'}. '_sql';
5401 #push @where, $class->$method();
5402 push @where, FS::cust_main->$method();
5406 # parse cancelled package checkbox
5411 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
5412 unless $params->{'cancelled_pkgs'};
5418 foreach my $field (qw( signupdate )) {
5420 next unless exists($params->{$field});
5422 my($beginning, $ending) = @{$params->{$field}};
5425 "cust_main.$field IS NOT NULL",
5426 "cust_main.$field >= $beginning",
5427 "cust_main.$field <= $ending";
5429 $orderby ||= "ORDER BY cust_main.$field";
5437 my @payby = grep /^([A-Z]{4})$/, @{ $params->{'payby'} };
5439 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
5446 #my $balance_sql = $class->balance_sql();
5447 my $balance_sql = FS::cust_main->balance_sql();
5449 push @where, map { s/current_balance/$balance_sql/; $_ }
5450 @{ $params->{'current_balance'} };
5453 # setup queries, subs, etc. for the search
5456 $orderby ||= 'ORDER BY custnum';
5458 # here is the agent virtualization
5459 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
5461 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
5463 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
5465 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
5467 my $select = join(', ',
5468 'cust_main.custnum',
5469 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
5472 my(@extra_headers) = ();
5473 my(@extra_fields) = ();
5475 if ($params->{'flattened_pkgs'}) {
5477 if ($dbh->{Driver}->{Name} eq 'Pg') {
5479 $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";
5481 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
5482 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
5483 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
5485 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
5486 "omitting packing information from report.";
5489 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";
5491 my $sth = dbh->prepare($header_query) or die dbh->errstr;
5492 $sth->execute() or die $sth->errstr;
5493 my $headerrow = $sth->fetchrow_arrayref;
5494 my $headercount = $headerrow ? $headerrow->[0] : 0;
5495 while($headercount) {
5496 unshift @extra_headers, "Package ". $headercount;
5497 unshift @extra_fields, eval q!sub {my $c = shift;
5498 my @a = split '\|', $c->magic;
5499 my $p = $a[!.--$headercount. q!];
5507 'table' => 'cust_main',
5508 'select' => $select,
5510 'extra_sql' => $extra_sql,
5511 'order_by' => $orderby,
5512 'count_query' => $count_query,
5513 'extra_headers' => \@extra_headers,
5514 'extra_fields' => \@extra_fields,
5519 =item email_search_sql HASHREF
5523 Emails a notice to the specified customers.
5525 Valid parameters are those of the L<search_sql> method, plus the following:
5547 Optional job queue job for status updates.
5551 Returns an error message, or false for success.
5553 If an error occurs during any email, stops the enture send and returns that
5554 error. Presumably if you're getting SMTP errors aborting is better than
5555 retrying everything.
5559 sub email_search_sql {
5560 my($class, $params) = @_;
5562 my $from = delete $params->{from};
5563 my $subject = delete $params->{subject};
5564 my $html_body = delete $params->{html_body};
5565 my $text_body = delete $params->{text_body};
5567 my $job = delete $params->{'job'};
5569 my $sql_query = $class->search_sql($params);
5571 my $count_query = delete($sql_query->{'count_query'});
5572 my $count_sth = dbh->prepare($count_query)
5573 or die "Error preparing $count_query: ". dbh->errstr;
5575 or die "Error executing $count_query: ". $count_sth->errstr;
5576 my $count_arrayref = $count_sth->fetchrow_arrayref;
5577 my $num_cust = $count_arrayref->[0];
5579 #my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
5580 #my @extra_fields = @{ delete($sql_query->{'extra_fields'}) };
5583 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
5585 #eventually order+limit magic to reduce memory use?
5586 foreach my $cust_main ( qsearch($sql_query) ) {
5588 my $to = $cust_main->invoicing_list_emailonly_scalar;
5591 my $error = send_email(
5595 'subject' => $subject,
5596 'html_body' => $html_body,
5597 'text_body' => $text_body,
5600 return $error if $error;
5602 if ( $job ) { #progressbar foo
5604 if ( time - $min_sec > $last ) {
5605 my $error = $job->update_statustext(
5606 int( 100 * $num / $num_cust )
5608 die $error if $error;
5618 use Storable qw(thaw);
5621 sub process_email_search_sql {
5623 #warn "$me process_re_X $method for job $job\n" if $DEBUG;
5625 my $param = thaw(decode_base64(shift));
5626 warn Dumper($param) if $DEBUG;
5628 $param->{'job'} = $job;
5630 my $error = FS::cust_main->email_search_sql( $param );
5631 die $error if $error;
5635 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
5637 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
5638 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
5639 appropriate ship_ field is also searched).
5641 Additional options are the same as FS::Record::qsearch
5646 my( $self, $fuzzy, $hash, @opt) = @_;
5651 check_and_rebuild_fuzzyfiles();
5652 foreach my $field ( keys %$fuzzy ) {
5654 my $all = $self->all_X($field);
5655 next unless scalar(@$all);
5658 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
5661 foreach ( keys %match ) {
5662 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
5663 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
5666 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
5669 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
5671 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
5679 Returns a masked version of the named field
5684 my ($self,$field) = @_;
5688 'x'x(length($self->getfield($field))-4).
5689 substr($self->getfield($field), (length($self->getfield($field))-4));
5699 =item smart_search OPTION => VALUE ...
5701 Accepts the following options: I<search>, the string to search for. The string
5702 will be searched for as a customer number, phone number, name or company name,
5703 as an exact, or, in some cases, a substring or fuzzy match (see the source code
5704 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
5705 skip fuzzy matching when an exact match is found.
5707 Any additional options are treated as an additional qualifier on the search
5710 Returns a (possibly empty) array of FS::cust_main objects.
5717 #here is the agent virtualization
5718 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5722 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
5723 my $search = delete $options{'search'};
5724 ( my $alphanum_search = $search ) =~ s/\W//g;
5726 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
5728 #false laziness w/Record::ut_phone
5729 my $phonen = "$1-$2-$3";
5730 $phonen .= " x$4" if $4;
5732 push @cust_main, qsearch( {
5733 'table' => 'cust_main',
5734 'hashref' => { %options },
5735 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5737 join(' OR ', map "$_ = '$phonen'",
5738 qw( daytime night fax
5739 ship_daytime ship_night ship_fax )
5742 " AND $agentnums_sql", #agent virtualization
5745 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
5746 #try looking for matches with extensions unless one was specified
5748 push @cust_main, qsearch( {
5749 'table' => 'cust_main',
5750 'hashref' => { %options },
5751 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5753 join(' OR ', map "$_ LIKE '$phonen\%'",
5755 ship_daytime ship_night )
5758 " AND $agentnums_sql", #agent virtualization
5763 # custnum search (also try agent_custid), with some tweaking options if your
5764 # legacy cust "numbers" have letters
5765 } elsif ( $search =~ /^\s*(\d+)\s*$/
5766 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
5767 && $search =~ /^\s*(\w\w?\d+)\s*$/
5772 push @cust_main, qsearch( {
5773 'table' => 'cust_main',
5774 'hashref' => { 'custnum' => $1, %options },
5775 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5778 push @cust_main, qsearch( {
5779 'table' => 'cust_main',
5780 'hashref' => { 'agent_custid' => $1, %options },
5781 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5784 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
5786 my($company, $last, $first) = ( $1, $2, $3 );
5788 # "Company (Last, First)"
5789 #this is probably something a browser remembered,
5790 #so just do an exact search
5792 foreach my $prefix ( '', 'ship_' ) {
5793 push @cust_main, qsearch( {
5794 'table' => 'cust_main',
5795 'hashref' => { $prefix.'first' => $first,
5796 $prefix.'last' => $last,
5797 $prefix.'company' => $company,
5800 'extra_sql' => " AND $agentnums_sql",
5804 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
5805 # try (ship_){last,company}
5809 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
5810 # # full strings the browser remembers won't work
5811 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
5813 use Lingua::EN::NameParse;
5814 my $NameParse = new Lingua::EN::NameParse(
5816 allow_reversed => 1,
5819 my($last, $first) = ( '', '' );
5820 #maybe disable this too and just rely on NameParse?
5821 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
5823 ($last, $first) = ( $1, $2 );
5825 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
5826 } elsif ( ! $NameParse->parse($value) ) {
5828 my %name = $NameParse->components;
5829 $first = $name{'given_name_1'};
5830 $last = $name{'surname_1'};
5834 if ( $first && $last ) {
5836 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
5839 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5841 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
5842 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
5845 push @cust_main, qsearch( {
5846 'table' => 'cust_main',
5847 'hashref' => \%options,
5848 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5851 # or it just be something that was typed in... (try that in a sec)
5855 my $q_value = dbh->quote($value);
5858 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5859 $sql .= " ( LOWER(last) = $q_value
5860 OR LOWER(company) = $q_value
5861 OR LOWER(ship_last) = $q_value
5862 OR LOWER(ship_company) = $q_value
5865 push @cust_main, qsearch( {
5866 'table' => 'cust_main',
5867 'hashref' => \%options,
5868 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5871 #no exact match, trying substring/fuzzy
5872 #always do substring & fuzzy (unless they're explicity config'ed off)
5873 #getting complaints searches are not returning enough
5874 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
5876 #still some false laziness w/search_sql (was search/cust_main.cgi)
5881 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
5882 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
5885 if ( $first && $last ) {
5888 { 'first' => { op=>'ILIKE', value=>"%$first%" },
5889 'last' => { op=>'ILIKE', value=>"%$last%" },
5891 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
5892 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
5899 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
5900 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
5904 foreach my $hashref ( @hashrefs ) {
5906 push @cust_main, qsearch( {
5907 'table' => 'cust_main',
5908 'hashref' => { %$hashref,
5911 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
5920 " AND $agentnums_sql", #extra_sql #agent virtualization
5923 if ( $first && $last ) {
5924 push @cust_main, FS::cust_main->fuzzy_search(
5925 { 'last' => $last, #fuzzy hashref
5926 'first' => $first }, #
5930 foreach my $field ( 'last', 'company' ) {
5932 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
5937 #eliminate duplicates
5939 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5949 Accepts the following options: I<email>, the email address to search for. The
5950 email address will be searched for as an email invoice destination and as an
5953 #Any additional options are treated as an additional qualifier on the search
5954 #(i.e. I<agentnum>).
5956 Returns a (possibly empty) array of FS::cust_main objects (but usually just
5966 my $email = delete $options{'email'};
5968 #we're only being used by RT at the moment... no agent virtualization yet
5969 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5973 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
5975 my ( $user, $domain ) = ( $1, $2 );
5977 warn "$me smart_search: searching for $user in domain $domain"
5983 'table' => 'cust_main_invoice',
5984 'hashref' => { 'dest' => $email },
5991 map $_->cust_svc->cust_pkg,
5993 'table' => 'svc_acct',
5994 'hashref' => { 'username' => $user, },
5996 'AND ( SELECT domain FROM svc_domain
5997 WHERE svc_acct.domsvc = svc_domain.svcnum
5998 ) = '. dbh->quote($domain),
6004 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
6006 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
6013 =item check_and_rebuild_fuzzyfiles
6017 use vars qw(@fuzzyfields);
6018 @fuzzyfields = ( 'last', 'first', 'company' );
6020 sub check_and_rebuild_fuzzyfiles {
6021 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6022 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
6025 =item rebuild_fuzzyfiles
6029 sub rebuild_fuzzyfiles {
6031 use Fcntl qw(:flock);
6033 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6034 mkdir $dir, 0700 unless -d $dir;
6036 foreach my $fuzzy ( @fuzzyfields ) {
6038 open(LOCK,">>$dir/cust_main.$fuzzy")
6039 or die "can't open $dir/cust_main.$fuzzy: $!";
6041 or die "can't lock $dir/cust_main.$fuzzy: $!";
6043 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
6044 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
6046 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
6047 my $sth = dbh->prepare("SELECT $field FROM cust_main".
6048 " WHERE $field != '' AND $field IS NOT NULL");
6049 $sth->execute or die $sth->errstr;
6051 while ( my $row = $sth->fetchrow_arrayref ) {
6052 print CACHE $row->[0]. "\n";
6057 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
6059 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
6070 my( $self, $field ) = @_;
6071 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6072 open(CACHE,"<$dir/cust_main.$field")
6073 or die "can't open $dir/cust_main.$field: $!";
6074 my @array = map { chomp; $_; } <CACHE>;
6079 =item append_fuzzyfiles LASTNAME COMPANY
6083 sub append_fuzzyfiles {
6084 #my( $first, $last, $company ) = @_;
6086 &check_and_rebuild_fuzzyfiles;
6088 use Fcntl qw(:flock);
6090 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6092 foreach my $field (qw( first last company )) {
6097 open(CACHE,">>$dir/cust_main.$field")
6098 or die "can't open $dir/cust_main.$field: $!";
6099 flock(CACHE,LOCK_EX)
6100 or die "can't lock $dir/cust_main.$field: $!";
6102 print CACHE "$value\n";
6104 flock(CACHE,LOCK_UN)
6105 or die "can't unlock $dir/cust_main.$field: $!";
6120 #warn join('-',keys %$param);
6121 my $fh = $param->{filehandle};
6122 my $agentnum = $param->{agentnum};
6124 my $refnum = $param->{refnum};
6125 my $pkgpart = $param->{pkgpart};
6127 #my @fields = @{$param->{fields}};
6128 my $format = $param->{'format'};
6131 if ( $format eq 'simple' ) {
6132 @fields = qw( cust_pkg.setup dayphone first last
6133 address1 address2 city state zip comments );
6135 } elsif ( $format eq 'extended' ) {
6136 @fields = qw( agent_custid refnum
6137 last first address1 address2 city state zip country
6139 ship_last ship_first ship_address1 ship_address2
6140 ship_city ship_state ship_zip ship_country
6141 payinfo paycvv paydate
6144 svc_acct.username svc_acct._password
6147 } elsif ( $format eq 'extended-plus_company' ) {
6148 @fields = qw( agent_custid refnum
6149 last first company address1 address2 city state zip country
6151 ship_last ship_first ship_company ship_address1 ship_address2
6152 ship_city ship_state ship_zip ship_country
6153 payinfo paycvv paydate
6156 svc_acct.username svc_acct._password
6160 die "unknown format $format";
6163 eval "use Text::CSV_XS;";
6166 my $csv = new Text::CSV_XS;
6173 local $SIG{HUP} = 'IGNORE';
6174 local $SIG{INT} = 'IGNORE';
6175 local $SIG{QUIT} = 'IGNORE';
6176 local $SIG{TERM} = 'IGNORE';
6177 local $SIG{TSTP} = 'IGNORE';
6178 local $SIG{PIPE} = 'IGNORE';
6180 my $oldAutoCommit = $FS::UID::AutoCommit;
6181 local $FS::UID::AutoCommit = 0;
6184 #while ( $columns = $csv->getline($fh) ) {
6186 while ( defined($line=<$fh>) ) {
6188 $csv->parse($line) or do {
6189 $dbh->rollback if $oldAutoCommit;
6190 return "can't parse: ". $csv->error_input();
6193 my @columns = $csv->fields();
6194 #warn join('-',@columns);
6197 agentnum => $agentnum,
6199 country => $conf->config('countrydefault') || 'US',
6200 payby => $payby, #default
6201 paydate => '12/2037', #default
6203 my $billtime = time;
6204 my %cust_pkg = ( pkgpart => $pkgpart );
6206 foreach my $field ( @fields ) {
6208 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
6210 #$cust_pkg{$1} = str2time( shift @$columns );
6211 if ( $1 eq 'pkgpart' ) {
6212 $cust_pkg{$1} = shift @columns;
6213 } elsif ( $1 eq 'setup' ) {
6214 $billtime = str2time(shift @columns);
6216 $cust_pkg{$1} = str2time( shift @columns );
6219 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
6221 $svc_acct{$1} = shift @columns;
6225 #refnum interception
6226 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
6228 my $referral = $columns[0];
6229 my %hash = ( 'referral' => $referral,
6230 'agentnum' => $agentnum,
6234 my $part_referral = qsearchs('part_referral', \%hash )
6235 || new FS::part_referral \%hash;
6237 unless ( $part_referral->refnum ) {
6238 my $error = $part_referral->insert;
6240 $dbh->rollback if $oldAutoCommit;
6241 return "can't auto-insert advertising source: $referral: $error";
6245 $columns[0] = $part_referral->refnum;
6248 #$cust_main{$field} = shift @$columns;
6249 $cust_main{$field} = shift @columns;
6253 $cust_main{'payby'} = 'CARD' if length($cust_main{'payinfo'});
6255 my $invoicing_list = $cust_main{'invoicing_list'}
6256 ? [ delete $cust_main{'invoicing_list'} ]
6259 my $cust_main = new FS::cust_main ( \%cust_main );
6262 tie my %hash, 'Tie::RefHash'; #this part is important
6264 if ( $cust_pkg{'pkgpart'} ) {
6265 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
6268 if ( $svc_acct{'username'} ) {
6269 my $part_pkg = $cust_pkg->part_pkg;
6270 unless ( $part_pkg ) {
6271 $dbh->rollback if $oldAutoCommit;
6272 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
6274 $svc_acct{svcpart} = $part_pkg->svcpart( 'svc_acct' );
6275 push @svc_acct, new FS::svc_acct ( \%svc_acct )
6278 $hash{$cust_pkg} = \@svc_acct;
6281 my $error = $cust_main->insert( \%hash, $invoicing_list );
6284 $dbh->rollback if $oldAutoCommit;
6285 return "can't insert customer for $line: $error";
6288 if ( $format eq 'simple' ) {
6290 #false laziness w/bill.cgi
6291 $error = $cust_main->bill( 'time' => $billtime );
6293 $dbh->rollback if $oldAutoCommit;
6294 return "can't bill customer for $line: $error";
6297 $error = $cust_main->apply_payments_and_credits;
6299 $dbh->rollback if $oldAutoCommit;
6300 return "can't bill customer for $line: $error";
6303 $error = $cust_main->collect();
6305 $dbh->rollback if $oldAutoCommit;
6306 return "can't collect customer for $line: $error";
6314 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6316 return "Empty file!" unless $imported;
6328 #warn join('-',keys %$param);
6329 my $fh = $param->{filehandle};
6330 my @fields = @{$param->{fields}};
6332 eval "use Text::CSV_XS;";
6335 my $csv = new Text::CSV_XS;
6342 local $SIG{HUP} = 'IGNORE';
6343 local $SIG{INT} = 'IGNORE';
6344 local $SIG{QUIT} = 'IGNORE';
6345 local $SIG{TERM} = 'IGNORE';
6346 local $SIG{TSTP} = 'IGNORE';
6347 local $SIG{PIPE} = 'IGNORE';
6349 my $oldAutoCommit = $FS::UID::AutoCommit;
6350 local $FS::UID::AutoCommit = 0;
6353 #while ( $columns = $csv->getline($fh) ) {
6355 while ( defined($line=<$fh>) ) {
6357 $csv->parse($line) or do {
6358 $dbh->rollback if $oldAutoCommit;
6359 return "can't parse: ". $csv->error_input();
6362 my @columns = $csv->fields();
6363 #warn join('-',@columns);
6366 foreach my $field ( @fields ) {
6367 $row{$field} = shift @columns;
6370 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
6371 unless ( $cust_main ) {
6372 $dbh->rollback if $oldAutoCommit;
6373 return "unknown custnum $row{'custnum'}";
6376 if ( $row{'amount'} > 0 ) {
6377 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
6379 $dbh->rollback if $oldAutoCommit;
6383 } elsif ( $row{'amount'} < 0 ) {
6384 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
6387 $dbh->rollback if $oldAutoCommit;
6397 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6399 return "Empty file!" unless $imported;
6405 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
6407 Sends a templated email notification to the customer (see L<Text::Template>).
6409 OPTIONS is a hash and may include
6411 I<from> - the email sender (default is invoice_from)
6413 I<to> - comma-separated scalar or arrayref of recipients
6414 (default is invoicing_list)
6416 I<subject> - The subject line of the sent email notification
6417 (default is "Notice from company_name")
6419 I<extra_fields> - a hashref of name/value pairs which will be substituted
6422 The following variables are vavailable in the template.
6424 I<$first> - the customer first name
6425 I<$last> - the customer last name
6426 I<$company> - the customer company
6427 I<$payby> - a description of the method of payment for the customer
6428 # would be nice to use FS::payby::shortname
6429 I<$payinfo> - the account information used to collect for this customer
6430 I<$expdate> - the expiration of the customer payment in seconds from epoch
6435 my ($customer, $template, %options) = @_;
6437 return unless $conf->exists($template);
6439 my $from = $conf->config('invoice_from') if $conf->exists('invoice_from');
6440 $from = $options{from} if exists($options{from});
6442 my $to = join(',', $customer->invoicing_list_emailonly);
6443 $to = $options{to} if exists($options{to});
6445 my $subject = "Notice from " . $conf->config('company_name')
6446 if $conf->exists('company_name');
6447 $subject = $options{subject} if exists($options{subject});
6449 my $notify_template = new Text::Template (TYPE => 'ARRAY',
6450 SOURCE => [ map "$_\n",
6451 $conf->config($template)]
6453 or die "can't create new Text::Template object: Text::Template::ERROR";
6454 $notify_template->compile()
6455 or die "can't compile template: Text::Template::ERROR";
6457 $FS::notify_template::_template::company_name = $conf->config('company_name');
6458 $FS::notify_template::_template::company_address =
6459 join("\n", $conf->config('company_address') ). "\n";
6461 my $paydate = $customer->paydate || '2037-12-31';
6462 $FS::notify_template::_template::first = $customer->first;
6463 $FS::notify_template::_template::last = $customer->last;
6464 $FS::notify_template::_template::company = $customer->company;
6465 $FS::notify_template::_template::payinfo = $customer->mask_payinfo;
6466 my $payby = $customer->payby;
6467 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6468 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6470 #credit cards expire at the end of the month/year of their exp date
6471 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6472 $FS::notify_template::_template::payby = 'credit card';
6473 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6474 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6476 }elsif ($payby eq 'COMP') {
6477 $FS::notify_template::_template::payby = 'complimentary account';
6479 $FS::notify_template::_template::payby = 'current method';
6481 $FS::notify_template::_template::expdate = $expire_time;
6483 for (keys %{$options{extra_fields}}){
6485 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
6488 send_email(from => $from,
6490 subject => $subject,
6491 body => $notify_template->fill_in( PACKAGE =>
6492 'FS::notify_template::_template' ),
6497 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
6499 Generates a templated notification to the customer (see L<Text::Template>).
6501 OPTIONS is a hash and may include
6503 I<extra_fields> - a hashref of name/value pairs which will be substituted
6504 into the template. These values may override values mentioned below
6505 and those from the customer record.
6507 The following variables are available in the template instead of or in addition
6508 to the fields of the customer record.
6510 I<$payby> - a description of the method of payment for the customer
6511 # would be nice to use FS::payby::shortname
6512 I<$payinfo> - the masked account information used to collect for this customer
6513 I<$expdate> - the expiration of the customer payment method in seconds from epoch
6514 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
6518 sub generate_letter {
6519 my ($self, $template, %options) = @_;
6521 return unless $conf->exists($template);
6523 my $letter_template = new Text::Template
6525 SOURCE => [ map "$_\n", $conf->config($template)],
6526 DELIMITERS => [ '[@--', '--@]' ],
6528 or die "can't create new Text::Template object: Text::Template::ERROR";
6530 $letter_template->compile()
6531 or die "can't compile template: Text::Template::ERROR";
6533 my %letter_data = map { $_ => $self->$_ } $self->fields;
6534 $letter_data{payinfo} = $self->mask_payinfo;
6536 #my $paydate = $self->paydate || '2037-12-31';
6537 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
6539 my $payby = $self->payby;
6540 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6541 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6543 #credit cards expire at the end of the month/year of their exp date
6544 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6545 $letter_data{payby} = 'credit card';
6546 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6547 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6549 }elsif ($payby eq 'COMP') {
6550 $letter_data{payby} = 'complimentary account';
6552 $letter_data{payby} = 'current method';
6554 $letter_data{expdate} = $expire_time;
6556 for (keys %{$options{extra_fields}}){
6557 $letter_data{$_} = $options{extra_fields}->{$_};
6560 unless(exists($letter_data{returnaddress})){
6561 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
6562 $self->agent_template)
6564 if ( length($retadd) ) {
6565 $letter_data{returnaddress} = $retadd;
6566 } elsif ( grep /\S/, $conf->config('company_address') ) {
6567 $letter_data{returnaddress} =
6568 join( '\\*'."\n", map s/( {2,})/'~' x length($1)/eg,
6569 $conf->config('company_address')
6572 $letter_data{returnaddress} = '~';
6576 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
6578 $letter_data{company_name} = $conf->config('company_name');
6580 my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
6581 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
6585 ) or die "can't open temp file: $!\n";
6587 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
6589 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
6593 =item print_ps TEMPLATE
6595 Returns an postscript letter filled in from TEMPLATE, as a scalar.
6601 my $file = $self->generate_letter(@_);
6602 FS::Misc::generate_ps($file);
6605 =item print TEMPLATE
6607 Prints the filled in template.
6609 TEMPLATE is the name of a L<Text::Template> to fill in and print.
6613 sub queueable_print {
6616 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
6617 or die "invalid customer number: " . $opt{custvnum};
6619 my $error = $self->print( $opt{template} );
6620 die $error if $error;
6624 my ($self, $template) = (shift, shift);
6625 do_print [ $self->print_ps($template) ];
6628 sub agent_template {
6630 $self->_agent_plandata('agent_templatename');
6633 sub agent_invoice_from {
6635 $self->_agent_plandata('agent_invoice_from');
6638 sub _agent_plandata {
6639 my( $self, $option ) = @_;
6641 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
6642 #agent-specific Conf
6644 use FS::part_event::Condition;
6646 my $agentnum = $self->agentnum;
6649 if ( driver_name =~ /^Pg/i ) {
6651 } elsif ( driver_name =~ /^mysql/i ) {
6654 die "don't know how to use regular expressions in ". driver_name. " databases";
6657 my $part_event_option =
6659 'select' => 'part_event_option.*',
6660 'table' => 'part_event_option',
6662 LEFT JOIN part_event USING ( eventpart )
6663 LEFT JOIN part_event_option AS peo_agentnum
6664 ON ( part_event.eventpart = peo_agentnum.eventpart
6665 AND peo_agentnum.optionname = 'agentnum'
6666 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
6668 LEFT JOIN part_event_option AS peo_cust_bill_age
6669 ON ( part_event.eventpart = peo_cust_bill_age.eventpart
6670 AND peo_cust_bill_age.optionname = 'cust_bill_age'
6673 #'hashref' => { 'optionname' => $option },
6674 #'hashref' => { 'part_event_option.optionname' => $option },
6676 " WHERE part_event_option.optionname = ". dbh->quote($option).
6677 " AND action = 'cust_bill_send_agent' ".
6678 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
6679 " AND peo_agentnum.optionname = 'agentnum' ".
6680 " AND agentnum IS NULL OR agentnum = $agentnum ".
6682 CASE WHEN peo_cust_bill_age.optionname != 'cust_bill_age'
6684 ELSE ". FS::part_event::Condition->age2seconds_sql('peo_cust_bill_age.optionvalue').
6686 , part_event.weight".
6690 unless ( $part_event_option ) {
6691 return $self->agent->invoice_template || ''
6692 if $option eq 'agent_templatename';
6696 $part_event_option->optionvalue;
6701 ## actual sub, not a method, designed to be called from the queue.
6702 ## sets up the customer, and calls the bill_and_collect
6703 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
6704 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
6705 $cust_main->bill_and_collect(
6716 The delete method should possibly take an FS::cust_main object reference
6717 instead of a scalar customer number.
6719 Bill and collect options should probably be passed as references instead of a
6722 There should probably be a configuration file with a list of allowed credit
6725 No multiple currency support (probably a larger project than just this module).
6727 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
6729 Birthdates rely on negative epoch values.
6731 The payby for card/check batches is broken. With mixed batching, bad
6734 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
6738 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
6739 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
6740 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.