diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 5bbe69528..dfffb5631 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -181,6 +181,9 @@ sub exportTaxData { my $taxData = WebGUI::Text::joinCSV(@columns) . "\n"; while (my $taxRow = $taxIterator->hashRef() ) { my @taxData = @{ $taxRow }{@columns}; + foreach my $column (@taxData) { + $column =~ tr/,/|/; ##Convert to the alternation syntax for the text file + } $taxData .= WebGUI::Text::joinCSV(@taxData) . "\n"; } my $storage = WebGUI::Storage->createTemp($self->session); @@ -222,10 +225,10 @@ sub getTaxRates { my $code = $address->get('code'); my $result = $self->session->db->buildArrayRef( q{ - select taxRate from tax where country=? - and (state='' or state=?) - and (city='' or city=?) - and (code='' or code=?) + select taxRate from tax where find_in_set(?, country) + and (state='' or find_in_set(?, lower(state))) + and (city='' or find_in_set(?, lower(city))) + and (code='' or find_in_set(?, lower(code))) }, [ $country, $state, $city, $code, ]); return $result; @@ -273,7 +276,8 @@ sub importTaxData { chomp $taxRow; $taxRow =~ s/\s*#.+$//; next unless $taxRow; - my @taxRow = WebGUI::Text::splitCSV($taxRow); + local $_; + my @taxRow = map { tr/|/,/; $_; } WebGUI::Text::splitCSV($taxRow); WebGUI::Error::InvalidFile->throw(error => qq{Error found in the CSV file}, brokenFile => $filePath, brokenLine => $line) unless scalar @taxRow == 5; push @taxData, [ @taxRow ]; diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index 1e4567186..bd6cf8608 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -36,7 +36,7 @@ my $session = WebGUI::Test->session; my $addExceptions = getAddExceptions($session); -my $tests = 68 + 2*scalar(@{$addExceptions}); +my $tests = 72 + 2*scalar(@{$addExceptions}); plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -209,6 +209,23 @@ my $wiData = $taxer->getItems->hashRef; ##Need to ignore the taxId from the database cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct'); +my $newTaxId = $taxer->add({ + country => 'USA|U.S.A.', + state => 'washington|WA', + taxRate => '7', + code => '', + city => '', +}); +$taxer->delete({taxId => $wisconsinTaxId}); +$storage = $taxer->exportTaxData(); +@fileLines = split /\n+/, $storage->getFileContentsAsScalar('siteTaxData.csv'); +my @row1 = WebGUI::Text::splitCSV($fileLines[1]); +my $wiData = $taxer->getItems->hashRef; +##Need to ignore the taxId from the database +cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct'); + +$taxer->delete({taxId => $newTaxId}); + ####################################################################### # # import @@ -392,6 +409,27 @@ cmp_deeply( 'importTaxData: error handling for a file with a bad header', ); +ok( + $taxer->importTaxData( + WebGUI::Test->getTestCollateralPath('taxTables/alternations.csv') + ), + 'Tax data with alternations inserted', +); + +my $altData = $taxer->getItems->hashRef; ##Just 1 row +cmp_deeply( + $altData, + { + taxId => ignore, + country => q{U.S.A.,USA}, + state => q{WI,Wisconsin}, + city => q{Madison}, + code => 53701, + taxRate => 0.5, + }, + 'import: Data correctly loaded with alternations' +); + ####################################################################### # # getTaxRates @@ -417,6 +455,13 @@ my $taxFreeAddress = $book->addAddress({ code => '97123', country => 'USA', }); +my $alternateAddress = $book->addAddress({ + label => 'using alternations', + city => 'Los Angeles', + state => 'CalifornIA', + code => '97123', + country => 'U.S.A.', +}); eval { $taxer->getTaxRates(); }; $e = Exception::Class->caught(); @@ -443,6 +488,12 @@ cmp_deeply( 'getTaxRates: return correct data for a state with no tax data' ); +cmp_deeply( + $taxer->getTaxRates($alternateAddress), + [7.25], #Only 7.25 because it uses the alternate address for USA + 'getTaxRates: return correct data for a state when the address has alternations' +); + ####################################################################### # # calculate diff --git a/t/supporting_collateral/taxTables/alternations.csv b/t/supporting_collateral/taxTables/alternations.csv new file mode 100644 index 000000000..b0db2bbc5 --- /dev/null +++ b/t/supporting_collateral/taxTables/alternations.csv @@ -0,0 +1,2 @@ +country,state,city,code,taxRate +U.S.A.|USA,WI|Wisconsin,Madison,53701,0.5 diff --git a/t/supporting_collateral/taxTables/largeTaxTable.csv b/t/supporting_collateral/taxTables/largeTaxTable.csv index 2168b9cf4..5744ada1a 100644 --- a/t/supporting_collateral/taxTables/largeTaxTable.csv +++ b/t/supporting_collateral/taxTables/largeTaxTable.csv @@ -5,5 +5,5 @@ USA,WI,,53701,0.5 USA,WI,,53702,0.5 USA,WI,,53703,0.5 USA,WI,,53704,0.5 -USA,CA,,,7.25 +USA|U.S.A.,CA|California,,,7.25 USA,,,97123,0.0