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);
34 use FS::cust_bill_pkg;
36 use FS::cust_pay_pending;
37 use FS::cust_pay_void;
40 use FS::part_referral;
41 use FS::cust_main_county;
43 use FS::cust_main_invoice;
44 use FS::cust_credit_bill;
45 use FS::cust_bill_pay;
46 use FS::prepay_credit;
49 use FS::part_bill_event qw(due_events);
50 use FS::cust_bill_event;
51 use FS::cust_tax_exempt;
52 use FS::cust_tax_exempt_pkg;
54 use FS::payment_gateway;
55 use FS::agent_payment_gateway;
57 use FS::payinfo_Mixin;
59 @ISA = qw( FS::Record FS::payinfo_Mixin );
61 @EXPORT_OK = qw( smart_search );
63 $realtime_bop_decline_quiet = 0;
65 # 1 is mostly method/subroutine entry and options
66 # 2 traces progress of some operations
67 # 3 is even more information including possibly sensitive data
69 $me = '[FS::cust_main]';
73 $ignore_expired_card = 0;
75 @encrypted_fields = ('payinfo', 'paycvv');
76 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
78 #ask FS::UID to run this stuff for us later
79 #$FS::UID::callback{'FS::cust_main'} = sub {
80 install_callback FS::UID sub {
82 #yes, need it for stuff below (prolly should be cached)
87 my ( $hashref, $cache ) = @_;
88 if ( exists $hashref->{'pkgnum'} ) {
89 #@{ $self->{'_pkgnum'} } = ();
90 my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
91 $self->{'_pkgnum'} = $subcache;
92 #push @{ $self->{'_pkgnum'} },
93 FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
99 FS::cust_main - Object methods for cust_main records
105 $record = new FS::cust_main \%hash;
106 $record = new FS::cust_main { 'column' => 'value' };
108 $error = $record->insert;
110 $error = $new_record->replace($old_record);
112 $error = $record->delete;
114 $error = $record->check;
116 @cust_pkg = $record->all_pkgs;
118 @cust_pkg = $record->ncancelled_pkgs;
120 @cust_pkg = $record->suspended_pkgs;
122 $error = $record->bill;
123 $error = $record->bill %options;
124 $error = $record->bill 'time' => $time;
126 $error = $record->collect;
127 $error = $record->collect %options;
128 $error = $record->collect 'invoice_time' => $time,
133 An FS::cust_main object represents a customer. FS::cust_main inherits from
134 FS::Record. The following fields are currently supported:
138 =item custnum - primary key (assigned automatically for new customers)
140 =item agentnum - agent (see L<FS::agent>)
142 =item refnum - Advertising source (see L<FS::part_referral>)
148 =item ss - social security number (optional)
150 =item company - (optional)
154 =item address2 - (optional)
158 =item county - (optional, see L<FS::cust_main_county>)
160 =item state - (see L<FS::cust_main_county>)
164 =item country - (see L<FS::cust_main_county>)
166 =item daytime - phone (optional)
168 =item night - phone (optional)
170 =item fax - phone (optional)
172 =item ship_first - name
174 =item ship_last - name
176 =item ship_company - (optional)
180 =item ship_address2 - (optional)
184 =item ship_county - (optional, see L<FS::cust_main_county>)
186 =item ship_state - (see L<FS::cust_main_county>)
190 =item ship_country - (see L<FS::cust_main_county>)
192 =item ship_daytime - phone (optional)
194 =item ship_night - phone (optional)
196 =item ship_fax - phone (optional)
198 =item payby - Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
200 =item payinfo - Payment Information (See L<FS::payinfo_Mixin> for data format)
202 =item paymask - Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
206 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
208 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
210 =item paystart_month - start date month (maestro/solo cards only)
212 =item paystart_year - start date year (maestro/solo cards only)
214 =item payissue - issue number (maestro/solo cards only)
216 =item payname - name on card or billing name
218 =item payip - IP address from which payment information was received
220 =item tax - tax exempt, empty or `Y'
222 =item otaker - order taker (assigned automatically, see L<FS::UID>)
224 =item comments - comments (optional)
226 =item referral_custnum - referring customer number
228 =item spool_cdr - Enable individual CDR spooling, empty or `Y'
238 Creates a new customer. To add the customer to the database, see L<"insert">.
240 Note that this stores the hash reference, not a distinct copy of the hash it
241 points to. You can ask the object for a copy with the I<hash> method.
245 sub table { 'cust_main'; }
247 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
249 Adds this customer to the database. If there is an error, returns the error,
250 otherwise returns false.
252 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
253 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
254 are inserted atomicly, or the transaction is rolled back. Passing an empty
255 hash reference is equivalent to not supplying this parameter. There should be
256 a better explanation of this, but until then, here's an example:
259 tie %hash, 'Tie::RefHash'; #this part is important
261 $cust_pkg => [ $svc_acct ],
264 $cust_main->insert( \%hash );
266 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
267 be set as the invoicing list (see L<"invoicing_list">). Errors return as
268 expected and rollback the entire transaction; it is not necessary to call
269 check_invoicing_list first. The invoicing_list is set after the records in the
270 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
271 invoicing_list destination to the newly-created svc_acct. Here's an example:
273 $cust_main->insert( {}, [ $email, 'POST' ] );
275 Currently available options are: I<depend_jobnum> and I<noexport>.
277 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
278 on the supplied jobnum (they will not run until the specific job completes).
279 This can be used to defer provisioning until some action completes (such
280 as running the customer's credit card successfully).
282 The I<noexport> option is deprecated. If I<noexport> is set true, no
283 provisioning jobs (exports) are scheduled. (You can schedule them later with
284 the B<reexport> method.)
290 my $cust_pkgs = @_ ? shift : {};
291 my $invoicing_list = @_ ? shift : '';
293 warn "$me insert called with options ".
294 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
297 local $SIG{HUP} = 'IGNORE';
298 local $SIG{INT} = 'IGNORE';
299 local $SIG{QUIT} = 'IGNORE';
300 local $SIG{TERM} = 'IGNORE';
301 local $SIG{TSTP} = 'IGNORE';
302 local $SIG{PIPE} = 'IGNORE';
304 my $oldAutoCommit = $FS::UID::AutoCommit;
305 local $FS::UID::AutoCommit = 0;
308 my $prepay_identifier = '';
309 my( $amount, $seconds ) = ( 0, 0 );
311 if ( $self->payby eq 'PREPAY' ) {
313 $self->payby('BILL');
314 $prepay_identifier = $self->payinfo;
317 warn " looking up prepaid card $prepay_identifier\n"
320 my $error = $self->get_prepay($prepay_identifier, \$amount, \$seconds);
322 $dbh->rollback if $oldAutoCommit;
323 #return "error applying prepaid card (transaction rolled back): $error";
327 $payby = 'PREP' if $amount;
329 } elsif ( $self->payby =~ /^(CASH|WEST|MCRD)$/ ) {
332 $self->payby('BILL');
333 $amount = $self->paid;
337 warn " inserting $self\n"
340 $self->signupdate(time) unless $self->signupdate;
342 my $error = $self->SUPER::insert;
344 $dbh->rollback if $oldAutoCommit;
345 #return "inserting cust_main record (transaction rolled back): $error";
349 warn " setting invoicing list\n"
352 if ( $invoicing_list ) {
353 $error = $self->check_invoicing_list( $invoicing_list );
355 $dbh->rollback if $oldAutoCommit;
356 #return "checking invoicing_list (transaction rolled back): $error";
359 $self->invoicing_list( $invoicing_list );
362 if ( $conf->config('cust_main-skeleton_tables')
363 && $conf->config('cust_main-skeleton_custnum') ) {
365 warn " inserting skeleton records\n"
368 my $error = $self->start_copy_skel;
370 $dbh->rollback if $oldAutoCommit;
376 warn " ordering packages\n"
379 $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
381 $dbh->rollback if $oldAutoCommit;
386 $dbh->rollback if $oldAutoCommit;
387 return "No svc_acct record to apply pre-paid time";
391 warn " inserting initial $payby payment of $amount\n"
393 $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
395 $dbh->rollback if $oldAutoCommit;
396 return "inserting payment (transaction rolled back): $error";
400 unless ( $import || $skip_fuzzyfiles ) {
401 warn " queueing fuzzyfiles update\n"
403 $error = $self->queue_fuzzyfiles_update;
405 $dbh->rollback if $oldAutoCommit;
406 return "updating fuzzy search cache: $error";
410 warn " insert complete; committing transaction\n"
413 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
418 sub start_copy_skel {
421 #'mg_user_preference' => {},
422 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
423 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
424 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
425 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
426 my @tables = eval($conf->config_binary('cust_main-skeleton_tables'));
429 _copy_skel( 'cust_main', #tablename
430 $conf->config('cust_main-skeleton_custnum'), #sourceid
431 $self->custnum, #destid
432 @tables, #child tables
436 #recursive subroutine, not a method
438 my( $table, $sourceid, $destid, %child_tables ) = @_;
441 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
442 ( $table, $primary_key ) = ( $1, $2 );
444 my $dbdef_table = dbdef->table($table);
445 $primary_key = $dbdef_table->primary_key
446 or return "$table has no primary key".
447 " (or do you need to run dbdef-create?)";
450 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
451 join (', ', keys %child_tables). "\n"
454 foreach my $child_table_def ( keys %child_tables ) {
458 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
459 ( $child_table, $child_pkey ) = ( $1, $2 );
461 $child_table = $child_table_def;
463 $child_pkey = dbdef->table($child_table)->primary_key;
464 # or return "$table has no primary key".
465 # " (or do you need to run dbdef-create?)\n";
469 if ( keys %{ $child_tables{$child_table_def} } ) {
471 return "$child_table has no primary key".
472 " (run dbdef-create or try specifying it?)\n"
475 #false laziness w/Record::insert and only works on Pg
476 #refactor the proper last-inserted-id stuff out of Record::insert if this
477 # ever gets use for anything besides a quick kludge for one customer
478 my $default = dbdef->table($child_table)->column($child_pkey)->default;
479 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
480 or return "can't parse $child_table.$child_pkey default value ".
481 " for sequence name: $default";
486 my @sel_columns = grep { $_ ne $primary_key }
487 dbdef->table($child_table)->columns;
488 my $sel_columns = join(', ', @sel_columns );
490 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
491 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
492 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
494 my $sel_st = "SELECT $sel_columns FROM $child_table".
495 " WHERE $primary_key = $sourceid";
498 my $sel_sth = dbh->prepare( $sel_st )
499 or return dbh->errstr;
501 $sel_sth->execute or return $sel_sth->errstr;
503 while ( my $row = $sel_sth->fetchrow_hashref ) {
505 warn " selected row: ".
506 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
510 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
511 my $ins_sth =dbh->prepare($statement)
512 or return dbh->errstr;
513 my @param = ( $destid, map $row->{$_}, @ins_columns );
514 warn " $statement: [ ". join(', ', @param). " ]\n"
516 $ins_sth->execute( @param )
517 or return $ins_sth->errstr;
519 #next unless keys %{ $child_tables{$child_table} };
520 next unless $sequence;
522 #another section of that laziness
523 my $seq_sql = "SELECT currval('$sequence')";
524 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
525 $seq_sth->execute or return $seq_sth->errstr;
526 my $insertid = $seq_sth->fetchrow_arrayref->[0];
528 # don't drink soap! recurse! recurse! okay!
530 _copy_skel( $child_table_def,
531 $row->{$child_pkey}, #sourceid
533 %{ $child_tables{$child_table_def} },
535 return $error if $error;
545 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
547 Like the insert method on an existing record, this method orders a package
548 and included services atomicaly. Pass a Tie::RefHash data structure to this
549 method containing FS::cust_pkg and FS::svc_I<tablename> objects. There should
550 be a better explanation of this, but until then, here's an example:
553 tie %hash, 'Tie::RefHash'; #this part is important
555 $cust_pkg => [ $svc_acct ],
558 $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
560 Services can be new, in which case they are inserted, or existing unaudited
561 services, in which case they are linked to the newly-created package.
563 Currently available options are: I<depend_jobnum> and I<noexport>.
565 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
566 on the supplied jobnum (they will not run until the specific job completes).
567 This can be used to defer provisioning until some action completes (such
568 as running the customer's credit card successfully).
570 The I<noexport> option is deprecated. If I<noexport> is set true, no
571 provisioning jobs (exports) are scheduled. (You can schedule them later with
572 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
573 on the cust_main object is not recommended, as existing services will also be
580 my $cust_pkgs = shift;
583 my %svc_options = ();
584 $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
585 if exists $options{'depend_jobnum'};
586 warn "$me order_pkgs called with options ".
587 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
590 local $SIG{HUP} = 'IGNORE';
591 local $SIG{INT} = 'IGNORE';
592 local $SIG{QUIT} = 'IGNORE';
593 local $SIG{TERM} = 'IGNORE';
594 local $SIG{TSTP} = 'IGNORE';
595 local $SIG{PIPE} = 'IGNORE';
597 my $oldAutoCommit = $FS::UID::AutoCommit;
598 local $FS::UID::AutoCommit = 0;
601 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
603 foreach my $cust_pkg ( keys %$cust_pkgs ) {
604 $cust_pkg->custnum( $self->custnum );
605 my $error = $cust_pkg->insert;
607 $dbh->rollback if $oldAutoCommit;
608 return "inserting cust_pkg (transaction rolled back): $error";
610 foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
611 if ( $svc_something->svcnum ) {
612 my $old_cust_svc = $svc_something->cust_svc;
613 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
614 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
615 $error = $new_cust_svc->replace($old_cust_svc);
617 $svc_something->pkgnum( $cust_pkg->pkgnum );
618 if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
619 $svc_something->seconds( $svc_something->seconds + $$seconds );
622 $error = $svc_something->insert(%svc_options);
625 $dbh->rollback if $oldAutoCommit;
626 #return "inserting svc_ (transaction rolled back): $error";
632 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
636 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
638 Recharges this (existing) customer with the specified prepaid card (see
639 L<FS::prepay_credit>), specified either by I<identifier> or as an
640 FS::prepay_credit object. If there is an error, returns the error, otherwise
643 Optionally, four scalar references can be passed as well. They will have their
644 values filled in with the amount, number of seconds, and number of upload and
645 download bytes applied by this prepaid
650 sub recharge_prepay {
651 my( $self, $prepay_credit, $amountref, $secondsref,
652 $upbytesref, $downbytesref, $totalbytesref ) = @_;
654 local $SIG{HUP} = 'IGNORE';
655 local $SIG{INT} = 'IGNORE';
656 local $SIG{QUIT} = 'IGNORE';
657 local $SIG{TERM} = 'IGNORE';
658 local $SIG{TSTP} = 'IGNORE';
659 local $SIG{PIPE} = 'IGNORE';
661 my $oldAutoCommit = $FS::UID::AutoCommit;
662 local $FS::UID::AutoCommit = 0;
665 my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
667 my $error = $self->get_prepay($prepay_credit, \$amount,
668 \$seconds, \$upbytes, \$downbytes, \$totalbytes)
669 || $self->increment_seconds($seconds)
670 || $self->increment_upbytes($upbytes)
671 || $self->increment_downbytes($downbytes)
672 || $self->increment_totalbytes($totalbytes)
673 || $self->insert_cust_pay_prepay( $amount,
675 ? $prepay_credit->identifier
680 $dbh->rollback if $oldAutoCommit;
684 if ( defined($amountref) ) { $$amountref = $amount; }
685 if ( defined($secondsref) ) { $$secondsref = $seconds; }
686 if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
687 if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
688 if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
690 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
695 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ , AMOUNTREF, SECONDSREF
697 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
698 specified either by I<identifier> or as an FS::prepay_credit object.
700 References to I<amount> and I<seconds> scalars should be passed as arguments
701 and will be incremented by the values of the prepaid card.
703 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
704 check or set this customer's I<agentnum>.
706 If there is an error, returns the error, otherwise returns false.
712 my( $self, $prepay_credit, $amountref, $secondsref,
713 $upref, $downref, $totalref) = @_;
715 local $SIG{HUP} = 'IGNORE';
716 local $SIG{INT} = 'IGNORE';
717 local $SIG{QUIT} = 'IGNORE';
718 local $SIG{TERM} = 'IGNORE';
719 local $SIG{TSTP} = 'IGNORE';
720 local $SIG{PIPE} = 'IGNORE';
722 my $oldAutoCommit = $FS::UID::AutoCommit;
723 local $FS::UID::AutoCommit = 0;
726 unless ( ref($prepay_credit) ) {
728 my $identifier = $prepay_credit;
730 $prepay_credit = qsearchs(
732 { 'identifier' => $prepay_credit },
737 unless ( $prepay_credit ) {
738 $dbh->rollback if $oldAutoCommit;
739 return "Invalid prepaid card: ". $identifier;
744 if ( $prepay_credit->agentnum ) {
745 if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
746 $dbh->rollback if $oldAutoCommit;
747 return "prepaid card not valid for agent ". $self->agentnum;
749 $self->agentnum($prepay_credit->agentnum);
752 my $error = $prepay_credit->delete;
754 $dbh->rollback if $oldAutoCommit;
755 return "removing prepay_credit (transaction rolled back): $error";
758 $$amountref += $prepay_credit->amount;
759 $$secondsref += $prepay_credit->seconds;
760 $$upref += $prepay_credit->upbytes;
761 $$downref += $prepay_credit->downbytes;
762 $$totalref += $prepay_credit->totalbytes;
764 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
769 =item increment_upbytes SECONDS
771 Updates this customer's single or primary account (see L<FS::svc_acct>) by
772 the specified number of upbytes. If there is an error, returns the error,
773 otherwise returns false.
777 sub increment_upbytes {
778 _increment_column( shift, 'upbytes', @_);
781 =item increment_downbytes SECONDS
783 Updates this customer's single or primary account (see L<FS::svc_acct>) by
784 the specified number of downbytes. If there is an error, returns the error,
785 otherwise returns false.
789 sub increment_downbytes {
790 _increment_column( shift, 'downbytes', @_);
793 =item increment_totalbytes SECONDS
795 Updates this customer's single or primary account (see L<FS::svc_acct>) by
796 the specified number of totalbytes. If there is an error, returns the error,
797 otherwise returns false.
801 sub increment_totalbytes {
802 _increment_column( shift, 'totalbytes', @_);
805 =item increment_seconds SECONDS
807 Updates this customer's single or primary account (see L<FS::svc_acct>) by
808 the specified number of seconds. If there is an error, returns the error,
809 otherwise returns false.
813 sub increment_seconds {
814 _increment_column( shift, 'seconds', @_);
817 =item _increment_column AMOUNT
819 Updates this customer's single or primary account (see L<FS::svc_acct>) by
820 the specified number of seconds or bytes. If there is an error, returns
821 the error, otherwise returns false.
825 sub _increment_column {
826 my( $self, $column, $amount ) = @_;
827 warn "$me increment_column called: $column, $amount\n"
830 return '' unless $amount;
832 my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
833 $self->ncancelled_pkgs;
836 return 'No packages with primary or single services found'.
837 ' to apply pre-paid time';
838 } elsif ( scalar(@cust_pkg) > 1 ) {
839 #maybe have a way to specify the package/account?
840 return 'Multiple packages found to apply pre-paid time';
843 my $cust_pkg = $cust_pkg[0];
844 warn " found package pkgnum ". $cust_pkg->pkgnum. "\n"
848 $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
851 return 'No account found to apply pre-paid time';
852 } elsif ( scalar(@cust_svc) > 1 ) {
853 return 'Multiple accounts found to apply pre-paid time';
856 my $svc_acct = $cust_svc[0]->svc_x;
857 warn " found service svcnum ". $svc_acct->pkgnum.
858 ' ('. $svc_acct->email. ")\n"
861 $column = "increment_$column";
862 $svc_acct->$column($amount);
866 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
868 Inserts a prepayment in the specified amount for this customer. An optional
869 second argument can specify the prepayment identifier for tracking purposes.
870 If there is an error, returns the error, otherwise returns false.
874 sub insert_cust_pay_prepay {
875 shift->insert_cust_pay('PREP', @_);
878 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
880 Inserts a cash payment in the specified amount for this customer. An optional
881 second argument can specify the payment identifier for tracking purposes.
882 If there is an error, returns the error, otherwise returns false.
886 sub insert_cust_pay_cash {
887 shift->insert_cust_pay('CASH', @_);
890 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
892 Inserts a Western Union payment in the specified amount for this customer. An
893 optional second argument can specify the prepayment identifier for tracking
894 purposes. If there is an error, returns the error, otherwise returns false.
898 sub insert_cust_pay_west {
899 shift->insert_cust_pay('WEST', @_);
902 sub insert_cust_pay {
903 my( $self, $payby, $amount ) = splice(@_, 0, 3);
904 my $payinfo = scalar(@_) ? shift : '';
906 my $cust_pay = new FS::cust_pay {
907 'custnum' => $self->custnum,
908 'paid' => sprintf('%.2f', $amount),
909 #'_date' => #date the prepaid card was purchased???
911 'payinfo' => $payinfo,
919 This method is deprecated. See the I<depend_jobnum> option to the insert and
920 order_pkgs methods for a better way to defer provisioning.
922 Re-schedules all exports by calling the B<reexport> method of all associated
923 packages (see L<FS::cust_pkg>). If there is an error, returns the error;
924 otherwise returns false.
931 carp "WARNING: FS::cust_main::reexport is deprectated; ".
932 "use the depend_jobnum option to insert or order_pkgs to delay export";
934 local $SIG{HUP} = 'IGNORE';
935 local $SIG{INT} = 'IGNORE';
936 local $SIG{QUIT} = 'IGNORE';
937 local $SIG{TERM} = 'IGNORE';
938 local $SIG{TSTP} = 'IGNORE';
939 local $SIG{PIPE} = 'IGNORE';
941 my $oldAutoCommit = $FS::UID::AutoCommit;
942 local $FS::UID::AutoCommit = 0;
945 foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
946 my $error = $cust_pkg->reexport;
948 $dbh->rollback if $oldAutoCommit;
953 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
958 =item delete NEW_CUSTNUM
960 This deletes the customer. If there is an error, returns the error, otherwise
963 This will completely remove all traces of the customer record. This is not
964 what you want when a customer cancels service; for that, cancel all of the
965 customer's packages (see L</cancel>).
967 If the customer has any uncancelled packages, you need to pass a new (valid)
968 customer number for those packages to be transferred to. Cancelled packages
969 will be deleted. Did I mention that this is NOT what you want when a customer
970 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
972 You can't delete a customer with invoices (see L<FS::cust_bill>),
973 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
974 refunds (see L<FS::cust_refund>).
981 local $SIG{HUP} = 'IGNORE';
982 local $SIG{INT} = 'IGNORE';
983 local $SIG{QUIT} = 'IGNORE';
984 local $SIG{TERM} = 'IGNORE';
985 local $SIG{TSTP} = 'IGNORE';
986 local $SIG{PIPE} = 'IGNORE';
988 my $oldAutoCommit = $FS::UID::AutoCommit;
989 local $FS::UID::AutoCommit = 0;
992 if ( $self->cust_bill ) {
993 $dbh->rollback if $oldAutoCommit;
994 return "Can't delete a customer with invoices";
996 if ( $self->cust_credit ) {
997 $dbh->rollback if $oldAutoCommit;
998 return "Can't delete a customer with credits";
1000 if ( $self->cust_pay ) {
1001 $dbh->rollback if $oldAutoCommit;
1002 return "Can't delete a customer with payments";
1004 if ( $self->cust_refund ) {
1005 $dbh->rollback if $oldAutoCommit;
1006 return "Can't delete a customer with refunds";
1009 my @cust_pkg = $self->ncancelled_pkgs;
1011 my $new_custnum = shift;
1012 unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1013 $dbh->rollback if $oldAutoCommit;
1014 return "Invalid new customer number: $new_custnum";
1016 foreach my $cust_pkg ( @cust_pkg ) {
1017 my %hash = $cust_pkg->hash;
1018 $hash{'custnum'} = $new_custnum;
1019 my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1020 my $error = $new_cust_pkg->replace($cust_pkg,
1021 options => { $cust_pkg->options },
1024 $dbh->rollback if $oldAutoCommit;
1029 my @cancelled_cust_pkg = $self->all_pkgs;
1030 foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1031 my $error = $cust_pkg->delete;
1033 $dbh->rollback if $oldAutoCommit;
1038 foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
1039 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
1041 my $error = $cust_main_invoice->delete;
1043 $dbh->rollback if $oldAutoCommit;
1048 my $error = $self->SUPER::delete;
1050 $dbh->rollback if $oldAutoCommit;
1054 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1059 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
1061 Replaces the OLD_RECORD with this one in the database. If there is an error,
1062 returns the error, otherwise returns false.
1064 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1065 be set as the invoicing list (see L<"invoicing_list">). Errors return as
1066 expected and rollback the entire transaction; it is not necessary to call
1067 check_invoicing_list first. Here's an example:
1069 $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1077 warn "$me replace called\n"
1080 local $SIG{HUP} = 'IGNORE';
1081 local $SIG{INT} = 'IGNORE';
1082 local $SIG{QUIT} = 'IGNORE';
1083 local $SIG{TERM} = 'IGNORE';
1084 local $SIG{TSTP} = 'IGNORE';
1085 local $SIG{PIPE} = 'IGNORE';
1087 # We absolutely have to have an old vs. new record to make this work.
1088 if (!defined($old)) {
1089 $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
1092 my $curuser = $FS::CurrentUser::CurrentUser;
1093 if ( $self->payby eq 'COMP'
1094 && $self->payby ne $old->payby
1095 && ! $curuser->access_right('Complimentary customer')
1098 return "You are not permitted to create complimentary accounts.";
1101 local($ignore_expired_card) = 1
1102 if $old->payby =~ /^(CARD|DCRD)$/
1103 && $self->payby =~ /^(CARD|DCRD)$/
1104 && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1106 my $oldAutoCommit = $FS::UID::AutoCommit;
1107 local $FS::UID::AutoCommit = 0;
1110 my $error = $self->SUPER::replace($old);
1113 $dbh->rollback if $oldAutoCommit;
1117 if ( @param ) { # INVOICING_LIST_ARYREF
1118 my $invoicing_list = shift @param;
1119 $error = $self->check_invoicing_list( $invoicing_list );
1121 $dbh->rollback if $oldAutoCommit;
1124 $self->invoicing_list( $invoicing_list );
1127 if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
1128 grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
1129 # card/check/lec info has changed, want to retry realtime_ invoice events
1130 my $error = $self->retry_realtime;
1132 $dbh->rollback if $oldAutoCommit;
1137 unless ( $import || $skip_fuzzyfiles ) {
1138 $error = $self->queue_fuzzyfiles_update;
1140 $dbh->rollback if $oldAutoCommit;
1141 return "updating fuzzy search cache: $error";
1145 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1150 =item queue_fuzzyfiles_update
1152 Used by insert & replace to update the fuzzy search cache
1156 sub queue_fuzzyfiles_update {
1159 local $SIG{HUP} = 'IGNORE';
1160 local $SIG{INT} = 'IGNORE';
1161 local $SIG{QUIT} = 'IGNORE';
1162 local $SIG{TERM} = 'IGNORE';
1163 local $SIG{TSTP} = 'IGNORE';
1164 local $SIG{PIPE} = 'IGNORE';
1166 my $oldAutoCommit = $FS::UID::AutoCommit;
1167 local $FS::UID::AutoCommit = 0;
1170 my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1171 my $error = $queue->insert( map $self->getfield($_),
1172 qw(first last company)
1175 $dbh->rollback if $oldAutoCommit;
1176 return "queueing job (transaction rolled back): $error";
1179 if ( $self->ship_last ) {
1180 $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
1181 $error = $queue->insert( map $self->getfield("ship_$_"),
1182 qw(first last company)
1185 $dbh->rollback if $oldAutoCommit;
1186 return "queueing job (transaction rolled back): $error";
1190 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1197 Checks all fields to make sure this is a valid customer record. If there is
1198 an error, returns the error, otherwise returns false. Called by the insert
1199 and replace methods.
1206 warn "$me check BEFORE: \n". $self->_dump
1210 $self->ut_numbern('custnum')
1211 || $self->ut_number('agentnum')
1212 || $self->ut_textn('agent_custid')
1213 || $self->ut_number('refnum')
1214 || $self->ut_textn('custbatch')
1215 || $self->ut_name('last')
1216 || $self->ut_name('first')
1217 || $self->ut_snumbern('birthdate')
1218 || $self->ut_snumbern('signupdate')
1219 || $self->ut_textn('company')
1220 || $self->ut_text('address1')
1221 || $self->ut_textn('address2')
1222 || $self->ut_text('city')
1223 || $self->ut_textn('county')
1224 || $self->ut_textn('state')
1225 || $self->ut_country('country')
1226 || $self->ut_anything('comments')
1227 || $self->ut_numbern('referral_custnum')
1228 || $self->ut_textn('stateid')
1229 || $self->ut_textn('stateid_state')
1231 #barf. need message catalogs. i18n. etc.
1232 $error .= "Please select an advertising source."
1233 if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1234 return $error if $error;
1236 return "Unknown agent"
1237 unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1239 return "Unknown refnum"
1240 unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1242 return "Unknown referring custnum: ". $self->referral_custnum
1243 unless ! $self->referral_custnum
1244 || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1246 if ( $self->ss eq '' ) {
1251 $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1252 or return "Illegal social security number: ". $self->ss;
1253 $self->ss("$1-$2-$3");
1257 # bad idea to disable, causes billing to fail because of no tax rates later
1258 # unless ( $import ) {
1259 unless ( qsearch('cust_main_county', {
1260 'country' => $self->country,
1263 return "Unknown state/county/country: ".
1264 $self->state. "/". $self->county. "/". $self->country
1265 unless qsearch('cust_main_county',{
1266 'state' => $self->state,
1267 'county' => $self->county,
1268 'country' => $self->country,
1274 $self->ut_phonen('daytime', $self->country)
1275 || $self->ut_phonen('night', $self->country)
1276 || $self->ut_phonen('fax', $self->country)
1277 || $self->ut_zip('zip', $self->country)
1279 return $error if $error;
1281 if ( $conf->exists('cust_main-require_phone')
1282 && ! length($self->daytime) && ! length($self->night)
1285 my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1287 : FS::Msgcat::_gettext('daytime');
1288 my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1290 : FS::Msgcat::_gettext('night');
1292 return "$daytime_label or $night_label is required"
1296 if ( $self->has_ship_address
1297 && scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
1298 $self->addr_fields )
1302 $self->ut_name('ship_last')
1303 || $self->ut_name('ship_first')
1304 || $self->ut_textn('ship_company')
1305 || $self->ut_text('ship_address1')
1306 || $self->ut_textn('ship_address2')
1307 || $self->ut_text('ship_city')
1308 || $self->ut_textn('ship_county')
1309 || $self->ut_textn('ship_state')
1310 || $self->ut_country('ship_country')
1312 return $error if $error;
1314 #false laziness with above
1315 unless ( qsearchs('cust_main_county', {
1316 'country' => $self->ship_country,
1319 return "Unknown ship_state/ship_county/ship_country: ".
1320 $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
1321 unless qsearch('cust_main_county',{
1322 'state' => $self->ship_state,
1323 'county' => $self->ship_county,
1324 'country' => $self->ship_country,
1330 $self->ut_phonen('ship_daytime', $self->ship_country)
1331 || $self->ut_phonen('ship_night', $self->ship_country)
1332 || $self->ut_phonen('ship_fax', $self->ship_country)
1333 || $self->ut_zip('ship_zip', $self->ship_country)
1335 return $error if $error;
1337 return "Unit # is required."
1338 if $self->ship_address2 =~ /^\s*$/
1339 && $conf->exists('cust_main-require_address2');
1341 } else { # ship_ info eq billing info, so don't store dup info in database
1343 $self->setfield("ship_$_", '')
1344 foreach $self->addr_fields;
1346 return "Unit # is required."
1347 if $self->address2 =~ /^\s*$/
1348 && $conf->exists('cust_main-require_address2');
1352 #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1353 # or return "Illegal payby: ". $self->payby;
1355 FS::payby->can_payby($self->table, $self->payby)
1356 or return "Illegal payby: ". $self->payby;
1358 $error = $self->ut_numbern('paystart_month')
1359 || $self->ut_numbern('paystart_year')
1360 || $self->ut_numbern('payissue')
1361 || $self->ut_textn('paytype')
1363 return $error if $error;
1365 if ( $self->payip eq '' ) {
1368 $error = $self->ut_ip('payip');
1369 return $error if $error;
1372 # If it is encrypted and the private key is not availaible then we can't
1373 # check the credit card.
1375 my $check_payinfo = 1;
1377 if ($self->is_encrypted($self->payinfo)) {
1381 if ( $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1383 my $payinfo = $self->payinfo;
1384 $payinfo =~ s/\D//g;
1385 $payinfo =~ /^(\d{13,16})$/
1386 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1388 $self->payinfo($payinfo);
1390 or return gettext('invalid_card'); # . ": ". $self->payinfo;
1392 return gettext('unknown_card_type')
1393 if cardtype($self->payinfo) eq "Unknown";
1395 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1397 return 'Banned credit card: banned on '.
1398 time2str('%a %h %o at %r', $ban->_date).
1399 ' by '. $ban->otaker.
1400 ' (ban# '. $ban->bannum. ')';
1403 if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
1404 if ( cardtype($self->payinfo) eq 'American Express card' ) {
1405 $self->paycvv =~ /^(\d{4})$/
1406 or return "CVV2 (CID) for American Express cards is four digits.";
1409 $self->paycvv =~ /^(\d{3})$/
1410 or return "CVV2 (CVC2/CID) is three digits.";
1417 my $cardtype = cardtype($payinfo);
1418 if ( $cardtype =~ /^(Switch|Solo)$/i ) {
1420 return "Start date or issue number is required for $cardtype cards"
1421 unless $self->paystart_month && $self->paystart_year or $self->payissue;
1423 return "Start month must be between 1 and 12"
1424 if $self->paystart_month
1425 and $self->paystart_month < 1 || $self->paystart_month > 12;
1427 return "Start year must be 1990 or later"
1428 if $self->paystart_year
1429 and $self->paystart_year < 1990;
1431 return "Issue number must be beween 1 and 99"
1433 and $self->payissue < 1 || $self->payissue > 99;
1436 $self->paystart_month('');
1437 $self->paystart_year('');
1438 $self->payissue('');
1441 } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
1443 my $payinfo = $self->payinfo;
1444 $payinfo =~ s/[^\d\@]//g;
1445 if ( $conf->exists('echeck-nonus') ) {
1446 $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@aba';
1447 $payinfo = "$1\@$2";
1449 $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
1450 $payinfo = "$1\@$2";
1452 $self->payinfo($payinfo);
1455 my $ban = qsearchs('banned_pay', $self->_banned_pay_hashref);
1457 return 'Banned ACH account: banned on '.
1458 time2str('%a %h %o at %r', $ban->_date).
1459 ' by '. $ban->otaker.
1460 ' (ban# '. $ban->bannum. ')';
1463 } elsif ( $self->payby eq 'LECB' ) {
1465 my $payinfo = $self->payinfo;
1466 $payinfo =~ s/\D//g;
1467 $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
1469 $self->payinfo($payinfo);
1472 } elsif ( $self->payby eq 'BILL' ) {
1474 $error = $self->ut_textn('payinfo');
1475 return "Illegal P.O. number: ". $self->payinfo if $error;
1478 } elsif ( $self->payby eq 'COMP' ) {
1480 my $curuser = $FS::CurrentUser::CurrentUser;
1481 if ( ! $self->custnum
1482 && ! $curuser->access_right('Complimentary customer')
1485 return "You are not permitted to create complimentary accounts."
1488 $error = $self->ut_textn('payinfo');
1489 return "Illegal comp account issuer: ". $self->payinfo if $error;
1492 } elsif ( $self->payby eq 'PREPAY' ) {
1494 my $payinfo = $self->payinfo;
1495 $payinfo =~ s/\W//g; #anything else would just confuse things
1496 $self->payinfo($payinfo);
1497 $error = $self->ut_alpha('payinfo');
1498 return "Illegal prepayment identifier: ". $self->payinfo if $error;
1499 return "Unknown prepayment identifier"
1500 unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
1505 if ( $self->paydate eq '' || $self->paydate eq '-' ) {
1506 return "Expiration date required"
1507 unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD)$/;
1511 if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1512 ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1513 } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1514 ( $m, $y ) = ( $3, "20$2" );
1516 return "Illegal expiration date: ". $self->paydate;
1518 $self->paydate("$y-$m-01");
1519 my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
1520 return gettext('expired_card')
1522 && !$ignore_expired_card
1523 && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
1526 if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
1527 ( ! $conf->exists('require_cardname')
1528 || $self->payby !~ /^(CARD|DCRD)$/ )
1530 $self->payname( $self->first. " ". $self->getfield('last') );
1532 $self->payname =~ /^([\w \,\.\-\'\&]+)$/
1533 or return gettext('illegal_name'). " payname: ". $self->payname;
1537 foreach my $flag (qw( tax spool_cdr )) {
1538 $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
1542 $self->otaker(getotaker) unless $self->otaker;
1544 warn "$me check AFTER: \n". $self->_dump
1547 $self->SUPER::check;
1552 Returns a list of fields which have ship_ duplicates.
1557 qw( last first company
1558 address1 address2 city county state zip country
1563 =item has_ship_address
1565 Returns true if this customer record has a separate shipping address.
1569 sub has_ship_address {
1571 scalar( grep { $self->getfield("ship_$_") ne '' } $self->addr_fields );
1576 Returns all packages (see L<FS::cust_pkg>) for this customer.
1583 return $self->num_pkgs unless wantarray;
1586 if ( $self->{'_pkgnum'} ) {
1587 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
1589 @cust_pkg = qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
1592 sort sort_packages @cust_pkg;
1595 =item ncancelled_pkgs
1597 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
1601 sub ncancelled_pkgs {
1604 return $self->num_ncancelled_pkgs unless wantarray;
1607 if ( $self->{'_pkgnum'} ) {
1609 @cust_pkg = grep { ! $_->getfield('cancel') }
1610 values %{ $self->{'_pkgnum'}->cache };
1615 qsearch( 'cust_pkg', {
1616 'custnum' => $self->custnum,
1620 qsearch( 'cust_pkg', {
1621 'custnum' => $self->custnum,
1626 sort sort_packages @cust_pkg;
1630 # This should be generalized to use config options to determine order.
1632 if ( $a->get('cancel') and $b->get('cancel') ) {
1633 $a->pkgnum <=> $b->pkgnum;
1634 } elsif ( $a->get('cancel') or $b->get('cancel') ) {
1635 return -1 if $b->get('cancel');
1636 return 1 if $a->get('cancel');
1639 $a->pkgnum <=> $b->pkgnum;
1643 =item suspended_pkgs
1645 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
1649 sub suspended_pkgs {
1651 grep { $_->susp } $self->ncancelled_pkgs;
1654 =item unflagged_suspended_pkgs
1656 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1657 customer (thouse packages without the `manual_flag' set).
1661 sub unflagged_suspended_pkgs {
1663 return $self->suspended_pkgs
1664 unless dbdef->table('cust_pkg')->column('manual_flag');
1665 grep { ! $_->manual_flag } $self->suspended_pkgs;
1668 =item unsuspended_pkgs
1670 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1675 sub unsuspended_pkgs {
1677 grep { ! $_->susp } $self->ncancelled_pkgs;
1680 =item num_cancelled_pkgs
1682 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1687 sub num_cancelled_pkgs {
1688 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
1691 sub num_ncancelled_pkgs {
1692 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
1696 my( $self ) = shift;
1697 my $sql = scalar(@_) ? shift : '';
1698 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
1699 my $sth = dbh->prepare(
1700 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
1701 ) or die dbh->errstr;
1702 $sth->execute($self->custnum) or die $sth->errstr;
1703 $sth->fetchrow_arrayref->[0];
1708 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1709 and L<FS::cust_pkg>) for this customer. Always returns a list: an empty list
1710 on success or a list of errors.
1716 grep { $_->unsuspend } $self->suspended_pkgs;
1721 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1723 Returns a list: an empty list on success or a list of errors.
1729 grep { $_->suspend(@_) } $self->unsuspended_pkgs;
1732 =item suspend_if_pkgpart PKGPART [ , PKGPART ... ]
1734 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1735 PKGPARTs (see L<FS::part_pkg>).
1737 Returns a list: an empty list on success or a list of errors.
1741 sub suspend_if_pkgpart {
1743 my (@pkgparts, %opt);
1744 if (ref($_[0]) eq 'HASH'){
1745 @pkgparts = @{$_[0]{pkgparts}};
1750 grep { $_->suspend(%opt) }
1751 grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1752 $self->unsuspended_pkgs;
1755 =item suspend_unless_pkgpart PKGPART [ , PKGPART ... ]
1757 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1758 listed PKGPARTs (see L<FS::part_pkg>).
1760 Returns a list: an empty list on success or a list of errors.
1764 sub suspend_unless_pkgpart {
1766 my (@pkgparts, %opt);
1767 if (ref($_[0]) eq 'HASH'){
1768 @pkgparts = @{$_[0]{pkgparts}};
1773 grep { $_->suspend(%opt) }
1774 grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1775 $self->unsuspended_pkgs;
1778 =item cancel [ OPTION => VALUE ... ]
1780 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1782 Available options are: I<quiet>, I<reasonnum>, and I<ban>
1784 I<quiet> can be set true to supress email cancellation notices.
1786 # I<reasonnum> can be set to a cancellation reason (see L<FS::cancel_reason>)
1788 I<ban> can be set true to ban this customer's credit card or ACH information,
1791 Always returns a list: an empty list on success or a list of errors.
1799 if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
1801 #should try decryption (we might have the private key)
1802 # and if not maybe queue a job for the server that does?
1803 return ( "Can't (yet) ban encrypted credit cards" )
1804 if $self->is_encrypted($self->payinfo);
1806 my $ban = new FS::banned_pay $self->_banned_pay_hashref;
1807 my $error = $ban->insert;
1808 return ( $error ) if $error;
1812 grep { $_ } map { $_->cancel(@_) } $self->ncancelled_pkgs;
1815 sub _banned_pay_hashref {
1826 'payby' => $payby2ban{$self->payby},
1827 'payinfo' => md5_base64($self->payinfo),
1828 #don't ever *search* on reason! #'reason' =>
1834 Returns all notes (see L<FS::cust_main_note>) for this customer.
1841 qsearch( 'cust_main_note',
1842 { 'custnum' => $self->custnum },
1844 'ORDER BY _DATE DESC'
1850 Returns the agent (see L<FS::agent>) for this customer.
1856 qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1861 Generates invoices (see L<FS::cust_bill>) for this customer. Usually used in
1862 conjunction with the collect method.
1864 If there is an error, returns the error, otherwise returns false.
1866 Options are passed as name-value pairs. Currently available options are:
1870 =item resetup - if set true, re-charges setup fees.
1872 =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:
1876 $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1878 =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.
1885 my( $self, %options ) = @_;
1886 return '' if $self->payby eq 'COMP';
1887 warn "$me bill customer ". $self->custnum. "\n"
1890 my $time = $options{'time'} || time;
1895 local $SIG{HUP} = 'IGNORE';
1896 local $SIG{INT} = 'IGNORE';
1897 local $SIG{QUIT} = 'IGNORE';
1898 local $SIG{TERM} = 'IGNORE';
1899 local $SIG{TSTP} = 'IGNORE';
1900 local $SIG{PIPE} = 'IGNORE';
1902 my $oldAutoCommit = $FS::UID::AutoCommit;
1903 local $FS::UID::AutoCommit = 0;
1906 $self->select_for_update; #mutex
1908 #create a new invoice
1909 #(we'll remove it later if it doesn't actually need to be generated [contains
1910 # no line items] and we're inside a transaciton so nothing else will see it)
1911 my $cust_bill = new FS::cust_bill ( {
1912 'custnum' => $self->custnum,
1913 '_date' => ( $options{'invoice_time'} || $time ),
1914 #'charged' => $charged,
1917 $error = $cust_bill->insert;
1919 $dbh->rollback if $oldAutoCommit;
1920 return "can't create invoice for customer #". $self->custnum. ": $error";
1922 my $invnum = $cust_bill->invnum;
1925 # find the packages which are due for billing, find out how much they are
1926 # & generate invoice database.
1929 my( $total_setup, $total_recur, $postal_charge ) = ( 0, 0, 0 );
1931 my @precommit_hooks = ();
1933 my @cust_pkgs = qsearch('cust_pkg', { 'custnum' => $self->custnum } );
1934 foreach my $cust_pkg (@cust_pkgs) {
1936 #NO!! next if $cust_pkg->cancel;
1937 next if $cust_pkg->getfield('cancel');
1939 warn " bill package ". $cust_pkg->pkgnum. "\n" if $DEBUG > 1;
1941 #? to avoid use of uninitialized value errors... ?
1942 $cust_pkg->setfield('bill', '')
1943 unless defined($cust_pkg->bill);
1945 my $part_pkg = $cust_pkg->part_pkg;
1947 my %hash = $cust_pkg->hash;
1948 my $old_cust_pkg = new FS::cust_pkg \%hash;
1958 if ( ! $cust_pkg->setup &&
1960 ( $conf->exists('disable_setup_suspended_pkgs') &&
1961 ! $cust_pkg->getfield('susp')
1962 ) || ! $conf->exists('disable_setup_suspended_pkgs')
1964 || $options{'resetup'}
1967 warn " bill setup\n" if $DEBUG > 1;
1969 $setup = eval { $cust_pkg->calc_setup( $time, \@details ) };
1971 $dbh->rollback if $oldAutoCommit;
1972 return "$@ running calc_setup for $cust_pkg\n";
1975 $unitsetup = $cust_pkg->part_pkg->unit_setup || $setup; #XXX uuh
1977 $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
1981 # bill recurring fee
1984 #XXX unit stuff here too
1988 if ( $part_pkg->getfield('freq') ne '0' &&
1989 ! $cust_pkg->getfield('susp') &&
1990 ( $cust_pkg->getfield('bill') || 0 ) <= $time
1993 # XXX should this be a package event? probably. events are called
1994 # at collection time at the moment, though...
1995 if ( $part_pkg->can('reset_usage') ) {
1996 warn " resetting usage counters" if $DEBUG > 1;
1997 $part_pkg->reset_usage($cust_pkg);
2000 warn " bill recur\n" if $DEBUG > 1;
2002 # XXX shared with $recur_prog
2003 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2005 #over two params! lets at least switch to a hashref for the rest...
2006 my %param = ( 'precommit_hooks' => \@precommit_hooks, );
2008 $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details, \%param ) };
2010 $dbh->rollback if $oldAutoCommit;
2011 return "$@ running calc_recur for $cust_pkg\n";
2014 #change this bit to use Date::Manip? CAREFUL with timezones (see
2015 # mailing list archive)
2016 my ($sec,$min,$hour,$mday,$mon,$year) =
2017 (localtime($sdate) )[0,1,2,3,4,5];
2019 #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
2020 # only for figuring next bill date, nothing else, so, reset $sdate again
2022 $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
2023 $cust_pkg->last_bill($sdate);
2025 if ( $part_pkg->freq =~ /^\d+$/ ) {
2026 $mon += $part_pkg->freq;
2027 until ( $mon < 12 ) { $mon -= 12; $year++; }
2028 } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
2030 $mday += $weeks * 7;
2031 } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
2034 } elsif ( $part_pkg->freq =~ /^(\d+)h$/ ) {
2038 $dbh->rollback if $oldAutoCommit;
2039 return "unparsable frequency: ". $part_pkg->freq;
2041 $cust_pkg->setfield('bill',
2042 timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
2045 warn "\$setup is undefined" unless defined($setup);
2046 warn "\$recur is undefined" unless defined($recur);
2047 warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
2050 # If $cust_pkg has been modified, update it and create cust_bill_pkg records
2053 if ( $cust_pkg->modified ) { # hmmm.. and if the options are modified?
2055 warn " package ". $cust_pkg->pkgnum. " modified; updating\n"
2058 $error=$cust_pkg->replace($old_cust_pkg,
2059 options => { $cust_pkg->options },
2061 if ( $error ) { #just in case
2062 $dbh->rollback if $oldAutoCommit;
2063 return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
2066 $setup = sprintf( "%.2f", $setup );
2067 $recur = sprintf( "%.2f", $recur );
2068 if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
2069 $dbh->rollback if $oldAutoCommit;
2070 return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
2072 if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
2073 $dbh->rollback if $oldAutoCommit;
2074 return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
2077 if ( $setup != 0 || $recur != 0 ) {
2079 unless ($postal_charge) {
2080 $postal_charge = 1; # try only once
2081 my $postal_pkg = $self->charge_postal_fee();
2082 if ( $postal_pkg && !ref( $postal_pkg ) ) {
2083 $dbh->rollback if $oldAutoCommit;
2084 return "can't charge postal invoice fee for customer ".
2085 $self->custnum. ": $postal_pkg";
2087 push @cust_pkgs, $postal_pkg if $postal_pkg;
2090 warn " charges (setup=$setup, recur=$recur); adding line items\n"
2092 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2093 'invnum' => $invnum,
2094 'pkgnum' => $cust_pkg->pkgnum,
2096 'unitsetup' => $unitsetup,
2098 'unitrecur' => $unitrecur,
2099 'quantity' => $cust_pkg->quantity,
2101 'edate' => $cust_pkg->bill,
2102 'details' => \@details,
2104 $error = $cust_bill_pkg->insert;
2106 $dbh->rollback if $oldAutoCommit;
2107 return "can't create invoice line item for invoice #$invnum: $error";
2109 $total_setup += $setup;
2110 $total_recur += $recur;
2116 unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
2119 ( $conf->exists('tax-ship_address') && length($self->ship_last) )
2122 my %taxhash = map { $_ => $self->get("$prefix$_") }
2123 qw( state county country );
2125 $taxhash{'taxclass'} = $part_pkg->taxclass;
2127 my @taxes = qsearch( 'cust_main_county', \%taxhash );
2130 $taxhash{'taxclass'} = '';
2131 @taxes = qsearch( 'cust_main_county', \%taxhash );
2134 #one more try at a whole-country tax rate
2136 $taxhash{$_} = '' foreach qw( state county );
2137 @taxes = qsearch( 'cust_main_county', \%taxhash );
2140 # maybe eliminate this entirely, along with all the 0% records
2142 $dbh->rollback if $oldAutoCommit;
2144 "fatal: can't find tax rate for state/county/country/taxclass ".
2145 join('/', ( map $self->get("$prefix$_"),
2146 qw(state county country)
2148 $part_pkg->taxclass ). "\n";
2151 foreach my $tax ( @taxes ) {
2153 my $taxable_charged = 0;
2154 $taxable_charged += $setup
2155 unless $part_pkg->setuptax =~ /^Y$/i
2156 || $tax->setuptax =~ /^Y$/i;
2157 $taxable_charged += $recur
2158 unless $part_pkg->recurtax =~ /^Y$/i
2159 || $tax->recurtax =~ /^Y$/i;
2160 next unless $taxable_charged;
2162 if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
2163 #my ($mon,$year) = (localtime($sdate) )[4,5];
2164 my ($mon,$year) = (localtime( $sdate || $cust_bill->_date ) )[4,5];
2166 my $freq = $part_pkg->freq || 1;
2167 if ( $freq !~ /(\d+)$/ ) {
2168 $dbh->rollback if $oldAutoCommit;
2169 return "daily/weekly package definitions not (yet?)".
2170 " compatible with monthly tax exemptions";
2172 my $taxable_per_month =
2173 sprintf("%.2f", $taxable_charged / $freq );
2175 #call the whole thing off if this customer has any old
2176 #exemption records...
2177 my @cust_tax_exempt =
2178 qsearch( 'cust_tax_exempt' => { custnum=> $self->custnum } );
2179 if ( @cust_tax_exempt ) {
2180 $dbh->rollback if $oldAutoCommit;
2182 'this customer still has old-style tax exemption records; '.
2183 'run bin/fs-migrate-cust_tax_exempt?';
2186 foreach my $which_month ( 1 .. $freq ) {
2188 #maintain the new exemption table now
2191 FROM cust_tax_exempt_pkg
2192 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
2193 LEFT JOIN cust_bill USING ( invnum )
2199 my $sth = dbh->prepare($sql) or do {
2200 $dbh->rollback if $oldAutoCommit;
2201 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2209 $dbh->rollback if $oldAutoCommit;
2210 return "fatal: can't lookup exising exemption: ". dbh->errstr;
2212 my $existing_exemption = $sth->fetchrow_arrayref->[0] || 0;
2214 my $remaining_exemption =
2215 $tax->exempt_amount - $existing_exemption;
2216 if ( $remaining_exemption > 0 ) {
2217 my $addl = $remaining_exemption > $taxable_per_month
2218 ? $taxable_per_month
2219 : $remaining_exemption;
2220 $taxable_charged -= $addl;
2222 my $cust_tax_exempt_pkg = new FS::cust_tax_exempt_pkg ( {
2223 'billpkgnum' => $cust_bill_pkg->billpkgnum,
2224 'taxnum' => $tax->taxnum,
2225 'year' => 1900+$year,
2227 'amount' => sprintf("%.2f", $addl ),
2229 $error = $cust_tax_exempt_pkg->insert;
2231 $dbh->rollback if $oldAutoCommit;
2232 return "fatal: can't insert cust_tax_exempt_pkg: $error";
2234 } # if $remaining_exemption > 0
2238 #until ( $mon < 12 ) { $mon -= 12; $year++; }
2239 until ( $mon < 13 ) { $mon -= 12; $year++; }
2241 } #foreach $which_month
2243 } #if $tax->exempt_amount
2245 $taxable_charged = sprintf( "%.2f", $taxable_charged);
2247 #$tax += $taxable_charged * $cust_main_county->tax / 100
2248 $tax{ $tax->taxname || 'Tax' } +=
2249 $taxable_charged * $tax->tax / 100
2251 } #foreach my $tax ( @taxes )
2253 } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
2255 } #if $setup != 0 || $recur != 0
2257 } #if $cust_pkg->modified
2259 } #foreach my $cust_pkg
2261 unless ( $cust_bill->cust_bill_pkg ) {
2262 $cust_bill->delete; #don't create an invoice w/o line items
2263 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2267 my $charged = sprintf( "%.2f", $total_setup + $total_recur );
2269 foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
2270 my $tax = sprintf("%.2f", $tax{$taxname} );
2271 $charged = sprintf( "%.2f", $charged+$tax );
2273 my $cust_bill_pkg = new FS::cust_bill_pkg ({
2274 'invnum' => $invnum,
2280 'itemdesc' => $taxname,
2282 $error = $cust_bill_pkg->insert;
2284 $dbh->rollback if $oldAutoCommit;
2285 return "can't create invoice line item for invoice #$invnum: $error";
2287 $total_setup += $tax;
2291 $cust_bill->charged( sprintf( "%.2f", $total_setup + $total_recur ) );
2292 $error = $cust_bill->replace;
2294 $dbh->rollback if $oldAutoCommit;
2295 return "can't update charged for invoice #$invnum: $error";
2298 foreach my $hook ( @precommit_hooks ) {
2300 &{$hook}; #($self) ?
2303 $dbh->rollback if $oldAutoCommit;
2304 return "$@ running precommit hook $hook\n";
2308 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2312 =item collect OPTIONS
2314 (Attempt to) collect money for this customer's outstanding invoices (see
2315 L<FS::cust_bill>). Usually used after the bill method.
2317 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
2318 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
2319 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
2321 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
2322 and the invoice events web interface.
2324 If there is an error, returns the error, otherwise returns false.
2326 Options are passed as name-value pairs.
2328 Currently available options are:
2330 invoice_time - Use this time when deciding when to print invoices and
2331 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>
2332 for conversion functions.
2334 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
2337 quiet - set true to surpress email card/ACH decline notices.
2339 freq - "1d" for the traditional, daily events (the default), or "1m" for the
2342 payby - allows for one time override of normal customer billing method
2347 my( $self, %options ) = @_;
2348 my $invoice_time = $options{'invoice_time'} || time;
2351 local $SIG{HUP} = 'IGNORE';
2352 local $SIG{INT} = 'IGNORE';
2353 local $SIG{QUIT} = 'IGNORE';
2354 local $SIG{TERM} = 'IGNORE';
2355 local $SIG{TSTP} = 'IGNORE';
2356 local $SIG{PIPE} = 'IGNORE';
2358 my $oldAutoCommit = $FS::UID::AutoCommit;
2359 local $FS::UID::AutoCommit = 0;
2362 $self->select_for_update; #mutex
2364 my $balance = $self->balance;
2365 warn "$me collect customer ". $self->custnum. ": balance $balance\n"
2367 unless ( $balance > 0 ) { #redundant?????
2368 $dbh->rollback if $oldAutoCommit; #hmm
2372 if ( exists($options{'retry_card'}) ) {
2373 carp 'retry_card option passed to collect is deprecated; use retry';
2374 $options{'retry'} ||= $options{'retry_card'};
2376 if ( exists($options{'retry'}) && $options{'retry'} ) {
2377 my $error = $self->retry_realtime;
2379 $dbh->rollback if $oldAutoCommit;
2385 if ( defined $options{'freq'} && $options{'freq'} eq '1m' ) {
2386 $extra_sql = " AND freq = '1m' ";
2388 $extra_sql = " AND ( freq = '1d' OR freq IS NULL OR freq = '' ) ";
2391 foreach my $cust_bill ( $self->open_cust_bill ) {
2393 # don't try to charge for the same invoice if it's already in a batch
2394 #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
2396 last if $self->balance <= 0;
2398 warn " invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")\n"
2401 foreach my $part_bill_event ( due_events ( $cust_bill,
2402 exists($options{'payby'})
2408 last if $cust_bill->owed <= 0 # don't run subsequent events if owed<=0
2409 || $self->balance <= 0; # or if balance<=0
2412 local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
2413 warn " do_event " . $cust_bill . " ". (%options) . "\n"
2416 if (my $error = $part_bill_event->do_event($cust_bill, %options)) {
2417 # gah, even with transactions.
2418 $dbh->commit if $oldAutoCommit; #well.
2427 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2432 =item retry_realtime
2434 Schedules realtime / batch credit card / electronic check / LEC billing
2435 events for for retry. Useful if card information has changed or manual
2436 retry is desired. The 'collect' method must be called to actually retry
2439 Implementation details: For each of this customer's open invoices, changes
2440 the status of the first "done" (with statustext error) realtime processing
2445 sub retry_realtime {
2448 local $SIG{HUP} = 'IGNORE';
2449 local $SIG{INT} = 'IGNORE';
2450 local $SIG{QUIT} = 'IGNORE';
2451 local $SIG{TERM} = 'IGNORE';
2452 local $SIG{TSTP} = 'IGNORE';
2453 local $SIG{PIPE} = 'IGNORE';
2455 my $oldAutoCommit = $FS::UID::AutoCommit;
2456 local $FS::UID::AutoCommit = 0;
2459 foreach my $cust_bill (
2460 grep { $_->cust_bill_event }
2461 $self->open_cust_bill
2463 my @cust_bill_event =
2464 sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
2466 #$_->part_bill_event->plan eq 'realtime-card'
2467 $_->part_bill_event->eventcode =~
2468 /\$cust_bill\->(batch|realtime)_(card|ach|lec)/
2469 && $_->status eq 'done'
2472 $cust_bill->cust_bill_event;
2473 next unless @cust_bill_event;
2474 my $error = $cust_bill_event[0]->retry;
2476 $dbh->rollback if $oldAutoCommit;
2477 return "error scheduling invoice event for retry: $error";
2482 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2487 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
2489 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
2490 via a Business::OnlinePayment realtime gateway. See
2491 L<http://420.am/business-onlinepayment> for supported gateways.
2493 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2495 Available options are: I<description>, I<invnum>, I<quiet>, I<paynum_ref>, I<payunique>
2497 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2498 I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
2499 if set, will override the value from the customer record.
2501 I<description> is a free-text field passed to the gateway. It defaults to
2502 "Internet services".
2504 If an I<invnum> is specified, this payment (if successful) is applied to the
2505 specified invoice. If you don't specify an I<invnum> you might want to
2506 call the B<apply_payments> method.
2508 I<quiet> can be set true to surpress email decline notices.
2510 I<paynum_ref> can be set to a scalar reference. It will be filled in with the
2511 resulting paynum, if any.
2513 I<payunique> is a unique identifier for this payment.
2515 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
2522 my( $self, $method, $amount, %options ) = @_;
2524 warn "$me realtime_bop: $method $amount\n";
2525 warn " $_ => $options{$_}\n" foreach keys %options;
2528 $options{'description'} ||= 'Internet services';
2530 eval "use Business::OnlinePayment";
2533 my $payinfo = exists($options{'payinfo'})
2534 ? $options{'payinfo'}
2537 my %method2payby = (
2548 if ( $options{'invnum'} ) {
2549 my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{'invnum'} } );
2550 die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
2552 map { $_->part_pkg->taxclass }
2554 map { $_->cust_pkg }
2555 $cust_bill->cust_bill_pkg;
2556 unless ( grep { $taxclasses[0] ne $_ } @taxclasses ) { #unless there are
2557 #different taxclasses
2558 $taxclass = $taxclasses[0];
2562 #look for an agent gateway override first
2564 if ( $method eq 'CC' ) {
2565 $cardtype = cardtype($payinfo);
2566 } elsif ( $method eq 'ECHECK' ) {
2569 $cardtype = $method;
2573 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2574 cardtype => $cardtype,
2575 taxclass => $taxclass, } )
2576 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2578 taxclass => $taxclass, } )
2579 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2580 cardtype => $cardtype,
2582 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
2584 taxclass => '', } );
2586 my $payment_gateway = '';
2587 my( $processor, $login, $password, $action, @bop_options );
2588 if ( $override ) { #use a payment gateway override
2590 $payment_gateway = $override->payment_gateway;
2592 $processor = $payment_gateway->gateway_module;
2593 $login = $payment_gateway->gateway_username;
2594 $password = $payment_gateway->gateway_password;
2595 $action = $payment_gateway->gateway_action;
2596 @bop_options = $payment_gateway->options;
2598 } else { #use the standard settings from the config
2600 ( $processor, $login, $password, $action, @bop_options ) =
2601 $self->default_payment_gateway($method);
2609 my $address = exists($options{'address1'})
2610 ? $options{'address1'}
2612 my $address2 = exists($options{'address2'})
2613 ? $options{'address2'}
2615 $address .= ", ". $address2 if length($address2);
2617 my $o_payname = exists($options{'payname'})
2618 ? $options{'payname'}
2620 my($payname, $payfirst, $paylast);
2621 if ( $o_payname && $method ne 'ECHECK' ) {
2622 ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2623 or return "Illegal payname $payname";
2624 ($payfirst, $paylast) = ($1, $2);
2626 $payfirst = $self->getfield('first');
2627 $paylast = $self->getfield('last');
2628 $payname = "$payfirst $paylast";
2631 my @invoicing_list = $self->invoicing_list_emailonly;
2632 if ( $conf->exists('emailinvoiceautoalways')
2633 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
2634 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
2635 push @invoicing_list, $self->all_emails;
2638 my $email = ($conf->exists('business-onlinepayment-email-override'))
2639 ? $conf->config('business-onlinepayment-email-override')
2640 : $invoicing_list[0];
2644 my $payip = exists($options{'payip'})
2647 $content{customer_ip} = $payip
2650 $content{invoice_number} = $options{'invnum'}
2651 if exists($options{'invnum'}) && length($options{'invnum'});
2653 $content{email_customer} =
2654 ( $conf->exists('business-onlinepayment-email_customer')
2655 || $conf->exists('business-onlinepayment-email-override') );
2658 if ( $method eq 'CC' ) {
2660 $content{card_number} = $payinfo;
2661 $paydate = exists($options{'paydate'})
2662 ? $options{'paydate'}
2664 $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2665 $content{expiration} = "$2/$1";
2667 my $paycvv = exists($options{'paycvv'})
2668 ? $options{'paycvv'}
2670 $content{cvv2} = $paycvv
2673 my $paystart_month = exists($options{'paystart_month'})
2674 ? $options{'paystart_month'}
2675 : $self->paystart_month;
2677 my $paystart_year = exists($options{'paystart_year'})
2678 ? $options{'paystart_year'}
2679 : $self->paystart_year;
2681 $content{card_start} = "$paystart_month/$paystart_year"
2682 if $paystart_month && $paystart_year;
2684 my $payissue = exists($options{'payissue'})
2685 ? $options{'payissue'}
2687 $content{issue_number} = $payissue if $payissue;
2689 $content{recurring_billing} = 'YES'
2690 if qsearch('cust_pay', { 'custnum' => $self->custnum,
2692 'payinfo' => $payinfo,
2694 || qsearch('cust_pay', { 'custnum' => $self->custnum,
2696 'paymask' => $self->mask_payinfo('CARD', $payinfo),
2700 } elsif ( $method eq 'ECHECK' ) {
2701 ( $content{account_number}, $content{routing_code} ) =
2702 split('@', $payinfo);
2703 $content{bank_name} = $o_payname;
2704 $content{bank_state} = exists($options{'paystate'})
2705 ? $options{'paystate'}
2706 : $self->getfield('paystate');
2707 $content{account_type} = exists($options{'paytype'})
2708 ? uc($options{'paytype'}) || 'CHECKING'
2709 : uc($self->getfield('paytype')) || 'CHECKING';
2710 $content{account_name} = $payname;
2711 $content{customer_org} = $self->company ? 'B' : 'I';
2712 $content{state_id} = exists($options{'stateid'})
2713 ? $options{'stateid'}
2714 : $self->getfield('stateid');
2715 $content{state_id_state} = exists($options{'stateid_state'})
2716 ? $options{'stateid_state'}
2717 : $self->getfield('stateid_state');
2718 $content{customer_ssn} = exists($options{'ss'})
2721 } elsif ( $method eq 'LEC' ) {
2722 $content{phone} = $payinfo;
2726 # run transaction(s)
2729 my $balance = exists( $options{'balance'} )
2730 ? $options{'balance'}
2733 $self->select_for_update; #mutex ... just until we get our pending record in
2735 #the checks here are intended to catch concurrent payments
2736 #double-form-submission prevention is taken care of in cust_pay_pending::check
2739 return "The customer's balance has changed; $method transaction aborted."
2740 if $self->balance < $balance;
2741 #&& $self->balance < $amount; #might as well anyway?
2743 #also check and make sure there aren't *other* pending payments for this cust
2745 my @pending = qsearch('cust_pay_pending', {
2746 'custnum' => $self->custnum,
2747 'status' => { op=>'!=', value=>'done' }
2749 return "A payment is already being processed for this customer (".
2750 join(', ', map 'paypendingnum '. $_->paypendingnum, @pending ).
2751 "); $method transaction aborted."
2752 if scalar(@pending);
2754 #okay, good to go, if we're a duplicate, cust_pay_pending will kick us out
2756 my $cust_pay_pending = new FS::cust_pay_pending {
2757 'custnum' => $self->custnum,
2758 #'invnum' => $options{'invnum'},
2761 'payby' => $method2payby{$method},
2762 'payinfo' => $payinfo,
2763 'paydate' => $paydate,
2765 'gatewaynum' => ( $payment_gateway ? $payment_gateway->gatewaynum : '' ),
2767 $cust_pay_pending->payunique( $options{payunique} )
2768 if defined($options{payunique}) && length($options{payunique});
2769 my $cpp_new_err = $cust_pay_pending->insert; #mutex lost when this is inserted
2770 return $cpp_new_err if $cpp_new_err;
2772 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
2774 my $transaction = new Business::OnlinePayment( $processor, @bop_options );
2775 $transaction->content(
2778 'password' => $password,
2779 'action' => $action1,
2780 'description' => $options{'description'},
2781 'amount' => $amount,
2782 #'invoice_number' => $options{'invnum'},
2783 'customer_id' => $self->custnum,
2784 'last_name' => $paylast,
2785 'first_name' => $payfirst,
2787 'address' => $address,
2788 'city' => ( exists($options{'city'})
2791 'state' => ( exists($options{'state'})
2794 'zip' => ( exists($options{'zip'})
2797 'country' => ( exists($options{'country'})
2798 ? $options{'country'}
2800 'referer' => 'http://cleanwhisker.420.am/',
2802 'phone' => $self->daytime || $self->night,
2806 $cust_pay_pending->status('pending');
2807 my $cpp_pending_err = $cust_pay_pending->replace;
2808 return $cpp_pending_err if $cpp_pending_err;
2810 $transaction->submit();
2812 if ( $transaction->is_success() && $action2 ) {
2814 $cust_pay_pending->status('authorized');
2815 my $cpp_authorized_err = $cust_pay_pending->replace;
2816 return $cpp_authorized_err if $cpp_authorized_err;
2818 my $auth = $transaction->authorization;
2819 my $ordernum = $transaction->can('order_number')
2820 ? $transaction->order_number
2824 new Business::OnlinePayment( $processor, @bop_options );
2831 password => $password,
2832 order_number => $ordernum,
2834 authorization => $auth,
2835 description => $options{'description'},
2838 foreach my $field (qw( authorization_source_code returned_ACI
2839 transaction_identifier validation_code
2840 transaction_sequence_num local_transaction_date
2841 local_transaction_time AVS_result_code )) {
2842 $capture{$field} = $transaction->$field() if $transaction->can($field);
2845 $capture->content( %capture );
2849 unless ( $capture->is_success ) {
2850 my $e = "Authorization successful but capture failed, custnum #".
2851 $self->custnum. ': '. $capture->result_code.
2852 ": ". $capture->error_message;
2859 $cust_pay_pending->status($transaction->is_success() ? 'captured' : 'declined');
2860 my $cpp_captured_err = $cust_pay_pending->replace;
2861 return $cpp_captured_err if $cpp_captured_err;
2864 # remove paycvv after initial transaction
2867 #false laziness w/misc/process/payment.cgi - check both to make sure working
2869 if ( defined $self->dbdef_table->column('paycvv')
2870 && length($self->paycvv)
2871 && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
2873 my $error = $self->remove_cvv;
2875 warn "WARNING: error removing cvv: $error\n";
2883 if ( $transaction->is_success() ) {
2886 if ( $payment_gateway ) { # agent override
2887 $paybatch = $payment_gateway->gatewaynum. '-';
2890 $paybatch .= "$processor:". $transaction->authorization;
2892 $paybatch .= ':'. $transaction->order_number
2893 if $transaction->can('order_number')
2894 && length($transaction->order_number);
2896 my $cust_pay = new FS::cust_pay ( {
2897 'custnum' => $self->custnum,
2898 'invnum' => $options{'invnum'},
2901 'payby' => $method2payby{$method},
2902 'payinfo' => $payinfo,
2903 'paybatch' => $paybatch,
2904 'paydate' => $paydate,
2906 #doesn't hurt to know, even though the dup check is in cust_pay_pending now
2907 $cust_pay->payunique( $options{payunique} )
2908 if defined($options{payunique}) && length($options{payunique});
2910 my $oldAutoCommit = $FS::UID::AutoCommit;
2911 local $FS::UID::AutoCommit = 0;
2914 #start a transaction, insert the cust_pay and set cust_pay_pending.status to done in a single transction
2916 my $error = $cust_pay->insert($options{'manual'} ? ( 'manual' => 1 ) : () );
2919 $cust_pay->invnum(''); #try again with no specific invnum
2920 my $error2 = $cust_pay->insert( $options{'manual'} ?
2921 ( 'manual' => 1 ) : ()
2924 # gah. but at least we have a record of the state we had to abort in
2925 # from cust_pay_pending now.
2926 my $e = "WARNING: $method captured but payment not recorded - ".
2927 "error inserting payment ($processor): $error2".
2928 " (previously tried insert with invnum #$options{'invnum'}" .
2929 ": $error ) - pending payment saved as paypendingnum ".
2930 $cust_pay_pending->paypendingnum. "\n";
2936 if ( $options{'paynum_ref'} ) {
2937 ${ $options{'paynum_ref'} } = $cust_pay->paynum;
2940 $cust_pay_pending->status('done');
2941 $cust_pay_pending->statustext('captured');
2942 my $cpp_done_err = $cust_pay_pending->replace;
2944 if ( $cpp_done_err ) {
2946 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
2947 my $e = "WARNING: $method captured but payment not recorded - ".
2948 "error updating status for paypendingnum ".
2949 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
2955 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2956 return ''; #no error
2962 my $perror = "$processor error: ". $transaction->error_message;
2964 unless ( $transaction->error_message ) {
2967 #this should be normalized :/
2969 # bad, ad-hoc B:OP:PayflowPro "transaction_response" BS
2970 if ( $transaction->can('param')
2971 && $transaction->param('transaction_response') ) {
2972 $t_response = $transaction->param('transaction_response')
2974 # slightly better, ad-hoc B:OP:TransactionCentral without "param"
2975 } elsif ( $transaction->can('response_page') ) {
2977 'page' => ( $transaction->can('response_page')
2978 ? $transaction->response_page
2981 'code' => ( $transaction->can('response_code')
2982 ? $transaction->response_code
2985 'headers' => ( $transaction->can('response_headers')
2986 ? $transaction->response_headers
2992 "No additional debugging information available for $processor";
2995 $perror .= "No error_message returned from $processor -- ".
2996 ( ref($t_response) ? Dumper($t_response) : $t_response );
3000 if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
3001 && $conf->exists('emaildecline')
3002 && grep { $_ ne 'POST' } $self->invoicing_list
3003 && ! grep { $transaction->error_message =~ /$_/ }
3004 $conf->config('emaildecline-exclude')
3006 my @templ = $conf->config('declinetemplate');
3007 my $template = new Text::Template (
3009 SOURCE => [ map "$_\n", @templ ],
3010 ) or return "($perror) can't create template: $Text::Template::ERROR";
3011 $template->compile()
3012 or return "($perror) can't compile template: $Text::Template::ERROR";
3014 my $templ_hash = { error => $transaction->error_message };
3016 my $error = send_email(
3017 'from' => $conf->config('invoice_from'),
3018 'to' => [ grep { $_ ne 'POST' } $self->invoicing_list ],
3019 'subject' => 'Your payment could not be processed',
3020 'body' => [ $template->fill_in(HASH => $templ_hash) ],
3023 $perror .= " (also received error sending decline notification: $error)"
3028 $cust_pay_pending->status('done');
3029 $cust_pay_pending->statustext("declined: $perror");
3030 my $cpp_done_err = $cust_pay_pending->replace;
3031 if ( $cpp_done_err ) {
3032 my $e = "WARNING: $method declined but pending payment not resolved - ".
3033 "error updating status for paypendingnum ".
3034 $cust_pay_pending->paypendingnum. ": $cpp_done_err \n";
3036 $perror = "$e ($perror)";
3044 =item default_payment_gateway
3048 sub default_payment_gateway {
3049 my( $self, $method ) = @_;
3051 die "Real-time processing not enabled\n"
3052 unless $conf->exists('business-onlinepayment');
3055 my $bop_config = 'business-onlinepayment';
3056 $bop_config .= '-ach'
3057 if $method =~ /^(ECHECK|CHEK)$/ && $conf->exists($bop_config. '-ach');
3058 my ( $processor, $login, $password, $action, @bop_options ) =
3059 $conf->config($bop_config);
3060 $action ||= 'normal authorization';
3061 pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
3062 die "No real-time processor is enabled - ".
3063 "did you set the business-onlinepayment configuration value?\n"
3066 ( $processor, $login, $password, $action, @bop_options )
3071 Removes the I<paycvv> field from the database directly.
3073 If there is an error, returns the error, otherwise returns false.
3079 my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
3080 or return dbh->errstr;
3081 $sth->execute($self->custnum)
3082 or return $sth->errstr;
3087 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
3089 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
3090 via a Business::OnlinePayment realtime gateway. See
3091 L<http://420.am/business-onlinepayment> for supported gateways.
3093 Available methods are: I<CC>, I<ECHECK> and I<LEC>
3095 Available options are: I<amount>, I<reason>, I<paynum>, I<paydate>
3097 Most gateways require a reference to an original payment transaction to refund,
3098 so you probably need to specify a I<paynum>.
3100 I<amount> defaults to the original amount of the payment if not specified.
3102 I<reason> specifies a reason for the refund.
3104 I<paydate> specifies the expiration date for a credit card overriding the
3105 value from the customer record or the payment record. Specified as yyyy-mm-dd
3107 Implementation note: If I<amount> is unspecified or equal to the amount of the
3108 orignal payment, first an attempt is made to "void" the transaction via
3109 the gateway (to cancel a not-yet settled transaction) and then if that fails,
3110 the normal attempt is made to "refund" ("credit") the transaction via the
3111 gateway is attempted.
3113 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
3114 #I<zip>, I<payinfo> and I<paydate> are also available. Any of these options,
3115 #if set, will override the value from the customer record.
3117 #If an I<invnum> is specified, this payment (if successful) is applied to the
3118 #specified invoice. If you don't specify an I<invnum> you might want to
3119 #call the B<apply_payments> method.
3123 #some false laziness w/realtime_bop, not enough to make it worth merging
3124 #but some useful small subs should be pulled out
3125 sub realtime_refund_bop {
3126 my( $self, $method, %options ) = @_;
3128 warn "$me realtime_refund_bop: $method refund\n";
3129 warn " $_ => $options{$_}\n" foreach keys %options;
3132 eval "use Business::OnlinePayment";
3136 # look up the original payment and optionally a gateway for that payment
3140 my $amount = $options{'amount'};
3142 my( $processor, $login, $password, @bop_options ) ;
3143 my( $auth, $order_number ) = ( '', '', '' );
3145 if ( $options{'paynum'} ) {
3147 warn " paynum: $options{paynum}\n" if $DEBUG > 1;
3148 $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
3149 or return "Unknown paynum $options{'paynum'}";
3150 $amount ||= $cust_pay->paid;
3152 $cust_pay->paybatch =~ /^((\d+)\-)?(\w+):\s*([\w\-\/ ]*)(:([\w\-]+))?$/
3153 or return "Can't parse paybatch for paynum $options{'paynum'}: ".
3154 $cust_pay->paybatch;
3155 my $gatewaynum = '';
3156 ( $gatewaynum, $processor, $auth, $order_number ) = ( $2, $3, $4, $6 );
3158 if ( $gatewaynum ) { #gateway for the payment to be refunded
3160 my $payment_gateway =
3161 qsearchs('payment_gateway', { 'gatewaynum' => $gatewaynum } );
3162 die "payment gateway $gatewaynum not found"
3163 unless $payment_gateway;
3165 $processor = $payment_gateway->gateway_module;
3166 $login = $payment_gateway->gateway_username;
3167 $password = $payment_gateway->gateway_password;
3168 @bop_options = $payment_gateway->options;
3170 } else { #try the default gateway
3172 my( $conf_processor, $unused_action );
3173 ( $conf_processor, $login, $password, $unused_action, @bop_options ) =
3174 $self->default_payment_gateway($method);
3176 return "processor of payment $options{'paynum'} $processor does not".
3177 " match default processor $conf_processor"
3178 unless $processor eq $conf_processor;
3183 } else { # didn't specify a paynum, so look for agent gateway overrides
3184 # like a normal transaction
3187 if ( $method eq 'CC' ) {
3188 $cardtype = cardtype($self->payinfo);
3189 } elsif ( $method eq 'ECHECK' ) {
3192 $cardtype = $method;
3195 qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3196 cardtype => $cardtype,
3198 || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
3200 taxclass => '', } );
3202 if ( $override ) { #use a payment gateway override
3204 my $payment_gateway = $override->payment_gateway;
3206 $processor = $payment_gateway->gateway_module;
3207 $login = $payment_gateway->gateway_username;
3208 $password = $payment_gateway->gateway_password;
3209 #$action = $payment_gateway->gateway_action;
3210 @bop_options = $payment_gateway->options;
3212 } else { #use the standard settings from the config
3215 ( $processor, $login, $password, $unused_action, @bop_options ) =
3216 $self->default_payment_gateway($method);
3221 return "neither amount nor paynum specified" unless $amount;
3226 'password' => $password,
3227 'order_number' => $order_number,
3228 'amount' => $amount,
3229 'referer' => 'http://cleanwhisker.420.am/',
3231 $content{authorization} = $auth
3232 if length($auth); #echeck/ACH transactions have an order # but no auth
3233 #(at least with authorize.net)
3235 my $disable_void_after;
3236 if ($conf->exists('disable_void_after')
3237 && $conf->config('disable_void_after') =~ /^(\d+)$/) {
3238 $disable_void_after = $1;
3241 #first try void if applicable
3242 if ( $cust_pay && $cust_pay->paid == $amount
3244 ( not defined($disable_void_after) )
3245 || ( time < ($cust_pay->_date + $disable_void_after ) )
3248 warn " attempting void\n" if $DEBUG > 1;
3249 my $void = new Business::OnlinePayment( $processor, @bop_options );
3250 $void->content( 'action' => 'void', %content );
3252 if ( $void->is_success ) {
3253 my $error = $cust_pay->void($options{'reason'});
3255 # gah, even with transactions.
3256 my $e = 'WARNING: Card/ACH voided but database not updated - '.
3257 "error voiding payment: $error";
3261 warn " void successful\n" if $DEBUG > 1;
3266 warn " void unsuccessful, trying refund\n"
3270 my $address = $self->address1;
3271 $address .= ", ". $self->address2 if $self->address2;
3273 my($payname, $payfirst, $paylast);
3274 if ( $self->payname && $method ne 'ECHECK' ) {
3275 $payname = $self->payname;
3276 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
3277 or return "Illegal payname $payname";
3278 ($payfirst, $paylast) = ($1, $2);
3280 $payfirst = $self->getfield('first');
3281 $paylast = $self->getfield('last');
3282 $payname = "$payfirst $paylast";
3285 my @invoicing_list = $self->invoicing_list_emailonly;
3286 if ( $conf->exists('emailinvoiceautoalways')
3287 || $conf->exists('emailinvoiceauto') && ! @invoicing_list
3288 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
3289 push @invoicing_list, $self->all_emails;
3292 my $email = ($conf->exists('business-onlinepayment-email-override'))
3293 ? $conf->config('business-onlinepayment-email-override')
3294 : $invoicing_list[0];
3296 my $payip = exists($options{'payip'})
3299 $content{customer_ip} = $payip
3303 if ( $method eq 'CC' ) {
3306 $content{card_number} = $payinfo = $cust_pay->payinfo;
3307 (exists($options{'paydate'}) ? $options{'paydate'} : $cust_pay->paydate)
3308 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/ &&
3309 ($content{expiration} = "$2/$1"); # where available
3311 $content{card_number} = $payinfo = $self->payinfo;
3312 (exists($options{'paydate'}) ? $options{'paydate'} : $self->paydate)
3313 =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
3314 $content{expiration} = "$2/$1";
3317 } elsif ( $method eq 'ECHECK' ) {
3320 $payinfo = $cust_pay->payinfo;
3322 $payinfo = $self->payinfo;
3324 ( $content{account_number}, $content{routing_code} )= split('@', $payinfo );
3325 $content{bank_name} = $self->payname;
3326 $content{account_type} = 'CHECKING';
3327 $content{account_name} = $payname;
3328 $content{customer_org} = $self->company ? 'B' : 'I';
3329 $content{customer_ssn} = $self->ss;
3330 } elsif ( $method eq 'LEC' ) {
3331 $content{phone} = $payinfo = $self->payinfo;
3335 my $refund = new Business::OnlinePayment( $processor, @bop_options );
3336 my %sub_content = $refund->content(
3337 'action' => 'credit',
3338 'customer_id' => $self->custnum,
3339 'last_name' => $paylast,
3340 'first_name' => $payfirst,
3342 'address' => $address,
3343 'city' => $self->city,
3344 'state' => $self->state,
3345 'zip' => $self->zip,
3346 'country' => $self->country,
3348 'phone' => $self->daytime || $self->night,
3351 warn join('', map { " $_ => $sub_content{$_}\n" } keys %sub_content )
3355 return "$processor error: ". $refund->error_message
3356 unless $refund->is_success();
3358 my %method2payby = (
3364 my $paybatch = "$processor:". $refund->authorization;
3365 $paybatch .= ':'. $refund->order_number
3366 if $refund->can('order_number') && $refund->order_number;
3368 while ( $cust_pay && $cust_pay->unapplied < $amount ) {
3369 my @cust_bill_pay = $cust_pay->cust_bill_pay;
3370 last unless @cust_bill_pay;
3371 my $cust_bill_pay = pop @cust_bill_pay;
3372 my $error = $cust_bill_pay->delete;
3376 my $cust_refund = new FS::cust_refund ( {
3377 'custnum' => $self->custnum,
3378 'paynum' => $options{'paynum'},
3379 'refund' => $amount,
3381 'payby' => $method2payby{$method},
3382 'payinfo' => $payinfo,
3383 'paybatch' => $paybatch,
3384 'reason' => $options{'reason'} || 'card or ACH refund',
3386 my $error = $cust_refund->insert;
3388 $cust_refund->paynum(''); #try again with no specific paynum
3389 my $error2 = $cust_refund->insert;
3391 # gah, even with transactions.
3392 my $e = 'WARNING: Card/ACH refunded but database not updated - '.
3393 "error inserting refund ($processor): $error2".
3394 " (previously tried insert with paynum #$options{'paynum'}" .
3405 =item batch_card OPTION => VALUE...
3407 Adds a payment for this invoice to the pending credit card batch (see
3408 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
3409 runs the payment using a realtime gateway.
3414 my ($self, %options) = @_;
3417 if (exists($options{amount})) {
3418 $amount = $options{amount};
3420 $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
3422 return '' unless $amount > 0;
3424 my $invnum = delete $options{invnum};
3425 my $payby = $options{invnum} || $self->payby; #dubious
3427 if ($options{'realtime'}) {
3428 return $self->realtime_bop( FS::payby->payby2bop($self->payby),
3434 my $oldAutoCommit = $FS::UID::AutoCommit;
3435 local $FS::UID::AutoCommit = 0;
3438 #this needs to handle mysql as well as Pg, like svc_acct.pm
3439 #(make it into a common function if folks need to do batching with mysql)
3440 $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
3441 or return "Cannot lock pay_batch: " . $dbh->errstr;
3445 'payby' => FS::payby->payby2payment($payby),
3448 my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
3450 unless ( $pay_batch ) {
3451 $pay_batch = new FS::pay_batch \%pay_batch;
3452 my $error = $pay_batch->insert;
3454 $dbh->rollback if $oldAutoCommit;
3455 die "error creating new batch: $error\n";
3459 my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
3460 'batchnum' => $pay_batch->batchnum,
3461 'custnum' => $self->custnum,
3464 foreach (qw( address1 address2 city state zip country payby payinfo paydate
3466 $options{$_} = '' unless exists($options{$_});
3469 my $cust_pay_batch = new FS::cust_pay_batch ( {
3470 'batchnum' => $pay_batch->batchnum,
3471 'invnum' => $invnum || 0, # is there a better value?
3472 # this field should be
3474 # cust_bill_pay_batch now
3475 'custnum' => $self->custnum,
3476 'last' => $self->getfield('last'),
3477 'first' => $self->getfield('first'),
3478 'address1' => $options{address1} || $self->address1,
3479 'address2' => $options{address2} || $self->address2,
3480 'city' => $options{city} || $self->city,
3481 'state' => $options{state} || $self->state,
3482 'zip' => $options{zip} || $self->zip,
3483 'country' => $options{country} || $self->country,
3484 'payby' => $options{payby} || $self->payby,
3485 'payinfo' => $options{payinfo} || $self->payinfo,
3486 'exp' => $options{paydate} || $self->paydate,
3487 'payname' => $options{payname} || $self->payname,
3488 'amount' => $amount, # consolidating
3491 $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
3492 if $old_cust_pay_batch;
3495 if ($old_cust_pay_batch) {
3496 $error = $cust_pay_batch->replace($old_cust_pay_batch)
3498 $error = $cust_pay_batch->insert;
3502 $dbh->rollback if $oldAutoCommit;
3506 my $unapplied = $self->total_credited + $self->total_unapplied_payments + $self->in_transit_payments;
3507 foreach my $cust_bill ($self->open_cust_bill) {
3508 #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
3509 my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
3510 'invnum' => $cust_bill->invnum,
3511 'paybatchnum' => $cust_pay_batch->paybatchnum,
3512 'amount' => $cust_bill->owed,
3515 if ($unapplied >= $cust_bill_pay_batch->amount){
3516 $unapplied -= $cust_bill_pay_batch->amount;
3519 $cust_bill_pay_batch->amount(sprintf ( "%.2f",
3520 $cust_bill_pay_batch->amount - $unapplied )); $unapplied = 0;
3522 $error = $cust_bill_pay_batch->insert;
3524 $dbh->rollback if $oldAutoCommit;
3529 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3535 Returns the total owed for this customer on all invoices
3536 (see L<FS::cust_bill/owed>).
3542 $self->total_owed_date(2145859200); #12/31/2037
3545 =item total_owed_date TIME
3547 Returns the total owed for this customer on all invoices with date earlier than
3548 TIME. TIME is specified as a UNIX timestamp; see L<perlfunc/"time">). Also
3549 see L<Time::Local> and L<Date::Parse> for conversion functions.
3553 sub total_owed_date {
3557 foreach my $cust_bill (
3558 grep { $_->_date <= $time }
3559 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
3561 $total_bill += $cust_bill->owed;
3563 sprintf( "%.2f", $total_bill );
3566 =item apply_payments_and_credits
3568 Applies unapplied payments and credits.
3570 In most cases, this new method should be used in place of sequential
3571 apply_payments and apply_credits methods.
3573 If there is an error, returns the error, otherwise returns false.
3577 sub apply_payments_and_credits {
3580 local $SIG{HUP} = 'IGNORE';
3581 local $SIG{INT} = 'IGNORE';
3582 local $SIG{QUIT} = 'IGNORE';
3583 local $SIG{TERM} = 'IGNORE';
3584 local $SIG{TSTP} = 'IGNORE';
3585 local $SIG{PIPE} = 'IGNORE';
3587 my $oldAutoCommit = $FS::UID::AutoCommit;
3588 local $FS::UID::AutoCommit = 0;
3591 $self->select_for_update; #mutex
3593 foreach my $cust_bill ( $self->open_cust_bill ) {
3594 my $error = $cust_bill->apply_payments_and_credits;
3596 $dbh->rollback if $oldAutoCommit;
3597 return "Error applying: $error";
3601 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3606 =item apply_credits OPTION => VALUE ...
3608 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
3609 to outstanding invoice balances in chronological order (or reverse
3610 chronological order if the I<order> option is set to B<newest>) and returns the
3611 value of any remaining unapplied credits available for refund (see
3612 L<FS::cust_refund>).
3614 Dies if there is an error.
3622 local $SIG{HUP} = 'IGNORE';
3623 local $SIG{INT} = 'IGNORE';
3624 local $SIG{QUIT} = 'IGNORE';
3625 local $SIG{TERM} = 'IGNORE';
3626 local $SIG{TSTP} = 'IGNORE';
3627 local $SIG{PIPE} = 'IGNORE';
3629 my $oldAutoCommit = $FS::UID::AutoCommit;
3630 local $FS::UID::AutoCommit = 0;
3633 $self->select_for_update; #mutex
3635 unless ( $self->total_credited ) {
3636 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3640 my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
3641 qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
3643 my @invoices = $self->open_cust_bill;
3644 @invoices = sort { $b->_date <=> $a->_date } @invoices
3645 if defined($opt{'order'}) && $opt{'order'} eq 'newest';
3648 foreach my $cust_bill ( @invoices ) {
3651 if ( !defined($credit) || $credit->credited == 0) {
3652 $credit = pop @credits or last;
3655 if ($cust_bill->owed >= $credit->credited) {
3656 $amount=$credit->credited;
3658 $amount=$cust_bill->owed;
3661 my $cust_credit_bill = new FS::cust_credit_bill ( {
3662 'crednum' => $credit->crednum,
3663 'invnum' => $cust_bill->invnum,
3664 'amount' => $amount,
3666 my $error = $cust_credit_bill->insert;
3668 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3672 redo if ($cust_bill->owed > 0);
3676 my $total_credited = $self->total_credited;
3678 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3680 return $total_credited;
3683 =item apply_payments
3685 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
3686 to outstanding invoice balances in chronological order.
3688 #and returns the value of any remaining unapplied payments.
3690 Dies if there is an error.
3694 sub apply_payments {
3697 local $SIG{HUP} = 'IGNORE';
3698 local $SIG{INT} = 'IGNORE';
3699 local $SIG{QUIT} = 'IGNORE';
3700 local $SIG{TERM} = 'IGNORE';
3701 local $SIG{TSTP} = 'IGNORE';
3702 local $SIG{PIPE} = 'IGNORE';
3704 my $oldAutoCommit = $FS::UID::AutoCommit;
3705 local $FS::UID::AutoCommit = 0;
3708 $self->select_for_update; #mutex
3712 my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
3713 qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
3715 my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
3716 qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
3720 foreach my $cust_bill ( @invoices ) {
3723 if ( !defined($payment) || $payment->unapplied == 0 ) {
3724 $payment = pop @payments or last;
3727 if ( $cust_bill->owed >= $payment->unapplied ) {
3728 $amount = $payment->unapplied;
3730 $amount = $cust_bill->owed;
3733 my $cust_bill_pay = new FS::cust_bill_pay ( {
3734 'paynum' => $payment->paynum,
3735 'invnum' => $cust_bill->invnum,
3736 'amount' => $amount,
3738 my $error = $cust_bill_pay->insert;
3740 $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
3744 redo if ( $cust_bill->owed > 0);
3748 my $total_unapplied_payments = $self->total_unapplied_payments;
3750 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3752 return $total_unapplied_payments;
3755 =item total_credited
3757 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3758 customer. See L<FS::cust_credit/credited>.
3762 sub total_credited {
3764 my $total_credit = 0;
3765 foreach my $cust_credit ( qsearch('cust_credit', {
3766 'custnum' => $self->custnum,
3768 $total_credit += $cust_credit->credited;
3770 sprintf( "%.2f", $total_credit );
3773 =item total_unapplied_payments
3775 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3776 See L<FS::cust_pay/unapplied>.
3780 sub total_unapplied_payments {
3782 my $total_unapplied = 0;
3783 foreach my $cust_pay ( qsearch('cust_pay', {
3784 'custnum' => $self->custnum,
3786 $total_unapplied += $cust_pay->unapplied;
3788 sprintf( "%.2f", $total_unapplied );
3791 =item total_unapplied_refunds
3793 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
3794 customer. See L<FS::cust_refund/unapplied>.
3798 sub total_unapplied_refunds {
3800 my $total_unapplied = 0;
3801 foreach my $cust_refund ( qsearch('cust_refund', {
3802 'custnum' => $self->custnum,
3804 $total_unapplied += $cust_refund->unapplied;
3806 sprintf( "%.2f", $total_unapplied );
3811 Returns the balance for this customer (total_owed plus total_unrefunded, minus
3812 total_credited minus total_unapplied_payments).
3820 + $self->total_unapplied_refunds
3821 - $self->total_credited
3822 - $self->total_unapplied_payments
3826 =item balance_date TIME
3828 Returns the balance for this customer, only considering invoices with date
3829 earlier than TIME (total_owed_date minus total_credited minus
3830 total_unapplied_payments). TIME is specified as a UNIX timestamp; see
3831 L<perlfunc/"time">). Also see L<Time::Local> and L<Date::Parse> for conversion
3840 $self->total_owed_date($time)
3841 + $self->total_unapplied_refunds
3842 - $self->total_credited
3843 - $self->total_unapplied_payments
3847 =item in_transit_payments
3849 Returns the total of requests for payments for this customer pending in
3850 batches in transit to the bank. See L<FS::pay_batch> and L<FS::cust_pay_batch>
3854 sub in_transit_payments {
3856 my $in_transit_payments = 0;
3857 foreach my $pay_batch ( qsearch('pay_batch', {
3860 foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
3861 'batchnum' => $pay_batch->batchnum,
3862 'custnum' => $self->custnum,
3864 $in_transit_payments += $cust_pay_batch->amount;
3867 sprintf( "%.2f", $in_transit_payments );
3870 =item paydate_monthyear
3872 Returns a two-element list consisting of the month and year of this customer's
3873 paydate (credit card expiration date for CARD customers)
3877 sub paydate_monthyear {
3879 if ( $self->paydate =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
3881 } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
3888 =item invoicing_list [ ARRAYREF ]
3890 If an arguement is given, sets these email addresses as invoice recipients
3891 (see L<FS::cust_main_invoice>). Errors are not fatal and are not reported
3892 (except as warnings), so use check_invoicing_list first.
3894 Returns a list of email addresses (with svcnum entries expanded).
3896 Note: You can clear the invoicing list by passing an empty ARRAYREF. You can
3897 check it without disturbing anything by passing nothing.
3899 This interface may change in the future.
3903 sub invoicing_list {
3904 my( $self, $arrayref ) = @_;
3907 my @cust_main_invoice;
3908 if ( $self->custnum ) {
3909 @cust_main_invoice =
3910 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3912 @cust_main_invoice = ();
3914 foreach my $cust_main_invoice ( @cust_main_invoice ) {
3915 #warn $cust_main_invoice->destnum;
3916 unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3917 #warn $cust_main_invoice->destnum;
3918 my $error = $cust_main_invoice->delete;
3919 warn $error if $error;
3922 if ( $self->custnum ) {
3923 @cust_main_invoice =
3924 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3926 @cust_main_invoice = ();
3928 my %seen = map { $_->address => 1 } @cust_main_invoice;
3929 foreach my $address ( @{$arrayref} ) {
3930 next if exists $seen{$address} && $seen{$address};
3931 $seen{$address} = 1;
3932 my $cust_main_invoice = new FS::cust_main_invoice ( {
3933 'custnum' => $self->custnum,
3936 my $error = $cust_main_invoice->insert;
3937 warn $error if $error;
3941 if ( $self->custnum ) {
3943 qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3950 =item check_invoicing_list ARRAYREF
3952 Checks these arguements as valid input for the invoicing_list method. If there
3953 is an error, returns the error, otherwise returns false.
3957 sub check_invoicing_list {
3958 my( $self, $arrayref ) = @_;
3960 foreach my $address ( @$arrayref ) {
3962 if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3963 return 'Can\'t add FAX invoice destination with a blank FAX number.';
3966 my $cust_main_invoice = new FS::cust_main_invoice ( {
3967 'custnum' => $self->custnum,
3970 my $error = $self->custnum
3971 ? $cust_main_invoice->check
3972 : $cust_main_invoice->checkdest
3974 return $error if $error;
3978 return "Email address required"
3979 if $conf->exists('cust_main-require_invoicing_list_email')
3980 && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
3985 =item set_default_invoicing_list
3987 Sets the invoicing list to all accounts associated with this customer,
3988 overwriting any previous invoicing list.
3992 sub set_default_invoicing_list {
3994 $self->invoicing_list($self->all_emails);
3999 Returns the email addresses of all accounts provisioned for this customer.
4006 foreach my $cust_pkg ( $self->all_pkgs ) {
4007 my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
4009 map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4010 grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
4012 $list{$_}=1 foreach map { $_->email } @svc_acct;
4017 =item invoicing_list_addpost
4019 Adds postal invoicing to this customer. If this customer is already configured
4020 to receive postal invoices, does nothing.
4024 sub invoicing_list_addpost {
4026 return if grep { $_ eq 'POST' } $self->invoicing_list;
4027 my @invoicing_list = $self->invoicing_list;
4028 push @invoicing_list, 'POST';
4029 $self->invoicing_list(\@invoicing_list);
4032 =item invoicing_list_emailonly
4034 Returns the list of email invoice recipients (invoicing_list without non-email
4035 destinations such as POST and FAX).
4039 sub invoicing_list_emailonly {
4041 warn "$me invoicing_list_emailonly called"
4043 grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
4046 =item invoicing_list_emailonly_scalar
4048 Returns the list of email invoice recipients (invoicing_list without non-email
4049 destinations such as POST and FAX) as a comma-separated scalar.
4053 sub invoicing_list_emailonly_scalar {
4055 warn "$me invoicing_list_emailonly_scalar called"
4057 join(', ', $self->invoicing_list_emailonly);
4060 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
4062 Returns an array of customers referred by this customer (referral_custnum set
4063 to this custnum). If DEPTH is given, recurses up to the given depth, returning
4064 customers referred by customers referred by this customer and so on, inclusive.
4065 The default behavior is DEPTH 1 (no recursion).
4069 sub referral_cust_main {
4071 my $depth = @_ ? shift : 1;
4072 my $exclude = @_ ? shift : {};
4075 map { $exclude->{$_->custnum}++; $_; }
4076 grep { ! $exclude->{ $_->custnum } }
4077 qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
4081 map { $_->referral_cust_main($depth-1, $exclude) }
4088 =item referral_cust_main_ncancelled
4090 Same as referral_cust_main, except only returns customers with uncancelled
4095 sub referral_cust_main_ncancelled {
4097 grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
4100 =item referral_cust_pkg [ DEPTH ]
4102 Like referral_cust_main, except returns a flat list of all unsuspended (and
4103 uncancelled) packages for each customer. The number of items in this list may
4104 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
4108 sub referral_cust_pkg {
4110 my $depth = @_ ? shift : 1;
4112 map { $_->unsuspended_pkgs }
4113 grep { $_->unsuspended_pkgs }
4114 $self->referral_cust_main($depth);
4117 =item referring_cust_main
4119 Returns the single cust_main record for the customer who referred this customer
4120 (referral_custnum), or false.
4124 sub referring_cust_main {
4126 return '' unless $self->referral_custnum;
4127 qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
4130 =item credit AMOUNT, REASON
4132 Applies a credit to this customer. If there is an error, returns the error,
4133 otherwise returns false.
4138 my( $self, $amount, $reason, %options ) = @_;
4139 my $cust_credit = new FS::cust_credit {
4140 'custnum' => $self->custnum,
4141 'amount' => $amount,
4142 'reason' => $reason,
4144 $cust_credit->insert(%options);
4147 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
4149 Creates a one-time charge for this customer. If there is an error, returns
4150 the error, otherwise returns false.
4156 my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
4157 if ( ref( $_[0] ) ) {
4158 $amount = $_[0]->{amount};
4159 $quantity = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
4160 $pkg = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
4161 $comment = exists($_[0]->{comment}) ? $_[0]->{comment}
4162 : '$'. sprintf("%.2f",$amount);
4163 $taxclass = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
4164 $classnum = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
4165 $additional = $_[0]->{additional};
4169 $pkg = @_ ? shift : 'One-time charge';
4170 $comment = @_ ? shift : '$'. sprintf("%.2f",$amount);
4171 $taxclass = @_ ? shift : '';
4175 local $SIG{HUP} = 'IGNORE';
4176 local $SIG{INT} = 'IGNORE';
4177 local $SIG{QUIT} = 'IGNORE';
4178 local $SIG{TERM} = 'IGNORE';
4179 local $SIG{TSTP} = 'IGNORE';
4180 local $SIG{PIPE} = 'IGNORE';
4182 my $oldAutoCommit = $FS::UID::AutoCommit;
4183 local $FS::UID::AutoCommit = 0;
4186 my $part_pkg = new FS::part_pkg ( {
4188 'comment' => $comment,
4192 'classnum' => $classnum ? $classnum : '',
4193 'taxclass' => $taxclass,
4196 my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
4197 ( 0 .. @$additional - 1 )
4199 'additional_count' => scalar(@$additional),
4200 'setup_fee' => $amount,
4203 my $error = $part_pkg->insert( options => \%options );
4205 $dbh->rollback if $oldAutoCommit;
4209 my $pkgpart = $part_pkg->pkgpart;
4210 my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
4211 unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
4212 my $type_pkgs = new FS::type_pkgs \%type_pkgs;
4213 $error = $type_pkgs->insert;
4215 $dbh->rollback if $oldAutoCommit;
4220 my $cust_pkg = new FS::cust_pkg ( {
4221 'custnum' => $self->custnum,
4222 'pkgpart' => $pkgpart,
4223 'quantity' => $quantity,
4226 $error = $cust_pkg->insert;
4228 $dbh->rollback if $oldAutoCommit;
4232 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
4237 #=item charge_postal_fee
4239 #Applies a one time charge this customer. If there is an error,
4240 #returns the error, returns the cust_pkg charge object or false
4241 #if there was no charge.
4245 # This should be a customer event. For that to work requires that bill
4246 # also be a customer event.
4248 sub charge_postal_fee {
4251 my $pkgpart = $conf->config('postal_invoice-fee_pkgpart');
4252 return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
4254 my $cust_pkg = new FS::cust_pkg ( {
4255 'custnum' => $self->custnum,
4256 'pkgpart' => $pkgpart,
4260 my $error = $cust_pkg->insert;
4261 $error ? $error : $cust_pkg;
4266 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4272 sort { $a->_date <=> $b->_date }
4273 qsearch('cust_bill', { 'custnum' => $self->custnum, } )
4276 =item open_cust_bill
4278 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4283 sub open_cust_bill {
4285 grep { $_->owed > 0 } $self->cust_bill;
4290 Returns all the credits (see L<FS::cust_credit>) for this customer.
4296 sort { $a->_date <=> $b->_date }
4297 qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4302 Returns all the payments (see L<FS::cust_pay>) for this customer.
4308 sort { $a->_date <=> $b->_date }
4309 qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
4314 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4320 sort { $a->_date <=> $b->_date }
4321 qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4327 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4333 sort { $a->_date <=> $b->_date }
4334 qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4339 Returns a name string for this customer, either "Company (Last, First)" or
4346 my $name = $self->contact;
4347 $name = $self->company. " ($name)" if $self->company;
4353 Returns a name string for this (service/shipping) contact, either
4354 "Company (Last, First)" or "Last, First".
4360 if ( $self->get('ship_last') ) {
4361 my $name = $self->ship_contact;
4362 $name = $self->ship_company. " ($name)" if $self->ship_company;
4371 Returns this customer's full (billing) contact name only, "Last, First"
4377 $self->get('last'). ', '. $self->first;
4382 Returns this customer's full (shipping) contact name only, "Last, First"
4388 $self->get('ship_last')
4389 ? $self->get('ship_last'). ', '. $self->ship_first
4395 Returns this customer's full country name
4401 code2country($self->country);
4408 Returns a status string for this customer, currently:
4412 =item prospect - No packages have ever been ordered
4414 =item active - One or more recurring packages is active
4416 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
4418 =item suspended - All non-cancelled recurring packages are suspended
4420 =item cancelled - All recurring packages are cancelled
4426 sub status { shift->cust_status(@_); }
4430 for my $status (qw( prospect active inactive suspended cancelled )) {
4431 my $method = $status.'_sql';
4432 my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
4433 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
4434 $sth->execute( ($self->custnum) x $numnum )
4435 or die "Error executing 'SELECT $sql': ". $sth->errstr;
4436 return $status if $sth->fetchrow_arrayref->[0];
4440 =item ucfirst_cust_status
4442 =item ucfirst_status
4444 Returns the status with the first character capitalized.
4448 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
4450 sub ucfirst_cust_status {
4452 ucfirst($self->cust_status);
4457 Returns a hex triplet color string for this customer's status.
4461 use vars qw(%statuscolor);
4462 tie %statuscolor, 'Tie::IxHash',
4463 'prospect' => '7e0079', #'000000', #black? naw, purple
4464 'active' => '00CC00', #green
4465 'inactive' => '0000CC', #blue
4466 'suspended' => 'FF9900', #yellow
4467 'cancelled' => 'FF0000', #red
4470 sub statuscolor { shift->cust_statuscolor(@_); }
4472 sub cust_statuscolor {
4474 $statuscolor{$self->cust_status};
4479 =head1 CLASS METHODS
4485 Class method that returns the list of possible status strings for customers
4486 (see L<the status method|/status>). For example:
4488 @statuses = FS::cust_main->statuses();
4493 #my $self = shift; #could be class...
4499 Returns an SQL expression identifying prospective cust_main records (customers
4500 with no packages ever ordered)
4504 use vars qw($select_count_pkgs);
4505 $select_count_pkgs =
4506 "SELECT COUNT(*) FROM cust_pkg
4507 WHERE cust_pkg.custnum = cust_main.custnum";
4509 sub select_count_pkgs_sql {
4513 sub prospect_sql { "
4514 0 = ( $select_count_pkgs )
4519 Returns an SQL expression identifying active cust_main records (customers with
4520 active recurring packages).
4525 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. "
4531 Returns an SQL expression identifying inactive cust_main records (customers with
4532 no active recurring packages, but otherwise unsuspended/uncancelled).
4536 sub inactive_sql { "
4537 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4539 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
4545 Returns an SQL expression identifying suspended cust_main records.
4550 sub suspended_sql { susp_sql(@_); }
4552 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " )
4554 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " )
4560 Returns an SQL expression identifying cancelled cust_main records.
4564 sub cancelled_sql { cancel_sql(@_); }
4567 my $recurring_sql = FS::cust_pkg->recurring_sql;
4568 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
4571 0 < ( $select_count_pkgs )
4572 AND 0 < ( $select_count_pkgs AND $recurring_sql AND $cancelled_sql )
4573 AND 0 = ( $select_count_pkgs AND $recurring_sql
4574 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
4576 AND 0 = ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " )
4582 =item uncancelled_sql
4584 Returns an SQL expression identifying un-cancelled cust_main records.
4588 sub uncancelled_sql { uncancel_sql(@_); }
4589 sub uncancel_sql { "
4590 ( 0 < ( $select_count_pkgs
4591 AND ( cust_pkg.cancel IS NULL
4592 OR cust_pkg.cancel = 0
4595 OR 0 = ( $select_count_pkgs )
4601 Returns an SQL fragment to retreive the balance.
4606 ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
4607 WHERE cust_bill.custnum = cust_main.custnum )
4608 - ( SELECT COALESCE( SUM(paid), 0 ) FROM cust_pay
4609 WHERE cust_pay.custnum = cust_main.custnum )
4610 - ( SELECT COALESCE( SUM(amount), 0 ) FROM cust_credit
4611 WHERE cust_credit.custnum = cust_main.custnum )
4612 + ( SELECT COALESCE( SUM(refund), 0 ) FROM cust_refund
4613 WHERE cust_refund.custnum = cust_main.custnum )
4616 =item balance_date_sql START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4618 Returns an SQL fragment to retreive the balance for this customer, only
4619 considering invoices with date earlier than START_TIME, and optionally not
4620 later than END_TIME (total_owed_date minus total_credited minus
4621 total_unapplied_payments).
4623 Times are specified as SQL fragments or numeric
4624 UNIX timestamps; see L<perlfunc/"time">). Also see L<Time::Local> and
4625 L<Date::Parse> for conversion functions. The empty string can be passed
4626 to disable that time constraint completely.
4628 Available options are:
4632 =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)
4634 =item total - set to true to remove all customer comparison clauses, for totals
4636 =item where - WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
4638 =item join - JOIN clause (typically used with the total option)
4646 sub balance_date_sql {
4647 my( $class, $start, $end, %opt ) = @_;
4649 my $owed = FS::cust_bill->owed_sql;
4650 my $unapp_refund = FS::cust_refund->unapplied_sql;
4651 my $unapp_credit = FS::cust_credit->unapplied_sql;
4652 my $unapp_pay = FS::cust_pay->unapplied_sql;
4654 my $j = $opt{'join'} || '';
4656 my $owed_wh = $class->_money_table_where( 'cust_bill', $start,$end,%opt );
4657 my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
4658 my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
4659 my $pay_wh = $class->_money_table_where( 'cust_pay', $start,$end,%opt );
4661 " ( SELECT COALESCE(SUM($owed), 0) FROM cust_bill $j $owed_wh )
4662 + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
4663 - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
4664 - ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $j $pay_wh )
4669 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
4671 Helper method for balance_date_sql; name (and usage) subject to change
4672 (suggestions welcome).
4674 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
4675 cust_refund, cust_credit or cust_pay).
4677 If TABLE is "cust_bill" or the unapplied_date option is true, only
4678 considers records with date earlier than START_TIME, and optionally not
4679 later than END_TIME .
4683 sub _money_table_where {
4684 my( $class, $table, $start, $end, %opt ) = @_;
4687 push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
4688 if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
4689 push @where, "$table._date <= $start" if defined($start) && length($start);
4690 push @where, "$table._date > $end" if defined($end) && length($end);
4692 push @where, @{$opt{'where'}} if $opt{'where'};
4693 my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
4699 =item search_sql HASHREF
4703 Returns a qsearch hash expression to search for parameters specified in HREF.
4704 Valid parameters are
4712 =item cancelled_pkgs
4718 listref of start date, end date
4724 =item current_balance
4726 listref (list returned by FS::UI::Web::parse_lt_gt($cgi, 'current_balance'))
4730 =item flattened_pkgs
4739 my ($class, $params) = @_;
4750 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
4752 "cust_main.agentnum = $1";
4759 #prospect active inactive suspended cancelled
4760 if ( grep { $params->{'status'} eq $_ } FS::cust_main->statuses() ) {
4761 my $method = $params->{'status'}. '_sql';
4762 #push @where, $class->$method();
4763 push @where, FS::cust_main->$method();
4767 # parse cancelled package checkbox
4772 $pkgwhere .= "AND (cancel = 0 or cancel is null)"
4773 unless $params->{'cancelled_pkgs'};
4779 foreach my $field (qw( signupdate )) {
4781 next unless exists($params->{$field});
4783 my($beginning, $ending) = @{$params->{$field}};
4786 "cust_main.$field IS NOT NULL",
4787 "cust_main.$field >= $beginning",
4788 "cust_main.$field <= $ending";
4790 $orderby ||= "ORDER BY cust_main.$field";
4798 my @payby = grep /^([A-Z]{4})$/, @{ $params->{'payby'} };
4800 push @where, '( '. join(' OR ', map "cust_main.payby = '$_'", @payby). ' )';
4807 #my $balance_sql = $class->balance_sql();
4808 my $balance_sql = FS::cust_main->balance_sql();
4810 push @where, map { s/current_balance/$balance_sql/; $_ }
4811 @{ $params->{'current_balance'} };
4817 if ( $params->{'custbatch'} =~ /^([\w\/\-\:\.]+)$/ and $1 ) {
4819 "cust_main.custbatch = '$1'";
4823 # setup queries, subs, etc. for the search
4826 $orderby ||= 'ORDER BY custnum';
4828 # here is the agent virtualization
4829 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
4831 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
4833 my $addl_from = 'LEFT JOIN cust_pkg USING ( custnum ) ';
4835 my $count_query = "SELECT COUNT(*) FROM cust_main $extra_sql";
4837 my $select = join(', ',
4838 'cust_main.custnum',
4839 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
4842 my(@extra_headers) = ();
4843 my(@extra_fields) = ();
4845 if ($params->{'flattened_pkgs'}) {
4847 if ($dbh->{Driver}->{Name} eq 'Pg') {
4849 $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";
4851 }elsif ($dbh->{Driver}->{Name} =~ /^mysql/i) {
4852 $select .= ", GROUP_CONCAT(pkg SEPARATOR '|') as magic";
4853 $addl_from .= " LEFT JOIN part_pkg using ( pkgpart )";
4855 warn "warning: unknown database type ". $dbh->{Driver}->{Name}.
4856 "omitting packing information from report.";
4859 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";
4861 my $sth = dbh->prepare($header_query) or die dbh->errstr;
4862 $sth->execute() or die $sth->errstr;
4863 my $headerrow = $sth->fetchrow_arrayref;
4864 my $headercount = $headerrow ? $headerrow->[0] : 0;
4865 while($headercount) {
4866 unshift @extra_headers, "Package ". $headercount;
4867 unshift @extra_fields, eval q!sub {my $c = shift;
4868 my @a = split '\|', $c->magic;
4869 my $p = $a[!.--$headercount. q!];
4877 'table' => 'cust_main',
4878 'select' => $select,
4880 'extra_sql' => $extra_sql,
4881 'order_by' => $orderby,
4882 'count_query' => $count_query,
4883 'extra_headers' => \@extra_headers,
4884 'extra_fields' => \@extra_fields,
4889 =item email_search_sql HASHREF
4893 Emails a notice to the specified customers.
4895 Valid parameters are those of the L<search_sql> method, plus the following:
4917 Optional job queue job for status updates.
4921 Returns an error message, or false for success.
4923 If an error occurs during any email, stops the enture send and returns that
4924 error. Presumably if you're getting SMTP errors aborting is better than
4925 retrying everything.
4929 sub email_search_sql {
4930 my($class, $params) = @_;
4932 my $from = delete $params->{from};
4933 my $subject = delete $params->{subject};
4934 my $html_body = delete $params->{html_body};
4935 my $text_body = delete $params->{text_body};
4937 my $job = delete $params->{'job'};
4939 my $sql_query = $class->search_sql($params);
4941 my $count_query = delete($sql_query->{'count_query'});
4942 my $count_sth = dbh->prepare($count_query)
4943 or die "Error preparing $count_query: ". dbh->errstr;
4945 or die "Error executing $count_query: ". $count_sth->errstr;
4946 my $count_arrayref = $count_sth->fetchrow_arrayref;
4947 my $num_cust = $count_arrayref->[0];
4949 #my @extra_headers = @{ delete($sql_query->{'extra_headers'}) };
4950 #my @extra_fields = @{ delete($sql_query->{'extra_fields'}) };
4953 my( $num, $last, $min_sec ) = (0, time, 5); #progresbar foo
4955 #eventually order+limit magic to reduce memory use?
4956 foreach my $cust_main ( qsearch($sql_query) ) {
4958 my $to = $cust_main->invoicing_list_emailonly_scalar;
4961 my $error = send_email(
4965 'subject' => $subject,
4966 'html_body' => $html_body,
4967 'text_body' => $text_body,
4970 return $error if $error;
4972 if ( $job ) { #progressbar foo
4974 if ( time - $min_sec > $last ) {
4975 my $error = $job->update_statustext(
4976 int( 100 * $num / $num_cust )
4978 die $error if $error;
4988 use Storable qw(thaw);
4991 sub process_email_search_sql {
4993 #warn "$me process_re_X $method for job $job\n" if $DEBUG;
4995 my $param = thaw(decode_base64(shift));
4996 warn Dumper($param) if $DEBUG;
4998 $param->{'job'} = $job;
5000 my $error = FS::cust_main->email_search_sql( $param );
5001 die $error if $error;
5005 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
5007 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
5008 records. Currently, I<first>, I<last> and/or I<company> may be specified (the
5009 appropriate ship_ field is also searched).
5011 Additional options are the same as FS::Record::qsearch
5016 my( $self, $fuzzy, $hash, @opt) = @_;
5021 check_and_rebuild_fuzzyfiles();
5022 foreach my $field ( keys %$fuzzy ) {
5024 my $all = $self->all_X($field);
5025 next unless scalar(@$all);
5028 $match{$_}=1 foreach ( amatch( $fuzzy->{$field}, ['i'], @$all ) );
5031 foreach ( keys %match ) {
5032 push @fcust, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
5033 push @fcust, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt);
5036 push @cust_main, grep { ! $fsaw{$_->custnum}++ } @fcust;
5039 # we want the components of $fuzzy ANDed, not ORed, but still don't want dupes
5041 @cust_main = grep { ++$saw{$_->custnum} == scalar(keys %$fuzzy) } @cust_main;
5049 Returns a masked version of the named field
5054 my ($self, $field) = @_;
5058 'x'x(length($self->getfield($field))-4).
5059 substr($self->getfield($field), (length($self->getfield($field))-4));
5069 =item smart_search OPTION => VALUE ...
5071 Accepts the following options: I<search>, the string to search for. The string
5072 will be searched for as a customer number, phone number, name or company name,
5073 as an exact, or, in some cases, a substring or fuzzy match (see the source code
5074 for the exact heuristics used); I<no_fuzzy_on_exact>, causes smart_search to
5075 skip fuzzy matching when an exact match is found.
5077 Any additional options are treated as an additional qualifier on the search
5080 Returns a (possibly empty) array of FS::cust_main objects.
5087 #here is the agent virtualization
5088 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5092 my $skip_fuzzy = delete $options{'no_fuzzy_on_exact'};
5093 my $search = delete $options{'search'};
5094 ( my $alphanum_search = $search ) =~ s/\W//g;
5096 if ( $alphanum_search =~ /^1?(\d{3})(\d{3})(\d{4})(\d*)$/ ) { #phone# search
5098 #false laziness w/Record::ut_phone
5099 my $phonen = "$1-$2-$3";
5100 $phonen .= " x$4" if $4;
5102 push @cust_main, qsearch( {
5103 'table' => 'cust_main',
5104 'hashref' => { %options },
5105 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5107 join(' OR ', map "$_ = '$phonen'",
5108 qw( daytime night fax
5109 ship_daytime ship_night ship_fax )
5112 " AND $agentnums_sql", #agent virtualization
5115 unless ( @cust_main || $phonen =~ /x\d+$/ ) { #no exact match
5116 #try looking for matches with extensions unless one was specified
5118 push @cust_main, qsearch( {
5119 'table' => 'cust_main',
5120 'hashref' => { %options },
5121 'extra_sql' => ( scalar(keys %options) ? ' AND ' : ' WHERE ' ).
5123 join(' OR ', map "$_ LIKE '$phonen\%'",
5125 ship_daytime ship_night )
5128 " AND $agentnums_sql", #agent virtualization
5133 # custnum search (also try agent_custid), with some tweaking options if your
5134 # legacy cust "numbers" have letters
5135 } elsif ( $search =~ /^\s*(\d+)\s*$/
5136 || ( $conf->config('cust_main-agent_custid-format') eq 'ww?d+'
5137 && $search =~ /^\s*(\w\w?\d+)\s*$/
5142 push @cust_main, qsearch( {
5143 'table' => 'cust_main',
5144 'hashref' => { 'custnum' => $1, %options },
5145 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5148 push @cust_main, qsearch( {
5149 'table' => 'cust_main',
5150 'hashref' => { 'agent_custid' => $1, %options },
5151 'extra_sql' => " AND $agentnums_sql", #agent virtualization
5154 } elsif ( $search =~ /^\s*(\S.*\S)\s+\((.+), ([^,]+)\)\s*$/ ) {
5156 my($company, $last, $first) = ( $1, $2, $3 );
5158 # "Company (Last, First)"
5159 #this is probably something a browser remembered,
5160 #so just do an exact search
5162 foreach my $prefix ( '', 'ship_' ) {
5163 push @cust_main, qsearch( {
5164 'table' => 'cust_main',
5165 'hashref' => { $prefix.'first' => $first,
5166 $prefix.'last' => $last,
5167 $prefix.'company' => $company,
5170 'extra_sql' => " AND $agentnums_sql",
5174 } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { # value search
5175 # try (ship_){last,company}
5179 # # remove "(Last, First)" in "Company (Last, First)", otherwise the
5180 # # full strings the browser remembers won't work
5181 # $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
5183 use Lingua::EN::NameParse;
5184 my $NameParse = new Lingua::EN::NameParse(
5186 allow_reversed => 1,
5189 my($last, $first) = ( '', '' );
5190 #maybe disable this too and just rely on NameParse?
5191 if ( $value =~ /^(.+),\s*([^,]+)$/ ) { # Last, First
5193 ($last, $first) = ( $1, $2 );
5195 #} elsif ( $value =~ /^(.+)\s+(.+)$/ ) {
5196 } elsif ( ! $NameParse->parse($value) ) {
5198 my %name = $NameParse->components;
5199 $first = $name{'given_name_1'};
5200 $last = $name{'surname_1'};
5204 if ( $first && $last ) {
5206 my($q_last, $q_first) = ( dbh->quote($last), dbh->quote($first) );
5209 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5211 ( ( LOWER(last) = $q_last AND LOWER(first) = $q_first )
5212 OR ( LOWER(ship_last) = $q_last AND LOWER(ship_first) = $q_first )
5215 push @cust_main, qsearch( {
5216 'table' => 'cust_main',
5217 'hashref' => \%options,
5218 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5221 # or it just be something that was typed in... (try that in a sec)
5225 my $q_value = dbh->quote($value);
5228 my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
5229 $sql .= " ( LOWER(last) = $q_value
5230 OR LOWER(company) = $q_value
5231 OR LOWER(ship_last) = $q_value
5232 OR LOWER(ship_company) = $q_value
5235 push @cust_main, qsearch( {
5236 'table' => 'cust_main',
5237 'hashref' => \%options,
5238 'extra_sql' => "$sql AND $agentnums_sql", #agent virtualization
5241 #always do substring & fuzzy,
5242 #getting complains searches are not returning enough
5243 unless ( @cust_main && $skip_fuzzy ) { #no exact match, trying substring/fuzzy
5245 #still some false laziness w/search_sql (was search/cust_main.cgi)
5250 { 'company' => { op=>'ILIKE', value=>"%$value%" }, },
5251 { 'ship_company' => { op=>'ILIKE', value=>"%$value%" }, },
5254 if ( $first && $last ) {
5257 { 'first' => { op=>'ILIKE', value=>"%$first%" },
5258 'last' => { op=>'ILIKE', value=>"%$last%" },
5260 { 'ship_first' => { op=>'ILIKE', value=>"%$first%" },
5261 'ship_last' => { op=>'ILIKE', value=>"%$last%" },
5268 { 'last' => { op=>'ILIKE', value=>"%$value%" }, },
5269 { 'ship_last' => { op=>'ILIKE', value=>"%$value%" }, },
5273 foreach my $hashref ( @hashrefs ) {
5275 push @cust_main, qsearch( {
5276 'table' => 'cust_main',
5277 'hashref' => { %$hashref,
5280 'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
5289 " AND $agentnums_sql", #extra_sql #agent virtualization
5292 if ( $first && $last ) {
5293 push @cust_main, FS::cust_main->fuzzy_search(
5294 { 'last' => $last, #fuzzy hashref
5295 'first' => $first }, #
5299 foreach my $field ( 'last', 'company' ) {
5301 FS::cust_main->fuzzy_search( { $field => $value }, @fuzopts );
5306 #eliminate duplicates
5308 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5318 Accepts the following options: I<email>, the email address to search for. The
5319 email address will be searched for as an email invoice destination and as an
5322 #Any additional options are treated as an additional qualifier on the search
5323 #(i.e. I<agentnum>).
5325 Returns a (possibly empty) array of FS::cust_main objects (but usually just
5335 my $email = delete $options{'email'};
5337 #we're only being used by RT at the moment... no agent virtualization yet
5338 #my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
5342 if ( $email =~ /([^@]+)\@([^@]+)/ ) {
5344 my ( $user, $domain ) = ( $1, $2 );
5346 warn "$me smart_search: searching for $user in domain $domain"
5352 'table' => 'cust_main_invoice',
5353 'hashref' => { 'dest' => $email },
5360 map $_->cust_svc->cust_pkg,
5362 'table' => 'svc_acct',
5363 'hashref' => { 'username' => $user, },
5365 'AND ( SELECT domain FROM svc_domain
5366 WHERE svc_acct.domsvc = svc_domain.svcnum
5367 ) = '. dbh->quote($domain),
5373 @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
5375 warn "$me smart_search: found ". scalar(@cust_main). " unique customers"
5382 =item check_and_rebuild_fuzzyfiles
5386 use vars qw(@fuzzyfields);
5387 @fuzzyfields = ( 'last', 'first', 'company' );
5389 sub check_and_rebuild_fuzzyfiles {
5390 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5391 rebuild_fuzzyfiles() if grep { ! -e "$dir/cust_main.$_" } @fuzzyfields
5394 =item rebuild_fuzzyfiles
5398 sub rebuild_fuzzyfiles {
5400 use Fcntl qw(:flock);
5402 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5403 mkdir $dir, 0700 unless -d $dir;
5405 foreach my $fuzzy ( @fuzzyfields ) {
5407 open(LOCK,">>$dir/cust_main.$fuzzy")
5408 or die "can't open $dir/cust_main.$fuzzy: $!";
5410 or die "can't lock $dir/cust_main.$fuzzy: $!";
5412 open (CACHE,">$dir/cust_main.$fuzzy.tmp")
5413 or die "can't open $dir/cust_main.$fuzzy.tmp: $!";
5415 foreach my $field ( $fuzzy, "ship_$fuzzy" ) {
5416 my $sth = dbh->prepare("SELECT $field FROM cust_main".
5417 " WHERE $field != '' AND $field IS NOT NULL");
5418 $sth->execute or die $sth->errstr;
5420 while ( my $row = $sth->fetchrow_arrayref ) {
5421 print CACHE $row->[0]. "\n";
5426 close CACHE or die "can't close $dir/cust_main.$fuzzy.tmp: $!";
5428 rename "$dir/cust_main.$fuzzy.tmp", "$dir/cust_main.$fuzzy";
5439 my( $self, $field ) = @_;
5440 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5441 open(CACHE,"<$dir/cust_main.$field")
5442 or die "can't open $dir/cust_main.$field: $!";
5443 my @array = map { chomp; $_; } <CACHE>;
5448 =item append_fuzzyfiles LASTNAME COMPANY
5452 sub append_fuzzyfiles {
5453 #my( $first, $last, $company ) = @_;
5455 &check_and_rebuild_fuzzyfiles;
5457 use Fcntl qw(:flock);
5459 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
5461 foreach my $field (qw( first last company )) {
5466 open(CACHE,">>$dir/cust_main.$field")
5467 or die "can't open $dir/cust_main.$field: $!";
5468 flock(CACHE,LOCK_EX)
5469 or die "can't lock $dir/cust_main.$field: $!";
5471 print CACHE "$value\n";
5473 flock(CACHE,LOCK_UN)
5474 or die "can't unlock $dir/cust_main.$field: $!";
5483 =item process_batch_import
5485 Load a batch import as a queued JSRPC job
5489 use Storable qw(thaw);
5492 sub process_batch_import {
5495 my $param = thaw(decode_base64(shift));
5496 warn Dumper($param) if $DEBUG;
5498 my $files = $param->{'uploaded_files'}
5499 or die "No files provided.\n";
5501 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
5503 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
5504 my $file = $dir. $files{'file'};
5507 if ( $file =~ /\.(\w+)$/i ) {
5511 warn "can't parse file type from filename $file; defaulting to CSV";
5516 FS::cust_main::batch_import( {
5520 custbatch => $param->{custbatch},
5521 agentnum => $param->{'agentnum'},
5522 refnum => $param->{'refnum'},
5523 pkgpart => $param->{'pkgpart'},
5524 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
5525 # city state zip comments )],
5526 'format' => $param->{'format'},
5531 die "$error\n" if $error;
5539 #some false laziness w/cdr.pm now
5543 my $job = $param->{job};
5545 my $filename = $param->{file};
5546 my $type = $param->{type} || 'csv';
5548 my $custbatch = $param->{custbatch};
5550 my $agentnum = $param->{agentnum};
5551 my $refnum = $param->{refnum};
5552 my $pkgpart = $param->{pkgpart};
5554 my $format = $param->{'format'};
5558 if ( $format eq 'simple' ) {
5559 @fields = qw( cust_pkg.setup dayphone first last
5560 address1 address2 city state zip comments );
5562 } elsif ( $format eq 'extended' ) {
5563 @fields = qw( agent_custid refnum
5564 last first address1 address2 city state zip country
5566 ship_last ship_first ship_address1 ship_address2
5567 ship_city ship_state ship_zip ship_country
5568 payinfo paycvv paydate
5571 svc_acct.username svc_acct._password
5574 } elsif ( $format eq 'extended-plus_company' ) {
5575 @fields = qw( agent_custid refnum
5576 last first company address1 address2 city state zip country
5578 ship_last ship_first ship_company ship_address1 ship_address2
5579 ship_city ship_state ship_zip ship_country
5580 payinfo paycvv paydate
5583 svc_acct.username svc_acct._password
5587 die "unknown format $format";
5593 if ( $type eq 'csv' ) {
5595 eval "use Text::CSV_XS;";
5598 $parser = new Text::CSV_XS;
5600 @buffer = split(/\r?\n/, slurp($filename) );
5601 $count = scalar(@buffer);
5603 } elsif ( $type eq 'xls' ) {
5605 eval "use Spreadsheet::ParseExcel;";
5608 my $excel = new Spreadsheet::ParseExcel::Workbook->Parse($filename);
5609 $parser = $excel->{Worksheet}[0]; #first sheet
5611 $count = $parser->{MaxRow} || $parser->{MinRow};
5615 die "Unknown file type $type\n";
5620 local $SIG{HUP} = 'IGNORE';
5621 local $SIG{INT} = 'IGNORE';
5622 local $SIG{QUIT} = 'IGNORE';
5623 local $SIG{TERM} = 'IGNORE';
5624 local $SIG{TSTP} = 'IGNORE';
5625 local $SIG{PIPE} = 'IGNORE';
5627 my $oldAutoCommit = $FS::UID::AutoCommit;
5628 local $FS::UID::AutoCommit = 0;
5633 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
5637 if ( $type eq 'csv' ) {
5639 last unless scalar(@buffer);
5640 $line = shift(@buffer);
5642 $parser->parse($line) or do {
5643 $dbh->rollback if $oldAutoCommit;
5644 return "can't parse: ". $parser->error_input();
5646 @columns = $parser->fields();
5648 } elsif ( $type eq 'xls' ) {
5650 last if $row > ($parser->{MaxRow} || $parser->{MinRow});
5652 my @row = @{ $parser->{Cells}[$row] };
5653 @columns = map $_->{Val}, @row;
5656 #warn $z++. ": $_\n" for @columns;
5659 die "Unknown file type $type\n";
5662 #warn join('-',@columns);
5665 custbatch => $custbatch,
5666 agentnum => $agentnum,
5668 country => $conf->config('countrydefault') || 'US',
5669 payby => $payby, #default
5670 paydate => '12/2037', #default
5672 my $billtime = time;
5673 my %cust_pkg = ( pkgpart => $pkgpart );
5675 foreach my $field ( @fields ) {
5677 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
5679 #$cust_pkg{$1} = str2time( shift @$columns );
5680 if ( $1 eq 'pkgpart' ) {
5681 $cust_pkg{$1} = shift @columns;
5682 } elsif ( $1 eq 'setup' ) {
5683 $billtime = str2time(shift @columns);
5685 $cust_pkg{$1} = str2time( shift @columns );
5688 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
5690 $svc_acct{$1} = shift @columns;
5694 #refnum interception
5695 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
5697 my $referral = $columns[0];
5698 my %hash = ( 'referral' => $referral,
5699 'agentnum' => $agentnum,
5703 my $part_referral = qsearchs('part_referral', \%hash )
5704 || new FS::part_referral \%hash;
5706 unless ( $part_referral->refnum ) {
5707 my $error = $part_referral->insert;
5709 $dbh->rollback if $oldAutoCommit;
5710 return "can't auto-insert advertising source: $referral: $error";
5714 $columns[0] = $part_referral->refnum;
5717 my $value = shift @columns;
5718 $cust_main{$field} = $value if length($value);
5722 $cust_main{'payby'} = 'CARD'
5723 if defined $cust_main{'payinfo'}
5724 && length $cust_main{'payinfo'};
5726 my $invoicing_list = $cust_main{'invoicing_list'}
5727 ? [ delete $cust_main{'invoicing_list'} ]
5730 my $cust_main = new FS::cust_main ( \%cust_main );
5733 tie my %hash, 'Tie::RefHash'; #this part is important
5735 if ( $cust_pkg{'pkgpart'} ) {
5736 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
5739 if ( $svc_acct{'username'} ) {
5740 my $part_pkg = $cust_pkg->part_pkg;
5741 unless ( $part_pkg ) {
5742 $dbh->rollback if $oldAutoCommit;
5743 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
5745 $svc_acct{svcpart} = $part_pkg->svcpart( 'svc_acct' );
5746 push @svc_acct, new FS::svc_acct ( \%svc_acct )
5749 $hash{$cust_pkg} = \@svc_acct;
5752 my $error = $cust_main->insert( \%hash, $invoicing_list );
5755 $dbh->rollback if $oldAutoCommit;
5756 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
5759 if ( $format eq 'simple' ) {
5761 #false laziness w/bill.cgi
5762 $error = $cust_main->bill( 'time' => $billtime );
5764 $dbh->rollback if $oldAutoCommit;
5765 return "can't bill customer for $line: $error";
5768 $error = $cust_main->apply_payments_and_credits;
5770 $dbh->rollback if $oldAutoCommit;
5771 return "can't bill customer for $line: $error";
5774 $error = $cust_main->collect();
5776 $dbh->rollback if $oldAutoCommit;
5777 return "can't collect customer for $line: $error";
5784 if ( $job && time - $min_sec > $last ) { #progress bar
5785 $job->update_statustext( int(100 * $row / $count) );
5791 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
5793 return "Empty file!" unless $row;
5805 #warn join('-',keys %$param);
5806 my $fh = $param->{filehandle};
5807 my @fields = @{$param->{fields}};
5809 eval "use Text::CSV_XS;";
5812 my $csv = new Text::CSV_XS;
5819 local $SIG{HUP} = 'IGNORE';
5820 local $SIG{INT} = 'IGNORE';
5821 local $SIG{QUIT} = 'IGNORE';
5822 local $SIG{TERM} = 'IGNORE';
5823 local $SIG{TSTP} = 'IGNORE';
5824 local $SIG{PIPE} = 'IGNORE';
5826 my $oldAutoCommit = $FS::UID::AutoCommit;
5827 local $FS::UID::AutoCommit = 0;
5830 #while ( $columns = $csv->getline($fh) ) {
5832 while ( defined($line=<$fh>) ) {
5834 $csv->parse($line) or do {
5835 $dbh->rollback if $oldAutoCommit;
5836 return "can't parse: ". $csv->error_input();
5839 my @columns = $csv->fields();
5840 #warn join('-',@columns);
5843 foreach my $field ( @fields ) {
5844 $row{$field} = shift @columns;
5847 my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
5848 unless ( $cust_main ) {
5849 $dbh->rollback if $oldAutoCommit;
5850 return "unknown custnum $row{'custnum'}";
5853 if ( $row{'amount'} > 0 ) {
5854 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
5856 $dbh->rollback if $oldAutoCommit;
5860 } elsif ( $row{'amount'} < 0 ) {
5861 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
5864 $dbh->rollback if $oldAutoCommit;
5874 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
5876 return "Empty file!" unless $imported;
5882 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5884 Sends a templated email notification to the customer (see L<Text::Template>).
5886 OPTIONS is a hash and may include
5888 I<from> - the email sender (default is invoice_from)
5890 I<to> - comma-separated scalar or arrayref of recipients
5891 (default is invoicing_list)
5893 I<subject> - The subject line of the sent email notification
5894 (default is "Notice from company_name")
5896 I<extra_fields> - a hashref of name/value pairs which will be substituted
5899 The following variables are vavailable in the template.
5901 I<$first> - the customer first name
5902 I<$last> - the customer last name
5903 I<$company> - the customer company
5904 I<$payby> - a description of the method of payment for the customer
5905 # would be nice to use FS::payby::shortname
5906 I<$payinfo> - the account information used to collect for this customer
5907 I<$expdate> - the expiration of the customer payment in seconds from epoch
5912 my ($customer, $template, %options) = @_;
5914 return unless $conf->exists($template);
5916 my $from = $conf->config('invoice_from') if $conf->exists('invoice_from');
5917 $from = $options{from} if exists($options{from});
5919 my $to = join(',', $customer->invoicing_list_emailonly);
5920 $to = $options{to} if exists($options{to});
5922 my $subject = "Notice from " . $conf->config('company_name')
5923 if $conf->exists('company_name');
5924 $subject = $options{subject} if exists($options{subject});
5926 my $notify_template = new Text::Template (TYPE => 'ARRAY',
5927 SOURCE => [ map "$_\n",
5928 $conf->config($template)]
5930 or die "can't create new Text::Template object: Text::Template::ERROR";
5931 $notify_template->compile()
5932 or die "can't compile template: Text::Template::ERROR";
5934 my $paydate = $customer->paydate || '2037-12-31';
5935 $FS::notify_template::_template::first = $customer->first;
5936 $FS::notify_template::_template::last = $customer->last;
5937 $FS::notify_template::_template::company = $customer->company;
5938 $FS::notify_template::_template::payinfo = $customer->mask_payinfo;
5939 my $payby = $customer->payby;
5940 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5941 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5943 #credit cards expire at the end of the month/year of their exp date
5944 if ($payby eq 'CARD' || $payby eq 'DCRD') {
5945 $FS::notify_template::_template::payby = 'credit card';
5946 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5947 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5949 }elsif ($payby eq 'COMP') {
5950 $FS::notify_template::_template::payby = 'complimentary account';
5952 $FS::notify_template::_template::payby = 'current method';
5954 $FS::notify_template::_template::expdate = $expire_time;
5956 for (keys %{$options{extra_fields}}){
5958 ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
5961 send_email(from => $from,
5963 subject => $subject,
5964 body => $notify_template->fill_in( PACKAGE =>
5965 'FS::notify_template::_template' ),
5970 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5972 Generates a templated notification to the customer (see L<Text::Template>).
5974 OPTIONS is a hash and may include
5976 I<extra_fields> - a hashref of name/value pairs which will be substituted
5977 into the template. These values may override values mentioned below
5978 and those from the customer record.
5980 The following variables are available in the template instead of or in addition
5981 to the fields of the customer record.
5983 I<$payby> - a description of the method of payment for the customer
5984 # would be nice to use FS::payby::shortname
5985 I<$payinfo> - the masked account information used to collect for this customer
5986 I<$expdate> - the expiration of the customer payment method in seconds from epoch
5987 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress
5991 sub generate_letter {
5992 my ($self, $template, %options) = @_;
5994 return unless $conf->exists($template);
5996 my $letter_template = new Text::Template
5998 SOURCE => [ map "$_\n", $conf->config($template)],
5999 DELIMITERS => [ '[@--', '--@]' ],
6001 or die "can't create new Text::Template object: Text::Template::ERROR";
6003 $letter_template->compile()
6004 or die "can't compile template: Text::Template::ERROR";
6006 my %letter_data = map { $_ => $self->$_ } $self->fields;
6007 $letter_data{payinfo} = $self->mask_payinfo;
6009 my $paydate = $self->paydate || '2037-12-31';
6010 my $payby = $self->payby;
6011 my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
6012 my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
6014 #credit cards expire at the end of the month/year of their exp date
6015 if ($payby eq 'CARD' || $payby eq 'DCRD') {
6016 $letter_data{payby} = 'credit card';
6017 ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
6018 $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
6020 }elsif ($payby eq 'COMP') {
6021 $letter_data{payby} = 'complimentary account';
6023 $letter_data{payby} = 'current method';
6025 $letter_data{expdate} = $expire_time;
6027 for (keys %{$options{extra_fields}}){
6028 $letter_data{$_} = $options{extra_fields}->{$_};
6031 unless(exists($letter_data{returnaddress})){
6032 my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
6033 $self->_agent_template)
6036 $letter_data{returnaddress} = length($retadd) ? $retadd : '~';
6039 $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
6041 my $dir = $FS::UID::conf_dir."cache.". $FS::UID::datasrc;
6042 my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
6046 ) or die "can't open temp file: $!\n";
6048 $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
6050 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
6054 =item print_ps TEMPLATE
6056 Returns an postscript letter filled in from TEMPLATE, as a scalar.
6062 my $file = $self->generate_letter(@_);
6063 FS::Misc::generate_ps($file);
6066 =item print TEMPLATE
6068 Prints the filled in template.
6070 TEMPLATE is the name of a L<Text::Template> to fill in and print.
6074 sub queueable_print {
6077 my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
6078 or die "invalid customer number: " . $opt{custvnum};
6080 my $error = $self->print( $opt{template} );
6081 die $error if $error;
6085 my ($self, $template) = (shift, shift);
6086 do_print [ $self->print_ps($template) ];
6089 sub agent_template {
6091 $self->_agent_plandata('agent_templatename');
6094 sub agent_invoice_from {
6096 $self->_agent_plandata('agent_invoice_from');
6099 sub _agent_plandata {
6100 my( $self, $option ) = @_;
6103 if ( driver_name =~ /^Pg/i ) {
6105 } elsif ( driver_name =~ /^mysql/i ) {
6108 die "don't know how to use regular expressions in ". driver_name. " databases";
6111 my $part_bill_event = qsearchs( 'part_bill_event',
6113 'payby' => $self->payby,
6114 'plan' => 'send_agent',
6115 'plandata' => { 'op' => $regexp,
6116 'value' => "(^|\n)agentnum ".
6124 'ORDER BY seconds LIMIT 1'
6127 return '' unless $part_bill_event;
6129 if ( $part_bill_event->plandata =~ /^$option (.*)$/m ) {
6132 warn "can't parse part_bill_event eventpart#". $part_bill_event->eventpart.
6133 " plandata for $option";
6145 The delete method should possibly take an FS::cust_main object reference
6146 instead of a scalar customer number.
6148 Bill and collect options should probably be passed as references instead of a
6151 There should probably be a configuration file with a list of allowed credit
6154 No multiple currency support (probably a larger project than just this module).
6156 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
6158 Birthdates rely on negative epoch values.
6160 The payby for card/check batches is broken. With mixed batching, bad
6165 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
6166 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
6167 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.