6 use FS::Record qw( qsearch qsearchs );
12 FS::tax_class - Object methods for tax_class records
18 $record = new FS::tax_class \%hash;
19 $record = new FS::tax_class { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::tax_class object represents a tax class. FS::tax_class
32 inherits from FS::Record. The following fields are currently supported:
42 Vendor of the tax data
50 Human readable description of the tax class
60 Creates a new tax class. To add the tax class to the database, see L<"insert">.
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to. You can ask the object for a copy with the I<hash> method.
67 sub table { 'tax_class'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
78 Delete this record from the database.
82 =item replace OLD_RECORD
84 Replaces the OLD_RECORD with this one in the database. If there is an error,
85 returns the error, otherwise returns false.
91 Checks all fields to make sure this is a valid tax class. If there is
92 an error, returns the error, otherwise returns false. Called by the insert
101 $self->ut_numbern('taxclassnum')
102 || $self->ut_text('taxclass')
103 || $self->ut_textn('data_vendor')
104 || $self->ut_textn('description')
106 return $error if $error;
113 Loads part_pkg_taxrate records from an external CSV file. If there is
114 an error, returns the error, otherwise returns false.
121 my $fh = $param->{filehandle};
122 my $format = $param->{'format'};
130 if ( $format eq 'cch' ) {
131 @fields = qw( table name pos number length value description );
136 if ($hash->{'table'} eq 'DETAIL') {
137 push @{$data->{'taxcat'}}, [ $hash->{'value'}, $hash->{'description'} ]
138 if $hash->{'name'} eq 'TAXCAT';
140 push @{$data->{'taxtype'}}, [ $hash->{'value'}, $hash->{'description'} ]
141 if $hash->{'name'} eq 'TAXTYPE';
145 for qw( data_vendor table name pos number length value description );
152 foreach my $type (@{$data->{'taxtype'}}) {
153 foreach my $cat (@{$data->{'taxcat'}}) {
155 new FS::tax_class( { 'data_vendor' => 'cch',
156 'taxclass' => $type->[0].':'.$cat->[0],
157 'description' => $type->[1].':'.$cat->[1],
159 my $error = $tax_class->insert;
160 return $error if $error;
167 } elsif ( $format eq 'extended' ) {
168 die "unimplemented\n";
172 die "unknown format $format";
175 eval "use Text::CSV_XS;";
178 my $csv = new Text::CSV_XS;
180 local $SIG{HUP} = 'IGNORE';
181 local $SIG{INT} = 'IGNORE';
182 local $SIG{QUIT} = 'IGNORE';
183 local $SIG{TERM} = 'IGNORE';
184 local $SIG{TSTP} = 'IGNORE';
185 local $SIG{PIPE} = 'IGNORE';
187 my $oldAutoCommit = $FS::UID::AutoCommit;
188 local $FS::UID::AutoCommit = 0;
192 while ( defined($line=<$fh>) ) {
193 $csv->parse($line) or do {
194 $dbh->rollback if $oldAutoCommit;
195 return "can't parse: ". $csv->error_input();
198 my @columns = $csv->fields();
200 my %tax_class = ( 'data_vendor' => $format );
201 foreach my $field ( @fields ) {
202 $tax_class{$field} = shift @columns;
204 my $error = &{$hook}(\%tax_class);
206 $dbh->rollback if $oldAutoCommit;
209 next unless scalar(keys %tax_class);
211 my $tax_class = new FS::tax_class( \%tax_class );
212 $error = $tax_class->insert;
215 $dbh->rollback if $oldAutoCommit;
216 return "can't insert tax_class for $line: $error";
222 my $error = &{$endhook}();
224 $dbh->rollback if $oldAutoCommit;
225 return "can't insert tax_class for $line: $error";
228 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
230 return "Empty file!" unless $imported;
243 L<FS::Record>, schema.html from the base documentation.