X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill.html;h=3ae624af2cf350ede950fbff64ac6afd958ca720;hb=324f65798f7e4f28cae94a39a77260c650aa2669;hp=51241575ca12772fcf8cec31902c7aedc53ecf37;hpb=5cd995865b91204f7ce9233a8ba4d5eee62f9be4;p=freeside.git diff --git a/httemplate/search/cust_bill.html b/httemplate/search/cust_bill.html index 51241575c..3ae624af2 100755 --- a/httemplate/search/cust_bill.html +++ b/httemplate/search/cust_bill.html @@ -1,22 +1,30 @@ <% my( $count_query, $sql_query ); - if ( $cgi->keywords ) { - my($query) = $cgi->keywords; + if ( $cgi->param('begin') || $cgi->param('end') || $cgi->keywords ) { + my $owed = "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay WHERE cust_bill_pay.invnum = cust_bill.invnum ) - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill WHERE cust_credit_bill.invnum = cust_bill.invnum )"; + my @where; - my $orderby = ''; + my $orderby = 'ORDER BY cust_bill._date'; + + if ( $cgi->param('begin') =~ /^(\d+)$/ ) { + push @where, "cust_bill._date >= $1", + } + if ( $cgi->param('end') =~ /^(\d+)$/ ) { + push @where, "cust_bill._date < $1", + } + + my($query) = $cgi->keywords; 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; - } else { - die "unknown query string $query"; } my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : ''; @@ -33,7 +41,7 @@ } else { $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/; - $count_query = 'SELECT 1'; + $count_query = "SELECT COUNT(*) FROM cust_bill WHERE invnum = $2"; $sql_query = { 'table' => 'cust_bill', 'hashref' => { 'invnum' => $2 }, @@ -42,7 +50,13 @@ } my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ]; - my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ]; + my $clink = sub { + my $cust_bill = shift; + my $cust_main = $cust_bill->cust_main; + $cust_main + ? [ "${p}view/cust_main.cgi?", 'custnum' ] + : ''; + }; %> <%= include( 'elements/search.html', @@ -50,22 +64,28 @@ 'name' => 'invoices', 'query' => $sql_query, 'count_query' => $count_query, - 'count_addl' => [ '$%.2f total amount', + 'count_addl' => [ '$%.2f total invoiced', '$%.2f total outstanding balance', ], 'redirect' => $link, 'header' => - [ '', qw(Balance Amount Date ), 'Contact name', 'Company' ], + [ 'Invoice #', qw(Balance Amount Date), 'Contact name', + 'Company' ], 'fields' => [ 'invnum', sub { sprintf('$%.2f', shift->get('owed') ) }, sub { sprintf('$%.2f', shift->charged ) }, sub { time2str('%b %d %Y', shift->_date ) }, - sub { my $cust_main = shift->cust_main; - $cust_main->get('last'). ', '. $cust_main->first; + sub { my $cust_bill = shift; + my $cust_main = $cust_bill->cust_main; + $cust_main + ? $cust_main->get('last'). ', '. $cust_main->first + : "WARNING: can't find cust_main.custnum ". + $cust_bill->custnum. ' (cust_bill.invnum '. + $cust_bill->invnum. ')'; }, sub { my $cust_main = shift->cust_main; - $cust_main->company; + $cust_main ? $cust_main->company : ''; }, ], 'links' => [