Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / pay_batch.cgi
1 <% include( '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 %>
106 <%def .upload_incoming>
107 % if ( FS::payment_gateway->count("gateway_namespace = 'Business::BatchPayment' AND disabled IS NULL") > 0 ) { 
108 <& /elements/form-file_upload.html,
109     name      => 'FileUpload',
110     action    => $p.'misc/upload-batch.cgi',
111     num_files => 1,
112     fields    => [ 'gatewaynum' ],
113     message   => 'Incoming batch uploaded.',
114 &>
115 <BR>
116 <BR>
117 Upload incoming batch from gateway 
118 <& /elements/select-table.html,
119     table       => 'payment_gateway',
120     field       => 'gatewaynum',
121     name_col    => 'label',
122     value_col   => 'gatewaynum',
123     order_by    => 'ORDER BY gatewaynum',
124     empty_label => ' ',
125     hashref     =>
126       { 'gateway_namespace' => 'Business::BatchPayment',
127         'disabled' => '' },
128 &>
129 <BR>
130 <& '/elements/file-upload.html',
131     field     => 'file',
132     label     => 'Filename',
133     no_table  => 1,
134 &>
135 <INPUT TYPE="submit" VALUE="Upload">
136 </FORM>
137 % }
138 </%def>
139 <%init>
140
141 die "access denied"
142   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
143       || $FS::CurrentUser::CurrentUser->access_right('Process batches');
144
145 my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
146 my $hashref = {};
147 my $count_query = 'SELECT COUNT(*) FROM pay_batch';
148
149 my($begin, $end) = ( '', '' );
150
151 my @where;
152 if ( $cgi->param('beginning')
153      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
154   $begin = parse_datetime($1);
155   push @where, "download >= $begin";
156 }
157 if ( $cgi->param('ending')
158       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
159   $end = parse_datetime($1) + 86399;
160   push @where, "download < $end";
161 }
162
163 my @status;
164 if ( $cgi->param('open') ) {
165   push @status, "O";
166 }
167
168 if ( $cgi->param('intransit') ) {
169   push @status, "I";
170 }
171
172 if ( $cgi->param('resolved') ) {
173   push @status, "R";
174 }
175
176 push @where,
177      scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
178                      : q!status='X'!;  # kludgy, X is unused at present
179
180 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
181
182 my $cpb_link = [ "${p}search/cust_pay_batch.cgi?dcln=1;batchnum=", 'batchnum' ];
183 my $pay_link = [ "${p}search/cust_pay.html?magic=batchnum;batchnum=", 'batchnum' ];
184
185 my $resolved = $cgi->param('resolved') || 0;
186 $cgi->param('resolved' => !$resolved);
187 my $html_init = '<A HREF="' . $cgi->self_url . '"><I>'.
188     ($resolved ? 'Hide' : 'Show') . ' resolved batches</I></A><BR>';
189
190 my $money_char = FS::Conf->new->config('money_char') || '$';
191
192 </%init>