'taxname', 'varchar', 'NULL', $char_d, '', '',
'setuptax', 'char', 'NULL', 1, '', '', # Y = setup tax exempt
'recurtax', 'char', 'NULL', 1, '', '', # Y = recur tax exempt
+ 'source', 'varchar', 'NULL', $char_d, '', '',
],
'primary_key' => 'taxnum',
'unique' => [],
#populate tax statuses
'tax_status' => [],
+
+ #mark certain taxes as system-maintained
+ 'cust_main_county' => [],
;
\%hash;
use FS::part_pkg;
use FS::cust_tax_exempt;
use FS::cust_tax_exempt_pkg;
+use FS::upgrade_journal;
@EXPORT_OK = qw( regionselector );
=item recurtax - if 'Y', this tax does not apply to recurring fees
+=item source - the tax lookup method that created this tax record. For records
+created manually, this will be null.
+
=back
=head1 METHODS
|| $self->ut_textn('taxname')
|| $self->ut_enum('setuptax', [ '', 'Y' ] )
|| $self->ut_enum('recurtax', [ '', 'Y' ] )
+ || $self->ut_textn('source')
|| $self->SUPER::check
;
}
+sub _upgrade_data {
+ my $class = shift;
+ # assume taxes in Washington with district numbers, and null name, or
+ # named 'sales tax', are looked up via the wa_sales method. mark them.
+ my $journal = 'cust_main_county__source_wa_sales';
+ if (!FS::upgrade_journal->is_done($journal)) {
+ my @taxes = qsearch({
+ 'table' => 'cust_main_county',
+ 'extra_sql' => " WHERE tax > 0 AND country = 'US' AND state = 'WA'".
+ " AND district IS NOT NULL AND ( taxname IS NULL OR ".
+ " taxname ~* 'sales tax' )",
+ });
+ if ( @taxes ) {
+ warn "Flagging Washington state sales taxes: ".scalar(@taxes)." records.\n";
+ foreach (@taxes) {
+ $_->set('source', 'wa_sales');
+ my $error = $_->replace;
+ die $error if $error;
+ }
+ }
+ FS::upgrade_journal->set_done($journal);
+ }
+ '';
+}
+
=back
=head1 BUGS
=cut
+# this is run from the job queue so I'm not transactionizing it.
+
sub process_district_update {
my $class = shift;
my $id = shift;
my %hash = map { $_ => $tax_info->{$_} }
qw( district city county state country );
- $hash{'taxname'} = '';
-
- my $old = qsearchs('cust_main_county', \%hash);
- if ( $old ) {
- my $new = new FS::cust_main_county { $old->hash, %$tax_info };
- warn "updating tax rate for district ".$tax_info->{'district'} if $DEBUG;
- $error = $new->replace($old);
- }
- else {
+ $hash{'source'} = $method; # apply the update only to taxes we maintain
+
+ my @old = qsearch('cust_main_county', \%hash);
+ if ( @old ) {
+ foreach my $cust_main_county (@old) {
+ warn "updating tax rate #".$cust_main_county->taxnum.
+ " for district ".$tax_info->{'district'} if $DEBUG;
+ # update the tax rate only
+ $cust_main_county->set('tax', $tax_info->{'tax'});
+ $error ||= $cust_main_county->replace;
+ }
+ } else {
+ # make a new tax record, and mark it so we can find it later
+ $tax_info->{'source'} = $method;
my $new = new FS::cust_main_county $tax_info;
warn "creating tax rate for district ".$tax_info->{'district'} if $DEBUG;
$error = $new->insert;