/**
  * Restore initial customer group ID in quote if needed on collect_totals_after event of quote address
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute($observer)
 {
     $quoteAddress = $observer->getQuoteAddress();
     $configAddressType = $this->customerAddressHelper->getTaxCalculationAddressType();
     // Restore initial customer group ID in quote only if VAT is calculated based on shipping address
     if ($quoteAddress->hasPrevQuoteCustomerGroupId() && $configAddressType == \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING) {
         $quoteAddress->getQuote()->setCustomerGroupId($quoteAddress->getPrevQuoteCustomerGroupId());
         $quoteAddress->unsPrevQuoteCustomerGroupId();
     }
 }
Esempio n. 2
0
 /**
  * Check whether VAT ID validation is enabled
  *
  * @param \Magento\Quote\Model\Quote\Address $quoteAddress
  * @param \Magento\Store\Model\Store|int $store
  * @return bool
  */
 public function isEnabled(\Magento\Quote\Model\Quote\Address $quoteAddress, $store)
 {
     $configAddressType = $this->customerAddress->getTaxCalculationAddressType($store);
     // When VAT is based on billing address then Magento have to handle only billing addresses
     $additionalBillingAddressCondition = $configAddressType == \Magento\Customer\Model\Address\AbstractAddress::TYPE_BILLING ? $configAddressType != $quoteAddress->getAddressType() : false;
     // Handle only addresses that corresponds to VAT configuration
     if (!$this->customerAddress->isVatValidationEnabled($store) || $additionalBillingAddressCondition) {
         return false;
     }
     return true;
 }
 /**
  * Retrieve sales address (order or quote) on which tax calculation must be based
  *
  * @param \Magento\Sales\Model\Order $order
  * @param \Magento\Store\Model\Store|string|int|null $store
  * @return \Magento\Sales\Model\Order\Address|null
  */
 protected function _getVatRequiredSalesAddress($order, $store = null)
 {
     $configAddressType = $this->customerAddressHelper->getTaxCalculationAddressType($store);
     $requiredAddress = null;
     switch ($configAddressType) {
         case \Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING:
             $requiredAddress = $order->getShippingAddress();
             break;
         default:
             $requiredAddress = $order->getBillingAddress();
             break;
     }
     return $requiredAddress;
 }
 /**
  * Restore initial customer group ID in quote if needed on collect_totals_after event of quote address
  *
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     /** @var ShippingAssignmentInterface $shippingAssignment */
     $shippingAssignment = $observer->getEvent()->getShippingAssignment();
     /** @var Quote $quote */
     $quote = $observer->getEvent()->getQuote();
     $address = $shippingAssignment->getShipping()->getAddress();
     $configAddressType = $this->customerAddressHelper->getTaxCalculationAddressType();
     // Restore initial customer group ID in quote only if VAT is calculated based on shipping address
     if ($address->hasPrevQuoteCustomerGroupId() && $configAddressType == AbstractAddress::TYPE_SHIPPING) {
         $quote->setCustomerGroupId($address->getPrevQuoteCustomerGroupId());
         $address->unsPrevQuoteCustomerGroupId();
     }
 }
 /**
  * Address before save event handler
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if ($this->_coreRegistry->registry(self::VIV_CURRENTLY_SAVED_ADDRESS)) {
         $this->_coreRegistry->unregister(self::VIV_CURRENTLY_SAVED_ADDRESS);
     }
     /** @var $customerAddress Address */
     $customerAddress = $observer->getCustomerAddress();
     if ($customerAddress->getId()) {
         $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddress->getId());
     } else {
         $configAddressType = $this->_customerAddress->getTaxCalculationAddressType();
         $forceProcess = $configAddressType == AbstractAddress::TYPE_SHIPPING ? $customerAddress->getIsDefaultShipping() : $customerAddress->getIsDefaultBilling();
         if ($forceProcess) {
             $customerAddress->setForceProcess(true);
         } else {
             $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address');
         }
     }
 }
 /**
  * Check whether specified address should be processed in after_save event handler
  *
  * @param Address $address
  * @return bool
  */
 protected function _canProcessAddress($address)
 {
     if ($address->getForceProcess()) {
         return true;
     }
     if ($this->_coreRegistry->registry(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS) != $address->getId()) {
         return false;
     }
     $configAddressType = $this->_customerAddress->getTaxCalculationAddressType();
     if ($configAddressType == AbstractAddress::TYPE_SHIPPING) {
         return $this->_isDefaultShipping($address);
     }
     return $this->_isDefaultBilling($address);
 }
