Business::BatchPayment interface, #17373
[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                                       'Items',
18                                       'Unresolved',
19                                       'Amount',
20                                       'Status',
21                                     ],
22                  'align'         => 'rcllrrc',
23                  'fields'        => [ 'batchnum',
24                                       sub { 
25                                         FS::payby->shortname(shift->payby);
26                                       },
27                                       sub {
28                                         my $self = shift;
29                                         my $_date = $self->download;
30                                         if ( $_date ) {
31                                           time2str("%a %b %e %T %Y", $_date);
32                                         } elsif ( $self->status eq 'O' ) {
33                                           'Download batch';
34                                         } else {
35                                           '';
36                                         }
37                                       },
38                                       sub {
39                                         my $self = shift;
40                                         my $_date = $self->upload;
41                                         if ( $_date ) {
42                                           time2str("%a %b %e %T %Y", $_date);
43                                         } elsif ( $self->status eq 'I' ) {
44                                           'Upload results';
45                                         } else {
46                                           '';
47                                         }
48                                       },
49                                       sub {
50                                         FS::cust_pay_batch->count(
51                                           'batchnum = '.$_[0]->batchnum
52                                         )
53                                       },
54                                       sub {
55                                         FS::cust_pay_batch->count(
56                                           'status is null and batchnum = '.
57                                             $_[0]->batchnum
58                                         )
59                                       },
60                                       sub {
61                                         my $st = "SELECT SUM(amount) from cust_pay_batch WHERE batchnum=" . shift->batchnum;
62                                         my $sth = dbh->prepare($st)
63                                           or die dbh->errstr. "doing $st";
64                                         $sth->execute
65                                           or die "Error executing \"$st\": ". $sth->errstr;
66                                         $sth->fetchrow_arrayref->[0];
67                                       },
68                                       sub {
69                                         $statusmap{shift->status};
70                                       },
71                                     ],
72                  'links'         => [
73                                       $link,
74                                       '',
75                                       sub { shift->status eq 'O' ? $link : '' },
76                                       sub { shift->status eq 'I' ? $link : '' },
77                                     ],
78                  'size'         => [
79                                       '',
80                                       '',
81                                       sub { shift->status eq 'O' ? "+1" : '' },
82                                       sub { shift->status eq 'I' ? "+1" : '' },
83                                     ],
84                  'style'         => [
85                                       '',
86                                       '',
87                                       sub { shift->status eq 'O' ? "b" : '' },
88                                       sub { shift->status eq 'I' ? "b" : '' },
89                                     ],
90                  'html_init'     => $html_init,
91       )
92
93 %>
94 <%init>
95
96 die "access denied"
97   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
98       || $FS::CurrentUser::CurrentUser->access_right('Process batches');
99
100 my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
101 my $hashref = {};
102 my $count_query = 'SELECT COUNT(*) FROM pay_batch';
103
104 my($begin, $end) = ( '', '' );
105
106 my @where;
107 if ( $cgi->param('beginning')
108      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
109   $begin = parse_datetime($1);
110   push @where, "download >= $begin";
111 }
112 if ( $cgi->param('ending')
113       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
114   $end = parse_datetime($1) + 86399;
115   push @where, "download < $end";
116 }
117
118 my @status;
119 if ( $cgi->param('open') ) {
120   push @status, "O";
121 }
122
123 if ( $cgi->param('intransit') ) {
124   push @status, "I";
125 }
126
127 if ( $cgi->param('resolved') ) {
128   push @status, "R";
129 }
130
131 push @where,
132      scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
133                      : q!status='X'!;  # kludgy, X is unused at present
134
135 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
136
137 my $link = [ "${p}search/cust_pay_batch.cgi?dcln=1;batchnum=", 'batchnum' ];
138
139 my $resolved = $cgi->param('resolved') || 0;
140 $cgi->param('resolved' => !$resolved);
141 my $html_init = '<A HREF="' . $cgi->self_url . '"><I>'.
142     ($resolved ? 'Hide' : 'Show') . ' resolved batches</I></A><BR>';
143
144 </%init>