commerce_avatax_basic.inc in Drupal Commerce Connector for AvaTax 7.3
Basic version of collecting sales tax from AvaTax Calc service.
Copyright (C) adTumbler, Inc, adTumbler.com
File
commerce_avatax_basic.incView source
<?php
/**
* @file
* Basic version of collecting sales tax from AvaTax Calc service.
*
* Copyright (C) adTumbler, Inc, adTumbler.com
*
*/
/**
* Use populated order to calculate sales tax based on the specified address.
*
* @param $order
* The current order object.
*
* @param $ava_args
* An array containing from & to delivery details.
*
* @return
* FALSE if the tax calculation failed.
* An array containing tax amount, taxable amount, total order amount - if success
*/
function commerce_avatax_get_tax($order, $order_wrapper, $ava_args) {
// include in all Avalara Scripts
$library = libraries_load('avalara_avatax');
if (!$library || empty($library['loaded'])) {
drupal_set_message('Please install Avatax library.', 'warning');
return FALSE;
}
// Avatax Get Tax rates
// Create new origin, destination, client and tax request objects
$client = new TaxServiceSoap($ava_args['use_mode']);
$request = new GetTaxRequest();
$origin = new Address();
$destination = new Address();
// Populate the From address
$origin
->setLine1($ava_args['primary_street1']);
$origin
->setLine2($ava_args['primary_street2']);
$origin
->setCity($ava_args['primary_city']);
$origin
->setRegion($ava_args['primary_state']);
$origin
->setPostalCode($ava_args['primary_zip']);
// Populate the Destination address
$destination
->setLine1($ava_args['street1']);
$destination
->setLine2($ava_args['street2']);
$destination
->setCity($ava_args['city']);
$destination
->setRegion($ava_args['state']);
$destination
->setCountry($ava_args['country']);
$destination
->setPostalCode($ava_args['zip']);
// Set request object
$request
->setOriginAddress($origin);
$request
->setDestinationAddress($destination);
$request
->setcompanycode($ava_args['company_code']);
$request
->setDetailLevel(DetailLevel::$Tax);
$dateTime = new DateTime();
$request
->setDocType('SalesInvoice');
// Update 'dc-' to amend doc id in AvaTax - remember to update .erp
$request
->setDocCode('dc-' . $order->order_id . '');
$request
->setDocDate(date_format($dateTime, "Y-m-d"));
$request
->setSalespersonCode('Drupal Commerce');
$request
->setCustomerCode($ava_args['user_id']);
$request
->setLocationCode('');
$request
->setCustomerUsageType('');
$i = 1;
foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {
$line_item = $line_item_wrapper
->value();
if (in_array($line_item->type, commerce_product_line_item_types())) {
${'line' . $i} = new Line();
${'line' . $i}
->setNo($i);
${'line' . $i}
->setItemCode($line_item->line_item_label);
${'line' . $i}
->setDescription($line_item_wrapper->commerce_product->title
->value());
${'line' . $i}
->setTaxCode('');
${'line' . $i}
->setQty($line_item->quantity);
${'line' . $i}
->setAmount($line_item_wrapper->commerce_unit_price->amount
->value() / 100 * $line_item->quantity);
${'line' . $i}
->setDiscounted('false');
${'line' . $i}
->setRevAcct('');
${'line' . $i}
->setRef1('');
${'line' . $i}
->setRef2('');
${'line' . $i}
->setCustomerUsageType('');
$lines[] = ${'line' . $i};
$i++;
}
elseif (in_array($line_item->type, array(
'shipping',
))) {
${'line' . $i} = new Line();
${'line' . $i}
->setNo($i);
${'line' . $i}
->setItemCode('Shipping');
${'line' . $i}
->setDescription('Shipping');
${'line' . $i}
->setTaxCode($ava_args['shipcode']);
${'line' . $i}
->setQty($line_item->quantity);
${'line' . $i}
->setAmount($line_item_wrapper->commerce_unit_price->amount
->value() / 100 * $line_item->quantity);
${'line' . $i}
->setDiscounted('false');
${'line' . $i}
->setRevAcct('');
${'line' . $i}
->setRef1('');
${'line' . $i}
->setRef2('');
${'line' . $i}
->setCustomerUsageType('');
$lines[] = ${'line' . $i};
$i++;
}
elseif (in_array($line_item->type, array(
'commerce_coupon',
))) {
${'line' . $i} = new Line();
${'line' . $i}
->setNo($i);
${'line' . $i}
->setItemCode('Coupon');
${'line' . $i}
->setDescription('Coupon');
${'line' . $i}
->setTaxCode('');
${'line' . $i}
->setQty($line_item->quantity);
${'line' . $i}
->setAmount($line_item_wrapper->commerce_unit_price->amount
->value() / 100 * $line_item->quantity);
${'line' . $i}
->setDiscounted('false');
${'line' . $i}
->setRevAcct('');
${'line' . $i}
->setRef1('');
${'line' . $i}
->setRef2('');
${'line' . $i}
->setCustomerUsageType('');
$lines[] = ${'line' . $i};
$i++;
}
elseif (in_array($line_item->type, array(
'commerce_discount',
))) {
${'line' . $i} = new Line();
${'line' . $i}
->setNo($i);
${'line' . $i}
->setItemCode('Discount');
${'line' . $i}
->setDescription('Discount');
${'line' . $i}
->setTaxCode('');
${'line' . $i}
->setQty($line_item->quantity);
${'line' . $i}
->setAmount($line_item_wrapper->commerce_unit_price->amount
->value() / 100 * $line_item->quantity);
${'line' . $i}
->setDiscounted('false');
${'line' . $i}
->setRevAcct('');
${'line' . $i}
->setRef1('');
${'line' . $i}
->setRef2('');
${'line' . $i}
->setCustomerUsageType('');
$lines[] = ${'line' . $i};
$i++;
}
}
$request
->setLines($lines);
// Try AvaTax
try {
$getTaxResult = $client
->getTax($request);
if ($getTaxResult
->getResultCode() == SeverityLevel::$Success) {
$avatax_result = array(
'tax_amount' => $getTaxResult
->getTotalTax(),
'taxable_amount' => $getTaxResult
->getTotalTaxable(),
'total_amount' => $getTaxResult
->getTotalAmount(),
'tax_details' => array(),
);
$tax_info = array();
$i = 0;
foreach ($getTaxResult
->getTaxLines() as $tax_line) {
$line = array(
'Tax Line' => $tax_line
->getNo(),
'Amount' => $tax_line
->getTax(),
'Tax Code' => $tax_line
->getTaxCode(),
'Data' => array(),
);
$tax_info[] = $line;
$line_info = array();
foreach ($tax_line
->getTaxDetails() as $tax_details) {
$line_rates = array(
'Juris Type' => $tax_details
->getJurisType(),
'Juris Name' => $tax_details
->getJurisName(),
'Rate' => $tax_details
->getRate(),
'Amt' => $tax_details
->getTax(),
);
$line_info[] = $line_rates;
}
$tax_info[$i]['Data'] = $line_info;
$i++;
}
$avatax_result['tax_details'] = $tax_info;
}
else {
foreach ($getTaxResult
->getMessages() as $msg) {
drupal_set_message(t('AvaTax error: ' . $msg
->getName() . " - " . $msg
->getSummary() . " - " . $msg
->getDetails() . ''), 'error');
}
return FALSE;
}
} catch (SoapFault $exception) {
$msg = 'SOAP Exception: ';
if ($exception) {
$msg .= $exception->faultstring;
}
drupal_set_message(t('AvaTax message is: ' . $msg . '.'));
drupal_set_message(t('AvaTax last request is: ' . $client
->__getLastRequest() . '.'));
drupal_set_message(t('AvaTax last response is: ' . $client
->__getLastResponse() . '.'));
return FALSE;
}
return $avatax_result;
}
Functions
Name | Description |
---|---|
commerce_avatax_get_tax | Use populated order to calculate sales tax based on the specified address. |