diff options
author | Mark Wells <mark@freeside.biz> | 2015-03-13 15:24:09 -0700 |
---|---|---|
committer | Mark Wells <mark@freeside.biz> | 2015-03-13 15:24:09 -0700 |
commit | ed741d82e6ef8b2c66985f6b54d5cb26e0a70acb (patch) | |
tree | c22fd88a977c369da77cfccb78172d747c49a56d /httemplate/search/elements/commission.html | |
parent | 8e50b6f6fbaa9d732a371114c0dfc95c326cb890 (diff) |
restructure agent commission reporting, #23348
Diffstat (limited to 'httemplate/search/elements/commission.html')
-rw-r--r-- | httemplate/search/elements/commission.html | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/httemplate/search/elements/commission.html b/httemplate/search/elements/commission.html new file mode 100644 index 000000000..6f610639f --- /dev/null +++ b/httemplate/search/elements/commission.html @@ -0,0 +1,110 @@ +<& search.html, %opt &> +<%doc> +<& elements/commission.html, + name_singular => 'sales person', # or 'agent', 'employee', etc. + header => [ 'Sales person' ], # 'One-Time Sales', 'Recurring Sales', + # 'Commission' will be appended + fields => [ 'salesperson' ], # ditto + links => [ [ '/view/sales.html?', 'salesnum' ] ], # usual conventions + sales_detail_link => [ 'sales_commission_pkg.html?', 'salesnum' ], + credit_detail_link => [ 'cust_credit.html?commission_salesnum=', 'salesnum' ], + align => 'l', + query => { table => 'sales', # must be a Commission_Mixin + #other params as appropriate + }, + count_query => 'SELECT COUNT(*) FROM sales ...', + + # all other elements/search.html stuff will be passed through +&> + +The hash passed as 'query' will be passed through to the cust_bill_pkg_search +and cust_credit_search methods, and so can contain type-specific options. +</%doc> +<%init> +die "access denied" + unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); + +my %opt = @_; +my $conf = new FS::Conf; + +my $money_char = $conf->config('money_char') || '$'; + +my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, ''); + +my $date_format = $conf->config('date_format') || '%m/%d/%Y'; + +my $query = $opt{'query'}; + +my $paid = $cgi->param('paid') ? 1 : 0; +if ($beginning) { + $opt{'title'} .= ': ' . time2str($date_format, $beginning) . ' to ' . + time2str($date_format, $ending); +} +if ($paid) { + $opt{'title'} .= ' - paid sales only'; +} + +my $sales_sub_maker = sub { + my $field = shift; + sub { + my $object = shift; + my $search = $object->cust_bill_pkg_search( + $beginning, + $ending, + 'paid' => $paid, + %$query, + ); + $search->{select} = "SUM(cust_bill_pkg.$field) AS total_amount"; + my $result = qsearchs($search); + my $total = $result->get('total_amount') || 0; + + return $money_char. sprintf('%.2f', $total); + }; +}; + +my $commission_sub = sub { + my $object = shift; + + my $search = $object->cust_credit_search( + $beginning, + $ending, + %$query + ); + $search->{select} = 'SUM(cust_credit.amount) AS total_amount'; + my $result = qsearchs($search); + my $total = $result->get('total_amount') || 0; + + return $money_char. sprintf('%.2f', $total); +}; + +my $sales_link = $opt{'sales_detail_link'}; +if ($sales_link) { + my ($pre, $post) = split('\?', $sales_link->[0], 2); + $sales_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post; +} + +my $commission_link = $opt{'credit_detail_link'}; +if ($commission_link) { + my ($pre, $post) = split('\?', $commission_link->[0], 2); + $commission_link->[0] = $pre . "?begin=$beginning;end=$ending;" . $post; +} + +# merge our new stuff into %opt +my $header = $opt{'header'}; +push @$header, + 'One-time sales', + 'Recurring sales', + 'Commission' +; + +my $fields = $opt{'fields'}; +push @$fields, + $sales_sub_maker->('setup'), + $sales_sub_maker->('recur'), + $commission_sub +; + +push @{$opt{'links'}}, $sales_link, $sales_link, $commission_link; +$opt{'align'} .= 'rrr'; + +</%init> |