add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / bin / freeside-tax-location-update
1 #!/usr/bin/perl
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch dbh);
7 use FS::TaxEngine;
8 use FS::cust_location;
9
10 my %opt;
11 getopts('d', \%opt);
12
13 my $user = shift or die &usage;
14 adminsuidsetup($user);
15 $FS::UID::AutoCommit = 0;
16 my $dbh = dbh;
17
18 my $engine = FS::TaxEngine->new;
19 my %hash = ( 'geocode' => '',
20              'country' => 'US' );
21 $hash{'disabled'} = '' unless $opt{d};
22 my @locations = qsearch('cust_location', \%hash);
23 foreach my $location (@locations) {
24   print $location->location_label . "...";
25   # only take the first one (the 'default')
26   my ($cust_tax_location) = $engine->cust_tax_locations($location);
27   if ($cust_tax_location) {
28     print $cust_tax_location->geocode;
29     $location->set('geocode', $cust_tax_location->geocode);
30     # geocode is not an immutable location field, so this is safe
31     my $error = $location->replace;
32     if ( $error ) {
33       print "$error\n";
34     }
35   } else {
36     print "not found.";
37   }
38   print "\n";
39 }
40 $dbh->commit;
41 print "Finished!\n";
42
43 sub usage {
44     "Usage:\n\n  freeside-tax-location-update [ -d ] user\n\n"
45   }
46
47 =head1 NAME
48
49 freeside-tax-location-update - Update service locations with tax data vendor 
50 codes.
51
52 =head1 SYNOPSIS
53
54   freeside-tax-location-update [ -d ] user
55
56 =head1 DESCRIPTION
57
58 When using tax tables from an external vendor, there's a table of tax 
59 jurisdiction codes that act as a foreign key to the tax rate definitions.
60 The jurisdiction is usually chosen based on the customer's postal code.
61
62 This script finds all non-disabled customer locations that don't have a 
63 value in the 'geocode' field, finds the most likely matching geocode in the 
64 cust_tax_location table, and stores that geocode in the cust_location record.
65 This is not guaranteed to be accurate.  There may be multiple correct 
66 geocodes for a given zip code; the script chooses the one that's marked
67 as "default".
68
69 The -d option tells the script to work on disabled location records as well,
70 which is not likely to be necessary.
71
72 Updating the geocode this way is not a location change and does not trigger
73 a cancel/reorder of the customer's packages.
74
75 =cut