1 package FS::part_pkg_taxclass;
5 use Scalar::Util qw( blessed );
7 use FS::Record; # qw( qsearch qsearchs );
8 use FS::cust_main_county;
10 @ISA = qw(FS::Record);
14 FS::part_pkg_taxclass - Object methods for part_pkg_taxclass records
18 use FS::part_pkg_taxclass;
20 $record = new FS::part_pkg_taxclass \%hash;
21 $record = new FS::part_pkg_taxclass { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::part_pkg_taxclass object declares the existence of a taxable sales
34 class. FS::part_pkg_taxclass inherits from FS::Record.
36 FS::part_pkg_taxclass is not used in tax calculation. It is only used to
37 list a set of valid tax class names for use in the user interface. When
38 using internal taxes, the actual matching of tax definitions to package
39 tax class is a string match between tax class names. This is arguably
42 The following fields are currently supported:
56 Disabled flag, empty or 'Y'
66 Creates a new tax class. To add the tax class to the database, see L<"insert">.
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to. You can ask the object for a copy with the I<hash> method.
73 # the new method can be inherited from FS::Record, if a table method is defined
75 sub table { 'part_pkg_taxclass'; }
79 Adds this record to the database. If there is an error, returns the error,
80 otherwise returns false.
87 local $SIG{HUP} = 'IGNORE';
88 local $SIG{INT} = 'IGNORE';
89 local $SIG{QUIT} = 'IGNORE';
90 local $SIG{TERM} = 'IGNORE';
91 local $SIG{TSTP} = 'IGNORE';
92 local $SIG{PIPE} = 'IGNORE';
94 my $oldAutoCommit = $FS::UID::AutoCommit;
95 local $FS::UID::AutoCommit = 0;
98 my $error = $self->SUPER::insert;
100 $dbh->rollback if $oldAutoCommit;
104 my $sth = dbh->prepare("
105 SELECT country, state, county FROM cust_main_county
106 WHERE taxclass IS NOT NULL AND taxclass != ''
107 GROUP BY country, state, county
108 ") or die dbh->errstr;
109 $sth->execute or die $sth->errstr;
111 while ( my $row = $sth->fetchrow_hashref ) {
112 #warn "inserting for $row";
113 my $cust_main_county = new FS::cust_main_county {
114 'country' => $row->{country},
115 'state' => $row->{state},
116 'county' => $row->{county},
118 'taxclass' => $self->taxclass,
124 $error = $cust_main_county->insert;
127 $dbh->rollback if $oldAutoCommit;
132 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
138 Delete this record from the database.
142 # the delete method can be inherited from FS::Record
144 =item replace OLD_RECORD
146 Replaces the OLD_RECORD with this one in the database. If there is an error,
147 returns the error, otherwise returns false.
154 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
158 return "Can't change tax class name (disable and create anew)"
159 if $old->taxclass ne $new->taxclass;
161 $new->SUPER::replace(@_);
166 Checks all fields to make sure this is a valid tax class. If there is
167 an error, returns the error, otherwise returns false. Called by the insert
172 # the check method should currently be supplied - FS::Record contains some
173 # data checking routines
179 $self->ut_numbern('taxclassnum')
180 || $self->ut_text('taxclass')
181 || $self->ut_enum('disabled', [ '', 'Y' ] )
183 return $error if $error;
194 # Used by FS::Upgrade to migrate to a new database.
196 sub _upgrade_data { # class method
197 my ($class, %opts) = @_;
199 my $sth = dbh->prepare('
200 SELECT DISTINCT taxclass
201 FROM cust_main_county
202 LEFT JOIN part_pkg_taxclass USING ( taxclass )
203 WHERE taxclassnum IS NULL
204 AND taxclass IS NOT NULL
205 ') or die dbh->errstr;
206 $sth->execute or die $sth->errstr;
207 my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
208 my @taxclass = grep $_, keys %taxclass;
210 foreach my $taxclass ( @taxclass ) {
212 my $part_pkg_taxclass = new FS::part_pkg_taxclass ( {
213 'taxclass' => $taxclass,
215 my $error = $part_pkg_taxclass->insert;
216 die $error if $error;
224 Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text
225 taxclass instead of a key to this table.
229 L<FS::Record>, schema.html from the base documentation.