1 package FS::cust_tax_location;
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::Misc qw ( csv_from_fixed );
12 FS::cust_tax_location - Object methods for cust_tax_location records
16 use FS::cust_tax_location;
18 $record = new FS::cust_tax_location \%hash;
19 $record = new FS::cust_tax_location { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
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:
51 the upper bound of the last 4 zip code digits
55 the lower bound of the last 4 zip code digits
57 =item default_location
59 'Y' when this record represents the default for zip
61 =item geocode - the foreign key into FS::part_pkg_tax_rate and FS::tax_rate
72 Creates a new cust_tax_location. To add the cust_tax_location to the database,
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.
80 sub table { 'cust_tax_location'; }
84 Adds this record to the database. If there is an error, returns the error,
85 otherwise returns false.
91 Delete this record from the database.
95 =item replace OLD_RECORD
97 Replaces the OLD_RECORD with this one in the database. If there is an error,
98 returns the error, otherwise returns false.
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
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_location', [ '', 'Y' ] )
123 || $self->ut_enum('cityflag', [ '', 'I', 'O', 'B' ] )
124 || $self->ut_alpha('geocode')
126 return $error if $error;
128 #ugh! cch canada weirdness and more
129 if ($self->state eq 'CN' && $self->data_vendor eq 'cch-zip' ) {
130 $error = "Illegal cch canadian zip"
131 unless $self->zip =~ /^[A-Z]$/;
132 } elsif ($self->state =~ /^E([B-DFGILNPR-UW])$/ && $self->data_vendor eq 'cch-zip' ) {
133 $error = "Illegal cch european zip"
134 unless $self->zip =~ /^E$1$/;
136 $error = $self->ut_number('zip', $self->state eq 'CN' ? 'CA' : 'US');
138 return $error if $error;
140 #ugh! cch canada weirdness and more
141 return "must specify either city/county or plus4lo/plus4hi"
142 unless ( $self->plus4lo && $self->plus4hi ||
144 $self->state eq 'CN' ||
145 $self->state =~ /^E([B-DFGILNPR-UW])$/
154 my ($param, $job) = @_;
156 my $fh = $param->{filehandle};
157 my $format = $param->{'format'};
163 my @column_lengths = ();
164 my @column_callbacks = ();
165 if ( $format =~ /^cch-fixed/ ) {
166 $format =~ s/-fixed//;
169 $f =~ s/-update// && ($update = 1);
171 push @column_lengths, qw( 5 2 4 4 10 1 );
172 } elsif ( $f eq 'cch-zip' ) {
173 push @column_lengths, qw( 5 28 25 2 28 5 1 1 10 1 2 );
175 return "Unknown format: $format";
177 push @column_lengths, 1 if $update;
181 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
182 if ( $job || scalar(@column_lengths) ) {
183 my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
184 return $error if $error;
187 if ( $format eq 'cch' || $format eq 'cch-update' ) {
188 @fields = qw( zip state plus4lo plus4hi geocode default_location );
189 push @fields, 'actionflag' if $format eq 'cch-update';
191 $imported++ if $format eq 'cch-update'; #empty file ok
196 $hash->{'data_vendor'} = 'cch';
197 $hash->{'default_location'} =~ s/ //g;
199 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
200 delete($hash->{actionflag});
202 my $cust_tax_location = qsearchs('cust_tax_location', $hash);
203 return "Can't find cust_tax_location to delete: ".
204 join(" ", map { "$_ => ". $hash->{$_} } @fields)
205 unless $cust_tax_location;
207 my $error = $cust_tax_location->delete;
208 return $error if $error;
210 delete($hash->{$_}) foreach (keys %$hash);
213 delete($hash->{'actionflag'});
219 } elsif ( $format eq 'cch-zip' || $format eq 'cch-update-zip' ) {
220 @fields = qw( zip city county state postalcity countyfips countydef default_location geocode cityflag unique );
221 push @fields, 'actionflag' if $format eq 'cch-update-zip';
223 $imported++ if $format eq 'cch-update'; #empty file ok
228 $hash->{'data_vendor'} = 'cch-zip';
229 delete($hash->{$_}) foreach qw( countyfips countydef unique );
231 $hash->{'cityflag'} =~ s/ //g;
232 $hash->{'default_location'} =~ s/ //g;
234 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
235 delete($hash->{actionflag});
237 my $cust_tax_location = qsearchs('cust_tax_location', $hash);
238 return "Can't find cust_tax_location to delete: ".
239 join(" ", map { "$_ => ". $hash->{$_} } @fields)
240 unless $cust_tax_location;
242 my $error = $cust_tax_location->delete;
243 return $error if $error;
245 delete($hash->{$_}) foreach (keys %$hash);
248 delete($hash->{'actionflag'});
254 } elsif ( $format eq 'extended' ) {
255 die "unimplemented\n";
258 die "unknown format $format";
261 eval "use Text::CSV_XS;";
264 my $csv = new Text::CSV_XS;
266 local $SIG{HUP} = 'IGNORE';
267 local $SIG{INT} = 'IGNORE';
268 local $SIG{QUIT} = 'IGNORE';
269 local $SIG{TERM} = 'IGNORE';
270 local $SIG{TSTP} = 'IGNORE';
271 local $SIG{PIPE} = 'IGNORE';
273 my $oldAutoCommit = $FS::UID::AutoCommit;
274 local $FS::UID::AutoCommit = 0;
277 while ( defined($line=<$fh>) ) {
278 $csv->parse($line) or do {
279 $dbh->rollback if $oldAutoCommit;
280 return "can't parse: ". $csv->error_input();
283 if ( $job ) { # progress bar
284 if ( time - $min_sec > $last ) {
285 my $error = $job->update_statustext(
286 int( 100 * $imported / $count ). ",Importing locations"
288 die $error if $error;
293 my @columns = $csv->fields();
295 my %cust_tax_location = ( 'data_vendor' => $format );;
296 foreach my $field ( @fields ) {
297 $cust_tax_location{$field} = shift @columns;
299 if ( scalar( @columns ) ) {
300 $dbh->rollback if $oldAutoCommit;
301 return "Unexpected trailing columns in line (wrong format?): $line";
304 my $error = &{$hook}(\%cust_tax_location);
306 $dbh->rollback if $oldAutoCommit;
310 next unless scalar(keys %cust_tax_location);
312 my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
313 $error = $cust_tax_location->insert;
316 $dbh->rollback if $oldAutoCommit;
317 return "can't insert cust_tax_location for $line: $error";
323 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
325 return "Empty file!" unless ( $imported || $format =~ /^cch-update/ );
335 The author should be informed of any you find.
339 L<FS::Record>, schema.html from the base documentation.