Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / bin / freeside-wa-tax-table-update
1 #!/usr/bin/env perl
2
3 =head1 NAME
4
5 wa_tax_rate_update
6
7 =head1 DESCRIPTION
8
9 Tool to update city/district sales tax rates in I<cust_main_county> from 
10 the Washington State Department of Revenue website.
11
12 Creates, or updates, a L<FS::cust_main_county> row for every tax district
13 in Washington state. Some cities have different tax rates based on the
14 address, within the city.  Because of this, some cities have
15 district.
16
17 If tax classes are enabled, a row is created in every tax class for
18 every district.
19
20 Customer addresses aren't classified into districts here.  Instead,
21 when a Washington state address is inserted or changed in L<FS::cust_location>,
22 a job is queued for FS::geocode_Mixin::process_district_update, to ask the
23 Washington state API which tax district to use for this address.
24
25 Options:
26
27   -f <filename>: Skip downloading, and process the given excel file
28
29   -t <taxname>:  Updated or created records will be set to the given tax name.
30                  If not specified, conf value 'tax_district_taxname' will be used
31
32   -y <year>:     Specify year for tax table - defaults to current year
33
34   -q <quarter>:  Specify quarter for tax table - defaults to current quarter
35
36   -l <lookup>:   Attempt to look up the tax district classification for
37                  unclassified cust_location records in Washington.  Will
38                  notify of records that cannot be classified
39
40 =head1 Washington State Department of Revenue Resources
41
42 The state of Washington makes data files available via their public website.
43 It's possible the availability or format of these files may change.  As of now,
44 the only data file that contains both city and county names is published in
45 XLSX format.
46
47 =item WA Dept of Revenue
48
49 https://dor.wa.gov
50
51 =item Data file downloads
52
53 https://dor.wa.gov/find-taxes-rates/sales-and-use-tax-rates/downloadable-database
54
55 =item XLSX file example
56
57 https://dor.wa.gov/sites/default/files/legacy/Docs/forms/ExcsTx/LocSalUseTx/ExcelLocalSlsUserates_19_Q1.xlsx
58
59 =item CSV file example
60
61 https://dor.wa.gov/sites/default/files/legacy/downloads/Add_DataRates2018Q4.zip
62
63 =item Other district tax rows
64
65 When this tool updates the tax tables, any additional tax table rows with
66 a district set, where the 'source' column is not 'wa_sales', will have the
67 country, state, county, and city values kept updated to match the data
68 provided in the state tax tables
69
70 =item Address lookup API tool
71
72 http://webgis.dor.wa.gov/webapi/AddressRates.aspx?output=xml&addr=410 Terry Ave. North&city=&zip=98100
73
74 =cut
75
76 use strict;
77 use warnings;
78
79 our $VERSION = '0.02'; # Make Getopt:Std happy
80
81 use Getopt::Std;
82
83 use FS::Cron::tax_rate_update qw(
84   wa_sales_update_tax_table
85   wa_sales_log_customer_without_tax_district
86 );
87 use FS::Log;
88 use FS::UID qw(adminsuidsetup);
89
90 my %opts;
91 getopts( 't:y:q:f:l', \%opts );
92
93 my $user = shift
94   or die HELP_MESSAGE();
95
96 adminsuidsetup( $user )
97   or die "bad username '$user'\n";
98
99 my $log = FS::Log->new('wa_tax_rate_update');
100
101 $log->info('Begin wa_tax_rate_update');
102
103 {
104   local $@;
105   eval {
106     wa_sales_update_tax_table({
107       $opts{f} ? ( filename => $opts{f} ) : (),
108       $opts{t} ? ( taxname  => $opts{t} ) : (),
109       $opts{y} ? ( year     => $opts{y} ) : (),
110       $opts{q} ? ( quarter  => $opts{q} ) : (),
111     });
112   };
113
114   if ( $@ ) {
115     warn "Error: $@\n";
116     $log->error( "Error: $@" );
117   } else {
118     $log->info( 'Finished wa_tax_rate_update' );
119     warn "Finished wa_tax_rate_update\n";
120   }
121 }
122
123
124 if ( $opts{l} ) {
125   $log->info( 'Begin wa_sales_log_customer_without_tax_district' );
126
127   wa_sales_log_customer_without_tax_district();
128
129   $log->info( 'Finished wa_sales_log_customer_without_tax_district' );
130   warn "Finished wa_sales_log_customer_without_tax_district\n";
131 }
132
133 exit;
134
135 sub HELP_MESSAGE {
136   print "
137     Tool to update city/district sales tax rates in I<cust_main_county> from
138     the Washington State Department of Revenue website.
139
140     Usage: wa_tax_rate_update [-f filename] [-t taxname] [-y year] [-q quarter] [-l] freeside_username
141
142     Optional Options:
143       -f filename   Skip download, and process the specified filename
144       -t taxname    Apply tax name value to created or updated records
145                     defaults as conf value 'tax_district_taxname'
146       -y year       Year for data file download
147       -q quarter    Quarter of data file to download
148       -l lookup     Try to fix cust_location records without a district
149
150   ";
151   exit;
152 }
153