combine ticket notification scrips, #15353
[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                                       '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 %>
90 <%init>
91
92 die "access denied"
93   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports')
94       || $FS::CurrentUser::CurrentUser->access_right('Process batches');
95
96 my %statusmap = ('I'=>'In Transit', 'O'=>'Open', 'R'=>'Resolved');
97 my $hashref = {};
98 my $count_query = 'SELECT COUNT(*) FROM pay_batch';
99
100 my($begin, $end) = ( '', '' );
101
102 my @where;
103 if ( $cgi->param('beginning')
104      && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
105   $begin = parse_datetime($1);
106   push @where, "download >= $begin";
107 }
108 if ( $cgi->param('ending')
109       && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
110   $end = parse_datetime($1) + 86399;
111   push @where, "download < $end";
112 }
113
114 my @status;
115 if ( $cgi->param('open') ) {
116   push @status, "O";
117 }
118
119 if ( $cgi->param('intransit') ) {
120   push @status, "I";
121 }
122
123 if ( $cgi->param('resolved') ) {
124   push @status, "R";
125 }
126
127 push @where,
128      scalar(@status) ? q!(status='! . join(q!' OR status='!, @status) . q!')!
129                      : q!status='X'!;  # kludgy, X is unused at present
130
131 my $extra_sql = scalar(@where) ? 'WHERE ' . join(' AND ', @where) : ''; 
132
133 my $link = [ "${p}search/cust_pay_batch.cgi?dcln=1;batchnum=", 'batchnum' ];
134
135 my $resolved = $cgi->param('resolved') || 0;
136 $cgi->param('resolved' => !$resolved);
137 my $html_init = '<A HREF="' . $cgi->self_url . '"><I>'.
138     ($resolved ? 'Hide' : 'Show') . ' resolved batches</I></A><BR>';
139
140 </%init>