You are here

commerce_avatax_exemptions.module in Drupal Commerce Connector for AvaTax 7.4

Commerce AvaTax Exemptions.

File

modules/commerce_avatax_exemptions/commerce_avatax_exemptions.module
View source
<?php

/**
 * @file
 * Commerce AvaTax Exemptions.
 */

/**
 * Implements hook_commerce_checkout_pane_info().
 */
function commerce_avatax_exemptions_commerce_checkout_pane_info() {
  $panes['commerce_avatax_exemptions'] = array(
    'title' => t('Sales tax'),
    'page' => 'checkout',
    'weight' => 5,
    'file' => 'includes/commerce_avatax_exemptions.checkout_pane.inc',
    'base' => 'commerce_avatax_exemptions_pane',
  );
  return $panes;
}

/**
 * Implements hook_form_alter().
 */
function commerce_avatax_exemptions_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'commerce_checkout_form_checkout') {
    $order = $form_state['order'];
    $account = user_load($order->uid);
    $role_intersect = array_intersect(variable_get('commerce_avatax_exemptions_roles', array(
      1,
    )), array_keys($account->roles));
    if (empty($role_intersect)) {
      return;
    }
    $form['#prefix'] = '<div id="checkout_form">';
    $form['#suffix'] = '</div>';
    $profile_type = strtolower(variable_get('commerce_avatax_tax_address', 'Shipping'));
    if (isset($form['customer_profile_shipping']['commerce_customer_profile_copy']['#default_value'])) {
      if ($form['customer_profile_shipping']['commerce_customer_profile_copy']['#default_value']) {
        $profile_type = 'billing';
      }
    }
    if (isset($form['customer_profile_' . $profile_type]['commerce_customer_address'][LANGUAGE_NONE][0]['locality_block']['administrative_area'])) {
      $form['customer_profile_' . $profile_type]['commerce_customer_address'][LANGUAGE_NONE][0]['locality_block']['administrative_area']['#ajax'] = array(
        'wrapper' => 'checkout_form',
        'callback' => 'commerce_avatax_exemptions_checkout_form_ajax_callback',
      );
    }
    $state = '';
    if (isset($form['customer_profile_' . $profile_type]['commerce_customer_address'][LANGUAGE_NONE][0])) {
      $state = $form['customer_profile_' . $profile_type]['commerce_customer_address'][LANGUAGE_NONE][0]['#address']['administrative_area'];
    }
    $states = variable_get('commerce_avatax_exemptions_states', array());
    if (!$states || $state && in_array($state, $states)) {
      $form['commerce_avatax_exemptions']['#access'] = TRUE;
    }
  }
}

/**
 * Ajax callback for loading exemptions pane.
 */
function commerce_avatax_exemptions_checkout_form_ajax_callback($form, &$form_state) {
  return $form;
}