allow completely empty updates (again?)
[freeside.git] / FS / FS / cust_tax_location.pm
1 package FS::cust_tax_location;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::Misc qw ( csv_from_fixed );
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::cust_tax_location - Object methods for cust_tax_location records
13
14 =head1 SYNOPSIS
15
16   use FS::cust_tax_location;
17
18   $record = new FS::cust_tax_location \%hash;
19   $record = new FS::cust_tax_location { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::cust_tax_location object represents a mapping between a customer and
32 a tax location.  FS::cust_tax_location inherits from FS::Record.  The
33 following fields are currently supported:
34
35 =over 4
36
37 =item custlocationnum
38
39 primary key
40
41 =item data_vendor
42
43 a tax data vendor
44
45 =item zip 
46
47 =item state
48
49 =item plus4hi
50
51 the upper bound of the last 4 zip code digits
52
53 =item plus4lo
54
55 the lower bound of the last 4 zip code digits
56
57 =item default_location
58
59 'Y' when this record represents the default for zip
60
61 =item geocode - the foreign key into FS::part_pkg_tax_rate and FS::tax_rate
62
63
64 =back
65
66 =head1 METHODS
67
68 =over 4
69
70 =item new HASHREF
71
72 Creates a new cust_tax_location.  To add the cust_tax_location to the database,
73 see L<"insert">.
74
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to.  You can ask the object for a copy with the I<hash> method.
77
78 =cut
79
80 sub table { 'cust_tax_location'; }
81
82 =item insert
83
84 Adds this record to the database.  If there is an error, returns the error,
85 otherwise returns false.
86
87 =cut
88
89 =item delete
90
91 Delete this record from the database.
92
93 =cut
94
95 =item replace OLD_RECORD
96
97 Replaces the OLD_RECORD with this one in the database.  If there is an error,
98 returns the error, otherwise returns false.
99
100 =cut
101
102 =item check
103
104 Checks all fields to make sure this is a valid cust_tax_location.  If there is
105 an error, returns the error, otherwise returns false.  Called by the insert
106 and replace methods.
107
108 =cut
109
110 sub check {
111   my $self = shift;
112
113   my $error = 
114     $self->ut_numbern('custlocationnum')
115     || $self->ut_text('data_vendor')
116     || $self->ut_textn('city')
117     || $self->ut_textn('postalcity')
118     || $self->ut_textn('county')
119     || $self->ut_text('state')
120     || $self->ut_numbern('plus4hi')
121     || $self->ut_numbern('plus4lo')
122     || $self->ut_enum('default', [ '', ' ', 'Y' ] ) # wtf?
123     || $self->ut_enum('cityflag', [ '', 'I', 'O', 'B' ] )
124     || $self->ut_alpha('geocode')
125   ;
126   return $error if $error;
127
128   #ugh!  cch canada weirdness
129   if ($self->state eq 'CN') {
130     $error = "Illegal cch canadian zip"
131      unless $self->zip =~ /^[A-Z]$/;
132   } else {
133     $error = $self->ut_number('zip', $self->state eq 'CN' ? 'CA' : 'US');
134   }
135   return $error if $error;
136
137   #ugh!  cch canada weirdness
138   return "must specify either city/county or plus4lo/plus4hi"
139     unless ( $self->plus4lo && $self->plus4hi || 
140              ($self->city || $self->state eq 'CN') && $self->county
141            );
142
143   $self->SUPER::check;
144 }
145
146
147 sub batch_import {
148   my ($param, $job) = @_;
149
150   my $fh = $param->{filehandle};
151   my $format = $param->{'format'};
152
153   my $imported = 0;
154   my @fields;
155   my $hook;
156
157   my @column_lengths = ();
158   my @column_callbacks = ();
159   if ( $format =~ /^cch-fixed/ ) {
160     $format =~ s/-fixed//;
161     my $f = $format;
162     my $update = 0;
163     $f =~ s/-update// && ($update = 1);
164     if ($f eq 'cch') {
165       push @column_lengths, qw( 5 2 4 4 10 1 );
166     } elsif ( $f eq 'cch-zip' ) {
167       push @column_lengths, qw( 5 28 25 2 28 5 1 1 10 1 2 );
168     } else {
169       return "Unknown format: $format";
170     }
171     push @column_lengths, 1 if $update;
172   }
173
174   my $line;
175   my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
176   if ( $job || scalar(@column_lengths) ) {
177     my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
178     return $error if $error;
179   }
180
181   if ( $format eq 'cch' || $format eq 'cch-update' ) {
182     @fields = qw( zip state plus4lo plus4hi geocode default );
183     push @fields, 'actionflag' if $format eq 'cch-update';
184
185     $imported++ if $format eq 'cch-update'; #empty file ok
186     
187     $hook = sub {
188       my $hash = shift;
189
190       $hash->{'data_vendor'} = 'cch';
191
192       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
193         delete($hash->{actionflag});
194
195         my $cust_tax_location = qsearchs('cust_tax_location', $hash);
196         return "Can't find cust_tax_location to delete: ".
197                join(" ", map { "$_ => ". $hash->{$_} } @fields)
198           unless $cust_tax_location;
199
200         my $error = $cust_tax_location->delete;
201         return $error if $error;
202
203         delete($hash->{$_}) foreach (keys %$hash);
204       }
205
206       delete($hash->{'actionflag'});
207
208       '';
209       
210     };
211
212   } elsif ( $format eq 'cch-zip' || $format eq 'cch-update-zip' ) {
213     @fields = qw( zip city county state postalcity countyfips countydef default geocode cityflag unique );
214     push @fields, 'actionflag' if $format eq 'cch-update-zip';
215
216     $imported++ if $format eq 'cch-update'; #empty file ok
217     
218     $hook = sub {
219       my $hash = shift;
220
221       $hash->{'data_vendor'} = 'cch-zip';
222       delete($hash->{$_}) foreach qw( countyfips countydef unique );
223
224       $hash->{'cityflag'} =~ s/ //g;
225
226       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
227         delete($hash->{actionflag});
228
229         my $cust_tax_location = qsearchs('cust_tax_location', $hash);
230         return "Can't find cust_tax_location to delete: ".
231                join(" ", map { "$_ => ". $hash->{$_} } @fields)
232           unless $cust_tax_location;
233
234         my $error = $cust_tax_location->delete;
235         return $error if $error;
236
237         delete($hash->{$_}) foreach (keys %$hash);
238       }
239
240       delete($hash->{'actionflag'});
241
242       '';
243       
244     };
245
246   } elsif ( $format eq 'extended' ) {
247     die "unimplemented\n";
248     @fields = qw( );
249   } else {
250     die "unknown format $format";
251   }
252
253   eval "use Text::CSV_XS;";
254   die $@ if $@;
255
256   my $csv = new Text::CSV_XS;
257
258   local $SIG{HUP} = 'IGNORE';
259   local $SIG{INT} = 'IGNORE';
260   local $SIG{QUIT} = 'IGNORE';
261   local $SIG{TERM} = 'IGNORE';
262   local $SIG{TSTP} = 'IGNORE';
263   local $SIG{PIPE} = 'IGNORE';
264
265   my $oldAutoCommit = $FS::UID::AutoCommit;
266   local $FS::UID::AutoCommit = 0;
267   my $dbh = dbh;
268   
269   while ( defined($line=<$fh>) ) {
270     $csv->parse($line) or do {
271       $dbh->rollback if $oldAutoCommit;
272       return "can't parse: ". $csv->error_input();
273     };
274
275     if ( $job ) {  # progress bar
276       if ( time - $min_sec > $last ) {
277         my $error = $job->update_statustext(
278           int( 100 * $imported / $count )
279         );
280         die $error if $error;
281         $last = time;
282       }
283     }
284
285     my @columns = $csv->fields();
286
287     my %cust_tax_location = ( 'data_vendor' => $format );;
288     foreach my $field ( @fields ) {
289       $cust_tax_location{$field} = shift @columns; 
290     }
291     if ( scalar( @columns ) ) {
292       $dbh->rollback if $oldAutoCommit;
293       return "Unexpected trailing columns in line (wrong format?): $line";
294     }
295
296     my $error = &{$hook}(\%cust_tax_location);
297     if ( $error ) {
298       $dbh->rollback if $oldAutoCommit;
299       return $error;
300     }
301
302     next unless scalar(keys %cust_tax_location);
303
304     my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
305     $error = $cust_tax_location->insert;
306
307     if ( $error ) {
308       $dbh->rollback if $oldAutoCommit;
309       return "can't insert cust_tax_location for $line: $error";
310     }
311
312     $imported++;
313   }
314
315   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
316
317   return "Empty file!" unless ( $imported || $format =~ /^cch-update/ );
318
319   ''; #no error
320
321 }
322
323 =back
324
325 =head1 BUGS
326
327 The author should be informed of any you find.
328
329 =head1 SEE ALSO
330
331 L<FS::Record>, schema.html from the base documentation.
332
333 =cut
334
335 1;
336