virtualize referrals on customer addition
authorivan <ivan>
Fri, 11 Aug 2006 08:02:26 +0000 (08:02 +0000)
committerivan <ivan>
Fri, 11 Aug 2006 08:02:26 +0000 (08:02 +0000)
FS/FS/access_user.pm
FS/FS/part_referral.pm
httemplate/browse/part_referral.html
httemplate/edit/cust_main.cgi
httemplate/elements/select-agent.html
httemplate/elements/select-part_referral.html [new file with mode: 0644]
httemplate/elements/tr-select-agent.html
httemplate/elements/tr-select-part_referral.html [new file with mode: 0644]

index f32fa3a..830d7f8 100644 (file)
@@ -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
 
index e168865..87bc87c 100644 (file)
@@ -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];
index 4685931..c50a406 100755 (executable)
@@ -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.
 </TR>
 
 <%
-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;
+
 %>
       <TR>
 
-        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
-          <%= $part_referral->refnum %></A></TD>
-        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
-          <%= $part_referral->referral %></A></TD>
+        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">
+          <% if ( $part_referral->agentnum || $curuser->access_right('Edit global advertising sources') ) { 
+            $a++;
+          %>
+            <A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
+          <% } %>
+          <%= $part_referral->refnum %><%= $a ? '</A>' : '' %></TD>
+        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>">
+          <% if ( $a ) { %>
+            <A HREF="<%= $p %>edit/part_referral.html?<%= $part_referral->refnum %>">
+          <% } %>
+          <%= $part_referral->referral %><%= $a ? '</A>' : '' %></TD>
 
         <% if ( $show_agentnums ) { %>
           <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"><%= $part_referral->agentnum ? $part_referral->agent->agent : '(global)' %></TD>
index bb2a861..45cb69f 100755 (executable)
@@ -66,6 +66,8 @@ if ( $cgi->param('error') ) {
 $cgi->delete_all();
 my $action = $custnum ? 'Edit' : 'Add';
 
+my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
+
 %>
 
 <!-- top -->
@@ -77,38 +79,29 @@ my $action = $custnum ? 'Edit' : 'Add';
 ) %>
 
 <% if ( $error ) { %>
-<FONT SIZE="+1" COLOR="#ff0000">Error: <%= $error %></FONT>
+<FONT SIZE="+1" COLOR="#ff0000">Error: <%= $error %></FONT><BR><BR>
 <% } %>
 
 <FORM NAME="topform" STYLE="margin-bottom: 0">
 <INPUT TYPE="hidden" NAME="custnum" VALUE="<%= $custnum %>">
-Customer # <%= $custnum ? "<B>$custnum</B>" : " (NEW)" %>
-
-<!-- agent -->
-
-<%
+<% if ( $custnum ) { %>
+  Customer #<B><%= $custnum %></B> - 
+  <B><FONT COLOR="<%= $cust_main->statuscolor %>">
+    <%= ucfirst($cust_main->status) %>
+  </FONT></B>
+  <BR><BR>
+<% } %>
 
-my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
+<%= &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
+<!-- agent -->
 
+<%= include('/elements/tr-select-agent.html', $cust_main->agentnum,
+              'label'       => "<B>${r}Agent</B>",
+              'empty_label' => 'Select agent',
+           )
 %>
 
-<% if ( scalar(@agents) == 1 ) { %>
-  <INPUT TYPE="hidden" NAME="agentnum" VALUE="<%= $agentnum %>">
-<% } else { %>
-  <BR><BR><%=$r%>Agent <SELECT NAME="agentnum" SIZE="1">
-  <% foreach my $agent (sort { $a->agent cmp $b->agent; } @agents) { %>
-    <OPTION VALUE="<%= $agent->agentnum %>"<%= " SELECTED"x($agent->agentnum==$agentnum) %>><%= $agent->agent %>
-  <% } %>
-  </SELECT>
-<% } %>
-
 <!-- referral (advertising source) -->
 
 <%
@@ -118,28 +111,9 @@ if ( $custnum && ! $conf->exists('editreferrals') ) {
 
   <INPUT TYPE="hidden" NAME="refnum" VALUE="<%= $refnum %>">
 
-<%
- } 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;
-%>
-
-     <INPUT TYPE="hidden" NAME="refnum" VALUE="<%= $refnum %>">
-
 <% } else { %>
 
-     <BR><BR><%=$r%>Advertising source 
-     <SELECT NAME="refnum" SIZE="1">
-       <%= $refnum ? '' : '<OPTION VALUE="">' %>
-       <% foreach my $referral (sort { $a->refnum <=> $b->refnum } @referrals) { %>
-         <OPTION VALUE="<%= $referral->refnum %>" <%= $referral->refnum == $refnum ? 'SELECTED' : '' %>><%= $referral->refnum %>: <%= $referral->referral %>
-       <% } %>
-     </SELECT>
-<% } %>
+   <%= include('/elements/tr-select-part_referral.html') %>
 
 <% } %>
 
