<?php
namespace App\EventListener;
use Pimcore\Controller\FrontendController;
use \Pimcore\Model\DataObject;
use App\Library\EmailLibrary;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\EnterEvent;
use Symfony\Component\Workflow\Event\AnnounceEvent;
use Pimcore\Model\Comment;
use Symfony\Component\Workflow\Event\CompletedEvent;
use Symfony\Component\Workflow\Event\LeaveEvent;
class WorkflowEvents extends FrontendController {
private $executePreviousEvents = true;
private $team = '';
private $sendComment = false;
public function sendEmail($team,$objectData,$wfPlace = 'correction') {
$division = $objectData->getdivision();
$productName = $objectData->getname();
$escalationEmail = new DataObject\EscalationEmails\Listing();
$escalationEmail->setCondition(" division = '$division' ");
$escalationEmail = $escalationEmail->getdata();
if(count($escalationEmail) && !empty($escalationEmail[0])) {
$escalationEmail = $escalationEmail[0];
$productId = $objectData->getId();
if($team == 'snop') {
$emailIds = $escalationEmail->getsnopTeam();
} else if($team == 'finance') {
$emailIds = $escalationEmail->getfinanceTeam();
} else if($team == 'brand') {
$emailIds = $escalationEmail->getbrandTeam();
} else if($team == 'packaging') {
$emailIds = $escalationEmail->getpackagingTeam();
} else if($team == 'indirectTax') {
$emailIds = $escalationEmail->getindirecttaxTeam();
} else if($team == 'operationsTeam') {
$emailIds = $escalationEmail->getoperationsTeam();
} else if($team == 'demandPlanningTeam') {
$emailIds = $escalationEmail->getdemandPlanningTeam();
} else if($team == 'smisTeam') {
$emailIds = $escalationEmail->getsmisTeam();
} else if($team == 'sapTeam') {
$emailIds = $escalationEmail->getsapTeam();
}
if(!empty($emailIds)) {
$emailParam = array();
$emailParam['to'] = $emailIds;
$emailParam['subject'] = 'ITC - Product Approval';
// $emailParam['isDebug'] = true;
$templateData['product'] = $objectData;
$templateData['teamEmail'] = $team;
$correction = false;
$sendBackToSnop = false;
if($wfPlace == 'correction') {
$correction = true;
$this->sendComment = true;
} else if ($wfPlace == 'sendBackToSnop') {
$sendBackToSnop = true;
}
$templateData['url'] = \Pimcore\Tool::getHostUrl().'/admin/login/deeplink?object_'.$productId.'_object';
if (!empty($this->sendComment)) {
$dbConnection = \Pimcore\Db::get();
$notes = $dbConnection->fetchAssoc("SELECT * FROM `notes` WHERE cid = '$productId' ORDER BY `id` DESC LIMIT 1");
if(!empty(trim($notes['description']))) {
$templateData['notes'] = nl2br($notes['description']);
}
}
$user = \Pimcore\Tool\Session::getReadonly()->get("user");
if (empty($user)) {
$templateData['userInfo'] = \Pimcore\Model\User::getById($user->getId());
}
$templateData['correction'] = $correction;
$templateData['sendBackToSnop'] = $sendBackToSnop;
$templateData['productName'] = $productName;
$templateData['team'] = $this->team;
$emailParam['message'] = $this->render('default/escalation_email_template.html.twig', $templateData)->getContent();
$emailLib = new EmailLibrary();
$sendEmail = $emailLib->sendEmailNotification($emailParam);
}
}
}
public function snopCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('snop',$objectData);
}
}
public function financeCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('finance',$objectData);
}
}
public function brandCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('brand',$objectData,'correction');
}
}
public function packagingCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('packaging',$objectData);
}
}
public function indirectTaxCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('indirectTax',$objectData);
}
}
public function operationteamCorrection($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('operationsTeam',$objectData);
}
}
public function demandPlanningTeam($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('demandPlanningTeam',$objectData);
}
}
public function smisTeam($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('smisTeam',$objectData);
}
}
public function financeAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('finance',$objectData,'Finance');
}
}
public function brandAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('brand',$objectData,'Brand');
}
}
public function packagingAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('packaging',$objectData,'Packaging');
}
}
public function indirectTaxAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('indirectTax',$objectData,'Indirect Tax');
}
}
public function operationsAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('operationsTeam',$objectData,'Operations Team');
}
}
public function demandPlanningAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('demandPlanningTeam',$objectData,'Demand Planning Team');
}
}
public function smisAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('smisTeam',$objectData,'SMIS');
}
}
public function sapAction($objectData) {
if ($this->executePreviousEvents) {
$this->sendEmail('sapTeam',$objectData,'sap');
}
}
public function packagingToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Packaging Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function financeToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Finance Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function brandToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Brand Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function indirectTaxToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Indirect Tax Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function operationsToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Operations Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function demandPlanningToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'Demand Planning Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function smisToSnop($objectData) {
if ($this->executePreviousEvents) {
$this->team = 'SMIS Team';
$this->sendEmail('snop',$objectData,'sendBackToSnop');
}
}
public function sapApproved($objectData) {
$this->createMarketPlaceProduct($objectData);
}
public function createMarketPlaceProduct($productObj) {
$key = $productObj->getkey();
// Create Amazon DataObject
// $size = $productObj->getsize();
// $product_type = $productObj->getproduct_type();
$newObject = new DataObject\ProductAmazon();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('A_'.$key, 'object'));
// $newObject->setproduct_type($size);
// $newObject->setsize($product_type);
$newObject->setstoreProduct($productObj);
$newObject->seto_parentId('1');
$newObject->save(["versionNote" => "Product created from SAP"]);
// Create Big Basket DataObject
// $how_to_use = $productObj->gethow_to_use();
// $product_description = $productObj->getproduct_description();
$hsn_code = $productObj->gethsn_code();
$newObject = new DataObject\ProductBigBasket();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('BB_'.$key, 'object'));
// $newObject->sethow_to_use($how_to_use);
// $newObject->setproduct_description($product_description);
$newObject->sethsn_code($hsn_code);
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
// Create Blinkit DataObject
// $key_features = $productObj->getkey_features();
// $product_type = $productObj->getproduct_type();
// $price = $productObj->getprice();
// $ingredients = $productObj->getingredients();
$newObject = new DataObject\ProductBlinkit();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('BI_'.$key, 'object'));
// $newObject->setkey_features($key_features);
// $newObject->setproduct_type($product_type);
// $newObject->setprice($price);
// $newObject->setingredients($ingredients);
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$newObject = new DataObject\ProductFlipkart();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('FK_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
// Create JioMart DataObject
$hsn_code = $productObj->gethsn_code();
// $how_to_use = $productObj->gethow_to_use();
// $product_type = $productObj->getproduct_type();
// $ingredients = $productObj->getingredients();
// $size = $productObj->getsize();
// $food_type = $productObj->getfood_type();
$newObject = new DataObject\ProductJioMart();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('JM_'.$key, 'object'));
$newObject->sethsn_code($hsn_code);
// $newObject->sethow_to_use($how_to_use);
// $newObject->setproduct_type($product_type);
// $newObject->setingredients($ingredients);
// $newObject->setsize($size);
// $newObject->setfood_type($food_type);
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
// Create Swiggy DataObject
$sku_name = $productObj->getmaterial_number();
$weight = $productObj->getpack_weight();
$newObject = new DataObject\ProductSwiggy();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('SG_'.$key, 'object'));
$newObject->setsku_name($sku_name);
$newObject->setweight($weight);
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
// Create Zepto DataObject
$newObject = new DataObject\ProductZepto();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('Zepto_'.$key, 'object'));
$newObject->sethsn_code($hsn_code);
$newObject->seto_parentId('1');
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
$msku = $productObj->getold_material_number();
$sku_name = $productObj->getname();
$newObject = new DataObject\ProductITCStore();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey('Store_'.$key, 'object'));
$newObject->seto_parentId('1');
$newObject->setmsku_itcstore($msku);
$newObject->setsku_name($sku_name);
$newObject->setproduct_title($sku_name);
$newObject->setstoreProduct($productObj);
$newObject->save(["versionNote" => "Product created from SAP"]);
// $productSyncKey = "enlwq7pkg6t2od9blt93coomo7g6p9p0"; // Live Integration
$productSyncKey = "qzgg1vbpgwk6pjctbv6ku2a65906ocpb"; // Local
// $productUpdatedUrl = 'https://integration-5ojmyuq-23txbi6folvo6.ap-3.magentosite.cloud/rest/V1/bridge/catalog/product/add';
$productUpdatedUrl = 'http://local.itc.com/rest/V1/bridge/catalog/product/add';
$data = array( "product_id" => $productObj->getId(),"store_view_id" => "0", );
$data_json = json_encode(array("data" => $data));
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $productUpdatedUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $productSyncKey, 'Accept: application/json', 'Content-Type:application/json', ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
$resultArray = json_decode($result);
curl_close($curl);
}
public function snopEmailNotification(EnterEvent $e) {
$objectData = $e->getSubject();
$productId = $objectData->getId();
$transition = $e->getTransition();
$toPlace = $transition->getTos();
foreach ($toPlace as $key => $value) {
switch ($value) {
case 'packaging_to_snop':
$this->packagingToSnop($objectData);
break;
case 'finance_to_snop':
$this->financeToSnop($objectData);
break;
case 'brand_to_snop':
$this->brandToSnop($objectData);
break;
case 'indirectTax_to_snop':
$this->indirectTaxToSnop($objectData);
break;
case 'operations_to_snop':
$this->operationsToSnop($objectData);
break;
case 'demandPlanning_to_snop':
$this->demandPlanningToSnop($objectData);
break;
case 'smis_to_snop':
$this->smisToSnop($objectData);
break;
default:
# code...
break;
}
}
}
public function sapCorrectionEmails(AnnounceEvent $e) {
if ($this->executePreviousEvents) {
$this->sendComment = true;
$objectData = $e->getSubject();
$productId = $objectData->getId();
$transition = $e->getTransition();
$toPlace = $transition->getTos();
foreach ($toPlace as $key => $value) {
switch ($value) {
case 'snop_packaging_correction':
$this->packagingCorrection($objectData);
break;
case 'snop_finance_correction':
$this->financeCorrection($objectData);
break;
case 'snop_brand_correction':
$this->brandCorrection($objectData);
break;
case 'snop_indirectTax_correction':
$this->indirectTaxCorrection($objectData);
break;
case 'snop_operations_correction':
$this->operationteamCorrection($objectData);
break;
case 'snop_demandPlanning_correction':
$this->demandPlanningTeam($objectData);
break;
case 'snop_smis_correction':
$this->smisTeam($objectData);
break;
case 'packaging_correction':
$this->packagingCorrection($objectData);
break;
case 'finance_correction':
$this->financeCorrection($objectData);
break;
case 'brand_correction':
$this->brandCorrection($objectData);
break;
case 'indirectTax_correction':
$this->indirectTaxCorrection($objectData);
break;
case 'operations_correction':
$this->operationteamCorrection($objectData);
break;
case 'demandPlanning_correction':
$this->demandPlanningTeam($objectData);
break;
case 'smis_correction':
$this->smisTeam($objectData);
break;
case 'snop_correction':
$this->snopCorrection($objectData);
break;
case 'finance_action':
$this->financeAction($objectData);
break;
case 'brand_action':
$this->brandAction($objectData);
break;
case 'packaging_action':
$this->packagingAction($objectData);
break;
case 'indirect_tax_action':
$this->indirectTaxAction($objectData);
break;
case 'operations_action':
$this->operationsAction($objectData);
break;
case 'demand_planning_action':
$this->demandPlanningAction($objectData);
break;
case 'smis_action':
$this->smisAction($objectData);
break;
case 'sap_action':
$this->sapAction($objectData);
break;
case 'packaging_to_snop':
$this->packagingToSnop($objectData);
break;
case 'finance_to_snop':
$this->financeToSnop($objectData);
break;
case 'brand_to_snop':
$this->brandToSnop($objectData);
break;
case 'indirectTax_to_snop':
$this->indirectTaxToSnop($objectData);
break;
case 'operations_to_snop':
$this->operationsToSnop($objectData);
break;
case 'demandPlanning_to_snop':
$this->demandPlanningToSnop($objectData);
break;
case 'smis_to_snop':
$this->smisToSnop($objectData);
break;
case 'packaging_correction':
$this->packagingCorrection($objectData);
break;
default:
break;
}
}
$this->executePreviousEvents = false;
}
}
public function correctionEmails(CompletedEvent $e) {
if ($this->executePreviousEvents) {
$this->sendComment = true;
$objectData = $e->getSubject();
$productId = $objectData->getId();
$transition = $e->getTransition();
$toPlace = $transition->getTos();
foreach ($toPlace as $key => $value) {
switch ($value) {
case 'packaging_correction':
$this->packagingCorrection($objectData);
break;
case 'finance_correction':
$this->financeCorrection($objectData);
break;
case 'brand_correction':
$this->brandCorrection($objectData);
break;
case 'indirectTax_correction':
$this->indirectTaxCorrection($objectData);
break;
case 'operations_correction':
$this->operationteamCorrection($objectData);
break;
case 'demandPlanning_correction':
$this->demandPlanningTeam($objectData);
break;
case 'smis_correction':
$this->smisTeam($objectData);
break;
case 'snop_correction':
$this->snopCorrection($objectData);
break;
default:
# code...
break;
}
}
$this->executePreviousEvents = false;
}
}
public function emailNotification(EnterEvent $e) {
if ($this->executePreviousEvents) {
$objectData = $e->getSubject();
$productId = $objectData->getId();
$transition = $e->getTransition();
$toPlace = $transition->getTos();
foreach ($toPlace as $key => $value) {
switch ($value) {
case 'finance_action':
$this->financeAction($objectData);
break;
case 'brand_action':
$this->brandAction($objectData);
break;
case 'packaging_action':
$this->packagingAction($objectData);
break;
case 'indirect_tax_action':
$this->indirectTaxAction($objectData);
break;
case 'operations_action':
$this->operationsAction($objectData);
break;
case 'demand_planning_action':
$this->demandPlanningAction($objectData);
break;
case 'smis_action':
$this->smisAction($objectData);
break;
case 'sap_action':
$this->sapAction($objectData);
break;
case 'packaging_to_snop':
$this->packagingToSnop($objectData);
break;
case 'finance_to_snop':
$this->financeToSnop($objectData);
break;
case 'brand_to_snop':
$this->brandToSnop($objectData);
break;
case 'indirectTax_to_snop':
$this->indirectTaxToSnop($objectData);
break;
case 'operations_to_snop':
$this->operationsToSnop($objectData);
break;
case 'demandPlanning_to_snop':
$this->demandPlanningToSnop($objectData);
break;
case 'smis_to_snop':
$this->smisToSnop($objectData);
break;
case 'packaging_correction':
$this->packagingCorrection($objectData);
break;
case 'finance_correction':
$this->financeCorrection($objectData);
break;
case 'brand_correction':
$this->brandCorrection($objectData);
break;
case 'indirectTax_correction':
$this->indirectTaxCorrection($objectData);
break;
case 'operations_correction':
$this->operationteamCorrection($objectData);
break;
case 'demandPlanning_correction':
$this->demandPlanningTeam($objectData);
break;
case 'smis_correction':
$this->smisTeam($objectData);
break;
case 'snop_correction':
$this->snopCorrection($objectData);
break;
case 'sap_approved':
$this->sapApproved($objectData);
break;
default:
# code...
break;
}
}
$this->executePreviousEvents = false;
}
}
}