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