fix date parsing when using international dates (package date edit), RT#8027
[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                                     @pay_head,
24                                     '0-30',
25                                     '30-60',
26                                     '60-90',
27                                     '90+',
28                                     'Total',
29                                   ],
30                  'footer'      => [
31                                     'Total',
32                                     ( map '',
33                                           ( 1 .. 
34                                             scalar(FS::UI::Web::cust_header()-1)
35                                           ), @pay_labels
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                                   ],
49                  'fields'      => [
50                                     FS::UI::Web::cust_fields_subs(),
51                                     @pay_labels,
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                                   ],
58                  'links'       => [
59                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
60                                           FS::UI::Web::cust_header()
61                                     ),
62                                     @pay_links,
63                                     '',
64                                     '',
65                                     '',
66                                     '',
67                                     '',
68                                   ],
69                  #'align'       => 'rlccrrrrr',
70                  'align'       => FS::UI::Web::cust_aligns(). 
71                                   ('c' x @pay_labels).
72                                    'rrrrr',
73                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
74                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
75                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
76                                     ( map '', @pay_labels ),
77                                     #'-1', '', '', '', '',  '', ],
78                                     '', '', '', '', '',  '', ],
79                  'style'       => [ FS::UI::Web::cust_styles(),
80                                     ( map '', @pay_labels ),
81                                     #'b', '', '', '', '', 'b', ],
82                                     '', '', '', '', 'b', ],
83                  'color'       => [
84                                     FS::UI::Web::cust_colors(),
85                                     ( map '', @pay_labels ),
86                                     '',
87                                     '',
88                                     '',
89                                     '',
90                                     '',
91                                     '',
92                                   ],
93                %opt,
94              )
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 &{$range_sub}( @$_ ), @ranges );
118 my $range_cols = join(',', map call_range_sub($range_sub, @$_, 'offset' => $offset ), @$ranges );
119
120 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
121
122 my $active_sql    = FS::cust_pkg->active_sql;
123 my $inactive_sql  = FS::cust_pkg->inactive_sql;
124 my $suspended_sql = FS::cust_pkg->suspended_sql;
125 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
126
127 my $packages_cols = <<END;
128      ( $select_count_pkgs                    ) AS num_pkgs_sql,
129      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
130      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
131      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
132      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
133 END
134
135 my @where = ();
136
137 unless ( $cgi->param('all_customers') ) {
138 # Exclude entire cust_main records where the balance is >0
139   my $days = 0;
140   if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
141     $days = $1;
142   }
143
144   push @where,
145     call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
146 }
147
148 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
149   my $agentnum = $1;
150   push @where, "agentnum = $agentnum";
151 }
152
153 #status (false laziness w/cust_main::search_sql
154
155 #prospect active inactive suspended cancelled
156 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
157   my $method = $cgi->param('status'). '_sql';
158   #push @where, $class->$method();
159   push @where, FS::cust_main->$method();
160 }
161
162 #here is the agent virtualization
163 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
164
165 my $where = join(' AND ', @where);
166 $where = "WHERE $where" if $where;
167
168 my $count_sql = "select count(*) from cust_main $where";
169
170 my $sql_query = {
171   'table'     => 'cust_main',
172   'hashref'   => {},
173   'select'    => join(',',
174                    #'cust_main.*',
175                    'custnum',
176                    $range_cols,
177                    $packages_cols,
178                    FS::UI::Web::cust_sql_fields(),
179                    'payby',
180                  ),
181   'extra_sql' => $where,
182   'order_by'  => "order by coalesce(lower(company), ''), lower(last)",
183 };
184
185 my $total_sql =
186   "SELECT ".
187       join(',', map call_range_sub( $range_sub, @$_, 'offset' => $offset, 'sum'=>1 ), @$ranges).
188     " FROM cust_main $where";
189
190 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
191 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
192 my $row = $total_sth->fetchrow_hashref();
193
194 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
195
196 my (@pay_head, @pay_labels, @pay_links);
197
198 if($opt{'payment_links'} && $curuser->access_right('Process payment')) {
199   @pay_head = ({ label => 'Process', nodownload => 1, colspan => 2 },
200                 { nodownload => 1 });
201
202   @pay_labels = (map { my ($payby,$label) = @$_; 
203                         sub {($payby eq $_[0]->payby) ? "<b>$label</b>" : $label}
204                      } ([CARD => 'Card'], [CHEK => 'ACH']) );
205
206   @pay_links = (map { [ "${p}misc/payment.cgi?payby=$_;custnum=", 'custnum' ] }
207                          'CARD', 'CHEK' );
208 }
209
210 </%init>
211 <%once>
212
213 my $conf = new FS::Conf;
214 my $curuser = $FS::CurrentUser::CurrentUser;
215
216 my $money_char = $conf->config('money_char') || '$';
217
218 #Example:
219 #
220 # my $balance = balance(
221 #   $start, $end, 
222 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
223 #                 #or 0 / omit when using in a SELECT clause as a column
224 #                 #  ("AS balance_$start_$end")
225 #   'sum'    => 1, #set to true to get a SUM() of the values, for totals
226 #
227 #   #obsolete? options for totals (passed to cust_main::balance_date_sql)
228 #   'total'  => 1, #set to true to remove all customer comparison clauses
229 #   'join'   => $join,   #JOIN clause
230 #   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
231 # )
232
233 sub call_range_sub {
234   my($range_sub, $start, $end, %opt) = @_;
235
236   my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
237
238   my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
239
240   $sql = "SUM($sql)" if $opt{'sum'};
241
242   $sql.$as;
243
244 }
245
246 sub format_rangecol { #closures help alot
247   my $range = shift;
248   sub { sprintf( $money_char.'%.2f', shift->get("rangecol_$range") ) };
249 }
250
251 </%once>