4 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
6 use vars qw( $DEBUG $me $conf @encrypted_fields
7 $unsuspendauto $ignore_noapply
10 use Business::CreditCard;
12 use FS::UID qw( getotaker );
13 use FS::Misc qw( send_email );
14 use FS::Record qw( dbh qsearch qsearchs );
17 use FS::cust_main_Mixin;
18 use FS::payinfo_transaction_Mixin;
20 use FS::cust_bill_pay;
21 use FS::cust_pay_refund;
24 use FS::cust_pay_void;
25 use FS::upgrade_journal;
29 $me = '[FS::cust_pay]';
33 #ask FS::UID to run this stuff for us later
34 FS::UID->install_callback( sub {
36 $unsuspendauto = $conf->exists('unsuspendauto');
39 @encrypted_fields = ('payinfo');
43 FS::cust_pay - Object methods for cust_pay objects
49 $record = new FS::cust_pay \%hash;
50 $record = new FS::cust_pay { 'column' => 'value' };
52 $error = $record->insert;
54 $error = $new_record->replace($old_record);
56 $error = $record->delete;
58 $error = $record->check;
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
70 primary key (assigned automatically for new payments)
74 customer (see L<FS::cust_main>)
78 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
79 L<Time::Local> and L<Date::Parse> for conversion functions.
83 Amount of this payment
87 order taker (see L<FS::access_user>)
91 Payment Type (See L<FS::payinfo_Mixin> for valid values)
95 Payment Information (See L<FS::payinfo_Mixin> for data format)
99 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
103 obsolete text field for tracking card processing or other batch grouping
107 Optional unique identifer to prevent duplicate transactions.
111 books closed flag, empty or `Y'
115 Desired pkgnum when using experimental package balances.
119 The bank where the payment was deposited.
123 The name of the depositor.
127 The deposit account number.
135 The number of the batch this payment came from (see L<FS::pay_batch>),
136 or null if it was processed through a realtime gateway or entered manually.
140 The number of the realtime or batch gateway L<FS::payment_gateway>) this
141 payment was processed through. Null if it was entered manually or processed
142 by the "system default" gateway, which doesn't have a number.
146 The name of the processor module (Business::OnlinePayment, ::BatchPayment,
147 or ::OnlineThirdPartyPayment subclass) used for this payment. Slightly
148 redundant with C<gatewaynum>.
152 The authorization number returned by the credit card network.
156 The transaction ID returned by the gateway, if any. This is usually what
157 you would use to initiate a void or refund of the payment.
167 Creates a new payment. To add the payment to the databse, see L<"insert">.
171 sub table { 'cust_pay'; }
172 sub cust_linked { $_[0]->cust_main_custnum; }
173 sub cust_unlinked_msg {
175 "WARNING: can't find cust_main.custnum ". $self->custnum.
176 ' (cust_pay.paynum '. $self->paynum. ')';
179 =item insert [ OPTION => VALUE ... ]
181 Adds this payment to the database.
183 For backwards-compatibility and convenience, if the additional field invnum
184 is defined, an FS::cust_bill_pay record for the full amount of the payment
185 will be created. In this case, custnum is optional.
187 If the additional field discount_term is defined then a prepayment discount
188 is taken for that length of time. It is an error for the customer to owe
189 after this payment is made.
191 A hash of optional arguments may be passed. Currently "manual" is supported.
192 If true, a payment receipt is sent instead of a statement when
193 'payment_receipt_email' configuration option is set.
198 my($self, %options) = @_;
200 local $SIG{HUP} = 'IGNORE';
201 local $SIG{INT} = 'IGNORE';
202 local $SIG{QUIT} = 'IGNORE';
203 local $SIG{TERM} = 'IGNORE';
204 local $SIG{TSTP} = 'IGNORE';
205 local $SIG{PIPE} = 'IGNORE';
207 my $oldAutoCommit = $FS::UID::AutoCommit;
208 local $FS::UID::AutoCommit = 0;
212 if ( $self->invnum ) {
213 $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
215 $dbh->rollback if $oldAutoCommit;
216 return "Unknown cust_bill.invnum: ". $self->invnum;
218 $self->custnum($cust_bill->custnum );
221 my $error = $self->check;
222 return $error if $error;
224 my $cust_main = $self->cust_main;
225 my $old_balance = $cust_main->balance;
227 $error = $self->SUPER::insert;
229 $dbh->rollback if $oldAutoCommit;
230 return "error inserting cust_pay: $error";
233 if ( my $credit_type = $conf->config('prepayment_discounts-credit_type') ) {
234 if ( my $months = $self->discount_term ) {
235 # XXX this should be moved out somewhere, but discount_term_values
237 my ($cust_bill) = ($cust_main->cust_bill)[-1]; # most recent invoice
238 return "can't accept prepayment for an unbilled customer" if !$cust_bill;
240 # %billing_pkgs contains this customer's active monthly packages.
241 # Recurring fees for those packages will be credited and then rebilled
242 # for the full discount term. Other packages on the last invoice
243 # (canceled, non-monthly recurring, or one-time charges) will be
245 my %billing_pkgs = map { $_->pkgnum => $_ }
246 grep { $_->part_pkg->freq eq '1' }
247 $cust_main->billing_pkgs;
248 my $credit = 0; # sum of recurring charges from that invoice
249 my $last_bill_date = 0; # the real bill date
250 foreach my $item ( $cust_bill->cust_bill_pkg ) {
251 next if !exists($billing_pkgs{$item->pkgnum}); # skip inactive packages
252 $credit += $item->recur;
253 $last_bill_date = $item->cust_pkg->last_bill
254 if defined($item->cust_pkg)
255 and $item->cust_pkg->last_bill > $last_bill_date
258 my $cust_credit = new FS::cust_credit {
259 'custnum' => $self->custnum,
260 'amount' => sprintf('%.2f', $credit),
261 'reason' => 'customer chose to prepay for discount',
263 $error = $cust_credit->insert('reason_type' => $credit_type);
265 $dbh->rollback if $oldAutoCommit;
266 return "error inserting prepayment credit: $error";
270 # bill for the entire term
271 $_->bill($_->last_bill) foreach (values %billing_pkgs);
272 $error = $cust_main->bill(
273 # no recurring_only, we want unbilled packages with start dates to
275 'no_usage_reset' => 1,
276 'time' => $last_bill_date, # not $cust_bill->_date
277 'pkg_list' => [ values %billing_pkgs ],
278 'freq_override' => $months,
281 $dbh->rollback if $oldAutoCommit;
282 return "error inserting cust_pay: $error";
284 $error = $cust_main->apply_payments_and_credits;
286 $dbh->rollback if $oldAutoCommit;
287 return "error inserting cust_pay: $error";
289 my $new_balance = $cust_main->balance;
290 if ($new_balance > 0) {
291 $dbh->rollback if $oldAutoCommit;
292 return "balance after prepay discount attempt: $new_balance";
294 # user friendly: override the "apply only to this invoice" mode
301 if ( $self->invnum ) {
302 my $cust_bill_pay = new FS::cust_bill_pay {
303 'invnum' => $self->invnum,
304 'paynum' => $self->paynum,
305 'amount' => $self->paid,
306 '_date' => $self->_date,
308 $error = $cust_bill_pay->insert(%options);
310 if ( $ignore_noapply ) {
311 warn "warning: error inserting cust_bill_pay: $error ".
312 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
314 $dbh->rollback if $oldAutoCommit;
315 return "error inserting cust_bill_pay: $error";
320 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
322 #false laziness w/ cust_credit::insert
323 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
324 my @errors = $cust_main->unsuspend;
326 # side-fx with nested transactions? upstack rolls back?
327 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
333 #bill setup fees for voip_cdr bill_every_call packages
334 #some false laziness w/search in freeside-cdrd
336 'LEFT JOIN part_pkg USING ( pkgpart ) '.
337 "LEFT JOIN part_pkg_option
338 ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
339 AND part_pkg_option.optionname = 'bill_every_call' )";
341 my $extra_sql = " AND plan = 'voip_cdr' AND optionvalue = '1' ".
342 " AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 ) ";
344 my @cust_pkg = qsearch({
345 'table' => 'cust_pkg',
346 'addl_from' => $addl_from,
347 'hashref' => { 'custnum' => $self->custnum,
351 'extra_sql' => $extra_sql,
355 warn "voip_cdr bill_every_call packages found; billing customer\n";
356 my $bill_error = $self->cust_main->bill_and_collect( 'fatal' => 'return' );
358 warn "WARNING: Error billing customer: $bill_error\n";
361 #end of billing setup fees for voip_cdr bill_every_call packages
363 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
366 my $trigger = $conf->config('payment_receipt-trigger',
367 $self->cust_main->agentnum) || 'cust_pay';
368 if ( $trigger eq 'cust_pay' ) {
369 my $error = $self->send_receipt(
370 'manual' => $options{'manual'},
371 'cust_bill' => $cust_bill,
372 'cust_main' => $cust_main,
374 warn "can't send payment receipt/statement: $error" if $error;
381 =item void [ REASON ]
383 Voids this payment: deletes the payment and all associated applications and
384 adds a record of the voided payment to the FS::cust_pay_void table.
391 local $SIG{HUP} = 'IGNORE';
392 local $SIG{INT} = 'IGNORE';
393 local $SIG{QUIT} = 'IGNORE';
394 local $SIG{TERM} = 'IGNORE';
395 local $SIG{TSTP} = 'IGNORE';
396 local $SIG{PIPE} = 'IGNORE';
398 my $oldAutoCommit = $FS::UID::AutoCommit;
399 local $FS::UID::AutoCommit = 0;
402 my $cust_pay_void = new FS::cust_pay_void ( {
403 map { $_ => $self->get($_) } $self->fields
405 $cust_pay_void->reason(shift) if scalar(@_);
406 my $error = $cust_pay_void->insert;
408 $dbh->rollback if $oldAutoCommit;
412 $error = $self->delete;
414 $dbh->rollback if $oldAutoCommit;
418 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
426 Unless the closed flag is set, deletes this payment and all associated
427 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>). In most
428 cases, you want to use the void method instead to leave a record of the
433 # very similar to FS::cust_credit::delete
436 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
438 local $SIG{HUP} = 'IGNORE';
439 local $SIG{INT} = 'IGNORE';
440 local $SIG{QUIT} = 'IGNORE';
441 local $SIG{TERM} = 'IGNORE';
442 local $SIG{TSTP} = 'IGNORE';
443 local $SIG{PIPE} = 'IGNORE';
445 my $oldAutoCommit = $FS::UID::AutoCommit;
446 local $FS::UID::AutoCommit = 0;
449 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
450 my $error = $app->delete;
452 $dbh->rollback if $oldAutoCommit;
457 my $error = $self->SUPER::delete(@_);
459 $dbh->rollback if $oldAutoCommit;
463 if ( $conf->exists('deletepayments')
464 && $conf->config('deletepayments') ne '' ) {
466 my $cust_main = $self->cust_main;
468 my $error = send_email(
469 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
470 #invoice_from??? well as good as any
471 'to' => $conf->config('deletepayments'),
472 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
474 "This is an automatic message from your Freeside installation\n",
475 "informing you that the following payment has been deleted:\n",
477 'paynum: '. $self->paynum. "\n",
478 'custnum: '. $self->custnum.
479 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
480 'paid: $'. sprintf("%.2f", $self->paid). "\n",
481 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
482 'payby: '. $self->payby. "\n",
483 'payinfo: '. $self->paymask. "\n",
484 'paybatch: '. $self->paybatch. "\n",
489 $dbh->rollback if $oldAutoCommit;
490 return "can't send payment deletion notification: $error";
495 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
501 =item replace [ OLD_RECORD ]
503 You can, but probably shouldn't modify payments...
505 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
506 supplied, replaces this record. If there is an error, returns the error,
507 otherwise returns false.
513 return "Can't modify closed payment" if $self->closed =~ /^Y/i;
514 $self->SUPER::replace(@_);
519 Checks all fields to make sure this is a valid payment. If there is an error,
520 returns the error, otherwise returns false. Called by the insert method.
527 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
530 $self->ut_numbern('paynum')
531 || $self->ut_numbern('custnum')
532 || $self->ut_numbern('_date')
533 || $self->ut_money('paid')
534 || $self->ut_alphan('otaker')
535 || $self->ut_textn('paybatch')
536 || $self->ut_textn('payunique')
537 || $self->ut_enum('closed', [ '', 'Y' ])
538 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
539 || $self->ut_textn('bank')
540 || $self->ut_alphan('depositor')
541 || $self->ut_numbern('account')
542 || $self->ut_numbern('teller')
543 || $self->ut_foreign_keyn('batchnum', 'pay_batch', 'batchnum')
544 || $self->payinfo_check()
546 return $error if $error;
548 return "paid must be > 0 " if $self->paid <= 0;
550 return "unknown cust_main.custnum: ". $self->custnum
552 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
554 $self->_date(time) unless $self->_date;
556 return "invalid discount_term"
557 if ($self->discount_term && $self->discount_term < 2);
559 if ( $self->payby eq 'CASH' and $conf->exists('require_cash_deposit_info') ) {
560 foreach (qw(bank depositor account teller)) {
561 return "$_ required" if $self->get($_) eq '';
565 #i guess not now, with cust_pay_pending, if we actually make it here, we _do_ want to record it
566 # # UNIQUE index should catch this too, without race conditions, but this
567 # # should give a better error message the other 99.9% of the time...
568 # if ( length($self->payunique)
569 # && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) {
570 # #well, it *could* be a better error message
571 # return "duplicate transaction".
572 # " - a payment with unique identifer ". $self->payunique.
579 =item send_receipt HASHREF | OPTION => VALUE ...
581 Sends a payment receipt for this payment..
589 Flag indicating the payment is being made manually.
593 Invoice (FS::cust_bill) object. If not specified, the most recent invoice
598 Customer (FS::cust_main) object (for efficiency).
606 my $opt = ref($_[0]) ? shift : { @_ };
608 my $cust_bill = $opt->{'cust_bill'};
609 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
611 my $conf = new FS::Conf;
613 return '' unless $conf->config_bool('payment_receipt', $cust_main->agentnum);
615 my @invoicing_list = $cust_main->invoicing_list_emailonly;
616 return '' unless @invoicing_list;
618 $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
622 if ( ( exists($opt->{'manual'}) && $opt->{'manual'} )
623 #|| ! $conf->exists('invoice_html_statement')
627 my $msgnum = $conf->config('payment_receipt_msgnum', $cust_main->agentnum);
629 my $msg_template = FS::msg_template->by_key($msgnum);
630 $error = $msg_template->send(
631 'cust_main' => $cust_main,
633 'from_config' => 'payment_receipt_from',
636 } elsif ( $conf->exists('payment_receipt_email') ) {
638 my $receipt_template = new Text::Template (
640 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
642 warn "can't create payment receipt template: $Text::Template::ERROR";
646 my $payby = $self->payby;
647 my $payinfo = $self->payinfo;
648 $payby =~ s/^BILL$/Check/ if $payinfo;
649 if ( $payby eq 'CARD' || $payby eq 'CHEK' ) {
650 $payinfo = $self->paymask
652 $payinfo = $self->decrypt($payinfo);
654 $payby =~ s/^CHEK$/Electronic check/;
657 'date' => time2str("%a %B %o, %Y", $self->_date),
658 'name' => $cust_main->name,
659 'paynum' => $self->paynum,
660 'paid' => sprintf("%.2f", $self->paid),
661 'payby' => ucfirst(lc($payby)),
662 'payinfo' => $payinfo,
663 'balance' => $cust_main->balance,
664 'company_name' => $conf->config('company_name', $cust_main->agentnum),
667 if ( $opt->{'cust_pkg'} ) {
668 $fill_in{'pkg'} = $opt->{'cust_pkg'}->part_pkg->pkg;
669 #setup date, other things?
673 'from' => $conf->config('invoice_from', $cust_main->agentnum),
674 #invoice_from??? well as good as any
675 'to' => \@invoicing_list,
676 'subject' => 'Payment receipt',
677 'body' => [ $receipt_template->fill_in( HASH => \%fill_in ) ],
682 warn "payment_receipt is on, but no payment_receipt_msgnum\n";
686 } elsif ( ! $cust_main->invoice_noemail ) { #not manual
688 my $queue = new FS::queue {
689 'paynum' => $self->paynum,
690 'job' => 'FS::cust_bill::queueable_email',
693 $error = $queue->insert(
694 'invnum' => $cust_bill->invnum,
695 'template' => 'statement',
696 'notice_name' => 'Statement',
702 warn "send_receipt: $error\n" if $error;
707 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
714 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
715 sort { $a->_date <=> $b->_date
716 || $a->invnum <=> $b->invnum }
717 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
721 =item cust_pay_refund
723 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
728 sub cust_pay_refund {
730 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
731 sort { $a->_date <=> $b->_date }
732 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
739 Returns the amount of this payment that is still unapplied; which is
740 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
741 applications (see L<FS::cust_pay_refund>).
747 my $amount = $self->paid;
748 $amount -= $_->amount foreach ( $self->cust_bill_pay );
749 $amount -= $_->amount foreach ( $self->cust_pay_refund );
750 sprintf("%.2f", $amount );
755 Returns the amount of this payment that has not been refuned; which is
756 paid minus all refund applications (see L<FS::cust_pay_refund>).
762 my $amount = $self->paid;
763 $amount -= $_->amount foreach ( $self->cust_pay_refund );
764 sprintf("%.2f", $amount );
769 Returns the "paid" field.
784 =item batch_insert CUST_PAY_OBJECT, ...
786 Class method which inserts multiple payments. Takes a list of FS::cust_pay
787 objects. Returns a list, each element representing the status of inserting the
788 corresponding payment - empty. If there is an error inserting any payment, the
789 entire transaction is rolled back, i.e. all payments are inserted or none are.
791 FS::cust_pay objects may have the pseudo-field 'apply_to', containing a
792 reference to an array of (uninserted) FS::cust_bill_pay objects. If so,
793 those objects will be inserted with the paynum of the payment, and for
794 each one, an error message or an empty string will be inserted into the
799 my @errors = FS::cust_pay->batch_insert(@cust_pay);
800 my $num_errors = scalar(grep $_, @errors);
801 if ( $num_errors == 0 ) {
802 #success; all payments were inserted
804 #failure; no payments were inserted.
810 my $self = shift; #class method
812 local $SIG{HUP} = 'IGNORE';
813 local $SIG{INT} = 'IGNORE';
814 local $SIG{QUIT} = 'IGNORE';
815 local $SIG{TERM} = 'IGNORE';
816 local $SIG{TSTP} = 'IGNORE';
817 local $SIG{PIPE} = 'IGNORE';
819 my $oldAutoCommit = $FS::UID::AutoCommit;
820 local $FS::UID::AutoCommit = 0;
826 foreach my $cust_pay (@_) {
827 my $error = $cust_pay->insert( 'manual' => 1 );
828 push @errors, $error;
829 $num_errors++ if $error;
831 if ( ref($cust_pay->get('apply_to')) eq 'ARRAY' ) {
833 foreach my $cust_bill_pay ( @{ $cust_pay->apply_to } ) {
834 if ( $error ) { # insert placeholders if cust_pay wasn't inserted
838 $cust_bill_pay->set('paynum', $cust_pay->paynum);
839 my $apply_error = $cust_bill_pay->insert;
840 push @errors, $apply_error || '';
841 $num_errors++ if $apply_error;
845 } elsif ( !$error ) { #normal case: apply payments as usual
846 $cust_pay->cust_main->apply_payments;
852 $dbh->rollback if $oldAutoCommit;
854 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
863 Returns an SQL fragment to retreive the unapplied amount.
868 my ($class, $start, $end) = @_;
869 my $bill_start = $start ? "AND cust_bill_pay._date <= $start" : '';
870 my $bill_end = $end ? "AND cust_bill_pay._date > $end" : '';
871 my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
872 my $refund_end = $end ? "AND cust_pay_refund._date > $end" : '';
876 ( SELECT SUM(amount) FROM cust_bill_pay
877 WHERE cust_pay.paynum = cust_bill_pay.paynum
878 $bill_start $bill_end )
882 ( SELECT SUM(amount) FROM cust_pay_refund
883 WHERE cust_pay.paynum = cust_pay_refund.paynum
884 $refund_start $refund_end )
893 # Used by FS::Upgrade to migrate to a new database.
897 sub _upgrade_data { #class method
898 my ($class, %opts) = @_;
900 warn "$me upgrading $class\n" if $DEBUG;
902 local $FS::payinfo_Mixin::ignore_masked_payinfo = 1;
905 # otaker/ivan upgrade
908 unless ( FS::upgrade_journal->is_done('cust_pay__otaker_ivan') ) {
910 #not the most efficient, but hey, it only has to run once
912 my $where = "WHERE ( otaker IS NULL OR otaker = '' OR otaker = 'ivan' ) ".
913 " AND usernum IS NULL ".
914 " AND 0 < ( SELECT COUNT(*) FROM cust_main ".
915 " WHERE cust_main.custnum = cust_pay.custnum ) ";
917 my $count_sql = "SELECT COUNT(*) FROM cust_pay $where";
919 my $sth = dbh->prepare($count_sql) or die dbh->errstr;
920 $sth->execute or die $sth->errstr;
921 my $total = $sth->fetchrow_arrayref->[0];
922 #warn "$total cust_pay records to update\n"
924 local($DEBUG) = 2 if $total > 1000; #could be a while, force progress info
929 my @cust_pay = qsearch( {
930 'table' => 'cust_pay',
932 'extra_sql' => $where,
933 'order_by' => 'ORDER BY paynum',
936 foreach my $cust_pay (@cust_pay) {
938 my $h_cust_pay = $cust_pay->h_search('insert');
940 next if $cust_pay->otaker eq $h_cust_pay->history_user;
941 #$cust_pay->otaker($h_cust_pay->history_user);
942 $cust_pay->set('otaker', $h_cust_pay->history_user);
944 $cust_pay->set('otaker', 'legacy');
947 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
948 my $error = $cust_pay->replace;
951 warn " *** WARNING: Error updating order taker for payment paynum ".
952 $cust_pay->paynun. ": $error\n";
956 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
959 if ( $DEBUG > 1 && $lastprog + 30 < time ) {
960 warn "$me $count/$total (".sprintf('%.2f',100*$count/$total). '%)'."\n";
966 FS::upgrade_journal->set_done('cust_pay__otaker_ivan');
970 # payinfo N/A upgrade
973 unless ( FS::upgrade_journal->is_done('cust_pay__payinfo_na') ) {
975 #XXX remove the 'N/A (tokenized)' part (or just this entire thing)
977 my @na_cust_pay = qsearch( {
978 'table' => 'cust_pay',
979 'hashref' => {}, #could be encrypted# { 'payinfo' => 'N/A' },
980 'extra_sql' => "WHERE ( payinfo = 'N/A' OR paymask = 'N/AA' OR paymask = 'N/A (tokenized)' ) AND payby IN ( 'CARD', 'CHEK' )",
983 foreach my $na ( @na_cust_pay ) {
985 next unless $na->payinfo eq 'N/A';
987 my $cust_pay_pending =
988 qsearchs('cust_pay_pending', { 'paynum' => $na->paynum } );
989 unless ( $cust_pay_pending ) {
990 warn " *** WARNING: not-yet recoverable N/A card for payment ".
991 $na->paynum. " (no cust_pay_pending)\n";
994 $na->$_($cust_pay_pending->$_) for qw( payinfo paymask );
995 my $error = $na->replace;
997 warn " *** WARNING: Error updating payinfo for payment paynum ".
998 $na->paynun. ": $error\n";
1004 FS::upgrade_journal->set_done('cust_pay__payinfo_na');
1008 # otaker->usernum upgrade
1011 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
1012 $class->_upgrade_otaker(%opts);
1013 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
1016 # migrate batchnums from the misused 'paybatch' field to 'batchnum'
1018 my @cust_pay = qsearch( {
1019 'table' => 'cust_pay',
1020 'addl_from' => ' JOIN pay_batch ON cust_pay.paybatch = CAST(pay_batch.batchnum AS text) ',
1022 foreach my $cust_pay (@cust_pay) {
1023 $cust_pay->set('batchnum' => $cust_pay->paybatch);
1024 $cust_pay->set('paybatch' => '');
1025 my $error = $cust_pay->replace;
1026 warn "error setting batchnum on cust_pay #".$cust_pay->paynum.":\n $error"
1031 # migrate gateway info from the misused 'paybatch' field
1034 # not only cust_pay, but also voided and refunded payments
1035 if (!FS::upgrade_journal->is_done('cust_pay__parse_paybatch_1')) {
1036 local $FS::Record::nowarn_classload=1;
1037 # really inefficient, but again, only has to run once
1038 foreach my $table (qw(cust_pay cust_pay_void cust_refund)) {
1039 my $and_batchnum_is_null =
1040 ( $table =~ /^cust_pay/ ? ' AND batchnum IS NULL' : '' );
1041 foreach my $object ( qsearch({
1043 extra_sql => "WHERE payby IN('CARD','CHEK') ".
1044 "AND (paybatch IS NOT NULL ".
1045 "OR (paybatch IS NULL AND auth IS NULL
1046 $and_batchnum_is_null ) )",
1049 if ( $object->paybatch eq '' ) {
1050 # repair for a previous upgrade that didn't save 'auth'
1051 my $pkey = $object->primary_key;
1052 # find the last history record that had a paybatch value
1054 table => "h_$table",
1056 $pkey => $object->$pkey,
1057 paybatch => { op=>'!=', value=>''},
1058 history_action => 'replace_old',
1060 order_by => 'ORDER BY history_date DESC LIMIT 1',
1063 warn "couldn't find paybatch history record for $table ".$object->$pkey."\n";
1066 # set paybatch to what it was in that record
1067 $object->set('paybatch', $h->paybatch)
1068 # and then upgrade it like the old records
1071 my $parsed = $object->_parse_paybatch;
1072 if (keys %$parsed) {
1073 $object->set($_ => $parsed->{$_}) foreach keys %$parsed;
1074 $object->set('auth' => $parsed->{authorization});
1075 $object->set('paybatch', '');
1076 my $error = $object->replace;
1077 warn "error parsing CARD/CHEK paybatch fields on $object #".
1078 $object->get($object->primary_key).":\n $error\n"
1083 FS::upgrade_journal->set_done('cust_pay__parse_paybatch_1');
1093 =item batch_import HASHREF
1095 Inserts new payments.
1102 my $fh = $param->{filehandle};
1103 my $agentnum = $param->{agentnum};
1104 my $format = $param->{'format'};
1105 my $paybatch = $param->{'paybatch'};
1107 # here is the agent virtualization
1108 my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
1112 if ( $format eq 'simple' ) {
1113 @fields = qw( custnum agent_custid paid payinfo );
1115 } elsif ( $format eq 'extended' ) {
1116 die "unimplemented\n";
1120 die "unknown format $format";
1123 eval "use Text::CSV_XS;";
1126 my $csv = new Text::CSV_XS;
1130 local $SIG{HUP} = 'IGNORE';
1131 local $SIG{INT} = 'IGNORE';
1132 local $SIG{QUIT} = 'IGNORE';
1133 local $SIG{TERM} = 'IGNORE';
1134 local $SIG{TSTP} = 'IGNORE';
1135 local $SIG{PIPE} = 'IGNORE';
1137 my $oldAutoCommit = $FS::UID::AutoCommit;
1138 local $FS::UID::AutoCommit = 0;
1142 while ( defined($line=<$fh>) ) {
1144 $csv->parse($line) or do {
1145 $dbh->rollback if $oldAutoCommit;
1146 return "can't parse: ". $csv->error_input();
1149 my @columns = $csv->fields();
1153 paybatch => $paybatch,
1157 foreach my $field ( @fields ) {
1159 if ( $field eq 'agent_custid'
1161 && $columns[0] =~ /\S+/ )
1164 my $agent_custid = $columns[0];
1165 my %hash = ( 'agent_custid' => $agent_custid,
1166 'agentnum' => $agentnum,
1169 if ( $cust_pay{'custnum'} !~ /^\s*$/ ) {
1170 $dbh->rollback if $oldAutoCommit;
1171 return "can't specify custnum with agent_custid $agent_custid";
1174 $cust_main = qsearchs({
1175 'table' => 'cust_main',
1176 'hashref' => \%hash,
1177 'extra_sql' => $extra_sql,
1180 unless ( $cust_main ) {
1181 $dbh->rollback if $oldAutoCommit;
1182 return "can't find customer with agent_custid $agent_custid";
1186 $columns[0] = $cust_main->custnum;
1189 $cust_pay{$field} = shift @columns;
1192 my $cust_pay = new FS::cust_pay( \%cust_pay );
1193 my $error = $cust_pay->insert;
1196 $dbh->rollback if $oldAutoCommit;
1197 return "can't insert payment for $line: $error";
1200 if ( $format eq 'simple' ) {
1201 # include agentnum for less surprise?
1202 $cust_main = qsearchs({
1203 'table' => 'cust_main',
1204 'hashref' => { 'custnum' => $cust_pay->custnum },
1205 'extra_sql' => $extra_sql,
1209 unless ( $cust_main ) {
1210 $dbh->rollback if $oldAutoCommit;
1211 return "can't find customer to which payments apply at line: $line";
1214 $error = $cust_main->apply_payments_and_credits;
1216 $dbh->rollback if $oldAutoCommit;
1217 return "can't apply payments to customer for $line: $error";
1225 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1227 return "Empty file!" unless $imported;
1237 Delete and replace methods.
1241 L<FS::cust_pay_pending>, L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>,
1242 schema.html from the base documentation.