X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill.cgi;h=45ff9f15786e7ae31e82fbc94603f0a5796ffef7;hp=6eba4bd4979eae4e5e3d48bd888a525dab5afc42;hb=4cf0547217f3ccbca2aa9024ab0b396aef8bab5c;hpb=c89d696aae0a695b107e7c20ac06aa4b5e69cdac
diff --git a/httemplate/search/cust_bill.cgi b/httemplate/search/cust_bill.cgi
index 6eba4bd49..45ff9f157 100755
--- a/httemplate/search/cust_bill.cgi
+++ b/httemplate/search/cust_bill.cgi
@@ -23,28 +23,35 @@ if ( $cgi->keywords ) {
my @where;
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".
@@ -52,6 +59,7 @@ if ( $cgi->keywords ) {
} 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".
@@ -60,6 +68,7 @@ if ( $cgi->keywords ) {
} 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".
@@ -68,20 +77,20 @@ if ( $cgi->keywords ) {
die "unknown query string $query";
}
- my $extra_sql = scalar(@where) ? join(' AND ', @where) : '';
+ 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',
{},
- 'cust_bill.*,
- charged - coalesce(sum(cust_bill_pay.amount),0)
- - coalesce(sum(cust_credit_bill.amount),0) as owed',
+ "cust_bill.*, $owed as owed",
"$extra_sql $orderby $limit"
);
} else {
@@ -93,8 +102,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 ) {
@@ -137,7 +145,11 @@ if ( scalar(@cust_bill) == 1 && $total == 1) {
print header("Invoice Search Results", menubar(
'Main Menu', popurl(2)
- )), "$total matching invoices found
$pager", &table(), <".
+ "\$$tot_balance total balance
".
+ "\$$tot_amount total amount
".
+ "
$pager". table(). <
|
Balance |
@@ -149,21 +161,17 @@ 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)
) {
my($invnum, $owed, $charged, $date ) = (
$cust_bill->invnum,
- sprintf("%.2f", $cust_bill->owed),
+ sprintf("%.2f", $cust_bill->getfield('owed')),
sprintf("%.2f", $cust_bill->charged),
$cust_bill->_date,
);
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";
@@ -196,10 +204,10 @@ END
}
$tot_balance = sprintf("%.2f", $tot_balance);
$tot_amount = sprintf("%.2f", $tot_amount);
- print < | Total | Total |
+ print "$pager
". table(). <----- | Total Balance | Total Amount |
| \$$tot_balance | \$$tot_amount |
- $pager
+