default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / misc / process / batch-cust_pay.cgi
1 <%init>
2 my $DEBUG = 0;
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 my @invrows = grep(/^invnum\d+\.\d+$/, keys %$param);
16 foreach my $row ( map /^custnum(\d+)$/, keys %$param ) {
17   my $custnum = $param->{"custnum$row"};
18   my $cust_main;
19   if ( $custnum =~ /^(\d+)$/ and $1 <= 2147483647 ) {
20     $cust_main = qsearchs({ 
21       'table'     => 'cust_main',
22       'hashref'   => { 'custnum' => $1 },
23       'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
24     });
25   }
26   if ( length($custnum) and !$cust_main ) { # not found, try agent_custid
27     $cust_main = qsearchs({ 
28       'table'     => 'cust_main',
29       'hashref'   => { 'agent_custid' => $custnum },
30       'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
31     });
32   }
33   $custnum = $cust_main->custnum if $cust_main;
34   # if !$cust_main, then this will throw an error on batch_insert
35
36   my $_date = $param->{"_date$row"} ? parse_datetime($param->{"_date$row"}) : '';
37
38   my $cust_pay = new FS::cust_pay {
39                     'custnum'        => $custnum,
40                     'paid'           => $param->{"paid$row"},
41                     'payby'          => 'BILL',
42                     'payinfo'        => $param->{"payinfo$row"},
43                     '_date'          => $_date,
44                     'discount_term'  => $param->{"discount_term$row"},
45                     'paybatch'       => $paybatch,
46                     'no_auto_apply'  => exists($param->{"no_auto_apply$row"}) ? 'Y' : '',
47                   }
48     if    $param->{"custnum$row"}
49        || $param->{"paid$row"}
50        || $param->{"payinfo$row"};
51   next if !$cust_pay;
52   #$row++;
53
54   # payment applications, if any
55   my @cust_bill_pay = ();
56   foreach my $app ( sort {$a <=> $b} map /^invnum$row\.(\d+)$/, @invrows ) {
57 #  for ( my $app = 0; exists($param->{"invnum$row.$app"}); $app++ ) {
58     next if !$param->{"invnum$row.$app"};
59     push @cust_bill_pay, new FS::cust_bill_pay {
60                             'invnum'  => $param->{"invnum$row.$app"},
61                             'amount'  => $param->{"amount$row.$app"}
62                           };
63   }
64   $cust_pay->set('apply_to', \@cust_bill_pay) if scalar(@cust_bill_pay) > 0;
65
66   push @cust_pay, $cust_pay;
67
68 }
69
70 my @errors = FS::cust_pay->batch_insert(@cust_pay);
71 my $num_errors = scalar(grep $_, @errors);
72 </%init>
73 %  if ( $num_errors ) {
74 %
75 %    $cgi->param('error', "$num_errors error". ($num_errors>1 ? 's' : '').
76 %                         ' - Batch not processed, correct and resubmit'
77 %               );
78 %
79 %    my $erow=0;
80 %    $cgi->param('error'. $erow++, shift @errors) while @errors;
81 %    
82 %    my $query = $m->scomp('/elements/create_uri_query');
83 %
84 <% $cgi->redirect($p."batch-cust_pay.html?$query")
85
86   %>
87 % } else {
88 %
89 %    
90 <% $cgi->redirect(popurl(3). "search/cust_pay.html?magic=paybatch;paybatch=$paybatch") %>
91 % }