Pg 8.1 fix was incorrect and broke things, this should actually work
[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 $recurring = <<END;
66         '0' != ( select freq from part_pkg
67                    where cust_pkg.pkgpart = part_pkg.pkgpart )
68 END
69
70   my $packages_cols = <<END;
71
72        ( select count(*) from cust_pkg
73            where cust_main.custnum = cust_pkg.custnum
74              and $recurring
75              and ( cancel = 0 or cancel is null )
76        ) as uncancelled_pkgs,
77
78        ( select count(*) from cust_pkg
79            where cust_main.custnum = cust_pkg.custnum
80              and $recurring
81              and ( cancel = 0 or cancel is null )
82              and ( susp = 0 or susp is null )
83        ) as active_pkgs
84
85 END
86
87   my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
88
89   my $agentnum = '';
90   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
91     $agentnum = $1;
92     $where .= " AND agentnum = '$agentnum' ";
93   }
94
95   my $count_sql = "select count(*) from cust_main $where";
96
97   my $sql_query = {
98     'table'     => 'cust_main',
99     'hashref'   => {},
100     'select'    => "*, $owed_cols, $packages_cols",
101     'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
102   };
103
104   my $total_sql = "select ".
105                     join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
106
107   my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
108   $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
109   my $row = $total_sth->fetchrow_hashref();
110
111   my $conf = new FS::Conf;
112   my $money_char = $conf->config('money_char') || '$';
113
114   my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
115              'crrrrr';
116
117   my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
118
119 %><%= include( 'elements/search.html',
120                  'title'       => 'Accounts Receivable Aging Summary',
121                  'name'        => 'customers',
122                  'query'       => $sql_query,
123                  'count_query' => $count_sql,
124                  'header'      => [
125                                     FS::UI::Web::cust_header(),
126                                     'Status', # (me)',
127                                     #'Status', # (cust_main)',
128                                     '0-30',
129                                     '30-60',
130                                     '60-90',
131                                     '90+',
132                                     'Total',
133                                   ],
134                  'footer'      => [
135                                     'Total',
136                                     ( map '',
137                                           ( 1 .. 
138                                             scalar(FS::UI::Web::cust_header()-1)
139                                           )
140                                     ),
141                                     '',
142                                     #'',
143                                     sprintf( $money_char.'%.2f',
144                                              $row->{'owed_0_30'} ),
145                                     sprintf( $money_char.'%.2f',
146                                              $row->{'owed_30_60'} ),
147                                     sprintf( $money_char.'%.2f',
148                                              $row->{'owed_60_90'} ),
149                                     sprintf( $money_char.'%.2f',
150                                              $row->{'owed_90_0'} ),
151                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
152                                              $row->{'owed_0_0'} ),
153                                   ],
154                  'fields'      => [
155                                     \&FS::UI::Web::cust_fields,
156                                     sub {
157                                           my $row = shift;
158                                           my $status = 'Cancelled';
159                                           my $statuscol = 'FF0000';
160                                           if ( $row->uncancelled_pkgs ) {
161                                             $status = 'Suspended';
162                                             $statuscol = 'FF9900';
163                                             if ( $row->active_pkgs ) {
164                                               $status = 'Active';
165                                               $statuscol = '00CC00';
166                                             }
167                                           }
168                                           $status;
169                                         },
170                                     #sub { ucfirst(shift->status) },
171                                     sub { sprintf( $money_char.'%.2f',
172                                                    shift->get('owed_0_30') ) },
173                                     sub { sprintf( $money_char.'%.2f',
174                                                    shift->get('owed_30_60') ) },
175                                     sub { sprintf( $money_char.'%.2f',
176                                                    shift->get('owed_60_90') ) },
177                                     sub { sprintf( $money_char.'%.2f',
178                                                    shift->get('owed_90_0') ) },
179                                     sub { sprintf( $money_char.'%.2f',
180                                                    shift->get('owed_0_0') ) },
181                                   ],
182                  'links'       => [
183                                     ( map $clink, FS::UI::Web::cust_header() ),
184                                     '',
185                                     #'',
186                                     '',
187                                     '',
188                                     '',
189                                     '',
190                                     '',
191                                   ],
192                  #'align'       => 'rlccrrrrr',
193                  'align'       => $align,
194                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
195                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
196                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
197                                     '-1', '', '', '', '',  '', ],
198                  'style'       => [ ( map '', FS::UI::Web::cust_header() ),
199                                     'b', '', '', '', '', 'b', ],
200                  'color'       => [
201                                     ( map '', FS::UI::Web::cust_header() ),
202                                     sub {  
203                                           my $row = shift;
204                                           my $status = 'Cancelled';
205                                           my $statuscol = 'FF0000';
206                                           if ( $row->uncancelled_pkgs ) {
207                                             $status = 'Suspended';
208                                             $statuscol = 'FF9900';
209                                             if ( $row->active_pkgs ) {
210                                               $status = 'Active';
211                                               $statuscol = '00CC00';
212                                             }
213                                           }
214                                            $statuscol;
215                                         },
216                                     #sub { shift->statuscolor; },
217                                     '',
218                                     '',
219                                     '',
220                                     '',
221                                     '',
222                                   ],
223
224              )
225 %>