function commerce_avatax_calculate_tax in Drupal Commerce Connector for AvaTax 7.5
Performs Tax calculation for a given order.
Parameters
EntityDrupalWrapper $order_wrapper: The wrapped order entity.
Return value
array|bool An associative array containing the request & the response, FALSE in case the request could not be performed.
2 calls to commerce_avatax_calculate_tax()
- commerce_avatax_commerce_cart_order_refresh in ./
commerce_avatax.module - Implements hook_commerce_cart_order_refresh().
- commerce_avatax_order_admin_calculate_tax_form_submit in includes/
commerce_avatax.admin.inc - Form submit callback for commerce_avatax_order_admin_calculate_tax_form().
File
- ./
commerce_avatax.module, line 386 - AvaTax service integration from Avalara, Inc.
Code
function commerce_avatax_calculate_tax($order_wrapper) {
// Skip tax calculation if the option is disabled, or if the company code
// is empty.
if (!commerce_avatax_tax_calculation_enabled()) {
return FALSE;
}
$company_code = commerce_avatax_company_code();
// If the company code is not configured, return FALSE.
if (empty($company_code) || !($avatax_object = commerce_avatax_object())) {
drupal_set_message(t("The Avatax module is not properly configured, please configure the company code."), 'error');
return FALSE;
}
$order = $order_wrapper
->value();
// Get the customer profile to use for tax calculation.
$field_name = commerce_avatax_get_customer_profile_field();
// Checks if the Sales Tax needs to be calculated for this address.
if (!$field_name || !commerce_avatax_check_address($order_wrapper, $field_name)) {
// Delete any existing AvaTax line items from the order.
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
// Remove the stored request & response from the order's data.
$order->data['commerce_avatax'] = array();
return FALSE;
}
module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax.calc');
$stored_request = array();
// Retrieve the stored request in the order's data array,
// compare it with the request that's about to be sent, and skip it if
// unnecessary.
if (isset($order->data['commerce_avatax']['request'])) {
$stored_request = $order->data['commerce_avatax']['request'];
}
// Prepare the request.
$request = commerce_avatax_create_transaction($order_wrapper, 'SalesOrder');
// In order for the request comparison to work properly,
// make sure the "date" is the same in both arrays.
if (isset($stored_request['date']) && isset($request['date'])) {
$stored_request['date'] = $request['date'];
}
// Stop here if the stored request in the order's object is similar to
// the one we're about to perform (i.e no need to perform an unnecessary
// request.
if ($stored_request == $request) {
return array(
'request' => $request,
'response' => $order->data['commerce_avatax']['response'] ? $order->data['commerce_avatax']['response'] : array(),
);
}
// Remove the AvaTax data from the order.
$order->data['commerce_avatax'] = array();
// Only perform the request the request array contains a "lines" key.
if (empty($request['lines'])) {
// Delete the Tax line item if present.
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
return FALSE;
}
$response = $avatax_object
->transactionsCreate($request);
if (empty($response['success']) || !isset($response['result']['totalTax'])) {
// Delete the Tax line item if present.
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
return FALSE;
}
// Store the request & the response in the order's data.
$order->data['commerce_avatax'] = array(
'request' => $request,
'response' => $response,
);
// Parse the result request.
$result = $response['result'];
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$currency_code = $order_wrapper->commerce_order_total->currency_code
->value();
$tax_price = array(
'amount' => commerce_currency_decimal_to_amount($result['totalTax'], $currency_code),
'currency_code' => $currency_code,
'data' => array(),
);
// Assume VAT when the customer's country is not in the US|CA.
$tax_type = 'sales_tax';
if (isset($order_wrapper->{$field_name})) {
$customer_address = $order_wrapper->{$field_name}->commerce_customer_address
->value();
if (!in_array($customer_address['country'], array(
'US',
'CA',
))) {
$tax_type = 'vat';
}
}
// Modify the existing tax line item or add a new one if that fails.
if (!commerce_avatax_set_existing_line_item_price($order_wrapper, $tax_price, $tax_type)) {
commerce_avatax_add_line_item($order_wrapper, $tax_price, $tax_type);
}
// Update the total order price, for the next rules condition (if any).
commerce_order_calculate_total($order);
return array(
'request' => $request,
'response' => $response,
);
}