Will things ever be the same again?
[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_sql,
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 ( $row->num_pkgs_sql == 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 %
137 <% include( 'elements/search.html',
138                  'title'       => 'Accounts Receivable Aging Summary',
139                  'name'        => 'customers',
140                  'query'       => $sql_query,
141                  'count_query' => $count_sql,
142                  'header'      => [
143                                     FS::UI::Web::cust_header(),
144                                     'Status', # (me)',
145                                     #'Status', # (cust_main)',
146                                     '0-30',
147                                     '30-60',
148                                     '60-90',
149                                     '90+',
150                                     'Total',
151                                   ],
152                  'footer'      => [
153                                     'Total',
154                                     ( map '',
155                                           ( 1 .. 
156                                             scalar(FS::UI::Web::cust_header()-1)
157                                           )
158                                     ),
159                                     '',
160                                     #'',
161                                     sprintf( $money_char.'%.2f',
162                                              $row->{'owed_0_30'} ),
163                                     sprintf( $money_char.'%.2f',
164                                              $row->{'owed_30_60'} ),
165                                     sprintf( $money_char.'%.2f',
166                                              $row->{'owed_60_90'} ),
167                                     sprintf( $money_char.'%.2f',
168                                              $row->{'owed_90_0'} ),
169                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
170                                              $row->{'owed_0_0'} ),
171                                   ],
172                  'fields'      => [
173                                     \&FS::UI::Web::cust_fields,
174                                     sub { ( &{$status_statuscol}(shift) )[0] },
175                                     #sub { ucfirst(shift->status) },
176                                     sub { sprintf( $money_char.'%.2f',
177                                                    shift->get('owed_0_30') ) },
178                                     sub { sprintf( $money_char.'%.2f',
179                                                    shift->get('owed_30_60') ) },
180                                     sub { sprintf( $money_char.'%.2f',
181                                                    shift->get('owed_60_90') ) },
182                                     sub { sprintf( $money_char.'%.2f',
183                                                    shift->get('owed_90_0') ) },
184                                     sub { sprintf( $money_char.'%.2f',
185                                                    shift->get('owed_0_0') ) },
186                                   ],
187                  'links'       => [
188                                     ( map $clink, FS::UI::Web::cust_header() ),
189                                     '',
190                                     #'',
191                                     '',
192                                     '',
193                                     '',
194                                     '',
195                                     '',
196                                   ],
197                  #'align'       => 'rlccrrrrr',
198                  'align'       => $align,
199                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
200                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
201                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
202                                     '-1', '', '', '', '',  '', ],
203                  'style'       => [ ( map '', FS::UI::Web::cust_header() ),
204                                     'b', '', '', '', '', 'b', ],
205                  'color'       => [
206                                     ( map '', FS::UI::Web::cust_header() ),
207                                     sub { ( &{$status_statuscol}(shift) )[1] },
208                                     #sub { shift->statuscolor; },
209                                     '',
210                                     '',
211                                     '',
212                                     '',
213                                     '',
214                                   ],
215
216              )
217 %>