<?php
namespace Jabber\GenericBundle\EventListener;
use Jabber\GenericBundle\Messenger\TestMessenger;
use Pimcore\Db;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Product;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class PriceChangesLive {
private $syncCompanyOne = false;
private $syncCompanyTwo = false;
/**
*
* @var ParameterBagInterface
*/
private $params = null;
public function __construct(ParameterBagInterface $params, private MessageBusInterface $bus) {
$this->params = $params;
}
public function OnPostSave(ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
// if (!$this->IgnoredUser($dataObject->getUserModification())) {
if ($dataObject->getProductLevel() == 'STYLE') {
$this->checkForChanges($dataObject);
if ($this->syncCompanyOne == true) {
$this->bus->dispatch(new TestMessenger($dataObject->getId(), 58));
}
if ($this->syncCompanyTwo == true) {
$this->bus->dispatch(new TestMessenger($dataObject->getId(), 65));
}
}
// }
}
}
}
}
private function checkForChanges(Product $dataObject) {
$storedRetail = Db::getConnection()->fetchAssociative('SELECT RetailPriceGBP, RetailPriceUSD FROM object_store_Product WHERE oo_id = ?', [$dataObject->getId()]);
$storedWebGb = Db::getConnection()->fetchAssociative('SELECT WebSellingPrice FROM object_localized_query_Product_en_GB WHERE ooo_id = ?', [$dataObject->getId()]);
$storedWebUs = Db::getConnection()->fetchAssociative('SELECT WebSellingPrice FROM object_localized_query_Product_en_US WHERE ooo_id = ?', [$dataObject->getId()]);
if ($storedRetail['RetailPriceGBP'] != $dataObject->getRetailPriceGBP()) {
$this->syncCompanyOne = true;
}
if ($storedRetail['RetailPriceUSD'] != $dataObject->getRetailPriceUSD()) {
$this->syncCompanyTwo = true;
}
if ($storedWebGb['WebSellingPrice'] != $dataObject->getWebSellingPrice('en_GB')) {
$this->syncCompanyOne = true;
}
if ($storedWebUs['WebSellingPrice'] != $dataObject->getWebSellingPrice('en_US')) {
$this->syncCompanyTwo = true;
}
}
// /**
// * Is user in ignore list for price changes
// *
// * @param int $userId
// * @return bool
// */
// private function IgnoredUser($userId) {
// $ignoredUsers = null;
// $priceBundleParams = $this->params->get('jabber_price_bundle');
// if (isset($priceBundleParams['ignored_users'])) {
// $ignoredUsers = $priceBundleParams['ignored_users'];
// }
// if (is_array($ignoredUsers)) {
// if (isset($ignoredUsers[$userId])) {
// return true;
// }
// }
// return false;
// }
}