You are here

function commerce_avatax_form_alter in Drupal Commerce Connector for AvaTax 7.4

Same name and namespace in other branches
  1. 7.5 commerce_avatax.module \commerce_avatax_form_alter()

Implements hook_form_alter().

File

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

Code

function commerce_avatax_form_alter(&$form, &$form_state, $form_id) {
  $address_validation_enabled = variable_get('commerce_avatax_validate_address');
  if ($form_id == 'commerce_checkout_form_checkout' && $address_validation_enabled) {
    $form['#attributes']['class'][] = 'commerce_checkout_form';
    $form['buttons']['validate'] = array(
      '#type' => 'submit',
      '#value' => t('Validate'),
      '#attributes' => array(
        'class' => array(
          'address-validate-btn',
        ),
      ),
      '#ajax' => array(
        'wrapper' => 'address_validation_wrapper',
        'callback' => 'commerce_avatax_validate_shipping_address_ajax_callback',
        'progress' => array(
          'type' => 'none',
        ),
      ),
    );
    $form['address_validation_result'] = array(
      '#type' => 'container',
      '#prefix' => '<div id="address_validation_wrapper">',
      '#suffix' => '</div>',
    );
    $taxable_address = 'customer_profile_shipping';
    if (isset($form_state['values']['customer_profile_shipping']['commerce_customer_profile_copy'])) {
      if ($form_state['values']['customer_profile_shipping']['commerce_customer_profile_copy']) {
        $taxable_address = 'customer_profile_billing';
      }
    }
    elseif (isset($form['customer_profile_shipping']['commerce_customer_profile_copy']['#default_value'])) {
      if ($form['customer_profile_shipping']['commerce_customer_profile_copy']['#default_value']) {
        $taxable_address = 'customer_profile_billing';
      }
    }
    drupal_add_js(array(
      'commerce_avatax' => array(
        'commerce_avatax_address_validation_profile' => $taxable_address,
      ),
    ), 'setting');
    if (isset($form_state['values'][$taxable_address]['commerce_customer_address'][LANGUAGE_NONE][0])) {
      $address_value = $form_state['values'][$taxable_address]['commerce_customer_address'][LANGUAGE_NONE][0];
      $city = $address_value['locality'];
      $state = $address_value['administrative_area'];
      $country = $address_value['country'];
      $postal_code = $address_value['postal_code'];
      $line1 = $address_value['thoroughfare'];
      $line2 = isset($address_value['premise']) ? $address_value['premise'] : '';
      $enabled_countries = variable_get('commerce_avatax_address_validate_countries', array(
        'US',
      ));
      if (!is_array($enabled_countries)) {
        $enabled_countries = array(
          $enabled_countries,
        );
      }
      if (!in_array($country, $enabled_countries)) {
        return;
      }
      $args = compact('line1', 'line2', 'city', 'state', 'country', 'postal_code');
      $validated_address = commerce_avatax_validate_address($args);
      if ($validated_address === NULL) {
        return;
      }
      $res = commerce_avatax_address_compare($args, $validated_address);
      $form_state['address_validation_result'] = $res;
    }
    $validate_countries_client_side = TRUE;
    if (isset($form[$taxable_address]['commerce_customer_address'][LANGUAGE_NONE][0]['country']['#access']) && $form[$taxable_address]['commerce_customer_address'][LANGUAGE_NONE][0]['country']['#access'] == FALSE) {
      $validate_countries_client_side = FALSE;
    }
    drupal_add_library('system', 'ui.dialog');
    $countries = variable_get('commerce_avatax_address_validate_countries', array(
      'US',
    ));
    if (!is_array($countries)) {
      $countries = array(
        $countries,
      );
    }
    drupal_add_js(array(
      'commerce_avatax' => array(
        'commerce_avatax_autocomplete_postal_code' => variable_get('commerce_avatax_autocomplete_postal_code', TRUE),
        'commerce_avatax_address_validate_countries' => $countries,
        'commerce_avatax_address_do_country_validate' => $validate_countries_client_side,
      ),
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'commerce_avatax') . '/js/address_validate.js');
    drupal_add_css(drupal_get_path('module', 'commerce_avatax') . '/theme/commerce_avatax_checkout.css');
    $form['buttons']['validate']['#validate'][] = 'commerce_avatax_checkout_validate';
  }
  elseif ($form_id == 'commerce_order_ui_order_form') {
    $order = $form['#entity'];
    $tax_address_profile = variable_get('commerce_avatax_tax_address', '');
    $profile = NULL;
    $address = array();
    if ($tax_address_profile == 'Billing') {
      $form_state_var = 'commerce_customer_billing';
      $profile = $form[$form_state_var][LANGUAGE_NONE]['profiles'][0]['profile']['#value'];
      if (isset($profile->commerce_customer_address[LANGUAGE_NONE][0])) {
        $address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
      }
    }
    else {
      $form_state_var = 'commerce_customer_shipping';
      $profile = $form[$form_state_var][LANGUAGE_NONE]['profiles'][0]['profile']['#value'];
      if (isset($profile->commerce_customer_address[LANGUAGE_NONE][0])) {
        $address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
      }
    }
    $has_avatax_items = FALSE;
    if ($order->order_id) {
      $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
      foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
        if ($line_item_wrapper->type
          ->value() == 'avatax') {
          $has_avatax_items = TRUE;
        }
      }
    }
    $has_line_items = FALSE;
    if ($order->order_id) {
      $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
      if (count($order_wrapper->commerce_line_items) != 0) {
        $has_line_items = TRUE;
      }
    }
    $has_new_line_items = FALSE;
    if (isset($form_state['line_item_save_warning'])) {
      $has_new_line_items = TRUE;
    }
    $is_complete_address = FALSE;
    if (isset($profile->profile_id)) {
      if ($address['postal_code'] && $address['country'] && $address['locality'] && $address['administrative_area'] && $address['thoroughfare']) {
        $is_complete_address = TRUE;
      }
    }
    else {
      if (isset($form_state['input'][$form_state_var][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0])) {
        $address = $form_state['input'][$form_state_var][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0];
        if ($address['postal_code'] && $address['country'] && $address['locality'] && $address['administrative_area'] && $address['thoroughfare']) {
          $is_complete_address = TRUE;
        }
      }
    }
    $is_selected_state = FALSE;
    $avatax_states = variable_get('commerce_avatax_select_states', array());
    if (isset($profile->profile_id)) {
      if (!empty($avatax_states) && in_array($address['administrative_area'], $avatax_states)) {
        $is_selected_state = TRUE;
      }
      if (empty($avatax_states)) {
        $is_selected_state = TRUE;
      }
    }
    else {
      if (isset($form_state['input'][$form_state_var][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0])) {
        $address = $form_state['input'][$form_state_var][LANGUAGE_NONE]['profiles'][0]['commerce_customer_address'][LANGUAGE_NONE][0];
        if (!empty($avatax_states) && in_array($address['administrative_area'], $avatax_states)) {
          $is_selected_state = TRUE;
        }
        if (empty($avatax_states)) {
          $is_selected_state = TRUE;
        }
      }
    }
    if (!$order->order_id || !$has_line_items || $has_new_line_items || $has_avatax_items || !$is_complete_address || !$is_selected_state) {
      $form['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_type']['#ajax'] = array(
        'wrapper' => 'line-item-manager',
        'callback' => 'commerce_avatax_line_item_add_btn',
      );
      $form['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_add']['#prefix'] = '<div id="line_item_add_btn">';
      $form['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_add']['#suffix'] = '</div>';
      if (isset($form_state['input']['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_type']) && $form_state['input']['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_type'] == 'avatax') {
        if (!$order->order_id) {
          drupal_set_message(t('Please save this order to allocate an order # and then calculate sales tax.'), 'warning');
        }
        elseif (!$has_line_items) {
          drupal_set_message(t('You can not calculate sales tax on an order with no line items.'), 'warning');
        }
        elseif ($has_avatax_items) {
          drupal_set_message(t('An order may not have two sales tax lines.'), 'warning');
        }
        elseif ($has_new_line_items) {
          drupal_set_message(t('Please save the order before calculating sales tax.'), 'warning');
        }
        elseif (!$is_complete_address) {
          drupal_set_message(t('Please save a complete taxable address before calculating sales tax.'), 'warning');
        }
        else {
          drupal_set_message(t('State - @state - is not configured for sales tax calculations. Please correct address and save the order.', array(
            '@state' => $address['administrative_area'],
          )), 'warning');
        }
        $form['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_add']['#disabled'] = TRUE;
      }
      else {
        $form['commerce_line_items'][LANGUAGE_NONE]['actions']['line_item_add']['#disabled'] = FALSE;
      }
    }

    // Disable save line item if no sales tax calculated.
    if (isset($form['commerce_line_items'][LANGUAGE_NONE]['actions']['avatax_error'])) {
      $form['commerce_line_items'][LANGUAGE_NONE]['actions']['save_line_item']['#disabled'] = TRUE;
    }
  }
}