1 <% include( 'elements/search.html',
2 'title' => 'Accounts Receivable Aging Summary',
5 'count_query' => $count_sql,
7 FS::UI::Web::cust_header(),
9 #'Status', # (cust_main)',
20 scalar(FS::UI::Web::cust_header()-1)
25 sprintf( $money_char.'%.2f',
26 $row->{'owed_0_30'} ),
27 sprintf( $money_char.'%.2f',
28 $row->{'owed_30_60'} ),
29 sprintf( $money_char.'%.2f',
30 $row->{'owed_60_90'} ),
31 sprintf( $money_char.'%.2f',
32 $row->{'owed_90_0'} ),
33 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
37 \&FS::UI::Web::cust_fields,
38 #sub { ( &{$status_statuscol}(shift) )[0] },
39 #sub { ucfirst(shift->status) },
40 sub { sprintf( $money_char.'%.2f',
41 shift->get('owed_0_30') ) },
42 sub { sprintf( $money_char.'%.2f',
43 shift->get('owed_30_60') ) },
44 sub { sprintf( $money_char.'%.2f',
45 shift->get('owed_60_90') ) },
46 sub { sprintf( $money_char.'%.2f',
47 shift->get('owed_90_0') ) },
48 sub { sprintf( $money_char.'%.2f',
49 shift->get('owed_0_0') ) },
52 ( map { $_ ne 'Cust. Status' ? $clink : '' }
53 FS::UI::Web::cust_header()
63 #'align' => 'rlccrrrrr',
64 'align' => FS::UI::Web::cust_aligns(). 'rrrrr',
65 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
66 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
67 'size' => [ ( map '', FS::UI::Web::cust_header() ),
68 #'-1', '', '', '', '', '', ],
69 '', '', '', '', '', ],
70 'style' => [ FS::UI::Web::cust_styles(),
71 #'b', '', '', '', '', 'b', ],
72 '', '', '', '', 'b', ],
74 FS::UI::Web::cust_colors(),
75 #sub { ( &{$status_statuscol}(shift) )[1] },
76 #sub { shift->statuscolor; },
89 my($start, $end, %opt) = @_;
93 #handle start and end ranges
96 push @where, "cust_bill._date <= extract(epoch from now())-".
100 push @where, "cust_bill._date > extract(epoch from now()) - ".
104 #handle 'cust' option
106 push @where, "cust_main.custnum = cust_bill.custnum"
109 #handle 'agentnum' option
111 if ( $opt{'agentnum'} ) {
112 $join = 'LEFT JOIN cust_main USING ( custnum )';
113 push @where, "agentnum = '$opt{'agentnum'}'";
116 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
118 my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
123 ( select sum(amount) from cust_bill_pay
124 where cust_bill.invnum = cust_bill_pay.invnum )
128 ( select sum(amount) from cust_credit_bill
129 where cust_bill.invnum = cust_credit_bill.invnum )
136 "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
144 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
154 my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
156 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
158 my $active_sql = FS::cust_pkg->active_sql;
159 my $inactive_sql = FS::cust_pkg->inactive_sql;
160 my $suspended_sql = FS::cust_pkg->suspended_sql;
161 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
163 my $packages_cols = <<END;
164 ( $select_count_pkgs ) AS num_pkgs_sql,
165 ( $select_count_pkgs AND $active_sql ) AS active_pkgs,
166 ( $select_count_pkgs AND $inactive_sql ) AS inactive_pkgs,
167 ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
168 ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
172 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
176 #my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
177 my $where = "where ". owed($days, 0, 'cust'=>1, 'noas'=>1). " > 0";
180 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
182 $where .= " AND agentnum = '$agentnum' ";
185 #here is the agent virtualization
186 $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
188 my $count_sql = "select count(*) from cust_main $where";
191 'table' => 'cust_main',
193 'select' => "*, $owed_cols, $packages_cols",
194 'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
197 my $total_sql = "select ".
198 join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
200 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
201 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
202 my $row = $total_sth->fetchrow_hashref();
204 my $conf = new FS::Conf;
205 my $money_char = $conf->config('money_char') || '$';
207 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
209 my $status_statuscol = sub {
210 #conceptual false laziness with cust_main::status...
213 my $status = 'unknown';
214 if ( $row->num_pkgs_sql == 0 ) {
215 $status = 'prospect';
216 } elsif ( $row->active_pkgs > 0 ) {
218 } elsif ( $row->inactive_pkgs > 0 ) {
219 $status = 'inactive';
220 } elsif ( $row->suspended_pkgs > 0 ) {
221 $status = 'suspended';
222 } elsif ( $row->cancelled_pkgs > 0 ) {
223 $status = 'cancelled'
226 ( ucfirst($status), $FS::cust_main::statuscolor{$status} );