![]() 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/AdminHistory/Observer/ |
<?php /** * Copyright (c) 2020 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]> */ declare(strict_types=1); namespace Cnc\AdminHistory\Observer; use Cnc\AdminHistory\Api\Data\ProductInterface as CncHistoryProductInterface; use Cnc\AdminHistory\Model\ProductFactory; use Cnc\AdminHistory\Model\Product; use Cnc\AdminHistory\Model\ResourceModel\Product as ProductResource; use Cnc\Catalog\Api\GetProductQtyInterface; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Exception; use Magento\Backend\Model\Auth\Session; use Magento\Catalog\Model\Product as MagentoProduct; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Stdlib\DateTime\DateTime; /** * Class SaveProductHistory * observer to log changes done in product edit page */ class SaveProductHistory implements ObserverInterface { /** * Attributes used in changes logging */ const ATTRIBUTES_TO_CHECK = [ 'name', 'qty', 'source_qty', 'status' ]; /** @var ProductFactory */ protected $productHistory; /** @var ProductResource */ protected $resource; /** @var Session */ protected $authSession; /** @var DateTime */ protected $date; /** * @var GetProductQtyInterface */ private $getProductQty; /** * @var MsiAttributes */ private $msiAttributes; /** * SaveProductHistory constructor. * @param ProductFactory $productHistory * @param ProductResource $resource * @param Session $authSession * @param DateTime $date * @param GetProductQtyInterface $getProductQty * @param MsiAttributes $msiAttributes */ public function __construct( ProductFactory $productHistory, ProductResource $resource, Session $authSession, DateTime $date, GetProductQtyInterface $getProductQty, MsiAttributes $msiAttributes ) { $this->productHistory = $productHistory; $this->resource = $resource; $this->authSession = $authSession; $this->date = $date; $this->getProductQty = $getProductQty; $this->msiAttributes = $msiAttributes; } /** * @param Observer $observer * @throws AlreadyExistsException * @throws Exception */ public function execute(Observer $observer): void { $changedValues = []; /** @var MagentoProduct $product */ $product = $observer->getProduct(); $controller = $observer->getController(); foreach (self::ATTRIBUTES_TO_CHECK as $attribute) { $currentValue = null; $originData[$attribute] = null; if ($attribute == 'qty' && $product->getOrigData('quantity_and_stock_status') && $product->getData('quantity_and_stock_status')) { $originData[$attribute] = array_key_exists( $attribute, $product->getOrigData('quantity_and_stock_status') ) ? (string)$product->getOrigData('quantity_and_stock_status')[$attribute] : false; if (is_array($product->getData('quantity_and_stock_status'))) { if (array_key_exists($attribute, $product->getData('quantity_and_stock_status'))) { $currentValue = (string)$product->getData('quantity_and_stock_status')[$attribute]; } elseif (is_array($product->getStockData()) && array_key_exists('qty', $product->getStockData())) { $currentValue = $product->getStockData()['qty']; } else { //qty value is not updated in this case, so we no need to log anything about qty, just skip. continue; } } else { $currentValue = $product->getQty(); } } elseif ($attribute == 'source_qty') { $request = $controller->getRequest(); $sources = $request->getParam('sources'); if (is_array($sources) && isset($sources['assigned_sources'])) { foreach ($sources['assigned_sources'] as $source) { $originQty = $this->getProductQty->execute($product->getSku(), $source['source_code']); $currentQty = $source['quantity']; if ($currentQty != $originQty) { $changedValues['qty_' . $source['source_code']] = [ CncHistoryProductInterface::OLD_VALUE => $originQty, CncHistoryProductInterface::NEW_VALUE => $currentQty ]; } } } } else { $originData[$attribute] = $product->getOrigData($attribute); $currentValue = $product->getData($attribute); } if ($currentValue != $originData[$attribute]) { $changedValues[$attribute] = [ CncHistoryProductInterface::OLD_VALUE => $originData[$attribute], CncHistoryProductInterface::NEW_VALUE => $currentValue ]; } } if (count($changedValues)) { $userName = $this->authSession->getUser()->getName(); $productId = $product->getId(); $updatedAt = $this->date->gmtDate(); foreach ($changedValues as $attribute => $values) { try { /** @var Product $productHistory */ $productHistory = $this->productHistory->create(); $productHistory->setAttributeCode($attribute); if (is_string($values[CncHistoryProductInterface::OLD_VALUE])) { $productHistory->setOldValue($values[CncHistoryProductInterface::OLD_VALUE]); } if (is_string($values[CncHistoryProductInterface::NEW_VALUE])) { $productHistory->setNewValue($values[CncHistoryProductInterface::NEW_VALUE]); } $productHistory->setProductId($productId); $productHistory->setUser($userName); $productHistory->setUpdatedAt($updatedAt); $this->resource->save($productHistory); } catch (AlreadyExistsException $e) { throw new AlreadyExistsException(__('Could not save product history log: %1', $e->getMessage())); } } } } }