/** * Compare data and rates for two tax rate requests for same products (product tax class ids). * Returns true if requests are similar (i.e. equal taxes rates will be applied to them) * * Notice: * a) productClassId MUST be identical for both requests, because we intend to check selling SAME products to DIFFERENT locations * b) due to optimization productClassId can be array of ids, not only single id * * @param Varien_Object $first * @param Varien_Object $second * @return bool */ public function compareRequests($first, $second) { $country = $first->getCountryId() == $second->getCountryId(); // "0" support for admin dropdown with --please select-- $region = (int) $first->getRegionId() == (int) $second->getRegionId(); $postcode = $first->getPostcode() == $second->getPostcode(); $taxClass = $first->getCustomerClassId() == $second->getCustomerClassId(); if ($country && $region && $postcode && $taxClass) { return true; } /** * Compare available tax rates for both requests */ $firstReqRates = $this->_getResource()->getRateIds($first); $secondReqRates = $this->_getResource()->getRateIds($second); if ($firstReqRates === $secondReqRates) { return true; } /** * If rates are not equal by ids then compare actual values * All product classes must have same rates to assume requests been similar */ $productClassId1 = $first->getProductClassId(); // Save to set it back later $productClassId2 = $second->getProductClassId(); // Save to set it back later // Ids are equal for both requests, so take any of them to process $ids = is_array($productClassId1) ? $productClassId1 : array($productClassId1); $identical = true; foreach ($ids as $productClassId) { $first->setProductClassId($productClassId); $rate1 = $this->getRate($first); $second->setProductClassId($productClassId); $rate2 = $this->getRate($second); if ($rate1 != $rate2) { $identical = false; break; } } $first->setProductClassId($productClassId1); $second->setProductClassId($productClassId2); return $identical; }
/** * Returns tax rates for request - either pereforms SELECT from DB, or returns already cached result * Notice that productClassId due to optimization can be array of ids * * @param Varien_Object $request * @return array */ protected function _getRates($request) { // Extract params that influence our SELECT statement and use them to create cache key $storeId = Mage::app()->getStore($request->getStore())->getId(); $customerClassId = $request->getCustomerClassId(); $countryId = $request->getCountryId(); $regionId = $request->getRegionId(); $postcode = $request->getPostcode(); // Process productClassId as it can be array or usual value. Form best key for cache. $productClassId = $request->getProductClassId(); $ids = is_array($productClassId) ? $productClassId : array($productClassId); foreach ($ids as $key => $val) { $ids[$key] = (int) $val; // Make it integer for equal cache keys even in case of null/false/0 values } $ids = array_unique($ids); sort($ids); $productClassKey = implode(',', $ids); // Form cache key and either get data from cache or from DB $cacheKey = implode('|', array($storeId, $customerClassId, $productClassKey, $countryId, $regionId, $postcode)); if (!isset($this->_ratesCache[$cacheKey])) { // Make SELECT and get data $select = $this->_getReadAdapter()->select(); $select->from(array('main_table' => $this->getMainTable()), array('tax_calculation_rate_id', 'tax_calculation_rule_id', 'customer_tax_class_id', 'product_tax_class_id'))->where('customer_tax_class_id = ?', (int) $customerClassId); if ($productClassId) { $select->where('product_tax_class_id IN (?)', $productClassId); } $ifnullTitleValue = $this->_getReadAdapter()->getCheckSql('title_table.value IS NULL', 'rate.code', 'title_table.value'); $ruleTableAliasName = $this->_getReadAdapter()->quoteIdentifier('rule.tax_calculation_rule_id'); $select->join(array('rule' => $this->getTable('tax/tax_calculation_rule')), $ruleTableAliasName . ' = main_table.tax_calculation_rule_id', array('rule.priority', 'rule.position'))->join(array('rate' => $this->getTable('tax/tax_calculation_rate')), 'rate.tax_calculation_rate_id = main_table.tax_calculation_rate_id', array('value' => 'rate.rate', 'rate.tax_country_id', 'rate.tax_region_id', 'rate.tax_postcode', 'rate.tax_calculation_rate_id', 'rate.code'))->joinLeft(array('title_table' => $this->getTable('tax/tax_calculation_rate_title')), "rate.tax_calculation_rate_id = title_table.tax_calculation_rate_id " . "AND title_table.store_id = '{$storeId}'", array('title' => $ifnullTitleValue))->where('rate.tax_country_id = ?', $countryId)->where("rate.tax_region_id IN(?)", array(0, (int) $regionId)); $postcodeIsNumeric = is_numeric($postcode); if ($postcodeIsNumeric) { $selectClone = clone $select; $selectClone->where('rate.zip_is_range IS NOT NULL'); } $select->where('rate.zip_is_range IS NULL'); if ($request->getPostcode() != '*') { $select->where("rate.tax_postcode IS NULL OR rate.tax_postcode IN('*', '', ?)", $this->_createSearchPostCodeTemplates($postcode)); if ($postcodeIsNumeric) { $selectClone->where('? BETWEEN rate.zip_from AND rate.zip_to', $postcode); } } /** * @see ZF-7592 issue http://framework.zend.com/issues/browse/ZF-7592 */ if ($postcodeIsNumeric) { $select = $this->_getReadAdapter()->select()->union(array('(' . $select . ')', '(' . $selectClone . ')')); } $select->order('priority ' . Varien_Db_Select::SQL_ASC)->order('tax_calculation_rule_id ' . Varien_Db_Select::SQL_ASC)->order('tax_country_id ' . Varien_Db_Select::SQL_DESC)->order('tax_region_id ' . Varien_Db_Select::SQL_DESC)->order('tax_postcode ' . Varien_Db_Select::SQL_DESC)->order('value ' . Varien_Db_Select::SQL_DESC); $this->_ratesCache[$cacheKey] = $this->_getReadAdapter()->fetchAll($select); } return $this->_ratesCache[$cacheKey]; }
/** * Get information about tax rates applied to request * * @param Varien_Object $request * @return array */ public function getAppliedRates($request) { if (!$request->getCountryId() || !$request->getCustomerClassId() || !$request->getProductClassId()) { return array(); } $cacheKey = $this->_getRequestCacheKey($request); if (!isset($this->_rateCalculationProcess[$cacheKey])) { $this->_rateCalculationProcess[$cacheKey] = $this->_getResource()->getCalculationProcess($request); } return $this->_rateCalculationProcess[$cacheKey]; }
/** * Load select and return tax rates * * @param Varien_Object $request * @return array */ protected function _getRates($request) { $storeId = Mage::app()->getStore($request->getStore())->getId(); $select = $this->_getReadAdapter()->select(); $select->from(array('main_table' => $this->getMainTable()))->where('customer_tax_class_id = ?', $request->getCustomerClassId()); if ($request->getProductClassId()) { $select->where('product_tax_class_id IN (?)', $request->getProductClassId()); } $select->join(array('rule' => $this->getTable('tax/tax_calculation_rule')), 'rule.tax_calculation_rule_id = main_table.tax_calculation_rule_id', array('rule.priority', 'rule.position')); $select->join(array('rate' => $this->getTable('tax/tax_calculation_rate')), 'rate.tax_calculation_rate_id = main_table.tax_calculation_rate_id', array('value' => 'rate.rate', 'rate.tax_country_id', 'rate.tax_region_id', 'rate.tax_postcode', 'rate.tax_calculation_rate_id', 'rate.code')); $select->joinLeft(array('title_table' => $this->getTable('tax/tax_calculation_rate_title')), "rate.tax_calculation_rate_id = title_table.tax_calculation_rate_id AND title_table.store_id = '{$storeId}'", array('title' => 'IFNULL(title_table.value, rate.code)')); $select->where("rate.tax_country_id = ?", $request->getCountryId())->where("rate.tax_region_id in ('*', '', ?)", $request->getRegionId()); $selectClone = clone $select; $select->where("rate.zip_is_range IS NULL")->where("rate.tax_postcode in ('*', '', ?)", $this->_createSearchPostCodeTemplates($request->getPostcode())); $selectClone->where("rate.zip_is_range IS NOT NULL")->where("? BETWEEN rate.zip_from AND rate.zip_to", $request->getPostcode()); /** * @see ZF-7592 issue http://framework.zend.com/issues/browse/ZF-7592 */ $select = $this->_getReadAdapter()->select()->union(array('(' . $select . ')', '(' . $selectClone . ')')); $order = array('priority ASC', 'tax_calculation_rule_id ASC', 'tax_country_id DESC', 'tax_region_id DESC', 'tax_postcode DESC', 'value DESC'); $select->order($order); return $this->_getReadAdapter()->fetchAll($select); }
/** * Compare data from two tax rate requests. * Return true if requests are semilar * * @param Varien_Object $first * @param Varien_Object $second * @return bool */ public function compareRequests($first, $second) { // var_dump($first->getCountryId() , $second->getCountryId()); echo '<br>'; // var_dump($first->getRegionId(), $second->getRegionId());echo '<br>'; $country = $first->getCountryId() == $second->getCountryId(); /** * "0" support for admin dropdown with --please select-- */ $region = (int) $first->getRegionId() == (int) $second->getRegionId() || $first->getRegionId() == '*' || $second->getRegionId() == '*' || $first->getRegionId() == '0' || $second->getRegionId() == '0'; $postcode = $first->getPostcode() == $second->getPostcode() || $first->getPostcode() == '*' || $second->getPostcode() == '*'; $taxClass = $first->getCustomerClassId() == $second->getCustomerClassId(); if ($country && $region && $postcode && $taxClass) { return true; } return false; }
/** * Compare data and rates for two tax rate requests. * Return true if requests are semilar * * @param Varien_Object $first * @param Varien_Object $second * @return bool */ public function compareRequests($first, $second) { $country = $first->getCountryId() == $second->getCountryId(); /** * "0" support for admin dropdown with --please select-- */ $region = (int) $first->getRegionId() == (int) $second->getRegionId(); $postcode = $first->getPostcode() == $second->getPostcode(); $taxClass = $first->getCustomerClassId() == $second->getCustomerClassId(); if ($country && $region && $postcode && $taxClass) { return true; } /** * Compare available tax rates for both requests */ $firstReqRates = $this->_getResource()->getRateIds($first); $secondReqRates = $this->_getResource()->getRateIds($second); if ($firstReqRates === $secondReqRates) { return true; } return false; }