You are here

commerce_avatax_erp.inc in Drupal Commerce Connector for AvaTax 7.3

AvaTax Sales Order Processing - sample module.

File

commerce_avatax_erp.inc
View source
<?php

/**
 * @file
 * AvaTax Sales Order Processing - sample module.
 */

/**
 * Implements hook_entity_update().
 */
function commerce_avatax_entity_update($entity, $type) {
  if ($type == 'commerce_order' && isset($entity->original)) {
    if ($entity->original->status != $entity->status && $entity->status == 'completed') {

      // Build order wrapper
      $order_wrapper = entity_metadata_wrapper('commerce_order', $entity);

      // Get state for sales tax address
      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();
          $state = $billing_address['administrative_area'];
        }
      }
      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();
          $state = $shipping_address['administrative_area'];
        }
      }

      // 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 FALSE;
        }
      }

      // Required for Avalara Scripts.
      $library = libraries_load('avalara_avatax');
      if (!$library || empty($library['loaded'])) {
        drupal_set_message('Please install Avatax library.', 'warning');
        return FALSE;
      }

      // Get Company code and Company Use Mode
      $company_code = variable_get('commerce_avatax_company_code', '');
      $use_mode = variable_get('commerce_avatax_use_mode', '');
      $client = new TaxServiceSoap($use_mode);
      $request = new CommitTaxRequest();

      // Locate Document by Invoice Number.
      $request
        ->setDocCode('dc-' . $entity->order_id . '');
      $request
        ->setDocType('SalesInvoice');
      $request
        ->setCompanyCode($company_code);
      try {
        $result = $client
          ->commitTax($request);
        if ($result
          ->getResultCode() != SeverityLevel::$Success) {
          foreach ($result
            ->getMessages() as $msg) {
            drupal_set_message(t('AvaTax error: ' . $msg
              ->getName() . " - " . $msg
              ->getSummary() . " - " . $msg
              ->getDetails() . ''), 'error');
          }
        }
      } catch (SoapFault $exception) {
        $msg = 'SOAP Exception: ';
        if ($exception) {
          $msg .= $exception->faultstring;
        }
        drupal_set_message(filter_xss('AvaTax message is: ' . $msg . '.'), 'error');
        drupal_set_message(filter_xss('AvaTax last request is: ' . $client
          ->__getLastRequest() . ''), 'error');
        drupal_set_message(filter_xss('AvaTax last response is: ' . $client
          ->__getLastResponse() . ''), 'error');
      }
    }
  }
}

/**
 * Implements hook_payment_order_paid_in_full().
 */
function commerce_avatax_commerce_payment_order_paid_in_full($transaction) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $transaction);

  // Get state for sales tax address
  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();
      $state = $billing_address['administrative_area'];
    }
  }
  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();
      $state = $shipping_address['administrative_area'];
    }
  }

  // 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 FALSE;
    }
  }

  // Required for Avalara Scripts.
  $library = libraries_load('avalara_avatax');
  if (!$library || empty($library['loaded'])) {
    drupal_set_message('Please install Avatax library.', 'warning');
    return FALSE;
  }

  // Get Company code and Company Use Mode
  $company_code = variable_get('commerce_avatax_company_code', '');
  $use_mode = variable_get('commerce_avatax_use_mode', '');

  // Get sales tax amount from avatax line item
  foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {
    $line_item = $line_item_wrapper
      ->value();
    if ($line_item->type == 'avatax') {
      $tax = $line_item->commerce_unit_price['und']['0']['amount'] / 100;
    }
  }
  $ava_order_total = $transaction->commerce_order_total['und']['0']['amount'] / 100 - $tax;
  $client = new TaxServiceSoap($use_mode);
  $request = new PostTaxRequest();

  // Locate Document by Invoice Number.
  $request
    ->setDocCode('dc-' . $transaction->order_id . '');
  $date_time = new DateTime();
  $request
    ->setDocDate(date_format($date_time, "Y-m-d"));
  $request
    ->setDocType('SalesInvoice');
  $request
    ->setCompanyCode($company_code);
  $request
    ->setTotalAmount($ava_order_total);
  $request
    ->setTotalTax($tax);
  try {
    $result = $client
      ->postTax($request);
    if ($result
      ->getResultCode() != SeverityLevel::$Success) {
      foreach ($result
        ->getMessages() as $msg) {
        drupal_set_message(t('AvaTax error: ' . $msg
          ->getName() . " - " . $msg
          ->getSummary() . " - " . $msg
          ->getDetails() . ''), 'error');
      }
    }
  } catch (SoapFault $exception) {
    $msg = 'SOAP Exception: ';
    if ($exception) {
      $msg .= $exception->faultstring;
    }
    drupal_set_message(filter_xss('AvaTax message is: ' . $msg . '.'), 'error');
    drupal_set_message(filter_xss('AvaTax last request is: ' . $client
      ->__getLastRequest() . ''), 'error');
    drupal_set_message(filter_xss('AvaTax last response is: ' . $client
      ->__getLastResponse() . ''), 'error');
  }
}
function commerce_avatax_void_order($commerce_order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $commerce_order);

  // Get state for sales tax address
  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();
      $state = $billing_address['administrative_area'];
    }
  }
  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();
      $state = $shipping_address['administrative_area'];
    }
  }

  // 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 FALSE;
    }
  }

  // Required for Avalara Scripts.
  $library = libraries_load('avalara_avatax');
  if (!$library || empty($library['loaded'])) {
    drupal_set_message('Please install Avatax library.', 'warning');
    return FALSE;
  }

  // Get Company code and Company Use Mode
  $company_code = variable_get('commerce_avatax_company_code', '');
  $use_mode = variable_get('commerce_avatax_use_mode', '');
  $client = new TaxServiceSoap($use_mode);
  $request = new CancelTaxRequest();

  // Locate Document by Invoice Number.
  $request
    ->setDocCode('dc-' . $commerce_order->order_id . '');
  $request
    ->setDocType('SalesInvoice');
  $request
    ->setCompanyCode($company_code);

  // Set type of cancel to delete.
  $code = CancelCode::$DocVoided;
  $request
    ->setCancelCode($code);
  try {
    $result = $client
      ->cancelTax($request);
    if ($result
      ->getResultCode() != SeverityLevel::$Success) {
      foreach ($result
        ->getMessages() as $msg) {
        drupal_set_message(t('AvaTax error: ' . $msg
          ->getName() . " - " . $msg
          ->getSummary() . " - " . $msg
          ->getDetails() . ''), 'error');
      }
    }
  } catch (SoapFault $exception) {
    $msg = 'SOAP Exception: ';
    if ($exception) {
      $msg .= $exception->faultstring;
    }
    drupal_set_message(filter_xss('AvaTax message is: ' . $msg . '.'), 'error');
    drupal_set_message(filter_xss('AvaTax last request is: ' . $client
      ->__getLastRequest() . ''), 'error');
    drupal_set_message(filter_xss('AvaTax last response is: ' . $client
      ->__getLastResponse() . ''), 'error');
  }
}

Functions