From e47e9758f480c664bfc3917d798cd69c7d354999 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 11 Aug 2006 08:02:26 +0000 Subject: [PATCH] virtualize referrals on customer addition --- FS/FS/access_user.pm | 16 +++++ FS/FS/part_referral.pm | 21 ++++--- httemplate/browse/part_referral.html | 29 +++++---- httemplate/edit/cust_main.cgi | 80 +++++++++--------------- httemplate/elements/select-agent.html | 8 +-- httemplate/elements/select-part_referral.html | 17 +++++ httemplate/elements/tr-select-agent.html | 10 ++- httemplate/elements/tr-select-part_referral.html | 30 +++++++++ 8 files changed, 132 insertions(+), 79 deletions(-) create mode 100644 httemplate/elements/select-part_referral.html create mode 100644 httemplate/elements/tr-select-part_referral.html diff --git a/FS/FS/access_user.pm b/FS/FS/access_user.pm index f32fa3a23..830d7f826 100644 --- a/FS/FS/access_user.pm +++ b/FS/FS/access_user.pm @@ -5,6 +5,7 @@ use vars qw( @ISA $htpasswd_file ); use FS::Record qw( qsearch qsearchs dbh ); use FS::m2m_Common; use FS::access_usergroup; +use FS::agent; @ISA = qw( FS::m2m_Common FS::Record ); @@ -318,6 +319,21 @@ sub agentnum { $sth->fetchrow_arrayref->[0]; } +=item agents + +Returns the list of agents this user can view (via group membership), as +FS::agent objects. + +=cut + +sub agents { + my $self = shift; + qsearch({ + 'table' => 'agent', + 'hashref' => { disabled=>'' }, + 'extra_sql' => ' AND '. $self->agentnums_sql, + }); +} =item access_right diff --git a/FS/FS/part_referral.pm b/FS/FS/part_referral.pm index e1688650d..87bc87cba 100644 --- a/FS/FS/part_referral.pm +++ b/FS/FS/part_referral.pm @@ -124,30 +124,37 @@ sub agent { =over 4 -=item acl_agentnum_sql +=item acl_agentnum_sql [ INCLUDE_GLOBAL_BOOL ] Returns an SQL fragment for searching for part_referral records allowed by the current users's agent ACLs (and "Edit global advertising sources" right). +Pass a true value to include global advertising sources (for example, when +simply using rather than editing advertising sources). + =cut sub acl_agentnum_sql { - #my $class = shift; + my $self = shift; my $curuser = $FS::CurrentUser::CurrentUser; my $sql = $curuser->agentnums_sql; $sql = " ( $sql OR agentnum IS NULL ) " - if $curuser->access_right('Edit global advertising sources'); + if $curuser->access_right('Edit global advertising sources') + or defined($_[0]) && $_[0]; $sql; } -=item all_part_referral +=item all_part_referral [ INCLUDE_GLOBAL_BOOL ] Returns all part_referral records allowed by the current users's agent ACLs (and "Edit global advertising sources" right). +Pass a true value to include global advertising sources (for example, when +simply using rather than editing advertising sources). + =cut sub all_part_referral { @@ -155,12 +162,12 @@ sub all_part_referral { qsearch({ 'table' => 'part_referral', - 'extra_sql' => ' WHERE '. $self->acl_agentnum_sql. ' ORDER BY refnum ', + 'extra_sql' => ' WHERE '. $self->acl_agentnum_sql(@_). ' ORDER BY refnum ', }); } -=item num_part_referral +=item num_part_referral [ INCLUDE_GLOBAL_BOOL ] Returns the number of part_referral records allowed by the current users's agent ACLs (and "Edit global advertising sources" right). @@ -171,7 +178,7 @@ sub num_part_referral { my $self = shift; my $sth = dbh->prepare( - 'SELECT COUNT(*) FROM part_referral WHERE '. $self->acl_agentnum_sql + 'SELECT COUNT(*) FROM part_referral WHERE '. $self->acl_agentnum_sql(@_) ) or die dbh->errstr; $sth->execute() or die $sth->errstr; $sth->fetchrow_arrayref->[0]; diff --git a/httemplate/browse/part_referral.html b/httemplate/browse/part_referral.html index 468593160..c50a406ed 100755 --- a/httemplate/browse/part_referral.html +++ b/httemplate/browse/part_referral.html @@ -32,7 +32,7 @@ Where a customer heard about your service. Tracked for informational purposes. 'Total' => 86400, # 60sec * 60min * 24hrs ); - $extra_sql = " WHERE ". FS::part_referral->acl_agentnum_sql; + my $curuser = $FS::CurrentUser::CurrentUser; my $statement = "SELECT COUNT(*) FROM h_cust_main WHERE history_action = 'insert' @@ -67,14 +67,7 @@ Where a customer heard about your service. Tracked for informational purposes. <% -foreach my $part_referral ( - - qsearch({ - 'table' => 'part_referral', - 'extra_sql' => "$extra_sql ORDER BY refnum", - }) - -) { +foreach my $part_referral ( FS::part_referral->all_part_referral(1) ) { if ( $bgcolor eq $bgcolor1 ) { $bgcolor = $bgcolor2; @@ -82,13 +75,23 @@ foreach my $part_referral ( $bgcolor = $bgcolor1; } + $a = 0; + %> - - <%= $part_referral->refnum %> - - <%= $part_referral->referral %> + + <% if ( $part_referral->agentnum || $curuser->access_right('Edit global advertising sources') ) { + $a++; + %> + + <% } %> + <%= $part_referral->refnum %><%= $a ? '' : '' %> + + <% if ( $a ) { %> + + <% } %> + <%= $part_referral->referral %><%= $a ? '' : '' %> <% if ( $show_agentnums ) { %> <%= $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %> diff --git a/httemplate/edit/cust_main.cgi b/httemplate/edit/cust_main.cgi index bb2a8618e..45cb69fc2 100755 --- a/httemplate/edit/cust_main.cgi +++ b/httemplate/edit/cust_main.cgi @@ -66,6 +66,8 @@ if ( $cgi->param('error') ) { $cgi->delete_all(); my $action = $custnum ? 'Edit' : 'Add'; +my $r = qq!* !; + %> @@ -77,38 +79,29 @@ my $action = $custnum ? 'Edit' : 'Add'; ) %> <% if ( $error ) { %> -Error: <%= $error %> +Error: <%= $error %>

<% } %>
-Customer # <%= $custnum ? "$custnum" : " (NEW)" %> - - - -<% +<% if ( $custnum ) { %> + Customer #<%= $custnum %> - + + <%= ucfirst($cust_main->status) %> + +

+<% } %> -my $r = qq!* !; +<%= &ntable("#cccccc") %> -my %agent_search = dbdef->table('agent')->column('disabled') - ? ( 'disabled' => '' ) : (); -my @agents = qsearch( 'agent', \%agent_search ); -#die "No agents created!" unless @agents; -eidiot "You have not created any agents (or all agents are disabled). You must create at least one agent before adding a customer. Go to ". popurl(2). "browse/agent.cgi and create one or more agents." unless @agents; -my $agentnum = $cust_main->agentnum || $agents[0]->agentnum; #default to first + +<%= include('/elements/tr-select-agent.html', $cust_main->agentnum, + 'label' => "${r}Agent", + 'empty_label' => 'Select agent', + ) %> -<% if ( scalar(@agents) == 1 ) { %> - -<% } else { %> -

