default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / part_pkg_taxrate.pm
index 1563621..bfb1ea8 100644 (file)
@@ -3,8 +3,9 @@ package FS::part_pkg_taxrate;
 use strict;
 use vars qw( @ISA );
 use Date::Parse;
-use FS::UID qw(dbh);
-use FS::Record qw( qsearch qsearchs );
+use DateTime;
+use DateTime::Format::Strptime;
+use FS::Record qw( qsearch qsearchs dbh );
 use FS::part_pkg_taxproduct;
 use FS::Misc qw(csv_from_fixed);
 
@@ -181,7 +182,7 @@ sub batch_import {
   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");
+                            /^(\d{4})(\d{2})(\d{2})$/ && ($r="$3/$2/$1");
                             $r;
                           };
     $column_callbacks[16] = $date_format;
@@ -242,14 +243,17 @@ 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'} || '') eq 'D') {
+          warn "WARNING: Can't find part_pkg_taxproduct for txmatrix deletion: ".
+            join(" ", map { "$_ => ". $hash->{$_} } @fields) .
+            " (ignored)\n";
+          next;
+        }
 
         $part_pkg_taxproduct{'description'} = 
           join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)),
-                      $providers{$hash->{'provider'}},
-                      $customers{$hash->{'customer'}},
+                      $providers{$hash->{'provider'}} || '',
+                      $customers{$hash->{'customer'}} || '',
               );
         $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct;
         my $error = $part_pkg_taxproduct->insert;
@@ -261,6 +265,8 @@ sub batch_import {
 
       delete($hash->{$_})
         for qw(group groupdesc item itemdesc provider customer rectype );
+      
+      # resolve the taxtype/taxcat fields to taxclassnums
 
       my %map = ( 'taxclassnum'      => [ 'taxtype', 'taxcat' ],
                   'taxclassnumtaxed' => [ 'taxtypetaxed', 'taxcattaxed' ],
@@ -274,32 +280,57 @@ sub batch_import {
                       'taxclass' => $class,
                     }
                   );
-        $hash->{$item} = $tax_class->taxclassnum
-          if $tax_class;
-
-        return "Can't find tax class for txmatrix deletion: ".
-               join(" ", map { "$_ => ". $hash->{$_} } @fields)
-          if ($hash->{'actionflag'} eq 'D' && !$tax_class && $class ne ':');
+        if ( $tax_class ) {
+          $hash->{$item} = $tax_class->taxclassnum;
+        } elsif ($class ne ':' and ($hash->{actionflag} || '') eq 'D') {
+          # return "Can't find tax class for txmatrix deletion: ".
+          warn "WARNING: Can't find tax class $class for txmatrix deletion (ignored)\n";
+          return ''; # don't delete the record, then
+        }
 
         delete($hash->{$_}) foreach @{$map{$item}};
       }
 
-      $hash->{'effdate'} = str2time($hash->{'effdate'});
+      my $parser = new DateTime::Format::Strptime( pattern => "%m/%d/%Y",
+                                                   time_zone => 'floating',
+                                                 );
+      my $dt = $parser->parse_datetime( $hash->{'effdate'} );
+      return "Can't parse effdate ". $hash->{'effdate'}. ': '. $parser->errstr
+        unless $dt;
+      $hash->{'effdate'} = $dt->epoch;
       $hash->{'country'} = 'US'; # CA is available
 
-      delete($hash->{'taxable'}) if ($hash->{'taxable'} eq 'N');
+      $hash->{'taxable'} = '' if ($hash->{'taxable'} eq 'N');
 
       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
         delete($hash->{actionflag});
 
-        my $part_pkg_taxrate = qsearchs('part_pkg_taxrate', $hash);
-        return "Can't find part_pkg_taxrate to delete: ".
-               #join(" ", map { "$_ => ". $hash->{$_} } @fields)
-               join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) )
-          unless $part_pkg_taxrate;
-
-        my $error = $part_pkg_taxrate->delete;
-        return $error if $error;
+        foreach my $intfield (qw( taxproductnum taxclassnum effdate )) {
+          if ( $hash->{$intfield} eq '' ) {
+            return "$intfield is empty in search! -- ".
+                   join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
+          }
+        }
+
+        my @part_pkg_taxrate = qsearch('part_pkg_taxrate', $hash);
+        unless ( scalar(@part_pkg_taxrate) || $param->{'delete_only'} ) {
+          if ( $hash->{taxproductnum} ) {
+            my $taxproduct =
+              qsearchs( 'part_pkg_taxproduct',
+                        { 'taxproductnum' => $hash->{taxproductnum} }
+                      );
+            $hash->{taxproductnum} .= ' ( '. $taxproduct->taxproduct. ' )'
+              if $taxproduct;
+          }
+          warn "WARNING: Can't find part_pkg_taxrate to delete: ".
+                 join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
+        }
+
+        foreach my $part_pkg_taxrate (@part_pkg_taxrate) {
+          my $error = $part_pkg_taxrate->delete;
+          return $error if $error;
+        }
 
         delete($hash->{$_}) foreach (keys %$hash);
       }
@@ -343,7 +374,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 matrix"
         );
         die $error if $error;
         $last = time;
@@ -358,7 +389,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 part_pkg_taxrate: $line";
     }
 
     my $error = &{$hook}(\%part_pkg_taxrate);
@@ -382,7 +413,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