You are here

function commerce_avatax_line_item_add_form in Drupal Commerce Connector for AvaTax 7.4

Same name and namespace in other branches
  1. 7.3 commerce_avatax.module \commerce_avatax_line_item_add_form()

Returns form for adding an AvaTax line item through line item manager widget.

File

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

Code

function commerce_avatax_line_item_add_form($form, &$form_state) {

  // Calculate the sales tax amount for this order.
  if (isset($form_state['commerce_order']->order_id)) {
    $order = commerce_order_load($form_state['commerce_order']->order_id);
  }
  else {
    $form = array();
    $form['avatax_error'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
    return $form;
  }
  $sales_tax_failed = commerce_avatax_manual_calculate_sales_tax($order);

  // Return empty form with name to detect error.
  $form = array();
  if ($sales_tax_failed) {
    $form['avatax_error'] = array(
      '#type' => 'value',
      '#value' => 0,
    );
    return $form;
  }

  // Store the available rates in the form.
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_avatax') . '/theme/commerce_avatax.admin.css';
  $form['avatax_rate'] = array(
    '#type' => 'value',
    '#value' => $order->avatax,
  );

  // Create an options array for the sales tax amount.
  $options = commerce_avatax_options($order);
  $form['avatax'] = array(
    '#type' => 'radios',
    '#title' => t('AvaTax'),
    '#options' => $options,
    '#default_value' => key($options),
  );
  return $form;
}