/** * Tests the Form\Register::getCountryId() use case where CountryId has been set on the form data * Object that has been set on the block. */ public function testGetCountryIdFormData() { $formData = new \Magento\Framework\Object(); $formData->setCountryId(self::COUNTRY_ID); $this->_block->setData(self::FORM_DATA, $formData); $this->assertEquals(self::COUNTRY_ID, $this->_block->getCountryId()); }
/** * Get request object with information necessary for getting tax rate * * Request object contain: * country_id (->getCountryId()) * region_id (->getRegionId()) * postcode (->getPostcode()) * customer_class_id (->getCustomerClassId()) * store (->getStore()) * * @param null|bool|\Magento\Framework\Object|CustomerAddress $shippingAddress * @param null|bool|\Magento\Framework\Object|CustomerAddress $billingAddress * @param null|int $customerTaxClass * @param null|int|\Magento\Store\Model\Store $store * @param int $customerId * @return \Magento\Framework\Object * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function getRateRequest($shippingAddress = null, $billingAddress = null, $customerTaxClass = null, $store = null, $customerId = null) { if ($shippingAddress === false && $billingAddress === false && $customerTaxClass === false) { return $this->getRateOriginRequest($store); } $address = new \Magento\Framework\Object(); $basedOn = $this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_BASED_ON, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); if ($shippingAddress === false && $basedOn == 'shipping' || $billingAddress === false && $basedOn == 'billing') { $basedOn = 'default'; } else { if (($billingAddress === null || !$billingAddress->getCountryId()) && $basedOn == 'billing' || ($shippingAddress === null || !$shippingAddress->getCountryId()) && $basedOn == 'shipping') { if ($customerId) { try { $defaultBilling = $this->customerAccountManagement->getDefaultBillingAddress($customerId); } catch (NoSuchEntityException $e) { } try { $defaultShipping = $this->customerAccountManagement->getDefaultShippingAddress($customerId); } catch (NoSuchEntityException $e) { } if ($basedOn == 'billing' && isset($defaultBilling) && $defaultBilling->getCountryId()) { $billingAddress = $defaultBilling; } elseif ($basedOn == 'shipping' && isset($defaultShipping) && $defaultShipping->getCountryId()) { $shippingAddress = $defaultShipping; } else { $basedOn = 'default'; } } else { $basedOn = 'default'; } } } switch ($basedOn) { case 'billing': $address = $billingAddress; break; case 'shipping': $address = $shippingAddress; break; case 'origin': $address = $this->getRateOriginRequest($store); break; case 'default': $address->setCountryId($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store))->setRegionId($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store))->setPostcode($this->_scopeConfig->getValue(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)); break; default: break; } if ($customerTaxClass === null || $customerTaxClass === false) { if ($customerId) { $customerData = $this->customerRepository->getById($customerId); $customerTaxClass = $this->customerGroupRepository->getById($customerData->getGroupId())->getTaxClassId(); } else { $customerTaxClass = $this->customerGroupManagement->getNotLoggedInGroup()->getTaxClassId(); } } $request = new \Magento\Framework\Object(); //TODO: Address is not completely refactored to use Data objects if ($address->getRegion() instanceof AddressRegion) { $regionId = $address->getRegion()->getRegionId(); } else { $regionId = $address->getRegionId(); } $request->setCountryId($address->getCountryId())->setRegionId($regionId)->setPostcode($address->getPostcode())->setStore($store)->setCustomerClassId($customerTaxClass); return $request; }
/** * Retrieve rates by customer tax class * * @param int $customerTaxClassId * @param int $productTaxClassId * @return array */ public function getRatesByCustomerTaxClass($customerTaxClass, $productTaxClass = null) { $adapter = $this->_getReadAdapter(); $customerTaxClassId = (int) $customerTaxClass; $calcJoinConditions = array('calc_table.tax_calculation_rate_id = main_table.tax_calculation_rate_id', $adapter->quoteInto('calc_table.customer_tax_class_id = ?', $customerTaxClassId)); if ($productTaxClass !== null) { $productTaxClassId = (int) $productTaxClass; $calcJoinConditions[] = $adapter->quoteInto('calc_table.product_tax_class_id = ?', $productTaxClassId); } $selectCSP = $adapter->select(); $selectCSP->from(array('main_table' => $this->getTable('tax_calculation_rate')), array('country' => 'tax_country_id', 'region_id' => 'tax_region_id', 'postcode' => 'tax_postcode'))->joinInner(array('calc_table' => $this->getTable('tax_calculation')), implode(' AND ', $calcJoinConditions), array('product_class' => 'calc_table.product_tax_class_id'))->joinLeft(array('state_table' => $this->getTable('directory_country_region')), 'state_table.region_id = main_table.tax_region_id', array('region_code' => 'state_table.code'))->distinct(true); $CSP = $adapter->fetchAll($selectCSP); $result = array(); foreach ($CSP as $one) { $request = new \Magento\Framework\Object(); $request->setCountryId($one['country'])->setRegionId($one['region_id'])->setPostcode($one['postcode'])->setCustomerClassId($customerTaxClassId)->setProductClassId($one['product_class']); $rate = $this->getRate($request); if ($rate) { $row = array('value' => $rate / 100, 'country' => $one['country'], 'state' => $one['region_code'], 'postcode' => $one['postcode'], 'product_class' => $one['product_class']); $result[] = $row; } } return $result; }