6 use FS::Record qw( qsearch qsearchs );
7 use FS::Misc qw( csv_from_fixed );
13 FS::tax_class - Object methods for tax_class records
19 $record = new FS::tax_class \%hash;
20 $record = new FS::tax_class { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::tax_class object represents a tax class. FS::tax_class
33 inherits from FS::Record. The following fields are currently supported:
43 Vendor of the tax data
51 Human readable description of the tax class
61 Creates a new tax class. To add the tax class to the database, see L<"insert">.
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to. You can ask the object for a copy with the I<hash> method.
68 sub table { 'tax_class'; }
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
79 Delete this record from the database.
86 return "Can't delete a tax class which has tax rates!"
87 if qsearch( 'tax_rate', { 'taxclassnum' => $self->taxclassnum } );
89 return "Can't delete a tax class which has package tax rates!"
90 if qsearch( 'part_pkg_taxrate', { 'taxclassnum' => $self->taxclassnum } );
92 return "Can't delete a tax class which has package tax rates!"
93 if qsearch( 'part_pkg_taxrate', { 'taxclassnumtaxed' => $self->taxclassnum } );
95 return "Can't delete a tax class which has package tax overrides!"
96 if qsearch( 'part_pkg_taxoverride', { 'taxclassnum' => $self->taxclassnum } );
98 $self->SUPER::delete(@_);
102 =item replace OLD_RECORD
104 Replaces the OLD_RECORD with this one in the database. If there is an error,
105 returns the error, otherwise returns false.
111 Checks all fields to make sure this is a valid tax class. If there is
112 an error, returns the error, otherwise returns false. Called by the insert
121 $self->ut_numbern('taxclassnum')
122 || $self->ut_text('taxclass')
123 || $self->ut_textn('data_vendor')
124 || $self->ut_textn('description')
126 return $error if $error;
133 Loads part_pkg_taxrate records from an external CSV file. If there is
134 an error, returns the error, otherwise returns false.
139 my ($param, $job) = @_;
141 my $fh = $param->{filehandle};
142 my $format = $param->{'format'};
151 my @column_lengths = ();
152 my @column_callbacks = ();
153 if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
154 $format =~ s/-fixed//;
155 push @column_lengths, qw( 8 10 3 2 2 10 100 );
156 push @column_lengths, 1 if $format eq 'cch-update';
160 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
161 if ( $job || scalar(@column_lengths) ) {
162 my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
163 return $error if $error;
166 if ( $format eq 'cch' || $format eq 'cch-update' ) {
167 @fields = qw( table name pos length number value description );
168 push @fields, 'actionflag' if $format eq 'cch-update';
174 if ($hash->{'table'} eq 'DETAIL') {
175 push @{$data->{'taxcat'}}, [ $hash->{'value'}, $hash->{'description'} ]
176 if ($hash->{'name'} eq 'TAXCAT' &&
177 (!exists($hash->{actionflag}) || $hash->{actionflag} eq 'I') );
179 push @{$data->{'taxtype'}}, [ $hash->{'value'}, $hash->{'description'} ]
180 if ($hash->{'name'} eq 'TAXTYPE' &&
181 (!exists($hash->{actionflag}) || $hash->{actionflag} eq 'I') );
183 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
184 my $name = $hash->{'name'};
185 my $value = $hash->{'value'};
186 return "Bad value for $name: $value"
187 unless $value =~ /^\d+$/;
189 if ($name eq 'TAXCAT' || $name eq 'TAXTYPE') {
190 my @tax_class = qsearch( 'tax_class',
191 { 'data_vendor' => 'cch' },
193 "AND taxclass LIKE '".
194 ($name eq 'TAXTYPE' ? $value : '%').":".
195 ($name eq 'TAXCAT' ? $value : '%')."'",
197 foreach (@tax_class) {
198 warn "deleting ". $_->taxclass. ' '. $_->description. "\n";
199 my $error = $_->delete;
200 return $error if $error;
208 for qw( data_vendor table name pos length number value description );
209 delete($hash->{actionflag}) if exists($hash->{actionflag});
217 my $sql = "SELECT DISTINCT ".
218 "substring(taxclass from 1 for position(':' in taxclass)-1),".
219 "substring(description from 1 for position(':' in description)-1) ".
220 "FROM tax_class WHERE data_vendor='cch'";
222 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
223 $sth->execute or die $sth->errstr;
224 my @old_types = @{$sth->fetchall_arrayref};
226 $sql = "SELECT DISTINCT ".
227 "substring(taxclass from position(':' in taxclass)+1),".
228 "substring(description from position(':' in description)+1) ".
229 "FROM tax_class WHERE data_vendor='cch'";
231 $sth = $dbh->prepare($sql) or die $dbh->errstr;
232 $sth->execute or die $sth->errstr;
233 my @old_cats = @{$sth->fetchall_arrayref};
235 my $catcount = exists($data->{'taxcat'}) ? scalar(@{$data->{'taxcat'}})
237 my $typecount = exists($data->{'taxtype'}) ? scalar(@{$data->{'taxtype'}})
240 my $count = scalar(@old_types) * $catcount
241 + $typecount * (scalar(@old_cats) + $catcount);
243 $imported = 1 if $format eq 'cch-update'; #empty file ok
245 foreach my $type (@old_types) {
246 foreach my $cat (@{$data->{'taxcat'}}) {
248 if ( $job ) { # progress bar
249 if ( time - $min_sec > $last ) {
250 my $error = $job->update_statustext(
251 int( 100 * $imported / $count ). ",Importing tax classes"
253 die $error if $error;
258 my %hash = ( 'data_vendor' => 'cch',
259 'taxclass' => $type->[0].':'.$cat->[0],
260 'description' => $type->[1].':'.$cat->[1],
262 unless ( qsearchs('tax_class', \%hash) ) {
263 my $tax_class = new FS::tax_class \%hash;
264 my $error = $tax_class->insert;
266 return "can't insert tax_class for ".
267 " old TAXTYPE ". $type->[0].':'.$type->[1].
268 " and new TAXCAT ". $cat->[0].':'. $cat->[1].
278 foreach my $type (@{$data->{'taxtype'}}) {
279 foreach my $cat (@old_cats, @{$data->{'taxcat'}}) {
281 if ( $job ) { # progress bar
282 if ( time - $min_sec > $last ) {
283 my $error = $job->update_statustext(
284 int( 100 * $imported / $count ). ",Importing tax classes"
286 die $error if $error;
292 new FS::tax_class( { 'data_vendor' => 'cch',
293 'taxclass' => $type->[0].':'.$cat->[0],
294 'description' => $type->[1].':'.$cat->[1],
296 my $error = $tax_class->insert;
297 return "can't insert tax_class for new TAXTYPE $type and TAXCAT $cat: $error" if $error;
305 } elsif ( $format eq 'extended' ) {
306 die "unimplemented\n";
310 die "unknown format $format";
313 eval "use Text::CSV_XS;";
316 my $csv = new Text::CSV_XS;
318 local $SIG{HUP} = 'IGNORE';
319 local $SIG{INT} = 'IGNORE';
320 local $SIG{QUIT} = 'IGNORE';
321 local $SIG{TERM} = 'IGNORE';
322 local $SIG{TSTP} = 'IGNORE';
323 local $SIG{PIPE} = 'IGNORE';
325 my $oldAutoCommit = $FS::UID::AutoCommit;
326 local $FS::UID::AutoCommit = 0;
328 while ( defined($line=<$fh>) ) {
330 if ( $job ) { # progress bar
331 if ( time - $min_sec > $last ) {
332 my $error = $job->update_statustext(
333 int( 100 * $imported / $count ). ",Importing tax classes"
335 die $error if $error;
340 $csv->parse($line) or do {
341 $dbh->rollback if $oldAutoCommit;
342 return "can't parse: ". $csv->error_input();
345 my @columns = $csv->fields();
347 my %tax_class = ( 'data_vendor' => $format );
348 foreach my $field ( @fields ) {
349 $tax_class{$field} = shift @columns;
351 if ( scalar( @columns ) ) {
352 $dbh->rollback if $oldAutoCommit;
353 return "Unexpected trailing columns in line (wrong format?) importing tax_class: $line";
356 my $error = &{$hook}(\%tax_class);
358 $dbh->rollback if $oldAutoCommit;
362 next unless scalar(keys %tax_class);
364 my $tax_class = new FS::tax_class( \%tax_class );
365 $error = $tax_class->insert;
367 $dbh->rollback if $oldAutoCommit;
368 return "can't insert tax_class for $line: $error";
374 my $error = &{$endhook}();
376 $dbh->rollback if $oldAutoCommit;
377 return "can't run end hook: $error";
380 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
382 return "Empty File!" unless ($imported || $format eq 'cch-update');
392 batch_import does not handle mixed I and D records in the same file for
397 L<FS::Record>, schema.html from the base documentation.