RT#866: fix payment links
[freeside.git] / httemplate / search / elements / cust_main_dayranges.html
1 <%doc>
2
3 Example:
4
5   include( 'elements/cust_main_dayranges.html',
6                  'title'       => 'Accounts Receivable Aging Summary',
7                  'range_sub'   => $mysub,
8          )
9
10   my $mysub = sub {
11     my( $start, $end ) = @_;
12
13     "SQL EXPRESSION BASED ON $start AND $end";
14   };
15
16 </%doc>
17 <% include( 'search.html',
18                  'name'        => 'customers',
19                  'query'       => $sql_query,
20                  'count_query' => $count_sql,
21                  'header'      => [ 
22                                     FS::UI::Web::cust_header(),
23                                     '0-30',
24                                     '30-60',
25                                     '60-90',
26                                     '90+',
27                                     'Total',
28                                     @pay_head,
29                                   ],
30                  'footer'      => [
31                                     'Total',
32                                     ( map '',
33                                           ( 1 .. 
34                                             scalar(FS::UI::Web::cust_header()-1)
35                                           ),
36                                     ),
37                                     
38                                     sprintf( $money_char.'%.2f',
39                                              $row->{'rangecol_0_30'} ),
40                                     sprintf( $money_char.'%.2f',
41                                              $row->{'rangecol_30_60'} ),
42                                     sprintf( $money_char.'%.2f',
43                                              $row->{'rangecol_60_90'} ),
44                                     sprintf( $money_char.'%.2f',
45                                              $row->{'rangecol_90_0'} ),
46                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
47                                              $row->{'rangecol_0_0'} ),
48                                     ('') x @pay_labels,
49                                   ],
50                  'fields'      => [
51                                     FS::UI::Web::cust_fields_subs(),
52                                     format_rangecol('0_30'),
53                                     format_rangecol('30_60'),
54                                     format_rangecol('60_90'),
55                                     format_rangecol('90_0'),
56                                     format_rangecol('0_0'),
57                                     @pay_labels,
58                                   ],
59                  'links'       => [
60                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
61                                           FS::UI::Web::cust_header()
62                                     ),
63                                     '',
64                                     '',
65                                     '',
66                                     '',
67                                     '',
68                                     @pay_links,
69                                   ],
70                  #'align'       => 'rlccrrrrr',
71                  'align'       => FS::UI::Web::cust_aligns(). 
72                                    'rrrrr'.
73                                   ('c' x @pay_labels),
74                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
75                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
76                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
77                                     #'-1', '', '', '', '',  '', ],
78                                     '', '', '', '', '',  '', 
79                                     ( map '', @pay_labels ),
80                                     ],
81                  'style'       => [ FS::UI::Web::cust_styles(),
82                                     #'b', '', '', '', '', 'b', ],
83                                     '', '', '', '', 'b', 
84                                     ( map '', @pay_labels ),
85                                     ],
86                  'color'       => [
87                                     FS::UI::Web::cust_colors(),
88                                     '',
89                                     '',
90                                     '',
91                                     '',
92                                     '',
93                                     '',
94                                     ( map '', @pay_labels ),
95                                   ],
96                %opt,
97              )
98 %>
99 <%init>
100
101 my %opt = @_;
102
103 #actually need to auto-generate other things too for a passed-in ranges to work
104 my $ranges = $opt{'ranges'} ? delete($opt{'ranges'}) : [
105   [  0, 30 ],
106   [ 30, 60 ],
107   [ 60, 90 ],
108   [ 90,  0 ],
109   [  0,  0 ],
110 ];
111
112 my $range_sub = delete($opt{'range_sub'}); #or die
113
114 my $offset = 0;
115 if($cgi->param('as_of')) {
116   $offset = int((time - parse_datetime($cgi->param('as_of'))) / 86400);
117   $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
118 }
119
120 #my $range_cols = join(',', map &{$range_sub}( @$_ ), @ranges );
121 my $range_cols = join(',', map call_range_sub($range_sub, @$_, 'offset' => $offset ), @$ranges );
122
123 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
124
125 my $active_sql    = FS::cust_pkg->active_sql;
126 my $inactive_sql  = FS::cust_pkg->inactive_sql;
127 my $suspended_sql = FS::cust_pkg->suspended_sql;
128 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
129
130 my $packages_cols = <<END;
131      ( $select_count_pkgs                    ) AS num_pkgs_sql,
132      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
133      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
134      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
135      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
136 END
137
138 my @where = ();
139
140 unless ( $cgi->param('all_customers') ) {
141 # Exclude entire cust_main records where the balance is >0
142   my $days = 0;
143   if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
144     $days = $1;
145   }
146
147   push @where,
148     call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
149 }
150
151 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
152   my $agentnum = $1;
153   push @where, "agentnum = $agentnum";
154 }
155
156 #status (false laziness w/cust_main::search_sql
157
158 #prospect active inactive suspended cancelled
159 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
160   my $method = $cgi->param('status'). '_sql';
161   #push @where, $class->$method();
162   push @where, FS::cust_main->$method();
163 }
164
165 #here is the agent virtualization
166 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
167
168 my $where = join(' AND ', @where);
169 $where = "WHERE $where" if $where;
170
171 my $count_sql = "select count(*) from cust_main $where";
172
173 my $sql_query = {
174   'table'     => 'cust_main',
175   'hashref'   => {},
176   'select'    => join(',',
177                    #'cust_main.*',
178                    'custnum',
179                    $range_cols,
180                    $packages_cols,
181                    FS::UI::Web::cust_sql_fields(),
182                    'payby',
183                  ),
184   'extra_sql' => $where,
185   'order_by'  => "order by coalesce(lower(company), ''), lower(last)",
186 };
187
188 my $total_sql =
189   "SELECT ".
190       join(',', map call_range_sub( $range_sub, @$_, 'offset' => $offset, 'sum'=>1 ), @$ranges).
191     " FROM cust_main $where";
192
193 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
194 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
195 my $row = $total_sth->fetchrow_hashref();
196
197 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
198
199 my (@payby, @pay_head, @pay_labels, @pay_links);
200
201 my %payby = map {$_ => 1} $conf->config('payby');
202 if(%payby) {
203   push @payby, 'CARD' if ($payby{'CARD'} or $payby{'DCRD'});
204   push @payby, 'CHEK' if ($payby{'CHEK'} or $payby{'DCHK'});
205 }
206 else {
207   @payby = ('CARD','CHEK')
208 }
209
210 if($opt{'payment_links'} && $curuser->access_right('Process payment') && @payby) {
211   my %label = ( CARD => 'Card',
212                 CHEK => 'E-Check' );
213   push @pay_head, ({nodownload => 1}) foreach @payby;
214   $pay_head[0] = { label => 'Process',
215                    nodownload => 1,
216                    colspan => scalar(@payby) };
217
218   @pay_labels = (map { my $payby = $_; 
219                        my $label = $label{$payby};
220                        sub {($payby eq $_[0]->payby) ? "<b>$label</b>" : $label}
221                      } @payby );
222
223   @pay_links = (map { [ "${p}misc/payment.cgi?payby=$_;custnum=", 'custnum' ] }
224                          @payby );
225 }
226
227 </%init>
228 <%once>
229
230 my $conf = new FS::Conf;
231 my $curuser = $FS::CurrentUser::CurrentUser;
232
233 my $money_char = $conf->config('money_char') || '$';
234
235 #Example:
236 #
237 # my $balance = balance(
238 #   $start, $end, 
239 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
240 #                 #or 0 / omit when using in a SELECT clause as a column
241 #                 #  ("AS balance_$start_$end")
242 #   'sum'    => 1, #set to true to get a SUM() of the values, for totals
243 #
244 #   #obsolete? options for totals (passed to cust_main::balance_date_sql)
245 #   'total'  => 1, #set to true to remove all customer comparison clauses
246 #   'join'   => $join,   #JOIN clause
247 #   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
248 # )
249
250 sub call_range_sub {
251   my($range_sub, $start, $end, %opt) = @_;
252
253   my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
254
255   my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
256
257   $sql = "SUM($sql)" if $opt{'sum'};
258
259   $sql.$as;
260
261 }
262
263 sub format_rangecol { #closures help alot
264   my $range = shift;
265   sub { sprintf( $money_char.'%.2f', shift->get("rangecol_$range") ) };
266 }
267
268 </%once>