You are here

function commerce_avatax_entity_update in Drupal Commerce Connector for AvaTax 7.3

Implements hook_entity_update().

File

./commerce_avatax_erp.inc, line 11
AvaTax Sales Order Processing - sample module.

Code

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