X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fsales_commission.html;h=710461c409decb9a4f7b00fbb3f5bd29f0818bd0;hb=f8066fa6a33f4ac33beec675b17de96c8c0a274b;hp=b4d40aeb20469718f8157a36cfbc91a299cc75dc;hpb=cd365522ec4e9f1b553bb1b5096c756c1fdb1d01;p=freeside.git diff --git a/httemplate/search/sales_commission.html b/httemplate/search/sales_commission.html index b4d40aeb2..710461c40 100644 --- a/httemplate/search/sales_commission.html +++ b/httemplate/search/sales_commission.html @@ -33,9 +33,16 @@ my $date_format = $conf->config('date_format') || '%m/%d/%Y'; my %query = ( 'table' => 'sales' ); my $count_query = "SELECT COUNT(*) FROM sales"; -my $salesnum; +if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) { + $query{hashref}->{agentnum} = $1; + $count_query .= " WHERE agentnum = $1"; +} + +my $salesnum = ''; if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) { $salesnum = $1; +} else { + $cgi->delete('salesnum'); } my $title = 'Sales person commission'; @@ -43,13 +50,14 @@ $title .= ': '. time2str($date_format, $beginning). ' to '. time2str($date_format, $ending) if $beginning; +my $paid = $cgi->param('paid') ? 1 : 0; +$title .= ' - paid sales only' if $paid; + my $cust_main_sales = $cgi->param('cust_main_sales') eq 'Y' ? 'Y' : ''; my $sales_link = [ 'sales_pkg_class.html?'. - "begin=$beginning;". - "end=$ending;". - "cust_main_sales=$cust_main_sales;". - "salesnum=", + # pass all of our parameters along + $cgi->query_string. ';salesnum=', 'salesnum' ]; @@ -58,16 +66,20 @@ my $sales_sub_maker = sub { sub { my $sales = shift; - #efficiency improvement: ask the db for a sum instead of all the records - my $total = 0; - my @cust_bill_pkg = $sales->cust_bill_pkg( + my $search = $sales->cust_bill_pkg_search( $beginning, $ending, 'cust_main_sales' => $cust_main_sales, + 'paid' => $paid, + ); + die 'cust_bill_pkg_search hashref not yet handled' if $search->{hashref}; + + my $total = FS::Record->scalar_sql( + "SELECT SUM(cust_bill_pkg.$field) FROM cust_bill_pkg ". #$search->{table} + $search->{addl_from}. ' '. $search->{extra_sql} ); - $total += $_->get($field) foreach @cust_bill_pkg; - $money_char. sprintf('%.2f', $total); + return $money_char. sprintf('%.2f', $total); }; }; @@ -75,11 +87,21 @@ my $commission_sub = sub { my $sales = shift; #efficiency improvement: ask the db for a sum instead of all the records - my $total_credit = 0; - my @cust_credit = $sales->cust_credit( $beginning, $ending ); - $total_credit += $_->amount foreach @cust_credit; + #my $total_credit = 0; + #my @cust_credit = $sales->cust_credit( $beginning, $ending ); + #$total_credit += $_->amount foreach @cust_credit; + + my $search = $sales->cust_credit_search( $beginning, $ending ); + + my $sql = + "SELECT SUM(cust_credit.amount) FROM cust_credit ". #$search->{table} + $search->{addl_from}. ' '. + ' WHERE commission_salesnum = ? '. #$search->{hashref} + $search->{extra_sql}; + + my $total = FS::Record->scalar_sql($sql, $sales->salesnum); - $money_char. sprintf('%.2f', $total_credit); + $money_char. sprintf('%.2f', $total); }; my $commission_link = [ 'cust_credit.html?'.