i should go to sleep
[freeside.git] / httemplate / search / report_receivables.cgi
1 <%
2
3   sub owed {
4     my($start, $end, %opt) = @_;
5
6     my @where = ();
7
8     #handle start and end ranges
9
10     #24h * 60m * 60s
11     push @where, "cust_bill._date <= extract(epoch from now())-".
12                  ($start * 86400)
13       if $start;
14   
15     push @where, "cust_bill._date > extract(epoch from now()) - ".
16                  ($end * 86400)
17       if $end;
18
19     #handle 'cust' option
20   
21     push @where, "cust_main.custnum = cust_bill.custnum"
22       if $opt{'cust'};
23
24     #handle 'agentnum' option
25     my $join = '';
26     if ( $opt{'agentnum'} ) {
27       $join = 'LEFT JOIN cust_main USING ( custnum )';
28       push @where, "agentnum = '$opt{'agentnum'}'";
29     }
30
31     my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
32   
33     my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
34
35     my $charged = <<END;
36   sum( charged
37        - coalesce(
38            ( select sum(amount) from cust_bill_pay
39              where cust_bill.invnum = cust_bill_pay.invnum )
40            ,0
41          )
42        - coalesce(
43            ( select sum(amount) from cust_credit_bill
44              where cust_bill.invnum = cust_credit_bill.invnum )
45            ,0
46          )
47
48      )
49 END
50
51     "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
52   
53   }
54
55   my @ranges = (
56     [  0, 30 ],
57     [ 30, 60 ],
58     [ 60, 90 ],
59     [ 90,  0 ],
60     [  0,  0 ],
61   );
62
63   my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
64
65   my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
66
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;
71
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
78 END
79
80   my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
81
82   my $agentnum = '';
83   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
84     $agentnum = $1;
85     $where .= " AND agentnum = '$agentnum' ";
86   }
87
88   #here is the agent virtualization
89   $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
90
91   my $count_sql = "select count(*) from cust_main $where";
92
93   my $sql_query = {
94     'table'     => 'cust_main',
95     'hashref'   => {},
96     'select'    => "*, $owed_cols, $packages_cols",
97     'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
98   };
99
100   my $total_sql = "select ".
101                     join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
102
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();
106
107   my $conf = new FS::Conf;
108   my $money_char = $conf->config('money_char') || '$';
109
110   my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
111              'crrrrr';
112
113   my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
114
115   my $status_statuscol = sub {
116     #conceptual false laziness with cust_main::status...
117     my $row = shift;
118
119     my $status = 'unknown';
120     if ( $self->num_pkgs == 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'
130     }
131
132     ( ucfirst($status), $FS::cust_main::statuscolor{$status} );
133   };
134
135
136 %><%= include( 'elements/search.html',
137                  'title'       => 'Accounts Receivable Aging Summary',
138                  'name'        => 'customers',
139                  'query'       => $sql_query,
140                  'count_query' => $count_sql,
141                  'header'      => [
142                                     FS::UI::Web::cust_header(),
143                                     'Status', # (me)',
144                                     #'Status', # (cust_main)',
145                                     '0-30',
146                                     '30-60',
147                                     '60-90',
148                                     '90+',
149                                     'Total',
150                                   ],
151                  'footer'      => [
152                                     'Total',
153                                     ( map '',
154                                           ( 1 .. 
155                                             scalar(FS::UI::Web::cust_header()-1)
156                                           )
157                                     ),
158                                     '',
159                                     #'',
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'} ),
170                                   ],
171                  'fields'      => [
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') ) },
185                                   ],
186                  'links'       => [
187                                     ( map $clink, FS::UI::Web::cust_header() ),
188                                     '',
189                                     #'',
190                                     '',
191                                     '',
192                                     '',
193                                     '',
194                                     '',
195                                   ],
196                  #'align'       => 'rlccrrrrr',
197                  'align'       => $align,
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', ],
204                  'color'       => [
205                                     ( map '', FS::UI::Web::cust_header() ),
206                                     sub { (shift->status_statuscol)[1] },
207                                     #sub { shift->statuscolor; },
208                                     '',
209                                     '',
210                                     '',
211                                     '',
212                                     '',
213                                   ],
214
215              )
216 %>