Skip to content

Commit

Permalink
[Analytics][Tracker] Feature Smile-SA#3340, add company_id and custom…
Browse files Browse the repository at this point in the history
…er_group_id in tracking data
  • Loading branch information
vahonc committed Nov 29, 2024
1 parent 06c9f8c commit 1aabcae
Show file tree
Hide file tree
Showing 18 changed files with 888 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

/**
* Block used to display customer company selector in reports.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
*/
class CustomerCompanySelector extends Template
{
/**
* Company status configuration path.
*
* @var string
*/
const CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH = 'btob/website_configuration/company_active';

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* @var \Magento\Company\Api\CompanyRepositoryInterface|null
*/
private $companyRepository = null;

/**
* CustomerCompanySelector constructor.
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @param Context $context The template context.
* @param ModuleManager $moduleManager Module manager.
* @param ScopeConfigInterface $scopeConfig Scope configuration.
* @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder.
* @param array $data Additional block data.
* @throws LocalizedException
*/
public function __construct(
Context $context,
ModuleManager $moduleManager,
ScopeConfigInterface $scopeConfig,
SearchCriteriaBuilder $searchCriteriaBuilder,
array $data = []
) {
$this->scopeConfig = $scopeConfig;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;

// Check if Magento_Company module is enabled before attempting to load the repository.
if ($moduleManager->isEnabled('Magento_Company')) {
if (interface_exists('\Magento\Company\Api\CompanyRepositoryInterface')) {
$this->companyRepository = ObjectManager::getInstance()->get(
\Magento\Company\Api\CompanyRepositoryInterface::class
);
} else {
throw new LocalizedException(__('CompanyRepositoryInterface is not available.'));
}
}

parent::__construct($context, $data);
}

/**
* Check if the Company feature is enabled.
*
* @return bool
*/
public function isCompanyEnabled()
{
return $this->scopeConfig->isSetFlag(
self::CONFIG_IS_B2B_COMPANY_ACTIVE_XPATH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Get the list of companies if the Company feature is enabled.
*
* @return CompanyInterface[]|array
* @throws LocalizedException
*/
public function getCompaniesList()
{
if ($this->isCompanyEnabled()) {
$searchCriteria = $this->searchCriteriaBuilder->create();

return $this->companyRepository->getList($searchCriteria)->getItems(); // Fetch company list.
}

return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report;

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;

/**
* Block used to display customer group selector in reports.
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
*/
class CustomerGroupSelector extends Template
{
/**
* @var CollectionFactory
*/
protected $customerGroupCollectionFactory;

/**
* CustomerGroupSelector constructor.
*
* @param Template\Context $context The context of the template.
* @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection.
* @param array $data Additional block data.
*/
public function __construct(
Template\Context $context,
CollectionFactory $customerGroupCollectionFactory,
array $data = []
) {
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
parent::__construct($context, $data);
}

/**
* Get customer groups in an option array format.
*
* @return array
*/
public function getCustomerGroups()
{
return $this->customerGroupCollectionFactory->create()->toOptionArray();
}
}
20 changes: 20 additions & 0 deletions src/module-elasticsuite-analytics/Model/Report/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public function getStoreId()
return $this->request->getParam('store');
}

/**
* Get customer group ID.
*
* @return mixed
*/
public function getCustomerGroupId()
{
return $this->request->getParam('customer_group');
}

/**
* Get customer company ID.
*
* @return mixed
*/
public function getCustomerCompanyId()
{
return $this->request->getParam('company_id');
}

/**
* Get date range.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Model\Report\Event;

use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context;

/**
* Customer company id filter query provider.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
*/
class CustomerCompanyIdFilterQueryProvider implements QueryProviderInterface
{
/**
* @var QueryFactory
*/
private $queryFactory;

/**
* @var Context
*/
private $context;

/**
* CustomerCompanyIdFilterQueryProvider constructor.
*
* @param QueryFactory $queryFactory Query factory.
* @param Context $context Report context.
*/
public function __construct(QueryFactory $queryFactory, Context $context)
{
$this->queryFactory = $queryFactory;
$this->context = $context;
}

/**
* {@inheritDoc}
*/
public function getQuery()
{
// Get customer company ID from the context.
$customerCompanyId = $this->context->getCustomerCompanyId();

// Check if customer company ID is set and not 'all'.
if ($customerCompanyId !== 'all' && $customerCompanyId !== null) {
// Return a TERM query for the customer company ID.
return $this->queryFactory->create(
QueryInterface::TYPE_TERM,
[
'field' => 'customer.company_id',
'value' => (int) $customerCompanyId,
]
);
}

// If 'all' is selected or no customer company ID is set, return null (no filtering).
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Model\Report\Event;

use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context;

/**
* Customer group id filter query provider.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
*/
class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface
{
/**
* @var QueryFactory
*/
private $queryFactory;

/**
* @var Context
*/
private $context;

/**
* CustomerGroupIdFilterQueryProvider constructor.
*
* @param QueryFactory $queryFactory Query factory.
* @param Context $context Report context.
*/
public function __construct(QueryFactory $queryFactory, Context $context)
{
$this->queryFactory = $queryFactory;
$this->context = $context;
}

/**
* {@inheritDoc}
*/
public function getQuery()
{
// Get customer group ID from the context.
$customerGroupId = $this->context->getCustomerGroupId();

// Check if customer group ID is set and not 'all'.
if ($customerGroupId !== 'all' && $customerGroupId !== null) {
// Return a TERM query for the customer group ID.
return $this->queryFactory->create(
QueryInterface::TYPE_TERM,
[
'field' => 'customer.group_id',
'value' => (int) $customerGroupId,
]
);
}

// If 'all' is selected or no customer group ID is set, return null (no filtering).
return null;
}
}
Loading

0 comments on commit 1aabcae

Please sign in to comment.