4 my($start, $end, %opt) = @_;
8 #handle start and end ranges
11 push @where, "cust_bill._date <= extract(epoch from now())-".
15 push @where, "cust_bill._date > extract(epoch from now()) - ".
21 push @where, "cust_main.custnum = cust_bill.custnum"
24 #handle 'agentnum' option
26 if ( $opt{'agentnum'} ) {
27 $join = 'LEFT JOIN cust_main USING ( custnum )';
28 push @where, "agentnum = '$opt{'agentnum'}'";
31 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
33 my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
38 ( select sum(amount) from cust_bill_pay
39 where cust_bill.invnum = cust_bill_pay.invnum )
43 ( select sum(amount) from cust_credit_bill
44 where cust_bill.invnum = cust_credit_bill.invnum )
51 "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
63 my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
65 my $recurring = <<END;
66 '0' != ( select freq from part_pkg
67 where cust_pkg.pkgpart = part_pkg.pkgpart )
70 my $packages_cols = <<END;
72 ( select count(*) from cust_pkg
73 where cust_main.custnum = cust_pkg.custnum
75 and ( cancel = 0 or cancel is null )
76 ) as uncancelled_pkgs,
78 ( select count(*) from cust_pkg
79 where cust_main.custnum = cust_pkg.custnum
81 and ( cancel = 0 or cancel is null )
82 and ( susp = 0 or susp is null )
87 my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
90 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
92 $where .= " AND agentnum = '$agentnum' ";
95 my $count_sql = "select count(*) from cust_main $where";
98 'table' => 'cust_main',
100 'select' => "*, $owed_cols, $packages_cols",
101 'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
104 my $total_sql = "select ".
105 join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
107 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
108 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
109 my $row = $total_sth->fetchrow_hashref();
111 my $conf = new FS::Conf;
112 my $money_char = $conf->config('money_char') || '$';
114 my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
117 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
119 %><%= include( 'elements/search.html',
120 'title' => 'Accounts Receivable Aging Summary',
121 'name' => 'customers',
122 'query' => $sql_query,
123 'count_query' => $count_sql,
125 FS::UI::Web::cust_header(),
127 #'Status', # (cust_main)',
138 scalar(FS::UI::Web::cust_header()-1)
143 sprintf( $money_char.'%.2f',
144 $row->{'owed_0_30'} ),
145 sprintf( $money_char.'%.2f',
146 $row->{'owed_30_60'} ),
147 sprintf( $money_char.'%.2f',
148 $row->{'owed_60_90'} ),
149 sprintf( $money_char.'%.2f',
150 $row->{'owed_90_0'} ),
151 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
152 $row->{'owed_0_0'} ),
155 \&FS::UI::Web::cust_fields,
158 my $status = 'Cancelled';
159 my $statuscol = 'FF0000';
160 if ( $row->uncancelled_pkgs ) {
161 $status = 'Suspended';
162 $statuscol = 'FF9900';
163 if ( $row->active_pkgs ) {
165 $statuscol = '00CC00';
170 #sub { ucfirst(shift->status) },
171 sub { sprintf( $money_char.'%.2f',
172 shift->get('owed_0_30') ) },
173 sub { sprintf( $money_char.'%.2f',
174 shift->get('owed_30_60') ) },
175 sub { sprintf( $money_char.'%.2f',
176 shift->get('owed_60_90') ) },
177 sub { sprintf( $money_char.'%.2f',
178 shift->get('owed_90_0') ) },
179 sub { sprintf( $money_char.'%.2f',
180 shift->get('owed_0_0') ) },
183 ( map $clink, FS::UI::Web::cust_header() ),
192 #'align' => 'rlccrrrrr',
194 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
195 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
196 'size' => [ ( map '', FS::UI::Web::cust_header() ),
197 '-1', '', '', '', '', '', ],
198 'style' => [ ( map '', FS::UI::Web::cust_header() ),
199 'b', '', '', '', '', 'b', ],
201 ( map '', FS::UI::Web::cust_header() ),
204 my $status = 'Cancelled';
205 my $statuscol = 'FF0000';
206 if ( $row->uncancelled_pkgs ) {
207 $status = 'Suspended';
208 $statuscol = 'FF9900';
209 if ( $row->active_pkgs ) {
211 $statuscol = '00CC00';
216 #sub { shift->statuscolor; },