88dd0e54b706b85e3d9602d4a4c8a6c009aaae2c
[freeside.git] / FS / FS / cust_main.pm
1 package FS::cust_main;
2
3 require 5.006;
4 use strict;
5 use base qw( FS::cust_main::Packages FS::cust_main::Status
6              FS::cust_main::NationalID
7              FS::cust_main::Billing FS::cust_main::Billing_Realtime
8              FS::cust_main::Billing_Discount
9              FS::cust_main::Billing_ThirdParty
10              FS::cust_main::Location
11              FS::cust_main::Credit_Limit
12              FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
13              FS::geocode_Mixin FS::Quotable_Mixin FS::Sales_Mixin
14              FS::o2m_Common
15              FS::Record
16            );
17 use vars qw( $DEBUG $me $conf $default_agent_custid $custnum_display_length
18              @encrypted_fields
19              $import
20              $ignore_expired_card $ignore_banned_card $ignore_illegal_zip
21              $skip_fuzzyfiles
22              @paytypes
23            );
24 use Carp;
25 use Scalar::Util qw( blessed );
26 use Time::Local qw(timelocal);
27 use Storable qw(thaw);
28 use MIME::Base64;
29 use Data::Dumper;
30 use Tie::IxHash;
31 use Digest::MD5 qw(md5_base64);
32 use Date::Format;
33 #use Date::Manip;
34 use File::Temp; #qw( tempfile );
35 use Email::Address;
36 use Business::CreditCard 0.28;
37 use FS::UID qw( getotaker dbh driver_name );
38 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
39 use FS::Misc qw( generate_email send_email generate_ps do_print money_pretty card_types );
40 use FS::Msgcat qw(gettext);
41 use FS::CurrentUser;
42 use FS::TicketSystem;
43 use FS::payby;
44 use FS::cust_pkg;
45 use FS::cust_svc;
46 use FS::cust_bill;
47 use FS::cust_bill_void;
48 use FS::legacy_cust_bill;
49 use FS::cust_pay;
50 use FS::cust_pay_pending;
51 use FS::cust_pay_void;
52 use FS::cust_pay_batch;
53 use FS::cust_credit;
54 use FS::cust_refund;
55 use FS::part_referral;
56 use FS::cust_main_county;
57 use FS::cust_location;
58 use FS::cust_class;
59 use FS::cust_main_exemption;
60 use FS::cust_tax_adjustment;
61 use FS::cust_tax_location;
62 use FS::agent;
63 use FS::cust_main_invoice;
64 use FS::cust_tag;
65 use FS::prepay_credit;
66 use FS::queue;
67 use FS::part_pkg;
68 use FS::part_export;
69 #use FS::cust_event;
70 use FS::type_pkgs;
71 use FS::payment_gateway;
72 use FS::agent_payment_gateway;
73 use FS::banned_pay;
74 use FS::cust_main_note;
75 use FS::cust_attachment;
76 use FS::contact;
77 use FS::Locales;
78 use FS::upgrade_journal;
79 use FS::reason;
80
81 # 1 is mostly method/subroutine entry and options
82 # 2 traces progress of some operations
83 # 3 is even more information including possibly sensitive data
84 $DEBUG = 0;
85 $me = '[FS::cust_main]';
86
87 $import = 0;
88 $ignore_expired_card = 0;
89 $ignore_banned_card = 0;
90
91 $skip_fuzzyfiles = 0;
92
93 @encrypted_fields = ('payinfo', 'paycvv');
94 sub nohistory_fields { ('payinfo', 'paycvv'); }
95
96 @paytypes = ('', 'Personal checking', 'Personal savings', 'Business checking', 'Business savings');
97
98 #ask FS::UID to run this stuff for us later
99 #$FS::UID::callback{'FS::cust_main'} = sub { 
100 install_callback FS::UID sub { 
101   $conf = new FS::Conf;
102   $default_agent_custid   = $conf->exists('cust_main-default_agent_custid');
103   $custnum_display_length = $conf->config('cust_main-custnum-display_length');
104 };
105
106 sub _cache {
107   my $self = shift;
108   my ( $hashref, $cache ) = @_;
109   if ( exists $hashref->{'pkgnum'} ) {
110     #@{ $self->{'_pkgnum'} } = ();
111     my $subcache = $cache->subcache( 'pkgnum', 'cust_pkg', $hashref->{custnum});
112     $self->{'_pkgnum'} = $subcache;
113     #push @{ $self->{'_pkgnum'} },
114     FS::cust_pkg->new_or_cached($hashref, $subcache) if $hashref->{pkgnum};
115   }
116 }
117
118 =head1 NAME
119
120 FS::cust_main - Object methods for cust_main records
121
122 =head1 SYNOPSIS
123
124   use FS::cust_main;
125
126   $record = new FS::cust_main \%hash;
127   $record = new FS::cust_main { 'column' => 'value' };
128
129   $error = $record->insert;
130
131   $error = $new_record->replace($old_record);
132
133   $error = $record->delete;
134
135   $error = $record->check;
136
137   @cust_pkg = $record->all_pkgs;
138
139   @cust_pkg = $record->ncancelled_pkgs;
140
141   @cust_pkg = $record->suspended_pkgs;
142
143   $error = $record->bill;
144   $error = $record->bill %options;
145   $error = $record->bill 'time' => $time;
146
147   $error = $record->collect;
148   $error = $record->collect %options;
149   $error = $record->collect 'invoice_time'   => $time,
150                           ;
151
152 =head1 DESCRIPTION
153
154 An FS::cust_main object represents a customer.  FS::cust_main inherits from 
155 FS::Record.  The following fields are currently supported:
156
157 =over 4
158
159 =item custnum
160
161 Primary key (assigned automatically for new customers)
162
163 =item agentnum
164
165 Agent (see L<FS::agent>)
166
167 =item refnum
168
169 Advertising source (see L<FS::part_referral>)
170
171 =item first
172
173 First name
174
175 =item last
176
177 Last name
178
179 =item ss
180
181 Cocial security number (optional)
182
183 =item company
184
185 (optional)
186
187 =item daytime
188
189 phone (optional)
190
191 =item night
192
193 phone (optional)
194
195 =item fax
196
197 phone (optional)
198
199 =item mobile
200
201 phone (optional)
202
203 =item payby
204
205 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
206
207 =item payinfo
208
209 Payment Information (See L<FS::payinfo_Mixin> for data format)
210
211 =item paymask
212
213 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
214
215 =item paycvv
216
217 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
218
219 =item paydate
220
221 Expiration date, mm/yyyy, m/yyyy, mm/yy or m/yy
222
223 =item paystart_month
224
225 Start date month (maestro/solo cards only)
226
227 =item paystart_year
228
229 Start date year (maestro/solo cards only)
230
231 =item payissue
232
233 Issue number (maestro/solo cards only)
234
235 =item payname
236
237 Name on card or billing name
238
239 =item payip
240
241 IP address from which payment information was received
242
243 =item paycardtype
244
245 The credit card type (deduced from the card number).
246
247 =item tax
248
249 Tax exempt, empty or `Y'
250
251 =item usernum
252
253 Order taker (see L<FS::access_user>)
254
255 =item comments
256
257 Comments (optional)
258
259 =item referral_custnum
260
261 Referring customer number
262
263 =item spool_cdr
264
265 Enable individual CDR spooling, empty or `Y'
266
267 =item dundate
268
269 A suggestion to events (see L<FS::part_bill_event">) to delay until this unix timestamp
270
271 =item squelch_cdr
272
273 Discourage individual CDR printing, empty or `Y'
274
275 =item edit_subject
276
277 Allow self-service editing of ticket subjects, empty or 'Y'
278
279 =item calling_list_exempt
280
281 Do not call, empty or 'Y'
282
283 =item invoice_ship_address
284
285 Display ship_address ("Service address") on invoices for this customer, empty or 'Y'
286
287 =back
288
289 =head1 METHODS
290
291 =over 4
292
293 =item new HASHREF
294
295 Creates a new customer.  To add the customer to the database, see L<"insert">.
296
297 Note that this stores the hash reference, not a distinct copy of the hash it
298 points to.  You can ask the object for a copy with the I<hash> method.
299
300 =cut
301
302 sub table { 'cust_main'; }
303
304 =item insert [ CUST_PKG_HASHREF [ , INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
305
306 Adds this customer to the database.  If there is an error, returns the error,
307 otherwise returns false.
308
309 Usually the customer's location will not yet exist in the database, and
310 the C<bill_location> and C<ship_location> pseudo-fields must be set to 
311 uninserted L<FS::cust_location> objects.  These will be inserted and linked
312 (in both directions) to the new customer record.  If they're references 
313 to the same object, they will become the same location.
314
315 CUST_PKG_HASHREF: If you pass a Tie::RefHash data structure to the insert
316 method containing FS::cust_pkg and FS::svc_I<tablename> objects, all records
317 are inserted atomicly, or the transaction is rolled back.  Passing an empty
318 hash reference is equivalent to not supplying this parameter.  There should be
319 a better explanation of this, but until then, here's an example:
320
321   use Tie::RefHash;
322   tie %hash, 'Tie::RefHash'; #this part is important
323   %hash = (
324     $cust_pkg => [ $svc_acct ],
325     ...
326   );
327   $cust_main->insert( \%hash );
328
329 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
330 be set as the invoicing list (see L<"invoicing_list">).  Errors return as
331 expected and rollback the entire transaction; it is not necessary to call 
332 check_invoicing_list first.  The invoicing_list is set after the records in the
333 CUST_PKG_HASHREF above are inserted, so it is now possible to set an
334 invoicing_list destination to the newly-created svc_acct.  Here's an example:
335
336   $cust_main->insert( {}, [ $email, 'POST' ] );
337
338 Currently available options are: I<depend_jobnum>, I<noexport>,
339 I<tax_exemption> and I<prospectnum>.
340
341 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
342 on the supplied jobnum (they will not run until the specific job completes).
343 This can be used to defer provisioning until some action completes (such
344 as running the customer's credit card successfully).
345
346 The I<noexport> option is deprecated.  If I<noexport> is set true, no
347 provisioning jobs (exports) are scheduled.  (You can schedule them later with
348 the B<reexport> method.)
349
350 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
351 of tax names and exemption numbers.  FS::cust_main_exemption records will be
352 created and inserted.
353
354 If I<prospectnum> is set, moves contacts and locations from that prospect.
355
356 =cut
357
358 sub insert {
359   my $self = shift;
360   my $cust_pkgs = @_ ? shift : {};
361   my $invoicing_list = @_ ? shift : '';
362   my %options = @_;
363   warn "$me insert called with options ".
364        join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
365     if $DEBUG;
366
367   local $SIG{HUP} = 'IGNORE';
368   local $SIG{INT} = 'IGNORE';
369   local $SIG{QUIT} = 'IGNORE';
370   local $SIG{TERM} = 'IGNORE';
371   local $SIG{TSTP} = 'IGNORE';
372   local $SIG{PIPE} = 'IGNORE';
373
374   my $oldAutoCommit = $FS::UID::AutoCommit;
375   local $FS::UID::AutoCommit = 0;
376   my $dbh = dbh;
377
378   my $prepay_identifier = '';
379   my( $amount, $seconds, $upbytes, $downbytes, $totalbytes ) = (0, 0, 0, 0, 0);
380   my $payby = '';
381   if ( $self->payby eq 'PREPAY' ) {
382
383     $self->payby('BILL');
384     $prepay_identifier = $self->payinfo;
385     $self->payinfo('');
386
387     warn "  looking up prepaid card $prepay_identifier\n"
388       if $DEBUG > 1;
389
390     my $error = $self->get_prepay( $prepay_identifier,
391                                    'amount_ref'     => \$amount,
392                                    'seconds_ref'    => \$seconds,
393                                    'upbytes_ref'    => \$upbytes,
394                                    'downbytes_ref'  => \$downbytes,
395                                    'totalbytes_ref' => \$totalbytes,
396                                  );
397     if ( $error ) {
398       $dbh->rollback if $oldAutoCommit;
399       #return "error applying prepaid card (transaction rolled back): $error";
400       return $error;
401     }
402
403     $payby = 'PREP' if $amount;
404
405   } elsif ( $self->payby =~ /^(CASH|WEST|MCRD|MCHK|PPAL)$/ ) {
406
407     $payby = $1;
408     $self->payby('BILL');
409     $amount = $self->paid;
410
411   }
412
413   # insert locations
414   foreach my $l (qw(bill_location ship_location)) {
415
416     my $loc = delete $self->hashref->{$l} or return "$l not set";
417     
418     if ( !$loc->locationnum ) {
419       # warn the location that we're going to insert it with no custnum
420       $loc->set(custnum_pending => 1);
421       warn "  inserting $l\n"
422         if $DEBUG > 1;
423       my $error = $loc->insert;
424       if ( $error ) {
425         $dbh->rollback if $oldAutoCommit;
426         my $label = $l eq 'ship_location' ? 'service' : 'billing';
427         return "$error (in $label location)";
428       }
429
430     } elsif ( $loc->prospectnum ) {
431
432       $loc->prospectnum('');
433       $loc->set(custnum_pending => 1);
434       my $error = $loc->replace;
435       if ( $error ) {
436         $dbh->rollback if $oldAutoCommit;
437         my $label = $l eq 'ship_location' ? 'service' : 'billing';
438         return "$error (moving $label location)";
439       }
440
441     } elsif ( ($loc->custnum || 0) > 0 ) {
442       # then it somehow belongs to another customer--shouldn't happen
443       $dbh->rollback if $oldAutoCommit;
444       return "$l belongs to customer ".$loc->custnum;
445     }
446     # else it already belongs to this customer 
447     # (happens when ship_location is identical to bill_location)
448
449     $self->set($l.'num', $loc->locationnum);
450
451     if ( $self->get($l.'num') eq '' ) {
452       $dbh->rollback if $oldAutoCommit;
453       return "$l not set";
454     }
455   }
456
457   warn "  inserting $self\n"
458     if $DEBUG > 1;
459
460   $self->signupdate(time) unless $self->signupdate;
461
462   $self->auto_agent_custid()
463     if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
464
465   my $error =  $self->check_payinfo_cardtype
466             || $self->SUPER::insert;
467   if ( $error ) {
468     $dbh->rollback if $oldAutoCommit;
469     #return "inserting cust_main record (transaction rolled back): $error";
470     return $error;
471   }
472
473   # now set cust_location.custnum
474   foreach my $l (qw(bill_location ship_location)) {
475     warn "  setting $l.custnum\n"
476       if $DEBUG > 1;
477     my $loc = $self->$l;
478     unless ( $loc->custnum ) {
479       $loc->set(custnum => $self->custnum);
480       $error ||= $loc->replace;
481     }
482
483     if ( $error ) {
484       $dbh->rollback if $oldAutoCommit;
485       return "error setting $l custnum: $error";
486     }
487   }
488
489   warn "  setting invoicing list\n"
490     if $DEBUG > 1;
491
492   if ( $invoicing_list ) {
493     $error = $self->check_invoicing_list( $invoicing_list );
494     if ( $error ) {
495       $dbh->rollback if $oldAutoCommit;
496       #return "checking invoicing_list (transaction rolled back): $error";
497       return $error;
498     }
499     $self->invoicing_list( $invoicing_list );
500   }
501
502   warn "  setting customer tags\n"
503     if $DEBUG > 1;
504
505   foreach my $tagnum ( @{ $self->tagnum || [] } ) {
506     my $cust_tag = new FS::cust_tag { 'tagnum'  => $tagnum,
507                                       'custnum' => $self->custnum };
508     my $error = $cust_tag->insert;
509     if ( $error ) {
510       $dbh->rollback if $oldAutoCommit;
511       return $error;
512     }
513   }
514
515   my $prospectnum = delete $options{'prospectnum'};
516   if ( $prospectnum ) {
517
518     warn "  moving contacts and locations from prospect $prospectnum\n"
519       if $DEBUG > 1;
520
521     my $prospect_main =
522       qsearchs('prospect_main', { 'prospectnum' => $prospectnum } );
523     unless ( $prospect_main ) {
524       $dbh->rollback if $oldAutoCommit;
525       return "Unknown prospectnum $prospectnum";
526     }
527     $prospect_main->custnum($self->custnum);
528     $prospect_main->disabled('Y');
529     my $error = $prospect_main->replace;
530     if ( $error ) {
531       $dbh->rollback if $oldAutoCommit;
532       return $error;
533     }
534
535     my @contact = $prospect_main->contact;
536     my @cust_location = $prospect_main->cust_location;
537     my @qual = $prospect_main->qual;
538
539     foreach my $r ( @contact, @cust_location, @qual ) {
540       $r->prospectnum('');
541       $r->custnum($self->custnum);
542       my $error = $r->replace;
543       if ( $error ) {
544         $dbh->rollback if $oldAutoCommit;
545         return $error;
546       }
547     }
548
549   }
550
551   # validate card (needs custnum already set)
552   if ( $self->payby =~ /^(CARD|DCRD)$/
553        && $conf->exists('business-onlinepayment-verification') ) {
554     $error = $self->realtime_verify_bop({ 'method'=>'CC' });
555     if ( $error ) {
556       $dbh->rollback if $oldAutoCommit;
557       return $error;
558     }
559   }
560
561   warn "  setting contacts\n"
562     if $DEBUG > 1;
563
564   if ( my $contact = delete $options{'contact'} ) {
565
566     foreach my $c ( @$contact ) {
567       $c->custnum($self->custnum);
568       my $error = $c->insert;
569       if ( $error ) {
570         $dbh->rollback if $oldAutoCommit;
571         return $error;
572       }
573
574     }
575
576   } elsif ( my $contact_params = delete $options{'contact_params'} ) {
577
578     my $error = $self->process_o2m( 'table'  => 'contact',
579                                     'fields' => FS::contact->cgi_contact_fields,
580                                     'params' => $contact_params,
581                                   );
582     if ( $error ) {
583       $dbh->rollback if $oldAutoCommit;
584       return $error;
585     }
586   }
587
588   warn "  setting cust_main_exemption\n"
589     if $DEBUG > 1;
590
591   my $tax_exemption = delete $options{'tax_exemption'};
592   if ( $tax_exemption ) {
593
594     $tax_exemption = { map { $_ => '' } @$tax_exemption }
595       if ref($tax_exemption) eq 'ARRAY';
596
597     foreach my $taxname ( keys %$tax_exemption ) {
598       my $cust_main_exemption = new FS::cust_main_exemption {
599         'custnum'       => $self->custnum,
600         'taxname'       => $taxname,
601         'exempt_number' => $tax_exemption->{$taxname},
602       };
603       my $error = $cust_main_exemption->insert;
604       if ( $error ) {
605         $dbh->rollback if $oldAutoCommit;
606         return "inserting cust_main_exemption (transaction rolled back): $error";
607       }
608     }
609   }
610
611   warn "  ordering packages\n"
612     if $DEBUG > 1;
613
614   $error = $self->order_pkgs( $cust_pkgs,
615                               %options,
616                               'seconds_ref'    => \$seconds,
617                               'upbytes_ref'    => \$upbytes,
618                               'downbytes_ref'  => \$downbytes,
619                               'totalbytes_ref' => \$totalbytes,
620                             );
621   if ( $error ) {
622     $dbh->rollback if $oldAutoCommit;
623     return $error;
624   }
625
626   if ( $seconds ) {
627     $dbh->rollback if $oldAutoCommit;
628     return "No svc_acct record to apply pre-paid time";
629   }
630   if ( $upbytes || $downbytes || $totalbytes ) {
631     $dbh->rollback if $oldAutoCommit;
632     return "No svc_acct record to apply pre-paid data";
633   }
634
635   if ( $amount ) {
636     warn "  inserting initial $payby payment of $amount\n"
637       if $DEBUG > 1;
638     $error = $self->insert_cust_pay($payby, $amount, $prepay_identifier);
639     if ( $error ) {
640       $dbh->rollback if $oldAutoCommit;
641       return "inserting payment (transaction rolled back): $error";
642     }
643   }
644
645   unless ( $import || $skip_fuzzyfiles ) {
646     warn "  queueing fuzzyfiles update\n"
647       if $DEBUG > 1;
648     $error = $self->queue_fuzzyfiles_update;
649     if ( $error ) {
650       $dbh->rollback if $oldAutoCommit;
651       return "updating fuzzy search cache: $error";
652     }
653   }
654
655   # FS::geocode_Mixin::after_insert or something?
656   if ( $conf->config('tax_district_method') and !$import ) {
657     # if anything non-empty, try to look it up
658     my $queue = new FS::queue {
659       'job'     => 'FS::geocode_Mixin::process_district_update',
660       'custnum' => $self->custnum,
661     };
662     my $error = $queue->insert( ref($self), $self->custnum );
663     if ( $error ) {
664       $dbh->rollback if $oldAutoCommit;
665       return "queueing tax district update: $error";
666     }
667   }
668
669   # cust_main exports!
670   warn "  exporting\n" if $DEBUG > 1;
671
672   my $export_args = $options{'export_args'} || [];
673
674   my @part_export =
675     map qsearch( 'part_export', {exportnum=>$_} ),
676       $conf->config('cust_main-exports'); #, $agentnum
677
678   foreach my $part_export ( @part_export ) {
679     my $error = $part_export->export_insert($self, @$export_args);
680     if ( $error ) {
681       $dbh->rollback if $oldAutoCommit;
682       return "exporting to ". $part_export->exporttype.
683              " (transaction rolled back): $error";
684     }
685   }
686
687   #foreach my $depend_jobnum ( @$depend_jobnums ) {
688   #    warn "[$me] inserting dependancies on supplied job $depend_jobnum\n"
689   #      if $DEBUG;
690   #    foreach my $jobnum ( @jobnums ) {
691   #      my $queue = qsearchs('queue', { 'jobnum' => $jobnum } );
692   #      warn "[$me] inserting dependancy for job $jobnum on $depend_jobnum\n"
693   #        if $DEBUG;
694   #      my $error = $queue->depend_insert($depend_jobnum);
695   #      if ( $error ) {
696   #        $dbh->rollback if $oldAutoCommit;
697   #        return "error queuing job dependancy: $error";
698   #      }
699   #    }
700   #  }
701   #
702   #}
703   #
704   #if ( exists $options{'jobnums'} ) {
705   #  push @{ $options{'jobnums'} }, @jobnums;
706   #}
707
708   warn "  insert complete; committing transaction\n"
709     if $DEBUG > 1;
710
711   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
712   '';
713
714 }
715
716 use File::CounterFile;
717 sub auto_agent_custid {
718   my $self = shift;
719
720   my $format = $conf->config('cust_main-auto_agent_custid');
721   my $agent_custid;
722   if ( $format eq '1YMMXXXXXXXX' ) {
723
724     my $counter = new File::CounterFile 'cust_main.agent_custid';
725     $counter->lock;
726
727     my $ym = 100000000000 + time2str('%y%m00000000', time);
728     if ( $ym > $counter->value ) {
729       $counter->{'value'} = $agent_custid = $ym;
730       $counter->{'updated'} = 1;
731     } else {
732       $agent_custid = $counter->inc;
733     }
734
735     $counter->unlock;
736
737   } else {
738     die "Unknown cust_main-auto_agent_custid format: $format";
739   }
740
741   $self->agent_custid($agent_custid);
742
743 }
744
745 =item PACKAGE METHODS
746
747 Documentation on customer package methods has been moved to
748 L<FS::cust_main::Packages>.
749
750 =item recharge_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , AMOUNTREF, SECONDSREF, UPBYTEREF, DOWNBYTEREF ]
751
752 Recharges this (existing) customer with the specified prepaid card (see
753 L<FS::prepay_credit>), specified either by I<identifier> or as an
754 FS::prepay_credit object.  If there is an error, returns the error, otherwise
755 returns false.
756
757 Optionally, five scalar references can be passed as well.  They will have their
758 values filled in with the amount, number of seconds, and number of upload,
759 download, and total bytes applied by this prepaid card.
760
761 =cut
762
763 #the ref bullshit here should be refactored like get_prepay.  MyAccount.pm is
764 #the only place that uses these args
765 sub recharge_prepay { 
766   my( $self, $prepay_credit, $amountref, $secondsref, 
767       $upbytesref, $downbytesref, $totalbytesref ) = @_;
768
769   local $SIG{HUP} = 'IGNORE';
770   local $SIG{INT} = 'IGNORE';
771   local $SIG{QUIT} = 'IGNORE';
772   local $SIG{TERM} = 'IGNORE';
773   local $SIG{TSTP} = 'IGNORE';
774   local $SIG{PIPE} = 'IGNORE';
775
776   my $oldAutoCommit = $FS::UID::AutoCommit;
777   local $FS::UID::AutoCommit = 0;
778   my $dbh = dbh;
779
780   my( $amount, $seconds, $upbytes, $downbytes, $totalbytes) = ( 0, 0, 0, 0, 0 );
781
782   my $error = $self->get_prepay( $prepay_credit,
783                                  'amount_ref'     => \$amount,
784                                  'seconds_ref'    => \$seconds,
785                                  'upbytes_ref'    => \$upbytes,
786                                  'downbytes_ref'  => \$downbytes,
787                                  'totalbytes_ref' => \$totalbytes,
788                                )
789            || $self->increment_seconds($seconds)
790            || $self->increment_upbytes($upbytes)
791            || $self->increment_downbytes($downbytes)
792            || $self->increment_totalbytes($totalbytes)
793            || $self->insert_cust_pay_prepay( $amount,
794                                              ref($prepay_credit)
795                                                ? $prepay_credit->identifier
796                                                : $prepay_credit
797                                            );
798
799   if ( $error ) {
800     $dbh->rollback if $oldAutoCommit;
801     return $error;
802   }
803
804   if ( defined($amountref)  ) { $$amountref  = $amount;  }
805   if ( defined($secondsref) ) { $$secondsref = $seconds; }
806   if ( defined($upbytesref) ) { $$upbytesref = $upbytes; }
807   if ( defined($downbytesref) ) { $$downbytesref = $downbytes; }
808   if ( defined($totalbytesref) ) { $$totalbytesref = $totalbytes; }
809
810   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
811   '';
812
813 }
814
815 =item get_prepay IDENTIFIER | PREPAY_CREDIT_OBJ [ , OPTION => VALUE ... ]
816
817 Looks up and deletes a prepaid card (see L<FS::prepay_credit>),
818 specified either by I<identifier> or as an FS::prepay_credit object.
819
820 Available options are: I<amount_ref>, I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>.  The scalars (provided by references) will be
821 incremented by the values of the prepaid card.
822
823 If the prepaid card specifies an I<agentnum> (see L<FS::agent>), it is used to
824 check or set this customer's I<agentnum>.
825
826 If there is an error, returns the error, otherwise returns false.
827
828 =cut
829
830
831 sub get_prepay {
832   my( $self, $prepay_credit, %opt ) = @_;
833
834   local $SIG{HUP} = 'IGNORE';
835   local $SIG{INT} = 'IGNORE';
836   local $SIG{QUIT} = 'IGNORE';
837   local $SIG{TERM} = 'IGNORE';
838   local $SIG{TSTP} = 'IGNORE';
839   local $SIG{PIPE} = 'IGNORE';
840
841   my $oldAutoCommit = $FS::UID::AutoCommit;
842   local $FS::UID::AutoCommit = 0;
843   my $dbh = dbh;
844
845   unless ( ref($prepay_credit) ) {
846
847     my $identifier = $prepay_credit;
848
849     $prepay_credit = qsearchs(
850       'prepay_credit',
851       { 'identifier' => $identifier },
852       '',
853       'FOR UPDATE'
854     );
855
856     unless ( $prepay_credit ) {
857       $dbh->rollback if $oldAutoCommit;
858       return "Invalid prepaid card: ". $identifier;
859     }
860
861   }
862
863   if ( $prepay_credit->agentnum ) {
864     if ( $self->agentnum && $self->agentnum != $prepay_credit->agentnum ) {
865       $dbh->rollback if $oldAutoCommit;
866       return "prepaid card not valid for agent ". $self->agentnum;
867     }
868     $self->agentnum($prepay_credit->agentnum);
869   }
870
871   my $error = $prepay_credit->delete;
872   if ( $error ) {
873     $dbh->rollback if $oldAutoCommit;
874     return "removing prepay_credit (transaction rolled back): $error";
875   }
876
877   ${ $opt{$_.'_ref'} } += $prepay_credit->$_()
878     for grep $opt{$_.'_ref'}, qw( amount seconds upbytes downbytes totalbytes );
879
880   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
881   '';
882
883 }
884
885 =item increment_upbytes SECONDS
886
887 Updates this customer's single or primary account (see L<FS::svc_acct>) by
888 the specified number of upbytes.  If there is an error, returns the error,
889 otherwise returns false.
890
891 =cut
892
893 sub increment_upbytes {
894   _increment_column( shift, 'upbytes', @_);
895 }
896
897 =item increment_downbytes SECONDS
898
899 Updates this customer's single or primary account (see L<FS::svc_acct>) by
900 the specified number of downbytes.  If there is an error, returns the error,
901 otherwise returns false.
902
903 =cut
904
905 sub increment_downbytes {
906   _increment_column( shift, 'downbytes', @_);
907 }
908
909 =item increment_totalbytes SECONDS
910
911 Updates this customer's single or primary account (see L<FS::svc_acct>) by
912 the specified number of totalbytes.  If there is an error, returns the error,
913 otherwise returns false.
914
915 =cut
916
917 sub increment_totalbytes {
918   _increment_column( shift, 'totalbytes', @_);
919 }
920
921 =item increment_seconds SECONDS
922
923 Updates this customer's single or primary account (see L<FS::svc_acct>) by
924 the specified number of seconds.  If there is an error, returns the error,
925 otherwise returns false.
926
927 =cut
928
929 sub increment_seconds {
930   _increment_column( shift, 'seconds', @_);
931 }
932
933 =item _increment_column AMOUNT
934
935 Updates this customer's single or primary account (see L<FS::svc_acct>) by
936 the specified number of seconds or bytes.  If there is an error, returns
937 the error, otherwise returns false.
938
939 =cut
940
941 sub _increment_column {
942   my( $self, $column, $amount ) = @_;
943   warn "$me increment_column called: $column, $amount\n"
944     if $DEBUG;
945
946   return '' unless $amount;
947
948   my @cust_pkg = grep { $_->part_pkg->svcpart('svc_acct') }
949                       $self->ncancelled_pkgs;
950
951   if ( ! @cust_pkg ) {
952     return 'No packages with primary or single services found'.
953            ' to apply pre-paid time';
954   } elsif ( scalar(@cust_pkg) > 1 ) {
955     #maybe have a way to specify the package/account?
956     return 'Multiple packages found to apply pre-paid time';
957   }
958
959   my $cust_pkg = $cust_pkg[0];
960   warn "  found package pkgnum ". $cust_pkg->pkgnum. "\n"
961     if $DEBUG > 1;
962
963   my @cust_svc =
964     $cust_pkg->cust_svc( $cust_pkg->part_pkg->svcpart('svc_acct') );
965
966   if ( ! @cust_svc ) {
967     return 'No account found to apply pre-paid time';
968   } elsif ( scalar(@cust_svc) > 1 ) {
969     return 'Multiple accounts found to apply pre-paid time';
970   }
971   
972   my $svc_acct = $cust_svc[0]->svc_x;
973   warn "  found service svcnum ". $svc_acct->pkgnum.
974        ' ('. $svc_acct->email. ")\n"
975     if $DEBUG > 1;
976
977   $column = "increment_$column";
978   $svc_acct->$column($amount);
979
980 }
981
982 =item insert_cust_pay_prepay AMOUNT [ PAYINFO ]
983
984 Inserts a prepayment in the specified amount for this customer.  An optional
985 second argument can specify the prepayment identifier for tracking purposes.
986 If there is an error, returns the error, otherwise returns false.
987
988 =cut
989
990 sub insert_cust_pay_prepay {
991   shift->insert_cust_pay('PREP', @_);
992 }
993
994 =item insert_cust_pay_cash AMOUNT [ PAYINFO ]
995
996 Inserts a cash payment in the specified amount for this customer.  An optional
997 second argument can specify the payment identifier for tracking purposes.
998 If there is an error, returns the error, otherwise returns false.
999
1000 =cut
1001
1002 sub insert_cust_pay_cash {
1003   shift->insert_cust_pay('CASH', @_);
1004 }
1005
1006 =item insert_cust_pay_west AMOUNT [ PAYINFO ]
1007
1008 Inserts a Western Union payment in the specified amount for this customer.  An
1009 optional second argument can specify the prepayment identifier for tracking
1010 purposes.  If there is an error, returns the error, otherwise returns false.
1011
1012 =cut
1013
1014 sub insert_cust_pay_west {
1015   shift->insert_cust_pay('WEST', @_);
1016 }
1017
1018 sub insert_cust_pay {
1019   my( $self, $payby, $amount ) = splice(@_, 0, 3);
1020   my $payinfo = scalar(@_) ? shift : '';
1021
1022   my $cust_pay = new FS::cust_pay {
1023     'custnum' => $self->custnum,
1024     'paid'    => sprintf('%.2f', $amount),
1025     #'_date'   => #date the prepaid card was purchased???
1026     'payby'   => $payby,
1027     'payinfo' => $payinfo,
1028   };
1029   $cust_pay->insert;
1030
1031 }
1032
1033 =item reexport
1034
1035 This method is deprecated.  See the I<depend_jobnum> option to the insert and
1036 order_pkgs methods for a better way to defer provisioning.
1037
1038 Re-schedules all exports by calling the B<reexport> method of all associated
1039 packages (see L<FS::cust_pkg>).  If there is an error, returns the error;
1040 otherwise returns false.
1041
1042 =cut
1043
1044 sub reexport {
1045   my $self = shift;
1046
1047   carp "WARNING: FS::cust_main::reexport is deprectated; ".
1048        "use the depend_jobnum option to insert or order_pkgs to delay export";
1049
1050   local $SIG{HUP} = 'IGNORE';
1051   local $SIG{INT} = 'IGNORE';
1052   local $SIG{QUIT} = 'IGNORE';
1053   local $SIG{TERM} = 'IGNORE';
1054   local $SIG{TSTP} = 'IGNORE';
1055   local $SIG{PIPE} = 'IGNORE';
1056
1057   my $oldAutoCommit = $FS::UID::AutoCommit;
1058   local $FS::UID::AutoCommit = 0;
1059   my $dbh = dbh;
1060
1061   foreach my $cust_pkg ( $self->ncancelled_pkgs ) {
1062     my $error = $cust_pkg->reexport;
1063     if ( $error ) {
1064       $dbh->rollback if $oldAutoCommit;
1065       return $error;
1066     }
1067   }
1068
1069   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1070   '';
1071
1072 }
1073
1074 =item delete [ OPTION => VALUE ... ]
1075
1076 This deletes the customer.  If there is an error, returns the error, otherwise
1077 returns false.
1078
1079 This will completely remove all traces of the customer record.  This is not
1080 what you want when a customer cancels service; for that, cancel all of the
1081 customer's packages (see L</cancel>).
1082
1083 If the customer has any uncancelled packages, you need to pass a new (valid)
1084 customer number for those packages to be transferred to, as the "new_customer"
1085 option.  Cancelled packages will be deleted.  Did I mention that this is NOT
1086 what you want when a customer cancels service and that you really should be
1087 looking at L<FS::cust_pkg/cancel>?  
1088
1089 You can't delete a customer with invoices (see L<FS::cust_bill>),
1090 statements (see L<FS::cust_statement>), credits (see L<FS::cust_credit>),
1091 payments (see L<FS::cust_pay>) or refunds (see L<FS::cust_refund>), unless you
1092 set the "delete_financials" option to a true value.
1093
1094 =cut
1095
1096 sub delete {
1097   my( $self, %opt ) = @_;
1098
1099   local $SIG{HUP} = 'IGNORE';
1100   local $SIG{INT} = 'IGNORE';
1101   local $SIG{QUIT} = 'IGNORE';
1102   local $SIG{TERM} = 'IGNORE';
1103   local $SIG{TSTP} = 'IGNORE';
1104   local $SIG{PIPE} = 'IGNORE';
1105
1106   my $oldAutoCommit = $FS::UID::AutoCommit;
1107   local $FS::UID::AutoCommit = 0;
1108   my $dbh = dbh;
1109
1110   if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1111      $dbh->rollback if $oldAutoCommit;
1112      return "Can't delete a master agent customer";
1113   }
1114
1115   #use FS::access_user
1116   if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1117      $dbh->rollback if $oldAutoCommit;
1118      return "Can't delete a master employee customer";
1119   }
1120
1121   tie my %financial_tables, 'Tie::IxHash',
1122     'cust_bill'      => 'invoices',
1123     'cust_statement' => 'statements',
1124     'cust_credit'    => 'credits',
1125     'cust_pay'       => 'payments',
1126     'cust_refund'    => 'refunds',
1127   ;
1128    
1129   foreach my $table ( keys %financial_tables ) {
1130
1131     my @records = $self->$table();
1132
1133     if ( @records && ! $opt{'delete_financials'} ) {
1134       $dbh->rollback if $oldAutoCommit;
1135       return "Can't delete a customer with ". $financial_tables{$table};
1136     }
1137
1138     foreach my $record ( @records ) {
1139       my $error = $record->delete;
1140       if ( $error ) {
1141         $dbh->rollback if $oldAutoCommit;
1142         return "Error deleting ". $financial_tables{$table}. ": $error\n";
1143       }
1144     }
1145
1146   }
1147
1148   my @cust_pkg = $self->ncancelled_pkgs;
1149   if ( @cust_pkg ) {
1150     my $new_custnum = $opt{'new_custnum'};
1151     unless ( qsearchs( 'cust_main', { 'custnum' => $new_custnum } ) ) {
1152       $dbh->rollback if $oldAutoCommit;
1153       return "Invalid new customer number: $new_custnum";
1154     }
1155     foreach my $cust_pkg ( @cust_pkg ) {
1156       my %hash = $cust_pkg->hash;
1157       $hash{'custnum'} = $new_custnum;
1158       my $new_cust_pkg = new FS::cust_pkg ( \%hash );
1159       my $error = $new_cust_pkg->replace($cust_pkg,
1160                                          options => { $cust_pkg->options },
1161                                         );
1162       if ( $error ) {
1163         $dbh->rollback if $oldAutoCommit;
1164         return $error;
1165       }
1166     }
1167   }
1168   my @cancelled_cust_pkg = $self->all_pkgs;
1169   foreach my $cust_pkg ( @cancelled_cust_pkg ) {
1170     my $error = $cust_pkg->delete;
1171     if ( $error ) {
1172       $dbh->rollback if $oldAutoCommit;
1173       return $error;
1174     }
1175   }
1176
1177   #cust_tax_adjustment in financials?
1178   #cust_pay_pending?  ouch
1179   #cust_recon?
1180   foreach my $table (qw(
1181     cust_main_invoice cust_main_exemption cust_tag cust_attachment contact
1182     cust_location cust_main_note cust_tax_adjustment
1183     cust_pay_void cust_pay_batch queue cust_tax_exempt
1184   )) {
1185     foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1186       my $error = $record->delete;
1187       if ( $error ) {
1188         $dbh->rollback if $oldAutoCommit;
1189         return $error;
1190       }
1191     }
1192   }
1193
1194   my $sth = $dbh->prepare(
1195     'UPDATE cust_main SET referral_custnum = NULL WHERE referral_custnum = ?'
1196   ) or do {
1197     my $errstr = $dbh->errstr;
1198     $dbh->rollback if $oldAutoCommit;
1199     return $errstr;
1200   };
1201   $sth->execute($self->custnum) or do {
1202     my $errstr = $sth->errstr;
1203     $dbh->rollback if $oldAutoCommit;
1204     return $errstr;
1205   };
1206
1207   #tickets
1208
1209   my $ticket_dbh = '';
1210   if ($conf->config('ticket_system') eq 'RT_Internal') {
1211     $ticket_dbh = $dbh;
1212   } elsif ($conf->config('ticket_system') eq 'RT_External') {
1213     my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1214     $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1215       #or die "RT_External DBI->connect error: $DBI::errstr\n";
1216   }
1217
1218   if ( $ticket_dbh ) {
1219
1220     my $ticket_sth = $ticket_dbh->prepare(
1221       'DELETE FROM Links WHERE Target = ?'
1222     ) or do {
1223       my $errstr = $ticket_dbh->errstr;
1224       $dbh->rollback if $oldAutoCommit;
1225       return $errstr;
1226     };
1227     $ticket_sth->execute('freeside://freeside/cust_main/'.$self->custnum)
1228       or do {
1229         my $errstr = $ticket_sth->errstr;
1230         $dbh->rollback if $oldAutoCommit;
1231         return $errstr;
1232       };
1233
1234     #check and see if the customer is the only link on the ticket, and
1235     #if so, set the ticket to deleted status in RT?
1236     #maybe someday, for now this will at least fix tickets not displaying
1237
1238   }
1239
1240   #delete the customer record
1241
1242   my $error = $self->SUPER::delete;
1243   if ( $error ) {
1244     $dbh->rollback if $oldAutoCommit;
1245     return $error;
1246   }
1247
1248   # cust_main exports!
1249
1250   #my $export_args = $options{'export_args'} || [];
1251
1252   my @part_export =
1253     map qsearch( 'part_export', {exportnum=>$_} ),
1254       $conf->config('cust_main-exports'); #, $agentnum
1255
1256   foreach my $part_export ( @part_export ) {
1257     my $error = $part_export->export_delete( $self ); #, @$export_args);
1258     if ( $error ) {
1259       $dbh->rollback if $oldAutoCommit;
1260       return "exporting to ". $part_export->exporttype.
1261              " (transaction rolled back): $error";
1262     }
1263   }
1264
1265   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1266   '';
1267
1268 }
1269
1270 =item merge NEW_CUSTNUM [ , OPTION => VALUE ... ]
1271
1272 This merges this customer into the provided new custnum, and then deletes the
1273 customer.  If there is an error, returns the error, otherwise returns false.
1274
1275 The source customer's name, company name, phone numbers, agent,
1276 referring customer, customer class, advertising source, order taker, and
1277 billing information (except balance) are discarded.
1278
1279 All packages are moved to the target customer.  Packages with package locations
1280 are preserved.  Packages without package locations are moved to a new package
1281 location with the source customer's service/shipping address.
1282
1283 All invoices, statements, payments, credits and refunds are moved to the target
1284 customer.  The source customer's balance is added to the target customer.
1285
1286 All notes, attachments, tickets and customer tags are moved to the target
1287 customer.
1288
1289 Change history is not currently moved.
1290
1291 =cut
1292
1293 sub merge {
1294   my( $self, $new_custnum, %opt ) = @_;
1295
1296   return "Can't merge a customer into self" if $self->custnum == $new_custnum;
1297
1298   my $new_cust_main = qsearchs( 'cust_main', { 'custnum' => $new_custnum } )
1299     or return "Invalid new customer number: $new_custnum";
1300
1301   return 'Access denied: "Merge customer across agents" access right required to merge into a customer of a different agent'
1302     if $self->agentnum != $new_cust_main->agentnum 
1303     && ! $FS::CurrentUser::CurrentUser->access_right('Merge customer across agents');
1304
1305   local $SIG{HUP} = 'IGNORE';
1306   local $SIG{INT} = 'IGNORE';
1307   local $SIG{QUIT} = 'IGNORE';
1308   local $SIG{TERM} = 'IGNORE';
1309   local $SIG{TSTP} = 'IGNORE';
1310   local $SIG{PIPE} = 'IGNORE';
1311
1312   my $oldAutoCommit = $FS::UID::AutoCommit;
1313   local $FS::UID::AutoCommit = 0;
1314   my $dbh = dbh;
1315
1316   if ( qsearch('agent', { 'agent_custnum' => $self->custnum } ) ) {
1317      $dbh->rollback if $oldAutoCommit;
1318      return "Can't merge a master agent customer";
1319   }
1320
1321   #use FS::access_user
1322   if ( qsearch('access_user', { 'user_custnum' => $self->custnum } ) ) {
1323      $dbh->rollback if $oldAutoCommit;
1324      return "Can't merge a master employee customer";
1325   }
1326
1327   if ( qsearch('cust_pay_pending', { 'custnum' => $self->custnum,
1328                                      'status'  => { op=>'!=', value=>'done' },
1329                                    }
1330               )
1331   ) {
1332      $dbh->rollback if $oldAutoCommit;
1333      return "Can't merge a customer with pending payments";
1334   }
1335
1336   tie my %financial_tables, 'Tie::IxHash',
1337     'cust_bill'         => 'invoices',
1338     'cust_bill_void'    => 'voided invoices',
1339     'cust_statement'    => 'statements',
1340     'cust_credit'       => 'credits',
1341     'cust_credit_void'  => 'voided credits',
1342     'cust_pay'          => 'payments',
1343     'cust_pay_void'     => 'voided payments',
1344     'cust_refund'       => 'refunds',
1345   ;
1346    
1347   foreach my $table ( keys %financial_tables ) {
1348
1349     my @records = $self->$table();
1350
1351     foreach my $record ( @records ) {
1352       $record->custnum($new_custnum);
1353       my $error = $record->replace;
1354       if ( $error ) {
1355         $dbh->rollback if $oldAutoCommit;
1356         return "Error merging ". $financial_tables{$table}. ": $error\n";
1357       }
1358     }
1359
1360   }
1361
1362   my $name = $self->ship_name; #?
1363
1364   my $locationnum = '';
1365   foreach my $cust_pkg ( $self->all_pkgs ) {
1366     $cust_pkg->custnum($new_custnum);
1367
1368     unless ( $cust_pkg->locationnum ) {
1369       unless ( $locationnum ) {
1370         my $cust_location = new FS::cust_location {
1371           $self->location_hash,
1372           'custnum' => $new_custnum,
1373         };
1374         my $error = $cust_location->insert;
1375         if ( $error ) {
1376           $dbh->rollback if $oldAutoCommit;
1377           return $error;
1378         }
1379         $locationnum = $cust_location->locationnum;
1380       }
1381       $cust_pkg->locationnum($locationnum);
1382     }
1383
1384     my $error = $cust_pkg->replace;
1385     if ( $error ) {
1386       $dbh->rollback if $oldAutoCommit;
1387       return $error;
1388     }
1389
1390     # add customer (ship) name to svc_phone.phone_name if blank
1391     my @cust_svc = $cust_pkg->cust_svc;
1392     foreach my $cust_svc (@cust_svc) {
1393       my($label, $value, $svcdb) = $cust_svc->label;
1394       next unless $svcdb eq 'svc_phone';
1395       my $svc_phone = $cust_svc->svc_x;
1396       next if $svc_phone->phone_name;
1397       $svc_phone->phone_name($name);
1398       my $error = $svc_phone->replace;
1399       if ( $error ) {
1400         $dbh->rollback if $oldAutoCommit;
1401         return $error;
1402       }
1403     }
1404
1405   }
1406
1407   #not considered:
1408   # cust_tax_exempt (texas tax exemptions)
1409   # cust_recon (some sort of not-well understood thing for OnPac)
1410
1411   #these are moved over
1412   foreach my $table (qw(
1413     cust_tag cust_location contact cust_attachment cust_main_note
1414     cust_tax_adjustment cust_pay_batch queue
1415   )) {
1416     foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1417       $record->custnum($new_custnum);
1418       my $error = $record->replace;
1419       if ( $error ) {
1420         $dbh->rollback if $oldAutoCommit;
1421         return $error;
1422       }
1423     }
1424   }
1425
1426   #these aren't preserved
1427   foreach my $table (qw(
1428     cust_main_exemption cust_main_invoice
1429   )) {
1430     foreach my $record ( qsearch( $table, { 'custnum' => $self->custnum } ) ) {
1431       my $error = $record->delete;
1432       if ( $error ) {
1433         $dbh->rollback if $oldAutoCommit;
1434         return $error;
1435       }
1436     }
1437   }
1438
1439
1440   my $sth = $dbh->prepare(
1441     'UPDATE cust_main SET referral_custnum = ? WHERE referral_custnum = ?'
1442   ) or do {
1443     my $errstr = $dbh->errstr;
1444     $dbh->rollback if $oldAutoCommit;
1445     return $errstr;
1446   };
1447   $sth->execute($new_custnum, $self->custnum) or do {
1448     my $errstr = $sth->errstr;
1449     $dbh->rollback if $oldAutoCommit;
1450     return $errstr;
1451   };
1452
1453   #tickets
1454
1455   my $ticket_dbh = '';
1456   if ($conf->config('ticket_system') eq 'RT_Internal') {
1457     $ticket_dbh = $dbh;
1458   } elsif ($conf->config('ticket_system') eq 'RT_External') {
1459     my ($datasrc, $user, $pass) = $conf->config('ticket_system-rt_external_datasrc');
1460     $ticket_dbh = DBI->connect($datasrc, $user, $pass, { 'ChopBlanks' => 1 });
1461       #or die "RT_External DBI->connect error: $DBI::errstr\n";
1462   }
1463
1464   if ( $ticket_dbh ) {
1465
1466     my $ticket_sth = $ticket_dbh->prepare(
1467       'UPDATE Links SET Target = ? WHERE Target = ?'
1468     ) or do {
1469       my $errstr = $ticket_dbh->errstr;
1470       $dbh->rollback if $oldAutoCommit;
1471       return $errstr;
1472     };
1473     $ticket_sth->execute('freeside://freeside/cust_main/'.$new_custnum,
1474                          'freeside://freeside/cust_main/'.$self->custnum)
1475       or do {
1476         my $errstr = $ticket_sth->errstr;
1477         $dbh->rollback if $oldAutoCommit;
1478         return $errstr;
1479       };
1480
1481   }
1482
1483   #delete the customer record
1484
1485   my $error = $self->delete;
1486   if ( $error ) {
1487     $dbh->rollback if $oldAutoCommit;
1488     return $error;
1489   }
1490
1491   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1492   '';
1493
1494 }
1495
1496 =item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
1497
1498 Replaces the OLD_RECORD with this one in the database.  If there is an error,
1499 returns the error, otherwise returns false.
1500
1501 To change the customer's address, set the pseudo-fields C<bill_location> and
1502 C<ship_location>.  The address will still only change if at least one of the
1503 address fields differs from the existing values.
1504
1505 INVOICING_LIST_ARYREF: If you pass an arrarref to the insert method, it will
1506 be set as the invoicing list (see L<"invoicing_list">).  Errors return as
1507 expected and rollback the entire transaction; it is not necessary to call 
1508 check_invoicing_list first.  Here's an example:
1509
1510   $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
1511
1512 Currently available options are: I<tax_exemption>.
1513
1514 The I<tax_exemption> option can be set to an arrayref of tax names or a hashref
1515 of tax names and exemption numbers.  FS::cust_main_exemption records will be
1516 deleted and inserted as appropriate.
1517
1518 =cut
1519
1520 sub replace {
1521   my $self = shift;
1522
1523   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
1524               ? shift
1525               : $self->replace_old;
1526
1527   my @param = @_;
1528
1529   warn "$me replace called\n"
1530     if $DEBUG;
1531
1532   my $curuser = $FS::CurrentUser::CurrentUser;
1533   if (    $self->payby eq 'COMP'
1534        && $self->payby ne $old->payby
1535        && ! $curuser->access_right('Complimentary customer')
1536      )
1537   {
1538     return "You are not permitted to create complimentary accounts.";
1539   }
1540
1541   local($ignore_expired_card) = 1
1542     if $old->payby  =~ /^(CARD|DCRD)$/
1543     && $self->payby =~ /^(CARD|DCRD)$/
1544     && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1545
1546   local($ignore_banned_card) = 1
1547     if (    $old->payby  =~ /^(CARD|DCRD)$/ && $self->payby =~ /^(CARD|DCRD)$/
1548          || $old->payby  =~ /^(CHEK|DCHK)$/ && $self->payby =~ /^(CHEK|DCHK)$/ )
1549     && ( $old->payinfo eq $self->payinfo || $old->paymask eq $self->paymask );
1550
1551   if (    $self->payby =~ /^(CARD|DCRD)$/
1552        && $old->payinfo ne $self->payinfo
1553        && $old->paymask ne $self->paymask )
1554   {
1555     my $error = $self->check_payinfo_cardtype;
1556     return $error if $error;
1557
1558     if ( $conf->exists('business-onlinepayment-verification') ) {
1559       #need to standardize paydate for this, false laziness with check
1560       my( $m, $y );
1561       if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
1562         ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
1563       } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1564         ( $m, $y ) = ( $2, "19$1" );
1565       } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
1566         ( $m, $y ) = ( $3, "20$2" );
1567       } else {
1568         return "Illegal expiration date: ". $self->paydate;
1569       }
1570       $m = sprintf('%02d',$m);
1571       $self->paydate("$y-$m-01");
1572
1573       $error = $self->realtime_verify_bop({ 'method'=>'CC' });
1574       return $error if $error;
1575     }
1576   }
1577
1578   return "Invoicing locale is required"
1579     if $old->locale
1580     && ! $self->locale
1581     && $conf->exists('cust_main-require_locale');
1582
1583   local $SIG{HUP} = 'IGNORE';
1584   local $SIG{INT} = 'IGNORE';
1585   local $SIG{QUIT} = 'IGNORE';
1586   local $SIG{TERM} = 'IGNORE';
1587   local $SIG{TSTP} = 'IGNORE';
1588   local $SIG{PIPE} = 'IGNORE';
1589
1590   my $oldAutoCommit = $FS::UID::AutoCommit;
1591   local $FS::UID::AutoCommit = 0;
1592   my $dbh = dbh;
1593
1594   for my $l (qw(bill_location ship_location)) {
1595     my $old_loc = $old->$l;
1596     my $new_loc = $self->$l;
1597
1598     # find the existing location if there is one
1599     $new_loc->set('custnum' => $self->custnum);
1600     my $error = $new_loc->find_or_insert;
1601     if ( $error ) {
1602       $dbh->rollback if $oldAutoCommit;
1603       return $error;
1604     }
1605     $self->set($l.'num', $new_loc->locationnum);
1606   } #for $l
1607
1608   # replace the customer record
1609   my $error = $self->SUPER::replace($old);
1610
1611   if ( $error ) {
1612     $dbh->rollback if $oldAutoCommit;
1613     return $error;
1614   }
1615
1616   # now move packages to the new service location
1617   $self->set('ship_location', ''); #flush cache
1618   if ( $old->ship_locationnum and # should only be null during upgrade...
1619        $old->ship_locationnum != $self->ship_locationnum ) {
1620     $error = $old->ship_location->move_to($self->ship_location);
1621     if ( $error ) {
1622       $dbh->rollback if $oldAutoCommit;
1623       return $error;
1624     }
1625   }
1626   # don't move packages based on the billing location, but 
1627   # disable it if it's no longer in use
1628   if ( $old->bill_locationnum and
1629        $old->bill_locationnum != $self->bill_locationnum ) {
1630     $error = $old->bill_location->disable_if_unused;
1631     if ( $error ) {
1632       $dbh->rollback if $oldAutoCommit;
1633       return $error;
1634     }
1635   }
1636
1637   if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
1638     my $invoicing_list = shift @param;
1639     $error = $self->check_invoicing_list( $invoicing_list );
1640     if ( $error ) {
1641       $dbh->rollback if $oldAutoCommit;
1642       return $error;
1643     }
1644     $self->invoicing_list( $invoicing_list );
1645   }
1646
1647   if ( $self->exists('tagnum') ) { #so we don't delete these on edit by accident
1648
1649     #this could be more efficient than deleting and re-inserting, if it matters
1650     foreach my $cust_tag (qsearch('cust_tag', {'custnum'=>$self->custnum} )) {
1651       my $error = $cust_tag->delete;
1652       if ( $error ) {
1653         $dbh->rollback if $oldAutoCommit;
1654         return $error;
1655       }
1656     }
1657     foreach my $tagnum ( @{ $self->tagnum || [] } ) {
1658       my $cust_tag = new FS::cust_tag { 'tagnum'  => $tagnum,
1659                                         'custnum' => $self->custnum };
1660       my $error = $cust_tag->insert;
1661       if ( $error ) {
1662         $dbh->rollback if $oldAutoCommit;
1663         return $error;
1664       }
1665     }
1666
1667   }
1668
1669   my %options = @param;
1670
1671   my $tax_exemption = delete $options{'tax_exemption'};
1672   if ( $tax_exemption ) {
1673
1674     $tax_exemption = { map { $_ => '' } @$tax_exemption }
1675       if ref($tax_exemption) eq 'ARRAY';
1676
1677     my %cust_main_exemption =
1678       map { $_->taxname => $_ }
1679           qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
1680
1681     foreach my $taxname ( keys %$tax_exemption ) {
1682
1683       if ( $cust_main_exemption{$taxname} && 
1684            $cust_main_exemption{$taxname}->exempt_number eq $tax_exemption->{$taxname}
1685          )
1686       {
1687         delete $cust_main_exemption{$taxname};
1688         next;
1689       }
1690
1691       my $cust_main_exemption = new FS::cust_main_exemption {
1692         'custnum'       => $self->custnum,
1693         'taxname'       => $taxname,
1694         'exempt_number' => $tax_exemption->{$taxname},
1695       };
1696       my $error = $cust_main_exemption->insert;
1697       if ( $error ) {
1698         $dbh->rollback if $oldAutoCommit;
1699         return "inserting cust_main_exemption (transaction rolled back): $error";
1700       }
1701     }
1702
1703     foreach my $cust_main_exemption ( values %cust_main_exemption ) {
1704       my $error = $cust_main_exemption->delete;
1705       if ( $error ) {
1706         $dbh->rollback if $oldAutoCommit;
1707         return "deleting cust_main_exemption (transaction rolled back): $error";
1708       }
1709     }
1710
1711   }
1712
1713   if ( $self->payby =~ /^(CARD|CHEK|LECB)$/
1714        && ( ( $self->get('payinfo') ne $old->get('payinfo')
1715               && $self->get('payinfo') !~ /^99\d{14}$/ 
1716             )
1717             || grep { $self->get($_) ne $old->get($_) } qw(paydate payname)
1718           )
1719      )
1720   {
1721
1722     # card/check/lec info has changed, want to retry realtime_ invoice events
1723     my $error = $self->retry_realtime;
1724     if ( $error ) {
1725       $dbh->rollback if $oldAutoCommit;
1726       return $error;
1727     }
1728   }
1729
1730   unless ( $import || $skip_fuzzyfiles ) {
1731     $error = $self->queue_fuzzyfiles_update;
1732     if ( $error ) {
1733       $dbh->rollback if $oldAutoCommit;
1734       return "updating fuzzy search cache: $error";
1735     }
1736   }
1737
1738   # tax district update in cust_location
1739
1740   # cust_main exports!
1741
1742   my $export_args = $options{'export_args'} || [];
1743
1744   my @part_export =
1745     map qsearch( 'part_export', {exportnum=>$_} ),
1746       $conf->config('cust_main-exports'); #, $agentnum
1747
1748   foreach my $part_export ( @part_export ) {
1749     my $error = $part_export->export_replace( $self, $old, @$export_args);
1750     if ( $error ) {
1751       $dbh->rollback if $oldAutoCommit;
1752       return "exporting to ". $part_export->exporttype.
1753              " (transaction rolled back): $error";
1754     }
1755   }
1756
1757   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1758   '';
1759
1760 }
1761
1762 =item queue_fuzzyfiles_update
1763
1764 Used by insert & replace to update the fuzzy search cache
1765
1766 =cut
1767
1768 use FS::cust_main::Search;
1769 sub queue_fuzzyfiles_update {
1770   my $self = shift;
1771
1772   local $SIG{HUP} = 'IGNORE';
1773   local $SIG{INT} = 'IGNORE';
1774   local $SIG{QUIT} = 'IGNORE';
1775   local $SIG{TERM} = 'IGNORE';
1776   local $SIG{TSTP} = 'IGNORE';
1777   local $SIG{PIPE} = 'IGNORE';
1778
1779   my $oldAutoCommit = $FS::UID::AutoCommit;
1780   local $FS::UID::AutoCommit = 0;
1781   my $dbh = dbh;
1782
1783   foreach my $field ( 'first', 'last', 'company', 'ship_company' ) {
1784     my $queue = new FS::queue { 
1785       'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1786     };
1787     my @args = "cust_main.$field", $self->get($field);
1788     my $error = $queue->insert( @args );
1789     if ( $error ) {
1790       $dbh->rollback if $oldAutoCommit;
1791       return "queueing job (transaction rolled back): $error";
1792     }
1793   }
1794
1795   my @locations = $self->bill_location;
1796   push @locations, $self->ship_location if $self->has_ship_address;
1797   foreach my $location (@locations) {
1798     my $queue = new FS::queue { 
1799       'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
1800     };
1801     my @args = 'cust_location.address1', $location->address1;
1802     my $error = $queue->insert( @args );
1803     if ( $error ) {
1804       $dbh->rollback if $oldAutoCommit;
1805       return "queueing job (transaction rolled back): $error";
1806     }
1807   }
1808
1809   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1810   '';
1811
1812 }
1813
1814 =item check
1815
1816 Checks all fields to make sure this is a valid customer record.  If there is
1817 an error, returns the error, otherwise returns false.  Called by the insert
1818 and replace methods.
1819
1820 =cut
1821
1822 sub check {
1823   my $self = shift;
1824
1825   warn "$me check BEFORE: \n". $self->_dump
1826     if $DEBUG > 2;
1827
1828   my $error =
1829     $self->ut_numbern('custnum')
1830     || $self->ut_number('agentnum')
1831     || $self->ut_textn('agent_custid')
1832     || $self->ut_number('refnum')
1833     || $self->ut_foreign_key('bill_locationnum', 'cust_location','locationnum')
1834     || $self->ut_foreign_key('ship_locationnum', 'cust_location','locationnum')
1835     || $self->ut_foreign_keyn('classnum', 'cust_class', 'classnum')
1836     || $self->ut_foreign_keyn('salesnum', 'sales', 'salesnum')
1837     || $self->ut_textn('custbatch')
1838     || $self->ut_name('last')
1839     || $self->ut_name('first')
1840     || $self->ut_snumbern('signupdate')
1841     || $self->ut_snumbern('birthdate')
1842     || $self->ut_namen('spouse_last')
1843     || $self->ut_namen('spouse_first')
1844     || $self->ut_snumbern('spouse_birthdate')
1845     || $self->ut_snumbern('anniversary_date')
1846     || $self->ut_textn('company')
1847     || $self->ut_textn('ship_company')
1848     || $self->ut_anything('comments')
1849     || $self->ut_numbern('referral_custnum')
1850     || $self->ut_textn('stateid')
1851     || $self->ut_textn('stateid_state')
1852     || $self->ut_textn('invoice_terms')
1853     || $self->ut_floatn('cdr_termination_percentage')
1854     || $self->ut_floatn('credit_limit')
1855     || $self->ut_numbern('billday')
1856     || $self->ut_numbern('prorate_day')
1857     || $self->ut_flag('force_prorate_day')
1858     || $self->ut_flag('edit_subject')
1859     || $self->ut_flag('calling_list_exempt')
1860     || $self->ut_flag('invoice_noemail')
1861     || $self->ut_flag('message_noemail')
1862     || $self->ut_enum('locale', [ '', FS::Locales->locales ])
1863     || $self->ut_flag('invoice_ship_address')
1864   ;
1865
1866   foreach (qw(company ship_company)) {
1867     my $company = $self->get($_);
1868     $company =~ s/^\s+//; 
1869     $company =~ s/\s+$//; 
1870     $company =~ s/\s+/ /g;
1871     $self->set($_, $company);
1872   }
1873
1874   #barf.  need message catalogs.  i18n.  etc.
1875   $error .= "Please select an advertising source."
1876     if $error =~ /^Illegal or empty \(numeric\) refnum: /;
1877   return $error if $error;
1878
1879   return "Unknown agent"
1880     unless qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
1881
1882   return "Unknown refnum"
1883     unless qsearchs( 'part_referral', { 'refnum' => $self->refnum } );
1884
1885   return "Unknown referring custnum: ". $self->referral_custnum
1886     unless ! $self->referral_custnum 
1887            || qsearchs( 'cust_main', { 'custnum' => $self->referral_custnum } );
1888
1889   if ( $self->ss eq '' ) {
1890     $self->ss('');
1891   } else {
1892     my $ss = $self->ss;
1893     $ss =~ s/\D//g;
1894     $ss =~ /^(\d{3})(\d{2})(\d{4})$/
1895       or return "Illegal social security number: ". $self->ss;
1896     $self->ss("$1-$2-$3");
1897   }
1898
1899   #turn off invoice_ship_address if ship & bill are the same
1900   if ($self->bill_locationnum eq $self->ship_locationnum) {
1901     $self->invoice_ship_address('');
1902   }
1903
1904   # cust_main_county verification now handled by cust_location check
1905
1906   $error =
1907        $self->ut_phonen('daytime', $self->country)
1908     || $self->ut_phonen('night',   $self->country)
1909     || $self->ut_phonen('fax',     $self->country)
1910     || $self->ut_phonen('mobile',  $self->country)
1911   ;
1912   return $error if $error;
1913
1914   if ( $conf->exists('cust_main-require_phone', $self->agentnum)
1915        && ! $import
1916        && ! length($self->daytime) && ! length($self->night) && ! length($self->mobile)
1917      ) {
1918
1919     my $daytime_label = FS::Msgcat::_gettext('daytime') =~ /^(daytime)?$/
1920                           ? 'Day Phone'
1921                           : FS::Msgcat::_gettext('daytime');
1922     my $night_label = FS::Msgcat::_gettext('night') =~ /^(night)?$/
1923                         ? 'Night Phone'
1924                         : FS::Msgcat::_gettext('night');
1925
1926     my $mobile_label = FS::Msgcat::_gettext('mobile') =~ /^(mobile)?$/
1927                         ? 'Mobile Phone'
1928                         : FS::Msgcat::_gettext('mobile');
1929
1930     return "$daytime_label, $night_label or $mobile_label is required"
1931   
1932   }
1933
1934   #$self->payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP|PREPAY|CASH|WEST|MCRD)$/
1935   #  or return "Illegal payby: ". $self->payby;
1936   #$self->payby($1);
1937   FS::payby->can_payby($self->table, $self->payby)
1938     or return "Illegal payby: ". $self->payby;
1939
1940   $error =    $self->ut_numbern('paystart_month')
1941            || $self->ut_numbern('paystart_year')
1942            || $self->ut_numbern('payissue')
1943            || $self->ut_textn('paytype')
1944   ;
1945   return $error if $error;
1946
1947   if ( $self->payip eq '' ) {
1948     $self->payip('');
1949   } else {
1950     $error = $self->ut_ip('payip');
1951     return $error if $error;
1952   }
1953
1954   # If it is encrypted and the private key is not availaible then we can't
1955   # check the credit card.
1956   my $check_payinfo = ! $self->is_encrypted($self->payinfo);
1957
1958   # Need some kind of global flag to accept invalid cards, for testing
1959   # on scrubbed data.
1960   if ( !$import && $check_payinfo && $self->payby =~ /^(CARD|DCRD)$/ ) {
1961
1962     my $payinfo = $self->payinfo;
1963     $payinfo =~ s/\D//g;
1964     $payinfo =~ /^(\d{13,16}|\d{8,9})$/
1965       or return gettext('invalid_card'); # . ": ". $self->payinfo;
1966     $payinfo = $1;
1967     $self->payinfo($payinfo);
1968     validate($payinfo)
1969       or return gettext('invalid_card'); # . ": ". $self->payinfo;
1970
1971     my $cardtype = $self->paycardtype;
1972     if ( $payinfo =~ /^99\d{14}$/ ) {
1973       $self->set('is_tokenized', 'Y'); #so we don't try to do it again
1974       if ( $self->paymask =~ /^\d+x/ ) {
1975         $cardtype = cardtype($self->paymask);
1976       } else {
1977         #return "paycardtype required ".
1978         #       "(can't derive from a token and no paymask w/prefix provided)"
1979         #  unless $cardtype;
1980       }
1981     } else {
1982       $cardtype = cardtype($self->payinfo);
1983     }
1984
1985     return gettext('unknown_card_type') if $cardtype eq 'Unknown';
1986
1987     $self->set('paycardtype', $cardtype);
1988
1989     unless ( $ignore_banned_card ) {
1990       my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
1991       if ( $ban ) {
1992         if ( $ban->bantype eq 'warn' ) {
1993           #or others depending on value of $ban->reason ?
1994           return '_duplicate_card'.
1995                  ': disabled from'. time2str('%a %h %o at %r', $ban->_date).
1996                  ' until '.         time2str('%a %h %o at %r', $ban->_end_date).
1997                  ' (ban# '. $ban->bannum. ')'
1998             unless $self->override_ban_warn;
1999         } else {
2000           return 'Banned credit card: banned on '.
2001                  time2str('%a %h %o at %r', $ban->_date).
2002                  ' by '. $ban->otaker.
2003                  ' (ban# '. $ban->bannum. ')';
2004         }
2005       }
2006     }
2007
2008     if (length($self->paycvv) && !$self->is_encrypted($self->paycvv)) {
2009       if ( $cardtype eq 'American Express card' ) {
2010         $self->paycvv =~ /^(\d{4})$/
2011           or return "CVV2 (CID) for American Express cards is four digits.";
2012         $self->paycvv($1);
2013       } else {
2014         $self->paycvv =~ /^(\d{3})$/
2015           or return "CVV2 (CVC2/CID) is three digits.";
2016         $self->paycvv($1);
2017       }
2018     } else {
2019       $self->paycvv('');
2020     }
2021
2022     if ( $cardtype =~ /^(Switch|Solo)$/i ) {
2023
2024       return "Start date or issue number is required for $cardtype cards"
2025         unless $self->paystart_month && $self->paystart_year or $self->payissue;
2026
2027       return "Start month must be between 1 and 12"
2028         if $self->paystart_month
2029            and $self->paystart_month < 1 || $self->paystart_month > 12;
2030
2031       return "Start year must be 1990 or later"
2032         if $self->paystart_year
2033            and $self->paystart_year < 1990;
2034
2035       return "Issue number must be beween 1 and 99"
2036         if $self->payissue
2037           and $self->payissue < 1 || $self->payissue > 99;
2038
2039     } else {
2040       $self->paystart_month('');
2041       $self->paystart_year('');
2042       $self->payissue('');
2043     }
2044
2045   } elsif ( $check_payinfo && $self->payby =~ /^(CHEK|DCHK)$/ ) {
2046
2047     my $payinfo = $self->payinfo;
2048     $payinfo =~ s/[^\d\@\.]//g;
2049     if ( $conf->config('echeck-country') eq 'CA' ) {
2050       $payinfo =~ /^(\d+)\@(\d{5})\.(\d{3})$/
2051         or return 'invalid echeck account@branch.bank';
2052       $payinfo = "$1\@$2.$3";
2053     } elsif ( $conf->config('echeck-country') eq 'US' ) {
2054       $payinfo =~ /^(\d+)\@(\d{9})$/ or return 'invalid echeck account@aba';
2055       $payinfo = "$1\@$2";
2056     } else {
2057       $payinfo =~ /^(\d+)\@(\d+)$/ or return 'invalid echeck account@routing';
2058       $payinfo = "$1\@$2";
2059     }
2060     $self->payinfo($payinfo);
2061     $self->paycvv('');
2062
2063     unless ( $ignore_banned_card ) {
2064       my $ban = FS::banned_pay->ban_search( %{ $self->_banned_pay_hashref } );
2065       if ( $ban ) {
2066         if ( $ban->bantype eq 'warn' ) {
2067           #or others depending on value of $ban->reason ?
2068           return '_duplicate_ach' unless $self->override_ban_warn;
2069         } else {
2070           return 'Banned ACH account: banned on '.
2071                  time2str('%a %h %o at %r', $ban->_date).
2072                  ' by '. $ban->otaker.
2073                  ' (ban# '. $ban->bannum. ')';
2074         }
2075       }
2076     }
2077
2078   } elsif ( $self->payby eq 'LECB' ) {
2079
2080     my $payinfo = $self->payinfo;
2081     $payinfo =~ s/\D//g;
2082     $payinfo =~ /^1?(\d{10})$/ or return 'invalid btn billing telephone number';
2083     $payinfo = $1;
2084     $self->payinfo($payinfo);
2085     $self->paycvv('');
2086
2087   } elsif ( $self->payby eq 'BILL' ) {
2088
2089     $error = $self->ut_textn('payinfo');
2090     return "Illegal P.O. number: ". $self->payinfo if $error;
2091     $self->paycvv('');
2092
2093   } elsif ( $self->payby eq 'COMP' ) {
2094
2095     my $curuser = $FS::CurrentUser::CurrentUser;
2096     if (    ! $self->custnum
2097          && ! $curuser->access_right('Complimentary customer')
2098        )
2099     {
2100       return "You are not permitted to create complimentary accounts."
2101     }
2102
2103     $error = $self->ut_textn('payinfo');
2104     return "Illegal comp account issuer: ". $self->payinfo if $error;
2105     $self->paycvv('');
2106
2107   } elsif ( $self->payby eq 'PREPAY' ) {
2108
2109     my $payinfo = $self->payinfo;
2110     $payinfo =~ s/\W//g; #anything else would just confuse things
2111     $self->payinfo($payinfo);
2112     $error = $self->ut_alpha('payinfo');
2113     return "Illegal prepayment identifier: ". $self->payinfo if $error;
2114     return "Unknown prepayment identifier"
2115       unless qsearchs('prepay_credit', { 'identifier' => $self->payinfo } );
2116     $self->paycvv('');
2117
2118   } elsif ( $self->payby =~ /^CARD|DCRD$/ and $self->paymask ) {
2119     # either ignoring invalid cards, or we can't decrypt the payinfo, but
2120     # try to detect the card type anyway. this never returns failure, so
2121     # the contract of $ignore_invalid_cards is maintained.
2122     $self->set('paycardtype', cardtype($self->paymask));
2123   }
2124
2125   if ( $self->paydate eq '' || $self->paydate eq '-' ) {
2126     return "Expiration date required"
2127       # shouldn't payinfo_check do this?
2128       unless $self->payby =~ /^(BILL|PREPAY|CHEK|DCHK|LECB|CASH|WEST|MCRD|PPAL)$/;
2129     $self->paydate('');
2130   } else {
2131     my( $m, $y );
2132     if ( $self->paydate =~ /^(\d{1,2})[\/\-](\d{2}(\d{2})?)$/ ) {
2133       ( $m, $y ) = ( $1, length($2) == 4 ? $2 : "20$2" );
2134     } elsif ( $self->paydate =~ /^19(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
2135       ( $m, $y ) = ( $2, "19$1" );
2136     } elsif ( $self->paydate =~ /^(20)?(\d{2})[\/\-](\d{1,2})[\/\-]\d+$/ ) {
2137       ( $m, $y ) = ( $3, "20$2" );
2138     } else {
2139       return "Illegal expiration date: ". $self->paydate;
2140     }
2141     $m = sprintf('%02d',$m);
2142     $self->paydate("$y-$m-01");
2143     my($nowm,$nowy)=(localtime(time))[4,5]; $nowm++; $nowy+=1900;
2144     return gettext('expired_card')
2145       if !$import
2146       && !$ignore_expired_card 
2147       && ( $y<$nowy || ( $y==$nowy && $1<$nowm ) );
2148
2149     if ( my $error = $self->ut_daten('paydate') ) {
2150       return $error;
2151     }
2152   }
2153
2154   if ( $self->payname eq '' && $self->payby !~ /^(CHEK|DCHK)$/ &&
2155        ( ! $conf->exists('require_cardname')
2156          || $self->payby !~ /^(CARD|DCRD)$/  ) 
2157   ) {
2158     $self->payname( $self->first. " ". $self->getfield('last') );
2159   } else {
2160
2161     if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
2162       $self->payname =~ /^([\w \,\.\-\']*)$/
2163         or return gettext('illegal_name'). " payname: ". $self->payname;
2164       $self->payname($1);
2165     } else {
2166       $self->payname =~ /^([\w \,\.\-\'\&]*)$/
2167         or return gettext('illegal_name'). " payname: ". $self->payname;
2168       $self->payname($1);
2169     }
2170
2171   }
2172
2173   return "Please select an invoicing locale"
2174     if ! $self->locale
2175     && ! $self->custnum
2176     && $conf->exists('cust_main-require_locale');
2177
2178   return "Please select a customer class"
2179     if ! $self->classnum
2180     && $conf->exists('cust_main-require_classnum');
2181
2182   foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
2183     $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
2184     $self->$flag($1);
2185   }
2186
2187   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
2188
2189   warn "$me check AFTER: \n". $self->_dump
2190     if $DEBUG > 2;
2191
2192   $self->SUPER::check;
2193 }
2194
2195 sub check_payinfo_cardtype {
2196   my $self = shift;
2197
2198   return '' unless $self->payby =~ /^(CARD|DCRD)$/;
2199
2200   my $payinfo = $self->payinfo;
2201   $payinfo =~ s/\D//g;
2202
2203   if ( $payinfo =~ /^99\d{14}$/ ) {
2204     return '';
2205   }
2206
2207   my %bop_card_types = map { $_=>1 } values %{ card_types() };
2208   my $cardtype = cardtype($payinfo);
2209   $self->set('paycardtype', $cardtype);
2210
2211   return "$cardtype not accepted" unless $bop_card_types{$cardtype};
2212
2213   '';
2214
2215 }
2216
2217 =item replace_check
2218
2219 Additional checks for replace only.
2220
2221 =cut
2222
2223 sub replace_check {
2224   my ($new,$old) = @_;
2225   #preserve old value if global config is set
2226   if ($old && $conf->exists('invoice-ship_address')) {
2227     $new->invoice_ship_address($old->invoice_ship_address);
2228   }
2229   return '';
2230 }
2231
2232 =item addr_fields 
2233
2234 Returns a list of fields which have ship_ duplicates.
2235
2236 =cut
2237
2238 sub addr_fields {
2239   qw( last first company
2240       locationname
2241       address1 address2 city county state zip country
2242       latitude longitude
2243       daytime night fax mobile
2244     );
2245 }
2246
2247 =item has_ship_address
2248
2249 Returns true if this customer record has a separate shipping address.
2250
2251 =cut
2252
2253 sub has_ship_address {
2254   my $self = shift;
2255   $self->bill_locationnum != $self->ship_locationnum;
2256 }
2257
2258 =item location_hash
2259
2260 Returns a list of key/value pairs, with the following keys: address1, 
2261 adddress2, city, county, state, zip, country, district, and geocode.  The 
2262 shipping address is used if present.
2263
2264 =cut
2265
2266 sub location_hash {
2267   my $self = shift;
2268   $self->ship_location->location_hash;
2269 }
2270
2271 =item cust_location
2272
2273 Returns all locations (see L<FS::cust_location>) for this customer.
2274
2275 =cut
2276
2277 sub cust_location {
2278   my $self = shift;
2279   qsearch('cust_location', { 'custnum' => $self->custnum,
2280                              'prospectnum' => '' } );
2281 }
2282
2283 =item cust_contact
2284
2285 Returns all contacts (see L<FS::contact>) for this customer.
2286
2287 =cut
2288
2289 #already used :/ sub contact {
2290 sub cust_contact {
2291   my $self = shift;
2292   qsearch('contact', { 'custnum' => $self->custnum } );
2293 }
2294
2295 =item unsuspend
2296
2297 Unsuspends all unflagged suspended packages (see L</unflagged_suspended_pkgs>
2298 and L<FS::cust_pkg>) for this customer, except those on hold.
2299
2300 Returns a list: an empty list on success or a list of errors.
2301
2302 =cut
2303
2304 sub unsuspend {
2305   my $self = shift;
2306   grep { ($_->get('setup')) && $_->unsuspend } $self->suspended_pkgs(@_);
2307 }
2308
2309 =item release_hold
2310
2311 Unsuspends all suspended packages in the on-hold state (those without setup 
2312 dates) for this customer. 
2313
2314 =cut
2315
2316 sub release_hold {
2317   my $self = shift;
2318   grep { (!$_->setup) && $_->unsuspend } $self->suspended_pkgs;
2319 }
2320
2321 =item suspend
2322
2323 Suspends all unsuspended packages (see L<FS::cust_pkg>) for this customer.
2324
2325 Returns a list: an empty list on success or a list of errors.
2326
2327 =cut
2328
2329 sub suspend {
2330   my $self = shift;
2331   grep { $_->suspend(@_) } $self->unsuspended_pkgs;
2332 }
2333
2334 =item suspend_if_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2335
2336 Suspends all unsuspended packages (see L<FS::cust_pkg>) matching the listed
2337 PKGPARTs (see L<FS::part_pkg>).  Preferred usage is to pass a hashref instead
2338 of a list of pkgparts; the hashref has the following keys:
2339
2340 =over 4
2341
2342 =item pkgparts - listref of pkgparts
2343
2344 =item (other options are passed to the suspend method)
2345
2346 =back
2347
2348
2349 Returns a list: an empty list on success or a list of errors.
2350
2351 =cut
2352
2353 sub suspend_if_pkgpart {
2354   my $self = shift;
2355   my (@pkgparts, %opt);
2356   if (ref($_[0]) eq 'HASH'){
2357     @pkgparts = @{$_[0]{pkgparts}};
2358     %opt      = %{$_[0]};
2359   }else{
2360     @pkgparts = @_;
2361   }
2362   grep { $_->suspend(%opt) }
2363     grep { my $pkgpart = $_->pkgpart; grep { $pkgpart eq $_ } @pkgparts }
2364       $self->unsuspended_pkgs;
2365 }
2366
2367 =item suspend_unless_pkgpart HASHREF | PKGPART [ , PKGPART ... ]
2368
2369 Suspends all unsuspended packages (see L<FS::cust_pkg>) unless they match the
2370 given PKGPARTs (see L<FS::part_pkg>).  Preferred usage is to pass a hashref
2371 instead of a list of pkgparts; the hashref has the following keys:
2372
2373 =over 4
2374
2375 =item pkgparts - listref of pkgparts
2376
2377 =item (other options are passed to the suspend method)
2378
2379 =back
2380
2381 Returns a list: an empty list on success or a list of errors.
2382
2383 =cut
2384
2385 sub suspend_unless_pkgpart {
2386   my $self = shift;
2387   my (@pkgparts, %opt);
2388   if (ref($_[0]) eq 'HASH'){
2389     @pkgparts = @{$_[0]{pkgparts}};
2390     %opt      = %{$_[0]};
2391   }else{
2392     @pkgparts = @_;
2393   }
2394   grep { $_->suspend(%opt) }
2395     grep { my $pkgpart = $_->pkgpart; ! grep { $pkgpart eq $_ } @pkgparts }
2396       $self->unsuspended_pkgs;
2397 }
2398
2399 =item cancel [ OPTION => VALUE ... ]
2400
2401 Cancels all uncancelled packages (see L<FS::cust_pkg>) for this customer.
2402 The cancellation time will be now.
2403
2404 =back
2405
2406 Always returns a list: an empty list on success or a list of errors.
2407
2408 =cut
2409
2410 sub cancel {
2411   my $self = shift;
2412   my %opt = @_;
2413   warn "$me cancel called on customer ". $self->custnum. " with options ".
2414        join(', ', map { "$_: $opt{$_}" } keys %opt ). "\n"
2415     if $DEBUG;
2416   my @pkgs = $self->ncancelled_pkgs;
2417
2418   $self->cancel_pkgs( %opt, 'cust_pkg' => \@pkgs );
2419 }
2420
2421 =item cancel_pkgs OPTIONS
2422
2423 Cancels a specified list of packages. OPTIONS can include:
2424
2425 =over 4
2426
2427 =item cust_pkg - an arrayref of the packages. Required.
2428
2429 =item time - the cancellation time, used to calculate final bills and
2430 unused-time credits if any. Will be passed through to the bill() and
2431 FS::cust_pkg::cancel() methods.
2432
2433 =item quiet - can be set true to supress email cancellation notices.
2434
2435 =item reason - can be set to a cancellation reason (see L<FS:reason>), either a
2436 reasonnum of an existing reason, or passing a hashref will create a new reason.
2437 The hashref should have the following keys:
2438 typenum - Reason type (see L<FS::reason_type>)
2439 reason - Text of the new reason.
2440
2441 =item cust_pkg_reason - can be an arrayref of L<FS::cust_pkg_reason> objects
2442 for the individual packages, parallel to the C<cust_pkg> argument. The
2443 reason and reason_otaker arguments will be taken from those objects.
2444
2445 =item ban - can be set true to ban this customer's credit card or ACH information, if present.
2446
2447 =item nobill - can be set true to skip billing if it might otherwise be done.
2448
2449 =cut
2450
2451 sub cancel_pkgs {
2452   my( $self, %opt ) = @_;
2453
2454   # we're going to cancel services, which is not reversible
2455   # but on 3.x, don't strictly enforce this
2456   warn "cancel_pkgs should not be run inside a transaction"
2457     if $FS::UID::AutoCommit == 0;
2458
2459   local $FS::UID::AutoCommit = 0;
2460
2461   return ( 'access denied' )
2462     unless $FS::CurrentUser::CurrentUser->access_right('Cancel customer');
2463
2464   if ( $opt{'ban'} && $self->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ ) {
2465
2466     #should try decryption (we might have the private key)
2467     # and if not maybe queue a job for the server that does?
2468     return ( "Can't (yet) ban encrypted credit cards" )
2469       if $self->is_encrypted($self->payinfo);
2470
2471     my $ban = new FS::banned_pay $self->_new_banned_pay_hashref;
2472     my $error = $ban->insert;
2473     if ($error) {
2474       dbh->rollback;
2475       return ( $error );
2476     }
2477
2478   }
2479
2480   my @pkgs = @{ delete $opt{'cust_pkg'} };
2481   my $cancel_time = $opt{'time'} || time;
2482
2483   # bill all packages first, so we don't lose usage, service counts for
2484   # bulk billing, etc.
2485   if ( !$opt{nobill} && $conf->exists('bill_usage_on_cancel') ) {
2486     $opt{nobill} = 1;
2487     my $error = $self->bill( 'pkg_list' => [ @pkgs ],
2488                              'cancel'   => 1,
2489                              'time'     => $cancel_time );
2490     if ($error) {
2491       warn "Error billing during cancel, custnum ". $self->custnum. ": $error";
2492       dbh->rollback;
2493       return ( "Error billing during cancellation: $error" );
2494     }
2495   }
2496   dbh->commit;
2497
2498   $FS::UID::AutoCommit = 1;
2499   my @errors;
2500   # now cancel all services, the same way we would for individual packages.
2501   # if any of them fail, cancel the rest anyway.
2502   my @cust_svc = map { $_->cust_svc } @pkgs;
2503   my @sorted_cust_svc =
2504     map  { $_->[0] }
2505     sort { $a->[1] <=> $b->[1] }
2506     map  { [ $_, $_->svc_x ? $_->svc_x->table_info->{'cancel_weight'} : -1 ]; } @cust_svc
2507   ;
2508   warn "$me removing ".scalar(@sorted_cust_svc)." service(s) for customer ".
2509     $self->custnum."\n"
2510     if $DEBUG;
2511   foreach my $cust_svc (@sorted_cust_svc) {
2512     my $part_svc = $cust_svc->part_svc;
2513     next if ( defined($part_svc) and $part_svc->preserve );
2514     my $error = $cust_svc->cancel; # immediate cancel, no date option
2515     push @errors, $error if $error;
2516   }
2517   if (@errors) {
2518     return @errors;
2519   }
2520
2521   warn "$me cancelling ". scalar(@pkgs) ." package(s) for customer ".
2522     $self->custnum. "\n"
2523     if $DEBUG;
2524
2525   my @cprs;
2526   if ($opt{'cust_pkg_reason'}) {
2527     @cprs = @{ delete $opt{'cust_pkg_reason'} };
2528   }
2529   my $null_reason;
2530   foreach (@pkgs) {
2531     my %lopt = %opt;
2532     if (@cprs) {
2533       my $cpr = shift @cprs;
2534       if ( $cpr ) {
2535         $lopt{'reason'}        = $cpr->reasonnum;
2536         $lopt{'reason_otaker'} = $cpr->otaker;
2537       } else {
2538         warn "no reason found when canceling package ".$_->pkgnum."\n";
2539         $lopt{'reason'} = '';
2540       }
2541     }
2542     my $error = $_->cancel(%lopt);
2543     push @errors, 'pkgnum '.$_->pkgnum.': '.$error if $error;
2544   }
2545
2546   return @errors;
2547 }
2548
2549 sub _banned_pay_hashref {
2550   my $self = shift;
2551
2552   my %payby2ban = (
2553     'CARD' => 'CARD',
2554     'DCRD' => 'CARD',
2555     'CHEK' => 'CHEK',
2556     'DCHK' => 'CHEK'
2557   );
2558
2559   {
2560     'payby'   => $payby2ban{$self->payby},
2561     'payinfo' => $self->payinfo,
2562     #don't ever *search* on reason! #'reason'  =>
2563   };
2564 }
2565
2566 sub _new_banned_pay_hashref {
2567   my $self = shift;
2568   my $hr = $self->_banned_pay_hashref;
2569   $hr->{payinfo} = md5_base64($hr->{payinfo});
2570   $hr;
2571 }
2572
2573 =item notes
2574
2575 Returns all notes (see L<FS::cust_main_note>) for this customer.
2576
2577 =cut
2578
2579 sub notes {
2580   my($self,$orderby_classnum) = (shift,shift);
2581   my $orderby = "sticky DESC, _date DESC";
2582   $orderby = "classnum ASC, $orderby" if $orderby_classnum;
2583   qsearch( 'cust_main_note',
2584            { 'custnum' => $self->custnum },
2585            '',
2586            "ORDER BY $orderby",
2587          );
2588 }
2589
2590 =item agent
2591
2592 Returns the agent (see L<FS::agent>) for this customer.
2593
2594 =cut
2595
2596 sub agent {
2597   my $self = shift;
2598   qsearchs( 'agent', { 'agentnum' => $self->agentnum } );
2599 }
2600
2601 =item agent_name
2602
2603 Returns the agent name (see L<FS::agent>) for this customer.
2604
2605 =cut
2606
2607 sub agent_name {
2608   my $self = shift;
2609   $self->agent->agent;
2610 }
2611
2612 =item cust_tag
2613
2614 Returns any tags associated with this customer, as FS::cust_tag objects,
2615 or an empty list if there are no tags.
2616
2617 =cut
2618
2619 sub cust_tag {
2620   my $self = shift;
2621   qsearch('cust_tag', { 'custnum' => $self->custnum } );
2622 }
2623
2624 =item part_tag
2625
2626 Returns any tags associated with this customer, as FS::part_tag objects,
2627 or an empty list if there are no tags.
2628
2629 =cut
2630
2631 sub part_tag {
2632   my $self = shift;
2633   map $_->part_tag, $self->cust_tag; 
2634 }
2635
2636
2637 =item cust_class
2638
2639 Returns the customer class, as an FS::cust_class object, or the empty string
2640 if there is no customer class.
2641
2642 =cut
2643
2644 sub cust_class {
2645   my $self = shift;
2646   if ( $self->classnum ) {
2647     qsearchs('cust_class', { 'classnum' => $self->classnum } );
2648   } else {
2649     return '';
2650   } 
2651 }
2652
2653 =item categoryname 
2654
2655 Returns the customer category name, or the empty string if there is no customer
2656 category.
2657
2658 =cut
2659
2660 sub categoryname {
2661   my $self = shift;
2662   my $cust_class = $self->cust_class;
2663   $cust_class
2664     ? $cust_class->categoryname
2665     : '';
2666 }
2667
2668 =item classname 
2669
2670 Returns the customer class name, or the empty string if there is no customer
2671 class.
2672
2673 =cut
2674
2675 sub classname {
2676   my $self = shift;
2677   my $cust_class = $self->cust_class;
2678   $cust_class
2679     ? $cust_class->classname
2680     : '';
2681 }
2682
2683 =item BILLING METHODS
2684
2685 Documentation on billing methods has been moved to
2686 L<FS::cust_main::Billing>.
2687
2688 =item REALTIME BILLING METHODS
2689
2690 Documentation on realtime billing methods has been moved to
2691 L<FS::cust_main::Billing_Realtime>.
2692
2693 =item remove_cvv
2694
2695 Removes the I<paycvv> field from the database directly.
2696
2697 If there is an error, returns the error, otherwise returns false.
2698
2699 =cut
2700
2701 sub remove_cvv {
2702   my $self = shift;
2703   my $sth = dbh->prepare("UPDATE cust_main SET paycvv = '' WHERE custnum = ?")
2704     or return dbh->errstr;
2705   $sth->execute($self->custnum)
2706     or return $sth->errstr;
2707   $self->paycvv('');
2708   '';
2709 }
2710
2711 =item batch_card OPTION => VALUE...
2712
2713 Adds a payment for this invoice to the pending credit card batch (see
2714 L<FS::cust_pay_batch>), or, if the B<realtime> option is set to a true value,
2715 runs the payment using a realtime gateway.
2716
2717 Options may include:
2718
2719 B<amount>: the amount to be paid; defaults to the customer's balance minus
2720 any payments in transit.
2721
2722 B<payby>: the payment method; defaults to cust_main.payby
2723
2724 B<realtime>: runs this as a realtime payment instead of adding it to a 
2725 batch.  Deprecated.
2726
2727 B<invnum>: sets cust_pay_batch.invnum.
2728
2729 B<address1>, B<address2>, B<city>, B<state>, B<zip>, B<country>: sets 
2730 the billing address for the payment; defaults to the customer's billing
2731 location.
2732
2733 B<payinfo>, B<paydate>, B<payname>: sets the payment account, expiration
2734 date, and name; defaults to those fields in cust_main.
2735
2736 =cut
2737
2738 sub batch_card {
2739   my ($self, %options) = @_;
2740
2741   my $amount;
2742   if (exists($options{amount})) {
2743     $amount = $options{amount};
2744   }else{
2745     $amount = sprintf("%.2f", $self->balance - $self->in_transit_payments);
2746   }
2747   if ($amount <= 0) {
2748     warn(sprintf("Customer balance %.2f - in transit amount %.2f is <= 0.\n",
2749         $self->balance,
2750         $self->in_transit_payments
2751     ));
2752     return;
2753   }
2754   
2755   my $invnum = delete $options{invnum};
2756   my $payby = $options{payby} || $self->payby;  #still dubious
2757
2758   if ($options{'realtime'}) {
2759     return $self->realtime_bop( FS::payby->payby2bop($self->payby),
2760                                 $amount,
2761                                 %options,
2762                               );
2763   }
2764
2765   my $oldAutoCommit = $FS::UID::AutoCommit;
2766   local $FS::UID::AutoCommit = 0;
2767   my $dbh = dbh;
2768
2769   #this needs to handle mysql as well as Pg, like svc_acct.pm
2770   #(make it into a common function if folks need to do batching with mysql)
2771   $dbh->do("LOCK TABLE pay_batch IN SHARE ROW EXCLUSIVE MODE")
2772     or return "Cannot lock pay_batch: " . $dbh->errstr;
2773
2774   my %pay_batch = (
2775     'status' => 'O',
2776     'payby'  => FS::payby->payby2payment($payby),
2777   );
2778   $pay_batch{agentnum} = $self->agentnum if $conf->exists('batch-spoolagent');
2779
2780   my $pay_batch = qsearchs( 'pay_batch', \%pay_batch );
2781
2782   unless ( $pay_batch ) {
2783     $pay_batch = new FS::pay_batch \%pay_batch;
2784     my $error = $pay_batch->insert;
2785     if ( $error ) {
2786       $dbh->rollback if $oldAutoCommit;
2787       die "error creating new batch: $error\n";
2788     }
2789   }
2790
2791   my $old_cust_pay_batch = qsearchs('cust_pay_batch', {
2792       'batchnum' => $pay_batch->batchnum,
2793       'custnum'  => $self->custnum,
2794   } );
2795
2796   foreach (qw( address1 address2 city state zip country latitude longitude
2797                payby payinfo paydate payname paycode paytype ))
2798   {
2799     $options{$_} = '' unless exists($options{$_});
2800   }
2801
2802   my $loc = $self->bill_location;
2803
2804   my $cust_pay_batch = new FS::cust_pay_batch ( {
2805     'batchnum' => $pay_batch->batchnum,
2806     'invnum'   => $invnum || 0,                    # is there a better value?
2807                                                    # this field should be
2808                                                    # removed...
2809                                                    # cust_bill_pay_batch now
2810     'custnum'  => $self->custnum,
2811     'last'     => $self->getfield('last'),
2812     'first'    => $self->getfield('first'),
2813     'address1' => $options{address1} || $loc->address1,
2814     'address2' => $options{address2} || $loc->address2,
2815     'city'     => $options{city}     || $loc->city,
2816     'state'    => $options{state}    || $loc->state,
2817     'zip'      => $options{zip}      || $loc->zip,
2818     'country'  => $options{country}  || $loc->country,
2819     'payby'    => $options{payby}    || $self->payby,
2820     'payinfo'  => $options{payinfo}  || $self->payinfo,
2821     'paymask'  => ( $options{payinfo}
2822                       ? FS::payinfo_Mixin->mask_payinfo( $options{payby},
2823                                                          $options{payinfo} )
2824                       : $self->paymask
2825                   ),
2826     'exp'      => $options{paydate}  || $self->paydate,
2827     'payname'  => $options{payname}  || $self->payname,
2828     'amount'   => $amount,                         # consolidating
2829     'paycode'  => $options{paycode}  || '',
2830   } );
2831   
2832   $cust_pay_batch->paybatchnum($old_cust_pay_batch->paybatchnum)
2833     if $old_cust_pay_batch;
2834
2835   my $error;
2836   if ($old_cust_pay_batch) {
2837     $error = $cust_pay_batch->replace($old_cust_pay_batch)
2838   } else {
2839     $error = $cust_pay_batch->insert;
2840   }
2841
2842   if ( $error ) {
2843     $dbh->rollback if $oldAutoCommit;
2844     die $error;
2845   }
2846
2847   if ($options{'processing-fee'} > 0) {
2848     my $pf_cust_pkg;
2849     my $processing_fee_text = 'Payment Processing Fee';
2850
2851     unless ( $invnum ) { # probably from a payment screen
2852       # do we have any open invoices? pick earliest
2853       # uses the fact that cust_main->cust_bill sorts by date ascending
2854       my @open = $self->open_cust_bill;
2855       $invnum = $open[0]->invnum if scalar(@open);
2856     }
2857
2858     unless ( $invnum ) {  # still nothing? pick last closed invoice
2859       # again uses fact that cust_main->cust_bill sorts by date ascending
2860       my @closed = $self->cust_bill;
2861       $invnum = $closed[$#closed]->invnum if scalar(@closed);
2862     }
2863
2864     unless ( $invnum ) {
2865       # XXX: unlikely case - pre-paying before any invoices generated
2866       # what it should do is create a new invoice and pick it
2867       warn '\PROCESS FEE AND NO INVOICES PICKED TO APPLY IT!';
2868       return '';
2869     }
2870
2871     my $pf_change_error = $self->charge({
2872             'amount'  => $options{'processing-fee'},
2873             'pkg'   => $processing_fee_text,
2874             'setuptax'  => 'Y',
2875             'cust_pkg_ref' => \$pf_cust_pkg,
2876     });
2877
2878     if($pf_change_error) {
2879       warn 'Unable to add payment processing fee';
2880       return '';
2881     }
2882
2883     $pf_cust_pkg->setup(time);
2884     my $pf_error = $pf_cust_pkg->replace;
2885     if($pf_error) {
2886       warn 'Unable to set setup time on cust_pkg for processing fee';
2887       # but keep going...
2888     }
2889
2890     my $cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum });
2891     unless ( $cust_bill ) {
2892       warn "race condition + invoice deletion just happened";
2893       return '';
2894     }
2895
2896     my $grand_pf_error =
2897       $cust_bill->add_cc_surcharge($pf_cust_pkg->pkgnum,$options{'processing-fee'});
2898
2899     warn "cannot add Processing fee to invoice #$invnum: $grand_pf_error"
2900       if $grand_pf_error;
2901   }
2902
2903   my $unapplied =   $self->total_unapplied_credits
2904                   + $self->total_unapplied_payments
2905                   + $self->in_transit_payments;
2906   foreach my $cust_bill ($self->open_cust_bill) {
2907     #$dbh->commit or die $dbh->errstr if $oldAutoCommit;
2908     my $cust_bill_pay_batch = new FS::cust_bill_pay_batch {
2909       'invnum' => $cust_bill->invnum,
2910       'paybatchnum' => $cust_pay_batch->paybatchnum,
2911       'amount' => $cust_bill->owed,
2912       '_date' => time,
2913     };
2914     if ($unapplied >= $cust_bill_pay_batch->amount){
2915       $unapplied -= $cust_bill_pay_batch->amount;
2916       next;
2917     }else{
2918       $cust_bill_pay_batch->amount(sprintf ( "%.2f", 
2919                                    $cust_bill_pay_batch->amount - $unapplied ));      $unapplied = 0;
2920     }
2921     $error = $cust_bill_pay_batch->insert;
2922     if ( $error ) {
2923       $dbh->rollback if $oldAutoCommit;
2924       die $error;
2925     }
2926   }
2927
2928   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2929   '';
2930 }
2931
2932 =item total_owed
2933
2934 Returns the total owed for this customer on all invoices
2935 (see L<FS::cust_bill/owed>).
2936
2937 =cut
2938
2939 sub total_owed {
2940   my $self = shift;
2941   $self->total_owed_date(2145859200); #12/31/2037
2942 }
2943
2944 =item total_owed_date TIME
2945
2946 Returns the total owed for this customer on all invoices with date earlier than
2947 TIME.  TIME is specified as a UNIX timestamp; see L<perlfunc/"time">).  Also
2948 see L<Time::Local> and L<Date::Parse> for conversion functions.
2949
2950 =cut
2951
2952 sub total_owed_date {
2953   my $self = shift;
2954   my $time = shift;
2955
2956   my $custnum = $self->custnum;
2957
2958   my $owed_sql = FS::cust_bill->owed_sql;
2959
2960   my $sql = "
2961     SELECT SUM($owed_sql) FROM cust_bill
2962       WHERE custnum = $custnum
2963         AND _date <= $time
2964   ";
2965
2966   sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
2967
2968 }
2969
2970 =item total_owed_pkgnum PKGNUM
2971
2972 Returns the total owed on all invoices for this customer's specific package
2973 when using experimental package balances (see L<FS::cust_bill/owed_pkgnum>).
2974
2975 =cut
2976
2977 sub total_owed_pkgnum {
2978   my( $self, $pkgnum ) = @_;
2979   $self->total_owed_date_pkgnum(2145859200, $pkgnum); #12/31/2037
2980 }
2981
2982 =item total_owed_date_pkgnum TIME PKGNUM
2983
2984 Returns the total owed for this customer's specific package when using
2985 experimental package balances on all invoices with date earlier than
2986 TIME.  TIME is specified as a UNIX timestamp; see L<perlfunc/"time">).  Also
2987 see L<Time::Local> and L<Date::Parse> for conversion functions.
2988
2989 =cut
2990
2991 sub total_owed_date_pkgnum {
2992   my( $self, $time, $pkgnum ) = @_;
2993
2994   my $total_bill = 0;
2995   foreach my $cust_bill (
2996     grep { $_->_date <= $time }
2997       qsearch('cust_bill', { 'custnum' => $self->custnum, } )
2998   ) {
2999     $total_bill += $cust_bill->owed_pkgnum($pkgnum);
3000   }
3001   sprintf( "%.2f", $total_bill );
3002
3003 }
3004
3005 =item total_paid
3006
3007 Returns the total amount of all payments.
3008
3009 =cut
3010
3011 sub total_paid {
3012   my $self = shift;
3013   my $total = 0;
3014   $total += $_->paid foreach $self->cust_pay;
3015   sprintf( "%.2f", $total );
3016 }
3017
3018 =item total_unapplied_credits
3019
3020 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3021 customer.  See L<FS::cust_credit/credited>.
3022
3023 =item total_credited
3024
3025 Old name for total_unapplied_credits.  Don't use.
3026
3027 =cut
3028
3029 sub total_credited {
3030   #carp "total_credited deprecated, use total_unapplied_credits";
3031   shift->total_unapplied_credits(@_);
3032 }
3033
3034 sub total_unapplied_credits {
3035   my $self = shift;
3036
3037   my $custnum = $self->custnum;
3038
3039   my $unapplied_sql = FS::cust_credit->unapplied_sql;
3040
3041   my $sql = "
3042     SELECT SUM($unapplied_sql) FROM cust_credit
3043       WHERE custnum = $custnum
3044   ";
3045
3046   sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3047
3048 }
3049
3050 =item total_unapplied_credits_pkgnum PKGNUM
3051
3052 Returns the total outstanding credit (see L<FS::cust_credit>) for this
3053 customer.  See L<FS::cust_credit/credited>.
3054
3055 =cut
3056
3057 sub total_unapplied_credits_pkgnum {
3058   my( $self, $pkgnum ) = @_;
3059   my $total_credit = 0;
3060   $total_credit += $_->credited foreach $self->cust_credit_pkgnum($pkgnum);
3061   sprintf( "%.2f", $total_credit );
3062 }
3063
3064
3065 =item total_unapplied_payments
3066
3067 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer.
3068 See L<FS::cust_pay/unapplied>.
3069
3070 =cut
3071
3072 sub total_unapplied_payments {
3073   my $self = shift;
3074
3075   my $custnum = $self->custnum;
3076
3077   my $unapplied_sql = FS::cust_pay->unapplied_sql;
3078
3079   my $sql = "
3080     SELECT SUM($unapplied_sql) FROM cust_pay
3081       WHERE custnum = $custnum
3082   ";
3083
3084   sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3085
3086 }
3087
3088 =item total_unapplied_payments_pkgnum PKGNUM
3089
3090 Returns the total unapplied payments (see L<FS::cust_pay>) for this customer's
3091 specific package when using experimental package balances.  See
3092 L<FS::cust_pay/unapplied>.
3093
3094 =cut
3095
3096 sub total_unapplied_payments_pkgnum {
3097   my( $self, $pkgnum ) = @_;
3098   my $total_unapplied = 0;
3099   $total_unapplied += $_->unapplied foreach $self->cust_pay_pkgnum($pkgnum);
3100   sprintf( "%.2f", $total_unapplied );
3101 }
3102
3103
3104 =item total_unapplied_refunds
3105
3106 Returns the total unrefunded refunds (see L<FS::cust_refund>) for this
3107 customer.  See L<FS::cust_refund/unapplied>.
3108
3109 =cut
3110
3111 sub total_unapplied_refunds {
3112   my $self = shift;
3113   my $custnum = $self->custnum;
3114
3115   my $unapplied_sql = FS::cust_refund->unapplied_sql;
3116
3117   my $sql = "
3118     SELECT SUM($unapplied_sql) FROM cust_refund
3119       WHERE custnum = $custnum
3120   ";
3121
3122   sprintf( "%.2f", $self->scalar_sql($sql) || 0 );
3123
3124 }
3125
3126 =item balance
3127
3128 Returns the balance for this customer (total_owed plus total_unrefunded, minus
3129 total_unapplied_credits minus total_unapplied_payments).
3130
3131 =cut
3132
3133 sub balance {
3134   my $self = shift;
3135   $self->balance_date_range;
3136 }
3137
3138 =item balance_date TIME
3139
3140 Returns the balance for this customer, only considering invoices with date
3141 earlier than TIME (total_owed_date minus total_credited minus
3142 total_unapplied_payments).  TIME is specified as a UNIX timestamp; see
3143 L<perlfunc/"time">).  Also see L<Time::Local> and L<Date::Parse> for conversion
3144 functions.
3145
3146 =cut
3147
3148 sub balance_date {
3149   my $self = shift;
3150   $self->balance_date_range(shift);
3151 }
3152
3153 =item balance_date_range [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
3154
3155 Returns the balance for this customer, optionally considering invoices with
3156 date earlier than START_TIME, and not later than END_TIME
3157 (total_owed_date minus total_unapplied_credits minus total_unapplied_payments).
3158
3159 Times are specified as SQL fragments or numeric
3160 UNIX timestamps; see L<perlfunc/"time">).  Also see L<Time::Local> and
3161 L<Date::Parse> for conversion functions.  The empty string can be passed
3162 to disable that time constraint completely.
3163
3164 Accepts the same options as L<balance_date_sql>:
3165
3166 =over 4
3167
3168 =item unapplied_date
3169
3170 set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
3171
3172 =item cutoff
3173
3174 An absolute cutoff time.  Payments, credits, and refunds I<applied> after this 
3175 time will be ignored.  Note that START_TIME and END_TIME only limit the date 
3176 range for invoices and I<unapplied> payments, credits, and refunds.
3177
3178 =back
3179
3180 =cut
3181
3182 sub balance_date_range {
3183   my $self = shift;
3184   my $sql = 'SELECT SUM('. $self->balance_date_sql(@_).
3185             ') FROM cust_main WHERE custnum='. $self->custnum;
3186   sprintf( '%.2f', $self->scalar_sql($sql) || 0 );
3187 }
3188
3189 =item balance_pkgnum PKGNUM
3190
3191 Returns the balance for this customer's specific package when using
3192 experimental package balances (total_owed plus total_unrefunded, minus
3193 total_unapplied_credits minus total_unapplied_payments)
3194
3195 =cut
3196
3197 sub balance_pkgnum {
3198   my( $self, $pkgnum ) = @_;
3199
3200   sprintf( "%.2f",
3201       $self->total_owed_pkgnum($pkgnum)
3202 # n/a - refunds aren't part of pkg-balances since they don't apply to invoices
3203 #    + $self->total_unapplied_refunds_pkgnum($pkgnum)
3204     - $self->total_unapplied_credits_pkgnum($pkgnum)
3205     - $self->total_unapplied_payments_pkgnum($pkgnum)
3206   );
3207 }
3208
3209 =item in_transit_payments
3210
3211 Returns the total of requests for payments for this customer pending in 
3212 batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>
3213
3214 =cut
3215
3216 sub in_transit_payments {
3217   my $self = shift;
3218   my $in_transit_payments = 0;
3219   foreach my $pay_batch ( qsearch('pay_batch', {
3220     'status' => 'I',
3221   } ) ) {
3222     foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
3223       'batchnum' => $pay_batch->batchnum,
3224       'custnum' => $self->custnum,
3225       'status'  => '',
3226     } ) ) {
3227       $in_transit_payments += $cust_pay_batch->amount;
3228     }
3229   }
3230   sprintf( "%.2f", $in_transit_payments );
3231 }
3232
3233 =item payment_info
3234
3235 Returns a hash of useful information for making a payment.
3236
3237 =over 4
3238
3239 =item balance
3240
3241 Current balance.
3242
3243 =item payby
3244
3245 'CARD' (credit card - automatic), 'DCRD' (credit card - on-demand),
3246 'CHEK' (electronic check - automatic), 'DCHK' (electronic check - on-demand),
3247 'LECB' (Phone bill billing), 'BILL' (billing), or 'COMP' (free).
3248
3249 =back
3250
3251 For credit card transactions:
3252
3253 =over 4
3254
3255 =item card_type 1
3256
3257 =item payname
3258
3259 Exact name on card
3260
3261 =back
3262
3263 For electronic check transactions:
3264
3265 =over 4
3266
3267 =item stateid_state
3268
3269 =back
3270
3271 =cut
3272
3273 sub payment_info {
3274   my $self = shift;
3275
3276   my %return = ();
3277
3278   $return{balance} = $self->balance;
3279
3280   $return{payname} = $self->payname
3281                      || ( $self->first. ' '. $self->get('last') );
3282
3283   $return{$_} = $self->bill_location->$_
3284     for qw(address1 address2 city state zip);
3285
3286   $return{payby} = $self->payby;
3287   $return{stateid_state} = $self->stateid_state;
3288
3289   if ( $self->payby =~ /^(CARD|DCRD)$/ ) {
3290     $return{card_type} = cardtype($self->payinfo);
3291     $return{payinfo} = $self->paymask;
3292
3293     @return{'month', 'year'} = $self->paydate_monthyear;
3294
3295   }
3296
3297   if ( $self->payby =~ /^(CHEK|DCHK)$/ ) {
3298     my ($payinfo1, $payinfo2) = split '@', $self->paymask;
3299     $return{payinfo1} = $payinfo1;
3300     $return{payinfo2} = $payinfo2;
3301     $return{paytype}  = $self->paytype;
3302     $return{paystate} = $self->paystate;
3303
3304   }
3305
3306   #doubleclick protection
3307   my $_date = time;
3308   $return{paybatch} = "webui-MyAccount-$_date-$$-". rand() * 2**32;
3309
3310   %return;
3311
3312 }
3313
3314 =item paydate_monthyear
3315
3316 Returns a two-element list consisting of the month and year of this customer's
3317 paydate (credit card expiration date for CARD customers)
3318
3319 =cut
3320
3321 sub paydate_monthyear {
3322   my $self = shift;
3323   if ( $self->paydate  =~ /^(\d{4})-(\d{1,2})-\d{1,2}$/ ) { #Pg date format
3324     ( $2, $1 );
3325   } elsif ( $self->paydate =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
3326     ( $1, $3 );
3327   } else {
3328     ('', '');
3329   }
3330 }
3331
3332 =item paydate_epoch
3333
3334 Returns the exact time in seconds corresponding to the payment method 
3335 expiration date.  For CARD/DCRD customers this is the end of the month;
3336 for others (COMP is the only other payby that uses paydate) it's the start.
3337 Returns 0 if the paydate is empty or set to the far future.
3338
3339 =cut
3340
3341 sub paydate_epoch {
3342   my $self = shift;
3343   my ($month, $year) = $self->paydate_monthyear;
3344   return 0 if !$year or $year >= 2037;
3345   if ( $self->payby eq 'CARD' or $self->payby eq 'DCRD' ) {
3346     $month++;
3347     if ( $month == 13 ) {
3348       $month = 1;
3349       $year++;
3350     }
3351     return timelocal(0,0,0,1,$month-1,$year) - 1;
3352   }
3353   else {
3354     return timelocal(0,0,0,1,$month-1,$year);
3355   }
3356 }
3357
3358 =item paydate_epoch_sql
3359
3360 Class method.  Returns an SQL expression to obtain the payment expiration date
3361 as a number of seconds.
3362
3363 =cut
3364
3365 # Special expiration date behavior for non-CARD/DCRD customers has been 
3366 # carefully preserved.  Do we really use that?
3367 sub paydate_epoch_sql {
3368   my $class = shift;
3369   my $table = shift || 'cust_main';
3370   my ($case1, $case2);
3371   if ( driver_name eq 'Pg' ) {
3372     $case1 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) + INTERVAL '1 month') - 1";
3373     $case2 = "EXTRACT( EPOCH FROM CAST( $table.paydate AS TIMESTAMP ) )";
3374   }
3375   elsif ( lc(driver_name) eq 'mysql' ) {
3376     $case1 = "UNIX_TIMESTAMP( DATE_ADD( CAST( $table.paydate AS DATETIME ), INTERVAL 1 month ) ) - 1";
3377     $case2 = "UNIX_TIMESTAMP( CAST( $table.paydate AS DATETIME ) )";
3378   }
3379   else { return '' }
3380   return "CASE WHEN $table.payby IN('CARD','DCRD') 
3381   THEN ($case1)
3382   ELSE ($case2)
3383   END"
3384 }
3385
3386 =item tax_exemption TAXNAME
3387
3388 =cut
3389
3390 sub tax_exemption {
3391   my( $self, $taxname ) = @_;
3392
3393   qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
3394                                      'taxname' => $taxname,
3395                                    },
3396           );
3397 }
3398
3399 =item cust_main_exemption
3400
3401 =cut
3402
3403 sub cust_main_exemption {
3404   my $self = shift;
3405   qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } );
3406 }
3407
3408 =item invoicing_list [ ARRAYREF ]
3409
3410 If an arguement is given, sets these email addresses as invoice recipients
3411 (see L<FS::cust_main_invoice>).  Errors are not fatal and are not reported
3412 (except as warnings), so use check_invoicing_list first.
3413
3414 Returns a list of email addresses (with svcnum entries expanded).
3415
3416 Note: You can clear the invoicing list by passing an empty ARRAYREF.  You can
3417 check it without disturbing anything by passing nothing.
3418
3419 This interface may change in the future.
3420
3421 =cut
3422
3423 sub invoicing_list {
3424   my( $self, $arrayref ) = @_;
3425
3426   if ( $arrayref ) {
3427     my @cust_main_invoice;
3428     if ( $self->custnum ) {
3429       @cust_main_invoice = 
3430         qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3431     } else {
3432       @cust_main_invoice = ();
3433     }
3434     foreach my $cust_main_invoice ( @cust_main_invoice ) {
3435       #warn $cust_main_invoice->destnum;
3436       unless ( grep { $cust_main_invoice->address eq $_ } @{$arrayref} ) {
3437         #warn $cust_main_invoice->destnum;
3438         my $error = $cust_main_invoice->delete;
3439         warn $error if $error;
3440       }
3441     }
3442     if ( $self->custnum ) {
3443       @cust_main_invoice = 
3444         qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3445     } else {
3446       @cust_main_invoice = ();
3447     }
3448     my %seen = map { $_->address => 1 } @cust_main_invoice;
3449     foreach my $address ( @{$arrayref} ) {
3450       next if exists $seen{$address} && $seen{$address};
3451       $seen{$address} = 1;
3452       my $cust_main_invoice = new FS::cust_main_invoice ( {
3453         'custnum' => $self->custnum,
3454         'dest'    => $address,
3455       } );
3456       my $error = $cust_main_invoice->insert;
3457       warn $error if $error;
3458     }
3459   }
3460   
3461   if ( $self->custnum ) {
3462     map { $_->address }
3463       qsearch( 'cust_main_invoice', { 'custnum' => $self->custnum } );
3464   } else {
3465     ();
3466   }
3467
3468 }
3469
3470 =item check_invoicing_list ARRAYREF
3471
3472 Checks these arguements as valid input for the invoicing_list method.  If there
3473 is an error, returns the error, otherwise returns false.
3474
3475 =cut
3476
3477 sub check_invoicing_list {
3478   my( $self, $arrayref ) = @_;
3479
3480   foreach my $address ( @$arrayref ) {
3481
3482     if ($address eq 'FAX' and $self->getfield('fax') eq '') {
3483       return 'Can\'t add FAX invoice destination with a blank FAX number.';
3484     }
3485
3486     my $cust_main_invoice = new FS::cust_main_invoice ( {
3487       'custnum' => $self->custnum,
3488       'dest'    => $address,
3489     } );
3490     my $error = $self->custnum
3491                 ? $cust_main_invoice->check
3492                 : $cust_main_invoice->checkdest
3493     ;
3494     return $error if $error;
3495
3496   }
3497
3498   return "Email address required"
3499     if $conf->exists('cust_main-require_invoicing_list_email', $self->agentnum)
3500     && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
3501
3502   '';
3503 }
3504
3505 =item set_default_invoicing_list
3506
3507 Sets the invoicing list to all accounts associated with this customer,
3508 overwriting any previous invoicing list.
3509
3510 =cut
3511
3512 sub set_default_invoicing_list {
3513   my $self = shift;
3514   $self->invoicing_list($self->all_emails);
3515 }
3516
3517 =item all_emails
3518
3519 Returns the email addresses of all accounts provisioned for this customer.
3520
3521 =cut
3522
3523 sub all_emails {
3524   my $self = shift;
3525   my %list;
3526   foreach my $cust_pkg ( $self->all_pkgs ) {
3527     my @cust_svc = qsearch('cust_svc', { 'pkgnum' => $cust_pkg->pkgnum } );
3528     my @svc_acct =
3529       map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3530         grep { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
3531           @cust_svc;
3532     $list{$_}=1 foreach map { $_->email } @svc_acct;
3533   }
3534   keys %list;
3535 }
3536
3537 =item invoicing_list_addpost
3538
3539 Adds postal invoicing to this customer.  If this customer is already configured
3540 to receive postal invoices, does nothing.
3541
3542 =cut
3543
3544 sub invoicing_list_addpost {
3545   my $self = shift;
3546   return if grep { $_ eq 'POST' } $self->invoicing_list;
3547   my @invoicing_list = $self->invoicing_list;
3548   push @invoicing_list, 'POST';
3549   $self->invoicing_list(\@invoicing_list);
3550 }
3551
3552 =item invoicing_list_emailonly
3553
3554 Returns the list of email invoice recipients (invoicing_list without non-email
3555 destinations such as POST and FAX).
3556
3557 =cut
3558
3559 sub invoicing_list_emailonly {
3560   my $self = shift;
3561   warn "$me invoicing_list_emailonly called"
3562     if $DEBUG;
3563   grep { $_ !~ /^([A-Z]+)$/ } $self->invoicing_list;
3564 }
3565
3566 =item invoicing_list_emailonly_scalar
3567
3568 Returns the list of email invoice recipients (invoicing_list without non-email
3569 destinations such as POST and FAX) as a comma-separated scalar.
3570
3571 =cut
3572
3573 sub invoicing_list_emailonly_scalar {
3574   my $self = shift;
3575   warn "$me invoicing_list_emailonly_scalar called"
3576     if $DEBUG;
3577   join(', ', $self->invoicing_list_emailonly);
3578 }
3579
3580 =item contact_list [ CLASSNUM, ... ]
3581
3582 Returns a list of contacts (L<FS::contact> objects) for the customer. If
3583 a list of contact classnums is given, returns only contacts in those
3584 classes. If '0' is given, also returns contacts with no class.
3585
3586 If no arguments are given, returns all contacts for the customer.
3587
3588 =cut
3589
3590 sub contact_list {
3591   my $self = shift;
3592   my $search = {
3593     table       => 'contact',
3594     select      => 'contact.*',
3595     extra_sql   => ' WHERE contact.custnum = '.$self->custnum,
3596   };
3597
3598   my @orwhere;
3599   my @classnums;
3600   foreach (@_) {
3601     if ( $_ eq '0' ) {
3602       push @orwhere, 'contact.classnum is null';
3603     } elsif ( /^\d+$/ ) {
3604       push @classnums, $_;
3605     } else {
3606       die "bad classnum argument '$_'";
3607     }
3608   }
3609
3610   if (@classnums) {
3611     push @orwhere, 'contact.classnum IN ('.join(',', @classnums).')';
3612   }
3613   if (@orwhere) {
3614     $search->{extra_sql} .= ' AND (' .
3615                             join(' OR ', map "( $_ )", @orwhere) .
3616                             ')';
3617   }
3618
3619   qsearch($search);
3620 }
3621
3622 =item contact_list_email [ CLASSNUM, ... ]
3623
3624 Same as L</contact_list>, but returns email destinations instead of contact
3625 objects. Also accepts 'invoice' as an argument, in which case this will also
3626 return the invoice email address if any.
3627
3628 =cut
3629
3630 sub contact_list_email {
3631   my $self = shift;
3632   my @classnums;
3633   my $and_invoice;
3634   foreach (@_) {
3635     if (/^invoice$/) {
3636       $and_invoice = 1;
3637     } else {
3638       push @classnums, $_;
3639     }
3640   }
3641   my %emails;
3642   # if the only argument passed was 'invoice' then no classnums are
3643   # intended, so skip this.
3644   if ( @classnums ) {
3645     my @contacts = $self->contact_list(@classnums);
3646     foreach my $contact (@contacts) {
3647       foreach my $contact_email ($contact->contact_email) {
3648         # unlike on 4.x, we have a separate list of invoice email
3649         # destinations.
3650         # make sure they're not redundant with contact emails
3651         $emails{ $contact_email->emailaddress } =
3652           Email::Address->new( $contact->firstlast,
3653                                $contact_email->emailaddress
3654                              )->format;
3655       }
3656     }
3657   }
3658   if ( $and_invoice ) {
3659     foreach my $email ($self->invoicing_list_emailonly) {
3660       $emails{ $email } ||=
3661         Email::Address->new( $self->name_short, $email )->format;
3662     }
3663   }
3664   values %emails;
3665 }
3666
3667 =item referral_custnum_cust_main
3668
3669 Returns the customer who referred this customer (or the empty string, if
3670 this customer was not referred).
3671
3672 Note the difference with referral_cust_main method: This method,
3673 referral_custnum_cust_main returns the single customer (if any) who referred
3674 this customer, while referral_cust_main returns an array of customers referred
3675 BY this customer.
3676
3677 =cut
3678
3679 sub referral_custnum_cust_main {
3680   my $self = shift;
3681   return '' unless $self->referral_custnum;
3682   qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3683 }
3684
3685 =item referral_cust_main [ DEPTH [ EXCLUDE_HASHREF ] ]
3686
3687 Returns an array of customers referred by this customer (referral_custnum set
3688 to this custnum).  If DEPTH is given, recurses up to the given depth, returning
3689 customers referred by customers referred by this customer and so on, inclusive.
3690 The default behavior is DEPTH 1 (no recursion).
3691
3692 Note the difference with referral_custnum_cust_main method: This method,
3693 referral_cust_main, returns an array of customers referred BY this customer,
3694 while referral_custnum_cust_main returns the single customer (if any) who
3695 referred this customer.
3696
3697 =cut
3698
3699 sub referral_cust_main {
3700   my $self = shift;
3701   my $depth = @_ ? shift : 1;
3702   my $exclude = @_ ? shift : {};
3703
3704   my @cust_main =
3705     map { $exclude->{$_->custnum}++; $_; }
3706       grep { ! $exclude->{ $_->custnum } }
3707         qsearch( 'cust_main', { 'referral_custnum' => $self->custnum } );
3708
3709   if ( $depth > 1 ) {
3710     push @cust_main,
3711       map { $_->referral_cust_main($depth-1, $exclude) }
3712         @cust_main;
3713   }
3714
3715   @cust_main;
3716 }
3717
3718 =item referral_cust_main_ncancelled
3719
3720 Same as referral_cust_main, except only returns customers with uncancelled
3721 packages.
3722
3723 =cut
3724
3725 sub referral_cust_main_ncancelled {
3726   my $self = shift;
3727   grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
3728 }
3729
3730 =item referral_cust_pkg [ DEPTH ]
3731
3732 Like referral_cust_main, except returns a flat list of all unsuspended (and
3733 uncancelled) packages for each customer.  The number of items in this list may
3734 be useful for commission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
3735
3736 =cut
3737
3738 sub referral_cust_pkg {
3739   my $self = shift;
3740   my $depth = @_ ? shift : 1;
3741
3742   map { $_->unsuspended_pkgs }
3743     grep { $_->unsuspended_pkgs }
3744       $self->referral_cust_main($depth);
3745 }
3746
3747 =item referring_cust_main
3748
3749 Returns the single cust_main record for the customer who referred this customer
3750 (referral_custnum), or false.
3751
3752 =cut
3753
3754 sub referring_cust_main {
3755   my $self = shift;
3756   return '' unless $self->referral_custnum;
3757   qsearchs('cust_main', { 'custnum' => $self->referral_custnum } );
3758 }
3759
3760 =item credit AMOUNT, REASON [ , OPTION => VALUE ... ]
3761
3762 Applies a credit to this customer.  If there is an error, returns the error,
3763 otherwise returns false.
3764
3765 REASON can be a text string, an FS::reason object, or a scalar reference to
3766 a reasonnum.  If a text string, it will be automatically inserted as a new
3767 reason, and a 'reason_type' option must be passed to indicate the
3768 FS::reason_type for the new reason.
3769
3770 An I<addlinfo> option may be passed to set the credit's I<addlinfo> field.
3771 Likewise for I<eventnum>, I<commission_agentnum>, I<commission_salesnum> and
3772 I<commission_pkgnum>.
3773
3774 Any other options are passed to FS::cust_credit::insert.
3775
3776 =cut
3777
3778 sub credit {
3779   my( $self, $amount, $reason, %options ) = @_;
3780
3781   my $cust_credit = new FS::cust_credit {
3782     'custnum' => $self->custnum,
3783     'amount'  => $amount,
3784   };
3785
3786   if ( ref($reason) ) {
3787
3788     if ( ref($reason) eq 'SCALAR' ) {
3789       $cust_credit->reasonnum( $$reason );
3790     } else {
3791       $cust_credit->reasonnum( $reason->reasonnum );
3792     }
3793
3794   } else {
3795     $cust_credit->set('reason', $reason)
3796   }
3797
3798   $cust_credit->$_( delete $options{$_} )
3799     foreach grep exists($options{$_}),
3800               qw( addlinfo eventnum ),
3801               map "commission_$_", qw( agentnum salesnum pkgnum );
3802
3803   $cust_credit->insert(%options);
3804
3805 }
3806
3807 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
3808
3809 Creates a one-time charge for this customer.  If there is an error, returns
3810 the error, otherwise returns false.
3811
3812 New-style, with a hashref of options:
3813
3814   my $error = $cust_main->charge(
3815                                   {
3816                                     'amount'     => 54.32,
3817                                     'quantity'   => 1,
3818                                     'start_date' => str2time('7/4/2009'),
3819                                     'pkg'        => 'Description',
3820                                     'comment'    => 'Comment',
3821                                     'additional' => [], #extra invoice detail
3822                                     'classnum'   => 1,  #pkg_class
3823
3824                                     'setuptax'   => '', # or 'Y' for tax exempt
3825
3826                                     'locationnum'=> 1234, # optional
3827
3828                                     #internal taxation
3829                                     'taxclass'   => 'Tax class',
3830
3831                                     #vendor taxation
3832                                     'taxproduct' => 2,  #part_pkg_taxproduct
3833                                     'override'   => {}, #XXX describe
3834
3835                                     #will be filled in with the new object
3836                                     'cust_pkg_ref' => \$cust_pkg,
3837
3838                                     #generate an invoice immediately
3839                                     'bill_now' => 0,
3840                                     'invoice_terms' => '', #with these terms
3841                                   }
3842                                 );
3843
3844 Old-style:
3845
3846   my $error = $cust_main->charge( 54.32, 'Description', 'Comment', 'Tax class' );
3847
3848 =cut
3849
3850 #super false laziness w/quotation::charge
3851 sub charge {
3852   my $self = shift;
3853   my ( $amount, $setup_cost, $quantity, $start_date, $classnum );
3854   my ( $pkg, $comment, $additional );
3855   my ( $setuptax, $taxclass );   #internal taxes
3856   my ( $taxproduct, $override ); #vendor (CCH) taxes
3857   my $no_auto = '';
3858   my $separate_bill = '';
3859   my $cust_pkg_ref = '';
3860   my ( $bill_now, $invoice_terms ) = ( 0, '' );
3861   my $locationnum;
3862   if ( ref( $_[0] ) ) {
3863     $amount     = $_[0]->{amount};
3864     $setup_cost = $_[0]->{setup_cost};
3865     $quantity   = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
3866     $start_date = exists($_[0]->{start_date}) ? $_[0]->{start_date} : '';
3867     $no_auto    = exists($_[0]->{no_auto}) ? $_[0]->{no_auto} : '';
3868     $pkg        = exists($_[0]->{pkg}) ? $_[0]->{pkg} : 'One-time charge';
3869     $comment    = exists($_[0]->{comment}) ? $_[0]->{comment}
3870                                            : '$'. sprintf("%.2f",$amount);
3871     $setuptax   = exists($_[0]->{setuptax}) ? $_[0]->{setuptax} : '';
3872     $taxclass   = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
3873     $classnum   = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
3874     $additional = $_[0]->{additional} || [];
3875     $taxproduct = $_[0]->{taxproductnum};
3876     $override   = { '' => $_[0]->{tax_override} };
3877     $cust_pkg_ref = exists($_[0]->{cust_pkg_ref}) ? $_[0]->{cust_pkg_ref} : '';
3878     $bill_now = exists($_[0]->{bill_now}) ? $_[0]->{bill_now} : '';
3879     $invoice_terms = exists($_[0]->{invoice_terms}) ? $_[0]->{invoice_terms} : '';
3880     $locationnum = $_[0]->{locationnum} || $self->ship_locationnum;
3881     $separate_bill = $_[0]->{separate_bill} || '';
3882   } else { # yuck
3883     $amount     = shift;
3884     $setup_cost = '';
3885     $quantity   = 1;
3886     $start_date = '';
3887     $pkg        = @_ ? shift : 'One-time charge';
3888     $comment    = @_ ? shift : '$'. sprintf("%.2f",$amount);
3889     $setuptax   = '';
3890     $taxclass   = @_ ? shift : '';
3891     $additional = [];
3892   }
3893
3894   local $SIG{HUP} = 'IGNORE';
3895   local $SIG{INT} = 'IGNORE';
3896   local $SIG{QUIT} = 'IGNORE';
3897   local $SIG{TERM} = 'IGNORE';
3898   local $SIG{TSTP} = 'IGNORE';
3899   local $SIG{PIPE} = 'IGNORE';
3900
3901   my $oldAutoCommit = $FS::UID::AutoCommit;
3902   local $FS::UID::AutoCommit = 0;
3903   my $dbh = dbh;
3904
3905   my $part_pkg = new FS::part_pkg ( {
3906     'pkg'           => $pkg,
3907     'comment'       => $comment,
3908     'plan'          => 'flat',
3909     'freq'          => 0,
3910     'disabled'      => 'Y',
3911     'classnum'      => ( $classnum ? $classnum : '' ),
3912     'setuptax'      => $setuptax,
3913     'taxclass'      => $taxclass,
3914     'taxproductnum' => $taxproduct,
3915     'setup_cost'    => $setup_cost,
3916   } );
3917
3918   my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
3919                         ( 0 .. @$additional - 1 )
3920                   ),
3921                   'additional_count' => scalar(@$additional),
3922                   'setup_fee' => $amount,
3923                 );
3924
3925   my $error = $part_pkg->insert( options       => \%options,
3926                                  tax_overrides => $override,
3927                                );
3928   if ( $error ) {
3929     $dbh->rollback if $oldAutoCommit;
3930     return $error;
3931   }
3932
3933   my $pkgpart = $part_pkg->pkgpart;
3934   my %type_pkgs = ( 'typenum' => $self->agent->typenum, 'pkgpart' => $pkgpart );
3935   unless ( qsearchs('type_pkgs', \%type_pkgs ) ) {
3936     my $type_pkgs = new FS::type_pkgs \%type_pkgs;
3937     $error = $type_pkgs->insert;
3938     if ( $error ) {
3939       $dbh->rollback if $oldAutoCommit;
3940       return $error;
3941     }
3942   }
3943
3944   my $cust_pkg = new FS::cust_pkg ( {
3945     'custnum'    => $self->custnum,
3946     'pkgpart'    => $pkgpart,
3947     'quantity'   => $quantity,
3948     'start_date' => $start_date,
3949     'no_auto'    => $no_auto,
3950     'separate_bill' => $separate_bill,
3951     'locationnum'=> $locationnum,
3952   } );
3953
3954   $error = $cust_pkg->insert;
3955   if ( $error ) {
3956     $dbh->rollback if $oldAutoCommit;
3957     return $error;
3958   } elsif ( $cust_pkg_ref ) {
3959     ${$cust_pkg_ref} = $cust_pkg;
3960   }
3961
3962   if ( $bill_now ) {
3963     my $error = $self->bill( 'invoice_terms' => $invoice_terms,
3964                              'pkg_list'      => [ $cust_pkg ],
3965                            );
3966     if ( $error ) {
3967       $dbh->rollback if $oldAutoCommit;
3968       return $error;
3969     }   
3970   }
3971
3972   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
3973   return '';
3974
3975 }
3976
3977 #=item charge_postal_fee
3978 #
3979 #Applies a one time charge this customer.  If there is an error,
3980 #returns the error, returns the cust_pkg charge object or false
3981 #if there was no charge.
3982 #
3983 #=cut
3984 #
3985 # This should be a customer event.  For that to work requires that bill
3986 # also be a customer event.
3987
3988 sub charge_postal_fee {
3989   my $self = shift;
3990
3991   my $pkgpart = $conf->config('postal_invoice-fee_pkgpart', $self->agentnum);
3992   return '' unless ($pkgpart && grep { $_ eq 'POST' } $self->invoicing_list);
3993
3994   my $cust_pkg = new FS::cust_pkg ( {
3995     'custnum'  => $self->custnum,
3996     'pkgpart'  => $pkgpart,
3997     'quantity' => 1,
3998   } );
3999
4000   my $error = $cust_pkg->insert;
4001   $error ? $error : $cust_pkg;
4002 }
4003
4004 =item cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4005
4006 Returns all the invoices (see L<FS::cust_bill>) for this customer.
4007
4008 Optionally, a list or hashref of additional arguments to the qsearch call can
4009 be passed.
4010
4011 =cut
4012
4013 sub cust_bill {
4014   my $self = shift;
4015   my $opt = ref($_[0]) ? shift : { @_ };
4016
4017   #return $self->num_cust_bill unless wantarray || keys %$opt;
4018
4019   $opt->{'table'} = 'cust_bill';
4020   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4021   $opt->{'hashref'}{'custnum'} = $self->custnum;
4022   $opt->{'order_by'} ||= 'ORDER BY _date ASC';
4023
4024   map { $_ } #behavior of sort undefined in scalar context
4025     sort { $a->_date <=> $b->_date }
4026       qsearch($opt);
4027 }
4028
4029 =item open_cust_bill
4030
4031 Returns all the open (owed > 0) invoices (see L<FS::cust_bill>) for this
4032 customer.
4033
4034 =cut
4035
4036 sub open_cust_bill {
4037   my $self = shift;
4038
4039   $self->cust_bill(
4040     'extra_sql' => ' AND '. FS::cust_bill->owed_sql. ' > 0',
4041     #@_
4042   );
4043
4044 }
4045
4046 =item legacy_cust_bill [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4047
4048 Returns all the legacy invoices (see L<FS::legacy_cust_bill>) for this customer.
4049
4050 =cut
4051
4052 sub legacy_cust_bill {
4053   my $self = shift;
4054
4055   #return $self->num_legacy_cust_bill unless wantarray;
4056
4057   map { $_ } #behavior of sort undefined in scalar context
4058     sort { $a->_date <=> $b->_date }
4059       qsearch({ 'table'    => 'legacy_cust_bill',
4060                 'hashref'  => { 'custnum' => $self->custnum, },
4061                 'order_by' => 'ORDER BY _date ASC',
4062              });
4063 }
4064
4065 =item cust_statement [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4066
4067 Returns all the statements (see L<FS::cust_statement>) for this customer.
4068
4069 Optionally, a list or hashref of additional arguments to the qsearch call can
4070 be passed.
4071
4072 =cut
4073
4074 =item cust_bill_void
4075
4076 Returns all the voided invoices (see L<FS::cust_bill_void>) for this customer.
4077
4078 =cut
4079
4080 sub cust_bill_void {
4081   my $self = shift;
4082
4083   map { $_ } #return $self->num_cust_bill_void unless wantarray;
4084   sort { $a->_date <=> $b->_date }
4085     qsearch( 'cust_bill_void', { 'custnum' => $self->custnum } )
4086 }
4087
4088 sub cust_statement {
4089   my $self = shift;
4090   my $opt = ref($_[0]) ? shift : { @_ };
4091
4092   #return $self->num_cust_statement unless wantarray || keys %$opt;
4093
4094   $opt->{'table'} = 'cust_statement';
4095   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4096   $opt->{'hashref'}{'custnum'} = $self->custnum;
4097   $opt->{'order_by'} ||= 'ORDER BY _date ASC';
4098
4099   map { $_ } #behavior of sort undefined in scalar context
4100     sort { $a->_date <=> $b->_date }
4101       qsearch($opt);
4102 }
4103
4104 =item svc_x SVCDB [ OPTION => VALUE | EXTRA_QSEARCH_PARAMS_HASHREF ]
4105
4106 Returns all services of type SVCDB (such as 'svc_acct') for this customer.  
4107
4108 Optionally, a list or hashref of additional arguments to the qsearch call can 
4109 be passed following the SVCDB.
4110
4111 =cut
4112
4113 sub svc_x {
4114   my $self = shift;
4115   my $svcdb = shift;
4116   if ( ! $svcdb =~ /^svc_\w+$/ ) {
4117     warn "$me svc_x requires a svcdb";
4118     return;
4119   }
4120   my $opt = ref($_[0]) ? shift : { @_ };
4121
4122   $opt->{'table'} = $svcdb;
4123   $opt->{'addl_from'} = 
4124     'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum) '.
4125     ($opt->{'addl_from'} || '');
4126
4127   my $custnum = $self->custnum;
4128   $custnum =~ /^\d+$/ or die "bad custnum '$custnum'";
4129   my $where = "cust_pkg.custnum = $custnum";
4130
4131   my $extra_sql = $opt->{'extra_sql'} || '';
4132   if ( keys %{ $opt->{'hashref'} } ) {
4133     $extra_sql = " AND $where $extra_sql";
4134   }
4135   else {
4136     if ( $opt->{'extra_sql'} =~ /^\s*where\s(.*)/si ) {
4137       $extra_sql = "WHERE $where AND $1";
4138     }
4139     else {
4140       $extra_sql = "WHERE $where $extra_sql";
4141     }
4142   }
4143   $opt->{'extra_sql'} = $extra_sql;
4144
4145   qsearch($opt);
4146 }
4147
4148 # required for use as an eventtable; 
4149 sub svc_acct {
4150   my $self = shift;
4151   $self->svc_x('svc_acct', @_);
4152 }
4153
4154 =item cust_credit
4155
4156 Returns all the credits (see L<FS::cust_credit>) for this customer.
4157
4158 =cut
4159
4160 sub cust_credit {
4161   my $self = shift;
4162   map { $_ } #return $self->num_cust_credit unless wantarray;
4163   sort { $a->_date <=> $b->_date }
4164     qsearch( 'cust_credit', { 'custnum' => $self->custnum } )
4165 }
4166
4167 =item cust_credit_pkgnum
4168
4169 Returns all the credits (see L<FS::cust_credit>) for this customer's specific
4170 package when using experimental package balances.
4171
4172 =cut
4173
4174 sub cust_credit_pkgnum {
4175   my( $self, $pkgnum ) = @_;
4176   map { $_ } #return $self->num_cust_credit_pkgnum($pkgnum) unless wantarray;
4177   sort { $a->_date <=> $b->_date }
4178     qsearch( 'cust_credit', { 'custnum' => $self->custnum,
4179                               'pkgnum'  => $pkgnum,
4180                             }
4181     );
4182 }
4183
4184 =item cust_credit_void
4185
4186 Returns all voided credits (see L<FS::cust_credit_void>) for this customer.
4187
4188 =cut
4189
4190 sub cust_credit_void {
4191   my $self = shift;
4192   map { $_ }
4193   sort { $a->_date <=> $b->_date }
4194     qsearch( 'cust_credit_void', { 'custnum' => $self->custnum } )
4195 }
4196
4197 =item cust_pay
4198
4199 Returns all the payments (see L<FS::cust_pay>) for this customer.
4200
4201 =cut
4202
4203 sub cust_pay {
4204   my $self = shift;
4205   my $opt = ref($_[0]) ? shift : { @_ };
4206
4207   return $self->num_cust_pay unless wantarray || keys %$opt;
4208
4209   $opt->{'table'} = 'cust_pay';
4210   $opt->{'hashref'}{'custnum'} = $self->custnum;
4211
4212   map { $_ } #behavior of sort undefined in scalar context
4213     sort { $a->_date <=> $b->_date }
4214       qsearch($opt);
4215
4216 }
4217
4218 =item num_cust_pay
4219
4220 Returns the number of payments (see L<FS::cust_pay>) for this customer.  Also
4221 called automatically when the cust_pay method is used in a scalar context.
4222
4223 =cut
4224
4225 sub num_cust_pay {
4226   my $self = shift;
4227   my $sql = "SELECT COUNT(*) FROM cust_pay WHERE custnum = ?";
4228   my $sth = dbh->prepare($sql) or die dbh->errstr;
4229   $sth->execute($self->custnum) or die $sth->errstr;
4230   $sth->fetchrow_arrayref->[0];
4231 }
4232
4233 =item unapplied_cust_pay
4234
4235 Returns all the unapplied payments (see L<FS::cust_pay>) for this customer.
4236
4237 =cut
4238
4239 sub unapplied_cust_pay {
4240   my $self = shift;
4241
4242   $self->cust_pay(
4243     'extra_sql' => ' AND '. FS::cust_pay->unapplied_sql. ' > 0',
4244     #@_
4245   );
4246
4247 }
4248
4249 =item cust_pay_pkgnum
4250
4251 Returns all the payments (see L<FS::cust_pay>) for this customer's specific
4252 package when using experimental package balances.
4253
4254 =cut
4255
4256 sub cust_pay_pkgnum {
4257   my( $self, $pkgnum ) = @_;
4258   map { $_ } #return $self->num_cust_pay_pkgnum($pkgnum) unless wantarray;
4259   sort { $a->_date <=> $b->_date }
4260     qsearch( 'cust_pay', { 'custnum' => $self->custnum,
4261                            'pkgnum'  => $pkgnum,
4262                          }
4263     );
4264 }
4265
4266 =item cust_pay_void
4267
4268 Returns all voided payments (see L<FS::cust_pay_void>) for this customer.
4269
4270 =cut
4271
4272 sub cust_pay_void {
4273   my $self = shift;
4274   map { $_ } #return $self->num_cust_pay_void unless wantarray;
4275   sort { $a->_date <=> $b->_date }
4276     qsearch( 'cust_pay_void', { 'custnum' => $self->custnum } )
4277 }
4278
4279 =item cust_pay_batch [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
4280
4281 Returns all batched payments (see L<FS::cust_pay_batch>) for this customer.
4282
4283 Optionally, a list or hashref of additional arguments to the qsearch call can
4284 be passed.
4285
4286 =cut
4287
4288 sub cust_pay_batch {
4289   my $self = shift;
4290   my $opt = ref($_[0]) ? shift : { @_ };
4291
4292   #return $self->num_cust_statement unless wantarray || keys %$opt;
4293
4294   $opt->{'table'} = 'cust_pay_batch';
4295   $opt->{'hashref'} ||= {}; #i guess it would autovivify anyway...
4296   $opt->{'hashref'}{'custnum'} = $self->custnum;
4297   $opt->{'order_by'} ||= 'ORDER BY paybatchnum ASC';
4298
4299   map { $_ } #behavior of sort undefined in scalar context
4300     sort { $a->paybatchnum <=> $b->paybatchnum }
4301       qsearch($opt);
4302 }
4303
4304 =item cust_pay_pending
4305
4306 Returns all pending payments (see L<FS::cust_pay_pending>) for this customer
4307 (without status "done").
4308
4309 =cut
4310
4311 sub cust_pay_pending {
4312   my $self = shift;
4313   return $self->num_cust_pay_pending unless wantarray;
4314   sort { $a->_date <=> $b->_date }
4315     qsearch( 'cust_pay_pending', {
4316                                    'custnum' => $self->custnum,
4317                                    'status'  => { op=>'!=', value=>'done' },
4318                                  },
4319            );
4320 }
4321
4322 =item cust_pay_pending_attempt
4323
4324 Returns all payment attempts / declined payments for this customer, as pending
4325 payments objects (see L<FS::cust_pay_pending>), with status "done" but without
4326 a corresponding payment (see L<FS::cust_pay>).
4327
4328 =cut
4329
4330 sub cust_pay_pending_attempt {
4331   my $self = shift;
4332   return $self->num_cust_pay_pending_attempt unless wantarray;
4333   sort { $a->_date <=> $b->_date }
4334     qsearch( 'cust_pay_pending', {
4335                                    'custnum' => $self->custnum,
4336                                    'status'  => 'done',
4337                                    'paynum'  => '',
4338                                  },
4339            );
4340 }
4341
4342 =item num_cust_pay_pending
4343
4344 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
4345 customer (without status "done").  Also called automatically when the
4346 cust_pay_pending method is used in a scalar context.
4347
4348 =cut
4349
4350 sub num_cust_pay_pending {
4351   my $self = shift;
4352   $self->scalar_sql(
4353     " SELECT COUNT(*) FROM cust_pay_pending ".
4354       " WHERE custnum = ? AND status != 'done' ",
4355     $self->custnum
4356   );
4357 }
4358
4359 =item num_cust_pay_pending_attempt
4360
4361 Returns the number of pending payments (see L<FS::cust_pay_pending>) for this
4362 customer, with status "done" but without a corresp.  Also called automatically when the
4363 cust_pay_pending method is used in a scalar context.
4364
4365 =cut
4366
4367 sub num_cust_pay_pending_attempt {
4368   my $self = shift;
4369   $self->scalar_sql(
4370     " SELECT COUNT(*) FROM cust_pay_pending ".
4371       " WHERE custnum = ? AND status = 'done' AND paynum IS NULL",
4372     $self->custnum
4373   );
4374 }
4375
4376 =item cust_refund
4377
4378 Returns all the refunds (see L<FS::cust_refund>) for this customer.
4379
4380 =cut
4381
4382 sub cust_refund {
4383   my $self = shift;
4384   map { $_ } #return $self->num_cust_refund unless wantarray;
4385   sort { $a->_date <=> $b->_date }
4386     qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
4387 }
4388
4389 =item display_custnum
4390
4391 Returns the displayed customer number for this customer: agent_custid if
4392 cust_main-default_agent_custid is set and it has a value, custnum otherwise.
4393
4394 =cut
4395
4396 sub display_custnum {
4397   my $self = shift;
4398
4399   return $self->agent_custid
4400     if $default_agent_custid && $self->agent_custid;
4401
4402   my $prefix = $conf->config('cust_main-custnum-display_prefix', $self->agentnum) || '';
4403
4404   if ( $prefix ) {
4405     return $prefix . 
4406            sprintf('%0'.($custnum_display_length||8).'d', $self->custnum)
4407   } elsif ( $custnum_display_length ) {
4408     return sprintf('%0'.$custnum_display_length.'d', $self->custnum);
4409   } else {
4410     return $self->custnum;
4411   }
4412 }
4413
4414 =item name
4415
4416 Returns a name string for this customer, either "Company (Last, First)" or
4417 "Last, First".
4418
4419 =cut
4420
4421 sub name {
4422   my $self = shift;
4423   my $name = $self->contact;
4424   $name = $self->company. " ($name)" if $self->company;
4425   $name;
4426 }
4427
4428 =item service_contact
4429
4430 Returns the L<FS::contact> object for this customer that has the 'Service'
4431 contact class, or undef if there is no such contact.  Deprecated; don't use
4432 this in new code.
4433
4434 =cut
4435
4436 sub service_contact {
4437   my $self = shift;
4438   if ( !exists($self->{service_contact}) ) {
4439     my $classnum = $self->scalar_sql(
4440       'SELECT classnum FROM contact_class WHERE classname = \'Service\''
4441     ) || 0; #if it's zero, qsearchs will return nothing
4442     $self->{service_contact} = qsearchs('contact', { 
4443         'classnum' => $classnum, 'custnum' => $self->custnum
4444       }) || undef;
4445   }
4446   $self->{service_contact};
4447 }
4448
4449 =item ship_name
4450
4451 Returns a name string for this (service/shipping) contact, either
4452 "Company (Last, First)" or "Last, First".
4453
4454 =cut
4455
4456 sub ship_name {
4457   my $self = shift;
4458
4459   my $name = $self->ship_contact;
4460   $name = $self->company. " ($name)" if $self->company;
4461   $name;
4462 }
4463
4464 =item name_short
4465
4466 Returns a name string for this customer, either "Company" or "First Last".
4467
4468 =cut
4469
4470 sub name_short {
4471   my $self = shift;
4472   $self->company !~ /^\s*$/ ? $self->company : $self->contact_firstlast;
4473 }
4474
4475 =item ship_name_short
4476
4477 Returns a name string for this (service/shipping) contact, either "Company"
4478 or "First Last".
4479
4480 =cut
4481
4482 sub ship_name_short {
4483   my $self = shift;
4484   $self->service_contact 
4485     ? $self->ship_contact_firstlast 
4486     : $self->name_short
4487 }
4488
4489 =item contact
4490
4491 Returns this customer's full (billing) contact name only, "Last, First"
4492
4493 =cut
4494
4495 sub contact {
4496   my $self = shift;
4497   $self->get('last'). ', '. $self->first;
4498 }
4499
4500 =item ship_contact
4501
4502 Returns this customer's full (shipping) contact name only, "Last, First"
4503
4504 =cut
4505
4506 sub ship_contact {
4507   my $self = shift;
4508   my $contact = $self->service_contact || $self;
4509   $contact->get('last') . ', ' . $contact->get('first');
4510 }
4511
4512 =item contact_firstlast
4513
4514 Returns this customers full (billing) contact name only, "First Last".
4515
4516 =cut
4517
4518 sub contact_firstlast {
4519   my $self = shift;
4520   $self->first. ' '. $self->get('last');
4521 }
4522
4523 =item ship_contact_firstlast
4524
4525 Returns this customer's full (shipping) contact name only, "First Last".
4526
4527 =cut
4528
4529 sub ship_contact_firstlast {
4530   my $self = shift;
4531   my $contact = $self->service_contact || $self;
4532   $contact->get('first') . ' '. $contact->get('last');
4533 }
4534
4535 sub bill_country_full {
4536   my $self = shift;
4537   $self->bill_location->country_full;
4538 }
4539
4540 sub ship_country_full {
4541   my $self = shift;
4542   $self->ship_location->country_full;
4543 }
4544
4545 =item county_state_county [ PREFIX ]
4546
4547 Returns a string consisting of just the county, state and country.
4548
4549 =cut
4550
4551 sub county_state_country {
4552   my $self = shift;
4553   my $locationnum;
4554   if ( @_ && $_[0] && $self->has_ship_address ) {
4555     $locationnum = $self->ship_locationnum;
4556   } else {
4557     $locationnum = $self->bill_locationnum;
4558   }
4559   my $cust_location = qsearchs('cust_location', { locationnum=>$locationnum });
4560   $cust_location->county_state_country;
4561 }
4562
4563 =item geocode DATA_VENDOR
4564
4565 Returns a value for the customer location as encoded by DATA_VENDOR.
4566 Currently this only makes sense for "CCH" as DATA_VENDOR.
4567
4568 =cut
4569
4570 =item cust_status
4571
4572 =item status
4573
4574 Returns a status string for this customer, currently:
4575
4576 =over 4
4577
4578 =item prospect - No packages have ever been ordered
4579
4580 =item ordered - Recurring packages all are new (not yet billed).
4581
4582 =item active - One or more recurring packages is active
4583
4584 =item inactive - No active recurring packages, but otherwise unsuspended/uncancelled (the inactive status is new - previously inactive customers were mis-identified as cancelled)
4585
4586 =item suspended - All non-cancelled recurring packages are suspended
4587
4588 =item cancelled - All recurring packages are cancelled
4589
4590 =back
4591
4592 Behavior of inactive vs. cancelled edge cases can be adjusted with the
4593 cust_main-status_module configuration option.
4594
4595 =cut
4596
4597 sub status { shift->cust_status(@_); }
4598
4599 sub cust_status {
4600   my $self = shift;
4601   return $self->hashref->{cust_status} if $self->hashref->{cust_status};
4602   for my $status ( FS::cust_main->statuses() ) {
4603     my $method = $status.'_sql';
4604     my $numnum = ( my $sql = $self->$method() ) =~ s/cust_main\.custnum/?/g;
4605     my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
4606     $sth->execute( ($self->custnum) x $numnum )
4607       or die "Error executing 'SELECT $sql': ". $sth->errstr;
4608     if ( $sth->fetchrow_arrayref->[0] ) {
4609       $self->hashref->{cust_status} = $status;
4610       return $status;
4611     }
4612   }
4613 }
4614
4615 =item is_status_delay_cancel
4616
4617 Returns true if customer status is 'suspended'
4618 and all suspended cust_pkg return true for
4619 cust_pkg->is_status_delay_cancel.
4620
4621 This is not a real status, this only meant for hacking display 
4622 values, because otherwise treating the customer as suspended is 
4623 really the whole point of the delay_cancel option.
4624
4625 =cut
4626
4627 sub is_status_delay_cancel {
4628   my ($self) = @_;
4629   return 0 unless $self->status eq 'suspended';
4630   foreach my $cust_pkg ($self->ncancelled_pkgs) {
4631     return 0 unless $cust_pkg->is_status_delay_cancel;
4632   }
4633   return 1;
4634 }
4635
4636 =item ucfirst_cust_status
4637
4638 =item ucfirst_status
4639
4640 Returns the status with the first character capitalized.
4641
4642 =cut
4643
4644 sub ucfirst_status { shift->ucfirst_cust_status(@_); }
4645
4646 sub ucfirst_cust_status {
4647   my $self = shift;
4648   ucfirst($self->cust_status);
4649 }
4650
4651 =item statuscolor
4652
4653 Returns a hex triplet color string for this customer's status.
4654
4655 =cut
4656
4657 sub statuscolor { shift->cust_statuscolor(@_); }
4658
4659 sub cust_statuscolor {
4660   my $self = shift;
4661   __PACKAGE__->statuscolors->{$self->cust_status};
4662 }
4663
4664 =item tickets [ STATUS ]
4665
4666 Returns an array of hashes representing the customer's RT tickets.
4667
4668 An optional status (or arrayref or hashref of statuses) may be specified.
4669
4670 =cut
4671
4672 sub tickets {
4673   my $self = shift;
4674   my $status = ( @_ && $_[0] ) ? shift : '';
4675
4676   my $num = $conf->config('cust_main-max_tickets') || 10;
4677   my @tickets = ();
4678
4679   if ( $conf->config('ticket_system') ) {
4680     unless ( $conf->config('ticket_system-custom_priority_field') ) {
4681
4682       @tickets = @{ FS::TicketSystem->customer_tickets( $self->custnum,
4683                                                         $num,
4684                                                         undef,
4685                                                         $status,
4686                                                       )
4687                   };
4688
4689     } else {
4690
4691       foreach my $priority (
4692         $conf->config('ticket_system-custom_priority_field-values'), ''
4693       ) {
4694         last if scalar(@tickets) >= $num;
4695         push @tickets, 
4696           @{ FS::TicketSystem->customer_tickets( $self->custnum,
4697                                                  $num - scalar(@tickets),
4698                                                  $priority,
4699                                                  $status,
4700                                                )
4701            };
4702       }
4703     }
4704   }
4705   (@tickets);
4706 }
4707
4708 =item appointments [ STATUS ]
4709
4710 Returns an array of hashes representing the customer's RT tickets which
4711 are appointments.
4712
4713 =cut
4714
4715 sub appointments {
4716   my $self = shift;
4717   my $status = ( @_ && $_[0] ) ? shift : '';
4718
4719   return () unless $conf->config('ticket_system');
4720
4721   my $queueid = $conf->config('ticket_system-appointment-queueid');
4722
4723   @{ FS::TicketSystem->customer_tickets( $self->custnum,
4724                                          99,
4725                                          undef,
4726                                          $status,
4727                                          $queueid,
4728                                        )
4729   };
4730 }
4731
4732 # Return services representing svc_accts in customer support packages
4733 sub support_services {
4734   my $self = shift;
4735   my %packages = map { $_ => 1 } $conf->config('support_packages');
4736
4737   grep { $_->pkg_svc && $_->pkg_svc->primary_svc eq 'Y' }
4738     grep { $_->part_svc->svcdb eq 'svc_acct' }
4739     map { $_->cust_svc }
4740     grep { exists $packages{ $_->pkgpart } }
4741     $self->ncancelled_pkgs;
4742
4743 }
4744
4745 # Return a list of latitude/longitude for one of the services (if any)
4746 sub service_coordinates {
4747   my $self = shift;
4748
4749   my @svc_X = 
4750     grep { $_->latitude && $_->longitude }
4751     map { $_->svc_x }
4752     map { $_->cust_svc }
4753     $self->ncancelled_pkgs;
4754
4755   scalar(@svc_X) ? ( $svc_X[0]->latitude, $svc_X[0]->longitude ) : ()
4756 }
4757
4758 =item masked FIELD
4759
4760 Returns a masked version of the named field
4761
4762 =cut
4763
4764 sub masked {
4765 my ($self,$field) = @_;
4766
4767 # Show last four
4768
4769 'x'x(length($self->getfield($field))-4).
4770   substr($self->getfield($field), (length($self->getfield($field))-4));
4771
4772 }
4773
4774 =item payment_history
4775
4776 Returns an array of hashrefs standardizing information from cust_bill, cust_pay,
4777 cust_credit and cust_refund objects.  Each hashref has the following fields:
4778
4779 I<type> - one of 'Line item', 'Invoice', 'Payment', 'Credit', 'Refund' or 'Previous'
4780
4781 I<date> - value of _date field, unix timestamp
4782
4783 I<date_pretty> - user-friendly date
4784
4785 I<description> - user-friendly description of item
4786
4787 I<amount> - impact of item on user's balance 
4788 (positive for Invoice/Refund/Line item, negative for Payment/Credit.)
4789 Not to be confused with the native 'amount' field in cust_credit, see below.
4790
4791 I<amount_pretty> - includes money char
4792
4793 I<balance> - customer balance, chronologically as of this item
4794
4795 I<balance_pretty> - includes money char
4796
4797 I<charged> - amount charged for cust_bill (Invoice or Line item) records, undef for other types
4798
4799 I<paid> - amount paid for cust_pay records, undef for other types
4800
4801 I<credit> - amount credited for cust_credit records, undef for other types.
4802 Literally the 'amount' field from cust_credit, renamed here to avoid confusion.
4803
4804 I<refund> - amount refunded for cust_refund records, undef for other types
4805
4806 The four table-specific keys always have positive values, whether they reflect charges or payments.
4807
4808 The following options may be passed to this method:
4809
4810 I<line_items> - if true, returns charges ('Line item') rather than invoices
4811
4812 I<start_date> - unix timestamp, only include records on or after.
4813 If specified, an item of type 'Previous' will also be included.
4814 It does not have table-specific fields.
4815
4816 I<end_date> - unix timestamp, only include records before
4817
4818 I<reverse_sort> - order from newest to oldest (default is oldest to newest)
4819
4820 I<conf> - optional already-loaded FS::Conf object.
4821
4822 =cut
4823
4824 # Caution: this gets used by FS::ClientAPI::MyAccount::billing_history,
4825 # and also for sending customer statements, which should both be kept customer-friendly.
4826 # If you add anything that shouldn't be passed on through the API or exposed 
4827 # to customers, add a new option to include it, don't include it by default
4828 sub payment_history {
4829   my $self = shift;
4830   my $opt = ref($_[0]) ? $_[0] : { @_ };
4831
4832   my $conf = $$opt{'conf'} || new FS::Conf;
4833   my $money_char = $conf->config("money_char") || '$',
4834
4835   #first load entire history, 
4836   #need previous to calculate previous balance
4837   #loading after end_date shouldn't hurt too much?
4838   my @history = ();
4839   if ( $$opt{'line_items'} ) {
4840
4841     foreach my $cust_bill ( $self->cust_bill ) {
4842
4843       push @history, {
4844         'type'        => 'Line item',
4845         'description' => $_->desc( $self->locale ).
4846                            ( $_->sdate && $_->edate
4847                                ? ' '. time2str('%d-%b-%Y', $_->sdate).
4848                                  ' To '. time2str('%d-%b-%Y', $_->edate)
4849                                : ''
4850                            ),
4851         'amount'      => sprintf('%.2f', $_->setup + $_->recur ),
4852         'charged'     => sprintf('%.2f', $_->setup + $_->recur ),
4853         'date'        => $cust_bill->_date,
4854         'date_pretty' => $self->time2str_local('short', $cust_bill->_date ),
4855       }
4856         foreach $cust_bill->cust_bill_pkg;
4857
4858     }
4859
4860   } else {
4861
4862     push @history, {
4863                      'type'        => 'Invoice',
4864                      'description' => 'Invoice #'. $_->display_invnum,
4865                      'amount'      => sprintf('%.2f', $_->charged ),
4866                      'charged'     => sprintf('%.2f', $_->charged ),
4867                      'date'        => $_->_date,
4868                      'date_pretty' => $self->time2str_local('short', $_->_date ),
4869                    }
4870       foreach $self->cust_bill;
4871
4872   }
4873
4874   push @history, {
4875                    'type'        => 'Payment',
4876                    'description' => 'Payment', #XXX type
4877                    'amount'      => sprintf('%.2f', 0 - $_->paid ),
4878                    'paid'        => sprintf('%.2f', $_->paid ),
4879                    'date'        => $_->_date,
4880                    'date_pretty' => $self->time2str_local('short', $_->_date ),
4881                  }
4882     foreach $self->cust_pay;
4883
4884   push @history, {
4885                    'type'        => 'Credit',
4886                    'description' => 'Credit', #more info?
4887                    'amount'      => sprintf('%.2f', 0 -$_->amount ),
4888                    'credit'      => sprintf('%.2f', $_->amount ),
4889                    'date'        => $_->_date,
4890                    'date_pretty' => $self->time2str_local('short', $_->_date ),
4891                  }
4892     foreach $self->cust_credit;
4893
4894   push @history, {
4895                    'type'        => 'Refund',
4896                    'description' => 'Refund', #more info?  type, like payment?
4897                    'amount'      => $_->refund,
4898                    'refund'      => $_->refund,
4899                    'date'        => $_->_date,
4900                    'date_pretty' => $self->time2str_local('short', $_->_date ),
4901                  }
4902     foreach $self->cust_refund;
4903
4904   #put it all in chronological order
4905   @history = sort { $a->{'date'} <=> $b->{'date'} } @history;
4906
4907   #calculate balance, filter items outside date range
4908   my $previous = 0;
4909   my $balance = 0;
4910   my @out = ();
4911   foreach my $item (@history) {
4912     last if $$opt{'end_date'} && ($$item{'date'} >= $$opt{'end_date'});
4913     $balance += $$item{'amount'};
4914     if ($$opt{'start_date'} && ($$item{'date'} < $$opt{'start_date'})) {
4915       $previous += $$item{'amount'};
4916       next;
4917     }
4918     $$item{'balance'} = sprintf("%.2f",$balance);
4919     foreach my $key ( qw(amount balance) ) {
4920       $$item{$key.'_pretty'} = money_pretty($$item{$key});
4921     }
4922     push(@out,$item);
4923   }
4924
4925   # start with previous balance, if there was one
4926   if ($previous) {
4927     my $item = {
4928       'type'        => 'Previous',
4929       'description' => 'Previous balance',
4930       'amount'      => sprintf("%.2f",$previous),
4931       'balance'     => sprintf("%.2f",$previous),
4932       'date'        => $$opt{'start_date'},
4933       'date_pretty' => $self->time2str_local('short', $$opt{'start_date'} ),
4934     };
4935     #false laziness with above
4936     foreach my $key ( qw(amount balance) ) {
4937       $$item{$key.'_pretty'} = $$item{$key};
4938       $$item{$key.'_pretty'} =~ s/^(-?)/$1$money_char/;
4939     }
4940     unshift(@out,$item);
4941   }
4942
4943   @out = reverse @history if $$opt{'reverse_sort'};
4944
4945   return @out;
4946 }
4947
4948 =back
4949
4950 =head1 CLASS METHODS
4951
4952 =over 4
4953
4954 =item statuses
4955
4956 Class method that returns the list of possible status strings for customers
4957 (see L<the status method|/status>).  For example:
4958
4959   @statuses = FS::cust_main->statuses();
4960
4961 =cut
4962
4963 sub statuses {
4964   my $self = shift;
4965   keys %{ $self->statuscolors };
4966 }
4967
4968 =item cust_status_sql
4969
4970 Returns an SQL fragment to determine the status of a cust_main record, as a 
4971 string.
4972
4973 =cut
4974
4975 sub cust_status_sql {
4976   my $sql = 'CASE';
4977   for my $status ( FS::cust_main->statuses() ) {
4978     my $method = $status.'_sql';
4979     $sql .= ' WHEN ('.FS::cust_main->$method.") THEN '$status'";
4980   }
4981   $sql .= ' END';
4982   return $sql;
4983 }
4984
4985
4986 =item prospect_sql
4987
4988 Returns an SQL expression identifying prospective cust_main records (customers
4989 with no packages ever ordered)
4990
4991 =cut
4992
4993 use vars qw($select_count_pkgs);
4994 $select_count_pkgs =
4995   "SELECT COUNT(*) FROM cust_pkg
4996     WHERE cust_pkg.custnum = cust_main.custnum";
4997
4998 sub select_count_pkgs_sql {
4999   $select_count_pkgs;
5000 }
5001
5002 sub prospect_sql {
5003   " 0 = ( $select_count_pkgs ) ";
5004 }
5005
5006 =item ordered_sql
5007
5008 Returns an SQL expression identifying ordered cust_main records (customers with
5009 no active packages, but recurring packages not yet setup or one time charges
5010 not yet billed).
5011
5012 =cut
5013
5014 sub ordered_sql {
5015   FS::cust_main->none_active_sql.
5016   " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->not_yet_billed_sql. " ) ";
5017 }
5018
5019 =item active_sql
5020
5021 Returns an SQL expression identifying active cust_main records (customers with
5022 active recurring packages).
5023
5024 =cut
5025
5026 sub active_sql {
5027   " 0 < ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
5028 }
5029
5030 =item none_active_sql
5031
5032 Returns an SQL expression identifying cust_main records with no active
5033 recurring packages.  This includes customers of status prospect, ordered,
5034 inactive, and suspended.
5035
5036 =cut
5037
5038 sub none_active_sql {
5039   " 0 = ( $select_count_pkgs AND ". FS::cust_pkg->active_sql. " ) ";
5040 }
5041
5042 =item inactive_sql
5043
5044 Returns an SQL expression identifying inactive cust_main records (customers with
5045 no active recurring packages, but otherwise unsuspended/uncancelled).
5046
5047 =cut
5048
5049 sub inactive_sql {
5050   FS::cust_main->none_active_sql.
5051   " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->inactive_sql. " ) ";
5052 }
5053
5054 =item susp_sql
5055 =item suspended_sql
5056
5057 Returns an SQL expression identifying suspended cust_main records.
5058
5059 =cut
5060
5061
5062 sub suspended_sql { susp_sql(@_); }
5063 sub susp_sql {
5064   FS::cust_main->none_active_sql.
5065   " AND 0 < ( $select_count_pkgs AND ". FS::cust_pkg->suspended_sql. " ) ";
5066 }
5067
5068 =item cancel_sql
5069 =item cancelled_sql
5070
5071 Returns an SQL expression identifying cancelled cust_main records.
5072
5073 =cut
5074
5075 sub cancel_sql { shift->cancelled_sql(@_); }
5076
5077 =item uncancel_sql
5078 =item uncancelled_sql
5079
5080 Returns an SQL expression identifying un-cancelled cust_main records.
5081
5082 =cut
5083
5084 sub uncancelled_sql { uncancel_sql(@_); }
5085 sub uncancel_sql {
5086   my $self = shift;
5087   "( NOT (".$self->cancelled_sql.") )"; #sensitive to cust_main-status_module
5088 }
5089
5090 =item balance_sql
5091
5092 Returns an SQL fragment to retreive the balance.
5093
5094 =cut
5095
5096 sub balance_sql { "
5097     ( SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill
5098         WHERE cust_bill.custnum   = cust_main.custnum     )
5099   - ( SELECT COALESCE( SUM(paid),    0 ) FROM cust_pay
5100         WHERE cust_pay.custnum    = cust_main.custnum     )
5101   - ( SELECT COALESCE( SUM(amount),  0 ) FROM cust_credit
5102         WHERE cust_credit.custnum = cust_main.custnum     )
5103   + ( SELECT COALESCE( SUM(refund),  0 ) FROM cust_refund
5104         WHERE cust_refund.custnum = cust_main.custnum     )
5105 "; }
5106
5107 =item balance_date_sql [ START_TIME [ END_TIME [ OPTION => VALUE ... ] ] ]
5108
5109 Returns an SQL fragment to retreive the balance for this customer, optionally
5110 considering invoices with date earlier than START_TIME, and not
5111 later than END_TIME (total_owed_date minus total_unapplied_credits minus
5112 total_unapplied_payments).
5113
5114 Times are specified as SQL fragments or numeric
5115 UNIX timestamps; see L<perlfunc/"time">).  Also see L<Time::Local> and
5116 L<Date::Parse> for conversion functions.  The empty string can be passed
5117 to disable that time constraint completely.
5118
5119 Available options are:
5120
5121 =over 4
5122
5123 =item unapplied_date
5124
5125 set to true to disregard unapplied credits, payments and refunds outside the specified time period - by default the time period restriction only applies to invoices (useful for reporting, probably a bad idea for event triggering)
5126
5127 =item total
5128
5129 (unused.  obsolete?)
5130 set to true to remove all customer comparison clauses, for totals
5131
5132 =item where
5133
5134 (unused.  obsolete?)
5135 WHERE clause hashref (elements "AND"ed together) (typically used with the total option)
5136
5137 =item join
5138
5139 (unused.  obsolete?)
5140 JOIN clause (typically used with the total option)
5141
5142 =item cutoff
5143
5144 An absolute cutoff time.  Payments, credits, and refunds I<applied> after this 
5145 time will be ignored.  Note that START_TIME and END_TIME only limit the date 
5146 range for invoices and I<unapplied> payments, credits, and refunds.
5147
5148 =back
5149
5150 =cut
5151
5152 sub balance_date_sql {
5153   my( $class, $start, $end, %opt ) = @_;
5154
5155   my $cutoff = $opt{'cutoff'};
5156
5157   my $owed         = FS::cust_bill->owed_sql($cutoff);
5158   my $unapp_refund = FS::cust_refund->unapplied_sql($cutoff);
5159   my $unapp_credit = FS::cust_credit->unapplied_sql($cutoff);
5160   my $unapp_pay    = FS::cust_pay->unapplied_sql($cutoff);
5161
5162   my $j = $opt{'join'} || '';
5163
5164   my $owed_wh   = $class->_money_table_where( 'cust_bill',   $start,$end,%opt );
5165   my $refund_wh = $class->_money_table_where( 'cust_refund', $start,$end,%opt );
5166   my $credit_wh = $class->_money_table_where( 'cust_credit', $start,$end,%opt );
5167   my $pay_wh    = $class->_money_table_where( 'cust_pay',    $start,$end,%opt );
5168
5169   "   ( SELECT COALESCE(SUM($owed),         0) FROM cust_bill   $j $owed_wh   )
5170     + ( SELECT COALESCE(SUM($unapp_refund), 0) FROM cust_refund $j $refund_wh )
5171     - ( SELECT COALESCE(SUM($unapp_credit), 0) FROM cust_credit $j $credit_wh )
5172     - ( SELECT COALESCE(SUM($unapp_pay),    0) FROM cust_pay    $j $pay_wh    )
5173   ";
5174
5175 }
5176
5177 =item unapplied_payments_date_sql START_TIME [ END_TIME ]
5178
5179 Returns an SQL fragment to retreive the total unapplied payments for this
5180 customer, only considering payments with date earlier than START_TIME, and
5181 optionally not later than END_TIME.
5182
5183 Times are specified as SQL fragments or numeric
5184 UNIX timestamps; see L<perlfunc/"time">).  Also see L<Time::Local> and
5185 L<Date::Parse> for conversion functions.  The empty string can be passed
5186 to disable that time constraint completely.
5187
5188 Available options are:
5189
5190 =cut
5191
5192 sub unapplied_payments_date_sql {
5193   my( $class, $start, $end, %opt ) = @_;
5194
5195   my $cutoff = $opt{'cutoff'};
5196
5197   my $unapp_pay    = FS::cust_pay->unapplied_sql($cutoff);
5198
5199   my $pay_where = $class->_money_table_where( 'cust_pay', $start, $end,
5200                                                           'unapplied_date'=>1 );
5201
5202   " ( SELECT COALESCE(SUM($unapp_pay), 0) FROM cust_pay $pay_where ) ";
5203 }
5204
5205 =item _money_table_where TABLE START_TIME [ END_TIME [ OPTION => VALUE ... ] ]
5206
5207 Helper method for balance_date_sql; name (and usage) subject to change
5208 (suggestions welcome).
5209
5210 Returns a WHERE clause for the specified monetary TABLE (cust_bill,
5211 cust_refund, cust_credit or cust_pay).
5212
5213 If TABLE is "cust_bill" or the unapplied_date option is true, only
5214 considers records with date earlier than START_TIME, and optionally not
5215 later than END_TIME .
5216
5217 =cut
5218
5219 sub _money_table_where {
5220   my( $class, $table, $start, $end, %opt ) = @_;
5221
5222   my @where = ();
5223   push @where, "cust_main.custnum = $table.custnum" unless $opt{'total'};
5224   if ( $table eq 'cust_bill' || $opt{'unapplied_date'} ) {
5225     push @where, "$table._date <= $start" if defined($start) && length($start);
5226     push @where, "$table._date >  $end"   if defined($end)   && length($end);
5227   }
5228   push @where, @{$opt{'where'}} if $opt{'where'};
5229   my $where = scalar(@where) ? 'WHERE '. join(' AND ', @where ) : '';
5230
5231   $where;
5232
5233 }
5234
5235 #for dyanmic FS::$table->search in httemplate/misc/email_customers.html
5236 use FS::cust_main::Search;
5237 sub search {
5238   my $class = shift;
5239   FS::cust_main::Search->search(@_);
5240 }
5241
5242 =back
5243
5244 =head1 SUBROUTINES
5245
5246 =over 4
5247
5248 =item notify CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5249
5250 Deprecated.  Use event notification and message templates 
5251 (L<FS::msg_template>) instead.
5252
5253 Sends a templated email notification to the customer (see L<Text::Template>).
5254
5255 OPTIONS is a hash and may include
5256
5257 I<from> - the email sender (default is invoice_from)
5258
5259 I<to> - comma-separated scalar or arrayref of recipients 
5260    (default is invoicing_list)
5261
5262 I<subject> - The subject line of the sent email notification
5263    (default is "Notice from company_name")
5264
5265 I<extra_fields> - a hashref of name/value pairs which will be substituted
5266    into the template
5267
5268 The following variables are vavailable in the template.
5269
5270 I<$first> - the customer first name
5271 I<$last> - the customer last name
5272 I<$company> - the customer company
5273 I<$payby> - a description of the method of payment for the customer
5274             # would be nice to use FS::payby::shortname
5275 I<$payinfo> - the account information used to collect for this customer
5276 I<$expdate> - the expiration of the customer payment in seconds from epoch
5277
5278 =cut
5279
5280 sub notify {
5281   my ($self, $template, %options) = @_;
5282
5283   return unless $conf->exists($template);
5284
5285   my $from = $conf->invoice_from_full($self->agentnum)
5286     if $conf->exists('invoice_from', $self->agentnum);
5287   $from = $options{from} if exists($options{from});
5288
5289   my $to = join(',', $self->invoicing_list_emailonly);
5290   $to = $options{to} if exists($options{to});
5291   
5292   my $subject = "Notice from " . $conf->config('company_name', $self->agentnum)
5293     if $conf->exists('company_name', $self->agentnum);
5294   $subject = $options{subject} if exists($options{subject});
5295
5296   my $notify_template = new Text::Template (TYPE => 'ARRAY',
5297                                             SOURCE => [ map "$_\n",
5298                                               $conf->config($template)]
5299                                            )
5300     or die "can't create new Text::Template object: Text::Template::ERROR";
5301   $notify_template->compile()
5302     or die "can't compile template: Text::Template::ERROR";
5303
5304   $FS::notify_template::_template::company_name =
5305     $conf->config('company_name', $self->agentnum);
5306   $FS::notify_template::_template::company_address =
5307     join("\n", $conf->config('company_address', $self->agentnum) ). "\n";
5308
5309   my $paydate = $self->paydate || '2037-12-31';
5310   $FS::notify_template::_template::first = $self->first;
5311   $FS::notify_template::_template::last = $self->last;
5312   $FS::notify_template::_template::company = $self->company;
5313   $FS::notify_template::_template::payinfo = $self->mask_payinfo;
5314   my $payby = $self->payby;
5315   my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5316   my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5317
5318   #credit cards expire at the end of the month/year of their exp date
5319   if ($payby eq 'CARD' || $payby eq 'DCRD') {
5320     $FS::notify_template::_template::payby = 'credit card';
5321     ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5322     $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5323     $expire_time--;
5324   }elsif ($payby eq 'COMP') {
5325     $FS::notify_template::_template::payby = 'complimentary account';
5326   }else{
5327     $FS::notify_template::_template::payby = 'current method';
5328   }
5329   $FS::notify_template::_template::expdate = $expire_time;
5330
5331   for (keys %{$options{extra_fields}}){
5332     no strict "refs";
5333     ${"FS::notify_template::_template::$_"} = $options{extra_fields}->{$_};
5334   }
5335
5336   send_email(from => $from,
5337              to => $to,
5338              subject => $subject,
5339              body => $notify_template->fill_in( PACKAGE =>
5340                                                 'FS::notify_template::_template'                                              ),
5341             );
5342
5343 }
5344
5345 =item generate_letter CUSTOMER_OBJECT TEMPLATE_NAME OPTIONS
5346
5347 Generates a templated notification to the customer (see L<Text::Template>).
5348
5349 OPTIONS is a hash and may include
5350
5351 I<extra_fields> - a hashref of name/value pairs which will be substituted
5352    into the template.  These values may override values mentioned below
5353    and those from the customer record.
5354
5355 I<template_text> - if present, ignores TEMPLATE_NAME and uses the provided text
5356
5357 The following variables are available in the template instead of or in addition
5358 to the fields of the customer record.
5359
5360 I<$payby> - a description of the method of payment for the customer
5361             # would be nice to use FS::payby::shortname
5362 I<$payinfo> - the masked account information used to collect for this customer
5363 I<$expdate> - the expiration of the customer payment method in seconds from epoch
5364 I<$returnaddress> - the return address defaults to invoice_latexreturnaddress or company_address
5365
5366 =cut
5367
5368 # a lot like cust_bill::print_latex
5369 sub generate_letter {
5370   my ($self, $template, %options) = @_;
5371
5372   warn "Template $template does not exist" && return
5373     unless $conf->exists($template) || $options{'template_text'};
5374
5375   my $template_source = $options{'template_text'} 
5376                         ? [ $options{'template_text'} ] 
5377                         : [ map "$_\n", $conf->config($template) ];
5378
5379   my $letter_template = new Text::Template
5380                         ( TYPE       => 'ARRAY',
5381                           SOURCE     => $template_source,
5382                           DELIMITERS => [ '[@--', '--@]' ],
5383                         )
5384     or die "can't create new Text::Template object: Text::Template::ERROR";
5385
5386   $letter_template->compile()
5387     or die "can't compile template: Text::Template::ERROR";
5388
5389   my %letter_data = map { $_ => $self->$_ } $self->fields;
5390   $letter_data{payinfo} = $self->mask_payinfo;
5391
5392   #my $paydate = $self->paydate || '2037-12-31';
5393   my $paydate = $self->paydate =~ /^\S+$/ ? $self->paydate : '2037-12-31';
5394
5395   my $payby = $self->payby;
5396   my ($payyear,$paymonth,$payday) = split (/-/,$paydate);
5397   my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear);
5398
5399   #credit cards expire at the end of the month/year of their exp date
5400   if ($payby eq 'CARD' || $payby eq 'DCRD') {
5401     $letter_data{payby} = 'credit card';
5402     ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++);
5403     $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear);
5404     $expire_time--;
5405   }elsif ($payby eq 'COMP') {
5406     $letter_data{payby} = 'complimentary account';
5407   }else{
5408     $letter_data{payby} = 'current method';
5409   }
5410   $letter_data{expdate} = $expire_time;
5411
5412   for (keys %{$options{extra_fields}}){
5413     $letter_data{$_} = $options{extra_fields}->{$_};
5414   }
5415
5416   unless(exists($letter_data{returnaddress})){
5417     my $retadd = join("\n", $conf->config_orbase( 'invoice_latexreturnaddress',
5418                                                   $self->agent_template)
5419                      );
5420     if ( length($retadd) ) {
5421       $letter_data{returnaddress} = $retadd;
5422     } elsif ( grep /\S/, $conf->config('company_address', $self->agentnum) ) {
5423       $letter_data{returnaddress} =
5424         join( "\n", map { s/( {2,})/'~' x length($1)/eg;
5425                           s/$/\\\\\*/;
5426                           $_;
5427                         }
5428                     ( $conf->config('company_name', $self->agentnum),
5429                       $conf->config('company_address', $self->agentnum),
5430                     )
5431         );
5432     } else {
5433       $letter_data{returnaddress} = '~';
5434     }
5435   }
5436
5437   $letter_data{conf_dir} = "$FS::UID::conf_dir/conf.$FS::UID::datasrc";
5438
5439   $letter_data{company_name} = $conf->config('company_name', $self->agentnum);
5440
5441   my $dir = $FS::UID::conf_dir."/cache.". $FS::UID::datasrc;
5442
5443   my $lh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
5444                            DIR      => $dir,
5445                            SUFFIX   => '.eps',
5446                            UNLINK   => 0,
5447                          ) or die "can't open temp file: $!\n";
5448   print $lh $conf->config_binary('logo.eps', $self->agentnum)
5449     or die "can't write temp file: $!\n";
5450   close $lh;
5451   $letter_data{'logo_file'} = $lh->filename;
5452
5453   my $fh = new File::Temp( TEMPLATE => 'letter.'. $self->custnum. '.XXXXXXXX',
5454                            DIR      => $dir,
5455                            SUFFIX   => '.tex',
5456                            UNLINK   => 0,
5457                          ) or die "can't open temp file: $!\n";
5458
5459   $letter_template->fill_in( OUTPUT => $fh, HASH => \%letter_data );
5460   close $fh;
5461   $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
5462   return ($1, $letter_data{'logo_file'});
5463
5464 }
5465
5466 =item print_ps TEMPLATE 
5467
5468 Returns an postscript letter filled in from TEMPLATE, as a scalar.
5469
5470 =cut
5471
5472 sub print_ps {
5473   my $self = shift;
5474   my($file, $lfile) = $self->generate_letter(@_);
5475   my $ps = FS::Misc::generate_ps($file);
5476   unlink($file.'.tex');
5477   unlink($lfile);
5478
5479   $ps;
5480 }
5481
5482 =item print TEMPLATE
5483
5484 Prints the filled in template.
5485
5486 TEMPLATE is the name of a L<Text::Template> to fill in and print.
5487
5488 =cut
5489
5490 sub queueable_print {
5491   my %opt = @_;
5492
5493   my $self = qsearchs('cust_main', { 'custnum' => $opt{custnum} } )
5494     or die "invalid customer number: " . $opt{custnum};
5495
5496   my $error = $self->print( { 'template' => $opt{template} } );
5497   die $error if $error;
5498 }
5499
5500 sub print {
5501   my ($self, $template) = (shift, shift);
5502   do_print(
5503     [ $self->print_ps($template) ],
5504     'agentnum' => $self->agentnum,
5505   );
5506 }
5507
5508 #these three subs should just go away once agent stuff is all config overrides
5509
5510 sub agent_template {
5511   my $self = shift;
5512   $self->_agent_plandata('agent_templatename');
5513 }
5514
5515 sub agent_invoice_from {
5516   my $self = shift;
5517   $self->_agent_plandata('agent_invoice_from');
5518 }
5519
5520 sub _agent_plandata {
5521   my( $self, $option ) = @_;
5522
5523   #yuck.  this whole thing needs to be reconciled better with 1.9's idea of
5524   #agent-specific Conf
5525
5526   use FS::part_event::Condition;
5527   
5528   my $agentnum = $self->agentnum;
5529
5530   my $regexp = regexp_sql();
5531
5532   my $part_event_option =
5533     qsearchs({
5534       'select'    => 'part_event_option.*',
5535       'table'     => 'part_event_option',
5536       'addl_from' => q{
5537         LEFT JOIN part_event USING ( eventpart )
5538         LEFT JOIN part_event_option AS peo_agentnum
5539           ON ( part_event.eventpart = peo_agentnum.eventpart
5540                AND peo_agentnum.optionname = 'agentnum'
5541                AND peo_agentnum.optionvalue }. $regexp. q{ '(^|,)}. $agentnum. q{(,|$)'
5542              )
5543         LEFT JOIN part_event_condition
5544           ON ( part_event.eventpart = part_event_condition.eventpart
5545                AND part_event_condition.conditionname = 'cust_bill_age'
5546              )
5547         LEFT JOIN part_event_condition_option
5548           ON ( part_event_condition.eventconditionnum = part_event_condition_option.eventconditionnum
5549                AND part_event_condition_option.optionname = 'age'
5550              )
5551       },
5552       #'hashref'   => { 'optionname' => $option },
5553       #'hashref'   => { 'part_event_option.optionname' => $option },
5554       'extra_sql' =>
5555         " WHERE part_event_option.optionname = ". dbh->quote($option).
5556         " AND action = 'cust_bill_send_agent' ".
5557         " AND ( disabled IS NULL OR disabled != 'Y' ) ".
5558         " AND peo_agentnum.optionname = 'agentnum' ".
5559         " AND ( agentnum IS NULL OR agentnum = $agentnum ) ".
5560         " ORDER BY
5561            CASE WHEN part_event_condition_option.optionname IS NULL
5562            THEN -1
5563            ELSE ". FS::part_event::Condition->age2seconds_sql('part_event_condition_option.optionvalue').
5564         " END
5565           , part_event.weight".
5566         " LIMIT 1"
5567     });
5568     
5569   unless ( $part_event_option ) {
5570     return $self->agent->invoice_template || ''
5571       if $option eq 'agent_templatename';
5572     return '';
5573   }
5574
5575   $part_event_option->optionvalue;
5576
5577 }
5578
5579 =item queued_bill 'custnum' => CUSTNUM [ , OPTION => VALUE ... ]
5580
5581 Subroutine (not a method), designed to be called from the queue.
5582
5583 Takes a list of options and values.
5584
5585 Pulls up the customer record via the custnum option and calls bill_and_collect.
5586
5587 =cut
5588
5589 sub queued_bill {
5590   my (%args) = @_; #, ($time, $invoice_time, $check_freq, $resetup) = @_;
5591
5592   my $cust_main = qsearchs( 'cust_main', { custnum => $args{'custnum'} } );
5593   warn 'bill_and_collect custnum#'. $cust_main->custnum. "\n";#log custnum w/pid
5594
5595   #without this errors don't get rolled back
5596   $args{'fatal'} = 1; # runs from job queue, will be caught
5597
5598   $cust_main->bill_and_collect( %args );
5599 }
5600
5601 sub process_bill_and_collect {
5602   my $job = shift;
5603   my $param = thaw(decode_base64(shift));
5604   my $cust_main = qsearchs( 'cust_main', { custnum => $param->{'custnum'} } )
5605       or die "custnum '$param->{custnum}' not found!\n";
5606   $param->{'job'}   = $job;
5607   $param->{'fatal'} = 1; # runs from job queue, will be caught
5608   $param->{'retry'} = 1;
5609
5610   $cust_main->bill_and_collect( %$param );
5611 }
5612
5613 #starting to take quite a while for big dbs
5614 #   (JRNL: journaled so it only happens once per database)
5615 # - seq scan of h_cust_main (yuck), but not going to index paycvv, so
5616 # JRNL seq scan of cust_main on signupdate... index signupdate?  will that help?
5617 # JRNL seq scan of cust_main on paydate... index on substrings?  maybe set an
5618 # JRNL seq scan of cust_main on payinfo.. certainly not going toi ndex that...
5619 # JRNL leading/trailing spaces in first, last, company
5620 # - otaker upgrade?  journal and call it good?  (double check to make sure
5621 #    we're not still setting otaker here)
5622 #
5623 #only going to get worse with new location stuff...
5624
5625 sub _upgrade_data { #class method
5626   my ($class, %opts) = @_;
5627
5628   my @statements = (
5629     'UPDATE h_cust_main SET paycvv = NULL WHERE paycvv IS NOT NULL',
5630   );
5631
5632   #this seems to be the only expensive one.. why does it take so long?
5633   unless ( FS::upgrade_journal->is_done('cust_main__signupdate') ) {
5634     push @statements,
5635       'UPDATE cust_main SET signupdate = (SELECT signupdate FROM h_cust_main WHERE signupdate IS NOT NULL AND h_cust_main.custnum = cust_main.custnum ORDER BY historynum DESC LIMIT 1) WHERE signupdate IS NULL';
5636     FS::upgrade_journal->set_done('cust_main__signupdate');
5637   }
5638
5639   unless ( FS::upgrade_journal->is_done('cust_main__paydate') ) {
5640
5641     # fix yyyy-m-dd formatted paydates
5642     if ( driver_name =~ /^mysql/i ) {
5643       push @statements,
5644       "UPDATE cust_main SET paydate = CONCAT( SUBSTRING(paydate FROM 1 FOR 5), '0', SUBSTRING(paydate FROM 6) ) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
5645     } else { # the SQL standard
5646       push @statements, 
5647       "UPDATE cust_main SET paydate = SUBSTRING(paydate FROM 1 FOR 5) || '0' || SUBSTRING(paydate FROM 6) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
5648     }
5649     FS::upgrade_journal->set_done('cust_main__paydate');
5650   }
5651
5652   unless ( FS::upgrade_journal->is_done('cust_main__payinfo') ) {
5653
5654     push @statements, #fix the weird BILL with a cc# in payinfo problem
5655       #DCRD to be safe
5656       "UPDATE cust_main SET payby = 'DCRD' WHERE payby = 'BILL' and length(payinfo) = 16 and payinfo ". regexp_sql. q( '^[0-9]*$' );
5657
5658     FS::upgrade_journal->set_done('cust_main__payinfo');
5659     
5660   }
5661
5662   my $t = time;
5663   foreach my $sql ( @statements ) {
5664     my $sth = dbh->prepare($sql) or die dbh->errstr;
5665     $sth->execute or die $sth->errstr;
5666     #warn ( (time - $t). " seconds\n" );
5667     #$t = time;
5668   }
5669
5670   local($ignore_expired_card) = 1;
5671   local($ignore_banned_card) = 1;
5672   local($skip_fuzzyfiles) = 1;
5673   local($import) = 1; #prevent automatic geocoding (need its own variable?)
5674
5675   FS::cust_main::Location->_upgrade_data(%opts);
5676
5677   unless ( FS::upgrade_journal->is_done('cust_main__trimspaces') ) {
5678
5679     foreach my $cust_main ( qsearch({
5680       'table'     => 'cust_main', 
5681       'hashref'   => {},
5682       'extra_sql' => 'WHERE '.
5683                        join(' OR ',
5684                          map "$_ LIKE ' %' OR $_ LIKE '% ' OR $_ LIKE '%  %'",
5685                            qw( first last company )
5686                        ),
5687     }) ) {
5688       my $error = $cust_main->replace;
5689       die $error if $error;
5690     }
5691
5692     FS::upgrade_journal->set_done('cust_main__trimspaces');
5693
5694   }
5695
5696   $class->_upgrade_otaker(%opts);
5697
5698   # turn on encryption as part of regular upgrade, so all new records are immediately encrypted
5699   # existing records will be encrypted in queueable_upgrade (below)
5700   unless ($conf->exists('encryptionpublickey') || $conf->exists('encryptionprivatekey')) {
5701     eval "use FS::Setup";
5702     die $@ if $@;
5703     FS::Setup::enable_encryption();
5704   }
5705
5706   $class->_upgrade_data_paydate_edgebug;
5707 }
5708
5709 =item _upgrade_data_paydate_edgebug
5710
5711 Correct bad data injected into payment expire date column by Edge browser bug
5712
5713 The month and year values may have an extra character injected into form POST
5714 data by Edge browser.  It was possible for some bad month values to slip
5715 past data validation.
5716
5717 If the stored value was out of range, it was causing payments screen to crash.
5718 We can detect and fix this by dropping the second digit.
5719
5720 If the stored value is is 11 or 12, it's possible the user inputted a 1.  In
5721 this case, the payment method will fail to authorize, but the record will
5722 not cause crashdumps for being out of range.
5723
5724 In short, check for any expiration month > 12, and drop the extra digit
5725
5726 =cut
5727
5728 sub _upgrade_data_paydate_edgebug {
5729   my $journal_label = 'cust_main_paydate_edgebug';
5730   return if FS::upgrade_journal->is_done( $journal_label );
5731
5732   my $oldAutoCommit = $FS::UID::AutoCommit;
5733   local $FS::UID::AutoCommit = 0;
5734
5735   for my $row (
5736     FS::Record::qsearch(
5737       cust_main => { paydate => { op => '!=', value => '' }}
5738     )
5739   ) {
5740     next unless $row->ut_daten('paydate');
5741
5742     # paydate column stored in database has failed date validation
5743     my $bad_paydate = $row->paydate;
5744
5745     my @date = split /[\-\/]/, $bad_paydate;
5746     @date = @date[2,0,1] if $date[2] > 1900;
5747
5748     # Only autocorrecting when month > 12 - notify operator
5749     unless ( $date[1] > 12 ) {
5750       die sprintf(
5751         'Unable to correct bad paydate stored in cust_main row '.
5752         'custnum(%s) paydate(%s)',
5753         $row->custnum,
5754         $bad_paydate,
5755       );
5756     }
5757
5758     $date[1] = substr( $date[1], 0, 1 );
5759     $row->paydate( join('-', @date ));
5760
5761     if ( my $error = $row->replace ) {
5762       die sprintf(
5763         'Failed to autocorrect bad paydate stored in cust_main row '.
5764         'custnum(%s) paydate(%s) - error: %s',
5765         $row->custnum,
5766         $bad_paydate,
5767         $error
5768       );
5769     }
5770
5771     warn sprintf(
5772       'Autocorrected bad paydate stored in cust_main row '.
5773       "custnum(%s) old-paydate(%s) new-paydate(%s)\n",
5774       $row->custnum,
5775       $bad_paydate,
5776       $row->paydate,
5777     );
5778
5779   }
5780
5781   FS::upgrade_journal->set_done( $journal_label );
5782   dbh->commit unless $oldAutoCommit;
5783 }
5784
5785
5786 sub queueable_upgrade {
5787   my $class = shift;
5788
5789   ### encryption gets turned on in _upgrade_data, above
5790
5791   eval "use FS::upgrade_journal";
5792   die $@ if $@;
5793
5794   # prior to 2013 (commit f16665c9) payinfo was stored in history if not encrypted,
5795   # clear that out before encrypting/tokenizing anything else
5796   if (!FS::upgrade_journal->is_done('clear_payinfo_history')) {
5797     foreach my $table ('cust_main','cust_pay_pending','cust_pay','cust_pay_void','cust_refund') {
5798       my $sql = 'UPDATE h_'.$table.' SET payinfo = NULL WHERE payinfo IS NOT NULL';
5799       my $sth = dbh->prepare($sql) or die dbh->errstr;
5800       $sth->execute or die $sth->errstr;
5801     }
5802     FS::upgrade_journal->set_done('clear_payinfo_history');
5803   }
5804
5805   # fix Tokenized paycardtype and encrypt old records
5806   if (    ! FS::upgrade_journal->is_done('paycardtype_Tokenized')
5807        || ! FS::upgrade_journal->is_done('encryption_check')
5808      )
5809   {
5810
5811     # allow replacement of closed cust_pay/cust_refund records
5812     local $FS::payinfo_Mixin::allow_closed_replace = 1;
5813
5814     # because it looks like nothing's changing
5815     local $FS::Record::no_update_diff = 1;
5816
5817     # commit everything immediately
5818     local $FS::UID::AutoCommit = 1;
5819
5820     # encrypt what's there
5821     foreach my $table ('cust_main','cust_pay_pending','cust_pay','cust_pay_void','cust_refund') {
5822       my $tclass = 'FS::'.$table;
5823       my $lastrecnum = 0;
5824       my @recnums = ();
5825       while (my $recnum = _upgrade_next_recnum(dbh,$table,\$lastrecnum,\@recnums)) {
5826         my $record = $tclass->by_key($recnum);
5827         next unless $record; # small chance it's been deleted, that's ok
5828         next unless grep { $record->payby eq $_ } @FS::Record::encrypt_payby;
5829         # window for possible conflict is practically nonexistant,
5830         #   but just in case...
5831         $record = $record->select_for_update;
5832         if (!$record->custnum && $table eq 'cust_pay_pending') {
5833           $record->set('custnum_pending',1);
5834         }
5835         $record->paycardtype('') if $record->paycardtype eq 'Tokenized';
5836
5837         local($ignore_expired_card) = 1;
5838         local($ignore_banned_card) = 1;
5839         local($skip_fuzzyfiles) = 1;
5840         local($import) = 1;#prevent automatic geocoding (need its own variable?)
5841
5842         my $error = $record->replace;
5843         die "Error replacing $table ".$record->get($record->primary_key).": $error" if $error;
5844       }
5845     }
5846
5847     FS::upgrade_journal->set_done('paycardtype_Tokenized');
5848     FS::upgrade_journal->set_done('encryption_check') if $conf->exists('encryption');
5849   }
5850
5851 }
5852
5853 # not entirely false laziness w/ Billing_Realtime::_token_check_next_recnum
5854 # cust_payby might get deleted while this runs
5855 # not a method!
5856 sub _upgrade_next_recnum {
5857   my ($dbh,$table,$lastrecnum,$recnums) = @_;
5858   my $recnum = shift @$recnums;
5859   return $recnum if $recnum;
5860   my $tclass = 'FS::'.$table;
5861   my $paycardtypecheck = ($table ne 'cust_pay_pending') ? q( OR paycardtype = 'Tokenized') : '';
5862   my $sql = 'SELECT '.$tclass->primary_key.
5863             ' FROM '.$table.
5864             ' WHERE '.$tclass->primary_key.' > '.$$lastrecnum.
5865             "   AND payby IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
5866             "   AND ( length(payinfo) < 80$paycardtypecheck ) ".
5867             ' ORDER BY '.$tclass->primary_key.' LIMIT 500';
5868   my $sth = $dbh->prepare($sql) or die $dbh->errstr;
5869   $sth->execute() or die $sth->errstr;
5870   my @recnums;
5871   while (my $rec = $sth->fetchrow_hashref) {
5872     push @$recnums, $rec->{$tclass->primary_key};
5873   }
5874   $sth->finish();
5875   $$lastrecnum = $$recnums[-1];
5876   return shift @$recnums;
5877 }
5878
5879 =back
5880
5881 =head1 BUGS
5882
5883 The delete method.
5884
5885 The delete method should possibly take an FS::cust_main object reference
5886 instead of a scalar customer number.
5887
5888 Bill and collect options should probably be passed as references instead of a
5889 list.
5890
5891 There should probably be a configuration file with a list of allowed credit
5892 card types.
5893
5894 No multiple currency support (probably a larger project than just this module).
5895
5896 payinfo_masked false laziness with cust_pay.pm and cust_refund.pm
5897
5898 Birthdates rely on negative epoch values.
5899
5900 The payby for card/check batches is broken.  With mixed batching, bad
5901 things will happen.
5902
5903 B<collect> I<invoice_time> should be renamed I<time>, like B<bill>.
5904
5905 =head1 SEE ALSO
5906
5907 L<FS::Record>, L<FS::cust_pkg>, L<FS::cust_bill>, L<FS::cust_credit>
5908 L<FS::agent>, L<FS::part_referral>, L<FS::cust_main_county>,
5909 L<FS::cust_main_invoice>, L<FS::UID>, schema.html from the base documentation.
5910
5911 =cut
5912
5913 1;
5914