function commerce_avatax_retrieve_sales_tax in Drupal Commerce Connector for AvaTax 7.3
Same name and namespace in other branches
- 7.4 commerce_avatax.module \commerce_avatax_retrieve_sales_tax()
AvaTax service: returns the sales tax amount as an array.
Parameters
$order: The order object to delete the avatax line items from.
Return value
The AvaTax Calc sales tax values as an array.
2 calls to commerce_avatax_retrieve_sales_tax()
- commerce_avatax_calculate_sales_tax in ./
commerce_avatax.module - Calculate sales tax using regular web site checkout.
- commerce_avatax_manual_calculate_sales_tax in ./
commerce_avatax.module - Calculate sales tax for manual order entry.
File
- ./
commerce_avatax.module, line 487 - Calculate Sales Tax using AvaTax Calc service from Avalara, Inc.
Code
function commerce_avatax_retrieve_sales_tax($order) {
// Get Company Code, Company Use Mode and Pro Mode.
$company_code = variable_get('commerce_avatax_company_code', '');
$use_mode = variable_get('commerce_avatax_use_mode', '');
$pro_mode = variable_get('commerce_avatax_pro_mode', '');
// Sales Tax Shipping code
$shipcode = variable_get('commerce_avatax_shipcode', '');
$sales_tax = array(
'amount' => 0,
'currency_code' => 'USD',
'data' => array(),
);
// Build order wrapper
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Exit if there are no line items in the order wrapper
if (count($order_wrapper->commerce_line_items) == 0) {
drupal_set_message(t('There are no line items for this order. Please Cancel this entry!'), 'error');
return $sales_tax;
}
// Add logic - integrate with physical goods module - to use billing address if only digital products with the order
// Get address to be used for sales tax
if (variable_get('commerce_avatax_tax_address', '') == 'Billing') {
if (isset($order_wrapper->commerce_customer_billing->commerce_customer_address)) {
$billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address
->value();
$street1 = $billing_address['thoroughfare'];
$street2 = $billing_address['premise'];
$city = $billing_address['locality'];
$state = $billing_address['administrative_area'];
$country = $billing_address['country'];
$zip = $billing_address['postal_code'];
}
}
if (variable_get('commerce_avatax_tax_address', '') == 'Shipping') {
if (isset($order_wrapper->commerce_customer_shipping->commerce_customer_address)) {
$shipping_address = $order_wrapper->commerce_customer_shipping->commerce_customer_address
->value();
$street1 = $shipping_address['thoroughfare'];
$street2 = $shipping_address['premise'];
$city = $shipping_address['locality'];
$state = $shipping_address['administrative_area'];
$country = $shipping_address['country'];
$zip = $shipping_address['postal_code'];
}
}
// exit if address is incomplete
if (!$street1 || !$city || !$state || !$zip) {
drupal_set_message(t('Sales tax not calculated as shipping address incomplete. Please select "Cancel" and try again.'), 'error');
return $sales_tax;
}
// exit if delivery address state is not in list of active states
$avatax_states = array();
$avatax_states_str = variable_get('commerce_avatax_select_states', '');
if ($avatax_states_str) {
$avatax_states = explode(", ", $avatax_states_str);
if (!in_array($state, $avatax_states)) {
return $sales_tax;
}
}
// Get primary business location
$primary_street1 = variable_get('commerce_avatax_primary_street1', '');
$primary_street2 = variable_get('commerce_avatax_primary_street2', '');
$primary_city = variable_get('commerce_avatax_primary_city', '');
$primary_state = variable_get('commerce_avatax_primary_state', '');
$primary_zip = variable_get('commerce_avatax_primary_zip', '');
// Get User name or e-mail address
if ($order->uid == 0) {
if (arg(0) == 'system' && $order->order_id == 0) {
drupal_set_message(t('Order # has not been allocated. Please select "Cancel". You must FIRST save the order to allocate an order # - then add sales tax. '), 'error');
return $sales_tax;
}
elseif ($order->order_id != 0 && $order->mail == '') {
$user_id = 'administrator';
}
else {
$user_email = $order->mail;
$user_id = commerce_avatax_email_to_username($user_email);
}
}
else {
$user_data = user_load($order->uid);
$user_id = $user_data->name;
}
// Construct arguments for AvaTax functions
$ava_args = compact('company_code', 'pro_mode', 'user_id', 'shipcode', 'use_mode', 'street1', 'street2', 'city', 'state', 'country', 'zip', 'primary_street1', 'primary_street2', 'primary_city', 'primary_state', 'primary_zip');
// Get tax amount - conditional on version
$avatax_result = commerce_avatax_get_tax($order, $order_wrapper, $ava_args);
// Check that there was a return from the tax request.
if (!$avatax_result) {
// AvaTax will return an error code - Integrator to determine if the checkout is to be blocked!
return $sales_tax;
}
$sales_tax = array(
'amount' => $avatax_result['tax_amount'] * 100,
'currency_code' => 'USD',
'data' => array(),
'tax_details' => $avatax_result['tax_details'],
);
return $sales_tax;
}