X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Ftax_class.pm;h=04e9d37f95c48995fae5b4aa2ce496b86a49d425;hb=60dc4fe638eb9abc5a3ea92d43031dcbfeb71454;hp=2fa9fb0f8238b1da9bc9cb27a8df74e798639676;hpb=052ebe1ded9859bff023141274edf891e743275a;p=freeside.git diff --git a/FS/FS/tax_class.pm b/FS/FS/tax_class.pm index 2fa9fb0f8..04e9d37f9 100644 --- a/FS/FS/tax_class.pm +++ b/FS/FS/tax_class.pm @@ -4,6 +4,9 @@ use strict; use vars qw( @ISA ); use FS::UID qw(dbh); use FS::Record qw( qsearch qsearchs ); +use FS::Misc qw( csv_from_fixed ); +use FS::part_pkg_taxrate; +use FS::part_pkg_taxoverride; @ISA = qw(FS::Record); @@ -82,20 +85,39 @@ Delete this record from the database. sub delete { my $self = shift; - return "Can't delete a tax class which has tax rates!" - if qsearch( 'tax_rate', { 'taxclassnum' => $self->taxclassnum } ); - - return "Can't delete a tax class which has package tax rates!" - if qsearch( 'part_pkg_taxrate', { 'taxclassnum' => $self->taxclassnum } ); - - return "Can't delete a tax class which has package tax rates!" - if qsearch( 'part_pkg_taxrate', { 'taxclassnumtaxed' => $self->taxclassnum } ); + #return "Can't delete a tax class which has package tax rates!" + #if qsearch( 'part_pkg_taxrate', { 'taxclassnumtaxed' => $self->taxclassnum + # If this tax class is manually assigned to a package, + # then return a useful error message instead of just having a conniption. + my @overrides = qsearch( 'part_pkg_taxoverride', { + 'taxclassnum' => $self->taxclassnum + } ); + if (@overrides) { + return "Tried to delete tax class " . $self->taxclass . + ", which is assigned to package definition " . + join(', ', map { '#'.$_->pkgpart} @overrides) . + "."; + } - return "Can't delete a tax class which has package tax overrides!" - if qsearch( 'part_pkg_taxoverride', { 'taxclassnum' => $self->taxclassnum } ); + # part_pkg_taxrate.taxclass identifies taxes belonging to this taxclass. + # part_pkg_taxrate.taxclassnumtaxed identifies taxes applying to this + # taxclass. + # If this taxclass goes away, remove all of them. (CCH upgrade CAN'T + # remove them, because it removes the tax_class first and then doesn't + # know what the taxclassnum was. Yeah, I know. So it will just skip + # over them at the TXMATRIX stage.) + my @part_pkg_taxrate = ( + qsearch('part_pkg_taxrate', { 'taxclassnum' => $self->taxclassnum }), + qsearch('part_pkg_taxrate', { 'taxclassnumtaxed' => $self->taxclassnum }) + ); + foreach (@part_pkg_taxrate) { + my $error = $_->delete; + return "when deleting taxclass ".$self->taxclass.": $error" + if $error; + } $self->SUPER::delete(@_); - + } =item replace OLD_RECORD @@ -147,12 +169,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,21 +267,30 @@ 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; } } - my $tax_class = - new FS::tax_class( { 'data_vendor' => 'cch', - 'taxclass' => $type->[0].':'.$cat->[0], - 'description' => $type->[1].':'.$cat->[1], - } ); - my $error = $tax_class->insert; - return $error if $error; + my %hash = ( 'data_vendor' => 'cch', + 'taxclass' => $type->[0].':'.$cat->[0], + 'description' => $type->[1].':'.$cat->[1], + ); + unless ( qsearchs('tax_class', \%hash) ) { + my $tax_class = new FS::tax_class \%hash; + my $error = $tax_class->insert; + + return "can't insert tax_class for ". + " old TAXTYPE ". $type->[0].':'.$type->[1]. + " and new TAXCAT ". $cat->[0].':'. $cat->[1]. + " : $error" + if $error; + } + $imported++; + } } @@ -262,7 +300,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; @@ -275,7 +313,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 +349,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 +369,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 +393,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 @@ -370,9 +408,6 @@ sub batch_import { =head1 BUGS - batch_import does not handle mixed I and D records in the same file for - format cch-update - =head1 SEE ALSO L, schema.html from the base documentation.