2 <% $cgi->redirect($sales_link->[0] . $salesnum) %>
4 <& elements/search.html,
6 'name_singular' => 'sales person',
7 'header' => [ 'Sales person', 'One-Time Sales', 'Recurring Sales', 'Commission', ],
8 'fields' => [ 'salesperson',
9 $sales_sub_maker->('setup'),
10 $sales_sub_maker->('recur'),
13 'links' => [ '', $sales_link, $sales_link, $commission_link ],
16 'count_query' => $count_query,
23 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
25 my $conf = new FS::Conf;
27 my $money_char = $conf->config('money_char') || '$';
29 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
31 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
33 my %query = ( 'table' => 'sales' );
34 my $count_query = "SELECT COUNT(*) FROM sales";
36 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
37 $query{hashref}->{agentnum} = $1;
38 $count_query .= " WHERE agentnum = $1";
42 if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) {
45 $cgi->delete('salesnum');
48 my $title = 'Sales person commission';
49 $title .= ': '. time2str($date_format, $beginning). ' to '.
50 time2str($date_format, $ending)
53 my $paid = $cgi->param('paid') ? 1 : 0;
54 $title .= ' - paid sales only' if $paid;
56 my $cust_main_sales = $cgi->param('cust_main_sales') eq 'Y' ? 'Y' : '';
58 my $sales_link = [ 'sales_pkg_class.html?'.
59 # pass all of our parameters along
60 $cgi->query_string. ';salesnum=',
64 my $sales_sub_maker = sub {
69 my $search = $sales->cust_bill_pkg_search(
72 'cust_main_sales' => $cust_main_sales,
75 die 'cust_bill_pkg_search hashref not yet handled' if $search->{hashref};
77 my $total = FS::Record->scalar_sql(
78 "SELECT SUM(cust_bill_pkg.$field) FROM cust_bill_pkg ". #$search->{table}
79 $search->{addl_from}. ' '. $search->{extra_sql}
82 return $money_char. sprintf('%.2f', $total);
86 my $commission_sub = sub {
89 #efficiency improvement: ask the db for a sum instead of all the records
90 #my $total_credit = 0;
91 #my @cust_credit = $sales->cust_credit( $beginning, $ending );
92 #$total_credit += $_->amount foreach @cust_credit;
94 my $search = $sales->cust_credit_search( $beginning, $ending );
97 "SELECT SUM(cust_credit.amount) FROM cust_credit ". #$search->{table}
98 $search->{addl_from}. ' '.
99 ' WHERE commission_salesnum = ? '. #$search->{hashref}
100 $search->{extra_sql};
102 my $total = FS::Record->scalar_sql($sql, $sales->salesnum);
104 $money_char. sprintf('%.2f', $total);
107 my $commission_link = [ 'cust_credit.html?'.
110 "cust_main_sales=$cust_main_sales;".
111 'commission_salesnum=',