mysql me harder
[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->{'owed_0_30'} ),
23                                     sprintf( $money_char.'%.2f',
24                                              $row->{'owed_30_60'} ),
25                                     sprintf( $money_char.'%.2f',
26                                              $row->{'owed_60_90'} ),
27                                     sprintf( $money_char.'%.2f',
28                                              $row->{'owed_90_0'} ),
29                                     sprintf( '<b>'. $money_char.'%.2f'. '</b>',
30                                              $row->{'owed_0_0'} ),
31                                   ],
32                  'fields'      => [
33                                     \&FS::UI::Web::cust_fields,
34                                     sub { sprintf( $money_char.'%.2f',
35                                                    shift->get('owed_0_30') ) },
36                                     sub { sprintf( $money_char.'%.2f',
37                                                    shift->get('owed_30_60') ) },
38                                     sub { sprintf( $money_char.'%.2f',
39                                                    shift->get('owed_60_90') ) },
40                                     sub { sprintf( $money_char.'%.2f',
41                                                    shift->get('owed_90_0') ) },
42                                     sub { sprintf( $money_char.'%.2f',
43                                                    shift->get('owed_0_0') ) },
44                                   ],
45                  'links'       => [
46                                     ( map { $_ ne 'Cust. Status' ? $clink : '' }
47                                           FS::UI::Web::cust_header()
48                                     ),
49                                     '',
50                                     '',
51                                     '',
52                                     '',
53                                     '',
54                                   ],
55                  #'align'       => 'rlccrrrrr',
56                  'align'       => FS::UI::Web::cust_aligns(). 'rrrrr',
57                  #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
58                  #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
59                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
60                                     #'-1', '', '', '', '',  '', ],
61                                     '', '', '', '',  '', ],
62                  'style'       => [ FS::UI::Web::cust_styles(),
63                                     #'b', '', '', '', '', 'b', ],
64                                     '', '', '', '', 'b', ],
65                  'color'       => [
66                                     FS::UI::Web::cust_colors(),
67                                     '',
68                                     '',
69                                     '',
70                                     '',
71                                     '',
72                                   ],
73
74              )
75 %>
76 <%once>
77
78 sub owed {
79   my($start, $end, %opt) = @_;
80
81   my @where = ();
82
83   #handle start and end ranges
84
85   my $str2time = str2time;
86
87   #24h * 60m * 60s
88   push @where, "cust_bill._date <= $str2time now() ) - ". ($start * 86400)
89     if $start;
90
91   push @where, "cust_bill._date >  $str2time now() ) - ". ($end * 86400)
92     if $end;
93
94   #handle 'cust' option
95
96   push @where, "cust_main.custnum = cust_bill.custnum"
97     if $opt{'cust'};
98
99   #handle 'agentnum' option
100   my $join = '';
101   if ( $opt{'agentnum'} ) {
102     $join = 'LEFT JOIN cust_main USING ( custnum )';
103     push @where, "agentnum = '$opt{'agentnum'}'";
104   }
105
106   my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
107
108   my $as = $opt{'noas'} ? '' : "as owed_${start}_$end";
109
110   my $charged = <<END;
111 sum( charged
112      - coalesce(
113          ( select sum(amount) from cust_bill_pay
114            where cust_bill.invnum = cust_bill_pay.invnum )
115          ,0
116        )
117      - coalesce(
118          ( select sum(amount) from cust_credit_bill
119            where cust_bill.invnum = cust_credit_bill.invnum )
120          ,0
121        )
122
123    )
124 END
125
126   "coalesce( ( select $charged from cust_bill $join $where ) ,0 ) $as";
127
128 }
129
130 </%once>
131 <%init>
132
133 die "access denied"
134   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
135
136 my @ranges = (
137   [  0, 30 ],
138   [ 30, 60 ],
139   [ 60, 90 ],
140   [ 90,  0 ],
141   [  0,  0 ],
142 );
143
144 my $owed_cols = join(',', map owed( @$_, 'cust'=>1 ), @ranges );
145
146 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
147
148 my $active_sql    = FS::cust_pkg->active_sql;
149 my $inactive_sql  = FS::cust_pkg->inactive_sql;
150 my $suspended_sql = FS::cust_pkg->suspended_sql;
151 my $cancelled_sql = FS::cust_pkg->cancelled_sql;
152
153 my $packages_cols = <<END;
154      ( $select_count_pkgs                    ) AS num_pkgs_sql,
155      ( $select_count_pkgs AND $active_sql    ) AS active_pkgs,
156      ( $select_count_pkgs AND $inactive_sql  ) AS inactive_pkgs,
157      ( $select_count_pkgs AND $suspended_sql ) AS suspended_pkgs,
158      ( $select_count_pkgs AND $cancelled_sql ) AS cancelled_pkgs
159 END
160
161 my $days = 0;
162 if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
163   $days = $1;
164 }
165
166 #my $where = "where ". owed(0, 0, 'cust'=>1, 'noas'=>1). " > 0";
167 my $where = "where ". owed($days, 0, 'cust'=>1, 'noas'=>1). " > 0";
168
169 my $agentnum = '';
170 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
171   $agentnum = $1;
172   $where .= " AND agentnum = '$agentnum' ";
173 }
174
175 #here is the agent virtualization
176 $where .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
177
178 my $count_sql = "select count(*) from cust_main $where";
179
180 my $sql_query = {
181   'table'     => 'cust_main',
182   'hashref'   => {},
183   'select'    => "*, $owed_cols, $packages_cols",
184   'extra_sql' => "$where order by coalesce(lower(company), ''), lower(last)",
185 };
186
187 my $total_sql = "select ".
188                   join(',', map owed( @$_, 'agentnum'=>$agentnum ), @ranges );
189
190 my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
191 $total_sth->execute or die "error executing $total_sql: ". $total_sth->errstr;
192 my $row = $total_sth->fetchrow_hashref();
193
194 my $conf = new FS::Conf;
195 my $money_char = $conf->config('money_char') || '$';
196
197 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
198
199 </%init>