add history tables to field change upgrade instructions, add hints for pre-5.6 perl...
[freeside.git] / httemplate / search / cust_bill.cgi
1 <%
2
3 my $conf = new FS::Conf;
4 my $maxrecords = $conf->config('maxsearchrecordsperpage');
5
6 my $orderby = ''; #removeme
7
8 my $limit = '';
9 $limit .= "LIMIT $maxrecords" if $maxrecords;
10
11 my $offset = $cgi->param('offset') || 0;
12 $limit .= " OFFSET $offset" if $offset;
13
14 my($total, $tot_amount, $tot_balance);
15
16 my(@cust_bill);
17 if ( $cgi->keywords ) {
18   my($query) = $cgi->keywords;
19   my $owed = "charged - ( select coalesce(sum(amount),0) from cust_bill_pay
20                           where cust_bill_pay.invnum = cust_bill.invnum )
21                       - ( select coalesce(sum(amount),0) from cust_credit_bill
22                           where cust_credit_bill.invnum = cust_bill.invnum )";
23   my @where;
24   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
25     my($open, $days, $field) = ($1, $2, $3);
26     $field = "_date" if $field eq 'date';
27     $orderby = "ORDER BY cust_bill.$field";
28     push @where, "0 != $owed" if $open;
29     push @where, "cust_bill._date < ". (time-86400*$days) if $days;
30   } else {
31     die "unknown query string $query";
32   }
33
34   my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
35
36   my $statement = "SELECT COUNT(*), sum(charged), sum($owed)
37                    FROM cust_bill $extra_sql";
38   my $sth = dbh->prepare($statement) or die dbh->errstr. " doing $statement";
39   $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
40
41   ( $total, $tot_amount, $tot_balance ) = @{$sth->fetchrow_arrayref};
42
43   @cust_bill = qsearch(
44     'cust_bill',
45     {},
46     "cust_bill.*, $owed as owed",
47     "$extra_sql $orderby $limit"
48   );
49 } else {
50   $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/;
51   my $invnum = $2;
52   @cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum } );
53   $total = scalar(@cust_bill);
54 }
55
56 #if ( scalar(@cust_bill) == 1 ) {
57 if ( $total == 1 ) {
58   my $invnum = $cust_bill[0]->invnum;
59   print $cgi->redirect(popurl(2). "view/cust_bill.cgi?$invnum");  #redirect
60 } elsif ( scalar(@cust_bill) == 0 ) {
61 %>
62 <!-- mason kludge -->
63 <%
64   eidiot("Invoice not found.");
65 } else {
66 %>
67 <!-- mason kludge -->
68 <%
69
70   #begin pager
71   my $pager = '';
72   if ( $total != scalar(@cust_bill) && $maxrecords ) {
73     unless ( $offset == 0 ) {
74       $cgi->param('offset', $offset - $maxrecords);
75       $pager .= '<A HREF="'. $cgi->self_url.
76                 '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
77     }
78     my $poff;
79     my $page;
80     for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
81       $page++;
82       if ( $offset == $poff ) {
83         $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
84       } else {
85         $cgi->param('offset', $poff);
86         $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
87       }
88     }
89     unless ( $offset + $maxrecords > $total ) {
90       $cgi->param('offset', $offset + $maxrecords);
91       $pager .= '<A HREF="'. $cgi->self_url.
92                 '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
93     }
94   }
95   #end pager
96
97   print header("Invoice Search Results", menubar(
98           'Main Menu', popurl(2)
99         )).
100         "$total matching invoices found<BR>".
101         "\$$tot_balance total balance<BR>".
102         "\$$tot_amount total amount<BR>".
103         "<BR>$pager". table(). <<END;
104       <TR>
105         <TH></TH>
106         <TH>Balance</TH>
107         <TH>Amount</TH>
108         <TH>Date</TH>
109         <TH>Contact name</TH>
110         <TH>Company</TH>
111       </TR>
112 END
113
114   foreach my $cust_bill ( @cust_bill ) {
115     my($invnum, $owed, $charged, $date ) = (
116       $cust_bill->invnum,
117       sprintf("%.2f", $cust_bill->getfield('owed')),
118       sprintf("%.2f", $cust_bill->charged),
119       $cust_bill->_date,
120     );
121     my $pdate = time2str("%b %d %Y", $date);
122
123     my $rowspan = 1;
124
125     my $view = popurl(2). "view/cust_bill.cgi?$invnum";
126     print <<END;
127       <TR>
128         <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$invnum</FONT></A></TD>
129         <TD ROWSPAN=$rowspan ALIGN="right"><A HREF="$view"><FONT SIZE=-1>\$$owed</FONT></A></TD>
130         <TD ROWSPAN=$rowspan ALIGN="right"><A HREF="$view"><FONT SIZE=-1>\$$charged</FONT></A></TD>
131         <TD ROWSPAN=$rowspan><A HREF="$view"><FONT SIZE=-1>$pdate</FONT></A></TD>
132 END
133     my $custnum = $cust_bill->custnum;
134     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
135     if ( $cust_main ) {
136       my $cview = popurl(2). "view/cust_main.cgi?". $cust_main->custnum;
137       my ( $name, $company ) = (
138         $cust_main->last. ', '. $cust_main->first,
139         $cust_main->company,
140       );
141       print <<END;
142         <TD ROWSPAN=$rowspan><A HREF="$cview"><FONT SIZE=-1>$name</FONT></A></TD>
143         <TD ROWSPAN=$rowspan><A HREF="$cview"><FONT SIZE=-1>$company</FONT></A></TD>
144 END
145     } else {
146       print <<END
147         <TD ROWSPAN=$rowspan COLSPAN=2>WARNING: couldn't find cust_main.custnum $custnum (cust_bill.invnum $invnum)</TD>
148 END
149     }
150
151     print "</TR>";
152   }
153   $tot_balance = sprintf("%.2f", $tot_balance);
154   $tot_amount = sprintf("%.2f", $tot_amount);
155   print "</TABLE>$pager<BR>". table(). <<END;
156       <TR><TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD><TH><FONT SIZE=-1>Total<BR>Balance</FONT></TH><TH><FONT SIZE=-1>Total<BR>Amount</FONT></TH></TR>
157       <TR><TD></TD><TD ALIGN="right"><FONT SIZE=-1>\$$tot_balance</FONT></TD><TD ALIGN="right"><FONT SIZE=-1>\$$tot_amount</FONT></TD></TD></TR>
158     </TABLE>
159   </BODY>
160 </HTML>
161 END
162
163 }
164
165 %>