4 unless $FS::CurrentUser::CurrentUser->access_right('Post payment batch');
6 my $param = $cgi->Vars;
7 warn Dumper($param) if $DEBUG;
9 #my $paybatch = $param->{'paybatch'};
10 my $paybatch = time2str('webbatch-%Y/%m/%d-%T'. "-$$-". rand() * 2**32, time);
14 #while ( exists($param->{"custnum$row"}) ) {
15 for ( my $row = 0; exists($param->{"custnum$row"}); $row++ ) {
16 my $custnum = $param->{"custnum$row"};
18 if ( $custnum =~ /^(\d+)$/ and $1 <= 2147483647 ) {
19 $cust_main = qsearchs({
20 'table' => 'cust_main',
21 'hashref' => { 'custnum' => $1 },
22 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
25 if ( length($custnum) and !$cust_main ) { # not found, try agent_custid
26 $cust_main = qsearchs({
27 'table' => 'cust_main',
28 'hashref' => { 'agent_custid' => $custnum },
29 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
32 $custnum = $cust_main->custnum if $cust_main;
33 # if !$cust_main, then this will throw an error on batch_insert
35 my $cust_pay = new FS::cust_pay {
36 'custnum' => $custnum,
37 'paid' => $param->{"paid$row"},
39 'payinfo' => $param->{"payinfo$row"},
40 'discount_term' => $param->{"discount_term$row"},
41 'paybatch' => $paybatch,
43 if $param->{"custnum$row"}
44 || $param->{"paid$row"}
45 || $param->{"payinfo$row"};
49 # payment applications, if any
50 my @cust_bill_pay = ();
51 for ( my $app = 0; exists($param->{"invnum$row.$app"}); $app++ ) {
52 next if !$param->{"invnum$row.$app"};
53 push @cust_bill_pay, new FS::cust_bill_pay {
54 'invnum' => $param->{"invnum$row.$app"},
55 'amount' => $param->{"amount$row.$app"}
58 $cust_pay->set('apply_to', \@cust_bill_pay) if scalar(@cust_bill_pay) > 0;
60 push @cust_pay, $cust_pay;
64 my @errors = FS::cust_pay->batch_insert(@cust_pay);
65 my $num_errors = scalar(grep $_, @errors);
67 % if ( $num_errors ) {
69 % $cgi->param('error', "$num_errors error". ($num_errors>1 ? 's' : '').
70 % ' - Batch not processed, correct and resubmit'
74 % $cgi->param('error'. $erow++, shift @errors) while @errors;
76 % my $query = $m->scomp('/elements/create_uri_query');
78 <% $cgi->redirect($p."batch-cust_pay.html?$query")
84 <% $cgi->redirect(popurl(3). "search/cust_pay.html?magic=paybatch;paybatch=$paybatch") %>