merge webpay support in with autoselection of old realtime_bop and realtime_refund_bop
[freeside.git] / FS / FS / part_pkg_taxrate.pm
index 5ef887d..6d1414a 100644 (file)
@@ -6,6 +6,7 @@ use Date::Parse;
 use FS::UID qw(dbh);
 use FS::Record qw( qsearch qsearchs );
 use FS::part_pkg_taxproduct;
+use FS::Misc qw(csv_from_fixed);
 
 @ISA = qw(FS::Record);
 
@@ -150,7 +151,7 @@ sub check {
     || $self->ut_text('country')
     || $self->ut_foreign_keyn('taxclassnumtaxed', 'tax_class', 'taxclassnum')
     || $self->ut_foreign_key('taxclassnum', 'tax_class', 'taxclassnum')
-    || $self->ut_numbern('effective_date')
+    || $self->ut_snumbern('effdate')
     || $self->ut_enum('taxable', [ 'Y', '' ])
   ;
   return $error if $error;
@@ -175,12 +176,25 @@ sub batch_import {
   my @fields;
   my $hook;
 
+  my @column_lengths = ();
+  my @column_callbacks = ();
+  if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
+    $format =~ s/-fixed//;
+    my $date_format = sub { my $r='';
+                            /^(\d{4})(\d{2})(\d{2})$/ && ($r="$1/$2/$3");
+                            $r;
+                          };
+    $column_callbacks[16] = $date_format;
+    push @column_lengths, qw( 28 25 2 1 10 4 30 3 100 2 2 2 2 1 2 2 8 1 );
+    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_callbacks) ) {
+    my $error =
+      csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
+    return $error if $error;
   }
 
   if ( $format eq 'cch' ||  $format eq 'cch-update' ) {
@@ -230,7 +244,7 @@ sub batch_import {
       unless ($part_pkg_taxproduct) {
         return "Can't find part_pkg_taxproduct for txmatrix deletion: ".
                join(" ", map { "$_ => ". $hash->{$_} } @fields)
-          if $hash->{'actionflag'} eq 'D';
+          if ($hash->{'actionfield'} && $hash->{'actionflag'} eq 'D');
 
         $part_pkg_taxproduct{'description'} = 
           join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)),
@@ -265,14 +279,14 @@ sub batch_import {
 
         return "Can't find tax class for txmatrix deletion: ".
                join(" ", map { "$_ => ". $hash->{$_} } @fields)
-          if ($hash->{'actionflag'} eq 'D' && !$tax_class && $class ne ':');
+          if ( $hash->{'actionflag'} && $hash->{'actionflag'} eq 'D' &&
+               !$tax_class && $class ne ':'
+             );
 
         delete($hash->{$_}) foreach @{$map{$item}};
       }
 
       $hash->{'effdate'} = str2time($hash->{'effdate'});
-
-      $hash->{'effdate'} = str2time($hash->{'effdate'});
       $hash->{'country'} = 'US'; # CA is available
 
       delete($hash->{'taxable'}) if ($hash->{'taxable'} eq 'N');
@@ -370,7 +384,7 @@ sub batch_import {
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
 
-  return "Empty file!" unless $imported;
+  return "Empty file!" unless ( $imported || $format eq 'cch-update' );
 
   ''; #no error