1 package FS::part_pkg_taxrate;
7 use FS::Record qw( qsearch qsearchs );
8 use FS::part_pkg_taxproduct;
9 use FS::Misc qw(csv_from_fixed);
11 @ISA = qw(FS::Record);
15 FS::part_pkg_taxrate - Object methods for part_pkg_taxrate records
19 use FS::part_pkg_taxrate;
21 $record = new FS::part_pkg_taxrate \%hash;
22 $record = new FS::part_pkg_taxrate { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::part_pkg_taxrate object maps packages onto tax rates.
35 FS::part_pkg_taxrate inherits from FS::Record. The following fields are
50 Tax vendor location code
54 Class of package for tax purposes, Index into FS::part_pkg_taxproduct
78 Class of tax index into FS::tax_taxclass and FS::tax_rate
80 =item taxclassnumtaxed
82 Class of tax taxed by this entry.
100 Creates a new customer (location), package, tax rate mapping. To add the
101 mapping to the database, see L<"insert">.
103 Note that this stores the hash reference, not a distinct copy of the hash it
104 points to. You can ask the object for a copy with the I<hash> method.
108 sub table { 'part_pkg_taxrate'; }
112 Adds this record to the database. If there is an error, returns the error,
113 otherwise returns false.
119 Delete this record from the database.
123 =item replace OLD_RECORD
125 Replaces the OLD_RECORD with this one in the database. If there is an error,
126 returns the error, otherwise returns false.
132 Checks all fields to make sure this is a valid tax rate mapping. If there is
133 an error, returns the error, otherwise returns false. Called by the insert
142 $self->ut_numbern('pkgtaxratenum')
143 || $self->ut_textn('data_vendor')
144 || $self->ut_textn('geocode')
146 ut_foreign_key('taxproductnum', 'part_pkg_taxproduct', 'taxproductnum')
147 || $self->ut_textn('city')
148 || $self->ut_textn('county')
149 || $self->ut_textn('state')
150 || $self->ut_textn('local')
151 || $self->ut_text('country')
152 || $self->ut_foreign_keyn('taxclassnumtaxed', 'tax_class', 'taxclassnum')
153 || $self->ut_foreign_key('taxclassnum', 'tax_class', 'taxclassnum')
154 || $self->ut_snumbern('effdate')
155 || $self->ut_enum('taxable', [ 'Y', '' ])
157 return $error if $error;
164 Loads part_pkg_taxrate records from an external CSV file. If there is
165 an error, returns the error, otherwise returns false.
170 my ($param, $job) = @_;
172 my $fh = $param->{filehandle};
173 my $format = $param->{'format'};
179 my @column_lengths = ();
180 my @column_callbacks = ();
181 if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
182 $format =~ s/-fixed//;
183 my $date_format = sub { my $r='';
184 /^(\d{4})(\d{2})(\d{2})$/ && ($r="$1/$2/$3");
187 $column_callbacks[16] = $date_format;
188 push @column_lengths, qw( 28 25 2 1 10 4 30 3 100 2 2 2 2 1 2 2 8 1 );
189 push @column_lengths, 1 if $format eq 'cch-update';
193 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
194 if ( $job || scalar(@column_callbacks) ) {
196 csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
197 return $error if $error;
200 if ( $format eq 'cch' || $format eq 'cch-update' ) {
201 @fields = qw( city county state local geocode group groupdesc item
202 itemdesc provider customer taxtypetaxed taxcattaxed
203 taxable taxtype taxcat effdate rectype );
204 push @fields, 'actionflag' if $format eq 'cch-update';
206 $imported++ if $format eq 'cch-update'; #empty file ok
211 unless ( $hash->{'rectype'} eq 'R' or $hash->{'rectype'} eq 'T' ) {
212 delete($hash->{$_}) for (keys %$hash);
216 $hash->{'data_vendor'} = 'cch';
218 my %providers = ( '00' => 'Regulated LEC',
219 '01' => 'Regulated IXC',
220 '02' => 'Unregulated LEC',
221 '03' => 'Unregulated IXC',
226 my %customers = ( '00' => 'Residential',
227 '01' => 'Commercial',
228 '02' => 'Industrial',
230 '10' => 'Senior Citizen',
234 join(':', map{ $hash->{$_} } qw(group item provider customer ) );
236 my %part_pkg_taxproduct = ( 'data_vendor' => 'cch',
237 'taxproduct' => $taxproduct,
240 my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct',
241 { %part_pkg_taxproduct }
244 unless ($part_pkg_taxproduct) {
245 return "Can't find part_pkg_taxproduct for txmatrix deletion: ".
246 join(" ", map { "$_ => ". $hash->{$_} } @fields)
247 if ($hash->{'actionfield'} && $hash->{'actionflag'} eq 'D');
249 $part_pkg_taxproduct{'description'} =
250 join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)),
251 $providers{$hash->{'provider'}} || '',
252 $customers{$hash->{'customer'}} || '',
254 $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct;
255 my $error = $part_pkg_taxproduct->insert;
256 return "Error inserting tax product (part_pkg_taxproduct): $error"
260 $hash->{'taxproductnum'} = $part_pkg_taxproduct->taxproductnum;
263 for qw(group groupdesc item itemdesc provider customer rectype );
265 my %map = ( 'taxclassnum' => [ 'taxtype', 'taxcat' ],
266 'taxclassnumtaxed' => [ 'taxtypetaxed', 'taxcattaxed' ],
269 for my $item (keys %map) {
270 my $class = join(':', map($hash->{$_}, @{$map{$item}}));
272 qsearchs( 'tax_class',
273 { data_vendor => 'cch',
274 'taxclass' => $class,
277 $hash->{$item} = $tax_class->taxclassnum
280 return "Can't find tax class for txmatrix deletion: ".
281 join(" ", map { "$_ => ". $hash->{$_} } @fields)
282 if ( $hash->{'actionflag'} && $hash->{'actionflag'} eq 'D' &&
283 !$tax_class && $class ne ':'
286 delete($hash->{$_}) foreach @{$map{$item}};
289 $hash->{'effdate'} = str2time($hash->{'effdate'});
290 $hash->{'country'} = 'US'; # CA is available
292 delete($hash->{'taxable'}) if ($hash->{'taxable'} eq 'N');
294 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
295 delete($hash->{actionflag});
297 my $part_pkg_taxrate = qsearchs('part_pkg_taxrate', $hash);
298 unless ( $part_pkg_taxrate ) {
299 if ( $hash->{taxproductnum} ) {
301 qsearchs( 'part_pkg_taxproduct',
302 { 'taxproductnum' => $hash->{taxproductnum} }
304 $hash->{taxproductnum} .= ' ( '. $taxproduct->taxproduct. ' )'
307 return "Can't find part_pkg_taxrate to delete: ".
308 join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
311 my $error = $part_pkg_taxrate->delete;
312 return $error if $error;
314 delete($hash->{$_}) foreach (keys %$hash);
317 delete($hash->{actionflag});
322 } elsif ( $format eq 'extended' ) {
323 die "unimplemented\n";
327 die "unknown format $format";
330 eval "use Text::CSV_XS;";
333 my $csv = new Text::CSV_XS;
335 local $SIG{HUP} = 'IGNORE';
336 local $SIG{INT} = 'IGNORE';
337 local $SIG{QUIT} = 'IGNORE';
338 local $SIG{TERM} = 'IGNORE';
339 local $SIG{TSTP} = 'IGNORE';
340 local $SIG{PIPE} = 'IGNORE';
342 my $oldAutoCommit = $FS::UID::AutoCommit;
343 local $FS::UID::AutoCommit = 0;
346 while ( defined($line=<$fh>) ) {
347 $csv->parse($line) or do {
348 $dbh->rollback if $oldAutoCommit;
349 return "can't parse: ". $csv->error_input();
353 if ( $job ) { # progress bar
354 if ( time - $min_sec > $last ) {
355 my $error = $job->update_statustext(
356 int( 100 * $imported / $count )
358 die $error if $error;
363 my @columns = $csv->fields();
365 my %part_pkg_taxrate = ( 'data_vendor' => $format );
366 foreach my $field ( @fields ) {
367 $part_pkg_taxrate{$field} = shift @columns;
369 if ( scalar( @columns ) ) {
370 $dbh->rollback if $oldAutoCommit;
371 return "Unexpected trailing columns in line (wrong format?): $line";
374 my $error = &{$hook}(\%part_pkg_taxrate);
376 $dbh->rollback if $oldAutoCommit;
379 next unless scalar(keys %part_pkg_taxrate);
382 my $part_pkg_taxrate = new FS::part_pkg_taxrate( \%part_pkg_taxrate );
383 $error = $part_pkg_taxrate->insert;
386 $dbh->rollback if $oldAutoCommit;
387 return "can't insert part_pkg_taxrate for $line: $error";
393 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
395 return "Empty file!" unless ( $imported || $format eq 'cch-update' );
407 L<FS::Record>, schema.html from the base documentation.