fix multiple-agent virtualization properly for these reports
[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   #here is the agent virtualization
96   $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
97
98   my $count_sql = "select count(*) from cust_main $where";
99
100   my $sql_query = {
101     'table'     => 'cust_main',
102     'hashref'   => {},
103     'select'    => "*, $owed_cols, $packages_cols",
104     'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
105   };
106
107   my $total_sql = "select ".
108                     join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
109
110   my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
111   $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
112   my $row = $total_sth->fetchrow_hashref();
113
114   my $conf = new FS::Conf;
115   my $money_char = $conf->config('money_char') || '$';
116
117   my $align = join('', map { /#/ ? 'r' : 'l' } FS::UI::Web::cust_header() ).
118              'crrrrr';
119
120   my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
121
122 %><%= include( 'elements/search.html',
123                  'title'       => 'Accounts Receivable Aging Summary',
124                  'name'        => 'customers',
125                  'query'       => $sql_query,
126                  'count_query' => $count_sql,
127                  'header'      => [
128                                     FS::UI::Web::cust_header(),
129                                     'Status', # (me)',
130                                     #'Status', # (cust_main)',
131                                     '0-30',
132                                     '30-60',
133                                     '60-90',
134                                     '90+',
135                                     'Total',
136                                   ],
137                  'footer'      => [
138                                     'Total',
139                                     ( map '',
140                                           ( 1 .. 
141                                             scalar(FS::UI::Web::cust_header()-1)
142                                           )
143                                     ),
144                                     '',
145                                     #'',
146                                     sprintf( $money_char.'%.2f',
147                                              $row->{'owed_0_30'} ),
148                                     sprintf( $money_char.'%.2f',
149                                              $row->{'owed_30_60'} ),
150                                     sprintf( $money_char.'%.2f',
151                                              $row->{'owed_60_90'} ),
152                                     sprintf( $money_char.'%.2f',
153                                              $row->{'owed_90_0'} ),
154                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
155                                              $row->{'owed_0_0'} ),
156                                   ],
157                  'fields'      => [
158                                     \&FS::UI::Web::cust_fields,
159                                     sub {
160                                           my $row = shift;
161                                           my $status = 'Cancelled';
162                                           my $statuscol = 'FF0000';
163                                           if ( $row->uncancelled_pkgs ) {
164                                             $status = 'Suspended';
165                                             $statuscol = 'FF9900';
166                                             if ( $row->active_pkgs ) {
167                                               $status = 'Active';
168                                               $statuscol = '00CC00';
169                                             }
170                                           }
171                                           $status;
172                                         },
173                                     #sub { ucfirst(shift->status) },
174                                     sub { sprintf( $money_char.'%.2f',
175                                                    shift->get('owed_0_30') ) },
176                                     sub { sprintf( $money_char.'%.2f',
177                                                    shift->get('owed_30_60') ) },
178                                     sub { sprintf( $money_char.'%.2f',
179                                                    shift->get('owed_60_90') ) },
180                                     sub { sprintf( $money_char.'%.2f',
181                                                    shift->get('owed_90_0') ) },
182                                     sub { sprintf( $money_char.'%.2f',
183                                                    shift->get('owed_0_0') ) },
184                                   ],
185                  'links'       => [
186                                     ( map $clink, FS::UI::Web::cust_header() ),
187                                     '',
188                                     #'',
189                                     '',
190                                     '',
191                                     '',
192                                     '',
193                                     '',
194                                   ],
195                  #'align'       => 'rlccrrrrr',
196                  'align'       => $align,
197                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
198                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
199                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
200                                     '-1', '', '', '', '',  '', ],
201                  'style'       => [ ( map '', FS::UI::Web::cust_header() ),
202                                     'b', '', '', '', '', 'b', ],
203                  'color'       => [
204                                     ( map '', FS::UI::Web::cust_header() ),
205                                     sub {  
206                                           my $row = shift;
207                                           my $status = 'Cancelled';
208                                           my $statuscol = 'FF0000';
209                                           if ( $row->uncancelled_pkgs ) {
210                                             $status = 'Suspended';
211                                             $statuscol = 'FF9900';
212                                             if ( $row->active_pkgs ) {
213                                               $status = 'Active';
214                                               $statuscol = '00CC00';
215                                             }
216                                           }
217                                            $statuscol;
218                                         },
219                                     #sub { shift->statuscolor; },
220                                     '',
221                                     '',
222                                     '',
223                                     '',
224                                     '',
225                                   ],
226
227              )
228 %>