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()) - ".
19 % #handle 'cust' option
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";
35 % my $charged = <<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 $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
67 % my $active_sql = FS::cust_pkg->active_sql;
68 % my $inactive_sql = FS::cust_pkg->inactive_sql;
69 % my $suspended_sql = FS::cust_pkg->suspended_sql;
70 % my $cancelled_sql = FS::cust_pkg->cancelled_sql;
72 % my $packages_cols = <<END;
73 % ( $select_count_pkgs ) AS num_pkgs_sql,
74 % ( $select_count_pkgs AND $active_sql ) AS active_pkgs,
75 % ( $select_count_pkgs AND $inactive_sql ) AS inactive_pkgs,
76 % ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
77 % ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
80 % my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
83 % if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
85 % $where .= " AND agentnum = '$agentnum' ";
88 % #here is the agent virtualization
89 % $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
91 % my $count_sql = "select count(*) from cust_main $where";
94 % 'table' => 'cust_main',
96 % 'select' => "*, $owed_cols, $packages_cols",
97 % 'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
100 % my $total_sql = "select ".
101 % join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
103 % my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
104 % $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
105 % my $row = $total_sth->fetchrow_hashref();
107 % my $conf = new FS::Conf;
108 % my $money_char = $conf->config('money_char') || '$';
110 % my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
113 % my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
115 % my $status_statuscol = sub {
116 % #conceptual false laziness with cust_main::status...
119 % my $status = 'unknown';
120 % if ( $row->num_pkgs_sql == 0 ) {
121 % $status = 'prospect';
122 % } elsif ( $row->active_pkgs > 0 ) {
123 % $status = 'active';
124 % } elsif ( $row->inactive_pkgs > 0 ) {
125 % $status = 'inactive';
126 % } elsif ( $row->suspended_pkgs > 0 ) {
127 % $status = 'suspended';
128 % } elsif ( $row->cancelled_pkgs > 0 ) {
129 % $status = 'cancelled'
132 % ( ucfirst($status), $FS::cust_main::statuscolor{$status} );
137 <% include( 'elements/search.html',
138 'title' => 'Accounts Receivable Aging Summary',
139 'name' => 'customers',
140 'query' => $sql_query,
141 'count_query' => $count_sql,
143 FS::UI::Web::cust_header(),
145 #'Status', # (cust_main)',
156 scalar(FS::UI::Web::cust_header()-1)
161 sprintf( $money_char.'%.2f',
162 $row->{'owed_0_30'} ),
163 sprintf( $money_char.'%.2f',
164 $row->{'owed_30_60'} ),
165 sprintf( $money_char.'%.2f',
166 $row->{'owed_60_90'} ),
167 sprintf( $money_char.'%.2f',
168 $row->{'owed_90_0'} ),
169 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
170 $row->{'owed_0_0'} ),
173 \&FS::UI::Web::cust_fields,
174 sub { ( &{$status_statuscol}(shift) )[0] },
175 #sub { ucfirst(shift->status) },
176 sub { sprintf( $money_char.'%.2f',
177 shift->get('owed_0_30') ) },
178 sub { sprintf( $money_char.'%.2f',
179 shift->get('owed_30_60') ) },
180 sub { sprintf( $money_char.'%.2f',
181 shift->get('owed_60_90') ) },
182 sub { sprintf( $money_char.'%.2f',
183 shift->get('owed_90_0') ) },
184 sub { sprintf( $money_char.'%.2f',
185 shift->get('owed_0_0') ) },
188 ( map $clink, FS::UI::Web::cust_header() ),
197 #'align' => 'rlccrrrrr',
199 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
200 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
201 'size' => [ ( map '', FS::UI::Web::cust_header() ),
202 '-1', '', '', '', '', '', ],
203 'style' => [ ( map '', FS::UI::Web::cust_header() ),
204 'b', '', '', '', '', 'b', ],
206 ( map '', FS::UI::Web::cust_header() ),
207 sub { ( &{$status_statuscol}(shift) )[1] },
208 #sub { shift->statuscolor; },