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_number('zip')
117 || $self->ut_text('state')
118 || $self->ut_number('plus4hi')
119 || $self->ut_number('plus4lo')
120 || $self->ut_enum('default', [ '', ' ', 'Y' ] )
121 || $self->ut_number('geocode')
123 return $error if $error;
130 my ($param, $job) = @_;
132 my $fh = $param->{filehandle};
133 my $format = $param->{'format'};
139 my @column_lengths = ();
140 my @column_callbacks = ();
141 if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
142 $format =~ s/-fixed//;
143 push @column_lengths, qw( 5 2 4 4 10 1 );
144 push @column_lengths, 1 if $format eq 'cch-update';
148 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
149 if ( $job || scalar(@column_callbacks) ) {
150 my $error = csv_from_fixed(\$fh, \$count, \@column_lengths);
151 return $error if $error;
154 if ( $format eq 'cch' || $format eq 'cch-update' ) {
155 @fields = qw( zip state plus4lo plus4hi geocode default );
156 push @fields, 'actionflag' if $format eq 'cch-update';
158 $imported++ if $format eq 'cch-update'; #empty file ok
163 $hash->{'data_vendor'} = 'cch';
165 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
166 delete($hash->{actionflag});
168 my $cust_tax_location = qsearchs('cust_tax_location', $hash);
169 return "Can't find cust_tax_location to delete: ".
170 join(" ", map { "$_ => ". $hash->{$_} } @fields)
171 unless $cust_tax_location;
173 my $error = $cust_tax_location->delete;
174 return $error if $error;
176 delete($hash->{$_}) foreach (keys %$hash);
179 delete($hash->{'actionflag'});
185 } elsif ( $format eq 'extended' ) {
186 die "unimplemented\n";
189 die "unknown format $format";
192 eval "use Text::CSV_XS;";
195 my $csv = new Text::CSV_XS;
197 local $SIG{HUP} = 'IGNORE';
198 local $SIG{INT} = 'IGNORE';
199 local $SIG{QUIT} = 'IGNORE';
200 local $SIG{TERM} = 'IGNORE';
201 local $SIG{TSTP} = 'IGNORE';
202 local $SIG{PIPE} = 'IGNORE';
204 my $oldAutoCommit = $FS::UID::AutoCommit;
205 local $FS::UID::AutoCommit = 0;
208 while ( defined($line=<$fh>) ) {
209 $csv->parse($line) or do {
210 $dbh->rollback if $oldAutoCommit;
211 return "can't parse: ". $csv->error_input();
214 if ( $job ) { # progress bar
215 if ( time - $min_sec > $last ) {
216 my $error = $job->update_statustext(
217 int( 100 * $imported / $count )
219 die $error if $error;
224 my @columns = $csv->fields();
226 my %cust_tax_location = ( 'data_vendor' => $format );;
227 foreach my $field ( @fields ) {
228 $cust_tax_location{$field} = shift @columns;
230 if ( scalar( @columns ) ) {
231 $dbh->rollback if $oldAutoCommit;
232 return "Unexpected trailing columns in line (wrong format?): $line";
235 my $error = &{$hook}(\%cust_tax_location);
237 $dbh->rollback if $oldAutoCommit;
241 next unless scalar(keys %cust_tax_location);
243 my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
244 $error = $cust_tax_location->insert;
247 $dbh->rollback if $oldAutoCommit;
248 return "can't insert cust_tax_location for $line: $error";
254 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
256 return "Empty file!" unless $imported;
266 The author should be informed of any you find.
270 L<FS::Record>, schema.html from the base documentation.