<?php
namespace AppBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use \Pimcore\Model\DataObject;
use \Pimcore\Model\DataObject\PriceEvent;
use \Pimcore\Model\DataObject\ItemMaster;
use Pimcore\Db;
use Pimcore\Log\ApplicationLogger;
use Carbon\Carbon;
class PriceEventListener {
public function onPostUpdate (ElementEventInterface $e)
{
if($e instanceof AssetEvent)
{
$foo = $e->getAsset();
}
else if ($e instanceof DocumentEvent)
{
$foo = $e->getDocument();
}
else if ($e instanceof DataObjectEvent)
{
// do something with the object
$foo = $e->getObject();
$pid = $foo->getId();
$p_id=1;
$claname = '';
$otype = $foo->geto_type();
$o_parentId= $foo->getParentId();
$user = \Pimcore\Tool\Admin::getCurrentUser();
//$foo.reload();
if($otype == 'object')
{
$claname = $foo->geto_className();
$o_published =$foo->geto_published();
}
if(($claname == 'priceEvent' || $claname == 'PriceEvent' ) && $o_published == 1 && $user != null)
{
$Status = $foo->getStatus();
// throw new \Pimcore\Model\Element\ValidationException($Status, 1234);
if($Status == 'Approved')
{
//throw new \Pimcore\Model\Element\ValidationException("You can not update this object.", 1234);
}
$getDateSetter = new \Pimcore\Model\DataObject\DaysSetter\Listing();
$getDateSetter->setCondition("brand= :brand", ["brand" => 'all']);
$getDateSetter->load();
foreach ($getDateSetter as $itemc)
{
$days=$itemc->getdays();
}
$time = "+".$days." days";
$masterSkuDetails=$foo->getitemMasterSKU();
$changeValue=$foo->getchangeByValue();
$statusForValue=$foo->getchangeByPercentValue();
$getStartDate=$foo->getstartDate();
$getEndDate=$foo->getendDate();
$todayDate = date('Y-m-d');
$afterThreeDay = Date('Y-m-d', strtotime($time));
if (($getStartDate >= $todayDate) && ($getStartDate <= $afterThreeDay))
{
throw new \Pimcore\Model\Element\ValidationException("Please select right start date", 1234);
}
else if($getStartDate <= $todayDate)
{
throw new \Pimcore\Model\Element\ValidationException("Please select right start date", 1234);
}
if($getEndDate != NULL)
{
if ($getEndDate <= $getStartDate)
{
throw new \Pimcore\Model\Element\ValidationException("Please select right end date ", 1234);
}
}
if($statusForValue == "changeByPercentValue")
{
if(($changeValue < 999.99) && ($changeValue < -999.99))
{
throw new \Pimcore\Model\Element\ValidationException("Percent should not be less then 999.99 and greter then -999.99", 1234);
}
}
if($statusForValue == "changeByPrice")
{
if($changeValue < 0)
{
throw new \Pimcore\Model\Element\ValidationException("Price should not be less then 0", 1234);
}
}
$getDes=$foo->geteventDesc();
$statusApp=$foo->getStatus();
$itemMasterSku = count($masterSkuDetails);
$eventDesc= $foo->geteventDesc();
$startDate = $foo->getstartDate();
$endDate = $foo->getendDate();
$eventSeason = $foo->geteventSeason();
$priceLevel = $foo->getpriceLevel();
$store = $foo->getstore();
$Status = $foo->getStatus();
$uniqueNumber = $foo->geteventNum();
//throw new \Pimcore\Model\Element\ValidationException($o_parentId, 1234);
$parentData = DataObject::getById($o_parentId);
$po_type = $parentData->geto_type();
//throw new \Pimcore\Model\Element\ValidationException($po_type, 1234);
if($po_type != 'folder')
{
// throw new \Pimcore\Model\Element\ValidationException($po_type."You are not allowed to update this object.", 12345);
}
if($po_type == 'folder')
{
//throw new \Pimcore\Model\Element\ValidationException(" I am in.", 1234);
// $queryOne="select FLOOR(10000 + RAND() * 899999) AS random_num from object_price where 'eventNum' NOT IN (select eventNum from object_price WHERE eventNum IS NOT NULL) LIMIT 1";
// $statmentEvnt = Db::get()->prepare($queryOne);
// $statmentEvnt->execute();
// $dataGetEvnt =$statmentEvnt->fetchAll();
// $uniqueBumberPar=$dataGetEvnt[0]['random_num'];
// $eventNumberSet=DataObject\PriceEvent::getById($pid);
// $eventNumberSet->seteventNum($uniqueBumberPar);
// $eventNumberSet->save();
$tableOne = "object_price";
$query = "select * from ".$tableOne." where o_parentId='".$pid."'";
$statment = Db::get()->prepare($query);
$statment->execute();
$dataGet =$statment->fetchAll();
$totalResult = count($dataGet);
if($totalResult > 0)
{
// throw new \Pimcore\Model\Element\ValidationException("if", 1234);
$m=0;
while($m < count($dataGet))
{
$childId = $dataGet[$m]['o_id'];
$m++;
$priceEventObjectUpdate = DataObject::getById($childId);
$priceEventObjectUpdate->seteventDesc($eventDesc);
$priceEventObjectUpdate->setstartDate($startDate);
$priceEventObjectUpdate->setendDate($endDate);
$priceEventObjectUpdate->seteventSeason($eventSeason);
$priceEventObjectUpdate->setpriceLevel($priceLevel);
$priceEventObjectUpdate->setstore($store);
$priceEventObjectUpdate->setStatus($Status);
$priceEventObjectUpdate->seteventNum($uniqueNumber);
$priceEventObjectUpdate->setchangeByPercentValue($statusForValue);
$masterSkuDetails1=$priceEventObjectUpdate->getitemMasterSKU();
foreach ($masterSkuDetails1 as $itemMasterDeatil1)
{
$itemMasterskuid1= $itemMasterDeatil1->getId();
$myObject = DataObject::getById($itemMasterskuid1);
$skuUpdateObject=$myObject->getsku();
$myObject = DataObject::getById($itemMasterskuid1);
$child_o_published = $myObject->geto_published();
if(empty($child_o_published) || $child_o_published == 0)
{
throw new \Pimcore\Model\Element\ValidationException("Please select valid SKU.", 1234);
}
if($myObject->getSalesInformation())
{
if($myObject->getSalesInformation()->getSaleInformation())
{
$sellingCost=$myObject->getSalesInformation()->getSaleInformation()->getsellingPrice();
$originalSalePrice=$myObject->getSalesInformation()->getSaleInformation()->getoriginalSalePrice();
}
else
{
$sellingCost=0;
$originalSalePrice= 0;
}
}
else
{
$sellingCost=0;
$originalSalePrice = 0;
}
$priceEventObjectUpdate->setOriginalPrice($originalSalePrice);
if($sellingCost == NULL || $sellingCost == 0)
{
throw new \Pimcore\Model\Element\ValidationException("Selling price is missing for".$itemMasterskuid1 . " Object", 1234);
}
elseif($skuUpdateObject == NULL)
{
throw new \Pimcore\Model\Element\ValidationException($itemMasterskuid1 . " Object does not have sku number", 1234);
}
else
{
if($statusForValue == "changeByPercentValue")
{
if(($changeValue < 999.99) && ($changeValue < -999.99))
{
throw new \Pimcore\Model\Element\ValidationException("Percent should not be less then 999.99 and greter then -999.99", 1234);
}
else
{
$resultUpdatePrecent = ($sellingCost * ($changeValue/100)) +$sellingCost;
$priceEventObjectUpdate->setchangeByValue($foo->getchangeByValue());
$priceEventObjectUpdate->setCurrentPrice($sellingCost);
$priceEventObjectUpdate->setfinalPrice($resultUpdatePrecent);
$priceEventObjectUpdate->save();
}
}
if($statusForValue == "changeByPrice")
{
if($changeValue < 0 )
{
throw new \Pimcore\Model\Element\ValidationException("Price should not be less then 0", 1234);
}
else
{
$resultUpdatePrice=$changeValue;
$priceEventObjectUpdate->setchangeByValue($foo->getchangeByValue());
$priceEventObjectUpdate->setCurrentPrice($sellingCost);
$priceEventObjectUpdate->setfinalPrice($resultUpdatePrice);
$priceEventObjectUpdate->save();
}
}
}
}
}
}
else
{
// throw new \Pimcore\Model\Element\ValidationException("else", 1234);
foreach ($masterSkuDetails as $itemMasterDeatil)
{
$itemMasterskuid= $itemMasterDeatil->getId();
$myObject = DataObject::getById($itemMasterskuid);
$child_o_published = $myObject->geto_published();
if(empty($child_o_published) || $child_o_published == 0)
{
throw new \Pimcore\Model\Element\ValidationException("Please select valid SKU.", 1234);
}
$skuNumber= $itemMasterDeatil->getsku();
if($itemMasterDeatil->getSalesInformation())
{
if($itemMasterDeatil->getSalesInformation()->getSaleInformation())
{
$getSPrice=$itemMasterDeatil->getSalesInformation()->getSaleInformation()->getsellingPrice();
}
else
{
$getSPrice=0;
}
}
else
{
$getSPrice=0;
}
if($getSPrice == '0' || $getSPrice == NULL )
{
throw new \Pimcore\Model\Element\ValidationException("Selling price is missing for".$getSPrice . " Object", 1234);
}
elseif($skuNumber == NULL)
{
throw new \Pimcore\Model\Element\ValidationException($itemMasterskuid . " Object does not have sku number", 1234);
$setCode=$itemMasterDeatil->getId();
$query = "delete from object_relations_price where dest_id='".$itemMasterskuid."'and fieldname='itemMasterSKU' and src_id='".$pid."' ";
$stmt = Db::get()->prepare($query);
$stmt->execute();
}
else
{
$objectItemMasterSku = new DataObject\PriceEvent();
$objectItemMasterSku->setParent(DataObject::getById($pid));
$objectItemMasterSku->setKey(\Pimcore\Model\Element\Service::getValidKey($itemMasterskuid, 'object'));
// $queryOne="select FLOOR(10000 + RAND() * 899999) AS random_num from object_price where 'eventNum' NOT IN (select eventNum from object_price WHERE eventNum IS NOT NULL) LIMIT 1";
// $statmentEvnt = Db::get()->prepare($queryOne);
// $statmentEvnt->execute();
// $dataGetEvnt =$statmentEvnt->fetchAll();
// $uniqueNumber=$dataGetEvnt[0]['random_num'];
$objectItemMasterSku->seteventNum($uniqueNumber);
$objectItemMasterSku->seteventDesc($eventDesc);
$objectItemMasterSku->setstartDate($startDate);
$objectItemMasterSku->setendDate($endDate);
$objectItemMasterSku->seteventSeason($eventSeason);
$objectItemMasterSku->setpriceLevel($priceLevel);
$objectItemMasterSku->setstore($store);
$objectItemMasterSku->setStatus($Status);
$objectItemMasterSku->setchangeByPercentValue($statusForValue);
$objectItemMasterSku->setPublished(true);
// get child object
$myObject = DataObject::getById($itemMasterskuid);
$sellingCost = $myObject->getSalesInformation()->getSaleInformation()->getsellingPrice();
$originalSalePrice=$myObject->getSalesInformation()->getSaleInformation()->getoriginalSalePrice();
$objectItemMasterSku->setOriginalPrice($originalSalePrice);
if($statusForValue == "changeByPercentValue")
{
if(($changeValue < 999.99) && ($changeValue < -999.99))
{
throw new \Pimcore\Model\Element\ValidationException("Percent should not be less then 999.99 and greter then -999.99", 1234);
}
else
{
$result=($getSPrice * ($changeValue/100)) + $getSPrice;
$objectItemMasterSku->setCurrentPrice($getSPrice);
$objectItemMasterSku->setchangeByValue($changeValue);
$objectItemMasterSku->setfinalPrice($result);
// $objectItemMasterSku->save();
}
}
if($statusForValue == "changeByPrice")
{
if($changeValue < 0 )
{
throw new \Pimcore\Model\Element\ValidationException("Price should not be less then 0", 1234);
}
else
{
//throw new \Pimcore\Model\Element\ValidationException($changeValue, 1234);
$result=$changeValue;
$objectItemMasterSku->setCurrentPrice($getSPrice);
$objectItemMasterSku->setchangeByValue($changeValue);
$objectItemMasterSku->setfinalPrice($result);
// $objectItemMasterSku->save();
}
}
$objectItemMasterSku->save();
$newId = $objectItemMasterSku->getId();
$j=1;
$query = "INSERT INTO `object_relations_price` (`src_id`, `dest_id`, `type`, `fieldname`, `index`, `ownertype`, `ownername`, `position`) VALUES ('".$newId."', '".$itemMasterskuid."', 'object', 'itemMasterSKU', '".$j."', 'object', 'child', '0');";
$stmt = Db::get()->prepare($query);
$rdata = $stmt->execute();
$j++;
}
}
}
}
else
{
//throw new \Pimcore\Model\Element\ValidationException("You are not allowed to update this object.", 1234);
}
// else
// {
// $priceEventObjectUpdate = DataObject::getById($pid);
// $masterSkuDetails1=$priceEventObjectUpdate->getitemMasterSKU();
// foreach ($masterSkuDetails1 as $itemMasterDeatil1)
// {
// $itemMasterskuid1= $itemMasterDeatil1->getId();
// $myObject = DataObject::getById($itemMasterskuid1);
// $skuUpdateObject=$myObject->getsku();
// if($myObject->getSalesInformation())
// {
// if($myObject->getSalesInformation()->getSaleInformation())
// {
// $sellingCost=$myObject->getSalesInformation()->getSaleInformation()->getsellingPrice();
// }
// else
// {
// $sellingCost=0;
// }
// }
// else
// {
// $sellingCost=0;
// }
// if($sellingCost == NULL || $sellingCost == 0)
// {
// throw new \Pimcore\Model\Element\ValidationException("Selling price is missing for".$itemMasterskuid1 . " Object", 1234);
// }
// elseif($skuUpdateObject == NULL)
// {
// throw new \Pimcore\Model\Element\ValidationException($itemMasterskuid1 . " Object does not have sku number", 1234);
// }
// else
// {
// if($statusForValue == "changeByPercentValue")
// {
// if(($changeValue < 999.99) && ($changeValue < -999.99))
// {
// throw new \Pimcore\Model\Element\ValidationException("Percent should not be less then 999.99 and greter then -999.99", 1234);
// }
// else
// {
// $resultUpdatePrecent = ($sellingCost * ($changeValue/100)) +$sellingCost;
// $priceEventObjectUpdate->setchangeByValue($foo->getchangeByValue());
// $priceEventObjectUpdate->setCurrentPrice($sellingCost);
// $priceEventObjectUpdate->setfinalPrice($resultUpdatePrecent);
// $priceEventObjectUpdate->save();
// }
// }
// if($statusForValue == "changeByPrice")
// {
// if($changeValue < 0 )
// {
// throw new \Pimcore\Model\Element\ValidationException("Price should not be less then 0", 1234);
// }
// else
// {
// $resultUpdatePrice=$changeValue;
// $priceEventObjectUpdate->setchangeByValue($foo->getchangeByValue());
// $priceEventObjectUpdate->setCurrentPrice($sellingCost);
// $priceEventObjectUpdate->setfinalPrice($resultUpdatePrice);
// $priceEventObjectUpdate->save();
// }
// }
// }
// }
// }
}
}
}
public function onPreAdd(ElementEventInterface $e)
{
}
public function onPreUpdate(ElementEventInterface $e)
{
if($e instanceof AssetEvent)
{
$foo = $e->getAsset();
}
else if ($e instanceof DocumentEvent)
{
$foo = $e->getDocument();
}
else if ($e instanceof DataObjectEvent)
{
// do something with the object
$foo = $e->getObject();
$pid = $foo->getId();
$p_id=1;
$claname = '';
$otype = $foo->geto_type();
$o_parentId= $foo->getParentId();
$user = \Pimcore\Tool\Admin::getCurrentUser();
//$foo.reload();
if($otype == 'object')
{
$o_published =$foo->geto_published();
$claname = $foo->geto_className();
$tablename = "object_price";
$sql = "select * from ".$tablename." where o_id = '".$pid."' ";
$stmt = Db::get()->prepare($sql);
$stmt->execute();
$dataI =$stmt->fetchAll();
$o_published=$dataI[0]['o_published'];
}
if(($claname == 'priceEvent' || $claname == 'PriceEvent' ) && $user != null && $o_published == 1)
{
$o_published = $foo->geto_published();
$eventSeason = $foo->geteventSeason();
$endDate = $foo->getendDate();
$table = "object_price";
$sql = "select * from ".$table." where oo_id='".$pid."'";
$stmt = Db::get()->prepare($sql);
$stmt->execute();
$data =$stmt->fetchAll();
$dbstatus = $data[0]['Status'];
$Status = $foo->getStatus();
$mainobject = DataObject::getById($pid);
$mainstatus= $mainobject->getStatus();
$changeValue=$foo->getchangeByValue();
$userOwner = $foo->getuserOwner();
//throw new \Pimcore\Model\Element\ValidationException($userOwner, 1234);
$statusForValue=$foo->getchangeByPercentValue();
$priceEventObjectUpdate = DataObject::getById($pid);
// $objectItemMasterSku->setchangeByPriceInput($statusForValue);
// $priceEventObjectUpdate->setfinalPrice();
//throw new \Pimcore\Model\Element\ValidationException($pid." ".$Status." ".$dbstatus, 1234);
if($eventSeason == '7' || $eventSeason == 7)
{
if($endDate == '')
{
throw new \Pimcore\Model\Element\ValidationException("Please select end date.", 1234);
}
}
if($dbstatus == 'Approved' && $userOwner > 0)
{
throw new \Pimcore\Model\Element\ValidationException("You are not allowed to do changes.", 1234);
}
$masterSkuDetails=$priceEventObjectUpdate->getitemMasterSKU();
$mypObject ='';
foreach ($masterSkuDetails as $itemMasterDeatil)
{
$itemMasterskuid= $itemMasterDeatil->getId();
$mypObject = DataObject::getById($itemMasterskuid);
}
if($mypObject)
{
if($mypObject->getSalesInformation())
{
if($mypObject->getSalesInformation()->getSaleInformation())
{
$getSPrice=$mypObject->getSalesInformation()->getSaleInformation()->getsellingPrice();
}
else
{
$getSPrice=0;
}
}
else
{
$getSPrice=0;
}
}
if($statusForValue == "changeByPercentValue")
{
if(($changeValue < 999.99) && ($changeValue < -999.99))
{
throw new \Pimcore\Model\Element\ValidationException("Percent should not be less then 999.99 and greter then -999.99", 1234);
}
else
{
$result=($getSPrice * ($changeValue/100)) + $getSPrice;
// $objectItemMasterSku->setCurrentPrice($getSPrice);
// $objectItemMasterSku->setchangeByValue($changeValue);
$priceEventObjectUpdate->setfinalPrice($result);
// $objectItemMasterSku->save();
}
}
if($statusForValue == "changeByPrice")
{
if($changeValue < 0 )
{
throw new \Pimcore\Model\Element\ValidationException("Price should not be less then 0", 1234);
}
else
{
//throw new \Pimcore\Model\Element\ValidationException($changeValue, 1234);
$result=$changeValue;
// $objectItemMasterSku->setCurrentPrice($getSPrice);
//$objectItemMasterSku->setchangeByValue($changeValue);
$priceEventObjectUpdate->setfinalPrice($result);
// $objectItemMasterSku->save();
}
}
//throw new \Pimcore\Model\Element\ValidationException($o_published, 1234);
// throw new \Pimcore\Model\Element\ValidationException($pid." ".$Status." ".$mainstatus, 1234);
if($Status == 'Approved')
{
// throw new \Pimcore\Model\Element\ValidationException("You are not allowed to do changes.", 1234);
}
}
}
}
}