Zen Cart Logo
Forums / Currencies & Sales Taxes, VAT, GST, etc. / Local Sales Tax, advanced help needed

Local Sales Tax, advanced help needed

Views: 3,553

Results 1 to 5 of 5
19 Feb 2010, 7:55 PM
#1
wick avatar

wick

New Zenner

Join Date:
Jan 2010
Posts:
7
Plugin Contributions:
0

Local Sales Tax, advanced help needed

Hello, I have added the ability to charge the correct sales tax for shipping within Washington state to my zen cart 1.38a, but need a little help.

I installed the local sales tax mod, then replaced /includes/modules/order_total/ot_local_sales_taxes.php's process routine with the following to get the actual tax to charge from the Washington State Department of Revenue.

function process() {

  global $order;
  global $db;
  global $ot_local_sales_taxes_rate, $ot_local_sales_taxes_rate_description, $ot_local_sales_taxes_charge_tax_on_shipping, $ot_local_sales_taxes_basis;
				
  $ot_local_sales_taxes_charge_tax_on_shipping = true;		
  $ot_local_sales_taxes_basis = 'Shipping';
  $ot_local_sales_taxes_zoneid = $order->delivery['zone_id'];

  if ($ot_local_sales_taxes_zoneid == 62) {

// For delivery in Washington state, look up state and city sales tax rate from dept of revenue

             if ($x = simplexml_load_file("http://dor.wa.gov/AddressRates.aspx?output=xml&addr=" . urlencode($order->delivery['street_address'])  . "&city=" . urlencode($order->delivery['city']) . "&zip=" . urlencode($order->delivery['postcode']), "SimpleXMLElement", LIBXML_NOCDATA)) {

                $rc = (string)$x[code];
                if ($rc == '3' || $rc == '4' || $rc == '5' ) {
                   $taxRateLocal = 9.5;
                } else {
                   $lc = (string)$x[loccode];
                   $lr = (string)$x[localrate];
                   $tr = (string)$x[rate];
                   $taxRateLocal = $tr * 100;
                   }
             } else {
                $taxRateLocal = 9.5;
                }

             $ot_local_sales_taxes_rate_description = 'Washington Sales Tax';

// Get Subtotal for only items in matching tax class
	     $Taxable_Subtotal = 0;
	     for($i=0; $i<sizeof($order->products); $i++) {
		$Taxable_Subtotal += ($order->products[$i]['qty'] * $order->products[$i]['final_price']);
		}
					 							 
//use zen cart tax function to calculate the tax (Taxable subtotal + shipping if needed) 
	     $LocalTaxTotal = zen_calculate_tax(($Taxable_Subtotal + ($order->info['shipping_cost'] * (int)$ot_local_sales_taxes_charge_tax_on_shipping)), $taxRateLocal);
										 
//set the tax name based on description and tax amount
	     $taxName = $ot_local_sales_taxes_rate_description . " " . zen_display_tax_value($taxRateLocal,2) .'%';

//running total of tax for order - used for debugging  
	     $OrderLocalTax += $LocalTaxTotal;
					
//if taxable subtotal in this tax class
	     if(!$Taxable_Subtotal==0){							
//set our cart total info		
		$order->info['tax'] += $LocalTaxTotal;
		if(array_key_exists($taxName,$order->info['tax_groups'])){
			$order->info['tax_groups'][$taxName] += $LocalTaxTotal;
		}else{
			$order->info['tax_groups'] += array($taxName => $LocalTaxTotal);
		}
					
		$order->info['total'] = $order->info['shipping_cost'] + $order->info['subtotal'] + $order->info['tax'];
		}
	}

  }

I then removed the tax zone definition for Washington (since the above returns the total tax) and did not create any local sales tax items since I'm doing it myself.

This worked. The correct tax is shown in the order total.

The additional issue I want to address requires a greater understanding of the code than I currently have. The issue is, during the above lookup, I get the location code returned. When I file my state taxes, I need to summarize all the taxes per location code. Therefore, it would be helpful to keep record of this code in the order. I made a start, I created a new table _orders_tax_wa, and then found where I can write to it:

in /includes/classes/order.php (yes a core file)
in the middle of the function create, I have a working write to this table, but am setting the loc code to "1234" for now:

for ($i=0, $n=sizeof($zf_ot_modules); $i<$n; $i++) {
      $sql_data_array = array('orders_id' => $insert_id,
                              'title' => $zf_ot_modules[$i]['title'],
                              'text' => $zf_ot_modules[$i]['text'],
                              'value' => $zf_ot_modules[$i]['value'],
                              'class' => $zf_ot_modules[$i]['code'],
                              'sort_order' => $zf_ot_modules[$i]['sort_order']);

      zen_db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);

//add record of washington tax location code

     if ($zf_ot_modules[$i]['code'] == 'ot_tax') {
          $sql_data_array = array('orders_id' => $insert_id,
                              'value' => $zf_ot_modules[$i]['value'],
                              'loc' => '1234');

           zen_db_perform('zen_orders_tax_wa', $sql_data_array);
     }

      $zco_notifier->notify('NOTIFY_ORDER_DURING_CREATE_ADDED_ORDERTOTAL_LINE_ITEM', $sql_data_array);
    }

My problem is, the original location code is determined while still in checkout, and the data is written when the order is placed, how do I get the location code from the first routine to the second? As I said, this requires understanding the whole structure and I think I need someone to point me in the right direction.

Thank you very much, I really appreciate the community support here.

25 Feb 2010, 10:41 PM
#2
wick avatar

wick

New Zenner

Join Date:
Jan 2010
Posts:
7
Plugin Contributions:
0

Re: Local Sales Tax, advanced help needed

Just fyi to anyone who sees my posting as a solution that helps them. First, you might not have your shipping zone id for Washington set to 62, a more generic solution would probably be to check the delivery state. Second, the solution works for standard checkout, but I then added Google Checkout and ran into the same problem there, and there doesn't appear to be any way to get Google to lookup the tax rate from me.

12 Jun 2010, 10:50 PM
#3
andi98640 avatar

andi98640

New Zenner

Join Date:
Jan 2010
Posts:
62
Plugin Contributions:
0

Re: Local Sales Tax, advanced help needed

Thank you so much for your code!
Amazing work!
Works flawlessly for the bililng address. If they change the address the tax doesn't change. Any ideas to fix that?
I added the state file for my quickbooks to quickbooks to keep track for me. And quickbooks will keep track of it for you. Good luck on the rest.
Andi

7 Sep 2011, 11:20 PM
#4
rolyatnitram avatar

rolyatnitram

New Zenner

Join Date:
Sep 2011
Posts:
4
Plugin Contributions:
0

Re: Local Sales Tax, advanced help needed

Is the code in this thread still the latest version of this code. I am going to try it out but wondered if you had added any enhancements or bug fixes in the last year.

Thanks Martin

7 Sep 2011, 11:22 PM
#5
rolyatnitram avatar

rolyatnitram

New Zenner

Join Date:
Sep 2011
Posts:
4
Plugin Contributions:
0

Re: Local Sales Tax, advanced help needed

Also Andi98640 did you ever figure a fix for the tax rate not updating with changed addresses?

Martin