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