4 use vars qw( @ISA @EXPORT_OK $DEBUG $me $conf @encrypted_fields
5 $import $skip_fuzzyfiles $ignore_expired_card @paytypes);
6 use vars qw( $realtime_bop_decline_quiet ); #ugh
11 eval "use Time::Local;";
12 die "Time::Local minimum version 1.05 required with Perl versions before 5.6"
13 if $] < 5.006 && !defined($Time::Local::VERSION);
14 #eval "use Time::Local qw(timelocal timelocal_nocheck);";
15 eval "use Time::Local qw(timelocal_nocheck);";
17 use Digest::MD5 qw(md5_base64);
21 use File::Slurp qw( slurp );
22 use File::Temp qw( tempfile );
23 use String::Approx qw(amatch);
24 use Business::CreditCard 0.28;
27 use FS::UID qw( getotaker dbh driver_name );
28 use FS::Record qw( qsearchs qsearch dbdef );
29 use FS::Misc qw( generate_email send_email generate_ps do_print );
30 use FS::Msgcat qw(gettext);
35 use FS::cust_bill_pkg;
37 use FS::cust_pay_pending;
38 use FS::cust_pay_void;
41 use FS::part_referral;
42 use FS::cust_main_county;
44 use FS::cust_main_invoice;
45 use FS::cust_credit_bill;
46 use FS::cust_bill_pay;
47 use FS::prepay_credit;
50 use FS::part_bill_event qw(due_events);
51 use FS::cust_bill_event;
52 use FS::cust_tax_exempt;
53 use FS::cust_tax_exempt_pkg;
55 use FS::payment_gateway;
56 use FS::agent_payment_gateway;
58 use FS::payinfo_Mixin;
60 @ISA = qw( FS::Record FS::payinfo_Mixin );
62 @EXPORT_OK = qw( smart_search );
64 $realtime_bop_decline_quiet = 0;
66 # 1 is mostly method/subroutine entry and options
67 # 2 traces progress of some operations
68 # 3 is even more information including possibly sensitive data
70 $me = '[FS::cust_main]';
74 $ignore_expired_card = 0;
76 @encrypted_fields = ('payinfo', 'paycvv');
77 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
79 #ask FS::UID to run this stuff for us later
80 #$FS::UID::callback{'FS::cust_main'} = sub {
81 install_callback FS::UID sub {
83 #yes, need it for stuff below (prolly should be cached)
88 my ( $hashref, $cache ) = @_;
89 if ( exists $hashref->{'pkgnum'} ) {
90 #@{ $self->{'_pkgnum'} } = ();
91 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
92 $self->{'_pkgnum'} = $subcache;
93 #push @{ $self->{'_pkgnum'} },
94 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
100 FS::cust_main - Object methods for cust_main records
106 $record = new FS::cust_main \%hash;
107 $record = new FS::cust_main { 'column' => 'value' };
109 $error = $record->insert;
111 $error = $new_record->replace($old_record);
113 $error = $record->delete;
115 $error = $record->check;
117 @cust_pkg = $record->all_pkgs;
119 @cust_pkg = $record->ncancelled_pkgs;
121 @cust_pkg = $record->suspended_pkgs;
123 $error = $record->bill;
124 $error = $record->bill %options;
125 $error = $record->bill 'time' => $time;
127 $error = $record->collect;
128 $error = $record->collect %options;
129 $error = $record->collect 'invoice_time' => $time,
134 An FS::cust_main object represents a customer. FS::cust_main inherits from
135 FS::Record. The following fields are currently supported:
139 =item custnum - primary key (assigned automatically for new customers)
141 =item agentnum - agent (see L<FS::agent>)
143 =item refnum - Advertising source (see L<FS::part_referral>)
149 =item ss - social security number (optional)
151 =item company - (optional)
155 =item address2 - (optional)
159 =item county - (optional, see L<FS::cust_main_county>)
161 =item state - (see L<FS::cust_main_county>)
165 =item country - (see L<FS::cust_main_county>)
167 =item daytime - phone (optional)
169 =item night - phone (optional)
171 =item fax - phone (optional)
173 =item ship_first - name
175 =item ship_last - name
177 =item ship_company - (optional)
181 =item ship_address2 - (optional)
185 =item ship_county - (optional, see L<FS::cust_main_county>)
187 =item ship_state - (see L<FS::cust_main_county>)
191 =item ship_country - (see L<FS::cust_main_county>)
193 =item ship_daytime - phone (optional)
195 =item ship_night - phone (optional)
197 =item ship_fax - phone (optional)
199 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
201 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
203 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
207 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
209 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
211 =item paystart_month - start date month (maestro/solo cards only)
213 =item paystart_year - start date year (maestro/solo cards only)
215 =item payissue - issue number (maestro/solo cards only)
217 =item payname - name on card or billing name
219 =item payip - IP address from which payment information was received
221 =item tax - tax exempt, empty or `Y'
223 =item otaker - order taker (assigned automatically, see L<FS::UID>)
225 =item comments - comments (optional)
227 =item referral_custnum - referring customer number
229 =item spool_cdr - Enable individual CDR spooling, empty or `Y'
231 =item dundate - a suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
241 Creates a new customer. To add the customer to the database, see L<"insert">.
243 Note that this stores the hash reference, not a distinct copy of the hash it
244 points to. You can ask the object for a copy with the I<hash> method.
248 sub table { 'cust_main'; }
250 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
252 Adds this customer to the database. If there is an error, returns the error,
253 otherwise returns false.
255 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
256 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
257 are inserted atomicly, or the transaction is rolled back. Passing an empty
258 hash reference is equivalent to not supplying this parameter. There should be
259 a better explanation of this, but until then, here's an example:
262 tie %hash, 'Tie::RefHash'; #this part is important
264 $cust_pkg => [ $svc_acct ],
267 $cust_main->insert( \%hash );
269 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
270 be set as the invoicing list (see L<"invoicing_list">). Errors return as
271 expected and rollback the entire transaction; it is not necessary to call
272 check_invoicing_list first. The invoicing_list is set after the records in the
273 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
274 invoicing_list destination to the newly-created svc_acct. Here's an example:
276 $cust_main->insert( {}, [ $email, 'POST' ] );
278 Currently available options are: I<depend_jobnum> and I<noexport>.
280 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
281 on the supplied jobnum (they will not run until the specific job completes).
282 This can be used to defer provisioning until some action completes (such
283 as running the customer's credit card successfully).
285 The I<noexport> option is deprecated. If I<noexport> is set true, no
286 provisioning jobs (exports) are scheduled. (You can schedule them later with
287 the B<reexport> method.)
293 my $cust_pkgs = @_ ? shift : {};
294 my $invoicing_list = @_ ? shift : '';
296 warn "$me insert called with options ".
297 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
300 local $SIG{HUP} = 'IGNORE';
301 local $SIG{INT} = 'IGNORE';
302 local $SIG{QUIT} = 'IGNORE';
303 local $SIG{TERM} = 'IGNORE';
304 local $SIG{TSTP} = 'IGNORE';
305 local $SIG{PIPE} = 'IGNORE';
307 my $oldAutoCommit = $FS::UID::AutoCommit;
308 local $FS::UID::AutoCommit = 0;
311 my $prepay_identifier = '';
312 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
314 if ( $self->payby eq 'PREPAY' ) {
316 $self->payby('BILL');
317 $prepay_identifier = $self->payinfo;
320 warn " looking up prepaid card $prepay_identifier\n"
323 my $error = $self->get_prepay( $prepay_identifier,
324 'amount_ref' => \$amount,
325 'seconds_ref' => \$seconds,
326 'upbytes_ref' => \$upbytes,
327 'downbytes_ref' => \$downbytes,
328 'totalbytes_ref' => \$totalbytes,
331 $dbh->rollback if $oldAutoCommit;
332 #return "error applying prepaid card (transaction rolled back): $error";
336 $payby = 'PREP' if $amount;
338 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
341 $self->payby('BILL');
342 $amount = $self->paid;
346 warn " inserting $self\n"
349 $self->signupdate(time) unless $self->signupdate;
351 my $error = $self->SUPER::insert;
353 $dbh->rollback if $oldAutoCommit;
354 #return "inserting cust_main record (transaction rolled back): $error";
358 warn " setting invoicing list\n"
361 if ( $invoicing_list ) {
362 $error = $self->check_invoicing_list( $invoicing_list );
364 $dbh->rollback if $oldAutoCommit;
365 #return "checking invoicing_list (transaction rolled back): $error";
368 $self->invoicing_list( $invoicing_list );
371 if ( $conf->config('cust_main-skeleton_tables')
372 && $conf->config('cust_main-skeleton_custnum') ) {
374 warn " inserting skeleton records\n"
377 my $error = $self->start_copy_skel;
379 $dbh->rollback if $oldAutoCommit;
385 warn " ordering packages\n"
388 $error = $self->order_pkgs( $cust_pkgs,
390 'seconds_ref' => \$seconds,
391 'upbytes_ref' => \$upbytes,
392 'downbytes_ref' => \$downbytes,
393 'totalbytes_ref' => \$totalbytes,
396 $dbh->rollback if $oldAutoCommit;
401 $dbh->rollback if $oldAutoCommit;
402 return "No svc_acct record to apply pre-paid time";
404 if ( $upbytes || $downbytes || $totalbytes ) {
405 $dbh->rollback if $oldAutoCommit;
406 return "No svc_acct record to apply pre-paid data";
410 warn " inserting initial $payby payment of $amount\n"
412 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
414 $dbh->rollback if $oldAutoCommit;
415 return "inserting payment (transaction rolled back): $error";
419 unless ( $import || $skip_fuzzyfiles ) {
420 warn " queueing fuzzyfiles update\n"
422 $error = $self->queue_fuzzyfiles_update;
424 $dbh->rollback if $oldAutoCommit;
425 return "updating fuzzy search cache: $error";
429 warn " insert complete; committing transaction\n"
432 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
437 sub start_copy_skel {
440 #'mg_user_preference' => {},
441 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
442 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
443 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
444 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
445 my @tables = eval($conf->config_binary('cust_main-skeleton_tables'));
448 _copy_skel( 'cust_main', #tablename
449 $conf->config('cust_main-skeleton_custnum'), #sourceid
450 $self->custnum, #destid
451 @tables, #child tables
455 #recursive subroutine, not a method
457 my( $table, $sourceid, $destid, %child_tables ) = @_;
460 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
461 ( $table, $primary_key ) = ( $1, $2 );
463 my $dbdef_table = dbdef->table($table);
464 $primary_key = $dbdef_table->primary_key
465 or return "$table has no primary key".
466 " (or do you need to run dbdef-create?)";
469 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
470 join (', ', keys %child_tables). "\n"
473 foreach my $child_table_def ( keys %child_tables ) {
477 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
478 ( $child_table, $child_pkey ) = ( $1, $2 );
480 $child_table = $child_table_def;
482 $child_pkey = dbdef->table($child_table)->primary_key;
483 # or return "$table has no primary key".
484 # " (or do you need to run dbdef-create?)\n";
488 if ( keys %{ $child_tables{$child_table_def} } ) {
490 return "$child_table has no primary key".
491 " (run dbdef-create or try specifying it?)\n"
494 #false laziness w/Record::insert and only works on Pg
495 #refactor the proper last-inserted-id stuff out of Record::insert if this
496 # ever gets use for anything besides a quick kludge for one customer
497 my $default = dbdef->table($child_table)->column($child_pkey)->default;
498 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
499 or return "can't parse $child_table.$child_pkey default value ".
500 " for sequence name: $default";
505 my @sel_columns = grep { $_ ne $primary_key }
506 dbdef->table($child_table)->columns;
507 my $sel_columns = join(', ', @sel_columns );
509 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
510 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
511 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
513 my $sel_st = "SELECT $sel_columns FROM $child_table".
514 " WHERE $primary_key = $sourceid";
517 my $sel_sth = dbh->prepare( $sel_st )
518 or return dbh->errstr;
520 $sel_sth->execute or return $sel_sth->errstr;
522 while ( my $row = $sel_sth->fetchrow_hashref ) {
524 warn " selected row: ".
525 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
529 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
530 my $ins_sth =dbh->prepare($statement)
531 or return dbh->errstr;
532 my @param = ( $destid, map $row->{$_}, @ins_columns );
533 warn " $statement: [ ". join(', ', @param). " ]\n"
535 $ins_sth->execute( @param )
536 or return $ins_sth->errstr;
538 #next unless keys %{ $child_tables{$child_table} };
539 next unless $sequence;
541 #another section of that laziness
542 my $seq_sql = "SELECT currval('$sequence')";
543 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
544 $seq_sth->execute or return $seq_sth->errstr;
545 my $insertid = $seq_sth->fetchrow_arrayref->[0];
547 # don't drink soap! recurse! recurse! okay!
549 _copy_skel( $child_table_def,
550 $row->{$child_pkey}, #sourceid
552 %{ $child_tables{$child_table_def} },
554 return $error if $error;
564 #deprecated #=item order_pkgs HASHREF [ , SECONDSREF ] [ , OPTION => VALUE ... ]
565 =item order_pkgs HASHREF [ , OPTION => VALUE ... ]
567 Like the insert method on an existing record, this method orders a package
568 and included services atomicaly. Pass a Tie::RefHash data structure to this
569 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
570 be a better explanation of this, but until then, here's an example:
573 tie %hash, 'Tie::RefHash'; #this part is important
575 $cust_pkg => [ $svc_acct ],
578 $cust_main->order_pkgs( \%hash, 'noexport'=>1 );
580 Services can be new, in which case they are inserted, or existing unaudited
581 services, in which case they are linked to the newly-created package.
583 Currently available options are: I<depend_jobnum>, I<noexport>, I<seconds_ref>,
584 I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>.
586 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
587 on the supplied jobnum (they will not run until the specific job completes).
588 This can be used to defer provisioning until some action completes (such
589 as running the customer's credit card successfully).
591 The I<noexport> option is deprecated. If I<noexport> is set true, no
592 provisioning jobs (exports) are scheduled. (You can schedule them later with
593 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
594 on the cust_main object is not recommended, as existing services will also be
597 If I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, or I<totalbytes_ref> is
598 provided, the scalars (provided by references) will be incremented by the
599 values of the prepaid card.`
605 my $cust_pkgs = shift;
606 my $seconds_ref = ref($_[0]) ? shift : ''; #deprecated
608 $seconds_ref ||= $options{'seconds_ref'};
610 my %svc_options = ();
611 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
612 if exists $options{'depend_jobnum'};
613 warn "$me order_pkgs called with options ".
614 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
617 local $SIG{HUP} = 'IGNORE';
618 local $SIG{INT} = 'IGNORE';
619 local $SIG{QUIT} = 'IGNORE';
620 local $SIG{TERM} = 'IGNORE';
621 local $SIG{TSTP} = 'IGNORE';
622 local $SIG{PIPE} = 'IGNORE';
624 my $oldAutoCommit = $FS::UID::AutoCommit;
625 local $FS::UID::AutoCommit = 0;
628 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
630 foreach my $cust_pkg ( keys %$cust_pkgs ) {
631 $cust_pkg->custnum( $self->custnum );
632 my $error = $cust_pkg->insert;
634 $dbh->rollback if $oldAutoCommit;
635 return "inserting cust_pkg (transaction rolled back): $error";
637 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
638 if ( $svc_something->svcnum ) {
639 my $old_cust_svc = $svc_something->cust_svc;
640 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
641 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
642 $error = $new_cust_svc->replace($old_cust_svc);
644 $svc_something->pkgnum( $cust_pkg->pkgnum );
645 if ( $svc_something->isa('FS::svc_acct') ) {
646 foreach ( grep { $options{$_.'_ref'} && ${ $options{$_.'_ref'} } }
647 qw( seconds upbytes downbytes totalbytes )
649 $svc_something->$_( $svc_something->$_() + ${$options{$_.'_ref'}} );
650 ${ $options{$_.'_ref'} } = 0;
653 $error = $svc_something->insert(%svc_options);
656 $dbh->rollback if $oldAutoCommit;
657 #return "inserting svc_ (transaction rolled back): $error";
663 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
667 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
669 Recharges this (existing) customer with the specified prepaid card (see
670 L<FS::prepay_credit>), specified either by I<identifier> or as an
671 FS::prepay_credit object. If there is an error, returns the error, otherwise
674 Optionally, five scalar references can be passed as well. They will have their
675 values filled in with the amount, number of seconds, and number of upload,
676 download, and total bytes applied by this prepaid card.
680 #the ref bullshit here should be refactored like get_prepay. MyAccount.pm is
681 #the only place that uses these args
682 sub recharge_prepay {
683 my( $self, $prepay_credit, $amountref, $secondsref,
684 $upbytesref, $downbytesref, $totalbytesref ) = @_;
686 local $SIG{HUP} = 'IGNORE';
687 local $SIG{INT} = 'IGNORE';
688 local $SIG{QUIT} = 'IGNORE';
689 local $SIG{TERM} = 'IGNORE';
690 local $SIG{TSTP} = 'IGNORE';
691 local $SIG{PIPE} = 'IGNORE';
693 my $oldAutoCommit = $FS::UID::AutoCommit;
694 local $FS::UID::AutoCommit = 0;
697 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
699 my $error = $self->get_prepay( $prepay_credit,
700 'amount_ref' => \$amount,
701 'seconds_ref' => \$seconds,
702 'upbytes_ref' => \$upbytes,
703 'downbytes_ref' => \$downbytes,
704 'totalbytes_ref' => \$totalbytes,
706 || $self->increment_seconds($seconds)
707 || $self->increment_upbytes($upbytes)
708 || $self->increment_downbytes($downbytes)
709 || $self->increment_totalbytes($totalbytes)
710 || $self->insert_cust_pay_prepay( $amount,
712 ? $prepay_credit->identifier
717 $dbh->rollback if $oldAutoCommit;
721 if ( defined($amountref) ) { $$amountref = $amount; }
722 if ( defined($secondsref) ) { $$secondsref = $seconds; }
723 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
724 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
725 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
727 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
732 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
734 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
735 specified either by I<identifier> or as an FS::prepay_credit object.
737 Available options are: I<amount_ref>, I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>. The scalars (provided by references) will be
738 incremented by the values of the prepaid card.
740 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
741 check or set this customer's I<agentnum>.
743 If there is an error, returns the error, otherwise returns false.
749 my( $self, $prepay_credit, %opt ) = @_;
751 local $SIG{HUP} = 'IGNORE';
752 local $SIG{INT} = 'IGNORE';
753 local $SIG{QUIT} = 'IGNORE';
754 local $SIG{TERM} = 'IGNORE';
755 local $SIG{TSTP} = 'IGNORE';
756 local $SIG{PIPE} = 'IGNORE';
758 my $oldAutoCommit = $FS::UID::AutoCommit;
759 local $FS::UID::AutoCommit = 0;
762 unless ( ref($prepay_credit) ) {
764 my $identifier = $prepay_credit;
766 $prepay_credit = qsearchs(
768 { 'identifier' => $prepay_credit },
773 unless ( $prepay_credit ) {
774 $dbh->rollback if $oldAutoCommit;
775 return "Invalid prepaid card: ". $identifier;
780 if ( $prepay_credit->agentnum ) {
781 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
782 $dbh->rollback if $oldAutoCommit;
783 return "prepaid card not valid for agent ". $self->agentnum;
785 $self->agentnum($prepay_credit->agentnum);
788 my $error = $prepay_credit->delete;
790 $dbh->rollback if $oldAutoCommit;
791 return "removing prepay_credit (transaction rolled back): $error";
794 ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
795 for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
797 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
802 =item increment_upbytes SECONDS
804 Updates this customer's single or primary account (see L<FS::svc_acct>) by
805 the specified number of upbytes. If there is an error, returns the error,
806 otherwise returns false.
810 sub increment_upbytes {
811 _increment_column( shift, 'upbytes', @_);
814 =item increment_downbytes SECONDS
816 Updates this customer's single or primary account (see L<FS::svc_acct>) by
817 the specified number of downbytes. If there is an error, returns the error,
818 otherwise returns false.
822 sub increment_downbytes {
823 _increment_column( shift, 'downbytes', @_);
826 =item increment_totalbytes SECONDS
828 Updates this customer's single or primary account (see L<FS::svc_acct>) by
829 the specified number of totalbytes. If there is an error, returns the error,
830 otherwise returns false.
834 sub increment_totalbytes {
835 _increment_column( shift, 'totalbytes', @_);
838 =item increment_seconds SECONDS
840 Updates this customer's single or primary account (see L<FS::svc_acct>) by
841 the specified number of seconds. If there is an error, returns the error,
842 otherwise returns false.
846 sub increment_seconds {
847 _increment_column( shift, 'seconds', @_);
850 =item _increment_column AMOUNT
852 Updates this customer's single or primary account (see L<FS::svc_acct>) by
853 the specified number of seconds or bytes. If there is an error, returns
854 the error, otherwise returns false.
858 sub _increment_column {
859 my( $self, $column, $amount ) = @_;
860 warn "$me increment_column called: $column, $amount\n"
863 return '' unless $amount;
865 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
866 $self->ncancelled_pkgs;
869 return 'No packages with primary or single services found'.
870 ' to apply pre-paid time';
871 } elsif ( scalar(@cust_pkg) > 1 ) {
872 #maybe have a way to specify the package/account?
873 return 'Multiple packages found to apply pre-paid time';
876 my $cust_pkg = $cust_pkg[0];
877 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
881 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
884 return 'No account found to apply pre-paid time';
885 } elsif ( scalar(@cust_svc) > 1 ) {
886 return 'Multiple accounts found to apply pre-paid time';
889 my $svc_acct = $cust_svc[0]->svc_x;
890 warn " found service svcnum ". $svc_acct->pkgnum.
891 ' ('. $svc_acct->email. ")\n"
894 $column = "increment_$column";
895 $svc_acct->$column($amount);
899 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
901 Inserts a prepayment in the specified amount for this customer. An optional
902 second argument can specify the prepayment identifier for tracking purposes.
903 If there is an error, returns the error, otherwise returns false.
907 sub insert_cust_pay_prepay {
908 shift->insert_cust_pay('PREP', @_);
911 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
913 Inserts a cash payment in the specified amount for this customer. An optional
914 second argument can specify the payment identifier for tracking purposes.
915 If there is an error, returns the error, otherwise returns false.
919 sub insert_cust_pay_cash {
920 shift->insert_cust_pay('CASH', @_);
923 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
925 Inserts a Western Union payment in the specified amount for this customer. An
926 optional second argument can specify the prepayment identifier for tracking
927 purposes. If there is an error, returns the error, otherwise returns false.
931 sub insert_cust_pay_west {
932 shift->insert_cust_pay('WEST', @_);
935 sub insert_cust_pay {
936 my( $self, $payby, $amount ) = splice(@_, 0, 3);
937 my $payinfo = scalar(@_) ? shift : '';
939 my $cust_pay = new FS::cust_pay {
940 'custnum' => $self->custnum,
941 'paid' => sprintf('%.2f', $amount),
942 #'_date' => #date the prepaid card was purchased???
944 'payinfo' => $payinfo,
952 This method is deprecated. See the I<depend_jobnum> option to the insert and
953 order_pkgs methods for a better way to defer provisioning.
955 Re-schedules all exports by calling the B<reexport> method of all associated
956 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
957 otherwise returns false.
964 carp "WARNING: FS::cust_main::reexport is deprectated; ".
965 "use the depend_jobnum option to insert or order_pkgs to delay export";
967 local $SIG{HUP} = 'IGNORE';
968 local $SIG{INT} = 'IGNORE';
969 local $SIG{QUIT} = 'IGNORE';
970 local $SIG{TERM} = 'IGNORE';
971 local $SIG{TSTP} = 'IGNORE';
972 local $SIG{PIPE} = 'IGNORE';
974 my $oldAutoCommit = $FS::UID::AutoCommit;
975 local $FS::UID::AutoCommit = 0;
978 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
979 my $error = $cust_pkg->reexport;
981 $dbh->rollback if $oldAutoCommit;
986 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
991 =item delete NEW_CUSTNUM
993 This deletes the customer. If there is an error, returns the error, otherwise
996 This will completely remove all traces of the customer record. This is not
997 what you want when a customer cancels service; for that, cancel all of the
998 customer's packages (see L</cancel>).
1000 If the customer has any uncancelled packages, you need to pass a new (valid)
1001 customer number for those packages to be transferred to. Cancelled packages
1002 will be deleted. Did I mention that this is NOT what you want when a customer
1003 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
1005 You can't delete a customer with invoices (see L<FS::cust_bill>),
1006 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
1007 refunds (see L<FS::cust_refund>).
1014 local $SIG{HUP} = 'IGNORE';
1015 local $SIG{INT} = 'IGNORE';
1016 local $SIG{QUIT} = 'IGNORE';
1017 local $SIG{TERM} = 'IGNORE';
1018 local $SIG{TSTP} = 'IGNORE';
1019 local $SIG{PIPE} = 'IGNORE';
1021 my $oldAutoCommit = $FS::UID::AutoCommit;
1022 local $FS::UID::AutoCommit = 0;
1025 if ( $self->cust_bill ) {
1026 $dbh->rollback if $oldAutoCommit;
1027 return "Can't delete a customer with invoices";
1029 if ( $self->cust_credit ) {
1030 $dbh->rollback if $oldAutoCommit;
1031 return "Can't delete a customer with credits";
1033 if ( $self->cust_pay ) {
1034 $dbh->rollback if $oldAutoCommit;
1035 return "Can't delete a customer with payments";
1037 if ( $self->cust_refund ) {
1038 $dbh->rollback if $oldAutoCommit;
1039 return "Can't delete a customer with refunds";
1042 my @cust_pkg = $self->ncancelled_pkgs;
1044 my $new_custnum = shift;
1045 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1046 $dbh->rollback if $oldAutoCommit;
1047 return "Invalid new customer number: $new_custnum";
1049 foreach my $cust_pkg ( @cust_pkg ) {
1050 my %hash = $cust_pkg->hash;
1051 $hash{'custnum'} = $new_custnum;
1052 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1053 my $error = $new_cust_pkg->replace($cust_pkg,
1054 options => { $cust_pkg->options },
1057 $dbh->rollback if $oldAutoCommit;
1062 my @cancelled_cust_pkg = $self->all_pkgs;
1063 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1064 my $error = $cust_pkg->delete;
1066 $dbh->rollback if $oldAutoCommit;
1071 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1072 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1074 my $error = $cust_main_invoice->delete;
1076 $dbh->rollback if $oldAutoCommit;
1081 my $error = $self->SUPER::delete;
1083 $dbh->rollback if $oldAutoCommit;
1087 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1092 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
1094 Replaces the OLD_RECORD with this one in the database. If there is an error,
1095 returns the error, otherwise returns false.
1097 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1098 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1099 expected and rollback the entire transaction; it is not necessary to call
1100 check_invoicing_list first. Here's an example:
1102 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1110 warn "$me replace called\n"
1113 local $SIG{HUP} = 'IGNORE';
1114 local $SIG{INT} = 'IGNORE';
1115 local $SIG{QUIT} = 'IGNORE';
1116 local $SIG{TERM} = 'IGNORE';
1117 local $SIG{TSTP} = 'IGNORE';
1118 local $SIG{PIPE} = 'IGNORE';
1120 # We absolutely have to have an old vs. new record to make this work.
1121 if (!defined($old)) {
1122 $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
1125 my $curuser = $FS::CurrentUser::CurrentUser;
1126 if ( $self->payby eq 'COMP'
1127 && $self->payby ne $old->payby
1128 && ! $curuser->access_right('Complimentary customer')
1131 return "You are not permitted to create complimentary accounts.";
1134 local($ignore_expired_card) = 1
1135 if $old->payby =~ /^(CARD|DCRD)$/
1136 && $self->payby =~ /^(CARD|DCRD)$/
1137 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1139 my $oldAutoCommit = $FS::UID::AutoCommit;
1140 local $FS::UID::AutoCommit = 0;
1143 my $error = $self->SUPER::replace($old);
1146 $dbh->rollback if $oldAutoCommit;
1150 if ( @param ) { # INVOICING_LIST_ARYREF
1151 my $invoicing_list = shift @param;
1152 $error = $self->check_invoicing_list( $invoicing_list );
1154 $dbh->rollback if $oldAutoCommit;
1157 $self->invoicing_list( $invoicing_list );
1160 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1161 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1162 # card/check/lec info has changed, want to retry realtime_ invoice events
1163 my $error = $self->retry_realtime;
1165 $dbh->rollback if $oldAutoCommit;
1170 unless ( $import || $skip_fuzzyfiles ) {
1171 $error = $self->queue_fuzzyfiles_update;
1173 $dbh->rollback if $oldAutoCommit;
1174 return "updating fuzzy search cache: $error";
1178 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1183 =item queue_fuzzyfiles_update
1185 Used by insert & replace to update the fuzzy search cache
1189 sub queue_fuzzyfiles_update {
1192 local $SIG{HUP} = 'IGNORE';
1193 local $SIG{INT} = 'IGNORE';
1194 local $SIG{QUIT} = 'IGNORE';
1195 local $SIG{TERM} = 'IGNORE';
1196 local $SIG{TSTP} = 'IGNORE';
1197 local $SIG{PIPE} = 'IGNORE';
1199 my $oldAutoCommit = $FS::UID::AutoCommit;
1200 local $FS::UID::AutoCommit = 0;
1203 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1204 my $error = $queue->insert( map $self->getfield($_),
1205 qw(first last company)
1208 $dbh->rollback if $oldAutoCommit;
1209 return "queueing job (transaction rolled back): $error";
1212 if ( $self->ship_last ) {
1213 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1214 $error = $queue->insert( map $self->getfield("ship_$_"),
1215 qw(first last company)
1218 $dbh->rollback if $oldAutoCommit;
1219 return "queueing job (transaction rolled back): $error";
1223 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1230 Checks all fields to make sure this is a valid customer record. If there is
1231 an error, returns the error, otherwise returns false. Called by the insert
1232 and replace methods.
1239 warn "$me check BEFORE: \n". $self->_dump
1243 $self->ut_numbern('custnum')
1244 || $self->ut_number('agentnum')
1245 || $self->ut_textn('agent_custid')
1246 || $self->ut_number('refnum')
1247 || $self->ut_textn('custbatch')
1248 || $self->ut_name('last')
1249 || $self->ut_name('first')
1250 || $self->ut_snumbern('birthdate')
1251 || $self->ut_snumbern('signupdate')
1252 || $self->ut_textn('company')
1253 || $self->ut_text('address1')
1254 || $self->ut_textn('address2')
1255 || $self->ut_text('city')
1256 || $self->ut_textn('county')
1257 || $self->ut_textn('state')
1258 || $self->ut_country('country')
1259 || $self->ut_anything('comments')
1260 || $self->ut_numbern('referral_custnum')
1261 || $self->ut_textn('stateid')
1262 || $self->ut_textn('stateid_state')
1264 #barf. need message catalogs. i18n. etc.
1265 $error .= "Please select an advertising source."
1266 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1267 return $error if $error;
1269 return "Unknown agent"
1270 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1272 return "Unknown refnum"
1273 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1275 return "Unknown referring custnum: ". $self->referral_custnum
1276 unless ! $self->referral_custnum
1277 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1279 if ( $self->ss eq '' ) {
1284 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1285 or return "Illegal social security number: ". $self->ss;
1286 $self->ss("$1-$2-$3");
1290 # bad idea to disable, causes billing to fail because of no tax rates later
1291 # unless ( $import ) {
1292 unless ( qsearch('cust_main_county', {
1293 'country' => $self->country,
1296 return "Unknown state/county/country: ".
1297 $self->state. "/". $self->county. "/". $self->country
1298 unless qsearch('cust_main_county',{
1299 'state' => $self->state,
1300 'county' => $self->county,
1301 'country' => $self->country,
1307 $self->ut_phonen('daytime', $self->country)
1308 || $self->ut_phonen('night', $self->country)
1309 || $self->ut_phonen('fax', $self->country)
1310 || $self->ut_zip('zip', $self->country)
1312 return $error if $error;
1314 if ( $conf->exists('cust_main-require_phone')
1315 && ! length($self->daytime) && ! length($self->night)
1318 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1320 : FS::Msgcat::_gettext('daytime');
1321 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1323 : FS::Msgcat::_gettext('night');
1325 return "$daytime_label or $night_label is required"
1329 if ( $self->has_ship_address
1330 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1331 $self->addr_fields )
1335 $self->ut_name('ship_last')
1336 || $self->ut_name('ship_first')
1337 || $self->ut_textn('ship_company')
1338 || $self->ut_text('ship_address1')
1339 || $self->ut_textn('ship_address2')
1340 || $self->ut_text('ship_city')
1341 || $self->ut_textn('ship_county')
1342 || $self->ut_textn('ship_state')
1343 || $self->ut_country('ship_country')
1345 return $error if $error;
1347 #false laziness with above
1348 unless ( qsearchs('cust_main_county', {
1349 'country' => $self->ship_country,
1352 return "Unknown ship_state/ship_county/ship_country: ".
1353 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1354 unless qsearch('cust_main_county',{
1355 'state' => $self->ship_state,
1356 'county' => $self->ship_county,
1357 'country' => $self->ship_country,
1363 $self->ut_phonen('ship_daytime', $self->ship_country)
1364 || $self->ut_phonen('ship_night', $self->ship_country)
1365 || $self->ut_phonen('ship_fax', $self->ship_country)
1366 || $self->ut_zip('ship_zip', $self->ship_country)
1368 return $error if $error;
1370 return "Unit # is required."
1371 if $self->ship_address2 =~ /^\s*$/
1372 && $conf->exists('cust_main-require_address2');
1374 } else { # ship_ info eq billing info, so don't store dup info in database
1376 $self->setfield("ship_$_", '')
1377 foreach $self->addr_fields;
1379 return "Unit # is required."
1380 if $self->address2 =~ /^\s*$/
1381 && $conf->exists('cust_main-require_address2');
1385 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1386 # or return "Illegal payby: ". $self->payby;
1388 FS::payby->can_payby($self->table, $self->payby)
1389 or return "Illegal payby: ". $self->payby;
1391 $error = $self->ut_numbern('paystart_month')
1392 || $self->ut_numbern('paystart_year')
1393 || $self->ut_numbern('payissue')
1394 || $self->ut_textn('paytype')
1396 return $error if $error;
1398 if ( $self->payip eq '' ) {
1401 $error = $self->ut_ip('payip');
1402 return $error if $error;
1405 # If it is encrypted and the private key is not availaible then we can't
1406 # check the credit card.
1408 my $check_payinfo = 1;
1410 if ($self->is_encrypted($self->payinfo)) {
1414 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1416 my $payinfo = $self->payinfo;
1417 $payinfo =~ s/\D//g;
1418 $payinfo =~ /^(\d{13,16})$/
1419 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1421 $self->payinfo($payinfo);
1423 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1425 return gettext('unknown_card_type')
1426 if cardtype($self->payinfo) eq "Unknown";
1428 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1430 return 'Banned credit card: banned on '.
1431 time2str('%a %h %o at %r', $ban->_date).
1432 ' by '. $ban->otaker.
1433 ' (ban# '. $ban->bannum. ')';
1436 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1437 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1438 $self->paycvv =~ /^(\d{4})$/
1439 or return "CVV2 (CID) for American Express cards is four digits.";
1442 $self->paycvv =~ /^(\d{3})$/
1443 or return "CVV2 (CVC2/CID) is three digits.";
1450 my $cardtype = cardtype($payinfo);
1451 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1453 return "Start date or issue number is required for $cardtype cards"
1454 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1456 return "Start month must be between 1 and 12"
1457 if $self->paystart_month
1458 and $self->paystart_month < 1 || $self->paystart_month > 12;
1460 return "Start year must be 1990 or later"
1461 if $self->paystart_year
1462 and $self->paystart_year < 1990;
1464 return "Issue number must be beween 1 and 99"
1466 and $self->payissue < 1 || $self->payissue > 99;
1469 $self->paystart_month('');
1470 $self->paystart_year('');
1471 $self->payissue('');
1474 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1476 my $payinfo = $self->payinfo;
1477 $payinfo =~ s/[^\d\@]//g;
1478 if ( $conf->exists('echeck-nonus') ) {
1479 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1480 $payinfo = "$1\@$2";
1482 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1483 $payinfo = "$1\@$2";
1485 $self->payinfo($payinfo);
1488 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1490 return 'Banned ACH account: banned on '.
1491 time2str('%a %h %o at %r', $ban->_date).
1492 ' by '. $ban->otaker.
1493 ' (ban# '. $ban->bannum. ')';
1496 } elsif ( $self->payby eq 'LECB' ) {
1498 my $payinfo = $self->payinfo;
1499 $payinfo =~ s/\D//g;
1500 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1502 $self->payinfo($payinfo);
1505 } elsif ( $self->payby eq 'BILL' ) {
1507 $error = $self->ut_textn('payinfo');
1508 return "Illegal P.O. number: ". $self->payinfo if $error;
1511 } elsif ( $self->payby eq 'COMP' ) {
1513 my $curuser = $FS::CurrentUser::CurrentUser;
1514 if ( ! $self->custnum
1515 && ! $curuser->access_right('Complimentary customer')
1518 return "You are not permitted to create complimentary accounts."
1521 $error = $self->ut_textn('payinfo');
1522 return "Illegal comp account issuer: ". $self->payinfo if $error;
1525 } elsif ( $self->payby eq 'PREPAY' ) {
1527 my $payinfo = $self->payinfo;
1528 $payinfo =~ s/\W//g; #anything else would just confuse things
1529 $self->payinfo($payinfo);
1530 $error = $self->ut_alpha('payinfo');
1531 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1532 return "Unknown prepayment identifier"
1533 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1538 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1539 return "Expiration date required"
1540 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1544 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1545 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1546 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1547 ( $m, $y ) = ( $3, "20$2" );
1549 return "Illegal expiration date: ". $self->paydate;
1551 $self->paydate("$y-$m-01");
1552 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1553 return gettext('expired_card')
1555 && !$ignore_expired_card
1556 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1559 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1560 ( ! $conf->exists('require_cardname')
1561 || $self->payby !~ /^(CARD|DCRD)$/ )
1563 $self->payname( $self->first. " ". $self->getfield('last') );
1565 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1566 or return gettext('illegal_name'). " payname: ". $self->payname;
1570 foreach my $flag (qw( tax spool_cdr )) {
1571 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1575 $self->otaker(getotaker) unless $self->otaker;
1577 warn "$me check AFTER: \n". $self->_dump
1580 $self->SUPER::check;
1585 Returns a list of fields which have ship_ duplicates.
1590 qw( last first company
1591 address1 address2 city county state zip country
1596 =item has_ship_address
1598 Returns true if this customer record has a separate shipping address.
1602 sub has_ship_address {
1604 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
1609 Returns all packages (see L<FS::cust_pkg>) for this customer.
1616 return $self->num_pkgs unless wantarray;
1619 if ( $self->{'_pkgnum'} ) {
1620 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1622 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1625 sort sort_packages @cust_pkg;
1628 =item ncancelled_pkgs
1630 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1634 sub ncancelled_pkgs {
1637 return $self->num_ncancelled_pkgs unless wantarray;
1640 if ( $self->{'_pkgnum'} ) {
1642 @cust_pkg = grep { ! $_->getfield('cancel') }
1643 values %{ $self->{'_pkgnum'}->cache };
1648 qsearch( 'cust_pkg', {
1649 'custnum' => $self->custnum,
1653 qsearch( 'cust_pkg', {
1654 'custnum' => $self->custnum,
1659 sort sort_packages @cust_pkg;
1663 # This should be generalized to use config options to determine order.
1666 if ( $a->get('cancel') xor $b->get('cancel') ) {
1667 return -1 if $b->get('cancel');
1668 return 1 if $a->get('cancel');
1669 #shouldn't get here...
1672 my @a_cust_svc = $a->cust_svc;
1673 my @b_cust_svc = $b->cust_svc;
1674 return 0 if !scalar(@a_cust_svc) && !scalar(@b_cust_svc);
1675 return -1 if scalar(@a_cust_svc) && !scalar(@b_cust_svc);
1676 return 1 if !scalar(@a_cust_svc) && scalar(@b_cust_svc);
1677 $a_cust_svc[0]->svc_x->label cmp $b_cust_svc[0]->svc_x->label;
1682 =item suspended_pkgs
1684 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1688 sub suspended_pkgs {
1690 grep { $_->susp } $self->ncancelled_pkgs;
1693 =item unflagged_suspended_pkgs
1695 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1696 customer (thouse packages without the `manual_flag' set).
1700 sub unflagged_suspended_pkgs {
1702 return $self->suspended_pkgs
1703 unless dbdef->table('cust_pkg')->column('manual_flag');
1704 grep { ! $_->manual_flag } $self->suspended_pkgs;
1707 =item unsuspended_pkgs
1709 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1714 sub unsuspended_pkgs {
1716 grep { ! $_->susp } $self->ncancelled_pkgs;
1719 =item num_cancelled_pkgs
1721 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1726 sub num_cancelled_pkgs {
1727 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
1730 sub num_ncancelled_pkgs {
1731 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
1735 my( $self ) = shift;
1736 my $sql = scalar(@_) ? shift : '';
1737 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
1738 my $sth = dbh->prepare(
1739 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
1740 ) or die dbh->errstr;
1741 $sth->execute($self->custnum) or die $sth->errstr;
1742 $sth->fetchrow_arrayref->[0];
1747 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1748 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1749 on success or a list of errors.
1755 grep { $_->unsuspend } $self->suspended_pkgs;
1760 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1762 Returns a list: an empty list on success or a list of errors.
1768 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1771 =item suspend_if_pkgpart PKGPART [ , PKGPART ... ]
1773 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1774 PKGPARTs (see L<FS::part_pkg>).
1776 Returns a list: an empty list on success or a list of errors.
1780 sub suspend_if_pkgpart {
1782 my (@pkgparts, %opt);
1783 if (ref($_[0]) eq 'HASH'){
1784 @pkgparts = @{$_[0]{pkgparts}};
1789 grep { $_->suspend(%opt) }
1790 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1791 $self->unsuspended_pkgs;
1794 =item suspend_unless_pkgpart PKGPART [ , PKGPART ... ]
1796 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1797 listed PKGPARTs (see L<FS::part_pkg>).
1799 Returns a list: an empty list on success or a list of errors.
1803 sub suspend_unless_pkgpart {
1805 my (@pkgparts, %opt);
1806 if (ref($_[0]) eq 'HASH'){
1807 @pkgparts = @{$_[0]{pkgparts}};
1812 grep { $_->suspend(%opt) }
1813 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1814 $self->unsuspended_pkgs;
1817 =item cancel [ OPTION => VALUE ... ]
1819 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1821 Available options are: I<quiet>, I<reasonnum>, and I<ban>
1823 I<quiet> can be set true to supress email cancellation notices.
1825 # I<reasonnum> can be set to a cancellation reason (see L<FS::cancel_reason>)
1827 I<ban> can be set true to ban this customer's credit card or ACH information,
1830 Always returns a list: an empty list on success or a list of errors.
1838 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1840 #should try decryption (we might have the private key)
1841 # and if not maybe queue a job for the server that does?
1842 return ( "Can't (yet) ban encrypted credit cards" )
1843 if $self->is_encrypted($self->payinfo);
1845 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1846 my $error = $ban->insert;
1847 return ( $error ) if $error;
1851 grep { $_ } map { $_->cancel(@_) } $self->ncancelled_pkgs;
1854 sub _banned_pay_hashref {
1865 'payby' => $payby2ban{$self->payby},
1866 'payinfo' => md5_base64($self->payinfo),
1867 #don't ever *search* on reason! #'reason' =>
1873 Returns all notes (see L<FS::cust_main_note>) for this customer.
1880 qsearch( 'cust_main_note',
1881 { 'custnum' => $self->custnum },
1883 'ORDER BY _DATE DESC'
1889 Returns the agent (see L<FS::agent>) for this customer.
1895 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1900 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
1901 conjunction with the collect method.
1903 If there is an error, returns the error, otherwise returns false.
1905 Options are passed as name-value pairs. Currently available options are:
1909 =item resetup - if set true, re-charges setup fees.
1911 =item time - bills the customer as if it were that time. Specified as a UNIX timestamp; see L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion functions. For example:
1915 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1917 =item invoice_time - used in conjunction with the I<time> option, this option specifies the date of for the generated invoices. Other calculations, such as whether or not to generate the invoice in the first place, are not affected.
1924 my( $self, %options ) = @_;
1925 return '' if $self->payby eq 'COMP';
1926 warn "$me bill customer ". $self->custnum. "\n"
1929 my $time = $options{'time'} || time;
1934 local $SIG{HUP} = 'IGNORE';
1935 local $SIG{INT} = 'IGNORE';
1936 local $SIG{QUIT} = 'IGNORE';
1937 local $SIG{TERM} = 'IGNORE';
1938 local $SIG{TSTP} = 'IGNORE';
1939 local $SIG{PIPE} = 'IGNORE';
1941 my $oldAutoCommit = $FS::UID::AutoCommit;
1942 local $FS::UID::AutoCommit = 0;
1945 $self->select_for_update; #mutex
1947 #create a new invoice
1948 #(we'll remove it later if it doesn't actually need to be generated [contains
1949 # no line items] and we're inside a transaciton so nothing else will see it)
1950 my $cust_bill = new FS::cust_bill ( {
1951 'custnum' => $self->custnum,
1952 '_date' => ( $options{'invoice_time'} || $time ),
1953 #'charged' => $charged,
1956 $error = $cust_bill->insert;
1958 $dbh->rollback if $oldAutoCommit;
1959 return "can't create invoice for customer #". $self->custnum. ": $error";
1961 my $invnum = $cust_bill->invnum;
1964 # find the packages which are due for billing, find out how much they are
1965 # & generate invoice database.
1968 my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
1970 my @precommit_hooks = ();
1972 my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } );
1973 foreach my $cust_pkg (@cust_pkgs) {
1975 #NO!! next if $cust_pkg->cancel;
1976 next if $cust_pkg->getfield('cancel');
1978 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
1980 #? to avoid use of uninitialized value errors... ?
1981 $cust_pkg->setfield('bill', '')
1982 unless defined($cust_pkg->bill);
1984 my $part_pkg = $cust_pkg->part_pkg;
1986 my %hash = $cust_pkg->hash;
1987 my $old_cust_pkg = new FS::cust_pkg \%hash;
1997 if ( ! $cust_pkg->setup &&
1999 ( $conf->exists('disable_setup_suspended_pkgs') &&
2000 ! $cust_pkg->getfield('susp')
2001 ) || ! $conf->exists('disable_setup_suspended_pkgs')
2003 || $options{'resetup'}
2006 warn " bill setup\n" if $DEBUG > 1;
2008 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
2010 $dbh->rollback if $oldAutoCommit;
2011 return "$@ running calc_setup for $cust_pkg\n";
2014 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
2016 $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
2020 # bill recurring fee
2023 #XXX unit stuff here too
2027 if ( $part_pkg->getfield('freq') ne '0' &&
2028 ! $cust_pkg->getfield('susp') &&
2029 ( $cust_pkg->getfield('bill') || 0 ) <= $time
2032 # XXX should this be a package event? probably. events are called
2033 # at collection time at the moment, though...
2034 if ( $part_pkg->can('reset_usage') ) {
2035 warn " resetting usage counters" if $DEBUG > 1;
2036 $part_pkg->reset_usage($cust_pkg);
2039 warn " bill recur\n" if $DEBUG > 1;
2041 # XXX shared with $recur_prog
2042 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2044 #over two params! lets at least switch to a hashref for the rest...
2045 my %param = ( 'precommit_hooks' => \@precommit_hooks, );
2047 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
2049 $dbh->rollback if $oldAutoCommit;
2050 return "$@ running calc_recur for $cust_pkg\n";
2053 #change this bit to use Date::Manip? CAREFUL with timezones (see
2054 # mailing list archive)
2055 my ($sec,$min,$hour,$mday,$mon,$year) =
2056 (localtime($sdate) )[0,1,2,3,4,5];
2058 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
2059 # only for figuring next bill date, nothing else, so, reset $sdate again
2061 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2062 #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
2063 $cust_pkg->last_bill($sdate);
2065 if ( $part_pkg->freq =~ /^\d+$/ ) {
2066 $mon += $part_pkg->freq;
2067 until ( $mon < 12 ) { $mon -= 12; $year++; }
2068 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
2070 $mday += $weeks * 7;
2071 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
2074 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
2078 $dbh->rollback if $oldAutoCommit;
2079 return "unparsable frequency: ". $part_pkg->freq;
2081 $cust_pkg->setfield('bill',
2082 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
2085 warn "\$setup is undefined" unless defined($setup);
2086 warn "\$recur is undefined" unless defined($recur);
2087 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2090 # If $cust_pkg has been modified, update it and create cust_bill_pkg records
2093 if ( $cust_pkg->modified ) { # hmmm.. and if the options are modified?
2095 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2098 $error=$cust_pkg->replace($old_cust_pkg,
2099 options => { $cust_pkg->options },
2101 if ( $error ) { #just in case
2102 $dbh->rollback if $oldAutoCommit;
2103 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
2106 $setup = sprintf( "%.2f", $setup );
2107 $recur = sprintf( "%.2f", $recur );
2108 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2109 $dbh->rollback if $oldAutoCommit;
2110 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2112 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2113 $dbh->rollback if $oldAutoCommit;
2114 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2117 if ( $setup != 0 || $recur != 0 ) {
2119 unless ($postal_charge) {
2120 $postal_charge = 1; # try only once
2121 my $postal_pkg = $self->charge_postal_fee();
2122 if ( $postal_pkg && !ref( $postal_pkg ) ) {
2123 $dbh->rollback if $oldAutoCommit;
2124 return "can't charge postal invoice fee for customer ".
2125 $self->custnum. ": $postal_pkg";
2127 push @cust_pkgs, $postal_pkg if $postal_pkg;
2130 warn " charges (setup=$setup, recur=$recur); adding line items\n"
2133 push @details, map { $_->detail } $cust_pkg->cust_pkg_detail('I');
2135 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2136 'invnum' => $invnum,
2137 'pkgnum' => $cust_pkg->pkgnum,
2139 'unitsetup' => $unitsetup,
2141 'unitrecur' => $unitrecur,
2142 'quantity' => $cust_pkg->quantity,
2143 'details' => \@details,
2146 if ( $part_pkg->option('recur_temporality', 1) eq 'preceding' ) {
2147 $cust_bill_pkg->sdate( $hash{last_bill} );
2148 $cust_bill_pkg->edate( $sdate - 86399 ); #60s*60m*24h-1
2149 } else { #if ( $part_pkg->option('recur_temporality', 1) eq 'upcoming' ) {
2150 $cust_bill_pkg->sdate( $sdate );
2151 $cust_bill_pkg->edate( $cust_pkg->bill );
2154 $error = $cust_bill_pkg->insert;
2156 $dbh->rollback if $oldAutoCommit;
2157 return "can't create invoice line item for invoice #$invnum: $error";
2159 $total_setup += $setup;
2160 $total_recur += $recur;
2166 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2169 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
2172 my %taxhash = map { $_ => $self->get("$prefix$_") }
2173 qw( state county country );
2175 $taxhash{'taxclass'} = $part_pkg->taxclass;
2177 my @taxes = qsearch( 'cust_main_county', \%taxhash );
2180 $taxhash{'taxclass'} = '';
2181 @taxes = qsearch( 'cust_main_county', \%taxhash );
2184 #one more try at a whole-country tax rate
2186 $taxhash{$_} = '' foreach qw( state county );
2187 @taxes = qsearch( 'cust_main_county', \%taxhash );
2190 # maybe eliminate this entirely, along with all the 0% records
2192 $dbh->rollback if $oldAutoCommit;
2194 "fatal: can't find tax rate for state/county/country/taxclass ".
2195 join('/', ( map $self->get("$prefix$_"),
2196 qw(state county country)
2198 $part_pkg->taxclass ). "\n";
2201 foreach my $tax ( @taxes ) {
2203 my $taxable_charged = 0;
2204 $taxable_charged += $setup
2205 unless $part_pkg->setuptax =~ /^Y$/i
2206 || $tax->setuptax =~ /^Y$/i;
2207 $taxable_charged += $recur
2208 unless $part_pkg->recurtax =~ /^Y$/i
2209 || $tax->recurtax =~ /^Y$/i;
2210 next unless $taxable_charged;
2212 if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
2213 #my ($mon,$year) = (localtime($sdate) )[4,5];
2214 my ($mon,$year) = (localtime( $sdate || $cust_bill->_date ) )[4,5];
2216 my $freq = $part_pkg->freq || 1;
2217 if ( $freq !~ /(\d+)$/ ) {
2218 $dbh->rollback if $oldAutoCommit;
2219 return "daily/weekly package definitions not (yet?)".
2220 " compatible with monthly tax exemptions";
2222 my $taxable_per_month =
2223 sprintf("%.2f", $taxable_charged / $freq );
2225 #call the whole thing off if this customer has any old
2226 #exemption records...
2227 my @cust_tax_exempt =
2228 qsearch( 'cust_tax_exempt' => { custnum=> $self->custnum } );
2229 if ( @cust_tax_exempt ) {
2230 $dbh->rollback if $oldAutoCommit;
2232 'this customer still has old-style tax exemption records; '.
2233 'run bin/fs-migrate-cust_tax_exempt?';
2236 foreach my $which_month ( 1 .. $freq ) {
2238 #maintain the new exemption table now
2241 FROM cust_tax_exempt_pkg
2242 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
2243 LEFT JOIN cust_bill USING ( invnum )
2249 my $sth = dbh->prepare($sql) or do {
2250 $dbh->rollback if $oldAutoCommit;
2251 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2259 $dbh->rollback if $oldAutoCommit;
2260 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2262 my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0;
2264 my $remaining_exemption =
2265 $tax->exempt_amount - $existing_exemption;
2266 if ( $remaining_exemption > 0 ) {
2267 my $addl = $remaining_exemption > $taxable_per_month
2268 ? $taxable_per_month
2269 : $remaining_exemption;
2270 $taxable_charged -= $addl;
2272 my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( {
2273 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2274 'taxnum' => $tax->taxnum,
2275 'year' => 1900+$year,
2277 'amount' => sprintf("%.2f", $addl ),
2279 $error = $cust_tax_exempt_pkg->insert;
2281 $dbh->rollback if $oldAutoCommit;
2282 return "fatal: can't insert cust_tax_exempt_pkg: $error";
2284 } # if $remaining_exemption > 0
2288 #until ( $mon < 12 ) { $mon -= 12; $year++; }
2289 until ( $mon < 13 ) { $mon -= 12; $year++; }
2291 } #foreach $which_month
2293 } #if $tax->exempt_amount
2295 $taxable_charged = sprintf( "%.2f", $taxable_charged);
2297 #$tax += $taxable_charged * $cust_main_county->tax / 100
2298 $tax{ $tax->taxname || 'Tax' } +=
2299 $taxable_charged * $tax->tax / 100
2301 } #foreach my $tax ( @taxes )
2303 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2305 } #if $setup != 0 || $recur != 0
2307 } #if $cust_pkg->modified
2309 } #foreach my $cust_pkg
2311 unless ( $cust_bill->cust_bill_pkg ) {
2312 $cust_bill->delete; #don't create an invoice w/o line items
2313 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2317 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
2319 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
2320 my $tax = sprintf("%.2f", $tax{$taxname} );
2321 $charged = sprintf( "%.2f", $charged+$tax );
2323 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2324 'invnum' => $invnum,
2330 'itemdesc' => $taxname,
2332 $error = $cust_bill_pkg->insert;
2334 $dbh->rollback if $oldAutoCommit;
2335 return "can't create invoice line item for invoice #$invnum: $error";
2337 $total_setup += $tax;
2341 $cust_bill->charged( sprintf( "%.2f", $total_setup + $total_recur ) );
2342 $error = $cust_bill->replace;
2344 $dbh->rollback if $oldAutoCommit;
2345 return "can't update charged for invoice #$invnum: $error";
2348 foreach my $hook ( @precommit_hooks ) {
2350 &{$hook}; #($self) ?
2353 $dbh->rollback if $oldAutoCommit;
2354 return "$@ running precommit hook $hook\n";
2358 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2362 =item collect OPTIONS
2364 (Attempt to) collect money for this customer's outstanding invoices (see
2365 L<FS::cust_bill>). Usually used after the bill method.
2367 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
2368 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
2369 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
2371 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
2372 and the invoice events web interface.
2374 If there is an error, returns the error, otherwise returns false.
2376 Options are passed as name-value pairs.
2378 Currently available options are:
2380 invoice_time - Use this time when deciding when to print invoices and
2381 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>
2382 for conversion functions.
2384 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
2387 quiet - set true to surpress email card/ACH decline notices.
2389 freq - "1d" for the traditional, daily events (the default), or "1m" for the
2392 payby - allows for one time override of normal customer billing method
2397 my( $self, %options ) = @_;
2398 my $invoice_time = $options{'invoice_time'} || time;
2401 local $SIG{HUP} = 'IGNORE';
2402 local $SIG{INT} = 'IGNORE';
2403 local $SIG{QUIT} = 'IGNORE';
2404 local $SIG{TERM} = 'IGNORE';
2405 local $SIG{TSTP} = 'IGNORE';
2406 local $SIG{PIPE} = 'IGNORE';
2408 my $oldAutoCommit = $FS::UID::AutoCommit;
2409 local $FS::UID::AutoCommit = 0;
2412 $self->select_for_update; #mutex
2414 my $balance = $self->balance;
2415 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2417 unless ( $balance > 0 ) { #redundant?????
2418 $dbh->rollback if $oldAutoCommit; #hmm
2422 if ( exists($options{'retry_card'}) ) {
2423 carp 'retry_card option passed to collect is deprecated; use retry';
2424 $options{'retry'} ||= $options{'retry_card'};
2426 if ( exists($options{'retry'}) && $options{'retry'} ) {
2427 my $error = $self->retry_realtime;
2429 $dbh->rollback if $oldAutoCommit;
2435 if ( defined $options{'freq'} && $options{'freq'} eq '1m' ) {
2436 $extra_sql = " AND freq = '1m' ";
2438 $extra_sql = " AND ( freq = '1d' OR freq IS NULL OR freq = '' ) ";
2441 foreach my $cust_bill ( $self->open_cust_bill ) {
2443 # don't try to charge for the same invoice if it's already in a batch
2444 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
2446 last if $self->balance <= 0;
2448 warn " invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
2451 foreach my $part_bill_event ( due_events ( $cust_bill,
2452 exists($options{'payby'})
2458 last if $cust_bill->owed <= 0 # don't run subsequent events if owed<=0
2459 || $self->balance <= 0; # or if balance<=0
2462 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2463 warn " do_event " . $cust_bill . " ". (%options) . "\n"
2466 if (my $error = $part_bill_event->do_event($cust_bill, %options)) {
2467 # gah, even with transactions.
2468 $dbh->commit if $oldAutoCommit; #well.
2477 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2482 =item retry_realtime
2484 Schedules realtime / batch credit card / electronic check / LEC billing
2485 events for for retry. Useful if card information has changed or manual
2486 retry is desired. The 'collect' method must be called to actually retry
2489 Implementation details: For each of this customer's open invoices, changes
2490 the status of the first "done" (with statustext error) realtime processing
2495 sub retry_realtime {
2498 local $SIG{HUP} = 'IGNORE';
2499 local $SIG{INT} = 'IGNORE';
2500 local $SIG{QUIT} = 'IGNORE';
2501 local $SIG{TERM} = 'IGNORE';
2502 local $SIG{TSTP} = 'IGNORE';
2503 local $SIG{PIPE} = 'IGNORE';
2505 my $oldAutoCommit = $FS::UID::AutoCommit;
2506 local $FS::UID::AutoCommit = 0;
2509 foreach my $cust_bill (
2510 grep { $_->cust_bill_event }
2511 $self->open_cust_bill
2513 my @cust_bill_event =
2514 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
2516 #$_->part_bill_event->plan eq 'realtime-card'
2517 $_->part_bill_event->eventcode =~
2518 /\$cust_bill\->(batch|realtime)_(card|ach|lec)/
2519 && $_->status eq 'done'
2522 $cust_bill->cust_bill_event;
2523 next unless @cust_bill_event;
2524 my $error = $cust_bill_event[0]->retry;
2526 $dbh->rollback if $oldAutoCommit;
2527 return "error scheduling invoice event for retry: $error";
2532 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2537 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2539 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2540 via a Business::OnlinePayment realtime gateway. See
2541 L<http://420.am/business-onlinepayment> for supported gateways.
2543 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2545 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
2547 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2548 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2549 if set, will override the value from the customer record.
2551 I<description> is a free-text field passed to the gateway. It defaults to
2552 "Internet services".
2554 If an I<invnum> is specified, this payment (if successful) is applied to the
2555 specified invoice. If you don't specify an I<invnum> you might want to
2556 call the B<apply_payments> method.
2558 I<quiet> can be set true to surpress email decline notices.
2560 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
2561 resulting paynum, if any.
2563 I<payunique> is a unique identifier for this payment.
2565 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2572 my( $self, $method, $amount, %options ) = @_;
2574 warn "$me realtime_bop: $method $amount\n";
2575 warn " $_ => $options{$_}\n" foreach keys %options;
2578 $options{'description'} ||= 'Internet services';
2580 eval "use Business::OnlinePayment";
2583 my $payinfo = exists($options{'payinfo'})
2584 ? $options{'payinfo'}
2587 my %method2payby = (
2598 if ( $options{'invnum'} ) {
2599 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
2600 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
2602 map { $_->part_pkg->taxclass }
2604 map { $_->cust_pkg }
2605 $cust_bill->cust_bill_pkg;
2606 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
2607 #different taxclasses
2608 $taxclass = $taxclasses[0];
2612 #look for an agent gateway override first
2614 if ( $method eq 'CC' ) {
2615 $cardtype = cardtype($payinfo);
2616 } elsif ( $method eq 'ECHECK' ) {
2619 $cardtype = $method;
2623 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2624 cardtype => $cardtype,
2625 taxclass => $taxclass, } )
2626 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2628 taxclass => $taxclass, } )
2629 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2630 cardtype => $cardtype,
2632 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2634 taxclass => '', } );
2636 my $payment_gateway = '';
2637 my( $processor, $login, $password, $action, @bop_options );
2638 if ( $override ) { #use a payment gateway override
2640 $payment_gateway = $override->payment_gateway;
2642 $processor = $payment_gateway->gateway_module;
2643 $login = $payment_gateway->gateway_username;
2644 $password = $payment_gateway->gateway_password;
2645 $action = $payment_gateway->gateway_action;
2646 @bop_options = $payment_gateway->options;
2648 } else { #use the standard settings from the config
2650 ( $processor, $login, $password, $action, @bop_options ) =
2651 $self->default_payment_gateway($method);
2659 my $address = exists($options{'address1'})
2660 ? $options{'address1'}
2662 my $address2 = exists($options{'address2'})
2663 ? $options{'address2'}
2665 $address .= ", ". $address2 if length($address2);
2667 my $o_payname = exists($options{'payname'})
2668 ? $options{'payname'}
2670 my($payname, $payfirst, $paylast);
2671 if ( $o_payname && $method ne 'ECHECK' ) {
2672 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2673 or return "Illegal payname $payname";
2674 ($payfirst, $paylast) = ($1, $2);
2676 $payfirst = $self->getfield('first');
2677 $paylast = $self->getfield('last');
2678 $payname = "$payfirst $paylast";
2681 my @invoicing_list = $self->invoicing_list_emailonly;
2682 if ( $conf->exists('emailinvoiceautoalways')
2683 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
2684 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2685 push @invoicing_list, $self->all_emails;
2688 my $email = ($conf->exists('business-onlinepayment-email-override'))
2689 ? $conf->config('business-onlinepayment-email-override')
2690 : $invoicing_list[0];
2694 my $payip = exists($options{'payip'})
2697 $content{customer_ip} = $payip
2700 $content{invoice_number} = $options{'invnum'}
2701 if exists($options{'invnum'}) && length($options{'invnum'});
2703 $content{email_customer} =
2704 ( $conf->exists('business-onlinepayment-email_customer')
2705 || $conf->exists('business-onlinepayment-email-override') );
2708 if ( $method eq 'CC' ) {
2710 $content{card_number} = $payinfo;
2711 $paydate = exists($options{'paydate'})
2712 ? $options{'paydate'}
2714 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2715 $content{expiration} = "$2/$1";
2717 my $paycvv = exists($options{'paycvv'})
2718 ? $options{'paycvv'}
2720 $content{cvv2} = $paycvv
2723 my $paystart_month = exists($options{'paystart_month'})
2724 ? $options{'paystart_month'}
2725 : $self->paystart_month;
2727 my $paystart_year = exists($options{'paystart_year'})
2728 ? $options{'paystart_year'}
2729 : $self->paystart_year;
2731 $content{card_start} = "$paystart_month/$paystart_year"
2732 if $paystart_month && $paystart_year;
2734 my $payissue = exists($options{'payissue'})
2735 ? $options{'payissue'}
2737 $content{issue_number} = $payissue if $payissue;
2739 $content{recurring_billing} = 'YES'
2740 if qsearch('cust_pay', { 'custnum' => $self->custnum,
2742 'payinfo' => $payinfo,
2744 || qsearch('cust_pay', { 'custnum' => $self->custnum,
2746 'paymask' => $self->mask_payinfo('CARD', $payinfo),
2750 } elsif ( $method eq 'ECHECK' ) {
2751 ( $content{account_number}, $content{routing_code} ) =
2752 split('@', $payinfo);
2753 $content{bank_name} = $o_payname;
2754 $content{bank_state} = exists($options{'paystate'})
2755 ? $options{'paystate'}
2756 : $self->getfield('paystate');
2757 $content{account_type} = exists($options{'paytype'})
2758 ? uc($options{'paytype'}) || 'CHECKING'
2759 : uc($self->getfield('paytype')) || 'CHECKING';
2760 $content{account_name} = $payname;
2761 $content{customer_org} = $self->company ? 'B' : 'I';
2762 $content{state_id} = exists($options{'stateid'})
2763 ? $options{'stateid'}
2764 : $self->getfield('stateid');
2765 $content{state_id_state} = exists($options{'stateid_state'})
2766 ? $options{'stateid_state'}
2767 : $self->getfield('stateid_state');
2768 $content{customer_ssn} = exists($options{'ss'})
2771 } elsif ( $method eq 'LEC' ) {
2772 $content{phone} = $payinfo;
2776 # run transaction(s)
2779 my $balance = exists( $options{'balance'} )
2780 ? $options{'balance'}
2783 $self->select_for_update; #mutex ... just until we get our pending record in
2785 #the checks here are intended to catch concurrent payments
2786 #double-form-submission prevention is taken care of in cust_pay_pending::check
2789 return "The customer's balance has changed; $method transaction aborted."
2790 if $self->balance < $balance;
2791 #&& $self->balance < $amount; #might as well anyway?
2793 #also check and make sure there aren't *other* pending payments for this cust
2795 my @pending = qsearch('cust_pay_pending', {
2796 'custnum' => $self->custnum,
2797 'status' => { op=>'!=', value=>'done' }
2799 return "A payment is already being processed for this customer (".
2800 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
2801 "); $method transaction aborted."
2802 if scalar(@pending);
2804 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
2806 my $cust_pay_pending = new FS::cust_pay_pending {
2807 'custnum' => $self->custnum,
2808 #'invnum' => $options{'invnum'},
2811 'payby' => $method2payby{$method},
2812 'payinfo' => $payinfo,
2813 'paydate' => $paydate,
2815 'gatewaynum' => ( $payment_gateway ? $payment_gateway->gatewaynum : '' ),
2817 $cust_pay_pending->payunique( $options{payunique} )
2818 if defined($options{payunique}) && length($options{payunique});
2819 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
2820 return $cpp_new_err if $cpp_new_err;
2822 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
2824 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
2825 $transaction->content(
2828 'password' => $password,
2829 'action' => $action1,
2830 'description' => $options{'description'},
2831 'amount' => $amount,
2832 #'invoice_number' => $options{'invnum'},
2833 'customer_id' => $self->custnum,
2834 'last_name' => $paylast,
2835 'first_name' => $payfirst,
2837 'address' => $address,
2838 'city' => ( exists($options{'city'})
2841 'state' => ( exists($options{'state'})
2844 'zip' => ( exists($options{'zip'})
2847 'country' => ( exists($options{'country'})
2848 ? $options{'country'}
2850 'referer' => 'http://cleanwhisker.420.am/',
2852 'phone' => $self->daytime || $self->night,
2856 $cust_pay_pending->status('pending');
2857 my $cpp_pending_err = $cust_pay_pending->replace;
2858 return $cpp_pending_err if $cpp_pending_err;
2860 $transaction->submit();
2862 if ( $transaction->is_success() && $action2 ) {
2864 $cust_pay_pending->status('authorized');
2865 my $cpp_authorized_err = $cust_pay_pending->replace;
2866 return $cpp_authorized_err if $cpp_authorized_err;
2868 my $auth = $transaction->authorization;
2869 my $ordernum = $transaction->can('order_number')
2870 ? $transaction->order_number
2874 new Business::OnlinePayment( $processor, @bop_options );
2881 password => $password,
2882 order_number => $ordernum,
2884 authorization => $auth,
2885 description => $options{'description'},
2888 foreach my $field (qw( authorization_source_code returned_ACI
2889 transaction_identifier validation_code
2890 transaction_sequence_num local_transaction_date
2891 local_transaction_time AVS_result_code )) {
2892 $capture{$field} = $transaction->$field() if $transaction->can($field);
2895 $capture->content( %capture );
2899 unless ( $capture->is_success ) {
2900 my $e = "Authorization successful but capture failed, custnum #".
2901 $self->custnum. ': '. $capture->result_code.
2902 ": ". $capture->error_message;
2909 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
2910 my $cpp_captured_err = $cust_pay_pending->replace;
2911 return $cpp_captured_err if $cpp_captured_err;
2914 # remove paycvv after initial transaction
2917 #false laziness w/misc/process/payment.cgi - check both to make sure working
2919 if ( defined $self->dbdef_table->column('paycvv')
2920 && length($self->paycvv)
2921 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
2923 my $error = $self->remove_cvv;
2925 warn "WARNING: error removing cvv: $error\n";
2933 if ( $transaction->is_success() ) {
2936 if ( $payment_gateway ) { # agent override
2937 $paybatch = $payment_gateway->gatewaynum. '-';
2940 $paybatch .= "$processor:". $transaction->authorization;
2942 $paybatch .= ':'. $transaction->order_number
2943 if $transaction->can('order_number')
2944 && length($transaction->order_number);
2946 my $cust_pay = new FS::cust_pay ( {
2947 'custnum' => $self->custnum,
2948 'invnum' => $options{'invnum'},
2951 'payby' => $method2payby{$method},
2952 'payinfo' => $payinfo,
2953 'paybatch' => $paybatch,
2954 'paydate' => $paydate,
2956 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
2957 $cust_pay->payunique( $options{payunique} )
2958 if defined($options{payunique}) && length($options{payunique});
2960 my $oldAutoCommit = $FS::UID::AutoCommit;
2961 local $FS::UID::AutoCommit = 0;
2964 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
2966 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
2969 $cust_pay->invnum(''); #try again with no specific invnum
2970 my $error2 = $cust_pay->insert( $options{'manual'} ?
2971 ( 'manual' => 1 ) : ()
2974 # gah. but at least we have a record of the state we had to abort in
2975 # from cust_pay_pending now.
2976 my $e = "WARNING: $method captured but payment not recorded - ".
2977 "error inserting payment ($processor): $error2".
2978 " (previously tried insert with invnum #$options{'invnum'}" .
2979 ": $error ) - pending payment saved as paypendingnum ".
2980 $cust_pay_pending->paypendingnum. "\n";
2986 if ( $options{'paynum_ref'} ) {
2987 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
2990 $cust_pay_pending->status('done');
2991 $cust_pay_pending->statustext('captured');
2992 my $cpp_done_err = $cust_pay_pending->replace;
2994 if ( $cpp_done_err ) {
2996 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2997 my $e = "WARNING: $method captured but payment not recorded - ".
2998 "error updating status for paypendingnum ".
2999 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3005 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3006 return ''; #no error
3012 my $perror = "$processor error: ". $transaction->error_message;
3014 unless ( $transaction->error_message ) {
3017 #this should be normalized :/
3019 # bad, ad-hoc B:OP:PayflowPro "transaction_response" BS
3020 if ( $transaction->can('param')
3021 && $transaction->param('transaction_response') ) {
3022 $t_response = $transaction->param('transaction_response')
3024 # slightly better, ad-hoc B:OP:TransactionCentral without "param"
3025 } elsif ( $transaction->can('response_page') ) {
3027 'page' => ( $transaction->can('response_page')
3028 ? $transaction->response_page
3031 'code' => ( $transaction->can('response_code')
3032 ? $transaction->response_code
3035 'headers' => ( $transaction->can('response_headers')
3036 ? $transaction->response_headers
3042 "No additional debugging information available for $processor";
3045 $perror .= "No error_message returned from $processor -- ".
3046 ( ref($t_response) ? Dumper($t_response) : $t_response );
3050 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
3051 && $conf->exists('emaildecline')
3052 && grep { $_ ne 'POST' } $self->invoicing_list
3053 && ! grep { $transaction->error_message =~ /$_/ }
3054 $conf->config('emaildecline-exclude')
3056 my @templ = $conf->config('declinetemplate');
3057 my $template = new Text::Template (
3059 SOURCE => [ map "$_\n", @templ ],
3060 ) or return "($perror) can't create template: $Text::Template::ERROR";
3061 $template->compile()
3062 or return "($perror) can't compile template: $Text::Template::ERROR";
3064 my $templ_hash = { error => $transaction->error_message };
3066 my $error = send_email(
3067 'from' => $conf->config('invoice_from'),
3068 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
3069 'subject' => 'Your payment could not be processed',
3070 'body' => [ $template->fill_in(HASH => $templ_hash) ],
3073 $perror .= " (also received error sending decline notification: $error)"
3078 $cust_pay_pending->status('done');
3079 $cust_pay_pending->statustext("declined: $perror");
3080 my $cpp_done_err = $cust_pay_pending->replace;
3081 if ( $cpp_done_err ) {
3082 my $e = "WARNING: $method declined but pending payment not resolved - ".
3083 "error updating status for paypendingnum ".
3084 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3086 $perror = "$e ($perror)";
3094 =item default_payment_gateway
3098 sub default_payment_gateway {
3099 my( $self, $method ) = @_;
3101 die "Real-time processing not enabled\n"
3102 unless $conf->exists('business-onlinepayment');
3105 my $bop_config = 'business-onlinepayment';
3106 $bop_config .= '-ach'
3107 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
3108 my ( $processor, $login, $password, $action, @bop_options ) =
3109 $conf->config($bop_config);
3110 $action ||= 'normal authorization';
3111 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
3112 die "No real-time processor is enabled - ".
3113 "did you set the business-onlinepayment configuration value?\n"
3116 ( $processor, $login, $password, $action, @bop_options )
3121 Removes the I<paycvv> field from the database directly.
3123 If there is an error, returns the error, otherwise returns false.
3129 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
3130 or return dbh->errstr;
3131 $sth->execute($self->custnum)
3132 or return $sth->errstr;
3137 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
3139 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
3140 via a Business::OnlinePayment realtime gateway. See
3141 L<http://420.am/business-onlinepayment> for supported gateways.
3143 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3145 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
3147 Most gateways require a reference to an original payment transaction to refund,
3148 so you probably need to specify a I<paynum>.
3150 I<amount> defaults to the original amount of the payment if not specified.
3152 I<reason> specifies a reason for the refund.
3154 I<paydate> specifies the expiration date for a credit card overriding the
3155 value from the customer record or the payment record. Specified as yyyy-mm-dd
3157 Implementation note: If I<amount> is unspecified or equal to the amount of the
3158 orignal payment, first an attempt is made to "void" the transaction via
3159 the gateway (to cancel a not-yet settled transaction) and then if that fails,
3160 the normal attempt is made to "refund" ("credit") the transaction via the
3161 gateway is attempted.
3163 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3164 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3165 #if set, will override the value from the customer record.
3167 #If an I<invnum> is specified, this payment (if successful) is applied to the
3168 #specified invoice. If you don't specify an I<invnum> you might want to
3169 #call the B<apply_payments> method.
3173 #some false laziness w/realtime_bop, not enough to make it worth merging
3174 #but some useful small subs should be pulled out
3175 sub realtime_refund_bop {
3176 my( $self, $method, %options ) = @_;
3178 warn "$me realtime_refund_bop: $method refund\n";
3179 warn " $_ => $options{$_}\n" foreach keys %options;
3182 eval "use Business::OnlinePayment";
3186 # look up the original payment and optionally a gateway for that payment
3190 my $amount = $options{'amount'};
3192 my( $processor, $login, $password, @bop_options ) ;
3193 my( $auth, $order_number ) = ( '', '', '' );
3195 if ( $options{'paynum'} ) {
3197 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
3198 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
3199 or return "Unknown paynum $options{'paynum'}";
3200 $amount ||= $cust_pay->paid;
3202 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
3203 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
3204 $cust_pay->paybatch;
3205 my $gatewaynum = '';
3206 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
3208 if ( $gatewaynum ) { #gateway for the payment to be refunded
3210 my $payment_gateway =
3211 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
3212 die "payment gateway $gatewaynum not found"
3213 unless $payment_gateway;
3215 $processor = $payment_gateway->gateway_module;
3216 $login = $payment_gateway->gateway_username;
3217 $password = $payment_gateway->gateway_password;
3218 @bop_options = $payment_gateway->options;
3220 } else { #try the default gateway
3222 my( $conf_processor, $unused_action );
3223 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
3224 $self->default_payment_gateway($method);
3226 return "processor of payment $options{'paynum'} $processor does not".
3227 " match default processor $conf_processor"
3228 unless $processor eq $conf_processor;
3233 } else { # didn't specify a paynum, so look for agent gateway overrides
3234 # like a normal transaction
3237 if ( $method eq 'CC' ) {
3238 $cardtype = cardtype($self->payinfo);
3239 } elsif ( $method eq 'ECHECK' ) {
3242 $cardtype = $method;
3245 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3246 cardtype => $cardtype,
3248 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3250 taxclass => '', } );
3252 if ( $override ) { #use a payment gateway override
3254 my $payment_gateway = $override->payment_gateway;
3256 $processor = $payment_gateway->gateway_module;
3257 $login = $payment_gateway->gateway_username;
3258 $password = $payment_gateway->gateway_password;
3259 #$action = $payment_gateway->gateway_action;
3260 @bop_options = $payment_gateway->options;
3262 } else { #use the standard settings from the config
3265 ( $processor, $login, $password, $unused_action, @bop_options ) =
3266 $self->default_payment_gateway($method);
3271 return "neither amount nor paynum specified" unless $amount;
3276 'password' => $password,
3277 'order_number' => $order_number,
3278 'amount' => $amount,
3279 'referer' => 'http://cleanwhisker.420.am/',
3281 $content{authorization} = $auth
3282 if length($auth); #echeck/ACH transactions have an order # but no auth
3283 #(at least with authorize.net)
3285 my $disable_void_after;
3286 if ($conf->exists('disable_void_after')
3287 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
3288 $disable_void_after = $1;
3291 #first try void if applicable
3292 if ( $cust_pay && $cust_pay->paid == $amount
3294 ( not defined($disable_void_after) )
3295 || ( time < ($cust_pay->_date + $disable_void_after ) )
3298 warn " attempting void\n" if $DEBUG > 1;
3299 my $void = new Business::OnlinePayment( $processor, @bop_options );
3300 $void->content( 'action' => 'void', %content );
3302 if ( $void->is_success ) {
3303 my $error = $cust_pay->void($options{'reason'});
3305 # gah, even with transactions.
3306 my $e = 'WARNING: Card/ACH voided but database not updated - '.
3307 "error voiding payment: $error";
3311 warn " void successful\n" if $DEBUG > 1;
3316 warn " void unsuccessful, trying refund\n"
3320 my $address = $self->address1;
3321 $address .= ", ". $self->address2 if $self->address2;
3323 my($payname, $payfirst, $paylast);
3324 if ( $self->payname && $method ne 'ECHECK' ) {
3325 $payname = $self->payname;
3326 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3327 or return "Illegal payname $payname";
3328 ($payfirst, $paylast) = ($1, $2);
3330 $payfirst = $self->getfield('first');
3331 $paylast = $self->getfield('last');
3332 $payname = "$payfirst $paylast";
3335 my @invoicing_list = $self->invoicing_list_emailonly;
3336 if ( $conf->exists('emailinvoiceautoalways')
3337 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3338 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3339 push @invoicing_list, $self->all_emails;
3342 my $email = ($conf->exists('business-onlinepayment-email-override'))
3343 ? $conf->config('business-onlinepayment-email-override')
3344 : $invoicing_list[0];
3346 my $payip = exists($options{'payip'})
3349 $content{customer_ip} = $payip
3353 if ( $method eq 'CC' ) {
3356 $content{card_number} = $payinfo = $cust_pay->payinfo;
3357 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
3358 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
3359 ($content{expiration} = "$2/$1"); # where available
3361 $content{card_number} = $payinfo = $self->payinfo;
3362 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
3363 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3364 $content{expiration} = "$2/$1";
3367 } elsif ( $method eq 'ECHECK' ) {
3370 $payinfo = $cust_pay->payinfo;
3372 $payinfo = $self->payinfo;
3374 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
3375 $content{bank_name} = $self->payname;
3376 $content{account_type} = 'CHECKING';
3377 $content{account_name} = $payname;
3378 $content{customer_org} = $self->company ? 'B' : 'I';
3379 $content{customer_ssn} = $self->ss;
3380 } elsif ( $method eq 'LEC' ) {
3381 $content{phone} = $payinfo = $self->payinfo;
3385 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3386 my %sub_content = $refund->content(
3387 'action' => 'credit',
3388 'customer_id' => $self->custnum,
3389 'last_name' => $paylast,
3390 'first_name' => $payfirst,
3392 'address' => $address,
3393 'city' => $self->city,
3394 'state' => $self->state,
3395 'zip' => $self->zip,
3396 'country' => $self->country,
3398 'phone' => $self->daytime || $self->night,
3401 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3405 return "$processor error: ". $refund->error_message
3406 unless $refund->is_success();
3408 my %method2payby = (
3414 my $paybatch = "$processor:". $refund->authorization;
3415 $paybatch .= ':'. $refund->order_number
3416 if $refund->can('order_number') && $refund->order_number;
3418 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
3419 my @cust_bill_pay = $cust_pay->cust_bill_pay;
3420 last unless @cust_bill_pay;
3421 my $cust_bill_pay = pop @cust_bill_pay;
3422 my $error = $cust_bill_pay->delete;
3426 my $cust_refund = new FS::cust_refund ( {
3427 'custnum' => $self->custnum,
3428 'paynum' => $options{'paynum'},
3429 'refund' => $amount,
3431 'payby' => $method2payby{$method},
3432 'payinfo' => $payinfo,
3433 'paybatch' => $paybatch,
3434 'reason' => $options{'reason'} || 'card or ACH refund',
3436 my $error = $cust_refund->insert;
3438 $cust_refund->paynum(''); #try again with no specific paynum
3439 my $error2 = $cust_refund->insert;
3441 # gah, even with transactions.
3442 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
3443 "error inserting refund ($processor): $error2".
3444 " (previously tried insert with paynum #$options{'paynum'}" .
3455 =item batch_card OPTION => VALUE...
3457 Adds a payment for this invoice to the pending credit card batch (see
3458 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
3459 runs the payment using a realtime gateway.
3464 my ($self, %options) = @_;
3467 if (exists($options{amount})) {
3468 $amount = $options{amount};
3470 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
3472 return '' unless $amount > 0;
3474 my $invnum = delete $options{invnum};
3475 my $payby = $options{invnum} || $self->payby; #dubious
3477 if ($options{'realtime'}) {
3478 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
3484 my $oldAutoCommit = $FS::UID::AutoCommit;
3485 local $FS::UID::AutoCommit = 0;
3488 #this needs to handle mysql as well as Pg, like svc_acct.pm
3489 #(make it into a common function if folks need to do batching with mysql)
3490 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
3491 or return "Cannot lock pay_batch: " . $dbh->errstr;
3495 'payby' => FS::payby->payby2payment($payby),
3498 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
3500 unless ( $pay_batch ) {
3501 $pay_batch = new FS::pay_batch \%pay_batch;
3502 my $error = $pay_batch->insert;
3504 $dbh->rollback if $oldAutoCommit;
3505 die "error creating new batch: $error\n";
3509 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
3510 'batchnum' => $pay_batch->batchnum,
3511 'custnum' => $self->custnum,
3514 foreach (qw( address1 address2 city state zip country payby payinfo paydate
3516 $options{$_} = '' unless exists($options{$_});
3519 my $cust_pay_batch = new FS::cust_pay_batch ( {
3520 'batchnum' => $pay_batch->batchnum,
3521 'invnum' => $invnum || 0, # is there a better value?
3522 # this field should be
3524 # cust_bill_pay_batch now
3525 'custnum' => $self->custnum,
3526 'last' => $self->getfield('last'),
3527 'first' => $self->getfield('first'),
3528 'address1' => $options{address1} || $self->address1,
3529 'address2' => $options{address2} || $self->address2,
3530 'city' => $options{city} || $self->city,
3531 'state' => $options{state} || $self->state,
3532 'zip' => $options{zip} || $self->zip,
3533 'country' => $options{country} || $self->country,
3534 'payby' => $options{payby} || $self->payby,
3535 'payinfo' => $options{payinfo} || $self->payinfo,
3536 'exp' => $options{paydate} || $self->paydate,
3537 'payname' => $options{payname} || $self->payname,
3538 'amount' => $amount, # consolidating
3541 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
3542 if $old_cust_pay_batch;
3545 if ($old_cust_pay_batch) {
3546 $error = $cust_pay_batch->replace($old_cust_pay_batch)
3548 $error = $cust_pay_batch->insert;
3552 $dbh->rollback if $oldAutoCommit;
3556 my $unapplied = $self->total_credited + $self->total_unapplied_payments + $self->in_transit_payments;
3557 foreach my $cust_bill ($self->open_cust_bill) {
3558 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
3559 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
3560 'invnum' => $cust_bill->invnum,
3561 'paybatchnum' => $cust_pay_batch->paybatchnum,
3562 'amount' => $cust_bill->owed,
3565 if ($unapplied >= $cust_bill_pay_batch->amount){
3566 $unapplied -= $cust_bill_pay_batch->amount;
3569 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
3570 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
3572 $error = $cust_bill_pay_batch->insert;
3574 $dbh->rollback if $oldAutoCommit;
3579 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3585 Returns the total owed for this customer on all invoices
3586 (see L<FS::cust_bill/owed>).
3592 $self->total_owed_date(2145859200); #12/31/2037
3595 =item total_owed_date TIME
3597 Returns the total owed for this customer on all invoices with date earlier than
3598 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
3599 see L<Time::Local> and L<Date::Parse> for conversion functions.
3603 sub total_owed_date {
3607 foreach my $cust_bill (
3608 grep { $_->_date <= $time }
3609 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3611 $total_bill += $cust_bill->owed;
3613 sprintf( "%.2f", $total_bill );
3616 =item apply_payments_and_credits
3618 Applies unapplied payments and credits.
3620 In most cases, this new method should be used in place of sequential
3621 apply_payments and apply_credits methods.
3623 If there is an error, returns the error, otherwise returns false.
3627 sub apply_payments_and_credits {
3630 local $SIG{HUP} = 'IGNORE';
3631 local $SIG{INT} = 'IGNORE';
3632 local $SIG{QUIT} = 'IGNORE';
3633 local $SIG{TERM} = 'IGNORE';
3634 local $SIG{TSTP} = 'IGNORE';
3635 local $SIG{PIPE} = 'IGNORE';
3637 my $oldAutoCommit = $FS::UID::AutoCommit;
3638 local $FS::UID::AutoCommit = 0;
3641 $self->select_for_update; #mutex
3643 foreach my $cust_bill ( $self->open_cust_bill ) {
3644 my $error = $cust_bill->apply_payments_and_credits;
3646 $dbh->rollback if $oldAutoCommit;
3647 return "Error applying: $error";
3651 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3656 =item apply_credits OPTION => VALUE ...
3658 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
3659 to outstanding invoice balances in chronological order (or reverse
3660 chronological order if the I<order> option is set to B<newest>) and returns the
3661 value of any remaining unapplied credits available for refund (see
3662 L<FS::cust_refund>).
3664 Dies if there is an error.
3672 local $SIG{HUP} = 'IGNORE';
3673 local $SIG{INT} = 'IGNORE';
3674 local $SIG{QUIT} = 'IGNORE';
3675 local $SIG{TERM} = 'IGNORE';
3676 local $SIG{TSTP} = 'IGNORE';
3677 local $SIG{PIPE} = 'IGNORE';
3679 my $oldAutoCommit = $FS::UID::AutoCommit;
3680 local $FS::UID::AutoCommit = 0;
3683 $self->select_for_update; #mutex
3685 unless ( $self->total_credited ) {
3686 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3690 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
3691 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
3693 my @invoices = $self->open_cust_bill;
3694 @invoices = sort { $b->_date <=> $a->_date } @invoices
3695 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
3698 foreach my $cust_bill ( @invoices ) {
3701 if ( !defined($credit) || $credit->credited == 0) {
3702 $credit = pop @credits or last;
3705 if ($cust_bill->owed >= $credit->credited) {
3706 $amount=$credit->credited;
3708 $amount=$cust_bill->owed;
3711 my $cust_credit_bill = new FS::cust_credit_bill ( {
3712 'crednum' => $credit->crednum,
3713 'invnum' => $cust_bill->invnum,
3714 'amount' => $amount,
3716 my $error = $cust_credit_bill->insert;
3718 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3722 redo if ($cust_bill->owed > 0);
3726 my $total_credited = $self->total_credited;
3728 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3730 return $total_credited;
3733 =item apply_payments
3735 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
3736 to outstanding invoice balances in chronological order.
3738 #and returns the value of any remaining unapplied payments.
3740 Dies if there is an error.
3744 sub apply_payments {
3747 local $SIG{HUP} = 'IGNORE';
3748 local $SIG{INT} = 'IGNORE';
3749 local $SIG{QUIT} = 'IGNORE';
3750 local $SIG{TERM} = 'IGNORE';
3751 local $SIG{TSTP} = 'IGNORE';
3752 local $SIG{PIPE} = 'IGNORE';
3754 my $oldAutoCommit = $FS::UID::AutoCommit;
3755 local $FS::UID::AutoCommit = 0;
3758 $self->select_for_update; #mutex
3762 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
3763 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
3765 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
3766 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
3770 foreach my $cust_bill ( @invoices ) {
3773 if ( !defined($payment) || $payment->unapplied == 0 ) {
3774 $payment = pop @payments or last;
3777 if ( $cust_bill->owed >= $payment->unapplied ) {
3778 $amount = $payment->unapplied;
3780 $amount = $cust_bill->owed;
3783 my $cust_bill_pay = new FS::cust_bill_pay ( {
3784 'paynum' => $payment->paynum,
3785 'invnum' => $cust_bill->invnum,
3786 'amount' => $amount,
3788 my $error = $cust_bill_pay->insert;
3790 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3794 redo if ( $cust_bill->owed > 0);
3798 my $total_unapplied_payments = $self->total_unapplied_payments;
3800 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3802 return $total_unapplied_payments;
3805 =item total_credited
3807 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3808 customer. See L<FS::cust_credit/credited>.
3812 sub total_credited {
3814 my $total_credit = 0;
3815 foreach my $cust_credit ( qsearch('cust_credit', {
3816 'custnum' => $self->custnum,
3818 $total_credit += $cust_credit->credited;
3820 sprintf( "%.2f", $total_credit );
3823 =item total_unapplied_payments
3825 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3826 See L<FS::cust_pay/unapplied>.
3830 sub total_unapplied_payments {
3832 my $total_unapplied = 0;
3833 foreach my $cust_pay ( qsearch('cust_pay', {
3834 'custnum' => $self->custnum,
3836 $total_unapplied += $cust_pay->unapplied;
3838 sprintf( "%.2f", $total_unapplied );
3841 =item total_unapplied_refunds
3843 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
3844 customer. See L<FS::cust_refund/unapplied>.
3848 sub total_unapplied_refunds {
3850 my $total_unapplied = 0;
3851 foreach my $cust_refund ( qsearch('cust_refund', {
3852 'custnum' => $self->custnum,
3854 $total_unapplied += $cust_refund->unapplied;
3856 sprintf( "%.2f", $total_unapplied );
3861 Returns the balance for this customer (total_owed plus total_unrefunded, minus
3862 total_credited minus total_unapplied_payments).
3870 + $self->total_unapplied_refunds
3871 - $self->total_credited
3872 - $self->total_unapplied_payments
3876 =item balance_date TIME
3878 Returns the balance for this customer, only considering invoices with date
3879 earlier than TIME (total_owed_date minus total_credited minus
3880 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
3881 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
3890 $self->total_owed_date($time)
3891 + $self->total_unapplied_refunds
3892 - $self->total_credited
3893 - $self->total_unapplied_payments
3897 =item in_transit_payments
3899 Returns the total of requests for payments for this customer pending in
3900 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
3904 sub in_transit_payments {
3906 my $in_transit_payments = 0;
3907 foreach my $pay_batch ( qsearch('pay_batch', {
3910 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
3911 'batchnum' => $pay_batch->batchnum,
3912 'custnum' => $self->custnum,
3914 $in_transit_payments += $cust_pay_batch->amount;
3917 sprintf( "%.2f", $in_transit_payments );
3920 =item paydate_monthyear
3922 Returns a two-element list consisting of the month and year of this customer's
3923 paydate (credit card expiration date for CARD customers)
3927 sub paydate_monthyear {
3929 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
3931 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
3938 =item invoicing_list [ ARRAYREF ]
3940 If an arguement is given, sets these email addresses as invoice recipients
3941 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
3942 (except as warnings), so use check_invoicing_list first.
3944 Returns a list of email addresses (with svcnum entries expanded).
3946 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
3947 check it without disturbing anything by passing nothing.
3949 This interface may change in the future.
3953 sub invoicing_list {
3954 my( $self, $arrayref ) = @_;
3957 my @cust_main_invoice;
3958 if ( $self->custnum ) {
3959 @cust_main_invoice =
3960 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3962 @cust_main_invoice = ();
3964 foreach my $cust_main_invoice ( @cust_main_invoice ) {
3965 #warn $cust_main_invoice->destnum;
3966 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3967 #warn $cust_main_invoice->destnum;
3968 my $error = $cust_main_invoice->delete;
3969 warn $error if $error;
3972 if ( $self->custnum ) {
3973 @cust_main_invoice =
3974 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3976 @cust_main_invoice = ();
3978 my %seen = map { $_->address => 1 } @cust_main_invoice;
3979 foreach my $address ( @{$arrayref} ) {
3980 next if exists $seen{$address} && $seen{$address};
3981 $seen{$address} = 1;
3982 my $cust_main_invoice = new FS::cust_main_invoice ( {
3983 'custnum' => $self->custnum,
3986 my $error = $cust_main_invoice->insert;
3987 warn $error if $error;
3991 if ( $self->custnum ) {
3993 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
4000 =item check_invoicing_list ARRAYREF
4002 Checks these arguements as valid input for the invoicing_list method. If there
4003 is an error, returns the error, otherwise returns false.
4007 sub check_invoicing_list {
4008 my( $self, $arrayref ) = @_;
4010 foreach my $address ( @$arrayref ) {
4012 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
4013 return 'Can\'t add FAX invoice destination with a blank FAX number.';
4016 my $cust_main_invoice = new FS::cust_main_invoice ( {
4017 'custnum' => $self->custnum,
4020 my $error = $self->custnum
4021 ? $cust_main_invoice->check
4022 : $cust_main_invoice->checkdest
4024 return $error if $error;
4028 return "Email address required"
4029 if $conf->exists('cust_main-require_invoicing_list_email')
4030 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
4035 =item set_default_invoicing_list
4037 Sets the invoicing list to all accounts associated with this customer,
4038 overwriting any previous invoicing list.
4042 sub set_default_invoicing_list {
4044 $self->invoicing_list($self->all_emails);
4049 Returns the email addresses of all accounts provisioned for this customer.
4056 foreach my $cust_pkg ( $self->all_pkgs ) {
4057 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
4059 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4060 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4062 $list{$_}=1 foreach map { $_->email } @svc_acct;
4067 =item invoicing_list_addpost
4069 Adds postal invoicing to this customer. If this customer is already configured
4070 to receive postal invoices, does nothing.
4074 sub invoicing_list_addpost {
4076 return if grep { $_ eq 'POST' } $self->invoicing_list;
4077 my @invoicing_list = $self->invoicing_list;
4078 push @invoicing_list, 'POST';
4079 $self->invoicing_list(\@invoicing_list);
4082 =item invoicing_list_emailonly
4084 Returns the list of email invoice recipients (invoicing_list without non-email
4085 destinations such as POST and FAX).
4089 sub invoicing_list_emailonly {
4091 warn "$me invoicing_list_emailonly called"
4093 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
4096 =item invoicing_list_emailonly_scalar
4098 Returns the list of email invoice recipients (invoicing_list without non-email
4099 destinations such as POST and FAX) as a comma-separated scalar.
4103 sub invoicing_list_emailonly_scalar {
4105 warn "$me invoicing_list_emailonly_scalar called"
4107 join(', ', $self->invoicing_list_emailonly);
4110 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
4112 Returns an array of customers referred by this customer (referral_custnum set
4113 to this custnum). If DEPTH is given, recurses up to the given depth, returning
4114 customers referred by customers referred by this customer and so on, inclusive.
4115 The default behavior is DEPTH 1 (no recursion).
4119 sub referral_cust_main {
4121 my $depth = @_ ? shift : 1;
4122 my $exclude = @_ ? shift : {};
4125 map { $exclude->{$_->custnum}++; $_; }
4126 grep { ! $exclude->{ $_->custnum } }
4127 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
4131 map { $_->referral_cust_main($depth-1, $exclude) }
4138 =item referral_cust_main_ncancelled
4140 Same as referral_cust_main, except only returns customers with uncancelled
4145 sub referral_cust_main_ncancelled {
4147 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
4150 =item referral_cust_pkg [ DEPTH ]
4152 Like referral_cust_main, except returns a flat list of all unsuspended (and
4153 uncancelled) packages for each customer. The number of items in this list may
4154 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
4158 sub referral_cust_pkg {
4160 my $depth = @_ ? shift : 1;
4162 map { $_->unsuspended_pkgs }
4163 grep { $_->unsuspended_pkgs }
4164 $self->referral_cust_main($depth);
4167 =item referring_cust_main
4169 Returns the single cust_main record for the customer who referred this customer
4170 (referral_custnum), or false.
4174 sub referring_cust_main {
4176 return '' unless $self->referral_custnum;
4177 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
4180 =item credit AMOUNT, REASON
4182 Applies a credit to this customer. If there is an error, returns the error,
4183 otherwise returns false.
4188 my( $self, $amount, $reason, %options ) = @_;
4189 my $cust_credit = new FS::cust_credit {
4190 'custnum' => $self->custnum,
4191 'amount' => $amount,
4192 'reason' => $reason,
4194 $cust_credit->insert(%options);
4197 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
4199 Creates a one-time charge for this customer. If there is an error, returns
4200 the error, otherwise returns false.
4206 my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
4207 if ( ref( $_[0] ) ) {
4208 $amount = $_[0]->{amount};
4209 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
4210 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
4211 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
4212 : '$'. sprintf("%.2f",$amount);
4213 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
4214 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
4215 $additional = $_[0]->{additional};
4219 $pkg = @_ ? shift : 'One-time charge';
4220 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
4221 $taxclass = @_ ? shift : '';
4225 local $SIG{HUP} = 'IGNORE';
4226 local $SIG{INT} = 'IGNORE';
4227 local $SIG{QUIT} = 'IGNORE';
4228 local $SIG{TERM} = 'IGNORE';
4229 local $SIG{TSTP} = 'IGNORE';
4230 local $SIG{PIPE} = 'IGNORE';
4232 my $oldAutoCommit = $FS::UID::AutoCommit;
4233 local $FS::UID::AutoCommit = 0;
4236 my $part_pkg = new FS::part_pkg ( {
4238 'comment' => $comment,
4242 'classnum' => $classnum ? $classnum : '',
4243 'taxclass' => $taxclass,
4246 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
4247 ( 0 .. @$additional - 1 )
4249 'additional_count' => scalar(@$additional),
4250 'setup_fee' => $amount,
4253 my $error = $part_pkg->insert( options => \%options );
4255 $dbh->rollback if $oldAutoCommit;
4259 my $pkgpart = $part_pkg->pkgpart;
4260 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
4261 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
4262 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
4263 $error = $type_pkgs->insert;
4265 $dbh->rollback if $oldAutoCommit;
4270 my $cust_pkg = new FS::cust_pkg ( {
4271 'custnum' => $self->custnum,
4272 'pkgpart' => $pkgpart,
4273 'quantity' => $quantity,
4276 $error = $cust_pkg->insert;
4278 $dbh->rollback if $oldAutoCommit;
4282 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4287 #=item charge_postal_fee
4289 #Applies a one time charge this customer. If there is an error,
4290 #returns the error, returns the cust_pkg charge object or false
4291 #if there was no charge.
4295 # This should be a customer event. For that to work requires that bill
4296 # also be a customer event.
4298 sub charge_postal_fee {
4301 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
4302 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
4304 my $cust_pkg = new FS::cust_pkg ( {
4305 'custnum' => $self->custnum,
4306 'pkgpart' => $pkgpart,
4310 my $error = $cust_pkg->insert;
4311 $error ? $error : $cust_pkg;
4316 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4322 sort { $a->_date <=> $b->_date }
4323 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4326 =item open_cust_bill
4328 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4333 sub open_cust_bill {
4335 grep { $_->owed > 0 } $self->cust_bill;
4340 Returns all the credits (see L<FS::cust_credit>) for this customer.
4346 sort { $a->_date <=> $b->_date }
4347 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4352 Returns all the payments (see L<FS::cust_pay>) for this customer.
4358 sort { $a->_date <=> $b->_date }
4359 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
4364 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4370 sort { $a->_date <=> $b->_date }
4371 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4377 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4383 sort { $a->_date <=> $b->_date }
4384 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4389 Returns a name string for this customer, either "Company (Last, First)" or
4396 my $name = $self->contact;
4397 $name = $self->company. " ($name)" if $self->company;
4403 Returns a name string for this (service/shipping) contact, either
4404 "Company (Last, First)" or "Last, First".
4410 if ( $self->get('ship_last') ) {
4411 my $name = $self->ship_contact;
4412 $name = $self->ship_company. " ($name)" if $self->ship_company;
4421 Returns a name string for this customer, either "Company" or "First Last".
4427 $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
4430 =item ship_name_short
4432 Returns a name string for this (service/shipping) contact, either "Company"
4437 sub ship_name_short {
4439 if ( $self->get('ship_last') ) {
4440 $self->ship_company !~ /^\s*$/
4441 ? $self->ship_company
4442 : $self->ship_contact_firstlast;
4444 $self->name_company_or_firstlast;
4450 Returns this customer's full (billing) contact name only, "Last, First"
4456 $self->get('last'). ', '. $self->first;
4461 Returns this customer's full (shipping) contact name only, "Last, First"
4467 $self->get('ship_last')
4468 ? $self->get('ship_last'). ', '. $self->ship_first
4472 =item contact_firstlast
4474 Returns this customers full (billing) contact name only, "First Last".
4478 sub contact_firstlast {
4480 $self->first. ' '. $self->get('last');
4483 =item ship_contact_firstlast
4485 Returns this customer's full (shipping) contact name only, "First Last".
4489 sub ship_contact_firstlast {
4491 $self->get('ship_last')
4492 ? $self->first. ' '. $self->get('ship_last')
4493 : $self->contact_firstlast;
4498 Returns this customer's full country name
4504 code2country($self->country);
4511 Returns a status string for this customer, currently:
4515 =item prospect - No packages have ever been ordered
4517 =item active - One or more recurring packages is active
4519 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
4521 =item suspended - All non-cancelled recurring packages are suspended
4523 =item cancelled - All recurring packages are cancelled
4529 sub status { shift->cust_status(@_); }
4533 for my $status (qw( prospect active inactive suspended cancelled )) {
4534 my $method = $status.'_sql';
4535 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
4536 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
4537 $sth->execute( ($self->custnum) x $numnum )
4538 or die "Error executing 'SELECT $sql': ". $sth->errstr;
4539 return $status if $sth->fetchrow_arrayref->[0];
4543 =item ucfirst_cust_status
4545 =item ucfirst_status
4547 Returns the status with the first character capitalized.
4551 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
4553 sub ucfirst_cust_status {
4555 ucfirst($self->cust_status);
4560 Returns a hex triplet color string for this customer's status.
4564 use vars qw(%statuscolor);
4565 tie %statuscolor, 'Tie::IxHash',
4566 'prospect' => '7e0079', #'000000', #black? naw, purple
4567 'active' => '00CC00', #green
4568 'inactive' => '0000CC', #blue
4569 'suspended' => 'FF9900', #yellow
4570 'cancelled' => 'FF0000', #red
4573 sub statuscolor { shift->cust_statuscolor(@_); }
4575 sub cust_statuscolor {
4577 $statuscolor{$self->cust_status};
4582 =head1 CLASS METHODS
4588 Class method that returns the list of possible status strings for customers
4589 (see L<the status method|/status>). For example:
4591 @statuses = FS::cust_main->statuses();
4596 #my $self = shift; #could be class...
4602 Returns an SQL expression identifying prospective cust_main records (customers
4603 with no packages ever ordered)
4607 use vars qw($select_count_pkgs);
4608 $select_count_pkgs =
4609 "SELECT COUNT(*) FROM cust_pkg
4610 WHERE cust_pkg.custnum = cust_main.custnum";
4612 sub select_count_pkgs_sql {
4616 sub prospect_sql { "
4617 0 = ( $select_count_pkgs )
4622 Returns an SQL expression identifying active cust_main records (customers with
4623 active recurring packages).
4628 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
4634 Returns an SQL expression identifying inactive cust_main records (customers with
4635 no active recurring packages, but otherwise unsuspended/uncancelled).
4639 sub inactive_sql { "
4640 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4642 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
4648 Returns an SQL expression identifying suspended cust_main records.
4653 sub suspended_sql { susp_sql(@_); }
4655 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
4657 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4663 Returns an SQL expression identifying cancelled cust_main records.
4667 sub cancelled_sql { cancel_sql(@_); }
4670 my $recurring_sql = FS::cust_pkg->recurring_sql;
4671 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
4674 0 < ( $select_count_pkgs )
4675 AND 0 < ( $select_count_pkgs AND $recurring_sql AND $cancelled_sql )
4676 AND 0 = ( $select_count_pkgs AND $recurring_sql
4677 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4679 AND 0 = ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
4685 =item uncancelled_sql
4687 Returns an SQL expression identifying un-cancelled cust_main records.
4691 sub uncancelled_sql { uncancel_sql(@_); }
4692 sub uncancel_sql { "
4693 ( 0 < ( $select_count_pkgs
4694 AND ( cust_pkg.cancel IS NULL
4695 OR cust_pkg.cancel = 0
4698 OR 0 = ( $select_count_pkgs )
4704 Returns an SQL fragment to retreive the balance.
4709 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
4710 WHERE cust_bill.custnum = cust_main.custnum )
4711 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
4712 WHERE cust_pay.custnum = cust_main.custnum )
4713 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
4714 WHERE cust_credit.custnum = cust_main.custnum )
4715 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
4716 WHERE cust_refund.custnum = cust_main.custnum )
4719 =item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4721 Returns an SQL fragment to retreive the balance for this customer, only
4722 considering invoices with date earlier than START_TIME, and optionally not
4723 later than END_TIME (total_owed_date minus total_credited minus
4724 total_unapplied_payments).
4726 Times are specified as SQL fragments or numeric
4727 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4728 L<Date::Parse> for conversion functions. The empty string can be passed
4729 to disable that time constraint completely.
4731 Available options are:
4735 =item unapplied_date - 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)
4737 =item total - set to true to remove all customer comparison clauses, for totals
4739 =item where - WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
4741 =item join - JOIN clause (typically used with the total option)
4749 sub balance_date_sql {
4750 my( $class, $start, $end, %opt ) = @_;
4752 my $owed = FS::cust_bill->owed_sql;
4753 my $unapp_refund = FS::cust_refund->unapplied_sql;
4754 my $unapp_credit = FS::cust_credit->unapplied_sql;
4755 my $unapp_pay = FS::cust_pay->unapplied_sql;
4757 my $j = $opt{'join'} || '';
4759 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
4760 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
4761 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
4762 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
4764 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
4765 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
4766 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
4767 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
4772 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4774 Helper method for balance_date_sql; name (and usage) subject to change
4775 (suggestions welcome).
4777 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
4778 cust_refund, cust_credit or cust_pay).
4780 If TABLE is "cust_bill" or the unapplied_date option is true, only
4781 considers records with date earlier than START_TIME, and optionally not
4782 later than END_TIME .
4786 sub _money_table_where {
4787 my( $class, $table, $start, $end, %opt ) = @_;
4790 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
4791 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
4792 push @where, "$table._date <= $start" if defined($start) && length($start);
4793 push @where, "$table._date > $end" if defined($end) && length($end);
4795 push @where, @{$opt{'where'}} if $opt{'where'};
4796 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
4802 =item search_sql HASHREF
4806 Returns a qsearch hash expression to search for parameters specified in HREF.
4807 Valid parameters are
4815 =item cancelled_pkgs
4821 listref of start date, end date
4827 =item current_balance
4829 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
4833 =item flattened_pkgs
4842 my ($class, $params) = @_;
4853 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4855 "cust_main.agentnum = $1";
4862 #prospect active inactive suspended cancelled
4863 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
4864 my $method = $params->{'status'}. '_sql';
4865 #push @where, $class->$method();
4866 push @where, FS::cust_main->$method();
4870 # parse cancelled package checkbox
4875 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
4876 unless $params->{'cancelled_pkgs'};
4882 foreach my $field (qw( signupdate )) {
4884 next unless exists($params->{$field});
4886 my($beginning, $ending) = @{$params->{$field}};
4889 "cust_main.$field IS NOT NULL",
4890 "cust_main.$field >= $beginning",
4891 "cust_main.$field <= $ending";
4893 $orderby ||= "ORDER BY cust_main.$field";
4901 my @payby = grep /^([A-Z]{4})$/, @{ $params->{'payby'} };
4903 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
4910 #my $balance_sql = $class->balance_sql();
4911 my $balance_sql = FS::cust_main->balance_sql();
4913 push @where, map { s/current_balance/$balance_sql/; $_ }
4914 @{ $params->{'current_balance'} };
4920 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4922 "cust_main.custbatch = '$1'";
4926 # setup queries, subs, etc. for the search
4929 $orderby ||= 'ORDER BY custnum';
4931 # here is the agent virtualization
4932 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
4934 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4936 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
4938 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
4940 my $select = join(', ',
4941 'cust_main.custnum',
4942 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
4945 my(@extra_headers) = ();
4946 my(@extra_fields) = ();
4948 if ($params->{'flattened_pkgs'}) {
4950 if ($dbh->{Driver}->{Name} eq 'Pg') {
4952 $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";
4954 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
4955 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
4956 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
4958 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
4959 "omitting packing information from report.";
4962 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";
4964 my $sth = dbh->prepare($header_query) or die dbh->errstr;
4965 $sth->execute() or die $sth->errstr;
4966 my $headerrow = $sth->fetchrow_arrayref;
4967 my $headercount = $headerrow ? $headerrow->[0] : 0;
4968 while($headercount) {
4969 unshift @extra_headers, "Package ". $headercount;
4970 unshift @extra_fields, eval q!sub {my $c = shift;
4971 my @a = split '\|', $c->magic;
4972 my $p = $a[!.--$headercount. q!];
4980 'table' => 'cust_main',
4981 'select' => $select,
4983 'extra_sql' => $extra_sql,
4984 'order_by' => $orderby,
4985 'count_query' => $count_query,
4986 'extra_headers' => \@extra_headers,
4987 'extra_fields' => \@extra_fields,
4992 =item email_search_sql HASHREF
4996 Emails a notice to the specified customers.
4998 Valid parameters are those of the L<search_sql> method, plus the following:
5020 Optional job queue job for status updates.
5024 Returns an error message, or false for success.
5026 If an error occurs during any email, stops the enture send and returns that
5027 error. Presumably if you're getting SMTP errors aborting is better than
5028 retrying everything.
5032 sub email_search_sql {
5033 my($class, $params) = @_;
5035 my $from = delete $params->{from};
5036 my $subject = delete $params->{subject};
5037 my $html_body = delete $params->{html_body};
5038 my $text_body = delete $params->{text_body};
5040 my $job = delete $params->{'job'};
5042 my $sql_query = $class->search_sql($params);
5044 my $count_query = delete($sql_query->{'count_query'});
5045 my $count_sth = dbh->prepare($count_query)
5046 or die "Error preparing $count_query: ". dbh->errstr;
5048 or die "Error executing $count_query: ". $count_sth->errstr;
5049 my $count_arrayref = $count_sth->fetchrow_arrayref;
5050 my $num_cust = $count_arrayref->[0];
5052 #my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
5053 #my @extra_fields = @{ delete($sql_query->{'extra_fields'}) };
5056 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
5058 #eventually order+limit magic to reduce memory use?
5059 foreach my $cust_main ( qsearch($sql_query) ) {
5061 my $to = $cust_main->invoicing_list_emailonly_scalar;
5064 my $error = send_email(
5068 'subject' => $subject,
5069 'html_body' => $html_body,
5070 'text_body' => $text_body,
5073 return $error if $error;
5075 if ( $job ) { #progressbar foo
5077 if ( time - $min_sec > $last ) {
5078 my $error = $job->update_statustext(
5079 int( 100 * $num / $num_cust )
5081 die $error if $error;
5091 use Storable qw(thaw);
5094 sub process_email_search_sql {
5096 #warn "$me process_re_X $method for job $job\n" if $DEBUG;
5098 my $param = thaw(decode_base64(shift));
5099 warn Dumper($param) if $DEBUG;
5101 $param->{'job'} = $job;
5103 my $error = FS::cust_main->email_search_sql( $param );
5104 die $error if $error;
5108 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
5110 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
5111 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
5112 appropriate ship_ field is also searched).
5114 Additional options are the same as FS::Record::qsearch
5119 my( $self, $fuzzy, $hash, @opt) = @_;
5124 check_and_rebuild_fuzzyfiles();
5125 foreach my $field ( keys %$fuzzy ) {
5127 my $all = $self->all_X($field);
5128 next unless scalar(@$all);
5131 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
5134 foreach ( keys %match ) {
5135 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
5136 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
5139 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
5142 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
5144 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
5152 Returns a masked version of the named field
5157 my ($self, $field) = @_;
5161 'x'x(length($self->getfield($field))-4).
5162 substr($self->getfield($field), (length($self->getfield($field))-4));
5172 =item smart_search OPTION => VALUE ...
5174 Accepts the following options: I<search>, the string to search for. The string
5175 will be searched for as a customer number, phone number, name or company name,
5176 as an exact, or, in some cases, a substring or fuzzy match (see the source code
5177 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
5178 skip fuzzy matching when an exact match is found.
5180 Any additional options are treated as an additional qualifier on the search
5183 Returns a (possibly empty) array of FS::cust_main objects.
5190 #here is the agent virtualization
5191 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5195 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
5196 my $search = delete $options{'search'};
5197 ( my $alphanum_search = $search ) =~ s/\W//g;
5199 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
5201 #false laziness w/Record::ut_phone
5202 my $phonen = "$1-$2-$3";
5203 $phonen .= " x$4" if $4;
5205 push @cust_main, qsearch( {
5206 'table' => 'cust_main',
5207 'hashref' => { %options },
5208 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5210 join(' OR ', map "$_ = '$phonen'",
5211 qw( daytime night fax
5212 ship_daytime ship_night ship_fax )
5215 " AND $agentnums_sql", #agent virtualization
5218 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
5219 #try looking for matches with extensions unless one was specified
5221 push @cust_main, qsearch( {
5222 'table' => 'cust_main',
5223 'hashref' => { %options },
5224 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5226 join(' OR ', map "$_ LIKE '$phonen\%'",
5228 ship_daytime ship_night )
5231 " AND $agentnums_sql", #agent virtualization
5236 # custnum search (also try agent_custid), with some tweaking options if your
5237 # legacy cust "numbers" have letters
5238 } elsif ( $search =~ /^\s*(\d+)\s*$/
5239 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
5240 && $search =~ /^\s*(\w\w?\d+)\s*$/
5245 push @cust_main, qsearch( {
5246 'table' => 'cust_main',
5247 'hashref' => { 'custnum' => $1, %options },
5248 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5251 push @cust_main, qsearch( {
5252 'table' => 'cust_main',
5253 'hashref' => { 'agent_custid' => $1, %options },
5254 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5257 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
5259 my($company, $last, $first) = ( $1, $2, $3 );
5261 # "Company (Last, First)"
5262 #this is probably something a browser remembered,
5263 #so just do an exact search
5265 foreach my $prefix ( '', 'ship_' ) {
5266 push @cust_main, qsearch( {
5267 'table' => 'cust_main',
5268 'hashref' => { $prefix.'first' => $first,
5269 $prefix.'last' => $last,
5270 $prefix.'company' => $company,
5273 'extra_sql' => " AND $agentnums_sql",
5277 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
5278 # try (ship_){last,company}
5282 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
5283 # # full strings the browser remembers won't work
5284 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
5286 use Lingua::EN::NameParse;
5287 my $NameParse = new Lingua::EN::NameParse(
5289 allow_reversed => 1,
5292 my($last, $first) = ( '', '' );
5293 #maybe disable this too and just rely on NameParse?
5294 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
5296 ($last, $first) = ( $1, $2 );
5298 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
5299 } elsif ( ! $NameParse->parse($value) ) {
5301 my %name = $NameParse->components;
5302 $first = $name{'given_name_1'};
5303 $last = $name{'surname_1'};
5307 if ( $first && $last ) {
5309 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
5312 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5314 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
5315 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
5318 push @cust_main, qsearch( {
5319 'table' => 'cust_main',
5320 'hashref' => \%options,
5321 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5324 # or it just be something that was typed in... (try that in a sec)
5328 my $q_value = dbh->quote($value);
5331 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5332 $sql .= " ( LOWER(last) = $q_value
5333 OR LOWER(company) = $q_value
5334 OR LOWER(ship_last) = $q_value
5335 OR LOWER(ship_company) = $q_value
5338 push @cust_main, qsearch( {
5339 'table' => 'cust_main',
5340 'hashref' => \%options,
5341 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5344 #always do substring & fuzzy,
5345 #getting complains searches are not returning enough
5346 unless ( @cust_main && $skip_fuzzy ) { #no exact match, trying substring/fuzzy
5348 #still some false laziness w/search_sql (was search/cust_main.cgi)
5353 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
5354 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
5357 if ( $first && $last ) {
5360 { 'first' => { op=>'ILIKE', value=>"%$first%" },
5361 'last' => { op=>'ILIKE', value=>"%$last%" },
5363 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
5364 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
5371 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
5372 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
5376 foreach my $hashref ( @hashrefs ) {
5378 push @cust_main, qsearch( {
5379 'table' => 'cust_main',
5380 'hashref' => { %$hashref,
5383 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
5392 " AND $agentnums_sql", #extra_sql #agent virtualization
5395 if ( $first && $last ) {
5396 push @cust_main, FS::cust_main->fuzzy_search(
5397 { 'last' => $last, #fuzzy hashref
5398 'first' => $first }, #
5402 foreach my $field ( 'last', 'company' ) {
5404 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
5409 #eliminate duplicates
5411 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5421 Accepts the following options: I<email>, the email address to search for. The
5422 email address will be searched for as an email invoice destination and as an
5425 #Any additional options are treated as an additional qualifier on the search
5426 #(i.e. I<agentnum>).
5428 Returns a (possibly empty) array of FS::cust_main objects (but usually just
5438 my $email = delete $options{'email'};
5440 #we're only being used by RT at the moment... no agent virtualization yet
5441 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5445 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
5447 my ( $user, $domain ) = ( $1, $2 );
5449 warn "$me smart_search: searching for $user in domain $domain"
5455 'table' => 'cust_main_invoice',
5456 'hashref' => { 'dest' => $email },
5463 map $_->cust_svc->cust_pkg,
5465 'table' => 'svc_acct',
5466 'hashref' => { 'username' => $user, },
5468 'AND ( SELECT domain FROM svc_domain
5469 WHERE svc_acct.domsvc = svc_domain.svcnum
5470 ) = '. dbh->quote($domain),
5476 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5478 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
5485 =item check_and_rebuild_fuzzyfiles
5489 use vars qw(@fuzzyfields);
5490 @fuzzyfields = ( 'last', 'first', 'company' );
5492 sub check_and_rebuild_fuzzyfiles {
5493 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5494 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
5497 =item rebuild_fuzzyfiles
5501 sub rebuild_fuzzyfiles {
5503 use Fcntl qw(:flock);
5505 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5506 mkdir $dir, 0700 unless -d $dir;
5508 foreach my $fuzzy ( @fuzzyfields ) {
5510 open(LOCK,">>$dir/cust_main.$fuzzy")
5511 or die "can't open $dir/cust_main.$fuzzy: $!";
5513 or die "can't lock $dir/cust_main.$fuzzy: $!";
5515 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
5516 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
5518 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
5519 my $sth = dbh->prepare("SELECT $field FROM cust_main".
5520 " WHERE $field != '' AND $field IS NOT NULL");
5521 $sth->execute or die $sth->errstr;
5523 while ( my $row = $sth->fetchrow_arrayref ) {
5524 print CACHE $row->[0]. "\n";
5529 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
5531 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
5542 my( $self, $field ) = @_;
5543 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5544 open(CACHE,"<$dir/cust_main.$field")
5545 or die "can't open $dir/cust_main.$field: $!";
5546 my @array = map { chomp; $_; } <CACHE>;
5551 =item append_fuzzyfiles LASTNAME COMPANY
5555 sub append_fuzzyfiles {
5556 #my( $first, $last, $company ) = @_;
5558 &check_and_rebuild_fuzzyfiles;
5560 use Fcntl qw(:flock);
5562 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5564 foreach my $field (qw( first last company )) {
5569 open(CACHE,">>$dir/cust_main.$field")
5570 or die "can't open $dir/cust_main.$field: $!";
5571 flock(CACHE,LOCK_EX)
5572 or die "can't lock $dir/cust_main.$field: $!";
5574 print CACHE "$value\n";
5576 flock(CACHE,LOCK_UN)
5577 or die "can't unlock $dir/cust_main.$field: $!";
5586 =item process_batch_import
5588 Load a batch import as a queued JSRPC job
5592 use Storable qw(thaw);
5595 sub process_batch_import {
5598 my $param = thaw(decode_base64(shift));
5599 warn Dumper($param) if $DEBUG;
5601 my $files = $param->{'uploaded_files'}
5602 or die "No files provided.\n";
5604 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
5606 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
5607 my $file = $dir. $files{'file'};
5610 if ( $file =~ /\.(\w+)$/i ) {
5614 warn "can't parse file type from filename $file; defaulting to CSV";
5619 FS::cust_main::batch_import( {
5623 custbatch => $param->{custbatch},
5624 agentnum => $param->{'agentnum'},
5625 refnum => $param->{'refnum'},
5626 pkgpart => $param->{'pkgpart'},
5627 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
5628 # city state zip comments )],
5629 'format' => $param->{'format'},
5634 die "$error\n" if $error;
5642 #some false laziness w/cdr.pm now
5646 my $job = $param->{job};
5648 my $filename = $param->{file};
5649 my $type = $param->{type} || 'csv';
5651 my $custbatch = $param->{custbatch};
5653 my $agentnum = $param->{agentnum};
5654 my $refnum = $param->{refnum};
5655 my $pkgpart = $param->{pkgpart};
5657 my $format = $param->{'format'};
5661 if ( $format eq 'simple' ) {
5662 @fields = qw( cust_pkg.setup dayphone first last
5663 address1 address2 city state zip comments );
5665 } elsif ( $format eq 'extended' ) {
5666 @fields = qw( agent_custid refnum
5667 last first address1 address2 city state zip country
5669 ship_last ship_first ship_address1 ship_address2
5670 ship_city ship_state ship_zip ship_country
5671 payinfo paycvv paydate
5674 svc_acct.username svc_acct._password
5677 } elsif ( $format eq 'extended-plus_company' ) {
5678 @fields = qw( agent_custid refnum
5679 last first company address1 address2 city state zip country
5681 ship_last ship_first ship_company ship_address1 ship_address2
5682 ship_city ship_state ship_zip ship_country
5683 payinfo paycvv paydate
5686 svc_acct.username svc_acct._password
5690 die "unknown format $format";
5696 if ( $type eq 'csv' ) {
5698 eval "use Text::CSV_XS;";
5701 $parser = new Text::CSV_XS;
5703 @buffer = split(/\r?\n/, slurp($filename) );
5704 $count = scalar(@buffer);
5706 } elsif ( $type eq 'xls' ) {
5708 eval "use Spreadsheet::ParseExcel;";
5711 my $excel = new Spreadsheet::ParseExcel::Workbook->Parse($filename);
5712 $parser = $excel->{Worksheet}[0]; #first sheet
5714 $count = $parser->{MaxRow} || $parser->{MinRow};
5718 die "Unknown file type $type\n";
5723 local $SIG{HUP} = 'IGNORE';
5724 local $SIG{INT} = 'IGNORE';
5725 local $SIG{QUIT} = 'IGNORE';
5726 local $SIG{TERM} = 'IGNORE';
5727 local $SIG{TSTP} = 'IGNORE';
5728 local $SIG{PIPE} = 'IGNORE';
5730 my $oldAutoCommit = $FS::UID::AutoCommit;
5731 local $FS::UID::AutoCommit = 0;
5736 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
5740 if ( $type eq 'csv' ) {
5742 last unless scalar(@buffer);
5743 $line = shift(@buffer);
5745 $parser->parse($line) or do {
5746 $dbh->rollback if $oldAutoCommit;
5747 return "can't parse: ". $parser->error_input();
5749 @columns = $parser->fields();
5751 } elsif ( $type eq 'xls' ) {
5753 last if $row > ($parser->{MaxRow} || $parser->{MinRow});
5755 my @row = @{ $parser->{Cells}[$row] };
5756 @columns = map $_->{Val}, @row;
5759 #warn $z++. ": $_\n" for @columns;
5762 die "Unknown file type $type\n";
5765 #warn join('-',@columns);
5768 custbatch => $custbatch,
5769 agentnum => $agentnum,
5771 country => $conf->config('countrydefault') || 'US',
5772 payby => $payby, #default
5773 paydate => '12/2037', #default
5775 my $billtime = time;
5776 my %cust_pkg = ( pkgpart => $pkgpart );
5778 foreach my $field ( @fields ) {
5780 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
5782 #$cust_pkg{$1} = str2time( shift @$columns );
5783 if ( $1 eq 'pkgpart' ) {
5784 $cust_pkg{$1} = shift @columns;
5785 } elsif ( $1 eq 'setup' ) {
5786 $billtime = str2time(shift @columns);
5788 $cust_pkg{$1} = str2time( shift @columns );
5791 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
5793 $svc_acct{$1} = shift @columns;
5797 #refnum interception
5798 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
5800 my $referral = $columns[0];
5801 my %hash = ( 'referral' => $referral,
5802 'agentnum' => $agentnum,
5806 my $part_referral = qsearchs('part_referral', \%hash )
5807 || new FS::part_referral \%hash;
5809 unless ( $part_referral->refnum ) {
5810 my $error = $part_referral->insert;
5812 $dbh->rollback if $oldAutoCommit;
5813 return "can't auto-insert advertising source: $referral: $error";
5817 $columns[0] = $part_referral->refnum;
5820 my $value = shift @columns;
5821 $cust_main{$field} = $value if length($value);
5825 $cust_main{'payby'} = 'CARD'
5826 if defined $cust_main{'payinfo'}
5827 && length $cust_main{'payinfo'};
5829 my $invoicing_list = $cust_main{'invoicing_list'}
5830 ? [ delete $cust_main{'invoicing_list'} ]
5833 my $cust_main = new FS::cust_main ( \%cust_main );
5836 tie my %hash, 'Tie::RefHash'; #this part is important
5838 if ( $cust_pkg{'pkgpart'} ) {
5839 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
5842 if ( $svc_acct{'username'} ) {
5843 my $part_pkg = $cust_pkg->part_pkg;
5844 unless ( $part_pkg ) {
5845 $dbh->rollback if $oldAutoCommit;
5846 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
5848 $svc_acct{svcpart} = $part_pkg->svcpart( 'svc_acct' );
5849 push @svc_acct, new FS::svc_acct ( \%svc_acct )
5852 $hash{$cust_pkg} = \@svc_acct;
5855 my $error = $cust_main->insert( \%hash, $invoicing_list );
5858 $dbh->rollback if $oldAutoCommit;
5859 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
5862 if ( $format eq 'simple' ) {
5864 #false laziness w/bill.cgi
5865 $error = $cust_main->bill( 'time' => $billtime );
5867 $dbh->rollback if $oldAutoCommit;
5868 return "can't bill customer for $line: $error";
5871 $error = $cust_main->apply_payments_and_credits;
5873 $dbh->rollback if $oldAutoCommit;
5874 return "can't bill customer for $line: $error";
5877 $error = $cust_main->collect();
5879 $dbh->rollback if $oldAutoCommit;
5880 return "can't collect customer for $line: $error";
5887 if ( $job && time - $min_sec > $last ) { #progress bar
5888 $job->update_statustext( int(100 * $row / $count) );
5894 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
5896 return "Empty file!" unless $row;
5908 #warn join('-',keys %$param);
5909 my $fh = $param->{filehandle};
5910 my @fields = @{$param->{fields}};
5912 eval "use Text::CSV_XS;";
5915 my $csv = new Text::CSV_XS;
5922 local $SIG{HUP} = 'IGNORE';
5923 local $SIG{INT} = 'IGNORE';
5924 local $SIG{QUIT} = 'IGNORE';
5925 local $SIG{TERM} = 'IGNORE';
5926 local $SIG{TSTP} = 'IGNORE';
5927 local $SIG{PIPE} = 'IGNORE';
5929 my $oldAutoCommit = $FS::UID::AutoCommit;
5930 local $FS::UID::AutoCommit = 0;
5933 #while ( $columns = $csv->getline($fh) ) {
5935 while ( defined($line=<$fh>) ) {
5937 $csv->parse($line) or do {
5938 $dbh->rollback if $oldAutoCommit;
5939 return "can't parse: ". $csv->error_input();
5942 my @columns = $csv->fields();
5943 #warn join('-',@columns);
5946 foreach my $field ( @fields ) {
5947 $row{$field} = shift @columns;
5950 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
5951 unless ( $cust_main ) {
5952 $dbh->rollback if $oldAutoCommit;
5953 return "unknown custnum $row{'custnum'}";
5956 if ( $row{'amount'} > 0 ) {
5957 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
5959 $dbh->rollback if $oldAutoCommit;
5963 } elsif ( $row{'amount'} < 0 ) {
5964 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
5967 $dbh->rollback if $oldAutoCommit;
5977 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5979 return "Empty file!" unless $imported;
5985 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5987 Sends a templated email notification to the customer (see L<Text::Template>).
5989 OPTIONS is a hash and may include
5991 I<from> - the email sender (default is invoice_from)
5993 I<to> - comma-separated scalar or arrayref of recipients
5994 (default is invoicing_list)
5996 I<subject> - The subject line of the sent email notification
5997 (default is "Notice from company_name")
5999 I<extra_fields> - a hashref of name/value pairs which will be substituted
6002 The following variables are vavailable in the template.
6004 I<$first> - the customer first name
6005 I<$last> - the customer last name
6006 I<$company> - the customer company
6007 I<$payby> - a description of the method of payment for the customer
6008 # would be nice to use FS::payby::shortname
6009 I<$payinfo> - the account information used to collect for this customer
6010 I<$expdate> - the expiration of the customer payment in seconds from epoch
6015 my ($customer, $template, %options) = @_;
6017 return unless $conf->exists($template);
6019 my $from = $conf->config('invoice_from') if $conf->exists('invoice_from');
6020 $from = $options{from} if exists($options{from});
6022 my $to = join(',', $customer->invoicing_list_emailonly);
6023 $to = $options{to} if exists($options{to});
6025 my $subject = "Notice from " . $conf->config('company_name')
6026 if $conf->exists('company_name');
6027 $subject = $options{subject} if exists($options{subject});
6029 my $notify_template = new Text::Template (TYPE => 'ARRAY',
6030 SOURCE => [ map "$_\n",
6031 $conf->config($template)]
6033 or die "can't create new Text::Template object: Text::Template::ERROR";
6034 $notify_template->compile()
6035 or die "can't compile template: Text::Template::ERROR";
6037 my $paydate = $customer->paydate || '2037-12-31';
6038 $FS::notify_template::_template::first = $customer->first;
6039 $FS::notify_template::_template::last = $customer->last;
6040 $FS::notify_template::_template::company = $customer->company;
6041 $FS::notify_template::_template::payinfo = $customer->mask_payinfo;
6042 my $payby = $customer->payby;
6043 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6044 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6046 #credit cards expire at the end of the month/year of their exp date
6047 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6048 $FS::notify_template::_template::payby = 'credit card';
6049 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6050 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6052 }elsif ($payby eq 'COMP') {
6053 $FS::notify_template::_template::payby = 'complimentary account';
6055 $FS::notify_template::_template::payby = 'current method';
6057 $FS::notify_template::_template::expdate = $expire_time;
6059 for (keys %{$options{extra_fields}}){
6061 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
6064 send_email(from => $from,
6066 subject => $subject,
6067 body => $notify_template->fill_in( PACKAGE =>
6068 'FS::notify_template::_template' ),
6073 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
6075 Generates a templated notification to the customer (see L<Text::Template>).
6077 OPTIONS is a hash and may include
6079 I<extra_fields> - a hashref of name/value pairs which will be substituted
6080 into the template. These values may override values mentioned below
6081 and those from the customer record.
6083 The following variables are available in the template instead of or in addition
6084 to the fields of the customer record.
6086 I<$payby> - a description of the method of payment for the customer
6087 # would be nice to use FS::payby::shortname
6088 I<$payinfo> - the masked account information used to collect for this customer
6089 I<$expdate> - the expiration of the customer payment method in seconds from epoch
6090 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress
6094 sub generate_letter {
6095 my ($self, $template, %options) = @_;
6097 return unless $conf->exists($template);
6099 my $letter_template = new Text::Template
6101 SOURCE => [ map "$_\n", $conf->config($template)],
6102 DELIMITERS => [ '[@--', '--@]' ],
6104 or die "can't create new Text::Template object: Text::Template::ERROR";
6106 $letter_template->compile()
6107 or die "can't compile template: Text::Template::ERROR";
6109 my %letter_data = map { $_ => $self->$_ } $self->fields;
6110 $letter_data{payinfo} = $self->mask_payinfo;
6112 my $paydate = $self->paydate || '2037-12-31';
6113 my $payby = $self->payby;
6114 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6115 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6117 #credit cards expire at the end of the month/year of their exp date
6118 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6119 $letter_data{payby} = 'credit card';
6120 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6121 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6123 }elsif ($payby eq 'COMP') {
6124 $letter_data{payby} = 'complimentary account';
6126 $letter_data{payby} = 'current method';
6128 $letter_data{expdate} = $expire_time;
6130 for (keys %{$options{extra_fields}}){
6131 $letter_data{$_} = $options{extra_fields}->{$_};
6134 unless(exists($letter_data{returnaddress})){
6135 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
6136 $self->_agent_template)
6139 $letter_data{returnaddress} = length($retadd) ? $retadd : '~';
6142 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
6144 my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
6145 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
6149 ) or die "can't open temp file: $!\n";
6151 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
6153 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
6157 =item print_ps TEMPLATE
6159 Returns an postscript letter filled in from TEMPLATE, as a scalar.
6165 my $file = $self->generate_letter(@_);
6166 FS::Misc::generate_ps($file);
6169 =item print TEMPLATE
6171 Prints the filled in template.
6173 TEMPLATE is the name of a L<Text::Template> to fill in and print.
6177 sub queueable_print {
6180 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
6181 or die "invalid customer number: " . $opt{custvnum};
6183 my $error = $self->print( $opt{template} );
6184 die $error if $error;
6188 my ($self, $template) = (shift, shift);
6189 do_print [ $self->print_ps($template) ];
6192 sub agent_template {
6194 $self->_agent_plandata('agent_templatename');
6197 sub agent_invoice_from {
6199 $self->_agent_plandata('agent_invoice_from');
6202 sub _agent_plandata {
6203 my( $self, $option ) = @_;
6206 if ( driver_name =~ /^Pg/i ) {
6208 } elsif ( driver_name =~ /^mysql/i ) {
6211 die "don't know how to use regular expressions in ". driver_name. " databases";
6214 my $part_bill_event = qsearchs( 'part_bill_event',
6216 'payby' => $self->payby,
6217 'plan' => 'send_agent',
6218 'plandata' => { 'op' => $regexp,
6219 'value' => "(^|\n)agentnum ".
6227 'ORDER BY seconds LIMIT 1'
6230 return '' unless $part_bill_event;
6232 if ( $part_bill_event->plandata =~ /^$option (.*)$/m ) {
6235 warn "can't parse part_bill_event eventpart#". $part_bill_event->eventpart.
6236 " plandata for $option";
6248 The delete method should possibly take an FS::cust_main object reference
6249 instead of a scalar customer number.
6251 Bill and collect options should probably be passed as references instead of a
6254 There should probably be a configuration file with a list of allowed credit
6257 No multiple currency support (probably a larger project than just this module).
6259 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
6261 Birthdates rely on negative epoch values.
6263 The payby for card/check batches is broken. With mixed batching, bad
6268 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
6269 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
6270 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.