From b0127f6c830f6db2d341d6c2af025d7b92708dd0 Mon Sep 17 00:00:00 2001 From: alexsolonenko Date: Sat, 1 Apr 2023 08:58:38 +0300 Subject: [PATCH 1/2] Core - add parent customer functionality for sales documents --- Block/Adminhtml/CustomerAttributeList.php | 38 ++++-- .../Adminhtml/Edit/Tab/CustomerAttributes.php | 34 ++---- Service/Customer/GetParentCustomer.php | 44 +++++++ .../Customer/ParentResolveCustomerSession.php | 85 +++++++++++++ Service/PrepareSalesRequestQuery.php | 9 +- Setup/InstallData.php | 45 ------- .../Data/AddCustomerCompanyAttributes.php | 71 +++++++++++ Setup/UpgradeData.php | 115 ++++++------------ composer.json | 2 +- etc/di.xml | 12 ++ .../tab/view/customer_attribute_list.phtml | 18 ++- 11 files changed, 305 insertions(+), 168 deletions(-) create mode 100644 Service/Customer/GetParentCustomer.php create mode 100644 Service/Customer/ParentResolveCustomerSession.php create mode 100644 Setup/Patch/Data/AddCustomerCompanyAttributes.php diff --git a/Block/Adminhtml/CustomerAttributeList.php b/Block/Adminhtml/CustomerAttributeList.php index 8b0490a..49ca6cc 100644 --- a/Block/Adminhtml/CustomerAttributeList.php +++ b/Block/Adminhtml/CustomerAttributeList.php @@ -2,6 +2,8 @@ namespace Commerce365\Core\Block\Adminhtml; +use Commerce365\Core\Service\Customer\GetParentCustomer; +use Magento\Backend\Model\Url; use Magento\Customer\Api\Data\CustomerInterface; use Magento\Customer\Api\Data\CustomerInterfaceFactory; use Magento\Framework\Api\DataObjectHelper; @@ -11,17 +13,23 @@ class CustomerAttributeList extends \Magento\Backend\Block\Template private $customer; private CustomerInterfaceFactory $customerDataFactory; private DataObjectHelper $dataObjectHelper; + private GetParentCustomer $getParentCustomer; + private Url $urlBuilder; public function __construct( \Magento\Backend\Block\Template\Context $context, CustomerInterfaceFactory $customerDataFactory, DataObjectHelper $dataObjectHelper, + GetParentCustomer $getParentCustomer, + Url $urlBuilder, array $data = [] ) { $this->customerDataFactory = $customerDataFactory; $this->dataObjectHelper = $dataObjectHelper; parent::__construct($context, $data); + $this->getParentCustomer = $getParentCustomer; + $this->urlBuilder = $urlBuilder; } public function getCustomer() @@ -58,16 +66,6 @@ public function getBcContactNo() return ''; } - public function getParentCustomerId() - { - $customer = $this->getCustomer(); - if ($customer->getCustomAttribute('parent_customer_id')) { - return $customer->getCustomAttribute('parent_customer_id')->getValue(); - } - - return ''; - } - public function getBcCustomerName() { $customer = $this->getCustomer(); @@ -180,4 +178,24 @@ public function getBcCustomerBlocked() return "Unknown"; } } + + public function getParentCustomerUrl() + { + $parent = $this->getParentCustomer->execute($this->getCustomer()); + if (!$parent) { + return ''; + } + + return $this->urlBuilder->getUrl('*/*/edit', ['id' => $parent->getId()]); + } + + public function getParentCustomerEmail() + { + $parent = $this->getParentCustomer->execute($this->getCustomer()); + if (!$parent) { + return ''; + } + + return $parent->getEmail(); + } } diff --git a/Block/Adminhtml/Edit/Tab/CustomerAttributes.php b/Block/Adminhtml/Edit/Tab/CustomerAttributes.php index 49dba00..2b2be17 100644 --- a/Block/Adminhtml/Edit/Tab/CustomerAttributes.php +++ b/Block/Adminhtml/Edit/Tab/CustomerAttributes.php @@ -2,14 +2,15 @@ namespace Commerce365\Core\Block\Adminhtml\Edit\Tab; +use Commerce365\Core\Block\Adminhtml\CustomerAttributeList; use Magento\Backend\Block\Widget\Form\Generic; use Magento\Customer\Controller\RegistryConstants; +use Magento\Framework\Phrase; use Magento\Ui\Component\Layout\Tabs\TabInterface; class CustomerAttributes extends Generic implements TabInterface { protected $_coreRegistry; - protected $_systemStore; public function __construct( \Magento\Backend\Block\Template\Context $context, @@ -19,7 +20,6 @@ public function __construct( array $data = [] ) { $this->_coreRegistry = $registry; - $this->_systemStore = $systemStore; parent::__construct($context, $registry, $formFactory, $data); } @@ -30,24 +30,18 @@ public function getCustomerId() { return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); } - /** - * @return \Magento\Framework\Phrase - */ - public function getTabLabel() + + public function getTabLabel(): Phrase { return __('Commerce 365 label'); } - /** - * @return \Magento\Framework\Phrase - */ - public function getTabTitle() + + public function getTabTitle(): Phrase { return __('Commerce 365'); } - /** - * @return bool - */ - public function canShowTab() + + public function canShowTab(): bool { if ($this->getCustomerId()) { return true; @@ -58,7 +52,7 @@ public function canShowTab() /** * @return bool */ - public function isHidden() + public function isHidden(): bool { if ($this->getCustomerId()) { return false; @@ -70,7 +64,7 @@ public function isHidden() * * @return string */ - public function getTabClass() + public function getTabClass(): string { return ''; } @@ -81,9 +75,7 @@ public function getTabClass() */ public function getTabUrl() { - //replace the tab with the url you want return ''; - //return $this->getUrl('test/*/test', ['_current' => true]); } /** * Tab should be loaded trough Ajax call @@ -106,11 +98,9 @@ protected function _toHtml() public function getFormHtml() { - $html = $this->getLayout() - ->createBlock('Commerce365\Core\Block\Adminhtml\CustomerAttributeList') + return $this->getLayout() + ->createBlock(CustomerAttributeList::class) ->setTemplate('Commerce365_Core::tab/view/customer_attribute_list.phtml') ->toHtml(); - - return $html; } } diff --git a/Service/Customer/GetParentCustomer.php b/Service/Customer/GetParentCustomer.php new file mode 100644 index 0000000..8715378 --- /dev/null +++ b/Service/Customer/GetParentCustomer.php @@ -0,0 +1,44 @@ +customerRepository = $customerRepository; + } + + public function execute(CustomerInterface $customer): CustomerInterface + { + if (!$customer->getCustomAttribute('parent_customer_id')) { + return $customer; + } + + $parentId = $customer->getCustomAttribute('parent_customer_id')->getValue(); + try { + return $this->customerRepository->getById($parentId); + } catch (NoSuchEntityException $e) { + return $customer; + } + } + + public function getByCustomerId($customerId) + { + try { + $customer = $this->customerRepository->getById($customerId); + } catch (NoSuchEntityException $e) { + return null; + } + + return $this->execute($customer); + } +} diff --git a/Service/Customer/ParentResolveCustomerSession.php b/Service/Customer/ParentResolveCustomerSession.php new file mode 100644 index 0000000..ccaeb64 --- /dev/null +++ b/Service/Customer/ParentResolveCustomerSession.php @@ -0,0 +1,85 @@ +getParentCustomer = $getParentCustomer; + } + + public function getCustomer() + { + $customer = parent::getCustomer(); + + return $this->getParentCustomer->execute($customer); + } + + public function getCustomerId() + { + $customer = parent::getCustomer(); + + return $this->getParentCustomer->execute($customer)->getId(); + } +} diff --git a/Service/PrepareSalesRequestQuery.php b/Service/PrepareSalesRequestQuery.php index fafcc8c..bb47cf5 100644 --- a/Service/PrepareSalesRequestQuery.php +++ b/Service/PrepareSalesRequestQuery.php @@ -5,6 +5,7 @@ namespace Commerce365\Core\Service; use Commerce365\Core\Model\MainConfig; +use Commerce365\Core\Service\Customer\ParentResolveCustomerSession; use Magento\Customer\Model\Session; class PrepareSalesRequestQuery @@ -12,11 +13,7 @@ class PrepareSalesRequestQuery private MainConfig $mainConfig; private Session $customerSession; - /** - * @param MainConfig $mainConfig - * @param Session $customerSession - */ - public function __construct(MainConfig $mainConfig, Session $customerSession) + public function __construct(MainConfig $mainConfig, ParentResolveCustomerSession $customerSession) { $this->mainConfig = $mainConfig; $this->customerSession = $customerSession; @@ -25,8 +22,6 @@ public function execute(array $query) { $query['customerId'] = $this->customerSession->getCustomer()->getId(); -// $this->c365Helper->LogDebug('Retrieving sales documents for customer: ' . $query['customerId']); - //config returns 0 or 1, but we need false or true for our querystring parameter $query['webOrdersOnly'] = true; if ($this->mainConfig->getIncludeNonWebOrders()) { diff --git a/Setup/InstallData.php b/Setup/InstallData.php index 025fc9c..c2d8351 100644 --- a/Setup/InstallData.php +++ b/Setup/InstallData.php @@ -5,55 +5,10 @@ use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; -use Magento\Customer\Model\Customer; -use Magento\Customer\Setup\CustomerSetupFactory; - -use Magento\Eav\Setup\EavSetup; -use Magento\Eav\Setup\EavSetupFactory; class InstallData implements InstallDataInterface { - private $customerSetupFactory; - private $c365Helper; - private $eavSetupFactory; - - public function __construct(CustomerSetupFactory $customerSetupFactory, \Commerce365\Core\Helper\Data $helper, EavSetupFactory $eavSetupFactory) - { - $this->customerSetupFactory = $customerSetupFactory; - $this->c365Helper = $helper; - $this->eavSetupFactory = $eavSetupFactory; - } - public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { - $this->c365Helper->LogInfo('Commerce 365 Configuration Module - Install data (intial)'); - //$this->cleanup($setup); - - $this->c365Helper->LogEmergency('Testing the logging mechanism: Emergency'); - $this->c365Helper->LogCritical('Testing the logging mechanism: Critical'); - $this->c365Helper->LogError('Testing the logging mechanism: Error', ['param1' => 'value1']); - $this->c365Helper->LogWarning('Testing the logging mechanism: Warning'); - $this->c365Helper->LogInfo('Testing the logging mechanism: Info'); - $this->c365Helper->LogDebug('Testing the logging mechanism: Debug'); - } - - private function cleanup(ModuleDataSetupInterface $setup) - { - $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_customer_no'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_contact_no'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'parent_customer_id'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_company_name'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_customer_price_group'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_customer_discount_group'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_payment_terms_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_payment_method_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_shipment_method_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_shipment_agent_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_shipment_agent_service_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_location_code'); - $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'bc_blocked_code'); - - $this->c365Helper->LogInfo('Removed existing customer custom attributes'); } } diff --git a/Setup/Patch/Data/AddCustomerCompanyAttributes.php b/Setup/Patch/Data/AddCustomerCompanyAttributes.php new file mode 100644 index 0000000..9f50527 --- /dev/null +++ b/Setup/Patch/Data/AddCustomerCompanyAttributes.php @@ -0,0 +1,71 @@ +setupFactory = $setupFactory; + $this->moduleDataSetup = $moduleDataSetup; + $this->eavConfig = $eavConfig; + } + + public function getAliases(): array + { + return []; + } + + public static function getDependencies(): array + { + return []; + } + + public function apply() + { + $this->createAttribute('bc_contact_no', 'BC Contact Number'); + $this->createAttribute('parent_customer_id', 'Parent Customer ID for Contacts'); + } + + private function createAttribute($code, $label) + { + $eavSetup = $this->setupFactory->create(['setup' => $this->moduleDataSetup]); + $eavSetup->addAttribute( + Customer::ENTITY, + 'parent_customer_id', + [ + 'type' => 'varchar', + 'label' => 'Parent Customer ID for Contacts', + 'input' => 'text', + 'required' => false, + 'is_used_in_grid' => true, + 'is_visible_in_grid' => true, + 'is_filterable_in_grid' => true, + 'position' => 300, + 'system' => false, + 'visible' => false + ] + ); + + $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'parent_customer_id'); + + if ($attribute) { + $attribute->setData('used_in_forms', ['adminhtml_customer'])->save(); + } + } +} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 241ab7e..e90146f 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -2,105 +2,66 @@ namespace Commerce365\Core\Setup; -use Magento\Framework\Setup\UpgradeDataInterface; -use Magento\Framework\Setup\ModuleContextInterface; -use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Customer\Model\Customer; use Magento\Customer\Setup\CustomerSetupFactory; +use Magento\Framework\Setup\ModuleContextInterface; +use Magento\Framework\Setup\ModuleDataSetupInterface; -use Magento\Eav\Setup\EavSetup; -use Magento\Eav\Setup\EavSetupFactory; +use Magento\Framework\Setup\UpgradeDataInterface; class UpgradeData implements UpgradeDataInterface { - private $customerSetupFactory; - private $eavSetupFactory; - private $c365Helper; + private CustomerSetupFactory $customerSetupFactory; - public function __construct(CustomerSetupFactory $customerSetupFactory, EavSetupFactory $eavSetupFactory, \Commerce365\Core\Helper\Data $helper) + public function __construct(CustomerSetupFactory $customerSetupFactory) { $this->customerSetupFactory = $customerSetupFactory; - $this->eavSetupFactory = $eavSetupFactory; - $this->c365Helper = $helper; } public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { - - $this->c365Helper->LogInfo('Commerce 365 Configuration Module - Upgrade data'); - - $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_customer_no', 'BC Customer Number', 'varchar', 'text', 300, false, '', true); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_contact_no', 'BC Contact Number', 'varchar', 'text', 300, false, '', true); - $this->CheckAndCreateCustomerAttribute($setup, 'parent_customer_id', 'Parent Customer ID for Contacts', 'varchar', 'text', 300, false, '', true); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_company_name', 'Company Name', 'varchar', 'text', 305, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_customer_price_group', 'Customer Price Group', 'varchar', 'text', 310, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_customer_discount_group', 'Customer Discount Group', 'varchar', 'text', 311, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_payment_terms_code', 'Payment Terms Code', 'varchar', 'text', 320, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_payment_method_code', 'Payment Method Code', 'varchar', 'text', 321, false); - - $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_method_code', 'Shipment Method Code', 'varchar', 'text', 322, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_agent_code', 'Shipment Method Agent Code', 'varchar', 'text', 323, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_agent_service_code', 'Shipment Agent Service Code', 'varchar', 'text', 324, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_location_code', 'Location Code', 'varchar', 'text', 325, false); - $this->CheckAndCreateCustomerAttribute($setup, 'bc_blocked_code', 'Blocked Code', 'varchar', 'text', 326, false); - - $this->c365Helper->LogInfo('Commerce 365 Configuration Module - Upgrade data finished'); - $this->c365Helper->LogInfo('Good to see that you have upgraded. You are now running version 2.0.0 of the Commerce 365 M2 Configuration Module'); - - //update the bc_customer_no field to make it work properly in the admin grid - $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); - $customerSetup->updateAttribute(\Magento\Customer\Model\Customer::ENTITY,'bc_customer_no', - [ - 'visible' => true, - 'is_used_in_grid' => true, - 'is_visible_in_grid' => true, - 'is_filterable_in_grid' => true, - 'is_searchable_in_grid' => true - ]); - $customerSetup->updateAttribute(\Magento\Customer\Model\Customer::ENTITY,'bc_contact_no', - [ - 'visible' => true, - 'is_used_in_grid' => true, - 'is_visible_in_grid' => true, - 'is_filterable_in_grid' => true, - 'is_searchable_in_grid' => true - ]); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_company_name', 'Company Name', 'varchar', 'text', 305); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_customer_price_group', 'Customer Price Group', 'varchar', 'text', 310); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_customer_discount_group', 'Customer Discount Group', 'varchar', 'text', 311); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_payment_terms_code', 'Payment Terms Code', 'varchar', 'text', 320); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_payment_method_code', 'Payment Method Code', 'varchar', 'text', 321); + + $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_method_code', 'Shipment Method Code', 'varchar', 'text', 322); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_agent_code', 'Shipment Method Agent Code', 'varchar', 'text', 323); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_shipment_agent_service_code', 'Shipment Agent Service Code', 'varchar', 'text', 324); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_location_code', 'Location Code', 'varchar', 'text', 325); + $this->CheckAndCreateCustomerAttribute($setup, 'bc_blocked_code', 'Blocked Code', 'varchar', 'text', 326); } private function CheckAndCreateCustomerAttribute(ModuleDataSetupInterface $setup, string $attributeCode, string $label, string $type, string $input, int $position = 500, bool $visible = false, string $source = '', bool $usedInGrid = false) { $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); - if ($customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode) == false) { - $this->c365Helper->LogInfo('Customer attribute ' . $attributeCode . ' does not exist. Creating..'); - - $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); + if ($customerSetup->getAttribute(Customer::ENTITY, $attributeCode)) { + return; + } - $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode, [ - 'type' => $type, - 'label' => $label, - 'input' => $input, - 'source' => $source, - 'required' => false, - 'visible' => $visible, - 'position' => $position, - 'system' => false, - 'backend' => '', - 'is_used_in_grid' => $usedInGrid, - 'is_visible_in_grid' => $usedInGrid - ]); + $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); - $attribute = $customerSetup->getEavConfig()->getAttribute('customer', $attributeCode) - ->addData(['used_in_forms' => [ - 'adminhtml_customer' - ]]); - $attribute->save(); + $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, [ + 'type' => $type, + 'label' => $label, + 'input' => $input, + 'source' => $source, + 'required' => false, + 'visible' => $visible, + 'position' => $position, + 'system' => false, + 'backend' => '', + 'is_used_in_grid' => $usedInGrid, + 'is_visible_in_grid' => $usedInGrid + ]); - $this->c365Helper->LogInfo('Done!'); - } else { - $this->c365Helper->LogInfo('Attribute with code: ' . $attributeCode . ' already exists! Run clean method first in case you want to update.'); - } + $attribute = $customerSetup->getEavConfig()->getAttribute('customer', $attributeCode) + ->addData(['used_in_forms' => [ + 'adminhtml_customer' + ]]); + $attribute->save(); } } diff --git a/composer.json b/composer.json index 1eaf2bb..e039df3 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "commerce365/module-core", - "version": "1.0.1", + "version": "1.1.0", "description": "N/A", "type": "magento2-module", "require": { diff --git a/etc/di.xml b/etc/di.xml index cacee02..6abaa99 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -20,4 +20,16 @@ + + + Magento\Framework\Session\Generic\Proxy + + + + + + Commerce365\Core\Service\Customer\ParentResolveCustomerSession\Proxy + + + diff --git a/view/adminhtml/templates/tab/view/customer_attribute_list.phtml b/view/adminhtml/templates/tab/view/customer_attribute_list.phtml index 0af8ee3..5e06abb 100644 --- a/view/adminhtml/templates/tab/view/customer_attribute_list.phtml +++ b/view/adminhtml/templates/tab/view/customer_attribute_list.phtml @@ -68,12 +68,18 @@ $allowedAddressHtmlTags = ['b', 'br', 'em', 'i', 'li', 'ol', 'p', 'strong', 'sub escapeHtml(__('Blocked Status:')) ?> escapeHtml($block->getBcCustomerBlocked()) ?> - - - - - - + + escapeHtml(__('Contact Number:')) ?> + escapeHtml($block->getBcContactNo()) ?> + + + escapeHtml(__('Parent Customer:')) ?> + + + escapeHtml($block->getParentCustomerEmail()) ?> + + +
From 5c959c17a99a8de4a1fa8f372c6056ca06fd6aaa Mon Sep 17 00:00:00 2001 From: alexsolonenko Date: Tue, 4 Apr 2023 23:51:46 +0300 Subject: [PATCH 2/2] Core - set default sorting for documents --- Block/AbstractList.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Block/AbstractList.php b/Block/AbstractList.php index ac04312..6482765 100644 --- a/Block/AbstractList.php +++ b/Block/AbstractList.php @@ -113,6 +113,8 @@ public function getSortDirection() */ protected function addSortingToQuery(array $query): array { + $query['sortColumn'] = 'Document Date'; + $query['sortDirection'] = 'DESC'; if ($this->getSortColumn() && $this->getSortDirection()) { $query['sortColumn'] = $this->getSortColumn(); $query['sortDirection'] = $this->getSortDirection();