add cust_main-require_locale configuration setting, RT#17594
[freeside.git] / FS / FS / cust_main.pm
index e8bd7b2..cba28bc 100644 (file)
@@ -8,6 +8,7 @@ use base qw( FS::cust_main::Packages FS::cust_main::Status
              FS::cust_main::Billing_Discount
              FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
              FS::geocode_Mixin
+             FS::o2m_Common
              FS::Record
            );
 use vars qw( $DEBUG $me $conf
@@ -465,7 +466,7 @@ sub insert {
 
   $self->signupdate(time) unless $self->signupdate;
 
-  $self->censusyear($conf->config('census_year')) if $self->censustract;
+  $self->censusyear($conf->config('census_year')||'2012') if $self->censustract;
 
   $self->auto_agent_custid()
     if $conf->config('cust_main-auto_agent_custid') && ! $self->agent_custid;
@@ -1534,9 +1535,13 @@ sub replace {
 
   if ( $self->censustract ne '' and $self->censustract ne $old->censustract ) {
     # update censusyear whenever tract code changes
-    $self->censusyear($conf->config('census_year'));
+    $self->censusyear($conf->config('census_year')||'2012');
   }
 
+  return "Invoicing locale is required"
+    if $old->locale
+    && ! $self->locale
+    && $conf->exists('cust_main-require_locale');
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
@@ -1847,7 +1852,7 @@ sub check {
     return $error if $error;
   }
 
-  if ( $conf->exists('cust_main-require_phone')
+  if ( $conf->exists('cust_main-require_phone', $self->agentnum)
        && ! length($self->daytime) && ! length($self->night) && ! length($self->mobile)
      ) {
 
@@ -2138,6 +2143,11 @@ sub check {
     $self->payname($1);
   }
 
+  return "Please select an invoicing locale"
+    if ! $self->locale
+    && ! $self->custnum
+    && $conf->exists('cust_main-require_locale');
+
   foreach my $flag (qw( tax spool_cdr squelch_cdr archived email_csv_cdr )) {
     $self->$flag() =~ /^(Y?)$/ or return "Illegal $flag: ". $self->$flag();
     $self->$flag($1);
@@ -3214,7 +3224,7 @@ sub check_invoicing_list {
   }
 
   return "Email address required"
-    if $conf->exists('cust_main-require_invoicing_list_email')
+    if $conf->exists('cust_main-require_invoicing_list_email', $self->agentnum)
     && ! grep { $_ !~ /^([A-Z]+)$/ } @$arrayref;
 
   '';
@@ -3961,11 +3971,35 @@ cust_main-default_agent_custid is set and it has a value, custnum otherwise.
 
 sub display_custnum {
   my $self = shift;
+
+  my $prefix = $conf->config('cust_main-custnum-display_prefix', $self->agentnum) || '';
+  if ( my $special = $conf->config('cust_main-custnum-display_special') ) {
+    if ( $special eq 'CoStAg' ) {
+      $prefix = uc( join('',
+        $self->country,
+        ($self->state =~ /^(..)/),
+        $prefix || ($self->agent->agent =~ /^(..)/)
+      ) );
+    }
+    elsif ( $special eq 'CoStCl' ) {
+      $prefix = uc( join('',
+        $self->country,
+        ($self->state =~ /^(..)/),
+        ($self->classnum ? $self->cust_class->classname =~ /^(..)/ : '__')
+      ) );
+    }
+    # add any others here if needed
+  }
+
+  my $length = $conf->config('cust_main-custnum-display_length');
   if ( $conf->exists('cust_main-default_agent_custid') && $self->agent_custid ){
     return $self->agent_custid;
-  } elsif ( $conf->config('cust_main-custnum-display_prefix') ) {
-    return $conf->config('cust_main-custnum-display_prefix').
-           sprintf('%08d', $self->custnum)
+  } elsif ( $prefix ) {
+    $length = 8 if !defined($length);
+    return $prefix . 
+           sprintf('%0'.$length.'d', $self->custnum)
+  } elsif ( $length ) {
+    return sprintf('%0'.$length.'d', $self->custnum);
   } else {
     return $self->custnum;
   }
@@ -5060,7 +5094,7 @@ sub _upgrade_data { #class method
     'UPDATE cust_main SET signupdate = (SELECT signupdate FROM h_cust_main WHERE signupdate IS NOT NULL AND h_cust_main.custnum = cust_main.custnum ORDER BY historynum DESC LIMIT 1) WHERE signupdate IS NULL',
   );
   # fix yyyy-m-dd formatted paydates
-  if ( driver_name =~ /^mysql$/i ) {
+  if ( driver_name =~ /^mysql/i ) {
     push @statements,
     "UPDATE cust_main SET paydate = CONCAT( SUBSTRING(paydate FROM 1 FOR 5), '0', SUBSTRING(paydate FROM 6) ) WHERE SUBSTRING(paydate FROM 7 FOR 1) = '-'";
   }
@@ -5070,8 +5104,8 @@ sub _upgrade_data { #class method
   }
 
   push @statements, #fix the weird BILL with a cc# in payinfo problem
-    #DCRD to be safe, or CARD?
-    "UPDATE cust_main SET payby = 'DCRD' WHERE payby = 'BILL' and length(payinfo) = 16";
+    #DCRD to be safe
+    "UPDATE cust_main SET payby = 'DCRD' WHERE payby = 'BILL' and length(payinfo) = 16 and payinfo ". regexp_sql. q( '^[0-9]*$' );
 
   foreach my $sql ( @statements ) {
     my $sth = dbh->prepare($sql) or die dbh->errstr;