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.
PHP Code:
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:
PHP Code:
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.
Bookmarks