66d32a5a7bedc4f7b07c139713c2e92385813c4d
[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
7 @ISA = qw(FS::Record);
8
9 =head1 NAME
10
11 FS::cust_tax_location - Object methods for cust_tax_location records
12
13 =head1 SYNOPSIS
14
15   use FS::cust_tax_location;
16
17   $record = new FS::cust_tax_location \%hash;
18   $record = new FS::cust_tax_location { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
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:
33
34 =over 4
35
36 =item custlocationnum
37
38 primary key
39
40 =item data_vendor
41
42 a tax data vendor
43
44 =item zip 
45
46 =item state
47
48 =item plus4hi
49
50 the upper bound of the last 4 zip code digits
51
52 =item plus4lo
53
54 the lower bound of the last 4 zip code digits
55
56 =item default_location
57
58 'Y' when this record represents the default for zip
59
60 =item geocode - the foreign key into FS::part_pkg_tax_rate and FS::tax_rate
61
62
63 =back
64
65 =head1 METHODS
66
67 =over 4
68
69 =item new HASHREF
70
71 Creates a new cust_tax_location.  To add the cust_tax_location to the database,
72 see L<"insert">.
73
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.
76
77 =cut
78
79 sub table { 'cust_tax_location'; }
80
81 =item insert
82
83 Adds this record to the database.  If there is an error, returns the error,
84 otherwise returns false.
85
86 =cut
87
88 =item delete
89
90 Delete this record from the database.
91
92 =cut
93
94 =item replace OLD_RECORD
95
96 Replaces the OLD_RECORD with this one in the database.  If there is an error,
97 returns the error, otherwise returns false.
98
99 =cut
100
101 =item check
102
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
105 and replace methods.
106
107 =cut
108
109 sub check {
110   my $self = shift;
111
112   my $error = 
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')
121   ;
122   return $error if $error;
123
124   $self->SUPER::check;
125 }
126
127
128 sub batch_import {
129   my ($param, $job) = @_;
130
131   my $fh = $param->{filehandle};
132   my $format = $param->{'format'};
133
134   my $imported = 0;
135   my @fields;
136   my $hook;
137
138   my $line;
139   my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
140   if ( $job ) {
141     $count++
142       while ( defined($line=<$fh>) );
143     seek $fh, 0, 0;
144   }
145
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';
149
150     $imported++ if $format eq 'cch-update'; #empty file ok
151     
152     $hook = sub {
153       my $hash = shift;
154
155       $hash->{'data_vendor'} = 'cch';
156
157       if (exists($hash->{actionflag}) && $hash->{actionflag} eq 'D') {
158         delete($hash->{actionflag});
159
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;
164
165         my $error = $cust_tax_location->delete;
166         return $error if $error;
167
168         delete($hash->{$_}) foreach (keys %$hash);
169       }
170
171       delete($hash->{'actionflag'});
172
173       '';
174       
175     };
176
177   } elsif ( $format eq 'extended' ) {
178     die "unimplemented\n";
179     @fields = qw( );
180   } else {
181     die "unknown format $format";
182   }
183
184   eval "use Text::CSV_XS;";
185   die $@ if $@;
186
187   my $csv = new Text::CSV_XS;
188
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';
195
196   my $oldAutoCommit = $FS::UID::AutoCommit;
197   local $FS::UID::AutoCommit = 0;
198   my $dbh = dbh;
199   
200   while ( defined($line=<$fh>) ) {
201     $csv->parse($line) or do {
202       $dbh->rollback if $oldAutoCommit;
203       return "can't parse: ". $csv->error_input();
204     };
205
206     if ( $job ) {  # progress bar
207       if ( time - $min_sec > $last ) {
208         my $error = $job->update_statustext(
209           int( 100 * $imported / $count )
210         );
211         die $error if $error;
212         $last = time;
213       }
214     }
215
216     my @columns = $csv->fields();
217
218     my %cust_tax_location = ( 'data_vendor' => $format );;
219     foreach my $field ( @fields ) {
220       $cust_tax_location{$field} = shift @columns; 
221     }
222     if ( scalar( @columns ) ) {
223       $dbh->rollback if $oldAutoCommit;
224       return "Unexpected trailing columns in line (wrong format?): $line";
225     }
226
227     my $error = &{$hook}(\%cust_tax_location);
228     if ( $error ) {
229       $dbh->rollback if $oldAutoCommit;
230       return $error;
231     }
232
233     next unless scalar(keys %cust_tax_location);
234
235     my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
236     $error = $cust_tax_location->insert;
237
238     if ( $error ) {
239       $dbh->rollback if $oldAutoCommit;
240       return "can't insert cust_tax_location for $line: $error";
241     }
242
243     $imported++;
244   }
245
246   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
247
248   return "Empty file!" unless $imported;
249
250   ''; #no error
251
252 }
253
254 =back
255
256 =head1 BUGS
257
258 The author should be informed of any you find.
259
260 =head1 SEE ALSO
261
262 L<FS::Record>, schema.html from the base documentation.
263
264 =cut
265
266 1;
267