fix cch update removal of PLUS4/ZIP and TXMATRIX, RT#21687
[freeside.git] / FS / FS / part_pkg_taxrate.pm
1 package FS::part_pkg_taxrate;
2
3 use strict;
4 use vars qw( @ISA );
5 use Date::Parse;
6 use DateTime;
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);
11
12 @ISA = qw(FS::Record);
13
14 =head1 NAME
15
16 FS::part_pkg_taxrate - Object methods for part_pkg_taxrate records
17
18 =head1 SYNOPSIS
19
20   use FS::part_pkg_taxrate;
21
22   $record = new FS::part_pkg_taxrate \%hash;
23   $record = new FS::part_pkg_taxrate { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
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
37 currently supported:
38
39 =over 4
40
41 =item pkgtaxratenum
42
43 Primary key
44
45 =item data_vendor
46
47 Tax data vendor
48
49 =item geocode
50
51 Tax vendor location code
52
53 =item taxproductnum
54
55 Class of package for tax purposes, Index into FS::part_pkg_taxproduct
56
57 =item city
58
59 city
60
61 =item county
62
63 county
64
65 =item state
66
67 state
68
69 =item local
70
71 local
72
73 =item country
74
75 country
76
77 =item taxclassnum
78
79 Class of tax index into FS::tax_taxclass and FS::tax_rate
80
81 =item taxclassnumtaxed
82
83 Class of tax taxed by this entry.
84
85 =item taxable
86
87 taxable
88
89 =item effdate
90
91 effdate
92
93 =back
94
95 =head1 METHODS
96
97 =over 4
98
99 =item new HASHREF
100
101 Creates a new customer (location), package, tax rate mapping.  To add the
102 mapping to the database, see L<"insert">.
103
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.
106
107 =cut
108
109 sub table { 'part_pkg_taxrate'; }
110
111 =item insert
112
113 Adds this record to the database.  If there is an error, returns the error,
114 otherwise returns false.
115
116 =cut
117
118 =item delete
119
120 Delete this record from the database.
121
122 =cut
123
124 =item replace OLD_RECORD
125
126 Replaces the OLD_RECORD with this one in the database.  If there is an error,
127 returns the error, otherwise returns false.
128
129 =cut
130
131 =item check
132
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
135 and replace methods.
136
137 =cut
138
139 sub check {
140   my $self = shift;
141
142   my $error = 
143     $self->ut_numbern('pkgtaxratenum')
144     || $self->ut_textn('data_vendor')
145     || $self->ut_textn('geocode')
146     || $self->
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', '' ])
157   ;
158   return $error if $error;
159
160   $self->SUPER::check;
161 }
162
163 =item batch_import
164
165 Loads part_pkg_taxrate records from an external CSV file.  If there is
166 an error, returns the error, otherwise returns false. 
167
168 =cut 
169
170 sub batch_import {
171   my ($param, $job) = @_;
172
173   my $fh = $param->{filehandle};
174   my $format = $param->{'format'};
175
176   my $imported = 0;
177   my @fields;
178   my $hook;
179
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");
186                             $r;
187                           };
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';
191   }
192
193   my $line;
194   my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
195   if ( $job || scalar(@column_callbacks) ) {
196     my $error =
197       csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
198     return $error if $error;
199   }
200
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';
206
207     $imported++ if $format eq 'cch-update';  #empty file ok
208
209     $hook = sub { 
210       my $hash = shift;
211
212       unless ( $hash->{'rectype'} eq 'R' or $hash->{'rectype'} eq 'T' ) {
213         delete($hash->{$_}) for (keys %$hash);
214         return;
215       }
216
217       $hash->{'data_vendor'} = 'cch';
218
219       my %providers = ( '00' => 'Regulated LEC',
220                         '01' => 'Regulated IXC',
221                         '02' => 'Unregulated LEC',
222                         '03' => 'Unregulated IXC',
223                         '04' => 'ISP',
224                         '05' => 'Wireless',
225                       );
226
227       my %customers = ( '00' => 'Residential',
228                         '01' => 'Commercial',
229                         '02' => 'Industrial',
230                         '09' => 'Lifeline',
231                         '10' => 'Senior Citizen',
232                       );
233
234       my $taxproduct =
235         join(':', map{ $hash->{$_} } qw(group item provider customer ) );
236
237       my %part_pkg_taxproduct = ( 'data_vendor' => 'cch', 
238                                   'taxproduct' => $taxproduct,
239                                 );
240
241       my $part_pkg_taxproduct = qsearchs( 'part_pkg_taxproduct', 
242                                           { %part_pkg_taxproduct }
243                                         );
244
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');
249
250         $part_pkg_taxproduct{'description'} = 
251           join(' : ', (map{ $hash->{$_} } qw(groupdesc itemdesc)),
252                       $providers{$hash->{'provider'}} || '',
253                       $customers{$hash->{'customer'}} || '',
254               );
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"
258           if $error;
259
260       }
261       $hash->{'taxproductnum'} = $part_pkg_taxproduct->taxproductnum;
262
263       delete($hash->{$_})
264         for qw(group groupdesc item itemdesc provider customer rectype );
265
266       my %map = ( 'taxclassnum'      => [ 'taxtype', 'taxcat' ],
267                   'taxclassnumtaxed' => [ 'taxtypetaxed', 'taxcattaxed' ],
268                 );
269
270       for my $item (keys %map) {
271         my $class = join(':', map($hash->{$_}, @{$map{$item}}));
272         my $tax_class =
273           qsearchs( 'tax_class',
274                     { data_vendor => 'cch',
275                       'taxclass' => $class,
276                     }
277                   );
278         $hash->{$item} = $tax_class->taxclassnum
279           if $tax_class;
280
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 ':'
285              );
286
287         delete($hash->{$_}) foreach @{$map{$item}};
288       }
289
290       my $parser = new DateTime::Format::Strptime( pattern => "%m/%d/%Y",
291                                                    time_zone => 'floating',
292                                                  );
293       my $dt = $parser->parse_datetime( $hash->{'effdate'} );
294       return "Can't parse effdate ". $hash->{'effdate'}. ': '. $parser->errstr
295         unless $dt;
296       $hash->{'effdate'} = $dt->epoch;
297  
298       $hash->{'country'} = 'US'; # CA is available
299
300       $hash->{'taxable'} = '' if ($hash->{'taxable'} eq 'N');
301
302       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
303         delete($hash->{actionflag});
304
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) );
309           }
310         }
311
312         my @part_pkg_taxrate = qsearch('part_pkg_taxrate', $hash);
313         unless ( scalar(@part_pkg_taxrate) || $param->{'delete_only'} ) {
314           if ( $hash->{taxproductnum} ) {
315             my $taxproduct =
316               qsearchs( 'part_pkg_taxproduct',
317                         { 'taxproductnum' => $hash->{taxproductnum} }
318                       );
319             $hash->{taxproductnum} .= ' ( '. $taxproduct->taxproduct. ' )'
320               if $taxproduct;
321           }
322           return "Can't find part_pkg_taxrate to delete: ".
323                  join(" ", map { "$_ => *". $hash->{$_}. '*' } keys(%$hash) );
324         }
325
326         foreach my $part_pkg_taxrate (@part_pkg_taxrate) {
327           my $error = $part_pkg_taxrate->delete;
328           return $error if $error;
329         }
330
331         delete($hash->{$_}) foreach (keys %$hash);
332       }
333
334       delete($hash->{actionflag});
335
336       '';
337     };
338
339   } elsif ( $format eq 'extended' ) {
340     die "unimplemented\n";
341     @fields = qw( );
342     $hook = sub {};
343   } else {
344     die "unknown format $format";
345   }
346
347   eval "use Text::CSV_XS;";
348   die $@ if $@;
349
350   my $csv = new Text::CSV_XS;
351
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';
358
359   my $oldAutoCommit = $FS::UID::AutoCommit;
360   local $FS::UID::AutoCommit = 0;
361   my $dbh = dbh;
362   
363   while ( defined($line=<$fh>) ) {
364     $csv->parse($line) or do {
365       $dbh->rollback if $oldAutoCommit;
366       return "can't parse: ". $csv->error_input();
367     };
368
369
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"
374         );
375         die $error if $error;
376         $last = time;
377       }
378     }
379
380     my @columns = $csv->fields();
381
382     my %part_pkg_taxrate = ( 'data_vendor' => $format );
383     foreach my $field ( @fields ) {
384       $part_pkg_taxrate{$field} = shift @columns; 
385     }
386     if ( scalar( @columns ) ) {
387       $dbh->rollback if $oldAutoCommit;
388       return "Unexpected trailing columns in line (wrong format?) importing part_pkg_taxrate: $line";
389     }
390
391     my $error = &{$hook}(\%part_pkg_taxrate);
392     if ( $error ) {
393       $dbh->rollback if $oldAutoCommit;
394       return $error;
395     }
396     next unless scalar(keys %part_pkg_taxrate);
397
398
399     my $part_pkg_taxrate = new FS::part_pkg_taxrate( \%part_pkg_taxrate );
400     $error = $part_pkg_taxrate->insert;
401
402     if ( $error ) {
403       $dbh->rollback if $oldAutoCommit;
404       return "can't insert part_pkg_taxrate for $line: $error";
405     }
406
407     $imported++;
408   }
409
410   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
411
412   return "Empty file!" unless ( $imported || $format eq 'cch-update' );
413
414   ''; #no error
415
416 }
417
418 =back
419
420 =head1 BUGS
421
422 =head1 SEE ALSO
423
424 L<FS::Record>, schema.html from the base documentation.
425
426 =cut
427
428 1;
429
430