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