@@ -153,14 +127,20 @@ if ( $cust_main->referral_custnum
 ) {
 %>
 
-  <BR><BR>Referring Customer: 
-  <A HREF="<%= popurl(1) %>/cust_main.cgi?<%= $cust_main->referral_custnum %>"><%= $cust_main->referral_custnum %>: <%= $referring_cust_main->name %></A>
+  <TR>
+    <TD ALIGN="right">Referring customer</TD>
+    <TD>
+      <A HREF="<%= popurl(1) %>/cust_main.cgi?<%= $cust_main->referral_custnum %>"><%= $cust_main->referral_custnum %>: <%= $referring_cust_main->name %></A>
+    </TD>
+  </TR>
   <INPUT TYPE="hidden" NAME="referral_custnum" VALUE="<%= $cust_main->referral_custnum %>">
 
 <% } elsif ( ! $conf->exists('disable_customer_referrals') ) { %>
 
-  <BR><BR>Referring customer number: 
-  <INPUT TYPE="text" NAME="referral_custnum" VALUE="">
+  <TR>
+    <TD ALIGN="right">Referring customer</TD>
+    <TD><INPUT TYPE="text" NAME="referral_custnum" VALUE=""></TD>
+  </TR>
 
 <% } else { %>
 
@@ -168,6 +148,8 @@ if ( $cust_main->referral_custnum
 
 <% } %>
 
+</TABLE>
+
 <!-- contact info -->
 
 <BR><BR>
@@ -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;
index 78ec25f..009cc6e 100644 (file)
@@ -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 (file)
index 0000000..deb87bd
--- /dev/null
@@ -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,
+             )
+%>
index 83c8d17..6158f6f 100644 (file)
@@ -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;
   }
 
 %>
 <% } else { %>
 
   <TR>
-    <TD ALIGN="right"><%= $opt{'label'} || 'Agent' %></TD>
+    <TD ALIGN="right"><%= $opt{'label'} || 'Agent' %></TD>
     <TD>
       <%= include( '/elements/select-agent.html', $agentnum,
                      'agents' => \@agents,
+                     %opt,
                  )
       %>
     </TD>
diff --git a/httemplate/elements/tr-select-part_referral.html b/httemplate/elements/tr-select-part_referral.html
new file mode 100644 (file)
index 0000000..0108388
--- /dev/null
@@ -0,0 +1,30 @@
+<%
+  my( $refnum, %opt ) = @_;
+
+  $opt{'part_referrals'} ||=
+    [ FS::part_referral->all_part_referral( 1 ) ]; #1: include global
+
+  my $r = qq!<font color="#ff0000">*</font>&nbsp;!;
+
+%>
+
+<% 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 ) {
+%>
+
+     <INPUT TYPE="hidden" NAME="refnum" VALUE="<%= $opt{'part_referrals'}->[0]->refnum %>">
+
+<% } else { %>
+
+     <TR>
+       <TH ALIGN="right"><%=$r%>Advertising source</TH>
+       <TD>
+         <%= include( '/elements/select-part_referral.html', $refnum,
+                        'part_referrals' => $opt{'part_referrals'},
+                    )
+         %>
+       </TD>
+     </TR>
+
+<% } %>