5 <& elements/cust_main_dayranges.html,
6 'title' => 'Accounts Receivable Aging Summary',
11 my( $start, $end ) = @_;
13 "SQL EXPRESSION BASED ON $start AND $end";
14 # where $start and $end are unix timestamps
19 'name' => 'customers',
20 'query' => $sql_query,
21 'count_query' => $count_sql,
23 FS::UI::Web::cust_header(),
35 scalar(FS::UI::Web::cust_header()-1)
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'} ),
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'),
61 ( map { $_ ne 'Cust. Status' ? $clink : '' }
62 FS::UI::Web::cust_header()
71 'align' => FS::UI::Web::cust_aligns().
74 'size' => [ ( map '', FS::UI::Web::cust_header() ),
75 #'-1', '', '', '', '', '', ],
76 '', '', '', '', '', '',
77 ( map '', @pay_labels ),
79 'style' => [ FS::UI::Web::cust_styles(),
80 #'b', '', '', '', '', 'b', ],
82 ( map '', @pay_labels ),
84 'xls_format' => [ (map '', FS::UI::Web::cust_styles),
85 '', '', '', '', { bold => 1 },
88 FS::UI::Web::cust_colors(),
95 ( map '', @pay_labels ),
103 #actually need to auto-generate other things too for a passed-in ranges to work
104 my $ranges = $opt{'ranges'} ? delete($opt{'ranges'}) : [
112 my $range_sub = delete($opt{'range_sub'}); #or die
115 if($cgi->param('as_of')) {
116 $as_of = parse_datetime($cgi->param('as_of')) || '';
117 $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $as_of;
120 my $range_cols = join(',',
121 map call_range_sub($range_sub, @$_, 'as_of' => $as_of ), @$ranges );
123 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
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;
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
140 unless ( $cgi->param('all_customers') ) {
141 # Exclude entire cust_main records where the balance is >0
143 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
147 # If this is set, allow cust_main records with nonzero balances
148 my $negative = $cgi->param('negative') || 0;
151 call_range_sub($range_sub, $days, 0, 'as_of' => $as_of, 'no_as'=>1).
152 ($negative ? ' != 0' : ' > 0');
155 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
157 push @where, "agentnum = $agentnum";
160 #status (false laziness w/cust_main::search_sql
162 #prospect active inactive suspended cancelled
163 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
164 my $method = $cgi->param('status'). '_sql';
165 push @where, FS::cust_main->$method();
168 # cust_classnum (false laziness w/prepaid_income.html, elements/cust_pay_or_refund.html, cust_bill_pay.html, cust_bill_pkg.html, cust_bill_pkg_referral.html, unearned_detail.html, cust_credit.html, cust_credit_refund.html, cust_main::Search::search_sql)
169 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
170 my @classnums = grep /^\d*$/, $cgi->param('cust_classnum');
171 push @where, 'COALESCE( cust_main.classnum, 0) IN ( '.
172 join(',', map { $_ || '0' } @classnums ).
177 #here is the agent virtualization
178 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
180 my $where = join(' AND ', @where);
181 $where = "WHERE $where" if $where;
183 my $count_sql = "select count(*) from cust_main $where";
186 'table' => 'cust_main',
187 'addl_from' => FS::UI::Web::join_cust_main('cust_main'),
189 'select' => join(',',
194 FS::UI::Web::cust_sql_fields(),
197 'extra_sql' => $where,
198 'order_by' => "order by coalesce(lower(company), ''), lower(last)",
204 map call_range_sub( $range_sub, @$_, 'as_of' => $as_of, 'sum'=>1 ),
206 " FROM cust_main $where";
208 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
209 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
210 my $row = $total_sth->fetchrow_hashref();
212 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
214 my (@payby, @pay_head, @pay_labels, @pay_links);
216 my %payby = map {$_ => 1} $conf->config('payby');
218 push @payby, 'CARD' if ($payby{'CARD'} or $payby{'DCRD'});
219 push @payby, 'CHEK' if ($payby{'CHEK'} or $payby{'DCHK'});
222 @payby = ('CARD','CHEK')
225 if($opt{'payment_links'} && $curuser->access_right('Process payment') && @payby) {
226 my %label = ( CARD => 'Card',
228 push @pay_head, ({nodownload => 1}) foreach @payby;
229 $pay_head[0] = { label => 'Process',
231 colspan => scalar(@payby) };
233 @pay_labels = (map { my $payby = $_;
234 my $label = $label{$payby};
235 sub {($payby eq $_[0]->payby) ? "<b>".emt("$label (on file)")."</b>" : emt($label)}
238 @pay_links = (map { [ "${p}misc/payment.cgi?payby=$_;custnum=", 'custnum' ] }
245 my $conf = new FS::Conf;
246 my $curuser = $FS::CurrentUser::CurrentUser;
248 my $money_char = $conf->config('money_char') || '$';
252 # my $balance = balance(
254 # 'no_as' => 1, #set to true when using in a WHERE clause (supress AS clause)
255 # #or 0 / omit when using in a SELECT clause as a column
256 # # ("AS balance_$start_$end")
257 # 'sum' => 1, #set to true to get a SUM() of the values, for totals
259 # #obsolete? options for totals (passed to cust_main::balance_date_sql)
260 # 'total' => 1, #set to true to remove all customer comparison clauses
261 # 'join' => $join, #JOIN clause
262 # 'where' => \@where, #WHERE clause hashref (elements "AND"ed together)
266 my($range_sub, $startdays, $enddays, %opt) = @_;
268 my $as = $opt{'no_as'} ? '' : " AS rangecol_${startdays}_$enddays";
270 my $as_of = $opt{'as_of'} || time;
271 my $cutoff = DateTime->from_epoch(epoch => $as_of, time_zone => 'local');
272 $cutoff->truncate(to => 'day'); # local midnight on the report day
273 $cutoff->add(days => 1); # the day after that
274 $cutoff->subtract(seconds => 1); # the last second of the report day
276 my $start = $cutoff->clone;
277 $start->subtract(days => $startdays);
279 my $end = $cutoff->clone;
280 $end->subtract(days => $enddays);
282 #warn "cutoff ".$cutoff->epoch.", range $startdays-$enddays (".$start->epoch . '-' . ($enddays ? $end->epoch : '').")\n";
283 my $sql = &{$range_sub}( $start->epoch,
284 $enddays ? $end->epoch : '',
285 $cutoff->epoch ); #%opt?
287 $sql = "SUM($sql)" if $opt{'sum'};
293 sub format_rangecol { #closures help alot
295 sub { sprintf( $money_char.'%.2f', shift->get("rangecol_$range") ) };