5 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
6 $import $skip_fuzzyfiles $ignore_expired_card @paytypes);
7 use vars qw( $realtime_bop_decline_quiet ); #ugh
11 use Scalar::Util qw( blessed );
12 use Time::Local qw(timelocal_nocheck);
15 use Digest::MD5 qw(md5_base64);
19 use String::Approx qw(amatch);
20 use Business::CreditCard 0.28;
22 use FS::UID qw( getotaker dbh driver_name );
23 use FS::Record qw( qsearchs qsearch dbdef );
24 use FS::Misc qw( generate_email send_email generate_ps do_print );
25 use FS::Msgcat qw(gettext);
29 use FS::cust_bill_pkg;
31 use FS::cust_pay_pending;
32 use FS::cust_pay_void;
33 use FS::cust_pay_batch;
36 use FS::part_referral;
37 use FS::cust_main_county;
39 use FS::cust_main_invoice;
40 use FS::cust_credit_bill;
41 use FS::cust_bill_pay;
42 use FS::prepay_credit;
46 use FS::part_event_condition;
49 use FS::payment_gateway;
50 use FS::agent_payment_gateway;
52 use FS::payinfo_Mixin;
55 @ISA = qw( FS::payinfo_Mixin FS::Record );
57 @EXPORT_OK = qw( smart_search );
59 $realtime_bop_decline_quiet = 0;
61 # 1 is mostly method/subroutine entry and options
62 # 2 traces progress of some operations
63 # 3 is even more information including possibly sensitive data
65 $me = '[FS::cust_main]';
69 $ignore_expired_card = 0;
71 @encrypted_fields = ('payinfo', 'paycvv');
72 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
74 #ask FS::UID to run this stuff for us later
75 #$FS::UID::callback{'FS::cust_main'} = sub {
76 install_callback FS::UID sub {
78 #yes, need it for stuff below (prolly should be cached)
83 my ( $hashref, $cache ) = @_;
84 if ( exists $hashref->{'pkgnum'} ) {
85 #@{ $self->{'_pkgnum'} } = ();
86 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
87 $self->{'_pkgnum'} = $subcache;
88 #push @{ $self->{'_pkgnum'} },
89 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
95 FS::cust_main - Object methods for cust_main records
101 $record = new FS::cust_main \%hash;
102 $record = new FS::cust_main { 'column' => 'value' };
104 $error = $record->insert;
106 $error = $new_record->replace($old_record);
108 $error = $record->delete;
110 $error = $record->check;
112 @cust_pkg = $record->all_pkgs;
114 @cust_pkg = $record->ncancelled_pkgs;
116 @cust_pkg = $record->suspended_pkgs;
118 $error = $record->bill;
119 $error = $record->bill %options;
120 $error = $record->bill 'time' => $time;
122 $error = $record->collect;
123 $error = $record->collect %options;
124 $error = $record->collect 'invoice_time' => $time,
129 An FS::cust_main object represents a customer. FS::cust_main inherits from
130 FS::Record. The following fields are currently supported:
134 =item custnum - primary key (assigned automatically for new customers)
136 =item agentnum - agent (see L<FS::agent>)
138 =item refnum - Advertising source (see L<FS::part_referral>)
144 =item ss - social security number (optional)
146 =item company - (optional)
150 =item address2 - (optional)
154 =item county - (optional, see L<FS::cust_main_county>)
156 =item state - (see L<FS::cust_main_county>)
160 =item country - (see L<FS::cust_main_county>)
162 =item daytime - phone (optional)
164 =item night - phone (optional)
166 =item fax - phone (optional)
168 =item ship_first - name
170 =item ship_last - name
172 =item ship_company - (optional)
176 =item ship_address2 - (optional)
180 =item ship_county - (optional, see L<FS::cust_main_county>)
182 =item ship_state - (see L<FS::cust_main_county>)
186 =item ship_country - (see L<FS::cust_main_county>)
188 =item ship_daytime - phone (optional)
190 =item ship_night - phone (optional)
192 =item ship_fax - phone (optional)
194 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
196 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
198 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
202 Card Verification Value, "CVV2" (also known as CVC2 or CID), the 3 or 4 digit number on the back (or front, for American Express) of the credit card
204 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
206 =item paystart_month - start date month (maestro/solo cards only)
208 =item paystart_year - start date year (maestro/solo cards only)
210 =item payissue - issue number (maestro/solo cards only)
212 =item payname - name on card or billing name
214 =item payip - IP address from which payment information was received
216 =item tax - tax exempt, empty or `Y'
218 =item otaker - order taker (assigned automatically, see L<FS::UID>)
220 =item comments - comments (optional)
222 =item referral_custnum - referring customer number
224 =item spool_cdr - Enable individual CDR spooling, empty or `Y'
226 =item squelch_cdr - Discourage individual CDR printing, empty or `Y'
236 Creates a new customer. To add the customer to the database, see L<"insert">.
238 Note that this stores the hash reference, not a distinct copy of the hash it
239 points to. You can ask the object for a copy with the I<hash> method.
243 sub table { 'cust_main'; }
245 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
247 Adds this customer to the database. If there is an error, returns the error,
248 otherwise returns false.
250 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
251 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
252 are inserted atomicly, or the transaction is rolled back. Passing an empty
253 hash reference is equivalent to not supplying this parameter. There should be
254 a better explanation of this, but until then, here's an example:
257 tie %hash, 'Tie::RefHash'; #this part is important
259 $cust_pkg => [ $svc_acct ],
262 $cust_main->insert( \%hash );
264 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
265 be set as the invoicing list (see L<"invoicing_list">). Errors return as
266 expected and rollback the entire transaction; it is not necessary to call
267 check_invoicing_list first. The invoicing_list is set after the records in the
268 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
269 invoicing_list destination to the newly-created svc_acct. Here's an example:
271 $cust_main->insert( {}, [ $email, 'POST' ] );
273 Currently available options are: I<depend_jobnum> and I<noexport>.
275 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
276 on the supplied jobnum (they will not run until the specific job completes).
277 This can be used to defer provisioning until some action completes (such
278 as running the customer's credit card successfully).
280 The I<noexport> option is deprecated. If I<noexport> is set true, no
281 provisioning jobs (exports) are scheduled. (You can schedule them later with
282 the B<reexport> method.)
288 my $cust_pkgs = @_ ? shift : {};
289 my $invoicing_list = @_ ? shift : '';
291 warn "$me insert called with options ".
292 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
295 local $SIG{HUP} = 'IGNORE';
296 local $SIG{INT} = 'IGNORE';
297 local $SIG{QUIT} = 'IGNORE';
298 local $SIG{TERM} = 'IGNORE';
299 local $SIG{TSTP} = 'IGNORE';
300 local $SIG{PIPE} = 'IGNORE';
302 my $oldAutoCommit = $FS::UID::AutoCommit;
303 local $FS::UID::AutoCommit = 0;
306 my $prepay_identifier = '';
307 my( $amount, $seconds ) = ( 0, 0 );
309 if ( $self->payby eq 'PREPAY' ) {
311 $self->payby('BILL');
312 $prepay_identifier = $self->payinfo;
315 warn " looking up prepaid card $prepay_identifier\n"
318 my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
320 $dbh->rollback if $oldAutoCommit;
321 #return "error applying prepaid card (transaction rolled back): $error";
325 $payby = 'PREP' if $amount;
327 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
330 $self->payby('BILL');
331 $amount = $self->paid;
335 warn " inserting $self\n"
338 $self->signupdate(time) unless $self->signupdate;
340 my $error = $self->SUPER::insert;
342 $dbh->rollback if $oldAutoCommit;
343 #return "inserting cust_main record (transaction rolled back): $error";
347 warn " setting invoicing list\n"
350 if ( $invoicing_list ) {
351 $error = $self->check_invoicing_list( $invoicing_list );
353 $dbh->rollback if $oldAutoCommit;
354 #return "checking invoicing_list (transaction rolled back): $error";
357 $self->invoicing_list( $invoicing_list );
360 if ( $conf->config('cust_main-skeleton_tables')
361 && $conf->config('cust_main-skeleton_custnum') ) {
363 warn " inserting skeleton records\n"
366 my $error = $self->start_copy_skel;
368 $dbh->rollback if $oldAutoCommit;
374 warn " ordering packages\n"
377 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
379 $dbh->rollback if $oldAutoCommit;
384 $dbh->rollback if $oldAutoCommit;
385 return "No svc_acct record to apply pre-paid time";
389 warn " inserting initial $payby payment of $amount\n"
391 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
393 $dbh->rollback if $oldAutoCommit;
394 return "inserting payment (transaction rolled back): $error";
398 unless ( $import || $skip_fuzzyfiles ) {
399 warn " queueing fuzzyfiles update\n"
401 $error = $self->queue_fuzzyfiles_update;
403 $dbh->rollback if $oldAutoCommit;
404 return "updating fuzzy search cache: $error";
408 warn " insert complete; committing transaction\n"
411 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
416 sub start_copy_skel {
419 #'mg_user_preference' => {},
420 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
421 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
422 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
423 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
424 my @tables = eval(join('\n',$conf->config('cust_main-skeleton_tables')));
427 _copy_skel( 'cust_main', #tablename
428 $conf->config('cust_main-skeleton_custnum'), #sourceid
429 $self->custnum, #destid
430 @tables, #child tables
434 #recursive subroutine, not a method
436 my( $table, $sourceid, $destid, %child_tables ) = @_;
439 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
440 ( $table, $primary_key ) = ( $1, $2 );
442 my $dbdef_table = dbdef->table($table);
443 $primary_key = $dbdef_table->primary_key
444 or return "$table has no primary key".
445 " (or do you need to run dbdef-create?)";
448 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
449 join (', ', keys %child_tables). "\n"
452 foreach my $child_table_def ( keys %child_tables ) {
456 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
457 ( $child_table, $child_pkey ) = ( $1, $2 );
459 $child_table = $child_table_def;
461 $child_pkey = dbdef->table($child_table)->primary_key;
462 # or return "$table has no primary key".
463 # " (or do you need to run dbdef-create?)\n";
467 if ( keys %{ $child_tables{$child_table_def} } ) {
469 return "$child_table has no primary key".
470 " (run dbdef-create or try specifying it?)\n"
473 #false laziness w/Record::insert and only works on Pg
474 #refactor the proper last-inserted-id stuff out of Record::insert if this
475 # ever gets use for anything besides a quick kludge for one customer
476 my $default = dbdef->table($child_table)->column($child_pkey)->default;
477 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
478 or return "can't parse $child_table.$child_pkey default value ".
479 " for sequence name: $default";
484 my @sel_columns = grep { $_ ne $primary_key }
485 dbdef->table($child_table)->columns;
486 my $sel_columns = join(', ', @sel_columns );
488 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
489 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
490 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
492 my $sel_st = "SELECT $sel_columns FROM $child_table".
493 " WHERE $primary_key = $sourceid";
496 my $sel_sth = dbh->prepare( $sel_st )
497 or return dbh->errstr;
499 $sel_sth->execute or return $sel_sth->errstr;
501 while ( my $row = $sel_sth->fetchrow_hashref ) {
503 warn " selected row: ".
504 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
508 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
509 my $ins_sth =dbh->prepare($statement)
510 or return dbh->errstr;
511 my @param = ( $destid, map $row->{$_}, @ins_columns );
512 warn " $statement: [ ". join(', ', @param). " ]\n"
514 $ins_sth->execute( @param )
515 or return $ins_sth->errstr;
517 #next unless keys %{ $child_tables{$child_table} };
518 next unless $sequence;
520 #another section of that laziness
521 my $seq_sql = "SELECT currval('$sequence')";
522 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
523 $seq_sth->execute or return $seq_sth->errstr;
524 my $insertid = $seq_sth->fetchrow_arrayref->[0];
526 # don't drink soap! recurse! recurse! okay!
528 _copy_skel( $child_table_def,
529 $row->{$child_pkey}, #sourceid
531 %{ $child_tables{$child_table_def} },
533 return $error if $error;
543 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
545 Like the insert method on an existing record, this method orders a package
546 and included services atomicaly. Pass a Tie::RefHash data structure to this
547 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
548 be a better explanation of this, but until then, here's an example:
551 tie %hash, 'Tie::RefHash'; #this part is important
553 $cust_pkg => [ $svc_acct ],
556 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
558 Services can be new, in which case they are inserted, or existing unaudited
559 services, in which case they are linked to the newly-created package.
561 Currently available options are: I<depend_jobnum> and I<noexport>.
563 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
564 on the supplied jobnum (they will not run until the specific job completes).
565 This can be used to defer provisioning until some action completes (such
566 as running the customer's credit card successfully).
568 The I<noexport> option is deprecated. If I<noexport> is set true, no
569 provisioning jobs (exports) are scheduled. (You can schedule them later with
570 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
571 on the cust_main object is not recommended, as existing services will also be
578 my $cust_pkgs = shift;
581 my %svc_options = ();
582 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
583 if exists $options{'depend_jobnum'};
584 warn "$me order_pkgs called with options ".
585 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
588 local $SIG{HUP} = 'IGNORE';
589 local $SIG{INT} = 'IGNORE';
590 local $SIG{QUIT} = 'IGNORE';
591 local $SIG{TERM} = 'IGNORE';
592 local $SIG{TSTP} = 'IGNORE';
593 local $SIG{PIPE} = 'IGNORE';
595 my $oldAutoCommit = $FS::UID::AutoCommit;
596 local $FS::UID::AutoCommit = 0;
599 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
601 foreach my $cust_pkg ( keys %$cust_pkgs ) {
602 $cust_pkg->custnum( $self->custnum );
603 my $error = $cust_pkg->insert;
605 $dbh->rollback if $oldAutoCommit;
606 return "inserting cust_pkg (transaction rolled back): $error";
608 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
609 if ( $svc_something->svcnum ) {
610 my $old_cust_svc = $svc_something->cust_svc;
611 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
612 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
613 $error = $new_cust_svc->replace($old_cust_svc);
615 $svc_something->pkgnum( $cust_pkg->pkgnum );
616 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
617 $svc_something->seconds( $svc_something->seconds + $$seconds );
620 $error = $svc_something->insert(%svc_options);
623 $dbh->rollback if $oldAutoCommit;
624 #return "inserting svc_ (transaction rolled back): $error";
630 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
634 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
636 Recharges this (existing) customer with the specified prepaid card (see
637 L<FS::prepay_credit>), specified either by I<identifier> or as an
638 FS::prepay_credit object. If there is an error, returns the error, otherwise
641 Optionally, four scalar references can be passed as well. They will have their
642 values filled in with the amount, number of seconds, and number of upload and
643 download bytes applied by this prepaid
648 sub recharge_prepay {
649 my( $self, $prepay_credit, $amountref, $secondsref,
650 $upbytesref, $downbytesref, $totalbytesref ) = @_;
652 local $SIG{HUP} = 'IGNORE';
653 local $SIG{INT} = 'IGNORE';
654 local $SIG{QUIT} = 'IGNORE';
655 local $SIG{TERM} = 'IGNORE';
656 local $SIG{TSTP} = 'IGNORE';
657 local $SIG{PIPE} = 'IGNORE';
659 my $oldAutoCommit = $FS::UID::AutoCommit;
660 local $FS::UID::AutoCommit = 0;
663 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
665 my $error = $self->get_prepay($prepay_credit, \$amount,
666 \$seconds, \$upbytes, \$downbytes, \$totalbytes)
667 || $self->increment_seconds($seconds)
668 || $self->increment_upbytes($upbytes)
669 || $self->increment_downbytes($downbytes)
670 || $self->increment_totalbytes($totalbytes)
671 || $self->insert_cust_pay_prepay( $amount,
673 ? $prepay_credit->identifier
678 $dbh->rollback if $oldAutoCommit;
682 if ( defined($amountref) ) { $$amountref = $amount; }
683 if ( defined($secondsref) ) { $$secondsref = $seconds; }
684 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
685 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
686 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
688 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
693 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
695 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
696 specified either by I<identifier> or as an FS::prepay_credit object.
698 References to I<amount> and I<seconds> scalars should be passed as arguments
699 and will be incremented by the values of the prepaid card.
701 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
702 check or set this customer's I<agentnum>.
704 If there is an error, returns the error, otherwise returns false.
710 my( $self, $prepay_credit, $amountref, $secondsref,
711 $upref, $downref, $totalref) = @_;
713 local $SIG{HUP} = 'IGNORE';
714 local $SIG{INT} = 'IGNORE';
715 local $SIG{QUIT} = 'IGNORE';
716 local $SIG{TERM} = 'IGNORE';
717 local $SIG{TSTP} = 'IGNORE';
718 local $SIG{PIPE} = 'IGNORE';
720 my $oldAutoCommit = $FS::UID::AutoCommit;
721 local $FS::UID::AutoCommit = 0;
724 unless ( ref($prepay_credit) ) {
726 my $identifier = $prepay_credit;
728 $prepay_credit = qsearchs(
730 { 'identifier' => $prepay_credit },
735 unless ( $prepay_credit ) {
736 $dbh->rollback if $oldAutoCommit;
737 return "Invalid prepaid card: ". $identifier;
742 if ( $prepay_credit->agentnum ) {
743 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
744 $dbh->rollback if $oldAutoCommit;
745 return "prepaid card not valid for agent ". $self->agentnum;
747 $self->agentnum($prepay_credit->agentnum);
750 my $error = $prepay_credit->delete;
752 $dbh->rollback if $oldAutoCommit;
753 return "removing prepay_credit (transaction rolled back): $error";
756 $$amountref += $prepay_credit->amount;
757 $$secondsref += $prepay_credit->seconds;
758 $$upref += $prepay_credit->upbytes;
759 $$downref += $prepay_credit->downbytes;
760 $$totalref += $prepay_credit->totalbytes;
762 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
767 =item increment_upbytes SECONDS
769 Updates this customer's single or primary account (see L<FS::svc_acct>) by
770 the specified number of upbytes. If there is an error, returns the error,
771 otherwise returns false.
775 sub increment_upbytes {
776 _increment_column( shift, 'upbytes', @_);
779 =item increment_downbytes SECONDS
781 Updates this customer's single or primary account (see L<FS::svc_acct>) by
782 the specified number of downbytes. If there is an error, returns the error,
783 otherwise returns false.
787 sub increment_downbytes {
788 _increment_column( shift, 'downbytes', @_);
791 =item increment_totalbytes SECONDS
793 Updates this customer's single or primary account (see L<FS::svc_acct>) by
794 the specified number of totalbytes. If there is an error, returns the error,
795 otherwise returns false.
799 sub increment_totalbytes {
800 _increment_column( shift, 'totalbytes', @_);
803 =item increment_seconds SECONDS
805 Updates this customer's single or primary account (see L<FS::svc_acct>) by
806 the specified number of seconds. If there is an error, returns the error,
807 otherwise returns false.
811 sub increment_seconds {
812 _increment_column( shift, 'seconds', @_);
815 =item _increment_column AMOUNT
817 Updates this customer's single or primary account (see L<FS::svc_acct>) by
818 the specified number of seconds or bytes. If there is an error, returns
819 the error, otherwise returns false.
823 sub _increment_column {
824 my( $self, $column, $amount ) = @_;
825 warn "$me increment_column called: $column, $amount\n"
828 return '' unless $amount;
830 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
831 $self->ncancelled_pkgs;
834 return 'No packages with primary or single services found'.
835 ' to apply pre-paid time';
836 } elsif ( scalar(@cust_pkg) > 1 ) {
837 #maybe have a way to specify the package/account?
838 return 'Multiple packages found to apply pre-paid time';
841 my $cust_pkg = $cust_pkg[0];
842 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
846 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
849 return 'No account found to apply pre-paid time';
850 } elsif ( scalar(@cust_svc) > 1 ) {
851 return 'Multiple accounts found to apply pre-paid time';
854 my $svc_acct = $cust_svc[0]->svc_x;
855 warn " found service svcnum ". $svc_acct->pkgnum.
856 ' ('. $svc_acct->email. ")\n"
859 $column = "increment_$column";
860 $svc_acct->$column($amount);
864 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
866 Inserts a prepayment in the specified amount for this customer. An optional
867 second argument can specify the prepayment identifier for tracking purposes.
868 If there is an error, returns the error, otherwise returns false.
872 sub insert_cust_pay_prepay {
873 shift->insert_cust_pay('PREP', @_);
876 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
878 Inserts a cash payment in the specified amount for this customer. An optional
879 second argument can specify the payment identifier for tracking purposes.
880 If there is an error, returns the error, otherwise returns false.
884 sub insert_cust_pay_cash {
885 shift->insert_cust_pay('CASH', @_);
888 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
890 Inserts a Western Union payment in the specified amount for this customer. An
891 optional second argument can specify the prepayment identifier for tracking
892 purposes. If there is an error, returns the error, otherwise returns false.
896 sub insert_cust_pay_west {
897 shift->insert_cust_pay('WEST', @_);
900 sub insert_cust_pay {
901 my( $self, $payby, $amount ) = splice(@_, 0, 3);
902 my $payinfo = scalar(@_) ? shift : '';
904 my $cust_pay = new FS::cust_pay {
905 'custnum' => $self->custnum,
906 'paid' => sprintf('%.2f', $amount),
907 #'_date' => #date the prepaid card was purchased???
909 'payinfo' => $payinfo,
917 This method is deprecated. See the I<depend_jobnum> option to the insert and
918 order_pkgs methods for a better way to defer provisioning.
920 Re-schedules all exports by calling the B<reexport> method of all associated
921 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
922 otherwise returns false.
929 carp "WARNING: FS::cust_main::reexport is deprectated; ".
930 "use the depend_jobnum option to insert or order_pkgs to delay export";
932 local $SIG{HUP} = 'IGNORE';
933 local $SIG{INT} = 'IGNORE';
934 local $SIG{QUIT} = 'IGNORE';
935 local $SIG{TERM} = 'IGNORE';
936 local $SIG{TSTP} = 'IGNORE';
937 local $SIG{PIPE} = 'IGNORE';
939 my $oldAutoCommit = $FS::UID::AutoCommit;
940 local $FS::UID::AutoCommit = 0;
943 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
944 my $error = $cust_pkg->reexport;
946 $dbh->rollback if $oldAutoCommit;
951 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
956 =item delete NEW_CUSTNUM
958 This deletes the customer. If there is an error, returns the error, otherwise
961 This will completely remove all traces of the customer record. This is not
962 what you want when a customer cancels service; for that, cancel all of the
963 customer's packages (see L</cancel>).
965 If the customer has any uncancelled packages, you need to pass a new (valid)
966 customer number for those packages to be transferred to. Cancelled packages
967 will be deleted. Did I mention that this is NOT what you want when a customer
968 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
970 You can't delete a customer with invoices (see L<FS::cust_bill>),
971 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
972 refunds (see L<FS::cust_refund>).
979 local $SIG{HUP} = 'IGNORE';
980 local $SIG{INT} = 'IGNORE';
981 local $SIG{QUIT} = 'IGNORE';
982 local $SIG{TERM} = 'IGNORE';
983 local $SIG{TSTP} = 'IGNORE';
984 local $SIG{PIPE} = 'IGNORE';
986 my $oldAutoCommit = $FS::UID::AutoCommit;
987 local $FS::UID::AutoCommit = 0;
990 if ( $self->cust_bill ) {
991 $dbh->rollback if $oldAutoCommit;
992 return "Can't delete a customer with invoices";
994 if ( $self->cust_credit ) {
995 $dbh->rollback if $oldAutoCommit;
996 return "Can't delete a customer with credits";
998 if ( $self->cust_pay ) {
999 $dbh->rollback if $oldAutoCommit;
1000 return "Can't delete a customer with payments";
1002 if ( $self->cust_refund ) {
1003 $dbh->rollback if $oldAutoCommit;
1004 return "Can't delete a customer with refunds";
1007 my @cust_pkg = $self->ncancelled_pkgs;
1009 my $new_custnum = shift;
1010 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1011 $dbh->rollback if $oldAutoCommit;
1012 return "Invalid new customer number: $new_custnum";
1014 foreach my $cust_pkg ( @cust_pkg ) {
1015 my %hash = $cust_pkg->hash;
1016 $hash{'custnum'} = $new_custnum;
1017 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1018 my $error = $new_cust_pkg->replace($cust_pkg,
1019 options => { $cust_pkg->options },
1022 $dbh->rollback if $oldAutoCommit;
1027 my @cancelled_cust_pkg = $self->all_pkgs;
1028 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1029 my $error = $cust_pkg->delete;
1031 $dbh->rollback if $oldAutoCommit;
1036 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1037 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1039 my $error = $cust_main_invoice->delete;
1041 $dbh->rollback if $oldAutoCommit;
1046 my $error = $self->SUPER::delete;
1048 $dbh->rollback if $oldAutoCommit;
1052 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1057 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ]
1059 Replaces the OLD_RECORD with this one in the database. If there is an error,
1060 returns the error, otherwise returns false.
1062 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1063 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1064 expected and rollback the entire transaction; it is not necessary to call
1065 check_invoicing_list first. Here's an example:
1067 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1074 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1076 : $self->replace_old;
1080 warn "$me replace called\n"
1083 my $curuser = $FS::CurrentUser::CurrentUser;
1084 if ( $self->payby eq 'COMP'
1085 && $self->payby ne $old->payby
1086 && ! $curuser->access_right('Complimentary customer')
1089 return "You are not permitted to create complimentary accounts.";
1092 local($ignore_expired_card) = 1
1093 if $old->payby =~ /^(CARD|DCRD)$/
1094 && $self->payby =~ /^(CARD|DCRD)$/
1095 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1097 local $SIG{HUP} = 'IGNORE';
1098 local $SIG{INT} = 'IGNORE';
1099 local $SIG{QUIT} = 'IGNORE';
1100 local $SIG{TERM} = 'IGNORE';
1101 local $SIG{TSTP} = 'IGNORE';
1102 local $SIG{PIPE} = 'IGNORE';
1104 my $oldAutoCommit = $FS::UID::AutoCommit;
1105 local $FS::UID::AutoCommit = 0;
1108 my $error = $self->SUPER::replace($old);
1111 $dbh->rollback if $oldAutoCommit;
1115 if ( @param ) { # INVOICING_LIST_ARYREF
1116 my $invoicing_list = shift @param;
1117 $error = $self->check_invoicing_list( $invoicing_list );
1119 $dbh->rollback if $oldAutoCommit;
1122 $self->invoicing_list( $invoicing_list );
1125 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1126 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1127 # card/check/lec info has changed, want to retry realtime_ invoice events
1128 my $error = $self->retry_realtime;
1130 $dbh->rollback if $oldAutoCommit;
1135 unless ( $import || $skip_fuzzyfiles ) {
1136 $error = $self->queue_fuzzyfiles_update;
1138 $dbh->rollback if $oldAutoCommit;
1139 return "updating fuzzy search cache: $error";
1143 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1148 =item queue_fuzzyfiles_update
1150 Used by insert & replace to update the fuzzy search cache
1154 sub queue_fuzzyfiles_update {
1157 local $SIG{HUP} = 'IGNORE';
1158 local $SIG{INT} = 'IGNORE';
1159 local $SIG{QUIT} = 'IGNORE';
1160 local $SIG{TERM} = 'IGNORE';
1161 local $SIG{TSTP} = 'IGNORE';
1162 local $SIG{PIPE} = 'IGNORE';
1164 my $oldAutoCommit = $FS::UID::AutoCommit;
1165 local $FS::UID::AutoCommit = 0;
1168 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1169 my $error = $queue->insert( map $self->getfield($_),
1170 qw(first last company)
1173 $dbh->rollback if $oldAutoCommit;
1174 return "queueing job (transaction rolled back): $error";
1177 if ( $self->ship_last ) {
1178 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1179 $error = $queue->insert( map $self->getfield("ship_$_"),
1180 qw(first last company)
1183 $dbh->rollback if $oldAutoCommit;
1184 return "queueing job (transaction rolled back): $error";
1188 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1195 Checks all fields to make sure this is a valid customer record. If there is
1196 an error, returns the error, otherwise returns false. Called by the insert
1197 and replace methods.
1204 warn "$me check BEFORE: \n". $self->_dump
1208 $self->ut_numbern('custnum')
1209 || $self->ut_number('agentnum')
1210 || $self->ut_textn('agent_custid')
1211 || $self->ut_number('refnum')
1212 || $self->ut_name('last')
1213 || $self->ut_name('first')
1214 || $self->ut_snumbern('birthdate')
1215 || $self->ut_snumbern('signupdate')
1216 || $self->ut_textn('company')
1217 || $self->ut_text('address1')
1218 || $self->ut_textn('address2')
1219 || $self->ut_text('city')
1220 || $self->ut_textn('county')
1221 || $self->ut_textn('state')
1222 || $self->ut_country('country')
1223 || $self->ut_anything('comments')
1224 || $self->ut_numbern('referral_custnum')
1225 || $self->ut_textn('stateid')
1226 || $self->ut_textn('stateid_state')
1227 || $self->ut_textn('invoice_terms')
1229 #barf. need message catalogs. i18n. etc.
1230 $error .= "Please select an advertising source."
1231 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1232 return $error if $error;
1234 return "Unknown agent"
1235 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1237 return "Unknown refnum"
1238 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1240 return "Unknown referring custnum: ". $self->referral_custnum
1241 unless ! $self->referral_custnum
1242 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1244 if ( $self->ss eq '' ) {
1249 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1250 or return "Illegal social security number: ". $self->ss;
1251 $self->ss("$1-$2-$3");
1255 # bad idea to disable, causes billing to fail because of no tax rates later
1256 # unless ( $import ) {
1257 unless ( qsearch('cust_main_county', {
1258 'country' => $self->country,
1261 return "Unknown state/county/country: ".
1262 $self->state. "/". $self->county. "/". $self->country
1263 unless qsearch('cust_main_county',{
1264 'state' => $self->state,
1265 'county' => $self->county,
1266 'country' => $self->country,
1272 $self->ut_phonen('daytime', $self->country)
1273 || $self->ut_phonen('night', $self->country)
1274 || $self->ut_phonen('fax', $self->country)
1275 || $self->ut_zip('zip', $self->country)
1277 return $error if $error;
1279 if ( $conf->exists('cust_main-require_phone')
1280 && ! length($self->daytime) && ! length($self->night)
1283 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1285 : FS::Msgcat::_gettext('daytime');
1286 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1288 : FS::Msgcat::_gettext('night');
1290 return "$daytime_label or $night_label is required"
1294 if ( $self->has_ship_address
1295 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1296 $self->addr_fields )
1300 $self->ut_name('ship_last')
1301 || $self->ut_name('ship_first')
1302 || $self->ut_textn('ship_company')
1303 || $self->ut_text('ship_address1')
1304 || $self->ut_textn('ship_address2')
1305 || $self->ut_text('ship_city')
1306 || $self->ut_textn('ship_county')
1307 || $self->ut_textn('ship_state')
1308 || $self->ut_country('ship_country')
1310 return $error if $error;
1312 #false laziness with above
1313 unless ( qsearchs('cust_main_county', {
1314 'country' => $self->ship_country,
1317 return "Unknown ship_state/ship_county/ship_country: ".
1318 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1319 unless qsearch('cust_main_county',{
1320 'state' => $self->ship_state,
1321 'county' => $self->ship_county,
1322 'country' => $self->ship_country,
1328 $self->ut_phonen('ship_daytime', $self->ship_country)
1329 || $self->ut_phonen('ship_night', $self->ship_country)
1330 || $self->ut_phonen('ship_fax', $self->ship_country)
1331 || $self->ut_zip('ship_zip', $self->ship_country)
1333 return $error if $error;
1335 return "Unit # is required."
1336 if $self->ship_address2 =~ /^\s*$/
1337 && $conf->exists('cust_main-require_address2');
1339 } else { # ship_ info eq billing info, so don't store dup info in database
1341 $self->setfield("ship_$_", '')
1342 foreach $self->addr_fields;
1344 return "Unit # is required."
1345 if $self->address2 =~ /^\s*$/
1346 && $conf->exists('cust_main-require_address2');
1350 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1351 # or return "Illegal payby: ". $self->payby;
1353 FS::payby->can_payby($self->table, $self->payby)
1354 or return "Illegal payby: ". $self->payby;
1356 $error = $self->ut_numbern('paystart_month')
1357 || $self->ut_numbern('paystart_year')
1358 || $self->ut_numbern('payissue')
1359 || $self->ut_textn('paytype')
1361 return $error if $error;
1363 if ( $self->payip eq '' ) {
1366 $error = $self->ut_ip('payip');
1367 return $error if $error;
1370 # If it is encrypted and the private key is not availaible then we can't
1371 # check the credit card.
1373 my $check_payinfo = 1;
1375 if ($self->is_encrypted($self->payinfo)) {
1379 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1381 my $payinfo = $self->payinfo;
1382 $payinfo =~ s/\D//g;
1383 $payinfo =~ /^(\d{13,16})$/
1384 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1386 $self->payinfo($payinfo);
1388 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1390 return gettext('unknown_card_type')
1391 if cardtype($self->payinfo) eq "Unknown";
1393 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1395 return 'Banned credit card: banned on '.
1396 time2str('%a %h %o at %r', $ban->_date).
1397 ' by '. $ban->otaker.
1398 ' (ban# '. $ban->bannum. ')';
1401 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1402 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1403 $self->paycvv =~ /^(\d{4})$/
1404 or return "CVV2 (CID) for American Express cards is four digits.";
1407 $self->paycvv =~ /^(\d{3})$/
1408 or return "CVV2 (CVC2/CID) is three digits.";
1415 my $cardtype = cardtype($payinfo);
1416 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1418 return "Start date or issue number is required for $cardtype cards"
1419 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1421 return "Start month must be between 1 and 12"
1422 if $self->paystart_month
1423 and $self->paystart_month < 1 || $self->paystart_month > 12;
1425 return "Start year must be 1990 or later"
1426 if $self->paystart_year
1427 and $self->paystart_year < 1990;
1429 return "Issue number must be beween 1 and 99"
1431 and $self->payissue < 1 || $self->payissue > 99;
1434 $self->paystart_month('');
1435 $self->paystart_year('');
1436 $self->payissue('');
1439 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1441 my $payinfo = $self->payinfo;
1442 $payinfo =~ s/[^\d\@]//g;
1443 if ( $conf->exists('echeck-nonus') ) {
1444 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1445 $payinfo = "$1\@$2";
1447 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1448 $payinfo = "$1\@$2";
1450 $self->payinfo($payinfo);
1453 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1455 return 'Banned ACH account: banned on '.
1456 time2str('%a %h %o at %r', $ban->_date).
1457 ' by '. $ban->otaker.
1458 ' (ban# '. $ban->bannum. ')';
1461 } elsif ( $self->payby eq 'LECB' ) {
1463 my $payinfo = $self->payinfo;
1464 $payinfo =~ s/\D//g;
1465 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1467 $self->payinfo($payinfo);
1470 } elsif ( $self->payby eq 'BILL' ) {
1472 $error = $self->ut_textn('payinfo');
1473 return "Illegal P.O. number: ". $self->payinfo if $error;
1476 } elsif ( $self->payby eq 'COMP' ) {
1478 my $curuser = $FS::CurrentUser::CurrentUser;
1479 if ( ! $self->custnum
1480 && ! $curuser->access_right('Complimentary customer')
1483 return "You are not permitted to create complimentary accounts."
1486 $error = $self->ut_textn('payinfo');
1487 return "Illegal comp account issuer: ". $self->payinfo if $error;
1490 } elsif ( $self->payby eq 'PREPAY' ) {
1492 my $payinfo = $self->payinfo;
1493 $payinfo =~ s/\W//g; #anything else would just confuse things
1494 $self->payinfo($payinfo);
1495 $error = $self->ut_alpha('payinfo');
1496 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1497 return "Unknown prepayment identifier"
1498 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1503 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1504 return "Expiration date required"
1505 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1509 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1510 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1511 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1512 ( $m, $y ) = ( $3, "20$2" );
1514 return "Illegal expiration date: ". $self->paydate;
1516 $self->paydate("$y-$m-01");
1517 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1518 return gettext('expired_card')
1520 && !$ignore_expired_card
1521 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1524 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1525 ( ! $conf->exists('require_cardname')
1526 || $self->payby !~ /^(CARD|DCRD)$/ )
1528 $self->payname( $self->first. " ". $self->getfield('last') );
1530 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1531 or return gettext('illegal_name'). " payname: ". $self->payname;
1535 foreach my $flag (qw( tax spool_cdr squelch_cdr )) {
1536 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1540 $self->otaker(getotaker) unless $self->otaker;
1542 warn "$me check AFTER: \n". $self->_dump
1545 $self->SUPER::check;
1550 Returns a list of fields which have ship_ duplicates.
1555 qw( last first company
1556 address1 address2 city county state zip country
1561 =item has_ship_address
1563 Returns true if this customer record has a separate shipping address.
1567 sub has_ship_address {
1569 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
1574 Returns all packages (see L<FS::cust_pkg>) for this customer.
1581 return $self->num_pkgs unless wantarray;
1584 if ( $self->{'_pkgnum'} ) {
1585 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1587 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1590 sort sort_packages @cust_pkg;
1595 Synonym for B<all_pkgs>.
1600 shift->all_pkgs(@_);
1603 =item ncancelled_pkgs
1605 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1609 sub ncancelled_pkgs {
1612 return $self->num_ncancelled_pkgs unless wantarray;
1615 if ( $self->{'_pkgnum'} ) {
1617 warn "$me ncancelled_pkgs: returning cached objects"
1620 @cust_pkg = grep { ! $_->getfield('cancel') }
1621 values %{ $self->{'_pkgnum'}->cache };
1625 warn "$me ncancelled_pkgs: searching for packages with custnum ".
1626 $self->custnum. "\n"
1630 qsearch( 'cust_pkg', {
1631 'custnum' => $self->custnum,
1635 qsearch( 'cust_pkg', {
1636 'custnum' => $self->custnum,
1641 sort sort_packages @cust_pkg;
1645 # This should be generalized to use config options to determine order.
1647 if ( $a->get('cancel') and $b->get('cancel') ) {
1648 $a->pkgnum <=> $b->pkgnum;
1649 } elsif ( $a->get('cancel') or $b->get('cancel') ) {
1650 return -1 if $b->get('cancel');
1651 return 1 if $a->get('cancel');
1654 $a->pkgnum <=> $b->pkgnum;
1658 =item suspended_pkgs
1660 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1664 sub suspended_pkgs {
1666 grep { $_->susp } $self->ncancelled_pkgs;
1669 =item unflagged_suspended_pkgs
1671 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1672 customer (thouse packages without the `manual_flag' set).
1676 sub unflagged_suspended_pkgs {
1678 return $self->suspended_pkgs
1679 unless dbdef->table('cust_pkg')->column('manual_flag');
1680 grep { ! $_->manual_flag } $self->suspended_pkgs;
1683 =item unsuspended_pkgs
1685 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1690 sub unsuspended_pkgs {
1692 grep { ! $_->susp } $self->ncancelled_pkgs;
1695 =item num_cancelled_pkgs
1697 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1702 sub num_cancelled_pkgs {
1703 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
1706 sub num_ncancelled_pkgs {
1707 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
1711 my( $self ) = shift;
1712 my $sql = scalar(@_) ? shift : '';
1713 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
1714 my $sth = dbh->prepare(
1715 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
1716 ) or die dbh->errstr;
1717 $sth->execute($self->custnum) or die $sth->errstr;
1718 $sth->fetchrow_arrayref->[0];
1723 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1724 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1725 on success or a list of errors.
1731 grep { $_->unsuspend } $self->suspended_pkgs;
1736 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1738 Returns a list: an empty list on success or a list of errors.
1744 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1747 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1749 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1750 PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref instead
1751 of a list of pkgparts; the hashref has the following keys:
1755 =item pkgparts - listref of pkgparts
1757 =item (other options are passed to the suspend method)
1762 Returns a list: an empty list on success or a list of errors.
1766 sub suspend_if_pkgpart {
1768 my (@pkgparts, %opt);
1769 if (ref($_[0]) eq 'HASH'){
1770 @pkgparts = @{$_[0]{pkgparts}};
1775 grep { $_->suspend(%opt) }
1776 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1777 $self->unsuspended_pkgs;
1780 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
1782 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1783 given PKGPARTs (see L<FS::part_pkg>). Preferred usage is to pass a hashref
1784 instead of a list of pkgparts; the hashref has the following keys:
1788 =item pkgparts - listref of pkgparts
1790 =item (other options are passed to the suspend method)
1794 Returns a list: an empty list on success or a list of errors.
1798 sub suspend_unless_pkgpart {
1800 my (@pkgparts, %opt);
1801 if (ref($_[0]) eq 'HASH'){
1802 @pkgparts = @{$_[0]{pkgparts}};
1807 grep { $_->suspend(%opt) }
1808 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1809 $self->unsuspended_pkgs;
1812 =item cancel [ OPTION => VALUE ... ]
1814 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1816 Available options are:
1820 =item quiet - can be set true to supress email cancellation notices.
1822 =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.
1824 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
1828 Always returns a list: an empty list on success or a list of errors.
1833 my( $self, %opt ) = @_;
1835 warn "$me cancel called on customer ". $self->custnum. " with options ".
1836 join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
1839 return ( 'access denied' )
1840 unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
1842 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1844 #should try decryption (we might have the private key)
1845 # and if not maybe queue a job for the server that does?
1846 return ( "Can't (yet) ban encrypted credit cards" )
1847 if $self->is_encrypted($self->payinfo);
1849 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1850 my $error = $ban->insert;
1851 return ( $error ) if $error;
1855 my @pkgs = $self->ncancelled_pkgs;
1857 warn "$me cancelling ". scalar($self->ncancelled_pkgs). "/".
1858 scalar(@pkgs). " packages for customer ". $self->custnum. "\n"
1861 grep { $_ } map { $_->cancel(%opt) } $self->ncancelled_pkgs;
1864 sub _banned_pay_hashref {
1875 'payby' => $payby2ban{$self->payby},
1876 'payinfo' => md5_base64($self->payinfo),
1877 #don't ever *search* on reason! #'reason' =>
1883 Returns all notes (see L<FS::cust_main_note>) for this customer.
1890 qsearch( 'cust_main_note',
1891 { 'custnum' => $self->custnum },
1893 'ORDER BY _DATE DESC'
1899 Returns the agent (see L<FS::agent>) for this customer.
1905 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1908 =item bill_and_collect
1910 Cancels and suspends any packages due, generates bills, applies payments and
1913 Warns on errors (Does not currently: If there is an error, returns the error, otherwise returns false.)
1915 Options are passed as name-value pairs. Currently available options are:
1921 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:
1925 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1929 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.
1933 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
1937 If set true, re-charges setup fees.
1941 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
1947 sub bill_and_collect {
1948 my( $self, %options ) = @_;
1954 #$options{actual_time} not $options{time} because freeside-daily -d is for
1955 #pre-printing invoices
1956 my @cancel_pkgs = grep { $_->expire && $_->expire <= $options{actual_time} }
1957 $self->ncancelled_pkgs;
1959 foreach my $cust_pkg ( @cancel_pkgs ) {
1960 my $error = $cust_pkg->cancel;
1961 warn "Error cancelling expired pkg ". $cust_pkg->pkgnum.
1962 " for custnum ". $self->custnum. ": $error"
1970 #$options{actual_time} not $options{time} because freeside-daily -d is for
1971 #pre-printing invoices
1974 && ( ( $_->part_pkg->is_prepaid
1976 && $_->bill < $options{actual_time}
1979 && $_->adjourn <= $options{actual_time}
1983 $self->ncancelled_pkgs;
1985 foreach my $cust_pkg ( @susp_pkgs ) {
1986 my $error = $cust_pkg->suspend;
1987 warn "Error suspending package ". $cust_pkg->pkgnum.
1988 " for custnum ". $self->custnum. ": $error"
1996 my $error = $self->bill( %options );
1997 warn "Error billing, custnum ". $self->custnum. ": $error" if $error;
1999 $self->apply_payments_and_credits;
2001 $error = $self->collect( %options );
2002 warn "Error collecting, custnum". $self->custnum. ": $error" if $error;
2008 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
2009 conjunction with the collect method by calling B<bill_and_collect>.
2011 If there is an error, returns the error, otherwise returns false.
2013 Options are passed as name-value pairs. Currently available options are:
2019 If set true, re-charges setup fees.
2023 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:
2027 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
2031 An array ref of specific packages (objects) to attempt billing, instead trying all of them.
2033 $cust_main->bill( pkg_list => [$pkg1, $pkg2] );
2037 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.
2044 my( $self, %options ) = @_;
2045 return '' if $self->payby eq 'COMP';
2046 warn "$me bill customer ". $self->custnum. "\n"
2049 my $time = $options{'time'} || time;
2052 local $SIG{HUP} = 'IGNORE';
2053 local $SIG{INT} = 'IGNORE';
2054 local $SIG{QUIT} = 'IGNORE';
2055 local $SIG{TERM} = 'IGNORE';
2056 local $SIG{TSTP} = 'IGNORE';
2057 local $SIG{PIPE} = 'IGNORE';
2059 my $oldAutoCommit = $FS::UID::AutoCommit;
2060 local $FS::UID::AutoCommit = 0;
2063 $self->select_for_update; #mutex
2065 my @cust_bill_pkg = ();
2066 my @appended_cust_bill_pkg = ();
2069 # find the packages which are due for billing, find out how much they are
2070 # & generate invoice database.
2073 my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
2077 my @precommit_hooks = ();
2079 my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } );
2080 foreach my $cust_pkg (@cust_pkgs) {
2082 #NO!! next if $cust_pkg->cancel;
2083 next if $cust_pkg->getfield('cancel');
2085 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
2087 #? to avoid use of uninitialized value errors... ?
2088 $cust_pkg->setfield('bill', '')
2089 unless defined($cust_pkg->bill);
2091 #my $part_pkg = $cust_pkg->part_pkg;
2093 my $real_pkgpart = $cust_pkg->pkgpart;
2094 my %hash = $cust_pkg->hash;
2096 foreach my $part_pkg ( $cust_pkg->part_pkg->self_and_bill_linked ) {
2098 $cust_pkg->set($_, $hash{$_}) foreach qw ( setup last_bill bill );
2101 $self->_make_lines( 'part_pkg' => $part_pkg,
2102 'cust_pkg' => $cust_pkg,
2103 'precommit_hooks' => \@precommit_hooks,
2104 'line_items' => \@cust_bill_pkg,
2105 'appended_line_items' => \@appended_cust_bill_pkg,
2106 'setup' => \$total_setup,
2107 'recur' => \$total_recur,
2108 'tax_matrix' => \%taxlisthash,
2110 'options' => \%options,
2113 $dbh->rollback if $oldAutoCommit;
2117 } #foreach my $part_pkg
2119 } #foreach my $cust_pkg
2121 push @cust_bill_pkg, @appended_cust_bill_pkg;
2123 unless ( @cust_bill_pkg ) { #don't create an invoice w/o line items
2124 #but do commit any package date cycling that happened
2125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2129 my $postal_pkg = $self->charge_postal_fee();
2130 if ( $postal_pkg && !ref( $postal_pkg ) ) {
2131 $dbh->rollback if $oldAutoCommit;
2132 return "can't charge postal invoice fee for customer ".
2133 $self->custnum. ": $postal_pkg";
2135 if ( $postal_pkg ) {
2136 foreach my $part_pkg ( $postal_pkg->part_pkg->self_and_bill_linked ) {
2138 $self->_make_lines( 'part_pkg' => $part_pkg,
2139 'cust_pkg' => $postal_pkg,
2140 'precommit_hooks' => \@precommit_hooks,
2141 'line_items' => \@cust_bill_pkg,
2142 'appended_line_items' => \@appended_cust_bill_pkg,
2143 'setup' => \$total_setup,
2144 'recur' => \$total_recur,
2145 'tax_matrix' => \%taxlisthash,
2147 'options' => \%options,
2150 $dbh->rollback if $oldAutoCommit;
2156 warn "having a look at the taxes we found...\n" if $DEBUG > 2;
2157 foreach my $tax ( keys %taxlisthash ) {
2158 my $tax_object = shift @{ $taxlisthash{$tax} };
2159 warn "found ". $tax_object->taxname. " as $tax\n" if $DEBUG > 2;
2160 my $listref_or_error = $tax_object->taxline( @{ $taxlisthash{$tax} } );
2161 unless (ref($listref_or_error)) {
2162 $dbh->rollback if $oldAutoCommit;
2163 return $listref_or_error;
2165 unshift @{ $taxlisthash{$tax} }, $tax_object;
2167 warn "adding ". $listref_or_error->[1].
2168 " as ". $listref_or_error->[0]. "\n"
2170 $tax{ $tax_object->taxname } += $listref_or_error->[1];
2171 if ( $taxname{ $listref_or_error->[0] } ) {
2172 push @{ $taxname{ $listref_or_error->[0] } }, $tax_object->taxname;
2174 $taxname{ $listref_or_error->[0] } = [ $tax_object->taxname ];
2179 #some taxes are taxed
2182 warn "finding taxed taxes...\n" if $DEBUG > 2;
2183 foreach my $tax ( keys %taxlisthash ) {
2184 my $tax_object = shift @{ $taxlisthash{$tax} };
2185 warn "found possible taxed tax ". $tax_object->taxname. " we call $tax\n"
2187 next unless $tax_object->can('tax_on_tax');
2189 foreach my $tot ( $tax_object->tax_on_tax( $self ) ) {
2190 my $totname = ref( $tot ). ' '. $tot->taxnum;
2192 warn "checking $totname which we call ". $tot->taxname. " as applicable\n"
2194 next unless exists( $taxlisthash{ $totname } ); # only increase
2196 warn "adding $totname to taxed taxes\n" if $DEBUG > 2;
2197 if ( exists( $totlisthash{ $totname } ) ) {
2198 push @{ $totlisthash{ $totname } }, $tax{ $tax_object->taxname };
2200 $totlisthash{ $totname } = [ $tot, $tax{ $tax_object->taxname } ];
2205 warn "having a look at taxed taxes...\n" if $DEBUG > 2;
2206 foreach my $tax ( keys %totlisthash ) {
2207 my $tax_object = shift @{ $totlisthash{$tax} };
2208 warn "found previously found taxed tax ". $tax_object->taxname. "\n"
2210 my $listref_or_error = $tax_object->taxline( @{ $totlisthash{$tax} } );
2211 unless (ref($listref_or_error)) {
2212 $dbh->rollback if $oldAutoCommit;
2213 return $listref_or_error;
2216 warn "adding taxed tax amount ". $listref_or_error->[1].
2217 " as ". $tax_object->taxname. "\n"
2219 $tax{ $tax_object->taxname } += $listref_or_error->[1];
2222 #consolidate and create tax line items
2223 warn "consolidating and generating...\n" if $DEBUG > 2;
2224 foreach my $taxname ( keys %taxname ) {
2227 warn "adding $taxname\n" if $DEBUG > 1;
2228 foreach my $taxitem ( @{ $taxname{$taxname} } ) {
2229 $tax += $tax{$taxitem} unless $seen{$taxitem};
2230 warn "adding $tax{$taxitem}\n" if $DEBUG > 1;
2234 $tax = sprintf('%.2f', $tax );
2235 $total_setup = sprintf('%.2f', $total_setup+$tax );
2237 push @cust_bill_pkg, new FS::cust_bill_pkg {
2243 'itemdesc' => $taxname,
2248 my $charged = sprintf('%.2f', $total_setup + $total_recur );
2250 #create the new invoice
2251 my $cust_bill = new FS::cust_bill ( {
2252 'custnum' => $self->custnum,
2253 '_date' => ( $options{'invoice_time'} || $time ),
2254 'charged' => $charged,
2256 my $error = $cust_bill->insert;
2258 $dbh->rollback if $oldAutoCommit;
2259 return "can't create invoice for customer #". $self->custnum. ": $error";
2262 foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
2263 $cust_bill_pkg->invnum($cust_bill->invnum);
2264 my $error = $cust_bill_pkg->insert;
2266 $dbh->rollback if $oldAutoCommit;
2267 return "can't create invoice line item: $error";
2272 foreach my $hook ( @precommit_hooks ) {
2274 &{$hook}; #($self) ?
2277 $dbh->rollback if $oldAutoCommit;
2278 return "$@ running precommit hook $hook\n";
2282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2288 my ($self, %params) = @_;
2290 my $part_pkg = $params{part_pkg} or die "no part_pkg specified";
2291 my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
2292 my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
2293 my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
2294 my $appended_cust_bill_pkg = $params{appended_line_items}
2295 or die "no appended line buffer specified";
2296 my $total_setup = $params{setup} or die "no setup accumulator specified";
2297 my $total_recur = $params{recur} or die "no recur accumulator specified";
2298 my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
2299 my $time = $params{'time'} or die "no time specified";
2300 my (%options) = %{$params{options}}; #hmmm only for 'resetup'
2303 my $real_pkgpart = $cust_pkg->pkgpart;
2304 my %hash = $cust_pkg->hash;
2305 my $old_cust_pkg = new FS::cust_pkg \%hash;
2311 $cust_pkg->pkgpart($part_pkg->pkgpart);
2319 if ( ! $cust_pkg->setup &&
2321 ( $conf->exists('disable_setup_suspended_pkgs') &&
2322 ! $cust_pkg->getfield('susp')
2323 ) || ! $conf->exists('disable_setup_suspended_pkgs')
2325 || $options{'resetup'}
2328 warn " bill setup\n" if $DEBUG > 1;
2331 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
2332 return "$@ running calc_setup for $cust_pkg\n"
2335 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
2337 $cust_pkg->setfield('setup', $time)
2338 unless $cust_pkg->setup;
2339 #do need it, but it won't get written to the db
2340 #|| $cust_pkg->pkgpart != $real_pkgpart;
2345 # bill recurring fee
2348 #XXX unit stuff here too
2352 if ( $part_pkg->getfield('freq') ne '0' &&
2353 ! $cust_pkg->getfield('susp') &&
2354 ( $cust_pkg->getfield('bill') || 0 ) <= $time
2357 # XXX should this be a package event? probably. events are called
2358 # at collection time at the moment, though...
2359 $part_pkg->reset_usage($cust_pkg, 'debug'=>$DEBUG)
2360 if $part_pkg->can('reset_usage');
2361 #don't want to reset usage just cause we want a line item??
2362 #&& $part_pkg->pkgpart == $real_pkgpart;
2364 warn " bill recur\n" if $DEBUG > 1;
2367 # XXX shared with $recur_prog
2368 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2370 #over two params! lets at least switch to a hashref for the rest...
2371 my %param = ( 'precommit_hooks' => $precommit_hooks, );
2373 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
2374 return "$@ running calc_recur for $cust_pkg\n"
2378 #change this bit to use Date::Manip? CAREFUL with timezones (see
2379 # mailing list archive)
2380 my ($sec,$min,$hour,$mday,$mon,$year) =
2381 (localtime($sdate) )[0,1,2,3,4,5];
2383 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
2384 # only for figuring next bill date, nothing else, so, reset $sdate again
2386 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2387 $cust_pkg->last_bill($sdate);
2389 if ( $part_pkg->freq =~ /^\d+$/ ) {
2390 $mon += $part_pkg->freq;
2391 until ( $mon < 12 ) { $mon -= 12; $year++; }
2392 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
2394 $mday += $weeks * 7;
2395 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
2398 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
2402 return "unparsable frequency: ". $part_pkg->freq;
2404 $cust_pkg->setfield('bill',
2405 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
2409 warn "\$setup is undefined" unless defined($setup);
2410 warn "\$recur is undefined" unless defined($recur);
2411 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2414 # If there's line items, create em cust_bill_pkg records
2415 # If $cust_pkg has been modified, update it (if we're a real pkgpart)
2420 if ( $cust_pkg->modified && $cust_pkg->pkgpart == $real_pkgpart ) {
2421 # hmm.. and if just the options are modified in some weird price plan?
2423 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2426 my $error = $cust_pkg->replace( $old_cust_pkg,
2427 'options' => { $cust_pkg->options },
2429 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error"
2430 if $error; #just in case
2433 $setup = sprintf( "%.2f", $setup );
2434 $recur = sprintf( "%.2f", $recur );
2435 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2436 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2438 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2439 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2442 if ( $setup != 0 || $recur != 0 ) {
2444 warn " charges (setup=$setup, recur=$recur); adding line items\n"
2446 my $cust_bill_pkg = new FS::cust_bill_pkg {
2447 'pkgnum' => $cust_pkg->pkgnum,
2449 'unitsetup' => $unitsetup,
2451 'unitrecur' => $unitrecur,
2452 'quantity' => $cust_pkg->quantity,
2454 'edate' => $cust_pkg->bill,
2455 'details' => \@details,
2457 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
2458 unless $part_pkg->pkgpart == $real_pkgpart;
2459 push @$cust_bill_pkgs, $cust_bill_pkg;
2461 $$total_setup += $setup;
2462 $$total_recur += $recur;
2468 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2470 $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
2472 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2474 } #if $setup != 0 || $recur != 0
2478 if ( $part_pkg->can('append_cust_bill_pkgs') ) {
2479 my %param = ( 'precommit_hooks' => $precommit_hooks, );
2480 my ($more_cust_bill_pkgs) =
2481 eval { $part_pkg->append_cust_bill_pkgs( $cust_pkg, \$sdate, \%param ) };
2483 return "$@ running append_cust_bill_pkgs for $cust_pkg\n"
2485 return "$more_cust_bill_pkgs"
2486 unless ( ref($more_cust_bill_pkgs) );
2488 foreach my $cust_bill_pkg ( @{$more_cust_bill_pkgs} ) {
2490 $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
2491 unless $part_pkg->pkgpart == $real_pkgpart;
2492 push @$appended_cust_bill_pkg, $cust_bill_pkg;
2494 $$total_setup += $cust_bill_pkg->setup;
2495 $$total_recur += $cust_bill_pkg->recur;
2501 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2503 $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
2505 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2513 my $part_pkg = shift;
2514 my $taxlisthash = shift;
2515 my $cust_bill_pkg = shift;
2518 my @taxoverrides = $part_pkg->part_pkg_taxoverride;
2521 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
2525 if ( $conf->exists('enable_taxproducts')
2526 && (scalar(@taxoverrides) || $part_pkg->taxproductnum )
2530 my @taxclassnums = ();
2531 my $geocode = $self->geocode('cch');
2533 if ( scalar( @taxoverrides ) ) {
2534 @taxclassnums = map { $_->taxclassnum } @taxoverrides;
2535 }elsif ( $part_pkg->taxproductnum ) {
2536 @taxclassnums = map { $_->taxclassnum }
2537 $part_pkg->part_pkg_taxrate('cch', $geocode);
2542 join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
2544 @taxes = qsearch({ 'table' => 'tax_rate',
2545 'hashref' => { 'geocode' => $geocode, },
2546 'extra_sql' => $extra_sql,
2548 if scalar(@taxclassnums);
2553 my %taxhash = map { $_ => $self->get("$prefix$_") }
2554 qw( state county country );
2556 $taxhash{'taxclass'} = $part_pkg->taxclass;
2558 @taxes = qsearch( 'cust_main_county', \%taxhash );
2561 $taxhash{'taxclass'} = '';
2562 @taxes = qsearch( 'cust_main_county', \%taxhash );
2565 #one more try at a whole-country tax rate
2567 $taxhash{$_} = '' foreach qw( state county );
2568 @taxes = qsearch( 'cust_main_county', \%taxhash );
2571 } #if $conf->exists('enable_taxproducts')
2573 # maybe eliminate this entirely, along with all the 0% records
2576 if ( $conf->exists('enable_taxproducts') ) {
2578 "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
2579 join('/', ( map $self->get("$prefix$_"),
2582 $part_pkg->taxproduct_description,
2583 $part_pkg->pkgpart ). "\n";
2586 "fatal: can't find tax rate for state/county/country/taxclass ".
2587 join('/', ( map $self->get("$prefix$_"),
2588 qw(state county country)
2590 $part_pkg->taxclass ). "\n";
2595 foreach my $tax ( @taxes ) {
2596 my $taxname = ref( $tax ). ' '. $tax->taxnum;
2597 if ( exists( $taxlisthash->{ $taxname } ) ) {
2598 push @{ $taxlisthash->{ $taxname } }, $cust_bill_pkg;
2600 $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
2606 =item collect OPTIONS
2608 (Attempt to) collect money for this customer's outstanding invoices (see
2609 L<FS::cust_bill>). Usually used after the bill method.
2611 Actions are now triggered by billing events; see L<FS::part_event> and the
2612 billing events web interface. Old-style invoice events (see
2613 L<FS::part_bill_event>) have been deprecated.
2615 If there is an error, returns the error, otherwise returns false.
2617 Options are passed as name-value pairs.
2619 Currently available options are:
2625 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.
2629 Retry card/echeck/LEC transactions even when not scheduled by invoice events.
2633 set true to surpress email card/ACH decline notices.
2637 "1d" for the traditional, daily events (the default), or "1m" for the new monthly events (part_event.check_freq)
2641 allows for one time override of normal customer billing method
2645 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
2653 my( $self, %options ) = @_;
2654 my $invoice_time = $options{'invoice_time'} || time;
2657 local $SIG{HUP} = 'IGNORE';
2658 local $SIG{INT} = 'IGNORE';
2659 local $SIG{QUIT} = 'IGNORE';
2660 local $SIG{TERM} = 'IGNORE';
2661 local $SIG{TSTP} = 'IGNORE';
2662 local $SIG{PIPE} = 'IGNORE';
2664 my $oldAutoCommit = $FS::UID::AutoCommit;
2665 local $FS::UID::AutoCommit = 0;
2668 $self->select_for_update; #mutex
2671 my $balance = $self->balance;
2672 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2675 if ( exists($options{'retry_card'}) ) {
2676 carp 'retry_card option passed to collect is deprecated; use retry';
2677 $options{'retry'} ||= $options{'retry_card'};
2679 if ( exists($options{'retry'}) && $options{'retry'} ) {
2680 my $error = $self->retry_realtime;
2682 $dbh->rollback if $oldAutoCommit;
2687 # false laziness w/pay_batch::import_results
2689 my $due_cust_event = $self->due_cust_event(
2690 'debug' => ( $options{'debug'} || 0 ),
2691 'time' => $invoice_time,
2692 'check_freq' => $options{'check_freq'},
2694 unless( ref($due_cust_event) ) {
2695 $dbh->rollback if $oldAutoCommit;
2696 return $due_cust_event;
2699 foreach my $cust_event ( @$due_cust_event ) {
2703 #re-eval event conditions (a previous event could have changed things)
2704 unless ( $cust_event->test_conditions( 'time' => $invoice_time ) ) {
2705 #don't leave stray "new/locked" records around
2706 my $error = $cust_event->delete;
2708 #gah, even with transactions
2709 $dbh->commit if $oldAutoCommit; #well.
2716 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2717 warn " running cust_event ". $cust_event->eventnum. "\n"
2721 #if ( my $error = $cust_event->do_event(%options) ) { #XXX %options?
2722 if ( my $error = $cust_event->do_event() ) {
2723 #XXX wtf is this? figure out a proper dealio with return value
2725 # gah, even with transactions.
2726 $dbh->commit if $oldAutoCommit; #well.
2733 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2738 =item due_cust_event [ HASHREF | OPTION => VALUE ... ]
2740 Inserts database records for and returns an ordered listref of new events due
2741 for this customer, as FS::cust_event objects (see L<FS::cust_event>). If no
2742 events are due, an empty listref is returned. If there is an error, returns a
2743 scalar error message.
2745 To actually run the events, call each event's test_condition method, and if
2746 still true, call the event's do_event method.
2748 Options are passed as a hashref or as a list of name-value pairs. Available
2755 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.
2759 "Current time" for the events.
2763 Debugging level. Default is 0 (no debugging), or can be set to 1 (passed-in options), 2 (traces progress), 3 (more information), or 4 (include full search queries)
2767 Only return events for the specified eventtable (by default, events of all eventtables are returned)
2771 Explicitly pass the objects to be tested (typically used with eventtable).
2777 sub due_cust_event {
2779 my %opt = ref($_[0]) ? %{ $_[0] } : @_;
2782 #my $DEBUG = $opt{'debug'}
2783 local($DEBUG) = $opt{'debug'}
2784 if defined($opt{'debug'}) && $opt{'debug'} > $DEBUG;
2786 warn "$me due_cust_event called with options ".
2787 join(', ', map { "$_: $opt{$_}" } keys %opt). "\n"
2790 $opt{'time'} ||= time;
2792 local $SIG{HUP} = 'IGNORE';
2793 local $SIG{INT} = 'IGNORE';
2794 local $SIG{QUIT} = 'IGNORE';
2795 local $SIG{TERM} = 'IGNORE';
2796 local $SIG{TSTP} = 'IGNORE';
2797 local $SIG{PIPE} = 'IGNORE';
2799 my $oldAutoCommit = $FS::UID::AutoCommit;
2800 local $FS::UID::AutoCommit = 0;
2803 $self->select_for_update; #mutex
2806 # 1: find possible events (initial search)
2809 my @cust_event = ();
2811 my @eventtable = $opt{'eventtable'}
2812 ? ( $opt{'eventtable'} )
2813 : FS::part_event->eventtables_runorder;
2815 foreach my $eventtable ( @eventtable ) {
2818 if ( $opt{'objects'} ) {
2820 @objects = @{ $opt{'objects'} };
2824 #my @objects = $self->eventtable(); # sub cust_main { @{ [ $self ] }; }
2825 @objects = ( $eventtable eq 'cust_main' )
2827 : ( $self->$eventtable() );
2831 my @e_cust_event = ();
2833 my $cross = "CROSS JOIN $eventtable";
2834 $cross .= ' LEFT JOIN cust_main USING ( custnum )'
2835 unless $eventtable eq 'cust_main';
2837 foreach my $object ( @objects ) {
2839 #this first search uses the condition_sql magic for optimization.
2840 #the more possible events we can eliminate in this step the better
2842 my $cross_where = '';
2843 my $pkey = $object->primary_key;
2844 $cross_where = "$eventtable.$pkey = ". $object->$pkey();
2846 my $join = FS::part_event_condition->join_conditions_sql( $eventtable );
2848 FS::part_event_condition->where_conditions_sql( $eventtable,
2849 'time'=>$opt{'time'}
2851 my $order = FS::part_event_condition->order_conditions_sql( $eventtable );
2853 $extra_sql = "AND $extra_sql" if $extra_sql;
2855 #here is the agent virtualization
2856 $extra_sql .= " AND ( part_event.agentnum IS NULL
2857 OR part_event.agentnum = ". $self->agentnum. ' )';
2859 $extra_sql .= " $order";
2861 warn "searching for events for $eventtable ". $object->$pkey. "\n"
2862 if $opt{'debug'} > 2;
2863 my @part_event = qsearch( {
2864 'debug' => ( $opt{'debug'} > 3 ? 1 : 0 ),
2865 'select' => 'part_event.*',
2866 'table' => 'part_event',
2867 'addl_from' => "$cross $join",
2868 'hashref' => { 'check_freq' => ( $opt{'check_freq'} || '1d' ),
2869 'eventtable' => $eventtable,
2872 'extra_sql' => "AND $cross_where $extra_sql",
2876 my $pkey = $object->primary_key;
2877 warn " ". scalar(@part_event).
2878 " possible events found for $eventtable ". $object->$pkey(). "\n";
2881 push @e_cust_event, map { $_->new_cust_event($object) } @part_event;
2885 warn " ". scalar(@e_cust_event).
2886 " subtotal possible cust events found for $eventtable\n"
2889 push @cust_event, @e_cust_event;
2893 warn " ". scalar(@cust_event).
2894 " total possible cust events found in initial search\n"
2898 # 2: test conditions
2903 @cust_event = grep $_->test_conditions( 'time' => $opt{'time'},
2904 'stats_hashref' => \%unsat ),
2907 warn " ". scalar(@cust_event). " cust events left satisfying conditions\n"
2910 warn " invalid conditions not eliminated with condition_sql:\n".
2911 join('', map " $_: ".$unsat{$_}."\n", keys %unsat )
2918 foreach my $cust_event ( @cust_event ) {
2920 my $error = $cust_event->insert();
2922 $dbh->rollback if $oldAutoCommit;
2928 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2934 warn " returning events: ". Dumper(@cust_event). "\n"
2941 =item retry_realtime
2943 Schedules realtime / batch credit card / electronic check / LEC billing
2944 events for for retry. Useful if card information has changed or manual
2945 retry is desired. The 'collect' method must be called to actually retry
2948 Implementation details: For either this customer, or for each of this
2949 customer's open invoices, changes the status of the first "done" (with
2950 statustext error) realtime processing event to "failed".
2954 sub retry_realtime {
2957 local $SIG{HUP} = 'IGNORE';
2958 local $SIG{INT} = 'IGNORE';
2959 local $SIG{QUIT} = 'IGNORE';
2960 local $SIG{TERM} = 'IGNORE';
2961 local $SIG{TSTP} = 'IGNORE';
2962 local $SIG{PIPE} = 'IGNORE';
2964 my $oldAutoCommit = $FS::UID::AutoCommit;
2965 local $FS::UID::AutoCommit = 0;
2968 #a little false laziness w/due_cust_event (not too bad, really)
2970 my $join = FS::part_event_condition->join_conditions_sql;
2971 my $order = FS::part_event_condition->order_conditions_sql;
2974 . join ( ' OR ' , map {
2975 "( part_event.eventtable = " . dbh->quote($_)
2976 . " AND tablenum IN( SELECT " . dbdef->table($_)->primary_key . " from $_ where custnum = " . dbh->quote( $self->custnum ) . "))" ;
2977 } FS::part_event->eventtables)
2980 #here is the agent virtualization
2981 my $agent_virt = " ( part_event.agentnum IS NULL
2982 OR part_event.agentnum = ". $self->agentnum. ' )';
2984 #XXX this shouldn't be hardcoded, actions should declare it...
2985 my @realtime_events = qw(
2986 cust_bill_realtime_card
2987 cust_bill_realtime_check
2988 cust_bill_realtime_lec
2992 my $is_realtime_event = ' ( '. join(' OR ', map "part_event.action = '$_'",
2997 my @cust_event = qsearchs({
2998 'table' => 'cust_event',
2999 'select' => 'cust_event.*',
3000 'addl_from' => "LEFT JOIN part_event USING ( eventpart ) $join",
3001 'hashref' => { 'status' => 'done' },
3002 'extra_sql' => " AND statustext IS NOT NULL AND statustext != '' ".
3003 " AND $mine AND $is_realtime_event AND $agent_virt $order" # LIMIT 1"
3006 my %seen_invnum = ();
3007 foreach my $cust_event (@cust_event) {
3009 #max one for the customer, one for each open invoice
3010 my $cust_X = $cust_event->cust_X;
3011 next if $seen_invnum{ $cust_event->part_event->eventtable eq 'cust_bill'
3015 or $cust_event->part_event->eventtable eq 'cust_bill'
3018 my $error = $cust_event->retry;
3020 $dbh->rollback if $oldAutoCommit;
3021 return "error scheduling event for retry: $error";
3026 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3031 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
3033 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
3034 via a Business::OnlinePayment realtime gateway. See
3035 L<http://420.am/business-onlinepayment> for supported gateways.
3037 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3039 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
3041 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3042 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3043 if set, will override the value from the customer record.
3045 I<description> is a free-text field passed to the gateway. It defaults to
3046 "Internet services".
3048 If an I<invnum> is specified, this payment (if successful) is applied to the
3049 specified invoice. If you don't specify an I<invnum> you might want to
3050 call the B<apply_payments> method.
3052 I<quiet> can be set true to surpress email decline notices.
3054 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
3055 resulting paynum, if any.
3057 I<payunique> is a unique identifier for this payment.
3059 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
3064 my( $self, $method, $amount, %options ) = @_;
3066 warn "$me realtime_bop: $method $amount\n";
3067 warn " $_ => $options{$_}\n" foreach keys %options;
3070 $options{'description'} ||= 'Internet services';
3072 return $self->fake_bop($method, $amount, %options) if $options{'fake'};
3074 eval "use Business::OnlinePayment";
3077 my $payinfo = exists($options{'payinfo'})
3078 ? $options{'payinfo'}
3081 my %method2payby = (
3088 # check for banned credit card/ACH
3091 my $ban = qsearchs('banned_pay', {
3092 'payby' => $method2payby{$method},
3093 'payinfo' => md5_base64($payinfo),
3095 return "Banned credit card" if $ban;
3102 if ( $options{'invnum'} ) {
3103 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
3104 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
3106 map { $_->part_pkg->taxclass }
3108 map { $_->cust_pkg }
3109 $cust_bill->cust_bill_pkg;
3110 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
3111 #different taxclasses
3112 $taxclass = $taxclasses[0];
3116 #look for an agent gateway override first
3118 if ( $method eq 'CC' ) {
3119 $cardtype = cardtype($payinfo);
3120 } elsif ( $method eq 'ECHECK' ) {
3123 $cardtype = $method;
3127 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3128 cardtype => $cardtype,
3129 taxclass => $taxclass, } )
3130 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3132 taxclass => $taxclass, } )
3133 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3134 cardtype => $cardtype,
3136 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3138 taxclass => '', } );
3140 my $payment_gateway = '';
3141 my( $processor, $login, $password, $action, @bop_options );
3142 if ( $override ) { #use a payment gateway override
3144 $payment_gateway = $override->payment_gateway;
3146 $processor = $payment_gateway->gateway_module;
3147 $login = $payment_gateway->gateway_username;
3148 $password = $payment_gateway->gateway_password;
3149 $action = $payment_gateway->gateway_action;
3150 @bop_options = $payment_gateway->options;
3152 } else { #use the standard settings from the config
3154 ( $processor, $login, $password, $action, @bop_options ) =
3155 $self->default_payment_gateway($method);
3163 my $address = exists($options{'address1'})
3164 ? $options{'address1'}
3166 my $address2 = exists($options{'address2'})
3167 ? $options{'address2'}
3169 $address .= ", ". $address2 if length($address2);
3171 my $o_payname = exists($options{'payname'})
3172 ? $options{'payname'}
3174 my($payname, $payfirst, $paylast);
3175 if ( $o_payname && $method ne 'ECHECK' ) {
3176 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3177 or return "Illegal payname $payname";
3178 ($payfirst, $paylast) = ($1, $2);
3180 $payfirst = $self->getfield('first');
3181 $paylast = $self->getfield('last');
3182 $payname = "$payfirst $paylast";
3185 my @invoicing_list = $self->invoicing_list_emailonly;
3186 if ( $conf->exists('emailinvoiceautoalways')
3187 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3188 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3189 push @invoicing_list, $self->all_emails;
3192 my $email = ($conf->exists('business-onlinepayment-email-override'))
3193 ? $conf->config('business-onlinepayment-email-override')
3194 : $invoicing_list[0];
3198 my $payip = exists($options{'payip'})
3201 $content{customer_ip} = $payip
3204 $content{invoice_number} = $options{'invnum'}
3205 if exists($options{'invnum'}) && length($options{'invnum'});
3207 $content{email_customer} =
3208 ( $conf->exists('business-onlinepayment-email_customer')
3209 || $conf->exists('business-onlinepayment-email-override') );
3212 if ( $method eq 'CC' ) {
3214 $content{card_number} = $payinfo;
3215 $paydate = exists($options{'paydate'})
3216 ? $options{'paydate'}
3218 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3219 $content{expiration} = "$2/$1";
3221 my $paycvv = exists($options{'paycvv'})
3222 ? $options{'paycvv'}
3224 $content{cvv2} = $paycvv
3227 my $paystart_month = exists($options{'paystart_month'})
3228 ? $options{'paystart_month'}
3229 : $self->paystart_month;
3231 my $paystart_year = exists($options{'paystart_year'})
3232 ? $options{'paystart_year'}
3233 : $self->paystart_year;
3235 $content{card_start} = "$paystart_month/$paystart_year"
3236 if $paystart_month && $paystart_year;
3238 my $payissue = exists($options{'payissue'})
3239 ? $options{'payissue'}
3241 $content{issue_number} = $payissue if $payissue;
3243 $content{recurring_billing} = 'YES'
3244 if qsearch('cust_pay', { 'custnum' => $self->custnum,
3246 'payinfo' => $payinfo,
3248 || qsearch('cust_pay', { 'custnum' => $self->custnum,
3250 'paymask' => $self->mask_payinfo('CARD', $payinfo),
3254 } elsif ( $method eq 'ECHECK' ) {
3255 ( $content{account_number}, $content{routing_code} ) =
3256 split('@', $payinfo);
3257 $content{bank_name} = $o_payname;
3258 $content{bank_state} = exists($options{'paystate'})
3259 ? $options{'paystate'}
3260 : $self->getfield('paystate');
3261 $content{account_type} = exists($options{'paytype'})
3262 ? uc($options{'paytype'}) || 'CHECKING'
3263 : uc($self->getfield('paytype')) || 'CHECKING';
3264 $content{account_name} = $payname;
3265 $content{customer_org} = $self->company ? 'B' : 'I';
3266 $content{state_id} = exists($options{'stateid'})
3267 ? $options{'stateid'}
3268 : $self->getfield('stateid');
3269 $content{state_id_state} = exists($options{'stateid_state'})
3270 ? $options{'stateid_state'}
3271 : $self->getfield('stateid_state');
3272 $content{customer_ssn} = exists($options{'ss'})
3275 } elsif ( $method eq 'LEC' ) {
3276 $content{phone} = $payinfo;
3280 # run transaction(s)
3283 my $balance = exists( $options{'balance'} )
3284 ? $options{'balance'}
3287 $self->select_for_update; #mutex ... just until we get our pending record in
3289 #the checks here are intended to catch concurrent payments
3290 #double-form-submission prevention is taken care of in cust_pay_pending::check
3293 return "The customer's balance has changed; $method transaction aborted."
3294 if $self->balance < $balance;
3295 #&& $self->balance < $amount; #might as well anyway?
3297 #also check and make sure there aren't *other* pending payments for this cust
3299 my @pending = qsearch('cust_pay_pending', {
3300 'custnum' => $self->custnum,
3301 'status' => { op=>'!=', value=>'done' }
3303 return "A payment is already being processed for this customer (".
3304 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
3305 "); $method transaction aborted."
3306 if scalar(@pending);
3308 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
3310 my $cust_pay_pending = new FS::cust_pay_pending {
3311 'custnum' => $self->custnum,
3312 #'invnum' => $options{'invnum'},
3315 'payby' => $method2payby{$method},
3316 'payinfo' => $payinfo,
3317 'paydate' => $paydate,
3319 'gatewaynum' => ( $payment_gateway ? $payment_gateway->gatewaynum : '' ),
3321 $cust_pay_pending->payunique( $options{payunique} )
3322 if defined($options{payunique}) && length($options{payunique});
3323 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
3324 return $cpp_new_err if $cpp_new_err;
3326 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
3328 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
3329 $transaction->content(
3332 'password' => $password,
3333 'action' => $action1,
3334 'description' => $options{'description'},
3335 'amount' => $amount,
3336 #'invoice_number' => $options{'invnum'},
3337 'customer_id' => $self->custnum,
3338 'last_name' => $paylast,
3339 'first_name' => $payfirst,
3341 'address' => $address,
3342 'city' => ( exists($options{'city'})
3345 'state' => ( exists($options{'state'})
3348 'zip' => ( exists($options{'zip'})
3351 'country' => ( exists($options{'country'})
3352 ? $options{'country'}
3354 'referer' => 'http://cleanwhisker.420.am/',
3356 'phone' => $self->daytime || $self->night,
3360 $cust_pay_pending->status('pending');
3361 my $cpp_pending_err = $cust_pay_pending->replace;
3362 return $cpp_pending_err if $cpp_pending_err;
3365 my $BOP_TESTING = 0;
3366 my $BOP_TESTING_SUCCESS = 1;
3368 unless ( $BOP_TESTING ) {
3369 $transaction->submit();
3371 if ( $BOP_TESTING_SUCCESS ) {
3372 $transaction->is_success(1);
3373 $transaction->authorization('fake auth');
3375 $transaction->is_success(0);
3376 $transaction->error_message('fake failure');
3380 if ( $transaction->is_success() && $action2 ) {
3382 $cust_pay_pending->status('authorized');
3383 my $cpp_authorized_err = $cust_pay_pending->replace;
3384 return $cpp_authorized_err if $cpp_authorized_err;
3386 my $auth = $transaction->authorization;
3387 my $ordernum = $transaction->can('order_number')
3388 ? $transaction->order_number
3392 new Business::OnlinePayment( $processor, @bop_options );
3399 password => $password,
3400 order_number => $ordernum,
3402 authorization => $auth,
3403 description => $options{'description'},
3406 foreach my $field (qw( authorization_source_code returned_ACI
3407 transaction_identifier validation_code
3408 transaction_sequence_num local_transaction_date
3409 local_transaction_time AVS_result_code )) {
3410 $capture{$field} = $transaction->$field() if $transaction->can($field);
3413 $capture->content( %capture );
3417 unless ( $capture->is_success ) {
3418 my $e = "Authorization successful but capture failed, custnum #".
3419 $self->custnum. ': '. $capture->result_code.
3420 ": ". $capture->error_message;
3427 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
3428 my $cpp_captured_err = $cust_pay_pending->replace;
3429 return $cpp_captured_err if $cpp_captured_err;
3432 # remove paycvv after initial transaction
3435 #false laziness w/misc/process/payment.cgi - check both to make sure working
3437 if ( defined $self->dbdef_table->column('paycvv')
3438 && length($self->paycvv)
3439 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
3441 my $error = $self->remove_cvv;
3443 warn "WARNING: error removing cvv: $error\n";
3451 if ( $transaction->is_success() ) {
3454 if ( $payment_gateway ) { # agent override
3455 $paybatch = $payment_gateway->gatewaynum. '-';
3458 $paybatch .= "$processor:". $transaction->authorization;
3460 $paybatch .= ':'. $transaction->order_number
3461 if $transaction->can('order_number')
3462 && length($transaction->order_number);
3464 my $cust_pay = new FS::cust_pay ( {
3465 'custnum' => $self->custnum,
3466 'invnum' => $options{'invnum'},
3469 'payby' => $method2payby{$method},
3470 'payinfo' => $payinfo,
3471 'paybatch' => $paybatch,
3472 'paydate' => $paydate,
3474 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
3475 $cust_pay->payunique( $options{payunique} )
3476 if defined($options{payunique}) && length($options{payunique});
3478 my $oldAutoCommit = $FS::UID::AutoCommit;
3479 local $FS::UID::AutoCommit = 0;
3482 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
3484 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
3487 $cust_pay->invnum(''); #try again with no specific invnum
3488 my $error2 = $cust_pay->insert( $options{'manual'} ?
3489 ( 'manual' => 1 ) : ()
3492 # gah. but at least we have a record of the state we had to abort in
3493 # from cust_pay_pending now.
3494 my $e = "WARNING: $method captured but payment not recorded - ".
3495 "error inserting payment ($processor): $error2".
3496 " (previously tried insert with invnum #$options{'invnum'}" .
3497 ": $error ) - pending payment saved as paypendingnum ".
3498 $cust_pay_pending->paypendingnum. "\n";
3504 if ( $options{'paynum_ref'} ) {
3505 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
3508 $cust_pay_pending->status('done');
3509 $cust_pay_pending->statustext('captured');
3510 my $cpp_done_err = $cust_pay_pending->replace;
3512 if ( $cpp_done_err ) {
3514 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3515 my $e = "WARNING: $method captured but payment not recorded - ".
3516 "error updating status for paypendingnum ".
3517 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3523 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3524 return ''; #no error
3530 my $perror = "$processor error: ". $transaction->error_message;
3532 unless ( $transaction->error_message ) {
3535 if ( $transaction->can('response_page') ) {
3537 'page' => ( $transaction->can('response_page')
3538 ? $transaction->response_page
3541 'code' => ( $transaction->can('response_code')
3542 ? $transaction->response_code
3545 'headers' => ( $transaction->can('response_headers')
3546 ? $transaction->response_headers
3552 "No additional debugging information available for $processor";
3555 $perror .= "No error_message returned from $processor -- ".
3556 ( ref($t_response) ? Dumper($t_response) : $t_response );
3560 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
3561 && $conf->exists('emaildecline')
3562 && grep { $_ ne 'POST' } $self->invoicing_list
3563 && ! grep { $transaction->error_message =~ /$_/ }
3564 $conf->config('emaildecline-exclude')
3566 my @templ = $conf->config('declinetemplate');
3567 my $template = new Text::Template (
3569 SOURCE => [ map "$_\n", @templ ],
3570 ) or return "($perror) can't create template: $Text::Template::ERROR";
3571 $template->compile()
3572 or return "($perror) can't compile template: $Text::Template::ERROR";
3574 my $templ_hash = { error => $transaction->error_message };
3576 my $error = send_email(
3577 'from' => $conf->config('invoice_from'),
3578 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
3579 'subject' => 'Your payment could not be processed',
3580 'body' => [ $template->fill_in(HASH => $templ_hash) ],
3583 $perror .= " (also received error sending decline notification: $error)"
3588 $cust_pay_pending->status('done');
3589 $cust_pay_pending->statustext("declined: $perror");
3590 my $cpp_done_err = $cust_pay_pending->replace;
3591 if ( $cpp_done_err ) {
3592 my $e = "WARNING: $method declined but pending payment not resolved - ".
3593 "error updating status for paypendingnum ".
3594 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3596 $perror = "$e ($perror)";
3609 my( $self, $method, $amount, %options ) = @_;
3611 if ( $options{'fake_failure'} ) {
3612 return "Error: No error; test failure requested with fake_failure";
3615 my %method2payby = (
3622 #if ( $payment_gateway ) { # agent override
3623 # $paybatch = $payment_gateway->gatewaynum. '-';
3626 #$paybatch .= "$processor:". $transaction->authorization;
3628 #$paybatch .= ':'. $transaction->order_number
3629 # if $transaction->can('order_number')
3630 # && length($transaction->order_number);
3632 my $paybatch = 'FakeProcessor:54:32';
3634 my $cust_pay = new FS::cust_pay ( {
3635 'custnum' => $self->custnum,
3636 'invnum' => $options{'invnum'},
3639 'payby' => $method2payby{$method},
3640 #'payinfo' => $payinfo,
3641 'payinfo' => '4111111111111111',
3642 'paybatch' => $paybatch,
3643 #'paydate' => $paydate,
3644 'paydate' => '2012-05-01',
3646 $cust_pay->payunique( $options{payunique} ) if length($options{payunique});
3648 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
3651 $cust_pay->invnum(''); #try again with no specific invnum
3652 my $error2 = $cust_pay->insert( $options{'manual'} ?
3653 ( 'manual' => 1 ) : ()
3656 # gah, even with transactions.
3657 my $e = 'WARNING: Card/ACH debited but database not updated - '.
3658 "error inserting (fake!) payment: $error2".
3659 " (previously tried insert with invnum #$options{'invnum'}" .
3666 if ( $options{'paynum_ref'} ) {
3667 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
3670 return ''; #no error
3674 =item default_payment_gateway
3678 sub default_payment_gateway {
3679 my( $self, $method ) = @_;
3681 die "Real-time processing not enabled\n"
3682 unless $conf->exists('business-onlinepayment');
3685 my $bop_config = 'business-onlinepayment';
3686 $bop_config .= '-ach'
3687 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
3688 my ( $processor, $login, $password, $action, @bop_options ) =
3689 $conf->config($bop_config);
3690 $action ||= 'normal authorization';
3691 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
3692 die "No real-time processor is enabled - ".
3693 "did you set the business-onlinepayment configuration value?\n"
3696 ( $processor, $login, $password, $action, @bop_options )
3701 Removes the I<paycvv> field from the database directly.
3703 If there is an error, returns the error, otherwise returns false.
3709 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
3710 or return dbh->errstr;
3711 $sth->execute($self->custnum)
3712 or return $sth->errstr;
3717 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
3719 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
3720 via a Business::OnlinePayment realtime gateway. See
3721 L<http://420.am/business-onlinepayment> for supported gateways.
3723 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3725 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
3727 Most gateways require a reference to an original payment transaction to refund,
3728 so you probably need to specify a I<paynum>.
3730 I<amount> defaults to the original amount of the payment if not specified.
3732 I<reason> specifies a reason for the refund.
3734 I<paydate> specifies the expiration date for a credit card overriding the
3735 value from the customer record or the payment record. Specified as yyyy-mm-dd
3737 Implementation note: If I<amount> is unspecified or equal to the amount of the
3738 orignal payment, first an attempt is made to "void" the transaction via
3739 the gateway (to cancel a not-yet settled transaction) and then if that fails,
3740 the normal attempt is made to "refund" ("credit") the transaction via the
3741 gateway is attempted.
3743 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3744 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3745 #if set, will override the value from the customer record.
3747 #If an I<invnum> is specified, this payment (if successful) is applied to the
3748 #specified invoice. If you don't specify an I<invnum> you might want to
3749 #call the B<apply_payments> method.
3753 #some false laziness w/realtime_bop, not enough to make it worth merging
3754 #but some useful small subs should be pulled out
3755 sub realtime_refund_bop {
3756 my( $self, $method, %options ) = @_;
3758 warn "$me realtime_refund_bop: $method refund\n";
3759 warn " $_ => $options{$_}\n" foreach keys %options;
3762 eval "use Business::OnlinePayment";
3766 # look up the original payment and optionally a gateway for that payment
3770 my $amount = $options{'amount'};
3772 my( $processor, $login, $password, @bop_options ) ;
3773 my( $auth, $order_number ) = ( '', '', '' );
3775 if ( $options{'paynum'} ) {
3777 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
3778 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
3779 or return "Unknown paynum $options{'paynum'}";
3780 $amount ||= $cust_pay->paid;
3782 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
3783 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
3784 $cust_pay->paybatch;
3785 my $gatewaynum = '';
3786 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
3788 if ( $gatewaynum ) { #gateway for the payment to be refunded
3790 my $payment_gateway =
3791 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
3792 die "payment gateway $gatewaynum not found"
3793 unless $payment_gateway;
3795 $processor = $payment_gateway->gateway_module;
3796 $login = $payment_gateway->gateway_username;
3797 $password = $payment_gateway->gateway_password;
3798 @bop_options = $payment_gateway->options;
3800 } else { #try the default gateway
3802 my( $conf_processor, $unused_action );
3803 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
3804 $self->default_payment_gateway($method);
3806 return "processor of payment $options{'paynum'} $processor does not".
3807 " match default processor $conf_processor"
3808 unless $processor eq $conf_processor;
3813 } else { # didn't specify a paynum, so look for agent gateway overrides
3814 # like a normal transaction
3817 if ( $method eq 'CC' ) {
3818 $cardtype = cardtype($self->payinfo);
3819 } elsif ( $method eq 'ECHECK' ) {
3822 $cardtype = $method;
3825 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3826 cardtype => $cardtype,
3828 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3830 taxclass => '', } );
3832 if ( $override ) { #use a payment gateway override
3834 my $payment_gateway = $override->payment_gateway;
3836 $processor = $payment_gateway->gateway_module;
3837 $login = $payment_gateway->gateway_username;
3838 $password = $payment_gateway->gateway_password;
3839 #$action = $payment_gateway->gateway_action;
3840 @bop_options = $payment_gateway->options;
3842 } else { #use the standard settings from the config
3845 ( $processor, $login, $password, $unused_action, @bop_options ) =
3846 $self->default_payment_gateway($method);
3851 return "neither amount nor paynum specified" unless $amount;
3856 'password' => $password,
3857 'order_number' => $order_number,
3858 'amount' => $amount,
3859 'referer' => 'http://cleanwhisker.420.am/',
3861 $content{authorization} = $auth
3862 if length($auth); #echeck/ACH transactions have an order # but no auth
3863 #(at least with authorize.net)
3865 my $disable_void_after;
3866 if ($conf->exists('disable_void_after')
3867 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
3868 $disable_void_after = $1;
3871 #first try void if applicable
3872 if ( $cust_pay && $cust_pay->paid == $amount
3874 ( not defined($disable_void_after) )
3875 || ( time < ($cust_pay->_date + $disable_void_after ) )
3878 warn " attempting void\n" if $DEBUG > 1;
3879 my $void = new Business::OnlinePayment( $processor, @bop_options );
3880 $void->content( 'action' => 'void', %content );
3882 if ( $void->is_success ) {
3883 my $error = $cust_pay->void($options{'reason'});
3885 # gah, even with transactions.
3886 my $e = 'WARNING: Card/ACH voided but database not updated - '.
3887 "error voiding payment: $error";
3891 warn " void successful\n" if $DEBUG > 1;
3896 warn " void unsuccessful, trying refund\n"
3900 my $address = $self->address1;
3901 $address .= ", ". $self->address2 if $self->address2;
3903 my($payname, $payfirst, $paylast);
3904 if ( $self->payname && $method ne 'ECHECK' ) {
3905 $payname = $self->payname;
3906 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3907 or return "Illegal payname $payname";
3908 ($payfirst, $paylast) = ($1, $2);
3910 $payfirst = $self->getfield('first');
3911 $paylast = $self->getfield('last');
3912 $payname = "$payfirst $paylast";
3915 my @invoicing_list = $self->invoicing_list_emailonly;
3916 if ( $conf->exists('emailinvoiceautoalways')
3917 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3918 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3919 push @invoicing_list, $self->all_emails;
3922 my $email = ($conf->exists('business-onlinepayment-email-override'))
3923 ? $conf->config('business-onlinepayment-email-override')
3924 : $invoicing_list[0];
3926 my $payip = exists($options{'payip'})
3929 $content{customer_ip} = $payip
3933 if ( $method eq 'CC' ) {
3936 $content{card_number} = $payinfo = $cust_pay->payinfo;
3937 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
3938 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
3939 ($content{expiration} = "$2/$1"); # where available
3941 $content{card_number} = $payinfo = $self->payinfo;
3942 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
3943 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3944 $content{expiration} = "$2/$1";
3947 } elsif ( $method eq 'ECHECK' ) {
3950 $payinfo = $cust_pay->payinfo;
3952 $payinfo = $self->payinfo;
3954 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
3955 $content{bank_name} = $self->payname;
3956 $content{account_type} = 'CHECKING';
3957 $content{account_name} = $payname;
3958 $content{customer_org} = $self->company ? 'B' : 'I';
3959 $content{customer_ssn} = $self->ss;
3960 } elsif ( $method eq 'LEC' ) {
3961 $content{phone} = $payinfo = $self->payinfo;
3965 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3966 my %sub_content = $refund->content(
3967 'action' => 'credit',
3968 'customer_id' => $self->custnum,
3969 'last_name' => $paylast,
3970 'first_name' => $payfirst,
3972 'address' => $address,
3973 'city' => $self->city,
3974 'state' => $self->state,
3975 'zip' => $self->zip,
3976 'country' => $self->country,
3978 'phone' => $self->daytime || $self->night,
3981 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3985 return "$processor error: ". $refund->error_message
3986 unless $refund->is_success();
3988 my %method2payby = (
3994 my $paybatch = "$processor:". $refund->authorization;
3995 $paybatch .= ':'. $refund->order_number
3996 if $refund->can('order_number') && $refund->order_number;
3998 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
3999 my @cust_bill_pay = $cust_pay->cust_bill_pay;
4000 last unless @cust_bill_pay;
4001 my $cust_bill_pay = pop @cust_bill_pay;
4002 my $error = $cust_bill_pay->delete;
4006 my $cust_refund = new FS::cust_refund ( {
4007 'custnum' => $self->custnum,
4008 'paynum' => $options{'paynum'},
4009 'refund' => $amount,
4011 'payby' => $method2payby{$method},
4012 'payinfo' => $payinfo,
4013 'paybatch' => $paybatch,
4014 'reason' => $options{'reason'} || 'card or ACH refund',
4016 my $error = $cust_refund->insert;
4018 $cust_refund->paynum(''); #try again with no specific paynum
4019 my $error2 = $cust_refund->insert;
4021 # gah, even with transactions.
4022 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
4023 "error inserting refund ($processor): $error2".
4024 " (previously tried insert with paynum #$options{'paynum'}" .
4035 =item batch_card OPTION => VALUE...
4037 Adds a payment for this invoice to the pending credit card batch (see
4038 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
4039 runs the payment using a realtime gateway.
4044 my ($self, %options) = @_;
4047 if (exists($options{amount})) {
4048 $amount = $options{amount};
4050 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
4052 return '' unless $amount > 0;
4054 my $invnum = delete $options{invnum};
4055 my $payby = $options{invnum} || $self->payby; #dubious
4057 if ($options{'realtime'}) {
4058 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
4064 my $oldAutoCommit = $FS::UID::AutoCommit;
4065 local $FS::UID::AutoCommit = 0;
4068 #this needs to handle mysql as well as Pg, like svc_acct.pm
4069 #(make it into a common function if folks need to do batching with mysql)
4070 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
4071 or return "Cannot lock pay_batch: " . $dbh->errstr;
4075 'payby' => FS::payby->payby2payment($payby),
4078 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
4080 unless ( $pay_batch ) {
4081 $pay_batch = new FS::pay_batch \%pay_batch;
4082 my $error = $pay_batch->insert;
4084 $dbh->rollback if $oldAutoCommit;
4085 die "error creating new batch: $error\n";
4089 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
4090 'batchnum' => $pay_batch->batchnum,
4091 'custnum' => $self->custnum,
4094 foreach (qw( address1 address2 city state zip country payby payinfo paydate
4096 $options{$_} = '' unless exists($options{$_});
4099 my $cust_pay_batch = new FS::cust_pay_batch ( {
4100 'batchnum' => $pay_batch->batchnum,
4101 'invnum' => $invnum || 0, # is there a better value?
4102 # this field should be
4104 # cust_bill_pay_batch now
4105 'custnum' => $self->custnum,
4106 'last' => $self->getfield('last'),
4107 'first' => $self->getfield('first'),
4108 'address1' => $options{address1} || $self->address1,
4109 'address2' => $options{address2} || $self->address2,
4110 'city' => $options{city} || $self->city,
4111 'state' => $options{state} || $self->state,
4112 'zip' => $options{zip} || $self->zip,
4113 'country' => $options{country} || $self->country,
4114 'payby' => $options{payby} || $self->payby,
4115 'payinfo' => $options{payinfo} || $self->payinfo,
4116 'exp' => $options{paydate} || $self->paydate,
4117 'payname' => $options{payname} || $self->payname,
4118 'amount' => $amount, # consolidating
4121 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
4122 if $old_cust_pay_batch;
4125 if ($old_cust_pay_batch) {
4126 $error = $cust_pay_batch->replace($old_cust_pay_batch)
4128 $error = $cust_pay_batch->insert;
4132 $dbh->rollback if $oldAutoCommit;
4136 my $unapplied = $self->total_credited + $self->total_unapplied_payments + $self->in_transit_payments;
4137 foreach my $cust_bill ($self->open_cust_bill) {
4138 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
4139 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
4140 'invnum' => $cust_bill->invnum,
4141 'paybatchnum' => $cust_pay_batch->paybatchnum,
4142 'amount' => $cust_bill->owed,
4145 if ($unapplied >= $cust_bill_pay_batch->amount){
4146 $unapplied -= $cust_bill_pay_batch->amount;
4149 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
4150 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
4152 $error = $cust_bill_pay_batch->insert;
4154 $dbh->rollback if $oldAutoCommit;
4159 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4165 Returns the total owed for this customer on all invoices
4166 (see L<FS::cust_bill/owed>).
4172 $self->total_owed_date(2145859200); #12/31/2037
4175 =item total_owed_date TIME
4177 Returns the total owed for this customer on all invoices with date earlier than
4178 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
4179 see L<Time::Local> and L<Date::Parse> for conversion functions.
4183 sub total_owed_date {
4187 foreach my $cust_bill (
4188 grep { $_->_date <= $time }
4189 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4191 $total_bill += $cust_bill->owed;
4193 sprintf( "%.2f", $total_bill );
4196 =item apply_payments_and_credits
4198 Applies unapplied payments and credits.
4200 In most cases, this new method should be used in place of sequential
4201 apply_payments and apply_credits methods.
4203 If there is an error, returns the error, otherwise returns false.
4207 sub apply_payments_and_credits {
4210 local $SIG{HUP} = 'IGNORE';
4211 local $SIG{INT} = 'IGNORE';
4212 local $SIG{QUIT} = 'IGNORE';
4213 local $SIG{TERM} = 'IGNORE';
4214 local $SIG{TSTP} = 'IGNORE';
4215 local $SIG{PIPE} = 'IGNORE';
4217 my $oldAutoCommit = $FS::UID::AutoCommit;
4218 local $FS::UID::AutoCommit = 0;
4221 $self->select_for_update; #mutex
4223 foreach my $cust_bill ( $self->open_cust_bill ) {
4224 my $error = $cust_bill->apply_payments_and_credits;
4226 $dbh->rollback if $oldAutoCommit;
4227 return "Error applying: $error";
4231 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4236 =item apply_credits OPTION => VALUE ...
4238 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
4239 to outstanding invoice balances in chronological order (or reverse
4240 chronological order if the I<order> option is set to B<newest>) and returns the
4241 value of any remaining unapplied credits available for refund (see
4242 L<FS::cust_refund>).
4244 Dies if there is an error.
4252 local $SIG{HUP} = 'IGNORE';
4253 local $SIG{INT} = 'IGNORE';
4254 local $SIG{QUIT} = 'IGNORE';
4255 local $SIG{TERM} = 'IGNORE';
4256 local $SIG{TSTP} = 'IGNORE';
4257 local $SIG{PIPE} = 'IGNORE';
4259 my $oldAutoCommit = $FS::UID::AutoCommit;
4260 local $FS::UID::AutoCommit = 0;
4263 $self->select_for_update; #mutex
4265 unless ( $self->total_credited ) {
4266 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4270 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
4271 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
4273 my @invoices = $self->open_cust_bill;
4274 @invoices = sort { $b->_date <=> $a->_date } @invoices
4275 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
4278 foreach my $cust_bill ( @invoices ) {
4281 if ( !defined($credit) || $credit->credited == 0) {
4282 $credit = pop @credits or last;
4285 if ($cust_bill->owed >= $credit->credited) {
4286 $amount=$credit->credited;
4288 $amount=$cust_bill->owed;
4291 my $cust_credit_bill = new FS::cust_credit_bill ( {
4292 'crednum' => $credit->crednum,
4293 'invnum' => $cust_bill->invnum,
4294 'amount' => $amount,
4296 my $error = $cust_credit_bill->insert;
4298 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
4302 redo if ($cust_bill->owed > 0);
4306 my $total_credited = $self->total_credited;
4308 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4310 return $total_credited;
4313 =item apply_payments
4315 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
4316 to outstanding invoice balances in chronological order.
4318 #and returns the value of any remaining unapplied payments.
4320 Dies if there is an error.
4324 sub apply_payments {
4327 local $SIG{HUP} = 'IGNORE';
4328 local $SIG{INT} = 'IGNORE';
4329 local $SIG{QUIT} = 'IGNORE';
4330 local $SIG{TERM} = 'IGNORE';
4331 local $SIG{TSTP} = 'IGNORE';
4332 local $SIG{PIPE} = 'IGNORE';
4334 my $oldAutoCommit = $FS::UID::AutoCommit;
4335 local $FS::UID::AutoCommit = 0;
4338 $self->select_for_update; #mutex
4342 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
4343 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
4345 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
4346 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
4350 foreach my $cust_bill ( @invoices ) {
4353 if ( !defined($payment) || $payment->unapplied == 0 ) {
4354 $payment = pop @payments or last;
4357 if ( $cust_bill->owed >= $payment->unapplied ) {
4358 $amount = $payment->unapplied;
4360 $amount = $cust_bill->owed;
4363 my $cust_bill_pay = new FS::cust_bill_pay ( {
4364 'paynum' => $payment->paynum,
4365 'invnum' => $cust_bill->invnum,
4366 'amount' => $amount,
4368 my $error = $cust_bill_pay->insert;
4370 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
4374 redo if ( $cust_bill->owed > 0);
4378 my $total_unapplied_payments = $self->total_unapplied_payments;
4380 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4382 return $total_unapplied_payments;
4385 =item total_credited
4387 Returns the total outstanding credit (see L<FS::cust_credit>) for this
4388 customer. See L<FS::cust_credit/credited>.
4392 sub total_credited {
4394 my $total_credit = 0;
4395 foreach my $cust_credit ( qsearch('cust_credit', {
4396 'custnum' => $self->custnum,
4398 $total_credit += $cust_credit->credited;
4400 sprintf( "%.2f", $total_credit );
4403 =item total_unapplied_payments
4405 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
4406 See L<FS::cust_pay/unapplied>.
4410 sub total_unapplied_payments {
4412 my $total_unapplied = 0;
4413 foreach my $cust_pay ( qsearch('cust_pay', {
4414 'custnum' => $self->custnum,
4416 $total_unapplied += $cust_pay->unapplied;
4418 sprintf( "%.2f", $total_unapplied );
4421 =item total_unapplied_refunds
4423 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
4424 customer. See L<FS::cust_refund/unapplied>.
4428 sub total_unapplied_refunds {
4430 my $total_unapplied = 0;
4431 foreach my $cust_refund ( qsearch('cust_refund', {
4432 'custnum' => $self->custnum,
4434 $total_unapplied += $cust_refund->unapplied;
4436 sprintf( "%.2f", $total_unapplied );
4441 Returns the balance for this customer (total_owed plus total_unrefunded, minus
4442 total_credited minus total_unapplied_payments).
4450 + $self->total_unapplied_refunds
4451 - $self->total_credited
4452 - $self->total_unapplied_payments
4456 =item balance_date TIME
4458 Returns the balance for this customer, only considering invoices with date
4459 earlier than TIME (total_owed_date minus total_credited minus
4460 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
4461 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
4470 $self->total_owed_date($time)
4471 + $self->total_unapplied_refunds
4472 - $self->total_credited
4473 - $self->total_unapplied_payments
4477 =item in_transit_payments
4479 Returns the total of requests for payments for this customer pending in
4480 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
4484 sub in_transit_payments {
4486 my $in_transit_payments = 0;
4487 foreach my $pay_batch ( qsearch('pay_batch', {
4490 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
4491 'batchnum' => $pay_batch->batchnum,
4492 'custnum' => $self->custnum,
4494 $in_transit_payments += $cust_pay_batch->amount;
4497 sprintf( "%.2f", $in_transit_payments );
4500 =item paydate_monthyear
4502 Returns a two-element list consisting of the month and year of this customer's
4503 paydate (credit card expiration date for CARD customers)
4507 sub paydate_monthyear {
4509 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
4511 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
4518 =item invoicing_list [ ARRAYREF ]
4520 If an arguement is given, sets these email addresses as invoice recipients
4521 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
4522 (except as warnings), so use check_invoicing_list first.
4524 Returns a list of email addresses (with svcnum entries expanded).
4526 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
4527 check it without disturbing anything by passing nothing.
4529 This interface may change in the future.
4533 sub invoicing_list {
4534 my( $self, $arrayref ) = @_;
4537 my @cust_main_invoice;
4538 if ( $self->custnum ) {
4539 @cust_main_invoice =
4540 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4542 @cust_main_invoice = ();
4544 foreach my $cust_main_invoice ( @cust_main_invoice ) {
4545 #warn $cust_main_invoice->destnum;
4546 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
4547 #warn $cust_main_invoice->destnum;
4548 my $error = $cust_main_invoice->delete;
4549 warn $error if $error;
4552 if ( $self->custnum ) {
4553 @cust_main_invoice =
4554 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4556 @cust_main_invoice = ();
4558 my %seen = map { $_->address => 1 } @cust_main_invoice;
4559 foreach my $address ( @{$arrayref} ) {
4560 next if exists $seen{$address} && $seen{$address};
4561 $seen{$address} = 1;
4562 my $cust_main_invoice = new FS::cust_main_invoice ( {
4563 'custnum' => $self->custnum,
4566 my $error = $cust_main_invoice->insert;
4567 warn $error if $error;
4571 if ( $self->custnum ) {
4573 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4580 =item check_invoicing_list ARRAYREF
4582 Checks these arguements as valid input for the invoicing_list method. If there
4583 is an error, returns the error, otherwise returns false.
4587 sub check_invoicing_list {
4588 my( $self, $arrayref ) = @_;
4590 foreach my $address ( @$arrayref ) {
4592 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
4593 return 'Can\'t add FAX invoice destination with a blank FAX number.';
4596 my $cust_main_invoice = new FS::cust_main_invoice ( {
4597 'custnum' => $self->custnum,
4600 my $error = $self->custnum
4601 ? $cust_main_invoice->check
4602 : $cust_main_invoice->checkdest
4604 return $error if $error;
4608 return "Email address required"
4609 if $conf->exists('cust_main-require_invoicing_list_email')
4610 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
4615 =item set_default_invoicing_list
4617 Sets the invoicing list to all accounts associated with this customer,
4618 overwriting any previous invoicing list.
4622 sub set_default_invoicing_list {
4624 $self->invoicing_list($self->all_emails);
4629 Returns the email addresses of all accounts provisioned for this customer.
4636 foreach my $cust_pkg ( $self->all_pkgs ) {
4637 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
4639 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4640 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4642 $list{$_}=1 foreach map { $_->email } @svc_acct;
4647 =item invoicing_list_addpost
4649 Adds postal invoicing to this customer. If this customer is already configured
4650 to receive postal invoices, does nothing.
4654 sub invoicing_list_addpost {
4656 return if grep { $_ eq 'POST' } $self->invoicing_list;
4657 my @invoicing_list = $self->invoicing_list;
4658 push @invoicing_list, 'POST';
4659 $self->invoicing_list(\@invoicing_list);
4662 =item invoicing_list_emailonly
4664 Returns the list of email invoice recipients (invoicing_list without non-email
4665 destinations such as POST and FAX).
4669 sub invoicing_list_emailonly {
4671 warn "$me invoicing_list_emailonly called"
4673 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
4676 =item invoicing_list_emailonly_scalar
4678 Returns the list of email invoice recipients (invoicing_list without non-email
4679 destinations such as POST and FAX) as a comma-separated scalar.
4683 sub invoicing_list_emailonly_scalar {
4685 warn "$me invoicing_list_emailonly_scalar called"
4687 join(', ', $self->invoicing_list_emailonly);
4690 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
4692 Returns an array of customers referred by this customer (referral_custnum set
4693 to this custnum). If DEPTH is given, recurses up to the given depth, returning
4694 customers referred by customers referred by this customer and so on, inclusive.
4695 The default behavior is DEPTH 1 (no recursion).
4699 sub referral_cust_main {
4701 my $depth = @_ ? shift : 1;
4702 my $exclude = @_ ? shift : {};
4705 map { $exclude->{$_->custnum}++; $_; }
4706 grep { ! $exclude->{ $_->custnum } }
4707 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
4711 map { $_->referral_cust_main($depth-1, $exclude) }
4718 =item referral_cust_main_ncancelled
4720 Same as referral_cust_main, except only returns customers with uncancelled
4725 sub referral_cust_main_ncancelled {
4727 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
4730 =item referral_cust_pkg [ DEPTH ]
4732 Like referral_cust_main, except returns a flat list of all unsuspended (and
4733 uncancelled) packages for each customer. The number of items in this list may
4734 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
4738 sub referral_cust_pkg {
4740 my $depth = @_ ? shift : 1;
4742 map { $_->unsuspended_pkgs }
4743 grep { $_->unsuspended_pkgs }
4744 $self->referral_cust_main($depth);
4747 =item referring_cust_main
4749 Returns the single cust_main record for the customer who referred this customer
4750 (referral_custnum), or false.
4754 sub referring_cust_main {
4756 return '' unless $self->referral_custnum;
4757 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
4760 =item credit AMOUNT, REASON
4762 Applies a credit to this customer. If there is an error, returns the error,
4763 otherwise returns false.
4768 my( $self, $amount, $reason, %options ) = @_;
4769 my $cust_credit = new FS::cust_credit {
4770 'custnum' => $self->custnum,
4771 'amount' => $amount,
4772 'reason' => $reason,
4774 $cust_credit->insert(%options);
4777 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
4779 Creates a one-time charge for this customer. If there is an error, returns
4780 the error, otherwise returns false.
4786 my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
4787 if ( ref( $_[0] ) ) {
4788 $amount = $_[0]->{amount};
4789 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
4790 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
4791 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
4792 : '$'. sprintf("%.2f",$amount);
4793 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
4794 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
4795 $additional = $_[0]->{additional};
4799 $pkg = @_ ? shift : 'One-time charge';
4800 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
4801 $taxclass = @_ ? shift : '';
4805 local $SIG{HUP} = 'IGNORE';
4806 local $SIG{INT} = 'IGNORE';
4807 local $SIG{QUIT} = 'IGNORE';
4808 local $SIG{TERM} = 'IGNORE';
4809 local $SIG{TSTP} = 'IGNORE';
4810 local $SIG{PIPE} = 'IGNORE';
4812 my $oldAutoCommit = $FS::UID::AutoCommit;
4813 local $FS::UID::AutoCommit = 0;
4816 my $part_pkg = new FS::part_pkg ( {
4818 'comment' => $comment,
4822 'classnum' => $classnum ? $classnum : '',
4823 'taxclass' => $taxclass,
4826 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
4827 ( 0 .. @$additional - 1 )
4829 'additional_count' => scalar(@$additional),
4830 'setup_fee' => $amount,
4833 my $error = $part_pkg->insert( options => \%options );
4835 $dbh->rollback if $oldAutoCommit;
4839 my $pkgpart = $part_pkg->pkgpart;
4840 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
4841 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
4842 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
4843 $error = $type_pkgs->insert;
4845 $dbh->rollback if $oldAutoCommit;
4850 my $cust_pkg = new FS::cust_pkg ( {
4851 'custnum' => $self->custnum,
4852 'pkgpart' => $pkgpart,
4853 'quantity' => $quantity,
4856 $error = $cust_pkg->insert;
4858 $dbh->rollback if $oldAutoCommit;
4862 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4867 #=item charge_postal_fee
4869 #Applies a one time charge this customer. If there is an error,
4870 #returns the error, returns the cust_pkg charge object or false
4871 #if there was no charge.
4875 # This should be a customer event. For that to work requires that bill
4876 # also be a customer event.
4878 sub charge_postal_fee {
4881 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
4882 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
4884 my $cust_pkg = new FS::cust_pkg ( {
4885 'custnum' => $self->custnum,
4886 'pkgpart' => $pkgpart,
4890 my $error = $cust_pkg->insert;
4891 $error ? $error : $cust_pkg;
4896 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4902 sort { $a->_date <=> $b->_date }
4903 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4906 =item open_cust_bill
4908 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4913 sub open_cust_bill {
4915 grep { $_->owed > 0 } $self->cust_bill;
4920 Returns all the credits (see L<FS::cust_credit>) for this customer.
4926 sort { $a->_date <=> $b->_date }
4927 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4932 Returns all the payments (see L<FS::cust_pay>) for this customer.
4938 sort { $a->_date <=> $b->_date }
4939 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
4944 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4950 sort { $a->_date <=> $b->_date }
4951 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4954 =item cust_pay_batch
4956 Returns all batched payments (see L<FS::cust_pay_void>) for this customer.
4960 sub cust_pay_batch {
4962 sort { $a->_date <=> $b->_date }
4963 qsearch( 'cust_pay_batch', { 'custnum' => $self->custnum } )
4968 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4974 sort { $a->_date <=> $b->_date }
4975 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4980 Returns a name string for this customer, either "Company (Last, First)" or
4987 my $name = $self->contact;
4988 $name = $self->company. " ($name)" if $self->company;
4994 Returns a name string for this (service/shipping) contact, either
4995 "Company (Last, First)" or "Last, First".
5001 if ( $self->get('ship_last') ) {
5002 my $name = $self->ship_contact;
5003 $name = $self->ship_company. " ($name)" if $self->ship_company;
5012 Returns this customer's full (billing) contact name only, "Last, First"
5018 $self->get('last'). ', '. $self->first;
5023 Returns this customer's full (shipping) contact name only, "Last, First"
5029 $self->get('ship_last')
5030 ? $self->get('ship_last'). ', '. $self->ship_first
5036 Returns this customer's full country name
5042 code2country($self->country);
5045 =item geocode DATA_VENDOR
5047 Returns a value for the customer location as encoded by DATA_VENDOR.
5048 Currently this only makes sense for "CCH" as DATA_VENDOR.
5053 my ($self, $data_vendor) = (shift, shift); #always cch for now
5055 my $prefix = ( $conf->exists('tax-ship_address') && length($self->ship_last) )
5059 my ($zip,$plus4) = split /-/, $self->get("${prefix}zip")
5060 if $self->country eq 'US';
5062 #CCH specific location stuff
5063 my $extra_sql = "AND plus4lo <= '$plus4' AND plus4hi >= '$plus4'";
5066 my $cust_tax_location =
5068 'table' => 'cust_tax_location',
5069 'hashref' => { 'zip' => $zip, 'data_vendor' => $data_vendor },
5070 'extra_sql' => $extra_sql,
5073 $geocode = $cust_tax_location->geocode
5074 if $cust_tax_location;
5083 Returns a status string for this customer, currently:
5087 =item prospect - No packages have ever been ordered
5089 =item active - One or more recurring packages is active
5091 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
5093 =item suspended - All non-cancelled recurring packages are suspended
5095 =item cancelled - All recurring packages are cancelled
5101 sub status { shift->cust_status(@_); }
5105 for my $status (qw( prospect active inactive suspended cancelled )) {
5106 my $method = $status.'_sql';
5107 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
5108 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
5109 $sth->execute( ($self->custnum) x $numnum )
5110 or die "Error executing 'SELECT $sql': ". $sth->errstr;
5111 return $status if $sth->fetchrow_arrayref->[0];
5115 =item ucfirst_cust_status
5117 =item ucfirst_status
5119 Returns the status with the first character capitalized.
5123 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
5125 sub ucfirst_cust_status {
5127 ucfirst($self->cust_status);
5132 Returns a hex triplet color string for this customer's status.
5136 use vars qw(%statuscolor);
5137 tie %statuscolor, 'Tie::IxHash',
5138 'prospect' => '7e0079', #'000000', #black? naw, purple
5139 'active' => '00CC00', #green
5140 'inactive' => '0000CC', #blue
5141 'suspended' => 'FF9900', #yellow
5142 'cancelled' => 'FF0000', #red
5145 sub statuscolor { shift->cust_statuscolor(@_); }
5147 sub cust_statuscolor {
5149 $statuscolor{$self->cust_status};
5154 Returns an array of hashes representing the customer's RT tickets.
5161 my $num = $conf->config('cust_main-max_tickets') || 10;
5164 unless ( $conf->config('ticket_system-custom_priority_field') ) {
5166 @tickets = @{ FS::TicketSystem->customer_tickets($self->custnum, $num) };
5170 foreach my $priority (
5171 $conf->config('ticket_system-custom_priority_field-values'), ''
5173 last if scalar(@tickets) >= $num;
5175 @{ FS::TicketSystem->customer_tickets( $self->custnum,
5176 $num - scalar(@tickets),
5185 # Return services representing svc_accts in customer support packages
5186 sub support_services {
5188 my %packages = map { $_ => 1 } $conf->config('support_packages');
5190 grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
5191 grep { $_->part_svc->svcdb eq 'svc_acct' }
5192 map { $_->cust_svc }
5193 grep { exists $packages{ $_->pkgpart } }
5194 $self->ncancelled_pkgs;
5200 =head1 CLASS METHODS
5206 Class method that returns the list of possible status strings for customers
5207 (see L<the status method|/status>). For example:
5209 @statuses = FS::cust_main->statuses();
5214 #my $self = shift; #could be class...
5220 Returns an SQL expression identifying prospective cust_main records (customers
5221 with no packages ever ordered)
5225 use vars qw($select_count_pkgs);
5226 $select_count_pkgs =
5227 "SELECT COUNT(*) FROM cust_pkg
5228 WHERE cust_pkg.custnum = cust_main.custnum";
5230 sub select_count_pkgs_sql {
5234 sub prospect_sql { "
5235 0 = ( $select_count_pkgs )
5240 Returns an SQL expression identifying active cust_main records (customers with
5241 active recurring packages).
5246 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
5252 Returns an SQL expression identifying inactive cust_main records (customers with
5253 no active recurring packages, but otherwise unsuspended/uncancelled).
5257 sub inactive_sql { "
5258 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
5260 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
5266 Returns an SQL expression identifying suspended cust_main records.
5271 sub suspended_sql { susp_sql(@_); }
5273 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
5275 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
5281 Returns an SQL expression identifying cancelled cust_main records.
5285 sub cancelled_sql { cancel_sql(@_); }
5288 my $recurring_sql = FS::cust_pkg->recurring_sql;
5289 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
5292 0 < ( $select_count_pkgs )
5293 AND 0 < ( $select_count_pkgs AND $recurring_sql AND $cancelled_sql )
5294 AND 0 = ( $select_count_pkgs AND $recurring_sql
5295 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
5297 AND 0 = ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
5303 =item uncancelled_sql
5305 Returns an SQL expression identifying un-cancelled cust_main records.
5309 sub uncancelled_sql { uncancel_sql(@_); }
5310 sub uncancel_sql { "
5311 ( 0 < ( $select_count_pkgs
5312 AND ( cust_pkg.cancel IS NULL
5313 OR cust_pkg.cancel = 0
5316 OR 0 = ( $select_count_pkgs )
5322 Returns an SQL fragment to retreive the balance.
5327 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
5328 WHERE cust_bill.custnum = cust_main.custnum )
5329 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
5330 WHERE cust_pay.custnum = cust_main.custnum )
5331 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
5332 WHERE cust_credit.custnum = cust_main.custnum )
5333 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
5334 WHERE cust_refund.custnum = cust_main.custnum )
5337 =item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5339 Returns an SQL fragment to retreive the balance for this customer, only
5340 considering invoices with date earlier than START_TIME, and optionally not
5341 later than END_TIME (total_owed_date minus total_credited minus
5342 total_unapplied_payments).
5344 Times are specified as SQL fragments or numeric
5345 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
5346 L<Date::Parse> for conversion functions. The empty string can be passed
5347 to disable that time constraint completely.
5349 Available options are:
5353 =item unapplied_date
5355 set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
5360 set to true to remove all customer comparison clauses, for totals
5365 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
5370 JOIN clause (typically used with the total option)
5376 sub balance_date_sql {
5377 my( $class, $start, $end, %opt ) = @_;
5379 my $owed = FS::cust_bill->owed_sql;
5380 my $unapp_refund = FS::cust_refund->unapplied_sql;
5381 my $unapp_credit = FS::cust_credit->unapplied_sql;
5382 my $unapp_pay = FS::cust_pay->unapplied_sql;
5384 my $j = $opt{'join'} || '';
5386 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
5387 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
5388 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
5389 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
5391 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
5392 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
5393 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
5394 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
5399 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5401 Helper method for balance_date_sql; name (and usage) subject to change
5402 (suggestions welcome).
5404 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
5405 cust_refund, cust_credit or cust_pay).
5407 If TABLE is "cust_bill" or the unapplied_date option is true, only
5408 considers records with date earlier than START_TIME, and optionally not
5409 later than END_TIME .
5413 sub _money_table_where {
5414 my( $class, $table, $start, $end, %opt ) = @_;
5417 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
5418 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
5419 push @where, "$table._date <= $start" if defined($start) && length($start);
5420 push @where, "$table._date > $end" if defined($end) && length($end);
5422 push @where, @{$opt{'where'}} if $opt{'where'};
5423 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
5429 =item search_sql HASHREF
5433 Returns a qsearch hash expression to search for parameters specified in HREF.
5434 Valid parameters are
5442 =item cancelled_pkgs
5448 listref of start date, end date
5454 =item current_balance
5456 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
5460 =item flattened_pkgs
5469 my ($class, $params) = @_;
5480 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
5482 "cust_main.agentnum = $1";
5489 #prospect active inactive suspended cancelled
5490 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
5491 my $method = $params->{'status'}. '_sql';
5492 #push @where, $class->$method();
5493 push @where, FS::cust_main->$method();
5497 # parse cancelled package checkbox
5502 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
5503 unless $params->{'cancelled_pkgs'};
5509 foreach my $field (qw( signupdate )) {
5511 next unless exists($params->{$field});
5513 my($beginning, $ending) = @{$params->{$field}};
5516 "cust_main.$field IS NOT NULL",
5517 "cust_main.$field >= $beginning",
5518 "cust_main.$field <= $ending";
5520 $orderby ||= "ORDER BY cust_main.$field";
5528 my @payby = grep /^([A-Z]{4})$/, @{ $params->{'payby'} };
5530 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
5537 #my $balance_sql = $class->balance_sql();
5538 my $balance_sql = FS::cust_main->balance_sql();
5540 push @where, map { s/current_balance/$balance_sql/; $_ }
5541 @{ $params->{'current_balance'} };
5544 # setup queries, subs, etc. for the search
5547 $orderby ||= 'ORDER BY custnum';
5549 # here is the agent virtualization
5550 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
5552 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
5554 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
5556 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
5558 my $select = join(', ',
5559 'cust_main.custnum',
5560 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
5563 my(@extra_headers) = ();
5564 my(@extra_fields) = ();
5566 if ($params->{'flattened_pkgs'}) {
5568 if ($dbh->{Driver}->{Name} eq 'Pg') {
5570 $select .= ", array_to_string(array(select pkg from cust_pkg left join part_pkg using ( pkgpart ) where cust_main.custnum = cust_pkg.custnum $pkgwhere),'|') as magic";
5572 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
5573 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
5574 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
5576 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
5577 "omitting packing information from report.";
5580 my $header_query = "SELECT COUNT(cust_pkg.custnum = cust_main.custnum) AS count FROM cust_main $addl_from $extra_sql $pkgwhere group by cust_main.custnum order by count desc limit 1";
5582 my $sth = dbh->prepare($header_query) or die dbh->errstr;
5583 $sth->execute() or die $sth->errstr;
5584 my $headerrow = $sth->fetchrow_arrayref;
5585 my $headercount = $headerrow ? $headerrow->[0] : 0;
5586 while($headercount) {
5587 unshift @extra_headers, "Package ". $headercount;
5588 unshift @extra_fields, eval q!sub {my $c = shift;
5589 my @a = split '\|', $c->magic;
5590 my $p = $a[!.--$headercount. q!];
5598 'table' => 'cust_main',
5599 'select' => $select,
5601 'extra_sql' => $extra_sql,
5602 'order_by' => $orderby,
5603 'count_query' => $count_query,
5604 'extra_headers' => \@extra_headers,
5605 'extra_fields' => \@extra_fields,
5610 =item email_search_sql HASHREF
5614 Emails a notice to the specified customers.
5616 Valid parameters are those of the L<search_sql> method, plus the following:
5638 Optional job queue job for status updates.
5642 Returns an error message, or false for success.
5644 If an error occurs during any email, stops the enture send and returns that
5645 error. Presumably if you're getting SMTP errors aborting is better than
5646 retrying everything.
5650 sub email_search_sql {
5651 my($class, $params) = @_;
5653 my $from = delete $params->{from};
5654 my $subject = delete $params->{subject};
5655 my $html_body = delete $params->{html_body};
5656 my $text_body = delete $params->{text_body};
5658 my $job = delete $params->{'job'};
5660 my $sql_query = $class->search_sql($params);
5662 my $count_query = delete($sql_query->{'count_query'});
5663 my $count_sth = dbh->prepare($count_query)
5664 or die "Error preparing $count_query: ". dbh->errstr;
5666 or die "Error executing $count_query: ". $count_sth->errstr;
5667 my $count_arrayref = $count_sth->fetchrow_arrayref;
5668 my $num_cust = $count_arrayref->[0];
5670 #my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
5671 #my @extra_fields = @{ delete($sql_query->{'extra_fields'}) };
5674 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
5676 #eventually order+limit magic to reduce memory use?
5677 foreach my $cust_main ( qsearch($sql_query) ) {
5679 my $to = $cust_main->invoicing_list_emailonly_scalar;
5682 my $error = send_email(
5686 'subject' => $subject,
5687 'html_body' => $html_body,
5688 'text_body' => $text_body,
5691 return $error if $error;
5693 if ( $job ) { #progressbar foo
5695 if ( time - $min_sec > $last ) {
5696 my $error = $job->update_statustext(
5697 int( 100 * $num / $num_cust )
5699 die $error if $error;
5709 use Storable qw(thaw);
5712 sub process_email_search_sql {
5714 #warn "$me process_re_X $method for job $job\n" if $DEBUG;
5716 my $param = thaw(decode_base64(shift));
5717 warn Dumper($param) if $DEBUG;
5719 $param->{'job'} = $job;
5721 my $error = FS::cust_main->email_search_sql( $param );
5722 die $error if $error;
5726 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
5728 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
5729 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
5730 appropriate ship_ field is also searched).
5732 Additional options are the same as FS::Record::qsearch
5737 my( $self, $fuzzy, $hash, @opt) = @_;
5742 check_and_rebuild_fuzzyfiles();
5743 foreach my $field ( keys %$fuzzy ) {
5745 my $all = $self->all_X($field);
5746 next unless scalar(@$all);
5749 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
5752 foreach ( keys %match ) {
5753 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
5754 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
5757 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
5760 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
5762 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
5770 Returns a masked version of the named field
5775 my ($self,$field) = @_;
5779 'x'x(length($self->getfield($field))-4).
5780 substr($self->getfield($field), (length($self->getfield($field))-4));
5790 =item smart_search OPTION => VALUE ...
5792 Accepts the following options: I<search>, the string to search for. The string
5793 will be searched for as a customer number, phone number, name or company name,
5794 as an exact, or, in some cases, a substring or fuzzy match (see the source code
5795 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
5796 skip fuzzy matching when an exact match is found.
5798 Any additional options are treated as an additional qualifier on the search
5801 Returns a (possibly empty) array of FS::cust_main objects.
5808 #here is the agent virtualization
5809 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5813 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
5814 my $search = delete $options{'search'};
5815 ( my $alphanum_search = $search ) =~ s/\W//g;
5817 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
5819 #false laziness w/Record::ut_phone
5820 my $phonen = "$1-$2-$3";
5821 $phonen .= " x$4" if $4;
5823 push @cust_main, qsearch( {
5824 'table' => 'cust_main',
5825 'hashref' => { %options },
5826 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5828 join(' OR ', map "$_ = '$phonen'",
5829 qw( daytime night fax
5830 ship_daytime ship_night ship_fax )
5833 " AND $agentnums_sql", #agent virtualization
5836 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
5837 #try looking for matches with extensions unless one was specified
5839 push @cust_main, qsearch( {
5840 'table' => 'cust_main',
5841 'hashref' => { %options },
5842 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5844 join(' OR ', map "$_ LIKE '$phonen\%'",
5846 ship_daytime ship_night )
5849 " AND $agentnums_sql", #agent virtualization
5854 # custnum search (also try agent_custid), with some tweaking options if your
5855 # legacy cust "numbers" have letters
5856 } elsif ( $search =~ /^\s*(\d+)\s*$/
5857 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
5858 && $search =~ /^\s*(\w\w?\d+)\s*$/
5863 push @cust_main, qsearch( {
5864 'table' => 'cust_main',
5865 'hashref' => { 'custnum' => $1, %options },
5866 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5869 push @cust_main, qsearch( {
5870 'table' => 'cust_main',
5871 'hashref' => { 'agent_custid' => $1, %options },
5872 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5875 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
5877 my($company, $last, $first) = ( $1, $2, $3 );
5879 # "Company (Last, First)"
5880 #this is probably something a browser remembered,
5881 #so just do an exact search
5883 foreach my $prefix ( '', 'ship_' ) {
5884 push @cust_main, qsearch( {
5885 'table' => 'cust_main',
5886 'hashref' => { $prefix.'first' => $first,
5887 $prefix.'last' => $last,
5888 $prefix.'company' => $company,
5891 'extra_sql' => " AND $agentnums_sql",
5895 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
5896 # try (ship_){last,company}
5900 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
5901 # # full strings the browser remembers won't work
5902 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
5904 use Lingua::EN::NameParse;
5905 my $NameParse = new Lingua::EN::NameParse(
5907 allow_reversed => 1,
5910 my($last, $first) = ( '', '' );
5911 #maybe disable this too and just rely on NameParse?
5912 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
5914 ($last, $first) = ( $1, $2 );
5916 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
5917 } elsif ( ! $NameParse->parse($value) ) {
5919 my %name = $NameParse->components;
5920 $first = $name{'given_name_1'};
5921 $last = $name{'surname_1'};
5925 if ( $first && $last ) {
5927 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
5930 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5932 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
5933 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
5936 push @cust_main, qsearch( {
5937 'table' => 'cust_main',
5938 'hashref' => \%options,
5939 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5942 # or it just be something that was typed in... (try that in a sec)
5946 my $q_value = dbh->quote($value);
5949 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5950 $sql .= " ( LOWER(last) = $q_value
5951 OR LOWER(company) = $q_value
5952 OR LOWER(ship_last) = $q_value
5953 OR LOWER(ship_company) = $q_value
5956 push @cust_main, qsearch( {
5957 'table' => 'cust_main',
5958 'hashref' => \%options,
5959 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5962 #no exact match, trying substring/fuzzy
5963 #always do substring & fuzzy (unless they're explicity config'ed off)
5964 #getting complaints searches are not returning enough
5965 unless ( @cust_main && $skip_fuzzy || $conf->exists('disable-fuzzy') ) {
5967 #still some false laziness w/search_sql (was search/cust_main.cgi)
5972 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
5973 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
5976 if ( $first && $last ) {
5979 { 'first' => { op=>'ILIKE', value=>"%$first%" },
5980 'last' => { op=>'ILIKE', value=>"%$last%" },
5982 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
5983 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
5990 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
5991 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
5995 foreach my $hashref ( @hashrefs ) {
5997 push @cust_main, qsearch( {
5998 'table' => 'cust_main',
5999 'hashref' => { %$hashref,
6002 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
6011 " AND $agentnums_sql", #extra_sql #agent virtualization
6014 if ( $first && $last ) {
6015 push @cust_main, FS::cust_main->fuzzy_search(
6016 { 'last' => $last, #fuzzy hashref
6017 'first' => $first }, #
6021 foreach my $field ( 'last', 'company' ) {
6023 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
6028 #eliminate duplicates
6030 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
6040 Accepts the following options: I<email>, the email address to search for. The
6041 email address will be searched for as an email invoice destination and as an
6044 #Any additional options are treated as an additional qualifier on the search
6045 #(i.e. I<agentnum>).
6047 Returns a (possibly empty) array of FS::cust_main objects (but usually just
6057 my $email = delete $options{'email'};
6059 #we're only being used by RT at the moment... no agent virtualization yet
6060 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
6064 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
6066 my ( $user, $domain ) = ( $1, $2 );
6068 warn "$me smart_search: searching for $user in domain $domain"
6074 'table' => 'cust_main_invoice',
6075 'hashref' => { 'dest' => $email },
6082 map $_->cust_svc->cust_pkg,
6084 'table' => 'svc_acct',
6085 'hashref' => { 'username' => $user, },
6087 'AND ( SELECT domain FROM svc_domain
6088 WHERE svc_acct.domsvc = svc_domain.svcnum
6089 ) = '. dbh->quote($domain),
6095 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
6097 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
6104 =item check_and_rebuild_fuzzyfiles
6108 use vars qw(@fuzzyfields);
6109 @fuzzyfields = ( 'last', 'first', 'company' );
6111 sub check_and_rebuild_fuzzyfiles {
6112 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6113 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
6116 =item rebuild_fuzzyfiles
6120 sub rebuild_fuzzyfiles {
6122 use Fcntl qw(:flock);
6124 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6125 mkdir $dir, 0700 unless -d $dir;
6127 foreach my $fuzzy ( @fuzzyfields ) {
6129 open(LOCK,">>$dir/cust_main.$fuzzy")
6130 or die "can't open $dir/cust_main.$fuzzy: $!";
6132 or die "can't lock $dir/cust_main.$fuzzy: $!";
6134 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
6135 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
6137 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
6138 my $sth = dbh->prepare("SELECT $field FROM cust_main".
6139 " WHERE $field != '' AND $field IS NOT NULL");
6140 $sth->execute or die $sth->errstr;
6142 while ( my $row = $sth->fetchrow_arrayref ) {
6143 print CACHE $row->[0]. "\n";
6148 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
6150 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
6161 my( $self, $field ) = @_;
6162 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6163 open(CACHE,"<$dir/cust_main.$field")
6164 or die "can't open $dir/cust_main.$field: $!";
6165 my @array = map { chomp; $_; } <CACHE>;
6170 =item append_fuzzyfiles LASTNAME COMPANY
6174 sub append_fuzzyfiles {
6175 #my( $first, $last, $company ) = @_;
6177 &check_and_rebuild_fuzzyfiles;
6179 use Fcntl qw(:flock);
6181 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
6183 foreach my $field (qw( first last company )) {
6188 open(CACHE,">>$dir/cust_main.$field")
6189 or die "can't open $dir/cust_main.$field: $!";
6190 flock(CACHE,LOCK_EX)
6191 or die "can't lock $dir/cust_main.$field: $!";
6193 print CACHE "$value\n";
6195 flock(CACHE,LOCK_UN)
6196 or die "can't unlock $dir/cust_main.$field: $!";
6211 #warn join('-',keys %$param);
6212 my $fh = $param->{filehandle};
6213 my $agentnum = $param->{agentnum};
6215 my $refnum = $param->{refnum};
6216 my $pkgpart = $param->{pkgpart};
6218 #my @fields = @{$param->{fields}};
6219 my $format = $param->{'format'};
6222 if ( $format eq 'simple' ) {
6223 @fields = qw( cust_pkg.setup dayphone first last
6224 address1 address2 city state zip comments );
6226 } elsif ( $format eq 'extended' ) {
6227 @fields = qw( agent_custid refnum
6228 last first address1 address2 city state zip country
6230 ship_last ship_first ship_address1 ship_address2
6231 ship_city ship_state ship_zip ship_country
6232 payinfo paycvv paydate
6235 svc_acct.username svc_acct._password
6238 } elsif ( $format eq 'extended-plus_company' ) {
6239 @fields = qw( agent_custid refnum
6240 last first company address1 address2 city state zip country
6242 ship_last ship_first ship_company ship_address1 ship_address2
6243 ship_city ship_state ship_zip ship_country
6244 payinfo paycvv paydate
6247 svc_acct.username svc_acct._password
6251 die "unknown format $format";
6254 eval "use Text::CSV_XS;";
6257 my $csv = new Text::CSV_XS;
6264 local $SIG{HUP} = 'IGNORE';
6265 local $SIG{INT} = 'IGNORE';
6266 local $SIG{QUIT} = 'IGNORE';
6267 local $SIG{TERM} = 'IGNORE';
6268 local $SIG{TSTP} = 'IGNORE';
6269 local $SIG{PIPE} = 'IGNORE';
6271 my $oldAutoCommit = $FS::UID::AutoCommit;
6272 local $FS::UID::AutoCommit = 0;
6275 #while ( $columns = $csv->getline($fh) ) {
6277 while ( defined($line=<$fh>) ) {
6279 $csv->parse($line) or do {
6280 $dbh->rollback if $oldAutoCommit;
6281 return "can't parse: ". $csv->error_input();
6284 my @columns = $csv->fields();
6285 #warn join('-',@columns);
6288 agentnum => $agentnum,
6290 country => $conf->config('countrydefault') || 'US',
6291 payby => $payby, #default
6292 paydate => '12/2037', #default
6294 my $billtime = time;
6295 my %cust_pkg = ( pkgpart => $pkgpart );
6297 foreach my $field ( @fields ) {
6299 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
6301 #$cust_pkg{$1} = str2time( shift @$columns );
6302 if ( $1 eq 'pkgpart' ) {
6303 $cust_pkg{$1} = shift @columns;
6304 } elsif ( $1 eq 'setup' ) {
6305 $billtime = str2time(shift @columns);
6307 $cust_pkg{$1} = str2time( shift @columns );
6310 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
6312 $svc_acct{$1} = shift @columns;
6316 #refnum interception
6317 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
6319 my $referral = $columns[0];
6320 my %hash = ( 'referral' => $referral,
6321 'agentnum' => $agentnum,
6325 my $part_referral = qsearchs('part_referral', \%hash )
6326 || new FS::part_referral \%hash;
6328 unless ( $part_referral->refnum ) {
6329 my $error = $part_referral->insert;
6331 $dbh->rollback if $oldAutoCommit;
6332 return "can't auto-insert advertising source: $referral: $error";
6336 $columns[0] = $part_referral->refnum;
6339 #$cust_main{$field} = shift @$columns;
6340 $cust_main{$field} = shift @columns;
6344 $cust_main{'payby'} = 'CARD' if length($cust_main{'payinfo'});
6346 my $invoicing_list = $cust_main{'invoicing_list'}
6347 ? [ delete $cust_main{'invoicing_list'} ]
6350 my $cust_main = new FS::cust_main ( \%cust_main );
6353 tie my %hash, 'Tie::RefHash'; #this part is important
6355 if ( $cust_pkg{'pkgpart'} ) {
6356 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
6359 if ( $svc_acct{'username'} ) {
6360 my $part_pkg = $cust_pkg->part_pkg;
6361 unless ( $part_pkg ) {
6362 $dbh->rollback if $oldAutoCommit;
6363 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
6365 $svc_acct{svcpart} = $part_pkg->svcpart( 'svc_acct' );
6366 push @svc_acct, new FS::svc_acct ( \%svc_acct )
6369 $hash{$cust_pkg} = \@svc_acct;
6372 my $error = $cust_main->insert( \%hash, $invoicing_list );
6375 $dbh->rollback if $oldAutoCommit;
6376 return "can't insert customer for $line: $error";
6379 if ( $format eq 'simple' ) {
6381 #false laziness w/bill.cgi
6382 $error = $cust_main->bill( 'time' => $billtime );
6384 $dbh->rollback if $oldAutoCommit;
6385 return "can't bill customer for $line: $error";
6388 $error = $cust_main->apply_payments_and_credits;
6390 $dbh->rollback if $oldAutoCommit;
6391 return "can't bill customer for $line: $error";
6394 $error = $cust_main->collect();
6396 $dbh->rollback if $oldAutoCommit;
6397 return "can't collect customer for $line: $error";
6405 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6407 return "Empty file!" unless $imported;
6419 #warn join('-',keys %$param);
6420 my $fh = $param->{filehandle};
6421 my @fields = @{$param->{fields}};
6423 eval "use Text::CSV_XS;";
6426 my $csv = new Text::CSV_XS;
6433 local $SIG{HUP} = 'IGNORE';
6434 local $SIG{INT} = 'IGNORE';
6435 local $SIG{QUIT} = 'IGNORE';
6436 local $SIG{TERM} = 'IGNORE';
6437 local $SIG{TSTP} = 'IGNORE';
6438 local $SIG{PIPE} = 'IGNORE';
6440 my $oldAutoCommit = $FS::UID::AutoCommit;
6441 local $FS::UID::AutoCommit = 0;
6444 #while ( $columns = $csv->getline($fh) ) {
6446 while ( defined($line=<$fh>) ) {
6448 $csv->parse($line) or do {
6449 $dbh->rollback if $oldAutoCommit;
6450 return "can't parse: ". $csv->error_input();
6453 my @columns = $csv->fields();
6454 #warn join('-',@columns);
6457 foreach my $field ( @fields ) {
6458 $row{$field} = shift @columns;
6461 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
6462 unless ( $cust_main ) {
6463 $dbh->rollback if $oldAutoCommit;
6464 return "unknown custnum $row{'custnum'}";
6467 if ( $row{'amount'} > 0 ) {
6468 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
6470 $dbh->rollback if $oldAutoCommit;
6474 } elsif ( $row{'amount'} < 0 ) {
6475 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
6478 $dbh->rollback if $oldAutoCommit;
6488 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
6490 return "Empty file!" unless $imported;
6496 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
6498 Sends a templated email notification to the customer (see L<Text::Template>).
6500 OPTIONS is a hash and may include
6502 I<from> - the email sender (default is invoice_from)
6504 I<to> - comma-separated scalar or arrayref of recipients
6505 (default is invoicing_list)
6507 I<subject> - The subject line of the sent email notification
6508 (default is "Notice from company_name")
6510 I<extra_fields> - a hashref of name/value pairs which will be substituted
6513 The following variables are vavailable in the template.
6515 I<$first> - the customer first name
6516 I<$last> - the customer last name
6517 I<$company> - the customer company
6518 I<$payby> - a description of the method of payment for the customer
6519 # would be nice to use FS::payby::shortname
6520 I<$payinfo> - the account information used to collect for this customer
6521 I<$expdate> - the expiration of the customer payment in seconds from epoch
6526 my ($customer, $template, %options) = @_;
6528 return unless $conf->exists($template);
6530 my $from = $conf->config('invoice_from') if $conf->exists('invoice_from');
6531 $from = $options{from} if exists($options{from});
6533 my $to = join(',', $customer->invoicing_list_emailonly);
6534 $to = $options{to} if exists($options{to});
6536 my $subject = "Notice from " . $conf->config('company_name')
6537 if $conf->exists('company_name');
6538 $subject = $options{subject} if exists($options{subject});
6540 my $notify_template = new Text::Template (TYPE => 'ARRAY',
6541 SOURCE => [ map "$_\n",
6542 $conf->config($template)]
6544 or die "can't create new Text::Template object: Text::Template::ERROR";
6545 $notify_template->compile()
6546 or die "can't compile template: Text::Template::ERROR";
6548 $FS::notify_template::_template::company_name = $conf->config('company_name');
6549 $FS::notify_template::_template::company_address =
6550 join("\n", $conf->config('company_address') ). "\n";
6552 my $paydate = $customer->paydate || '2037-12-31';
6553 $FS::notify_template::_template::first = $customer->first;
6554 $FS::notify_template::_template::last = $customer->last;
6555 $FS::notify_template::_template::company = $customer->company;
6556 $FS::notify_template::_template::payinfo = $customer->mask_payinfo;
6557 my $payby = $customer->payby;
6558 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6559 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6561 #credit cards expire at the end of the month/year of their exp date
6562 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6563 $FS::notify_template::_template::payby = 'credit card';
6564 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6565 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6567 }elsif ($payby eq 'COMP') {
6568 $FS::notify_template::_template::payby = 'complimentary account';
6570 $FS::notify_template::_template::payby = 'current method';
6572 $FS::notify_template::_template::expdate = $expire_time;
6574 for (keys %{$options{extra_fields}}){
6576 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
6579 send_email(from => $from,
6581 subject => $subject,
6582 body => $notify_template->fill_in( PACKAGE =>
6583 'FS::notify_template::_template' ),
6588 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
6590 Generates a templated notification to the customer (see L<Text::Template>).
6592 OPTIONS is a hash and may include
6594 I<extra_fields> - a hashref of name/value pairs which will be substituted
6595 into the template. These values may override values mentioned below
6596 and those from the customer record.
6598 The following variables are available in the template instead of or in addition
6599 to the fields of the customer record.
6601 I<$payby> - a description of the method of payment for the customer
6602 # would be nice to use FS::payby::shortname
6603 I<$payinfo> - the masked account information used to collect for this customer
6604 I<$expdate> - the expiration of the customer payment method in seconds from epoch
6605 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
6609 sub generate_letter {
6610 my ($self, $template, %options) = @_;
6612 return unless $conf->exists($template);
6614 my $letter_template = new Text::Template
6616 SOURCE => [ map "$_\n", $conf->config($template)],
6617 DELIMITERS => [ '[@--', '--@]' ],
6619 or die "can't create new Text::Template object: Text::Template::ERROR";
6621 $letter_template->compile()
6622 or die "can't compile template: Text::Template::ERROR";
6624 my %letter_data = map { $_ => $self->$_ } $self->fields;
6625 $letter_data{payinfo} = $self->mask_payinfo;
6627 #my $paydate = $self->paydate || '2037-12-31';
6628 my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
6630 my $payby = $self->payby;
6631 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6632 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6634 #credit cards expire at the end of the month/year of their exp date
6635 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6636 $letter_data{payby} = 'credit card';
6637 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6638 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6640 }elsif ($payby eq 'COMP') {
6641 $letter_data{payby} = 'complimentary account';
6643 $letter_data{payby} = 'current method';
6645 $letter_data{expdate} = $expire_time;
6647 for (keys %{$options{extra_fields}}){
6648 $letter_data{$_} = $options{extra_fields}->{$_};
6651 unless(exists($letter_data{returnaddress})){
6652 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
6653 $self->agent_template)
6655 if ( length($retadd) ) {
6656 $letter_data{returnaddress} = $retadd;
6657 } elsif ( grep /\S/, $conf->config('company_address') ) {
6658 $letter_data{returnaddress} =
6659 join( '\\*'."\n", map s/( {2,})/'~' x length($1)/eg,
6660 $conf->config('company_address')
6663 $letter_data{returnaddress} = '~';
6667 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
6669 $letter_data{company_name} = $conf->config('company_name');
6671 my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
6672 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
6676 ) or die "can't open temp file: $!\n";
6678 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
6680 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
6684 =item print_ps TEMPLATE
6686 Returns an postscript letter filled in from TEMPLATE, as a scalar.
6692 my $file = $self->generate_letter(@_);
6693 FS::Misc::generate_ps($file);
6696 =item print TEMPLATE
6698 Prints the filled in template.
6700 TEMPLATE is the name of a L<Text::Template> to fill in and print.
6704 sub queueable_print {
6707 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
6708 or die "invalid customer number: " . $opt{custvnum};
6710 my $error = $self->print( $opt{template} );
6711 die $error if $error;
6715 my ($self, $template) = (shift, shift);
6716 do_print [ $self->print_ps($template) ];
6719 sub agent_template {
6721 $self->_agent_plandata('agent_templatename');
6724 sub agent_invoice_from {
6726 $self->_agent_plandata('agent_invoice_from');
6729 sub _agent_plandata {
6730 my( $self, $option ) = @_;
6732 #yuck. this whole thing needs to be reconciled better with 1.9's idea of
6733 #agent-specific Conf
6735 use FS::part_event::Condition;
6737 my $agentnum = $self->agentnum;
6740 if ( driver_name =~ /^Pg/i ) {
6742 } elsif ( driver_name =~ /^mysql/i ) {
6745 die "don't know how to use regular expressions in ". driver_name. " databases";
6748 my $part_event_option =
6750 'select' => 'part_event_option.*',
6751 'table' => 'part_event_option',
6753 LEFT JOIN part_event USING ( eventpart )
6754 LEFT JOIN part_event_option AS peo_agentnum
6755 ON ( part_event.eventpart = peo_agentnum.eventpart
6756 AND peo_agentnum.optionname = 'agentnum'
6757 AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
6759 LEFT JOIN part_event_option AS peo_cust_bill_age
6760 ON ( part_event.eventpart = peo_cust_bill_age.eventpart
6761 AND peo_cust_bill_age.optionname = 'cust_bill_age'
6764 #'hashref' => { 'optionname' => $option },
6765 #'hashref' => { 'part_event_option.optionname' => $option },
6767 " WHERE part_event_option.optionname = ". dbh->quote($option).
6768 " AND action = 'cust_bill_send_agent' ".
6769 " AND ( disabled IS NULL OR disabled != 'Y' ) ".
6770 " AND peo_agentnum.optionname = 'agentnum' ".
6771 " AND agentnum IS NULL OR agentnum = $agentnum ".
6773 CASE WHEN peo_cust_bill_age.optionname != 'cust_bill_age'
6775 ELSE ". FS::part_event::Condition->age2seconds_sql('peo_cust_bill_age.optionvalue').
6777 , part_event.weight".
6781 unless ( $part_event_option ) {
6782 return $self->agent->invoice_template || ''
6783 if $option eq 'agent_templatename';
6787 $part_event_option->optionvalue;
6792 ## actual sub, not a method, designed to be called from the queue.
6793 ## sets up the customer, and calls the bill_and_collect
6794 my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
6795 my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
6796 $cust_main->bill_and_collect(
6807 The delete method should possibly take an FS::cust_main object reference
6808 instead of a scalar customer number.
6810 Bill and collect options should probably be passed as references instead of a
6813 There should probably be a configuration file with a list of allowed credit
6816 No multiple currency support (probably a larger project than just this module).
6818 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
6820 Birthdates rely on negative epoch values.
6822 The payby for card/check batches is broken. With mixed batching, bad
6825 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
6829 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
6830 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
6831 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.