<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use symfony\Component\Routing\Annotation\Route;
use Pimcore\Model\DataObject;
use Pimcore\Model\Asset\Service;
use Pimcore\Model\DataObject\Service as DoService;
use Carbon\Carbon;
use Symfony\Component\HttpFoundation\JsonResponse;
class PincodeController extends FrontendController
{
/**
* @param Request $request
*
* @Route("/createItcPincode")
*
* @return Response
*
*/
public function createItcPincodeAction(Request $request): Response
{
$pincode = $request->get('pincode');
$locationcode = $request->get('locationcode');
$city = $request->get('city');
$state = $request->get('state');
$deliveryType = $request->get('deliveryType');
$is_shippingapplicable = $request->get('shippingapplicable');
$merichakki_available = $request->get('merichakki_available');
$nammachakki_available = $request->get('nammachakki_available');
$vin_instance = $request->get('vin_instance');
$createAssetFolder = DoService::createFolderByPath('Pincode');
$objParentId = $createAssetFolder->getId();
if(!empty($pincode)){
$location_code_array = DataObject\Locations::getById($locationcode);
$holidayObjectName = $pincode;
//Create a new object
$newObject = new DataObject\ItcAddPincode();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey($holidayObjectName, 'object'));
$newObject->setParentId($objParentId);
$newObject->setpincode($pincode);
$newObject->setlocation_code($location_code_array->getlocation_name());
$newObject->setcity($city);
$newObject->setstate($state);
$newObject->setdeliverytype($deliveryType);
$newObject->setis_shipping_applicable($is_shippingapplicable);
$newObject->setmerichakki_available($merichakki_available);
$newObject->setnammachakki_available($nammachakki_available);
$newObject->setvin_instance($vin_instance);
$newObject->setPublished(true);
$newObject->save();
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$entries = new DataObject\ItcAddPincode\Listing();
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}else{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
return $this->render('pincode/create-pincode.html.twig',['locationsList' => $locationsList ]);
}
}
/**
* @param Request $request
*
* @Route("/itcpincodeListingAction")
*
* @return Response
*
*/
public function itcpincodeListingAction(Request $request): Response
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
// return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
// $conn = $this->container->get('doctrine')->getConnection('default');
// $sql = "select * from ...;";
// $stmt = $conn->query(sprintf($sql, $credentials['username'], sha1($credentials['password'])));
// $res = $stmt->fetch();
return $this->render('pincode/listing-pincode.html.twig', compact('locationsList'));
}
/**
* @param Request $request
*
* @Route("/editItcPincode/{id}")
*
* @return Response
*
*/
public function editItcPincodeAction($id) {
$entries = DataObject\ItcAddPincode::getById($id);
$locationsLists = DataObject\Locations::getList();
$locationsLists->load();
$location_codes = $entries->getlocation_code();
$locationsList = new DataObject\Locations\Listing();
$locationsList->setCondition("location_name = :name", ["name" => $location_codes]);
$locationsList->load();
foreach($locationsList as $locations){
$selected = '';
if(!empty($hol_locs) && $hol_locs)
$selected = ' selected';
$locations->selected = $selected;
}
return $this->render('pincode/edit-pincode.html.twig', compact('entries','locationsList','locationsLists'));
}
/**
* @param Request $request
*
* @Route("/updateItcPincode")
*
* @return Response
*
*/
public function updateItcPincodeAction(Request $request): Response
{
$pincode = $request->get('pincode');
$locationcode = $request->get('locationcode');
$state = $request->get('state');
$city = $request->get('city');
$deliveryType = $request->get('deliveryType');
$pincodeid = $request->get('pincodeid');
$is_shippingapplicable = $request->get('shippingapplicable');
$merichakki_available = $request->get('merichakki_available');
$nammachakki_available = $request->get('nammachakki_available');
$vin_instance = $request->get('vin_instance');
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$location_code_array = DataObject\Locations::getById($locationcode);
//Create a new object
$newObject = DataObject\ItcAddPincode::getById($pincodeid);
$newObject->setpincode($pincode);
$newObject->setlocation_code($location_code_array->getlocation_name());
$newObject->setcity($city);
$newObject->setstate($state);
$newObject->setdeliverytype($deliveryType);
$newObject->setis_shipping_applicable($is_shippingapplicable);
$newObject->setmerichakki_available($merichakki_available);
$newObject->setnammachakki_available($nammachakki_available);
$newObject->setvin_instance($vin_instance);
$newObject->setPublished(true);
$newObject->save();
$list = new DataObject\ItcAddPincode\Listing();
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $list ]);
}
/**
* @param Request $request
*
* @Route("/deleteItcPincode/{id}")
*
* @return Response
*
*/
public function deleteItcPincodeAction($id)
{
$entries = DataObject\ItcAddPincode::getById($id);
$delete = $entries->delete();
if($delete){
$list = new DataObject\ItcAddPincode\Listing();
}else{
$list = new DataObject\ItcAddPincode\Listing();
}
$locationsList = DataObject\Locations::getList();
$locationsList->load();
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $list ]);
}
/**
* @param Request $request
*
* @Route("/importPincode1")
*
* @return Response
*
*/
public function importPincode1Action(Request $request): Response
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$entries = new DataObject\ItcAddPincode\Listing();
if(!empty(basename($_FILES["file"]["name"]))){
//get file extension
$getfile = basename($_FILES["file"]["name"]);
$extention = @end(explode('.', $getfile));
$lover_extention = strtolower($extention);
$filename = $_FILES["file"]["tmp_name"];
// check only file accepted
$allowTypes = array('csv');
if(in_array($lover_extention, $allowTypes))
{
if($_FILES["file"]["size"] > 0)
{
//open file and read
$file = fopen($filename, "r");
$i = 0;
while (($col = fgetcsv($file, 10000, ",")) !== FALSE)
{
$createAssetFolder = DoService::createFolderByPath('Pincode');
$objParentId = $createAssetFolder->getId();
if($i > 0 && $col[0] !='')
{
$holidayObjectName = $col[0];
$location = $col[1];
$location_code_array = explode(',',$location);
$location_code = explode(',',$col[4]);
//Create a new object
$newObject = new DataObject\ItcAddPincode();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey($holidayObjectName, 'object'));
$newObject->setParentId($objParentId);
$newObject->setpincode($col[0]);
$newObject->setlocation_code($col[1]);
$newObject->setcity($col[2]);
$newObject->setstate($col[3]);
$newObject->setdeliverytype($location_code);
$newObject->setis_shipping_applicable($col[5]);
$newObject->setmerichakki_available($col[6]);
$newObject->setnammachakki_available($col[7]);
$newObject->setvin_instance($col[8]);
$newObject->setPublished(true);
$newObject->save();
}
$i++;
}
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
}
else{
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
}
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
/**
* @param Request $request
*
* @Route("/importPincode")
*
* @return Response
*
*/
public function importPincodeAction(Request $request): Response
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$entries = new DataObject\ItcAddPincode\Listing();
if(!empty(basename($_FILES["file"]["name"]))){
/*------ Get ItcAddPincode Class data --------*/
$dbConnection = \Pimcore\Db::get();
$pincodeClassId = $dbConnection->fetchOne(" SELECT id FROM classes WHERE LOWER(name) = 'ItcAddPincode' ");
$pincodeObject = "object_".$pincodeClassId;
//get file extension
$getfile = basename($_FILES["file"]["name"]);
$extention = @end(explode('.', $getfile));
$lover_extention = strtolower($extention);
$filename = $_FILES["file"]["tmp_name"];
// check only file accepted
$allowTypes = array('csv');
if(in_array($lover_extention, $allowTypes))
{
if($_FILES["file"]["size"] > 0)
{
//open file and read
$file = fopen($filename, "r");
$i = 0;
while (($col = fgetcsv($file, 10000, ",")) !== FALSE)
{
$createAssetFolder = DoService::createFolderByPath('Pincode');
$objParentId = $createAssetFolder->getId();
if($i > 0 && $col[0] !='')
{
$pincodeObjectName = $col[0];
$location = $col[1];
$location_code_array = explode(',',$location);
$location_code = explode(',',$col[4]);
/*----- Get ItcAddPincode data ---------*/
// echo " select * from ".$pincodeObject." WHERE pincode = '".$col[0]."'"; die;
$pincodeId = $dbConnection->fetchOne(" select * from ".$pincodeObject." WHERE pincode = '".$pincodeObjectName."'");
if(!empty($pincodeId)){
$newObject = DataObject\ItcAddPincode::getById($pincodeId);
} else {
$newObject = new DataObject\ItcAddPincode();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey($pincodeObjectName, 'object'));
$newObject->setParentId($objParentId);
}
/*----- End of Get ItcAddPincode data ---------*/
//Create / Update a object
$newObject->setpincode($col[0]);
$newObject->setlocation_code($col[1]);
$newObject->setcity($col[2]);
$newObject->setstate($col[3]);
$newObject->setdeliverytype($location_code);
$newObject->setis_shipping_applicable($col[5]);
$newObject->setmerichakki_available($col[6]);
$newObject->setnammachakki_available($col[7]);
$newObject->setvin_instance($col[8]);
$newObject->setPublished(true);
$newObject->save();
}
$i++;
}
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
}
else{
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
}
return $this->render('pincode/listing-pincode.html.twig',['data' => $entries,'locationsList' => $entries ]);
}
public function verifyauthToken()
{
$api_key = 'api-key';
$api_key_value = 'cGltY29yZWFwaWtleXZhbHVl';//base64encode('pimcoreapikeyvalue);
$message = "No data found"; $tokenCheck = 0; $apikeyCheck = 0;
$headers=array();
foreach (getallheaders() as $name => $value) {
$headers[$name] = $value;
if($name == 'Authtoken') $tokenCheck = 1;
if($name == 'Api-Key') $apikeyCheck = 1;
}
// echo 'arr:<pre>';
// print_r(apache_request_headers());
// die;
if($tokenCheck == 0 && $apikeyCheck == 0){
$message = "Authentication Token is missing";;
} else {
$tokenCheck_valid = 0; $apikeyCheck_valid = 0;
/*--------------------- Validating Auth Token -----------------*/
if($tokenCheck == 1)
{
$Authtoken = $headers['Authtoken'];
$dbConnection = \Pimcore\Db::get();
$authTokencheck = $dbConnection->fetchOne("SELECT id FROM users WHERE authToken = '".$Authtoken."' ");
if(empty($authTokencheck)){
$message = "Authentication Token is mismatch";
} else {
$tokenCheck_valid = 1;
}
}
/*--------------------- Validating Api Key -----------------*/
if($apikeyCheck == 1)
{
$AuthApikey = $headers['Api-Key'];
if(empty($AuthApikey)){
$message = "Authentication Token is mismatch";
} else if($AuthApikey != $api_key_value){
$message = "Authentication Token is mismatch";
} else {
//echo $apikeyCheck_valid = 1;
}
}
$message = ($tokenCheck_valid == 1 || $apikeyCheck_valid == 1)?"No data found":$message;
}
return $message;
}
/**
* @param Request $request
*
* @Route("/wearhouseDetectionApi",methods={"POST"})
*
* @return Response
*
*/
public function wearhouseDetectionApiAction(Request $request)
{
$message = SELF::verifyauthToken();
if($message == "No data found")
{
$createAssetFolder = DoService::createFolderByPath('Location');
$objParentId = $createAssetFolder->getId();
if($request->get('pincode')){
$pincode = $request->get('pincode');
$entries = new DataObject\ItcAddPincode\Listing();
$entries->setCondition("pincode = :name", ["name" => $pincode]);
$entries->load();
$data = array();
foreach($entries as $key => $entrie){
$location_codes = $entrie->getlocation_code();
$location = new DataObject\Locations\Listing();
$location->setCondition("location_name = :name", ["name" => $location_codes]);
$location->load();
$loc_name = '';
$loc_code = '';
foreach($location as $location_code){
$loc_name = $location_code->getlocation_name();
$loc_code = $location_code->getlocation_code();
}
$data = array("pincode" => $entrie->getpincode(),
"store_code" => $loc_code,
"location_name" => $loc_name,
"city_name" => $entrie->getcity(),
"state" => $entrie->getstate(),
"delivery_type" => $entrie->getdeliverytype(),
"is_shipping_applicable" => $entrie->getis_shipping_applicable(),
"merichakki_available" => $entrie->getmerichakki_available(),
"nammachakki_available" => $entrie->getnammachakki_available(),
"vin_instance" => $entrie->getvin_instance(),
);
}
}elseif($request->get('city_name')){
$city = $request->get('city_name');
$entries = new DataObject\ItcAddPincode\Listing();
$entries->setCondition("city = :name OR location_code = :name", ["name" => $city,"location_code" => $city]);
$entries->setLimit(1);
$entries->load();
foreach($entries as $key => $entrie){
$location_codes = $entrie->getlocation_code();
$location = new DataObject\Locations\Listing();
$location->setCondition("location_name = :name", ["name" => $location_codes]);
$location->load();
$loc_name = '';
$loc_code = '';
foreach($location as $location_code){
$loc_name = $location_code->getlocation_name();
$loc_code = $location_code->getlocation_code();
}
$data = array("pincode" => $entrie->getpincode(),
"store_code" => $loc_code,
"location_name" => $loc_name,
"city_name" => $entrie->getcity(),
"state" => $entrie->getstate(),
"delivery_type" => $entrie->getdeliverytype(),
"is_shipping_applicable" => $entrie->getis_shipping_applicable(),
"merichakki_available" => $entrie->getmerichakki_available(),
"nammachakki_available" => $entrie->getnammachakki_available(),
"vin_instance" => $entrie->getvin_instance(),
);
}
}
}
else
$data[] = ['message'=>$message];
if(!empty($data)){
return $this->json(["success" => true, "data" => $data]);
}else{
return $this->json(["success" => false, "message" => "Service not available"]);
}
}
/**
* @Route("/getPincodesData")
* This action returns a JSON response.
*/
public function getPincodesDataAction(): JsonResponse
{
/*------ Get Shipping Rate Card Class data --------*/
$dbConnection = \Pimcore\Db::get();
$pincodeClassId = $dbConnection->fetchOne(" SELECT id FROM classes WHERE LOWER(name) = 'ItcAddPincode' ");
$pincodeObject = "object_".$pincodeClassId;
include "dt_scripts/pincodes_jsonp.php";
return $this->json($dt_ssp_data);
}
/**
* @param Request $request
*
* @Route("/importPincodeByLocation")
*
* @return Response
*
*/
public function importPincodeByLocationAction(Request $request): Response
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
// $entries = new DataObject\ItcAddPincode\Listing();
if(!empty(basename($_FILES["file"]["name"]))){
$locationcode = $request->get('locationcode');
$location_code_array = DataObject\Locations::getById($locationcode);
$locationname = $location_code_array->getlocation_name();
/*------ Get ItcAddPincode Class data --------*/
$dbConnection = \Pimcore\Db::get();
$pincodeClassId = $dbConnection->fetchOne(" SELECT id FROM classes WHERE LOWER(name) = 'ItcAddPincode' ");
$pincodeObject = "object_".$pincodeClassId;
//get file extension
$getfile = basename($_FILES["file"]["name"]);
$extention = @end(explode('.', $getfile));
$lover_extention = strtolower($extention);
$filename = $_FILES["file"]["tmp_name"];
// check only file accepted
$allowTypes = array('csv');
if(in_array($lover_extention, $allowTypes))
{
if($_FILES["file"]["size"] > 0)
{
//open file and read
$file = fopen($filename, "r");
$i = 0;
while (($col = fgetcsv($file, 10000, ",")) !== FALSE)
{
$createAssetFolder = DoService::createFolderByPath('Pincode');
$objParentId = $createAssetFolder->getId();
if($i > 0 && $col[0] !='' && $col[1] == $locationname)
{
$pincodeObjectName = $col[0];
$location = $col[1];
$location_code_array = explode(',',$location);
$location_code = explode(',',$col[4]);
/*----- Get ItcAddPincode data ---------*/
// echo " select * from ".$pincodeObject." WHERE pincode = '".$col[0]."'"; die;
$pincodeId = $dbConnection->fetchOne(" select * from ".$pincodeObject." WHERE pincode = '".$pincodeObjectName."'");
if(!empty($pincodeId)){
$newObject = DataObject\ItcAddPincode::getById($pincodeId);
} else {
$newObject = new DataObject\ItcAddPincode();
$newObject->setKey(\Pimcore\Model\Element\Service::getValidKey($pincodeObjectName, 'object'));
$newObject->setParentId($objParentId);
}
/*----- End of Get ItcAddPincode data ---------*/
$newObject->setpincode($col[0]);
$newObject->setlocation_code($col[1]);
$newObject->setcity($col[2]);
$newObject->setstate($col[3]);
$newObject->setdeliverytype($location_code);
$newObject->setis_shipping_applicable($col[5]);
$newObject->setmerichakki_available($col[6]);
$newObject->setnammachakki_available($col[7]);
$newObject->setvin_instance($col[8]);
$newObject->setPublished(true);
$newObject->save();
}
$i++;
}
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
}
else{
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
}
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
/**
* @param Request $request
*
* @Route("/deletePincodeByLocation")
*
* @return Response
*
*/
public function deletePincodeByLocationAction(Request $request): Response
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$locationcode = $request->get('locationcode');
if(!empty($locationcode)){
$locationcode = $request->get('locationcode');
$location_code_array = DataObject\Locations::getById($locationcode);
$locationname = $location_code_array->getlocation_name();
// $location_code = 'Gurgaon';
$pincodesList = DataObject\ItcAddPincode::getList();
$pincodesList->setCondition("location_code = '".$locationname."'");
$pincodesList->load();
if(count($pincodesList)>0)
{
foreach($pincodesList as $pincodeInfo)
{
$pincodesdata = DataObject\ItcAddPincode::getById($pincodeInfo->getId());
$pincodesdatadelete = $pincodesdata->delete();
}
$this->addFlash('success', 'Pincodes Deleted Successfully');
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
else{
$this->addFlash('failure', 'Pincodes Deletion failed');
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
}
$this->addFlash('failure', 'No Location selected');
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
/**
* @param Request $request
*
* @Route("/deleteAllItcPincode/{limit}}")
*
* @return Response
*
*/
public function deleteAllItcPincodeAction($limit=100)
{
$locationsList = DataObject\Locations::getList();
$locationsList->load();
$pincodesList = DataObject\ItcAddPincode::getList();
$slotsList->setLimit($limit);
$pincodesList->load();
if(count($pincodesList)>0)
{
foreach($pincodesList as $pincodeInfo)
{
$pincodesdata = DataObject\ItcAddPincode::getById($pincodeInfo->getId());
$pincodesdatadelete = $pincodesdata->delete();
}
$this->addFlash('success', 'Pincodes Deleted Successfully');
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
else{
$this->addFlash('failure', 'Pincodes Deletion failed');
return $this->render('pincode/listing-pincode.html.twig',['locationsList' => $locationsList ]);
}
}
}