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/Kaliop/ProductGridSalesCount/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Kaliop/ProductGridSalesCount/Model/Calculator.php
<?php
/**
 * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Krzysztof Majkowski <[email protected]>
 */

namespace Kaliop\ProductGridSalesCount\Model;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Action;
use Magento\Framework\App\ResourceConnection;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream;

class Calculator
{
    const LOG_FILE = 'numberofsales.log';

    /**
     * @var ResourceConnection
     */
    private $connection;

    /**
     * @var Action
     */
    private $action;

    /**
     * @var \Magento\Eav\Model\Config
     */
    private $eavConfig;

    /**
     * Calculator constructor.
     * @param ResourceConnection $connection
     * @param Action $action
     * @param \Magento\Eav\Model\Config $eavConfig
     */
    public function __construct(
        ResourceConnection $connection,
        Action $action,
        \Magento\Eav\Model\Config $eavConfig
    ) {
        $this->connection = $connection;
        $this->action = $action;
        $this->eavConfig = $eavConfig;
    }

    public function execute($ids = [])
    {
        $this->log('Calculation start');
        $simples = $this->getSimpleProductsData($ids);
        $grouped = $this->getGroupedProductsData($ids);
        $this->storeData($simples);
        $this->storeData($grouped);
        $this->log('Calculation finished');
    }

    /**
     * Get simple sales quantity.
     *
     * @return array
     */
    protected function getSimpleProductsData($ids = [])
    {
        $whereId = '';
        if (is_array($ids) && count($ids)) {
            $whereId = 'AND cpe.entity_id IN (' . join(',', $ids) . ')';
        }

        $sql = <<<SQL
SELECT cpe.entity_id, SUM(soi.qty_ordered) AS qty FROM catalog_product_entity AS cpe
LEFT JOIN sales_order_item AS soi ON soi.product_id = cpe.entity_id
LEFT JOIN sales_order AS so ON so.entity_id = soi.order_id
WHERE so.state NOT IN ('canceled') {$whereId}
GROUP BY cpe.entity_id ORDER BY qty DESC
SQL;

        $con = $this->connection->getConnection();
        return $con->fetchPairs($sql);
    }

    /**
     * Get grouped sales quantity.
     *
     * @return array
     */
    protected function getGroupedProductsData($ids = [])
    {
        $whereId = '';
        if (is_array($ids) && count($ids)) {
            $whereId = 'AND cpe.entity_id IN (' . join(',', $ids) . ')';
        }

        $sql = <<<SQL
SELECT soi.item_id, cpe.entity_id, soi.qty_ordered AS qty, soi.product_options FROM catalog_product_entity AS cpe
LEFT JOIN sales_order_item AS soi ON soi.product_id = cpe.entity_id
LEFT JOIN sales_order AS so ON so.entity_id = soi.order_id
WHERE soi.product_type = 'grouped' AND so.state NOT IN ('canceled') {$whereId} ORDER BY qty DESC
SQL;

        $groupedData = [];
        $con = $this->connection->getConnection();
        $data = $con->fetchAll($sql);
        foreach ($data as $row) {
            $pattern = '/.*"product_id":"([0-9]*)"/';
            $regexValue = null;
            $groupedId = preg_match($pattern, $row['product_options'], $regexValue) ? $regexValue[1] : false;
            if (!$groupedId) {
                continue;
            }

            if (!isset($groupedData[$groupedId])) {
                $groupedData[$groupedId] = 0;
            }
            $groupedData[$groupedId] += $row['qty'];
        }

        return $groupedData;
    }

    /**
     * Save data.
     *
     * @param array $data
     */
    protected function storeData($data)
    {
        foreach ($data as $productId => $qty) {
            try {
                $numberOrdered = $this->getNumberOrdered($productId);
                $qty = ($numberOrdered ? $qty + ((int)$numberOrdered) : $qty);
                $this->action->updateAttributes(
                    [$productId],
                    [Config::NUMBER_OF_SALES_ATTRIBUTE_CODE => $qty],
                    0
                );
            } catch (\Exception $e) {
                $this->log($e->getMessage());
                $this->log($e->getTraceAsString());
            }
        }
    }

    /**
     * @param int $productId
     * @return string
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    protected function getNumberOrdered(int $productId)
    {
        $attribute = $this->eavConfig->getAttribute(Product::ENTITY, 'cnc_number_ordered');
        $attributeId = $attribute->getId();
        $attributeTable = $attribute->getBackendTable();

        $sql = "SELECT value FROM {$attributeTable} WHERE attribute_id = {$attributeId} AND entity_id = {$productId} AND store_id = 0";
        return $this->connection->getConnection()->fetchOne($sql);
    }

    private function log($message)
    {
        $writer = new Stream(BP . '/var/log/' . self::LOG_FILE);
        $logger = new Logger();
        $logger->addWriter($writer);
        $logger->info($message);
    }
}

Spamworldpro Mini