checkpoint of new tax rating system
[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 = shift;
130
131   my $fh = $param->{filehandle};
132   my $format = $param->{'format'};
133
134   my @fields;
135   if ( $format eq 'cch' ) {
136     @fields = qw( zip state plus4lo plus4hi geocode default );
137   } elsif ( $format eq 'extended' ) {
138     die "unimplemented\n";
139     @fields = qw( );
140   } else {
141     die "unknown format $format";
142   }
143
144   eval "use Text::CSV_XS;";
145   die $@ if $@;
146
147   my $csv = new Text::CSV_XS;
148
149   my $imported = 0;
150
151   local $SIG{HUP} = 'IGNORE';
152   local $SIG{INT} = 'IGNORE';
153   local $SIG{QUIT} = 'IGNORE';
154   local $SIG{TERM} = 'IGNORE';
155   local $SIG{TSTP} = 'IGNORE';
156   local $SIG{PIPE} = 'IGNORE';
157
158   my $oldAutoCommit = $FS::UID::AutoCommit;
159   local $FS::UID::AutoCommit = 0;
160   my $dbh = dbh;
161   
162   my $line;
163   while ( defined($line=<$fh>) ) {
164     $csv->parse($line) or do {
165       $dbh->rollback if $oldAutoCommit;
166       return "can't parse: ". $csv->error_input();
167     };
168
169     my @columns = $csv->fields();
170
171     my %cust_tax_location = ( 'data_vendor' => $format );;
172     foreach my $field ( @fields ) {
173       $cust_tax_location{$field} = shift @columns; 
174     }
175
176     my $cust_tax_location = new FS::cust_tax_location( \%cust_tax_location );
177     my $error = $cust_tax_location->insert;
178
179     if ( $error ) {
180       $dbh->rollback if $oldAutoCommit;
181       return "can't insert cust_tax_location for $line: $error";
182     }
183
184     $imported++;
185   }
186
187   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188
189   return "Empty file!" unless $imported;
190
191   ''; #no error
192
193 }
194
195 =back
196
197 =head1 BUGS
198
199 The author should be informed of any you find.
200
201 =head1 SEE ALSO
202
203 L<FS::Record>, schema.html from the base documentation.
204
205 =cut
206
207 1;
208