<?php
/*
* Autor: Carlos Iglesias
* Description: Main Controller to handle routes and views, future FTLController
* Date: 08/December/2020
*/
namespace App\Controller;
use App\Service\FileService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use App\Service\ParseService;
use App\Service\ShipmentService;
use Carbon\Carbon;
set_time_limit(0);
class MainController extends AbstractController
{
#[Route('/', name:'upload')]
public function subirArchivo(Request $request, SessionInterface $session, FileService $fileService)
{
return $this->render('/upload.html.twig');
}
/**
* @Route("/upload-file", name="_upload_file")
*/
public function objectTypeInstanceUploadFile(Request $request, FileService $fileService)
{
$file = $request->files->get('file');
if(!$file){
return $this->json([]);
}
$path = $_ENV["PATH_ARCHIVOS"];
$uploadedFile = $fileService->createFile($file, $path);
$headerResult = $fileService->getSpreadSheetHeaders($uploadedFile, $path);
return $this->json($headerResult);
}
/**
* @Route("/shipments", name="_shipments")
*/
public function tablaQuotes(Request $request)
{
return $this->render('/listado.html.twig');
}
/**
* @Route("/getAllShipments", name="_get_all_shipments")
*/
public function getAllShipments(Request $request, ShipmentService $shipmentService)
{
$fechaActual = new \DateTime();
//$currentPage = $request->query->get('currentPage') ?? 1;
//dd($request->query->get('order'));
//$fechaClon = clone $fechaActual;
//$fechaHace1Mes = date_modify($fechaClon,'-100day');
$offSet = $request->query->get('start');
$limit = $request->query->get('length');
$order = $request->query->get('order') ?? ['column' => 'id', 'type' => 'asc'];
$colums = $request->query->get('columns') ?? [];
$shipments = $shipmentService->getAllShipmentsDelMes($offSet, $limit, $order, $colums);
$logs = [];
foreach($shipments['results'] as $shipment){
if($shipment->getEmpatado() != true){
$logs[] = [
"IdShip" => $shipment->getIdShip(),
"OpenDate" => $shipment->getOpenDate() != null ? Carbon::instance($shipment->getOpenDate())->toDateString() : null,
"Shipper" => $shipment->getShipper(),
"OpenedBy" => $shipment->getOpenedBy(),
"Branch" => $shipment->getBranch(),
"Type" => $shipment->getType(),
"SubType" => $shipment->getSubType(),
"Routing" => $shipment->getRouting(),
"Origin" => $shipment->getOrigin(),
"Consignee" => $shipment->getConsignee(),
"FinalDestination" => $shipment->getFinalDestination(),
"OpenPayableLocal" => $shipment->getOpenPayableLocal(),
"OpenReceivableProfit" => $shipment->getOpenReceivableProfit(),
"OpenPayableProfit" => $shipment->getOpenPayableProfit(),
"OpenReceivableLocal" => $shipment->getOpenReceivableLocal(),
"ActualProfit" => $shipment->getActualProfit(),
"SpecialServiceType" => $shipment->getSpecialServiceType(),
"Customer" => $shipment->getCustomer(),
"FinalArrivalDate" => $shipment->getFinalArrivalDate() != null ? Carbon::instance($shipment->getFinalArrivalDate())->toDateString() : null,
"FirstPickupETA" => $shipment->getFirstPickupETA() != null ? Carbon::instance($shipment->getFirstPickupETA())->toDateString() : null,
"FromWhere" => $shipment->getFromWhere(),
"TrailerNo" => $shipment->getTrailerNo(),
"CarrierNo" => $shipment->getCarrierNo(),
"EstimatedFinalArrivalDate" => $shipment->getEstimatedFinalArrivalDate() != null ? Carbon::instance($shipment->getEstimatedFinalArrivalDate())->toDateString() : null,
"Salesman" => $shipment->getSalesman(),
"FromPortCountry" => $shipment->getFromPort(),
"ToWhere" => $shipment->getToWhere(),
"ToPortCountry" => $shipment->getToPortCountry(),
'ActualFinalArrivalDate' => $shipment->getActualFinalArrivalDate() != null ? Carbon::instance($shipment->getActualFinalArrivalDate())->toDateString() : null
];
}
}
return $this->json([
'data' => $logs,
'recordsTotal' => $shipments['totalRecords'],
'recordsFiltered' => $shipments['totalRecords']
]);
}
/**
* @Route("/pushEmpate", name="_push_empate")
*/
public function pushEmpate(Request $request, ShipmentService $shipmentService)
{
$empateArray = [
//Para crear un empate
'month' => $request->query->get('month'),
'openDate' => $request->query->get('openDate'),
'requestForm' => $request->query->get('request'),
'idShip' => $request->query->get('idShip'),
'openedBy' => $request->query->get('openedBy'),
'customer' => $request->query->get('customer'),
'subtype' => $request->query->get('subtype'),
'fromWhere' => $request->query->get('fromWhere'),
'fromPort' => $request->query->get('fromPort'),
'toWhere' => $request->query->get('toWhere'),
'toPort' => $request->query->get('toPort'),
'eid' => $request->query->get('eid'),
'estimatedDate' => $request->query->get('estimatedDate'),
'carrier' => $request->query->get('carrier'),
'cliente' => $request->query->get('cliente'),
'branch' => $request->query->get('branch'),
'salesman' => $request->query->get('salesman'),
'notes' => $request->query->get('notas'),
'shipIdViejo' => $request->query->get('shipIdViejo'),
'empateId' => $request->query->get('empateId'),
'status' => $request->query->get('status'),
'statusReinicio'=> $request->query->get('statusReinicio'),
'cotizacion' => $request->query->get('cotizacion'),
];
$shipmentService->crearEmpate($empateArray);
return $this->json(["data" => true]);
}
/**
* @Route("/truckReport", name="_truck_report")
*/
public function tablaEmpates(Request $request)
{
return $this->render('/truckReport.html.twig');
}
/**
* @Route("/getAllEmpates", name="_get_all_empates")
*/
public function getAllEmpates(Request $request, ShipmentService $shipmentService)
{
$empates = $shipmentService->getAllEmpates();
$logs = [];
foreach($empates as $empate){
$fechaNull = new \DateTime('-0001-11-30');
$logs[] = [
'month' => ($empate->getMonth() == $fechaNull) ? '' : $empate->getMonth(),
'openDate' => ($empate->getOpenDate() == $fechaNull) ? '' : $empate->getOpenDate(),
'requestForm' => $empate->getRequest(),
'idShip' => $empate->getIdShip(),
'openedBy' => $empate->getOpenedBy(),
'customer' => $empate->getCustomer(),
'subtype' => $empate->getSubtype(),
'fromWhere' => $empate->getFromWhere(),
'fromPort' => $empate->getFromPort(),
'toWhere' => $empate->getToWhere(),
'toPort' => $empate->getToPort(),
'etd' => ($empate->getETD() == $fechaNull) ? '' : $empate->getETD(),
'estimatedDate' => ($empate->getEstimatedArrivalDate() == $fechaNull) ? '' : $empate->getEstimatedArrivalDate(),
'carrier' => $empate->getCarrier(),
'cliente' => $empate->getClienteEmpate(),
'branch' => $empate->getBranch(),
'salesman' => $empate->getSalesman(),
'notes' => $empate->getNotes(),
'linkedTo' => $empate->getLinkedTo()?->getIdShip(),
'id' => $empate->getId(),
'status' => $empate->getStatus(),
'statusReinicio'=> $empate->getStatusReinicio(),
'cotizacion' => $empate->getCotizacion()
];
}
// return $this->json($logs);
$encoders = [new JsonEncoder()];
$normalizers = [ new DateTimeNormalizer(array('datetime_format' => 'Y-m-d')), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$quotesTO= $serializer->serialize( ['data'=>$logs], 'json');
return new Response ($quotesTO);
}
/**
* @Route("/rutas", name="_rutas")
*/
public function rutas(Request $request, ShipmentService $shipmentService)
{
$fechaActual = new \DateTime();
$fechaClon = clone $fechaActual;
$fechaHace1Mes = date_modify($fechaClon,'-45day');
$rutasOrigen = $shipmentService->getAllRoutesOrigen($fechaHace1Mes, $fechaActual);
$rutasDestino = $shipmentService->getAllRoutesDestino($fechaHace1Mes, $fechaActual);
$subTypes = $shipmentService->getAllSubTypes($fechaHace1Mes, $fechaActual);
$shipmentsRoutes = $shipmentService->getAllRoutes($fechaHace1Mes, $fechaActual, null, null, null, null, null);
return $this->render('/filtro_ciudad.html.twig', [
'origenes' => $rutasOrigen,
'destinos' => $rutasDestino,
'searchedText' => null,
'shipments' => $shipmentsRoutes,
'subTypes' => $subTypes
]);
}
/**
* @Route("/filtrarShipments", name="_filtrar_shipments")
*/
public function filtrarShipments(Request $request, ShipmentService $shipmentService)
{
$origenes = $request->query->get("origenes");
$destinos = $request->query->get("destinos");
$subTypes = $request->query->get("subTypes");
$fromWhenString = $request->query->get("fromWhen");
$toWhenString = $request->query->get("toWhen");
$mostrarComo = $request->query->get("mostrarComo");
$fromWhen = ($fromWhenString) ? new \DateTime($fromWhenString) : null;
$toWhen = ($toWhenString) ? new \DateTime($toWhenString) : null;
$name = $request->query->get("name");
$fechaActual = new \DateTime();
$fechaClon = clone $fechaActual;
$fechaHace1Mes = date_modify($fechaClon,'-45day');
if($mostrarComo == 'list'){
$resultados = $shipmentService->getRouting($fechaHace1Mes, $fechaActual, $origenes, $destinos, $fromWhen, $toWhen);
if(!empty($resultados)){
$shipments = [];
foreach($resultados as $resultado){
$shipments[] = [
'id' => $resultado->getId(),
'origin' => $resultado->getOrigin(),
'idShip' => $resultado->getIdShip(),
'shipper'=> $resultado->getShipper(),
'type' => $resultado->getType(),
'subType' => $resultado->getSubType(),
'customer'=> $resultado->getCustomer(),
'finalArrivalDate' => ($resultado->getFinalArrivalDate() != null) ? ($resultado->getFinalArrivalDate())->format('d-m-Y') : null,
'finalDestination' => $resultado->getFinalDestination(),
'actualFinalArrivalDate' => ($resultado->getActualFinalArrivalDate() != null) ? ($resultado->getActualFinalArrivalDate())->format('d-m-Y') : null,
];
}
$response = $this->renderView('render_shipments_routes.html.twig', [
'shipments' => $shipments,
'showInTwoColumns' => true
]);
return $this->json(["data" => $response]);
}else{
return $this->json(["data" => 'No hay embarques con estas características.']);
}
}else{
$resultados = $shipmentService->getAllRoutes($fechaHace1Mes, $fechaActual, $origenes, $destinos, $fromWhen, $toWhen, $subTypes);
if(!empty($resultados)){
$response = $this->renderView('shipment_list_template.html.twig', [
'shipments' => $resultados,
]);
return $this->json(["data" => $response]);
}else{
return $this->json(["data" => 'Tú búsqueda no arrojo ningún resultado.']);
}
}
}
/**
* @Route("/getShipmentByRouting", name="_get_shipment_by_routing")
*/
public function getShipmentByRouting(Request $request, ShipmentService $shipmentService)
{
$origen = $request->query->get("origen");
$destino = $request->query->get("destino");
$fromWhenString = $request->query->get("fromWhen");
$toWhenString = $request->query->get("toWhen");
$subTypes = $request->query->get("subTypes");
$fromWhen = ($fromWhenString) ? new \DateTime($fromWhenString) : null;
$toWhen = ($toWhenString) ? new \DateTime($toWhenString) : null;
$fechaActual = new \DateTime();
$fechaClon = clone $fechaActual;
$fechaHace1Mes = date_modify($fechaClon,'-45day');
$resultados = $shipmentService->getRouting( $fechaHace1Mes, $fechaActual, $origen, $destino, $fromWhen, $toWhen, $subTypes);
if(!empty($resultados)){
$shipments = [];
foreach($resultados as $resultado){
$shipments[] = [
'id' => $resultado->getId(),
'origin' => $resultado->getOrigin(),
'idShip' => $resultado->getIdShip(),
'shipper'=> $resultado->getShipper(),
'type' => $resultado->getType(),
'subType' => $resultado->getSubType(),
'customer'=> $resultado->getCustomer(),
'finalArrivalDate' => ($resultado->getFinalArrivalDate() != null) ? ($resultado->getFinalArrivalDate())->format('d-m-Y') : null,
'finalDestination' => $resultado->getFinalDestination(),
'actualFinalArrivalDate' => ($resultado->getActualFinalArrivalDate() != null) ? ($resultado->getActualFinalArrivalDate())->format('d-m-Y') : null,
];
}
$response = $this->renderView('render_shipments_routes.html.twig', [
'shipments' => $shipments,
'showInTwoColumns' => false
]);
return $this->json(["data" => $response]);
}else{
return $this->json(["data" => 'No hay embarques con estas características.']);
}
}
/**
* @Route("/desempatarEmpate", name="_desempatar_empate")
*/
public function desempatarEmpate(Request $request, ShipmentService $shipmentService)
{
$idShip = $request->query->get("idShip");
$empate = $shipmentService->getEmpateById($idShip);
$shipmentService->desempatarEmpate($empate);
return $this->json(["data" => 'true']);
}
/**
* @Route("/metas", name="_metas")
*/
public function metas(Request $request)
{
$meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
return $this->render('admin/metas.html.twig', ['meses' => $meses]);
}
}