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 $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->inactive_sql;
70 my $cancelled_sql = FS::cust_pkg->inactive_sql;
72 my $packages_cols = <<END;
73 ( $select_count_pkgs ) AS num_pkgs,
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 ( $self->num_pkgs == 0 ) {
121 $status = 'prospect';
122 } elsif ( $row->active_pkgs > 0 ) {
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} );
136 %><%= include( 'elements/search.html',
137 'title' => 'Accounts Receivable Aging Summary',
138 'name' => 'customers',
139 'query' => $sql_query,
140 'count_query' => $count_sql,
142 FS::UI::Web::cust_header(),
144 #'Status', # (cust_main)',
155 scalar(FS::UI::Web::cust_header()-1)
160 sprintf( $money_char.'%.2f',
161 $row->{'owed_0_30'} ),
162 sprintf( $money_char.'%.2f',
163 $row->{'owed_30_60'} ),
164 sprintf( $money_char.'%.2f',
165 $row->{'owed_60_90'} ),
166 sprintf( $money_char.'%.2f',
167 $row->{'owed_90_0'} ),
168 sprintf( '<b>'. $money_char.'%.2f'. '</b>',
169 $row->{'owed_0_0'} ),
172 \&FS::UI::Web::cust_fields,
173 sub { (shift->status_statuscol)[0] },
174 #sub { ucfirst(shift->status) },
175 sub { sprintf( $money_char.'%.2f',
176 shift->get('owed_0_30') ) },
177 sub { sprintf( $money_char.'%.2f',
178 shift->get('owed_30_60') ) },
179 sub { sprintf( $money_char.'%.2f',
180 shift->get('owed_60_90') ) },
181 sub { sprintf( $money_char.'%.2f',
182 shift->get('owed_90_0') ) },
183 sub { sprintf( $money_char.'%.2f',
184 shift->get('owed_0_0') ) },
187 ( map $clink, FS::UI::Web::cust_header() ),
196 #'align' => 'rlccrrrrr',
198 #'size' => [ '', '', '-1', '-1', '', '', '', '', '', ],
199 #'style' => [ '', '', 'b', 'b', '', '', '', '', 'b', ],
200 'size' => [ ( map '', FS::UI::Web::cust_header() ),
201 '-1', '', '', '', '', '', ],
202 'style' => [ ( map '', FS::UI::Web::cust_header() ),
203 'b', '', '', '', '', 'b', ],
205 ( map '', FS::UI::Web::cust_header() ),
206 sub { (shift->status_statuscol)[1] },
207 #sub { shift->statuscolor; },