src/MDS/ApiBundle/Controller/ApiProposalsClientController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\MDS\ApiBundle\Controller;
  3. use App\Entity\ClientContact;
  4. use App\MDS\AvexpressBundle\Entity\AveFiles;
  5. use App\MDS\AvexpressBundle\Entity\AveProductFile;
  6. use App\MDS\GreenPatioBundle\Entity\Reservation;
  7. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeDetails;
  8. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeDescription;
  9. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
  10. use App\MDS\GreenPatioBundle\Entity\ReservationLoungePicture;
  11. use App\MDS\GreenPatioBundle\Entity\ReservationService;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class ApiProposalsClientController extends AbstractController
  19. {
  20.    private $em;
  21.     public function __construct(EntityManagerInterface $em)
  22.     {
  23.         $this->em $em;
  24.     }
  25.     /**
  26.      * @Route("/api/client/proposal/{token}", name="generate_token", methods={"GET"})
  27.      */
  28.     public function generateToken(Request $request$token)
  29.     {
  30.         // Obtener reserva asociada al token
  31.         // Quitamos el enviroment
  32.         $posToken strpos($token'&env');
  33.         if ($posToken !== false) {
  34.             $token substr($token0$posToken);
  35.         }
  36.         $reservation $this->em->getRepository(Reservation::class)->findOneBy(['token' => $token]);
  37.         if (!$reservation) {
  38.             throw $this->createNotFoundException('No se encontró ninguna reserva para el token proporcionado.');
  39.         }
  40.         // Obtener file de AvExpress asociada a la reserva
  41.         $fileAv $this->em->getRepository(AveFiles::class)->findOneByReservation($reservation->getId());
  42.         $data = [
  43.             'reservation' => $this->formatReservation($reservation),
  44.             'options' => [],
  45.             'reservationService' => []
  46.         ];
  47.         // Agregar servicios de la reserva
  48.         $reservationServices $this->em->getRepository(ReservationService::class)->findBy(['reservationId' => $reservation->getId()]);
  49.         foreach ($reservationServices as $service) {
  50.             if ( $service->getUnits() > ) {
  51.                 $units $service->getUnits();
  52.             } else {
  53.                 $units 1;
  54.             }
  55.             if ( $service->getPax() > ) {
  56.                 $pax $service->getPax();
  57.             } else {
  58.                 $pax 1;
  59.             }
  60.             $priceOver $service->getOver(); // Precio adicional fijo
  61.             $price $service->getPrice(); // Precio base del servicio
  62.             $commission $service->getCommission(); // Porcentaje de comisión
  63.             $iva $service->getIva(); // Porcentaje de IVA
  64.             // Calcula el precio total base por pax y unidades
  65.             $totalPrice = ($price $pax $units);
  66.             // Calcula la comisión como porcentaje del precio base
  67.             $priceCommission $totalPrice * ($commission 100);
  68.             // Suma la comisión al precio base
  69.             $totalConComision $totalPrice $priceCommission;
  70.             $dateInAt $service->getDateInAt();
  71.             $dateOutAt $service->getDateOutAt();
  72.             if ($dateInAt && $dateOutAt) {
  73.                 // Calcular la diferencia
  74.                 $interval $dateInAt->diff($dateOutAt);
  75.                 // Obtener los días
  76.                 $days = ($interval->days) + 1;
  77.             }
  78.             // Suma el precio fijo ($priceOver) al subtotal con comisión
  79.             $totalConDays $totalConComision $days;
  80.             $totalConOver $totalConDays $priceOver;
  81.             // Calcula el IVA como porcentaje del total con el over incluido
  82.             $priceIva $totalConOver * ($iva 100);
  83.             // Suma el IVA al total
  84.             $totalPrice $totalConOver $priceIva;
  85.             $data['reservationService'][] = [
  86.                 'id' => $service->getId(),
  87.                 'reservationId' => $service->getReservationId(),
  88.                 'supplierId' => $service->getSupplierId(),
  89.                 'serviceId' => $service->getServiceId(),
  90.                 'serviceCatId' => $service->getServiceCatId(),
  91.                 'serviceCatName' => $service->getServiceCatName(),
  92.                 'name' => $service->getName(),
  93.                 'price' => $service->getPrice(),
  94.                 'currency' => $service->getCurrency(),
  95.                 'units' => $units,
  96.                 'opCommission' => $service->getOpCommission(),
  97.                 'commission' => $service->getCommission(),
  98.                 'opOver' => $service->getOpOver(),
  99.                 'priceOver' => $priceOver,
  100.                 'opIva' => $service->getOpIva(),
  101.                 'iva' => $service->getIva(),
  102.                 'pax' => $pax,
  103.                 'hour' => $service->getHour(),
  104.                 'dateInAt' => $service->getDateInAt() ? $service->getDateInAt()->format('Y-m-d H:i:s') : null,
  105.                 'dateOutAt' => $service->getDateOutAt() ? $service->getDateOutAt()->format('Y-m-d H:i:s') : null,
  106.                 'contcolor' => $service->getContcolor(),
  107.                 'rank' => $service->getRank(),
  108.                 'assistantId' => $service->getAssistantId(),
  109.                 'activityId' => $service->getActivityId(),
  110.                 'pay' => $service->getPay(),
  111.                 'createdAt' => $service->getCreatedAt()->format('Y-m-d H:i:s'),
  112.                 'createdId' => $service->getCreatedId(),
  113.                 'updatedAt' => $service->getUpdatedAt()->format('Y-m-d H:i:s'),
  114.                 'updatedId' => $service->getUpdatedId(),
  115.                 'toInvoice' => $service->getToInvoice(),
  116.                 'totalSinIva' => $totalConOver,
  117.                 'totalIva' => $priceIva,
  118.                 'totalPrice' => $totalPrice,
  119.                 'viewInfo' => $service->getViewInfo()
  120.             ];
  121.         }
  122.         // Procesar los salones
  123.         $loungeItems $this->em->getRepository(ReservationLoungeSimple::class)->findBy(['idReservation' => $reservation->getId()]);
  124.         foreach ($loungeItems as $item) {
  125.             // Obtener el idioma en relación al reservationLoungeSimple
  126.             $data['reservation']['idWebLanguage'] = $item->getLanguage() ? $item->getLanguage() : 1// Default to Spanish if not set
  127.             $uniqueId $item->getId();
  128.             $rankQuote $item->getRankQuote();
  129.             $loungeIva $item->getIva();
  130.             if (empty($loungeIva) && !is_numeric($loungeIva)) {
  131.                 $loungeIva 21;
  132.             }
  133.             // Formatear los datos del lounge
  134.             $loungeData $this->formatLoungeData($item$this->em$uniqueId$loungeIva);
  135.             // Formatear los datos de los productos de Av en la sala
  136.             $productsAvData $this->formatProductsAv($fileAv->getId(), $item->getIdLounge(), $this->em);
  137.             // Agrupar por rankQuote
  138.             if (!isset($data['options'][$rankQuote])) {
  139.                 $data['options'][$rankQuote] = [];
  140.             }
  141.             $data['options'][$rankQuote][] = $loungeData;
  142.             if (!empty($productsAvData)){ $data['options'][$rankQuote][] = $productsAvData; }
  143.         }
  144.         return new JsonResponse($data);
  145.     }
  146.     private function formatReservation($reservation)
  147.     {
  148.         $contactEmail null;
  149.         if($reservation->getClientContact()){
  150.             $contact $this->em->getRepository(ClientContact::class)->findById($reservation->getClientContact());
  151.             $contactEmail $contact->getEmail();
  152.         } else{
  153.             $contactEmail $reservation->getContactUnregistered();
  154.         }
  155.         return [
  156.             'id' => $reservation->getId(),
  157.             'title' => $reservation->getTitle(),
  158.             'client' => $reservation->getClient(),
  159.             'createdAt' => $reservation->getCreatedAt()->format('Y-m-d H:i:s'),
  160.             'priority' => $reservation->getPriority(),
  161.             'dateStart' => $reservation->getDateStart() ? $reservation->getDateStart()->format('Y-m-d H:i:s') : null,
  162.             'dateEnd' => $reservation->getDateEnd() ? $reservation->getDateEnd()->format('Y-m-d H:i:s') : null,
  163.             'createdBy' => $reservation->getCreatedBy(),
  164.             'supplier' => $reservation->getSupplier(),
  165.             'status' => $reservation->getStatus(),
  166.             'updatedAt' => $reservation->getUpdatedAt()->format('Y-m-d H:i:s'),
  167.             'updatedBy' => $reservation->getUpdatedBy(),
  168.             'daysBlock' => $reservation->getDaysBlock(),
  169.             'advancePayment' => $reservation->getAdvancePayment(),
  170.             'idProposal' => $reservation->getIdProposal(),
  171.             'cateringName' => $reservation->getCateringName(),
  172.             'boolCatering' => $reservation->getBoolCatering(),
  173.             'pax' => $reservation->getPax(),
  174.             'deposit' => $reservation->getDeposit(),
  175.             'accessKey' => $reservation->getAccessKey(),
  176.             'description' => $reservation->getDescription(),
  177.             'clientContact' => $reservation->getClientContact(),
  178.             'contactUnregistered' => $reservation->getContactUnregistered(),
  179.             'days' => $reservation->getDays(),
  180.             'contract' => $reservation->getContract(),
  181.             'nameContactUnregistered' => $reservation->getNameContactUnregistered(),
  182.             'phoneContactUnregistered' => $reservation->getPhoneContactUnregistered(),
  183.             'token' => $reservation->getToken(),
  184.             'contactEmail' => $contactEmail,
  185.         ];
  186.     }
  187.     private function formatLoungeData($item$entityManager$uniqueId$loungeIva)
  188.     {
  189.         $loungeName $item->getLoungeName();
  190.         $reservationLoungeDetail $this->em->getRepository(ReservationLoungeDetails::class)->findOneBy(['name' => $loungeName]);
  191.         if (!$reservationLoungeDetail) {
  192.             return [];
  193.         }
  194.         try {
  195.             $loungeIva $item->getServicePrice() * ($loungeIva 100);
  196.         } catch (\Throwable $th) {
  197.             $loungeIva $item->getServicePrice() * (21 100);
  198.         }
  199.         $comboIds $reservationLoungeDetail->getCombo();
  200.         return [
  201.             'id' => $uniqueId,
  202.             'loungeId' => $reservationLoungeDetail->getId(),
  203.             'loungeName' => $reservationLoungeDetail->getName(),
  204.             'type' => $item->getType(), // Añadir type
  205.             'rankQuote' => $item->getRankQuote(), // Añadir rankQuote
  206.             'dateStart' => $item->getDateStart() ? $item->getDateStart()->format('Y-m-d H:i:s') : null,
  207.             'dateEnd' => $item->getDateEnd() ? $item->getDateEnd()->format('Y-m-d H:i:s') : null,
  208.             'pax' => $item->getPax(),
  209.             'price' => $item->getServicePrice(),
  210.             'loungeIva' => $loungeIva,
  211.             'combo' => $comboIds,
  212.             'isCombo' => $comboIds 0,
  213.             'descriptions' => $this->getDescriptions($reservationLoungeDetail$entityManager),
  214.             'loungeDescription' => $item->getLoungeDescription(),
  215.             'importantDescription' => (empty($item->getType())) ? $item->getImportantDescription() : '',
  216.             'importantDescGeneralText' => $item->getImportantDescGeneralText(),
  217.             'importantDescSchedules' => $item->getImportantDescSchedules(),
  218.             'importantDescParking' => $item->getImportantDescParking(),
  219.             'pictures' => $this->getPictures($reservationLoungeDetail$entityManager),
  220.             'comboDetails' => $comboIds $this->getComboDetails($comboIds$entityManager) : [],
  221.             'loungeDetails' => $this->getLoungeDetails($reservationLoungeDetail->getId(), $entityManager),
  222.             'totalPrice' => $item->getServicePrice() + $loungeIva
  223.         ];
  224.     }
  225.     private function getDescriptions($loungeDetail$entityManager)
  226.     {
  227.         $descriptions = [];
  228.         $result $this->em->getRepository(ReservationLoungeDescription::class)->findBy(['loungeId' => $loungeDetail->getId()]);
  229.         foreach ($result as $description) {
  230.             $descriptions[] = [
  231.                 'language' => $description->getLanguage(),
  232.                 'description' => $description->getDescription(),
  233.                 'createdAt' => $description->getCreatedAt()->format('Y-m-d H:i:s'),
  234.                 'updatedAt' => $description->getUpdatedAt()->format('Y-m-d H:i:s'),
  235.             ];
  236.         }
  237.         return $descriptions;
  238.     }
  239.     private function getPictures($loungeDetail$entityManager)
  240.     {
  241.         $pictures = [];
  242.         $result $this->em->getRepository(ReservationLoungePicture::class)->findBy(['loungeId' => $loungeDetail->getId()]);
  243.         foreach ($result as $picture) {
  244.             $pictures[] = [
  245.                 'id' => $picture->getId(),
  246.                 'title' => $picture->getTitle(),
  247.                 'imageLarge' => $picture->getImageLarge(),
  248.                 'imageMedium' => $picture->getImageMedium(),
  249.                 'imageSmall' => $picture->getImageSmall(),
  250.                 'createdAt' => $picture->getCreatedAt()->format('Y-m-d H:i:s'),
  251.                 'updatedAt' => $picture->getUpdatedAt()->format('Y-m-d H:i:s'),
  252.             ];
  253.         }
  254.         return $pictures;
  255.     }
  256.     private function getComboDetails($comboIds$entityManager)
  257.     {
  258.         $comboDetails = [];
  259.         $comboIdArray explode(','$comboIds);
  260.         foreach ($comboIdArray as $comboId) {
  261.             $comboDetail $this->em->getRepository(ReservationLoungeDetails::class)->find($comboId);
  262.             if ($comboDetail) {
  263.                 $comboDetails[] = [
  264.                     'id' => $comboDetail->getId(),
  265.                     'loungeName' => $comboDetail->getName(),
  266.                     'meters' => $comboDetail->getMeters(),
  267.                     'length' => $comboDetail->getLength(),
  268.                     'width' => $comboDetail->getWidth(),
  269.                     'height' => $comboDetail->getHeight(),
  270.                     'capSchool' => $comboDetail->getCapSchool(),
  271.                     'capTheater' => $comboDetail->getCapTheater(),
  272.                     'capCocktail' => $comboDetail->getCapCocktail(),
  273.                     'capBanquet' => $comboDetail->getCapBanquet(),
  274.                     'capImperial' => $comboDetail->getCapImperial(),
  275.                     'rankLounge' => $comboDetail->getRankLounge(),
  276.                     'descriptions' => $this->getDescriptions($comboDetail$entityManager),
  277.                     'pictures' => $this->getPictures($comboDetail$entityManager),
  278.                 ];
  279.             }
  280.         }
  281.         return $comboDetails;
  282.     }
  283.     private function getLoungeDetails($id$entityManager)
  284.     {
  285.         // Buscar detalles del lounge directamente en la tabla ReservationLoungeDetails
  286.         $loungeDetail $this->em->getRepository(ReservationLoungeDetails::class)->findOneBy(['id' => $id]);
  287.         if (!$loungeDetail) {
  288.             return null// Si no se encuentra, devolvemos null
  289.         }
  290.         return [
  291.             'id' => $loungeDetail->getId(),
  292.             'name' => $loungeDetail->getName(),
  293.             'meters' => $loungeDetail->getMeters(),
  294.             'length' => $loungeDetail->getLength(),
  295.             'width' => $loungeDetail->getWidth(),
  296.             'height' => $loungeDetail->getHeight(),
  297.             'capSchool' => $loungeDetail->getCapSchool(),
  298.             'capTheater' => $loungeDetail->getCapTheater(),
  299.             'capCocktail' => $loungeDetail->getCapCocktail(),
  300.             'capBanquet' => $loungeDetail->getCapBanquet(),
  301.             'capImperial' => $loungeDetail->getCapImperial(),
  302.             'rankLounge' => $loungeDetail->getRankLounge(),
  303.             'createdAt' => $loungeDetail->getCreatedAt() ? $loungeDetail->getCreatedAt()->format('Y-m-d H:i:s') : null,
  304.             'updatedAt' => $loungeDetail->getUpdatedAt() ? $loungeDetail->getUpdatedAt()->format('Y-m-d H:i:s') : null,
  305.         ];
  306.     }
  307.     private function formatProductsAv($fileAvId$loungeId,$entityManager)
  308.     {
  309.         $products $this->em->getRepository(AveProductFile::class)->findBy(['fileId' => $fileAvId'idGpLounge' => $loungeId,]);
  310.         if (!$products) {
  311.             return [];
  312.         }
  313.         $arrayProductsAv = [];
  314.         foreach ($products as $product){
  315.             $arrayProductsAv[] = [
  316.                 'id' => $product->getId(),
  317.                 'productName' => $product->getProductName(),
  318.                 'servicePrice' => $product->getServicePrice(),
  319.                 'pax' => $product->getPax(),
  320.                 'type' => $product->getType(),
  321.                 'units' => $product->getUnits(),
  322.                 'opCommission' => $product->getOpCommission(),
  323.                 'commission' => $product->getCommission(),
  324.                 'opOver' => $product->getOpOver(),
  325.                 'priceOver' => $product->getOver(),
  326.                 'opIva' => $product->getOpIva(),
  327.                 'iva' => $product->getIva(),
  328.                 'dateInAt' => $product->getDateInAt(),
  329.                 'dateOutAt' => $product->getDateOutAt(),
  330.                 'days' => $product->getDays(),
  331.                 'location' => $product->getLocation(),
  332.                 'idGpLounge' => $product->getIdGpLounge(),
  333.                 'subTotalPrice' => $product->getSubTotalPrice(),
  334.             ];
  335.         }
  336.         return $arrayProductsAv;
  337.     }
  338. }