This commit was generated by cvs2svn to compensate for changes in r6255,
[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                                     '0-30',
9                                     '30-60',
10                                     '60-90',
11                                     '90+',
12                                     'Total',
13                                   ],
14                  'footer'      => [
15                                     'Total',
16                                     ( map '',
17                                           ( 1 .. 
18                                             scalar(FS::UI::Web::cust_header()-1)
19                                           )
20                                     ),
21                                     sprintf( $money_char.'%.2f',
22                                              $row->{'balance_0_30'} ),
23                                     sprintf( $money_char.'%.2f',
24                                              $row->{'balance_30_60'} ),
25                                     sprintf( $money_char.'%.2f',
26                                              $row->{'balance_60_90'} ),
27                                     sprintf( $money_char.'%.2f',
28                                              $row->{'balance_90_0'} ),
29                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
30                                              $row->{'balance_0_0'} ),
31                                   ],
32                  'fields'      => [
33                                     \&FS::UI::Web::cust_fields,
34                                     format_balance('0_30'),
35                                     format_balance('30_60'),
36                                     format_balance('60_90'),
37                                     format_balance('90_0'),
38                                     format_balance('0_0'),
39                                   ],
40                  'links'       => [
41                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
42                                           FS::UI::Web::cust_header()
43                                     ),
44                                     '',
45                                     '',
46                                     '',
47                                     '',
48                                     '',
49                                   ],
50                  #'align'       => 'rlccrrrrr',
51                  'align'       => FS::UI::Web::cust_aligns(). 'rrrrr',
52                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
53                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
54                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
55                                     #'-1', '', '', '', '',  '', ],
56                                     '', '', '', '',  '', ],
57                  'style'       => [ FS::UI::Web::cust_styles(),
58                                     #'b', '', '', '', '', 'b', ],
59                                     '', '', '', '', 'b', ],
60                  'color'       => [
61                                     FS::UI::Web::cust_colors(),
62                                     '',
63                                     '',
64                                     '',
65                                     '',
66                                     '',
67                                   ],
68
69              )
70 %>
71 <%init>
72
73 die "access denied"
74   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
75
76 my @ranges = (
77   [  0, 30 ],
78   [ 30, 60 ],
79   [ 60, 90 ],
80   [ 90,  0 ],
81   [  0,  0 ],
82 );
83
84 my $owed_cols = join(',', map balance( @$_ ), @ranges );
85
86 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
87
88 my $active_sql    = FS::cust_pkg->active_sql;
89 my $inactive_sql  = FS::cust_pkg->inactive_sql;
90 my $suspended_sql = FS::cust_pkg->suspended_sql;
91 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
92
93 my $packages_cols = <<END;
94      ( $select_count_pkgs                    ) AS num_pkgs_sql,
95      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
96      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
97      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
98      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
99 END
100
101 my @where = ();
102
103 unless ( $cgi->param('all_customers') ) {
104
105   my $days = 0;
106   if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
107     $days = $1;
108   }
109
110   push @where, balance($days, 0, 'no_as'=>1). " > 0";
111
112 }
113
114 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
115   my $agentnum = $1;
116   push @where, "agentnum = $agentnum";
117 }
118
119 #here is the agent virtualization
120 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql;
121
122 my $where = join(' AND ', @where);
123 $where = "WHERE $where" if $where;
124
125 my $count_sql = "select count(*) from cust_main $where";
126
127 my $sql_query = {
128   'table'     => 'cust_main',
129   'hashref'   => {},
130   'select'    => "*, $owed_cols, $packages_cols",
131   'extra_sql' => $where,
132   'order_by'  => "order by coalesce(lower(company), ''), lower(last)",
133 };
134
135 my $join = 'LEFT JOIN cust_main USING ( custnum )';
136
137 my $total_sql = "SELECT ".
138   join(',', map balance( @$_, total=>1, join=>$join, where=>\@where ), @ranges);
139
140 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
141 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
142 my $row = $total_sth->fetchrow_hashref();
143
144 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
145
146 </%init>
147 <%once>
148
149 my $conf = new FS::Conf;
150
151 my $money_char = $conf->config('money_char') || '$';
152
153 #Example:
154 #
155 # my $balance = balance(
156 #   $start, $end, 
157 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
158 #                 #or 0 / omit when using in a SELECT clause as a column
159 #                 #  ("AS balance_$start_$end")
160 #   #options for totals
161 #   'total'  => 1, #set to true to remove all customer comparison clauses
162 #   'join'   => $join,   #JOIN clause
163 #   'where'  => \@where, #WHERE clause hashref (elements "AND"ed together)
164 # )
165
166 sub balance {
167   my($start, $end, %opt) = @_;
168
169   my $as = $opt{'no_as'} ? '' : " AS balance_${start}_$end";
170
171   #handle start and end ranges (86400 = 24h * 60m * 60s)
172   my $str2time = str2time_sql;
173   $start = $start ? "( $str2time now() ) - ".($start * 86400). ' )' : '';
174   $end   = $end   ? "( $str2time now() ) - ".($end   * 86400). ' )' : '';
175
176   $opt{'unapplied_date'} = 1;
177
178   FS::cust_main->balance_date_sql( $start, $end, %opt ). $as;
179
180 }
181
182 sub format_balance { #closures help alot
183   my $range = shift;
184   sub { sprintf( $money_char.'%.2f', shift->get("balance_$range") ) };
185 }
186
187 </%once>