/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Quote\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customer = $quote->getCustomer(); $storeId = $customer->getStoreId(); if ($customer->getCustomAttribute('disable_auto_group_change') && $customer->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) { $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup($storeId)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId(); } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customer->setGroupId($groupId); $quote->setCustomer($customer); } }
/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */ $shippingAssignment = $observer->getShippingAssignment(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $observer->getQuote(); /** @var \Magento\Quote\Model\Quote\Address $address */ $address = $shippingAssignment->getShipping()->getAddress(); $customer = $quote->getCustomer(); $storeId = $customer->getStoreId(); if ($customer->getDisableAutoGroupChange() || false == $this->vatValidator->isEnabled($address, $storeId)) { return; } $customerCountryCode = $address->getCountryId(); $customerVatNumber = $address->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) { $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup($storeId)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId(); } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($address, $storeId), $storeId); } if ($groupId) { $address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customer->setGroupId($groupId); $quote->setCustomer($customer); } }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function afterAddressSave($observer) { /** @var $customerAddress Address */ $customerAddress = $observer->getCustomerAddress(); $customer = $customerAddress->getCustomer(); if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) { return; } try { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true); if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId(); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); } } else { $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) { $customer->setGroupId($newGroupId); $customer->save(); } $customerAddress->setVatValidationResult($result); } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }
/** * Add success message for valid VAT ID * * @param Address $customerAddress * @param DataObject $validationResult * @return $this */ protected function addValidMessage($customerAddress, $validationResult) { $message = [(string) __('Your VAT ID was successfully validated.')]; $customer = $customerAddress->getCustomer(); if (!$this->scopeConfig->isSetFlag(HelperAddress::XML_PATH_VIV_DISABLE_AUTO_ASSIGN_DEFAULT) && !$customer->getDisableAutoGroupChange()) { $customerVatClass = $this->_customerVat->getCustomerVatClass($customerAddress->getCountryId(), $validationResult); $message[] = $customerVatClass == Vat::VAT_CLASS_DOMESTIC ? (string) __('You will be charged tax.') : (string) __('You will not be charged tax.'); } $this->messageManager->addSuccess(implode(' ', $message)); return $this; }
/** * Validate VAT number * * @param \Magento\Quote\Model\Quote\Address $quoteAddress * @param \Magento\Store\Model\Store|int $store * @return \Magento\Framework\Object */ public function validate(\Magento\Quote\Model\Quote\Address $quoteAddress, $store) { $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $merchantCountryCode = $this->customerVat->getMerchantCountryCode(); $merchantVatNumber = $this->customerVat->getMerchantVatNumber(); $validationResult = null; if ($this->customerAddress->hasValidateOnEachTransaction($store) || $customerCountryCode != $quoteAddress->getValidatedCountryCode() || $customerVatNumber != $quoteAddress->getValidatedVatNumber()) { // Send request to gateway $validationResult = $this->customerVat->checkVatNumber($customerCountryCode, $customerVatNumber, $merchantVatNumber !== '' ? $merchantCountryCode : '', $merchantVatNumber); // Store validation results in corresponding quote address $quoteAddress->setVatIsValid((int) $validationResult->getIsValid()); $quoteAddress->setVatRequestId($validationResult->getRequestIdentifier()); $quoteAddress->setVatRequestDate($validationResult->getRequestDate()); $quoteAddress->setVatRequestSuccess($validationResult->getRequestSuccess()); $quoteAddress->setValidatedVatNumber($customerVatNumber); $quoteAddress->setValidatedCountryCode($customerCountryCode); $quoteAddress->save(); } else { // Restore validation results from corresponding quote address $validationResult = new \Magento\Framework\Object(['is_valid' => (int) $quoteAddress->getVatIsValid(), 'request_identifier' => (string) $quoteAddress->getVatRequestId(), 'request_date' => (string) $quoteAddress->getVatRequestDate(), 'request_success' => (bool) $quoteAddress->getVatRequestSuccess()]); } return $validationResult; }