You are here

function commerce_avatax_ui_admin_settings in Drupal Commerce Connector for AvaTax 7.4

Same name and namespace in other branches
  1. 7.3 includes/commerce_avatax_ui.admin.inc \commerce_avatax_ui_admin_settings()

Admin settings menu callback.

1 string reference to 'commerce_avatax_ui_admin_settings'
commerce_avatax_ui_menu in ./commerce_avatax_ui.module
Implements hook_menu().

File

includes/commerce_avatax_ui.admin.inc, line 18
Admin settings for commerce_avatax.

Code

function commerce_avatax_ui_admin_settings($form, &$form_state) {
  include_once DRUPAL_ROOT . '/includes/locale.inc';
  $product_version = variable_get('commerce_avatax_product_version', COMMERCE_AVATAX_BASIC_VERSION);
  if (isset($form_state['values']['commerce_avatax_product_version']) && $form_state['values']['commerce_avatax_product_version']) {
    $product_version = $form_state['values']['commerce_avatax_product_version'];
  }
  $avatax_use_modes = array(
    COMMERCE_AVATAX_DEVELOPMENT_MODE => t('Development'),
    COMMERCE_AVATAX_PRODUCTION_MODE => t('Production'),
  );
  $use_mode = variable_get('commerce_avatax_use_mode', COMMERCE_AVATAX_DEVELOPMENT_MODE);
  if (isset($form_state['values']['commerce_avatax_use_mode']) && $form_state['values']['commerce_avatax_use_mode']) {
    $use_mode = $form_state['values']['commerce_avatax_use_mode'];
  }

  // Set AvaTax license details.
  $form['commerce_avatax_product_version'] = array(
    '#title' => t('Select AvaTax Version'),
    '#type' => 'select',
    '#options' => array(
      COMMERCE_AVATAX_BASIC_VERSION => t('AvaTax Basic'),
      COMMERCE_AVATAX_PRO_VERSION => t('AvaTax Pro'),
    ),
    '#default_value' => variable_get('commerce_avatax_product_version', COMMERCE_AVATAX_BASIC_VERSION),
    '#ajax' => array(
      'callback' => 'commerce_avatax_ui_update_form_options',
      'wrapper' => 'commerce_avatax_options',
    ),
  );
  $form['options'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="commerce_avatax_options">',
    '#suffix' => '</div>',
    '#tree' => FALSE,
  );

  // Configure sales tax description to be shown to users.
  $form['options']['commerce_avatax_tax_description'] = array(
    '#title' => t('Sales Tax Description'),
    '#description' => t('The Sales Tax description to be displayed on the order check out form'),
    '#type' => 'textfield',
    '#default_value' => variable_get('commerce_avatax_tax_description', 'Sales tax'),
  );
  $form['options']['shipping'] = array(
    '#type' => 'fieldset',
    '#title' => t('Shipping settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => FALSE,
  );
  $states = array(
    'AL' => t('Alabama'),
    'AK' => t('Alaska'),
    'AZ' => t('Arizona'),
    'AR' => t('Arkansas'),
    'CA' => t('California'),
    'CO' => t('Colorado'),
    'CT' => t('Connecticut'),
    'DE' => t('Delaware'),
    'DC' => t('District Of Columbia'),
    'FL' => t('Florida'),
    'GA' => t('Georgia'),
    'HI' => t('Hawaii'),
    'ID' => t('Idaho'),
    'IL' => t('Illinois'),
    'IN' => t('Indiana'),
    'IA' => t('Iowa'),
    'KS' => t('Kansas'),
    'KY' => t('Kentucky'),
    'LA' => t('Louisiana'),
    'ME' => t('Maine'),
    'MD' => t('Maryland'),
    'MA' => t('Massachusetts'),
    'MI' => t('Michigan'),
    'MN' => t('Minnesota'),
    'MS' => t('Mississippi'),
    'MO' => t('Missouri'),
    'MT' => t('Montana'),
    'NE' => t('Nebraska'),
    'NV' => t('Nevada'),
    'NH' => t('New Hampshire'),
    'NJ' => t('New Jersey'),
    'NM' => t('New Mexico'),
    'NY' => t('New York'),
    'NC' => t('North Carolina'),
    'ND' => t('North Dakota'),
    'OH' => t('Ohio'),
    'OK' => t('Oklahoma'),
    'OR' => t('Oregon'),
    'PA' => t('Pennsylvania'),
    'RI' => t('Rhode Island'),
    'SC' => t('South Carolina'),
    'SD' => t('South Dakota'),
    'TN' => t('Tennessee'),
    'TX' => t('Texas'),
    'UT' => t('Utah'),
    'VT' => t('Vermont'),
    'VA' => t('Virginia'),
    'WA' => t('Washington'),
    'WV' => t('West Virginia'),
    'WI' => t('Wisconsin'),
    'WY' => t('Wyoming'),
    'AA' => t('Armed Forces (Americas)'),
    'AE' => t('Armed Forces (Europe, Canada, Middle East, Africa)'),
    'AP' => t('Armed Forces (Pacific)'),
    'AS' => t('American Samoa'),
    'FM' => t('Federated States of Micronesia'),
    'GU' => t('Guam'),
    'MH' => t('Marshall Islands'),
    'MP' => t('Northern Mariana Islands'),
    'PW' => t('Palau'),
    'PR' => t('Puerto Rico'),
    'VI' => t('Virgin Islands'),
  );

  // Select the states that will use AvaTax to calculate the sales tax amount.
  $form['options']['shipping']['commerce_avatax_select_states'] = array(
    '#title' => t('AvaTax States'),
    '#description' => t('Select States - Leave blank for all states'),
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => $states,
    '#default_value' => variable_get('commerce_avatax_select_states', array()),
  );

  // Set Shipping Tax code to be used by AvaTax.
  $form['options']['shipping']['commerce_avatax_shipcode'] = array(
    '#title' => t('Shipping Tax Code'),
    '#description' => t('The Sales Tax code to be used for Shipping'),
    '#type' => 'textfield',
    '#default_value' => variable_get('commerce_avatax_shipcode', 'FR020100'),
  );
  $address_options = array(
    'Billing' => t('Billing'),
  );
  if (module_exists('commerce_shipping')) {
    $address_options['Shipping'] = t('Shipping');
  }

  // Configure address to use for sales tax.
  $form['options']['shipping']['commerce_avatax_tax_address'] = array(
    '#title' => t('Destination Address to Use for Sales Tax'),
    '#description' => t('Select Shipping address if you have installed Drupal Commerce Shipping Module'),
    '#type' => 'select',
    '#options' => $address_options,
    '#default_value' => variable_get('commerce_avatax_tax_address', 'Shipping'),
    '#ajax' => array(
      'callback' => 'commerce_avatax_ui_tax_address_ajax_callback',
      'wrapper' => 'address_validation_fieldset_wrapper',
    ),
  );

  // Set Street, City, State and Zip for Primary Business Office Location.
  $form['options']['shipping']['commerce_avatax_account_name'] = array(
    '#title' => t('Business or Company Name'),
    '#description' => t('Registered Name of Company or Business'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('commerce_avatax_account_name', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_street1'] = array(
    '#title' => t('Primary Business Street 1'),
    '#description' => t('The Primary Street 1 your business is located in'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('commerce_avatax_primary_street1', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_street2'] = array(
    '#title' => t('Primary Business Street 2'),
    '#description' => t('The Primary Street 2 your business is located in'),
    '#type' => 'textfield',
    '#default_value' => variable_get('commerce_avatax_primary_street2', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_city'] = array(
    '#title' => t('Primary Business City'),
    '#description' => t('The Primary City your business is located in'),
    '#required' => TRUE,
    '#type' => 'textfield',
    '#default_value' => variable_get('commerce_avatax_primary_city', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_state'] = array(
    '#title' => t('Primary Business State'),
    '#description' => t('The Primary State your business is located in'),
    '#type' => 'select',
    '#options' => $states,
    '#default_value' => variable_get('commerce_avatax_primary_state', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_country'] = array(
    '#title' => t('Primary Business Country'),
    '#description' => t('The primary country your business is located in'),
    '#type' => 'select',
    '#required' => TRUE,
    '#options' => country_get_list(),
    '#default_value' => variable_get('commerce_avatax_primary_country', ''),
  );
  $form['options']['shipping']['commerce_avatax_primary_zip'] = array(
    '#title' => t('Primary Business Zip'),
    '#description' => t('The Primary Zip Code your business is located in. NB - Must be a Valid 5 digit zip'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('commerce_avatax_primary_zip', ''),
  );
  $form['options']['address_validate_wrapper'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="address_validation_fieldset_wrapper">',
    '#suffix' => '</div>',
  );
  $tax_address = variable_get('commerce_avatax_tax_address', 'Shipping');
  if (isset($form_state['values']['commerce_avatax_tax_address'])) {
    $tax_address = $form_state['values']['commerce_avatax_tax_address'];
  }
  if ($tax_address == 'Shipping') {
    $form['options']['address_validate_wrapper']['address_validate'] = array(
      '#type' => 'fieldset',
      '#title' => t('Address validation'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => FALSE,
    );
    $form['options']['address_validate_wrapper']['address_validate']['commerce_avatax_validate_address'] = array(
      '#type' => 'checkbox',
      '#title' => t('Validate checkout shipping address'),
      '#default_value' => variable_get('commerce_avatax_validate_address', TRUE),
      '#ajax' => array(
        'callback' => 'commerce_avatax_ui_address_validate_ajax_callback',
        'wrapper' => 'commerce_avatax_address_validate_options',
      ),
    );
    $form['options']['address_validate_wrapper']['address_validate']['config'] = array(
      '#type' => 'container',
      '#tree' => FALSE,
      '#prefix' => '<div id="commerce_avatax_address_validate_options">',
      '#suffix' => '</div>',
    );
    $validate_address = variable_get('commerce_avatax_validate_address', TRUE);
    if (isset($form_state['values']['commerce_avatax_validate_address'])) {
      $validate_address = $form_state['values']['commerce_avatax_validate_address'];
    }
    if ($validate_address) {
      $countries = variable_get('commerce_avatax_address_validate_countries', array(
        'US',
      ));
      if (!is_array($countries)) {
        $countries = array(
          $countries,
        );
      }
      $form['options']['address_validate_wrapper']['address_validate']['config']['commerce_avatax_address_validate_countries'] = array(
        '#type' => 'select',
        '#title' => t('Countries'),
        '#description' => t('Countries to validate addresses?'),
        '#options' => array(
          'US' => t('United States'),
        ),
        '#default_value' => $countries,
        '#required' => TRUE,
      );
      $form['options']['address_validate_wrapper']['address_validate']['config']['commerce_avatax_address_postal_code'] = array(
        '#type' => 'checkbox',
        '#title' => t('Match on postal code'),
        '#description' => t('Postal codes are 9 digits, but most people enter the first 5 digits, do you want AvaTax to match all 9 digits?'),
        '#default_value' => variable_get('commerce_avatax_address_postal_code', TRUE),
      );
      $form['options']['address_validate_wrapper']['address_validate']['config']['commerce_avatax_autocomplete_postal_code'] = array(
        '#type' => 'checkbox',
        '#title' => t('Auto complete 5 digit postal code to 9 digits'),
        '#description' => t('Automatically insert the 9 digit postal code provided by AvaTax'),
        '#default_value' => variable_get('commerce_avatax_autocomplete_postal_code', TRUE),
      );
    }
  }
  $company_default = variable_get('commerce_avatax_' . $product_version . '_' . $use_mode . '_company');
  $account_default = variable_get('commerce_avatax_' . $product_version . '_' . $use_mode . '_account');
  if (isset($form_state['clicked_button']['#value']) && isset($form_state['account_no']) && !empty($form_state['account_no'])) {
    $account_default = $form_state['account_no'];
  }
  $license_default = variable_get('commerce_avatax_' . $product_version . '_' . $use_mode . '_license');
  if (isset($form_state['clicked_button']['#value']) && isset($form_state['license_key']) && !empty($form_state['license_key'])) {
    $license_default = $form_state['license_key'];
  }
  if ($product_version == COMMERCE_AVATAX_BASIC_VERSION || $product_version == COMMERCE_AVATAX_PRO_VERSION) {
    $form['options']['credentials'] = array(
      '#type' => 'fieldset',
      '#title' => t('AvaTax Credentials'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => FALSE,
    );
    $form['options']['credentials']['commerce_avatax_use_mode'] = array(
      '#title' => t('AvaTax Mode'),
      '#description' => t('Only select Production after completing the GO LIVE process with Avalara'),
      '#type' => 'select',
      '#options' => $avatax_use_modes,
      '#default_value' => $use_mode,
      '#ajax' => array(
        'callback' => 'commerce_avatax_ui_ajax_mode_credentials',
        'wrapper' => 'commerce_avatax_credentials_text_fields',
      ),
    );
    $form['options']['credentials']['fields'] = array(
      '#type' => 'container',
      '#prefix' => '<div id="commerce_avatax_credentials_text_fields">',
      '#suffix' => '</div>',
    );
    $form['options']['credentials']['fields']['commerce_avatax_' . $product_version . '_' . $use_mode . '_company'] = array(
      '#title' => t('Company Code'),
      '#description' => t('Enter the Company Code for your AvaTax version'),
      '#type' => 'textfield',
      '#default_value' => $company_default,
      '#required' => TRUE,
    );
    $form['options']['credentials']['fields']['commerce_avatax_' . $product_version . '_' . $use_mode . '_account'] = array(
      '#title' => t('@mode Account number', array(
        '@mode' => $use_mode,
      )),
      '#type' => 'textfield',
      '#default_value' => $account_default,
      '#required' => TRUE,
    );
    $form['options']['credentials']['fields']['commerce_avatax_' . $product_version . '_' . $use_mode . '_license'] = array(
      '#title' => t('@mode License key', array(
        '@mode' => $use_mode,
      )),
      '#type' => 'textfield',
      '#default_value' => $license_default,
      '#required' => TRUE,
    );
  }
  $form['options']['credentials']['fields']['validate_btn'] = array(
    '#name' => 'commerce_avatax_ui_credentials_validator_button',
    '#type' => 'button',
    '#value' => t('Validate credentials'),
    '#ajax' => array(
      'callback' => 'commerce_avatax_ui_credentials_validator_callback',
      'wrapper' => 'credentials_fields_validator_wrapper',
    ),
  );
  $form['options']['credentials']['fields']['validator_wrapper'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="credentials_fields_validator_wrapper">',
    '#suffix' => '</div>',
  );
  if (isset($form_state['clicked_button']['#name']) == 'commerce_avatax_ui_credentials_validator_button') {
    if ($form_state['values']['commerce_avatax_primary_street1'] && $form_state['values']['commerce_avatax_primary_city'] && $form_state['values']['commerce_avatax_primary_state'] && $form_state['values']['commerce_avatax_primary_zip']) {
      $validated = commerce_avatax_ui_admin_form_validate_credentials($form, $form_state);
      $form['options']['credentials']['fields']['validator_wrapper']['_validation_message'] = array(
        '#type' => 'item',
        '#markup' => '<p>' . $validated[1] . '</p>',
      );
    }
    else {
      $form['options']['credentials']['validator_wrapper']['_validation_message'] = array(
        '#type' => 'item',
        '#markup' => '<p>' . t('Please complete the required shipping address fields.') . '</p>',
      );
    }
  }
  if ($product_version == COMMERCE_AVATAX_BASIC_VERSION || $product_version == COMMERCE_AVATAX_PRO_VERSION) {
    $form['options']['erp'] = array(
      '#type' => 'fieldset',
      '#title' => t('Sales Order Processing'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['options']['erp']['commerce_avatax_erp_status'] = array(
      '#title' => t('Automatic updates to AvaTax doc status'),
      '#description' => t('Select "Yes" to enable automatic updates to AvaTax doc status'),
      '#type' => 'radios',
      '#options' => array(
        '0' => t('No'),
        '1' => t('Yes'),
      ),
      '#default_value' => variable_get('commerce_avatax_erp_status', '0'),
      '#ajax' => array(
        'callback' => 'commerce_avatax_ui_ajax_rules_options',
        'wrapper' => 'commerce_avatax_erp_rules',
      ),
    );
    $form['options']['erp']['rules'] = array(
      '#type' => 'container',
      '#tree' => FALSE,
      '#prefix' => '<div id="commerce_avatax_erp_rules">',
      '#suffix' => '</div>',
    );
    $rules = rules_config_load_multiple(array(
      'commerce_avatax_sop_commit',
      'commerce_avatax_sop_cancel',
      'commerce_avatax_sop_cancel_on_delete',
    ));
    if (!empty($rules)) {
      $erp_form_value = 0;
      if (isset($form_state['values']['commerce_avatax_erp_status'])) {
        $erp_form_value = $form_state['values']['commerce_avatax_erp_status'];
      }
      $erp_config_value = variable_get('commerce_avatax_erp_status', 0);
      if ($erp_form_value || $erp_config_value) {
        $form['options']['erp']['rules']['overwrite_rules'] = array(
          '#type' => 'checkbox',
          '#title' => t('Overwrite existing Sales Order Processing Rules'),
        );
      }
      if ($erp_config_value && !$erp_form_value) {
        $form['options']['erp']['rules']['delete_rules'] = array(
          '#type' => 'checkbox',
          '#title' => t('Delete Sales Order Processing Rules.'),
        );
      }
    }
  }
  if ($product_version == COMMERCE_AVATAX_BASIC_VERSION || $product_version == COMMERCE_AVATAX_PRO_VERSION) {
    $form['options']['exemptions'] = array(
      '#type' => 'fieldset',
      '#title' => t('AvaTax Exemption settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => FALSE,
    );
    $form['options']['exemptions']['commerce_avatax_exemptions_status'] = array(
      '#title' => t('Administer Sales Tax Exemptions'),
      '#description' => t('Select "Yes" to enter sales tax exemption codes for registered users'),
      '#type' => 'radios',
      '#options' => array(
        '0' => t('No'),
        '1' => t('Yes'),
      ),
      '#default_value' => variable_get('commerce_avatax_exemptions_status', 0),
    );
    if (variable_get('commerce_avatax_exemptions_status', 0)) {
      $form['options']['exemptions']['commerce_avatax_exemptions_delete_field'] = array(
        '#type' => 'checkbox',
        '#title' => t('Delete exemption code field from user profile.'),
        '#description' => t('WARNING: This action can not be undone. All user data will be lost.'),
        '#states' => array(
          'visible' => array(
            ':input[name="commerce_avatax_exemptions_status"]' => array(
              'value' => 0,
            ),
          ),
        ),
      );
    }
  }
  if ($product_version == COMMERCE_AVATAX_PRO_VERSION) {
    $form['options']['pro'] = array(
      '#type' => 'fieldset',
      '#title' => t('AvaTax Pro settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => FALSE,
    );
    $product_types = commerce_product_types();
    foreach ($product_types as $product_type => $product_type_info) {
      $product_types[$product_type] = $product_type_info['name'];
    }
    $form['options']['pro']['commerce_avatax_product_types'] = array(
      '#title' => t('Product types'),
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => $product_types,
      '#default_value' => variable_get('commerce_avatax_product_types', array()),
      '#description' => t('Which product types should have their taxes calculated by AvaTax? Leave empty to enable for all product types.'),
    );
  }
  $form['#validate'][] = 'commerce_avatax_ui_validate_address_validation_settings';
  $form['#submit'][] = 'commerce_avatax_ui_add_pro_tax_code_fields';
  $form['#submit'][] = 'commerce_avatax_ui_add_erp_rules';
  $form['#submit'][] = 'commerce_avatax_ui_exemption_field';
  return system_settings_form($form);
}