You are here

function commerce_avatax_create_transaction in Drupal Commerce Connector for AvaTax 7.5

Prepares the transaction request array to be sent to AvaTax.

Parameters

EntityDrupalWrapper $order_wrapper: The wrapped order entity.

string $transaction_type: The transaction type (e.g SalesOrder|SalesInvoice).

bool $commit: Boolean indicating whether or not to commit the transaction.

Return value

array|bool Returns the request body array to be sent, FALSE in case of failure.

1 call to commerce_avatax_create_transaction()
commerce_avatax_calculate_tax in ./commerce_avatax.module
Performs Tax calculation for a given order.

File

includes/commerce_avatax.calc.inc, line 20
AvaTax calculation/requests functions.

Code

function commerce_avatax_create_transaction($order_wrapper, $transaction_type = 'SalesOrder', $commit = FALSE) {

  // Return FALSE in case there are no line items.
  if ($order_wrapper->commerce_line_items
    ->count() === 0) {
    return FALSE;
  }
  $company_code = commerce_avatax_company_code();

  // Prepare the Request Body.
  $request_body = array(
    'type' => $transaction_type,
    'companyCode' => $company_code,
    'date' => format_date(REQUEST_TIME, 'custom', 'c'),
    'code' => 'DC-' . $order_wrapper
      ->getIdentifier(),
    'customerCode' => _commerce_avatax_transaction_get_customer_code($order_wrapper),
    'currencyCode' => $order_wrapper->commerce_order_total->currency_code
      ->value(),
    'addresses' => array(
      'shipFrom' => _commerce_avatax_transaction_get_ship_from(),
      'shipTo' => _commerce_avatax_transaction_get_ship_to($order_wrapper),
    ),
  );
  if (!empty($commit)) {
    $request_body['commit'] = TRUE;
  }

  // For non anonymous orders, send the exemption code if it exists.
  if ($order_wrapper->uid
    ->value() > 0) {

    // Check the Exemptions status.
    if (variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'exemptions_status', FALSE)) {
      if (isset($order_wrapper->owner->{COMMERCE_AVATAX_EXEMPTION_CODE_FIELD})) {
        $exemption_code = $order_wrapper->owner->{COMMERCE_AVATAX_EXEMPTION_CODE_FIELD}
          ->value();
        if (!empty($exemption_code)) {
          $request_body['customerUsageType'] = $exemption_code;
        }
      }
    }

    // Check if the user has a VAT ID field.
    if (variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'add_vat_field', FALSE)) {
      if (isset($order_wrapper->owner->{COMMERCE_AVATAX_VAT_ID_FIELD})) {
        $vat_id = $order_wrapper->owner->{COMMERCE_AVATAX_VAT_ID_FIELD}
          ->value();
        if (!empty($vat_id)) {
          $request_body['businessIdentificationNo'] = $vat_id;
        }
      }
    }
  }
  $tax_included = FALSE;

  // If the ship from country is not in the US|CA, assume the price entered
  // is "VAT-inclusive" if specified in the settings.
  if (isset($request_body['addresses']['shipFrom']['country'])) {
    if (!in_array($request_body['addresses']['shipFrom']['country'], array(
      'CA',
      'US',
    ))) {
      $tax_included = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'vat_inclusive', TRUE);
    }
  }
  _commerce_avatax_transaction_add_lines($request_body, $order_wrapper->commerce_line_items, $tax_included);
  $order = $order_wrapper
    ->value();
  drupal_alter('commerce_avatax_create_transaction', $request_body, $order);
  return $request_body;
}