default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / search / cust_pay_batch.cgi
1 <& elements/search.html,
2               'title'       => 'Batch payment details',
3               'name'        => 'batch details',
4               'query'       => $sql_query,
5               'count_query' => $count_query,
6               'html_init'   => $pay_batch ? $html_init : '',
7               'disable_download' => 1,
8               'header'      => [ '#',
9                                  'Inv #',
10                                  'Cust #',
11                                  'Customer',
12                                  'Card Name',
13                                  'Card',
14                                  'Exp',
15                                  'Amount',
16                                  'Status',
17                                  '', # delete link
18                                  '', # error_message
19                                ],
20               'fields'      => [  'paybatchnum',
21                                   'invnum',
22                                   'custnum',
23                                   sub { $_[0]->cust_main->name_short },
24                                   'payname',
25                                   'mask_payinfo',
26                                   sub {
27                                     return('') if $_[0]->payby ne 'CARD';
28                                     $_[0]->get('exp') =~ /^\d\d(\d\d)-(\d\d)/;
29                                     sprintf('%02d/%02d',$1,$2);
30                                   },
31                                   sub {
32                                     sprintf('%.02f', $_[0]->amount)
33                                   },
34                                   sub { $_[0]->display_status },
35                                   $sub_unbatch,
36                                   'error_message',
37                                 ],
38               'align'       => 'rrrlllcrllll',
39               'links'       => [ '',
40                                  ["${p}view/cust_bill.cgi?", 'invnum'],
41                                  (["${p}view/cust_main.cgi?", 'custnum']) x 2,
42                                ],
43               'link_onclicks' => [ ('') x 8,
44                                    $sub_receipt
45                                  ],
46 &>
47 <%init>
48
49 my $conf = new FS::Conf;
50
51 my $curuser = $FS::CurrentUser::CurrentUser;
52
53 die "access denied"
54   unless $curuser->access_right('Financial reports')
55       || $curuser->access_right('Process batches')
56       || $curuser->access_right('Process global batches')
57       || ( $cgi->param('custnum') 
58            && (    $conf->exists('batch-enable')
59                 || $conf->config('batch-enable_payby')
60               )
61          );
62
63 my( $count_query, $sql_query );
64 my $hashref = {};
65 my @search = ();
66 my $orderby = 'paybatchnum';
67
68 my( $pay_batch, $batchnum ) = ( '', '');
69 if ( $cgi->param('batchnum') && $cgi->param('batchnum') =~ /^(\d+)$/ ) {
70   push @search, "batchnum = $1";
71   $pay_batch = qsearchs('pay_batch', { 'batchnum' => $1 } );
72   die "Batch $1 not found!" unless $pay_batch;
73   $batchnum = $pay_batch->batchnum;
74 }
75
76 if ( $cgi->param('custnum') && $cgi->param('custnum') =~ /^(\d+)$/ ) {
77   push @search, "cust_pay_batch.custnum = $1";
78 }
79
80 if ( $cgi->param('status') && $cgi->param('status') =~ /^(\w)$/ ) {
81   push @search, "pay_batch.status = '$1'";
82 }
83
84 if ( $cgi->param('payby') ) {
85   $cgi->param('payby') =~ /^(CARD|CHEK)$/
86     or die "illegal payby " . $cgi->param('payby');
87
88   push @search, "cust_pay_batch.payby = '$1'";
89 }
90
91 if ( not $cgi->param('dcln') ) {
92   push @search, "cust_pay_batch.status IS DISTINCT FROM 'Approved'";
93 }
94
95 my ($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
96 unless ($pay_batch){
97   push @search, "pay_batch.upload >= $beginning" if ($beginning);
98   push @search, "pay_batch.upload <= $ending" if ($ending < 4294967295);#2^32-1
99   $orderby = "pay_batch.download,paybatchnum";
100 }
101
102 push @search, $curuser->agentnums_sql({ table => 'cust_main' });
103
104 push @search, $curuser->agentnums_sql({ table      => 'pay_batch',
105                                         null_right => 'Process global batches',
106                                      });
107
108 my $search = ' WHERE ' . join(' AND ', @search);
109
110 $count_query = 'SELECT COUNT(*) FROM cust_pay_batch ' .
111                   'LEFT JOIN cust_main USING ( custnum ) ' .
112                   'LEFT JOIN pay_batch USING ( batchnum )' .
113                   $search;
114
115 $sql_query = {
116   'table'     => 'cust_pay_batch',
117   'select'    => 'cust_pay_batch.*, cust_pay.paynum',
118   'hashref'   => {},
119   'addl_from' => 'LEFT JOIN pay_batch USING ( batchnum ) '.
120                  'LEFT JOIN cust_main USING ( custnum ) '.
121                  'LEFT JOIN cust_pay  USING ( batchnum, custnum ) ',
122   'extra_sql' => $search,
123   'order_by'  => "ORDER BY $orderby",
124 };
125
126 my $sub_receipt = sub {
127   my $paynum = shift->paynum or return '';
128   include('/elements/popup_link_onclick.html',
129     'action'  => $p.'view/cust_pay.html?link=popup;paynum='.$paynum,
130     'actionlabel' => emt('Payment Receipt'),
131   );
132 };
133
134 my $sub_unbatch = '';
135 if ( ($pay_batch && ($pay_batch->status eq 'O')) 
136   && ( $curuser->access_right('Process batches')
137        || $curuser->access_right('Process global batches') )
138 ) {
139   $sub_unbatch = sub {
140     my $self = shift;
141     return '' if $self->status; # sanity check, shouldn't happen
142     my $batchnum = $self->batchnum;
143     my $paybatchnum = $self->paybatchnum;
144     my $out = <<EOF;
145 <FORM name="delete_cust_pay_batch_$paybatchnum">
146 <INPUT TYPE="hidden" name="paybatchnum" value="$paybatchnum">
147 </FORM>
148 EOF
149     $out .= include('/elements/progress-init.html',
150               "delete_cust_pay_batch_$paybatchnum",
151               [ 'paybatchnum' ],
152               $p.'misc/process/delete-cust_pay_batch.cgi',
153               $p.'search/cust_pay_batch.cgi?' . $cgi->query_string,
154               "paybatchnum$paybatchnum",
155             );
156     my $onclick = 'if ( confirm(\'';
157     $onclick .= emt('Are you sure you want to delete batch payment ') . $self->paybatchnum;
158     $onclick .= emt(' from payment batch ') . $self->batchnum;
159     $onclick .= '\') ) { paybatchnum' . $paybatchnum . 'process() }';
160     return $out . '<A HREF="javascript:void(0)" ONCLICK="' . $onclick . '">delete</A>';
161   };
162 }
163
164 my $html_init = '';
165 if ( $pay_batch ) {
166   $html_init = include('elements/cust_pay_batch_top.html', 
167                     'pay_batch' => $pay_batch);
168 }
169 </%init>