that would be nice...
[freeside.git] / httemplate / search / cust_bill.cgi
index 3b6b8fa..6e5d201 100755 (executable)
@@ -21,60 +21,77 @@ if ( $cgi->keywords ) {
                       - ( select coalesce(sum(amount),0) from cust_credit_bill
                           where cust_credit_bill.invnum = cust_bill.invnum )";
   my @where;
-  if ( $query eq 'invnum' ) {
-    $sortby = \*invnum_sort;
-    #@cust_bill = qsearch('cust_bill', {} );
-  } elsif ( $query eq 'date' ) {
-    $sortby = \*date_sort;
-    #@cust_bill = qsearch('cust_bill', {} );
-  } elsif ( $query eq 'custnum' ) {
-    $sortby = \*custnum_sort;
-    #@cust_bill = qsearch('cust_bill', {} );
-  } elsif ( $query eq 'OPEN_invnum' ) {
-    $sortby = \*invnum_sort;
-    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
-    push @where, "0 != $owed";
-  } elsif ( $query eq 'OPEN_date' ) {
-    $sortby = \*date_sort;
-    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
-    push @where, "0 != $owed";
-  } elsif ( $query eq 'OPEN_custnum' ) {
-    $sortby = \*custnum_sort;
-    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
-    push @where, "0 != $owed";
-  } elsif ( $query =~ /^OPEN(\d+)_invnum$/ ) {
-    my $open = $1 * 86400;
-    $sortby = \*invnum_sort;
-    #@cust_bill =
-    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
-    push @where, "0 != $owed".
-                 "cust_bill._date < ". (time-$open);
-  } elsif ( $query =~ /^OPEN(\d+)_date$/ ) {
-    my $open = $1 * 86400;
-    $sortby = \*date_sort;
-    #@cust_bill =
-    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
-    push @where, "0 != $owed".
-                 "cust_bill._date < ". (time-$open);
-
-  } elsif ( $query =~ /^OPEN(\d+)_custnum$/ ) {
-    my $open = $1 * 86400;
-    $sortby = \*custnum_sort;
-    #@cust_bill =
-    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
-    push @where, "0 != $owed".
-                 "cust_bill._date < ". (time-$open);
+  if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
+    my($open, $days, $field) = ($1, $2, $3);
+    $field = "_date" if $field eq 'date';
+    $orderby = "ORDER BY cust_bill.$field";
+    push @where, "0 != $owed" if $open;
+    push @where, "cust_bill._date < ". (time-86400*days) if $days;
+#  if ( $query eq 'invnum' ) {
+#    $sortby = \*invnum_sort;
+#    $orderby = "ORDER BY cust_bill.invnum";
+#    #@cust_bill = qsearch('cust_bill', {} );
+#  } elsif ( $query eq 'date' ) {
+#    $sortby = \*date_sort;
+#    $orderby = "ORDER BY cust_bill._date";
+#    #@cust_bill = qsearch('cust_bill', {} );
+#  } elsif ( $query eq 'custnum' ) {
+#    $sortby = \*custnum_sort;
+#    $orderby = "ORDER BY cust_bill.custnum";
+#    #@cust_bill = qsearch('cust_bill', {} );
+#  } elsif ( $query eq 'OPEN_invnum' ) {
+#    $sortby = \*invnum_sort;
+#    $orderby = "ORDER BY cust_bill.invnum";
+#    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed";
+#  } elsif ( $query eq 'OPEN_date' ) {
+#    $sortby = \*date_sort;
+#    $orderby = "ORDER BY cust_bill._date";
+#    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed";
+#  } elsif ( $query eq 'OPEN_custnum' ) {
+#    $sortby = \*custnum_sort;
+#    $orderby = "ORDER BY cust_bill.custnum";
+#    #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed";
+#  } elsif ( $query =~ /^OPEN(\d+)_invnum$/ ) {
+#    my $open = $1 * 86400;
+#    $sortby = \*invnum_sort;
+#    $orderby = "ORDER BY cust_bill.invnum";
+#    #@cust_bill =
+#    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed",
+#                 "cust_bill._date < ". (time-$open);
+#  } elsif ( $query =~ /^OPEN(\d+)_date$/ ) {
+#    my $open = $1 * 86400;
+#    $sortby = \*date_sort;
+#    $orderby = "ORDER BY cust_bill._date";
+#    #@cust_bill =
+#    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed",
+#                 "cust_bill._date < ". (time-$open);
+#
+#  } elsif ( $query =~ /^OPEN(\d+)_custnum$/ ) {
+#    my $open = $1 * 86400;
+#    $sortby = \*custnum_sort;
+#    $orderby = "ORDER BY cust_bill.custnum";
+#    #@cust_bill =
+#    #  grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} );
+#    push @where, "0 != $owed",
+#                 "cust_bill._date < ". (time-$open);
   } else {
     die "unknown query string $query";
   }
 
   my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
 
-  my $statement = "SELECT COUNT(*) FROM cust_bill $extra_sql";
+  my $statement = "SELECT COUNT(*), sum(charged), sum($owed)
+                   FROM cust_bill $extra_sql";
   my $sth = dbh->prepare($statement) or die dbh->errstr. " doing $statement";
   $sth->execute or die "Error executing \"$statement\": ". $sth->errstr;
 
-  $total = $sth->fetchrow_arrayref->[0];
+  #$total = $sth->fetchrow_arrayref->[0];
+  ( $total, $tot_amount, $tot_balance ) = @{$sth->fetchrow_arrayref};
 
   @cust_bill = qsearch(
     'cust_bill',
@@ -91,8 +108,7 @@ if ( $cgi->keywords ) {
 }
 
 #if ( scalar(@cust_bill) == 1 ) {
-if ( scalar(@cust_bill) == 1 && $total == 1) {
-#if ( $total == 1 ) {
+if ( $total == 1 ) {
   my $invnum = $cust_bill[0]->invnum;
   print $cgi->redirect(popurl(2). "view/cust_bill.cgi?$invnum");  #redirect
 } elsif ( scalar(@cust_bill) == 0 ) {
@@ -135,7 +151,11 @@ if ( scalar(@cust_bill) == 1 && $total == 1) {
 
   print header("Invoice Search Results", menubar(
           'Main Menu', popurl(2)
-        )), "$total matching invoices found<BR><BR>$pager", &table(), <<END;
+        )).
+        "$total matching invoices found<BR>".
+        "\$$tot_balance total balance<BR>".
+        "\$$tot_amount total amount<BR>".
+        "<BR>$pager". table(). <<END;
       <TR>
         <TH></TH>
         <TH>Balance</TH>
@@ -147,7 +167,6 @@ if ( scalar(@cust_bill) == 1 && $total == 1) {
 END
 
   my(%saw, $cust_bill);
-#  my($tot_balance, $tot_amount) = (0, 0); #BOGUS
   foreach $cust_bill (
     sort $sortby grep(!$saw{$_->invnum}++, @cust_bill)
   ) {
@@ -159,9 +178,6 @@ END
     );
     my $pdate = time2str("%b %d %Y", $date);
 
-    $tot_balance += $owed;
-    $tot_amount += $charged;
-
     my $rowspan = 1;
 
     my $view = popurl(2). "view/cust_bill.cgi?$invnum";
@@ -194,10 +210,10 @@ END
   }
   $tot_balance = sprintf("%.2f", $tot_balance);
   $tot_amount = sprintf("%.2f", $tot_amount);
-  print <<END;
-      <TR><TD></TD><TH><FONT SIZE=-1>Total</FONT></TH><TH><FONT SIZE=-1>Total</FONT></TH></TR>
+  print "</TABLE>$pager<BR>". table(). <<END;
+      <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>
       <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>
-    </TABLE>$pager
+    </TABLE>
   </BODY>
 </HTML>
 END