when refunding against a specific card payment, use the card details from the payment...
[freeside.git] / FS / FS / cust_main.pm
1 package FS::cust_main;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $conf $DEBUG $import );
5 use vars qw( $realtime_bop_decline_quiet ); #ugh
6 use Safe;
7 use Carp;
8 use Exporter;
9 BEGIN {
10   eval "use Time::Local;";
11   die "Time::Local minimum version 1.05 required with Perl versions before 5.6"
12     if $] < 5.006 && !defined($Time::Local::VERSION);
13   #eval "use Time::Local qw(timelocal timelocal_nocheck);";
14   eval "use Time::Local qw(timelocal_nocheck);";
15 }
16 use Date::Format;
17 #use Date::Manip;
18 use String::Approx qw(amatch);
19 use Business::CreditCard;
20 use FS::UID qw( getotaker dbh );
21 use FS::Record qw( qsearchs qsearch dbdef );
22 use FS::Misc qw( send_email );
23 use FS::cust_pkg;
24 use FS::cust_bill;
25 use FS::cust_bill_pkg;
26 use FS::cust_pay;
27 use FS::cust_pay_void;
28 use FS::cust_credit;
29 use FS::cust_refund;
30 use FS::part_referral;
31 use FS::cust_main_county;
32 use FS::agent;
33 use FS::cust_main_invoice;
34 use FS::cust_credit_bill;
35 use FS::cust_bill_pay;
36 use FS::prepay_credit;
37 use FS::queue;
38 use FS::part_pkg;
39 use FS::part_bill_event;
40 use FS::cust_bill_event;
41 use FS::cust_tax_exempt;
42 use FS::type_pkgs;
43 use FS::Msgcat qw(gettext);
44
45 @ISA = qw( FS::Record );
46
47 @EXPORT_OK = qw( smart_search );
48
49 $realtime_bop_decline_quiet = 0;
50
51 $DEBUG = 0;
52 #$DEBUG = 1;
53
54 $import = 0;
55
56 #ask FS::UID to run this stuff for us later
57 #$FS::UID::callback{'FS::cust_main'} = sub { 
58 install_callback FS::UID sub { 
59   $conf = new FS::Conf;
60   #yes, need it for stuff below (prolly should be cached)
61 };
62
63 sub _cache {
64   my $self = shift;
65   my ( $hashref, $cache ) = @_;
66   if ( exists $hashref->{'pkgnum'} ) {
67 #    #@{ $self->{'_pkgnum'} } = ();
68     my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
69     $self->{'_pkgnum'} = $subcache;
70     #push @{ $self->{'_pkgnum'} },
71     FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
72   }
73 }
74
75 =head1 NAME
76
77 FS::cust_main - Object methods for cust_main records
78
79 =head1 SYNOPSIS
80
81   use FS::cust_main;
82
83   $record = new FS::cust_main \%hash;
84   $record = new FS::cust_main { 'column' => 'value' };
85
86   $error = $record->insert;
87
88   $error = $new_record->replace($old_record);
89
90   $error = $record->delete;
91
92   $error = $record->check;
93
94   @cust_pkg = $record->all_pkgs;
95
96   @cust_pkg = $record->ncancelled_pkgs;
97
98   @cust_pkg = $record->suspended_pkgs;
99
100   $error = $record->bill;
101   $error = $record->bill %options;
102   $error = $record->bill 'time' => $time;
103
104   $error = $record->collect;
105   $error = $record->collect %options;
106   $error = $record->collect 'invoice_time'   => $time,
107                             'batch_card'     => 'yes',
108                             'report_badcard' => 'yes',
109                           ;
110
111 =head1 DESCRIPTION
112
113 An FS::cust_main object represents a customer.  FS::cust_main inherits from 
114 FS::Record.  The following fields are currently supported:
115
116 =over 4
117
118 =item custnum - primary key (assigned automatically for new customers)
119
120 =item agentnum - agent (see L<FS::agent>)
121
122 =item refnum - Advertising source (see L<FS::part_referral>)
123
124 =item first - name
125
126 =item last - name
127
128 =item ss - social security number (optional)
129
130 =item company - (optional)
131
132 =item address1
133
134 =item address2 - (optional)
135
136 =item city
137
138 =item county - (optional, see L<FS::cust_main_county>)
139
140 =item state - (see L<FS::cust_main_county>)
141
142 =item zip
143
144 =item country - (see L<FS::cust_main_county>)
145
146 =item daytime - phone (optional)
147
148 =item night - phone (optional)
149
150 =item fax - phone (optional)
151
152 =item ship_first - name
153
154 =item ship_last - name
155
156 =item ship_company - (optional)
157
158 =item ship_address1
159
160 =item ship_address2 - (optional)
161
162 =item ship_city
163
164 =item ship_county - (optional, see L<FS::cust_main_county>)
165
166 =item ship_state - (see L<FS::cust_main_county>)
167
168 =item ship_zip
169
170 =item ship_country - (see L<FS::cust_main_county>)
171
172 =item ship_daytime - phone (optional)
173
174 =item ship_night - phone (optional)
175
176 =item ship_fax - phone (optional)
177
178 =item payby - I<CARD> (credit card - automatic), I<DCRD> (credit card - on-demand), I<CHEK> (electronic check - automatic), I<DCHK> (electronic check - on-demand), I<LECB> (Phone bill billing), I<BILL> (billing), I<COMP> (free), or I<PREPAY> (special billing type: applies a payment from a prepaid card - see L<FS::prepay_credit> - and sets billing type to I<BILL>)
179
180 =item payinfo - card number, P.O., comp issuer (4-8 lowercase alphanumerics; think username) or prepayment identifier (see L<FS::prepay_credit>)
181
182 =item paycvv - 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
183
184 =item paydate - expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
185
186 =item payname - name on card or billing name
187
188 =item tax - tax exempt, empty or `Y'
189
190 =item otaker - order taker (assigned automatically, see L<FS::UID>)
191
192 =item comments - comments (optional)
193
194 =item referral_custnum - referring customer number
195
196 =back
197
198 =head1 METHODS
199
200 =over 4
201
202 =item new HASHREF
203
204 Creates a new customer.  To add the customer to the database, see L<"insert">.
205
206 Note that this stores the hash reference, not a distinct copy of the hash it
207 points to.  You can ask the object for a copy with the I<hash> method.
208
209 =cut
210
211 sub table { 'cust_main'; }
212
213 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
214
215 Adds this customer to the database.  If there is an error, returns the error,
216 otherwise returns false.
217
218 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
219 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
220 are inserted atomicly, or the transaction is rolled back.  Passing an empty
221 hash reference is equivalent to not supplying this parameter.  There should be
222 a better explanation of this, but until then, here's an example:
223
224   use Tie::RefHash;
225   tie %hash, 'Tie::RefHash'; #this part is important
226   %hash = (
227     $cust_pkg => [ $svc_acct ],
228     ...
229   );
230   $cust_main->insert( \%hash );
231
232 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
233 be set as the invoicing list (see L<"invoicing_list">).  Errors return as
234 expected and rollback the entire transaction; it is not necessary to call 
235 check_invoicing_list first.  The invoicing_list is set after the records in the
236 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
237 invoicing_list destination to the newly-created svc_acct.  Here's an example:
238
239   $cust_main->insert( {}, [ $email, 'POST' ] );
240
241 Currently available options are: I<depend_jobnum> and I<noexport>.
242
243 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
244 on the supplied jobnum (they will not run until the specific job completes).
245 This can be used to defer provisioning until some action completes (such
246 as running the customer's credit card sucessfully).
247
248 The I<noexport> option is deprecated.  If I<noexport> is set true, no
249 provisioning jobs (exports) are scheduled.  (You can schedule them later with
250 the B<reexport> method.)
251
252 =cut
253
254 sub insert {
255   my $self = shift;
256   my $cust_pkgs = @_ ? shift : {};
257   my $invoicing_list = @_ ? shift : '';
258   my %options = @_;
259   warn "FS::cust_main::insert called with options ".
260        join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
261     if $DEBUG;
262
263   local $SIG{HUP} = 'IGNORE';
264   local $SIG{INT} = 'IGNORE';
265   local $SIG{QUIT} = 'IGNORE';
266   local $SIG{TERM} = 'IGNORE';
267   local $SIG{TSTP} = 'IGNORE';
268   local $SIG{PIPE} = 'IGNORE';
269
270   my $oldAutoCommit = $FS::UID::AutoCommit;
271   local $FS::UID::AutoCommit = 0;
272   my $dbh = dbh;
273
274   my $prepay_credit = '';
275   my $seconds = 0;
276   if ( $self->payby eq 'PREPAY' ) {
277     $self->payby('BILL');
278     $prepay_credit = qsearchs(
279       'prepay_credit',
280       { 'identifier' => $self->payinfo },
281       '',
282       'FOR UPDATE'
283     );
284     unless ( $prepay_credit ) {
285       $dbh->rollback if $oldAutoCommit;
286       return "Invalid prepaid card: ". $self->payinfo;
287     }
288     $seconds = $prepay_credit->seconds;
289     if ( $prepay_credit->agentnum ) {
290       if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
291         $dbh->rollback if $oldAutoCommit;
292         return "prepaid card not valid for agent ". $self->agentnum;
293       }
294       $self->agentnum($prepay_credit->agentnum);
295     }
296     my $error = $prepay_credit->delete;
297     if ( $error ) {
298       $dbh->rollback if $oldAutoCommit;
299       return "removing prepay_credit (transaction rolled back): $error";
300     }
301   }
302
303   my $error = $self->SUPER::insert;
304   if ( $error ) {
305     $dbh->rollback if $oldAutoCommit;
306     #return "inserting cust_main record (transaction rolled back): $error";
307     return $error;
308   }
309
310   # invoicing list
311   if ( $invoicing_list ) {
312     $error = $self->check_invoicing_list( $invoicing_list );
313     if ( $error ) {
314       $dbh->rollback if $oldAutoCommit;
315       return "checking invoicing_list (transaction rolled back): $error";
316     }
317     $self->invoicing_list( $invoicing_list );
318   }
319
320   # packages
321   $error = $self->order_pkgs($cust_pkgs, \$seconds, %options);
322   if ( $error ) {
323     $dbh->rollback if $oldAutoCommit;
324     return $error;
325   }
326
327   if ( $seconds ) {
328     $dbh->rollback if $oldAutoCommit;
329     return "No svc_acct record to apply pre-paid time";
330   }
331
332   if ( $prepay_credit && $prepay_credit->amount ) {
333     my $cust_pay = new FS::cust_pay {
334       'custnum' => $self->custnum,
335       'paid'    => $prepay_credit->amount,
336       #'_date'   => #date the prepaid card was purchased???
337       'payby'   => 'PREP',
338       'payinfo' => $prepay_credit->identifier,
339     };
340     $error = $cust_pay->insert;
341     if ( $error ) {
342       $dbh->rollback if $oldAutoCommit;
343       return "inserting prepayment (transaction rolled back): $error";
344     }
345   }
346
347   $error = $self->queue_fuzzyfiles_update;
348   if ( $error ) {
349     $dbh->rollback if $oldAutoCommit;
350     return "updating fuzzy search cache: $error";
351   }
352
353   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
354   '';
355
356 }
357
358 =item order_pkgs HASHREF, [ SECONDSREF, [ , OPTION => VALUE ... ] ]
359
360 Like the insert method on an existing record, this method orders a package
361 and included services atomicaly.  Pass a Tie::RefHash data structure to this
362 method containing FS::cust_pkg and FS::svc_I<tablename> objects.  There should
363 be a better explanation of this, but until then, here's an example:
364
365   use Tie::RefHash;
366   tie %hash, 'Tie::RefHash'; #this part is important
367   %hash = (
368     $cust_pkg => [ $svc_acct ],
369     ...
370   );
371   $cust_main->order_pkgs( \%hash, \'0', 'noexport'=>1 );
372
373 Currently available options are: I<depend_jobnum> and I<noexport>.
374
375 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
376 on the supplied jobnum (they will not run until the specific job completes).
377 This can be used to defer provisioning until some action completes (such
378 as running the customer's credit card sucessfully).
379
380 The I<noexport> option is deprecated.  If I<noexport> is set true, no
381 provisioning jobs (exports) are scheduled.  (You can schedule them later with
382 the B<reexport> method for each cust_pkg object.  Using the B<reexport> method
383 on the cust_main object is not recommended, as existing services will also be
384 reexported.)
385
386 =cut
387
388 sub order_pkgs {
389   my $self = shift;
390   my $cust_pkgs = shift;
391   my $seconds = shift;
392   my %options = @_;
393   my %svc_options = ();
394   $svc_options{'depend_jobnum'} = $options{'depend_jobnum'}
395     if exists $options{'depend_jobnum'};
396   warn "FS::cust_main::order_pkgs called with options ".
397        join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
398     if $DEBUG;
399
400   local $SIG{HUP} = 'IGNORE';
401   local $SIG{INT} = 'IGNORE';
402   local $SIG{QUIT} = 'IGNORE';
403   local $SIG{TERM} = 'IGNORE';
404   local $SIG{TSTP} = 'IGNORE';
405   local $SIG{PIPE} = 'IGNORE';
406
407   my $oldAutoCommit = $FS::UID::AutoCommit;
408   local $FS::UID::AutoCommit = 0;
409   my $dbh = dbh;
410
411   local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
412
413   foreach my $cust_pkg ( keys %$cust_pkgs ) {
414     $cust_pkg->custnum( $self->custnum );
415     my $error = $cust_pkg->insert;
416     if ( $error ) {
417       $dbh->rollback if $oldAutoCommit;
418       return "inserting cust_pkg (transaction rolled back): $error";
419     }
420     foreach my $svc_something ( @{$cust_pkgs->{$cust_pkg}} ) {
421       $svc_something->pkgnum( $cust_pkg->pkgnum );
422       if ( $seconds && $$seconds && $svc_something->isa('FS::svc_acct') ) {
423         $svc_something->seconds( $svc_something->seconds + $$seconds );
424         $$seconds = 0;
425       }
426       $error = $svc_something->insert(%svc_options);
427       if ( $error ) {
428         $dbh->rollback if $oldAutoCommit;
429         #return "inserting svc_ (transaction rolled back): $error";
430         return $error;
431       }
432     }
433   }
434
435   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
436   ''; #no error
437 }
438
439 =item reexport
440
441 This method is deprecated.  See the I<depend_jobnum> option to the insert and
442 order_pkgs methods for a better way to defer provisioning.
443
444 Re-schedules all exports by calling the B<reexport> method of all associated
445 packages (see L<FS::cust_pkg>).  If there is an error, returns the error;
446 otherwise returns false.
447
448 =cut
449
450 sub reexport {
451   my $self = shift;
452
453   carp "warning: FS::cust_main::reexport is deprectated; ".
454        "use the depend_jobnum option to insert or order_pkgs to delay export";
455
456   local $SIG{HUP} = 'IGNORE';
457   local $SIG{INT} = 'IGNORE';
458   local $SIG{QUIT} = 'IGNORE';
459   local $SIG{TERM} = 'IGNORE';
460   local $SIG{TSTP} = 'IGNORE';
461   local $SIG{PIPE} = 'IGNORE';
462
463   my $oldAutoCommit = $FS::UID::AutoCommit;
464   local $FS::UID::AutoCommit = 0;
465   my $dbh = dbh;
466
467   foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
468     my $error = $cust_pkg->reexport;
469     if ( $error ) {
470       $dbh->rollback if $oldAutoCommit;
471       return $error;
472     }
473   }
474
475   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
476   '';
477
478 }
479
480 =item delete NEW_CUSTNUM
481
482 This deletes the customer.  If there is an error, returns the error, otherwise
483 returns false.
484
485 This will completely remove all traces of the customer record.  This is not
486 what you want when a customer cancels service; for that, cancel all of the
487 customer's packages (see L</cancel>).
488
489 If the customer has any uncancelled packages, you need to pass a new (valid)
490 customer number for those packages to be transferred to.  Cancelled packages
491 will be deleted.  Did I mention that this is NOT what you want when a customer
492 cancels service and that you really should be looking see L<FS::cust_pkg/cancel>?
493
494 You can't delete a customer with invoices (see L<FS::cust_bill>),
495 or credits (see L<FS::cust_credit>), payments (see L<FS::cust_pay>) or
496 refunds (see L<FS::cust_refund>).
497
498 =cut
499
500 sub delete {
501   my $self = shift;
502
503   local $SIG{HUP} = 'IGNORE';
504   local $SIG{INT} = 'IGNORE';
505   local $SIG{QUIT} = 'IGNORE';
506   local $SIG{TERM} = 'IGNORE';
507   local $SIG{TSTP} = 'IGNORE';
508   local $SIG{PIPE} = 'IGNORE';
509
510   my $oldAutoCommit = $FS::UID::AutoCommit;
511   local $FS::UID::AutoCommit = 0;
512   my $dbh = dbh;
513
514   if ( $self->cust_bill ) {
515     $dbh->rollback if $oldAutoCommit;
516     return "Can't delete a customer with invoices";
517   }
518   if ( $self->cust_credit ) {
519     $dbh->rollback if $oldAutoCommit;
520     return "Can't delete a customer with credits";
521   }
522   if ( $self->cust_pay ) {
523     $dbh->rollback if $oldAutoCommit;
524     return "Can't delete a customer with payments";
525   }
526   if ( $self->cust_refund ) {
527     $dbh->rollback if $oldAutoCommit;
528     return "Can't delete a customer with refunds";
529   }
530
531   my @cust_pkg = $self->ncancelled_pkgs;
532   if ( @cust_pkg ) {
533     my $new_custnum = shift;
534     unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
535       $dbh->rollback if $oldAutoCommit;
536       return "Invalid new customer number: $new_custnum";
537     }
538     foreach my $cust_pkg ( @cust_pkg ) {
539       my %hash = $cust_pkg->hash;
540       $hash{'custnum'} = $new_custnum;
541       my $new_cust_pkg = new FS::cust_pkg ( \%hash );
542       my $error = $new_cust_pkg->replace($cust_pkg);
543       if ( $error ) {
544         $dbh->rollback if $oldAutoCommit;
545         return $error;
546       }
547     }
548   }
549   my @cancelled_cust_pkg = $self->all_pkgs;
550   foreach my $cust_pkg ( @cancelled_cust_pkg ) {
551     my $error = $cust_pkg->delete;
552     if ( $error ) {
553       $dbh->rollback if $oldAutoCommit;
554       return $error;
555     }
556   }
557
558   foreach my $cust_main_invoice ( #(email invoice destinations, not invoices)
559     qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } )
560   ) {
561     my $error = $cust_main_invoice->delete;
562     if ( $error ) {
563       $dbh->rollback if $oldAutoCommit;
564       return $error;
565     }
566   }
567
568   my $error = $self->SUPER::delete;
569   if ( $error ) {
570     $dbh->rollback if $oldAutoCommit;
571     return $error;
572   }
573
574   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
575   '';
576
577 }
578
579 =item replace OLD_RECORD [ INVOICING_LIST_ARYREF ]
580
581 Replaces the OLD_RECORD with this one in the database.  If there is an error,
582 returns the error, otherwise returns false.
583
584 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
585 be set as the invoicing list (see L<"invoicing_list">).  Errors return as
586 expected and rollback the entire transaction; it is not necessary to call 
587 check_invoicing_list first.  Here's an example:
588
589   $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
590
591 =cut
592
593 sub replace {
594   my $self = shift;
595   my $old = shift;
596   my @param = @_;
597
598   local $SIG{HUP} = 'IGNORE';
599   local $SIG{INT} = 'IGNORE';
600   local $SIG{QUIT} = 'IGNORE';
601   local $SIG{TERM} = 'IGNORE';
602   local $SIG{TSTP} = 'IGNORE';
603   local $SIG{PIPE} = 'IGNORE';
604
605   if ( $self->payby eq 'COMP' && $self->payby ne $old->payby
606        && $conf->config('users-allow_comp')                  ) {
607     return "You are not permitted to create complimentary accounts."
608       unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
609   }
610
611   my $oldAutoCommit = $FS::UID::AutoCommit;
612   local $FS::UID::AutoCommit = 0;
613   my $dbh = dbh;
614
615   my $error = $self->SUPER::replace($old);
616
617   if ( $error ) {
618     $dbh->rollback if $oldAutoCommit;
619     return $error;
620   }
621
622   if ( @param ) { # INVOICING_LIST_ARYREF
623     my $invoicing_list = shift @param;
624     $error = $self->check_invoicing_list( $invoicing_list );
625     if ( $error ) {
626       $dbh->rollback if $oldAutoCommit;
627       return $error;
628     }
629     $self->invoicing_list( $invoicing_list );
630   }
631
632   if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
633        grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
634     # card/check/lec info has changed, want to retry realtime_ invoice events
635     my $error = $self->retry_realtime;
636     if ( $error ) {
637       $dbh->rollback if $oldAutoCommit;
638       return $error;
639     }
640   }
641
642   $error = $self->queue_fuzzyfiles_update;
643   if ( $error ) {
644     $dbh->rollback if $oldAutoCommit;
645     return "updating fuzzy search cache: $error";
646   }
647
648   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
649   '';
650
651 }
652
653 =item queue_fuzzyfiles_update
654
655 Used by insert & replace to update the fuzzy search cache
656
657 =cut
658
659 sub queue_fuzzyfiles_update {
660   my $self = shift;
661
662   local $SIG{HUP} = 'IGNORE';
663   local $SIG{INT} = 'IGNORE';
664   local $SIG{QUIT} = 'IGNORE';
665   local $SIG{TERM} = 'IGNORE';
666   local $SIG{TSTP} = 'IGNORE';
667   local $SIG{PIPE} = 'IGNORE';
668
669   my $oldAutoCommit = $FS::UID::AutoCommit;
670   local $FS::UID::AutoCommit = 0;
671   my $dbh = dbh;
672
673   my $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
674   my $error = $queue->insert($self->getfield('last'), $self->company);
675   if ( $error ) {
676     $dbh->rollback if $oldAutoCommit;
677     return "queueing job (transaction rolled back): $error";
678   }
679
680   if ( defined $self->dbdef_table->column('ship_last') && $self->ship_last ) {
681     $queue = new FS::queue { 'job' => 'FS::cust_main::append_fuzzyfiles' };
682     $error = $queue->insert($self->getfield('ship_last'), $self->ship_company);
683     if ( $error ) {
684       $dbh->rollback if $oldAutoCommit;
685       return "queueing job (transaction rolled back): $error";
686     }
687   }
688
689   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
690   '';
691
692 }
693
694 =item check
695
696 Checks all fields to make sure this is a valid customer record.  If there is
697 an error, returns the error, otherwise returns false.  Called by the insert
698 and repalce methods.
699
700 =cut
701
702 sub check {
703   my $self = shift;
704
705   #warn "BEFORE: \n". $self->_dump;
706
707   my $error =
708     $self->ut_numbern('custnum')
709     || $self->ut_number('agentnum')
710     || $self->ut_number('refnum')
711     || $self->ut_name('last')
712     || $self->ut_name('first')
713     || $self->ut_textn('company')
714     || $self->ut_text('address1')
715     || $self->ut_textn('address2')
716     || $self->ut_text('city')
717     || $self->ut_textn('county')
718     || $self->ut_textn('state')
719     || $self->ut_country('country')
720     || $self->ut_anything('comments')
721     || $self->ut_numbern('referral_custnum')
722   ;
723   #barf.  need message catalogs.  i18n.  etc.
724   $error .= "Please select an advertising source."
725     if $error =~ /^Illegal or empty \(numeric\) refnum: /;
726   return $error if $error;
727
728   return "Unknown agent"
729     unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
730
731   return "Unknown refnum"
732     unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
733
734   return "Unknown referring custnum ". $self->referral_custnum
735     unless ! $self->referral_custnum 
736            || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
737
738   if ( $self->ss eq '' ) {
739     $self->ss('');
740   } else {
741     my $ss = $self->ss;
742     $ss =~ s/\D//g;
743     $ss =~ /^(\d{3})(\d{2})(\d{4})$/
744       or return "Illegal social security number: ". $self->ss;
745     $self->ss("$1-$2-$3");
746   }
747
748
749 # bad idea to disable, causes billing to fail because of no tax rates later
750 #  unless ( $import ) {
751     unless ( qsearch('cust_main_county', {
752       'country' => $self->country,
753       'state'   => '',
754      } ) ) {
755       return "Unknown state/county/country: ".
756         $self->state. "/". $self->county. "/". $self->country
757         unless qsearch('cust_main_county',{
758           'state'   => $self->state,
759           'county'  => $self->county,
760           'country' => $self->country,
761         } );
762     }
763 #  }
764
765   $error =
766     $self->ut_phonen('daytime', $self->country)
767     || $self->ut_phonen('night', $self->country)
768     || $self->ut_phonen('fax', $self->country)
769     || $self->ut_zip('zip', $self->country)
770   ;
771   return $error if $error;
772
773   my @addfields = qw(
774     last first company address1 address2 city county state zip
775     country daytime night fax
776   );
777
778   if ( defined $self->dbdef_table->column('ship_last') ) {
779     if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
780                        @addfields )
781          && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
782        )
783     {
784       my $error =
785         $self->ut_name('ship_last')
786         || $self->ut_name('ship_first')
787         || $self->ut_textn('ship_company')
788         || $self->ut_text('ship_address1')
789         || $self->ut_textn('ship_address2')
790         || $self->ut_text('ship_city')
791         || $self->ut_textn('ship_county')
792         || $self->ut_textn('ship_state')
793         || $self->ut_country('ship_country')
794       ;
795       return $error if $error;
796
797       #false laziness with above
798       unless ( qsearchs('cust_main_county', {
799         'country' => $self->ship_country,
800         'state'   => '',
801        } ) ) {
802         return "Unknown ship_state/ship_county/ship_country: ".
803           $self->ship_state. "/". $self->ship_county. "/". $self->ship_country
804           unless qsearchs('cust_main_county',{
805             'state'   => $self->ship_state,
806             'county'  => $self->ship_county,
807             'country' => $self->ship_country,
808           } );
809       }
810       #eofalse
811
812       $error =
813         $self->ut_phonen('ship_daytime', $self->ship_country)
814         || $self->ut_phonen('ship_night', $self->ship_country)
815         || $self->ut_phonen('ship_fax', $self->ship_country)
816         || $self->ut_zip('ship_zip', $self->ship_country)
817       ;
818       return $error if $error;
819
820     } else { # ship_ info eq billing info, so don't store dup info in database
821       $self->setfield("ship_$_", '')
822         foreach qw( last first company address1 address2 city county state zip
823                     country daytime night fax );
824     }
825   }
826
827   $self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY)$/
828     or return "Illegal payby: ". $self->payby;
829   $self->payby($1);
830
831   if ( $self->payby eq 'CARD' || $self->payby eq 'DCRD' ) {
832
833     my $payinfo = $self->payinfo;
834     $payinfo =~ s/\D//g;
835     $payinfo =~ /^(\d{13,16})$/
836       or return gettext('invalid_card'); # . ": ". $self->payinfo;
837     $payinfo = $1;
838     $self->payinfo($payinfo);
839     validate($payinfo)
840       or return gettext('invalid_card'); # . ": ". $self->payinfo;
841     return gettext('unknown_card_type')
842       if cardtype($self->payinfo) eq "Unknown";
843     if ( defined $self->dbdef_table->column('paycvv') ) {
844       if ( length($self->paycvv) ) {
845         if ( cardtype($self->payinfo) eq 'American Express card' ) {
846           $self->paycvv =~ /^(\d{4})$/
847             or return "CVV2 (CID) for American Express cards is four digits.";
848           $self->paycvv($1);
849         } else {
850           $self->paycvv =~ /^(\d{3})$/
851             or return "CVV2 (CVC2/CID) is three digits.";
852           $self->paycvv($1);
853         }
854       } else {
855         $self->paycvv('');
856       }
857     }
858
859   } elsif ( $self->payby eq 'CHEK' || $self->payby eq 'DCHK' ) {
860
861     my $payinfo = $self->payinfo;
862     $payinfo =~ s/[^\d\@]//g;
863     $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
864     $payinfo = "$1\@$2";
865     $self->payinfo($payinfo);
866     $self->paycvv('') if $self->dbdef_table->column('paycvv');
867
868   } elsif ( $self->payby eq 'LECB' ) {
869
870     my $payinfo = $self->payinfo;
871     $payinfo =~ s/\D//g;
872     $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
873     $payinfo = $1;
874     $self->payinfo($payinfo);
875     $self->paycvv('') if $self->dbdef_table->column('paycvv');
876
877   } elsif ( $self->payby eq 'BILL' ) {
878
879     $error = $self->ut_textn('payinfo');
880     return "Illegal P.O. number: ". $self->payinfo if $error;
881     $self->paycvv('') if $self->dbdef_table->column('paycvv');
882
883   } elsif ( $self->payby eq 'COMP' ) {
884
885     if ( !$self->custnum && $conf->config('users-allow_comp') ) {
886       return "You are not permitted to create complimentary accounts."
887         unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
888     }
889
890     $error = $self->ut_textn('payinfo');
891     return "Illegal comp account issuer: ". $self->payinfo if $error;
892     $self->paycvv('') if $self->dbdef_table->column('paycvv');
893
894   } elsif ( $self->payby eq 'PREPAY' ) {
895
896     my $payinfo = $self->payinfo;
897     $payinfo =~ s/\W//g; #anything else would just confuse things
898     $self->payinfo($payinfo);
899     $error = $self->ut_alpha('payinfo');
900     return "Illegal prepayment identifier: ". $self->payinfo if $error;
901     return "Unknown prepayment identifier"
902       unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
903     $self->paycvv('') if $self->dbdef_table->column('paycvv');
904
905   }
906
907   if ( $self->paydate eq '' || $self->paydate eq '-' ) {
908     return "Expriation date required"
909       unless $self->payby =~ /^(BILL|PREPAY|CHEK|LECB)$/;
910     $self->paydate('');
911   } else {
912     my( $m, $y );
913     if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
914       ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
915     } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
916       ( $m, $y ) = ( $3, "20$2" );
917     } else {
918       return "Illegal expiration date: ". $self->paydate;
919     }
920     $self->paydate("$y-$m-01");
921     my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
922     return gettext('expired_card')
923       if !$import && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
924   }
925
926   if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
927        ( ! $conf->exists('require_cardname')
928          || $self->payby !~ /^(CARD|DCRD)$/  ) 
929   ) {
930     $self->payname( $self->first. " ". $self->getfield('last') );
931   } else {
932     $self->payname =~ /^([\w \,\.\-\']+)$/
933       or return gettext('illegal_name'). " payname: ". $self->payname;
934     $self->payname($1);
935   }
936
937   $self->tax =~ /^(Y?)$/ or return "Illegal tax: ". $self->tax;
938   $self->tax($1);
939
940   $self->otaker(getotaker) unless $self->otaker;
941
942   #warn "AFTER: \n". $self->_dump;
943
944   $self->SUPER::check;
945 }
946
947 =item all_pkgs
948
949 Returns all packages (see L<FS::cust_pkg>) for this customer.
950
951 =cut
952
953 sub all_pkgs {
954   my $self = shift;
955   if ( $self->{'_pkgnum'} ) {
956     values %{ $self->{'_pkgnum'}->cache };
957   } else {
958     qsearch( 'cust_pkg', { 'custnum' => $self->custnum });
959   }
960 }
961
962 =item ncancelled_pkgs
963
964 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
965
966 =cut
967
968 sub ncancelled_pkgs {
969   my $self = shift;
970   if ( $self->{'_pkgnum'} ) {
971     grep { ! $_->getfield('cancel') } values %{ $self->{'_pkgnum'}->cache };
972   } else {
973     @{ [ # force list context
974       qsearch( 'cust_pkg', {
975         'custnum' => $self->custnum,
976         'cancel'  => '',
977       }),
978       qsearch( 'cust_pkg', {
979         'custnum' => $self->custnum,
980         'cancel'  => 0,
981       }),
982     ] };
983   }
984 }
985
986 =item suspended_pkgs
987
988 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
989
990 =cut
991
992 sub suspended_pkgs {
993   my $self = shift;
994   grep { $_->susp } $self->ncancelled_pkgs;
995 }
996
997 =item unflagged_suspended_pkgs
998
999 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
1000 customer (thouse packages without the `manual_flag' set).
1001
1002 =cut
1003
1004 sub unflagged_suspended_pkgs {
1005   my $self = shift;
1006   return $self->suspended_pkgs
1007     unless dbdef->table('cust_pkg')->column('manual_flag');
1008   grep { ! $_->manual_flag } $self->suspended_pkgs;
1009 }
1010
1011 =item unsuspended_pkgs
1012
1013 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
1014 this customer.
1015
1016 =cut
1017
1018 sub unsuspended_pkgs {
1019   my $self = shift;
1020   grep { ! $_->susp } $self->ncancelled_pkgs;
1021 }
1022
1023 =item num_cancelled_pkgs
1024
1025 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
1026 customer.
1027
1028 =cut
1029
1030 sub num_cancelled_pkgs {
1031   my $self = shift;
1032   $self->num_pkgs("cancel IS NOT NULL AND cust_pkg.cancel != 0");
1033 }
1034
1035 sub num_pkgs {
1036   my( $self, $sql ) = @_;
1037   my $sth = dbh->prepare(
1038     "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? AND $sql"
1039   ) or die dbh->errstr;
1040   $sth->execute($self->custnum) or die $sth->errstr;
1041   $sth->fetchrow_arrayref->[0];
1042 }
1043
1044 =item unsuspend
1045
1046 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
1047 and L<FS::cust_pkg>) for this customer.  Always returns a list: an empty list
1048 on success or a list of errors.
1049
1050 =cut
1051
1052 sub unsuspend {
1053   my $self = shift;
1054   grep { $_->unsuspend } $self->suspended_pkgs;
1055 }
1056
1057 =item suspend
1058
1059 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
1060 Always returns a list: an empty list on success or a list of errors.
1061
1062 =cut
1063
1064 sub suspend {
1065   my $self = shift;
1066   grep { $_->suspend } $self->unsuspended_pkgs;
1067 }
1068
1069 =item suspend_if_pkgpart PKGPART [ , PKGPART ... ]
1070
1071 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
1072 PKGPARTs (see L<FS::part_pkg>).  Always returns a list: an empty list on
1073 success or a list of errors.
1074
1075 =cut
1076
1077 sub suspend_if_pkgpart {
1078   my $self = shift;
1079   my @pkgparts = @_;
1080   grep { $_->suspend }
1081     grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
1082       $self->unsuspended_pkgs;
1083 }
1084
1085 =item suspend_unless_pkgpart PKGPART [ , PKGPART ... ]
1086
1087 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
1088 listed PKGPARTs (see L<FS::part_pkg>).  Always returns a list: an empty list
1089 on success or a list of errors.
1090
1091 =cut
1092
1093 sub suspend_unless_pkgpart {
1094   my $self = shift;
1095   my @pkgparts = @_;
1096   grep { $_->suspend }
1097     grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
1098       $self->unsuspended_pkgs;
1099 }
1100
1101 =item cancel [ OPTION => VALUE ... ]
1102
1103 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
1104
1105 Available options are: I<quiet>
1106
1107 I<quiet> can be set true to supress email cancellation notices.
1108
1109 Always returns a list: an empty list on success or a list of errors.
1110
1111 =cut
1112
1113 sub cancel {
1114   my $self = shift;
1115   grep { $_ } map { $_->cancel(@_) } $self->ncancelled_pkgs;
1116 }
1117
1118 =item agent
1119
1120 Returns the agent (see L<FS::agent>) for this customer.
1121
1122 =cut
1123
1124 sub agent {
1125   my $self = shift;
1126   qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1127 }
1128
1129 =item bill OPTIONS
1130
1131 Generates invoices (see L<FS::cust_bill>) for this customer.  Usually used in
1132 conjunction with the collect method.
1133
1134 Options are passed as name-value pairs.
1135
1136 Currently available options are:
1137
1138 resetup - if set true, re-charges setup fees.
1139
1140 time - bills the customer as if it were that time.  Specified as a UNIX
1141 timestamp; see L<perlfunc/"time">).  Also see L<Time::Local> and
1142 L<Date::Parse> for conversion functions.  For example:
1143
1144  use Date::Parse;
1145  ...
1146  $cust_main->bill( 'time' => str2time('April 20th, 2001') );
1147
1148
1149 If there is an error, returns the error, otherwise returns false.
1150
1151 =cut
1152
1153 sub bill {
1154   my( $self, %options ) = @_;
1155   return '' if $self->payby eq 'COMP';
1156   warn "bill customer ". $self->custnum if $DEBUG;
1157
1158   my $time = $options{'time'} || time;
1159
1160   my $error;
1161
1162   #put below somehow?
1163   local $SIG{HUP} = 'IGNORE';
1164   local $SIG{INT} = 'IGNORE';
1165   local $SIG{QUIT} = 'IGNORE';
1166   local $SIG{TERM} = 'IGNORE';
1167   local $SIG{TSTP} = 'IGNORE';
1168   local $SIG{PIPE} = 'IGNORE';
1169
1170   my $oldAutoCommit = $FS::UID::AutoCommit;
1171   local $FS::UID::AutoCommit = 0;
1172   my $dbh = dbh;
1173
1174   $self->select_for_update; #mutex
1175
1176   # find the packages which are due for billing, find out how much they are
1177   # & generate invoice database.
1178  
1179   my( $total_setup, $total_recur ) = ( 0, 0 );
1180   #my( $taxable_setup, $taxable_recur ) = ( 0, 0 );
1181   my @cust_bill_pkg = ();
1182   #my $tax = 0;##
1183   #my $taxable_charged = 0;##
1184   #my $charged = 0;##
1185
1186   my %tax;
1187
1188   foreach my $cust_pkg (
1189     qsearch('cust_pkg', { 'custnum' => $self->custnum } )
1190   ) {
1191
1192     #NO!! next if $cust_pkg->cancel;  
1193     next if $cust_pkg->getfield('cancel');  
1194
1195     warn "  bill package ". $cust_pkg->pkgnum if $DEBUG;
1196
1197     #? to avoid use of uninitialized value errors... ?
1198     $cust_pkg->setfield('bill', '')
1199       unless defined($cust_pkg->bill);
1200  
1201     my $part_pkg = $cust_pkg->part_pkg;
1202
1203     my %hash = $cust_pkg->hash;
1204     my $old_cust_pkg = new FS::cust_pkg \%hash;
1205
1206     my @details = ();
1207
1208     # bill setup
1209     my $setup = 0;
1210     if ( !$cust_pkg->setup || $options{'resetup'} ) {
1211     
1212       warn "    bill setup" if $DEBUG;
1213
1214       $setup = eval { $cust_pkg->calc_setup( $time ) };
1215       if ( $@ ) {
1216         $dbh->rollback if $oldAutoCommit;
1217         return $@;
1218       }
1219
1220       $cust_pkg->setfield('setup', $time) unless $cust_pkg->setup;
1221     }
1222
1223     #bill recurring fee
1224     my $recur = 0;
1225     my $sdate;
1226     if ( $part_pkg->getfield('freq') ne '0' &&
1227          ! $cust_pkg->getfield('susp') &&
1228          ( $cust_pkg->getfield('bill') || 0 ) <= $time
1229     ) {
1230
1231       warn "    bill recur" if $DEBUG;
1232
1233       # XXX shared with $recur_prog
1234       $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1235
1236       $recur = eval { $cust_pkg->calc_recur( \$sdate, \@details ) };
1237       if ( $@ ) {
1238         $dbh->rollback if $oldAutoCommit;
1239         return $@;
1240       }
1241
1242       #change this bit to use Date::Manip? CAREFUL with timezones (see
1243       # mailing list archive)
1244       my ($sec,$min,$hour,$mday,$mon,$year) =
1245         (localtime($sdate) )[0,1,2,3,4,5];
1246
1247       #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
1248       # only for figuring next bill date, nothing else, so, reset $sdate again
1249       # here
1250       $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
1251       $cust_pkg->last_bill($sdate)
1252         if $cust_pkg->dbdef_table->column('last_bill');
1253
1254       if ( $part_pkg->freq =~ /^\d+$/ ) {
1255         $mon += $part_pkg->freq;
1256         until ( $mon < 12 ) { $mon -= 12; $year++; }
1257       } elsif ( $part_pkg->freq =~ /^(\d+)w$/ ) {
1258         my $weeks = $1;
1259         $mday += $weeks * 7;
1260       } elsif ( $part_pkg->freq =~ /^(\d+)d$/ ) {
1261         my $days = $1;
1262         $mday += $days;
1263       } else {
1264         $dbh->rollback if $oldAutoCommit;
1265         return "unparsable frequency: ". $part_pkg->freq;
1266       }
1267       $cust_pkg->setfield('bill',
1268         timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year));
1269     }
1270
1271     warn "\$setup is undefined" unless defined($setup);
1272     warn "\$recur is undefined" unless defined($recur);
1273     warn "\$cust_pkg->bill is undefined" unless defined($cust_pkg->bill);
1274
1275     if ( $cust_pkg->modified ) {
1276
1277       warn "  package ". $cust_pkg->pkgnum. " modified; updating\n" if $DEBUG;
1278
1279       $error=$cust_pkg->replace($old_cust_pkg);
1280       if ( $error ) { #just in case
1281         $dbh->rollback if $oldAutoCommit;
1282         return "Error modifying pkgnum ". $cust_pkg->pkgnum. ": $error";
1283       }
1284
1285       $setup = sprintf( "%.2f", $setup );
1286       $recur = sprintf( "%.2f", $recur );
1287       if ( $setup < 0 && ! $conf->exists('allow_negative_charges') ) {
1288         $dbh->rollback if $oldAutoCommit;
1289         return "negative setup $setup for pkgnum ". $cust_pkg->pkgnum;
1290       }
1291       if ( $recur < 0 && ! $conf->exists('allow_negative_charges') ) {
1292         $dbh->rollback if $oldAutoCommit;
1293         return "negative recur $recur for pkgnum ". $cust_pkg->pkgnum;
1294       }
1295       if ( $setup != 0 || $recur != 0 ) {
1296         warn "    charges (setup=$setup, recur=$recur); queueing line items\n"
1297           if $DEBUG;
1298         my $cust_bill_pkg = new FS::cust_bill_pkg ({
1299           'pkgnum'  => $cust_pkg->pkgnum,
1300           'setup'   => $setup,
1301           'recur'   => $recur,
1302           'sdate'   => $sdate,
1303           'edate'   => $cust_pkg->bill,
1304           'details' => \@details,
1305         });
1306         push @cust_bill_pkg, $cust_bill_pkg;
1307         $total_setup += $setup;
1308         $total_recur += $recur;
1309
1310         unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
1311
1312           my @taxes = qsearch( 'cust_main_county', {
1313                                  'state'    => $self->state,
1314                                  'county'   => $self->county,
1315                                  'country'  => $self->country,
1316                                  'taxclass' => $part_pkg->taxclass,
1317                                                                       } );
1318           unless ( @taxes ) {
1319             @taxes =  qsearch( 'cust_main_county', {
1320                                   'state'    => $self->state,
1321                                   'county'   => $self->county,
1322                                   'country'  => $self->country,
1323                                   'taxclass' => '',
1324                                                                       } );
1325           }
1326
1327           #one more try at a whole-country tax rate
1328           unless ( @taxes ) {
1329             @taxes =  qsearch( 'cust_main_county', {
1330                                   'state'    => '',
1331                                   'county'   => '',
1332                                   'country'  => $self->country,
1333                                   'taxclass' => '',
1334                                                                       } );
1335           }
1336
1337           # maybe eliminate this entirely, along with all the 0% records
1338           unless ( @taxes ) {
1339             $dbh->rollback if $oldAutoCommit;
1340             return
1341               "fatal: can't find tax rate for state/county/country/taxclass ".
1342               join('/', ( map $self->$_(), qw(state county country) ),
1343                         $part_pkg->taxclass ).  "\n";
1344           }
1345   
1346           foreach my $tax ( @taxes ) {
1347
1348             my $taxable_charged = 0;
1349             $taxable_charged += $setup
1350               unless $part_pkg->setuptax =~ /^Y$/i
1351                   || $tax->setuptax =~ /^Y$/i;
1352             $taxable_charged += $recur
1353               unless $part_pkg->recurtax =~ /^Y$/i
1354                   || $tax->recurtax =~ /^Y$/i;
1355             next unless $taxable_charged;
1356
1357             if ( $tax->exempt_amount && $tax->exempt_amount > 0 ) {
1358               my ($mon,$year) = (localtime($sdate) )[4,5];
1359               $mon++;
1360               my $freq = $part_pkg->freq || 1;
1361               if ( $freq !~ /(\d+)$/ ) {
1362                 $dbh->rollback if $oldAutoCommit;
1363                 return "daily/weekly package definitions not (yet?)".
1364                        " compatible with monthly tax exemptions";
1365               }
1366               my $taxable_per_month = sprintf("%.2f", $taxable_charged / $freq );
1367               foreach my $which_month ( 1 .. $freq ) {
1368                 my %hash = (
1369                   'custnum' => $self->custnum,
1370                   'taxnum'  => $tax->taxnum,
1371                   'year'    => 1900+$year,
1372                   'month'   => $mon++,
1373                 );
1374                 #until ( $mon < 12 ) { $mon -= 12; $year++; }
1375                 until ( $mon < 13 ) { $mon -= 12; $year++; }
1376                 my $cust_tax_exempt =
1377                   qsearchs('cust_tax_exempt', \%hash)
1378                   || new FS::cust_tax_exempt( { %hash, 'amount' => 0 } );
1379                 my $remaining_exemption = sprintf("%.2f",
1380                   $tax->exempt_amount - $cust_tax_exempt->amount );
1381                 if ( $remaining_exemption > 0 ) {
1382                   my $addl = $remaining_exemption > $taxable_per_month
1383                     ? $taxable_per_month
1384                     : $remaining_exemption;
1385                   $taxable_charged -= $addl;
1386                   my $new_cust_tax_exempt = new FS::cust_tax_exempt ( {
1387                     $cust_tax_exempt->hash,
1388                     'amount' =>
1389                       sprintf("%.2f", $cust_tax_exempt->amount + $addl),
1390                   } );
1391                   $error = $new_cust_tax_exempt->exemptnum
1392                     ? $new_cust_tax_exempt->replace($cust_tax_exempt)
1393                     : $new_cust_tax_exempt->insert;
1394                   if ( $error ) {
1395                     $dbh->rollback if $oldAutoCommit;
1396                     return "fatal: can't update cust_tax_exempt: $error";
1397                   }
1398   
1399                 } # if $remaining_exemption > 0
1400   
1401               } #foreach $which_month
1402   
1403             } #if $tax->exempt_amount
1404
1405             $taxable_charged = sprintf( "%.2f", $taxable_charged);
1406
1407             #$tax += $taxable_charged * $cust_main_county->tax / 100
1408             $tax{ $tax->taxname || 'Tax' } +=
1409               $taxable_charged * $tax->tax / 100
1410
1411           } #foreach my $tax ( @taxes )
1412
1413         } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
1414
1415       } #if $setup != 0 || $recur != 0
1416       
1417     } #if $cust_pkg->modified
1418
1419   } #foreach my $cust_pkg
1420
1421   my $charged = sprintf( "%.2f", $total_setup + $total_recur );
1422 #  my $taxable_charged = sprintf( "%.2f", $taxable_setup + $taxable_recur );
1423
1424   unless ( @cust_bill_pkg ) { #don't create invoices with no line items
1425     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1426     return '';
1427   } 
1428
1429 #  unless ( $self->tax =~ /Y/i
1430 #           || $self->payby eq 'COMP'
1431 #           || $taxable_charged == 0 ) {
1432 #    my $cust_main_county = qsearchs('cust_main_county',{
1433 #        'state'   => $self->state,
1434 #        'county'  => $self->county,
1435 #        'country' => $self->country,
1436 #    } ) or die "fatal: can't find tax rate for state/county/country ".
1437 #               $self->state. "/". $self->county. "/". $self->country. "\n";
1438 #    my $tax = sprintf( "%.2f",
1439 #      $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
1440 #    );
1441
1442   if ( dbdef->table('cust_bill_pkg')->column('itemdesc') ) { #1.5 schema
1443
1444     foreach my $taxname ( grep { $tax{$_} > 0 } keys %tax ) {
1445       my $tax = sprintf("%.2f", $tax{$taxname} );
1446       $charged = sprintf( "%.2f", $charged+$tax );
1447   
1448       my $cust_bill_pkg = new FS::cust_bill_pkg ({
1449         'pkgnum'   => 0,
1450         'setup'    => $tax,
1451         'recur'    => 0,
1452         'sdate'    => '',
1453         'edate'    => '',
1454         'itemdesc' => $taxname,
1455       });
1456       push @cust_bill_pkg, $cust_bill_pkg;
1457     }
1458   
1459   } else { #1.4 schema
1460
1461     my $tax = 0;
1462     foreach ( values %tax ) { $tax += $_ };
1463     $tax = sprintf("%.2f", $tax);
1464     if ( $tax > 0 ) {
1465       $charged = sprintf( "%.2f", $charged+$tax );
1466
1467       my $cust_bill_pkg = new FS::cust_bill_pkg ({
1468         'pkgnum' => 0,
1469         'setup'  => $tax,
1470         'recur'  => 0,
1471         'sdate'  => '',
1472         'edate'  => '',
1473       });
1474       push @cust_bill_pkg, $cust_bill_pkg;
1475     }
1476
1477   }
1478
1479   my $cust_bill = new FS::cust_bill ( {
1480     'custnum' => $self->custnum,
1481     '_date'   => $time,
1482     'charged' => $charged,
1483   } );
1484   $error = $cust_bill->insert;
1485   if ( $error ) {
1486     $dbh->rollback if $oldAutoCommit;
1487     return "can't create invoice for customer #". $self->custnum. ": $error";
1488   }
1489
1490   my $invnum = $cust_bill->invnum;
1491   my $cust_bill_pkg;
1492   foreach $cust_bill_pkg ( @cust_bill_pkg ) {
1493     #warn $invnum;
1494     $cust_bill_pkg->invnum($invnum);
1495     $error = $cust_bill_pkg->insert;
1496     if ( $error ) {
1497       $dbh->rollback if $oldAutoCommit;
1498       return "can't create invoice line item for customer #". $self->custnum.
1499              ": $error";
1500     }
1501   }
1502   
1503   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1504   ''; #no error
1505 }
1506
1507 =item collect OPTIONS
1508
1509 (Attempt to) collect money for this customer's outstanding invoices (see
1510 L<FS::cust_bill>).  Usually used after the bill method.
1511
1512 Depending on the value of `payby', this may print or email an invoice (I<BILL>,
1513 I<DCRD>, or I<DCHK>), charge a credit card (I<CARD>), charge via electronic
1514 check/ACH (I<CHEK>), or just add any necessary (pseudo-)payment (I<COMP>).
1515
1516 Most actions are now triggered by invoice events; see L<FS::part_bill_event>
1517 and the invoice events web interface.
1518
1519 If there is an error, returns the error, otherwise returns false.
1520
1521 Options are passed as name-value pairs.
1522
1523 Currently available options are:
1524
1525 invoice_time - Use this time when deciding when to print invoices and
1526 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>
1527 for conversion functions.
1528
1529 retry - Retry card/echeck/LEC transactions even when not scheduled by invoice
1530 events.
1531
1532 retry_card - Deprecated alias for 'retry'
1533
1534 batch_card - This option is deprecated.  See the invoice events web interface
1535 to control whether cards are batched or run against a realtime gateway.
1536
1537 report_badcard - This option is deprecated.
1538
1539 force_print - This option is deprecated; see the invoice events web interface.
1540
1541 quiet - set true to surpress email card/ACH decline notices.
1542
1543 =cut
1544
1545 sub collect {
1546   my( $self, %options ) = @_;
1547   my $invoice_time = $options{'invoice_time'} || time;
1548
1549   #put below somehow?
1550   local $SIG{HUP} = 'IGNORE';
1551   local $SIG{INT} = 'IGNORE';
1552   local $SIG{QUIT} = 'IGNORE';
1553   local $SIG{TERM} = 'IGNORE';
1554   local $SIG{TSTP} = 'IGNORE';
1555   local $SIG{PIPE} = 'IGNORE';
1556
1557   my $oldAutoCommit = $FS::UID::AutoCommit;
1558   local $FS::UID::AutoCommit = 0;
1559   my $dbh = dbh;
1560
1561   $self->select_for_update; #mutex
1562
1563   my $balance = $self->balance;
1564   warn "collect customer ". $self->custnum. ": balance $balance" if $DEBUG;
1565   unless ( $balance > 0 ) { #redundant?????
1566     $dbh->rollback if $oldAutoCommit; #hmm
1567     return '';
1568   }
1569
1570   if ( exists($options{'retry_card'}) ) {
1571     carp 'retry_card option passed to collect is deprecated; use retry';
1572     $options{'retry'} ||= $options{'retry_card'};
1573   }
1574   if ( exists($options{'retry'}) && $options{'retry'} ) {
1575     my $error = $self->retry_realtime;
1576     if ( $error ) {
1577       $dbh->rollback if $oldAutoCommit;
1578       return $error;
1579     }
1580   }
1581
1582   foreach my $cust_bill ( $self->open_cust_bill ) {
1583
1584     # don't try to charge for the same invoice if it's already in a batch
1585     #next if qsearchs( 'cust_pay_batch', { 'invnum' => $cust_bill->invnum } );
1586
1587     last if $self->balance <= 0;
1588
1589     warn "invnum ". $cust_bill->invnum. " (owed ". $cust_bill->owed. ")"
1590       if $DEBUG;
1591
1592     foreach my $part_bill_event (
1593       sort {    $a->seconds   <=> $b->seconds
1594              || $a->weight    <=> $b->weight
1595              || $a->eventpart <=> $b->eventpart }
1596         grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
1597                && ! qsearch( 'cust_bill_event', {
1598                                 'invnum'    => $cust_bill->invnum,
1599                                 'eventpart' => $_->eventpart,
1600                                 'status'    => 'done',
1601                                                                    } )
1602              }
1603           qsearch('part_bill_event', { 'payby'    => $self->payby,
1604                                        'disabled' => '',           } )
1605     ) {
1606
1607       last if $cust_bill->owed <= 0  # don't run subsequent events if owed<=0
1608            || $self->balance   <= 0; # or if balance<=0
1609
1610       warn "calling invoice event (". $part_bill_event->eventcode. ")\n"
1611         if $DEBUG;
1612       my $cust_main = $self; #for callback
1613
1614       my $error;
1615       {
1616         local $realtime_bop_decline_quiet = 1 if $options{'quiet'};
1617         local $SIG{__DIE__}; # don't want Mason __DIE__ handler active
1618         $error = eval $part_bill_event->eventcode;
1619       }
1620
1621       my $status = '';
1622       my $statustext = '';
1623       if ( $@ ) {
1624         $status = 'failed';
1625         $statustext = $@;
1626       } elsif ( $error ) {
1627         $status = 'done';
1628         $statustext = $error;
1629       } else {
1630         $status = 'done'
1631       }
1632
1633       #add cust_bill_event
1634       my $cust_bill_event = new FS::cust_bill_event {
1635         'invnum'     => $cust_bill->invnum,
1636         'eventpart'  => $part_bill_event->eventpart,
1637         #'_date'      => $invoice_time,
1638         '_date'      => time,
1639         'status'     => $status,
1640         'statustext' => $statustext,
1641       };
1642       $error = $cust_bill_event->insert;
1643       if ( $error ) {
1644         #$dbh->rollback if $oldAutoCommit;
1645         #return "error: $error";
1646
1647         # gah, even with transactions.
1648         $dbh->commit if $oldAutoCommit; #well.
1649         my $e = 'WARNING: Event run but database not updated - '.
1650                 'error inserting cust_bill_event, invnum #'. $cust_bill->invnum.
1651                 ', eventpart '. $part_bill_event->eventpart.
1652                 ": $error";
1653         warn $e;
1654         return $e;
1655       }
1656
1657
1658     }
1659
1660   }
1661
1662   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1663   '';
1664
1665 }
1666
1667 =item retry_realtime
1668
1669 Schedules realtime credit card / electronic check / LEC billing events for
1670 for retry.  Useful if card information has changed or manual retry is desired.
1671 The 'collect' method must be called to actually retry the transaction.
1672
1673 Implementation details: For each of this customer's open invoices, changes
1674 the status of the first "done" (with statustext error) realtime processing
1675 event to "failed".
1676
1677 =cut
1678
1679 sub retry_realtime {
1680   my $self = shift;
1681
1682   local $SIG{HUP} = 'IGNORE';
1683   local $SIG{INT} = 'IGNORE';
1684   local $SIG{QUIT} = 'IGNORE';
1685   local $SIG{TERM} = 'IGNORE';
1686   local $SIG{TSTP} = 'IGNORE';
1687   local $SIG{PIPE} = 'IGNORE';
1688
1689   my $oldAutoCommit = $FS::UID::AutoCommit;
1690   local $FS::UID::AutoCommit = 0;
1691   my $dbh = dbh;
1692
1693   foreach my $cust_bill (
1694     grep { $_->cust_bill_event }
1695       $self->open_cust_bill
1696   ) {
1697     my @cust_bill_event =
1698       sort { $a->part_bill_event->seconds <=> $b->part_bill_event->seconds }
1699         grep {
1700                #$_->part_bill_event->plan eq 'realtime-card'
1701                $_->part_bill_event->eventcode =~
1702                    /\$cust_bill\->realtime_(card|ach|lec)/
1703                  && $_->status eq 'done'
1704                  && $_->statustext
1705              }
1706           $cust_bill->cust_bill_event;
1707     next unless @cust_bill_event;
1708     my $error = $cust_bill_event[0]->retry;
1709     if ( $error ) {
1710       $dbh->rollback if $oldAutoCommit;
1711       return "error scheduling invoice event for retry: $error";
1712     }
1713
1714   }
1715
1716   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1717   '';
1718
1719 }
1720
1721 =item realtime_bop METHOD AMOUNT [ OPTION => VALUE ... ]
1722
1723 Runs a realtime credit card, ACH (electronic check) or phone bill transaction
1724 via a Business::OnlinePayment realtime gateway.  See
1725 L<http://420.am/business-onlinepayment> for supported gateways.
1726
1727 Available methods are: I<CC>, I<ECHECK> and I<LEC>
1728
1729 Available options are: I<description>, I<invnum>, I<quiet>
1730
1731 The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
1732 I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
1733 if set, will override the value from the customer record.
1734
1735 I<description> is a free-text field passed to the gateway.  It defaults to
1736 "Internet services".
1737
1738 If an I<invnum> is specified, this payment (if sucessful) is applied to the
1739 specified invoice.  If you don't specify an I<invnum> you might want to
1740 call the B<apply_payments> method.
1741
1742 I<quiet> can be set true to surpress email decline notices.
1743
1744 (moved from cust_bill) (probably should get realtime_{card,ach,lec} here too)
1745
1746 =cut
1747
1748 sub realtime_bop {
1749   my( $self, $method, $amount, %options ) = @_;
1750   if ( $DEBUG ) {
1751     warn "$self $method $amount\n";
1752     warn "  $_ => $options{$_}\n" foreach keys %options;
1753   }
1754
1755   $options{'description'} ||= 'Internet services';
1756
1757   #pre-requisites
1758   die "Real-time processing not enabled\n"
1759     unless $conf->exists('business-onlinepayment');
1760   eval "use Business::OnlinePayment";  
1761   die $@ if $@;
1762
1763   #load up config
1764   my $bop_config = 'business-onlinepayment';
1765   $bop_config .= '-ach'
1766     if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
1767   my ( $processor, $login, $password, $action, @bop_options ) =
1768     $conf->config($bop_config);
1769   $action ||= 'normal authorization';
1770   pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
1771   die "No real-time processor is enabled - ".
1772       "did you set the business-onlinepayment configuration value?\n"
1773     unless $processor;
1774
1775   #massage data
1776
1777   my $address = exists($options{'address1'})
1778                     ? $options{'address1'}
1779                     : $self->address1;
1780   my $address2 = exists($options{'address2'})
1781                     ? $options{'address2'}
1782                     : $self->address2;
1783   $address .= ", ". $address2 if length($address2);
1784
1785   my $o_payname = exists($options{'payname'})
1786                     ? $options{'payname'}
1787                     : $self->payname;
1788   my($payname, $payfirst, $paylast);
1789   if ( $o_payname && $method ne 'ECHECK' ) {
1790     ($payname = $o_payname) =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
1791       or return "Illegal payname $payname";
1792     ($payfirst, $paylast) = ($1, $2);
1793   } else {
1794     $payfirst = $self->getfield('first');
1795     $paylast = $self->getfield('last');
1796     $payname =  "$payfirst $paylast";
1797   }
1798
1799   my @invoicing_list = grep { $_ ne 'POST' } $self->invoicing_list;
1800   if ( $conf->exists('emailinvoiceauto')
1801        || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
1802     push @invoicing_list, $self->all_emails;
1803   }
1804   my $email = $invoicing_list[0];
1805
1806   my $payinfo = exists($options{'payinfo'})
1807                   ? $options{'payinfo'}
1808                   : $self->payinfo;
1809
1810   my %content = ();
1811   if ( $method eq 'CC' ) { 
1812
1813     $content{card_number} = $payinfo;
1814     my $paydate = exists($options{'paydate'})
1815                     ? $options{'paydate'}
1816                     : $self->paydate;
1817     $paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
1818     $content{expiration} = "$2/$1";
1819
1820     if ( defined $self->dbdef_table->column('paycvv') ) {
1821       my $paycvv = exists($options{'paycvv'})
1822                      ? $options{'paycvv'}
1823                      : $self->paycvv;
1824       $content{cvv2} = $self->paycvv
1825         if length($paycvv);
1826     }
1827
1828     $content{recurring_billing} = 'YES'
1829       if qsearch('cust_pay', { 'custnum' => $self->custnum,
1830                                'payby'   => 'CARD',
1831                                'payinfo' => $payinfo,
1832                              } );
1833
1834   } elsif ( $method eq 'ECHECK' ) {
1835     ( $content{account_number}, $content{routing_code} ) =
1836       split('@', $payinfo);
1837     $content{bank_name} = $o_payname;
1838     $content{account_type} = 'CHECKING';
1839     $content{account_name} = $payname;
1840     $content{customer_org} = $self->company ? 'B' : 'I';
1841     $content{customer_ssn} = exists($options{'ss'})
1842                                ? $options{'ss'}
1843                                : $self->ss;
1844   } elsif ( $method eq 'LEC' ) {
1845     $content{phone} = $payinfo;
1846   }
1847
1848   #transaction(s)
1849
1850   my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
1851
1852   my $transaction = new Business::OnlinePayment( $processor, @bop_options );
1853   $transaction->content(
1854     'type'           => $method,
1855     'login'          => $login,
1856     'password'       => $password,
1857     'action'         => $action1,
1858     'description'    => $options{'description'},
1859     'amount'         => $amount,
1860     'invoice_number' => $options{'invnum'},
1861     'customer_id'    => $self->custnum,
1862     'last_name'      => $paylast,
1863     'first_name'     => $payfirst,
1864     'name'           => $payname,
1865     'address'        => $address,
1866     'city'           => ( exists($options{'city'})
1867                             ? $options{'city'}
1868                             : $self->city          ),
1869     'state'          => ( exists($options{'state'})
1870                             ? $options{'state'}
1871                             : $self->state          ),
1872     'zip'            => ( exists($options{'zip'})
1873                             ? $options{'zip'}
1874                             : $self->zip          ),
1875     'country'        => ( exists($options{'country'})
1876                             ? $options{'country'}
1877                             : $self->country          ),
1878     'referer'        => 'http://cleanwhisker.420.am/',
1879     'email'          => $email,
1880     'phone'          => $self->daytime || $self->night,
1881     %content, #after
1882   );
1883   $transaction->submit();
1884
1885   if ( $transaction->is_success() && $action2 ) {
1886     my $auth = $transaction->authorization;
1887     my $ordernum = $transaction->can('order_number')
1888                    ? $transaction->order_number
1889                    : '';
1890
1891     my $capture =
1892       new Business::OnlinePayment( $processor, @bop_options );
1893
1894     my %capture = (
1895       %content,
1896       type           => $method,
1897       action         => $action2,
1898       login          => $login,
1899       password       => $password,
1900       order_number   => $ordernum,
1901       amount         => $amount,
1902       authorization  => $auth,
1903       description    => $options{'description'},
1904     );
1905
1906     foreach my $field (qw( authorization_source_code returned_ACI                                          transaction_identifier validation_code           
1907                            transaction_sequence_num local_transaction_date    
1908                            local_transaction_time AVS_result_code          )) {
1909       $capture{$field} = $transaction->$field() if $transaction->can($field);
1910     }
1911
1912     $capture->content( %capture );
1913
1914     $capture->submit();
1915
1916     unless ( $capture->is_success ) {
1917       my $e = "Authorization sucessful but capture failed, custnum #".
1918               $self->custnum. ': '.  $capture->result_code.
1919               ": ". $capture->error_message;
1920       warn $e;
1921       return $e;
1922     }
1923
1924   }
1925
1926   #remove paycvv after initial transaction
1927   #false laziness w/misc/process/payment.cgi - check both to make sure working
1928   # correctly
1929   if ( defined $self->dbdef_table->column('paycvv')
1930        && length($self->paycvv)
1931        && ! grep { $_ eq cardtype($payinfo) } $conf->config('cvv-save')
1932   ) {
1933     my $error = $self->remove_cvv;
1934     if ( $error ) {
1935       warn "error removing cvv: $error\n";
1936     }
1937   }
1938
1939   #result handling
1940   if ( $transaction->is_success() ) {
1941
1942     my %method2payby = (
1943       'CC'     => 'CARD',
1944       'ECHECK' => 'CHEK',
1945       'LEC'    => 'LECB',
1946     );
1947
1948     my $paybatch = "$processor:". $transaction->authorization;
1949     $paybatch .= ':'. $transaction->order_number
1950       if $transaction->can('order_number')
1951       && length($transaction->order_number);
1952
1953     my $cust_pay = new FS::cust_pay ( {
1954        'custnum'  => $self->custnum,
1955        'invnum'   => $options{'invnum'},
1956        'paid'     => $amount,
1957        '_date'     => '',
1958        'payby'    => $method2payby{$method},
1959        'payinfo'  => $payinfo,
1960        'paybatch' => $paybatch,
1961     } );
1962     my $error = $cust_pay->insert;
1963     if ( $error ) {
1964       $cust_pay->invnum(''); #try again with no specific invnum
1965       my $error2 = $cust_pay->insert;
1966       if ( $error2 ) {
1967         # gah, even with transactions.
1968         my $e = 'WARNING: Card/ACH debited but database not updated - '.
1969                 "error inserting payment ($processor): $error2".
1970                 " (previously tried insert with invnum #$options{'invnum'}" .
1971                 ": $error )";
1972         warn $e;
1973         return $e;
1974       }
1975     }
1976     return ''; #no error
1977
1978   } else {
1979
1980     my $perror = "$processor error: ". $transaction->error_message;
1981
1982     if ( !$options{'quiet'} && !$realtime_bop_decline_quiet
1983          && $conf->exists('emaildecline')
1984          && grep { $_ ne 'POST' } $self->invoicing_list
1985          && ! grep { $transaction->error_message =~ /$_/ }
1986                    $conf->config('emaildecline-exclude')
1987     ) {
1988       my @templ = $conf->config('declinetemplate');
1989       my $template = new Text::Template (
1990         TYPE   => 'ARRAY',
1991         SOURCE => [ map "$_\n", @templ ],
1992       ) or return "($perror) can't create template: $Text::Template::ERROR";
1993       $template->compile()
1994         or return "($perror) can't compile template: $Text::Template::ERROR";
1995
1996       my $templ_hash = { error => $transaction->error_message };
1997
1998       my $error = send_email(
1999         'from'    => $conf->config('invoice_from'),
2000         'to'      => [ grep { $_ ne 'POST' } $self->invoicing_list ],
2001         'subject' => 'Your payment could not be processed',
2002         'body'    => [ $template->fill_in(HASH => $templ_hash) ],
2003       );
2004
2005       $perror .= " (also received error sending decline notification: $error)"
2006         if $error;
2007
2008     }
2009   
2010     return $perror;
2011   }
2012
2013 }
2014
2015 =item remove_cvv
2016
2017 Removes the I<paycvv> field from the database directly.
2018
2019 If there is an error, returns the error, otherwise returns false.
2020
2021 =cut
2022
2023 sub remove_cvv {
2024   my $self = shift;
2025   my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2026     or return dbh->errstr;
2027   $sth->execute($self->custnum)
2028     or return $sth->errstr;
2029   $self->paycvv('');
2030   '';
2031 }
2032
2033 =item realtime_refund_bop METHOD [ OPTION => VALUE ... ]
2034
2035 Refunds a realtime credit card, ACH (electronic check) or phone bill transaction
2036 via a Business::OnlinePayment realtime gateway.  See
2037 L<http://420.am/business-onlinepayment> for supported gateways.
2038
2039 Available methods are: I<CC>, I<ECHECK> and I<LEC>
2040
2041 Available options are: I<amount>, I<reason>, I<paynum>
2042
2043 Most gateways require a reference to an original payment transaction to refund,
2044 so you probably need to specify a I<paynum>.
2045
2046 I<amount> defaults to the original amount of the payment if not specified.
2047
2048 I<reason> specifies a reason for the refund.
2049
2050 Implementation note: If I<amount> is unspecified or equal to the amount of the
2051 orignal payment, first an attempt is made to "void" the transaction via
2052 the gateway (to cancel a not-yet settled transaction) and then if that fails,
2053 the normal attempt is made to "refund" ("credit") the transaction via the
2054 gateway is attempted.
2055
2056 #The additional options I<payname>, I<address1>, I<address2>, I<city>, I<state>,
2057 #I<zip>, I<payinfo> and I<paydate> are also available.  Any of these options,
2058 #if set, will override the value from the customer record.
2059
2060 #If an I<invnum> is specified, this payment (if sucessful) is applied to the
2061 #specified invoice.  If you don't specify an I<invnum> you might want to
2062 #call the B<apply_payments> method.
2063
2064 =cut
2065
2066 #some false laziness w/realtime_bop, not enough to make it worth merging
2067 #but some useful small subs should be pulled out
2068 sub realtime_refund_bop {
2069   my( $self, $method, %options ) = @_;
2070   if ( $DEBUG ) {
2071     warn "$self $method refund\n";
2072     warn "  $_ => $options{$_}\n" foreach keys %options;
2073   }
2074
2075   #pre-requisites
2076   die "Real-time processing not enabled\n"
2077     unless $conf->exists('business-onlinepayment');
2078   eval "use Business::OnlinePayment";  
2079   die $@ if $@;
2080
2081   #load up config
2082   my $bop_config = 'business-onlinepayment';
2083   $bop_config .= '-ach'
2084     if $method eq 'ECHECK' && $conf->exists($bop_config. '-ach');
2085   my ( $processor, $login, $password, $unused_action, @bop_options ) =
2086     $conf->config($bop_config);
2087   #$action ||= 'normal authorization';
2088   pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
2089   die "No real-time processor is enabled - ".
2090       "did you set the business-onlinepayment configuration value?\n"
2091     unless $processor;
2092
2093   my $cust_pay = '';
2094   my $amount = $options{'amount'};
2095   my( $pay_processor, $auth, $order_number ) = ( '', '', '' );
2096   if ( $options{'paynum'} ) {
2097     warn "FS::cust_main::realtime_bop: paynum: $options{paynum}\n" if $DEBUG;
2098     $cust_pay = qsearchs('cust_pay', { paynum=>$options{'paynum'} } )
2099       or return "Unknown paynum $options{'paynum'}";
2100     $amount ||= $cust_pay->paid;
2101     $cust_pay->paybatch =~ /^(\w+):(\w*)(:(\w+))?$/
2102       or return "Can't parse paybatch for paynum $options{'paynum'}: ".
2103                 $cust_pay->paybatch;
2104     ( $pay_processor, $auth, $order_number ) = ( $1, $2, $4 );
2105     return "processor of payment $options{'paynum'} $pay_processor does not".
2106            " match current processor $processor"
2107       unless $pay_processor eq $processor;
2108   }
2109   return "neither amount nor paynum specified" unless $amount;
2110
2111   my %content = (
2112     'type'           => $method,
2113     'login'          => $login,
2114     'password'       => $password,
2115     'order_number'   => $order_number,
2116     'amount'         => $amount,
2117     'referer'        => 'http://cleanwhisker.420.am/',
2118   );
2119   $content{authorization} = $auth
2120     if length($auth); #echeck/ACH transactions have an order # but no auth
2121                       #(at least with authorize.net)
2122
2123   #first try void if applicable
2124   if ( $cust_pay && $cust_pay->paid == $amount ) { #and check dates?
2125     warn "FS::cust_main::realtime_bop: attempting void\n" if $DEBUG;
2126     my $void = new Business::OnlinePayment( $processor, @bop_options );
2127     $void->content( 'action' => 'void', %content );
2128     $void->submit();
2129     if ( $void->is_success ) {
2130       my $error = $cust_pay->void($options{'reason'});
2131       if ( $error ) {
2132         # gah, even with transactions.
2133         my $e = 'WARNING: Card/ACH voided but database not updated - '.
2134                 "error voiding payment: $error";
2135         warn $e;
2136         return $e;
2137       }
2138       warn "FS::cust_main::realtime_bop: void successful\n" if $DEBUG;
2139       return '';
2140     }
2141   }
2142
2143   warn "FS::cust_main::realtime_bop: void unsuccessful, trying refund\n"
2144     if $DEBUG;
2145
2146   #massage data
2147   my $address = $self->address1;
2148   $address .= ", ". $self->address2 if $self->address2;
2149
2150   my($payname, $payfirst, $paylast);
2151   if ( $self->payname && $method ne 'ECHECK' ) {
2152     $payname = $self->payname;
2153     $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
2154       or return "Illegal payname $payname";
2155     ($payfirst, $paylast) = ($1, $2);
2156   } else {
2157     $payfirst = $self->getfield('first');
2158     $paylast = $self->getfield('last');
2159     $payname =  "$payfirst $paylast";
2160   }
2161
2162   if ( $method eq 'CC' ) {
2163
2164     if ( $cust_pay ) {
2165       $content{card_number} = $cust_pay->payinfo;
2166       #$self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2167       #$content{expiration} = "$2/$1";
2168     } else {
2169       $content{card_number} = $self->payinfo;
2170       $self->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
2171       $content{expiration} = "$2/$1";
2172     }
2173
2174   } elsif ( $method eq 'ECHECK' ) {
2175     ( $content{account_number}, $content{routing_code} ) =
2176       split('@', $self->payinfo);
2177     $content{bank_name} = $self->payname;
2178     $content{account_type} = 'CHECKING';
2179     $content{account_name} = $payname;
2180     $content{customer_org} = $self->company ? 'B' : 'I';
2181     $content{customer_ssn} = $self->ss;
2182   } elsif ( $method eq 'LEC' ) {
2183     $content{phone} = $self->payinfo;
2184   }
2185
2186   #then try refund
2187   my $refund = new Business::OnlinePayment( $processor, @bop_options );
2188   my %sub_content = $refund->content(
2189     'action'         => 'credit',
2190     'customer_id'    => $self->custnum,
2191     'last_name'      => $paylast,
2192     'first_name'     => $payfirst,
2193     'name'           => $payname,
2194     'address'        => $address,
2195     'city'           => $self->city,
2196     'state'          => $self->state,
2197     'zip'            => $self->zip,
2198     'country'        => $self->country,
2199     %content, #after
2200   );
2201   warn join('', map { "  $_ => $sub_content{$_}\n" } keys %sub_content )
2202     if $DEBUG > 1;
2203   $refund->submit();
2204
2205   return "$processor error: ". $refund->error_message
2206     unless $refund->is_success();
2207
2208   my %method2payby = (
2209     'CC'     => 'CARD',
2210     'ECHECK' => 'CHEK',
2211     'LEC'    => 'LECB',
2212   );
2213
2214   my $paybatch = "$processor:". $refund->authorization;
2215   $paybatch .= ':'. $refund->order_number
2216     if $refund->can('order_number') && $refund->order_number;
2217
2218   while ( $cust_pay && $cust_pay->unappled < $amount ) {
2219     my @cust_bill_pay = $cust_pay->cust_bill_pay;
2220     last unless @cust_bill_pay;
2221     my $cust_bill_pay = pop @cust_bill_pay;
2222     my $error = $cust_bill_pay->delete;
2223     last if $error;
2224   }
2225
2226   my $cust_refund = new FS::cust_refund ( {
2227     'custnum'  => $self->custnum,
2228     'paynum'   => $options{'paynum'},
2229     'refund'   => $amount,
2230     '_date'    => '',
2231     'payby'    => $method2payby{$method},
2232     'payinfo'  => $self->payinfo,
2233     'paybatch' => $paybatch,
2234     'reason'   => $options{'reason'} || 'card or ACH refund',
2235   } );
2236   my $error = $cust_refund->insert;
2237   if ( $error ) {
2238     $cust_refund->paynum(''); #try again with no specific paynum
2239     my $error2 = $cust_refund->insert;
2240     if ( $error2 ) {
2241       # gah, even with transactions.
2242       my $e = 'WARNING: Card/ACH refunded but database not updated - '.
2243               "error inserting refund ($processor): $error2".
2244               " (previously tried insert with paynum #$options{'paynum'}" .
2245               ": $error )";
2246       warn $e;
2247       return $e;
2248     }
2249   }
2250
2251   ''; #no error
2252
2253 }
2254
2255 =item total_owed
2256
2257 Returns the total owed for this customer on all invoices
2258 (see L<FS::cust_bill/owed>).
2259
2260 =cut
2261
2262 sub total_owed {
2263   my $self = shift;
2264   $self->total_owed_date(2145859200); #12/31/2037
2265 }
2266
2267 =item total_owed_date TIME
2268
2269 Returns the total owed for this customer on all invoices with date earlier than
2270 TIME.  TIME is specified as a UNIX timestamp; see L<perlfunc/"time">).  Also
2271 see L<Time::Local> and L<Date::Parse> for conversion functions.
2272
2273 =cut
2274
2275 sub total_owed_date {
2276   my $self = shift;
2277   my $time = shift;
2278   my $total_bill = 0;
2279   foreach my $cust_bill (
2280     grep { $_->_date <= $time }
2281       qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2282   ) {
2283     $total_bill += $cust_bill->owed;
2284   }
2285   sprintf( "%.2f", $total_bill );
2286 }
2287
2288 =item apply_credits OPTION => VALUE ...
2289
2290 Applies (see L<FS::cust_credit_bill>) unapplied credits (see L<FS::cust_credit>)
2291 to outstanding invoice balances in chronological order (or reverse
2292 chronological order if the I<order> option is set to B<newest>) and returns the
2293 value of any remaining unapplied credits available for refund (see
2294 L<FS::cust_refund>).
2295
2296 =cut
2297
2298 sub apply_credits {
2299   my $self = shift;
2300   my %opt = @_;
2301
2302   return 0 unless $self->total_credited;
2303
2304   my @credits = sort { $b->_date <=> $a->_date} (grep { $_->credited > 0 }
2305       qsearch('cust_credit', { 'custnum' => $self->custnum } ) );
2306
2307   my @invoices = $self->open_cust_bill;
2308   @invoices = sort { $b->_date <=> $a->_date } @invoices
2309     if defined($opt{'order'}) && $opt{'order'} eq 'newest';
2310
2311   my $credit;
2312   foreach my $cust_bill ( @invoices ) {
2313     my $amount;
2314
2315     if ( !defined($credit) || $credit->credited == 0) {
2316       $credit = pop @credits or last;
2317     }
2318
2319     if ($cust_bill->owed >= $credit->credited) {
2320       $amount=$credit->credited;
2321     }else{
2322       $amount=$cust_bill->owed;
2323     }
2324     
2325     my $cust_credit_bill = new FS::cust_credit_bill ( {
2326       'crednum' => $credit->crednum,
2327       'invnum'  => $cust_bill->invnum,
2328       'amount'  => $amount,
2329     } );
2330     my $error = $cust_credit_bill->insert;
2331     die $error if $error;
2332     
2333     redo if ($cust_bill->owed > 0);
2334
2335   }
2336
2337   return $self->total_credited;
2338 }
2339
2340 =item apply_payments
2341
2342 Applies (see L<FS::cust_bill_pay>) unapplied payments (see L<FS::cust_pay>)
2343 to outstanding invoice balances in chronological order.
2344
2345  #and returns the value of any remaining unapplied payments.
2346
2347 =cut
2348
2349 sub apply_payments {
2350   my $self = shift;
2351
2352   #return 0 unless
2353
2354   my @payments = sort { $b->_date <=> $a->_date } ( grep { $_->unapplied > 0 }
2355       qsearch('cust_pay', { 'custnum' => $self->custnum } ) );
2356
2357   my @invoices = sort { $a->_date <=> $b->_date} (grep { $_->owed > 0 }
2358       qsearch('cust_bill', { 'custnum' => $self->custnum } ) );
2359
2360   my $payment;
2361
2362   foreach my $cust_bill ( @invoices ) {
2363     my $amount;
2364
2365     if ( !defined($payment) || $payment->unapplied == 0 ) {
2366       $payment = pop @payments or last;
2367     }
2368
2369     if ( $cust_bill->owed >= $payment->unapplied ) {
2370       $amount = $payment->unapplied;
2371     } else {
2372       $amount = $cust_bill->owed;
2373     }
2374
2375     my $cust_bill_pay = new FS::cust_bill_pay ( {
2376       'paynum' => $payment->paynum,
2377       'invnum' => $cust_bill->invnum,
2378       'amount' => $amount,
2379     } );
2380     my $error = $cust_bill_pay->insert;
2381     die $error if $error;
2382
2383     redo if ( $cust_bill->owed > 0);
2384
2385   }
2386
2387   return $self->total_unapplied_payments;
2388 }
2389
2390 =item total_credited
2391
2392 Returns the total outstanding credit (see L<FS::cust_credit>) for this
2393 customer.  See L<FS::cust_credit/credited>.
2394
2395 =cut
2396
2397 sub total_credited {
2398   my $self = shift;
2399   my $total_credit = 0;
2400   foreach my $cust_credit ( qsearch('cust_credit', {
2401     'custnum' => $self->custnum,
2402   } ) ) {
2403     $total_credit += $cust_credit->credited;
2404   }
2405   sprintf( "%.2f", $total_credit );
2406 }
2407
2408 =item total_unapplied_payments
2409
2410 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
2411 See L<FS::cust_pay/unapplied>.
2412
2413 =cut
2414
2415 sub total_unapplied_payments {
2416   my $self = shift;
2417   my $total_unapplied = 0;
2418   foreach my $cust_pay ( qsearch('cust_pay', {
2419     'custnum' => $self->custnum,
2420   } ) ) {
2421     $total_unapplied += $cust_pay->unapplied;
2422   }
2423   sprintf( "%.2f", $total_unapplied );
2424 }
2425
2426 =item balance
2427
2428 Returns the balance for this customer (total_owed minus total_credited
2429 minus total_unapplied_payments).
2430
2431 =cut
2432
2433 sub balance {
2434   my $self = shift;
2435   sprintf( "%.2f",
2436     $self->total_owed - $self->total_credited - $self->total_unapplied_payments
2437   );
2438 }
2439
2440 =item balance_date TIME
2441
2442 Returns the balance for this customer, only considering invoices with date
2443 earlier than TIME (total_owed_date minus total_credited minus
2444 total_unapplied_payments).  TIME is specified as a UNIX timestamp; see
2445 L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion
2446 functions.
2447
2448 =cut
2449
2450 sub balance_date {
2451   my $self = shift;
2452   my $time = shift;
2453   sprintf( "%.2f",
2454     $self->total_owed_date($time)
2455       - $self->total_credited
2456       - $self->total_unapplied_payments
2457   );
2458 }
2459
2460 =item paydate_monthyear
2461
2462 Returns a two-element list consisting of the month and year of this customer's
2463 paydate (credit card expiration date for CARD customers)
2464
2465 =cut
2466
2467 sub paydate_monthyear {
2468   my $self = shift;
2469   if ( $self->paydate  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
2470     ( $2, $1 );
2471   } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
2472     ( $1, $3 );
2473   } else {
2474     ('', '');
2475   }
2476 }
2477
2478 =item payinfo_masked
2479
2480 Returns a "masked" payinfo field with all but the last four characters replaced
2481 by 'x'es.  Useful for displaying credit cards.
2482
2483 =cut
2484
2485 sub payinfo_masked {
2486   my $self = shift;
2487   my $payinfo = $self->payinfo;
2488   'x'x(length($payinfo)-4). substr($payinfo,(length($payinfo)-4));
2489 }
2490
2491 =item invoicing_list [ ARRAYREF ]
2492
2493 If an arguement is given, sets these email addresses as invoice recipients
2494 (see L<FS::cust_main_invoice>).  Errors are not fatal and are not reported
2495 (except as warnings), so use check_invoicing_list first.
2496
2497 Returns a list of email addresses (with svcnum entries expanded).
2498
2499 Note: You can clear the invoicing list by passing an empty ARRAYREF.  You can
2500 check it without disturbing anything by passing nothing.
2501
2502 This interface may change in the future.
2503
2504 =cut
2505
2506 sub invoicing_list {
2507   my( $self, $arrayref ) = @_;
2508   if ( $arrayref ) {
2509     my @cust_main_invoice;
2510     if ( $self->custnum ) {
2511       @cust_main_invoice = 
2512         qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2513     } else {
2514       @cust_main_invoice = ();
2515     }
2516     foreach my $cust_main_invoice ( @cust_main_invoice ) {
2517       #warn $cust_main_invoice->destnum;
2518       unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
2519         #warn $cust_main_invoice->destnum;
2520         my $error = $cust_main_invoice->delete;
2521         warn $error if $error;
2522       }
2523     }
2524     if ( $self->custnum ) {
2525       @cust_main_invoice = 
2526         qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2527     } else {
2528       @cust_main_invoice = ();
2529     }
2530     my %seen = map { $_->address => 1 } @cust_main_invoice;
2531     foreach my $address ( @{$arrayref} ) {
2532       next if exists $seen{$address} && $seen{$address};
2533       $seen{$address} = 1;
2534       my $cust_main_invoice = new FS::cust_main_invoice ( {
2535         'custnum' => $self->custnum,
2536         'dest'    => $address,
2537       } );
2538       my $error = $cust_main_invoice->insert;
2539       warn $error if $error;
2540     }
2541   }
2542   if ( $self->custnum ) {
2543     map { $_->address }
2544       qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
2545   } else {
2546     ();
2547   }
2548 }
2549
2550 =item check_invoicing_list ARRAYREF
2551
2552 Checks these arguements as valid input for the invoicing_list method.  If there
2553 is an error, returns the error, otherwise returns false.
2554
2555 =cut
2556
2557 sub check_invoicing_list {
2558   my( $self, $arrayref ) = @_;
2559   foreach my $address ( @{$arrayref} ) {
2560     my $cust_main_invoice = new FS::cust_main_invoice ( {
2561       'custnum' => $self->custnum,
2562       'dest'    => $address,
2563     } );
2564     my $error = $self->custnum
2565                 ? $cust_main_invoice->check
2566                 : $cust_main_invoice->checkdest
2567     ;
2568     return $error if $error;
2569   }
2570   '';
2571 }
2572
2573 =item set_default_invoicing_list
2574
2575 Sets the invoicing list to all accounts associated with this customer,
2576 overwriting any previous invoicing list.
2577
2578 =cut
2579
2580 sub set_default_invoicing_list {
2581   my $self = shift;
2582   $self->invoicing_list($self->all_emails);
2583 }
2584
2585 =item all_emails
2586
2587 Returns the email addresses of all accounts provisioned for this customer.
2588
2589 =cut
2590
2591 sub all_emails {
2592   my $self = shift;
2593   my %list;
2594   foreach my $cust_pkg ( $self->all_pkgs ) {
2595     my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
2596     my @svc_acct =
2597       map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2598         grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
2599           @cust_svc;
2600     $list{$_}=1 foreach map { $_->email } @svc_acct;
2601   }
2602   keys %list;
2603 }
2604
2605 =item invoicing_list_addpost
2606
2607 Adds postal invoicing to this customer.  If this customer is already configured
2608 to receive postal invoices, does nothing.
2609
2610 =cut
2611
2612 sub invoicing_list_addpost {
2613   my $self = shift;
2614   return if grep { $_ eq 'POST' } $self->invoicing_list;
2615   my @invoicing_list = $self->invoicing_list;
2616   push @invoicing_list, 'POST';
2617   $self->invoicing_list(\@invoicing_list);
2618 }
2619
2620 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
2621
2622 Returns an array of customers referred by this customer (referral_custnum set
2623 to this custnum).  If DEPTH is given, recurses up to the given depth, returning
2624 customers referred by customers referred by this customer and so on, inclusive.
2625 The default behavior is DEPTH 1 (no recursion).
2626
2627 =cut
2628
2629 sub referral_cust_main {
2630   my $self = shift;
2631   my $depth = @_ ? shift : 1;
2632   my $exclude = @_ ? shift : {};
2633
2634   my @cust_main =
2635     map { $exclude->{$_->custnum}++; $_; }
2636       grep { ! $exclude->{ $_->custnum } }
2637         qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
2638
2639   if ( $depth > 1 ) {
2640     push @cust_main,
2641       map { $_->referral_cust_main($depth-1, $exclude) }
2642         @cust_main;
2643   }
2644
2645   @cust_main;
2646 }
2647
2648 =item referral_cust_main_ncancelled
2649
2650 Same as referral_cust_main, except only returns customers with uncancelled
2651 packages.
2652
2653 =cut
2654
2655 sub referral_cust_main_ncancelled {
2656   my $self = shift;
2657   grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
2658 }
2659
2660 =item referral_cust_pkg [ DEPTH ]
2661
2662 Like referral_cust_main, except returns a flat list of all unsuspended (and
2663 uncancelled) packages for each customer.  The number of items in this list may
2664 be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
2665
2666 =cut
2667
2668 sub referral_cust_pkg {
2669   my $self = shift;
2670   my $depth = @_ ? shift : 1;
2671
2672   map { $_->unsuspended_pkgs }
2673     grep { $_->unsuspended_pkgs }
2674       $self->referral_cust_main($depth);
2675 }
2676
2677 =item referring_cust_main
2678
2679 Returns the single cust_main record for the customer who referred this customer
2680 (referral_custnum), or false.
2681
2682 =cut
2683
2684 sub referring_cust_main {
2685   my $self = shift;
2686   return '' unless $self->referral_custnum;
2687   qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
2688 }
2689
2690 =item credit AMOUNT, REASON
2691
2692 Applies a credit to this customer.  If there is an error, returns the error,
2693 otherwise returns false.
2694
2695 =cut
2696
2697 sub credit {
2698   my( $self, $amount, $reason ) = @_;
2699   my $cust_credit = new FS::cust_credit {
2700     'custnum' => $self->custnum,
2701     'amount'  => $amount,
2702     'reason'  => $reason,
2703   };
2704   $cust_credit->insert;
2705 }
2706
2707 =item charge AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
2708
2709 Creates a one-time charge for this customer.  If there is an error, returns
2710 the error, otherwise returns false.
2711
2712 =cut
2713
2714 sub charge {
2715   my ( $self, $amount ) = ( shift, shift );
2716   my $pkg      = @_ ? shift : 'One-time charge';
2717   my $comment  = @_ ? shift : '$'. sprintf("%.2f",$amount);
2718   my $taxclass = @_ ? shift : '';
2719
2720   local $SIG{HUP} = 'IGNORE';
2721   local $SIG{INT} = 'IGNORE';
2722   local $SIG{QUIT} = 'IGNORE';
2723   local $SIG{TERM} = 'IGNORE';
2724   local $SIG{TSTP} = 'IGNORE';
2725   local $SIG{PIPE} = 'IGNORE';
2726
2727   my $oldAutoCommit = $FS::UID::AutoCommit;
2728   local $FS::UID::AutoCommit = 0;
2729   my $dbh = dbh;
2730
2731   my $part_pkg = new FS::part_pkg ( {
2732     'pkg'      => $pkg,
2733     'comment'  => $comment,
2734     #'setup'    => $amount,
2735     #'recur'    => '0',
2736     'plan'     => 'flat',
2737     'plandata' => "setup_fee=$amount",
2738     'freq'     => 0,
2739     'disabled' => 'Y',
2740     'taxclass' => $taxclass,
2741   } );
2742
2743   my $error = $part_pkg->insert;
2744   if ( $error ) {
2745     $dbh->rollback if $oldAutoCommit;
2746     return $error;
2747   }
2748
2749   my $pkgpart = $part_pkg->pkgpart;
2750   my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
2751   unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
2752     my $type_pkgs = new FS::type_pkgs \%type_pkgs;
2753     $error = $type_pkgs->insert;
2754     if ( $error ) {
2755       $dbh->rollback if $oldAutoCommit;
2756       return $error;
2757     }
2758   }
2759
2760   my $cust_pkg = new FS::cust_pkg ( {
2761     'custnum' => $self->custnum,
2762     'pkgpart' => $pkgpart,
2763   } );
2764
2765   $error = $cust_pkg->insert;
2766   if ( $error ) {
2767     $dbh->rollback if $oldAutoCommit;
2768     return $error;
2769   }
2770
2771   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2772   '';
2773
2774 }
2775
2776 =item cust_bill
2777
2778 Returns all the invoices (see L<FS::cust_bill>) for this customer.
2779
2780 =cut
2781
2782 sub cust_bill {
2783   my $self = shift;
2784   sort { $a->_date <=> $b->_date }
2785     qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2786 }
2787
2788 =item open_cust_bill
2789
2790 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
2791 customer.
2792
2793 =cut
2794
2795 sub open_cust_bill {
2796   my $self = shift;
2797   grep { $_->owed > 0 } $self->cust_bill;
2798 }
2799
2800 =item cust_credit
2801
2802 Returns all the credits (see L<FS::cust_credit>) for this customer.
2803
2804 =cut
2805
2806 sub cust_credit {
2807   my $self = shift;
2808   sort { $a->_date <=> $b->_date }
2809     qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
2810 }
2811
2812 =item cust_pay
2813
2814 Returns all the payments (see L<FS::cust_pay>) for this customer.
2815
2816 =cut
2817
2818 sub cust_pay {
2819   my $self = shift;
2820   sort { $a->_date <=> $b->_date }
2821     qsearch( 'cust_pay', { 'custnum' => $self->custnum } )
2822 }
2823
2824 =item cust_pay_void
2825
2826 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
2827
2828 =cut
2829
2830 sub cust_pay_void {
2831   my $self = shift;
2832   sort { $a->_date <=> $b->_date }
2833     qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
2834 }
2835
2836
2837 =item cust_refund
2838
2839 Returns all the refunds (see L<FS::cust_refund>) for this customer.
2840
2841 =cut
2842
2843 sub cust_refund {
2844   my $self = shift;
2845   sort { $a->_date <=> $b->_date }
2846     qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
2847 }
2848
2849 =item select_for_update
2850
2851 Selects this record with the SQL "FOR UPDATE" command.  This can be useful as
2852 a mutex.
2853
2854 =cut
2855
2856 sub select_for_update {
2857   my $self = shift;
2858   qsearch('cust_main', { 'custnum' => $self->custnum }, '*', 'FOR UPDATE' );
2859 }
2860
2861 =item name
2862
2863 Returns a name string for this customer, either "Company (Last, First)" or
2864 "Last, First".
2865
2866 =cut
2867
2868 sub name {
2869   my $self = shift;
2870   my $name = $self->get('last'). ', '. $self->first;
2871   $name = $self->company. " ($name)" if $self->company;
2872   $name;
2873 }
2874
2875 =item status
2876
2877 Returns a status string for this customer, currently:
2878
2879 =over 4
2880
2881 =item prospect - No packages have ever been ordered
2882
2883 =item active - One or more recurring packages is active
2884
2885 =item suspended - All non-cancelled recurring packages are suspended
2886
2887 =item cancelled - All recurring packages are cancelled
2888
2889 =back
2890
2891 =cut
2892
2893 sub status {
2894   my $self = shift;
2895   for my $status (qw( prospect active suspended cancelled )) {
2896     my $method = $status.'_sql';
2897     my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
2898     my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
2899     $sth->execute( ($self->custnum) x $numnum ) or die $sth->errstr;
2900     return $status if $sth->fetchrow_arrayref->[0];
2901   }
2902 }
2903
2904 =item statuscolor
2905
2906 Returns a hex triplet color string for this customer's status.
2907
2908 =cut
2909
2910 my %statuscolor = (
2911   'prospect'  => '000000',
2912   'active'    => '00CC00',
2913   'suspended' => 'FF9900',
2914   'cancelled' => 'FF0000',
2915 );
2916 sub statuscolor {
2917   my $self = shift;
2918   $statuscolor{$self->status};
2919 }
2920
2921 =back
2922
2923 =head1 CLASS METHODS
2924
2925 =over 4
2926
2927 =item prospect_sql
2928
2929 Returns an SQL expression identifying prospective cust_main records (customers
2930 with no packages ever ordered)
2931
2932 =cut
2933
2934 sub prospect_sql { "
2935   0 = ( SELECT COUNT(*) FROM cust_pkg
2936           WHERE cust_pkg.custnum = cust_main.custnum
2937       )
2938 "; }
2939
2940 =item active_sql
2941
2942 Returns an SQL expression identifying active cust_main records.
2943
2944 =cut
2945
2946 sub active_sql { "
2947   0 < ( SELECT COUNT(*) FROM cust_pkg
2948           WHERE cust_pkg.custnum = cust_main.custnum
2949             AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2950             AND ( cust_pkg.susp   IS NULL OR cust_pkg.susp   = 0 )
2951       )
2952 "; }
2953
2954 =item susp_sql
2955 =item suspended_sql
2956
2957 Returns an SQL expression identifying suspended cust_main records.
2958
2959 =cut
2960
2961 sub suspended_sql { susp_sql(@_); }
2962 sub susp_sql { "
2963     0 < ( SELECT COUNT(*) FROM cust_pkg
2964             WHERE cust_pkg.custnum = cust_main.custnum
2965               AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2966         )
2967     AND 0 = ( SELECT COUNT(*) FROM cust_pkg
2968                 WHERE cust_pkg.custnum = cust_main.custnum
2969                   AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 )
2970                   AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2971             )
2972 "; }
2973
2974 =item cancel_sql
2975 =item cancelled_sql
2976
2977 Returns an SQL expression identifying cancelled cust_main records.
2978
2979 =cut
2980
2981 sub cancelled_sql { cancel_sql(@_); }
2982 sub cancel_sql { "
2983   0 < ( SELECT COUNT(*) FROM cust_pkg
2984           WHERE cust_pkg.custnum = cust_main.custnum
2985       )
2986   AND 0 = ( SELECT COUNT(*) FROM cust_pkg
2987               WHERE cust_pkg.custnum = cust_main.custnum
2988                 AND ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
2989           )
2990 "; }
2991
2992 =item fuzzy_search FUZZY_HASHREF [ HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ ]
2993
2994 Performs a fuzzy (approximate) search and returns the matching FS::cust_main
2995 records.  Currently, only I<last> or I<company> may be specified (the
2996 appropriate ship_ field is also searched if applicable).
2997
2998 Additional options are the same as FS::Record::qsearch
2999
3000 =cut
3001
3002 sub fuzzy_search {
3003   my( $self, $fuzzy, $hash, @opt) = @_;
3004   #$self
3005   $hash ||= {};
3006   my @cust_main = ();
3007
3008   check_and_rebuild_fuzzyfiles();
3009   foreach my $field ( keys %$fuzzy ) {
3010     my $sub = \&{"all_$field"};
3011     my %match = ();
3012     $match{$_}=1 foreach ( amatch($fuzzy->{$field}, ['i'], @{ &$sub() } ) );
3013
3014     foreach ( keys %match ) {
3015       push @cust_main, qsearch('cust_main', { %$hash, $field=>$_}, @opt);
3016       push @cust_main, qsearch('cust_main', { %$hash, "ship_$field"=>$_}, @opt)
3017         if defined dbdef->table('cust_main')->column('ship_last');
3018     }
3019   }
3020
3021   my %saw = ();
3022   @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
3023
3024   @cust_main;
3025
3026 }
3027
3028 =back
3029
3030 =head1 SUBROUTINES
3031
3032 =over 4
3033
3034 =item smart_search OPTION => VALUE ...
3035
3036 Accepts the following options: I<search>, the string to search for.  The string
3037 will be searched for as a customer number, last name or company name, first
3038 searching for an exact match then fuzzy and substring matches.
3039
3040 Any additional options treated as an additional qualifier on the search
3041 (i.e. I<agentnum>).
3042
3043 Returns a (possibly empty) array of FS::cust_main objects.
3044
3045 =cut
3046
3047 sub smart_search {
3048   my %options = @_;
3049   my $search = delete $options{'search'};
3050   my @cust_main = ();
3051
3052   if ( $search =~ /^\s*(\d+)\s*$/ ) { # customer # search
3053
3054     push @cust_main, qsearch('cust_main', { 'custnum' => $1, %options } );
3055
3056   } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { #value search
3057
3058     my $value = lc($1);
3059     my $q_value = dbh->quote($value);
3060
3061     #exact
3062     my $sql = scalar(keys %options) ? ' AND ' : ' WHERE ';
3063     $sql .= " ( LOWER(last) = $q_value OR LOWER(company) = $q_value";
3064     $sql .= " OR LOWER(ship_last) = $q_value OR LOWER(ship_company) = $q_value"
3065       if defined dbdef->table('cust_main')->column('ship_last');
3066     $sql .= ' )';
3067
3068     push @cust_main, qsearch( 'cust_main', \%options, '', $sql );
3069
3070     unless ( @cust_main ) {  #no exact match, trying substring/fuzzy
3071
3072       #still some false laziness w/ search/cust_main.cgi
3073
3074       #substring
3075       push @cust_main, qsearch( 'cust_main',
3076                                 { 'last'     => { 'op'    => 'ILIKE',
3077                                                   'value' => "%$q_value%" },
3078                                   %options,
3079                                 }
3080                               );
3081       push @cust_main, qsearch( 'cust_main',
3082                                 { 'ship_last' => { 'op'    => 'ILIKE',
3083                                                    'value' => "%$q_value%" },
3084                                   %options,
3085
3086                                 }
3087                               )
3088         if defined dbdef->table('cust_main')->column('ship_last');
3089
3090       push @cust_main, qsearch( 'cust_main',
3091                                 { 'company'  => { 'op'    => 'ILIKE',
3092                                                   'value' => "%$q_value%" },
3093                                   %options,
3094                                 }
3095                               );
3096       push @cust_main, qsearch( 'cust_main',
3097                                 { 'ship_company' => { 'op' => 'ILIKE',
3098                                                    'value' => "%$q_value%" },
3099                                   %options,
3100                                 }
3101                               )
3102         if defined dbdef->table('cust_main')->column('ship_last');
3103
3104       #fuzzy
3105       push @cust_main, FS::cust_main->fuzzy_search(
3106         { 'last'     => $value },
3107         \%options,
3108       );
3109       push @cust_main, FS::cust_main->fuzzy_search(
3110         { 'company'  => $value },
3111         \%options,
3112       );
3113
3114     }
3115
3116   }
3117
3118   @cust_main;
3119
3120 }
3121
3122 =item check_and_rebuild_fuzzyfiles
3123
3124 =cut
3125
3126 sub check_and_rebuild_fuzzyfiles {
3127   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3128   -e "$dir/cust_main.last" && -e "$dir/cust_main.company"
3129     or &rebuild_fuzzyfiles;
3130 }
3131
3132 =item rebuild_fuzzyfiles
3133
3134 =cut
3135
3136 sub rebuild_fuzzyfiles {
3137
3138   use Fcntl qw(:flock);
3139
3140   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3141
3142   #last
3143
3144   open(LASTLOCK,">>$dir/cust_main.last")
3145     or die "can't open $dir/cust_main.last: $!";
3146   flock(LASTLOCK,LOCK_EX)
3147     or die "can't lock $dir/cust_main.last: $!";
3148
3149   my @all_last = map $_->getfield('last'), qsearch('cust_main', {});
3150   push @all_last,
3151                  grep $_, map $_->getfield('ship_last'), qsearch('cust_main',{})
3152     if defined dbdef->table('cust_main')->column('ship_last');
3153
3154   open (LASTCACHE,">$dir/cust_main.last.tmp")
3155     or die "can't open $dir/cust_main.last.tmp: $!";
3156   print LASTCACHE join("\n", @all_last), "\n";
3157   close LASTCACHE or die "can't close $dir/cust_main.last.tmp: $!";
3158
3159   rename "$dir/cust_main.last.tmp", "$dir/cust_main.last";
3160   close LASTLOCK;
3161
3162   #company
3163
3164   open(COMPANYLOCK,">>$dir/cust_main.company")
3165     or die "can't open $dir/cust_main.company: $!";
3166   flock(COMPANYLOCK,LOCK_EX)
3167     or die "can't lock $dir/cust_main.company: $!";
3168
3169   my @all_company = grep $_ ne '', map $_->company, qsearch('cust_main',{});
3170   push @all_company,
3171        grep $_ ne '', map $_->ship_company, qsearch('cust_main', {})
3172     if defined dbdef->table('cust_main')->column('ship_last');
3173
3174   open (COMPANYCACHE,">$dir/cust_main.company.tmp")
3175     or die "can't open $dir/cust_main.company.tmp: $!";
3176   print COMPANYCACHE join("\n", @all_company), "\n";
3177   close COMPANYCACHE or die "can't close $dir/cust_main.company.tmp: $!";
3178
3179   rename "$dir/cust_main.company.tmp", "$dir/cust_main.company";
3180   close COMPANYLOCK;
3181
3182 }
3183
3184 =item all_last
3185
3186 =cut
3187
3188 sub all_last {
3189   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3190   open(LASTCACHE,"<$dir/cust_main.last")
3191     or die "can't open $dir/cust_main.last: $!";
3192   my @array = map { chomp; $_; } <LASTCACHE>;
3193   close LASTCACHE;
3194   \@array;
3195 }
3196
3197 =item all_company
3198
3199 =cut
3200
3201 sub all_company {
3202   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3203   open(COMPANYCACHE,"<$dir/cust_main.company")
3204     or die "can't open $dir/cust_main.last: $!";
3205   my @array = map { chomp; $_; } <COMPANYCACHE>;
3206   close COMPANYCACHE;
3207   \@array;
3208 }
3209
3210 =item append_fuzzyfiles LASTNAME COMPANY
3211
3212 =cut
3213
3214 sub append_fuzzyfiles {
3215   my( $last, $company ) = @_;
3216
3217   &check_and_rebuild_fuzzyfiles;
3218
3219   use Fcntl qw(:flock);
3220
3221   my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
3222
3223   if ( $last ) {
3224
3225     open(LAST,">>$dir/cust_main.last")
3226       or die "can't open $dir/cust_main.last: $!";
3227     flock(LAST,LOCK_EX)
3228       or die "can't lock $dir/cust_main.last: $!";
3229
3230     print LAST "$last\n";
3231
3232     flock(LAST,LOCK_UN)
3233       or die "can't unlock $dir/cust_main.last: $!";
3234     close LAST;
3235   }
3236
3237   if ( $company ) {
3238
3239     open(COMPANY,">>$dir/cust_main.company")
3240       or die "can't open $dir/cust_main.company: $!";
3241     flock(COMPANY,LOCK_EX)
3242       or die "can't lock $dir/cust_main.company: $!";
3243
3244     print COMPANY "$company\n";
3245
3246     flock(COMPANY,LOCK_UN)
3247       or die "can't unlock $dir/cust_main.company: $!";
3248
3249     close COMPANY;
3250   }
3251
3252   1;
3253 }
3254
3255 =item batch_import
3256
3257 =cut
3258
3259 sub batch_import {
3260   my $param = shift;
3261   #warn join('-',keys %$param);
3262   my $fh = $param->{filehandle};
3263   my $agentnum = $param->{agentnum};
3264   my $refnum = $param->{refnum};
3265   my $pkgpart = $param->{pkgpart};
3266   my @fields = @{$param->{fields}};
3267
3268   eval "use Date::Parse;";
3269   die $@ if $@;
3270   eval "use Text::CSV_XS;";
3271   die $@ if $@;
3272
3273   my $csv = new Text::CSV_XS;
3274   #warn $csv;
3275   #warn $fh;
3276
3277   my $imported = 0;
3278   #my $columns;
3279
3280   local $SIG{HUP} = 'IGNORE';
3281   local $SIG{INT} = 'IGNORE';
3282   local $SIG{QUIT} = 'IGNORE';
3283   local $SIG{TERM} = 'IGNORE';
3284   local $SIG{TSTP} = 'IGNORE';
3285   local $SIG{PIPE} = 'IGNORE';
3286
3287   my $oldAutoCommit = $FS::UID::AutoCommit;
3288   local $FS::UID::AutoCommit = 0;
3289   my $dbh = dbh;
3290   
3291   #while ( $columns = $csv->getline($fh) ) {
3292   my $line;
3293   while ( defined($line=<$fh>) ) {
3294
3295     $csv->parse($line) or do {
3296       $dbh->rollback if $oldAutoCommit;
3297       return "can't parse: ". $csv->error_input();
3298     };
3299
3300     my @columns = $csv->fields();
3301     #warn join('-',@columns);
3302
3303     my %cust_main = (
3304       agentnum => $agentnum,
3305       refnum   => $refnum,
3306       country  => $conf->config('countrydefault') || 'US',
3307       payby    => 'BILL', #default
3308       paydate  => '12/2037', #default
3309     );
3310     my $billtime = time;
3311     my %cust_pkg = ( pkgpart => $pkgpart );
3312     foreach my $field ( @fields ) {
3313       if ( $field =~ /^cust_pkg\.(setup|bill|susp|expire|cancel)$/ ) {
3314         #$cust_pkg{$1} = str2time( shift @$columns );
3315         if ( $1 eq 'setup' ) {
3316           $billtime = str2time(shift @columns);
3317         } else {
3318           $cust_pkg{$1} = str2time( shift @columns );
3319         }
3320       } else {
3321         #$cust_main{$field} = shift @$columns; 
3322         $cust_main{$field} = shift @columns; 
3323       }
3324     }
3325
3326     my $cust_pkg = new FS::cust_pkg ( \%cust_pkg ) if $pkgpart;
3327     my $cust_main = new FS::cust_main ( \%cust_main );
3328     use Tie::RefHash;
3329     tie my %hash, 'Tie::RefHash'; #this part is important
3330     $hash{$cust_pkg} = [] if $pkgpart;
3331     my $error = $cust_main->insert( \%hash );
3332
3333     if ( $error ) {
3334       $dbh->rollback if $oldAutoCommit;
3335       return "can't insert customer for $line: $error";
3336     }
3337
3338     #false laziness w/bill.cgi
3339     $error = $cust_main->bill( 'time' => $billtime );
3340     if ( $error ) {
3341       $dbh->rollback if $oldAutoCommit;
3342       return "can't bill customer for $line: $error";
3343     }
3344
3345     $cust_main->apply_payments;
3346     $cust_main->apply_credits;
3347
3348     $error = $cust_main->collect();
3349     if ( $error ) {
3350       $dbh->rollback if $oldAutoCommit;
3351       return "can't collect customer for $line: $error";
3352     }
3353
3354     $imported++;
3355   }
3356
3357   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3358
3359   return "Empty file!" unless $imported;
3360
3361   ''; #no error
3362
3363 }
3364
3365 =item batch_charge
3366
3367 =cut
3368
3369 sub batch_charge {
3370   my $param = shift;
3371   #warn join('-',keys %$param);
3372   my $fh = $param->{filehandle};
3373   my @fields = @{$param->{fields}};
3374
3375   eval "use Date::Parse;";
3376   die $@ if $@;
3377   eval "use Text::CSV_XS;";
3378   die $@ if $@;
3379
3380   my $csv = new Text::CSV_XS;
3381   #warn $csv;
3382   #warn $fh;
3383
3384   my $imported = 0;
3385   #my $columns;
3386
3387   local $SIG{HUP} = 'IGNORE';
3388   local $SIG{INT} = 'IGNORE';
3389   local $SIG{QUIT} = 'IGNORE';
3390   local $SIG{TERM} = 'IGNORE';
3391   local $SIG{TSTP} = 'IGNORE';
3392   local $SIG{PIPE} = 'IGNORE';
3393
3394   my $oldAutoCommit = $FS::UID::AutoCommit;
3395   local $FS::UID::AutoCommit = 0;
3396   my $dbh = dbh;
3397   
3398   #while ( $columns = $csv->getline($fh) ) {
3399   my $line;
3400   while ( defined($line=<$fh>) ) {
3401
3402     $csv->parse($line) or do {
3403       $dbh->rollback if $oldAutoCommit;
3404       return "can't parse: ". $csv->error_input();
3405     };
3406
3407     my @columns = $csv->fields();
3408     #warn join('-',@columns);
3409
3410     my %row = ();
3411     foreach my $field ( @fields ) {
3412       $row{$field} = shift @columns;
3413     }
3414
3415     my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
3416     unless ( $cust_main ) {
3417       $dbh->rollback if $oldAutoCommit;
3418       return "unknown custnum $row{'custnum'}";
3419     }
3420
3421     if ( $row{'amount'} > 0 ) {
3422       my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
3423       if ( $error ) {
3424         $dbh->rollback if $oldAutoCommit;
3425         return $error;
3426       }
3427       $imported++;
3428     } elsif ( $row{'amount'} < 0 ) {
3429       my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
3430                                       $row{'pkg'}                         );
3431       if ( $error ) {
3432         $dbh->rollback if $oldAutoCommit;
3433         return $error;
3434       }
3435       $imported++;
3436     } else {
3437       #hmm?
3438     }
3439
3440   }
3441
3442   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3443
3444   return "Empty file!" unless $imported;
3445
3446   ''; #no error
3447
3448 }
3449
3450 =back
3451
3452 =head1 BUGS
3453
3454 The delete method.
3455
3456 The delete method should possibly take an FS::cust_main object reference
3457 instead of a scalar customer number.
3458
3459 Bill and collect options should probably be passed as references instead of a
3460 list.
3461
3462 There should probably be a configuration file with a list of allowed credit
3463 card types.
3464
3465 No multiple currency support (probably a larger project than just this module).
3466
3467 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
3468
3469 =head1 SEE ALSO
3470
3471 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
3472 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
3473 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.
3474
3475 =cut
3476
3477 1;
3478