You are here

function commerce_avatax_get_tax in Drupal Commerce Connector for AvaTax 7.3

Same name in this branch
  1. 7.3 commerce_avatax_pro.inc \commerce_avatax_get_tax()
  2. 7.3 commerce_avatax_basic.inc \commerce_avatax_get_tax()
Same name and namespace in other branches
  1. 7.4 includes/commerce_avatax_calc.inc \commerce_avatax_get_tax()

Gets the tax amount for the order based on the delivery address.

Parameters

$order: The current order object.

$ava_args: An array containing from & to delivery details.

Return value

FALSE if the tax calculation failed. An array containing tax amount, taxable amount, total order amount - if success

1 call to commerce_avatax_get_tax()
commerce_avatax_retrieve_sales_tax in ./commerce_avatax.module
AvaTax service: returns the sales tax amount as an array.

File

./commerce_avatax_pro.inc, line 23
Avalara GetTax amount.

Code

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('');

  // Pro mode configuration.
  $pro_mode = $ava_args['pro_mode'];

  // Return error if AvaTax Pro has been selected without being configured.
  if ($pro_mode == 'not_set') {
    drupal_set_message(filter_xss('Please refer module README for configuraion of module in Pro Mode.'), 'error');
    return FALSE;
  }
  if ($pro_mode == 'tax_code') {
    $i = 1;
    foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {

      // Enter the Drupal Commerce field name for the tax code data.
      $tax_field_name = 'not_set';

      // Return error if AvaTax Pro has been selected without $tax_field_name configured.
      if ($tax_field_name == 'not_set') {
        drupal_set_message(filter_xss('Selection of Avatax Pro in Item Code mode requires (line 85) $tax_field_name to be configured.'), 'error');
        return FALSE;
      }
      $line_item = $line_item_wrapper
        ->value();
      if (in_array($line_item->type, commerce_product_line_item_types())) {
        $tax_code = '';
        $product_id = $line_item->commerce_product['und'][0]['product_id'];
        $prod_data = commerce_product_load($product_id);
        $tax_code = $prod_data->field_sales_tax['und'][0]['value'];
        ${'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($tax_code);
        ${'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++;
      }
    }
  }
  if ($pro_mode == 'taxonomy_tax_code') {
    $i = 1;
    $coupon_value = 0;
    foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {

      // Enter the Drupal Commerce field name for the tax code data without the 'field_' .
      $tax_field_name = 'not_set';

      // Return error if AvaTax Pro has been selected without $tax_field_name configured.
      if ($tax_field_name == 'not_set') {
        drupal_set_message(filter_xss('Selection of Avatax Pro in Taxonomy mode requires (line 124) $tax_field_name to be configured.'), 'error');
        return FALSE;
      }
      $line_item = $line_item_wrapper
        ->value();
      if (in_array($line_item->type, commerce_product_line_item_types())) {
        $tax_code = '';
        $product_id = $line_item->commerce_product['und'][0]['product_id'];
        $prod_data = commerce_product_load($product_id);
        if ($prod_data->{'field_' . $tax_field_name}) {
          $tid = $prod_data->{'field_' . $tax_field_name}['und'][0]['tid'];
          $taxonomy_term = taxonomy_term_load($tid);
          $tax_code = $taxonomy_term->name;
        }
        ${'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($tax_code);
        ${'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++;
      }
    }
  }
  foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {
    $line_item = $line_item_wrapper
      ->value();
    if (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 Amt');
      ${'line' . $i}
        ->setTaxCode($tax_code);
      ${'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 Amt');
      ${'line' . $i}
        ->setTaxCode($tax_code);
      ${'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) {
      $tax_data = 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++;
      }
      $tax_data['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 $tax_data;
}