5 include( 'elements/cust_main_dayranges.html',
6 'title' => 'Accounts Receivable Aging Summary',
11 my( $start, $end ) = @_;
13 "SQL EXPRESSION BASED ON $start AND $end";
17 <% include( 'search.html',
18 'name' => 'customers',
19 'query' => $sql_query,
20 'count_query' => $count_sql,
22 FS::UI::Web::cust_header(),
33 scalar(FS::UI::Web::cust_header()-1)
36 sprintf( $money_char.'%.2f',
37 $row->{'rangecol_0_30'} ),
38 sprintf( $money_char.'%.2f',
39 $row->{'rangecol_30_60'} ),
40 sprintf( $money_char.'%.2f',
41 $row->{'rangecol_60_90'} ),
42 sprintf( $money_char.'%.2f',
43 $row->{'rangecol_90_0'} ),
44 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
45 $row->{'rangecol_0_0'} ),
48 \&FS::UI::Web::cust_fields,
49 format_rangecol('0_30'),
50 format_rangecol('30_60'),
51 format_rangecol('60_90'),
52 format_rangecol('90_0'),
53 format_rangecol('0_0'),
56 ( map { $_ ne 'Cust. Status' ? $clink : '' }
57 FS::UI::Web::cust_header()
65 #'align' => 'rlccrrrrr',
66 'align' => FS::UI::Web::cust_aligns(). 'rrrrr',
67 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
68 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
69 'size' => [ ( map '', FS::UI::Web::cust_header() ),
70 #'-1', '', '', '', '', '', ],
71 '', '', '', '', '', ],
72 'style' => [ FS::UI::Web::cust_styles(),
73 #'b', '', '', '', '', 'b', ],
74 '', '', '', '', 'b', ],
76 FS::UI::Web::cust_colors(),
90 #actually need to auto-generate other things too for a passed-in ranges to work
91 my $ranges = $opt{'ranges'} ? delete($opt{'ranges'}) : [
99 my $range_sub = delete($opt{'range_sub'}); #or die
102 if($cgi->param('as_of')) {
103 $offset = int((time - str2time($cgi->param('as_of'))) / 86400);
104 $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
107 #my $range_cols = join(',', map &{$range_sub}( @$_ ), @ranges );
108 my $range_cols = join(',', map call_range_sub($range_sub, @$_, 'offset' => $offset ), @$ranges );
110 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
112 my $active_sql = FS::cust_pkg->active_sql;
113 my $inactive_sql = FS::cust_pkg->inactive_sql;
114 my $suspended_sql = FS::cust_pkg->suspended_sql;
115 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
117 my $packages_cols = <<END;
118 ( $select_count_pkgs ) AS num_pkgs_sql,
119 ( $select_count_pkgs AND $active_sql ) AS active_pkgs,
120 ( $select_count_pkgs AND $inactive_sql ) AS inactive_pkgs,
121 ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
122 ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
127 unless ( $cgi->param('all_customers') ) {
128 # Exclude entire cust_main records where the balance is >0
130 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
135 call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
138 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
140 push @where, "agentnum = $agentnum";
143 #status (false laziness w/cust_main::search_sql
145 #prospect active inactive suspended cancelled
146 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
147 my $method = $cgi->param('status'). '_sql';
148 #push @where, $class->$method();
149 push @where, FS::cust_main->$method();
152 #here is the agent virtualization
153 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
155 my $where = join(' AND ', @where);
156 $where = "WHERE $where" if $where;
158 my $count_sql = "select count(*) from cust_main $where";
161 'table' => 'cust_main',
163 'select' => join(',',
168 FS::UI::Web::cust_sql_fields(),
170 'extra_sql' => $where,
171 'order_by' => "order by coalesce(lower(company), ''), lower(last)",
176 join(',', map call_range_sub( $range_sub, @$_, 'offset' => $offset, 'sum'=>1 ), @$ranges).
177 " FROM cust_main $where";
179 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
180 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
181 my $row = $total_sth->fetchrow_hashref();
183 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
188 my $conf = new FS::Conf;
190 my $money_char = $conf->config('money_char') || '$';
194 # my $balance = balance(
196 # 'no_as' => 1, #set to true when using in a WHERE clause (supress AS clause)
197 # #or 0 / omit when using in a SELECT clause as a column
198 # # ("AS balance_$start_$end")
199 # 'sum' => 1, #set to true to get a SUM() of the values, for totals
201 # #obsolete? options for totals (passed to cust_main::balance_date_sql)
202 # 'total' => 1, #set to true to remove all customer comparison clauses
203 # 'join' => $join, #JOIN clause
204 # 'where' => \@where, #WHERE clause hashref (elements "AND"ed together)
208 my($range_sub, $start, $end, %opt) = @_;
210 my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
212 my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
214 $sql = "SUM($sql)" if $opt{'sum'};
220 sub format_rangecol { #closures help alot
222 sub { sprintf( $money_char.'%.2f', shift->get("rangecol_$range") ) };