You are here

commerce_avatax.module in Drupal Commerce Connector for AvaTax 8

Provides module functionality.

File

commerce_avatax.module
View source
<?php

/**
 * @file
 * Provides module functionality.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function commerce_avatax_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() === 'commerce_store') {
    $fields['avatax_company_code'] = BaseFieldDefinition::create('string')
      ->setLabel(t('AvaTax company code'))
      ->setDescription(t('The company code to send to AvaTax when calculating taxes.'))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 0,
    ]);
  }
  if ($entity_type
    ->id() === 'commerce_product_variation') {
    $fields['avatax_tax_code'] = BaseFieldDefinition::create('string')
      ->setLabel(t('AvaTax tax code'))
      ->setDescription(t('Browse codes in Avalara\'s <a href="@tax-code-finder" target="_blank">tax code finder</a>.', [
      '@tax-code-finder' => 'https://taxcode.avatax.avalara.com',
    ]))
      ->setSetting('display_description', TRUE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 0,
    ]);
  }
  if ($entity_type
    ->id() === 'user') {
    $fields['avatax_customer_code'] = BaseFieldDefinition::create('string')
      ->setLabel(t('AvaTax customer code'))
      ->setDisplayConfigurable('form', TRUE)
      ->setSetting('max_length', 50)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 0,
    ]);
    $fields['avatax_tax_exemption_number'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Tax Exemption number'))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 0,
    ]);
    $fields['avatax_tax_exemption_type'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Tax Exemption type'))
      ->setSetting('allowed_values_function', [
      '\\Drupal\\commerce_avatax\\Avatax',
      'getExemptionTypes',
    ])
      ->setDisplayOptions('form', [
      'type' => 'options_select',
      'weight' => 4,
    ])
      ->setDisplayConfigurable('form', TRUE);
  }
  return $fields;
}

/**
 * Implements hook_entity_field_access().
 */
function commerce_avatax_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if (in_array($field_definition
    ->getName(), [
    'avatax_customer_code',
    'avatax_tax_exemption_type',
    'avatax_tax_exemption_number',
  ], TRUE) && $operation == 'edit') {
    return AccessResult::forbiddenIf(!$account
      ->hasPermission('configure avatax exemptions'));
  }
  return AccessResult::neutral();
}

/**
 * Implements hook_commerce_inline_form_alter().
 */
function commerce_avatax_commerce_inline_form_customer_profile_alter(array &$inline_form, FormStateInterface $form_state) {
  $customer_profile_alter = \Drupal::service('commerce_avatax.customer_profile_alter');
  if ($customer_profile_alter
    ->applies($inline_form, $form_state)) {
    $customer_profile_alter
      ->alter($inline_form, $form_state);
  }
}

/**
 * Implements hook_theme().
 */
function commerce_avatax_theme() {
  return [
    'avatax_address' => [
      'variables' => [
        'original' => [],
        'suggestion' => [],
        'fields' => [],
      ],
    ],
  ];
}