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