You are here

function commerce_avatax_retrieve_sales_tax in Drupal Commerce Connector for AvaTax 7.4

Same name and namespace in other branches
  1. 7.3 commerce_avatax.module \commerce_avatax_retrieve_sales_tax()

AvaTax service: returns the sales tax amount as an array.

Parameters

object $order: The order object to calculate the AvaTax line items for.

bool $commit: Should we also commit the transaction.

Return value

array The AvaTax sales tax values as an array.

3 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.
_commerce_avatax_update in ./commerce_avatax.rules.inc
Send Commit/Cancel operation to AvaTax.

File

./commerce_avatax.module, line 452
Calculate Sales Tax using AvaTax service from Avalara, Inc.

Code

function commerce_avatax_retrieve_sales_tax($order, $product_version, $commit = FALSE) {
  $sales_tax = array(
    'amount' => 0,
    'currency_code' => commerce_default_currency(),
    'data' => array(),
  );
  $use_mode = variable_get('commerce_avatax_use_mode');
  $avatax_microtime = variable_get('commerce_avatax_install_time');
  $doc_code_prefix = 'dc';
  $company_code = variable_get('commerce_avatax_' . $product_version . '_' . $use_mode . '_company', '');
  if (!$company_code) {
    drupal_set_message(t('AvaTax company code is not set.'), 'error');
    return FALSE;
  }

  // Sales Tax Shipping code.
  $shipcode = variable_get('commerce_avatax_shipcode', '');

  // 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.'), 'error');
    return FALSE;
  }

  // Get taxable address.
  $tax_address_profile = variable_get('commerce_avatax_tax_address', '');
  if ($tax_address_profile == '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'];
    }
  }
  elseif ($tax_address_profile == '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'];
    }
  }

  // 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_country = variable_get('commerce_avatax_primary_country', '');
  $primary_zip = variable_get('commerce_avatax_primary_zip', '');

  // Initialize sales tax exemption variable.
  $avatax_exemption_code = '';

  // Get User name or e-mail address.
  if ($order->uid == 0) {
    if ($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);
    if (variable_get('commerce_avatax_exemptions_status', 0)) {
      if (isset($user_data->avatax_exemption_code[LANGUAGE_NONE][0]['value'])) {
        $avatax_exemption_code = $user_data->avatax_exemption_code[LANGUAGE_NONE][0]['value'];
      }
    }
    $user_id = $user_data->name;
  }
  $doc_date = REQUEST_TIME;
  if ($commit) {
    foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {

      // If this line item is an AvaTax line item...
      if ($line_item_wrapper->type
        ->value() == 'avatax') {

        // Store its ID for later deletion and remove the reference from the
        // line item reference field.
        $doc_date = $line_item_wrapper->created
          ->value();
      }
    }
  }

  // Get currency code from the order.
  $avatax_total = $order_wrapper->commerce_order_total
    ->value();
  $currency_code = $avatax_total['currency_code'];

  // Construct arguments for AvaTax functions.
  $ava_args = compact('product_version', 'company_code', 'doc_code_prefix', 'doc_date', 'user_id', 'avatax_exemption_code', 'commit', 'currency_code', 'shipcode', 'use_mode', 'street1', 'street2', 'city', 'state', 'country', 'zip', 'primary_street1', 'primary_street2', 'primary_city', 'primary_state', 'primary_country', 'primary_zip');
  module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax_calc');

  // Get sales tax from AvaTax cloud service.
  $avatax_array = commerce_avatax_get_tax($order, $order_wrapper, $ava_args);

  // Check that there was a return from the tax request.
  if (!$avatax_array) {
    drupal_set_message(t("AvaTax did not calculate sales tax."), 'error');
    return FALSE;
  }
  $sales_tax = array(
    'amount' => $avatax_array['TotalTax'] * 100,
    'currency_code' => $currency_code,
    'data' => array(),
  );
  return $sales_tax;
}