sales tax districts, #15089
[freeside.git] / FS / bin / freeside-tax-district-update
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use Date::Parse 'str2time';
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch dbh);
8 use FS::Conf;
9 use FS::cust_main;
10 use FS::h_cust_main;
11
12 my %opt;
13 getopts('n', \%opt);
14
15 my $user = shift or die &usage;
16 adminsuidsetup($user);
17 $FS::UID::AutoCommit = 0;
18 my $dbh = dbh;
19
20 my $conf = FS::Conf->new;
21 my $method = $conf->config('tax_district_method')
22   or die "no tax district lookup method configured.\n";
23
24 my %limit;
25 %limit = ( district => '' ) if $opt{'n'};
26 my @location = qsearch( 'cust_main', \%limit ),
27                qsearch( 'cust_location', { disabled => '', %limit } );
28
29 # breaking the rules somewhat by modifying cust_location records in place 
30 # instead of doing a proper package change, but we're not changing the 
31 # actual address
32 warn scalar(@location)." records found.\n";
33 my $queued = 0; my $updated = 0;
34 foreach my $location (@location) {
35   my $error;
36   my $job = FS::queue->new({
37       job => 'FS::geocode_Mixin::process_district_update'
38     });
39   my $class = ref($location);
40   my $id = $class eq 'FS::cust_main' ? 
41                                   $location->custnum : 
42                                   $location->locationnum;
43   $error = $job->insert($class, $id);
44   if ( $error ) {
45     $dbh->rollback;
46     die "error queueing update for $class $id\n";
47   }
48   $queued++;
49 }
50 warn "Queued $queued tax district lookups.\n";
51 $dbh->commit;
52
53 sub usage {
54   "Usage:\n\n  freeside-tax-district-update [ -n ] user\n\n"
55 }
56
57 =head1 NAME
58
59 freeside-tax-district-update - Update tax district codes from a lookup source.
60
61 =head1 SYNOPSIS
62
63   freeside-tax-district-update [ -n ] user
64
65 =head1 DESCRIPTION
66
67 Updates the 'district' field for all customers and service locations 
68 using an online tax information lookup method.  Currently the only 
69 one supported is the Washington Department of Revenue sales tax table, 
70 and looking up the tax district will create a cust_main_county record
71 with the tax rate for that district.
72
73 The -n option tells the script to ignore customers and locations that 
74 already have a district code.
75
76 The actual lookup operation will run from the job queue.
77
78 =cut