1 package FS::cust_tax_location;
5 use FS::Record qw( qsearch qsearchs dbh );
11 FS::cust_tax_location - Object methods for cust_tax_location records
15 use FS::cust_tax_location;
17 $record = new FS::cust_tax_location \%hash;
18 $record = new FS::cust_tax_location { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::cust_tax_location object represents a mapping between a customer and
31 a tax location. FS::cust_tax_location inherits from FS::Record. The
32 following fields are currently supported:
50 the upper bound of the last 4 zip code digits
54 the lower bound of the last 4 zip code digits
56 =item default_location
58 'Y' when this record represents the default for zip
60 =item geocode - the foreign key into FS::part_pkg_tax_rate and FS::tax_rate
71 Creates a new cust_tax_location. To add the cust_tax_location to the database,
74 Note that this stores the hash reference, not a distinct copy of the hash it
75 points to. You can ask the object for a copy with the I<hash> method.
79 sub table { 'cust_tax_location'; }
83 Adds this record to the database. If there is an error, returns the error,
84 otherwise returns false.
90 Delete this record from the database.
94 =item replace OLD_RECORD
96 Replaces the OLD_RECORD with this one in the database. If there is an error,
97 returns the error, otherwise returns false.
103 Checks all fields to make sure this is a valid cust_tax_location. If there is
104 an error, returns the error, otherwise returns false. Called by the insert
113 $self->ut_numbern('custlocationnum')
114 || $self->ut_text('data_vendor')
115 || $self->ut_number('zip')
116 || $self->ut_text('state')
117 || $self->ut_number('plus4hi')
118 || $self->ut_number('plus4lo')
119 || $self->ut_enum('default', [ '', ' ', 'Y' ] )
120 || $self->ut_number('geocode')
122 return $error if $error;
129 my ($param, $job) = @_;
131 my $fh = $param->{filehandle};
132 my $format = $param->{'format'};
139 my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
142 while ( defined($line=<$fh>) );
146 if ( $format eq 'cch' || $format eq 'cch-update' ) {
147 @fields = qw( zip state plus4lo plus4hi geocode default );
148 push @fields, 'actionflag' if $format eq 'cch-update';
150 $imported++ if $format eq 'cch-update'; #empty file ok
155 $hash->{'data_vendor'} = 'cch';
157 if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
158 delete($hash->{actionflag});
160 my $cust_tax_location = qsearchs('cust_tax_location', $hash);
161 return "Can't find cust_tax_location to delete: ".
162 join(" ", map { "$_ => ". $hash->{$_} } @fields)
163 unless $cust_tax_location;
165 my $error = $cust_tax_location->delete;
166 return $error if $error;
168 delete($hash->{$_}) foreach (keys %$hash);
171 delete($hash->{'actionflag'});
177 } elsif ( $format eq 'extended' ) {
178 die "unimplemented\n";
181 die "unknown format $format";
184 eval "use Text::CSV_XS;";
187 my $csv = new Text::CSV_XS;
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 my $oldAutoCommit = $FS::UID::AutoCommit;
197 local $FS::UID::AutoCommit = 0;
200 while ( defined($line=<$fh>) ) {
201 $csv->parse($line) or do {
202 $dbh->rollback if $oldAutoCommit;
203 return "can't parse: ". $csv->error_input();
206 if ( $job ) { # progress bar
207 if ( time - $min_sec > $last ) {
208 my $error = $job->update_statustext(
209 int( 100 * $imported / $count )
211 die $error if $error;
216 my @columns = $csv->fields();
218 my %cust_tax_location = ( 'data_vendor' => $format );;
219 foreach my $field ( @fields ) {
220 $cust_tax_location{$field} = shift @columns;
222 if ( scalar( @columns ) ) {
223 $dbh->rollback if $oldAutoCommit;
224 return "Unexpected trailing columns in line (wrong format?): $line";
227 my $error = &{$hook}(\%cust_tax_location);
229 $dbh->rollback if $oldAutoCommit;
233 next unless scalar(keys %cust_tax_location);
235 my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
236 $error = $cust_tax_location->insert;
239 $dbh->rollback if $oldAutoCommit;
240 return "can't insert cust_tax_location for $line: $error";
246 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
248 return "Empty file!" unless $imported;
258 The author should be informed of any you find.
262 L<FS::Record>, schema.html from the base documentation.