1 package FS::part_pkg_taxclass;
6 use FS::Record; # qw( qsearch qsearchs );
7 use FS::cust_main_county;
13 FS::part_pkg_taxclass - Object methods for part_pkg_taxclass records
17 use FS::part_pkg_taxclass;
19 $record = new FS::part_pkg_taxclass \%hash;
20 $record = new FS::part_pkg_taxclass { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::part_pkg_taxclass object represents a tax class. FS::part_pkg_taxclass
33 inherits from FS::Record. The following fields are currently supported:
47 Disabled flag, empty or 'Y'
57 Creates a new tax class. To add the tax class to the database, see L<"insert">.
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to. You can ask the object for a copy with the I<hash> method.
64 # the new method can be inherited from FS::Record, if a table method is defined
66 sub table { 'part_pkg_taxclass'; }
70 Adds this record to the database. If there is an error, returns the error,
71 otherwise returns false.
78 local $SIG{HUP} = 'IGNORE';
79 local $SIG{INT} = 'IGNORE';
80 local $SIG{QUIT} = 'IGNORE';
81 local $SIG{TERM} = 'IGNORE';
82 local $SIG{TSTP} = 'IGNORE';
83 local $SIG{PIPE} = 'IGNORE';
85 my $oldAutoCommit = $FS::UID::AutoCommit;
86 local $FS::UID::AutoCommit = 0;
89 my $error = $self->SUPER::insert;
91 $dbh->rollback if $oldAutoCommit;
95 my $sth = dbh->prepare("
96 SELECT country, state, county FROM cust_main_county
97 WHERE taxclass IS NOT NULL AND taxclass != ''
98 GROUP BY country, state, county
99 ") or die dbh->errstr;
100 $sth->execute or die $sth->errstr;
102 while ( my $row = $sth->fetchrow_hashref ) {
103 #warn "inserting for $row";
104 my $cust_main_county = new FS::cust_main_county {
105 'country' => $row->{country},
106 'state' => $row->{state},
107 'county' => $row->{county},
109 'taxclass' => $self->taxclass,
115 $error = $cust_main_county->insert;
118 $dbh->rollback if $oldAutoCommit;
123 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
129 Delete this record from the database.
133 # the delete method can be inherited from FS::Record
135 =item replace OLD_RECORD
137 Replaces the OLD_RECORD with this one in the database. If there is an error,
138 returns the error, otherwise returns false.
145 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
149 return "Can't change tax class name (disable and create anew)"
150 if $old->taxclass ne $new->taxclass;
152 $new->SUPER::replace(@_);
157 Checks all fields to make sure this is a valid tax class. If there is
158 an error, returns the error, otherwise returns false. Called by the insert
163 # the check method should currently be supplied - FS::Record contains some
164 # data checking routines
170 $self->ut_numbern('taxclassnum')
171 || $self->ut_text('taxclass')
172 || $self->ut_enum('disabled', [ '', 'Y' ] )
174 return $error if $error;
185 # Used by FS::Upgrade to migrate to a new database.
187 sub _upgrade_data { # class method
188 my ($class, %opts) = @_;
190 my $sth = dbh->prepare('
191 SELECT DISTINCT taxclass
192 FROM cust_main_county
193 LEFT JOIN part_pkg_taxclass USING ( taxclass )
194 WHERE taxclassnum IS NULL
195 AND taxclass IS NOT NULL
196 ') or die dbh->errstr;
197 $sth->execute or die $sth->errstr;
198 my %taxclass = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
199 my @taxclass = grep $_, keys %taxclass;
201 foreach my $taxclass ( @taxclass ) {
203 my $part_pkg_taxclass = new FS::part_pkg_taxclass ( {
204 'taxclass' => $taxclass,
206 my $error = $part_pkg_taxclass->insert;
207 die $error if $error;
215 Other tables (cust_main_county, part_pkg, agent_payment_gateway) have a text
216 taxclass instead of a key to this table.
220 L<FS::Record>, schema.html from the base documentation.