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->{'owed_0_30'} ),
23 sprintf( $money_char.'%.2f',
24 $row->{'owed_30_60'} ),
25 sprintf( $money_char.'%.2f',
26 $row->{'owed_60_90'} ),
27 sprintf( $money_char.'%.2f',
28 $row->{'owed_90_0'} ),
29 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
33 \&FS::UI::Web::cust_fields,
34 sub { sprintf( $money_char.'%.2f',
35 shift->get('owed_0_30') ) },
36 sub { sprintf( $money_char.'%.2f',
37 shift->get('owed_30_60') ) },
38 sub { sprintf( $money_char.'%.2f',
39 shift->get('owed_60_90') ) },
40 sub { sprintf( $money_char.'%.2f',
41 shift->get('owed_90_0') ) },
42 sub { sprintf( $money_char.'%.2f',
43 shift->get('owed_0_0') ) },
46 ( map { $_ ne 'Cust. Status' ? $clink : '' }
47 FS::UI::Web::cust_header()
55 #'align' => 'rlccrrrrr',
56 'align' => FS::UI::Web::cust_aligns(). 'rrrrr',
57 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
58 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
59 'size' => [ ( map '', FS::UI::Web::cust_header() ),
60 #'-1', '', '', '', '', '', ],
61 '', '', '', '', '', ],
62 'style' => [ FS::UI::Web::cust_styles(),
63 #'b', '', '', '', '', 'b', ],
64 '', '', '', '', 'b', ],
66 FS::UI::Web::cust_colors(),
79 my($start, $end, %opt) = @_;
83 #handle start and end ranges
86 push @where, "cust_bill._date <= extract(epoch from now())-".
90 push @where, "cust_bill._date > extract(epoch from now()) - ".
96 push @where, "cust_main.custnum = cust_bill.custnum"
99 #handle 'agentnum' option
101 if ( $opt{'agentnum'} ) {
102 $join = 'LEFT JOIN cust_main USING ( custnum )';
103 push @where, "agentnum = '$opt{'agentnum'}'";
106 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
108 my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
113 ( select sum(amount) from cust_bill_pay
114 where cust_bill.invnum = cust_bill_pay.invnum )
118 ( select sum(amount) from cust_credit_bill
119 where cust_bill.invnum = cust_credit_bill.invnum )
126 "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
134 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
144 my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
146 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
148 my $active_sql = FS::cust_pkg->active_sql;
149 my $inactive_sql = FS::cust_pkg->inactive_sql;
150 my $suspended_sql = FS::cust_pkg->suspended_sql;
151 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
153 my $packages_cols = <<END;
154 ( $select_count_pkgs ) AS num_pkgs_sql,
155 ( $select_count_pkgs AND $active_sql ) AS active_pkgs,
156 ( $select_count_pkgs AND $inactive_sql ) AS inactive_pkgs,
157 ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
158 ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
162 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
166 #my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
167 my $where = "where ". owed($days, 0, 'cust'=>1, 'noas'=>1). " > 0";
170 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
172 $where .= " AND agentnum = '$agentnum' ";
175 #here is the agent virtualization
176 $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
178 my $count_sql = "select count(*) from cust_main $where";
181 'table' => 'cust_main',
183 'select' => "*, $owed_cols, $packages_cols",
184 'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
187 my $total_sql = "select ".
188 join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
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();
194 my $conf = new FS::Conf;
195 my $money_char = $conf->config('money_char') || '$';
197 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];