src/Controller/MainController.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * Autor:       Carlos Iglesias
  4.  * Description: Main Controller to handle routes and views, future FTLController
  5.  * Date:        08/December/2020
  6.  */
  7. namespace App\Controller;
  8. use App\Service\FileService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  16. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  17. use Symfony\Component\Serializer\Serializer;
  18. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  19. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  20. use Symfony\Component\Form\Extension\Core\Type\FileType;
  21. use App\Service\ParseService;
  22. use App\Service\ShipmentService;
  23. use Carbon\Carbon;
  24. set_time_limit(0);
  25. class MainController extends AbstractController
  26. {
  27.     
  28.     #[Route('/'name:'upload')]
  29.     public function subirArchivo(Request $requestSessionInterface $sessionFileService $fileService)
  30.     {
  31.         return $this->render('/upload.html.twig');
  32.     }
  33.     
  34.     /**
  35.      * @Route("/upload-file", name="_upload_file")
  36.      */
  37.     public function objectTypeInstanceUploadFile(Request $requestFileService $fileService)
  38.     {
  39.         $file $request->files->get('file');
  40.         if(!$file){
  41.             return $this->json([]);
  42.         }
  43.         $path $_ENV["PATH_ARCHIVOS"];
  44.         $uploadedFile $fileService->createFile($file$path);
  45.         $headerResult $fileService->getSpreadSheetHeaders($uploadedFile$path);
  46.         
  47.         return $this->json($headerResult);
  48.     }
  49.     /**
  50.      * @Route("/shipments", name="_shipments")
  51.      */
  52.     public function tablaQuotes(Request $request)
  53.     {
  54.         return $this->render('/listado.html.twig');
  55.     }
  56.     /**
  57.      * @Route("/getAllShipments", name="_get_all_shipments")
  58.      */
  59.     public function getAllShipments(Request $requestShipmentService $shipmentService)
  60.     {        
  61.         $fechaActual   = new \DateTime();
  62.         //$currentPage = $request->query->get('currentPage') ?? 1;
  63.         //dd($request->query->get('order'));
  64.         //$fechaClon = clone $fechaActual;
  65.         //$fechaHace1Mes = date_modify($fechaClon,'-100day');
  66.         $offSet $request->query->get('start');
  67.         $limit $request->query->get('length');
  68.         $order $request->query->get('order') ?? ['column' => 'id''type' => 'asc'];
  69.         $colums $request->query->get('columns') ?? [];
  70.         $shipments $shipmentService->getAllShipmentsDelMes($offSet$limit$order$colums);
  71.         
  72.         $logs = [];
  73.         foreach($shipments['results'] as $shipment){
  74.             if($shipment->getEmpatado() != true){
  75.                 $logs[] = [
  76.                     "IdShip"                    => $shipment->getIdShip(),
  77.                     "OpenDate"                  => $shipment->getOpenDate() != null Carbon::instance($shipment->getOpenDate())->toDateString() : null,
  78.                     "Shipper"                   => $shipment->getShipper(),
  79.                     "OpenedBy"                  => $shipment->getOpenedBy(),
  80.                     "Branch"                    => $shipment->getBranch(),
  81.                     "Type"                      => $shipment->getType(),
  82.                     "SubType"                   => $shipment->getSubType(),
  83.                     "Routing"                   => $shipment->getRouting(),
  84.                     "Origin"                    => $shipment->getOrigin(),
  85.                     "Consignee"                 => $shipment->getConsignee(),
  86.                     "FinalDestination"          => $shipment->getFinalDestination(),
  87.                     "OpenPayableLocal"          => $shipment->getOpenPayableLocal(),
  88.                     "OpenReceivableProfit"      => $shipment->getOpenReceivableProfit(),
  89.                     "OpenPayableProfit"         => $shipment->getOpenPayableProfit(),
  90.                     "OpenReceivableLocal"       => $shipment->getOpenReceivableLocal(),
  91.                     "ActualProfit"              => $shipment->getActualProfit(),
  92.                     "SpecialServiceType"        => $shipment->getSpecialServiceType(),
  93.                     "Customer"                  => $shipment->getCustomer(),
  94.                     "FinalArrivalDate"          => $shipment->getFinalArrivalDate() != null Carbon::instance($shipment->getFinalArrivalDate())->toDateString() : null,
  95.                     "FirstPickupETA"            => $shipment->getFirstPickupETA() != null Carbon::instance($shipment->getFirstPickupETA())->toDateString() : null,
  96.                     "FromWhere"                 => $shipment->getFromWhere(),
  97.                     "TrailerNo"                 => $shipment->getTrailerNo(),
  98.                     "CarrierNo"                 => $shipment->getCarrierNo(),
  99.                     "EstimatedFinalArrivalDate" => $shipment->getEstimatedFinalArrivalDate() != null Carbon::instance($shipment->getEstimatedFinalArrivalDate())->toDateString() : null,
  100.                     "Salesman"                  => $shipment->getSalesman(),
  101.                     "FromPortCountry"           => $shipment->getFromPort(),
  102.                     "ToWhere"                   => $shipment->getToWhere(),
  103.                     "ToPortCountry"             => $shipment->getToPortCountry(),
  104.                     'ActualFinalArrivalDate'    => $shipment->getActualFinalArrivalDate() != null Carbon::instance($shipment->getActualFinalArrivalDate())->toDateString() : null
  105.                 ];  
  106.             } 
  107.         }
  108.         
  109.          return $this->json([
  110.                 'data' => $logs
  111.                 'recordsTotal' => $shipments['totalRecords'],
  112.                 'recordsFiltered' => $shipments['totalRecords']
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/pushEmpate", name="_push_empate")
  117.      */
  118.     public function pushEmpate(Request $requestShipmentService $shipmentService)
  119.     {
  120.         $empateArray = [
  121.         //Para crear un empate
  122.             'month'         => $request->query->get('month'),
  123.             'openDate'      => $request->query->get('openDate'),
  124.             'requestForm'   => $request->query->get('request'),
  125.             'idShip'        => $request->query->get('idShip'),
  126.             'openedBy'      => $request->query->get('openedBy'),
  127.             'customer'      => $request->query->get('customer'),
  128.             'subtype'       => $request->query->get('subtype'),
  129.             'fromWhere'     => $request->query->get('fromWhere'),
  130.             'fromPort'      => $request->query->get('fromPort'),
  131.             'toWhere'       => $request->query->get('toWhere'),
  132.             'toPort'        => $request->query->get('toPort'),
  133.             'eid'           => $request->query->get('eid'),
  134.             'estimatedDate' => $request->query->get('estimatedDate'),
  135.             'carrier'       => $request->query->get('carrier'),
  136.             'cliente'       => $request->query->get('cliente'),
  137.             'branch'        => $request->query->get('branch'),
  138.             'salesman'      => $request->query->get('salesman'),
  139.             'notes'         => $request->query->get('notas'),
  140.             'shipIdViejo'   => $request->query->get('shipIdViejo'),
  141.             'empateId'      => $request->query->get('empateId'),
  142.             'status'        => $request->query->get('status'),
  143.             'statusReinicio'=> $request->query->get('statusReinicio'),
  144.             'cotizacion'    => $request->query->get('cotizacion'),
  145.         ];
  146.         $shipmentService->crearEmpate($empateArray);
  147.         return $this->json(["data" => true]);
  148.     }
  149.     /**
  150.      * @Route("/truckReport", name="_truck_report")
  151.      */
  152.     public function tablaEmpates(Request $request)
  153.     {
  154.         return $this->render('/truckReport.html.twig');
  155.     }
  156.     /**
  157.      * @Route("/getAllEmpates", name="_get_all_empates")
  158.      */
  159.     public function getAllEmpates(Request $requestShipmentService $shipmentService)
  160.     {
  161.         $empates $shipmentService->getAllEmpates();
  162.         
  163.         $logs = [];
  164.         foreach($empates as $empate){
  165.             $fechaNull = new \DateTime('-0001-11-30');
  166.                 $logs[] = [
  167.                     'month'         => ($empate->getMonth() == $fechaNull) ? '' $empate->getMonth(),
  168.                     'openDate'      => ($empate->getOpenDate() == $fechaNull) ? '' $empate->getOpenDate(),
  169.                     'requestForm'   => $empate->getRequest(),
  170.                     'idShip'        => $empate->getIdShip(),
  171.                     'openedBy'      => $empate->getOpenedBy(),
  172.                     'customer'      => $empate->getCustomer(),
  173.                     'subtype'       => $empate->getSubtype(),
  174.                     'fromWhere'     => $empate->getFromWhere(),
  175.                     'fromPort'      => $empate->getFromPort(),
  176.                     'toWhere'       => $empate->getToWhere(),
  177.                     'toPort'        => $empate->getToPort(),
  178.                     'etd'           => ($empate->getETD() == $fechaNull) ? '' $empate->getETD(),
  179.                     'estimatedDate' => ($empate->getEstimatedArrivalDate() == $fechaNull) ? '' $empate->getEstimatedArrivalDate(),
  180.                     'carrier'       => $empate->getCarrier(),
  181.                     'cliente'       => $empate->getClienteEmpate(),
  182.                     'branch'        => $empate->getBranch(),
  183.                     'salesman'      => $empate->getSalesman(),
  184.                     'notes'         => $empate->getNotes(),
  185.                     'linkedTo'      => $empate->getLinkedTo()?->getIdShip(),
  186.                     'id'            => $empate->getId(),
  187.                     'status'        => $empate->getStatus(),
  188.                     'statusReinicio'=> $empate->getStatusReinicio(),
  189.                     'cotizacion'    => $empate->getCotizacion()
  190.                 ];  
  191.         }
  192.         // return $this->json($logs);
  193.         $encoders = [new JsonEncoder()];
  194.         $normalizers = [ new DateTimeNormalizer(array('datetime_format' => 'Y-m-d')), new ObjectNormalizer()];
  195.         $serializer = new Serializer($normalizers$encoders);
  196.         $quotesTO$serializer->serialize( ['data'=>$logs], 'json');
  197.         return new Response ($quotesTO);
  198.     }
  199.     /**
  200.      * @Route("/rutas", name="_rutas")
  201.      */
  202.     public function rutas(Request $requestShipmentService $shipmentService)
  203.     {
  204.         $fechaActual   = new \DateTime();        
  205.         $fechaClon = clone $fechaActual;
  206.         $fechaHace1Mes date_modify($fechaClon,'-45day');
  207.         $rutasOrigen $shipmentService->getAllRoutesOrigen($fechaHace1Mes$fechaActual);
  208.         $rutasDestino $shipmentService->getAllRoutesDestino($fechaHace1Mes$fechaActual);
  209.         $subTypes $shipmentService->getAllSubTypes($fechaHace1Mes$fechaActual);
  210.         $shipmentsRoutes $shipmentService->getAllRoutes($fechaHace1Mes$fechaActualnullnullnullnullnull);
  211.         
  212.         return $this->render('/filtro_ciudad.html.twig', [
  213.             'origenes' => $rutasOrigen,
  214.             'destinos' => $rutasDestino,
  215.             'searchedText' => null,
  216.             'shipments' => $shipmentsRoutes,
  217.             'subTypes' => $subTypes
  218.         ]);
  219.     }
  220.     /**
  221.      * @Route("/filtrarShipments", name="_filtrar_shipments")
  222.      */
  223.     public function filtrarShipments(Request $requestShipmentService $shipmentService)
  224.     {
  225.         $origenes $request->query->get("origenes");
  226.         $destinos $request->query->get("destinos");
  227.         $subTypes $request->query->get("subTypes");
  228.         $fromWhenString $request->query->get("fromWhen");
  229.         $toWhenString $request->query->get("toWhen");
  230.         $mostrarComo $request->query->get("mostrarComo");
  231.         
  232.         $fromWhen = ($fromWhenString) ? new \DateTime($fromWhenString) : null;
  233.         $toWhen = ($toWhenString) ? new \DateTime($toWhenString) : null;
  234.         $name $request->query->get("name");
  235.         $fechaActual   = new \DateTime();        
  236.         $fechaClon = clone $fechaActual;
  237.         $fechaHace1Mes date_modify($fechaClon,'-45day');
  238.         if($mostrarComo == 'list'){
  239.             $resultados $shipmentService->getRouting($fechaHace1Mes$fechaActual$origenes$destinos$fromWhen$toWhen);
  240.             
  241.             if(!empty($resultados)){
  242.                 $shipments = [];
  243.                 foreach($resultados as $resultado){
  244.                     $shipments[] = [
  245.                         'id' => $resultado->getId(),
  246.                         'origin' => $resultado->getOrigin(),
  247.                         'idShip' => $resultado->getIdShip(),
  248.                         'shipper'=> $resultado->getShipper(),
  249.                         'type' => $resultado->getType(),
  250.                         'subType' => $resultado->getSubType(),
  251.                         'customer'=> $resultado->getCustomer(),
  252.                         'finalArrivalDate' => ($resultado->getFinalArrivalDate() != null) ? ($resultado->getFinalArrivalDate())->format('d-m-Y') : null,
  253.                         'finalDestination' => $resultado->getFinalDestination(),
  254.                         'actualFinalArrivalDate' => ($resultado->getActualFinalArrivalDate() != null) ? ($resultado->getActualFinalArrivalDate())->format('d-m-Y') : null,
  255.                     ];
  256.                 }
  257.                 $response $this->renderView('render_shipments_routes.html.twig', [
  258.                     'shipments' => $shipments,
  259.                     'showInTwoColumns' => true
  260.                 ]);
  261.                 return $this->json(["data" => $response]);
  262.             }else{
  263.                 return $this->json(["data" => 'No hay embarques con estas características.']);
  264.             }
  265.         }else{
  266.             $resultados $shipmentService->getAllRoutes($fechaHace1Mes$fechaActual$origenes$destinos$fromWhen$toWhen$subTypes);
  267.             
  268.             if(!empty($resultados)){
  269.                 $response $this->renderView('shipment_list_template.html.twig', [
  270.                     'shipments' => $resultados,
  271.                 ]);
  272.                 return $this->json(["data" => $response]);
  273.             }else{
  274.                 return $this->json(["data" => 'Tú búsqueda no arrojo ningún resultado.']);
  275.             }
  276.         }
  277.     }
  278.     /**
  279.      * @Route("/getShipmentByRouting", name="_get_shipment_by_routing")
  280.      */
  281.     public function getShipmentByRouting(Request $requestShipmentService $shipmentService)
  282.     {
  283.         $origen $request->query->get("origen");
  284.         $destino $request->query->get("destino");
  285.         $fromWhenString $request->query->get("fromWhen");
  286.         $toWhenString $request->query->get("toWhen");
  287.         $subTypes $request->query->get("subTypes");
  288.         
  289.         $fromWhen = ($fromWhenString) ? new \DateTime($fromWhenString) : null;
  290.         $toWhen = ($toWhenString) ? new \DateTime($toWhenString) : null;
  291.         $fechaActual   = new \DateTime();        
  292.         $fechaClon = clone $fechaActual;
  293.         $fechaHace1Mes date_modify($fechaClon,'-45day');
  294.         $resultados $shipmentService->getRouting$fechaHace1Mes$fechaActual$origen$destino$fromWhen$toWhen$subTypes);
  295.         if(!empty($resultados)){
  296.             $shipments = [];
  297.             foreach($resultados as $resultado){
  298.                 $shipments[] = [
  299.                     'id' => $resultado->getId(),
  300.                     'origin' => $resultado->getOrigin(),
  301.                     'idShip' => $resultado->getIdShip(),
  302.                     'shipper'=> $resultado->getShipper(),
  303.                     'type' => $resultado->getType(),
  304.                     'subType' => $resultado->getSubType(),
  305.                     'customer'=> $resultado->getCustomer(),
  306.                     'finalArrivalDate' => ($resultado->getFinalArrivalDate() != null) ? ($resultado->getFinalArrivalDate())->format('d-m-Y') : null,
  307.                     'finalDestination' => $resultado->getFinalDestination(),
  308.                     'actualFinalArrivalDate' => ($resultado->getActualFinalArrivalDate() != null) ? ($resultado->getActualFinalArrivalDate())->format('d-m-Y') : null,
  309.                 ];
  310.             }
  311.             $response $this->renderView('render_shipments_routes.html.twig', [
  312.                 'shipments' => $shipments,
  313.                 'showInTwoColumns' => false
  314.             ]);
  315.             return $this->json(["data" => $response]);
  316.         }else{
  317.             return $this->json(["data" => 'No hay embarques con estas características.']);
  318.         }
  319.     }
  320.     /**
  321.      * @Route("/desempatarEmpate", name="_desempatar_empate")
  322.      */
  323.     public function desempatarEmpate(Request $requestShipmentService $shipmentService)
  324.     {
  325.         $idShip $request->query->get("idShip");
  326.         $empate $shipmentService->getEmpateById($idShip);
  327.         $shipmentService->desempatarEmpate($empate);
  328.         return $this->json(["data" => 'true']);
  329.     }
  330.     /**
  331.      * @Route("/metas", name="_metas")
  332.      */
  333.     public function metas(Request $request)
  334.     {   
  335.         $meses = ['Enero''Febrero''Marzo''Abril''Mayo''Junio',
  336.             'Julio''Agosto''Septiembre''Octubre''Noviembre''Diciembre'];
  337.         return $this->render('admin/metas.html.twig', ['meses' => $meses]);
  338.     }
  339. }