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