You are here

function uc_tax_rate_load in Ubercart 8.4

Loads a tax rate or all tax rates from the database.

Parameters

$rate_id: The ID of the specific rate to load or NULL to return all available rates.

Return value

An object representing the requested tax rate or an array of all tax rates keyed by rate ID.

4 calls to uc_tax_rate_load()
hook_uc_calculate_tax in uc_tax/uc_tax.api.php
Calculates tax line items for an order.
TaxRateMethodsForm::buildForm in uc_tax/src/Form/TaxRateMethodsForm.php
Form constructor.
uc_tax_default_rules_configuration in uc_tax/uc_tax.rules_defaults.inc
Implements hook_default_rules_configuration().
uc_tax_filter_rates in uc_tax/uc_tax.module
List all the taxes that can apply to an order.

File

uc_tax/uc_tax.module, line 331
Ubercart Tax module.

Code

function uc_tax_rate_load($rate_id = NULL) {
  static $rates = [];

  // If the rates have not been cached yet...
  if (empty($rates)) {

    // Get all the rate data from the database.
    $connection = \Drupal::database();
    $result = $connection
      ->query('SELECT id, name, rate, shippable, weight, display_include, inclusion_text FROM {uc_tax} ORDER BY weight');

    // Loop through each returned row.
    foreach ($result as $rate) {
      $rate->taxed_product_types = [];
      $rate->taxed_line_items = [];

      // Disabled by default, overridden in config.
      $rate->enabled = FALSE;
      $rates[$rate->id] = $rate;
    }
    foreach ([
      'taxed_product_types',
      'taxed_line_items',
    ] as $field) {
      $result = $connection
        ->select('uc_tax_' . $field, 't')
        ->fields('t', [
        'tax_id',
        'type',
      ])
        ->execute();
      foreach ($result as $record) {
        $rates[$record->tax_id]->{$field}[] = $record->type;
      }
    }
  }

  // Return a rate as specified.
  if ($rate_id) {
    return isset($rates[$rate_id]) ? $rates[$rate_id] : FALSE;
  }
  else {
    return $rates;
  }
}