/** * 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); } }
/** * Verify that the Delete button does not exist for the default group. * @magentoAppIsolation enabled */ public function testDeleteButtonNotExistInDefaultGroup() { $groupId = $this->groupManagement->getDefaultGroup(0)->getId(); $this->registry->register(RegistryConstants::CURRENT_GROUP_ID, $groupId); $this->getRequest()->setParam('id', $groupId); /** @var $block Edit */ $block = $this->layout->createBlock('Magento\\Customer\\Block\\Adminhtml\\Group\\Edit', 'block'); $buttonsHtml = $block->getButtonsHtml(); $this->assertNotContains('delete', $buttonsHtml); }
/** * Method set default group id to the customers collection * * @param \Magento\Framework\Model\AbstractModel $group * @return $this */ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $group) { $customerCollection = $this->_createCustomersCollection()->addAttributeToFilter('group_id', $group->getId())->load(); foreach ($customerCollection as $customer) { /** @var $customer \Magento\Customer\Model\Customer */ $customer->load($customer->getId()); $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStoreId())->getId(); $customer->setGroupId($defaultGroupId); $customer->save(); } return parent::_afterDelete($group); }
/** * Set current attribute to entry (for specified product) * * @param Product $product * @param Entry $entry * @return Entry */ public function convertAttribute($product, $entry) { $product->setWebsiteId($this->_storeManager->getStore($product->getStoreId())->getWebsiteId()); $defaultCustomerGroup = $this->groupManagement->getDefaultGroup($product->getStoreId()); $product->setCustomerGroupId($defaultCustomerGroup->getId()); /** @var \Magento\Store\Model\Store $store */ $store = $this->_storeManager->getStore($product->getStoreId()); $isSalePriceAllowed = $this->_config->getTargetCountry($product->getStoreId()) == 'US'; // get tax settings $priceDisplayType = $this->_taxData->getPriceDisplayType($product->getStoreId()); $inclTax = $priceDisplayType == Config::DISPLAY_TYPE_INCLUDING_TAX; $finalPrice = $this->_getFinalPrice($product, $store, $inclTax, $isSalePriceAllowed); // calculate price attribute value $price = $this->_getPrice($product, $store, $priceDisplayType, $inclTax, $isSalePriceAllowed); if ($isSalePriceAllowed) { // set sale_price and effective dates for it if ($price && $price - $finalPrice > 0.0001) { $this->_setAttributePrice($entry, $product, $price); $this->_setAttributePrice($entry, $product, $finalPrice, 'sale_price'); $this->_setEffectiveDate($product, $entry); } else { $this->_setAttributePrice($entry, $product, $finalPrice); $entry->removeContentAttribute('sale_price_effective_date'); $entry->removeContentAttribute('sale_price'); } // calculate taxes $tax = $this->getGroupAttributeTax(); if (!$inclTax && !is_null($tax)) { $tax->convertAttribute($product, $entry); } } else { $this->_setAttributePrice($entry, $product, $price); } return $entry; }
/** * @param string $formCode * @param RequestInterface $request * @return \Magento\Customer\Api\Data\CustomerInterface */ public function extract($formCode, RequestInterface $request) { $customerForm = $this->formFactory->create('customer', $formCode); $customerData = $customerForm->extractData($request); $allowedAttributes = $customerForm->getAllowedAttributes(); $isGroupIdEmpty = isset($allowedAttributes['group_id']); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $store = $this->storeManager->getStore(); if ($isGroupIdEmpty) { $customerDataObject->setGroupId($this->customerGroupManagement->getDefaultGroup($store->getId())->getId()); } $customerDataObject->setWebsiteId($store->getWebsiteId()); $customerDataObject->setStoreId($store->getId()); return $customerDataObject; }
/** * 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); } }
/** * Retrieve quote model object * * @return \Magento\Quote\Model\Quote */ public function getQuote() { if ($this->_quote === null) { $this->_quote = $this->quoteFactory->create(); if ($this->getStoreId()) { if (!$this->getQuoteId()) { $this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId()); $this->_quote->setIsActive(false); $this->_quote->setStoreId($this->getStoreId()); $this->quoteRepository->save($this->_quote); $this->setQuoteId($this->_quote->getId()); $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); } else { $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); $this->_quote->setStoreId($this->getStoreId()); } if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) { $customer = $this->customerRepository->getById($this->getCustomerId()); $this->_quote->assignCustomer($customer); $this->quoteRepository->save($this->_quote); } } $this->_quote->setIgnoreOldQty(true); $this->_quote->setIsSuperMode(true); } return $this->_quote; }
/** * @param $testGroup * @param $storeId */ private function assertDefaultGroupMatches($testGroup, $storeId) { $group = $this->groupManagement->getDefaultGroup($storeId); $this->assertEquals($testGroup['id'], $group->getId()); $this->assertEquals($testGroup['code'], $group->getCode()); $this->assertEquals($testGroup['tax_class_id'], $group->getTaxClassId()); $this->assertEquals($testGroup['tax_class_name'], $group->getTaxClassName()); }
/** * Test retrieving a valid group form. */ public function testGetForm() { $this->registry->register(RegistryConstants::CURRENT_GROUP_ID, $this->groupManagement->getDefaultGroup(0)->getId()); /** @var $block Form */ $block = $this->layout->createBlock('Magento\\Customer\\Block\\Adminhtml\\Group\\Edit\\Form', 'block'); $form = $block->getForm(); $this->assertEquals('edit_form', $form->getId()); $this->assertEquals('post', $form->getMethod()); $baseFieldSet = $form->getElement('base_fieldset'); $this->assertNotNull($baseFieldSet); $groupCodeElement = $form->getElement('customer_group_code'); $this->assertNotNull($groupCodeElement); $taxClassIdElement = $form->getElement('tax_class_id'); $this->assertNotNull($taxClassIdElement); $idElement = $form->getElement('id'); $this->assertNotNull($idElement); $this->assertEquals('1', $idElement->getValue()); $this->assertEquals('3', $taxClassIdElement->getValue()); /** @var \Magento\Tax\Model\TaxClass\Source\Customer $taxClassCustomer */ $taxClassCustomer = Bootstrap::getObjectManager()->get('Magento\\Tax\\Model\\TaxClass\\Source\\Customer'); $this->assertEquals($taxClassCustomer->toOptionArray(false), $taxClassIdElement->getData('values')); $this->assertEquals('General', $groupCodeElement->getValue()); }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $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(); $this->customerSession->setCustomerGroupId($defaultGroupId); } } 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(); $this->customerSession->setCustomerGroupId($newGroupId); } $customerAddress->setVatValidationResult($result); if ($this->appState->getAreaCode() == Area::AREA_FRONTEND) { if ($result->getIsValid()) { $this->addValidMessage($customerAddress, $result); } elseif ($result->getRequestSuccess()) { $this->addInvalidMessage($customerAddress); } else { $this->addErrorMessage($customerAddress); } } } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }
/** * Fetch default customer tax class * * @param null|Store|string|int $store * @return int */ public function getDefaultCustomerTaxClass($store = null) { if ($this->_defaultCustomerTaxClass === null) { //Not catching the exception here since default group is expected $defaultCustomerGroup = $this->customerGroupManagement->getDefaultGroup($store); $this->_defaultCustomerTaxClass = $defaultCustomerGroup->getTaxClassId(); } return $this->_defaultCustomerTaxClass; }