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