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;
28 $me = '[FS::cust_pay]';
32 #ask FS::UID to run this stuff for us later
33 FS::UID->install_callback( sub {
35 $unsuspendauto = $conf->exists('unsuspendauto');
38 @encrypted_fields = ('payinfo');
42 FS::cust_pay - Object methods for cust_pay objects
48 $record = new FS::cust_pay \%hash;
49 $record = new FS::cust_pay { 'column' => 'value' };
51 $error = $record->insert;
53 $error = $new_record->replace($old_record);
55 $error = $record->delete;
57 $error = $record->check;
61 An FS::cust_pay object represents a payment; the transfer of money from a
62 customer. FS::cust_pay inherits from FS::Record. The following fields are
69 primary key (assigned automatically for new payments)
73 customer (see L<FS::cust_main>)
77 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
78 L<Time::Local> and L<Date::Parse> for conversion functions.
82 Amount of this payment
86 order taker (see L<FS::access_user>)
90 Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
94 Payment Information (See L<FS::payinfo_Mixin> for data format)
98 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
102 text field for tracking card processing or other batch grouping
106 Optional unique identifer to prevent duplicate transactions.
110 books closed flag, empty or `Y'
114 Desired pkgnum when using experimental package balances.
124 Creates a new payment. To add the payment to the databse, see L<"insert">.
128 sub table { 'cust_pay'; }
129 sub cust_linked { $_[0]->cust_main_custnum; }
130 sub cust_unlinked_msg {
132 "WARNING: can't find cust_main.custnum ". $self->custnum.
133 ' (cust_pay.paynum '. $self->paynum. ')';
136 =item insert [ OPTION => VALUE ... ]
138 Adds this payment to the database.
140 For backwards-compatibility and convenience, if the additional field invnum
141 is defined, an FS::cust_bill_pay record for the full amount of the payment
142 will be created. In this case, custnum is optional.
144 If the additional field discount_term is defined then a prepayment discount
145 is taken for that length of time. It is an error for the customer to owe
146 after this payment is made.
148 A hash of optional arguments may be passed. Currently "manual" is supported.
149 If true, a payment receipt is sent instead of a statement when
150 'payment_receipt_email' configuration option is set.
155 my($self, %options) = @_;
157 local $SIG{HUP} = 'IGNORE';
158 local $SIG{INT} = 'IGNORE';
159 local $SIG{QUIT} = 'IGNORE';
160 local $SIG{TERM} = 'IGNORE';
161 local $SIG{TSTP} = 'IGNORE';
162 local $SIG{PIPE} = 'IGNORE';
164 my $oldAutoCommit = $FS::UID::AutoCommit;
165 local $FS::UID::AutoCommit = 0;
169 if ( $self->invnum ) {
170 $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } )
172 $dbh->rollback if $oldAutoCommit;
173 return "Unknown cust_bill.invnum: ". $self->invnum;
175 $self->custnum($cust_bill->custnum );
178 my $error = $self->check;
179 return $error if $error;
181 my $cust_main = $self->cust_main;
182 my $old_balance = $cust_main->balance;
184 $error = $self->SUPER::insert;
186 $dbh->rollback if $oldAutoCommit;
187 return "error inserting cust_pay: $error";
190 if ( my $credit_type = $conf->config('prepayment_discounts-credit_type') ) {
191 if ( my $months = $self->discount_term ) {
192 #hmmm... error handling
193 my ($credit, $savings, $total) =
194 $cust_main->discount_term_values($months);
195 my $cust_credit = new FS::cust_credit {
196 'custnum' => $self->custnum,
198 'reason' => 'customer chose to prepay for discount',
200 $error = $cust_credit->insert('reason_type' => $credit_type);
202 $dbh->rollback if $oldAutoCommit;
203 return "error inserting cust_pay: $error";
205 my @pkgs = $cust_main->_discount_pkgs_and_bill;
206 my $cust_bill = shift(@pkgs);
207 @pkgs = &FS::cust_main::Billing::_discountable_pkgs_at_term($months, @pkgs);
208 $_->bill($_->last_bill) foreach @pkgs;
209 $error = $cust_main->bill(
210 'recurring_only' => 1,
211 'time' => $cust_bill->invoice_date,
212 'no_usage_reset' => 1,
213 'pkg_list' => \@pkgs,
214 'freq_override' => $months,
217 $dbh->rollback if $oldAutoCommit;
218 return "error inserting cust_pay: $error";
220 $error = $cust_main->apply_payments_and_credits;
222 $dbh->rollback if $oldAutoCommit;
223 return "error inserting cust_pay: $error";
225 my $new_balance = $cust_main->balance;
226 if ($new_balance > 0) {
227 $dbh->rollback if $oldAutoCommit;
228 return "balance after prepay discount attempt: $new_balance";
235 if ( $self->invnum ) {
236 my $cust_bill_pay = new FS::cust_bill_pay {
237 'invnum' => $self->invnum,
238 'paynum' => $self->paynum,
239 'amount' => $self->paid,
240 '_date' => $self->_date,
242 $error = $cust_bill_pay->insert(%options);
244 if ( $ignore_noapply ) {
245 warn "warning: error inserting cust_bill_pay: $error ".
246 "(ignore_noapply flag set; inserting cust_pay record anyway)\n";
248 $dbh->rollback if $oldAutoCommit;
249 return "error inserting cust_bill_pay: $error";
254 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
256 #false laziness w/ cust_credit::insert
257 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
258 my @errors = $cust_main->unsuspend;
260 # side-fx with nested transactions? upstack rolls back?
261 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
267 #bill setup fees for voip_cdr bill_every_call packages
268 #some false laziness w/search in freeside-cdrd
270 'LEFT JOIN part_pkg USING ( pkgpart ) '.
271 "LEFT JOIN part_pkg_option
272 ON ( cust_pkg.pkgpart = part_pkg_option.pkgpart
273 AND part_pkg_option.optionname = 'bill_every_call' )";
275 my $extra_sql = " AND plan = 'voip_cdr' AND optionvalue = '1' ".
276 " AND ( cust_pkg.setup IS NULL OR cust_pkg.setup = 0 ) ";
278 my @cust_pkg = qsearch({
279 'table' => 'cust_pkg',
280 'addl_from' => $addl_from,
281 'hashref' => { 'custnum' => $self->custnum,
285 'extra_sql' => $extra_sql,
289 warn "voip_cdr bill_every_call packages found; billing customer\n";
290 my $bill_error = $self->cust_main->bill_and_collect( 'fatal' => 'return' );
292 warn "WARNING: Error billing customer: $bill_error\n";
295 #end of billing setup fees for voip_cdr bill_every_call packages
297 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
300 my $trigger = $conf->config('payment_receipt-trigger',
301 $self->cust_main->agentnum) || 'cust_pay';
302 if ( $trigger eq 'cust_pay' ) {
303 my $error = $self->send_receipt(
304 'manual' => $options{'manual'},
305 'cust_bill' => $cust_bill,
306 'cust_main' => $cust_main,
308 warn "can't send payment receipt/statement: $error" if $error;
315 =item void [ REASON ]
317 Voids this payment: deletes the payment and all associated applications and
318 adds a record of the voided payment to the FS::cust_pay_void table.
325 local $SIG{HUP} = 'IGNORE';
326 local $SIG{INT} = 'IGNORE';
327 local $SIG{QUIT} = 'IGNORE';
328 local $SIG{TERM} = 'IGNORE';
329 local $SIG{TSTP} = 'IGNORE';
330 local $SIG{PIPE} = 'IGNORE';
332 my $oldAutoCommit = $FS::UID::AutoCommit;
333 local $FS::UID::AutoCommit = 0;
336 my $cust_pay_void = new FS::cust_pay_void ( {
337 map { $_ => $self->get($_) } $self->fields
339 $cust_pay_void->reason(shift) if scalar(@_);
340 my $error = $cust_pay_void->insert;
342 $dbh->rollback if $oldAutoCommit;
346 $error = $self->delete;
348 $dbh->rollback if $oldAutoCommit;
352 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
360 Unless the closed flag is set, deletes this payment and all associated
361 applications (see L<FS::cust_bill_pay> and L<FS::cust_pay_refund>). In most
362 cases, you want to use the void method instead to leave a record of the
367 # very similar to FS::cust_credit::delete
370 return "Can't delete closed payment" if $self->closed =~ /^Y/i;
372 local $SIG{HUP} = 'IGNORE';
373 local $SIG{INT} = 'IGNORE';
374 local $SIG{QUIT} = 'IGNORE';
375 local $SIG{TERM} = 'IGNORE';
376 local $SIG{TSTP} = 'IGNORE';
377 local $SIG{PIPE} = 'IGNORE';
379 my $oldAutoCommit = $FS::UID::AutoCommit;
380 local $FS::UID::AutoCommit = 0;
383 foreach my $app ( $self->cust_bill_pay, $self->cust_pay_refund ) {
384 my $error = $app->delete;
386 $dbh->rollback if $oldAutoCommit;
391 my $error = $self->SUPER::delete(@_);
393 $dbh->rollback if $oldAutoCommit;
397 if ( $conf->exists('deletepayments')
398 && $conf->config('deletepayments') ne '' ) {
400 my $cust_main = $self->cust_main;
402 my $error = send_email(
403 'from' => $conf->config('invoice_from', $self->cust_main->agentnum),
404 #invoice_from??? well as good as any
405 'to' => $conf->config('deletepayments'),
406 'subject' => 'FREESIDE NOTIFICATION: Payment deleted',
408 "This is an automatic message from your Freeside installation\n",
409 "informing you that the following payment has been deleted:\n",
411 'paynum: '. $self->paynum. "\n",
412 'custnum: '. $self->custnum.
413 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
414 'paid: $'. sprintf("%.2f", $self->paid). "\n",
415 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
416 'payby: '. $self->payby. "\n",
417 'payinfo: '. $self->paymask. "\n",
418 'paybatch: '. $self->paybatch. "\n",
423 $dbh->rollback if $oldAutoCommit;
424 return "can't send payment deletion notification: $error";
429 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
435 =item replace [ OLD_RECORD ]
437 You can, but probably shouldn't modify payments...
439 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
440 supplied, replaces this record. If there is an error, returns the error,
441 otherwise returns false.
447 return "Can't modify closed payment" if $self->closed =~ /^Y/i;
448 $self->SUPER::replace(@_);
453 Checks all fields to make sure this is a valid payment. If there is an error,
454 returns the error, otherwise returns false. Called by the insert method.
461 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
464 $self->ut_numbern('paynum')
465 || $self->ut_numbern('custnum')
466 || $self->ut_numbern('_date')
467 || $self->ut_money('paid')
468 || $self->ut_alphan('otaker')
469 || $self->ut_textn('paybatch')
470 || $self->ut_textn('payunique')
471 || $self->ut_enum('closed', [ '', 'Y' ])
472 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
473 || $self->payinfo_check()
474 || $self->ut_numbern('discount_term')
476 return $error if $error;
478 return "paid must be > 0 " if $self->paid <= 0;
480 return "unknown cust_main.custnum: ". $self->custnum
482 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
484 $self->_date(time) unless $self->_date;
486 return "invalid discount_term"
487 if ($self->discount_term && $self->discount_term < 2);
489 #i guess not now, with cust_pay_pending, if we actually make it here, we _do_ want to record it
490 # # UNIQUE index should catch this too, without race conditions, but this
491 # # should give a better error message the other 99.9% of the time...
492 # if ( length($self->payunique)
493 # && qsearchs('cust_pay', { 'payunique' => $self->payunique } ) ) {
494 # #well, it *could* be a better error message
495 # return "duplicate transaction".
496 # " - a payment with unique identifer ". $self->payunique.
503 =item send_receipt HASHREF | OPTION => VALUE ...
505 Sends a payment receipt for this payment..
513 Flag indicating the payment is being made manually.
517 Invoice (FS::cust_bill) object. If not specified, the most recent invoice
522 Customer (FS::cust_main) object (for efficiency).
530 my $opt = ref($_[0]) ? shift : { @_ };
532 my $cust_bill = $opt->{'cust_bill'};
533 my $cust_main = $opt->{'cust_main'} || $self->cust_main;
535 my $conf = new FS::Conf;
537 return '' unless $conf->exists('payment_receipt', $cust_main->agentnum);
539 my @invoicing_list = $cust_main->invoicing_list_emailonly;
540 return '' unless @invoicing_list;
542 $cust_bill ||= ($cust_main->cust_bill)[-1]; #rather inefficient though?
546 if ( ( exists($opt->{'manual'}) && $opt->{'manual'} )
547 #|| ! $conf->exists('invoice_html_statement')
551 my $msgnum = $conf->config('payment_receipt_msgnum', $cust_main->agentnum);
553 my $msg_template = FS::msg_template->by_key($msgnum);
554 $error = $msg_template->send('cust_main'=> $cust_main, 'object'=> $self);
556 } elsif ( $conf->exists('payment_receipt_email') ) {
558 my $receipt_template = new Text::Template (
560 SOURCE => [ map "$_\n", $conf->config('payment_receipt_email') ],
562 warn "can't create payment receipt template: $Text::Template::ERROR";
566 my $payby = $self->payby;
567 my $payinfo = $self->payinfo;
568 $payby =~ s/^BILL$/Check/ if $payinfo;
569 if ( $payby eq 'CARD' || $payby eq 'CHEK' ) {
570 $payinfo = $self->paymask
572 $payinfo = $self->decrypt($payinfo);
574 $payby =~ s/^CHEK$/Electronic check/;
577 'date' => time2str("%a %B %o, %Y", $self->_date),
578 'name' => $cust_main->name,
579 'paynum' => $self->paynum,
580 'paid' => sprintf("%.2f", $self->paid),
581 'payby' => ucfirst(lc($payby)),
582 'payinfo' => $payinfo,
583 'balance' => $cust_main->balance,
584 'company_name' => $conf->config('company_name', $cust_main->agentnum),
587 if ( $opt->{'cust_pkg'} ) {
588 $fill_in{'pkg'} = $opt->{'cust_pkg'}->part_pkg->pkg;
589 #setup date, other things?
593 'from' => $conf->config('invoice_from', $cust_main->agentnum),
594 #invoice_from??? well as good as any
595 'to' => \@invoicing_list,
596 'subject' => 'Payment receipt',
597 'body' => [ $receipt_template->fill_in( HASH => \%fill_in ) ],
602 warn "payment_receipt is on, but no payment_receipt_msgnum\n";
608 my $queue = new FS::queue {
609 'paynum' => $self->paynum,
610 'job' => 'FS::cust_bill::queueable_email',
613 $error = $queue->insert(
614 'invnum' => $cust_bill->invnum,
615 'template' => 'statement',
616 'notice_name' => 'Statement',
622 warn "send_receipt: $error\n" if $error;
627 Returns all applications to invoices (see L<FS::cust_bill_pay>) for this
634 map { $_ } #return $self->num_cust_bill_pay unless wantarray;
635 sort { $a->_date <=> $b->_date
636 || $a->invnum <=> $b->invnum }
637 qsearch( 'cust_bill_pay', { 'paynum' => $self->paynum } )
641 =item cust_pay_refund
643 Returns all applications of refunds (see L<FS::cust_pay_refund>) to this
648 sub cust_pay_refund {
650 map { $_ } #return $self->num_cust_pay_refund unless wantarray;
651 sort { $a->_date <=> $b->_date }
652 qsearch( 'cust_pay_refund', { 'paynum' => $self->paynum } )
659 Returns the amount of this payment that is still unapplied; which is
660 paid minus all payment applications (see L<FS::cust_bill_pay>) and refund
661 applications (see L<FS::cust_pay_refund>).
667 my $amount = $self->paid;
668 $amount -= $_->amount foreach ( $self->cust_bill_pay );
669 $amount -= $_->amount foreach ( $self->cust_pay_refund );
670 sprintf("%.2f", $amount );
675 Returns the amount of this payment that has not been refuned; which is
676 paid minus all refund applications (see L<FS::cust_pay_refund>).
682 my $amount = $self->paid;
683 $amount -= $_->amount foreach ( $self->cust_pay_refund );
684 sprintf("%.2f", $amount );
689 Returns the "paid" field.
704 =item batch_insert CUST_PAY_OBJECT, ...
706 Class method which inserts multiple payments. Takes a list of FS::cust_pay
707 objects. Returns a list, each element representing the status of inserting the
708 corresponding payment - empty. If there is an error inserting any payment, the
709 entire transaction is rolled back, i.e. all payments are inserted or none are.
713 my @errors = FS::cust_pay->batch_insert(@cust_pay);
714 my $num_errors = scalar(grep $_, @errors);
715 if ( $num_errors == 0 ) {
716 #success; all payments were inserted
718 #failure; no payments were inserted.
724 my $self = shift; #class method
726 local $SIG{HUP} = 'IGNORE';
727 local $SIG{INT} = 'IGNORE';
728 local $SIG{QUIT} = 'IGNORE';
729 local $SIG{TERM} = 'IGNORE';
730 local $SIG{TSTP} = 'IGNORE';
731 local $SIG{PIPE} = 'IGNORE';
733 my $oldAutoCommit = $FS::UID::AutoCommit;
734 local $FS::UID::AutoCommit = 0;
740 my $error = $_->insert( 'manual' => 1 );
744 $_->cust_main->apply_payments;
750 $dbh->rollback if $oldAutoCommit;
752 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
761 Returns an SQL fragment to retreive the unapplied amount.
766 my ($class, $start, $end) = @_;
767 my $bill_start = $start ? "AND cust_bill_pay._date <= $start" : '';
768 my $bill_end = $end ? "AND cust_bill_pay._date > $end" : '';
769 my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
770 my $refund_end = $end ? "AND cust_pay_refund._date > $end" : '';
774 ( SELECT SUM(amount) FROM cust_bill_pay
775 WHERE cust_pay.paynum = cust_bill_pay.paynum
776 $bill_start $bill_end )
780 ( SELECT SUM(amount) FROM cust_pay_refund
781 WHERE cust_pay.paynum = cust_pay_refund.paynum
782 $refund_start $refund_end )
791 # Used by FS::Upgrade to migrate to a new database.
795 sub _upgrade_data { #class method
796 my ($class, %opts) = @_;
798 warn "$me upgrading $class\n" if $DEBUG;
801 # otaker/ivan upgrade
804 #not the most efficient, but hey, it only has to run once
806 my $where = "WHERE ( otaker IS NULL OR otaker = '' OR otaker = 'ivan' ) ".
807 " AND usernum IS NULL ".
808 " AND 0 < ( SELECT COUNT(*) FROM cust_main ".
809 " WHERE cust_main.custnum = cust_pay.custnum ) ";
811 my $count_sql = "SELECT COUNT(*) FROM cust_pay $where";
813 my $sth = dbh->prepare($count_sql) or die dbh->errstr;
814 $sth->execute or die $sth->errstr;
815 my $total = $sth->fetchrow_arrayref->[0];
816 #warn "$total cust_pay records to update\n"
818 local($DEBUG) = 2 if $total > 1000; #could be a while, force progress info
823 my @cust_pay = qsearch( {
824 'table' => 'cust_pay',
826 'extra_sql' => $where,
827 'order_by' => 'ORDER BY paynum',
830 foreach my $cust_pay (@cust_pay) {
832 my $h_cust_pay = $cust_pay->h_search('insert');
834 next if $cust_pay->otaker eq $h_cust_pay->history_user;
835 #$cust_pay->otaker($h_cust_pay->history_user);
836 $cust_pay->set('otaker', $h_cust_pay->history_user);
838 $cust_pay->set('otaker', 'legacy');
841 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
842 my $error = $cust_pay->replace;
845 warn " *** WARNING: Error updating order taker for payment paynum ".
846 $cust_pay->paynun. ": $error\n";
850 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
853 if ( $DEBUG > 1 && $lastprog + 30 < time ) {
854 warn "$me $count/$total (". sprintf('%.2f',100*$count/$total). '%)'. "\n";
861 # payinfo N/A upgrade
864 #XXX remove the 'N/A (tokenized)' part (or just this entire thing)
866 my @na_cust_pay = qsearch( {
867 'table' => 'cust_pay',
868 'hashref' => {}, #could be encrypted# { 'payinfo' => 'N/A' },
869 'extra_sql' => "WHERE ( payinfo = 'N/A' OR paymask = 'N/AA' OR paymask = 'N/A (tokenized)' ) AND payby IN ( 'CARD', 'CHEK' )",
872 foreach my $na ( @na_cust_pay ) {
874 next unless $na->payinfo eq 'N/A';
876 my $cust_pay_pending =
877 qsearchs('cust_pay_pending', { 'paynum' => $na->paynum } );
878 unless ( $cust_pay_pending ) {
879 warn " *** WARNING: not-yet recoverable N/A card for payment ".
880 $na->paynum. " (no cust_pay_pending)\n";
883 $na->$_($cust_pay_pending->$_) for qw( payinfo paymask );
884 my $error = $na->replace;
886 warn " *** WARNING: Error updating payinfo for payment paynum ".
887 $na->paynun. ": $error\n";
894 # otaker->usernum upgrade
897 delete $FS::payby::hash{'COMP'}->{cust_pay}; #quelle kludge
898 $class->_upgrade_otaker(%opts);
899 $FS::payby::hash{'COMP'}->{cust_pay} = ''; #restore it
909 =item batch_import HASHREF
911 Inserts new payments.
918 my $fh = $param->{filehandle};
919 my $agentnum = $param->{agentnum};
920 my $format = $param->{'format'};
921 my $paybatch = $param->{'paybatch'};
923 # here is the agent virtualization
924 my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
928 if ( $format eq 'simple' ) {
929 @fields = qw( custnum agent_custid paid payinfo );
931 } elsif ( $format eq 'extended' ) {
932 die "unimplemented\n";
936 die "unknown format $format";
939 eval "use Text::CSV_XS;";
942 my $csv = new Text::CSV_XS;
946 local $SIG{HUP} = 'IGNORE';
947 local $SIG{INT} = 'IGNORE';
948 local $SIG{QUIT} = 'IGNORE';
949 local $SIG{TERM} = 'IGNORE';
950 local $SIG{TSTP} = 'IGNORE';
951 local $SIG{PIPE} = 'IGNORE';
953 my $oldAutoCommit = $FS::UID::AutoCommit;
954 local $FS::UID::AutoCommit = 0;
958 while ( defined($line=<$fh>) ) {
960 $csv->parse($line) or do {
961 $dbh->rollback if $oldAutoCommit;
962 return "can't parse: ". $csv->error_input();
965 my @columns = $csv->fields();
969 paybatch => $paybatch,
973 foreach my $field ( @fields ) {
975 if ( $field eq 'agent_custid'
977 && $columns[0] =~ /\S+/ )
980 my $agent_custid = $columns[0];
981 my %hash = ( 'agent_custid' => $agent_custid,
982 'agentnum' => $agentnum,
985 if ( $cust_pay{'custnum'} !~ /^\s*$/ ) {
986 $dbh->rollback if $oldAutoCommit;
987 return "can't specify custnum with agent_custid $agent_custid";
990 $cust_main = qsearchs({
991 'table' => 'cust_main',
993 'extra_sql' => $extra_sql,
996 unless ( $cust_main ) {
997 $dbh->rollback if $oldAutoCommit;
998 return "can't find customer with agent_custid $agent_custid";
1002 $columns[0] = $cust_main->custnum;
1005 $cust_pay{$field} = shift @columns;
1008 my $cust_pay = new FS::cust_pay( \%cust_pay );
1009 my $error = $cust_pay->insert;
1012 $dbh->rollback if $oldAutoCommit;
1013 return "can't insert payment for $line: $error";
1016 if ( $format eq 'simple' ) {
1017 # include agentnum for less surprise?
1018 $cust_main = qsearchs({
1019 'table' => 'cust_main',
1020 'hashref' => { 'custnum' => $cust_pay->custnum },
1021 'extra_sql' => $extra_sql,
1025 unless ( $cust_main ) {
1026 $dbh->rollback if $oldAutoCommit;
1027 return "can't find customer to which payments apply at line: $line";
1030 $error = $cust_main->apply_payments_and_credits;
1032 $dbh->rollback if $oldAutoCommit;
1033 return "can't apply payments to customer for $line: $error";
1041 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1043 return "Empty file!" unless $imported;
1053 Delete and replace methods.
1057 L<FS::cust_pay_pending>, L<FS::cust_bill_pay>, L<FS::cust_bill>, L<FS::Record>,
1058 schema.html from the base documentation.