<?php
namespace AppBundle\EventListener;
use AppBundle\Service\UtilService;
use GeoIp2\Util;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Model\DataObject\Accessories;
use Pimcore\Model\DataObject\Vehicle;
use Pimcore\Model\DataObject\VehicleMaster;
use Pimcore\Model\DataObject\VehiclePrice;
use Pimcore\Model\DataObject\VehiclePriceAttribute;
use Pimcore\Model\DataObject\VehiclePriceOptions;
use Pimcore\Model\DataObject\MinBookingAmount;
use Pimcore\Model\DataObject\Tax;
use Symfony\Component\HttpClient\HttpClient;
use \Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Pimcore\Tool\Frontend;
use Pimcore\Model\DataObject\AccessoryPacks;
use Pimcore\Model\DataObject\Pincode;
use Pimcore\Log\ApplicationLogger;
class VehicleListener {
public function onApproval(TransitionEvent $event){
$vehicle = $event->getSubject();
if(!$vehicle->hasChildren()){
if($vehicle->getParent() instanceof VehicleMaster){
$parentId = $vehicle->getParentId();
$vehicle = VehicleMaster::getById($parentId);
}
}
$tmpData['id'] = $vehicle->getId();
$tmpData['lcdv'] = $vehicle->getLcdv();
$tmpData['startDate'] = $vehicle->getStartDate()->toDateString('Y-m-d');
$tmpData['endDate'] = $vehicle->getEndDate()->toDateString('Y-m-d');
$tmpData['name'] = $vehicle->getName();
$tmpData['commercialDesc'] = $vehicle->getCommercialDescription();
$tmpData['cubicCapacity'] = $vehicle->getCubicCapacity();
$tmpData['kilowatt'] = $vehicle->getKilowatt();
$tmpData['sittingCapacity'] = $vehicle->getSittingCapacity();
$tmpData['isantiTheftFaceLifted'] = $vehicle->getIsantiTheftFaceLifted();
$tmpData['dst_is_disabled'] = $vehicle->getDst_is_disabled();
if($tmpData['dst_is_disabled'] == true){
$tmpData['dst_is_disabled'] = 'Yes';
}
if($tmpData['dst_is_disabled'] == false){
$tmpData['dst_is_disabled'] = 'No';
}
$tmpData['dp_is_disabled'] = $vehicle->getDp_is_disabled();
if($tmpData['dp_is_disabled'] == true){
$tmpData['dp_is_disabled'] = 'Yes';
}
if($tmpData['dp_is_disabled'] == false){
$tmpData['dp_is_disabled'] = 'No';
}
$minBookingAmount = MinBookingAmount::getByVehicle($vehicle);
if ($minBookingAmount->getCount() > 0) {
$minBookingAmount = $minBookingAmount->getData()[0];
$tmpData['preBookingAmount'] = $minBookingAmount->getPreBookingAmount()->getValue();
$tmpData['bookingAmount'] = $minBookingAmount->getBookingAmount()->getValue();
} else {
$tmpData['preBookingAmount'] = 0;
$tmpData['bookingAmount'] = 0;
}
$tmpData['lcdvAttributes'] = [];
$tmpData['lcdvAttributes']['validityStart'] = $vehicle->getValidityStart()->toDateString('Y-m-d');
$tmpData['lcdvAttributes']['validityEnd'] = $vehicle->getValidityEnd()->toDateString('Y-m-d');
$tmpData['lcdvAttributes']['Attributes'] = [];
$couplet = ['Basic', 'Descriptive', 'Personal'];
foreach ($couplet as $value) {
if ($value == 'Basic' && !empty($vehicle->getBasicInformation())) {
$basicAttributeInfo = $vehicle->getBasicInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['basic'] = UtilService::processAttributes($basicAttributeInfo);
} else if ($value == 'Descriptive' && !empty($vehicle->getDescriptiveInformation())) {
$descriptiveAttributeInfo = $vehicle->getDescriptiveInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['descriptive'] = UtilService::processAttributes($descriptiveAttributeInfo);
} else if ($value == 'Personal' && !empty($vehicle->getPersonalInformation())) {
$personalAttributeInfo = $vehicle->getPersonalInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['personal'] = UtilService::processAttributes($personalAttributeInfo);
}
}
$vehicleData['vehicleInfo'] = $tmpData;
$vehicleData['variantInfo'] = [];
if ($vehicle->hasChildren()) {
$children = $vehicle->getChildren();
foreach ($children as $child) {
if ($child instanceof VehicleMaster) {
//if ($child->isPublished()) {
$tmpChildData = UtilService::getVehicleChild($child);
array_push($vehicleData['variantInfo'], $tmpChildData);
//}
}
}
}
$responseArr = [];
$responseArr['data'] = $vehicleData;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/productSync';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
public function onObjectPostDelete(ElementEventInterface $event){
if($event instanceof DataObjectEvent) {
if($event->getObject() instanceof VehicleMaster){
$object = $event->getObject();
$lcdv = $object->getLcdv();
$resp = [];
$resp['lcdv16']='';
$resp['lcdv24']='';
if(strlen($lcdv ) == 16){
$resp['lcdv16']=$lcdv;
}else{
$resp['lcdv24']=$lcdv;
}
$responseArr = [];
$responseArr['data'] = $resp;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/productSync';
//$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
}
}
public function onObjectPostUpdate (ElementEventInterface $event) {
if($event instanceof DataObjectEvent) {
if($event->getObject() instanceof VehicleMaster){
$object = $event->getObject();
$id = $object->getId();
$vehicleData = [];
$vehicle = VehicleMaster::getById($id);
if($vehicle->isPublished()) {
if(!$vehicle->hasChildren()){
if($vehicle->getParent() instanceof VehicleMaster){
$parentId = $vehicle->getParentId();
$vehicle = VehicleMaster::getById($parentId);
}
}
$tmpData['id'] = $vehicle->getId();
$tmpData['lcdv'] = $vehicle->getLcdv();
$tmpData['startDate'] = $vehicle->getStartDate()->toDateString('Y-m-d');
$tmpData['endDate'] = $vehicle->getEndDate()->toDateString('Y-m-d');
$tmpData['name'] = $vehicle->getName();
$tmpData['commercialDesc'] = $vehicle->getCommercialDescription();
$tmpData['model'] = $vehicle->getModelName();
$tmpData['variant'] = $vehicle->getVariantName();
$tmpData['cubicCapacity'] = $vehicle->getCubicCapacity();
$tmpData['kilowatt'] = $vehicle->getKilowatt();
$tmpData['sittingCapacity'] = $vehicle->getSittingCapacity();
$tmpData['isantiTheftFaceLifted'] = $vehicle->getIsantiTheftFaceLifted();
$tmpData['dst_is_disabled'] = $vehicle->getDst_is_disabled();
if($tmpData['dst_is_disabled'] == true){
$tmpData['dst_is_disabled'] = 'Yes';
}
if($tmpData['dst_is_disabled'] == false){
$tmpData['dst_is_disabled'] = 'No';
}
$tmpData['dp_is_disabled'] = $vehicle->getDp_is_disabled();
if($tmpData['dp_is_disabled'] == true){
$tmpData['dp_is_disabled'] = 'Yes';
}
if($tmpData['dp_is_disabled'] == false){
$tmpData['dp_is_disabled'] = 'No';
}
$minBookingAmount = MinBookingAmount::getByVehicle($vehicle);
if ($minBookingAmount->getCount() > 0) {
$minBookingAmount = $minBookingAmount->getData()[0];
$tmpData['preBookingAmount'] = $minBookingAmount->getPreBookingAmount()->getValue();
$tmpData['bookingAmount'] = $minBookingAmount->getBookingAmount()->getValue();
} else {
$tmpData['preBookingAmount'] = 0;
$tmpData['bookingAmount'] = 0;
}
$tmpData['lcdvAttributes'] = [];
$tmpData['lcdvAttributes']['validityStart'] = $vehicle->getValidityStart()->toDateString('Y-m-d');
$tmpData['lcdvAttributes']['validityEnd'] = $vehicle->getValidityEnd()->toDateString('Y-m-d');
$tmpData['lcdvAttributes']['Attributes'] = [];
$couplet = ['Basic', 'Descriptive', 'Personal'];
foreach ($couplet as $value) {
if ($value == 'Basic' && !empty($vehicle->getBasicInformation())) {
$basicAttributeInfo = $vehicle->getBasicInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['basic'] = UtilService::processAttributes($basicAttributeInfo);
} else if ($value == 'Descriptive' && !empty($vehicle->getDescriptiveInformation())) {
$descriptiveAttributeInfo = $vehicle->getDescriptiveInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['descriptive'] = UtilService::processAttributes($descriptiveAttributeInfo);
} else if ($value == 'Personal' && !empty($vehicle->getPersonalInformation())) {
$personalAttributeInfo = $vehicle->getPersonalInformation()->getItems();
$tmpData['lcdvAttributes']['Attributes']['personal'] = UtilService::processAttributes($personalAttributeInfo);
}
}
$vehicleData['vehicleInfo'] = $tmpData;
$vehicleData['variantInfo'] = [];
if ($vehicle->hasChildren()) {
$children = $vehicle->getChildren();
foreach ($children as $child) {
if ($child instanceof VehicleMaster && !empty($child)) {
if ($child->isPublished()) {
$tmpChildData = UtilService::getVehicleChild($child);
array_push($vehicleData['variantInfo'], $tmpChildData);
}
}
}
}
$vehicleData['axisInfo']=[];
$vehicleData['axisInfo']['axisVariantName'] = $vehicle->getAxisVariantName();
$vehicleData['axisInfo']['axisbrandId'] = $vehicle->getAxisbrandId();
$vehicleData['axisInfo']['axisvariantId'] = $vehicle->getAxisvariantId();
$vehicleData['axisInfo']['axismakeId'] = $vehicle->getAxismakeId();
$vehicleData['axisInfo']['axiscategory'] = $vehicle->getAxiscategory();
// $file = fopen("VMJSON.txt", "w");
// $v = fwrite($file, json_encode($vehicleData));
// fclose($file);
$responseArr = [];
$responseArr['data'] = $vehicleData;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/productSync';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}else{
$responseArr = [];
$responseArr['data'] = $vehicle->getLcdv();
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/productSync';
//$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
$allVehicles = new DataObject\VehicleMaster\Listing();
$allVehicles->setUnpublished(false);
$allVehiclesData = $allVehicles->load();
$tmpAllDataArr = [];
if(count($allVehiclesData) > 0){
foreach($allVehiclesData as $vehicle){
if(strlen($vehicle->getLcdv()) == 16 && $vehicle->isPublished()) {
$dataArrModel['car_family'] = substr($vehicle->getLcdv(), 0, 4);
$dataArrModel['car_name'] = $vehicle->getName();
$dataArrModel['LCDV16'] = $vehicle->getLcdv();
$dataArrModel['model_name'] = $vehicle->getModelName();
array_push($tmpAllDataArr, $dataArrModel);
}
}
$responseArr = [];
$responseArr['success'] = 'true';
$responseArr['errors'] = [];
$responseArr['data'] = $tmpAllDataArr;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/modelSync';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
} else if($event->getObject() instanceof Accessories){
$object = $event->getObject();
$id = $object->getId();
$axsData = [];
$axs = Accessories::getById($id);
if($axs->isPublished()){
$tmpData['id'] = $axs->getId();
$tmpData['referenceId'] = $axs->getReferenceId();
$tmpData['brand'] = $axs->getBrand();
$tmpData['vehicleFamily'] = $axs->getVehicleFamily();
$tmpData['bodyStyle'] = $axs->getBodystyle();
$tmpData['language'] = $axs->getLangue();
$tmpData['universe'] = $axs->getUniverse();
$tmpData['subUniverse'] = $axs->getSubUniverse();
$tmpData['commercialName'] = $axs->getCommercialName();
$tmpData['commercialDesc'] = $axs->getCommercialDescription();
$tmpData['sellingUnit'] = $axs->getSellingUnit();
$tmpData['linkedRefs'] = $axs->getLinkedReferences();
$tmpData['prixHt'] = $axs->getPrixHT();
$tmpData['prixTTC'] = $axs->getPrixTTC();
$tmpData['dealerFitting'] = $axs->getDealerFitting();
$tmpData['accessoryRange'] = $axs->getAccessoryRange();
$tmpData['date'] = $axs->getDate();
$tmpData['accessorycolor'] = $axs->getAccessorycolor();
$tmpData['files'] = [];
$tmpData['category'] = $axs->getCategory();
$tmpData['subCategory'] = $axs->getSubCategory();
$tmpData['onlineSales'] = $axs->getOnlineSales();
$tmpData['variant'] = $axs->getVariant();
if($tmpData['variant'] == null){
$tmpData['variant'] = [];
}
$tmpData['applicable_sol'] = $axs->getApplicable_sol();
$files = $axs->getFiles();
foreach($files as $file){
$tmpData['files'] = $file['accImages']->getData();
}
$tmpData['sitting_capacity'] = $axs->getSitting_capacity();
$tmpData['CC24_category'] = $axs->getCC24_category();
$tmpData['CC24_subCategory'] = $axs->getCC24_subCategory();
$tmpData['CC24_files'] = $axs->getCC24_files();
$tmpData['CC22_category'] = $axs->getCC22_category();
$tmpData['CC22_subCategory'] = $axs->getCC22_subCategory();
$tmpData['CC22_files'] = $axs->getCC22_files();
$tmpData['group_id'] = $axs->getGroup_id();
$tmpData['Is3D'] = $axs->getIs3D();
if($tmpData['Is3D'] == null){
$tmpData['Is3D'] = 'No';
}
array_push($axsData,$tmpData);
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData().'/rest/V1/accessoriesSync';
$this->callAPI('POST',$apiURL,json_encode($axsData));
// $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
// fwrite($myfile, json_encode($a->getContent()));
}
}else if($event->getObject() instanceof AccessoryPacks){
$object = $event->getObject();
$id = $object->getId();
$axspackData = [];
$axs = AccessoryPacks::getById($id);
if($axs->isPublished()){
$tmpData['id'] = $axs->getId();
$tmpData['packCode'] = $axs->getPackCode();
$tmpData['HSNCODE'] = $axs->getHSNCODE();
$tmpData['price'] = $axs->getPrice();
$tmpData['packName'] = $axs->getPackName();
$tmpData['variant'] = $axs->getVariant();
$tmpData['charname'] = $axs->getCharname();
$tmpData['charoptions'] = $axs->getCharoptions();
$tmpData['sap_pack_id'] = $axs->getSap_pack_id();
$tmpData['applicable_sol'] = $axs->getApplicable_sol();
// $tmpData['bodycolor'] = $axs->getBodycolor();
// $tmpData['roofcolor'] = $axs->getRoofcolor();
// $tmpData['ipcolor'] = $axs->getIpcolor();
$tmpData['accessories'] = [];
$accessories = $axs->getAccessories();
foreach($accessories as $accessory){
//print_r($accessory);
$accessoryDetail = Accessories::getById($accessory->getObjectId());
$tmpData['accessories'][] = $accessoryDetail->getReferenceId();
}
$tmpData['not_allow_accessory'] = [];
$not_allow_accessories = $axs->getNotAllowAccessories();
foreach($not_allow_accessories as $not_accessory){
//print_r($accessory);
$not_accessoryDetail = Accessories::getById($not_accessory->getObjectId());
$tmpData['not_allow_accessory'][] = $not_accessoryDetail->getReferenceId();
}
$tmpData['3d_code'] = $axs->getCode_3d();
$tmpData['sitting_capacity'] = $axs->getSitting_capacity();
array_push($axspackData,$tmpData);
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData().'/rest/V1/accessoryPacksSync';
$this->callAPI('POST',$apiURL,json_encode($axspackData));
// $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
// fwrite($myfile, json_encode($a->getContent()));
}
}
else if ($event->getObject() instanceof VehiclePrice){
$object = $event->getObject();
$id = $object->getId();
$price = VehiclePrice::getById($id);
if($price->isPublished()){
$lcdv = $price->getLcdv16();
$responseArr = [];
$responseArr['lcdv16'] = $lcdv;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/updatedPrice';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
}
else if ($event->getObject() instanceof VehiclePriceAttribute){
$object = $event->getObject();
$id = $object->getId();
$price = VehiclePrice::getById($id);
if($price->isPublished()){
$lcdv = $price->getLcdv16();
$responseArr = [];
$responseArr['lcdv16'] = $lcdv;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/updatedPrice';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
}
else if ($event->getObject() instanceof VehiclePriceOptions){
$object = $event->getObject();
$id = $object->getId();
$price = VehiclePrice::getById($id);
if(!empty($price) && $price->isPublished()){
$lcdv = $price->getLcdv16();
$responseArr = [];
$responseArr['lcdv16'] = $lcdv;
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("MAGNETO_APIURL", null);
$apiURL = $apiURLData->getData() . '/rest/V1/updatedPrice';
$this->callAPI('POST',$apiURL,json_encode($responseArr));
}
}
else if($event->getObject() instanceof Pincode){
$object = $event->getObject();
$id = $object->getId();
$pincodeData = [];
$pin_code = Pincode::getById($id);
// print_r($pin_code);exit;
if($pin_code->isPublished()){
$tmpData['id'] = $pin_code->getId();
$tmpData['PostalCode'] = $pin_code->getCode();
$tmpData['CityName'] = $pin_code->getDistrictName();
$tmpData['CityCode'] = $pin_code->getDistrictCode();
$tmpData['StateCode'] = $pin_code->getStateCode();
$tmpData['IsActive'] = "true";
//Get State Name from State Code
$stateDataListing = DataObject\Region::getByCode($pin_code->getStateCode());
if($stateDataListing->getCount() > 0){
$stateData = $stateDataListing->load()[0];
$tmpData['StateName'] = ucfirst($stateData->getName());
}else{
$tmpData['StateName'] = '';
}
array_push($pincodeData,$tmpData);
$apiURLData = \Pimcore\Model\WebsiteSetting::getByName("DMS_PINCODE_SYNC_URL", null);
$apiURL = $apiURLData->getData();
$pin_response = $this->callAPIPincode('POST',$apiURL,json_encode($pincodeData));
$log_data['time'] = date('m/d/Y h:i:s a', time());
$log_data['response'] = $pin_response->getContent();
$myfile = fopen("../var/logs/pincodeLog.txt", "a") or die("Unable to open file!");
fwrite($myfile, "\n".json_encode($log_data));
}
}
}
}
public function callAPI($method, $url, $data)
{
$client = HttpClient::create();
return $response = $client->request(
$method,
$url,
[ 'headers' => [
'Content-Type' => 'application/json',
],
'body'=>$data]
);
}
public function callAPIPincode($method, $url, $data)
{
try{
$config =Frontend::getWebsiteConfig();
$dms_username = $config->get('DMS_PINCODE_USERNAME');
$dms_password = $config->get('DMS_PINCODE_PASSWORD');
$client = HttpClient::create();
return $response = $client->request(
$method,
$url,
[ 'headers' => [
'Content-Type' => 'application/json',
"Authorization"=>'Basic '.base64_encode("$dms_username:$dms_password")
// "Host" => "13.126.172.20"
],
'body'=>$data]
);
}catch (\Exception $e){
echo $e->getMessage();
exit;
}
}
}