diff options
author | Ivan Kohler <ivan@freeside.biz> | 2013-08-26 01:09:20 -0700 |
---|---|---|
committer | Ivan Kohler <ivan@freeside.biz> | 2013-08-26 01:09:20 -0700 |
commit | eedfe58ba50912dbfb490aa47930bbc0ab9e3ac9 (patch) | |
tree | bd929f854b0de3dcd4a950716e7ac4db4183caed /httemplate | |
parent | 8bdf1185af6956707e1da314422613cf0deea3a0 (diff) |
continue sales person work: customer and package selection, commissions, reporting. RT#23402
Diffstat (limited to 'httemplate')
-rw-r--r-- | httemplate/search/cust_bill_pkg.cgi | 14 | ||||
-rwxr-xr-x | httemplate/search/cust_credit.html | 25 | ||||
-rw-r--r-- | httemplate/search/report_sales_commission.html | 26 | ||||
-rw-r--r-- | httemplate/search/sales_commission.html | 64 | ||||
-rw-r--r-- | httemplate/search/sales_pkg_class.html | 74 | ||||
-rw-r--r-- | httemplate/view/cust_main/packages/package.html | 2 |
6 files changed, 189 insertions, 16 deletions
diff --git a/httemplate/search/cust_bill_pkg.cgi b/httemplate/search/cust_bill_pkg.cgi index bf73d74bd..744286a68 100644 --- a/httemplate/search/cust_bill_pkg.cgi +++ b/httemplate/search/cust_bill_pkg.cgi @@ -283,6 +283,20 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) { push @where, "cust_main.agentnum = $1"; } +# salesnum +if ( $cgi->param('salesnum') =~ /^(\d+)$/ ) { + + my $salesnum = $1; + + my $cmp_salesnum = $cgi->param('cust_main_sales') + ? ' COALESCE( cust_pkg.salesnum, cust_main.salesnum )' + : ' cust_pkg.salesnum '; + + push @where, "$cmp_salesnum = $salesnum"; + + $cgi->param('classnum', 0) unless $cgi->param('classnum'); +} + # refnum if ( $cgi->param('refnum') =~ /^(\d+)$/ ) { push @where, "cust_main.refnum = $1"; diff --git a/httemplate/search/cust_credit.html b/httemplate/search/cust_credit.html index cabf8c002..fb6ef834b 100755 --- a/httemplate/search/cust_credit.html +++ b/httemplate/search/cust_credit.html @@ -84,6 +84,7 @@ push @style, '', ; my @search = (); +my $addl_from = ''; if ( $cgi->param('usernum') =~ /^(\d+)$/ ) { push @search, "cust_credit.usernum = $1"; @@ -96,6 +97,23 @@ if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) { $title = $agent->agent. " $title"; } +# commission_salesnum +if ( $cgi->param('commission_salesnum') =~ /^(\d+)$/ ) { + push @search, "commission_salesnum = $1"; +} + +# commission_classnum +if ( grep { $_ eq 'commission_classnum' } $cgi->param ) { + $cgi->param('commission_classnum') =~ /^(\d*)$/ or die 'guru meditation #13'; + my $classnum = $1; + push @search, "part_pkg.classnum ". ( $classnum ? " = $classnum" + : " IS NULL " ); + + $addl_from .= + ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '. + ' LEFT JOIN part_pkg USING ( pkgpart ) '; +} + if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) { push @search, "refnum = $1"; my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } ); @@ -124,7 +142,7 @@ push @search, "_date >= $beginning ", push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' ); #here is the agent virtualization -push @search, $FS::CurrentUser::CurrentUser->agentnums_sql; +push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main'); my @select = ( 'cust_credit.*', @@ -141,7 +159,8 @@ my $where = 'WHERE '. join(' AND ', @search); my $count_query = 'SELECT COUNT(*), SUM(amount) '; $count_query .= ', SUM(' . FS::cust_credit->unapplied_sql . ') ' if $unapplied; -$count_query .= 'FROM cust_credit'. FS::UI::Web::join_cust_main('cust_credit'). +$count_query .= 'FROM cust_credit'. + $addl_from. FS::UI::Web::join_cust_main('cust_credit'). $where; my @count_addl = ( $money_char.'%.2f total credited (gross)' ); @@ -152,7 +171,7 @@ my $sql_query = { 'select' => join(', ',@select), 'hashref' => {}, 'extra_sql' => $where, - 'addl_from' => FS::UI::Web::join_cust_main('cust_credit') + 'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit') }; </%init> diff --git a/httemplate/search/report_sales_commission.html b/httemplate/search/report_sales_commission.html index 03527f6bd..cc17e6bed 100644 --- a/httemplate/search/report_sales_commission.html +++ b/httemplate/search/report_sales_commission.html @@ -4,11 +4,31 @@ <TABLE BGCOLOR="#cccccc" CELLSPACING=0> -<% include( '/elements/tr-select-agent.html' ) %> +<& /elements/tr-select-agent.html, + 'onchange' => 'agent_changed(this)', +&> -<% include( '/elements/tr-select-sales.html' ) %> +<SCRIPT TYPE="text/javascript"> -<% include( '/elements/tr-input-beginning_ending.html', ) %> + function agent_changed(what) { + salesnum_agentnum_changed(what); + } + + <&| /elements/onload.js &> + agent_changed(document.getElementById('agentnum')) + </&> + +</SCRIPT> + +<& /elements/tr-select-sales.html &> + +<& /elements/tr-checkbox.html, + 'label' => 'Customer sales person if there is no package sales person', + 'field' => 'cust_main_sales', + 'value' => 'Y', +&> + +<& /elements/tr-input-beginning_ending.html &> </TABLE> diff --git a/httemplate/search/sales_commission.html b/httemplate/search/sales_commission.html index 19e11889b..d7b7a88ad 100644 --- a/httemplate/search/sales_commission.html +++ b/httemplate/search/sales_commission.html @@ -1,12 +1,13 @@ <& elements/search.html, - 'title' => 'Sales person commission report', + 'title' => $title, 'name_singular' => 'sales person', # 'redirect' => sub { #my( $sales, $cgi ) = @); # $saleslink; # }, 'header' => [ 'Sales person', 'Sales', 'Commission', ], - 'fields' => [ 'salesperson', sub{'XXX';}, sub{'XXX';}, ], - 'links' => [ '', $saleslink, $commissionlink ], + 'fields' => [ 'salesperson', $sales_sub, $commission_sub, ], + 'links' => [ '', $sales_link, $commission_link ], + 'align' => 'lrr', 'query' => { 'table' => 'sales', }, 'count_query' => 'SELECT COUNT(*) FROM sales', 'disableable' => 1, @@ -16,8 +17,61 @@ die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); -my $saleslink = [ 'sales_pkg_class.html?salesnum=', 'salesnum' ]; +my $conf = new FS::Conf; -my $commissionlink = ''; #XXX +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 $title = 'Sales person commission'; +$title .= ': '. time2str($date_format, $beginning). ' to '. + time2str($date_format, $ending) + if $beginning; + +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=", + 'salesnum' + ]; + +my $sales_sub = sub { + my $sales = shift; + + #efficiency improvement: ask the db for a sum instead of all the records + my $total_recur = 0; + my @cust_bill_pkg = $sales->cust_bill_pkg( + $beginning, + $ending, + 'cust_main_sales' => $cust_main_sales, + ); + $total_recur += $_->recur foreach @cust_bill_pkg; + + $money_char. sprintf('%.2f', $total_recur); +}; + +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; + + $money_char. sprintf('%.2f', $total_credit); +}; + +my $commission_link = [ 'cust_credit.html?'. + "begin=$beginning;". + "end=$ending;". + "cust_main_sales=$cust_main_sales;". + 'commission_salesnum=', + 'salesnum' + ]; </%init> diff --git a/httemplate/search/sales_pkg_class.html b/httemplate/search/sales_pkg_class.html index bb67c70a6..c57aae66d 100644 --- a/httemplate/search/sales_pkg_class.html +++ b/httemplate/search/sales_pkg_class.html @@ -1,22 +1,88 @@ <& elements/search.html, - 'title' => $sales->salesperson. ' commission package classes', + 'title' => $title, 'name_singular' => 'package class', 'header' => [ 'Package class', 'Sales', 'Commission', ], - 'fields' => [ 'classname', sub{'XXX';}, sub{'XXX';}, ], - #'links' + 'fields' => [ 'classname', $sales_sub, $commission_sub, ], + 'links' => [ '', $sales_link, $commission_link ], + 'align' => 'lrr', 'query' => { 'table' => 'sales_pkg_class', 'hashref' => { 'salesnum' => $salesnum }, }, - 'count_query' => "SELECT COUNT(*) FROM sales_pkg_class WHERE salesnum = $salesnum", + 'count_query' => "SELECT COUNT(*) FROM sales_pkg_class WHERE salesnum = $salesnum", #show some totals? &> <%init> die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Financial reports'); +my $conf = new FS::Conf; + +my $money_char = $conf->config('money_char') || '$'; + $cgi->param('salesnum') =~ /^(\d+)$/ or die 'illegal salesnum'; my $salesnum = $1; my $sales = qsearchs('sales', { 'salesnum'=>$salesnum } ) or die 'unknown salesnum'; +my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, ''); + +my $date_format = $conf->config('date_format') || '%m/%d/%Y'; + +my $title = $sales->salesperson. ' commission'; +$title .= ': '. time2str($date_format, $beginning). ' to '. + time2str($date_format, $ending) + if $beginning; + +my $cust_main_sales = $cgi->param('cust_main_sales') eq 'Y' ? 'Y' : ''; + +my $sales_link = [ 'cust_bill_pkg.cgi?'. + "begin=$beginning;". + "end=$ending;". + "cust_main_sales=$cust_main_sales;". + 'salesnum='. $sales->salesnum. ';'. + 'nottax=1;'. + 'classnum=', + 'classnum' + ]; + +my $sales_sub = sub { + my $sales_pkg_class = shift; + + #efficiency improvement: ask the db for a sum instead of all the records + my $total_recur = 0; + my @cust_bill_pkg = $sales->cust_bill_pkg( + $beginning, + $ending, + 'cust_main_sales' => $cust_main_sales, + 'classnum' => $sales_pkg_class->classnum, + ); + $total_recur += $_->recur foreach @cust_bill_pkg; + + $money_char. sprintf('%.2f', $total_recur); +}; + +my $commission_sub = sub { + my $sales_pkg_class = 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, + 'commission_classnum' => $sales_pkg_class->classnum, + ); + $total_credit += $_->amount foreach @cust_credit; + + $money_char. sprintf('%.2f', $total_credit); +}; + +my $commission_link = [ 'cust_credit.html?'. + "begin=$beginning;". + "end=$ending;". + "cust_main_sales=$cust_main_sales;". + 'commission_salesnum='. $sales->salesnum. ';'. + 'commission_classnum=', + 'classnum' + ]; + </%init> diff --git a/httemplate/view/cust_main/packages/package.html b/httemplate/view/cust_main/packages/package.html index f0ece9884..1c8db15f4 100644 --- a/httemplate/view/cust_main/packages/package.html +++ b/httemplate/view/cust_main/packages/package.html @@ -107,7 +107,7 @@ % if ( $cust_pkg->salesnum ) { % $change_sales_label = 'change'; <% mt('Sales Person:') |h %> - <B><% $cust_pkg->salesperson %></B> + <B><% $cust_pkg->salesperson |h %></B> % } % if ( $curuser->access_right('Change customer package') |