3 my $conf = new FS::Conf;
4 my $maxrecords = $conf->config('maxsearchrecordsperpage');
6 my $orderby = ''; #removeme
9 $limit .= "LIMIT $maxrecords" if $maxrecords;
11 my $offset = $cgi->param('offset') || 0;
12 $limit .= " OFFSET $offset" if $offset;
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 )";
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;
31 die "unknown query string $query";
34 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
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;
41 ( $total, $tot_amount, $tot_balance ) = @{$sth->fetchrow_arrayref};
46 "cust_bill.*, $owed as owed",
47 "$extra_sql $orderby $limit"
50 $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/;
52 @cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum } );
53 $total = scalar(@cust_bill);
56 #if ( scalar(@cust_bill) == 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 ) {
64 eidiot("Invoice not found.");
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> ';
80 for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
82 if ( $offset == $poff ) {
83 $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
85 $cgi->param('offset', $poff);
86 $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
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> ';
97 print header("Invoice Search Results", menubar(
98 'Main Menu', popurl(2)
100 "$total matching invoices found<BR>".
101 "\$$tot_balance total balance<BR>".
102 "\$$tot_amount total amount<BR>".
103 "<BR>$pager". table(). <<END;
109 <TH>Contact name</TH>
114 foreach my $cust_bill ( @cust_bill ) {
115 my($invnum, $owed, $charged, $date ) = (
117 sprintf("%.2f", $cust_bill->getfield('owed')),
118 sprintf("%.2f", $cust_bill->charged),
121 my $pdate = time2str("%b %d %Y", $date);
125 my $view = popurl(2). "view/cust_bill.cgi?$invnum";
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>
133 my $custnum = $cust_bill->custnum;
134 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } );
136 my $cview = popurl(2). "view/cust_main.cgi?". $cust_main->custnum;
137 my ( $name, $company ) = (
138 $cust_main->last. ', '. $cust_main->first,
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>
147 <TD ROWSPAN=$rowspan COLSPAN=2>WARNING: couldn't find cust_main.custnum $custnum (cust_bill.invnum $invnum)</TD>
153 $tot_balance = sprintf("%.2f", $tot_balance);
154 $tot_amount = sprintf("%.2f", $tot_amount);
155 print "</TABLE>$pager<BR>". table(). <<END;
156 <TR><TD> </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>