/** * 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(); } }
/** * Retrieve disable auto group change checkbox state * * @return string */ public function getDisableAutoGroupChangeCheckboxState() { $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); $checkedByDefault = $customerId ? false : $this->_addressHelper->isDisableAutoGroupAssignDefaultValue(); $value = $this->getDisableAutoGroupChangeAttributeValue(); $state = ''; if (!empty($value) || $checkedByDefault) { $state = 'checked'; } return $state; }
/** * 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; }
/** * @param string|null $configAddressType * @dataProvider restoreCustomerGroupIdDataProvider */ public function testExecute($configAddressType) { $quoteAddress = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', ['getQuote', 'setCustomerGroupId', 'getPrevQuoteCustomerGroupId', 'unsPrevQuoteCustomerGroupId', 'hasPrevQuoteCustomerGroupId'], [], '', false); $observer = $this->getMock('Magento\\Framework\\Event\\Observer', ['getQuoteAddress'], [], '', false); $observer->expects($this->once())->method('getQuoteAddress')->will($this->returnValue($quoteAddress)); $this->customerAddressHelperMock->expects($this->once())->method('getTaxCalculationAddressType')->will($this->returnValue($configAddressType)); $quoteAddress->expects($this->once())->method('hasPrevQuoteCustomerGroupId'); $id = $quoteAddress->expects($this->any())->method('getPrevQuoteCustomerGroupId'); $quoteAddress->expects($this->any())->method('setCustomerGroupId')->with($id); $quoteAddress->expects($this->any())->method('getQuote'); $quoteAddress->expects($this->any())->method('unsPrevQuoteCustomerGroupId'); $this->assertNull($this->quote->execute($observer)); }
/** * 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 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); } }
/** * @param string $configAddressType * @param string|int $vatRequestId * @param string|int $vatRequestDate * @param string $orderHistoryComment * @dataProvider addVatRequestParamsOrderCommentDataProvider */ public function testAddVatRequestParamsOrderComment($configAddressType, $vatRequestId, $vatRequestDate, $orderHistoryComment) { $this->customerAddressHelperMock->expects($this->once())->method('getTaxCalculationAddressType')->will($this->returnValue($configAddressType)); $orderAddressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', ['getVatRequestId', 'getVatRequestDate', '__wakeup'], [], '', false); $orderAddressMock->expects($this->any())->method('getVatRequestId')->will($this->returnValue($vatRequestId)); $orderAddressMock->expects($this->any())->method('getVatRequestDate')->will($this->returnValue($vatRequestDate)); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['getShippingAddress', '__wakeup', 'addStatusHistoryComment', 'getBillingAddress'])->getMock(); $orderMock->expects($this->any())->method('getShippingAddress')->will($this->returnValue($orderAddressMock)); if ($orderHistoryComment === null) { $orderMock->expects($this->never())->method('addStatusHistoryComment'); } else { $orderMock->expects($this->once())->method('addStatusHistoryComment')->with($orderHistoryComment, false); } $observer = $this->getMock('Magento\\Framework\\Event\\Observer', ['getOrder'], [], '', false); $observer->expects($this->once())->method('getOrder')->will($this->returnValue($orderMock)); $this->assertNull($this->observer->execute($observer)); }
/** * Represent customer address in 'online' format. * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return string */ public function getAddressAsString(\Magento\Customer\Api\Data\AddressInterface $address) { $formatTypeRenderer = $this->_addressHelper->getFormatTypeRenderer('oneline'); $result = ''; if ($formatTypeRenderer) { $result = $formatTypeRenderer->renderArray($this->addressMapper->toFlatArray($address)); } return $this->escapeHtml($result); }
/** * @param string $code * @param array $result * @dataProvider getFormatDataProvider */ public function testGetFormat($code, $result) { if ($result) { $renderer = $this->getMockBuilder('Magento\\Customer\\Block\\Address\\Renderer\\RendererInterface')->disableOriginalConstructor()->getMock(); $renderer->expects($this->once())->method('getFormatArray')->will($this->returnValue(array('key' => 'value'))); } $this->addressConfig->expects($this->once())->method('getFormatByCode')->with($code)->will($this->returnValue(new \Magento\Framework\Object(!empty($result) ? array('renderer' => $renderer) : array()))); $this->assertEquals($result, $this->helper->getFormat($code)); }
/** * Retrieve field configuration for street address attribute * * @param string $attributeCode * @param array $attributeConfig * @param string $providerName name of the storage container used by UI component * @param string $dataScopePrefix * @return array */ protected function getStreetFieldConfig($attributeCode, array $attributeConfig, $providerName, $dataScopePrefix) { $streetLines = []; for ($lineIndex = 0; $lineIndex < $this->addressHelper->getStreetLines(); $lineIndex++) { $isFirstLine = $lineIndex === 0; $streetLines[] = ['component' => 'Magento_Ui/js/form/element/abstract', 'config' => ['customScope' => $dataScopePrefix, 'template' => 'ui/form/field', 'elementTmpl' => 'ui/form/element/input'], 'dataScope' => $lineIndex, 'provider' => $providerName, 'validation' => $isFirstLine ? ['required-entry' => true] : [], 'additionalClasses' => $isFirstLine ?: 'additional']; } return ['component' => 'Magento_Ui/js/form/components/group', 'label' => __('Address'), 'required' => true, 'dataScope' => $dataScopePrefix . '.' . $attributeCode, 'provider' => $providerName, 'sortOrder' => $attributeConfig['sortOrder'], 'type' => 'group', 'config' => ['template' => 'ui/group/group', 'additionalClasses' => 'street'], 'children' => $streetLines]; }
/** * @return string|null */ public function getBillingAddressHtml() { try { $address = $this->_addressService->getAddress($this->getCustomer()->getDefaultBilling()); } catch (NoSuchEntityException $e) { return __('The customer does not have default billing address.'); } return $this->_addressHelper->getFormatTypeRenderer('html')->renderArray(AddressConverter::toFlatArray($address)); }
/** * Get billing address request data * * @param \Magento\Framework\Object $address * @return array */ protected function _getBillingAddress(\Magento\Framework\Object $address) { $request = array('billing_first_name' => $address->getFirstname(), 'billing_last_name' => $address->getLastname(), 'billing_city' => $address->getCity(), 'billing_state' => $address->getRegionCode() ? $address->getRegionCode() : $address->getCity(), 'billing_zip' => $address->getPostcode(), 'billing_country' => $address->getCountry()); // convert streets to tow lines format $street = $this->_customerAddress->convertStreetLines($address->getStreet(), 2); $request['billing_address1'] = isset($street[0]) ? $street[0] : ''; $request['billing_address2'] = isset($street[1]) ? $street[1] : ''; return $request; }
/** * Represent customer address in HTML format. * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return string */ public function getAddressAsHtml(\Magento\Customer\Api\Data\AddressInterface $address) { $formatTypeRenderer = $this->_customerAddressHelper->getFormatTypeRenderer('html'); $result = ''; if ($formatTypeRenderer) { $result = $formatTypeRenderer->renderArray($this->addressMapper->toFlatArray($address)); } return $result; }
/** * Represent customer address in 'online' format. * * @param \Magento\Customer\Service\V1\Data\Address $addressData * @return string */ public function getAddressAsString($addressData) { $formatTypeRenderer = $this->_addressHelper->getFormatTypeRenderer('oneline'); $result = ''; if ($formatTypeRenderer) { $result = $formatTypeRenderer->renderArray(AddressConverter::toFlatArray($addressData)); } return $this->escapeHtml($result); }
/** * Represent customer address in HTML format. * * @param \Magento\Customer\Service\V1\Data\Address $addressData * @return string */ public function getAddressAsHtml($addressData) { $formatTypeRenderer = $this->_customerAddressHelper->getFormatTypeRenderer('html'); $result = ''; if ($formatTypeRenderer) { $result = $formatTypeRenderer->renderArray(AddressConverter::toFlatArray($addressData)); } return $result; }
/** * 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 if address attribute is visible on frontend * * @param string $attributeCode * @param array $attributeConfig * @param array $additionalConfig field configuration provided via layout XML * @return bool */ protected function isFieldVisible($attributeCode, array $attributeConfig, array $additionalConfig = []) { // TODO move this logic to separate model so it can be customized if ($attributeConfig['visible'] == false || isset($additionalConfig['visible']) && $additionalConfig['visible'] == false) { return false; } if ($attributeCode == 'vat_id' && !$this->addressHelper->isVatAttributeVisible()) { return false; } return true; }
/** * Retrieve default address format * * @return \Magento\Framework\Object */ protected function _getDefaultFormat() { $store = $this->getStore(); $storeId = $store->getId(); if (!isset($this->_defaultTypes[$storeId])) { $this->_defaultTypes[$storeId] = new \Magento\Framework\Object(); $this->_defaultTypes[$storeId]->setCode('default')->setDefaultFormat('{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}' . '{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}, ' . '{{var street}}, {{var city}}, {{var region}} {{var postcode}}, {{var country}}'); $this->_defaultTypes[$storeId]->setRenderer($this->_addressHelper->getRenderer(self::DEFAULT_ADDRESS_RENDERER)->setType($this->_defaultTypes[$storeId])); } return $this->_defaultTypes[$storeId]; }
/** * Retrieve billing address html * * @return \Magento\Framework\Phrase|string */ public function getBillingAddressHtml() { try { $address = $this->accountManagement->getDefaultBillingAddress($this->getCustomer()->getId()); } catch (NoSuchEntityException $e) { return __('The customer does not have default billing address.'); } if ($address === null) { return __('The customer does not have default billing address.'); } return $this->addressHelper->getFormatTypeRenderer('html')->renderArray($this->addressMapper->toFlatArray($address)); }
/** * @param string|null $configAddressType * @dataProvider restoreCustomerGroupIdDataProvider */ public function testExecute($configAddressType) { $eventMock = $this->getMock('\\Magento\\Framework\\Event', ['getShippingAssignment', 'getQuote'], [], '', false); $observer = $this->getMock('Magento\\Framework\\Event\\Observer', ['getEvent'], [], '', false); $observer->expects($this->exactly(2))->method('getEvent')->willReturn($eventMock); $shippingAssignmentMock = $this->getMock('\\Magento\\Quote\\Api\\Data\\ShippingAssignmentInterface'); $quoteMock = $this->getMock('\\Magento\\Quote\\Model\\Quote', [], [], '', false); $eventMock->expects($this->once())->method('getShippingAssignment')->willReturn($shippingAssignmentMock); $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock); $shippingMock = $this->getMock('\\Magento\\Quote\\Api\\Data\\ShippingInterface'); $shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock); $quoteAddress = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Address', ['getPrevQuoteCustomerGroupId', 'unsPrevQuoteCustomerGroupId', 'hasPrevQuoteCustomerGroupId'], [], '', false); $shippingMock->expects($this->once())->method('getAddress')->willReturn($quoteAddress); $this->customerAddressHelperMock->expects($this->once())->method('getTaxCalculationAddressType')->will($this->returnValue($configAddressType)); $quoteAddress->expects($this->once())->method('hasPrevQuoteCustomerGroupId'); $id = $quoteAddress->expects($this->any())->method('getPrevQuoteCustomerGroupId'); $quoteAddress->expects($this->any())->method('setCustomerGroupId')->with($id); $quoteAddress->expects($this->any())->method('getQuote'); $quoteAddress->expects($this->any())->method('unsPrevQuoteCustomerGroupId'); $this->quote->execute($observer); }
/** * Export streets from address data * * @param DataObject $address * @return array */ protected function getAddressStreets(DataObject $address) { $street1 = ''; $street2 = ''; $data = $this->customerAddress->convertStreetLines($address->getStreet(), 2); if (!empty($data[0])) { $street1 = $data[0]; } if (!empty($data[1])) { $street2 = $data[1]; } return [$street1, $street2]; }
/** * 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); }
/** * Street address workaround: divides address lines into parts by specified keys * (keys should go as 3rd, 4th[...] parameters) * * @param \Magento\Framework\Object $address * @param array $to * @return void */ protected function _importStreetFromAddress(\Magento\Framework\Object $address, array &$to) { $keys = func_get_args(); array_shift($keys); array_shift($keys); $street = $address->getStreet(); if (!$keys || !$street || !is_array($street)) { return; } $street = $this->_customerAddress->convertStreetLines($address->getStreet(), count($keys)); $i = 0; foreach ($keys as $key) { $to[$key] = isset($street[$i]) ? $street[$i] : ''; $i++; } }
/** * @param $customerId * @param $key * @param $vatValidationEnabled * @param $addressType * @param $successMessage * * @dataProvider getSuccessMessageDataProvider */ public function testSuccessMessage($customerId, $key, $vatValidationEnabled, $addressType, $successMessage) { $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false)); $this->requestMock->expects($this->any())->method('getParam')->willReturnMap([['id', false, $customerId], ['key', false, $key]]); $this->customerRepositoryMock->expects($this->any())->method('getById')->with($customerId)->will($this->returnValue($this->customerDataMock)); $email = '*****@*****.**'; $this->customerDataMock->expects($this->once())->method('getEmail')->will($this->returnValue($email)); $this->customerAccountManagementMock->expects($this->once())->method('activate')->with($this->equalTo($email), $this->equalTo($key))->will($this->returnValue($this->customerDataMock)); $this->customerSessionMock->expects($this->any())->method('setCustomerDataAsLoggedIn')->with($this->equalTo($this->customerDataMock))->willReturnSelf(); $this->messageManagerMock->expects($this->any())->method('addSuccess')->with($this->stringContains($successMessage))->willReturnSelf(); $this->addressHelperMock->expects($this->once())->method('isVatValidationEnabled')->will($this->returnValue($vatValidationEnabled)); $this->addressHelperMock->expects($this->any())->method('getTaxCalculationAddressType')->will($this->returnValue($addressType)); $this->storeMock->expects($this->any())->method('getFrontendName')->will($this->returnValue('frontend')); $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($this->storeMock)); $this->model->execute(); }
/** * 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; }
/** * @param string $key * @return null|string */ public function getConfig($key) { return $this->_addressHelper->getConfig($key); }
/** * @param $customerId * @param $customerEmail * @param $password * @param $confirmationStatus * @param $vatValidationEnabled * @param $addressType * @param $successMessage * * @dataProvider getSuccessMessageDataProvider */ public function testSuccessMessage($customerId, $customerEmail, $password, $confirmationStatus, $vatValidationEnabled, $addressType, $successMessage) { $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false)); $this->registration->expects($this->once())->method('isAllowed')->will($this->returnValue(true)); $this->customerUrl->expects($this->once())->method('getEmailConfirmationUrl')->will($this->returnValue($customerEmail)); $this->customerSessionMock->expects($this->once())->method('regenerateId'); $this->customerMock->expects($this->any())->method('getId')->will($this->returnValue($customerId)); $this->customerMock->expects($this->any())->method('getEmail')->will($this->returnValue($customerEmail)); $this->customerExtractorMock->expects($this->any())->method('extract')->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))->will($this->returnValue($this->customerMock)); $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true)); $this->requestMock->expects($this->any())->method('getPost')->will($this->returnValue(false)); $this->requestMock->expects($this->any())->method('getParam')->willReturnMap([['password', null, $password], ['password_confirmation', null, $password], ['is_subscribed', false, true]]); $this->customerMock->expects($this->once())->method('setAddresses')->with($this->equalTo([]))->will($this->returnSelf()); $this->accountManagement->expects($this->once())->method('createAccount')->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')->will($this->returnValue($this->customerMock)); $this->accountManagement->expects($this->once())->method('getConfirmationStatus')->with($this->equalTo($customerId))->will($this->returnValue($confirmationStatus)); $this->subscriberMock->expects($this->once())->method('subscribeCustomerById')->with($this->equalTo($customerId)); $this->messageManagerMock->expects($this->any())->method('addSuccess')->with($this->stringContains($successMessage))->will($this->returnSelf()); $this->addressHelperMock->expects($this->any())->method('isVatValidationEnabled')->will($this->returnValue($vatValidationEnabled)); $this->addressHelperMock->expects($this->any())->method('getTaxCalculationAddressType')->will($this->returnValue($addressType)); $this->model->execute(); }
/** * @param $attributeCode * @dataProvider getAttributeValidationClass */ public function testGetAttributeValidationClass($attributeCode, $expectedClass) { $this->assertEquals($expectedClass, $this->helper->getAttributeValidationClass($attributeCode)); }
/** * Format the given address to the given type * * @param AddressInterface $address * @param string $type * @return string */ public function format(AddressInterface $address, $type) { return $this->_addressHelper->getFormatTypeRenderer($type)->renderArray($this->addressMapper->toFlatArray($address)); }