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