You are here

function commerce_reports_tax_order_save in Commerce Reporting 7.3

Same name and namespace in other branches
  1. 7.4 modules/tax/commerce_reports_tax.module \commerce_reports_tax_order_save()

Stores the tax information of an order.

If the order has already been processed in the past it will compare the information in the database with the generated information and update accordingly.

2 calls to commerce_reports_tax_order_save()
commerce_reports_tax_entity_update in modules/tax/commerce_reports_tax.module
Implements hook_entity_update().
_commerce_reports_tax_generate in modules/tax/commerce_reports_tax.batch.inc
Build the table of tax information.

File

modules/tax/commerce_reports_tax.module, line 255

Code

function commerce_reports_tax_order_save($order, $update = FALSE) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_data = $wrapper->commerce_order_total->data
    ->value();
  $applied_taxes = array();
  $base_price = 0;

  // Loop through all the components of this order, to retrieve all applies tax rates and
  // to determine the base price of the order.
  foreach ($order_data['components'] as $component) {
    if (!empty($component['price']['data']['tax_rate'])) {

      // If this component contains a tax rate, process and store.
      $rate = $component['price']['data']['tax_rate'];

      // For each applied tax rate to this order, create a new object to hold the information.
      // If the report is detailled, we will also use this object to save to the database.
      $tax = new stdClass();

      // Report_id is a dummy field at this moment. In the future we want to be able to distinguish different reports.
      $tax->tax_rate = $rate['name'];
      $tax->order_id = $wrapper->order_id
        ->value();
      $tax->currency_code = $wrapper->commerce_order_total->currency_code
        ->value();
      $tax->taxable =& $base_price;
      $tax->taxed = (int) $component['price']['amount'];

      // Store the record to an array.
      $applied_taxes[$tax->tax_rate . $tax->order_id . $tax->currency_code] = $tax;
    }
    elseif ($component['name'] == 'base_price') {

      // Aggregate the base price for this order.
      $base_price += $component['price']['amount'];
    }
  }

  // If we know the tax information is already stored, we need to check the database and see what's already in there.
  if ($update) {
    $result = db_query("SELECT tax_rate, order_id, currency_code, taxable, taxed FROM {commerce_reports_tax} WHERE order_id = :order_id", array(
      ':order_id' => $order->order_id,
    ));
    while ($row = $result
      ->fetchAssoc()) {
      $id = $row['tax_rate'] . $row['order_id'] . $row['currency_code'];

      // If there exists an applied tax that corresponds with the one in the database; update it if necessary.
      if (!empty($applied_taxes[$id])) {
        if ($applied_taxes[$id] != $row) {
          drupal_write_record('commerce_reports_tax', $applied_taxes[$id], array(
            'tax_rate',
            'order_id',
            'currency_code',
          ));
          unset($applied_taxes[$id]);
        }
      }
      else {
        db_query("DELETE FROM {commerce_reports_tax} WHERE tax_rate = :tax_rate AND order_id = :order_id AND currency_code = :currency_code", array(
          ':tax_rate' => $row['tax_rate'],
          ':order_id' => $row['order_id'],
          ':currency_code' => $row['currency_code'],
        ));
      }
    }
  }

  // Insert any of the remaining applied taxes.
  foreach ($applied_taxes as $tax) {
    $result = drupal_write_record('commerce_reports_tax', $tax);
  }
}