'tax_validity_range', 'primary' => 'id_tax_validity_range', 'fields' => array( 'id_tax' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'valid_from' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'allow_null' => true), 'valid_to' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'allow_null' => true), ), ); /** * All validity ranges for a tax in entry order (id_tax_validity_range order), cached per request. * * @param int $idTax * @return array */ public static function getByTaxId($idTax) { $idTax = (int) $idTax; $cacheId = 'TaxValidityRange::getByTaxId-' . $idTax; if (Cache::isStored($cacheId)) { return Cache::retrieve($cacheId); } $ranges = Db::getInstance()->executeS( 'SELECT * FROM `' . _DB_PREFIX_ . 'tax_validity_range` WHERE `id_tax` = ' . $idTax . ' ORDER BY `id_tax_validity_range` ASC' ); Cache::store($cacheId, $ranges); return $ranges; } /** * Whether $date falls within any of the given ranges (OR across rows) — a blank bound on either * @param array $ranges Rows as returned by getByTaxId() * @param DateTime $date * @return bool */ public static function dateMatchesAnyRange($ranges, $date) { if (empty($ranges)) { return true; } foreach ($ranges as $range) { $from = (!empty($range['valid_from']) && $range['valid_from'] !== '0000-00-00') ? new DateTime($range['valid_from']) : null; $to = (!empty($range['valid_to']) && $range['valid_to'] !== '0000-00-00') ? new DateTime($range['valid_to']) : null; if ((!$from || $date >= $from) && (!$to || $date <= $to)) { return true; } } return false; } /** * Replace all validity-range rows for $idTax with the submitted arrays, skipping rows where * @param int $idTax * @param array $froms * @param array $tos * @return void */ public static function saveAll($idTax, $froms, $tos) { Db::getInstance()->execute( 'DELETE FROM `' . _DB_PREFIX_ . 'tax_validity_range` WHERE `id_tax` = ' . $idTax ); foreach ($froms as $rowIndex => $from) { $to = isset($tos[$rowIndex]) ? $tos[$rowIndex] : ''; $fromEmpty = ($from === '' || $from === false); $toEmpty = ($to === '' || $to === false); if ($fromEmpty && $toEmpty) { continue; } $range = new TaxValidityRange(); $range->id_tax = $idTax; $range->valid_from = $fromEmpty ? null : $from; $range->valid_to = $toEmpty ? null : $to; $range->add(); } } }