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::Misc::DateTime qw( parse_datetime ); #for batch_import
15 use FS::Record qw( dbh qsearch qsearchs );
18 use FS::cust_main_Mixin;
19 use FS::payinfo_transaction_Mixin;
21 use FS::cust_bill_pay;
22 use FS::cust_pay_refund;
25 use FS::cust_pay_void;
26 use FS::upgrade_journal;
31 $me = '[FS::cust_pay]';
35 #ask FS::UID to run this stuff for us later
36 FS::UID->install_callback( sub {
38 $unsuspendauto = $conf->exists('unsuspendauto');
41 @encrypted_fields = ('payinfo');
42 sub nohistory_fields { ('payinfo'); }
46 FS::cust_pay - Object methods for cust_pay objects
52 $record = new FS::cust_pay \%hash;
53 $record = new FS::cust_pay { 'column' => 'value' };
55 $error = $record->insert;
57 $error = $new_record->replace($old_record);
59 $error = $record->delete;
61 $error = $record->check;
65 An FS::cust_pay object represents a payment; the transfer of money from a
66 customer. FS::cust_pay inherits from FS::Record. The following fields are
73 primary key (assigned automatically for new payments)
77 customer (see L<FS::cust_main>)
81 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
82 L<Time::Local> and L<Date::Parse> for conversion functions.
86 Amount of this payment
90 order taker (see L<FS::access_user>)
94 Payment Type (See L<FS::payinfo_Mixin> for valid values)
98 Payment Information (See L<FS::payinfo_Mixin> for data format)
102 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
106 obsolete text field for tracking card processing or other batch grouping
110 Optional unique identifer to prevent duplicate transactions.
114 books closed flag, empty or `Y'
118 Desired pkgnum when using experimental package balances.
122 The bank where the payment was deposited.
126 The name of the depositor.
130 The deposit account number.
138 The number of the batch this payment came from (see L<FS::pay_batch>),
139 or null if it was processed through a realtime gateway or entered manually.
143 The number of the realtime or batch gateway L<FS::payment_gateway>) this
144 payment was processed through. Null if it was entered manually or processed
145 by the "system default" gateway, which doesn't have a number.
149 The name of the processor module (Business::OnlinePayment, ::BatchPayment,
150 or ::OnlineThirdPartyPayment subclass) used for this payment. Slightly
151 redundant with C<gatewaynum>.
155 The authorization number returned by the credit card network.
159 The transaction ID returned by the gateway, if any. This is usually what
160 you would use to initiate a void or refund of the payment.
170 Creates a new payment. To add the payment to the databse, see L<"insert">.
174 sub table { 'cust_pay'; }
175 sub cust_linked { $_[0]->cust_main_custnum; }
176 sub cust_unlinked_msg {
178 "WARNING: can't find cust_main.custnum ". $self->custnum.
179 ' (cust_pay.paynum '. $self->paynum. ')';
182 =item insert [ OPTION => VALUE ... ]
184 Adds this payment to the database.
186 For backwards-compatibility and convenience, if the additional field invnum
187 is defined, an FS::cust_bill_pay record for the full amount of the payment
188 will be created. In this case, custnum is optional.
190 If the additional field discount_term is defined then a prepayment discount
191 is taken for that length of time. It is an error for the customer to owe
192 after this payment is made.
194 A hash of optional arguments may be passed. Currently "manual" is supported.
195 If true, a payment receipt is sent instead of a statement when
196 'payment_receipt_email' configuration option is set.
198 About the "manual" flag: Normally, if the 'payment_receipt' config option
199 is set, and the customer has an invoice email address, inserting a payment
200 causes a I<statement> to be emailed to the customer. If the payment is
201 considered "manual" (or if the customer has no invoices), then it will
202 instead send a I<payment receipt>. "manual" should be true whenever a
203 payment is created directly from the web interface, from a user-initiated
204 realtime payment, or from a third-party payment via self-service. It should
205 be I<false> when creating a payment from a billing event or from a batch.
210 my($self, %options) = @_;
212 local $SIG{HUP} = 'IGNORE';
213 local $SIG{INT} = 'IGNORE';
214 local $SIG{QUIT} = 'IGNORE';
215 local $SIG{TERM} = 'IGNORE';
216 local $SIG{TSTP} = 'IGNORE';
217 local $SIG{PIPE} = 'IGNORE';
219 my $oldAutoCommit = $FS::UID::AutoCommit;
220 local $FS::UID::AutoCommit = 0;
224 if ( $self->invnum ) {
225 $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
227 $dbh->rollback if $oldAutoCommit;
228 return "Unknown cust_bill.invnum: ". $self->invnum;
230 $self->custnum($cust_bill->custnum );
233 my $error = $self->check;
234 return $error if $error;
236 my $cust_main = $self->cust_main;
237 my $old_balance = $cust_main->balance;
239 $error = $self->SUPER::insert;
241 $dbh->rollback if $oldAutoCommit;
242 return "error inserting cust_pay: $error";
245 if ( my $credit_type = $conf->config('prepayment_discounts-credit_type') ) {
246 if ( my $months = $self->discount_term ) {
247 # XXX this should be moved out somewhere, but discount_term_values
249 my ($cust_bill) = ($cust_main->cust_bill)[-1]; # most recent invoice
250 return "can't accept prepayment for an unbilled customer" if !$cust_bill;
252 # %billing_pkgs contains this customer's active monthly packages.
253 # Recurring fees for those packages will be credited and then rebilled
254 # for the full discount term. Other packages on the last invoice
255 # (canceled, non-monthly recurring, or one-time charges) will be
257 my %billing_pkgs = map { $_->pkgnum => $_ }
258 grep { $_->part_pkg->freq eq '1' }
259 $cust_main->billing_pkgs;
260 my $credit = 0; # sum of recurring charges from that invoice
261 my $last_bill_date = 0; # the real bill date
262 foreach my $item ( $cust_bill->cust_bill_pkg ) {
263 next if !exists($billing_pkgs{$item->pkgnum}); # skip inactive packages
264 $credit += $item->recur;
265 $last_bill_date = $item->cust_pkg->last_bill
266 if defined($item->cust_pkg)
267 and $item->cust_pkg->last_bill > $last_bill_date
270 my $cust_credit = new FS::cust_credit {
271 'custnum' => $self->custnum,
272 'amount' => sprintf('%.2f', $credit),
273 'reason' => 'customer chose to prepay for discount',
275 $error = $cust_credit->insert('reason_type' => $credit_type);
277 $dbh->rollback if $oldAutoCommit;
278 return "error inserting prepayment credit: $error";
282 # bill for the entire term
283 $_->bill($_->last_bill) foreach (values %billing_pkgs);
284 $error = $cust_main->bill(
285 # no recurring_only, we want unbilled packages with start dates to
287 'no_usage_reset' => 1,
288 'time' => $last_bill_date, # not $cust_bill->_date
289 'pkg_list' => [ values %billing_pkgs ],
290 'freq_override' => $months,
293 $dbh->rollback if $oldAutoCommit;
294 return "error inserting cust_pay: $error";
296 $error = $cust_main->apply_payments_and_credits;
298 $dbh->rollback if $oldAutoCommit;
299 return "error inserting cust_pay: $error";
301 my $new_balance = $cust_main->balance;
302 if ($new_balance > 0) {
303 $dbh->rollback if $oldAutoCommit;
304 return "balance after prepay discount attempt: $new_balance";
306 # user friendly: override the "apply only to this invoice" mode
313 if ( $self->invnum ) {
314 my $cust_bill_pay = new FS::cust_bill_pay {
315 'invnum' => $self->invnum,
316 'paynum' => $self->paynum,
317 'amount' => $self->paid,
318 '_date' => $self->_date,
320 $error = $cust_bill_pay->insert(%options);
322 if ( $ignore_noapply ) {
323 warn "warning: error inserting cust_bill_pay: $error ".
324 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
326 $dbh->rollback if $oldAutoCommit;
327 return "error inserting cust_bill_pay: $error";
332 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
334 #false laziness w/ cust_credit::insert
335 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
336 my @errors = $cust_main->unsuspend;
338 # side-fx with nested transactions? upstack rolls back?
339 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
345 #bill setup fees for voip_cdr bill_every_call packages
346 #some false laziness w/search in freeside-cdrd
348 'LEFT JOIN part_pkg USING ( pkgpart ) '.
349 "LEFT JOIN part_pkg_option
350 ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
351 AND part_pkg_option.optionname = 'bill_every_call' )";
353 my $extra_sql = " AND plan = 'voip_cdr' AND optionvalue = '1' ".
354 " AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 ) ";
356 my @cust_pkg = qsearch({
357 'table' => 'cust_pkg',
358 'addl_from' => $addl_from,
359 'hashref' => { 'custnum' => $self->custnum,
363 'extra_sql' => $extra_sql,
367 warn "voip_cdr bill_every_call packages found; billing customer\n";
368 my $bill_error = $self->cust_main->bill_and_collect( 'fatal' => 'return' );
370 warn "WARNING: Error billing customer: $bill_error\n";
373 #end of billing setup fees for voip_cdr bill_every_call packages
375 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
378 my $trigger = $conf->config('payment_receipt-trigger',
379 $self->cust_main->agentnum) || 'cust_pay';
380 if ( $trigger eq 'cust_pay' ) {
381 my $error = $self->send_receipt(
382 'manual' => $options{'manual'},
383 'cust_bill' => $cust_bill,
384 'cust_main' => $cust_main,
386 warn "can't send payment receipt/statement: $error" if $error;
393 =item void [ REASON ]
395 Voids this payment: deletes the payment and all associated applications and
396 adds a record of the voided payment to the FS::cust_pay_void table.
403 local $SIG{HUP} = 'IGNORE';
404 local $SIG{INT} = 'IGNORE';
405 local $SIG{QUIT} = 'IGNORE';
406 local $SIG{TERM} = 'IGNORE';
407 local $SIG{TSTP} = 'IGNORE';
408 local $SIG{PIPE} = 'IGNORE';
410 my $oldAutoCommit = $FS::UID::AutoCommit;
411 local $FS::UID::AutoCommit = 0;
414 my $cust_pay_void = new FS::cust_pay_void ( {
415 map { $_ => $self->get($_) } $self->fields
417 $cust_pay_void->reason(shift) if scalar(@_);
418 my $error = $cust_pay_void->insert;
420 $dbh->rollback if $oldAutoCommit;
424 $error = $self->delete;
426 $dbh->rollback if $oldAutoCommit;
430 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
438 Unless the closed flag is set, deletes this payment and all associated
439 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>). In most
440 cases, you want to use the void method instead to leave a record of the
445 # very similar to FS::cust_credit::delete
448 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
450 local $SIG{HUP} = 'IGNORE';
451 local $SIG{INT} = 'IGNORE';
452 local $SIG{QUIT} = 'IGNORE';
453 local $SIG{TERM} = 'IGNORE';
454 local $SIG{TSTP} = 'IGNORE';
455 local $SIG{PIPE} = 'IGNORE';
457 my $oldAutoCommit = $FS::UID::AutoCommit;
458 local $FS::UID::AutoCommit = 0;
461 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
462 my $error = $app->delete;
464 $dbh->rollback if $oldAutoCommit;
469 my $error = $self->SUPER::delete(@_);
471 $dbh->rollback if $oldAutoCommit;
475 if ( $conf->exists('deletepayments')
476 && $conf->config('deletepayments') ne '' ) {
478 my $cust_main = $self->cust_main;
480 my $error = send_email(
481 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
482 #invoice_from??? well as good as any
483 'to' => $conf->config('deletepayments'),
484 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
486 "This is an automatic message from your Freeside installation\n",
487 "informing you that the following payment has been deleted:\n",
489 'paynum: '. $self->paynum. "\n",
490 'custnum: '. $self->custnum.
491 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
492 'paid: $'. sprintf("%.2f", $self->paid). "\n",
493 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
494 'payby: '. $self->payby. "\n",
495 'payinfo: '. $self->paymask. "\n",
496 'paybatch: '. $self->paybatch. "\n",
501 $dbh->rollback if $oldAutoCommit;
502 return "can't send payment deletion notification: $error";
507 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
513 =item replace [ OLD_RECORD ]
515 You can, but probably shouldn't modify payments...
517 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
518 supplied, replaces this record. If there is an error, returns the error,
519 otherwise returns false.
525 return "Can't modify closed payment" if $self->closed =~ /^Y/i;
526 $self->SUPER::replace(@_);
531 Checks all fields to make sure this is a valid payment. If there is an error,
532 returns the error, otherwise returns false. Called by the insert method.
539 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
542 $self->ut_numbern('paynum')
543 || $self->ut_numbern('custnum')
544 || $self->ut_numbern('_date')
545 || $self->ut_money('paid')
546 || $self->ut_alphan('otaker')
547 || $self->ut_textn('paybatch')
548 || $self->ut_textn('payunique')
549 || $self->ut_enum('closed', [ '', 'Y' ])
550 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
551 || $self->ut_textn('bank')
552 || $self->ut_alphan('depositor')
553 || $self->ut_numbern('account')
554 || $self->ut_numbern('teller')
555 || $self->ut_foreign_keyn('batchnum', 'pay_batch', 'batchnum')
556 || $self->payinfo_check()
558 return $error if $error;
560 return "paid must be > 0 " if $self->paid <= 0;
562 return "unknown cust_main.custnum: ". $self->custnum
564 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
566 $self->_date(time) unless $self->_date;
568 return "invalid discount_term"
569 if ($self->discount_term && $self->discount_term < 2);
571 if ( $self->payby eq 'CASH' and $conf->exists('require_cash_deposit_info') ) {
572 foreach (qw(bank depositor account teller)) {
573 return "$_ required" if $self->get($_) eq '';
577 #i guess not now, with cust_pay_pending, if we actually make it here, we _do_ want to record it
578 # # UNIQUE index should catch this too, without race conditions, but this
579 # # should give a better error message the other 99.9% of the time...
580 # if ( length($self->payunique)
581 # && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) {
582 # #well, it *could* be a better error message
583 # return "duplicate transaction".
584 # " - a payment with unique identifer ". $self->payunique.
591 =item send_receipt HASHREF | OPTION => VALUE ...
593 Sends a payment receipt for this payment..
601 Flag indicating the payment is being made manually.
605 Invoice (FS::cust_bill) object. If not specified, the most recent invoice
610 Customer (FS::cust_main) object (for efficiency).
618 my $opt = ref($_[0]) ? shift : { @_ };
620 my $cust_bill = $opt->{'cust_bill'};
621 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
623 my $conf = new FS::Conf;
625 return '' unless $conf->config_bool('payment_receipt', $cust_main->agentnum);
627 my @invoicing_list = $cust_main->invoicing_list_emailonly;
628 return '' unless @invoicing_list;
630 $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
634 if ( ( exists($opt->{'manual'}) && $opt->{'manual'} )
635 #|| ! $conf->exists('invoice_html_statement')
639 my $msgnum = $conf->config('payment_receipt_msgnum', $cust_main->agentnum);
642 my $queue = new FS::queue {
643 'job' => 'FS::Misc::process_send_email',
644 'paynum' => $self->paynum,
645 'custnum' => $cust_main->custnum,
647 $error = $queue->insert(
648 FS::msg_template->by_key($msgnum)->prepare(
649 'cust_main' => $cust_main,
651 'from_config' => 'payment_receipt_from',
653 'msgtype' => 'receipt', # override msg_template's default
656 } elsif ( $conf->exists('payment_receipt_email') ) {
658 my $receipt_template = new Text::Template (
660 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
662 warn "can't create payment receipt template: $Text::Template::ERROR";
666 my $payby = $self->payby;
667 my $payinfo = $self->payinfo;
668 $payby =~ s/^BILL$/Check/ if $payinfo;
669 if ( $payby eq 'CARD' || $payby eq 'CHEK' ) {
670 $payinfo = $self->paymask
672 $payinfo = $self->decrypt($payinfo);
674 $payby =~ s/^CHEK$/Electronic check/;
677 'date' => time2str("%a %B %o, %Y", $self->_date),
678 'name' => $cust_main->name,
679 'paynum' => $self->paynum,
680 'paid' => sprintf("%.2f", $self->paid),
681 'payby' => ucfirst(lc($payby)),
682 'payinfo' => $payinfo,
683 'balance' => $cust_main->balance,
684 'company_name' => $conf->config('company_name', $cust_main->agentnum),
687 if ( $opt->{'cust_pkg'} ) {
688 $fill_in{'pkg'} = $opt->{'cust_pkg'}->part_pkg->pkg;
689 #setup date, other things?
692 my $queue = new FS::queue {
693 'job' => 'FS::Misc::process_send_generated_email',
694 'paynum' => $self->paynum,
695 'custnum' => $cust_main->custnum,
696 'msgtype' => 'receipt',
698 $error = $queue->insert(
699 'from' => $conf->config('invoice_from', $cust_main->agentnum),
700 #invoice_from??? well as good as any
701 'to' => \@invoicing_list,
702 'subject' => 'Payment receipt',
703 'body' => [ $receipt_template->fill_in( HASH => \%fill_in ) ],
708 warn "payment_receipt is on, but no payment_receipt_msgnum\n";
712 } elsif ( ! $cust_main->invoice_noemail ) { #not manual
714 my $queue = new FS::queue {
715 'job' => 'FS::cust_bill::queueable_email',
716 'paynum' => $self->paynum,
717 'custnum' => $cust_main->custnum,
720 $error = $queue->insert(
721 'invnum' => $cust_bill->invnum,
722 'template' => 'statement',
723 'notice_name' => 'Statement',
729 warn "send_receipt: $error\n" if $error;
734 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
741 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
742 sort { $a->_date <=> $b->_date
743 || $a->invnum <=> $b->invnum }
744 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
748 =item cust_pay_refund
750 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
755 sub cust_pay_refund {
757 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
758 sort { $a->_date <=> $b->_date }
759 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
766 Returns the amount of this payment that is still unapplied; which is
767 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
768 applications (see L<FS::cust_pay_refund>).
774 my $amount = $self->paid;
775 $amount -= $_->amount foreach ( $self->cust_bill_pay );
776 $amount -= $_->amount foreach ( $self->cust_pay_refund );
777 sprintf("%.2f", $amount );
782 Returns the amount of this payment that has not been refuned; which is
783 paid minus all refund applications (see L<FS::cust_pay_refund>).
789 my $amount = $self->paid;
790 $amount -= $_->amount foreach ( $self->cust_pay_refund );
791 sprintf("%.2f", $amount );
796 Returns the "paid" field.
811 =item batch_insert CUST_PAY_OBJECT, ...
813 Class method which inserts multiple payments. Takes a list of FS::cust_pay
814 objects. Returns a list, each element representing the status of inserting the
815 corresponding payment - empty. If there is an error inserting any payment, the
816 entire transaction is rolled back, i.e. all payments are inserted or none are.
818 FS::cust_pay objects may have the pseudo-field 'apply_to', containing a
819 reference to an array of (uninserted) FS::cust_bill_pay objects. If so,
820 those objects will be inserted with the paynum of the payment, and for
821 each one, an error message or an empty string will be inserted into the
826 my @errors = FS::cust_pay->batch_insert(@cust_pay);
827 my $num_errors = scalar(grep $_, @errors);
828 if ( $num_errors == 0 ) {
829 #success; all payments were inserted
831 #failure; no payments were inserted.
837 my $self = shift; #class method
839 local $SIG{HUP} = 'IGNORE';
840 local $SIG{INT} = 'IGNORE';
841 local $SIG{QUIT} = 'IGNORE';
842 local $SIG{TERM} = 'IGNORE';
843 local $SIG{TSTP} = 'IGNORE';
844 local $SIG{PIPE} = 'IGNORE';
846 my $oldAutoCommit = $FS::UID::AutoCommit;
847 local $FS::UID::AutoCommit = 0;
853 foreach my $cust_pay (@_) {
854 my $error = $cust_pay->insert( 'manual' => 1 );
855 push @errors, $error;
856 $num_errors++ if $error;
858 if ( ref($cust_pay->get('apply_to')) eq 'ARRAY' ) {
860 foreach my $cust_bill_pay ( @{ $cust_pay->apply_to } ) {
861 if ( $error ) { # insert placeholders if cust_pay wasn't inserted
865 $cust_bill_pay->set('paynum', $cust_pay->paynum);
866 my $apply_error = $cust_bill_pay->insert;
867 push @errors, $apply_error || '';
868 $num_errors++ if $apply_error;
872 } elsif ( !$error ) { #normal case: apply payments as usual
873 $cust_pay->cust_main->apply_payments;
879 $dbh->rollback if $oldAutoCommit;
881 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
890 Returns an SQL fragment to retreive the unapplied amount.
895 my ($class, $start, $end) = @_;
896 my $bill_start = $start ? "AND cust_bill_pay._date <= $start" : '';
897 my $bill_end = $end ? "AND cust_bill_pay._date > $end" : '';
898 my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
899 my $refund_end = $end ? "AND cust_pay_refund._date > $end" : '';
903 ( SELECT SUM(amount) FROM cust_bill_pay
904 WHERE cust_pay.paynum = cust_bill_pay.paynum
905 $bill_start $bill_end )
909 ( SELECT SUM(amount) FROM cust_pay_refund
910 WHERE cust_pay.paynum = cust_pay_refund.paynum
911 $refund_start $refund_end )
920 # Used by FS::Upgrade to migrate to a new database.
924 sub _upgrade_data { #class method
925 my ($class, %opts) = @_;
927 warn "$me upgrading $class\n" if $DEBUG;
929 local $FS::payinfo_Mixin::ignore_masked_payinfo = 1;
932 # otaker/ivan upgrade
935 unless ( FS::upgrade_journal->is_done('cust_pay__otaker_ivan') ) {
937 #not the most efficient, but hey, it only has to run once
939 my $where = "WHERE ( otaker IS NULL OR otaker = '' OR otaker = 'ivan' ) ".
940 " AND usernum IS NULL ".
941 " AND 0 < ( SELECT COUNT(*) FROM cust_main ".
942 " WHERE cust_main.custnum = cust_pay.custnum ) ";
944 my $count_sql = "SELECT COUNT(*) FROM cust_pay $where";
946 my $sth = dbh->prepare($count_sql) or die dbh->errstr;
947 $sth->execute or die $sth->errstr;
948 my $total = $sth->fetchrow_arrayref->[0];
949 #warn "$total cust_pay records to update\n"
951 local($DEBUG) = 2 if $total > 1000; #could be a while, force progress info
956 my @cust_pay = qsearch( {
957 'table' => 'cust_pay',
959 'extra_sql' => $where,
960 'order_by' => 'ORDER BY paynum',
963 foreach my $cust_pay (@cust_pay) {
965 my $h_cust_pay = $cust_pay->h_search('insert');
967 next if $cust_pay->otaker eq $h_cust_pay->history_user;
968 #$cust_pay->otaker($h_cust_pay->history_user);
969 $cust_pay->set('otaker', $h_cust_pay->history_user);
971 $cust_pay->set('otaker', 'legacy');
974 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
975 my $error = $cust_pay->replace;
978 warn " *** WARNING: Error updating order taker for payment paynum ".
979 $cust_pay->paynun. ": $error\n";
983 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
986 if ( $DEBUG > 1 && $lastprog + 30 < time ) {
987 warn "$me $count/$total (".sprintf('%.2f',100*$count/$total). '%)'."\n";
993 FS::upgrade_journal->set_done('cust_pay__otaker_ivan');
997 # payinfo N/A upgrade
1000 unless ( FS::upgrade_journal->is_done('cust_pay__payinfo_na') ) {
1002 #XXX remove the 'N/A (tokenized)' part (or just this entire thing)
1004 my @na_cust_pay = qsearch( {
1005 'table' => 'cust_pay',
1006 'hashref' => {}, #could be encrypted# { 'payinfo' => 'N/A' },
1007 'extra_sql' => "WHERE ( payinfo = 'N/A' OR paymask = 'N/AA' OR paymask = 'N/A (tokenized)' ) AND payby IN ( 'CARD', 'CHEK' )",
1010 foreach my $na ( @na_cust_pay ) {
1012 next unless $na->payinfo eq 'N/A';
1014 my $cust_pay_pending =
1015 qsearchs('cust_pay_pending', { 'paynum' => $na->paynum } );
1016 unless ( $cust_pay_pending ) {
1017 warn " *** WARNING: not-yet recoverable N/A card for payment ".
1018 $na->paynum. " (no cust_pay_pending)\n";
1021 $na->$_($cust_pay_pending->$_) for qw( payinfo paymask );
1022 my $error = $na->replace;
1024 warn " *** WARNING: Error updating payinfo for payment paynum ".
1025 $na->paynun. ": $error\n";
1031 FS::upgrade_journal->set_done('cust_pay__payinfo_na');
1035 # otaker->usernum upgrade
1038 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
1039 $class->_upgrade_otaker(%opts);
1040 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
1043 # migrate batchnums from the misused 'paybatch' field to 'batchnum'
1045 my $search = FS::Cursor->new( {
1046 'table' => 'cust_pay',
1047 'addl_from' => ' JOIN pay_batch ON cust_pay.paybatch = CAST(pay_batch.batchnum AS text) ',
1049 while (my $cust_pay = $search->fetch) {
1050 $cust_pay->set('batchnum' => $cust_pay->paybatch);
1051 $cust_pay->set('paybatch' => '');
1052 my $error = $cust_pay->replace;
1053 warn "error setting batchnum on cust_pay #".$cust_pay->paynum.":\n $error"
1058 # migrate gateway info from the misused 'paybatch' field
1061 # not only cust_pay, but also voided and refunded payments
1062 if (!FS::upgrade_journal->is_done('cust_pay__parse_paybatch_1')) {
1063 local $FS::Record::nowarn_classload=1;
1064 # really inefficient, but again, only has to run once
1065 foreach my $table (qw(cust_pay cust_pay_void cust_refund)) {
1066 my $and_batchnum_is_null =
1067 ( $table =~ /^cust_pay/ ? ' AND batchnum IS NULL' : '' );
1068 my $search = FS::Cursor->new({
1070 extra_sql => "WHERE payby IN('CARD','CHEK') ".
1071 "AND (paybatch IS NOT NULL ".
1072 "OR (paybatch IS NULL AND auth IS NULL
1073 $and_batchnum_is_null ) )",
1075 while ( my $object = $search->fetch ) {
1076 if ( $object->paybatch eq '' ) {
1077 # repair for a previous upgrade that didn't save 'auth'
1078 my $pkey = $object->primary_key;
1079 # find the last history record that had a paybatch value
1081 table => "h_$table",
1083 $pkey => $object->$pkey,
1084 paybatch => { op=>'!=', value=>''},
1085 history_action => 'replace_old',
1087 order_by => 'ORDER BY history_date DESC LIMIT 1',
1090 warn "couldn't find paybatch history record for $table ".$object->$pkey."\n";
1093 # if the paybatch didn't have an auth string, then it's fine
1094 $h->paybatch =~ /:(\w+):/ or next;
1095 # set paybatch to what it was in that record
1096 $object->set('paybatch', $h->paybatch)
1097 # and then upgrade it like the old records
1100 my $parsed = $object->_parse_paybatch;
1101 if (keys %$parsed) {
1102 $object->set($_ => $parsed->{$_}) foreach keys %$parsed;
1103 $object->set('auth' => $parsed->{authorization});
1104 $object->set('paybatch', '');
1105 my $error = $object->replace;
1106 warn "error parsing CARD/CHEK paybatch fields on $object #".
1107 $object->get($object->primary_key).":\n $error\n"
1112 FS::upgrade_journal->set_done('cust_pay__parse_paybatch_1');
1122 =item batch_import HASHREF
1124 Inserts new payments.
1131 my $fh = $param->{filehandle};
1132 my $format = $param->{'format'};
1134 my $agentnum = $param->{agentnum};
1135 my $_date = $param->{_date};
1136 $_date = parse_datetime($_date) if $_date && $_date =~ /\D/;
1137 my $paybatch = $param->{'paybatch'};
1139 my $custnum_prefix = $conf->config('cust_main-custnum-display_prefix');
1140 my $custnum_length = $conf->config('cust_main-custnum-display_length') || 8;
1142 # here is the agent virtualization
1143 my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
1147 if ( $format eq 'simple' ) {
1148 @fields = qw( custnum agent_custid paid payinfo );
1150 } elsif ( $format eq 'extended' ) {
1151 die "unimplemented\n";
1155 die "unknown format $format";
1158 eval "use Text::CSV_XS;";
1161 my $csv = new Text::CSV_XS;
1165 local $SIG{HUP} = 'IGNORE';
1166 local $SIG{INT} = 'IGNORE';
1167 local $SIG{QUIT} = 'IGNORE';
1168 local $SIG{TERM} = 'IGNORE';
1169 local $SIG{TSTP} = 'IGNORE';
1170 local $SIG{PIPE} = 'IGNORE';
1172 my $oldAutoCommit = $FS::UID::AutoCommit;
1173 local $FS::UID::AutoCommit = 0;
1177 while ( defined($line=<$fh>) ) {
1179 $csv->parse($line) or do {
1180 $dbh->rollback if $oldAutoCommit;
1181 return "can't parse: ". $csv->error_input();
1184 my @columns = $csv->fields();
1188 paybatch => $paybatch,
1190 $cust_pay{_date} = $_date if $_date;
1193 foreach my $field ( @fields ) {
1195 if ( $field eq 'agent_custid'
1197 && $columns[0] =~ /\S+/ )
1200 my $agent_custid = $columns[0];
1201 my %hash = ( 'agent_custid' => $agent_custid,
1202 'agentnum' => $agentnum,
1205 if ( $cust_pay{'custnum'} !~ /^\s*$/ ) {
1206 $dbh->rollback if $oldAutoCommit;
1207 return "can't specify custnum with agent_custid $agent_custid";
1210 $cust_main = qsearchs({
1211 'table' => 'cust_main',
1212 'hashref' => \%hash,
1213 'extra_sql' => $extra_sql,
1216 unless ( $cust_main ) {
1217 $dbh->rollback if $oldAutoCommit;
1218 return "can't find customer with agent_custid $agent_custid";
1222 $columns[0] = $cust_main->custnum;
1225 $cust_pay{$field} = shift @columns;
1228 if ( $custnum_prefix && $cust_pay{custnum} =~ /^$custnum_prefix(0*([1-9]\d*))$/
1229 && length($1) == $custnum_length ) {
1230 $cust_pay{custnum} = $2;
1233 my $cust_pay = new FS::cust_pay( \%cust_pay );
1234 my $error = $cust_pay->insert;
1237 $dbh->rollback if $oldAutoCommit;
1238 return "can't insert payment for $line: $error";
1241 if ( $format eq 'simple' ) {
1242 # include agentnum for less surprise?
1243 $cust_main = qsearchs({
1244 'table' => 'cust_main',
1245 'hashref' => { 'custnum' => $cust_pay->custnum },
1246 'extra_sql' => $extra_sql,
1250 unless ( $cust_main ) {
1251 $dbh->rollback if $oldAutoCommit;
1252 return "can't find customer to which payments apply at line: $line";
1255 $error = $cust_main->apply_payments_and_credits;
1257 $dbh->rollback if $oldAutoCommit;
1258 return "can't apply payments to customer for $line: $error";
1266 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1268 return "Empty file!" unless $imported;
1278 Delete and replace methods.
1282 L<FS::cust_pay_pending>, L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>,
1283 schema.html from the base documentation.