444f68ada3b9828027bfdb860c5cb8a1a256c818
[freeside.git] / FS / FS / cust_pay.pm
1 package FS::cust_pay;
2
3 use strict;
4 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
5              FS::Record );
6 use vars qw( $DEBUG $me $conf @encrypted_fields
7              $unsuspendauto $ignore_noapply 
8            );
9 use Date::Format;
10 use Business::CreditCard;
11 use Text::Template;
12 use FS::UID qw( getotaker );
13 use FS::Misc qw( send_email );
14 use FS::Record qw( dbh qsearch qsearchs );
15 use FS::CurrentUser;
16 use FS::payby;
17 use FS::cust_main_Mixin;
18 use FS::payinfo_transaction_Mixin;
19 use FS::cust_bill;
20 use FS::cust_bill_pay;
21 use FS::cust_pay_refund;
22 use FS::cust_main;
23 use FS::cust_pkg;
24 use FS::cust_pay_void;
25 use FS::upgrade_journal;
26
27 $DEBUG = 0;
28
29 $me = '[FS::cust_pay]';
30
31 $ignore_noapply = 0;
32
33 #ask FS::UID to run this stuff for us later
34 FS::UID->install_callback( sub { 
35   $conf = new FS::Conf;
36   $unsuspendauto = $conf->exists('unsuspendauto');
37 } );
38
39 @encrypted_fields = ('payinfo');
40
41 =head1 NAME
42
43 FS::cust_pay - Object methods for cust_pay objects
44
45 =head1 SYNOPSIS
46
47   use FS::cust_pay;
48
49   $record = new FS::cust_pay \%hash;
50   $record = new FS::cust_pay { 'column' => 'value' };
51
52   $error = $record->insert;
53
54   $error = $new_record->replace($old_record);
55
56   $error = $record->delete;
57
58   $error = $record->check;
59
60 =head1 DESCRIPTION
61
62 An FS::cust_pay object represents a payment; the transfer of money from a
63 customer.  FS::cust_pay inherits from FS::Record.  The following fields are
64 currently supported:
65
66 =over 4
67
68 =item paynum
69
70 primary key (assigned automatically for new payments)
71
72 =item custnum
73
74 customer (see L<FS::cust_main>)
75
76 =item _date
77
78 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
79 L<Time::Local> and L<Date::Parse> for conversion functions.
80
81 =item paid
82
83 Amount of this payment
84
85 =item usernum
86
87 order taker (see L<FS::access_user>)
88
89 =item payby
90
91 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
92
93 =item payinfo
94
95 Payment Information (See L<FS::payinfo_Mixin> for data format)
96
97 =item paymask
98
99 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
100
101 =item paybatch
102
103 text field for tracking card processing or other batch grouping
104
105 =item payunique
106
107 Optional unique identifer to prevent duplicate transactions.
108
109 =item closed
110
111 books closed flag, empty or `Y'
112
113 =item pkgnum
114
115 Desired pkgnum when using experimental package balances.
116
117 =item bank
118
119 The bank where the payment was deposited.
120
121 =item depositor
122
123 The name of the depositor.
124
125 =item account
126
127 The deposit account number.
128
129 =item teller
130
131 The teller number.
132
133 =back
134
135 =head1 METHODS
136
137 =over 4 
138
139 =item new HASHREF
140
141 Creates a new payment.  To add the payment to the databse, see L<"insert">.
142
143 =cut
144
145 sub table { 'cust_pay'; }
146 sub cust_linked { $_[0]->cust_main_custnum; } 
147 sub cust_unlinked_msg {
148   my $self = shift;
149   "WARNING: can't find cust_main.custnum ". $self->custnum.
150   ' (cust_pay.paynum '. $self->paynum. ')';
151 }
152
153 =item insert [ OPTION => VALUE ... ]
154
155 Adds this payment to the database.
156
157 For backwards-compatibility and convenience, if the additional field invnum
158 is defined, an FS::cust_bill_pay record for the full amount of the payment
159 will be created.  In this case, custnum is optional.
160
161 If the additional field discount_term is defined then a prepayment discount
162 is taken for that length of time.  It is an error for the customer to owe
163 after this payment is made.
164
165 A hash of optional arguments may be passed.  Currently "manual" is supported.
166 If true, a payment receipt is sent instead of a statement when
167 'payment_receipt_email' configuration option is set.
168
169 =cut
170
171 sub insert {
172   my($self, %options) = @_;
173
174   local $SIG{HUP} = 'IGNORE';
175   local $SIG{INT} = 'IGNORE';
176   local $SIG{QUIT} = 'IGNORE';
177   local $SIG{TERM} = 'IGNORE';
178   local $SIG{TSTP} = 'IGNORE';
179   local $SIG{PIPE} = 'IGNORE';
180
181   my $oldAutoCommit = $FS::UID::AutoCommit;
182   local $FS::UID::AutoCommit = 0;
183   my $dbh = dbh;
184
185   my $cust_bill;
186   if ( $self->invnum ) {
187     $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
188       or do {
189         $dbh->rollback if $oldAutoCommit;
190         return "Unknown cust_bill.invnum: ". $self->invnum;
191       };
192     $self->custnum($cust_bill->custnum );
193   }
194
195   my $error = $self->check;
196   return $error if $error;
197
198   my $cust_main = $self->cust_main;
199   my $old_balance = $cust_main->balance;
200
201   $error = $self->SUPER::insert;
202   if ( $error ) {
203     $dbh->rollback if $oldAutoCommit;
204     return "error inserting cust_pay: $error";
205   }
206
207   if ( my $credit_type = $conf->config('prepayment_discounts-credit_type') ) {
208     if ( my $months = $self->discount_term ) {
209       # XXX this should be moved out somewhere, but discount_term_values
210       # doesn't fit right
211       my ($cust_bill) = ($cust_main->cust_bill)[-1]; # most recent invoice
212       return "can't accept prepayment for an unbilled customer" if !$cust_bill;
213
214       # %billing_pkgs contains this customer's active monthly packages. 
215       # Recurring fees for those packages will be credited and then rebilled 
216       # for the full discount term.  Other packages on the last invoice 
217       # (canceled, non-monthly recurring, or one-time charges) will be 
218       # left as they are.
219       my %billing_pkgs = map { $_->pkgnum => $_ } 
220                          grep { $_->part_pkg->freq eq '1' } 
221                          $cust_main->billing_pkgs;
222       my $credit = 0; # sum of recurring charges from that invoice
223       my $last_bill_date = 0; # the real bill date
224       foreach my $item ( $cust_bill->cust_bill_pkg ) {
225         next if !exists($billing_pkgs{$item->pkgnum}); # skip inactive packages
226         $credit += $item->recur;
227         $last_bill_date = $item->cust_pkg->last_bill 
228           if defined($item->cust_pkg) 
229             and $item->cust_pkg->last_bill > $last_bill_date
230       }
231
232       my $cust_credit = new FS::cust_credit {
233         'custnum' => $self->custnum,
234         'amount'  => sprintf('%.2f', $credit),
235         'reason'  => 'customer chose to prepay for discount',
236       };
237       $error = $cust_credit->insert('reason_type' => $credit_type);
238       if ( $error ) {
239         $dbh->rollback if $oldAutoCommit;
240         return "error inserting prepayment credit: $error";
241       }
242       # don't apply it yet
243
244       # bill for the entire term
245       $_->bill($_->last_bill) foreach (values %billing_pkgs);
246       $error = $cust_main->bill(
247         # no recurring_only, we want unbilled packages with start dates to 
248         # get billed
249         'no_usage_reset' => 1,
250         'time'           => $last_bill_date, # not $cust_bill->_date
251         'pkg_list'       => [ values %billing_pkgs ],
252         'freq_override'  => $months,
253       );
254       if ( $error ) {
255         $dbh->rollback if $oldAutoCommit;
256         return "error inserting cust_pay: $error";
257       }
258       $error = $cust_main->apply_payments_and_credits;
259       if ( $error ) {
260         $dbh->rollback if $oldAutoCommit;
261         return "error inserting cust_pay: $error";
262       }
263       my $new_balance = $cust_main->balance;
264       if ($new_balance > 0) {
265         $dbh->rollback if $oldAutoCommit;
266         return "balance after prepay discount attempt: $new_balance";
267       }
268       # user friendly: override the "apply only to this invoice" mode
269       $self->invnum('');
270       
271     }
272
273   }
274
275   if ( $self->invnum ) {
276     my $cust_bill_pay = new FS::cust_bill_pay {
277       'invnum' => $self->invnum,
278       'paynum' => $self->paynum,
279       'amount' => $self->paid,
280       '_date'  => $self->_date,
281     };
282     $error = $cust_bill_pay->insert(%options);
283     if ( $error ) {
284       if ( $ignore_noapply ) {
285         warn "warning: error inserting cust_bill_pay: $error ".
286              "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
287       } else {
288         $dbh->rollback if $oldAutoCommit;
289         return "error inserting cust_bill_pay: $error";
290       }
291     }
292   }
293
294   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
295
296   #false laziness w/ cust_credit::insert
297   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
298     my @errors = $cust_main->unsuspend;
299     #return 
300     # side-fx with nested transactions?  upstack rolls back?
301     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
302          join(' / ', @errors)
303       if @errors;
304   }
305   #eslaf
306
307   #bill setup fees for voip_cdr bill_every_call packages
308   #some false laziness w/search in freeside-cdrd
309   my $addl_from =
310     'LEFT JOIN part_pkg USING ( pkgpart ) '.
311     "LEFT JOIN part_pkg_option
312        ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
313             AND part_pkg_option.optionname = 'bill_every_call' )";
314
315   my $extra_sql = " AND plan = 'voip_cdr' AND optionvalue = '1' ".
316                   " AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 ) ";
317
318   my @cust_pkg = qsearch({
319     'table'     => 'cust_pkg',
320     'addl_from' => $addl_from,
321     'hashref'   => { 'custnum' => $self->custnum,
322                      'susp'    => '',
323                      'cancel'  => '',
324                    },
325     'extra_sql' => $extra_sql,
326   });
327
328   if ( @cust_pkg ) {
329     warn "voip_cdr bill_every_call packages found; billing customer\n";
330     my $bill_error = $self->cust_main->bill_and_collect( 'fatal' => 'return' );
331     if ( $bill_error ) {
332       warn "WARNING: Error billing customer: $bill_error\n";
333     }
334   }
335   #end of billing setup fees for voip_cdr bill_every_call packages
336
337   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
338
339   #payment receipt
340   my $trigger = $conf->config('payment_receipt-trigger', 
341                               $self->cust_main->agentnum) || 'cust_pay';
342   if ( $trigger eq 'cust_pay' ) {
343     my $error = $self->send_receipt(
344       'manual'    => $options{'manual'},
345       'cust_bill' => $cust_bill,
346       'cust_main' => $cust_main,
347     );
348     warn "can't send payment receipt/statement: $error" if $error;
349   }
350
351   '';
352
353 }
354
355 =item void [ REASON ]
356
357 Voids this payment: deletes the payment and all associated applications and
358 adds a record of the voided payment to the FS::cust_pay_void table.
359
360 =cut
361
362 sub void {
363   my $self = shift;
364
365   local $SIG{HUP} = 'IGNORE';
366   local $SIG{INT} = 'IGNORE';
367   local $SIG{QUIT} = 'IGNORE';
368   local $SIG{TERM} = 'IGNORE';
369   local $SIG{TSTP} = 'IGNORE';
370   local $SIG{PIPE} = 'IGNORE';
371
372   my $oldAutoCommit = $FS::UID::AutoCommit;
373   local $FS::UID::AutoCommit = 0;
374   my $dbh = dbh;
375
376   my $cust_pay_void = new FS::cust_pay_void ( {
377     map { $_ => $self->get($_) } $self->fields
378   } );
379   $cust_pay_void->reason(shift) if scalar(@_);
380   my $error = $cust_pay_void->insert;
381   if ( $error ) {
382     $dbh->rollback if $oldAutoCommit;
383     return $error;
384   }
385
386   $error = $self->delete;
387   if ( $error ) {
388     $dbh->rollback if $oldAutoCommit;
389     return $error;
390   }
391
392   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
393
394   '';
395
396 }
397
398 =item delete
399
400 Unless the closed flag is set, deletes this payment and all associated
401 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>).  In most
402 cases, you want to use the void method instead to leave a record of the
403 deleted payment.
404
405 =cut
406
407 # very similar to FS::cust_credit::delete
408 sub delete {
409   my $self = shift;
410   return "Can't delete closed payment" if $self->closed =~ /^Y/i;
411
412   local $SIG{HUP} = 'IGNORE';
413   local $SIG{INT} = 'IGNORE';
414   local $SIG{QUIT} = 'IGNORE';
415   local $SIG{TERM} = 'IGNORE';
416   local $SIG{TSTP} = 'IGNORE';
417   local $SIG{PIPE} = 'IGNORE';
418
419   my $oldAutoCommit = $FS::UID::AutoCommit;
420   local $FS::UID::AutoCommit = 0;
421   my $dbh = dbh;
422
423   foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
424     my $error = $app->delete;
425     if ( $error ) {
426       $dbh->rollback if $oldAutoCommit;
427       return $error;
428     }
429   }
430
431   my $error = $self->SUPER::delete(@_);
432   if ( $error ) {
433     $dbh->rollback if $oldAutoCommit;
434     return $error;
435   }
436
437   if (    $conf->exists('deletepayments')
438        && $conf->config('deletepayments') ne '' ) {
439
440     my $cust_main = $self->cust_main;
441
442     my $error = send_email(
443       'from'    => $conf->config('invoice_from', $self->cust_main->agentnum),
444                                  #invoice_from??? well as good as any
445       'to'      => $conf->config('deletepayments'),
446       'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
447       'body'    => [
448         "This is an automatic message from your Freeside installation\n",
449         "informing you that the following payment has been deleted:\n",
450         "\n",
451         'paynum: '. $self->paynum. "\n",
452         'custnum: '. $self->custnum.
453           " (". $cust_main->last. ", ". $cust_main->first. ")\n",
454         'paid: $'. sprintf("%.2f", $self->paid). "\n",
455         'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
456         'payby: '. $self->payby. "\n",
457         'payinfo: '. $self->paymask. "\n",
458         'paybatch: '. $self->paybatch. "\n",
459       ],
460     );
461
462     if ( $error ) {
463       $dbh->rollback if $oldAutoCommit;
464       return "can't send payment deletion notification: $error";
465     }
466
467   }
468
469   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
470
471   '';
472
473 }
474
475 =item replace [ OLD_RECORD ]
476
477 You can, but probably shouldn't modify payments...
478
479 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
480 supplied, replaces this record.  If there is an error, returns the error,
481 otherwise returns false.
482
483 =cut
484
485 sub replace {
486   my $self = shift;
487   return "Can't modify closed payment" if $self->closed =~ /^Y/i;
488   $self->SUPER::replace(@_);
489 }
490
491 =item check
492
493 Checks all fields to make sure this is a valid payment.  If there is an error,
494 returns the error, otherwise returns false.  Called by the insert method.
495
496 =cut
497
498 sub check {
499   my $self = shift;
500
501   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
502
503   my $error =
504     $self->ut_numbern('paynum')
505     || $self->ut_numbern('custnum')
506     || $self->ut_numbern('_date')
507     || $self->ut_money('paid')
508     || $self->ut_alphan('otaker')
509     || $self->ut_textn('paybatch')
510     || $self->ut_textn('payunique')
511     || $self->ut_enum('closed', [ '', 'Y' ])
512     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
513     || $self->ut_textn('bank')
514     || $self->ut_alphan('depositor')
515     || $self->ut_numbern('account')
516     || $self->ut_numbern('teller')
517     || $self->payinfo_check()
518   ;
519   return $error if $error;
520
521   return "paid must be > 0 " if $self->paid <= 0;
522
523   return "unknown cust_main.custnum: ". $self->custnum
524     unless $self->invnum
525            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
526
527   $self->_date(time) unless $self->_date;
528
529   return "invalid discount_term"
530    if ($self->discount_term && $self->discount_term < 2);
531
532   if ( $self->payby eq 'CASH' and $conf->exists('require_cash_deposit_info') ) {
533     foreach (qw(bank depositor account teller)) {
534       return "$_ required" if $self->get($_) eq '';
535     }
536   }
537
538 #i guess not now, with cust_pay_pending, if we actually make it here, we _do_ want to record it
539 #  # UNIQUE index should catch this too, without race conditions, but this
540 #  # should give a better error message the other 99.9% of the time...
541 #  if ( length($self->payunique)
542 #       && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) {
543 #    #well, it *could* be a better error message
544 #    return "duplicate transaction".
545 #           " - a payment with unique identifer ". $self->payunique.
546 #           " already exists";
547 #  }
548
549   $self->SUPER::check;
550 }
551
552 =item send_receipt HASHREF | OPTION => VALUE ...
553
554 Sends a payment receipt for this payment..
555
556 Available options:
557
558 =over 4
559
560 =item manual
561
562 Flag indicating the payment is being made manually.
563
564 =item cust_bill
565
566 Invoice (FS::cust_bill) object.  If not specified, the most recent invoice
567 will be assumed.
568
569 =item cust_main
570
571 Customer (FS::cust_main) object (for efficiency).
572
573 =back
574
575 =cut
576
577 sub send_receipt {
578   my $self = shift;
579   my $opt = ref($_[0]) ? shift : { @_ };
580
581   my $cust_bill = $opt->{'cust_bill'};
582   my $cust_main = $opt->{'cust_main'} || $self->cust_main;
583
584   my $conf = new FS::Conf;
585
586   return '' unless $conf->config_bool('payment_receipt', $cust_main->agentnum);
587
588   my @invoicing_list = $cust_main->invoicing_list_emailonly;
589   return '' unless @invoicing_list;
590
591   $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
592
593   my $error = '';
594
595   if (    ( exists($opt->{'manual'}) && $opt->{'manual'} )
596        #|| ! $conf->exists('invoice_html_statement')
597        || ! $cust_bill
598      )
599   {
600     my $msgnum = $conf->config('payment_receipt_msgnum', $cust_main->agentnum);
601     if ( $msgnum ) {
602       my $msg_template = FS::msg_template->by_key($msgnum);
603       $error = $msg_template->send(
604         'cust_main'   => $cust_main,
605         'object'      => $self,
606         'from_config' => 'payment_receipt_from',
607       );
608
609     } elsif ( $conf->exists('payment_receipt_email') ) {
610
611       my $receipt_template = new Text::Template (
612         TYPE   => 'ARRAY',
613         SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
614       ) or do {
615         warn "can't create payment receipt template: $Text::Template::ERROR";
616         return '';
617       };
618
619       my $payby = $self->payby;
620       my $payinfo = $self->payinfo;
621       $payby =~ s/^BILL$/Check/ if $payinfo;
622       if ( $payby eq 'CARD' || $payby eq 'CHEK' ) {
623         $payinfo = $self->paymask
624       } else {
625         $payinfo = $self->decrypt($payinfo);
626       }
627       $payby =~ s/^CHEK$/Electronic check/;
628
629       my %fill_in = (
630         'date'         => time2str("%a %B %o, %Y", $self->_date),
631         'name'         => $cust_main->name,
632         'paynum'       => $self->paynum,
633         'paid'         => sprintf("%.2f", $self->paid),
634         'payby'        => ucfirst(lc($payby)),
635         'payinfo'      => $payinfo,
636         'balance'      => $cust_main->balance,
637         'company_name' => $conf->config('company_name', $cust_main->agentnum),
638       );
639
640       if ( $opt->{'cust_pkg'} ) {
641         $fill_in{'pkg'} = $opt->{'cust_pkg'}->part_pkg->pkg;
642         #setup date, other things?
643       }
644
645       $error = send_email(
646         'from'    => $conf->config('invoice_from', $cust_main->agentnum),
647                                    #invoice_from??? well as good as any
648         'to'      => \@invoicing_list,
649         'subject' => 'Payment receipt',
650         'body'    => [ $receipt_template->fill_in( HASH => \%fill_in ) ],
651       );
652
653     } else {
654
655       warn "payment_receipt is on, but no payment_receipt_msgnum\n";
656
657     }
658
659   } elsif ( ! $cust_main->invoice_noemail ) { #not manual
660
661     my $queue = new FS::queue {
662        'paynum' => $self->paynum,
663        'job'    => 'FS::cust_bill::queueable_email',
664     };
665
666     $error = $queue->insert(
667       'invnum'      => $cust_bill->invnum,
668       'template'    => 'statement',
669       'notice_name' => 'Statement',
670       'no_coupon'   => 1,
671     );
672
673   }
674   
675     warn "send_receipt: $error\n" if $error;
676 }
677
678 =item cust_bill_pay
679
680 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
681 payment.
682
683 =cut
684
685 sub cust_bill_pay {
686   my $self = shift;
687   map { $_ } #return $self->num_cust_bill_pay unless wantarray;
688   sort {    $a->_date  <=> $b->_date
689          || $a->invnum <=> $b->invnum }
690     qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
691   ;
692 }
693
694 =item cust_pay_refund
695
696 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
697 payment.
698
699 =cut
700
701 sub cust_pay_refund {
702   my $self = shift;
703   map { $_ } #return $self->num_cust_pay_refund unless wantarray;
704   sort { $a->_date <=> $b->_date }
705     qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
706   ;
707 }
708
709
710 =item unapplied
711
712 Returns the amount of this payment that is still unapplied; which is
713 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
714 applications (see L<FS::cust_pay_refund>).
715
716 =cut
717
718 sub unapplied {
719   my $self = shift;
720   my $amount = $self->paid;
721   $amount -= $_->amount foreach ( $self->cust_bill_pay );
722   $amount -= $_->amount foreach ( $self->cust_pay_refund );
723   sprintf("%.2f", $amount );
724 }
725
726 =item unrefunded
727
728 Returns the amount of this payment that has not been refuned; which is
729 paid minus all  refund applications (see L<FS::cust_pay_refund>).
730
731 =cut
732
733 sub unrefunded {
734   my $self = shift;
735   my $amount = $self->paid;
736   $amount -= $_->amount foreach ( $self->cust_pay_refund );
737   sprintf("%.2f", $amount );
738 }
739
740 =item amount
741
742 Returns the "paid" field.
743
744 =cut
745
746 sub amount {
747   my $self = shift;
748   $self->paid();
749 }
750
751 =back
752
753 =head1 CLASS METHODS
754
755 =over 4
756
757 =item batch_insert CUST_PAY_OBJECT, ...
758
759 Class method which inserts multiple payments.  Takes a list of FS::cust_pay
760 objects.  Returns a list, each element representing the status of inserting the
761 corresponding payment - empty.  If there is an error inserting any payment, the
762 entire transaction is rolled back, i.e. all payments are inserted or none are.
763
764 For example:
765
766   my @errors = FS::cust_pay->batch_insert(@cust_pay);
767   my $num_errors = scalar(grep $_, @errors);
768   if ( $num_errors == 0 ) {
769     #success; all payments were inserted
770   } else {
771     #failure; no payments were inserted.
772   }
773
774 =cut
775
776 sub batch_insert {
777   my $self = shift; #class method
778
779   local $SIG{HUP} = 'IGNORE';
780   local $SIG{INT} = 'IGNORE';
781   local $SIG{QUIT} = 'IGNORE';
782   local $SIG{TERM} = 'IGNORE';
783   local $SIG{TSTP} = 'IGNORE';
784   local $SIG{PIPE} = 'IGNORE';
785
786   my $oldAutoCommit = $FS::UID::AutoCommit;
787   local $FS::UID::AutoCommit = 0;
788   my $dbh = dbh;
789
790   my $errors = 0;
791   
792   my @errors = map {
793     my $error = $_->insert( 'manual' => 1 );
794     if ( $error ) { 
795       $errors++;
796     } else {
797       $_->cust_main->apply_payments;
798     }
799     $error;
800   } @_;
801
802   if ( $errors ) {
803     $dbh->rollback if $oldAutoCommit;
804   } else {
805     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
806   }
807
808   @errors;
809
810 }
811
812 =item unapplied_sql
813
814 Returns an SQL fragment to retreive the unapplied amount.
815
816 =cut 
817
818 sub unapplied_sql {
819   my ($class, $start, $end) = @_;
820   my $bill_start   = $start ? "AND cust_bill_pay._date <= $start"   : '';
821   my $bill_end     = $end   ? "AND cust_bill_pay._date > $end"     : '';
822   my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
823   my $refund_end   = $end   ? "AND cust_pay_refund._date > $end"   : '';
824
825   "paid
826         - COALESCE( 
827                     ( SELECT SUM(amount) FROM cust_bill_pay
828                         WHERE cust_pay.paynum = cust_bill_pay.paynum
829                         $bill_start $bill_end )
830                     ,0
831                   )
832         - COALESCE(
833                     ( SELECT SUM(amount) FROM cust_pay_refund
834                         WHERE cust_pay.paynum = cust_pay_refund.paynum
835                         $refund_start $refund_end )
836                     ,0
837                   )
838   ";
839
840 }
841
842 # _upgrade_data
843 #
844 # Used by FS::Upgrade to migrate to a new database.
845
846 use FS::h_cust_pay;
847
848 sub _upgrade_data {  #class method
849   my ($class, %opts) = @_;
850
851   warn "$me upgrading $class\n" if $DEBUG;
852
853   ##
854   # otaker/ivan upgrade
855   ##
856
857   unless ( FS::upgrade_journal->is_done('cust_pay__otaker_ivan') ) {
858
859     #not the most efficient, but hey, it only has to run once
860
861     my $where = "WHERE ( otaker IS NULL OR otaker = '' OR otaker = 'ivan' ) ".
862                 "  AND usernum IS NULL ".
863                 "  AND 0 < ( SELECT COUNT(*) FROM cust_main                 ".
864                 "              WHERE cust_main.custnum = cust_pay.custnum ) ";
865
866     my $count_sql = "SELECT COUNT(*) FROM cust_pay $where";
867
868     my $sth = dbh->prepare($count_sql) or die dbh->errstr;
869     $sth->execute or die $sth->errstr;
870     my $total = $sth->fetchrow_arrayref->[0];
871     #warn "$total cust_pay records to update\n"
872     #  if $DEBUG;
873     local($DEBUG) = 2 if $total > 1000; #could be a while, force progress info
874
875     my $count = 0;
876     my $lastprog = 0;
877
878     my @cust_pay = qsearch( {
879         'table'     => 'cust_pay',
880         'hashref'   => {},
881         'extra_sql' => $where,
882         'order_by'  => 'ORDER BY paynum',
883     } );
884
885     foreach my $cust_pay (@cust_pay) {
886
887       my $h_cust_pay = $cust_pay->h_search('insert');
888       if ( $h_cust_pay ) {
889         next if $cust_pay->otaker eq $h_cust_pay->history_user;
890         #$cust_pay->otaker($h_cust_pay->history_user);
891         $cust_pay->set('otaker', $h_cust_pay->history_user);
892       } else {
893         $cust_pay->set('otaker', 'legacy');
894       }
895
896       delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
897       my $error = $cust_pay->replace;
898
899       if ( $error ) {
900         warn " *** WARNING: Error updating order taker for payment paynum ".
901              $cust_pay->paynun. ": $error\n";
902         next;
903       }
904
905       $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
906
907       $count++;
908       if ( $DEBUG > 1 && $lastprog + 30 < time ) {
909         warn "$me $count/$total (".sprintf('%.2f',100*$count/$total). '%)'."\n";
910         $lastprog = time;
911       }
912
913     }
914
915     FS::upgrade_journal->set_done('cust_pay__otaker_ivan');
916   }
917
918   ###
919   # payinfo N/A upgrade
920   ###
921
922   unless ( FS::upgrade_journal->is_done('cust_pay__payinfo_na') ) {
923
924     #XXX remove the 'N/A (tokenized)' part (or just this entire thing)
925
926     my @na_cust_pay = qsearch( {
927       'table'     => 'cust_pay',
928       'hashref'   => {}, #could be encrypted# { 'payinfo' => 'N/A' },
929       'extra_sql' => "WHERE ( payinfo = 'N/A' OR paymask = 'N/AA' OR paymask = 'N/A (tokenized)' ) AND payby IN ( 'CARD', 'CHEK' )",
930     } );
931
932     foreach my $na ( @na_cust_pay ) {
933
934       next unless $na->payinfo eq 'N/A';
935
936       my $cust_pay_pending =
937         qsearchs('cust_pay_pending', { 'paynum' => $na->paynum } );
938       unless ( $cust_pay_pending ) {
939         warn " *** WARNING: not-yet recoverable N/A card for payment ".
940              $na->paynum. " (no cust_pay_pending)\n";
941         next;
942       }
943       $na->$_($cust_pay_pending->$_) for qw( payinfo paymask );
944       my $error = $na->replace;
945       if ( $error ) {
946         warn " *** WARNING: Error updating payinfo for payment paynum ".
947              $na->paynun. ": $error\n";
948         next;
949       }
950
951     }
952
953     FS::upgrade_journal->set_done('cust_pay__payinfo_na');
954   }
955
956   ###
957   # otaker->usernum upgrade
958   ###
959
960   delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
961   $class->_upgrade_otaker(%opts);
962   $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
963
964 }
965
966 =back
967
968 =head1 SUBROUTINES
969
970 =over 4 
971
972 =item batch_import HASHREF
973
974 Inserts new payments.
975
976 =cut
977
978 sub batch_import {
979   my $param = shift;
980
981   my $fh = $param->{filehandle};
982   my $agentnum = $param->{agentnum};
983   my $format = $param->{'format'};
984   my $paybatch = $param->{'paybatch'};
985
986   # here is the agent virtualization
987   my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
988
989   my @fields;
990   my $payby;
991   if ( $format eq 'simple' ) {
992     @fields = qw( custnum agent_custid paid payinfo );
993     $payby = 'BILL';
994   } elsif ( $format eq 'extended' ) {
995     die "unimplemented\n";
996     @fields = qw( );
997     $payby = 'BILL';
998   } else {
999     die "unknown format $format";
1000   }
1001
1002   eval "use Text::CSV_XS;";
1003   die $@ if $@;
1004
1005   my $csv = new Text::CSV_XS;
1006
1007   my $imported = 0;
1008
1009   local $SIG{HUP} = 'IGNORE';
1010   local $SIG{INT} = 'IGNORE';
1011   local $SIG{QUIT} = 'IGNORE';
1012   local $SIG{TERM} = 'IGNORE';
1013   local $SIG{TSTP} = 'IGNORE';
1014   local $SIG{PIPE} = 'IGNORE';
1015
1016   my $oldAutoCommit = $FS::UID::AutoCommit;
1017   local $FS::UID::AutoCommit = 0;
1018   my $dbh = dbh;
1019   
1020   my $line;
1021   while ( defined($line=<$fh>) ) {
1022
1023     $csv->parse($line) or do {
1024       $dbh->rollback if $oldAutoCommit;
1025       return "can't parse: ". $csv->error_input();
1026     };
1027
1028     my @columns = $csv->fields();
1029
1030     my %cust_pay = (
1031       payby    => $payby,
1032       paybatch => $paybatch,
1033     );
1034
1035     my $cust_main;
1036     foreach my $field ( @fields ) {
1037
1038       if ( $field eq 'agent_custid'
1039         && $agentnum
1040         && $columns[0] =~ /\S+/ )
1041       {
1042
1043         my $agent_custid = $columns[0];
1044         my %hash = ( 'agent_custid' => $agent_custid,
1045                      'agentnum'     => $agentnum,
1046                    );
1047
1048         if ( $cust_pay{'custnum'} !~ /^\s*$/ ) {
1049           $dbh->rollback if $oldAutoCommit;
1050           return "can't specify custnum with agent_custid $agent_custid";
1051         }
1052
1053         $cust_main = qsearchs({
1054                                 'table'     => 'cust_main',
1055                                 'hashref'   => \%hash,
1056                                 'extra_sql' => $extra_sql,
1057                              });
1058
1059         unless ( $cust_main ) {
1060           $dbh->rollback if $oldAutoCommit;
1061           return "can't find customer with agent_custid $agent_custid";
1062         }
1063
1064         $field = 'custnum';
1065         $columns[0] = $cust_main->custnum;
1066       }
1067
1068       $cust_pay{$field} = shift @columns; 
1069     }
1070
1071     my $cust_pay = new FS::cust_pay( \%cust_pay );
1072     my $error = $cust_pay->insert;
1073
1074     if ( $error ) {
1075       $dbh->rollback if $oldAutoCommit;
1076       return "can't insert payment for $line: $error";
1077     }
1078
1079     if ( $format eq 'simple' ) {
1080       # include agentnum for less surprise?
1081       $cust_main = qsearchs({
1082                              'table'     => 'cust_main',
1083                              'hashref'   => { 'custnum' => $cust_pay->custnum },
1084                              'extra_sql' => $extra_sql,
1085                            })
1086         unless $cust_main;
1087
1088       unless ( $cust_main ) {
1089         $dbh->rollback if $oldAutoCommit;
1090         return "can't find customer to which payments apply at line: $line";
1091       }
1092
1093       $error = $cust_main->apply_payments_and_credits;
1094       if ( $error ) {
1095         $dbh->rollback if $oldAutoCommit;
1096         return "can't apply payments to customer for $line: $error";
1097       }
1098
1099     }
1100
1101     $imported++;
1102   }
1103
1104   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1105
1106   return "Empty file!" unless $imported;
1107
1108   ''; #no error
1109
1110 }
1111
1112 =back
1113
1114 =head1 BUGS
1115
1116 Delete and replace methods.  
1117
1118 =head1 SEE ALSO
1119
1120 L<FS::cust_pay_pending>, L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>,
1121 schema.html from the base documentation.
1122
1123 =cut
1124
1125 1;
1126