Author Topic: Washington State sales tax - destination-based implementation  (Read 31220 times)

FinallyInSeattle

  • Beginner
  • *
  • Posts: 25
I think I've worked out the implementation needed to retrieve the sales tax to charge for a Washington state address.  It's based on the "WA State Sales Tax Rate Lookup URL Interface" documented at http://dor.wa.gov/Content/FindTaxesAndRates/RetailSalesTax/DestinationBased/ClientInterface.aspx.

The changes below reflect the code in VM 1.1.3.

1) Make sure you have a sales tax rate recorded in your VM Tax rate list for WA - this will be used as a "fallback rate" if errors are encountered.

2) in \classes\ps_checkout.php - change line 1468 from:
Code: [Select]
$q = "SELECT state, country FROM #__{vm}_user_info ";to:
Code: [Select]
$q = "SELECT address_1, city, state, zip, country FROM #__{vm}_user_info ";
3) in same file, replace lines 1479 - 1487
Code: [Select]
if ($db->next_record()) {
$rate = $order_taxable * floatval( $db->f("tax_rate") );
if (empty($rate)) {
$order_tax = 0.0;
}
else {
$order_tax = $rate;
}
     

with

Code: [Select]
if ($db->next_record()) {
if ($state == "WA" && $country == "USA") {
$taxrate = $this->WA_sales_tax($address, $city, $zipcode);
if (empty($taxrate) || $taxrate == -1.0) {
$taxrate = floatval( $db->f("tax_rate") );
}
$order_tax = $order_taxable * $taxrate;
}
else {
$rate = $order_taxable * floatval( $db->f("tax_rate") );
if (empty($rate)) {
$order_tax = 0.0;
}
else {
$order_tax = $rate;
}
}
     

The content of the WA_sales_tax function is as follows:

Code: [Select]
function WA_sales_tax($address, $city, $zipcode) {
        $URL = "http://dor.wa.gov/AddressRates.aspx?output=xml&";

        $URL .= "addr=" . urlencode($address) 
. "&city=" . urlencode($city)
. "&zip=" . urlencode($zipcode);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

$parser = xml_parser_create();
xml_parse_into_struct($parser, $response, $values);

//result code
$resultCode = $values[0]["attributes"]["CODE"];

//Debugging help
$help = $values[0]["attributes"]["DEBUGHINT"];

//tax rate
$rate = $values[0]["attributes"]["RATE"];

//tax location code
$code = $values[0]["attributes"]["LOCCODE"];

//The standardized address
$address = $values[1]["attributes"];

$zip = (strlen($address["PLUS4"]) > 0 && $address["PLUS4"] != "0000") ? $address["ZIP"] . "-" . $address["PLUS4"] : $address["ZIP"];

$street = $values[1]["attributes"]["STREET"];

xml_parser_free($parser);

curl_close($ch);

return floatval($rate);
}

Let me know if you see issues or room for improvement.

Mike Wyatt

  • Jr. Member
  • **
  • Posts: 71
Re: Washington State sales tax - destination-based implementation
« Reply #1 on: March 26, 2009, 23:19:48 pm »
Thanks for creating this solution!

Please forgive my newbie question, but what should be done with the function WA-sales_tax file?

FinallyInSeattle

  • Beginner
  • *
  • Posts: 25
Re: Washington State sales tax - destination-based implementation
« Reply #2 on: March 27, 2009, 02:55:28 am »
Thanks for creating this solution!

Please forgive my newbie question, but what should be done with the function WA-sales_tax file?

The entire function needs to be pasted into \classes\ps_checkout.php.

Bruce Morgan

  • Full Member
  • ***
  • Posts: 672
Re: Washington State sales tax - destination-based implementation
« Reply #3 on: March 27, 2009, 15:01:53 pm »
Tis might be slightly off topic but does this hack allow for multiple tax rates?  That capability is supposedly built into the VM core biut I have neve been able to make it work for WA state.  Is that an issue for you?  Great work by the way!

Bruce
www.pepper-passion.com

Mike Wyatt

  • Jr. Member
  • **
  • Posts: 71
Re: Washington State sales tax - destination-based implementation
« Reply #4 on: March 27, 2009, 18:02:14 pm »
Hi

Implemented as shown, but tax is not showing. Can you give a quick idea of the other tax settings that must be in place for this to work? Thanks!

FinallyInSeattle

  • Beginner
  • *
  • Posts: 25
Re: Washington State sales tax - destination-based implementation
« Reply #5 on: March 28, 2009, 04:46:58 am »
Hi

Implemented as shown, but tax is not showing. Can you give a quick idea of the other tax settings that must be in place for this to work? Thanks!

Did you do step #1?  Unless you have a tax rate entered in the VM Tax rate list, none of the tax logic will run.

Mike Wyatt

  • Jr. Member
  • **
  • Posts: 71
Re: Washington State sales tax - destination-based implementation
« Reply #6 on: March 28, 2009, 17:50:30 pm »
Yes, I have a tax rate listed for Washington.

I will receheck all code changes again to make sure I didn't make an error.

Mike Wyatt

  • Jr. Member
  • **
  • Posts: 71
Re: Washington State sales tax - destination-based implementation
« Reply #7 on: March 28, 2009, 20:56:43 pm »
Reworked all (several times) exactly as instructed... Now I get this error:

Parse error: syntax error, unexpected T_ELSE in /home/xxxxxx/public_html/administrator/components/com_virtuemart/classes/ps_checkout.php on line 1496

Can I send you my ps_checkout file for a look? Any help greatly appreciated!

FinallyInSeattle

  • Beginner
  • *
  • Posts: 25
Re: Washington State sales tax - destination-based implementation
« Reply #8 on: March 30, 2009, 17:42:07 pm »
Sent you a personal message ...

Antimidas

  • Beginner
  • *
  • Posts: 2
Re: Washington State sales tax - destination-based implementation
« Reply #9 on: March 31, 2009, 20:08:44 pm »
Awesome job working out the code!  I think I can use the bulk of it for MN, WI, SD, ND, NE, CO, WY and IA as well, but we do not have a website to reference here like you do in WA.  How hard would it be to adapt the existing tax table that I have to a reference lookup inside of MySQL?  Could the existing tad table be modified or extended to do a CVS import?  I have 8979 records in an Access DB that cover all of the aforementioned states.  And I have very limited PHP development knowledge.

If it would help in the development of a fix for future versions, I can post the table here with all of the current tax rates that I have.

Zombie

  • Beginner
  • *
  • Posts: 36
    • Thunder Kiss NW
Re: Washington State sales tax - destination-based implementation
« Reply #10 on: April 09, 2009, 04:58:16 am »
Could you also send my a copy of your file so I can get mine to work also?

Thanks   ;D

Zombie

  • Beginner
  • *
  • Posts: 36
    • Thunder Kiss NW
Re: Washington State sales tax - destination-based implementation
« Reply #11 on: April 13, 2009, 05:43:18 am »
Can anyone help me out?
I need a copy of the file so I can implement it also.

VoxxBoxx

  • Beginner
  • *
  • Posts: 11
Re: Washington State sales tax - destination-based implementation
« Reply #12 on: April 13, 2009, 16:12:08 pm »
Is this the same issue that we are having in Canada--each province has its own sales tax, plus a separate federal tax? Essentially in each province we need two separate taxes applied.

Zombie

  • Beginner
  • *
  • Posts: 36
    • Thunder Kiss NW
Re: Washington State sales tax - destination-based implementation
« Reply #13 on: April 16, 2009, 05:27:48 am »
What's up with this??

I got the code in, default tax code is in.
No matter what address I ship to it always uses the default tax rate?

Does this code actually work?

land0

  • Beginner
  • *
  • Posts: 10
Re: Washington State sales tax - destination-based implementation
« Reply #14 on: April 29, 2009, 15:22:10 pm »
Hello All,

Seattle thanks for the starter code. Tested this and received no change in the tax rate. Debugged it and saw that it was only loading the default tax rate.

I am curious about to things.
1 how do we debug the server connection to the WA server?
2 What would it take to make this work for VMart 1.0.15?

Thank you in advance for your time on this.