X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg_taxrate.pm;h=bfb1ea8e27ff45f2e3cb59872f6df4d99d7e0a9b;hp=aa1c3df76a85b3183013f2eaabe77f14dbd50fe8;hb=HEAD;hpb=4104f4e3d1b387296b16b4a035b4b7f42e0c5977 diff --git a/FS/FS/part_pkg_taxrate.pm b/FS/FS/part_pkg_taxrate.pm index aa1c3df76..bfb1ea8e2 100644 --- a/FS/FS/part_pkg_taxrate.pm +++ b/FS/FS/part_pkg_taxrate.pm @@ -3,9 +3,11 @@ 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); @ISA = qw(FS::Record); @@ -150,7 +152,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; @@ -166,17 +168,43 @@ an error, returns the error, otherwise returns false. =cut sub batch_import { - my $param = shift; + my ($param, $job) = @_; my $fh = $param->{filehandle}; my $format = $param->{'format'}; + my $imported = 0; my @fields; my $hook; - if ( $format eq 'cch' ) { + + 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 || 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' ) { @fields = qw( city county state local geocode group groupdesc item itemdesc provider customer taxtypetaxed taxcattaxed taxable taxtype taxcat effdate rectype ); + push @fields, 'actionflag' if $format eq 'cch-update'; + + $imported++ if $format eq 'cch-update'; #empty file ok $hook = sub { my $hash = shift; @@ -186,6 +214,8 @@ sub batch_import { return; } + $hash->{'data_vendor'} = 'cch'; + my %providers = ( '00' => 'Regulated LEC', '01' => 'Regulated IXC', '02' => 'Unregulated LEC', @@ -211,11 +241,19 @@ sub batch_import { my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', { %part_pkg_taxproduct } ); + unless ($part_pkg_taxproduct) { + 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'}} || 'Unknown', - $customers{$hash->{'customer'}} || 'Unknown', + join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)), + $providers{$hash->{'provider'}} || '', + $customers{$hash->{'customer'}} || '', ); $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct; my $error = $part_pkg_taxproduct->insert; @@ -227,30 +265,77 @@ 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' ], ); for my $item (keys %map) { + my $class = join(':', map($hash->{$_}, @{$map{$item}})); my $tax_class = qsearchs( 'tax_class', { data_vendor => 'cch', - 'taxclass' => join(':', map($hash->{$_}, @{$map{$item}})), + 'taxclass' => $class, } ); - $hash->{$item} = $tax_class->taxclassnum - if $tax_class; + 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'}); - - $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 = 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); + } + + delete($hash->{actionflag}); ''; }; @@ -268,8 +353,6 @@ sub batch_import { my $csv = new Text::CSV_XS; - my $imported = 0; - local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; local $SIG{QUIT} = 'IGNORE'; @@ -281,19 +364,34 @@ sub batch_import { local $FS::UID::AutoCommit = 0; my $dbh = dbh; - my $line; while ( defined($line=<$fh>) ) { $csv->parse($line) or do { $dbh->rollback if $oldAutoCommit; return "can't parse: ". $csv->error_input(); }; + + if ( $job ) { # progress bar + if ( time - $min_sec > $last ) { + my $error = $job->update_statustext( + int( 100 * $imported / $count ). ",Importing tax matrix" + ); + die $error if $error; + $last = time; + } + } + my @columns = $csv->fields(); my %part_pkg_taxrate = ( 'data_vendor' => $format ); foreach my $field ( @fields ) { $part_pkg_taxrate{$field} = shift @columns; } + if ( scalar( @columns ) ) { + $dbh->rollback if $oldAutoCommit; + return "Unexpected trailing columns in line (wrong format?) importing part_pkg_taxrate: $line"; + } + my $error = &{$hook}(\%part_pkg_taxrate); if ( $error ) { $dbh->rollback if $oldAutoCommit; @@ -315,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