This commit was generated by cvs2svn to compensate for changes in r5562,
[freeside.git] / httemplate / search / report_receivables.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Accounts Receivable Aging Summary',
3                  'name'        => 'customers',
4                  'query'       => $sql_query,
5                  'count_query' => $count_sql,
6                  'header'      => [
7                                     FS::UI::Web::cust_header(),
8                                     #'Status', # (me)',
9                                     #'Status', # (cust_main)',
10                                     '0-30',
11                                     '30-60',
12                                     '60-90',
13                                     '90+',
14                                     'Total',
15                                   ],
16                  'footer'      => [
17                                     'Total',
18                                     ( map '',
19                                           ( 1 .. 
20                                             scalar(FS::UI::Web::cust_header()-1)
21                                           )
22                                     ),
23                                     #'',
24                                     #'',
25                                     sprintf( $money_char.'%.2f',
26                                              $row->{'owed_0_30'} ),
27                                     sprintf( $money_char.'%.2f',
28                                              $row->{'owed_30_60'} ),
29                                     sprintf( $money_char.'%.2f',
30                                              $row->{'owed_60_90'} ),
31                                     sprintf( $money_char.'%.2f',
32                                              $row->{'owed_90_0'} ),
33                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
34                                              $row->{'owed_0_0'} ),
35                                   ],
36                  'fields'      => [
37                                     \&FS::UI::Web::cust_fields,
38                                     #sub { ( &{$status_statuscol}(shift) )[0] },
39                                     #sub { ucfirst(shift->status) },
40                                     sub { sprintf( $money_char.'%.2f',
41                                                    shift->get('owed_0_30') ) },
42                                     sub { sprintf( $money_char.'%.2f',
43                                                    shift->get('owed_30_60') ) },
44                                     sub { sprintf( $money_char.'%.2f',
45                                                    shift->get('owed_60_90') ) },
46                                     sub { sprintf( $money_char.'%.2f',
47                                                    shift->get('owed_90_0') ) },
48                                     sub { sprintf( $money_char.'%.2f',
49                                                    shift->get('owed_0_0') ) },
50                                   ],
51                  'links'       => [
52                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
53                                           FS::UI::Web::cust_header()
54                                     ),
55                                     #'',
56                                     #'',
57                                     '',
58                                     '',
59                                     '',
60                                     '',
61                                     '',
62                                   ],
63                  #'align'       => 'rlccrrrrr',
64                  'align'       => FS::UI::Web::cust_aligns(). 'rrrrr',
65                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
66                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
67                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
68                                     #'-1', '', '', '', '',  '', ],
69                                     '', '', '', '',  '', ],
70                  'style'       => [ FS::UI::Web::cust_styles(),
71                                     #'b', '', '', '', '', 'b', ],
72                                     '', '', '', '', 'b', ],
73                  'color'       => [
74                                     FS::UI::Web::cust_colors(),
75                                     #sub { ( &{$status_statuscol}(shift) )[1] },
76                                     #sub { shift->statuscolor; },
77                                     '',
78                                     '',
79                                     '',
80                                     '',
81                                     '',
82                                   ],
83
84              )
85 %>
86 <%once>
87
88 sub owed {
89   my($start, $end, %opt) = @_;
90
91   my @where = ();
92
93   #handle start and end ranges
94
95   #24h * 60m * 60s
96   push @where, "cust_bill._date <= extract(epoch from now())-".
97                ($start * 86400)
98     if $start;
99
100   push @where, "cust_bill._date > extract(epoch from now()) - ".
101                ($end * 86400)
102     if $end;
103
104   #handle 'cust' option
105
106   push @where, "cust_main.custnum = cust_bill.custnum"
107     if $opt{'cust'};
108
109   #handle 'agentnum' option
110   my $join = '';
111   if ( $opt{'agentnum'} ) {
112     $join = 'LEFT JOIN cust_main USING ( custnum )';
113     push @where, "agentnum = '$opt{'agentnum'}'";
114   }
115
116   my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
117
118   my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
119
120   my $charged = <<END;
121 sum( charged
122      - coalesce(
123          ( select sum(amount) from cust_bill_pay
124            where cust_bill.invnum = cust_bill_pay.invnum )
125          ,0
126        )
127      - coalesce(
128          ( select sum(amount) from cust_credit_bill
129            where cust_bill.invnum = cust_credit_bill.invnum )
130          ,0
131        )
132
133    )
134 END
135
136   "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
137
138 }
139
140 </%once>
141 <%init>
142
143 die "access denied"
144   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
145
146 my @ranges = (
147   [  0, 30 ],
148   [ 30, 60 ],
149   [ 60, 90 ],
150   [ 90,  0 ],
151   [  0,  0 ],
152 );
153
154 my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
155
156 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
157
158 my $active_sql    = FS::cust_pkg->active_sql;
159 my $inactive_sql  = FS::cust_pkg->inactive_sql;
160 my $suspended_sql = FS::cust_pkg->suspended_sql;
161 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
162
163 my $packages_cols = <<END;
164      ( $select_count_pkgs                    ) AS num_pkgs_sql,
165      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
166      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
167      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
168      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
169 END
170
171 my $days = 0;
172 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
173   $days = $1;
174 }
175
176 #my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
177 my $where = "where ". owed($days, 0, 'cust'=>1, 'noas'=>1). " > 0";
178
179 my $agentnum = '';
180 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
181   $agentnum = $1;
182   $where .= " AND agentnum = '$agentnum' ";
183 }
184
185 #here is the agent virtualization
186 $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
187
188 my $count_sql = "select count(*) from cust_main $where";
189
190 my $sql_query = {
191   'table'     => 'cust_main',
192   'hashref'   => {},
193   'select'    => "*, $owed_cols, $packages_cols",
194   'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
195 };
196
197 my $total_sql = "select ".
198                   join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
199
200 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
201 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
202 my $row = $total_sth->fetchrow_hashref();
203
204 my $conf = new FS::Conf;
205 my $money_char = $conf->config('money_char') || '$';
206
207 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
208
209 my $status_statuscol = sub {
210   #conceptual false laziness with cust_main::status...
211   my $row = shift;
212
213   my $status = 'unknown';
214   if ( $row->num_pkgs_sql == 0 ) {
215     $status = 'prospect';
216   } elsif ( $row->active_pkgs    > 0 ) {
217     $status = 'active';
218   } elsif ( $row->inactive_pkgs  > 0 ) {
219     $status = 'inactive';
220   } elsif ( $row->suspended_pkgs > 0 ) {
221     $status = 'suspended';
222   } elsif ( $row->cancelled_pkgs > 0 ) {
223     $status = 'cancelled'
224   }
225
226   ( ucfirst($status), $FS::cust_main::statuscolor{$status} );
227 };
228
229 </%init>