1 package FS::part_pkg_taxrate;
7 use DateTime::Format::Strptime;
8 use FS::Record qw( qsearch qsearchs dbh );
9 use FS::part_pkg_taxproduct;
10 use FS::Misc qw(csv_from_fixed);
12 @ISA = qw(FS::Record);
16 FS::part_pkg_taxrate - Object methods for part_pkg_taxrate records
20 use FS::part_pkg_taxrate;
22 $record = new FS::part_pkg_taxrate \%hash;
23 $record = new FS::part_pkg_taxrate { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::part_pkg_taxrate object maps packages onto tax rates.
36 FS::part_pkg_taxrate inherits from FS::Record. The following fields are
51 Tax vendor location code
55 Class of package for tax purposes, Index into FS::part_pkg_taxproduct
79 Class of tax index into FS::tax_taxclass and FS::tax_rate
81 =item taxclassnumtaxed
83 Class of tax taxed by this entry.
101 Creates a new customer (location), package, tax rate mapping. To add the
102 mapping to the database, see L<"insert">.
104 Note that this stores the hash reference, not a distinct copy of the hash it
105 points to. You can ask the object for a copy with the I<hash> method.
109 sub table { 'part_pkg_taxrate'; }
113 Adds this record to the database. If there is an error, returns the error,
114 otherwise returns false.
120 Delete this record from the database.
124 =item replace OLD_RECORD
126 Replaces the OLD_RECORD with this one in the database. If there is an error,
127 returns the error, otherwise returns false.
133 Checks all fields to make sure this is a valid tax rate mapping. If there is
134 an error, returns the error, otherwise returns false. Called by the insert
143 $self->ut_numbern('pkgtaxratenum')
144 || $self->ut_textn('data_vendor')
145 || $self->ut_textn('geocode')
147 ut_foreign_key('taxproductnum', 'part_pkg_taxproduct', 'taxproductnum')
148 || $self->ut_textn('city')
149 || $self->ut_textn('county')
150 || $self->ut_textn('state')
151 || $self->ut_textn('local')
152 || $self->ut_text('country')
153 || $self->ut_foreign_keyn('taxclassnumtaxed', 'tax_class', 'taxclassnum')
154 || $self->ut_foreign_key('taxclassnum', 'tax_class', 'taxclassnum')
155 || $self->ut_snumbern('effdate')
156 || $self->ut_enum('taxable', [ 'Y', '' ])
158 return $error if $error;
165 Loads part_pkg_taxrate records from an external CSV file. If there is
166 an error, returns the error, otherwise returns false.
171 my ($param, $job) = @_;
173 my $fh = $param->{filehandle};
174 my $format = $param->{'format'};
180 my @column_lengths = ();
181 my @column_callbacks = ();
182 if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
183 $format =~ s/-fixed//;
184 my $date_format = sub { my $r='';
185 /^(\d{4})(\d{2})(\d{2})$/ && ($r="$3/$2/$1");
188 $column_callbacks[16] = $date_format;
189 push @column_lengths, qw( 28 25 2 1 10 4 30 3 100 2 2 2 2 1 2 2 8 1 );
190 push @column_lengths, 1 if $format eq 'cch-update';
194 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
195 if ( $job || scalar(@column_callbacks) ) {
197 csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
198 return $error if $error;
201 if ( $format eq 'cch' || $format eq 'cch-update' ) {
202 @fields = qw( city county state local geocode group groupdesc item
203 itemdesc provider customer taxtypetaxed taxcattaxed
204 taxable taxtype taxcat effdate rectype );
205 push @fields, 'actionflag' if $format eq 'cch-update';
207 $imported++ if $format eq 'cch-update'; #empty file ok
212 unless ( $hash->{'rectype'} eq 'R' or $hash->{'rectype'} eq 'T' ) {
213 delete($hash->{$_}) for (keys %$hash);
217 $hash->{'data_vendor'} = 'cch';
219 my %providers = ( '00' => 'Regulated LEC',
220 '01' => 'Regulated IXC',
221 '02' => 'Unregulated LEC',
222 '03' => 'Unregulated IXC',
227 my %customers = ( '00' => 'Residential',
228 '01' => 'Commercial',
229 '02' => 'Industrial',
231 '10' => 'Senior Citizen',
235 join(':', map{ $hash->{$_} } qw(group item provider customer ) );
237 my %part_pkg_taxproduct = ( 'data_vendor' => 'cch',
238 'taxproduct' => $taxproduct,
241 my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct',
242 { %part_pkg_taxproduct }
245 unless ($part_pkg_taxproduct) {
246 return "Can't find part_pkg_taxproduct for txmatrix deletion: ".
247 join(" ", map { "$_ => ". $hash->{$_} } @fields)
248 if ($hash->{'actionfield'} && $hash->{'actionflag'} eq 'D');
250 $part_pkg_taxproduct{'description'} =
251 join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)),
252 $providers{$hash->{'provider'}} || '',
253 $customers{$hash->{'customer'}} || '',
255 $part_pkg_taxproduct = new FS::part_pkg_taxproduct \%part_pkg_taxproduct;
256 my $error = $part_pkg_taxproduct->insert;
257 return "Error inserting tax product (part_pkg_taxproduct): $error"
261 $hash->{'taxproductnum'} = $part_pkg_taxproduct->taxproductnum;
264 for qw(group groupdesc item itemdesc provider customer rectype );
266 my %map = ( 'taxclassnum' => [ 'taxtype', 'taxcat' ],
267 'taxclassnumtaxed' => [ 'taxtypetaxed', 'taxcattaxed' ],
270 for my $item (keys %map) {
271 my $class = join(':', map($hash->{$_}, @{$map{$item}}));
273 qsearchs( 'tax_class',
274 { data_vendor => 'cch',
275 'taxclass' => $class,
278 $hash->{$item} = $tax_class->taxclassnum
281 return "Can't find tax class for txmatrix deletion: ".
282 join(" ", map { "$_ => ". $hash->{$_} } @fields)
283 if ( $hash->{'actionflag'} && $hash->{'actionflag'} eq 'D' &&
284 !$tax_class && $class ne ':'
287 delete($hash->{$_}) foreach @{$map{$item}};
290 my $parser = new DateTime::Format::Strptime( pattern => "%m/%d/%Y",
291 time_zone => 'floating',
293 my $dt = $parser->parse_datetime( $hash->{'effdate'} );
294 return "Can't parse effdate ". $hash->{'effdate'}. ': '. $parser->errstr
296 $hash->{'effdate'} = $dt->epoch;
298 $hash->{'country'} = 'US'; # CA is available
300 $hash->{'taxable'} = '' if ($hash->{'taxable'} eq 'N');
302 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
303 delete($hash->{actionflag});
305 foreach my $intfield (qw( taxproductnum taxclassnum effdate )) {
306 if ( $hash->{$intfield} eq '' ) {
307 return "$intfield is empty in search! -- ".
308 join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
312 my @part_pkg_taxrate = qsearch('part_pkg_taxrate', $hash);
313 unless ( scalar(@part_pkg_taxrate) || $param->{'delete_only'} ) {
314 if ( $hash->{taxproductnum} ) {
316 qsearchs( 'part_pkg_taxproduct',
317 { 'taxproductnum' => $hash->{taxproductnum} }
319 $hash->{taxproductnum} .= ' ( '. $taxproduct->taxproduct. ' )'
322 return "Can't find part_pkg_taxrate to delete: ".
323 join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
326 foreach my $part_pkg_taxrate (@part_pkg_taxrate) {
327 my $error = $part_pkg_taxrate->delete;
328 return $error if $error;
331 delete($hash->{$_}) foreach (keys %$hash);
334 delete($hash->{actionflag});
339 } elsif ( $format eq 'extended' ) {
340 die "unimplemented\n";
344 die "unknown format $format";
347 eval "use Text::CSV_XS;";
350 my $csv = new Text::CSV_XS;
352 local $SIG{HUP} = 'IGNORE';
353 local $SIG{INT} = 'IGNORE';
354 local $SIG{QUIT} = 'IGNORE';
355 local $SIG{TERM} = 'IGNORE';
356 local $SIG{TSTP} = 'IGNORE';
357 local $SIG{PIPE} = 'IGNORE';
359 my $oldAutoCommit = $FS::UID::AutoCommit;
360 local $FS::UID::AutoCommit = 0;
363 while ( defined($line=<$fh>) ) {
364 $csv->parse($line) or do {
365 $dbh->rollback if $oldAutoCommit;
366 return "can't parse: ". $csv->error_input();
370 if ( $job ) { # progress bar
371 if ( time - $min_sec > $last ) {
372 my $error = $job->update_statustext(
373 int( 100 * $imported / $count ). ",Importing tax matrix"
375 die $error if $error;
380 my @columns = $csv->fields();
382 my %part_pkg_taxrate = ( 'data_vendor' => $format );
383 foreach my $field ( @fields ) {
384 $part_pkg_taxrate{$field} = shift @columns;
386 if ( scalar( @columns ) ) {
387 $dbh->rollback if $oldAutoCommit;
388 return "Unexpected trailing columns in line (wrong format?) importing part_pkg_taxrate: $line";
391 my $error = &{$hook}(\%part_pkg_taxrate);
393 $dbh->rollback if $oldAutoCommit;
396 next unless scalar(keys %part_pkg_taxrate);
399 my $part_pkg_taxrate = new FS::part_pkg_taxrate( \%part_pkg_taxrate );
400 $error = $part_pkg_taxrate->insert;
403 $dbh->rollback if $oldAutoCommit;
404 return "can't insert part_pkg_taxrate for $line: $error";
410 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
412 return "Empty file!" unless ( $imported || $format eq 'cch-update' );
424 L<FS::Record>, schema.html from the base documentation.