sort these case-insensitive
[freeside.git] / httemplate / search / report_receivables.cgi
1 <!-- mason kludge -->
2 <%
3
4   my $charged = <<END;
5   sum( charged
6        - coalesce(
7            ( select sum(amount) from cust_bill_pay
8              where cust_bill.invnum = cust_bill_pay.invnum )
9            ,0
10          )
11        - coalesce(
12            ( select sum(amount) from cust_credit_bill
13              where cust_bill.invnum = cust_credit_bill.invnum )
14            ,0
15          )
16
17      )
18 END
19
20   my $owed_cols = <<END;
21        coalesce(
22          ( select $charged from cust_bill
23            where cust_bill._date > extract(epoch from now())-2592000
24              and cust_main.custnum = cust_bill.custnum
25          )
26          ,0
27        ) as owed_0_30,
28
29        coalesce(
30          ( select $charged from cust_bill
31            where cust_bill._date >  extract(epoch from now())-5184000
32              and cust_bill._date <= extract(epoch from now())-2592000
33              and cust_main.custnum = cust_bill.custnum
34          )
35          ,0
36        ) as owed_30_60,
37
38        coalesce(
39          ( select $charged from cust_bill
40            where cust_bill._date >  extract(epoch from now())-7776000
41              and cust_bill._date <= extract(epoch from now())-5184000
42              and cust_main.custnum = cust_bill.custnum
43          )
44          ,0
45        ) as owed_60_90,
46
47        coalesce(
48          ( select $charged from cust_bill
49            where cust_bill._date <= extract(epoch from now())-7776000
50              and cust_main.custnum = cust_bill.custnum
51          )
52          ,0
53        ) as owed_90_plus,
54
55        coalesce(
56          ( select $charged from cust_bill
57            where cust_main.custnum = cust_bill.custnum
58          )
59          ,0
60        ) as owed_total
61 END
62
63   my $sql = <<END;
64
65 select *, $owed_cols from cust_main
66 where 0 <
67   coalesce(
68            ( select $charged from cust_bill
69              where cust_main.custnum = cust_bill.custnum
70            )
71            ,0
72          )
73
74 order by lower(company), lower(last)
75
76 END
77
78   my $total_sql = "select $owed_cols";
79
80   my $sth = dbh->prepare($sql) or die dbh->errstr;
81   $sth->execute or die $sth->errstr;
82
83   my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
84   $total_sth->execute or die $total_sth->errstr;
85
86 %>
87 <%= header('Accounts Receivable Aging Summary', menubar( 'Main Menu'=>$p, ) ) %>
88 <%= table() %>
89   <TR>
90     <TH>Customer</TH>
91     <TH>0-30</TH>
92     <TH>30-60</TH>
93     <TH>60-90</TH>
94     <TH>90+</TH>
95     <TH>Total</TH>
96   </TR>
97 <% while ( my $row = $sth->fetchrow_hashref() ) { %>
98   <TR>
99     <TD><A HREF="<%= $p %>view/cust_main.cgi?<%= $row->{'custnum'} %>">
100         <%= $row->{'company'} ? $row->{'company'}. ' (' : '' %><%= $row->{'last'}. ', '. $row->{'first'} %><%= $row->{'company'} ? ')' : '' %></A>
101     </TD>
102     <TD ALIGN="right">$<%= sprintf("%.2f", $row->{'owed_0_30'} ) %></TD>
103     <TD ALIGN="right">$<%= sprintf("%.2f", $row->{'owed_30_60'} ) %></TD>
104     <TD ALIGN="right">$<%= sprintf("%.2f", $row->{'owed_60_90'} ) %></TD>
105     <TD ALIGN="right">$<%= sprintf("%.2f", $row->{'owed_90_plus'} ) %></TD>
106     <TD ALIGN="right"><B>$<%= sprintf("%.2f", $row->{'owed_total'} ) %></B></TD>
107   </TR>
108 <% } %>
109 <% my $row = $total_sth->fetchrow_hashref(); %>
110   <TR>
111     <TD COLSPAN=6>&nbsp;</TD>
112   </TR>
113   <TR>
114     <TD><I>Total</I></TD>
115     <TD ALIGN="right"><I>$<%= sprintf("%.2f", $row->{'owed_0_30'} ) %></TD>
116     <TD ALIGN="right"><I>$<%= sprintf("%.2f", $row->{'owed_30_60'} ) %></TD>
117     <TD ALIGN="right"><I>$<%= sprintf("%.2f", $row->{'owed_60_90'} ) %></TD>
118     <TD ALIGN="right"><I>$<%= sprintf("%.2f", $row->{'owed_90_plus'} ) %></TD>
119     <TD ALIGN="right"><I><B>$<%= sprintf("%.2f", $row->{'owed_total'} ) %></B></I></TD>
120   </TR>
121 </TABLE>
122 </BODY>
123 </HTML>