a6b90ea74482c68208899eb85e81e79fd10e82c1
[freeside.git] / httemplate / misc / process / batch-cust_pay.cgi
1 %  die "access denied"
2 %    unless $FS::CurrentUser::CurrentUser->access_right('Post payment batch');
3 %
4 %  my $param = $cgi->Vars;
5 %
6 %  #my $paybatch = $param->{'paybatch'};
7 %  my $paybatch = time2str('webbatch-%Y/%m/%d-%T'. "-$$-". rand() * 2**32, time);
8 %
9 %  my @cust_pay = ();
10 %  #my $row = 0;
11 %  #while ( exists($param->{"custnum$row"}) ) {
12 %  for ( my $row = 0; exists($param->{"custnum$row"}); $row++ ) {
13 %    my $custnum = $param->{"custnum$row"};
14 %    my $cust_main;
15 %    if ( $custnum =~ /^(\d+)$/ and $1 <= 2147483647 ) {
16 %      $cust_main = qsearchs({ 
17 %        'table'     => 'cust_main',
18 %        'hashref'   => { 'custnum' => $1 },
19 %        'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
20 %      });
21 %    }
22 %    if ( length($custnum) and !$cust_main ) { # not found, try agent_custid
23 %      $cust_main = qsearchs({ 
24 %        'table'     => 'cust_main',
25 %        'hashref'   => { 'agent_custid' => $custnum },
26 %        'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
27 %      });
28 %    }
29 %    $custnum = $cust_main->custnum if $cust_main;
30 %    # if !$cust_main, then this will throw an error on batch_insert
31 %
32 %    push @cust_pay, new FS::cust_pay {
33 %                      'custnum'        => $custnum,
34 %                      'paid'           => $param->{"paid$row"},
35 %                      'payby'          => 'BILL',
36 %                      'payinfo'        => $param->{"payinfo$row"},
37 %                      'discount_term'  => $param->{"discount_term$row"},
38 %                      'paybatch'       => $paybatch,
39 %                    }
40 %      if    $param->{"custnum$row"}
41 %         || $param->{"paid$row"}
42 %         || $param->{"payinfo$row"};
43 %    #$row++;
44 %  }
45 %
46 %  my @errors = FS::cust_pay->batch_insert(@cust_pay);
47 %  my $num_errors = scalar(grep $_, @errors);
48 %
49 %  if ( $num_errors ) {
50 %
51 %    $cgi->param('error', "$num_errors error". ($num_errors>1 ? 's' : '').
52 %                         ' - Batch not processed, correct and resubmit'
53 %               );
54 %
55 %    my $erow=0;
56 %    $cgi->param('error'. $erow++, shift @errors) while @errors;
57 %    
58 %    my $query = $m->scomp('/elements/create_uri_query');
59 %
60 <% $cgi->redirect($p."batch-cust_pay.html?$query")
61
62   %>
63 % } else {
64 %
65 %    
66 <% $cgi->redirect(popurl(3). "search/cust_pay.html?magic=paybatch;paybatch=$paybatch") %>
67 % } 
68