add options to auto-generate agent_custid and display it as the customer number,...
authorivan <ivan>
Fri, 10 Oct 2008 01:15:50 +0000 (01:15 +0000)
committerivan <ivan>
Fri, 10 Oct 2008 01:15:50 +0000 (01:15 +0000)
FS/FS/Conf.pm
FS/FS/UI/Web.pm
FS/FS/UI/Web/small_custview.pm
FS/FS/cust_main.pm
httemplate/edit/cust_main.cgi
httemplate/index.html
httemplate/search/cust_main.cgi
httemplate/view/cust_main/misc.html

index 610d590..46e636b 100644 (file)
@@ -2423,6 +2423,23 @@ worry that config_items is freeside-specific and icky.
   },
 
   {
+    'key'         => 'cust_main-default_agent_custid',
+    'section'     => 'UI',
+    'description' => 'Display the agent_custid field instead of the custnum field.',
+    'type'        => 'checkbox',
+  },
+
+  {
+    'key'         => 'cust_main-auto_agent_custid',
+    'section'     => 'UI',
+    'description' => 'Automatically assign an agent_custid - select format',
+    'type'        => 'select',
+    'select_hash' => [ '' => 'No',
+                       '1YMMXXXXXXXX' => '1YMMXXXXXXXX',
+                     ],
+  },
+
+  {
     'key'         => 'cust_main-default_areacode',
     'section'     => 'UI',
     'description' => 'Default area code for customers.',
index 2e9261b..744d5f7 100644 (file)
@@ -214,6 +214,8 @@ sub cust_header {
   warn "FS::UI:Web::cust_header called"
     if $DEBUG;
 
+  my $conf = new FS::Conf;
+
   my %header2method = (
     'Customer'                 => 'name',
     'Cust. Status'             => 'ucfirst_cust_status',
@@ -239,6 +241,8 @@ sub cust_header {
     'Payment Type'             => 'payby',
     'Current Balance'          => 'current_balance',
   );
+  $header2method{'Cust#'} = 'display_custnum'
+    if $conf->exists('cust_main-default_agent_custid');
 
   my %header2colormethod = (
     'Cust. Status' => 'cust_statuscolor',
@@ -261,7 +265,6 @@ sub cust_header {
 
   } else {
 
-    my $conf = new FS::Conf;
     if (    $conf->exists('cust-fields')
          && $conf->config('cust-fields') =~ /^([\w\. \|\#\(\)]+):?/
        )
index 079ae93..f8e2020 100644 (file)
@@ -33,7 +33,7 @@ sub small_custview {
   $html = qq!View <A HREF="$url?! . $cust_main->custnum . '">'
     if $url;
 
-  $html .= 'Customer #<B>'. $cust_main->custnum. '</B></A>'.
+  $html .= 'Customer #<B>'. $cust_main->display_custnum. '</B></A>'.
     ' - <B><FONT COLOR="#'. $cust_main->statuscolor. '">'.
     ucfirst($cust_main->status). '</FONT></B>'.
     ntable('#e8e8e8'). '<TR><TD VALIGN="top">'. ntable("#cccccc",2).
index 225854c..c048482 100644 (file)
@@ -343,6 +343,9 @@ sub insert {
 
   $self->signupdate(time) unless $self->signupdate;
 
+  $self->auto_agent_custid()
+    if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
+
   my $error = $self->SUPER::insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -419,6 +422,35 @@ sub insert {
 
 }
 
+use File::CounterFile;
+sub auto_agent_custid {
+  my $self = shift;
+
+  my $format = $conf->config('cust_main-auto_agent_custid');
+  my $agent_custid;
+  if ( $format eq '1YMMXXXXXXXX' ) {
+
+    my $counter = new File::CounterFile 'cust_main.agent_custid';
+    $counter->lock;
+
+    my $ym = 100000000000 + time2str('%y%m00000000', time);
+    if ( $ym > $counter->value ) {
+      $counter->{'value'} = $agent_custid = $ym;
+      $counter->{'updated'} = 1;
+    } else {
+      $agent_custid = $counter->inc;
+    }
+
+    $counter->unlock;
+
+  } else {
+    die "Unknown cust_main-auto_agent_custid format: $format";
+  }
+
+  $self->agent_custid($agent_custid);
+
+}
+
 sub start_copy_skel {
   my $self = shift;
 
@@ -1233,6 +1265,7 @@ sub check {
     || $self->ut_textn('stateid_state')
     || $self->ut_textn('invoice_terms')
   ;
+
   #barf.  need message catalogs.  i18n.  etc.
   $error .= "Please select an advertising source."
     if $error =~ /^Illegal or empty \(numeric\) refnum: /;
@@ -5051,6 +5084,22 @@ sub cust_refund {
     qsearch( 'cust_refund', { 'custnum' => $self->custnum } )
 }
 
+=item display_custnum
+
+Returns the displayed customer number for this customer: agent_custid if
+cust_main-default_agent_custid is set and it has a value, custnum otherwise.
+
+=cut
+
+sub display_custnum {
+  my $self = shift;
+  if ( $conf->exists('cust_main-default_agent_custid') && $self->agent_custid ){
+    return $self->agent_custid;
+  } else {
+    return $self->custnum;
+  }
+}
+
 =item name
 
 Returns a name string for this customer, either "Company (Last, First)" or
index 10086a4..8de73c5 100755 (executable)
@@ -12,7 +12,7 @@
 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum %>">
 
 % if ( $custnum ) { 
-  Customer #<B><% $custnum %></B> - 
+  Customer #<B><% $cust_main->display_custnum %></B> - 
   <B><FONT COLOR="#<% $cust_main->statuscolor %>">
     <% ucfirst($cust_main->status) %>
   </FONT></B>
index 60ab26f..c813991 100644 (file)
@@ -36,7 +36,7 @@
 % next unless $cust_main; 
 
     <TR>
-      <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><A HREF="view/cust_main.cgi?<% $custnum %>"><% $custnum %>: <% $cust_main->name %></A></TD>
+      <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><A HREF="view/cust_main.cgi?<% $custnum %>"><% $cust_main->display_custnum %>: <% $cust_main->name %></A></TD>
     </TR>
 
 %       if ( $bgcolor eq $bgcolor1 ) {
index 1ddafae..36e4374 100755 (executable)
 %  if ( $cgi->param('browse') ) {
 %    my $query = $cgi->param('browse');
 %    if ( $query eq 'custnum' ) {
-%      $sortby=\*custnum_sort;
-%      $orderby = "ORDER BY custnum";
+%      if ( $conf->exists('cust_main-default_agent_custid') ) {
+%        $sortby=\*display_custnum_sort;
+%        $orderby = "ORDER BY CASE WHEN agent_custid IS NOT NULL AND agent_custid != '' THEN CAST(agent_custid AS BIGINT) ELSE custnum END";
+%      } else {
+%        $sortby=\*custnum_sort;
+%        $orderby = "ORDER BY custnum";
+%      }
 %    } elsif ( $query eq 'last' ) {
 %      $sortby=\*last_sort;
 %      $orderby = "ORDER BY LOWER(last || ' ' || first)";
 %    my $statuscol = $cust_main->statuscolor;
 
     <TR>
-      <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $custnum %></FONT></A></TD>
+      <TD CLASS="grid" ALIGN="right" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% $cust_main->display_custnum %></FONT></A></TD>
       <TD CLASS="grid" ALIGN="center" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><FONT SIZE="-1" COLOR="#<% $statuscol %>"><B><% ucfirst($status) %></B></FONT></TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><A HREF="<% $view %>"><FONT SIZE=-1><% "$last, $first" %></FONT></A></TD>
       <TD CLASS="grid" BGCOLOR="<% $bgcolor %>" ROWSPAN=<% $rowspan || 1 %>><% $pcompany %></TD>
 %  || lc($a->first) cmp lc($b->first);;
 %}
 %
+%sub display_custnum_sort {
+%  $a->display_custnum <=> $b->display_custnum;
+%}
+%
 %sub custnum_sort {
 %  $a->getfield('custnum') <=> $b->getfield('custnum');
 %}
index 9528db2..060da87 100644 (file)
@@ -1,41 +1,29 @@
-%
-%  my( $cust_main ) = @_;
-%  my $conf = new FS::Conf;
-%  my $date_format = ($conf->config('date_format') || "%m/%d/%Y");
-%
-
-
 <% ntable("#cccccc") %><TR><TD><% &ntable("#cccccc",2) %>
 
 <TR>
   <TD ALIGN="right">Customer&nbsp;number</TD>
-  <TD BGCOLOR="#ffffff"><% $cust_main->custnum %></TD>
+  <TD BGCOLOR="#ffffff"><% $cust_main->display_custnum %></TD>
 </TR>
 
 <TR>
   <TD ALIGN="right">Status</TD>
   <TD BGCOLOR="#ffffff"><FONT COLOR="#<% $cust_main->statuscolor %>"><B><% ucfirst($cust_main->status) %></B></FONT></TD>
 </TR>
-%
-%  my @agents = qsearch( 'agent', {} );
-%  my $agent;
-%  unless ( scalar(@agents) == 1 ) {
-%    $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
-%
 
+%my $agent;
+%if ( $num_agents == 1 ) {
+%  my @agents = qsearchs( 'agent', {} );
+%  $agent = $agents[0];
+%} else {
+%  $agent = qsearchs('agent',{ 'agentnum' => $cust_main->agentnum } );
+   <TR>
+     <TD ALIGN="right">Agent</TD>
+     <TD BGCOLOR="#ffffff"><% $agent->agentnum %>: <% $agent->agent %></TD>
+   </TR>
+% }
 
-<TR>
-  <TD ALIGN="right">Agent</TD>
-  <TD BGCOLOR="#ffffff"><% $agent->agentnum %>: <% $agent->agent %></TD>
-</TR>
-%
-%  } else {
-%    $agent = $agents[0];
-%  }
-%
-%  if ( $cust_main->agent_custid ) {
-%
-
+%  if ( $cust_main->agent_custid
+%       && ! $conf->exists('cust_main-default_agent_custid') ) {
 
 <TR>
   <TD ALIGN="right">Agent customer ref#</TD>
 % }
 
 </TABLE></TD></TR></TABLE>
+<%init>
+
+my( $cust_main ) = @_;
+my $conf = new FS::Conf;
+my $date_format = ($conf->config('date_format') || "%m/%d/%Y");
+
+my $sth = dbh->prepare('SELECT COUNT(*) FROM agent') or die dbh->errstr;
+$sth->execute or die $sth->errstr;
+my $num_agents = $sth->fetchrow_arrayref->[0];
 
+</%init>