manual control of quick payment application, #15861
[freeside.git] / httemplate / misc / process / batch-cust_pay.cgi
1 <%init>
2 my $DEBUG = 1;
3 die "access denied"
4   unless $FS::CurrentUser::CurrentUser->access_right('Post payment batch');
5
6 my $param = $cgi->Vars;
7 warn Dumper($param) if $DEBUG;
8
9 #my $paybatch = $param->{'paybatch'};
10 my $paybatch = time2str('webbatch-%Y/%m/%d-%T'. "-$$-". rand() * 2**32, time);
11
12 my @cust_pay = ();
13 #my $row = 0;
14 #while ( exists($param->{"custnum$row"}) ) {
15 for ( my $row = 0; exists($param->{"custnum$row"}); $row++ ) {
16   my $custnum = $param->{"custnum$row"};
17   my $cust_main;
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,
23     });
24   }
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,
30     });
31   }
32   $custnum = $cust_main->custnum if $cust_main;
33   # if !$cust_main, then this will throw an error on batch_insert
34
35   my $cust_pay = new FS::cust_pay {
36                     'custnum'        => $custnum,
37                     'paid'           => $param->{"paid$row"},
38                     'payby'          => 'BILL',
39                     'payinfo'        => $param->{"payinfo$row"},
40                     'discount_term'  => $param->{"discount_term$row"},
41                     'paybatch'       => $paybatch,
42                   }
43     if    $param->{"custnum$row"}
44        || $param->{"paid$row"}
45        || $param->{"payinfo$row"};
46   next if !$cust_pay;
47   #$row++;
48
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"}
56                           };
57   }
58   $cust_pay->set('apply_to', \@cust_bill_pay) if scalar(@cust_bill_pay) > 0;
59
60   push @cust_pay, $cust_pay;
61
62 }
63
64 my @errors = FS::cust_pay->batch_insert(@cust_pay);
65 my $num_errors = scalar(grep $_, @errors);
66 </%init>
67 %  if ( $num_errors ) {
68 %
69 %    $cgi->param('error', "$num_errors error". ($num_errors>1 ? 's' : '').
70 %                         ' - Batch not processed, correct and resubmit'
71 %               );
72 %
73 %    my $erow=0;
74 %    $cgi->param('error'. $erow++, shift @errors) while @errors;
75 %    
76 %    my $query = $m->scomp('/elements/create_uri_query');
77 %
78 <% $cgi->redirect($p."batch-cust_pay.html?$query")
79
80   %>
81 % } else {
82 %
83 %    
84 <% $cgi->redirect(popurl(3). "search/cust_pay.html?magic=paybatch;paybatch=$paybatch") %>
85 % }