1 <% include( 'elements/search.html',
2 'title' => 'Accounts Receivable Aging Summary',
5 'count_query' => $count_sql,
7 FS::UI::Web::cust_header(),
18 scalar(FS::UI::Web::cust_header()-1)
21 sprintf( $money_char.'%.2f',
22 $row->{'balance_0_30'} ),
23 sprintf( $money_char.'%.2f',
24 $row->{'balance_30_60'} ),
25 sprintf( $money_char.'%.2f',
26 $row->{'balance_60_90'} ),
27 sprintf( $money_char.'%.2f',
28 $row->{'balance_90_0'} ),
29 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
30 $row->{'balance_0_0'} ),
33 \&FS::UI::Web::cust_fields,
34 format_balance('0_30'),
35 format_balance('30_60'),
36 format_balance('60_90'),
37 format_balance('90_0'),
38 format_balance('0_0'),
41 ( map { $_ ne 'Cust. Status' ? $clink : '' }
42 FS::UI::Web::cust_header()
50 #'align' => 'rlccrrrrr',
51 'align' => FS::UI::Web::cust_aligns(). 'rrrrr',
52 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
53 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
54 'size' => [ ( map '', FS::UI::Web::cust_header() ),
55 #'-1', '', '', '', '', '', ],
56 '', '', '', '', '', ],
57 'style' => [ FS::UI::Web::cust_styles(),
58 #'b', '', '', '', '', 'b', ],
59 '', '', '', '', 'b', ],
61 FS::UI::Web::cust_colors(),
74 unless $FS::CurrentUser::CurrentUser->access_right('Receivables report')
75 or $FS::CurrentUser::CurrentUser->access_right('Financial reports');
85 my $owed_cols = join(',', map balance( @$_ ), @ranges );
87 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
89 my $active_sql = FS::cust_pkg->active_sql;
90 my $inactive_sql = FS::cust_pkg->inactive_sql;
91 my $suspended_sql = FS::cust_pkg->suspended_sql;
92 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
94 my $packages_cols = <<END;
95 ( $select_count_pkgs ) AS num_pkgs_sql,
96 ( $select_count_pkgs AND $active_sql ) AS active_pkgs,
97 ( $select_count_pkgs AND $inactive_sql ) AS inactive_pkgs,
98 ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
99 ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
104 unless ( $cgi->param('all_customers') ) {
107 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
111 push @where, balance($days, 0, 'no_as'=>1). ' > 0'; # != 0';
115 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
117 push @where, "agentnum = $agentnum";
120 #status (false laziness w/cust_main::search_sql
122 #prospect active inactive suspended cancelled
123 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
124 my $method = $cgi->param('status'). '_sql';
125 #push @where, $class->$method();
126 push @where, FS::cust_main->$method();
129 #here is the agent virtualization
130 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
132 my $where = join(' AND ', @where);
133 $where = "WHERE $where" if $where;
135 my $count_sql = "select count(*) from cust_main $where";
138 'table' => 'cust_main',
140 'select' => join(',',
145 FS::UI::Web::cust_sql_fields(),
147 'extra_sql' => $where,
148 'order_by' => "order by coalesce(lower(company), ''), lower(last)",
151 my $total_sql = "SELECT ". join(',', map balance( @$_, 'sum'=>1 ), @ranges).
152 " FROM cust_main $where";
154 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
155 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
156 my $row = $total_sth->fetchrow_hashref();
158 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
163 my $conf = new FS::Conf;
165 my $money_char = $conf->config('money_char') || '$';
169 # my $balance = balance(
171 # 'no_as' => 1, #set to true when using in a WHERE clause (supress AS clause)
172 # #or 0 / omit when using in a SELECT clause as a column
173 # # ("AS balance_$start_$end")
174 # 'sum' => 1, #set to true to get a SUM() of the values, for totals
176 # #obsolete? options for totals (passed to cust_main::balance_date_sql)
177 # 'total' => 1, #set to true to remove all customer comparison clauses
178 # 'join' => $join, #JOIN clause
179 # 'where' => \@where, #WHERE clause hashref (elements "AND"ed together)
183 my($start, $end, %opt) = @_;
185 my $as = $opt{'no_as'} ? '' : " AS balance_${start}_$end";
187 #handle start and end ranges (86400 = 24h * 60m * 60s)
188 my $str2time = str2time_sql;
189 my $closing = str2time_sql_closing;
190 $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
191 $end = $end ? "( $str2time now() $closing - ".($end * 86400). ' )' : '';
193 $opt{'unapplied_date'} = 1;
195 ( $opt{sum} ? 'SUM( ' : '' ).
196 FS::cust_main->balance_date_sql( $start, $end, %opt ).
197 ( $opt{sum} ? ' )' : '' ).
202 sub format_balance { #closures help alot
204 sub { sprintf( $money_char.'%.2f', shift->get("balance_$range") ) };