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 Time::Local qw(timelocal_nocheck);
14 use Digest::MD5 qw(md5_base64);
18 use String::Approx qw(amatch);
19 use Business::CreditCard 0.28;
22 use FS::UID qw( getotaker dbh );
23 use FS::Record qw( qsearchs qsearch dbdef );
24 use FS::Misc qw( send_email generate_ps do_print );
25 use FS::Msgcat qw(gettext);
29 use FS::cust_bill_pkg;
31 use FS::cust_pay_void;
32 use FS::cust_pay_batch;
35 use FS::part_referral;
36 use FS::cust_main_county;
38 use FS::cust_main_invoice;
39 use FS::cust_credit_bill;
40 use FS::cust_bill_pay;
41 use FS::prepay_credit;
45 use FS::part_event_condition;
47 use FS::cust_tax_exempt;
48 use FS::cust_tax_exempt_pkg;
50 use FS::payment_gateway;
51 use FS::agent_payment_gateway;
53 use FS::payinfo_Mixin;
55 @ISA = qw( FS::Record FS::payinfo_Mixin );
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";
354 $self->invoicing_list( $invoicing_list );
357 if ( $conf->config('cust_main-skeleton_tables')
358 && $conf->config('cust_main-skeleton_custnum') ) {
360 warn " inserting skeleton records\n"
363 my $error = $self->start_copy_skel;
365 $dbh->rollback if $oldAutoCommit;
371 warn " ordering packages\n"
374 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
376 $dbh->rollback if $oldAutoCommit;
381 $dbh->rollback if $oldAutoCommit;
382 return "No svc_acct record to apply pre-paid time";
386 warn " inserting initial $payby payment of $amount\n"
388 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
390 $dbh->rollback if $oldAutoCommit;
391 return "inserting payment (transaction rolled back): $error";
395 unless ( $import || $skip_fuzzyfiles ) {
396 warn " queueing fuzzyfiles update\n"
398 $error = $self->queue_fuzzyfiles_update;
400 $dbh->rollback if $oldAutoCommit;
401 return "updating fuzzy search cache: $error";
405 warn " insert complete; committing transaction\n"
408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
413 sub start_copy_skel {
416 #'mg_user_preference' => {},
417 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
418 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
419 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
420 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
421 my @tables = eval(join('\n',$conf->config('cust_main-skeleton_tables')));
424 _copy_skel( 'cust_main', #tablename
425 $conf->config('cust_main-skeleton_custnum'), #sourceid
426 $self->custnum, #destid
427 @tables, #child tables
431 #recursive subroutine, not a method
433 my( $table, $sourceid, $destid, %child_tables ) = @_;
436 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
437 ( $table, $primary_key ) = ( $1, $2 );
439 my $dbdef_table = dbdef->table($table);
440 $primary_key = $dbdef_table->primary_key
441 or return "$table has no primary key".
442 " (or do you need to run dbdef-create?)";
445 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
446 join (', ', keys %child_tables). "\n"
449 foreach my $child_table_def ( keys %child_tables ) {
453 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
454 ( $child_table, $child_pkey ) = ( $1, $2 );
456 $child_table = $child_table_def;
458 $child_pkey = dbdef->table($child_table)->primary_key;
459 # or return "$table has no primary key".
460 # " (or do you need to run dbdef-create?)\n";
464 if ( keys %{ $child_tables{$child_table_def} } ) {
466 return "$child_table has no primary key".
467 " (run dbdef-create or try specifying it?)\n"
470 #false laziness w/Record::insert and only works on Pg
471 #refactor the proper last-inserted-id stuff out of Record::insert if this
472 # ever gets use for anything besides a quick kludge for one customer
473 my $default = dbdef->table($child_table)->column($child_pkey)->default;
474 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
475 or return "can't parse $child_table.$child_pkey default value ".
476 " for sequence name: $default";
481 my @sel_columns = grep { $_ ne $primary_key }
482 dbdef->table($child_table)->columns;
483 my $sel_columns = join(', ', @sel_columns );
485 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
486 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
487 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
489 my $sel_st = "SELECT $sel_columns FROM $child_table".
490 " WHERE $primary_key = $sourceid";
493 my $sel_sth = dbh->prepare( $sel_st )
494 or return dbh->errstr;
496 $sel_sth->execute or return $sel_sth->errstr;
498 while ( my $row = $sel_sth->fetchrow_hashref ) {
500 warn " selected row: ".
501 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
505 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
506 my $ins_sth =dbh->prepare($statement)
507 or return dbh->errstr;
508 my @param = ( $destid, map $row->{$_}, @ins_columns );
509 warn " $statement: [ ". join(', ', @param). " ]\n"
511 $ins_sth->execute( @param )
512 or return $ins_sth->errstr;
514 #next unless keys %{ $child_tables{$child_table} };
515 next unless $sequence;
517 #another section of that laziness
518 my $seq_sql = "SELECT currval('$sequence')";
519 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
520 $seq_sth->execute or return $seq_sth->errstr;
521 my $insertid = $seq_sth->fetchrow_arrayref->[0];
523 # don't drink soap! recurse! recurse! okay!
525 _copy_skel( $child_table_def,
526 $row->{$child_pkey}, #sourceid
528 %{ $child_tables{$child_table_def} },
530 return $error if $error;
540 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
542 Like the insert method on an existing record, this method orders a package
543 and included services atomicaly. Pass a Tie::RefHash data structure to this
544 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
545 be a better explanation of this, but until then, here's an example:
548 tie %hash, 'Tie::RefHash'; #this part is important
550 $cust_pkg => [ $svc_acct ],
553 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
555 Services can be new, in which case they are inserted, or existing unaudited
556 services, in which case they are linked to the newly-created package.
558 Currently available options are: I<depend_jobnum> and I<noexport>.
560 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
561 on the supplied jobnum (they will not run until the specific job completes).
562 This can be used to defer provisioning until some action completes (such
563 as running the customer's credit card successfully).
565 The I<noexport> option is deprecated. If I<noexport> is set true, no
566 provisioning jobs (exports) are scheduled. (You can schedule them later with
567 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
568 on the cust_main object is not recommended, as existing services will also be
575 my $cust_pkgs = shift;
578 my %svc_options = ();
579 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
580 if exists $options{'depend_jobnum'};
581 warn "$me order_pkgs called with options ".
582 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
585 local $SIG{HUP} = 'IGNORE';
586 local $SIG{INT} = 'IGNORE';
587 local $SIG{QUIT} = 'IGNORE';
588 local $SIG{TERM} = 'IGNORE';
589 local $SIG{TSTP} = 'IGNORE';
590 local $SIG{PIPE} = 'IGNORE';
592 my $oldAutoCommit = $FS::UID::AutoCommit;
593 local $FS::UID::AutoCommit = 0;
596 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
598 foreach my $cust_pkg ( keys %$cust_pkgs ) {
599 $cust_pkg->custnum( $self->custnum );
600 my $error = $cust_pkg->insert;
602 $dbh->rollback if $oldAutoCommit;
603 return "inserting cust_pkg (transaction rolled back): $error";
605 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
606 if ( $svc_something->svcnum ) {
607 my $old_cust_svc = $svc_something->cust_svc;
608 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
609 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
610 $error = $new_cust_svc->replace($old_cust_svc);
612 $svc_something->pkgnum( $cust_pkg->pkgnum );
613 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
614 $svc_something->seconds( $svc_something->seconds + $$seconds );
617 $error = $svc_something->insert(%svc_options);
620 $dbh->rollback if $oldAutoCommit;
621 #return "inserting svc_ (transaction rolled back): $error";
627 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
631 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
633 Recharges this (existing) customer with the specified prepaid card (see
634 L<FS::prepay_credit>), specified either by I<identifier> or as an
635 FS::prepay_credit object. If there is an error, returns the error, otherwise
638 Optionally, four scalar references can be passed as well. They will have their
639 values filled in with the amount, number of seconds, and number of upload and
640 download bytes applied by this prepaid
645 sub recharge_prepay {
646 my( $self, $prepay_credit, $amountref, $secondsref,
647 $upbytesref, $downbytesref, $totalbytesref ) = @_;
649 local $SIG{HUP} = 'IGNORE';
650 local $SIG{INT} = 'IGNORE';
651 local $SIG{QUIT} = 'IGNORE';
652 local $SIG{TERM} = 'IGNORE';
653 local $SIG{TSTP} = 'IGNORE';
654 local $SIG{PIPE} = 'IGNORE';
656 my $oldAutoCommit = $FS::UID::AutoCommit;
657 local $FS::UID::AutoCommit = 0;
660 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
662 my $error = $self->get_prepay($prepay_credit, \$amount,
663 \$seconds, \$upbytes, \$downbytes, \$totalbytes)
664 || $self->increment_seconds($seconds)
665 || $self->increment_upbytes($upbytes)
666 || $self->increment_downbytes($downbytes)
667 || $self->increment_totalbytes($totalbytes)
668 || $self->insert_cust_pay_prepay( $amount,
670 ? $prepay_credit->identifier
675 $dbh->rollback if $oldAutoCommit;
679 if ( defined($amountref) ) { $$amountref = $amount; }
680 if ( defined($secondsref) ) { $$secondsref = $seconds; }
681 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
682 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
683 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
685 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
690 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
692 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
693 specified either by I<identifier> or as an FS::prepay_credit object.
695 References to I<amount> and I<seconds> scalars should be passed as arguments
696 and will be incremented by the values of the prepaid card.
698 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
699 check or set this customer's I<agentnum>.
701 If there is an error, returns the error, otherwise returns false.
707 my( $self, $prepay_credit, $amountref, $secondsref,
708 $upref, $downref, $totalref) = @_;
710 local $SIG{HUP} = 'IGNORE';
711 local $SIG{INT} = 'IGNORE';
712 local $SIG{QUIT} = 'IGNORE';
713 local $SIG{TERM} = 'IGNORE';
714 local $SIG{TSTP} = 'IGNORE';
715 local $SIG{PIPE} = 'IGNORE';
717 my $oldAutoCommit = $FS::UID::AutoCommit;
718 local $FS::UID::AutoCommit = 0;
721 unless ( ref($prepay_credit) ) {
723 my $identifier = $prepay_credit;
725 $prepay_credit = qsearchs(
727 { 'identifier' => $prepay_credit },
732 unless ( $prepay_credit ) {
733 $dbh->rollback if $oldAutoCommit;
734 return "Invalid prepaid card: ". $identifier;
739 if ( $prepay_credit->agentnum ) {
740 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
741 $dbh->rollback if $oldAutoCommit;
742 return "prepaid card not valid for agent ". $self->agentnum;
744 $self->agentnum($prepay_credit->agentnum);
747 my $error = $prepay_credit->delete;
749 $dbh->rollback if $oldAutoCommit;
750 return "removing prepay_credit (transaction rolled back): $error";
753 $$amountref += $prepay_credit->amount;
754 $$secondsref += $prepay_credit->seconds;
755 $$upref += $prepay_credit->upbytes;
756 $$downref += $prepay_credit->downbytes;
757 $$totalref += $prepay_credit->totalbytes;
759 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
764 =item increment_upbytes SECONDS
766 Updates this customer's single or primary account (see L<FS::svc_acct>) by
767 the specified number of upbytes. If there is an error, returns the error,
768 otherwise returns false.
772 sub increment_upbytes {
773 _increment_column( shift, 'upbytes', @_);
776 =item increment_downbytes SECONDS
778 Updates this customer's single or primary account (see L<FS::svc_acct>) by
779 the specified number of downbytes. If there is an error, returns the error,
780 otherwise returns false.
784 sub increment_downbytes {
785 _increment_column( shift, 'downbytes', @_);
788 =item increment_totalbytes SECONDS
790 Updates this customer's single or primary account (see L<FS::svc_acct>) by
791 the specified number of totalbytes. If there is an error, returns the error,
792 otherwise returns false.
796 sub increment_totalbytes {
797 _increment_column( shift, 'totalbytes', @_);
800 =item increment_seconds SECONDS
802 Updates this customer's single or primary account (see L<FS::svc_acct>) by
803 the specified number of seconds. If there is an error, returns the error,
804 otherwise returns false.
808 sub increment_seconds {
809 _increment_column( shift, 'seconds', @_);
812 =item _increment_column AMOUNT
814 Updates this customer's single or primary account (see L<FS::svc_acct>) by
815 the specified number of seconds or bytes. If there is an error, returns
816 the error, otherwise returns false.
820 sub _increment_column {
821 my( $self, $column, $amount ) = @_;
822 warn "$me increment_column called: $column, $amount\n"
825 return '' unless $amount;
827 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
828 $self->ncancelled_pkgs;
831 return 'No packages with primary or single services found'.
832 ' to apply pre-paid time';
833 } elsif ( scalar(@cust_pkg) > 1 ) {
834 #maybe have a way to specify the package/account?
835 return 'Multiple packages found to apply pre-paid time';
838 my $cust_pkg = $cust_pkg[0];
839 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
843 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
846 return 'No account found to apply pre-paid time';
847 } elsif ( scalar(@cust_svc) > 1 ) {
848 return 'Multiple accounts found to apply pre-paid time';
851 my $svc_acct = $cust_svc[0]->svc_x;
852 warn " found service svcnum ". $svc_acct->pkgnum.
853 ' ('. $svc_acct->email. ")\n"
856 $column = "increment_$column";
857 $svc_acct->$column($amount);
861 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
863 Inserts a prepayment in the specified amount for this customer. An optional
864 second argument can specify the prepayment identifier for tracking purposes.
865 If there is an error, returns the error, otherwise returns false.
869 sub insert_cust_pay_prepay {
870 shift->insert_cust_pay('PREP', @_);
873 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
875 Inserts a cash payment in the specified amount for this customer. An optional
876 second argument can specify the payment identifier for tracking purposes.
877 If there is an error, returns the error, otherwise returns false.
881 sub insert_cust_pay_cash {
882 shift->insert_cust_pay('CASH', @_);
885 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
887 Inserts a Western Union payment in the specified amount for this customer. An
888 optional second argument can specify the prepayment identifier for tracking
889 purposes. If there is an error, returns the error, otherwise returns false.
893 sub insert_cust_pay_west {
894 shift->insert_cust_pay('WEST', @_);
897 sub insert_cust_pay {
898 my( $self, $payby, $amount ) = splice(@_, 0, 3);
899 my $payinfo = scalar(@_) ? shift : '';
901 my $cust_pay = new FS::cust_pay {
902 'custnum' => $self->custnum,
903 'paid' => sprintf('%.2f', $amount),
904 #'_date' => #date the prepaid card was purchased???
906 'payinfo' => $payinfo,
914 This method is deprecated. See the I<depend_jobnum> option to the insert and
915 order_pkgs methods for a better way to defer provisioning.
917 Re-schedules all exports by calling the B<reexport> method of all associated
918 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
919 otherwise returns false.
926 carp "WARNING: FS::cust_main::reexport is deprectated; ".
927 "use the depend_jobnum option to insert or order_pkgs to delay export";
929 local $SIG{HUP} = 'IGNORE';
930 local $SIG{INT} = 'IGNORE';
931 local $SIG{QUIT} = 'IGNORE';
932 local $SIG{TERM} = 'IGNORE';
933 local $SIG{TSTP} = 'IGNORE';
934 local $SIG{PIPE} = 'IGNORE';
936 my $oldAutoCommit = $FS::UID::AutoCommit;
937 local $FS::UID::AutoCommit = 0;
940 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
941 my $error = $cust_pkg->reexport;
943 $dbh->rollback if $oldAutoCommit;
948 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
953 =item delete NEW_CUSTNUM
955 This deletes the customer. If there is an error, returns the error, otherwise
958 This will completely remove all traces of the customer record. This is not
959 what you want when a customer cancels service; for that, cancel all of the
960 customer's packages (see L</cancel>).
962 If the customer has any uncancelled packages, you need to pass a new (valid)
963 customer number for those packages to be transferred to. Cancelled packages
964 will be deleted. Did I mention that this is NOT what you want when a customer
965 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
967 You can't delete a customer with invoices (see L<FS::cust_bill>),
968 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
969 refunds (see L<FS::cust_refund>).
976 local $SIG{HUP} = 'IGNORE';
977 local $SIG{INT} = 'IGNORE';
978 local $SIG{QUIT} = 'IGNORE';
979 local $SIG{TERM} = 'IGNORE';
980 local $SIG{TSTP} = 'IGNORE';
981 local $SIG{PIPE} = 'IGNORE';
983 my $oldAutoCommit = $FS::UID::AutoCommit;
984 local $FS::UID::AutoCommit = 0;
987 if ( $self->cust_bill ) {
988 $dbh->rollback if $oldAutoCommit;
989 return "Can't delete a customer with invoices";
991 if ( $self->cust_credit ) {
992 $dbh->rollback if $oldAutoCommit;
993 return "Can't delete a customer with credits";
995 if ( $self->cust_pay ) {
996 $dbh->rollback if $oldAutoCommit;
997 return "Can't delete a customer with payments";
999 if ( $self->cust_refund ) {
1000 $dbh->rollback if $oldAutoCommit;
1001 return "Can't delete a customer with refunds";
1004 my @cust_pkg = $self->ncancelled_pkgs;
1006 my $new_custnum = shift;
1007 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1008 $dbh->rollback if $oldAutoCommit;
1009 return "Invalid new customer number: $new_custnum";
1011 foreach my $cust_pkg ( @cust_pkg ) {
1012 my %hash = $cust_pkg->hash;
1013 $hash{'custnum'} = $new_custnum;
1014 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1015 my $error = $new_cust_pkg->replace($cust_pkg,
1016 options => { $cust_pkg->options },
1019 $dbh->rollback if $oldAutoCommit;
1024 my @cancelled_cust_pkg = $self->all_pkgs;
1025 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1026 my $error = $cust_pkg->delete;
1028 $dbh->rollback if $oldAutoCommit;
1033 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1034 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1036 my $error = $cust_main_invoice->delete;
1038 $dbh->rollback if $oldAutoCommit;
1043 my $error = $self->SUPER::delete;
1045 $dbh->rollback if $oldAutoCommit;
1049 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1054 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
1056 Replaces the OLD_RECORD with this one in the database. If there is an error,
1057 returns the error, otherwise returns false.
1059 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1060 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1061 expected and rollback the entire transaction; it is not necessary to call
1062 check_invoicing_list first. Here's an example:
1064 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1072 warn "$me replace called\n"
1075 local $SIG{HUP} = 'IGNORE';
1076 local $SIG{INT} = 'IGNORE';
1077 local $SIG{QUIT} = 'IGNORE';
1078 local $SIG{TERM} = 'IGNORE';
1079 local $SIG{TSTP} = 'IGNORE';
1080 local $SIG{PIPE} = 'IGNORE';
1082 # We absolutely have to have an old vs. new record to make this work.
1083 if (!defined($old)) {
1084 $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
1087 my $curuser = $FS::CurrentUser::CurrentUser;
1088 if ( $self->payby eq 'COMP'
1089 && $self->payby ne $old->payby
1090 && ! $curuser->access_right('Complimentary customer')
1093 return "You are not permitted to create complimentary accounts.";
1096 local($ignore_expired_card) = 1
1097 if $old->payby =~ /^(CARD|DCRD)$/
1098 && $self->payby =~ /^(CARD|DCRD)$/
1099 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1101 my $oldAutoCommit = $FS::UID::AutoCommit;
1102 local $FS::UID::AutoCommit = 0;
1105 my $error = $self->SUPER::replace($old);
1108 $dbh->rollback if $oldAutoCommit;
1112 if ( @param ) { # INVOICING_LIST_ARYREF
1113 my $invoicing_list = shift @param;
1114 $error = $self->check_invoicing_list( $invoicing_list );
1116 $dbh->rollback if $oldAutoCommit;
1119 $self->invoicing_list( $invoicing_list );
1122 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1123 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1124 # card/check/lec info has changed, want to retry realtime_ invoice events
1125 my $error = $self->retry_realtime;
1127 $dbh->rollback if $oldAutoCommit;
1132 unless ( $import || $skip_fuzzyfiles ) {
1133 $error = $self->queue_fuzzyfiles_update;
1135 $dbh->rollback if $oldAutoCommit;
1136 return "updating fuzzy search cache: $error";
1140 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1145 =item queue_fuzzyfiles_update
1147 Used by insert & replace to update the fuzzy search cache
1151 sub queue_fuzzyfiles_update {
1154 local $SIG{HUP} = 'IGNORE';
1155 local $SIG{INT} = 'IGNORE';
1156 local $SIG{QUIT} = 'IGNORE';
1157 local $SIG{TERM} = 'IGNORE';
1158 local $SIG{TSTP} = 'IGNORE';
1159 local $SIG{PIPE} = 'IGNORE';
1161 my $oldAutoCommit = $FS::UID::AutoCommit;
1162 local $FS::UID::AutoCommit = 0;
1165 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1166 my $error = $queue->insert( map $self->getfield($_),
1167 qw(first last company)
1170 $dbh->rollback if $oldAutoCommit;
1171 return "queueing job (transaction rolled back): $error";
1174 if ( $self->ship_last ) {
1175 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1176 $error = $queue->insert( map $self->getfield("ship_$_"),
1177 qw(first last company)
1180 $dbh->rollback if $oldAutoCommit;
1181 return "queueing job (transaction rolled back): $error";
1185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1192 Checks all fields to make sure this is a valid customer record. If there is
1193 an error, returns the error, otherwise returns false. Called by the insert
1194 and replace methods.
1201 warn "$me check BEFORE: \n". $self->_dump
1205 $self->ut_numbern('custnum')
1206 || $self->ut_number('agentnum')
1207 || $self->ut_textn('agent_custid')
1208 || $self->ut_number('refnum')
1209 || $self->ut_name('last')
1210 || $self->ut_name('first')
1211 || $self->ut_snumbern('birthdate')
1212 || $self->ut_snumbern('signupdate')
1213 || $self->ut_textn('company')
1214 || $self->ut_text('address1')
1215 || $self->ut_textn('address2')
1216 || $self->ut_text('city')
1217 || $self->ut_textn('county')
1218 || $self->ut_textn('state')
1219 || $self->ut_country('country')
1220 || $self->ut_anything('comments')
1221 || $self->ut_numbern('referral_custnum')
1222 || $self->ut_textn('stateid')
1223 || $self->ut_textn('stateid_state')
1224 || $self->ut_textn('invoice_terms')
1226 #barf. need message catalogs. i18n. etc.
1227 $error .= "Please select an advertising source."
1228 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1229 return $error if $error;
1231 return "Unknown agent"
1232 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1234 return "Unknown refnum"
1235 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1237 return "Unknown referring custnum: ". $self->referral_custnum
1238 unless ! $self->referral_custnum
1239 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1241 if ( $self->ss eq '' ) {
1246 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1247 or return "Illegal social security number: ". $self->ss;
1248 $self->ss("$1-$2-$3");
1252 # bad idea to disable, causes billing to fail because of no tax rates later
1253 # unless ( $import ) {
1254 unless ( qsearch('cust_main_county', {
1255 'country' => $self->country,
1258 return "Unknown state/county/country: ".
1259 $self->state. "/". $self->county. "/". $self->country
1260 unless qsearch('cust_main_county',{
1261 'state' => $self->state,
1262 'county' => $self->county,
1263 'country' => $self->country,
1269 $self->ut_phonen('daytime', $self->country)
1270 || $self->ut_phonen('night', $self->country)
1271 || $self->ut_phonen('fax', $self->country)
1272 || $self->ut_zip('zip', $self->country)
1274 return $error if $error;
1277 last first company address1 address2 city county state zip
1278 country daytime night fax
1281 if ( defined $self->dbdef_table->column('ship_last') ) {
1282 if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1284 && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
1288 $self->ut_name('ship_last')
1289 || $self->ut_name('ship_first')
1290 || $self->ut_textn('ship_company')
1291 || $self->ut_text('ship_address1')
1292 || $self->ut_textn('ship_address2')
1293 || $self->ut_text('ship_city')
1294 || $self->ut_textn('ship_county')
1295 || $self->ut_textn('ship_state')
1296 || $self->ut_country('ship_country')
1298 return $error if $error;
1300 #false laziness with above
1301 unless ( qsearchs('cust_main_county', {
1302 'country' => $self->ship_country,
1305 return "Unknown ship_state/ship_county/ship_country: ".
1306 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1307 unless qsearch('cust_main_county',{
1308 'state' => $self->ship_state,
1309 'county' => $self->ship_county,
1310 'country' => $self->ship_country,
1316 $self->ut_phonen('ship_daytime', $self->ship_country)
1317 || $self->ut_phonen('ship_night', $self->ship_country)
1318 || $self->ut_phonen('ship_fax', $self->ship_country)
1319 || $self->ut_zip('ship_zip', $self->ship_country)
1321 return $error if $error;
1323 } else { # ship_ info eq billing info, so don't store dup info in database
1324 $self->setfield("ship_$_", '')
1325 foreach qw( last first company address1 address2 city county state zip
1326 country daytime night fax );
1330 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1331 # or return "Illegal payby: ". $self->payby;
1333 FS::payby->can_payby($self->table, $self->payby)
1334 or return "Illegal payby: ". $self->payby;
1336 $error = $self->ut_numbern('paystart_month')
1337 || $self->ut_numbern('paystart_year')
1338 || $self->ut_numbern('payissue')
1339 || $self->ut_textn('paytype')
1341 return $error if $error;
1343 if ( $self->payip eq '' ) {
1346 $error = $self->ut_ip('payip');
1347 return $error if $error;
1350 # If it is encrypted and the private key is not availaible then we can't
1351 # check the credit card.
1353 my $check_payinfo = 1;
1355 if ($self->is_encrypted($self->payinfo)) {
1359 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1361 my $payinfo = $self->payinfo;
1362 $payinfo =~ s/\D//g;
1363 $payinfo =~ /^(\d{13,16})$/
1364 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1366 $self->payinfo($payinfo);
1368 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1370 return gettext('unknown_card_type')
1371 if cardtype($self->payinfo) eq "Unknown";
1373 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1375 return 'Banned credit card: banned on '.
1376 time2str('%a %h %o at %r', $ban->_date).
1377 ' by '. $ban->otaker.
1378 ' (ban# '. $ban->bannum. ')';
1381 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1382 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1383 $self->paycvv =~ /^(\d{4})$/
1384 or return "CVV2 (CID) for American Express cards is four digits.";
1387 $self->paycvv =~ /^(\d{3})$/
1388 or return "CVV2 (CVC2/CID) is three digits.";
1395 my $cardtype = cardtype($payinfo);
1396 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1398 return "Start date or issue number is required for $cardtype cards"
1399 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1401 return "Start month must be between 1 and 12"
1402 if $self->paystart_month
1403 and $self->paystart_month < 1 || $self->paystart_month > 12;
1405 return "Start year must be 1990 or later"
1406 if $self->paystart_year
1407 and $self->paystart_year < 1990;
1409 return "Issue number must be beween 1 and 99"
1411 and $self->payissue < 1 || $self->payissue > 99;
1414 $self->paystart_month('');
1415 $self->paystart_year('');
1416 $self->payissue('');
1419 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1421 my $payinfo = $self->payinfo;
1422 $payinfo =~ s/[^\d\@]//g;
1423 if ( $conf->exists('echeck-nonus') ) {
1424 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1426 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1428 $payinfo = "$1\@$2";
1429 $self->payinfo($payinfo);
1432 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1434 return 'Banned ACH account: banned on '.
1435 time2str('%a %h %o at %r', $ban->_date).
1436 ' by '. $ban->otaker.
1437 ' (ban# '. $ban->bannum. ')';
1440 } elsif ( $self->payby eq 'LECB' ) {
1442 my $payinfo = $self->payinfo;
1443 $payinfo =~ s/\D//g;
1444 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1446 $self->payinfo($payinfo);
1449 } elsif ( $self->payby eq 'BILL' ) {
1451 $error = $self->ut_textn('payinfo');
1452 return "Illegal P.O. number: ". $self->payinfo if $error;
1455 } elsif ( $self->payby eq 'COMP' ) {
1457 my $curuser = $FS::CurrentUser::CurrentUser;
1458 if ( ! $self->custnum
1459 && ! $curuser->access_right('Complimentary customer')
1462 return "You are not permitted to create complimentary accounts."
1465 $error = $self->ut_textn('payinfo');
1466 return "Illegal comp account issuer: ". $self->payinfo if $error;
1469 } elsif ( $self->payby eq 'PREPAY' ) {
1471 my $payinfo = $self->payinfo;
1472 $payinfo =~ s/\W//g; #anything else would just confuse things
1473 $self->payinfo($payinfo);
1474 $error = $self->ut_alpha('payinfo');
1475 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1476 return "Unknown prepayment identifier"
1477 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1482 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1483 return "Expiration date required"
1484 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1488 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1489 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1490 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1491 ( $m, $y ) = ( $3, "20$2" );
1493 return "Illegal expiration date: ". $self->paydate;
1495 $self->paydate("$y-$m-01");
1496 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1497 return gettext('expired_card')
1499 && !$ignore_expired_card
1500 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1503 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1504 ( ! $conf->exists('require_cardname')
1505 || $self->payby !~ /^(CARD|DCRD)$/ )
1507 $self->payname( $self->first. " ". $self->getfield('last') );
1509 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1510 or return gettext('illegal_name'). " payname: ". $self->payname;
1514 foreach my $flag (qw( tax spool_cdr )) {
1515 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1519 $self->otaker(getotaker) unless $self->otaker;
1521 warn "$me check AFTER: \n". $self->_dump
1524 $self->SUPER::check;
1529 Returns all packages (see L<FS::cust_pkg>) for this customer.
1536 return $self->num_pkgs unless wantarray;
1539 if ( $self->{'_pkgnum'} ) {
1540 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1542 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1545 sort sort_packages @cust_pkg;
1550 Synonym for B<all_pkgs>.
1555 shift->all_pkgs(@_);
1558 =item ncancelled_pkgs
1560 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1564 sub ncancelled_pkgs {
1567 return $self->num_ncancelled_pkgs unless wantarray;
1570 if ( $self->{'_pkgnum'} ) {
1572 warn "$me ncancelled_pkgs: returning cached objects"
1575 @cust_pkg = grep { ! $_->getfield('cancel') }
1576 values %{ $self->{'_pkgnum'}->cache };
1580 warn "$me ncancelled_pkgs: searching for packages for custnum ".
1585 qsearch( 'cust_pkg', {
1586 'custnum' => $self->custnum,
1590 qsearch( 'cust_pkg', {
1591 'custnum' => $self->custnum,
1596 sort sort_packages @cust_pkg;
1600 # This should be generalized to use config options to determine order.
1602 if ( $a->get('cancel') and $b->get('cancel') ) {
1603 $a->pkgnum <=> $b->pkgnum;
1604 } elsif ( $a->get('cancel') or $b->get('cancel') ) {
1605 return -1 if $b->get('cancel');
1606 return 1 if $a->get('cancel');
1609 $a->pkgnum <=> $b->pkgnum;
1613 =item suspended_pkgs
1615 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1619 sub suspended_pkgs {
1621 grep { $_->susp } $self->ncancelled_pkgs;
1624 =item unflagged_suspended_pkgs
1626 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1627 customer (thouse packages without the `manual_flag' set).
1631 sub unflagged_suspended_pkgs {
1633 return $self->suspended_pkgs
1634 unless dbdef->table('cust_pkg')->column('manual_flag');
1635 grep { ! $_->manual_flag } $self->suspended_pkgs;
1638 =item unsuspended_pkgs
1640 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1645 sub unsuspended_pkgs {
1647 grep { ! $_->susp } $self->ncancelled_pkgs;
1650 =item num_cancelled_pkgs
1652 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1657 sub num_cancelled_pkgs {
1658 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
1661 sub num_ncancelled_pkgs {
1662 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
1666 my( $self, $sql ) = @_;
1667 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
1668 my $sth = dbh->prepare(
1669 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
1670 ) or die dbh->errstr;
1671 $sth->execute($self->custnum) or die $sth->errstr;
1672 $sth->fetchrow_arrayref->[0];
1677 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1678 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1679 on success or a list of errors.
1685 grep { $_->unsuspend } $self->suspended_pkgs;
1690 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1692 Returns a list: an empty list on success or a list of errors.
1698 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1701 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1703 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1704 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
1705 of a list of pkgparts; the hashref has the following keys:
1709 =item pkgparts - listref of pkgparts
1711 =item (other options are passed to the suspend method)
1716 Returns a list: an empty list on success or a list of errors.
1720 sub suspend_if_pkgpart {
1722 my (@pkgparts, %opt);
1723 if (ref($_[0]) eq 'HASH'){
1724 @pkgparts = @{$_[0]{pkgparts}};
1729 grep { $_->suspend(%opt) }
1730 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1731 $self->unsuspended_pkgs;
1734 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1736 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1737 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
1738 instead of a list of pkgparts; the hashref has the following keys:
1742 =item pkgparts - listref of pkgparts
1744 =item (other options are passed to the suspend method)
1748 Returns a list: an empty list on success or a list of errors.
1752 sub suspend_unless_pkgpart {
1754 my (@pkgparts, %opt);
1755 if (ref($_[0]) eq 'HASH'){
1756 @pkgparts = @{$_[0]{pkgparts}};
1761 grep { $_->suspend(%opt) }
1762 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1763 $self->unsuspended_pkgs;
1766 =item cancel [ OPTION => VALUE ... ]
1768 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1770 Available options are:
1774 =item quiet - can be set true to supress email cancellation notices.
1776 =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.
1778 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
1782 Always returns a list: an empty list on success or a list of errors.
1787 my( $self, %opt ) = @_;
1789 warn "$me cancel called on customer ". $self->custnum. " with options ".
1790 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
1793 return ( 'access denied' )
1794 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
1796 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1798 #should try decryption (we might have the private key)
1799 # and if not maybe queue a job for the server that does?
1800 return ( "Can't (yet) ban encrypted credit cards" )
1801 if $self->is_encrypted($self->payinfo);
1803 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1804 my $error = $ban->insert;
1805 return ( $error ) if $error;
1809 my @pkgs = $self->ncancelled_pkgs;
1811 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
1812 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
1815 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
1818 sub _banned_pay_hashref {
1829 'payby' => $payby2ban{$self->payby},
1830 'payinfo' => md5_base64($self->payinfo),
1831 #don't ever *search* on reason! #'reason' =>
1837 Returns all notes (see L<FS::cust_main_note>) for this customer.
1844 qsearch( 'cust_main_note',
1845 { 'custnum' => $self->custnum },
1847 'ORDER BY _DATE DESC'
1853 Returns the agent (see L<FS::agent>) for this customer.
1859 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1862 =item bill_and_collect
1864 Cancels and suspends any packages due, generates bills, applies payments and
1867 Warns on errors (Does not currently: If there is an error, returns the error, otherwise returns false.)
1869 Options are passed as name-value pairs. Currently available options are:
1873 =item time - 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:
1877 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1879 =item invoice_time - 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.
1881 =item check_freq - "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1883 =item resetup - if set true, re-charges setup fees.
1889 sub bill_and_collect {
1890 my( $self, %options ) = @_;
1896 #$^T not $options{time} because freeside-daily -d is for pre-printing invoices
1897 foreach my $cust_pkg (
1898 grep { $_->expire && $_->expire <= $^T } $self->ncancelled_pkgs
1900 my $error = $cust_pkg->cancel;
1901 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum.
1902 " for custnum ". $self->custnum. ": $error"
1910 #$^T not $options{time} because freeside-daily -d is for pre-printing invoices
1911 foreach my $cust_pkg (
1912 grep { ( $_->part_pkg->is_prepaid && $_->bill && $_->bill < $^T
1913 || $_->adjourn && $_->adjourn <= $^T
1917 $self->ncancelled_pkgs
1919 my $error = $cust_pkg->suspend;
1920 warn "Error suspending package ". $cust_pkg->pkgnum.
1921 " for custnum ". $self->custnum. ": $error"
1929 my $error = $self->bill( %options );
1930 warn "Error billing, custnum ". $self->custnum. ": $error" if $error;
1932 $self->apply_payments_and_credits;
1934 $error = $self->collect( %options );
1935 warn "Error collecting, custnum". $self->custnum. ": $error" if $error;
1941 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
1942 conjunction with the collect method by calling B<bill_and_collect>.
1944 If there is an error, returns the error, otherwise returns false.
1946 Options are passed as name-value pairs. Currently available options are:
1950 =item resetup - if set true, re-charges setup fees.
1952 =item time - 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:
1956 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1958 =item pkg_list - An array ref of specific packages (objects) to attempt billing, instead trying all of them.
1960 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
1962 =item invoice_time - 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.
1969 my( $self, %options ) = @_;
1970 return '' if $self->payby eq 'COMP';
1971 warn "$me bill customer ". $self->custnum. "\n"
1974 my $time = $options{'time'} || time;
1979 local $SIG{HUP} = 'IGNORE';
1980 local $SIG{INT} = 'IGNORE';
1981 local $SIG{QUIT} = 'IGNORE';
1982 local $SIG{TERM} = 'IGNORE';
1983 local $SIG{TSTP} = 'IGNORE';
1984 local $SIG{PIPE} = 'IGNORE';
1986 my $oldAutoCommit = $FS::UID::AutoCommit;
1987 local $FS::UID::AutoCommit = 0;
1990 $self->select_for_update; #mutex
1992 #create a new invoice
1993 #(we'll remove it later if it doesn't actually need to be generated [contains
1994 # no line items] and we're inside a transaciton so nothing else will see it)
1995 my $cust_bill = new FS::cust_bill ( {
1996 'custnum' => $self->custnum,
1997 '_date' => ( $options{'invoice_time'} || $time ),
1998 #'charged' => $charged,
2001 $error = $cust_bill->insert;
2003 $dbh->rollback if $oldAutoCommit;
2004 return "can't create invoice for customer #". $self->custnum. ": $error";
2006 my $invnum = $cust_bill->invnum;
2009 # find the packages which are due for billing, find out how much they are
2010 # & generate invoice database.
2013 my( $total_setup, $total_recur ) = ( 0, 0 );
2015 my @precommit_hooks = ();
2017 foreach my $cust_pkg (
2018 qsearch('cust_pkg', { 'custnum' => $self->custnum } )
2021 #NO!! next if $cust_pkg->cancel;
2022 next if $cust_pkg->getfield('cancel');
2024 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
2026 #? to avoid use of uninitialized value errors... ?
2027 $cust_pkg->setfield('bill', '')
2028 unless defined($cust_pkg->bill);
2030 my $part_pkg = $cust_pkg->part_pkg;
2032 my %hash = $cust_pkg->hash;
2033 my $old_cust_pkg = new FS::cust_pkg \%hash;
2042 if ( ! $cust_pkg->setup &&
2044 ( $conf->exists('disable_setup_suspended_pkgs') &&
2045 ! $cust_pkg->getfield('susp')
2046 ) || ! $conf->exists('disable_setup_suspended_pkgs')
2048 || $options{'resetup'}
2051 warn " bill setup\n" if $DEBUG > 1;
2053 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
2055 $dbh->rollback if $oldAutoCommit;
2056 return "$@ running calc_setup for $cust_pkg\n";
2059 $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
2063 # bill recurring fee
2068 if ( $part_pkg->getfield('freq') ne '0' &&
2069 ! $cust_pkg->getfield('susp') &&
2070 ( $cust_pkg->getfield('bill') || 0 ) <= $time
2073 # XXX should this be a package event? probably. events are called
2074 # at collection time at the moment, though...
2075 if ( $part_pkg->can('reset_usage') ) {
2076 warn " resetting usage counters" if $DEBUG > 1;
2077 $part_pkg->reset_usage($cust_pkg);
2080 warn " bill recur\n" if $DEBUG > 1;
2082 # XXX shared with $recur_prog
2083 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2085 #over two params! lets at least switch to a hashref for the rest...
2086 my %param = ( 'precommit_hooks' => \@precommit_hooks, );
2088 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
2090 $dbh->rollback if $oldAutoCommit;
2091 return "$@ running calc_recur for $cust_pkg\n";
2094 #change this bit to use Date::Manip? CAREFUL with timezones (see
2095 # mailing list archive)
2096 my ($sec,$min,$hour,$mday,$mon,$year) =
2097 (localtime($sdate) )[0,1,2,3,4,5];
2099 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
2100 # only for figuring next bill date, nothing else, so, reset $sdate again
2102 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2103 $cust_pkg->last_bill($sdate)
2104 if $cust_pkg->dbdef_table->column('last_bill');
2106 if ( $part_pkg->freq =~ /^\d+$/ ) {
2107 $mon += $part_pkg->freq;
2108 until ( $mon < 12 ) { $mon -= 12; $year++; }
2109 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
2111 $mday += $weeks * 7;
2112 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
2115 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
2119 $dbh->rollback if $oldAutoCommit;
2120 return "unparsable frequency: ". $part_pkg->freq;
2122 $cust_pkg->setfield('bill',
2123 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
2126 warn "\$setup is undefined" unless defined($setup);
2127 warn "\$recur is undefined" unless defined($recur);
2128 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2131 # If $cust_pkg has been modified, update it and create cust_bill_pkg records
2134 if ( $cust_pkg->modified ) { # hmmm.. and if the options are modified?
2136 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2139 $error=$cust_pkg->replace($old_cust_pkg,
2140 options => { $cust_pkg->options },
2142 if ( $error ) { #just in case
2143 $dbh->rollback if $oldAutoCommit;
2144 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
2147 $setup = sprintf( "%.2f", $setup );
2148 $recur = sprintf( "%.2f", $recur );
2149 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2150 $dbh->rollback if $oldAutoCommit;
2151 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2153 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2154 $dbh->rollback if $oldAutoCommit;
2155 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2158 if ( $setup != 0 || $recur != 0 ) {
2160 warn " charges (setup=$setup, recur=$recur); adding line items\n"
2162 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2163 'invnum' => $invnum,
2164 'pkgnum' => $cust_pkg->pkgnum,
2168 'edate' => $cust_pkg->bill,
2169 'details' => \@details,
2171 $error = $cust_bill_pkg->insert;
2173 $dbh->rollback if $oldAutoCommit;
2174 return "can't create invoice line item for invoice #$invnum: $error";
2176 $total_setup += $setup;
2177 $total_recur += $recur;
2183 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2186 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
2189 my %taxhash = map { $_ => $self->get("$prefix$_") }
2190 qw( state county country );
2192 $taxhash{'taxclass'} = $part_pkg->taxclass;
2194 my @taxes = qsearch( 'cust_main_county', \%taxhash );
2197 $taxhash{'taxclass'} = '';
2198 @taxes = qsearch( 'cust_main_county', \%taxhash );
2201 #one more try at a whole-country tax rate
2203 $taxhash{$_} = '' foreach qw( state county );
2204 @taxes = qsearch( 'cust_main_county', \%taxhash );
2207 # maybe eliminate this entirely, along with all the 0% records
2209 $dbh->rollback if $oldAutoCommit;
2211 "fatal: can't find tax rate for state/county/country/taxclass ".
2212 join('/', ( map $self->get("$prefix$_"),
2213 qw(state county country)
2215 $part_pkg->taxclass ). "\n";
2218 foreach my $tax ( @taxes ) {
2220 my $taxable_charged = 0;
2221 $taxable_charged += $setup
2222 unless $part_pkg->setuptax =~ /^Y$/i
2223 || $tax->setuptax =~ /^Y$/i;
2224 $taxable_charged += $recur
2225 unless $part_pkg->recurtax =~ /^Y$/i
2226 || $tax->recurtax =~ /^Y$/i;
2227 next unless $taxable_charged;
2229 if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
2230 #my ($mon,$year) = (localtime($sdate) )[4,5];
2231 my ($mon,$year) = (localtime( $sdate || $cust_bill->_date ) )[4,5];
2233 my $freq = $part_pkg->freq || 1;
2234 if ( $freq !~ /(\d+)$/ ) {
2235 $dbh->rollback if $oldAutoCommit;
2236 return "daily/weekly package definitions not (yet?)".
2237 " compatible with monthly tax exemptions";
2239 my $taxable_per_month =
2240 sprintf("%.2f", $taxable_charged / $freq );
2242 #call the whole thing off if this customer has any old
2243 #exemption records...
2244 my @cust_tax_exempt =
2245 qsearch( 'cust_tax_exempt' => { custnum=> $self->custnum } );
2246 if ( @cust_tax_exempt ) {
2247 $dbh->rollback if $oldAutoCommit;
2249 'this customer still has old-style tax exemption records; '.
2250 'run bin/fs-migrate-cust_tax_exempt?';
2253 foreach my $which_month ( 1 .. $freq ) {
2255 #maintain the new exemption table now
2258 FROM cust_tax_exempt_pkg
2259 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
2260 LEFT JOIN cust_bill USING ( invnum )
2266 my $sth = dbh->prepare($sql) or do {
2267 $dbh->rollback if $oldAutoCommit;
2268 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2276 $dbh->rollback if $oldAutoCommit;
2277 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2279 my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0;
2281 my $remaining_exemption =
2282 $tax->exempt_amount - $existing_exemption;
2283 if ( $remaining_exemption > 0 ) {
2284 my $addl = $remaining_exemption > $taxable_per_month
2285 ? $taxable_per_month
2286 : $remaining_exemption;
2287 $taxable_charged -= $addl;
2289 my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( {
2290 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2291 'taxnum' => $tax->taxnum,
2292 'year' => 1900+$year,
2294 'amount' => sprintf("%.2f", $addl ),
2296 $error = $cust_tax_exempt_pkg->insert;
2298 $dbh->rollback if $oldAutoCommit;
2299 return "fatal: can't insert cust_tax_exempt_pkg: $error";
2301 } # if $remaining_exemption > 0
2305 #until ( $mon < 12 ) { $mon -= 12; $year++; }
2306 until ( $mon < 13 ) { $mon -= 12; $year++; }
2308 } #foreach $which_month
2310 } #if $tax->exempt_amount
2312 $taxable_charged = sprintf( "%.2f", $taxable_charged);
2314 #$tax += $taxable_charged * $cust_main_county->tax / 100
2315 $tax{ $tax->taxname || 'Tax' } +=
2316 $taxable_charged * $tax->tax / 100
2318 } #foreach my $tax ( @taxes )
2320 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2322 } #if $setup != 0 || $recur != 0
2324 } #if $cust_pkg->modified
2326 } #foreach my $cust_pkg
2328 unless ( $cust_bill->cust_bill_pkg ) {
2329 $cust_bill->delete; #don't create an invoice w/o line items
2331 # XXX this seems to be broken
2332 #( DBD::Pg::st execute failed: ERROR: syntax error at or near "hcb" )
2333 # # get rid of our fake history too, waste of unecessary space
2334 # my $h_cleanup_query = q{
2335 # DELETE FROM h_cust_bill hcb
2336 # WHERE hcb.invnum = ?
2337 # AND NOT EXISTS ( SELECT 1 FROM cust_bill cb where cb.invnum = hcb.invnum )
2339 # my $h_sth = $dbh->prepare($h_cleanup_query);
2340 # $h_sth->execute($invnum);
2342 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2346 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
2348 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
2349 my $tax = sprintf("%.2f", $tax{$taxname} );
2350 $charged = sprintf( "%.2f", $charged+$tax );
2352 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2353 'invnum' => $invnum,
2359 'itemdesc' => $taxname,
2361 $error = $cust_bill_pkg->insert;
2363 $dbh->rollback if $oldAutoCommit;
2364 return "can't create invoice line item for invoice #$invnum: $error";
2366 $total_setup += $tax;
2370 $cust_bill->charged( sprintf( "%.2f", $total_setup + $total_recur ) );
2371 $error = $cust_bill->replace;
2373 $dbh->rollback if $oldAutoCommit;
2374 return "can't update charged for invoice #$invnum: $error";
2377 foreach my $hook ( @precommit_hooks ) {
2379 &{$hook}; #($self) ?
2382 $dbh->rollback if $oldAutoCommit;
2383 return "$@ running precommit hook $hook\n";
2387 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2391 =item collect OPTIONS
2393 (Attempt to) collect money for this customer's outstanding invoices (see
2394 L<FS::cust_bill>). Usually used after the bill method.
2396 Actions are now triggered by billing events; see L<FS::part_event> and the
2397 billing events web interface. Old-style invoice events (see
2398 L<FS::part_bill_event>) have been deprecated.
2400 If there is an error, returns the error, otherwise returns false.
2402 Options are passed as name-value pairs.
2404 Currently available options are:
2408 =item invoice_time - 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.
2410 =item retry - Retry card/echeck/LEC transactions even when not scheduled by invoice events.
2412 =item quiet - set true to surpress email card/ACH decline notices.
2414 =item check_freq - "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
2416 =item payby - allows for one time override of normal customer billing method
2421 my( $self, %options ) = @_;
2422 my $invoice_time = $options{'invoice_time'} || time;
2425 local $SIG{HUP} = 'IGNORE';
2426 local $SIG{INT} = 'IGNORE';
2427 local $SIG{QUIT} = 'IGNORE';
2428 local $SIG{TERM} = 'IGNORE';
2429 local $SIG{TSTP} = 'IGNORE';
2430 local $SIG{PIPE} = 'IGNORE';
2432 my $oldAutoCommit = $FS::UID::AutoCommit;
2433 local $FS::UID::AutoCommit = 0;
2436 $self->select_for_update; #mutex
2439 my $balance = $self->balance;
2440 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2443 if ( exists($options{'retry_card'}) ) {
2444 carp 'retry_card option passed to collect is deprecated; use retry';
2445 $options{'retry'} ||= $options{'retry_card'};
2447 if ( exists($options{'retry'}) && $options{'retry'} ) {
2448 my $error = $self->retry_realtime;
2450 $dbh->rollback if $oldAutoCommit;
2455 # false laziness w/pay_batch::import_results
2457 my $due_cust_event = $self->due_cust_event(
2458 'time' => $invoice_time,
2459 'check_freq' => $options{'check_freq'},
2461 unless( ref($due_cust_event) ) {
2462 $dbh->rollback if $oldAutoCommit;
2463 return $due_cust_event;
2466 foreach my $cust_event ( @$due_cust_event ) {
2470 #re-eval event conditions (a previous event could have changed things)
2471 next unless $cust_event->test_conditions( 'time' => $invoice_time );
2474 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2475 warn " running cust_event ". $cust_event->eventnum. "\n"
2479 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
2480 if ( my $error = $cust_event->do_event() ) {
2481 #XXX wtf is this? figure out a proper dealio with return value
2483 # gah, even with transactions.
2484 $dbh->commit if $oldAutoCommit; #well.
2491 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2496 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
2498 Inserts database records for and returns an ordered listref of new events due
2499 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
2500 events are due, an empty listref is returned. If there is an error, returns a
2501 scalar error message.
2503 To actually run the events, call each event's test_condition method, and if
2504 still true, call the event's do_event method.
2506 Options are passed as a hashref or as a list of name-value pairs. Available
2511 =item check_freq - 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.
2513 =item time - "Current time" for the events.
2515 =item debug - Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), or 3 (more information)
2517 =item eventtable - Only return events for the specified eventtable (by default, events of all eventtables are returned)
2519 =item objects - Explicitly pass the objects to be tested (typically used with eventtable).
2525 sub due_cust_event {
2527 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
2530 #my $DEBUG = $opt{'debug'}
2531 local($DEBUG) = $opt{'debug'}
2532 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
2534 warn "$me due_cust_event called with options ".
2535 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
2538 $opt{'time'} ||= time;
2540 local $SIG{HUP} = 'IGNORE';
2541 local $SIG{INT} = 'IGNORE';
2542 local $SIG{QUIT} = 'IGNORE';
2543 local $SIG{TERM} = 'IGNORE';
2544 local $SIG{TSTP} = 'IGNORE';
2545 local $SIG{PIPE} = 'IGNORE';
2547 my $oldAutoCommit = $FS::UID::AutoCommit;
2548 local $FS::UID::AutoCommit = 0;
2551 $self->select_for_update; #mutex
2554 # 1: find possible events (initial search)
2557 my @cust_event = ();
2559 my @eventtable = $opt{'eventtable'}
2560 ? ( $opt{'eventtable'} )
2561 : FS::part_event->eventtables_runorder;
2563 foreach my $eventtable ( @eventtable ) {
2566 if ( $opt{'objects'} ) {
2568 @objects = @{ $opt{'objects'} };
2572 #my @objects = $self->eventtable(); # sub cust_main { @{ [ $self ] }; }
2573 @objects = ( $eventtable eq 'cust_main' )
2575 : ( $self->$eventtable() );
2579 my @e_cust_event = ();
2581 my $cross = "CROSS JOIN $eventtable";
2582 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
2583 unless $eventtable eq 'cust_main';
2585 foreach my $object ( @objects ) {
2587 #this first search uses the condition_sql magic for optimization.
2588 #the more possible events we can eliminate in this step the better
2590 my $cross_where = '';
2591 my $pkey = $object->primary_key;
2592 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
2594 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
2596 FS::part_event_condition->where_conditions_sql( $eventtable,
2597 'time'=>$opt{'time'}
2599 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
2601 $extra_sql = "AND $extra_sql" if $extra_sql;
2603 #here is the agent virtualization
2604 $extra_sql .= " AND ( part_event.agentnum IS NULL
2605 OR part_event.agentnum = ". $self->agentnum. ' )';
2607 $extra_sql .= " $order";
2609 my @part_event = qsearch( {
2610 'select' => 'part_event.*',
2611 'table' => 'part_event',
2612 'addl_from' => "$cross $join",
2613 'hashref' => { 'check_freq' => ( $opt{'check_freq'} || '1d' ),
2614 'eventtable' => $eventtable,
2617 'extra_sql' => "AND $cross_where $extra_sql",
2621 my $pkey = $object->primary_key;
2622 warn " ". scalar(@part_event).
2623 " possible events found for $eventtable ". $object->$pkey(). "\n";
2626 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
2630 warn " ". scalar(@e_cust_event).
2631 " subtotal possible cust events found for $eventtable"
2634 push @cust_event, @e_cust_event;
2638 warn " ". scalar(@cust_event).
2639 " total possible cust events found in initial search\n"
2643 # 2: test conditions
2648 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
2649 'stats_hashref' => \%unsat ),
2652 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
2655 warn " invalid conditions not eliminated with condition_sql:\n".
2656 join('', map " $_: ".$unsat{$_}."\n", keys %unsat );
2662 foreach my $cust_event ( @cust_event ) {
2664 my $error = $cust_event->insert();
2666 $dbh->rollback if $oldAutoCommit;
2672 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2678 warn " returning events: ". Dumper(@cust_event). "\n"
2685 =item retry_realtime
2687 Schedules realtime / batch credit card / electronic check / LEC billing
2688 events for for retry. Useful if card information has changed or manual
2689 retry is desired. The 'collect' method must be called to actually retry
2692 Implementation details: For either this customer, or for each of this
2693 customer's open invoices, changes the status of the first "done" (with
2694 statustext error) realtime processing event to "failed".
2698 sub retry_realtime {
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 #a little false laziness w/due_cust_event (not too bad, really)
2714 my $join = FS::part_event_condition->join_conditions_sql;
2715 my $order = FS::part_event_condition->order_conditions_sql;
2717 #here is the agent virtualization
2718 my $agent_virt = " ( part_event.agentnum IS NULL
2719 OR part_event.agentnum = ". $self->agentnum. ' )';
2721 #XXX this shouldn't be hardcoded, actions should declare it...
2722 my @realtime_events = qw(
2723 cust_bill_realtime_card
2724 cust_bill_realtime_check
2725 cust_bill_realtime_lec
2729 my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
2734 my @cust_event = qsearchs({
2735 'table' => 'cust_event',
2736 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
2737 'hashref' => { 'status' => 'done' },
2738 'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
2739 " AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
2742 my %seen_invnum = ();
2743 foreach my $cust_event (@cust_event) {
2745 #max one for the customer, one for each open invoice
2746 my $cust_X = $cust_event->cust_X;
2747 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
2751 or $cust_event->part_event->eventtable eq 'cust_bill'
2754 my $error = $cust_event->retry;
2756 $dbh->rollback if $oldAutoCommit;
2757 return "error scheduling event for retry: $error";
2762 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2767 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2769 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2770 via a Business::OnlinePayment realtime gateway. See
2771 L<http://420.am/business-onlinepayment> for supported gateways.
2773 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2775 Available options are: I<description>, I<invnum>, I<quiet>
2777 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2778 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2779 if set, will override the value from the customer record.
2781 I<description> is a free-text field passed to the gateway. It defaults to
2782 "Internet services".
2784 If an I<invnum> is specified, this payment (if successful) is applied to the
2785 specified invoice. If you don't specify an I<invnum> you might want to
2786 call the B<apply_payments> method.
2788 I<quiet> can be set true to surpress email decline notices.
2790 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2795 my( $self, $method, $amount, %options ) = @_;
2797 warn "$me realtime_bop: $method $amount\n";
2798 warn " $_ => $options{$_}\n" foreach keys %options;
2801 $options{'description'} ||= 'Internet services';
2803 eval "use Business::OnlinePayment";
2806 my $payinfo = exists($options{'payinfo'})
2807 ? $options{'payinfo'}
2810 my %method2payby = (
2817 # check for banned credit card/ACH
2820 my $ban = qsearchs('banned_pay', {
2821 'payby' => $method2payby{$method},
2822 'payinfo' => md5_base64($payinfo),
2824 return "Banned credit card" if $ban;
2831 if ( $options{'invnum'} ) {
2832 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
2833 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
2835 map { $_->part_pkg->taxclass }
2837 map { $_->cust_pkg }
2838 $cust_bill->cust_bill_pkg;
2839 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
2840 #different taxclasses
2841 $taxclass = $taxclasses[0];
2845 #look for an agent gateway override first
2847 if ( $method eq 'CC' ) {
2848 $cardtype = cardtype($payinfo);
2849 } elsif ( $method eq 'ECHECK' ) {
2852 $cardtype = $method;
2856 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2857 cardtype => $cardtype,
2858 taxclass => $taxclass, } )
2859 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2861 taxclass => $taxclass, } )
2862 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2863 cardtype => $cardtype,
2865 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2867 taxclass => '', } );
2869 my $payment_gateway = '';
2870 my( $processor, $login, $password, $action, @bop_options );
2871 if ( $override ) { #use a payment gateway override
2873 $payment_gateway = $override->payment_gateway;
2875 $processor = $payment_gateway->gateway_module;
2876 $login = $payment_gateway->gateway_username;
2877 $password = $payment_gateway->gateway_password;
2878 $action = $payment_gateway->gateway_action;
2879 @bop_options = $payment_gateway->options;
2881 } else { #use the standard settings from the config
2883 ( $processor, $login, $password, $action, @bop_options ) =
2884 $self->default_payment_gateway($method);
2892 my $address = exists($options{'address1'})
2893 ? $options{'address1'}
2895 my $address2 = exists($options{'address2'})
2896 ? $options{'address2'}
2898 $address .= ", ". $address2 if length($address2);
2900 my $o_payname = exists($options{'payname'})
2901 ? $options{'payname'}
2903 my($payname, $payfirst, $paylast);
2904 if ( $o_payname && $method ne 'ECHECK' ) {
2905 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2906 or return "Illegal payname $payname";
2907 ($payfirst, $paylast) = ($1, $2);
2909 $payfirst = $self->getfield('first');
2910 $paylast = $self->getfield('last');
2911 $payname = "$payfirst $paylast";
2914 my @invoicing_list = $self->invoicing_list_emailonly;
2915 if ( $conf->exists('emailinvoiceautoalways')
2916 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
2917 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2918 push @invoicing_list, $self->all_emails;
2921 my $email = ($conf->exists('business-onlinepayment-email-override'))
2922 ? $conf->config('business-onlinepayment-email-override')
2923 : $invoicing_list[0];
2927 my $payip = exists($options{'payip'})
2930 $content{customer_ip} = $payip
2933 $content{invoice_number} = $options{'invnum'}
2934 if exists($options{'invnum'}) && length($options{'invnum'});
2937 if ( $method eq 'CC' ) {
2939 $content{card_number} = $payinfo;
2940 $paydate = exists($options{'paydate'})
2941 ? $options{'paydate'}
2943 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2944 $content{expiration} = "$2/$1";
2946 my $paycvv = exists($options{'paycvv'})
2947 ? $options{'paycvv'}
2949 $content{cvv2} = $self->paycvv
2952 my $paystart_month = exists($options{'paystart_month'})
2953 ? $options{'paystart_month'}
2954 : $self->paystart_month;
2956 my $paystart_year = exists($options{'paystart_year'})
2957 ? $options{'paystart_year'}
2958 : $self->paystart_year;
2960 $content{card_start} = "$paystart_month/$paystart_year"
2961 if $paystart_month && $paystart_year;
2963 my $payissue = exists($options{'payissue'})
2964 ? $options{'payissue'}
2966 $content{issue_number} = $payissue if $payissue;
2968 $content{recurring_billing} = 'YES'
2969 if qsearch('cust_pay', { 'custnum' => $self->custnum,
2971 'payinfo' => $payinfo,
2973 || qsearch('cust_pay', { 'custnum' => $self->custnum,
2975 'paymask' => $self->mask_payinfo('CARD', $payinfo),
2979 } elsif ( $method eq 'ECHECK' ) {
2980 ( $content{account_number}, $content{routing_code} ) =
2981 split('@', $payinfo);
2982 $content{bank_name} = $o_payname;
2983 $content{bank_state} = exists($options{'paystate'})
2984 ? $options{'paystate'}
2985 : $self->getfield('paystate');
2986 $content{account_type} = exists($options{'paytype'})
2987 ? uc($options{'paytype'}) || 'CHECKING'
2988 : uc($self->getfield('paytype')) || 'CHECKING';
2989 $content{account_name} = $payname;
2990 $content{customer_org} = $self->company ? 'B' : 'I';
2991 $content{state_id} = exists($options{'stateid'})
2992 ? $options{'stateid'}
2993 : $self->getfield('stateid');
2994 $content{state_id_state} = exists($options{'stateid_state'})
2995 ? $options{'stateid_state'}
2996 : $self->getfield('stateid_state');
2997 $content{customer_ssn} = exists($options{'ss'})
3000 } elsif ( $method eq 'LEC' ) {
3001 $content{phone} = $payinfo;
3005 # run transaction(s)
3008 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
3010 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
3011 $transaction->content(
3014 'password' => $password,
3015 'action' => $action1,
3016 'description' => $options{'description'},
3017 'amount' => $amount,
3018 #'invoice_number' => $options{'invnum'},
3019 'customer_id' => $self->custnum,
3020 'last_name' => $paylast,
3021 'first_name' => $payfirst,
3023 'address' => $address,
3024 'city' => ( exists($options{'city'})
3027 'state' => ( exists($options{'state'})
3030 'zip' => ( exists($options{'zip'})
3033 'country' => ( exists($options{'country'})
3034 ? $options{'country'}
3036 'referer' => 'http://cleanwhisker.420.am/',
3038 'phone' => $self->daytime || $self->night,
3041 $transaction->submit();
3043 if ( $transaction->is_success() && $action2 ) {
3044 my $auth = $transaction->authorization;
3045 my $ordernum = $transaction->can('order_number')
3046 ? $transaction->order_number
3050 new Business::OnlinePayment( $processor, @bop_options );
3057 password => $password,
3058 order_number => $ordernum,
3060 authorization => $auth,
3061 description => $options{'description'},
3064 foreach my $field (qw( authorization_source_code returned_ACI
3065 transaction_identifier validation_code
3066 transaction_sequence_num local_transaction_date
3067 local_transaction_time AVS_result_code )) {
3068 $capture{$field} = $transaction->$field() if $transaction->can($field);
3071 $capture->content( %capture );
3075 unless ( $capture->is_success ) {
3076 my $e = "Authorization successful but capture failed, custnum #".
3077 $self->custnum. ': '. $capture->result_code.
3078 ": ". $capture->error_message;
3086 # remove paycvv after initial transaction
3089 #false laziness w/misc/process/payment.cgi - check both to make sure working
3091 if ( defined $self->dbdef_table->column('paycvv')
3092 && length($self->paycvv)
3093 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
3095 my $error = $self->remove_cvv;
3097 warn "WARNING: error removing cvv: $error\n";
3105 if ( $transaction->is_success() ) {
3107 my %method2payby = (
3114 if ( $payment_gateway ) { # agent override
3115 $paybatch = $payment_gateway->gatewaynum. '-';
3118 $paybatch .= "$processor:". $transaction->authorization;
3120 $paybatch .= ':'. $transaction->order_number
3121 if $transaction->can('order_number')
3122 && length($transaction->order_number);
3124 my $cust_pay = new FS::cust_pay ( {
3125 'custnum' => $self->custnum,
3126 'invnum' => $options{'invnum'},
3129 'payby' => $method2payby{$method},
3130 'payinfo' => $payinfo,
3131 'paybatch' => $paybatch,
3132 'paydate' => $paydate,
3134 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
3136 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
3139 $cust_pay->invnum(''); #try again with no specific invnum
3140 my $error2 = $cust_pay->insert( $options{'manual'} ?
3141 ( 'manual' => 1 ) : ()
3144 # gah, even with transactions.
3145 my $e = 'WARNING: Card/ACH debited but database not updated - '.
3146 "error inserting payment ($processor): $error2".
3147 " (previously tried insert with invnum #$options{'invnum'}" .
3153 return ''; #no error
3157 my $perror = "$processor error: ". $transaction->error_message;
3159 unless ( $transaction->error_message ) {
3162 if ( $transaction->can('response_page') ) {
3164 'page' => ( $transaction->can('response_page')
3165 ? $transaction->response_page
3168 'code' => ( $transaction->can('response_code')
3169 ? $transaction->response_code
3172 'headers' => ( $transaction->can('response_headers')
3173 ? $transaction->response_headers
3179 "No additional debugging information available for $processor";
3182 $perror .= "No error_message returned from $processor -- ".
3183 ( ref($t_response) ? Dumper($t_response) : $t_response );
3187 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
3188 && $conf->exists('emaildecline')
3189 && grep { $_ ne 'POST' } $self->invoicing_list
3190 && ! grep { $transaction->error_message =~ /$_/ }
3191 $conf->config('emaildecline-exclude')
3193 my @templ = $conf->config('declinetemplate');
3194 my $template = new Text::Template (
3196 SOURCE => [ map "$_\n", @templ ],
3197 ) or return "($perror) can't create template: $Text::Template::ERROR";
3198 $template->compile()
3199 or return "($perror) can't compile template: $Text::Template::ERROR";
3201 my $templ_hash = { error => $transaction->error_message };
3203 my $error = send_email(
3204 'from' => $conf->config('invoice_from'),
3205 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
3206 'subject' => 'Your payment could not be processed',
3207 'body' => [ $template->fill_in(HASH => $templ_hash) ],
3210 $perror .= " (also received error sending decline notification: $error)"
3220 =item default_payment_gateway
3224 sub default_payment_gateway {
3225 my( $self, $method ) = @_;
3227 die "Real-time processing not enabled\n"
3228 unless $conf->exists('business-onlinepayment');
3231 my $bop_config = 'business-onlinepayment';
3232 $bop_config .= '-ach'
3233 if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
3234 my ( $processor, $login, $password, $action, @bop_options ) =
3235 $conf->config($bop_config);
3236 $action ||= 'normal authorization';
3237 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
3238 die "No real-time processor is enabled - ".
3239 "did you set the business-onlinepayment configuration value?\n"
3242 ( $processor, $login, $password, $action, @bop_options )
3247 Removes the I<paycvv> field from the database directly.
3249 If there is an error, returns the error, otherwise returns false.
3255 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
3256 or return dbh->errstr;
3257 $sth->execute($self->custnum)
3258 or return $sth->errstr;
3263 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
3265 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
3266 via a Business::OnlinePayment realtime gateway. See
3267 L<http://420.am/business-onlinepayment> for supported gateways.
3269 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3271 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
3273 Most gateways require a reference to an original payment transaction to refund,
3274 so you probably need to specify a I<paynum>.
3276 I<amount> defaults to the original amount of the payment if not specified.
3278 I<reason> specifies a reason for the refund.
3280 I<paydate> specifies the expiration date for a credit card overriding the
3281 value from the customer record or the payment record. Specified as yyyy-mm-dd
3283 Implementation note: If I<amount> is unspecified or equal to the amount of the
3284 orignal payment, first an attempt is made to "void" the transaction via
3285 the gateway (to cancel a not-yet settled transaction) and then if that fails,
3286 the normal attempt is made to "refund" ("credit") the transaction via the
3287 gateway is attempted.
3289 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3290 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3291 #if set, will override the value from the customer record.
3293 #If an I<invnum> is specified, this payment (if successful) is applied to the
3294 #specified invoice. If you don't specify an I<invnum> you might want to
3295 #call the B<apply_payments> method.
3299 #some false laziness w/realtime_bop, not enough to make it worth merging
3300 #but some useful small subs should be pulled out
3301 sub realtime_refund_bop {
3302 my( $self, $method, %options ) = @_;
3304 warn "$me realtime_refund_bop: $method refund\n";
3305 warn " $_ => $options{$_}\n" foreach keys %options;
3308 eval "use Business::OnlinePayment";
3312 # look up the original payment and optionally a gateway for that payment
3316 my $amount = $options{'amount'};
3318 my( $processor, $login, $password, @bop_options ) ;
3319 my( $auth, $order_number ) = ( '', '', '' );
3321 if ( $options{'paynum'} ) {
3323 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
3324 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
3325 or return "Unknown paynum $options{'paynum'}";
3326 $amount ||= $cust_pay->paid;
3328 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
3329 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
3330 $cust_pay->paybatch;
3331 my $gatewaynum = '';
3332 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
3334 if ( $gatewaynum ) { #gateway for the payment to be refunded
3336 my $payment_gateway =
3337 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
3338 die "payment gateway $gatewaynum not found"
3339 unless $payment_gateway;
3341 $processor = $payment_gateway->gateway_module;
3342 $login = $payment_gateway->gateway_username;
3343 $password = $payment_gateway->gateway_password;
3344 @bop_options = $payment_gateway->options;
3346 } else { #try the default gateway
3348 my( $conf_processor, $unused_action );
3349 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
3350 $self->default_payment_gateway($method);
3352 return "processor of payment $options{'paynum'} $processor does not".
3353 " match default processor $conf_processor"
3354 unless $processor eq $conf_processor;
3359 } else { # didn't specify a paynum, so look for agent gateway overrides
3360 # like a normal transaction
3363 if ( $method eq 'CC' ) {
3364 $cardtype = cardtype($self->payinfo);
3365 } elsif ( $method eq 'ECHECK' ) {
3368 $cardtype = $method;
3371 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3372 cardtype => $cardtype,
3374 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3376 taxclass => '', } );
3378 if ( $override ) { #use a payment gateway override
3380 my $payment_gateway = $override->payment_gateway;
3382 $processor = $payment_gateway->gateway_module;
3383 $login = $payment_gateway->gateway_username;
3384 $password = $payment_gateway->gateway_password;
3385 #$action = $payment_gateway->gateway_action;
3386 @bop_options = $payment_gateway->options;
3388 } else { #use the standard settings from the config
3391 ( $processor, $login, $password, $unused_action, @bop_options ) =
3392 $self->default_payment_gateway($method);
3397 return "neither amount nor paynum specified" unless $amount;
3402 'password' => $password,
3403 'order_number' => $order_number,
3404 'amount' => $amount,
3405 'referer' => 'http://cleanwhisker.420.am/',
3407 $content{authorization} = $auth
3408 if length($auth); #echeck/ACH transactions have an order # but no auth
3409 #(at least with authorize.net)
3411 my $disable_void_after;
3412 if ($conf->exists('disable_void_after')
3413 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
3414 $disable_void_after = $1;
3417 #first try void if applicable
3418 if ( $cust_pay && $cust_pay->paid == $amount
3420 ( not defined($disable_void_after) )
3421 || ( time < ($cust_pay->_date + $disable_void_after ) )
3424 warn " attempting void\n" if $DEBUG > 1;
3425 my $void = new Business::OnlinePayment( $processor, @bop_options );
3426 $void->content( 'action' => 'void', %content );
3428 if ( $void->is_success ) {
3429 my $error = $cust_pay->void($options{'reason'});
3431 # gah, even with transactions.
3432 my $e = 'WARNING: Card/ACH voided but database not updated - '.
3433 "error voiding payment: $error";
3437 warn " void successful\n" if $DEBUG > 1;
3442 warn " void unsuccessful, trying refund\n"
3446 my $address = $self->address1;
3447 $address .= ", ". $self->address2 if $self->address2;
3449 my($payname, $payfirst, $paylast);
3450 if ( $self->payname && $method ne 'ECHECK' ) {
3451 $payname = $self->payname;
3452 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3453 or return "Illegal payname $payname";
3454 ($payfirst, $paylast) = ($1, $2);
3456 $payfirst = $self->getfield('first');
3457 $paylast = $self->getfield('last');
3458 $payname = "$payfirst $paylast";
3461 my @invoicing_list = $self->invoicing_list_emailonly;
3462 if ( $conf->exists('emailinvoiceautoalways')
3463 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3464 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3465 push @invoicing_list, $self->all_emails;
3468 my $email = ($conf->exists('business-onlinepayment-email-override'))
3469 ? $conf->config('business-onlinepayment-email-override')
3470 : $invoicing_list[0];
3472 my $payip = exists($options{'payip'})
3475 $content{customer_ip} = $payip
3479 if ( $method eq 'CC' ) {
3482 $content{card_number} = $payinfo = $cust_pay->payinfo;
3483 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
3484 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
3485 ($content{expiration} = "$2/$1"); # where available
3487 $content{card_number} = $payinfo = $self->payinfo;
3488 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
3489 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3490 $content{expiration} = "$2/$1";
3493 } elsif ( $method eq 'ECHECK' ) {
3496 $payinfo = $cust_pay->payinfo;
3498 $payinfo = $self->payinfo;
3500 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
3501 $content{bank_name} = $self->payname;
3502 $content{account_type} = 'CHECKING';
3503 $content{account_name} = $payname;
3504 $content{customer_org} = $self->company ? 'B' : 'I';
3505 $content{customer_ssn} = $self->ss;
3506 } elsif ( $method eq 'LEC' ) {
3507 $content{phone} = $payinfo = $self->payinfo;
3511 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3512 my %sub_content = $refund->content(
3513 'action' => 'credit',
3514 'customer_id' => $self->custnum,
3515 'last_name' => $paylast,
3516 'first_name' => $payfirst,
3518 'address' => $address,
3519 'city' => $self->city,
3520 'state' => $self->state,
3521 'zip' => $self->zip,
3522 'country' => $self->country,
3524 'phone' => $self->daytime || $self->night,
3527 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3531 return "$processor error: ". $refund->error_message
3532 unless $refund->is_success();
3534 my %method2payby = (
3540 my $paybatch = "$processor:". $refund->authorization;
3541 $paybatch .= ':'. $refund->order_number
3542 if $refund->can('order_number') && $refund->order_number;
3544 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
3545 my @cust_bill_pay = $cust_pay->cust_bill_pay;
3546 last unless @cust_bill_pay;
3547 my $cust_bill_pay = pop @cust_bill_pay;
3548 my $error = $cust_bill_pay->delete;
3552 my $cust_refund = new FS::cust_refund ( {
3553 'custnum' => $self->custnum,
3554 'paynum' => $options{'paynum'},
3555 'refund' => $amount,
3557 'payby' => $method2payby{$method},
3558 'payinfo' => $payinfo,
3559 'paybatch' => $paybatch,
3560 'reason' => $options{'reason'} || 'card or ACH refund',
3562 my $error = $cust_refund->insert;
3564 $cust_refund->paynum(''); #try again with no specific paynum
3565 my $error2 = $cust_refund->insert;
3567 # gah, even with transactions.
3568 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
3569 "error inserting refund ($processor): $error2".
3570 " (previously tried insert with paynum #$options{'paynum'}" .
3581 =item batch_card OPTION => VALUE...
3583 Adds a payment for this invoice to the pending credit card batch (see
3584 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
3585 runs the payment using a realtime gateway.
3590 my ($self, %options) = @_;
3593 if (exists($options{amount})) {
3594 $amount = $options{amount};
3596 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
3598 return '' unless $amount > 0;
3600 my $invnum = delete $options{invnum};
3601 my $payby = $options{invnum} || $self->payby; #dubious
3603 if ($options{'realtime'}) {
3604 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
3610 my $oldAutoCommit = $FS::UID::AutoCommit;
3611 local $FS::UID::AutoCommit = 0;
3614 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
3615 or return "Cannot lock pay_batch: " . $dbh->errstr;
3619 'payby' => FS::payby->payby2payment($payby),
3622 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
3624 unless ( $pay_batch ) {
3625 $pay_batch = new FS::pay_batch \%pay_batch;
3626 my $error = $pay_batch->insert;
3628 $dbh->rollback if $oldAutoCommit;
3629 die "error creating new batch: $error\n";
3633 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
3634 'batchnum' => $pay_batch->batchnum,
3635 'custnum' => $self->custnum,
3638 foreach (qw( address1 address2 city state zip country payby payinfo paydate
3640 $options{$_} = '' unless exists($options{$_});
3643 my $cust_pay_batch = new FS::cust_pay_batch ( {
3644 'batchnum' => $pay_batch->batchnum,
3645 'invnum' => $invnum || 0, # is there a better value?
3646 # this field should be
3648 # cust_bill_pay_batch now
3649 'custnum' => $self->custnum,
3650 'last' => $self->getfield('last'),
3651 'first' => $self->getfield('first'),
3652 'address1' => $options{address1} || $self->address1,
3653 'address2' => $options{address2} || $self->address2,
3654 'city' => $options{city} || $self->city,
3655 'state' => $options{state} || $self->state,
3656 'zip' => $options{zip} || $self->zip,
3657 'country' => $options{country} || $self->country,
3658 'payby' => $options{payby} || $self->payby,
3659 'payinfo' => $options{payinfo} || $self->payinfo,
3660 'exp' => $options{paydate} || $self->paydate,
3661 'payname' => $options{payname} || $self->payname,
3662 'amount' => $amount, # consolidating
3665 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
3666 if $old_cust_pay_batch;
3669 if ($old_cust_pay_batch) {
3670 $error = $cust_pay_batch->replace($old_cust_pay_batch)
3672 $error = $cust_pay_batch->insert;
3676 $dbh->rollback if $oldAutoCommit;
3680 my $unapplied = $self->total_credited + $self->total_unapplied_payments + $self->in_transit_payments;
3681 foreach my $cust_bill ($self->open_cust_bill) {
3682 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
3683 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
3684 'invnum' => $cust_bill->invnum,
3685 'paybatchnum' => $cust_pay_batch->paybatchnum,
3686 'amount' => $cust_bill->owed,
3689 if ($unapplied >= $cust_bill_pay_batch->amount){
3690 $unapplied -= $cust_bill_pay_batch->amount;
3693 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
3694 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
3696 $error = $cust_bill_pay_batch->insert;
3698 $dbh->rollback if $oldAutoCommit;
3703 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3709 Returns the total owed for this customer on all invoices
3710 (see L<FS::cust_bill/owed>).
3716 $self->total_owed_date(2145859200); #12/31/2037
3719 =item total_owed_date TIME
3721 Returns the total owed for this customer on all invoices with date earlier than
3722 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
3723 see L<Time::Local> and L<Date::Parse> for conversion functions.
3727 sub total_owed_date {
3731 foreach my $cust_bill (
3732 grep { $_->_date <= $time }
3733 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3735 $total_bill += $cust_bill->owed;
3737 sprintf( "%.2f", $total_bill );
3740 =item apply_payments_and_credits
3742 Applies unapplied payments and credits.
3744 In most cases, this new method should be used in place of sequential
3745 apply_payments and apply_credits methods.
3747 If there is an error, returns the error, otherwise returns false.
3751 sub apply_payments_and_credits {
3754 local $SIG{HUP} = 'IGNORE';
3755 local $SIG{INT} = 'IGNORE';
3756 local $SIG{QUIT} = 'IGNORE';
3757 local $SIG{TERM} = 'IGNORE';
3758 local $SIG{TSTP} = 'IGNORE';
3759 local $SIG{PIPE} = 'IGNORE';
3761 my $oldAutoCommit = $FS::UID::AutoCommit;
3762 local $FS::UID::AutoCommit = 0;
3765 $self->select_for_update; #mutex
3767 foreach my $cust_bill ( $self->open_cust_bill ) {
3768 my $error = $cust_bill->apply_payments_and_credits;
3770 $dbh->rollback if $oldAutoCommit;
3771 return "Error applying: $error";
3775 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3780 =item apply_credits OPTION => VALUE ...
3782 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
3783 to outstanding invoice balances in chronological order (or reverse
3784 chronological order if the I<order> option is set to B<newest>) and returns the
3785 value of any remaining unapplied credits available for refund (see
3786 L<FS::cust_refund>).
3788 Dies if there is an error.
3796 local $SIG{HUP} = 'IGNORE';
3797 local $SIG{INT} = 'IGNORE';
3798 local $SIG{QUIT} = 'IGNORE';
3799 local $SIG{TERM} = 'IGNORE';
3800 local $SIG{TSTP} = 'IGNORE';
3801 local $SIG{PIPE} = 'IGNORE';
3803 my $oldAutoCommit = $FS::UID::AutoCommit;
3804 local $FS::UID::AutoCommit = 0;
3807 $self->select_for_update; #mutex
3809 unless ( $self->total_credited ) {
3810 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3814 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
3815 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
3817 my @invoices = $self->open_cust_bill;
3818 @invoices = sort { $b->_date <=> $a->_date } @invoices
3819 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
3822 foreach my $cust_bill ( @invoices ) {
3825 if ( !defined($credit) || $credit->credited == 0) {
3826 $credit = pop @credits or last;
3829 if ($cust_bill->owed >= $credit->credited) {
3830 $amount=$credit->credited;
3832 $amount=$cust_bill->owed;
3835 my $cust_credit_bill = new FS::cust_credit_bill ( {
3836 'crednum' => $credit->crednum,
3837 'invnum' => $cust_bill->invnum,
3838 'amount' => $amount,
3840 my $error = $cust_credit_bill->insert;
3842 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3846 redo if ($cust_bill->owed > 0);
3850 my $total_credited = $self->total_credited;
3852 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3854 return $total_credited;
3857 =item apply_payments
3859 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
3860 to outstanding invoice balances in chronological order.
3862 #and returns the value of any remaining unapplied payments.
3864 Dies if there is an error.
3868 sub apply_payments {
3871 local $SIG{HUP} = 'IGNORE';
3872 local $SIG{INT} = 'IGNORE';
3873 local $SIG{QUIT} = 'IGNORE';
3874 local $SIG{TERM} = 'IGNORE';
3875 local $SIG{TSTP} = 'IGNORE';
3876 local $SIG{PIPE} = 'IGNORE';
3878 my $oldAutoCommit = $FS::UID::AutoCommit;
3879 local $FS::UID::AutoCommit = 0;
3882 $self->select_for_update; #mutex
3886 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
3887 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
3889 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
3890 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
3894 foreach my $cust_bill ( @invoices ) {
3897 if ( !defined($payment) || $payment->unapplied == 0 ) {
3898 $payment = pop @payments or last;
3901 if ( $cust_bill->owed >= $payment->unapplied ) {
3902 $amount = $payment->unapplied;
3904 $amount = $cust_bill->owed;
3907 my $cust_bill_pay = new FS::cust_bill_pay ( {
3908 'paynum' => $payment->paynum,
3909 'invnum' => $cust_bill->invnum,
3910 'amount' => $amount,
3912 my $error = $cust_bill_pay->insert;
3914 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3918 redo if ( $cust_bill->owed > 0);
3922 my $total_unapplied_payments = $self->total_unapplied_payments;
3924 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3926 return $total_unapplied_payments;
3929 =item total_credited
3931 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3932 customer. See L<FS::cust_credit/credited>.
3936 sub total_credited {
3938 my $total_credit = 0;
3939 foreach my $cust_credit ( qsearch('cust_credit', {
3940 'custnum' => $self->custnum,
3942 $total_credit += $cust_credit->credited;
3944 sprintf( "%.2f", $total_credit );
3947 =item total_unapplied_payments
3949 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3950 See L<FS::cust_pay/unapplied>.
3954 sub total_unapplied_payments {
3956 my $total_unapplied = 0;
3957 foreach my $cust_pay ( qsearch('cust_pay', {
3958 'custnum' => $self->custnum,
3960 $total_unapplied += $cust_pay->unapplied;
3962 sprintf( "%.2f", $total_unapplied );
3965 =item total_unapplied_refunds
3967 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
3968 customer. See L<FS::cust_refund/unapplied>.
3972 sub total_unapplied_refunds {
3974 my $total_unapplied = 0;
3975 foreach my $cust_refund ( qsearch('cust_refund', {
3976 'custnum' => $self->custnum,
3978 $total_unapplied += $cust_refund->unapplied;
3980 sprintf( "%.2f", $total_unapplied );
3985 Returns the balance for this customer (total_owed plus total_unrefunded, minus
3986 total_credited minus total_unapplied_payments).
3994 + $self->total_unapplied_refunds
3995 - $self->total_credited
3996 - $self->total_unapplied_payments
4000 =item balance_date TIME
4002 Returns the balance for this customer, only considering invoices with date
4003 earlier than TIME (total_owed_date minus total_credited minus
4004 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
4005 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
4014 $self->total_owed_date($time)
4015 + $self->total_unapplied_refunds
4016 - $self->total_credited
4017 - $self->total_unapplied_payments
4021 =item in_transit_payments
4023 Returns the total of requests for payments for this customer pending in
4024 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
4028 sub in_transit_payments {
4030 my $in_transit_payments = 0;
4031 foreach my $pay_batch ( qsearch('pay_batch', {
4034 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
4035 'batchnum' => $pay_batch->batchnum,
4036 'custnum' => $self->custnum,
4038 $in_transit_payments += $cust_pay_batch->amount;
4041 sprintf( "%.2f", $in_transit_payments );
4044 =item paydate_monthyear
4046 Returns a two-element list consisting of the month and year of this customer's
4047 paydate (credit card expiration date for CARD customers)
4051 sub paydate_monthyear {
4053 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
4055 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
4062 =item invoicing_list [ ARRAYREF ]
4064 If an arguement is given, sets these email addresses as invoice recipients
4065 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
4066 (except as warnings), so use check_invoicing_list first.
4068 Returns a list of email addresses (with svcnum entries expanded).
4070 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
4071 check it without disturbing anything by passing nothing.
4073 This interface may change in the future.
4077 sub invoicing_list {
4078 my( $self, $arrayref ) = @_;
4081 my @cust_main_invoice;
4082 if ( $self->custnum ) {
4083 @cust_main_invoice =
4084 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4086 @cust_main_invoice = ();
4088 foreach my $cust_main_invoice ( @cust_main_invoice ) {
4089 #warn $cust_main_invoice->destnum;
4090 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
4091 #warn $cust_main_invoice->destnum;
4092 my $error = $cust_main_invoice->delete;
4093 warn $error if $error;
4096 if ( $self->custnum ) {
4097 @cust_main_invoice =
4098 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4100 @cust_main_invoice = ();
4102 my %seen = map { $_->address => 1 } @cust_main_invoice;
4103 foreach my $address ( @{$arrayref} ) {
4104 next if exists $seen{$address} && $seen{$address};
4105 $seen{$address} = 1;
4106 my $cust_main_invoice = new FS::cust_main_invoice ( {
4107 'custnum' => $self->custnum,
4110 my $error = $cust_main_invoice->insert;
4111 warn $error if $error;
4115 if ( $self->custnum ) {
4117 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4124 =item check_invoicing_list ARRAYREF
4126 Checks these arguements as valid input for the invoicing_list method. If there
4127 is an error, returns the error, otherwise returns false.
4131 sub check_invoicing_list {
4132 my( $self, $arrayref ) = @_;
4133 foreach my $address ( @{$arrayref} ) {
4135 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
4136 return 'Can\'t add FAX invoice destination with a blank FAX number.';
4139 my $cust_main_invoice = new FS::cust_main_invoice ( {
4140 'custnum' => $self->custnum,
4143 my $error = $self->custnum
4144 ? $cust_main_invoice->check
4145 : $cust_main_invoice->checkdest
4147 return $error if $error;
4152 =item set_default_invoicing_list
4154 Sets the invoicing list to all accounts associated with this customer,
4155 overwriting any previous invoicing list.
4159 sub set_default_invoicing_list {
4161 $self->invoicing_list($self->all_emails);
4166 Returns the email addresses of all accounts provisioned for this customer.
4173 foreach my $cust_pkg ( $self->all_pkgs ) {
4174 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
4176 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4177 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4179 $list{$_}=1 foreach map { $_->email } @svc_acct;
4184 =item invoicing_list_addpost
4186 Adds postal invoicing to this customer. If this customer is already configured
4187 to receive postal invoices, does nothing.
4191 sub invoicing_list_addpost {
4193 return if grep { $_ eq 'POST' } $self->invoicing_list;
4194 my @invoicing_list = $self->invoicing_list;
4195 push @invoicing_list, 'POST';
4196 $self->invoicing_list(\@invoicing_list);
4199 =item invoicing_list_emailonly
4201 Returns the list of email invoice recipients (invoicing_list without non-email
4202 destinations such as POST and FAX).
4206 sub invoicing_list_emailonly {
4208 warn "$me invoicing_list_emailonly called"
4210 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
4213 =item invoicing_list_emailonly_scalar
4215 Returns the list of email invoice recipients (invoicing_list without non-email
4216 destinations such as POST and FAX) as a comma-separated scalar.
4220 sub invoicing_list_emailonly_scalar {
4222 warn "$me invoicing_list_emailonly_scalar called"
4224 join(', ', $self->invoicing_list_emailonly);
4227 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
4229 Returns an array of customers referred by this customer (referral_custnum set
4230 to this custnum). If DEPTH is given, recurses up to the given depth, returning
4231 customers referred by customers referred by this customer and so on, inclusive.
4232 The default behavior is DEPTH 1 (no recursion).
4236 sub referral_cust_main {
4238 my $depth = @_ ? shift : 1;
4239 my $exclude = @_ ? shift : {};
4242 map { $exclude->{$_->custnum}++; $_; }
4243 grep { ! $exclude->{ $_->custnum } }
4244 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
4248 map { $_->referral_cust_main($depth-1, $exclude) }
4255 =item referral_cust_main_ncancelled
4257 Same as referral_cust_main, except only returns customers with uncancelled
4262 sub referral_cust_main_ncancelled {
4264 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
4267 =item referral_cust_pkg [ DEPTH ]
4269 Like referral_cust_main, except returns a flat list of all unsuspended (and
4270 uncancelled) packages for each customer. The number of items in this list may
4271 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
4275 sub referral_cust_pkg {
4277 my $depth = @_ ? shift : 1;
4279 map { $_->unsuspended_pkgs }
4280 grep { $_->unsuspended_pkgs }
4281 $self->referral_cust_main($depth);
4284 =item referring_cust_main
4286 Returns the single cust_main record for the customer who referred this customer
4287 (referral_custnum), or false.
4291 sub referring_cust_main {
4293 return '' unless $self->referral_custnum;
4294 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
4297 =item credit AMOUNT, REASON
4299 Applies a credit to this customer. If there is an error, returns the error,
4300 otherwise returns false.
4305 my( $self, $amount, $reason ) = @_;
4306 my $cust_credit = new FS::cust_credit {
4307 'custnum' => $self->custnum,
4308 'amount' => $amount,
4309 'reason' => $reason,
4311 $cust_credit->insert;
4314 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
4316 Creates a one-time charge for this customer. If there is an error, returns
4317 the error, otherwise returns false.
4323 my ( $amount, $pkg, $comment, $taxclass, $additional );
4324 if ( ref( $_[0] ) ) {
4325 $amount = $_[0]->{amount};
4326 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
4327 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
4328 : '$'. sprintf("%.2f",$amount);
4329 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
4330 $additional = $_[0]->{additional};
4333 $pkg = @_ ? shift : 'One-time charge';
4334 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
4335 $taxclass = @_ ? shift : '';
4339 local $SIG{HUP} = 'IGNORE';
4340 local $SIG{INT} = 'IGNORE';
4341 local $SIG{QUIT} = 'IGNORE';
4342 local $SIG{TERM} = 'IGNORE';
4343 local $SIG{TSTP} = 'IGNORE';
4344 local $SIG{PIPE} = 'IGNORE';
4346 my $oldAutoCommit = $FS::UID::AutoCommit;
4347 local $FS::UID::AutoCommit = 0;
4350 my $part_pkg = new FS::part_pkg ( {
4352 'comment' => $comment,
4356 'taxclass' => $taxclass,
4359 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
4360 ( 0 .. @$additional - 1 )
4362 'additional_count' => scalar(@$additional),
4363 'setup_fee' => $amount,
4366 my $error = $part_pkg->insert( options => \%options );
4368 $dbh->rollback if $oldAutoCommit;
4372 my $pkgpart = $part_pkg->pkgpart;
4373 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
4374 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
4375 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
4376 $error = $type_pkgs->insert;
4378 $dbh->rollback if $oldAutoCommit;
4383 my $cust_pkg = new FS::cust_pkg ( {
4384 'custnum' => $self->custnum,
4385 'pkgpart' => $pkgpart,
4388 $error = $cust_pkg->insert;
4390 $dbh->rollback if $oldAutoCommit;
4394 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4401 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4407 sort { $a->_date <=> $b->_date }
4408 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4411 =item open_cust_bill
4413 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4418 sub open_cust_bill {
4420 grep { $_->owed > 0 } $self->cust_bill;
4425 Returns all the credits (see L<FS::cust_credit>) for this customer.
4431 sort { $a->_date <=> $b->_date }
4432 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4437 Returns all the payments (see L<FS::cust_pay>) for this customer.
4443 sort { $a->_date <=> $b->_date }
4444 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
4449 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4455 sort { $a->_date <=> $b->_date }
4456 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4459 =item cust_pay_batch
4461 Returns all batched payments (see L<FS::cust_pay_void>) for this customer.
4465 sub cust_pay_batch {
4467 sort { $a->_date <=> $b->_date }
4468 qsearch( 'cust_pay_batch', { 'custnum' => $self->custnum } )
4473 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4479 sort { $a->_date <=> $b->_date }
4480 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4485 Returns a name string for this customer, either "Company (Last, First)" or
4492 my $name = $self->contact;
4493 $name = $self->company. " ($name)" if $self->company;
4499 Returns a name string for this (service/shipping) contact, either
4500 "Company (Last, First)" or "Last, First".
4506 if ( $self->get('ship_last') ) {
4507 my $name = $self->ship_contact;
4508 $name = $self->ship_company. " ($name)" if $self->ship_company;
4517 Returns this customer's full (billing) contact name only, "Last, First"
4523 $self->get('last'). ', '. $self->first;
4528 Returns this customer's full (shipping) contact name only, "Last, First"
4534 $self->get('ship_last')
4535 ? $self->get('ship_last'). ', '. $self->ship_first
4541 Returns this customer's full country name
4547 code2country($self->country);
4554 Returns a status string for this customer, currently:
4558 =item prospect - No packages have ever been ordered
4560 =item active - One or more recurring packages is active
4562 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
4564 =item suspended - All non-cancelled recurring packages are suspended
4566 =item cancelled - All recurring packages are cancelled
4572 sub status { shift->cust_status(@_); }
4576 for my $status (qw( prospect active inactive suspended cancelled )) {
4577 my $method = $status.'_sql';
4578 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
4579 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
4580 $sth->execute( ($self->custnum) x $numnum )
4581 or die "Error executing 'SELECT $sql': ". $sth->errstr;
4582 return $status if $sth->fetchrow_arrayref->[0];
4586 =item ucfirst_cust_status
4588 =item ucfirst_status
4590 Returns the status with the first character capitalized.
4594 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
4596 sub ucfirst_cust_status {
4598 ucfirst($self->cust_status);
4603 Returns a hex triplet color string for this customer's status.
4607 use vars qw(%statuscolor);
4608 tie my %statuscolor, 'Tie::IxHash',
4609 'prospect' => '7e0079', #'000000', #black? naw, purple
4610 'active' => '00CC00', #green
4611 'inactive' => '0000CC', #blue
4612 'suspended' => 'FF9900', #yellow
4613 'cancelled' => 'FF0000', #red
4616 sub statuscolor { shift->cust_statuscolor(@_); }
4618 sub cust_statuscolor {
4620 $statuscolor{$self->cust_status};
4625 =head1 CLASS METHODS
4631 Class method that returns the list of possible status strings for customers
4632 (see L<the status method|/status>). For example:
4634 @statuses = FS::cust_main->statuses();
4639 #my $self = shift; #could be class...
4645 Returns an SQL expression identifying prospective cust_main records (customers
4646 with no packages ever ordered)
4650 use vars qw($select_count_pkgs);
4651 $select_count_pkgs =
4652 "SELECT COUNT(*) FROM cust_pkg
4653 WHERE cust_pkg.custnum = cust_main.custnum";
4655 sub select_count_pkgs_sql {
4659 sub prospect_sql { "
4660 0 = ( $select_count_pkgs )
4665 Returns an SQL expression identifying active cust_main records (customers with
4666 no active recurring packages, but otherwise unsuspended/uncancelled).
4671 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
4677 Returns an SQL expression identifying inactive cust_main records (customers with
4678 active recurring packages).
4682 sub inactive_sql { "
4683 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4685 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
4691 Returns an SQL expression identifying suspended cust_main records.
4696 sub suspended_sql { susp_sql(@_); }
4698 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
4700 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4706 Returns an SQL expression identifying cancelled cust_main records.
4710 sub cancelled_sql { cancel_sql(@_); }
4713 my $recurring_sql = FS::cust_pkg->recurring_sql;
4714 #my $recurring_sql = "
4715 # '0' != ( select freq from part_pkg
4716 # where cust_pkg.pkgpart = part_pkg.pkgpart )
4720 0 < ( $select_count_pkgs )
4721 AND 0 = ( $select_count_pkgs AND $recurring_sql
4722 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4728 =item uncancelled_sql
4730 Returns an SQL expression identifying un-cancelled cust_main records.
4734 sub uncancelled_sql { uncancel_sql(@_); }
4735 sub uncancel_sql { "
4736 ( 0 < ( $select_count_pkgs
4737 AND ( cust_pkg.cancel IS NULL
4738 OR cust_pkg.cancel = 0
4741 OR 0 = ( $select_count_pkgs )
4747 Returns an SQL fragment to retreive the balance.
4752 COALESCE( ( SELECT SUM(charged) FROM cust_bill
4753 WHERE cust_bill.custnum = cust_main.custnum ), 0)
4754 - COALESCE( ( SELECT SUM(paid) FROM cust_pay
4755 WHERE cust_pay.custnum = cust_main.custnum ), 0)
4756 - COALESCE( ( SELECT SUM(amount) FROM cust_credit
4757 WHERE cust_credit.custnum = cust_main.custnum ), 0)
4758 + COALESCE( ( SELECT SUM(refund) FROM cust_refund
4759 WHERE cust_refund.custnum = cust_main.custnum ), 0)
4762 =item balance_date_sql TIME
4764 Returns an SQL fragment to retreive the balance for this customer, only
4765 considering invoices with date earlier than TIME. (total_owed_date minus total_credited minus
4766 total_unapplied_payments). TIME is specified as an SQL fragment or a numeric
4767 UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and
4768 L<Date::Parse> for conversion functions.
4772 sub balance_date_sql {
4773 my( $class, $time ) = @_;
4775 my $owed_sql = FS::cust_bill->owed_sql;
4776 my $unapp_refund_sql = FS::cust_refund->unapplied_sql;
4777 #my $unapp_credit_sql = FS::cust_credit->unapplied_sql;
4778 my $unapp_credit_sql = FS::cust_credit->credited_sql;
4779 my $unapp_pay_sql = FS::cust_pay->unapplied_sql;
4782 COALESCE( ( SELECT SUM($owed_sql) FROM cust_bill
4783 WHERE cust_bill.custnum = cust_main.custnum
4784 AND cust_bill._date <= $time )
4787 + COALESCE( ( SELECT SUM($unapp_refund_sql) FROM cust_refund
4788 WHERE cust_refund.custnum = cust_main.custnum )
4791 - COALESCE( ( SELECT SUM($unapp_credit_sql) FROM cust_credit
4792 WHERE cust_credit.custnum = cust_main.custnum )
4795 - COALESCE( ( SELECT SUM($unapp_pay_sql) FROM cust_pay
4796 WHERE cust_pay.custnum = cust_main.custnum )
4804 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
4806 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
4807 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
4808 appropriate ship_ field is also searched).
4810 Additional options are the same as FS::Record::qsearch
4815 my( $self, $fuzzy, $hash, @opt) = @_;
4820 check_and_rebuild_fuzzyfiles();
4821 foreach my $field ( keys %$fuzzy ) {
4823 my $all = $self->all_X($field);
4824 next unless scalar(@$all);
4827 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
4830 foreach ( keys %match ) {
4831 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
4832 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
4835 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
4838 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
4840 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
4848 Returns a masked version of the named field
4853 my ($self,$field) = @_;
4857 'x'x(length($self->getfield($field))-4).
4858 substr($self->getfield($field), (length($self->getfield($field))-4));
4868 =item smart_search OPTION => VALUE ...
4870 Accepts the following options: I<search>, the string to search for. The string
4871 will be searched for as a customer number, phone number, name or company name,
4872 as an exact, or, in some cases, a substring or fuzzy match (see the source code
4873 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
4874 skip fuzzy matching when an exact match is found.
4876 Any additional options are treated as an additional qualifier on the search
4879 Returns a (possibly empty) array of FS::cust_main objects.
4886 #here is the agent virtualization
4887 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
4891 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
4892 my $search = delete $options{'search'};
4893 ( my $alphanum_search = $search ) =~ s/\W//g;
4895 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
4897 #false laziness w/Record::ut_phone
4898 my $phonen = "$1-$2-$3";
4899 $phonen .= " x$4" if $4;
4901 push @cust_main, qsearch( {
4902 'table' => 'cust_main',
4903 'hashref' => { %options },
4904 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
4906 join(' OR ', map "$_ = '$phonen'",
4907 qw( daytime night fax
4908 ship_daytime ship_night ship_fax )
4911 " AND $agentnums_sql", #agent virtualization
4914 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
4915 #try looking for matches with extensions unless one was specified
4917 push @cust_main, qsearch( {
4918 'table' => 'cust_main',
4919 'hashref' => { %options },
4920 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
4922 join(' OR ', map "$_ LIKE '$phonen\%'",
4924 ship_daytime ship_night )
4927 " AND $agentnums_sql", #agent virtualization
4932 } elsif ( $search =~ /^\s*(\d+)\s*$/ ) { # customer # search
4934 push @cust_main, qsearch( {
4935 'table' => 'cust_main',
4936 'hashref' => { 'custnum' => $1, %options },
4937 'extra_sql' => " AND $agentnums_sql", #agent virtualization
4940 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
4942 my($company, $last, $first) = ( $1, $2, $3 );
4944 # "Company (Last, First)"
4945 #this is probably something a browser remembered,
4946 #so just do an exact search
4948 foreach my $prefix ( '', 'ship_' ) {
4949 push @cust_main, qsearch( {
4950 'table' => 'cust_main',
4951 'hashref' => { $prefix.'first' => $first,
4952 $prefix.'last' => $last,
4953 $prefix.'company' => $company,
4956 'extra_sql' => " AND $agentnums_sql",
4960 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
4961 # try (ship_){last,company}
4965 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
4966 # # full strings the browser remembers won't work
4967 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
4969 use Lingua::EN::NameParse;
4970 my $NameParse = new Lingua::EN::NameParse(
4972 allow_reversed => 1,
4975 my($last, $first) = ( '', '' );
4976 #maybe disable this too and just rely on NameParse?
4977 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
4979 ($last, $first) = ( $1, $2 );
4981 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
4982 } elsif ( ! $NameParse->parse($value) ) {
4984 my %name = $NameParse->components;
4985 $first = $name{'given_name_1'};
4986 $last = $name{'surname_1'};
4990 if ( $first && $last ) {
4992 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
4995 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
4997 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
4998 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
5001 push @cust_main, qsearch( {
5002 'table' => 'cust_main',
5003 'hashref' => \%options,
5004 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5007 # or it just be something that was typed in... (try that in a sec)
5011 my $q_value = dbh->quote($value);
5014 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5015 $sql .= " ( LOWER(last) = $q_value
5016 OR LOWER(company) = $q_value
5017 OR LOWER(ship_last) = $q_value
5018 OR LOWER(ship_company) = $q_value
5021 push @cust_main, qsearch( {
5022 'table' => 'cust_main',
5023 'hashref' => \%options,
5024 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5027 #no exact match, trying substring/fuzzy
5028 #always do substring & fuzzy (unless they're explicity config'ed off)
5029 #getting complaints searches are not returning enough
5030 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
5032 #still some false laziness w/ search/cust_main.cgi
5037 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
5038 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
5041 if ( $first && $last ) {
5044 { 'first' => { op=>'ILIKE', value=>"%$first%" },
5045 'last' => { op=>'ILIKE', value=>"%$last%" },
5047 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
5048 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
5055 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
5056 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
5060 foreach my $hashref ( @hashrefs ) {
5062 push @cust_main, qsearch( {
5063 'table' => 'cust_main',
5064 'hashref' => { %$hashref,
5067 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
5076 " AND $agentnums_sql", #extra_sql #agent virtualization
5079 if ( $first && $last ) {
5080 push @cust_main, FS::cust_main->fuzzy_search(
5081 { 'last' => $last, #fuzzy hashref
5082 'first' => $first }, #
5086 foreach my $field ( 'last', 'company' ) {
5088 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
5093 #eliminate duplicates
5095 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5103 =item check_and_rebuild_fuzzyfiles
5107 use vars qw(@fuzzyfields);
5108 @fuzzyfields = ( 'last', 'first', 'company' );
5110 sub check_and_rebuild_fuzzyfiles {
5111 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
5112 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
5115 =item rebuild_fuzzyfiles
5119 sub rebuild_fuzzyfiles {
5121 use Fcntl qw(:flock);
5123 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
5124 mkdir $dir, 0700 unless -d $dir;
5126 foreach my $fuzzy ( @fuzzyfields ) {
5128 open(LOCK,">>$dir/cust_main.$fuzzy")
5129 or die "can't open $dir/cust_main.$fuzzy: $!";
5131 or die "can't lock $dir/cust_main.$fuzzy: $!";
5133 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
5134 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
5136 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
5137 my $sth = dbh->prepare("SELECT $field FROM cust_main".
5138 " WHERE $field != '' AND $field IS NOT NULL");
5139 $sth->execute or die $sth->errstr;
5141 while ( my $row = $sth->fetchrow_arrayref ) {
5142 print CACHE $row->[0]. "\n";
5147 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
5149 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
5160 my( $self, $field ) = @_;
5161 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
5162 open(CACHE,"<$dir/cust_main.$field")
5163 or die "can't open $dir/cust_main.$field: $!";
5164 my @array = map { chomp; $_; } <CACHE>;
5169 =item append_fuzzyfiles LASTNAME COMPANY
5173 sub append_fuzzyfiles {
5174 #my( $first, $last, $company ) = @_;
5176 &check_and_rebuild_fuzzyfiles;
5178 use Fcntl qw(:flock);
5180 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
5182 foreach my $field (qw( first last company )) {
5187 open(CACHE,">>$dir/cust_main.$field")
5188 or die "can't open $dir/cust_main.$field: $!";
5189 flock(CACHE,LOCK_EX)
5190 or die "can't lock $dir/cust_main.$field: $!";
5192 print CACHE "$value\n";
5194 flock(CACHE,LOCK_UN)
5195 or die "can't unlock $dir/cust_main.$field: $!";
5210 #warn join('-',keys %$param);
5211 my $fh = $param->{filehandle};
5212 my $agentnum = $param->{agentnum};
5214 my $refnum = $param->{refnum};
5215 my $pkgpart = $param->{pkgpart};
5217 #my @fields = @{$param->{fields}};
5218 my $format = $param->{'format'};
5221 if ( $format eq 'simple' ) {
5222 @fields = qw( cust_pkg.setup dayphone first last
5223 address1 address2 city state zip comments );
5225 } elsif ( $format eq 'extended' ) {
5226 @fields = qw( agent_custid refnum
5227 last first address1 address2 city state zip country
5229 ship_last ship_first ship_address1 ship_address2
5230 ship_city ship_state ship_zip ship_country
5231 payinfo paycvv paydate
5234 svc_acct.username svc_acct._password
5238 die "unknown format $format";
5241 eval "use Text::CSV_XS;";
5244 my $csv = new Text::CSV_XS;
5251 local $SIG{HUP} = 'IGNORE';
5252 local $SIG{INT} = 'IGNORE';
5253 local $SIG{QUIT} = 'IGNORE';
5254 local $SIG{TERM} = 'IGNORE';
5255 local $SIG{TSTP} = 'IGNORE';
5256 local $SIG{PIPE} = 'IGNORE';
5258 my $oldAutoCommit = $FS::UID::AutoCommit;
5259 local $FS::UID::AutoCommit = 0;
5262 #while ( $columns = $csv->getline($fh) ) {
5264 while ( defined($line=<$fh>) ) {
5266 $csv->parse($line) or do {
5267 $dbh->rollback if $oldAutoCommit;
5268 return "can't parse: ". $csv->error_input();
5271 my @columns = $csv->fields();
5272 #warn join('-',@columns);
5275 agentnum => $agentnum,
5277 country => $conf->config('countrydefault') || 'US',
5278 payby => $payby, #default
5279 paydate => '12/2037', #default
5281 my $billtime = time;
5282 my %cust_pkg = ( pkgpart => $pkgpart );
5284 foreach my $field ( @fields ) {
5286 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
5288 #$cust_pkg{$1} = str2time( shift @$columns );
5289 if ( $1 eq 'pkgpart' ) {
5290 $cust_pkg{$1} = shift @columns;
5291 } elsif ( $1 eq 'setup' ) {
5292 $billtime = str2time(shift @columns);
5294 $cust_pkg{$1} = str2time( shift @columns );
5297 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
5299 $svc_acct{$1} = shift @columns;
5303 #refnum interception
5304 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
5306 my $referral = $columns[0];
5307 my %hash = ( 'referral' => $referral,
5308 'agentnum' => $agentnum,
5312 my $part_referral = qsearchs('part_referral', \%hash )
5313 || new FS::part_referral \%hash;
5315 unless ( $part_referral->refnum ) {
5316 my $error = $part_referral->insert;
5318 $dbh->rollback if $oldAutoCommit;
5319 return "can't auto-insert advertising source: $referral: $error";
5323 $columns[0] = $part_referral->refnum;
5326 #$cust_main{$field} = shift @$columns;
5327 $cust_main{$field} = shift @columns;
5331 $cust_main{'payby'} = 'CARD' if length($cust_main{'payinfo'});
5333 my $invoicing_list = $cust_main{'invoicing_list'}
5334 ? [ delete $cust_main{'invoicing_list'} ]
5337 my $cust_main = new FS::cust_main ( \%cust_main );
5340 tie my %hash, 'Tie::RefHash'; #this part is important
5342 if ( $cust_pkg{'pkgpart'} ) {
5343 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
5346 if ( $svc_acct{'username'} ) {
5347 my $part_pkg = $cust_pkg->part_pkg;
5348 unless ( $part_pkg ) {
5349 $dbh->rollback if $oldAutoCommit;
5350 return "unknown pkgnum ". $cust_pkg{'pkgpart'};
5352 $svc_acct{svcpart} = $part_pkg->svcpart( 'svc_acct' );
5353 push @svc_acct, new FS::svc_acct ( \%svc_acct )
5356 $hash{$cust_pkg} = \@svc_acct;
5359 my $error = $cust_main->insert( \%hash, $invoicing_list );
5362 $dbh->rollback if $oldAutoCommit;
5363 return "can't insert customer for $line: $error";
5366 if ( $format eq 'simple' ) {
5368 #false laziness w/bill.cgi
5369 $error = $cust_main->bill( 'time' => $billtime );
5371 $dbh->rollback if $oldAutoCommit;
5372 return "can't bill customer for $line: $error";
5375 $error = $cust_main->apply_payments_and_credits;
5377 $dbh->rollback if $oldAutoCommit;
5378 return "can't bill customer for $line: $error";
5381 $error = $cust_main->collect();
5383 $dbh->rollback if $oldAutoCommit;
5384 return "can't collect customer for $line: $error";
5392 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5394 return "Empty file!" unless $imported;
5406 #warn join('-',keys %$param);
5407 my $fh = $param->{filehandle};
5408 my @fields = @{$param->{fields}};
5410 eval "use Text::CSV_XS;";
5413 my $csv = new Text::CSV_XS;
5420 local $SIG{HUP} = 'IGNORE';
5421 local $SIG{INT} = 'IGNORE';
5422 local $SIG{QUIT} = 'IGNORE';
5423 local $SIG{TERM} = 'IGNORE';
5424 local $SIG{TSTP} = 'IGNORE';
5425 local $SIG{PIPE} = 'IGNORE';
5427 my $oldAutoCommit = $FS::UID::AutoCommit;
5428 local $FS::UID::AutoCommit = 0;
5431 #while ( $columns = $csv->getline($fh) ) {
5433 while ( defined($line=<$fh>) ) {
5435 $csv->parse($line) or do {
5436 $dbh->rollback if $oldAutoCommit;
5437 return "can't parse: ". $csv->error_input();
5440 my @columns = $csv->fields();
5441 #warn join('-',@columns);
5444 foreach my $field ( @fields ) {
5445 $row{$field} = shift @columns;
5448 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
5449 unless ( $cust_main ) {
5450 $dbh->rollback if $oldAutoCommit;
5451 return "unknown custnum $row{'custnum'}";
5454 if ( $row{'amount'} > 0 ) {
5455 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
5457 $dbh->rollback if $oldAutoCommit;
5461 } elsif ( $row{'amount'} < 0 ) {
5462 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
5465 $dbh->rollback if $oldAutoCommit;
5475 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5477 return "Empty file!" unless $imported;
5483 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5485 Sends a templated email notification to the customer (see L<Text::Template>).
5487 OPTIONS is a hash and may include
5489 I<from> - the email sender (default is invoice_from)
5491 I<to> - comma-separated scalar or arrayref of recipients
5492 (default is invoicing_list)
5494 I<subject> - The subject line of the sent email notification
5495 (default is "Notice from company_name")
5497 I<extra_fields> - a hashref of name/value pairs which will be substituted
5500 The following variables are vavailable in the template.
5502 I<$first> - the customer first name
5503 I<$last> - the customer last name
5504 I<$company> - the customer company
5505 I<$payby> - a description of the method of payment for the customer
5506 # would be nice to use FS::payby::shortname
5507 I<$payinfo> - the account information used to collect for this customer
5508 I<$expdate> - the expiration of the customer payment in seconds from epoch
5513 my ($customer, $template, %options) = @_;
5515 return unless $conf->exists($template);
5517 my $from = $conf->config('invoice_from') if $conf->exists('invoice_from');
5518 $from = $options{from} if exists($options{from});
5520 my $to = join(',', $customer->invoicing_list_emailonly);
5521 $to = $options{to} if exists($options{to});
5523 my $subject = "Notice from " . $conf->config('company_name')
5524 if $conf->exists('company_name');
5525 $subject = $options{subject} if exists($options{subject});
5527 my $notify_template = new Text::Template (TYPE => 'ARRAY',
5528 SOURCE => [ map "$_\n",
5529 $conf->config($template)]
5531 or die "can't create new Text::Template object: Text::Template::ERROR";
5532 $notify_template->compile()
5533 or die "can't compile template: Text::Template::ERROR";
5535 my $paydate = $customer->paydate;
5536 $FS::notify_template::_template::first = $customer->first;
5537 $FS::notify_template::_template::last = $customer->last;
5538 $FS::notify_template::_template::company = $customer->company;
5539 $FS::notify_template::_template::payinfo = $customer->mask_payinfo;
5540 my $payby = $customer->payby;
5541 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5542 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5544 #credit cards expire at the end of the month/year of their exp date
5545 if ($payby eq 'CARD' || $payby eq 'DCRD') {
5546 $FS::notify_template::_template::payby = 'credit card';
5547 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5548 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5550 }elsif ($payby eq 'COMP') {
5551 $FS::notify_template::_template::payby = 'complimentary account';
5553 $FS::notify_template::_template::payby = 'current method';
5555 $FS::notify_template::_template::expdate = $expire_time;
5557 for (keys %{$options{extra_fields}}){
5559 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
5562 send_email(from => $from,
5564 subject => $subject,
5565 body => $notify_template->fill_in( PACKAGE =>
5566 'FS::notify_template::_template' ),
5571 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5573 Generates a templated notification to the customer (see L<Text::Template>).
5575 OPTIONS is a hash and may include
5577 I<extra_fields> - a hashref of name/value pairs which will be substituted
5578 into the template. These values may override values mentioned below
5579 and those from the customer record.
5581 The following variables are available in the template instead of or in addition
5582 to the fields of the customer record.
5584 I<$payby> - a description of the method of payment for the customer
5585 # would be nice to use FS::payby::shortname
5586 I<$payinfo> - the masked account information used to collect for this customer
5587 I<$expdate> - the expiration of the customer payment method in seconds from epoch
5588 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress
5592 sub generate_letter {
5593 my ($self, $template, %options) = @_;
5595 return unless $conf->exists($template);
5597 my $letter_template = new Text::Template
5599 SOURCE => [ map "$_\n", $conf->config($template)],
5600 DELIMITERS => [ '[@--', '--@]' ],
5602 or die "can't create new Text::Template object: Text::Template::ERROR";
5604 $letter_template->compile()
5605 or die "can't compile template: Text::Template::ERROR";
5607 my %letter_data = map { $_ => $self->$_ } $self->fields;
5608 $letter_data{payinfo} = $self->mask_payinfo;
5610 my $paydate = $self->paydate;
5611 my $payby = $self->payby;
5612 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5613 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5615 #credit cards expire at the end of the month/year of their exp date
5616 if ($payby eq 'CARD' || $payby eq 'DCRD') {
5617 $letter_data{payby} = 'credit card';
5618 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5619 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5621 }elsif ($payby eq 'COMP') {
5622 $letter_data{payby} = 'complimentary account';
5624 $letter_data{payby} = 'current method';
5626 $letter_data{expdate} = $expire_time;
5628 for (keys %{$options{extra_fields}}){
5629 $letter_data{$_} = $options{extra_fields}->{$_};
5632 unless(exists($letter_data{returnaddress})){
5633 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
5634 $self->agent_template)
5637 $letter_data{returnaddress} = length($retadd) ? $retadd : '~';
5640 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
5642 my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
5643 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
5647 ) or die "can't open temp file: $!\n";
5649 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
5651 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
5655 =item print_ps TEMPLATE
5657 Returns an postscript letter filled in from TEMPLATE, as a scalar.
5663 my $file = $self->generate_letter(@_);
5664 FS::Misc::generate_ps($file);
5667 =item print TEMPLATE
5669 Prints the filled in template.
5671 TEMPLATE is the name of a L<Text::Template> to fill in and print.
5675 sub queueable_print {
5678 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
5679 or die "invalid customer number: " . $opt{custvnum};
5681 my $error = $self->print( $opt{template} );
5682 die $error if $error;
5686 my ($self, $template) = (shift, shift);
5687 do_print [ $self->print_ps($template) ];
5690 sub agent_template {
5692 $self->_agent_plandata('agent_templatename');
5695 sub agent_invoice_from {
5697 $self->_agent_plandata('agent_invoice_from');
5700 sub _agent_plandata {
5701 my( $self, $option ) = @_;
5703 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
5704 #agent-specific Conf
5706 my $agentnum = $self->agentnum;
5708 my $part_event_option =
5710 'table' => 'part_event_option',
5712 LEFT JOIN part_event USING ( eventpart )
5713 LEFT JOIN part_event_option AS peo_agentnum
5714 ON ( part_event.eventpart = peo_agentnum.eventpart
5715 AND peo_agentnum.optionname = 'agentnum'
5716 AND peo_agentnum.optionvalue ~ '(^|,)agentnum(,|$)'
5718 LEFT JOIN part_event_option AS peo_cust_bill_age
5719 ON ( part_event.eventpart = peo_cust_bill_age.eventpart
5720 AND peo_cust_bill_age.optionname = 'cust_bill_age'
5723 #'hashref' => { 'optionname' => $option },
5724 'hashref' => { 'part_event_option.optionname' => $option },
5725 'extra_sql' => " AND event = 'cust_bill_send_agent' ".
5726 " AND peo_agentnum.optionname = 'agentnum' ".
5727 " AND agentnum IS NULL OR agentnum = $agentnum ".
5729 CASE WHEN peo_cust_bill_age.optionname != 'cust_bill_age'
5731 ELSE EXTRACT( EPOCH FROM
5732 REPLACE( peo_cust_bill_age.optionvalue,
5738 , part_event.weight".
5742 unless ( $part_event_option ) {
5743 return $self->agent->invoice_template || ''
5744 if $option eq '$agent_templatename';
5748 $part_event_option->optionvalue;
5758 The delete method should possibly take an FS::cust_main object reference
5759 instead of a scalar customer number.
5761 Bill and collect options should probably be passed as references instead of a
5764 There should probably be a configuration file with a list of allowed credit
5767 No multiple currency support (probably a larger project than just this module).
5769 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
5771 Birthdates rely on negative epoch values.
5773 The payby for card/check batches is broken. With mixed batching, bad
5776 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
5780 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
5781 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
5782 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.