use strict;
use vars qw( @ISA );
-use FS::UID qw(dbh);
-use FS::Record qw( qsearch qsearchs );
+use Scalar::Util qw( blessed );
+use FS::UID qw( dbh );
+use FS::Record; # qw( qsearch qsearchs );
+use FS::cust_main_county;
@ISA = qw(FS::Record);
=head1 DESCRIPTION
-An FS::part_pkg_taxclass object represents a tax class. FS::part_pkg_taxclass
-inherits from FS::Record. The following fields are currently supported:
+An FS::part_pkg_taxclass object declares the existence of a taxable sales
+class. FS::part_pkg_taxclass inherits from FS::Record.
+
+FS::part_pkg_taxclass is not used in tax calculation. It is only used to
+list a set of valid tax class names for use in the user interface. When
+using internal taxes, the actual matching of tax definitions to package
+tax class is a string match between tax class names. This is arguably
+a bug.
+
+The following fields are currently supported:
=over 4
Tax class
+=item disabled
+
+Disabled flag, empty or 'Y'
+
=back
=head1 METHODS
=cut
-# the insert method can be inherited from FS::Record
+sub insert {
+ my $self = shift;
+
+ local $SIG{HUP} = 'IGNORE';
+ local $SIG{INT} = 'IGNORE';
+ local $SIG{QUIT} = 'IGNORE';
+ local $SIG{TERM} = 'IGNORE';
+ local $SIG{TSTP} = 'IGNORE';
+ local $SIG{PIPE} = 'IGNORE';
+
+ my $oldAutoCommit = $FS::UID::AutoCommit;
+ local $FS::UID::AutoCommit = 0;
+ my $dbh = dbh;
+
+ my $error = $self->SUPER::insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+
+ my $sth = dbh->prepare("
+ SELECT country, state, county FROM cust_main_county
+ WHERE taxclass IS NOT NULL AND taxclass != ''
+ GROUP BY country, state, county
+ ") or die dbh->errstr;
+ $sth->execute or die $sth->errstr;
+
+ while ( my $row = $sth->fetchrow_hashref ) {
+ #warn "inserting for $row";
+ my $cust_main_county = new FS::cust_main_county {
+ 'country' => $row->{country},
+ 'state' => $row->{state},
+ 'county' => $row->{county},
+ 'tax' => 0,
+ 'taxclass' => $self->taxclass,
+ #exempt_amount
+ #taxname
+ #setuptax
+ #recurtax
+ };
+ $error = $cust_main_county->insert;
+ #last if $error;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return $error;
+ }
+ }
+
+ $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+ '';
+}
=item delete
=cut
-# the replace method can be inherited from FS::Record
+sub replace {
+ my $new = shift;
+
+ my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
+ ? shift
+ : $new->replace_old;
+
+ return "Can't change tax class name (disable and create anew)"
+ if $old->taxclass ne $new->taxclass;
+
+ $new->SUPER::replace(@_);
+}
=item check
my $error =
$self->ut_numbern('taxclassnum')
|| $self->ut_text('taxclass')
+ || $self->ut_enum('disabled', [ '', 'Y' ] )
;
return $error if $error;