include elements/search.html the right way to avoid problems with XLS download, ...
[freeside.git] / httemplate / search / pay_batch.cgi
1 <& elements/search.html,
2                  'title'         => 'Payment Batches',
3                  'name_singular' => 'batch',
4                  'query'         => { 'table'     => 'pay_batch',
5                                       'hashref'   => $hashref,
6                                       'extra_sql' => $extra_sql,
7                                       'order_by'  => 'ORDER BY batchnum DESC',
8                                     },
9                  'count_query'   => "$count_query $extra_sql",
10                  'agent_virt'    => 1,
11                  'agent_null_right' => 'Process batches', #'Process global batches',
12                  'agent_pos'     => 1,
13                  'header'        => [ 'Batch',
14                                       'Type',
15                                       'First Download',
16                                       'Last Upload',
17                                       '', # requests
18                                       '', # req amt
19                                       '', # payments
20                                       '', # pay amt
21                                       'Status',
22                                     ],
23                  'align'         => 'rcllrrrrc',
24                  'fields'        => [ 'batchnum',
25                                       sub { 
26                                         FS::payby->shortname(shift->payby);
27                                       },
28                                       sub {
29                                         my $self = shift;
30                                         my $_date = $self->download;
31                                         if ( $_date ) {
32                                           time2str("%a %b %e %T %Y", $_date);
33                                         } elsif ( $self->status eq 'O' ) {
34                                           'Download batch';
35                                         } else {
36                                           '';
37                                         }
38                                       },
39                                       sub {
40                                         my $self = shift;
41                                         my $_date = $self->upload;
42                                         if ( $_date ) {
43                                           time2str("%a %b %e %T %Y", $_date);
44                                         } elsif ( $self->status eq 'I' ) {
45                                           'Upload results';
46                                         } else {
47                                           '';
48                                         }
49                                       },
50                                       sub {
51                                         my $c = FS::cust_pay_batch->count('batchnum = '.$_[0]->batchnum);
52                                         $c ? "$c requested" : ''
53                                       },
54                                       sub {
55                                         my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=" . shift->batchnum;
56                                         my $sth = dbh->prepare($st)
57                                           or die dbh->errstr. "doing $st";
58                                         $sth->execute
59                                           or die "Error executing \"$st\": ". $sth->errstr;
60                                         my $total = $sth->fetchrow_arrayref->[0];
61                                         $total ? $money_char.sprintf('%.2f',$total) : '';
62                                       },
63                                       sub {
64                                         my $c = FS::cust_pay->count('batchnum = '.$_[0]->batchnum);
65                                         $c ? "$c paid" : ''
66                                       },
67                                       sub {
68                                         my $st = "SELECT SUM(paid) from cust_pay WHERE batchnum=" . shift->batchnum;
69                                         my $sth = dbh->prepare($st)
70                                           or die dbh->errstr. "doing $st";
71                                         $sth->execute
72                                           or die "Error executing \"$st\": ". $sth->errstr;
73                                         my $total = $sth->fetchrow_arrayref->[0];
74                                         $total ? $money_char.sprintf('%.2f',$total) : '';
75                                       },
76                                       sub {
77                                         $statusmap{shift->status};
78                                       },
79                                     ],
80                  'links'         => [
81                                       '',
82                                       '',
83                                       sub { shift->status eq 'O' ? $cpb_link : '' },
84                                       sub { shift->status eq 'I' ? $cpb_link : '' },
85                                       $cpb_link,
86                                       $cpb_link,
87                                       $pay_link,
88                                       $pay_link,
89                                     ],
90                  'size'         => [
91                                       '',
92                                       '',
93                                       sub { shift->status eq 'O' ? "+1" : '' },
94                                       sub { shift->status eq 'I' ? "+1" : '' },
95                                     ],
96                  'style'         => [
97                                       '',
98                                       '',
99                                       sub { shift->status eq 'O' ? "b" : '' },
100                                       sub { shift->status eq 'I' ? "b" : '' },
101                                     ],
102                  'html_init'     => $html_init,
103                  'html_foot'     => include('.upload_incoming'),
104 &>
105 <%def .upload_incoming>
106 % if ( FS::payment_gateway->count("gateway_namespace = 'Business::BatchPayment' AND disabled IS NULL") > 0 ) { 
107 <& /elements/form-file_upload.html,
108     name      => 'FileUpload',
109     action    => $p.'misc/upload-batch.cgi',
110     num_files => 1,
111     fields    => [ 'gatewaynum' ],
112     message   => 'Incoming batch uploaded.',
113 &>
114 <BR>
115 <BR>
116 Upload incoming batch from gateway 
117 <& /elements/select-table.html,
118     table       => 'payment_gateway',
119     field       => 'gatewaynum',
120     name_col    => 'label',
121     value_col   => 'gatewaynum',
122     order_by    => 'ORDER BY gatewaynum',
123     empty_label => ' ',
124     hashref     =>
125       { 'gateway_namespace' => 'Business::BatchPayment',
126         'disabled' => '' },
127 &>
128 <BR>
129 <& '/elements/file-upload.html',
130     field     => 'file',
131     label     => 'Filename',
132     no_table  => 1,
133 &>
134 <INPUT TYPE="submit" VALUE="Upload">
135 </FORM>
136 % }
137 </%def>
138 <%init>
139
140 die "access denied"
141   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
142       || $FS::CurrentUser::CurrentUser->access_right('Process batches');
143
144 my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
145 my $hashref = {};
146 my $count_query = 'SELECT COUNT(*) FROM pay_batch';
147
148 my($begin, $end) = ( '', '' );
149
150 my @where;
151 if ( $cgi->param('beginning')
152      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
153   $begin = parse_datetime($1);
154   push @where, "download >= $begin";
155 }
156 if ( $cgi->param('ending')
157       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
158   $end = parse_datetime($1) + 86399;
159   push @where, "download < $end";
160 }
161
162 my @status;
163 if ( $cgi->param('open') ) {
164   push @status, "O";
165 }
166
167 if ( $cgi->param('intransit') ) {
168   push @status, "I";
169 }
170
171 if ( $cgi->param('resolved') ) {
172   push @status, "R";
173 }
174
175 push @where,
176      scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
177                      : q!status='X'!;  # kludgy, X is unused at present
178
179 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
180
181 my $cpb_link = [ "${p}search/cust_pay_batch.cgi?dcln=1;batchnum=", 'batchnum' ];
182 my $pay_link = [ "${p}search/cust_pay.html?magic=batchnum;batchnum=", 'batchnum' ];
183
184 my $resolved = $cgi->param('resolved') || 0;
185 $cgi->param('resolved' => !$resolved);
186 my $html_init = '<A HREF="' . $cgi->self_url . '"><I>'.
187     ($resolved ? 'Hide' : 'Show') . ' resolved batches</I></A><BR>';
188
189 my $money_char = FS::Conf->new->config('money_char') || '$';
190
191 </%init>