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 represents a tax class. FS::part_pkg_taxclass
34 inherits from FS::Record. The following fields are currently supported:
48 Disabled flag, empty or 'Y'
58 Creates a new tax class. To add the tax class to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 # the new method can be inherited from FS::Record, if a table method is defined
67 sub table { 'part_pkg_taxclass'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
79 local $SIG{HUP} = 'IGNORE';
80 local $SIG{INT} = 'IGNORE';
81 local $SIG{QUIT} = 'IGNORE';
82 local $SIG{TERM} = 'IGNORE';
83 local $SIG{TSTP} = 'IGNORE';
84 local $SIG{PIPE} = 'IGNORE';
86 my $oldAutoCommit = $FS::UID::AutoCommit;
87 local $FS::UID::AutoCommit = 0;
90 my $error = $self->SUPER::insert;
92 $dbh->rollback if $oldAutoCommit;
96 my $sth = dbh->prepare("
97 SELECT country, state, county FROM cust_main_county
98 WHERE taxclass IS NOT NULL AND taxclass != ''
99 GROUP BY country, state, county
100 ") or die dbh->errstr;
101 $sth->execute or die $sth->errstr;
103 while ( my $row = $sth->fetchrow_hashref ) {
104 #warn "inserting for $row";
105 my $cust_main_county = new FS::cust_main_county {
106 'country' => $row->{country},
107 'state' => $row->{state},
108 'county' => $row->{county},
110 'taxclass' => $self->taxclass,
116 $error = $cust_main_county->insert;
119 $dbh->rollback if $oldAutoCommit;
124 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
130 Delete this record from the database.
134 # the delete method can be inherited from FS::Record
136 =item replace OLD_RECORD
138 Replaces the OLD_RECORD with this one in the database. If there is an error,
139 returns the error, otherwise returns false.
146 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
150 return "Can't change tax class name (disable and create anew)"
151 if $old->taxclass ne $new->taxclass;
153 $new->SUPER::replace(@_);
158 Checks all fields to make sure this is a valid tax class. If there is
159 an error, returns the error, otherwise returns false. Called by the insert
164 # the check method should currently be supplied - FS::Record contains some
165 # data checking routines
171 $self->ut_numbern('taxclassnum')
172 || $self->ut_text('taxclass')
173 || $self->ut_enum('disabled', [ '', 'Y' ] )
175 return $error if $error;
186 # Used by FS::Upgrade to migrate to a new database.
188 sub _upgrade_data { # class method
189 my ($class, %opts) = @_;
191 my $sth = dbh->prepare('
192 SELECT DISTINCT taxclass
193 FROM cust_main_county
194 LEFT JOIN part_pkg_taxclass USING ( taxclass )
195 WHERE taxclassnum IS NULL
196 AND taxclass IS NOT NULL
197 ') or die dbh->errstr;
198 $sth->execute or die $sth->errstr;
199 my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
200 my @taxclass = grep $_, keys %taxclass;
202 foreach my $taxclass ( @taxclass ) {
204 my $part_pkg_taxclass = new FS::part_pkg_taxclass ( {
205 'taxclass' => $taxclass,
207 my $error = $part_pkg_taxclass->insert;
208 die $error if $error;
216 Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text
217 taxclass instead of a key to this table.
221 L<FS::Record>, schema.html from the base documentation.