View source
<?php
define('COMMERCE_AVATAX_PRODUCTION_MODE', 'prod');
define('COMMERCE_AVATAX_DEVELOPMENT_MODE', 'dev');
define('COMMERCE_AVATAX_VAR_PREFIX', 'commerce_avatax_');
define('COMMERCE_AVATAX_ACCOUNT_NUMBER', 'account_number');
define('COMMERCE_AVATAX_LICENSE_KEY', 'license_key');
define('COMMERCE_AVATAX_COMPANY_CODE', 'company_code');
define('COMMERCE_AVATAX_TAX_CODE_FIELD', 'commerce_avatax_code');
define('COMMERCE_AVATAX_EXEMPTION_CODE_FIELD', 'commerce_avatax_exemption_code');
define('COMMERCE_AVATAX_VAT_ID_FIELD', 'commerce_avatax_vat_id');
function commerce_avatax_page_alter() {
$path = current_path();
if (module_exists('commerce_tax') && strpos($path, 'admin/commerce/config/taxes') === 0) {
drupal_set_message(t('Please disable Commerce Tax module to avoid duplicate sales tax line items. Configure Commerce AvaTax !here.', array(
'!here' => l(t('here'), 'admin/commerce/config/avatax'),
)));
}
}
function commerce_avatax_permission() {
return array(
'administer avatax' => array(
'title' => t('Administer Avatax'),
'description' => t('Allows users to configure Commerce AvaTax'),
),
'configure avatax exemptions' => array(
'title' => t('Configure Tax exemptions'),
'description' => t('Allow users to configure tax exemptions'),
),
);
}
function commerce_avatax_field_access($op, $field, $entity_type, $entity, $account) {
if ($field['field_name'] == 'commerce_avatax_exemption_code' && $op == 'edit') {
return user_access('configure avatax exemptions', $account);
}
}
function commerce_avatax_module_implements_alter(&$implementations, $hook) {
if (in_array($hook, array(
'form_alter',
'commerce_cart_order_refresh',
)) && isset($implementations['commerce_avatax'])) {
$group = $implementations['commerce_avatax'];
unset($implementations['commerce_avatax']);
$implementations['commerce_avatax'] = $group;
}
}
function commerce_avatax_menu() {
$items['admin/commerce/config/avatax'] = array(
'title' => 'Avatax ',
'description' => 'Avatax Configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_avatax_credentials_settings_form',
),
'file' => 'includes/commerce_avatax.admin.inc',
'access arguments' => array(
'administer avatax',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/commerce/config/avatax/credentials'] = array(
'title' => 'Credentials',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'access arguments' => array(
'administer avatax',
),
'weight' => -10,
);
$items['admin/commerce/config/avatax/shipping-settings'] = array(
'title' => 'Shipping settings',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer avatax',
),
'page arguments' => array(
'commerce_avatax_shipping_settings_form',
),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => -1,
);
$items['admin/commerce/config/avatax/address-validation'] = array(
'title' => 'Address validation',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer avatax',
),
'page arguments' => array(
'commerce_avatax_address_settings_form',
),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/commerce/config/avatax/general-settings'] = array(
'title' => 'General settings',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer avatax',
),
'page arguments' => array(
'commerce_avatax_general_settings_form',
),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['admin/commerce/config/avatax/global-vat'] = array(
'title' => 'Global VAT',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer avatax',
),
'page arguments' => array(
'commerce_avatax_global_vat_settings_form',
),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => 10,
);
$items['admin/commerce/orders/%commerce_order/edit/calculate-tax'] = array(
'title' => 'Calculate Taxes',
'description' => 'Call the AvaTax service in order to calculate & apply the Tax amount.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_avatax_order_admin_calculate_tax_form',
3,
),
'access callback' => 'commerce_avatax_order_admin_form_access',
'access arguments' => array(
3,
),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/commerce_avatax.admin.inc',
);
return $items;
}
function commerce_avatax_order_admin_form_access($order) {
$company_code = commerce_avatax_company_code();
$order_is_cart = module_exists('commerce_cart') && commerce_cart_order_is_cart($order);
if (!commerce_avatax_tax_calculation_enabled() || empty($company_code) || $order_is_cart) {
return FALSE;
}
if (empty($order->commerce_line_items)) {
return FALSE;
}
return commerce_order_access('update', $order);
}
function commerce_avatax_api_mode() {
return variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'api_mode', COMMERCE_AVATAX_DEVELOPMENT_MODE);
}
function commerce_avatax_account_number() {
return variable_get(COMMERCE_AVATAX_VAR_PREFIX . commerce_avatax_api_mode() . '_' . COMMERCE_AVATAX_ACCOUNT_NUMBER, '');
}
function commerce_avatax_company_code() {
return variable_get(COMMERCE_AVATAX_VAR_PREFIX . commerce_avatax_api_mode() . '_' . COMMERCE_AVATAX_COMPANY_CODE, '');
}
function commerce_avatax_license_key() {
return variable_get(COMMERCE_AVATAX_VAR_PREFIX . commerce_avatax_api_mode() . '_' . COMMERCE_AVATAX_LICENSE_KEY, '');
}
function commerce_avatax_tax_calculation_enabled() {
return variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'tax_calculation_enabled', TRUE);
}
function commerce_avatax_object($api_key = '', $api_mode = '') {
$avatax =& drupal_static(__FUNCTION__, array());
if (empty($api_key)) {
$account_number = commerce_avatax_account_number();
$license_key = commerce_avatax_license_key();
if (!empty($account_number) && !empty($license_key)) {
$api_key = base64_encode("{$account_number}:{$license_key}");
}
else {
return FALSE;
}
}
if (!isset($avatax[$api_key])) {
$logger = NULL;
$api_mode = empty($api_mode) ? commerce_avatax_api_mode() : $api_mode;
if (variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'enable_logging', FALSE)) {
$logger = 'watchdog';
}
$server_machine_name = gethostname();
$module_info = system_get_info('module', 'commerce_avatax');
$version = !empty($module_info['version']) ? $module_info['version'] : '5.x';
$headers = array(
"x-Avalara-Client" => "Drupal Commerce; Version [{$version}]; REST; V2; [{$server_machine_name}]",
);
$avatax[$api_key] = new Avatax($api_key, $api_mode, $logger, $headers);
}
return $avatax[$api_key];
}
function commerce_avatax_commerce_line_item_type_info() {
$line_item_types = array();
$line_item_types['avatax'] = array(
'name' => t('AvaTax'),
'description' => t('Tax calculated by AvaTax.'),
'add_form_submit_value' => t('Add Tax'),
'base' => 'commerce_avatax_line_item',
);
return $line_item_types;
}
function commerce_avatax_commerce_price_component_type_info() {
$components = array();
$components['avatax_sales_tax'] = array(
'title' => t('AvaTax sales tax'),
'display_title' => t('Sales tax'),
'weight' => 25,
);
$components['avatax_vat'] = array(
'title' => t('AvaTax VAT'),
'display_title' => t('VAT'),
'weight' => 25,
);
return $components;
}
function commerce_avatax_line_item_title($line_item) {
return !empty($line_item->line_item_label) ? $line_item->line_item_label : t('Tax');
}
function commerce_avatax_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['instance']['widget']['type'] == 'commerce_line_item_manager') {
foreach ($element['line_items'] as $line_item_id => $line_item) {
if ($line_item['line_item']['#value']->type == 'avatax') {
$element['line_items'][$line_item_id]['commerce_unit_price']['#access'] = FALSE;
$element['line_items'][$line_item_id]['quantity']['#access'] = FALSE;
}
}
}
}
function commerce_avatax_line_item_new($tax_price, $tax_type = 'sales_tax', $order_id = 0, $data = array()) {
$types_mapping = array(
'sales_tax' => array(
'line_item_label' => t('Sales Tax'),
'price_component' => 'avatax_sales_tax',
),
'vat' => array(
'line_item_label' => t('VAT'),
'price_component' => 'avatax_vat',
),
);
if (isset($types_mapping[$tax_type])) {
$line_item_label = $types_mapping[$tax_type]['line_item_label'];
$price_component = $types_mapping[$tax_type]['price_component'];
}
else {
$line_item_label = t('Sales Tax');
$price_component = $types_mapping['sales_tax']['price_component'];
}
$line_item = entity_create('commerce_line_item', array(
'type' => 'avatax',
'order_id' => $order_id,
'quantity' => 1,
'line_item_label' => $line_item_label,
'data' => $data,
));
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_unit_price->amount = $tax_price['amount'];
$line_item_wrapper->commerce_unit_price->currency_code = $tax_price['currency_code'];
$base_price = array(
'amount' => 0,
'currency_code' => $tax_price['currency_code'],
'data' => array(),
);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($base_price, $price_component, $tax_price, TRUE);
return $line_item;
}
function commerce_avatax_commerce_cart_order_refresh($order_wrapper) {
if ($order_wrapper->commerce_line_items
->count() === 0) {
return;
}
commerce_avatax_calculate_tax($order_wrapper);
}
function commerce_avatax_calculate_tax($order_wrapper) {
if (!commerce_avatax_tax_calculation_enabled()) {
return FALSE;
}
$company_code = commerce_avatax_company_code();
if (empty($company_code) || !($avatax_object = commerce_avatax_object())) {
drupal_set_message(t("The Avatax module is not properly configured, please configure the company code."), 'error');
return FALSE;
}
$order = $order_wrapper
->value();
$field_name = commerce_avatax_get_customer_profile_field();
if (!$field_name || !commerce_avatax_check_address($order_wrapper, $field_name)) {
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
$order->data['commerce_avatax'] = array();
return FALSE;
}
module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax.calc');
$stored_request = array();
if (isset($order->data['commerce_avatax']['request'])) {
$stored_request = $order->data['commerce_avatax']['request'];
}
$request = commerce_avatax_create_transaction($order_wrapper, 'SalesOrder');
if (isset($stored_request['date']) && isset($request['date'])) {
$stored_request['date'] = $request['date'];
}
if ($stored_request == $request) {
return array(
'request' => $request,
'response' => $order->data['commerce_avatax']['response'] ? $order->data['commerce_avatax']['response'] : array(),
);
}
$order->data['commerce_avatax'] = array();
if (empty($request['lines'])) {
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
return FALSE;
}
$response = $avatax_object
->transactionsCreate($request);
if (empty($response['success']) || !isset($response['result']['totalTax'])) {
commerce_avatax_delete_tax_line_items($order_wrapper, TRUE);
return FALSE;
}
$order->data['commerce_avatax'] = array(
'request' => $request,
'response' => $response,
);
$result = $response['result'];
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$currency_code = $order_wrapper->commerce_order_total->currency_code
->value();
$tax_price = array(
'amount' => commerce_currency_decimal_to_amount($result['totalTax'], $currency_code),
'currency_code' => $currency_code,
'data' => array(),
);
$tax_type = 'sales_tax';
if (isset($order_wrapper->{$field_name})) {
$customer_address = $order_wrapper->{$field_name}->commerce_customer_address
->value();
if (!in_array($customer_address['country'], array(
'US',
'CA',
))) {
$tax_type = 'vat';
}
}
if (!commerce_avatax_set_existing_line_item_price($order_wrapper, $tax_price, $tax_type)) {
commerce_avatax_add_line_item($order_wrapper, $tax_price, $tax_type);
}
commerce_order_calculate_total($order);
return array(
'request' => $request,
'response' => $response,
);
}
function commerce_avatax_set_existing_line_item_price(EntityDrupalWrapper $order_wrapper, $tax_price, $tax_type) {
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if (!$line_item_wrapper
->value() || $line_item_wrapper
->getBundle() != 'avatax') {
continue;
}
commerce_avatax_set_price_component($line_item_wrapper, $tax_price, $tax_type);
$line_item_wrapper
->save();
return $line_item_wrapper;
}
return FALSE;
}
function commerce_avatax_add_line_item(EntityDrupalWrapper $order_wrapper, $tax_price, $tax_type) {
$line_item = entity_create('commerce_line_item', array(
'type' => 'avatax',
'order_id' => $order_wrapper
->getIdentifier(),
'quantity' => 1,
));
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
commerce_avatax_set_price_component($line_item_wrapper, $tax_price, $tax_type);
commerce_line_item_save($line_item);
$order_wrapper->commerce_line_items[] = $line_item;
return $line_item_wrapper;
}
function commerce_avatax_set_price_component($line_item_wrapper, $tax_price, $tax_type) {
$types_mapping = array(
'sales_tax' => array(
'line_item_label' => t('Sales Tax'),
'price_component' => 'avatax_sales_tax',
),
'vat' => array(
'line_item_label' => t('VAT'),
'price_component' => 'avatax_vat',
),
);
if (isset($types_mapping[$tax_type])) {
$line_item_label = $types_mapping[$tax_type]['line_item_label'];
$price_component = $types_mapping[$tax_type]['price_component'];
}
else {
$line_item_label = t('Sales Tax');
$price_component = $types_mapping['sales_tax']['price_component'];
}
$line_item_wrapper->line_item_label = $line_item_label;
$line_item_wrapper->commerce_unit_price->amount = $tax_price['amount'];
$line_item_wrapper->commerce_unit_price->currency_code = $tax_price['currency_code'];
$base_price = array(
'amount' => 0,
'currency_code' => $tax_price['currency_code'],
'data' => array(),
);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($base_price, $price_component, $tax_price, TRUE);
}
function commerce_avatax_check_address(EntityDrupalWrapper $order_wrapper, $field_name) {
if (!isset($order_wrapper->{$field_name}) || is_null($order_wrapper->{$field_name}
->value())) {
return FALSE;
}
if (!isset($order_wrapper->{$field_name}->commerce_customer_address)) {
return FALSE;
}
$customer_address = $order_wrapper->{$field_name}->commerce_customer_address
->value();
if ($customer_address['country'] == 'US') {
$avatax_states = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'select_states', array());
if (!empty($avatax_states) && !in_array($customer_address['administrative_area'], $avatax_states)) {
return FALSE;
}
}
return TRUE;
}
function commerce_avatax_delete_tax_line_items(EntityDrupalWrapper $order_wrapper, $skip_save = FALSE) {
$line_items_to_delete = array();
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
if (!$line_item_wrapper
->value() || $line_item_wrapper
->getBundle() != 'avatax') {
continue;
}
$line_items_to_delete[] = $line_item_wrapper->line_item_id
->value();
$order_wrapper->commerce_line_items
->offsetUnset($delta);
}
if (!empty($line_items_to_delete)) {
if (!$skip_save) {
$order_wrapper
->save();
}
drupal_register_shutdown_function('commerce_line_item_delete_multiple', $line_items_to_delete);
}
}
function commerce_avatax_email_to_username($user_email) {
$name = substr($user_email, 0, strpos($user_email, '@'));
$name = preg_replace('/[^A-Za-z0-9_.-]/', '', $name);
$name = trim(substr($name, 0, USERNAME_MAX_LENGTH - 4));
return $name;
}
function commerce_avatax_commerce_payment_order_paid_in_full($order) {
$company_code = commerce_avatax_company_code();
if (!commerce_avatax_tax_calculation_enabled() || empty($company_code) || empty($order->data['commerce_avatax']['request'])) {
return;
}
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$tax_line_item = FALSE;
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
if (!$line_item_wrapper
->value() || $line_item_wrapper
->getBundle() != 'avatax') {
continue;
}
$tax_line_item = $line_item_wrapper
->value();
break;
}
if (!$tax_line_item) {
return;
}
module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax.calc');
if (empty($company_code) || !($avatax_object = commerce_avatax_object())) {
drupal_set_message(t("The Avatax module is not properly configured, please configure the company code."), 'error');
return;
}
$request_body = $order->data['commerce_avatax']['request'];
$commit = !variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'disable_document_committing', FALSE);
if ($commit) {
$request_body['type'] = 'SalesInvoice';
}
$request_body['commit'] = $commit;
$response = $avatax_object
->transactionsCreate($request_body);
if ($response['success'] && isset($response['result']['id'])) {
$order->data['commerce_avatax'] = array(
'transaction_id' => $response['result']['id'],
'transaction_code' => $response['result']['code'],
'company_code' => $company_code,
'request' => $request_body,
'response' => $response,
);
}
}
function commerce_avatax_commit_transaction($order) {
if (isset($order->data['commerce_avatax']['transaction_code']) && ($avatax = commerce_avatax_object())) {
if (!empty($order->data['commerce_avatax']['company_code'])) {
$avatax
->transactionsCommit($order->data['commerce_avatax']['company_code'], $order->data['commerce_avatax']['transaction_code']);
}
}
}
function commerce_avatax_void_transaction($order, $code = 'DocDeleted') {
if (isset($order->data['commerce_avatax']['transaction_code']) && ($avatax = commerce_avatax_object())) {
if (!empty($order->data['commerce_avatax']['company_code'])) {
$parameters = array(
'code' => $code,
);
$avatax
->transactionsVoid($order->data['commerce_avatax']['company_code'], $order->data['commerce_avatax']['transaction_code'], $parameters);
}
}
}
function commerce_avatax_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'commerce_checkout_form_') === 0) {
$address_validation_enabled = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'validate_address', TRUE);
if (!$address_validation_enabled) {
return;
}
$checkout_page_id = substr($form_id, 23);
$customer_profile_pane_found = FALSE;
foreach (commerce_checkout_panes(array(
'enabled' => TRUE,
'page' => $checkout_page_id,
)) as $pane_id => $checkout_pane) {
if (substr($pane_id, 0, 16) == 'customer_profile' && isset($form[$pane_id])) {
$customer_profile_pane_found = TRUE;
if (isset($form[$pane_id]['commerce_customer_profile_copy'])) {
array_unshift($form[$pane_id]['commerce_customer_profile_copy']['#element_validate'], 'commerce_avatax_profile_copy_validate');
break;
}
}
}
if ($customer_profile_pane_found) {
$form['#attached']['library'][] = array(
'system',
'ui.dialog',
);
$module_path = drupal_get_path('module', 'commerce_avatax');
$form['#attached']['js'][] = $module_path . '/js/commerce_avatax.js';
$form['#attached']['css'][] = $module_path . '/css/commerce_avatax.checkout.css';
$form['address_validation_result'] = array(
'#type' => 'container',
'#id' => 'commerce-avatax-address-validation-wrapper',
);
$validate_handlers = $form['buttons']['continue']['#validate'];
$validate_handlers[] = 'commerce_avatax_checkout_validate';
array_unshift($form['buttons']['continue']['#submit'], 'commerce_avatax_address_suggestion_apply');
$form['buttons']['validate-address'] = array(
'#value' => t('Validate address'),
'#type' => 'submit',
'#attributes' => array(
'class' => array(
'element-invisible',
),
),
'#id' => 'commerce-avatax-address-validate-btn',
'#validate' => $validate_handlers,
'#ajax' => array(
'callback' => 'commerce_avatax_validate_shipping_address_ajax_callback',
'progress' => array(
'type' => 'none',
),
),
);
$form['use_suggested_address'] = array(
'#type' => 'hidden',
);
}
}
}
function commerce_avatax_profile_copy_validate($element, &$form_state, $form) {
$triggering_element = end($form_state['triggering_element']['#array_parents']);
if ($triggering_element == 'validate-address') {
array_pop($form_state['triggering_element']['#array_parents']);
$form_state['triggering_element']['#array_parents'][] = 'continue';
}
}
function commerce_avatax_address_suggestion_apply($form, &$form_state) {
if (!isset($form_state['commerce_avatax']) || empty($form_state['values']['use_suggested_address'])) {
return;
}
$settings = $form_state['commerce_avatax'];
$profile = commerce_customer_profile_load($settings['customer_profile_to_update']);
if (!is_object($profile) || !isset($settings['address_validation_result']['suggestions'])) {
return;
}
$address_suggestion = reset($settings['address_validation_result']['suggestions']);
$profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
foreach ($address_suggestion as $key => $value) {
$profile_wrapper->commerce_customer_address->{$key} = $value;
}
$profile_wrapper
->save();
if ($settings['customer_profile_to_update'] != $profile->profile_id) {
$pane_id = 'customer_profile_' . $profile->type;
if ($field_name = variable_get('commerce_' . $pane_id . '_field', '')) {
$order = commerce_order_load($form_state['order']->order_id);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->{$field_name} = $profile->profile_id;
commerce_order_save($order);
}
}
}
function commerce_avatax_validate_shipping_address_ajax_callback($form, &$form_state) {
$commands = array();
if (isset($form_state['commerce_avatax']) && !empty($form_state['commerce_avatax']['address_validation_failed'])) {
if (!empty($form_state['commerce_avatax']['address_validation_result'])) {
$validation_result = $form_state['commerce_avatax']['address_validation_result'];
$buttons = array();
if ($validation_result['result'] == 'invalid') {
$buttons[] = array(
'code' => 'invalid',
'text' => t('Let me change the address'),
);
}
elseif ($validation_result['result'] == 'needs_correction') {
$buttons[] = array(
'code' => 'recommended',
'text' => t('Use recommended'),
);
$buttons[] = array(
'code' => 'keep_address',
'text' => t('Use as entered'),
);
$buttons[] = array(
'code' => 'invalid',
'text' => t('Let me change the address'),
);
}
$commands[] = array(
'command' => 'commerce_avatax_address_modal_display',
'html' => $validation_result['msg'],
'buttons' => $buttons,
'selector' => '#commerce-avatax-address-validation-wrapper',
);
}
}
else {
$commands[] = ajax_command_invoke(NULL, 'commerceAvaTaxUnblockCheckout', array());
}
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
function commerce_avatax_checkout_validate($form, &$form_state) {
if (!empty($form_state['rebuild'])) {
return;
}
$form_state['commerce_avatax'] = array();
$field_name = commerce_avatax_get_customer_profile_field();
if (!$field_name || !($field = field_info_field($field_name))) {
return;
}
$profile_type = $field['settings']['profile_type'];
$pane_id = 'customer_profile_' . $profile_type;
if (isset($form_state['pane_' . $pane_id]['profile'])) {
$profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $form_state['pane_' . $pane_id]['profile']);
}
else {
$order = commerce_order_load($form_state['order']->order_id);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$profile_wrapper = $order_wrapper->{$field_name};
}
if (!isset($profile_wrapper->commerce_customer_address)) {
return;
}
$address = $profile_wrapper->commerce_customer_address
->value();
$country = $address['country'];
$enabled_countries = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'address_validate_countries', array(
'US',
));
if (!in_array($country, $enabled_countries)) {
return;
}
module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax.address');
$validated_addresses = commerce_avatax_validate_address($address);
if ($validated_addresses === NULL) {
return;
}
$result = commerce_avatax_address_compare($address, $validated_addresses);
if ($result['result'] != 'valid') {
$form_state['commerce_avatax']['address_validation_failed'] = TRUE;
$form_state['commerce_avatax']['address_validation_result'] = $result;
$form_state['commerce_avatax']['customer_profile_to_update'] = $profile_wrapper
->getIdentifier();
$form_state['rebuild'] = TRUE;
}
}
function commerce_avatax_address_suggestion_form($form, &$form_state, $original_address, $suggestions) {
if (count($suggestions) == 1) {
$form['info'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('Your shipping address is different from the post office records. We suggest you accept the recommended address to avoid shipping delays.') . '</p>',
);
}
else {
$form['info'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('Your shipping address is different from the post office records. We suggest you accept one of the recommended addresses to avoid shipping delays.') . '</p>',
);
}
$form['original_address'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('Entered address is:') . '</p>' . theme('commerce_avatax_address', array(
'address' => $original_address,
)),
);
$options = array();
foreach ($suggestions as $key => $address) {
$options[$key] = theme('commerce_avatax_address', array(
'address' => $address,
));
}
$form['addresses'] = array(
'#title' => t('Recommended address'),
'#type' => 'radios',
'#options' => $options,
'#default_value' => '0',
);
return $form;
}
function commerce_avatax_theme() {
return array(
'commerce_avatax_address' => array(
'variables' => array(
'address' => array(),
),
),
);
}
function commerce_avatax_address_fields_mapping($flip = FALSE) {
$mapping = array(
'locality' => 'city',
'administrative_area' => 'region',
'country' => 'country',
'postal_code' => 'postalCode',
'thoroughfare' => 'line1',
'premise' => 'line2',
);
if ($flip) {
$mapping = array_flip($mapping);
}
return $mapping;
}
function theme_commerce_avatax_address($variables) {
$address = $variables['address'];
$components = array(
$address['thoroughfare'],
);
if (!empty($address['premise'])) {
$components[] = $address['premise'];
}
$components[] = $address['locality'];
$components[] = $address['administrative_area'] . ' ' . $address['postal_code'];
$components[] = $address['country'];
return implode('<br/>', $components);
}
function commerce_avatax_flush_caches() {
module_load_install('commerce_avatax');
commerce_avatax_install_helper();
}
function commerce_avatax_exemption_codes_allowed_values() {
return array(
'A' => 'Federal government (United States)',
'B' => 'State government (United States)',
'C' => 'Tribe / Status Indian / Indian Band',
'D' => 'Foreign diplomat',
'E' => 'Charitable or benevolent org',
'F' => 'Religious or educational org',
'G' => 'Resale',
'H' => 'Commercial agricultural production',
'I' => 'Industrial production / manufacturer',
'J' => 'Direct pay permit (United States)',
'K' => 'Direct mail (United States)',
'L' => 'Other',
'N' => 'Local government (United States)',
'P' => 'Commercial aquaculture (Canada)',
'Q' => 'Commercial Fishery (Canada)',
'R' => 'Non-resident (Canada)',
);
}
function commerce_avatax_void_codes_list() {
return drupal_map_assoc(array(
'Unspecified',
'PostFailed',
'DocDeleted',
'DocVoided',
'AdjustmentCancelled',
));
}
function commerce_avatax_get_customer_profile_field() {
$customer_profile_to_use = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'tax_address', 'shipping');
$profile_types = commerce_customer_profile_types();
if (!isset($profile_types[$customer_profile_to_use])) {
$customer_profile_to_use = key($profile_types);
}
$pane_id = 'customer_profile_' . $customer_profile_to_use;
if ($field_name = variable_get('commerce_' . $pane_id . '_field', '')) {
return $field_name;
}
return FALSE;
}
function commerce_avatax_commerce_cart_order_empty($order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$line_items_to_delete = array();
foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
if ($line_item_wrapper
->getBundle() == 'avatax') {
$line_items_to_delete[] = $line_item_wrapper
->getIdentifier();
$order_wrapper->commerce_line_items
->offsetUnset($delta);
}
}
commerce_line_item_delete_multiple($line_items_to_delete);
}