improved taxproduct tax report RT#4783
[freeside.git] / FS / FS / tax_rate_location.pm
1 package FS::tax_rate_location;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6
7 =head1 NAME
8
9 FS::tax_rate_location - Object methods for tax_rate_location records
10
11 =head1 SYNOPSIS
12
13   use FS::tax_rate_location;
14
15   $record = new FS::tax_rate_location \%hash;
16   $record = new FS::tax_rate_location { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::tax_rate_location object represents an example.  FS::tax_rate_location inherits from
29 FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item taxratelocationnum
34
35 Primary key (assigned automatically for new tax_rate_locations)
36
37 =item data_vendor
38
39 The tax data vendor
40
41 =item geocode
42
43 A unique geographic location code provided by the data vendor
44
45 =item city
46
47 City
48
49 =item county
50
51 County
52
53 =item state
54
55 State
56
57 =item disabled
58
59 If 'Y' this record is no longer active.
60
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new tax rate location.  To add the record to the database, see
71  L<"insert">.
72
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to.  You can ask the object for a copy with the I<hash> method.
75
76 =cut
77
78 sub table { 'tax_rate_location'; }
79
80 =item insert
81
82 Adds this record to the database.  If there is an error, returns the error,
83 otherwise returns false.
84
85 =cut
86
87 =item delete
88
89 Delete this record from the database.
90
91 =cut
92
93 sub delete {
94   return "Can't delete tax rate locations.  Set disable to 'Y' instead.";
95   # check that it is unused in any cust_bill_pkg_tax_location records instead?
96 }
97
98 =item replace OLD_RECORD
99
100 Replaces the OLD_RECORD with this one in the database.  If there is an error,
101 returns the error, otherwise returns false.
102
103 =cut
104
105 =item check
106
107 Checks all fields to make sure this is a valid tax rate location.  If there is
108 an error, returns the error, otherwise returns false.  Called by the insert
109 and replace methods.
110
111 =cut
112
113 sub check {
114   my $self = shift;
115
116   my $error = 
117     $self->ut_numbern('taxratelocationnum')
118     || $self->ut_textn('data_vendor')
119     || $self->ut_alpha('geocode')
120     || $self->ut_textn('city')
121     || $self->ut_textn('county')
122     || $self->ut_textn('state')
123     || $self->ut_enum('disabled', [ '', 'Y' ])
124   ;
125   return $error if $error;
126
127   my $t = qsearchs( 'tax_rate_location',
128                     { map { $_ => $self->$_ } qw( data_vendor geocode ) },
129                   );
130
131   return "geocode already in use for this vendor"
132     if ( $t && $t->taxratelocationnum != $self->taxratelocationnum );
133
134   return "may only be disabled"
135     if ( $t && scalar( grep { $t->$_ ne $self->$_ } 
136                        grep { $_ ne 'disabled' }
137                        $self->fields
138                      )
139        );
140
141   $self->SUPER::check;
142 }
143
144 =back
145
146 =head1 SUBROUTINES
147
148 =over 4
149
150 =item batch_import
151
152 =cut
153
154 sub batch_import {
155   my ($param, $job) = @_;
156
157   my $fh = $param->{filehandle};
158   my $format = $param->{'format'};
159
160   my %insert = ();
161   my %delete = ();
162
163   my @fields;
164   my $hook;
165
166   my @column_lengths = ();
167   my @column_callbacks = ();
168   if ( $format eq 'cch-fixed' || $format eq 'cch-fixed-update' ) {
169     $format =~ s/-fixed//;
170     my $trim = sub { my $r = shift; $r =~ s/^\s*//; $r =~ s/\s*$//; $r };
171     push @column_lengths, qw( 28 25 2 10 );
172     push @column_lengths, 1 if $format eq 'cch-update';
173     push @column_callbacks, $trim foreach (@column_lengths);
174   }
175
176   my $line;
177   my ( $count, $last, $min_sec ) = (0, time, 5); #progressbar
178   if ( $job || scalar(@column_callbacks) ) {
179     my $error =
180       csv_from_fixed(\$fh, \$count, \@column_lengths, \@column_callbacks);
181     return $error if $error;
182   }
183
184   if ( $format eq 'cch' || $format eq 'cch-update' ) {
185     @fields = qw( city county state geocode );
186     push @fields, 'actionflag' if $format eq 'cch-update';
187
188     $hook = sub {
189       my $hash = shift;
190
191       $hash->{'data_vendor'} ='cch';
192
193       if (exists($hash->{'actionflag'}) && $hash->{'actionflag'} eq 'D') {
194         delete($hash->{actionflag});
195
196         $hash->{deleted} = '';
197         my $tax_rate_location = qsearchs('tax_rate_location', $hash);
198         return "Can't find tax_rate_location to delete: ".
199                join(" ", map { "$_ => ". $hash->{$_} } @fields)
200           unless $tax_rate_location;
201
202         $tax_rate_location->disabled('Y');
203         my $error = $tax_rate_location->replace;
204         return $error if $error;
205
206         delete($hash->{$_}) foreach (keys %$hash);
207       }
208
209       delete($hash->{'actionflag'});
210
211       '';
212
213     };
214
215   } elsif ( $format eq 'extended' ) {
216     die "unimplemented\n";
217     @fields = qw( );
218     $hook = sub {};
219   } else {
220     die "unknown format $format";
221   }
222
223   eval "use Text::CSV_XS;";
224   die $@ if $@;
225
226   my $csv = new Text::CSV_XS;
227
228   my $imported = 0;
229
230   local $SIG{HUP} = 'IGNORE';
231   local $SIG{INT} = 'IGNORE';
232   local $SIG{QUIT} = 'IGNORE';
233   local $SIG{TERM} = 'IGNORE';
234   local $SIG{TSTP} = 'IGNORE';
235   local $SIG{PIPE} = 'IGNORE';
236
237   my $oldAutoCommit = $FS::UID::AutoCommit;
238   local $FS::UID::AutoCommit = 0;
239   my $dbh = dbh;
240
241   while ( defined($line=<$fh>) ) {
242     $csv->parse($line) or do {
243       $dbh->rollback if $oldAutoCommit;
244       return "can't parse: ". $csv->error_input();
245     };
246
247     if ( $job ) {  # progress bar
248       if ( time - $min_sec > $last ) {
249         my $error = $job->update_statustext(
250           int( 100 * $imported / $count )
251         );
252         die $error if $error;
253         $last = time;
254       }
255     }
256
257     my @columns = $csv->fields();
258
259     my %tax_rate_location = ();
260     foreach my $field ( @fields ) {
261       $tax_rate_location{$field} = shift @columns;
262     }
263     if ( scalar( @columns ) ) {
264       $dbh->rollback if $oldAutoCommit;
265       return "Unexpected trailing columns in line (wrong format?): $line";
266     }
267
268     my $error = &{$hook}(\%tax_rate_location);
269     if ( $error ) {
270       $dbh->rollback if $oldAutoCommit;
271       return $error;
272     }
273
274     if (scalar(keys %tax_rate_location)) { #inserts only
275
276       my $tax_rate_location = new FS::tax_rate_location( \%tax_rate_location );
277       $error = $tax_rate_location->insert;
278
279       if ( $error ) {
280         $dbh->rollback if $oldAutoCommit;
281         return "can't insert tax_rate for $line: $error";
282       }
283
284     }
285
286     $imported++;
287
288   }
289
290   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
291
292   return "Empty file!" unless ($imported || $format eq 'cch-update');
293
294   ''; #no error
295
296 }
297
298 =head1 BUGS
299
300 Currently somewhat specific to CCH supplied data.
301
302 =head1 SEE ALSO
303
304 L<FS::Record>, schema.html from the base documentation.
305
306 =cut
307
308 1;
309