'tax_price_tier', 'primary' => 'id_tax_price_tier', 'fields' => array( 'id_tax' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'min_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'), 'max_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'), 'tax_value' => array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'), ), ); /** * All tiers for a tax in entry order (id_tax_price_tier order), cached per request. * * @param int $idTax * @return array */ public static function getByTaxId($idTax) { $idTax = (int) $idTax; $cacheId = 'TaxPriceTier::getByTaxId-' . $idTax; if (Cache::isStored($cacheId)) { return Cache::retrieve($cacheId); } $tiers = Db::getInstance()->executeS( 'SELECT * FROM `' . _DB_PREFIX_ . 'tax_price_tier` WHERE `id_tax` = ' . $idTax . ' ORDER BY `id_tax_price_tier` ASC' ); Cache::store($cacheId, $tiers); return $tiers; } /** * Find the matching tier for a given unit price, or false when no tier matches. * * @param int $idTax * @param float $unitPrice * @return array|false */ public static function getMatchingTier($idTax, $unitPrice) { $tiers = self::getByTaxId($idTax); foreach ($tiers as $tier) { $maxAmount = (float) $tier['max_amount']; $inLower = ($unitPrice >= (float) $tier['min_amount']); $inUpper = ($maxAmount == 0 || $unitPrice <= $maxAmount); if ($inLower && $inUpper) { return $tier; } } return false; } /** * Replace all tier rows for $idTax with the submitted arrays, skipping entries where tax_value is empty. * * @param int $idTax * @param array $mins * @param array $maxs * @param array $values * @return void */ public static function saveAll($idTax, $mins, $maxs, $values) { $idTax = (int) $idTax; Db::getInstance()->execute( 'DELETE FROM `' . _DB_PREFIX_ . 'tax_price_tier` WHERE `id_tax` = ' . $idTax ); foreach ($values as $rowIndex => $value) { if ($value === '' || $value === false) { continue; } $tier = new TaxPriceTier(); $tier->id_tax = $idTax; $tier->min_amount = isset($mins[$rowIndex]) ? (float) $mins[$rowIndex] : 0; $tier->max_amount = isset($maxs[$rowIndex]) ? (float) $maxs[$rowIndex] : 0; $tier->tax_value = (float) $value; $tier->add(); } } }