Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/Sales/Plugin/View/Element/UiComponent/DataProvider/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Sales/Plugin/View/Element/UiComponent/DataProvider/Reporting.php
<?php
/**
 * Copyright (c) 2022 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Radosław Stępień <[email protected]> <[email protected]>
 */
namespace Cnc\Sales\Plugin\View\Element\UiComponent\DataProvider;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\Exception\NoSuchEntityException;

class Reporting
{
    public static $table = 'sales_order_grid';
    public static $leftJoinTable = 'sales_order_item';

    /**
     * @var AttributeRepositoryInterface
     */
    private $attributeRepository;

    /**
     * @var int|null
     */
    private $stateOfWearAttributeId = null;

    /**
     * @param AttributeRepositoryInterface $attributeRepository
     */
    public function __construct(AttributeRepositoryInterface $attributeRepository)
    {
        $this->attributeRepository = $attributeRepository;
    }

    /**
     * @param ReportingInterface $subject
     * @param SearchCriteriaInterface $searchCriteria
     * @return SearchCriteriaInterface[]
     * @throws \Magento\Framework\Exception\InputException
     */
    public function beforeSearch(ReportingInterface $subject, SearchCriteriaInterface $searchCriteria)
    {
        foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
            foreach ($filterGroup->getFilters() as $filter) {
                if ($filter->getField() === 'cnc_state_of_wear') {
                    $filter->setField('csow.name');
                }
            }
        }

        foreach ($searchCriteria->getSortOrders() as $sortOrder) {
            if ($sortOrder->getField() === 'cnc_state_of_wear') {
                $sortOrder->setField('csow.name');
            }
        }

        return [$searchCriteria];
    }

    /**
     * @param ReportingInterface $subject
     * @param SearchResultInterface $result
     * @param SearchCriteriaInterface $searchCriteria
     * @return SearchResultInterface
     */
    public function afterSearch(
        ReportingInterface $subject,
        SearchResultInterface $result,
        SearchCriteriaInterface $searchCriteria
    ): SearchResultInterface {
        if ($result->getMainTable() === $result->getConnection()->getTableName(self::$table)) {
            $leftJoinTableName = $result->getConnection()->getTableName(self::$leftJoinTable);
            $attributeId = $this->getStateOfWearAttributeId();
            $result->getSelect()
                ->joinLeft(
                    ['soi' => $leftJoinTableName],
                    "soi.order_id = main_table.entity_id",
                    ['soi.sku']
                );
            $result->getSelect()
                ->joinLeft(
                    ['cpei' => 'catalog_product_entity_int'],
                    "cpei.attribute_id = {$attributeId} AND cpei.entity_id = soi.product_id AND " .
                    "cpei.store_id = 0",
                    ['cpei.value']
                );
            $result->getSelect()
                ->joinLeft(
                    ['csow' => 'cnc_state_of_wear'],
                    'csow.entity_id = cpei.value',
                    ['csow.name']
                );

            $where = $result->getSelect()->getPart(Select::WHERE);
            $result->getSelect()->setPart(Select::WHERE, $where)->group('main_table.entity_id');
        }
        return $result;
    }

    /**
     * @return int|null
     */
    private function getStateOfWearAttributeId(): ?int
    {
        if (!$this->stateOfWearAttributeId) {
            try {
                $attributeId = $this->attributeRepository->get(
                    ProductAttributeInterface::ENTITY_TYPE_CODE,
                    \Cnc\Catalog\Model\Config::PRODUCT_ATTRIBUTE_CODE_STATE_OF_WEAR
                )->getAttributeId();

                $this->stateOfWearAttributeId = $attributeId ? (int)$attributeId : null;
            } catch (NoSuchEntityException $e) {
                return null;
            }
        }

        return $this->stateOfWearAttributeId;
    }
}

Spamworldpro Mini