Esempio n. 7
0
 /**
  * Retrieve success message
  *
  * @return string
  */
 protected function getSuccessMessage()
 {
     if ($this->addressHelper->isVatValidationEnabled()) {
         if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
             // @codingStandardsIgnoreStart
             $message = __('If you are a registered VAT customer, please <a href="%1">click here</a> to enter your shipping address for proper VAT calculation.', $this->urlModel->getUrl('customer/address/edit'));
             // @codingStandardsIgnoreEnd
         } else {
             // @codingStandardsIgnoreStart
             $message = __('If you are a registered VAT customer, please <a href="%1">click here</a> to enter your billing address for proper VAT calculation.', $this->urlModel->getUrl('customer/address/edit'));
             // @codingStandardsIgnoreEnd
         }
     } else {
         $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName());
     }
     return $message;
 }
Esempio n. 8
0
 /**
  * Adds a welcome message to the session
  *
  * @return void
  */
 protected function _addWelcomeMessage()
 {
     $this->messageManager->addSuccess(__('Thank you for registering with %1.', $this->_storeManager->getStore()->getFrontendName()));
     if ($this->_isVatValidationEnabled()) {
         // Show corresponding VAT message to customer
         $configAddressType = $this->_addressHelper->getTaxCalculationAddressType();
         $editAddersUrl = $this->_createUrl()->getUrl('customer/address/edit');
         switch ($configAddressType) {
             case \Magento\Customer\Helper\Address::TYPE_SHIPPING:
                 // @codingStandardsIgnoreStart
                 $userPrompt = __('If you are a registered VAT customer, please click <a href="%1">here</a> to enter you shipping address for proper VAT calculation', $editAddersUrl);
                 // @codingStandardsIgnoreEnd
                 break;
             default:
                 // @codingStandardsIgnoreStart
                 $userPrompt = __('If you are a registered VAT customer, please click <a href="%1">here</a> to enter you billing address for proper VAT calculation', $editAddersUrl);
                 // @codingStandardsIgnoreEnd
                 break;
         }
         $this->messageManager->addSuccess($userPrompt);
     }
 }
Esempio n. 9
0
 /**
  * @param $store
  * @param $result
  * @dataProvider getTaxCalculationAddressTypeDataProvider
  */
 public function testGetTaxCalculationAddressType($store, $result)
 {
     $this->scopeConfig->expects($this->once())->method('getValue')->with(\Magento\Customer\Helper\Address::XML_PATH_VIV_TAX_CALCULATION_ADDRESS_TYPE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store)->will($this->returnValue($result));
     $this->assertEquals($result, $this->helper->getTaxCalculationAddressType($store));
 }
 /**
  * Retrieve sales address (order or quote) on which tax calculation must be based
  *
  * @param Order $order
  * @param \Magento\Store\Model\Store|string|int|null $store
  * @return Address|null
  */
 protected function _getVatRequiredSalesAddress($order, $store = null)
 {
     $configAddressType = $this->customerAddressHelper->getTaxCalculationAddressType($store);
     $requiredAddress = $configAddressType === AbstractAddress::TYPE_SHIPPING ? $order->getShippingAddress() : $order->getBillingAddress();
     return $requiredAddress;
 }