You are here

function uc_taxes_rate_save in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 uc_taxes/uc_taxes.module \uc_taxes_rate_save()

Saves a tax rate to the database.

Parameters

$rate: The tax rate object to be saved.

bool $reset: If TRUE, resets the Rules cache after saving. Defaults to TRUE.

Return value

The saved tax rate object including the rate ID for new rates.

5 calls to uc_taxes_rate_save()
UbercartTaxesTestCase::testInclusiveTaxes in uc_taxes/tests/uc_taxes.test
UbercartTaxesTestCase::testStoredTaxDisplay in uc_taxes/tests/uc_taxes.test
UbercartTaxesTestCase::testTaxProductClassUpdate in uc_taxes/tests/uc_taxes.test
uc_taxes_clone in uc_taxes/uc_taxes.admin.inc
Clones a tax rate.
uc_taxes_form_submit in uc_taxes/uc_taxes.admin.inc
Form submission handler for uc_taxes_form().

File

uc_taxes/uc_taxes.module, line 350
Ubercart Taxes module.

Code

function uc_taxes_rate_save($rate, $reset = TRUE) {

  // Save it as a new rate if no ID is specified.
  if (empty($rate->id)) {
    drupal_write_record('uc_taxes', $rate);
  }
  else {
    drupal_write_record('uc_taxes', $rate, array(
      'id',
    ));
  }
  db_delete('uc_taxed_product_types')
    ->condition('tax_id', $rate->id)
    ->execute();
  db_delete('uc_taxed_line_items')
    ->condition('tax_id', $rate->id)
    ->execute();
  $p_insert = db_insert('uc_taxed_product_types')
    ->fields(array(
    'tax_id',
    'type',
  ));
  $l_insert = db_insert('uc_taxed_line_items')
    ->fields(array(
    'tax_id',
    'type',
  ));
  foreach ($rate->taxed_product_types as $type) {
    $p_insert
      ->values(array(
      'tax_id' => $rate->id,
      'type' => $type,
    ));
  }
  foreach ($rate->taxed_line_items as $type) {
    $l_insert
      ->values(array(
      'tax_id' => $rate->id,
      'type' => $type,
    ));
  }
  $p_insert
    ->execute();
  $l_insert
    ->execute();
  if ($reset) {

    // Ensure Rules picks up the new condition.
    entity_flush_caches();
  }
  return $rate;
}