RT# 80488 WA tax tables maintained with Cron
[freeside.git] / bin / wa_tax_rate_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
64 =item Address lookup API tool
65
66 http://webgis.dor.wa.gov/webapi/AddressRates.aspx?output=xml&addr=410 Terry Ave. North&city=&zip=98100
67
68 =cut
69
70 use strict;
71 use warnings;
72
73 our $VERSION = '0.02'; # Make Getopt:Std happy
74
75 use Getopt::Std;
76
77 use FS::Cron::tax_rate_update qw(
78   wa_sales_update_tax_table
79   wa_sales_log_customer_without_tax_district
80 );
81 use FS::Log;
82 use FS::UID qw(adminsuidsetup);
83
84 my %opts;
85 getopts( 't:y:q:f:l', \%opts );
86
87 my $user = shift
88   or die HELP_MESSAGE();
89
90 adminsuidsetup( $user )
91   or die "bad username '$user'\n";
92
93 my $log = FS::Log->new('wa_tax_rate_update');
94
95 $log->info('Begin wa_tax_rate_update');
96
97 {
98   local $@;
99   eval {
100     wa_sales_update_tax_table({
101       $opts{f} ? ( filename => $opts{f} ) : (),
102       $opts{t} ? ( taxname  => $opts{t} ) : (),
103       $opts{y} ? ( year     => $opts{y} ) : (),
104       $opts{q} ? ( quarter  => $opts{q} ) : (),
105     });
106   };
107
108   if ( $@ ) {
109     $log->error( "Error: $@" );
110     warn "Error: $@\n";
111   } else {
112     $log->info( 'Finished wa_tax_rate_update' );
113     warn "Finished wa_tax_rate_update\n";
114   }
115 }
116
117
118 if ( $opts{l} ) {
119   $log->info( 'Begin wa_sales_log_customer_without_tax_district' );
120
121   wa_sales_log_customer_without_tax_district();
122
123   $log->info( 'Finished wa_sales_log_customer_without_tax_district' );
124   warn "Finished wa_sales_log_customer_without_tax_district\n";
125 }
126
127 exit;
128
129 sub HELP_MESSAGE {
130   print "
131     Tool to update city/district sales tax rates in I<cust_main_county> from
132     the Washington State Department of Revenue website.
133
134     Usage: [-f filename] [-t taxname] [-y year] [-q quarter] [-l] freeside_username
135
136     Optional Options:
137       -f filename   Skip download, and process the specified filename
138       -t taxname    Apply tax name value to created or updated records
139                     defaults as conf value 'tax_district_taxname'
140       -y year       Year for data file download
141       -q quarter    Quarter of data file to download
142       -t lookup     Try to fix cust_location records without a district
143
144   ";
145   exit;
146 }
147