fix cch update removal of PLUS4/ZIP and TXMATRIX, RT#21687
[freeside.git] / FS / FS / cust_tax_location.pm
index bb61089..aec9410 100644 (file)
@@ -119,25 +119,31 @@ sub check {
     || $self->ut_text('state')
     || $self->ut_numbern('plus4hi')
     || $self->ut_numbern('plus4lo')
-    || $self->ut_enum('default', [ '', ' ', 'Y' ] ) # wtf?
+    || $self->ut_enum('default_location', [ '', 'Y' ] )
     || $self->ut_enum('cityflag', [ '', 'I', 'O', 'B' ] )
     || $self->ut_alpha('geocode')
   ;
   return $error if $error;
 
-  #ugh!  cch canada weirdness
-  if ($self->state eq 'CN') {
+  #ugh!  cch canada weirdness and more
+  if ($self->state eq 'CN' && $self->data_vendor eq 'cch-zip' ) {
     $error = "Illegal cch canadian zip"
      unless $self->zip =~ /^[A-Z]$/;
+  } elsif ($self->state =~ /^E([B-DFGILNPR-UW])$/ && $self->data_vendor eq 'cch-zip' ) {
+    $error = "Illegal cch european zip"
+     unless $self->zip =~ /^E$1$/;
   } else {
     $error = $self->ut_number('zip', $self->state eq 'CN' ? 'CA' : 'US');
   }
   return $error if $error;
 
-  #ugh!  cch canada weirdness
+  #ugh!  cch canada weirdness and more
   return "must specify either city/county or plus4lo/plus4hi"
     unless ( $self->plus4lo && $self->plus4hi || 
-             ($self->city || $self->state eq 'CN') && $self->county
+             ( $self->city ||
+               $self->state eq 'CN' ||
+               $self->state =~ /^E([B-DFGILNPR-UW])$/
+             ) && $self->county
            );
 
   $self->SUPER::check;
@@ -179,7 +185,7 @@ sub batch_import {
   }
 
   if ( $format eq 'cch' || $format eq 'cch-update' ) {
-    @fields = qw( zip state plus4lo plus4hi geocode default );
+    @fields = qw( zip state plus4lo plus4hi geocode default_location );
     push @fields, 'actionflag' if $format eq 'cch-update';
 
     $imported++ if $format eq 'cch-update'; #empty file ok
@@ -188,6 +194,7 @@ sub batch_import {
       my $hash = shift;
 
       $hash->{'data_vendor'} = 'cch';
+      $hash->{'default_location'} =~ s/ //g;
 
       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
         delete($hash->{actionflag});
@@ -210,8 +217,8 @@ sub batch_import {
     };
 
   } elsif ( $format eq 'cch-zip' || $format eq 'cch-update-zip' ) {
-    @fields = qw( zip city county state postalcity countyfips countydef default geocode cityflag unique );
-    push @fields, 'actionflag' if $format eq 'cch-update';
+    @fields = qw( zip city county state postalcity countyfips countydef default_location geocode cityflag unique );
+    push @fields, 'actionflag' if $format eq 'cch-update-zip';
 
     $imported++ if $format eq 'cch-update'; #empty file ok
     
@@ -221,18 +228,21 @@ sub batch_import {
       $hash->{'data_vendor'} = 'cch-zip';
       delete($hash->{$_}) foreach qw( countyfips countydef unique );
 
-      $hash->{'cityflag'} = s/ //g;
+      $hash->{'cityflag'} =~ s/ //g;
+      $hash->{'default_location'} =~ s/ //g;
 
       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
         delete($hash->{actionflag});
 
-        my $cust_tax_location = qsearchs('cust_tax_location', $hash);
+        my @cust_tax_location = qsearch('cust_tax_location', $hash);
         return "Can't find cust_tax_location to delete: ".
                join(" ", map { "$_ => ". $hash->{$_} } @fields)
-          unless $cust_tax_location;
+          unless scalar(@cust_tax_location) || $param->{'delete_only'} ;
 
-        my $error = $cust_tax_location->delete;
-        return $error if $error;
+        foreach my $cust_tax_location (@cust_tax_location) {
+          my $error = $cust_tax_location->delete;
+          return $error if $error;
+        }
 
         delete($hash->{$_}) foreach (keys %$hash);
       }
@@ -275,7 +285,7 @@ sub batch_import {
     if ( $job ) {  # progress bar
       if ( time - $min_sec > $last ) {
         my $error = $job->update_statustext(
-          int( 100 * $imported / $count )
+          int( 100 * $imported / $count ). ",Importing locations"
         );
         die $error if $error;
         $last = time;
@@ -290,7 +300,7 @@ sub batch_import {
     }
     if ( scalar( @columns ) ) {
       $dbh->rollback if $oldAutoCommit;
-      return "Unexpected trailing columns in line (wrong format?): $line";
+      return "Unexpected trailing columns in line (wrong format?) importing cust_tax_location: $line";
     }
 
     my $error = &{$hook}(\%cust_tax_location);
@@ -314,7 +324,7 @@ sub batch_import {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  return "Empty file!" unless $imported;
+  return "Empty file!" unless ( $imported || $format =~ /^cch-update/ );
 
   ''; #no error