default to a session cookie instead of setting an explicit timeout, weird timezone...
[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_location', { disabled => '', %limit } );
27
28 # breaking the rules somewhat by modifying cust_location records in place 
29 # instead of doing a proper package change, but we're not changing the 
30 # actual address
31 warn scalar(@location)." records found.\n";
32 my $queued = 0; my $updated = 0;
33 foreach my $location (@location) {
34   my $error;
35   my $job = FS::queue->new({
36       job => 'FS::geocode_Mixin::process_district_update'
37     });
38   my $class = 'FS::cust_location';
39   my $id = $location->locationnum;
40   $error = $job->insert($class, $id);
41   if ( $error ) {
42     $dbh->rollback;
43     die "error queueing update for $class $id\n";
44   }
45   $queued++;
46 }
47 warn "Queued $queued tax district lookups.\n";
48 $dbh->commit;
49
50 sub usage {
51   "Usage:\n\n  freeside-tax-district-update [ -n ] user\n\n"
52 }
53
54 =head1 NAME
55
56 freeside-tax-district-update - Update tax district codes from a lookup source.
57
58 =head1 SYNOPSIS
59
60   freeside-tax-district-update [ -n ] user
61
62 =head1 DESCRIPTION
63
64 Updates the 'district' field for all customers and service locations 
65 using an online tax information lookup method.  Currently the only 
66 one supported is the Washington Department of Revenue sales tax table, 
67 and looking up the tax district will create a cust_main_county record
68 with the tax rate for that district.
69
70 The -n option tells the script to ignore customers and locations that 
71 already have a district code.
72
73 The actual lookup operation will run from the job queue.
74
75 =cut