<%=$r%>Agent -<% } %> - <% @@ -118,28 +111,9 @@ if ( $custnum && ! $conf->exists('editreferrals') ) { -<% - } else { - - my(@referrals) = qsearch('part_referral',{}); - if ( scalar(@referrals) == 0 ) { - eidiot "You have not created any advertising sources. You must create at least one advertising source before adding a customer. Go to ". popurl(2). "browse/part_referral.cgi and create one or more advertising sources."; - } elsif ( scalar(@referrals) == 1 ) { - $refnum ||= $referrals[0]->refnum; -%> - - - <% } else { %> -

<%=$r%>Advertising source - -<% } %> + <%= include('/elements/tr-select-part_referral.html') %> <% } %> @@ -153,14 +127,20 @@ if ( $cust_main->referral_custnum ) { %> -

Referring Customer: - <%= $cust_main->referral_custnum %>: <%= $referring_cust_main->name %> + + Referring customer + + <%= $cust_main->referral_custnum %>: <%= $referring_cust_main->name %> + + <% } elsif ( ! $conf->exists('disable_customer_referrals') ) { %> -

Referring customer number: - + + Referring customer + + <% } else { %> @@ -168,6 +148,8 @@ if ( $cust_main->referral_custnum <% } %> + +

@@ -377,10 +359,10 @@ unless ( $custnum ) { #false laziness, copied from FS::cust_pkg::order my $pkgpart; + my @agents = $FS::CurrentUser::CurrentUser->agents; if ( scalar(@agents) == 1 ) { # $pkgpart->{PKGPART} is true iff $custnum may purchase PKGPART - my($agent)=qsearchs('agent',{'agentnum'=> $agentnum }); - $pkgpart = $agent->pkgpart_hashref; + $pkgpart = $agents[0]->pkgpart_hashref; } else { #can't know (agent not chosen), so, allow all my %typenum; diff --git a/httemplate/elements/select-agent.html b/httemplate/elements/select-agent.html index 78ec25f82..009cc6e06 100644 --- a/httemplate/elements/select-agent.html +++ b/httemplate/elements/select-agent.html @@ -1,8 +1,7 @@ <% my( $agentnum, %opt ) = @_; - my %select_opt = (); - $select_opt{'records'} = $opt{'agents'} + $opt{'records'} = delete $opt{'agents'} if $opt{'agents'}; %><%= include( '/elements/select-table.html', @@ -12,7 +11,8 @@ 'empty_label' => 'all', 'hashref' => { 'disabled' => '' }, 'extra_sql' => ' AND '. - $FS::CurrentUser::CurrentUser->agentnums_sql, - %select_opt, + $FS::CurrentUser::CurrentUser->agentnums_sql. + ' ORDER BY agent', + %opt, ) %> diff --git a/httemplate/elements/select-part_referral.html b/httemplate/elements/select-part_referral.html new file mode 100644 index 000000000..deb87bd3b --- /dev/null +++ b/httemplate/elements/select-part_referral.html @@ -0,0 +1,17 @@ +<% + my( $refnum, %opt ) = @_; + + $opt{'records'} = delete $opt{'part_referrals'} + if $opt{'part_referrals'}; + +%><%= include( '/elements/select-table.html', + 'table' => 'part_referral', + 'name_col' => 'referral', + 'value' => $refnum, + 'empty_label' => 'Select advertising source', + 'hashref' => { 'disabled' => '' }, + 'extra_sql' => ' AND '. + FS::part_referral->all_part_referral(1), + %opt, + ) +%> diff --git a/httemplate/elements/tr-select-agent.html b/httemplate/elements/tr-select-agent.html index 83c8d1758..6158f6f47 100644 --- a/httemplate/elements/tr-select-agent.html +++ b/httemplate/elements/tr-select-agent.html @@ -7,12 +7,9 @@ #here is the agent virtualization my $agentnums_href = $FS::CurrentUser::CurrentUser->agentnums_href; @agents = grep $agentnums_href->{$_->agentnum}, @{ $opt{'agents'} }; + delete $opt{'agents'}; } else { - @agents = qsearch( { - 'table' => 'agent', - 'hashref' => { disabled=>'' }, - 'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql, - }); + @agents = $FS::CurrentUser::CurrentUser->agents; } %> @@ -24,10 +21,11 @@ <% } else { %> - <%= $opt{'label'} || 'Agent: ' %> + <%= $opt{'label'} || 'Agent' %> <%= include( '/elements/select-agent.html', $agentnum, 'agents' => \@agents, + %opt, ) %> diff --git a/httemplate/elements/tr-select-part_referral.html b/httemplate/elements/tr-select-part_referral.html new file mode 100644 index 000000000..0108388bc --- /dev/null +++ b/httemplate/elements/tr-select-part_referral.html @@ -0,0 +1,30 @@ +<% + my( $refnum, %opt ) = @_; + + $opt{'part_referrals'} ||= + [ FS::part_referral->all_part_referral( 1 ) ]; #1: include global + + my $r = qq!* !; + +%> + +<% if ( scalar( @{$opt{'part_referrals'}} ) == 0 ) { + eidiot "You have not created any advertising sources. You must create at least one advertising source before adding a customer. Go to ". popurl(2). "browse/part_referral.html and create one or more advertising sources."; + } elsif ( scalar( @{$opt{'part_referrals'}} ) == 1 ) { +%> + + + +<% } else { %> + + + <%=$r%>Advertising source + + <%= include( '/elements/select-part_referral.html', $refnum, + 'part_referrals' => $opt{'part_referrals'}, + ) + %> + + + +<% } %> -- 2.11.0