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 #here is the agent virtualization
96 $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
98 my $count_sql = "select count(*) from cust_main $where";
101 'table' => 'cust_main',
103 'select' => "*, $owed_cols, $packages_cols",
104 'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
107 my $total_sql = "select ".
108 join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
110 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
111 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
112 my $row = $total_sth->fetchrow_hashref();
114 my $conf = new FS::Conf;
115 my $money_char = $conf->config('money_char') || '$';
117 my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
120 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
122 %><%= include( 'elements/search.html',
123 'title' => 'Accounts Receivable Aging Summary',
124 'name' => 'customers',
125 'query' => $sql_query,
126 'count_query' => $count_sql,
128 FS::UI::Web::cust_header(),
130 #'Status', # (cust_main)',
141 scalar(FS::UI::Web::cust_header()-1)
146 sprintf( $money_char.'%.2f',
147 $row->{'owed_0_30'} ),
148 sprintf( $money_char.'%.2f',
149 $row->{'owed_30_60'} ),
150 sprintf( $money_char.'%.2f',
151 $row->{'owed_60_90'} ),
152 sprintf( $money_char.'%.2f',
153 $row->{'owed_90_0'} ),
154 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
155 $row->{'owed_0_0'} ),
158 \&FS::UI::Web::cust_fields,
161 my $status = 'Cancelled';
162 my $statuscol = 'FF0000';
163 if ( $row->uncancelled_pkgs ) {
164 $status = 'Suspended';
165 $statuscol = 'FF9900';
166 if ( $row->active_pkgs ) {
168 $statuscol = '00CC00';
173 #sub { ucfirst(shift->status) },
174 sub { sprintf( $money_char.'%.2f',
175 shift->get('owed_0_30') ) },
176 sub { sprintf( $money_char.'%.2f',
177 shift->get('owed_30_60') ) },
178 sub { sprintf( $money_char.'%.2f',
179 shift->get('owed_60_90') ) },
180 sub { sprintf( $money_char.'%.2f',
181 shift->get('owed_90_0') ) },
182 sub { sprintf( $money_char.'%.2f',
183 shift->get('owed_0_0') ) },
186 ( map $clink, FS::UI::Web::cust_header() ),
195 #'align' => 'rlccrrrrr',
197 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
198 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
199 'size' => [ ( map '', FS::UI::Web::cust_header() ),
200 '-1', '', '', '', '', '', ],
201 'style' => [ ( map '', FS::UI::Web::cust_header() ),
202 'b', '', '', '', '', 'b', ],
204 ( map '', FS::UI::Web::cust_header() ),
207 my $status = 'Cancelled';
208 my $statuscol = 'FF0000';
209 if ( $row->uncancelled_pkgs ) {
210 $status = 'Suspended';
211 $statuscol = 'FF9900';
212 if ( $row->active_pkgs ) {
214 $statuscol = '00CC00';
219 #sub { shift->statuscolor; },