X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg_taxrate.pm;h=e29c3d0b47c524705358d85ea90525dbcad4e192;hb=f88a13f394b3132a439608728b31f9fb5026fdfd;hp=6267d7a446708412cb00f11b58f9508c40be063a;hpb=8381e7232f90ac22b3f655cdccd0d39e2bde1d63;p=freeside.git diff --git a/FS/FS/part_pkg_taxrate.pm b/FS/FS/part_pkg_taxrate.pm index 6267d7a44..e29c3d0b4 100644 --- a/FS/FS/part_pkg_taxrate.pm +++ b/FS/FS/part_pkg_taxrate.pm @@ -3,9 +3,12 @@ package FS::part_pkg_taxrate; use strict; use vars qw( @ISA ); use Date::Parse; +use DateTime; +use DateTime::Format::Strptime; 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); @@ -175,12 +178,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="$3/$2/$1"); + $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,12 +246,12 @@ 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)), - $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; @@ -265,24 +281,48 @@ 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'}); + 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}); + 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 = 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; + unless ( $part_pkg_taxrate ) { + if ( $hash->{taxproductnum} ) { + my $taxproduct = + qsearchs( 'part_pkg_taxproduct', + { 'taxproductnum' => $hash->{taxproductnum} } + ); + $hash->{taxproductnum} .= ' ( '. $taxproduct->taxproduct. ' )' + if $taxproduct; + } + return "Can't find part_pkg_taxrate to delete: ". + join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) ); + } my $error = $part_pkg_taxrate->delete; return $error if $error; @@ -329,7 +369,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; @@ -368,7 +408,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