You are here

function commerce_avatax_ui_exemption_field in Drupal Commerce Connector for AvaTax 7.4

Submit callback for adding/removing exemption code field to the user profile.

1 string reference to 'commerce_avatax_ui_exemption_field'
commerce_avatax_ui_admin_settings in includes/commerce_avatax_ui.admin.inc
Admin settings menu callback.

File

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

Code

function commerce_avatax_ui_exemption_field($form, &$form_state) {
  $exemption_status = isset($form_state['values']['commerce_avatax_exemptions_status']) ? $form_state['values']['commerce_avatax_exemptions_status'] : FALSE;
  if ($exemption_status) {

    // Exemption status is YES.
    // Create the field and instance if they do not exist.
    $field = field_info_field('avatax_exemption_code');
    if (!$field) {
      field_create_field(array(
        'cardinality' => 1,
        'field_name' => 'avatax_exemption_code',
        'settings' => array(
          'allowed_values' => array(
            'E' => 'Charitable or benevolent org',
            'H' => 'Commercial agricultural production',
            'J' => 'Direct pay permit',
            'K' => 'Direct mail',
            'A' => 'Federal government',
            'D' => 'Foreign diplomat',
            'I' => 'Industrial production / manufacturer',
            'N' => 'Local government',
            'B' => 'State government',
            'C' => 'Tribe / Status Indian / Indian Band',
            'F' => 'Religious or educational org',
            'G' => 'Resale',
            'L' => 'Other',
          ),
        ),
        'type' => 'list_text',
      ));
    }
    $instance = field_info_instance('user', 'avatax_exemption_code', 'user');
    if (!$instance) {
      field_create_instance(array(
        'bundle' => 'user',
        'display' => array(
          'default' => array(
            'label' => 'above',
            'module' => 'list',
            'settings' => array(),
            'type' => 'list_default',
            'weight' => 0,
          ),
        ),
        'entity_type' => 'user',
        'field_name' => 'avatax_exemption_code',
        'label' => 'AvaTax Exemption Code',
        'required' => 0,
        'settings' => array(
          'user_register_form' => 0,
        ),
        'widget' => array(
          'active' => 1,
          'module' => 'options',
          'settings' => array(),
          'type' => 'options_select',
          'weight' => 7,
        ),
      ));
    }
  }
  else {

    // Exemption status is NO.
    // If the previous status is YES, and user has asked to delete the fields.
    $previous_state = variable_get('commerce_avatax_exemptions_status');
    if ($previous_state) {
      if ($form_state['values']['commerce_avatax_exemptions_delete_field']) {
        $instance = field_info_instance('user', 'avatax_exemption_code', 'user');
        if ($instance) {
          field_delete_instance($instance);
        }
        $field = field_info_field('avatax_exemption_code');
        if ($field) {
          field_delete_field('avatax_exemption_code');
        }
      }
    }
  }
}