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