/**
  * Retrieve generic object with all the misc store information values
  *
  * @param Store $store
  * @return DataObject
  */
 public function getStoreInformationObject(Store $store)
 {
     $info = new DataObject(['name' => $store->getConfig(self::XML_PATH_STORE_INFO_NAME), 'phone' => $store->getConfig(self::XML_PATH_STORE_INFO_PHONE), 'hours' => $store->getConfig(self::XML_PATH_STORE_INFO_HOURS), 'street_line1' => $store->getConfig(self::XML_PATH_STORE_INFO_STREET_LINE1), 'street_line2' => $store->getConfig(self::XML_PATH_STORE_INFO_STREET_LINE2), 'city' => $store->getConfig(self::XML_PATH_STORE_INFO_CITY), 'postcode' => $store->getConfig(self::XML_PATH_STORE_INFO_POSTCODE), 'region_id' => $store->getConfig(self::XML_PATH_STORE_INFO_REGION_CODE), 'country_id' => $store->getConfig(self::XML_PATH_STORE_INFO_COUNTRY_CODE), 'vat_number' => $store->getConfig(self::XML_PATH_STORE_INFO_VAT_NUMBER)]);
     if ($info->getRegionId()) {
         $info->setRegion($this->regionFactory->create()->load($info->getRegionId())->getName());
     }
     if ($info->getCountryId()) {
         $info->setCountry($this->countryFactory->create()->loadByCode($info->getCountryId())->getName());
     }
     return $info;
 }
