fix CCH update adding a TAXCAT, RT#21687
[freeside.git] / FS / FS / tax_class.pm
index 2fa9fb0..eeb8993 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use vars qw( @ISA );
 use FS::UID qw(dbh);
 use FS::Record qw( qsearch qsearchs );
+use FS::Misc qw( csv_from_fixed );
 
 @ISA = qw(FS::Record);
 
@@ -147,12 +148,19 @@ sub batch_import {
   my $imported = 0;
   my $dbh = dbh;
 
+  my @column_lengths = ();
+  my @column_callbacks = ();
+  if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+    $format =~ s/-fixed//;
+    push @column_lengths, qw( 8 10 3 2 2 10 100 );
+    push @column_lengths, 1 if $format eq 'cch-update';
+  }
+
   my $line;
   my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
-  if ( $job ) {
-    $count++
-      while ( defined($line=<$fh>) );
-    seek $fh, 0, 0;
+  if ( $job || scalar(@column_lengths) ) {
+    my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
+    return $error if $error;
   }
 
   if ( $format eq 'cch' || $format eq 'cch-update' ) {
@@ -238,7 +246,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 tax classes"
               );
               die $error if $error;
               $last = time;
@@ -251,18 +259,20 @@ sub batch_import {
                                  'description' => $type->[1].':'.$cat->[1],
                              } );
           my $error = $tax_class->insert;
-          return $error if $error;
+          return "can't insert tax_class for old TAXTYPE $type and new TAXCAT $cat: $error" if $error;
           $imported++;
         }
       }
 
+      my %cats = map { $_=>1 } ( @old_cats, @{$data->{'taxcat'}} );
+
       foreach my $type (@{$data->{'taxtype'}}) {
-        foreach my $cat (@old_cats, @{$data->{'taxcat'}}) {
+        foreach my $cat (keys %cats) {
 
           if ( $job ) {  # progress bar
             if ( time - $min_sec > $last ) {
               my $error = $job->update_statustext(
-                int( 100 * $imported / $count )
+                int( 100 * $imported / $count ). ",Importing tax classes"
               );
               die $error if $error;
               $last = time;
@@ -275,7 +285,7 @@ sub batch_import {
                                  'description' => $type->[1].':'.$cat->[1],
                              } );
           my $error = $tax_class->insert;
-          return $error if $error;
+          return "can't insert tax_class for new TAXTYPE $type and TAXCAT $cat: $error" if $error;
           $imported++;
         }
       }
@@ -311,7 +321,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 tax classes"
         );
         die $error if $error;
         $last = time;
@@ -331,7 +341,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 tax_class: $line";
     }
 
     my $error = &{$hook}(\%tax_class);
@@ -355,12 +365,12 @@ sub batch_import {
   my $error = &{$endhook}();
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
-    return "can't insert tax_class for $line: $error";
+    return "can't run end hook: $error";
   }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  return "Empty File!" unless $imported;
+  return "Empty File!" unless ($imported || $format eq 'cch-update');
 
   ''; #no error