Example #2
0
 /**
  * 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 \Magento\Framework\DataObject $request
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _getRates($request)
 {
     // Extract params that influence our SELECT statement and use them to create cache key
     $storeId = $this->_storeManager->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 : [$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('|', [$storeId, $customerClassId, $productClassKey, $countryId, $regionId, $postcode]);
     if (!isset($this->_ratesCache[$cacheKey])) {
         // Make SELECT and get data
         $select = $this->getConnection()->select();
         $select->from(['main_table' => $this->getMainTable()], ['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->getConnection()->getCheckSql('title_table.value IS NULL', 'rate.code', 'title_table.value');
         $ruleTableAliasName = $this->getConnection()->quoteIdentifier('rule.tax_calculation_rule_id');
         $select->join(['rule' => $this->getTable('tax_calculation_rule')], $ruleTableAliasName . ' = main_table.tax_calculation_rule_id', ['rule.priority', 'rule.position', 'rule.calculate_subtotal'])->join(['rate' => $this->getTable('tax_calculation_rate')], 'rate.tax_calculation_rate_id = main_table.tax_calculation_rate_id', ['value' => 'rate.rate', 'rate.tax_country_id', 'rate.tax_region_id', 'rate.tax_postcode', 'rate.tax_calculation_rate_id', 'rate.code'])->joinLeft(['title_table' => $this->getTable('tax_calculation_rate_title')], "rate.tax_calculation_rate_id = title_table.tax_calculation_rate_id " . "AND title_table.store_id = '{$storeId}'", ['title' => $ifnullTitleValue])->where('rate.tax_country_id = ?', $countryId)->where("rate.tax_region_id IN(?)", [0, (int) $regionId]);
         $postcodeIsNumeric = is_numeric($postcode);
         $postcodeIsRange = false;
         $originalPostcode = null;
         if (is_string($postcode) && preg_match('/^(.+)-(.+)$/', $postcode, $matches)) {
             if ($countryId == self::USA_COUNTRY_CODE && is_numeric($matches[2]) && strlen($matches[2]) == 4) {
                 $postcodeIsNumeric = true;
                 $originalPostcode = $postcode;
                 $postcode = $matches[1];
             } else {
                 $postcodeIsRange = true;
                 $zipFrom = $matches[1];
                 $zipTo = $matches[2];
             }
         }
         if ($postcodeIsNumeric || $postcodeIsRange) {
             $selectClone = clone $select;
             $selectClone->where('rate.zip_is_range IS NOT NULL');
         }
         $select->where('rate.zip_is_range IS NULL');
         if ($postcode != '*' || $postcodeIsRange) {
             $select->where("rate.tax_postcode IS NULL OR rate.tax_postcode IN('*', '', ?)", $postcodeIsRange ? $postcode : $this->_createSearchPostCodeTemplates($postcode, $originalPostcode));
             if ($postcodeIsNumeric) {
                 $selectClone->where('? BETWEEN rate.zip_from AND rate.zip_to', $postcode);
             } elseif ($postcodeIsRange) {
                 $selectClone->where('rate.zip_from >= ?', $zipFrom)->where('rate.zip_to <= ?', $zipTo);
             }
         }
         /**
          * @see ZF-7592 issue http://framework.zend.com/issues/browse/ZF-7592
          */
         if ($postcodeIsNumeric || $postcodeIsRange) {
             $select = $this->getConnection()->select()->union(['(' . $select . ')', '(' . $selectClone . ')']);
         }
         $select->order('priority ' . \Magento\Framework\DB\Select::SQL_ASC)->order('tax_calculation_rule_id ' . \Magento\Framework\DB\Select::SQL_ASC)->order('tax_country_id ' . \Magento\Framework\DB\Select::SQL_DESC)->order('tax_region_id ' . \Magento\Framework\DB\Select::SQL_DESC)->order('tax_postcode ' . \Magento\Framework\DB\Select::SQL_DESC)->order('value ' . \Magento\Framework\DB\Select::SQL_DESC);
         $fetchResult = $this->getConnection()->fetchAll($select);
         $filteredRates = [];
         if ($fetchResult) {
             foreach ($fetchResult as $rate) {
                 if (!isset($filteredRates[$rate['tax_calculation_rate_id']])) {
                     $filteredRates[$rate['tax_calculation_rate_id']] = $rate;
                 }
             }
         }
         $this->_ratesCache[$cacheKey] = array_values($filteredRates);
     }
     return $this->_ratesCache[$cacheKey];
 }
 /**
  * Get cache key value for specific tax rate request
  *
  * @param   \Magento\Framework\DataObject $request
  * @return  string
  */
 protected function _getRequestCacheKey($request)
 {
     $store = $request->getStore();
     $key = '';
     if ($store instanceof \Magento\Store\Model\Store) {
         $key = $store->getId() . '|';
     } elseif (is_numeric($store)) {
         $key = $store . '|';
     }
     $key .= $request->getProductClassId() . '|' . $request->getCustomerClassId() . '|' . $request->getCountryId() . '|' . $request->getRegionId() . '|' . $request->getPostcode();
     return $key;
 }
 /**
  * Determine whether address is taxable, based on either country or region
  *
  * @param \Magento\Framework\DataObject $address
  * @param $storeId
  * @return bool
  */
 public function isAddressTaxable(\Magento\Framework\DataObject $address, $storeId)
 {
     $isTaxable = true;
     // Filtering just by country (not region)
     if (!$this->getFilterTaxByRegion($storeId)) {
         $countryFilters = explode(',', $this->getTaxCalculationCountriesEnabled($storeId));
         $countryId = $address->getCountryId();
         if (!in_array($countryId, $countryFilters)) {
             $isTaxable = false;
         }
         // Filtering by region within countries
     } else {
         $regionFilters = explode(',', $this->getRegionFilterList($storeId));
         $entityId = $address->getRegionId() ?: $address->getCountryId();
         if (!in_array($entityId, $regionFilters)) {
             $isTaxable = false;
         }
     }
     return $isTaxable;
 }
Example #5
0
 /**
  * Collect rates by address
  *
  * @param \Magento\Framework\DataObject $address
  * @param null|bool|array $limitCarrier
  * @return $this
  */
 public function collectRatesByAddress(\Magento\Framework\DataObject $address, $limitCarrier = null)
 {
     /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */
     $request = $this->_shipmentRequestFactory->create();
     $request->setAllItems($address->getAllItems());
     $request->setDestCountryId($address->getCountryId());
     $request->setDestRegionId($address->getRegionId());
     $request->setDestPostcode($address->getPostcode());
     $request->setPackageValue($address->getBaseSubtotal());
     $request->setPackageValueWithDiscount($address->getBaseSubtotalWithDiscount());
     $request->setPackageWeight($address->getWeight());
     $request->setFreeMethodWeight($address->getFreeMethodWeight());
     $request->setPackageQty($address->getItemQty());
     $request->setStoreId($this->_storeManager->getStore()->getId());
     $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
     $request->setBaseCurrency($this->_storeManager->getStore()->getBaseCurrency());
     $request->setPackageCurrency($this->_storeManager->getStore()->getCurrentCurrency());
     $request->setLimitCarrier($limitCarrier);
     $request->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax());
     return $this->collectRates($request);
 }