src/MDS/GreenPatioBundle/Controller/BlvReservationsInvoiceController.php line 599

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * Date: 19/06/2025
  5.  * Time: 14:49
  6.  */
  7. namespace App\MDS\GreenPatioBundle\Controller;
  8. use App\Entity\Cities;
  9. use App\Entity\Client;
  10. use App\Entity\Country;
  11. use App\Entity\Regions;
  12. use App\Entity\SettingsCompany;
  13. use App\Entity\Supplier;
  14. use App\MDS\GreenPatioBundle\Entity\ReservationBudgetItems;
  15. use App\MDS\GreenPatioBundle\Entity\BlvReservationInvoiceItems;
  16. use App\MDS\GreenPatioBundle\Entity\ReservationInvoiceProformaItems;
  17. use App\MDS\GreenPatioBundle\Entity\BlvReservationInvoiceRecItems;
  18. use App\MDS\GreenPatioBundle\Entity\ReservationPaymentsClient;
  19. use App\MDS\GreenPatioBundle\Entity\ReservationProforma;
  20. use App\MDS\GreenPatioBundle\Entity\BlvReservationInvoice;
  21. use App\MDS\GreenPatioBundle\Entity\BlvReservationInvoiceRec;
  22. use App\MDS\GreenPatioBundle\Entity\ReservationInvoiceDepositControl;
  23. use App\MDS\GreenPatioBundle\Entity\Reservation;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use App\MDS\GreenPatioBundle\Entity\ReservationInvoiceDepositItems;
  29. use App\MDS\GreenPatioBundle\Entity\ReservationLoungeSimple;
  30. use App\MDS\GreenPatioBundle\Entity\ReservationService;
  31. use DateTime;
  32. use PHPExcel_Style_Fill;
  33. use PHPExcel_Style_NumberFormat;
  34. use PHPExcel_Style_Alignment;
  35. use PHPExcel_Shared_Font;
  36. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  37. use FOS\RestBundle\Controller\Annotations as Rest;
  38. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  39. class BlvReservationsInvoiceController extends AbstractController
  40. {
  41.     /**
  42.      * @Route("/blv/reservations/invoice/generatenew",  name="reservations_generatenew_invoice_blv")
  43.      * Generar una nueva factura Bella View
  44.      */
  45.     public function generatenewInvoiceAction(EntityManagerInterface $em, Request $request)
  46.     {
  47.         // Se genera y muestra en nueva pantalla la nueva proforma
  48.         $arrayBools = array( 'lounge' => null, 'service' => null, 'payment' => null, );
  49.         $arrayBools['lounge'] = $request->query->get('lounge');
  50.         $arrayBools['service'] = $request->query->get('service');
  51.         $arrayBools['payment'] = $request->query->get('payment');
  52.         $clientName = $request->query->get('clientName');
  53.         $clientAddress = $request->query->get('clientAddress');
  54.         $clientDocument = $request->query->get('clientDocument');
  55.         $clientType = $request->query->get('clientType');
  56.         $id = $request->query->get('id');
  57.         //INICIO: buscamos el ID de la factura
  58.         $allFacturas = $em->getRepository(BlvReservationInvoice::class)->findAll();
  59.         if (!empty($allFacturas)){ $invoiceId = end($allFacturas)->getId() + 1; } else { $invoiceId = 100; }
  60.         $boolMakeInvoice = false;  // Control para verificar si se debe hacer factura o todos los indicadores (sala, servicios y pagos) estaban en falso
  61.         //FIN: buscamos el ID de la factura
  62.         /* Obtengo usuario logueado */
  63.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  64.         $user_id = $user_logueado->getId();
  65.         //Acumuladores de datos globales de la factura
  66.         $valorTotalNet = 0; $valorVat = 0;
  67.         //INICIO: Creamos los items de la factura
  68.         if (!empty($arrayBools['lounge'])) {
  69.             foreach ($arrayBools['lounge'] as $key => $item) {
  70.                 if ($item == 'true') {
  71.                     $boolMakeInvoice = true;
  72.                     $sala = $em->getRepository(ReservationLoungeSimple::class)->findOneById($key);
  73.                     $itemInvoice = new BlvReservationInvoiceItems();
  74.                     $itemInvoice->setReservationId($id);
  75.                     $itemInvoice->setInvoiceId($invoiceId);
  76.                     $itemInvoice->setItemType('LOUNGE');
  77.                     $itemInvoice->setLngControlId($key);
  78.                     $itemInvoice->setLngLoungeName($sala->getLoungeName());
  79.                     $itemInvoice->setLngIdLounge($sala->getIdLounge());
  80.                     $itemInvoice->setLngIdLounge($sala->getIdLounge());
  81.                     $itemInvoice->setLngDateStart($sala->getDateStart());
  82.                     $itemInvoice->setLngDateEnd($sala->getDateEnd());
  83.                     $itemInvoice->setLngServicePrice($sala->getServicePrice());
  84.                     $itemInvoice->setLngPax($sala->getPax());
  85.                     $itemInvoice->setLngType($sala->getType());
  86.                     $itemInvoice->setLngIva($sala->getIva());
  87.                     $itemInvoice->setLngOpIva($sala->getOpIva());
  88.                     $itemInvoice->setCreatedId($user_id);
  89.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  90.                     $itemInvoice->setUpdatedId($user_id);
  91.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  92.                     $itemInvoice->setLngHourStart($sala->getHourStart());
  93.                     $itemInvoice->setLngMinStart($sala->getMinStart());
  94.                     $itemInvoice->setLngHourEnd($sala->getHourEnd());
  95.                     $itemInvoice->setLngMinEnd($sala->getMinEnd());
  96.                     $em->persist($itemInvoice);
  97.                     $em->flush();
  98.                     //Acumulamos neto e iva para la factura
  99.                     $valorTotalNet += $itemInvoice->getLngServicePrice();
  100.                     $valorVat += round(($itemInvoice->getLngServicePrice() * ($itemInvoice->getLngIva() / 100)),2,PHP_ROUND_HALF_UP);
  101.                 }
  102.             }
  103.         }
  104.         if (!empty($arrayBools['service'])) {
  105.             foreach ($arrayBools['service'] as $key => $item) {
  106.                 if ($item == 'true') {
  107.                     $boolMakeInvoice = true;
  108.                     $servicio = $em->getRepository(ReservationService::class)->findOneById($key);
  109.                     $itemInvoice = new BlvReservationInvoiceItems();
  110.                     $itemInvoice->setReservationId($id);
  111.                     $itemInvoice->setInvoiceId($invoiceId);
  112.                     $itemInvoice->setItemType('SERVICE');
  113.                     $itemInvoice->setSrvControlId($key);
  114.                     $itemInvoice->setSrvName($servicio->getName());
  115.                     $itemInvoice->setSrvSupplierId($servicio->getSupplierId());
  116.                     $itemInvoice->setSrvServiceId($servicio->getServiceId());
  117.                     $itemInvoice->setSrvServiceCatId($servicio->getServiceCatId());
  118.                     $itemInvoice->setSrvServiceCatName($servicio->getServiceCatName());
  119.                     $itemInvoice->setSrvPrice($servicio->getPrice());
  120.                     $itemInvoice->setSrvCurrency($servicio->getCurrency());
  121.                     $itemInvoice->setSrvUnits($servicio->getUnits());
  122.                     $itemInvoice->setSrvOpCommission($servicio->getOpCommission());
  123.                     $itemInvoice->setSrvCommission($servicio->getCommission());
  124.                     $itemInvoice->setSrvOpOver($servicio->getOpOver());
  125.                     $itemInvoice->setSrvOver($servicio->getOver());
  126.                     $itemInvoice->setSrvOpIva($servicio->getOpIva());
  127.                     $itemInvoice->setSrvIva($servicio->getIva());
  128.                     $itemInvoice->setSrvPax($servicio->getPax());
  129.                     $itemInvoice->setSrvHour($servicio->getHour());
  130.                     $itemInvoice->setSrvDateInAt($servicio->getDateInAt());
  131.                     $itemInvoice->setSrvDateOutAt($servicio->getDateOutAt());
  132.                     $itemInvoice->setSrvContcolor($servicio->getContcolor());
  133.                     $itemInvoice->setSrvRank($servicio->getRank());
  134.                     $itemInvoice->setSrvAssistantId($servicio->getAssistantId());
  135.                     $itemInvoice->setSrvActivityId($servicio->getActivityId());
  136.                     $itemInvoice->setSrvPay($servicio->getPay());
  137.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  138.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  139.                     $itemInvoice->setCreatedId($user_id);
  140.                     $itemInvoice->setUpdatedId($user_id);
  141.                     $em->persist($itemInvoice);
  142.                     $em->flush();
  143.                     //Acumulamos neto e iva para la factura
  144.                     if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  145.                         $subtotal = 0;
  146.                         $neto = 0;
  147.                         $subtotalService = 0;
  148.                     } else {
  149.                         $subtotalService = $itemInvoice->getSrvPrice();
  150.                         $subneto = $itemInvoice->getSrvPrice();
  151.                         // Commission
  152.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  153.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  154.                             $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  155.                         } else {
  156.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  157.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  158.                         }
  159.                         // Over
  160.                         if ($itemInvoice->getSrvOpOver()=='1'){
  161.                             $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  162.                             $subneto = $subneto + $itemInvoice->getSrvOver();
  163.                         } else {
  164.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  165.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  166.                         }
  167.                         // IVA
  168.                         if ($itemInvoice->getSrvOpIva()=='1'){
  169.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  170.                         } else {
  171.                             $subtotalService = $itemInvoice->getSrvPrice();
  172.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  173.                         }
  174.                         switch ($itemInvoice->getSrvServiceCatId()){
  175.                             case 1: // Alojamiento
  176.                                 // el numero de noches $numNoches; precio unitario $subneto
  177.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  178.                                 // La personas no afectan este calculo
  179.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  180.                                 $subnetoUnit = $subneto;
  181.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  182.                                 break;
  183.                             case 2: //Actividades
  184.                                 // El número de personas es considerado en el calculo
  185.                                 $pax = $itemInvoice->getSrvPax();
  186.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  187.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  188.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  189.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  190.                                 $subnetoUnit = $subneto;
  191.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  192.                                 break;
  193.                             case 3: // AV
  194.                                 $pax = $itemInvoice->getSrvPax();
  195.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  196.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  197.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  198.                                 $unitsServ = $itemInvoice->getSrvUnits();
  199.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  200.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  201.                                 $subnetoUnit = $subneto;
  202.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  203.                                 break;
  204.                             case 4: //Creative
  205.                                 $pax = $itemInvoice->getSrvPax();
  206.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  207.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  208.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  209.                                 $unitsServ = $itemInvoice->getSrvUnits();
  210.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  211.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  212.                                 $subnetoUnit = $subneto;
  213.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  214.                                 break;
  215.                             case 5: //Cruise
  216.                                 $pax = $itemInvoice->getSrvPax();
  217.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  218.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  219.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  220.                                 $unitsServ = $itemInvoice->getSrvUnits();
  221.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  222.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  223.                                 $subnetoUnit = $subneto;
  224.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  225.                                 break;
  226.                             case 6: //Entertaiment
  227.                                 $pax = $itemInvoice->getSrvPax();
  228.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  229.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  230.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  231.                                 $unitsServ = $itemInvoice->getSrvUnits();
  232.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  233.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  234.                                 $subnetoUnit = $subneto;
  235.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  236.                                 break;
  237.                             case 7: // Gifts
  238.                                 $pax = $itemInvoice->getSrvPax();
  239.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  240.                                 $days = 1;
  241.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  242.                                 $unitsServ = $itemInvoice->getSrvUnits();
  243.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  244.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  245.                                 $subnetoUnit = $subneto;
  246.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  247.                                 break;
  248.                             case 8: //Guide
  249.                                 $pax = $itemInvoice->getSrvPax();
  250.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  251.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  252.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  253.                                 $unitsServ = $itemInvoice->getSrvUnits();
  254.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  255.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  256.                                 $subnetoUnit = $subneto;
  257.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  258.                                 break;
  259.                             case 9: //Itineraries
  260.                                 $pax = $itemInvoice->getSrvPax();
  261.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  262.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  263.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  264.                                 $unitsServ = $itemInvoice->getSrvUnits();
  265.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  266.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  267.                                 $subnetoUnit = $subneto;
  268.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  269.                                 break;
  270.                             case 10: //Lounge  -- No Aplica
  271.                                 break;
  272.                             case 11: //Menu (Catering)
  273.                                 $pax = $itemInvoice->getSrvPax();
  274.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  275.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  276.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  277.                                 $unitsServ = $itemInvoice->getSrvUnits();
  278.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  279.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  280.                                 $subnetoUnit = $subneto;
  281.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  282.                                 break;
  283.                             case 12: //Others
  284.                                 $pax = $itemInvoice->getSrvPax();
  285.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  286.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  287.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  288.                                  $unitsServ = $itemInvoice->getSrvUnits();
  289.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  290.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  291.                                 $subnetoUnit = $subneto;
  292.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  293.                                 break;
  294.                             case 13: //Transport
  295.                                 $pax = $itemInvoice->getSrvPax();
  296.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  297.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  298.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  299.                                 $unitsServ = $itemInvoice->getSrvUnits();
  300.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  301.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  302.                                 $subnetoUnit = $subneto;
  303.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  304.                                 break;
  305.                             case 14: //Technology
  306.                                 $pax = $itemInvoice->getSrvPax();
  307.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  308.                                 $days = 1;
  309.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  310.                                 $unitsServ = $itemInvoice->getSrvUnits();
  311.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  312.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  313.                                 $subnetoUnit = $subneto;
  314.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  315.                                 break;
  316.                             case 15: //Assisstant
  317.                                 $pax = $itemInvoice->getSrvPax();
  318.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  319.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  320.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  321.                                 $unitsServ = $itemInvoice->getSrvUnits();
  322.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  323.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  324.                                 $subnetoUnit = $subneto;
  325.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  326.                                 break;
  327.                             case 16: //DDR
  328.                                 $pax = $itemInvoice->getSrvPax();
  329.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  330.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  331.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  332.                                 $unitsServ = $itemInvoice->getSrvUnits();
  333.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  334.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  335.                                 $subnetoUnit = $subneto;
  336.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  337.                                 break;
  338.                             case 17: //Seguridad
  339.                                 $pax = $itemInvoice->getSrvPax();
  340.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  341.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  342.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  343.                                 $unitsServ = $itemInvoice->getSrvUnits();
  344.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  345.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  346.                                 $subnetoUnit = $subneto;
  347.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  348.                                 break;
  349.                             case 18: //WiFi
  350.                                 $pax = $itemInvoice->getSrvPax();
  351.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  352.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  353.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  354.                                 $unitsServ = $itemInvoice->getSrvUnits();
  355.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  356.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  357.                                 $subnetoUnit = $subneto;
  358.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  359.                                 break;
  360.                             case 19: //Mobiliario
  361.                                 $pax = $itemInvoice->getSrvPax();
  362.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  363.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  364.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  365.                                 $unitsServ = $itemInvoice->getSrvUnits();
  366.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  367.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  368.                                 $subnetoUnit = $subneto;
  369.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  370.                                 break;
  371.                             case 20: //Parking
  372.                                 $pax = $itemInvoice->getSrvPax();
  373.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  374.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  375.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  376.                                 $unitsServ = $itemInvoice->getSrvUnits();
  377.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  378.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  379.                                 $subnetoUnit = $subneto;
  380.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  381.                                 break;
  382.                             case 21: //Limpieza
  383.                                 $pax = $itemInvoice->getSrvPax();
  384.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  385.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  386.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  387.                                 $unitsServ = $itemInvoice->getSrvUnits();
  388.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  389.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  390.                                 $subnetoUnit = $subneto;
  391.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  392.                                 break;
  393.                             default: //Others (por default)
  394.                                 $pax = $itemInvoice->getSrvPax();
  395.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  396.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  397.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  398.                                 $unitsServ = $itemInvoice->getSrvUnits();
  399.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  400.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  401.                                 $subnetoUnit = $subneto;
  402.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  403.                                 break;
  404.                         }
  405.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  406.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  407.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  408.                         $valorTotalNet = $valorTotalNet + $neto;
  409.                         $valorVat = $valorVat + ($subtotal - $neto);
  410.                     }
  411.                 }
  412.             }
  413.         }
  414.         if (!empty($arrayBools['payment'])) {
  415.             foreach ($arrayBools['payment'] as $key => $item) {
  416.                 if ($item == 'true') {
  417.                     $boolMakeInvoice = true;
  418.                     $pago = $em->getRepository(ReservationPaymentsClient::class)->findOneById($key);
  419.                     $itemInvoice = new BlvReservationInvoiceItems();
  420.                     $itemInvoice->setReservationId($id);
  421.                     $itemInvoice->setInvoiceId($invoiceId);
  422.                     $itemInvoice->setItemType('PAYMENT');
  423.                     $itemInvoice->setPayControlId($key);
  424.                     $itemInvoice->setPayAmount($pago->getAmount());
  425.                     $itemInvoice->setPayVat($pago->getVat());
  426.                     $itemInvoice->setPayAmountTotal($pago->getAmountTotal());
  427.                     $itemInvoice->setPayDatePayAt($pago->getDatePayAt());
  428.                     $itemInvoice->setPayWayToPay($pago->getWayToPay());
  429.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  430.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  431.                     $itemInvoice->setCreatedId($user_id);
  432.                     $itemInvoice->setUpdatedId($user_id);
  433.                     $em->persist($itemInvoice);
  434.                     $em->flush();
  435.                     //Acumulamos neto e iva para la factura
  436.                     //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  437.                     $valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();
  438.                     $valorVat = $valorVat - round(($itemInvoice->getPayAmount() * ($itemInvoice->getPayVat()/100)),2,PHP_ROUND_HALF_UP);
  439.                 }
  440.             }
  441.         }
  442.         //FIN: Creamos los items de la factura
  443.         $valorTotal = $valorTotalNet + $valorVat;
  444.         //INICIO: Creamos la factura
  445.         $newInvoice = new BlvReservationInvoice();
  446.         $newInvoice->setNumber("LGD-".date('dmy')."-".$id);
  447.         $newInvoice->setDateAt(new DateTime('now'));
  448.         $newInvoice->setType('Invoice');
  449.         $newInvoice->setReservationId($id);
  450.         $newInvoice->setTotalNet($valorTotalNet);
  451.         $newInvoice->setVat($valorVat);
  452.         $newInvoice->setTotal($valorTotal);
  453.         $newInvoice->setCreatedAt(new DateTime('now'));
  454.         $newInvoice->setCreatedId($user_id);
  455.         $newInvoice->setUpdatedAt(new DateTime('now'));
  456.         $newInvoice->setUpdatedId($user_id);
  457.         $newInvoice->setMaster('master');
  458.         $newInvoice->setClientName($clientName);
  459.         $newInvoice->setClientAddress($clientAddress);
  460.         $newInvoice->setClientDocument($clientDocument);
  461.         $newInvoice->setClientType($clientType);
  462.         if ($boolMakeInvoice){
  463.             // Solo se debe crear factura si hay un elemento
  464.             $em->persist($newInvoice);
  465.             $em->flush();
  466.             $newInvoice->setNumber($newInvoice->getNumber().'-'.$newInvoice->getId());
  467.             $em->persist($newInvoice);
  468.             $em->flush();
  469.         } else {
  470.             // Se llama a la vista de facturas del expediente
  471.             return $this->redirectToRoute('reservations_viewreservation_invoices',array('id' => $id));
  472.         }
  473.         //FIN: Creamos la factura
  474.         $data = [];
  475.         $data = $this->baseInvoiceSelectedReservation($id, $arrayBools,'I'.$newInvoice->getId());
  476.         $paymentsClient = new ReservationPaymentsClient();
  477.         $paymentsClient->setReservationId($id);
  478.         $reserva = $em->getRepository(Reservation::class)->findOneById($id);
  479.         if ($boolMakeInvoice){
  480.             // Si se creo la factura se cambia el estado de la reserva a Facturado
  481.             $reserva->setStatus('Invoiced');
  482.             $em->persist($reserva);
  483.             $em->flush();
  484.         }
  485.         $reservaInv = new BlvReservationInvoice();
  486.         $reservaInv->setReservationId($id);
  487.         $idLounges = [];
  488.         $loungesBoolToInvoice = [];
  489.         if (array_key_exists('lounge',$data['datasupplier'])) {
  490.             foreach ($data['datasupplier']['lounge'] as $item) {
  491.                 $idLounges[] = $item['id'];
  492.                 $loungesBoolToInvoice[] = 1;
  493.             }
  494.             $idLounges = implode(',', $idLounges);
  495.             $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  496.         } else {
  497.             $idLounges = '0';
  498.             $loungesBoolToInvoice = '0';
  499.         }
  500.         $idPayments = [];
  501.         $paymentsBoolToInvoice = [];
  502.         if (array_key_exists('payment',$data['datasupplier'])) {
  503.             foreach ($data['datasupplier']['payment'] as $item) {
  504.                 $idPayments[] = $item['id'];
  505.                 $paymentsBoolToInvoice[] = 1;
  506.             }
  507.             $idPayments = implode(',', $idPayments);
  508.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  509.         } else {
  510.             $idPayments = '0';
  511.             $paymentsBoolToInvoice = '0';
  512.         }
  513.         if(empty($data['type'])){$data['type'] = 'Invoice';}
  514.         return $this->redirectToRoute('reservations_viewnew_invoice_blv',array('id' => $newInvoice->getId()));
  515.     }
  516.     /**
  517.      * @Route("/blv/reservations/invoice/viewnew/{id}",  name="reservations_viewnew_invoice_blv")
  518.      * Ver factura Bella View
  519.      */
  520.     public function viewNewInvoiceAction($id, EntityManagerInterface $em, Request $request)
  521.     {
  522.         // Se genera y muestra en nueva pantalla la nueva proforma
  523.         $arrayBools = array( 'lounge' => null, 'service' => null, 'payment' => null, );
  524.         $arrayBools['lounge'] = $request->request->get('inv_lounge_hidden_bool');
  525.         $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  526.         $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  527.         /* Obtengo usuario logueado */
  528.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  529.         $user_id = $user_logueado->getId();
  530.         //Acumuladores de datos globales de la factura
  531.         $valorTotalNet = 0;
  532.         $valorVat = 0;
  533.         //INICIO: Creamos los items de la factura
  534.         $arrayInvoicedItems = $em->getRepository(BlvReservationInvoiceItems::class)->findByInvoiceId($id);
  535.         if (!empty($arrayInvoicedItems)) {
  536.             foreach ($arrayInvoicedItems as $itemInvoice) {
  537.                 if ($itemInvoice->getItemType() == 'LOUNGE') {
  538.                     //Acumulamos neto e iva para la factura
  539.                     $valorTotalNet = $valorTotalNet + $itemInvoice->getLngServicePrice();
  540.                     $valorVat = $valorVat + round(($itemInvoice->getLngServicePrice() * ($itemInvoice->getLngIva() / 100)),2,PHP_ROUND_HALF_UP);
  541.                 }
  542.             }
  543.         }
  544.         if (!empty($arrayInvoicedItems)) {
  545.             foreach ($arrayInvoicedItems as $itemInvoice) {
  546.                 if ($itemInvoice->getItemType() == 'SERVICE') {
  547.                     //Acumulamos neto e iva para la factura
  548.                     if (is_null($itemInvoice->getSrvPrice())){
  549.                         $subtotal = $neto = $subtotalService = 0;
  550.                     } else {
  551.                         $subtotalService = $itemInvoice->getSrvPrice();
  552.                         $subtotalService = str_replace(',','.',$subtotalService);
  553.                         $subtotalService = floatval($subtotalService);
  554.                         $subneto = $itemInvoice->getSrvPrice();
  555.                         // Commission
  556.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  557.                             if (!($itemInvoice->getSrvCommission()=='0')) {
  558.                                 $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission() / 100));
  559.                                 $subneto = $subneto * (1 + ($itemInvoice->getSrvCommission() / 100));
  560.                             }
  561.                         } else {
  562.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  563.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  564.                         }
  565.                         // Over
  566.                         if ($itemInvoice->getSrvOpOver()=='1'){
  567.                             if (!($itemInvoice->getSrvOver()=='0')) {
  568.                                 $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  569.                                 $subneto = $subneto + $itemInvoice->getSrvOver();
  570.                             }
  571.                         } else {
  572.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  573.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  574.                         }
  575.                         // IVA
  576.                         if ($itemInvoice->getSrvOpIva()=='1'){
  577.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  578.                         } else {
  579.                             $subtotalService = $itemInvoice->getSrvPrice();
  580.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  581.                         }
  582.                         $subneto = str_replace(',','.',$subneto);
  583.                         $subneto = floatval($subneto);
  584.                         switch ($itemInvoice->getSrvServiceCatId()){
  585.                             case 1: // Alojamiento
  586.                                 // el numero de noches $numNoches; precio unitario $subneto
  587.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  588.                                 // La personas no afectan este calculo
  589.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  590.                                 $subnetoUnit = $subneto;
  591.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  592.                                 break;
  593.                             case 2: //Actividades
  594.                                 // El número de personas es considerado en el calculo
  595.                                 $pax = $itemInvoice->getSrvPax();
  596.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  597.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  598.                                 if ($days > 1){
  599.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  600.                                 } else {
  601.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  602.                                 }
  603.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  604.                                 $subnetoUnit = $subneto;
  605.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  606.                                 break;
  607.                             case 3: // AV
  608.                                 $pax = $itemInvoice->getSrvPax();
  609.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  610.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  611.                                 if ($days > 1){
  612.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  613.                                 } else {
  614.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  615.                                 }
  616.                                 $unitsServ = $itemInvoice->getSrvUnits();
  617.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  618.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  619.                                 $subnetoUnit = $subneto;
  620.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  621.                                 break;
  622.                             case 4: //Creative
  623.                                 $pax = $itemInvoice->getSrvPax();
  624.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  625.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  626.                                 if ($days > 1){
  627.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  628.                                 } else {
  629.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  630.                                 }
  631.                                 $unitsServ = $itemInvoice->getSrvUnits();
  632.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  633.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  634.                                 $subnetoUnit = $subneto;
  635.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  636.                                 break;
  637.                             case 5: //Cruise
  638.                                 $pax = $itemInvoice->getSrvPax();
  639.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  640.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  641.                                 if ($days > 1){
  642.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  643.                                 } else {
  644.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  645.                                 }
  646.                                 $unitsServ = $itemInvoice->getSrvUnits();
  647.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  648.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  649.                                 $subnetoUnit = $subneto;
  650.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  651.                                 break;
  652.                             case 6: //Entertaiment
  653.                                 $pax = $itemInvoice->getSrvPax();
  654.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  655.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  656.                                 if ($days > 1){
  657.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  658.                                 } else {
  659.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  660.                                 }
  661.                                 $unitsServ = $itemInvoice->getSrvUnits();
  662.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  663.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  664.                                 $subnetoUnit = $subneto;
  665.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  666.                                 break;
  667.                             case 7: // Gifts
  668.                                 $pax = $itemInvoice->getSrvPax();
  669.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  670.                                 $days = 1;
  671.                                 if ($days > 1){
  672.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  673.                                 } else {
  674.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  675.                                 }
  676.                                 $unitsServ = $itemInvoice->getSrvUnits();
  677.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  678.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  679.                                 $subnetoUnit = $subneto;
  680.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  681.                                 break;
  682.                             case 8: //Guide
  683.                                 $pax = $itemInvoice->getSrvPax();
  684.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  685.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  686.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  687.                                 $unitsServ = $itemInvoice->getSrvUnits();
  688.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  689.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  690.                                 $subnetoUnit = $subneto;
  691.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  692.                                 break;
  693.                             case 9: //Itineraries
  694.                                 $pax = $itemInvoice->getSrvPax();
  695.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  696.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  697.                                 if ($days > 1){
  698.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  699.                                 } else {
  700.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  701.                                 }
  702.                                 $unitsServ = $itemInvoice->getSrvUnits();
  703.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  704.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  705.                                 $subnetoUnit = $subneto;
  706.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  707.                                 break;
  708.                             case 10: //Lounge  -- No Aplica
  709.                                 break;
  710.                             case 11: //Menu
  711.                                 $pax = $itemInvoice->getSrvPax();
  712.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  713.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  714.                                 if ($days > 1){
  715.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  716.                                 } else {
  717.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  718.                                 }
  719.                                 $unitsServ = $itemInvoice->getSrvUnits();
  720.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  721.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  722.                                 $subnetoUnit = $subneto;
  723.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  724.                                 break;
  725.                             case 12: //Others
  726.                                 $pax = $itemInvoice->getSrvPax();
  727.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  728.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  729.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  730.                                 $unitsServ = $itemInvoice->getSrvUnits();
  731.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; } else { $unitsServ = intval($unitsServ); }
  732.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  733.                                 $subnetoUnit = $subneto;
  734.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  735.                                 break;
  736.                             case 13: //Transport
  737.                                 $pax = $itemInvoice->getSrvPax();
  738.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  739.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  740.                                 if ($days > 1){
  741.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  742.                                 } else {
  743.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  744.                                 }
  745.                                 $unitsServ = $itemInvoice->getSrvUnits();
  746.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  747.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  748.                                 $subnetoUnit = $subneto;
  749.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  750.                                 break;
  751.                             case 14: //Technology
  752.                                 $pax = $itemInvoice->getSrvPax();
  753.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  754.                                 $days = 1;
  755.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  756.                                 $unitsServ = $itemInvoice->getSrvUnits();
  757.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  758.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  759.                                 $subnetoUnit = $subneto;
  760.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  761.                                 break;
  762.                             case 15: //Assisstant
  763.                                 $pax = $itemInvoice->getSrvPax();
  764.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  765.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  766.                                 if ($days > 1){
  767.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  768.                                 } else {
  769.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  770.                                 }
  771.                                 $unitsServ = $itemInvoice->getSrvUnits();
  772.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  773.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  774.                                 $subnetoUnit = $subneto;
  775.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  776.                                 break;
  777.                             case 16: //DDR
  778.                                 $pax = $itemInvoice->getSrvPax();
  779.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  780.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  781.                                 if ($days > 1){
  782.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  783.                                 } else {
  784.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  785.                                 }
  786.                                 $unitsServ = $itemInvoice->getSrvUnits();
  787.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  788.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  789.                                 $subnetoUnit = $subneto;
  790.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  791.                                 break;
  792.                             case 17: //Seguridad
  793.                                 $pax = $itemInvoice->getSrvPax();
  794.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  795.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  796.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  797.                                 $unitsServ = $itemInvoice->getSrvUnits();
  798.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  799.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  800.                                 $subnetoUnit = $subneto;
  801.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  802.                                 break;
  803.                             case 18: //WiFi
  804.                                 $pax = $itemInvoice->getSrvPax();
  805.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  806.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  807.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  808.                                 $unitsServ = $itemInvoice->getSrvUnits();
  809.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  810.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  811.                                 $subnetoUnit = $subneto;
  812.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  813.                                 break;
  814.                             case 19: //Mobiliario
  815.                                 $pax = $itemInvoice->getSrvPax();
  816.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  817.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  818.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  819.                                 $unitsServ = $itemInvoice->getSrvUnits();
  820.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  821.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  822.                                 $subnetoUnit = $subneto;
  823.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  824.                                 break;
  825.                             case 20: //Parking
  826.                                 $pax = $itemInvoice->getSrvPax();
  827.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  828.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  829.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  830.                                 $unitsServ = $itemInvoice->getSrvUnits();
  831.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  832.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  833.                                 $subnetoUnit = $subneto;
  834.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  835.                                 break;
  836.                             case 21: //Limpieza
  837.                                 $pax = $itemInvoice->getSrvPax();
  838.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  839.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  840.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  841.                                 $unitsServ = $itemInvoice->getSrvUnits();
  842.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  843.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  844.                                 $subnetoUnit = $subneto;
  845.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  846.                                 break;
  847.                             default: //Others (por default)
  848.                                 $pax = $itemInvoice->getSrvPax();
  849.                                 if (empty($pax) or $pax == "0") { $pax = 1; }
  850.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  851.                                 $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  852.                                 $unitsServ = $itemInvoice->getSrvUnits();
  853.                                 if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; } else { $unitsServ = intval($unitsServ); }
  854.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  855.                                 $subnetoUnit = $subneto;
  856.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  857.                                 break;
  858.                         }
  859.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  860.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  861.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  862.                         $valorTotalNet = $valorTotalNet + $neto;
  863.                         $valorVat = $valorVat + ($subtotal - $neto);
  864.                     }
  865.                 }
  866.             }
  867.         }
  868.         if (!empty($arrayInvoicedItems)) {
  869.             foreach ($arrayInvoicedItems as $itemInvoice) {
  870.                 if ($itemInvoice->getItemType() == 'PAYMENT') {
  871.                 }
  872.             }
  873.         }
  874.         //FIN: Creamos los items de la factura
  875.         $valorTotal = $valorTotalNet + $valorVat;
  876.         //INICIO: Creamos la factura
  877.         $newInvoice = $em->getRepository(BlvReservationInvoice::class)->findOneById($id);
  878.         //FIN: Creamos la factura
  879.         $data = [];
  880.         $data = $this->baseInvoiceDoneReservation($newInvoice->getReservationId(), $id,'I');
  881.         $paymentsClient = new ReservationPaymentsClient();
  882.         $paymentsClient->setReservationId($id);
  883.         $reservaInv = new BlvReservationInvoice();
  884.         $reservaInv->setReservationId($id);
  885.         $idLounges = [];
  886.         $loungesBoolToInvoice = [];
  887.         if (array_key_exists('lounge',$data['datasupplier'])) {
  888.             foreach ($data['datasupplier']['lounge'] as $item) {
  889.                 $idLounges[] = $item['id'];
  890.                 $loungesBoolToInvoice[] = 1;
  891.             }
  892.             $idLounges = implode(',', $idLounges);
  893.             $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  894.         } else {
  895.             $idLounges = '0';
  896.             $loungesBoolToInvoice = '0';
  897.         }
  898.         $idPayments = [];
  899.         $paymentsBoolToInvoice = [];
  900.         if (array_key_exists('payment',$data['datasupplier'])) {
  901.             foreach ($data['datasupplier']['payment'] as $item) {
  902.                 $idPayments[] = $item['id'];
  903.                 $paymentsBoolToInvoice[] = 1;
  904.             }
  905.             $idPayments = implode(',', $idPayments);
  906.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  907.         } else {
  908.             $idPayments = '0';
  909.             $paymentsBoolToInvoice = '0';
  910.         }
  911.         // Una factura solo se puede rectificar una vez
  912.         $rectf = $em->getRepository(BlvReservationInvoiceRec::class)->findOneByInvoiceToRec($id);
  913.         $boolToRec = empty($rectf);
  914.         $allInvoices = $em->getRepository(BlvReservationInvoice::class)->findByReservationId($newInvoice->getReservationId());
  915.         $allInvoicesRec = $em->getRepository(BlvReservationInvoiceRec::class)->findByReservationId($newInvoice->getReservationId());
  916.         foreach ($allInvoicesRec as $item){ array_push($allInvoices, $item); }
  917.         $allProformas = $em->getRepository(ReservationProforma::class)->findByReservationId($newInvoice->getReservationId());
  918.         if(empty($data['type'])){$data['type'] = 'Invoice';}
  919.         $reservaEquis = $em->getRepository(Reservation::class)->findOneById($newInvoice->getReservationId());
  920.         if(empty($newInvoice->getClientName()) and empty($newInvoice->getClientAddress()) and empty($newInvoice->getClientDocument())){
  921.             $newInvoice = $this->clientDataToInvoice($newInvoice, 'Client', $reservaEquis->getClient(), $newInvoice->getReservationId());
  922.         }
  923.         return $this->render('MDS/GreenPatioBundle/blv/invoice-new-invoice.html.twig',
  924.             array(
  925.                 'id' => $newInvoice->getReservationId(),
  926.                 'cid' => '',
  927.                 'fid' => $id,
  928.                 'invoice' => $allInvoices,
  929.                 'proformas' => $allProformas,
  930.                 'boolToRec' => $boolToRec,
  931.                 'numberadmin' => '',
  932.                 'type' => $data['type'],
  933.                 'number' => $newInvoice->getId(),
  934.                 'prefix' => $data['prefix'],
  935.                 'date' => $data['date'],
  936.                 'token' =>  $data['token'],
  937.                 'reservation' => $data['reservation'],
  938.                 'company' => $data['company'],
  939.                 'clients' => $data['clients'],
  940.                 'user' => $user_logueado,
  941.                 'invoicedeposititems' => '',
  942.                 'arrayItems' => $data['arrayItems'],
  943.                 'datasupplier' => $data['datasupplier'],
  944.                 'totales_neto' => $data['totales_neto'],
  945.                 'totales_iva' => '',
  946.                 'totales' => $data['totales'],
  947.                 'bases_imponibles' => $data['bases_imponibles'],
  948.                 'balance' => $data['balance'],
  949.                 'paymentInvoice' => $data['paymentInvoice'],
  950.                 'currency' => $data['currency'],
  951.                 'clientName' => $newInvoice->getClientName(),
  952.                 'clientAddress' => $newInvoice->getClientAddress(),
  953.                 'clientDocument' => $newInvoice->getClientDocument(),
  954.             )
  955.         );
  956.     }
  957.     /**
  958.      * @Route("/blv/reservations/invoice/viewrec/{id}",  name="reservations_viewrec_invoice_blv")
  959.      * Ver la factura rectificativa
  960.      */
  961.     public function viewRecInvoiceAction($id, EntityManagerInterface $em, Request $request)
  962.     {
  963.         // Se genera y muestra en nueva pantalla la nueva proforma
  964.         $arrayBools = array( 'lounge' => null, 'service' => null, 'payment' => null, );
  965.         $arrayBools['lounge'] = $request->request->get('inv_lounge_hidden_bool');
  966.         $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  967.         $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  968.         /* Obtengo usuario logueado */
  969.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  970.         $user_id = $user_logueado->getId();
  971.         //Acumuladores de datos globales de la factura
  972.         $valorTotalNet = $valorVat = 0;
  973.         //INICIO: Creamos los items de la factura
  974.         $arrayInvoicedItems = $em->getRepository(BlvReservationInvoiceRecItems::class)->findByInvoiceId($id);
  975.         if (!empty($arrayInvoicedItems)) {
  976.             foreach ($arrayInvoicedItems as $itemInvoice) {
  977.                 if ($itemInvoice->getItemType() == 'LOUNGE') {
  978.                     $ivaNew = $itemInvoice->getLngIva();
  979.                     //Acumulamos neto e iva para la factura
  980.                     $valorTotalNet = $valorTotalNet + $itemInvoice->getLngServicePrice();
  981.                     $valorVat = $valorVat + round(($itemInvoice->getLngServicePrice() * ($ivaNew / 100)),2,PHP_ROUND_HALF_UP);
  982.                 }
  983.             }
  984.         }
  985.         if (!empty($arrayInvoicedItems)) {
  986.             foreach ($arrayInvoicedItems as $itemInvoice) {
  987.                 if ($itemInvoice->getItemType() == 'SERVICE') {
  988.                     //Acumulamos neto e iva para la factura
  989.                     if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  990.                         $subtotal = 0;
  991.                         $neto = 0;
  992.                         $subtotalService = 0;
  993.                     } else {
  994.                         $subtotal = 0;
  995.                         $subtotalService = $itemInvoice->getSrvPrice();
  996.                         $subneto = $itemInvoice->getSrvPrice();
  997.                         // Commission
  998.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  999.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  1000.                             $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  1001.                         } else {
  1002.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  1003.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  1004.                         }
  1005.                         // Over
  1006.                         if ($itemInvoice->getSrvOpOver()=='1'){
  1007.                             $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  1008.                             $subneto = $subneto + $itemInvoice->getSrvOver();
  1009.                         } else {
  1010.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  1011.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  1012.                         }
  1013.                         // IVA
  1014.                         if ($itemInvoice->getSrvOpIva()=='1'){
  1015.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  1016.                         } else {
  1017.                             $subtotalService = $itemInvoice->getSrvPrice();
  1018.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  1019.                         }
  1020.                         switch ($itemInvoice->getSrvServiceCatId()){
  1021.                             case 1: // Alojamiento
  1022.                                 // el numero de noches $numNoches; precio unitario $subneto
  1023.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  1024.                                 // La personas no afectan este calculo
  1025.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  1026.                                 $subnetoUnit = $subneto;
  1027.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  1028.                                 break;
  1029.                             case 2: //Actividades
  1030.                                 // El número de personas es considerado en el calculo
  1031.                                 $pax = $itemInvoice->getSrvPax();
  1032.                                 if (empty($pax) or $pax == "0") {
  1033.                                     $pax = 1;
  1034.                                 }
  1035.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1036.                                 if ($days > 1){
  1037.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1038.                                 } else {
  1039.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1040.                                 }
  1041.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  1042.                                 $subnetoUnit = $subneto;
  1043.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  1044.                                 break;
  1045.                             case 3: // AV
  1046.                                 $pax = $itemInvoice->getSrvPax();
  1047.                                 if (empty($pax) or $pax == "0") {
  1048.                                     $pax = 1;
  1049.                                 }
  1050.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1051.                                 if ($days > 1){
  1052.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1053.                                 } else {
  1054.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1055.                                 }
  1056.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1057.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1058.                                     $unitsServ = 1;
  1059.                                 }
  1060.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1061.                                 $subnetoUnit = $subneto;
  1062.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1063.                                 break;
  1064.                             case 4: //Creative
  1065.                                 $pax = $itemInvoice->getSrvPax();
  1066.                                 if (empty($pax) or $pax == "0") {
  1067.                                     $pax = 1;
  1068.                                 }
  1069.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1070.                                 if ($days > 1){
  1071.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1072.                                 } else {
  1073.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1074.                                 }
  1075.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1076.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1077.                                     $unitsServ = 1;
  1078.                                 }
  1079.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1080.                                 $subnetoUnit = $subneto;
  1081.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1082.                                 break;
  1083.                             case 5: //Cruise
  1084.                                 $pax = $itemInvoice->getSrvPax();
  1085.                                 if (empty($pax) or $pax == "0") {
  1086.                                     $pax = 1;
  1087.                                 }
  1088.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  1089.                                 if ($days > 1){
  1090.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1091.                                 } else {
  1092.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1093.                                 }
  1094.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1095.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1096.                                     $unitsServ = 1;
  1097.                                 }
  1098.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1099.                                 $subnetoUnit = $subneto;
  1100.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1101.                                 break;
  1102.                             case 6: //Entertaiment
  1103.                                 $pax = $itemInvoice->getSrvPax();
  1104.                                 if (empty($pax) or $pax == "0") {
  1105.                                     $pax = 1;
  1106.                                 }
  1107.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1108.                                 if ($days > 1){
  1109.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1110.                                 } else {
  1111.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1112.                                 }
  1113.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1114.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1115.                                     $unitsServ = 1;
  1116.                                 }
  1117.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1118.                                 $subnetoUnit = $subneto;
  1119.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1120.                                 break;
  1121.                             case 7: // Gifts
  1122.                                 $pax = $itemInvoice->getSrvPax();
  1123.                                 if (empty($pax) or $pax == "0") {
  1124.                                     $pax = 1;
  1125.                                 }
  1126.                                 $days = 1;
  1127.                                 if ($days > 1){
  1128.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1129.                                 } else {
  1130.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1131.                                 }
  1132.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1133.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1134.                                     $unitsServ = 1;
  1135.                                 }
  1136.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1137.                                 $subnetoUnit = $subneto;
  1138.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1139.                                 break;
  1140.                             case 8: //Guide
  1141.                                 $pax = $itemInvoice->getSrvPax();
  1142.                                 if (empty($pax) or $pax == "0") {
  1143.                                     $pax = 1;
  1144.                                 }
  1145.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1146.                                 if ($days > 1){
  1147.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1148.                                 } else {
  1149.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1150.                                 }
  1151.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1152.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1153.                                     $unitsServ = 1;
  1154.                                 }
  1155.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1156.                                 $subnetoUnit = $subneto;
  1157.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1158.                                 break;
  1159.                             case 9: //Itineraries
  1160.                                 $pax = $itemInvoice->getSrvPax();
  1161.                                 if (empty($pax) or $pax == "0") {
  1162.                                     $pax = 1;
  1163.                                 }
  1164.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1165.                                 if ($days > 1){
  1166.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1167.                                 } else {
  1168.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1169.                                 }
  1170.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1171.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1172.                                     $unitsServ = 1;
  1173.                                 }
  1174.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1175.                                 $subnetoUnit = $subneto;
  1176.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1177.                                 break;
  1178.                             case 10: //Lounge  -- No Aplica
  1179.                                 break;
  1180.                             case 11: //Menu
  1181.                                 $pax = $itemInvoice->getSrvPax();
  1182.                                 if (empty($pax) or $pax == "0") {
  1183.                                     $pax = 1;
  1184.                                 }
  1185.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1186.                                 if ($days > 1){
  1187.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1188.                                 } else {
  1189.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1190.                                 }
  1191.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1192.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1193.                                     $unitsServ = 1;
  1194.                                 }
  1195.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1196.                                 $subnetoUnit = $subneto;
  1197.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1198.                                 break;
  1199.                             case 12: //Others
  1200.                                 $pax = $itemInvoice->getSrvPax();
  1201.                                 if (empty($pax) or $pax == "0") {
  1202.                                     $pax = 1;
  1203.                                 }
  1204.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1205.                                 if ($days > 1){
  1206.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1207.                                 } else {
  1208.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1209.                                 }
  1210.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1211.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1212.                                     $unitsServ = 1;
  1213.                                 }
  1214.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1215.                                 $subnetoUnit = $subneto;
  1216.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1217.                                 break;
  1218.                             case 13: //Transport
  1219.                                 $pax = $itemInvoice->getSrvPax();
  1220.                                 if (empty($pax) or $pax == "0") {
  1221.                                     $pax = 1;
  1222.                                 }
  1223.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1224.                                 if ($days > 1){
  1225.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1226.                                 } else {
  1227.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1228.                                 }
  1229.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1230.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1231.                                     $unitsServ = 1;
  1232.                                 }
  1233.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1234.                                 $subnetoUnit = $subneto;
  1235.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1236.                                 break;
  1237.                             case 14: //Technology
  1238.                                 $pax = $itemInvoice->getSrvPax();
  1239.                                 if (empty($pax) or $pax == "0") {
  1240.                                     $pax = 1;
  1241.                                 }
  1242.                                 $days = 1;
  1243.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1244.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1245.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1246.                                     $unitsServ = 1;
  1247.                                 }
  1248.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1249.                                 $subnetoUnit = $subneto;
  1250.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1251.                                 break;
  1252.                             case 15: //Assisstant
  1253.                                 $pax = $itemInvoice->getSrvPax();
  1254.                                 if (empty($pax) or $pax == "0") {
  1255.                                     $pax = 1;
  1256.                                 }
  1257.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1258.                                 if ($days > 1){
  1259.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1260.                                 } else {
  1261.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1262.                                 }
  1263.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1264.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1265.                                     $unitsServ = 1;
  1266.                                 }
  1267.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1268.                                 $subnetoUnit = $subneto;
  1269.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1270.                                 break;
  1271.                             case 16: //DDR
  1272.                                 $pax = $itemInvoice->getSrvPax();
  1273.                                 if (empty($pax) or $pax == "0") {
  1274.                                     $pax = 1;
  1275.                                 }
  1276.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1277.                                 if ($days > 1){
  1278.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1279.                                 } else {
  1280.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1281.                                 }
  1282.                                 $unitsServ = $itemInvoice->getSrvUnits();
  1283.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1284.                                     $unitsServ = 1;
  1285.                                 }
  1286.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1287.                                 $subnetoUnit = $subneto;
  1288.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1289.                                 break;
  1290.                             default:
  1291.                                 break;
  1292.                         }
  1293.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  1294.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  1295.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  1296.                         $valorTotalNet = $valorTotalNet + $neto;
  1297.                         $valorVat = $valorVat + ($subtotal - $neto);
  1298.                     }
  1299.                 }
  1300.             }
  1301.         }
  1302.         if (!empty($arrayInvoicedItems)) {
  1303.             foreach ($arrayInvoicedItems as $itemInvoice) {
  1304.                 if ($itemInvoice->getItemType() == 'PAYMENT') {
  1305.                     //Acumulamos neto e iva para la factura
  1306.                     //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  1307.                 }
  1308.             }
  1309.         }
  1310.         //FIN: Creamos los items de la factura
  1311.         $valorTotal = $valorTotalNet + $valorVat;
  1312.         //INICIO: Creamos la factura
  1313.         $newInvoice = $em->getRepository(BlvReservationInvoiceRec::class)->findOneById($id);
  1314.         //FIN: Creamos la factura
  1315.         $data = [];
  1316.         $data = $this->baseInvoiceDoneReservation($newInvoice->getReservationId(), $id,'R');
  1317.         $paymentsClient = new ReservationPaymentsClient();
  1318.         $paymentsClient->setReservationId($id);
  1319.         $reservaInv = new BlvReservationInvoice();
  1320.         $reservaInv->setReservationId($id);
  1321.         $idLounges = array();
  1322.         $loungesBoolToInvoice = array();
  1323.         if (array_key_exists('lounge',$data['datasupplier'])) {
  1324.             foreach ($data['datasupplier']['lounge'] as $item) {
  1325.                 $idLounges[] = $item['id'];
  1326.                 $loungesBoolToInvoice[] = 1;
  1327.             }
  1328.             $idLounges = implode(',', $idLounges);
  1329.             $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  1330.         } else {
  1331.             $idLounges = '0';
  1332.             $loungesBoolToInvoice = '0';
  1333.         }
  1334.         $idPayments = [];
  1335.         $paymentsBoolToInvoice = [];
  1336.         if (array_key_exists('payment',$data['datasupplier'])) {
  1337.             foreach ($data['datasupplier']['payment'] as $item) {
  1338.                 $idPayments[] = $item['id'];
  1339.                 $paymentsBoolToInvoice[] = 1;
  1340.             }
  1341.             $idPayments = implode(',', $idPayments);
  1342.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  1343.         } else {
  1344.             $idPayments = '0';
  1345.             $paymentsBoolToInvoice = '0';
  1346.         }
  1347.         // Una factura solo se puede rectificar una vez
  1348.         $boolToRec = false; // Estas viendo una factura rectificativa
  1349.         $allInvoices = $em->getRepository(BlvReservationInvoice::class)->findByReservationId($newInvoice->getReservationId());
  1350.         $allInvoicesRec = $em->getRepository(BlvReservationInvoiceRec::class)->findByReservationId($newInvoice->getReservationId());
  1351.         foreach ($allInvoicesRec as $item){ array_push($allInvoices, $item); }
  1352.         $allProformas = $em->getRepository(ReservationProforma::class)->findByReservationId($newInvoice->getReservationId());
  1353.         // Por ser una factura rectificativa multiplicamos por -1 el balance
  1354.         if(empty($data['type'])){$data['type'] = 'Invoice Rec';}
  1355.         $reservaEquis = $em->getRepository(Reservation::class)->findOneById($newInvoice->getReservationId());
  1356.         if(empty($newInvoice->getClientName()) and empty($newInvoice->getClientAddress()) and empty($newInvoice->getClientDocument())){
  1357.             $newInvoice = $this->clientDataToInvoice($newInvoice, 'Client', $reservaEquis->getClient(), $newInvoice->getReservationId());
  1358.         }
  1359.         return $this->render('MDS/GreenPatioBundle/blv/invoice-new-invoice.html.twig',
  1360.             array(
  1361.                 'id' => $newInvoice->getReservationId(),
  1362.                 'invoiceIdToRec' => $newInvoice->getInvoiceToRec(),
  1363.                 'cid' => '',
  1364.                 'fid' => $id,
  1365.                 'invoice' => $allInvoices,
  1366.                 'proformas' => $allProformas,
  1367.                 'boolToRec' => $boolToRec,
  1368.                 'numberadmin' => '',
  1369.                 'type' => $data['type'],
  1370.                 'number' => $newInvoice->getId(),
  1371.                 'prefix' => $data['prefix'],
  1372.                 'date' => $data['date'],
  1373.                 'token' =>  $data['token'],
  1374.                 'reservation' => $data['reservation'],
  1375.                 'company' => $data['company'],
  1376.                 'clients' => $data['clients'],
  1377.                 'user' => $user_logueado,
  1378.                 'invoicedeposititems' => '',
  1379.                 'arrayItems' => $data['arrayItems'],
  1380.                 'datasupplier' => $data['datasupplier'],
  1381.                 'totales_neto' => $data['totales_neto'],
  1382.                 'totales_iva' => '',
  1383.                 'totales' => $data['totales'],
  1384.                 'bases_imponibles' => $data['bases_imponibles'],
  1385.                 'balance' => $data['balance'],
  1386.                 'paymentInvoice' => $data['paymentInvoice'],
  1387.                 'currency' => $data['currency'],
  1388.                 'clientName' => $newInvoice->getClientName(),
  1389.                 'clientAddress' => $newInvoice->getClientAddress(),
  1390.                 'clientDocument' => $newInvoice->getClientDocument(),
  1391.             )
  1392.         );
  1393.     }
  1394.     /**
  1395.      * @Route("/blv/reservations/invoice/invoice-proforma/print/{type}/{id}",  name="reservations_invoiceorproforma_print_blv")
  1396.      */
  1397.     public function detailsInvoiceProformaPrintAction($type, $id, EntityManagerInterface $em, Request $request)
  1398.     {
  1399.         $data = [];
  1400.         // Si el ID de factura viene vacio regresamos a ver facturas
  1401.         if (empty($type)){ return $this->redirectToRoute('reservations_viewreservation_invoices',array('id' => $id)); }
  1402.         //Acumuladores de datos globales de la factura o proforma
  1403.         $valorTotalNet = 0;
  1404.         $valorVat = 0;
  1405.         if ($type == 'I'){
  1406.             //Es una factura
  1407.             //INICIO: Creamos los items de la factura
  1408.             $arrayInvoicedItems = $em->getRepository(BlvReservationInvoiceItems::class)->findByInvoiceId($id);
  1409.             if (!empty($arrayInvoicedItems)) {
  1410.                 foreach ($arrayInvoicedItems as $itemInvoice) {
  1411.                     if ($itemInvoice->getItemType() == 'LOUNGE') {
  1412.                         //Acumulamos neto e iva para la factura
  1413.                         $valorTotalNet = $valorTotalNet + $itemInvoice->getLngServicePrice();
  1414.                         $valorVat = $valorVat + round(($itemInvoice->getLngServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  1415.                     }
  1416.                 }
  1417.             }
  1418.             if (!empty($arrayInvoicedItems)) {
  1419.                 foreach ($arrayInvoicedItems as $itemInvoice) {
  1420.                     if ($itemInvoice->getItemType() == 'SERVICE') {
  1421.                         //Acumulamos neto e iva para la factura
  1422.                         if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  1423.                             $subtotal = 0;
  1424.                             $neto = 0;
  1425.                             $subtotalService = 0;
  1426.                         } else {
  1427.                             $subtotalService = $itemInvoice->getSrvPrice();
  1428.                             $subtotalService = str_replace(',','.',$subtotalService);
  1429.                             $subtotalService = floatval($subtotalService);
  1430.                             $subneto = $itemInvoice->getSrvPrice();
  1431.                             $subneto = str_replace(',','.',$subneto);
  1432.                             $subneto = floatval($subneto);
  1433.                             // Commission
  1434.                             if ($itemInvoice->getSrvOpCommission()=='1'){
  1435.                                 $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  1436.                                 $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  1437.                             } else {
  1438.                                 $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  1439.                                 $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  1440.                             }
  1441.                             // Over
  1442.                             if ($itemInvoice->getSrvOpOver()=='1'){
  1443.                                 $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  1444.                                 $subneto = $subneto + $itemInvoice->getSrvOver();
  1445.                             } else {
  1446.                                 $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  1447.                                 $subneto = $subneto - $itemInvoice->getSrvOver();
  1448.                             }
  1449.                             // IVA
  1450.                             if ($itemInvoice->getSrvOpIva()=='1'){
  1451.                                 $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  1452.                             } else {
  1453.                                 $subtotalService = $itemInvoice->getSrvPrice();
  1454.                                 $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  1455.                             }
  1456.                             switch ($itemInvoice->getSrvServiceCatId()){
  1457.                                 case 1: // Alojamiento
  1458.                                     // el numero de noches $numNoches; precio unitario $subneto
  1459.                                     $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  1460.                                     // La personas no afectan este calculo
  1461.                                     $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  1462.                                     $subnetoUnit = $subneto;
  1463.                                     $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  1464.                                     break;
  1465.                                 case 2: //Actividades
  1466.                                     // El número de personas es considerado en el calculo
  1467.                                     $pax = $itemInvoice->getSrvPax();
  1468.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1469.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1470.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1471.                                     $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  1472.                                     $subnetoUnit = $subneto;
  1473.                                     $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  1474.                                     break;
  1475.                                 case 3: // AV
  1476.                                     $pax = $itemInvoice->getSrvPax();
  1477.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1478.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1479.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1480.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1481.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1482.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1483.                                     $subnetoUnit = $subneto;
  1484.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1485.                                     break;
  1486.                                 case 4: //Creative
  1487.                                     $pax = $itemInvoice->getSrvPax();
  1488.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1489.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1490.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1491.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1492.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1493.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1494.                                     $subnetoUnit = $subneto;
  1495.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1496.                                     break;
  1497.                                 case 5: //Cruise
  1498.                                     $pax = $itemInvoice->getSrvPax();
  1499.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1500.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  1501.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1502.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1503.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1504.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1505.                                     $subnetoUnit = $subneto;
  1506.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1507.                                     break;
  1508.                                 case 6: //Entertaiment
  1509.                                     $pax = $itemInvoice->getSrvPax();
  1510.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1511.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1512.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1513.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1514.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1515.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1516.                                     $subnetoUnit = $subneto;
  1517.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1518.                                     break;
  1519.                                 case 7: // Gifts
  1520.                                     $pax = $itemInvoice->getSrvPax();
  1521.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1522.                                     $days = 1;
  1523.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1524.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1525.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1526.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1527.                                     $subnetoUnit = $subneto;
  1528.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1529.                                     break;
  1530.                                 case 8: //Guide
  1531.                                     $pax = $itemInvoice->getSrvPax();
  1532.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1533.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1534.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1535.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1536.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1537.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1538.                                     $subnetoUnit = $subneto;
  1539.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1540.                                     break;
  1541.                                 case 9: //Itineraries
  1542.                                     $pax = $itemInvoice->getSrvPax();
  1543.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1544.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1545.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1546.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1547.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1548.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1549.                                     $subnetoUnit = $subneto;
  1550.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1551.                                     break;
  1552.                                 case 10: //Lounge  -- No Aplica
  1553.                                     break;
  1554.                                 case 11: //Menu (Catering)
  1555.                                     $pax = $itemInvoice->getSrvPax();
  1556.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1557.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1558.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1559.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1560.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1561.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1562.                                     $subnetoUnit = $subneto;
  1563.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1564.                                     break;
  1565.                                 case 12: //Others
  1566.                                     $pax = $itemInvoice->getSrvPax();
  1567.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1568.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1569.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1570.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1571.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1572.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1573.                                     $subnetoUnit = $subneto;
  1574.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1575.                                     break;
  1576.                                 case 13: //Transport
  1577.                                     $pax = $itemInvoice->getSrvPax();
  1578.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1579.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1580.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1581.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1582.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1583.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1584.                                     $subnetoUnit = $subneto;
  1585.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1586.                                     break;
  1587.                                 case 14: //Technology
  1588.                                     $pax = $itemInvoice->getSrvPax();
  1589.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1590.                                     $days = 1;
  1591.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1592.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1593.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1594.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1595.                                     $subnetoUnit = $subneto;
  1596.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1597.                                     break;
  1598.                                 case 15: //Assisstant
  1599.                                     $pax = $itemInvoice->getSrvPax();
  1600.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1601.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1602.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1603.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1604.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1605.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1606.                                     $subnetoUnit = $subneto;
  1607.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1608.                                     break;
  1609.                                 case 16: //DDR
  1610.                                     $pax = $itemInvoice->getSrvPax();
  1611.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1612.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1613.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1614.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1615.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1616.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1617.                                     $subnetoUnit = $subneto;
  1618.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1619.                                     break;
  1620.                                 case 17: //Seguridad
  1621.                                     $pax = $itemInvoice->getSrvPax();
  1622.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1623.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1624.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1625.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1626.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1627.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1628.                                     $subnetoUnit = $subneto;
  1629.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1630.                                     break;
  1631.                                 case 18: //WiFi
  1632.                                     $pax = $itemInvoice->getSrvPax();
  1633.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1634.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1635.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1636.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1637.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1638.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1639.                                     $subnetoUnit = $subneto;
  1640.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1641.                                     break;
  1642.                                 case 19: //Mobiliario
  1643.                                     $pax = $itemInvoice->getSrvPax();
  1644.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1645.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1646.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1647.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1648.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1649.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1650.                                     $subnetoUnit = $subneto;
  1651.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1652.                                     break;
  1653.                                 case 20: //Parking
  1654.                                     $pax = $itemInvoice->getSrvPax();
  1655.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1656.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1657.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1658.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1659.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1660.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1661.                                     $subnetoUnit = $subneto;
  1662.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1663.                                     break;
  1664.                                 case 21: //Limpieza
  1665.                                     $pax = $itemInvoice->getSrvPax();
  1666.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1667.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1668.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1669.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1670.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1671.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1672.                                     $subnetoUnit = $subneto;
  1673.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1674.                                     break;
  1675.                                 default: //Others (por default)
  1676.                                     $pax = $itemInvoice->getSrvPax();
  1677.                                     if (empty($pax) or $pax == "0") { $pax = 1; }
  1678.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1679.                                     $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1680.                                     $unitsServ = $itemInvoice->getSrvUnits();
  1681.                                     if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1682.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1683.                                     $subnetoUnit = $subneto;
  1684.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  1685.                                     break;
  1686.                             }
  1687.                             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  1688.                             $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  1689.                             $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  1690.                             $valorTotalNet = $valorTotalNet + $neto;
  1691.                             $valorVat = $valorVat + ($subtotal - $neto);
  1692.                         }
  1693.                     }
  1694.                 }
  1695.             }
  1696.             if (!empty($arrayInvoicedItems)) {
  1697.                 foreach ($arrayInvoicedItems as $itemInvoice) {
  1698.                     if ($itemInvoice->getItemType() == 'PAYMENT') {
  1699.                         //Acumulamos neto e iva para la factura
  1700.                         //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  1701.                     }
  1702.                 }
  1703.             }
  1704.             //FIN: Creamos los items de la factura
  1705.             $valorTotal = $valorTotalNet + $valorVat;
  1706.             //INICIO: Creamos la factura
  1707.             $newInvoice = $em->getRepository(BlvReservationInvoice::class)->findOneById($id);
  1708.             //FIN: Creamos la factura
  1709.             $data = [];
  1710.             $data = $this->baseInvoiceDoneReservation($newInvoice->getReservationId(), $id,'I');
  1711.             $paymentsClient = new ReservationPaymentsClient();
  1712.             $paymentsClient->setReservationId($id);
  1713.             $reservaInv = new BlvReservationInvoice();
  1714.             $reservaInv->setReservationId($id);
  1715.             $idLounges = array();
  1716.             $loungesBoolToInvoice = array();
  1717.             if (array_key_exists('lounge',$data['datasupplier'])) {
  1718.                 foreach ($data['datasupplier']['lounge'] as $item) {
  1719.                     $idLounges[] = $item['id'];
  1720.                     $loungesBoolToInvoice[] = 1;
  1721.                 }
  1722.                 $idLounges = implode(',', $idLounges);
  1723.                 $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  1724.             } else {
  1725.                 $idLounges = '0';
  1726.                 $loungesBoolToInvoice = '0';
  1727.             }
  1728.             $idPayments = array();
  1729.             $paymentsBoolToInvoice = array();
  1730.             if (array_key_exists('payment',$data['datasupplier'])) {
  1731.                 foreach ($data['datasupplier']['payment'] as $item) {
  1732.                     $idPayments[] = $item['id'];
  1733.                     $paymentsBoolToInvoice[] = 1;
  1734.                 }
  1735.                 $idPayments = implode(',', $idPayments);
  1736.                 $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  1737.             } else {
  1738.                 $idPayments = '0';
  1739.                 $paymentsBoolToInvoice = '0';
  1740.             }
  1741.             //Es una proforma
  1742.             $proforma = $em->getRepository(ReservationProforma::class)->findOneByReservationId($id);
  1743.             $invoiceIdToRec = null;
  1744.             $reservaEquis = $em->getRepository(Reservation::class)->findOneById($newInvoice->getReservationId());
  1745.             if(empty($newInvoice->getClientName()) and empty($newInvoice->getClientAddress()) and empty($newInvoice->getClientDocument())){
  1746.                 $newInvoice = $this->clientDataToInvoice($newInvoice, 'Client', $reservaEquis->getClient(), $newInvoice->getReservationId());
  1747.             }
  1748.         } else {
  1749.             if ($type == 'R'){
  1750.                 //El tipo es una factura rectificativa
  1751.                 //INICIO: Creamos los items de la factura rectficativa
  1752.                 $arrayInvoicedItems = $em->getRepository(BlvReservationInvoiceRecItems::class)->findByInvoiceRecId($id);
  1753.                 if (!empty($arrayInvoicedItems)) {
  1754.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  1755.                         if ($itemInvoice->getItemType() == 'LOUNGE') {
  1756.                             //Acumulamos neto e iva para la factura rectificativa
  1757.                             $valorTotalNet = $valorTotalNet + $itemInvoice->getLngServicePrice();
  1758.                             $valorVat = $valorVat + round(($itemInvoice->getLngServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  1759.                         }
  1760.                     }
  1761.                 }
  1762.                 if (!empty($arrayInvoicedItems)) {
  1763.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  1764.                         if ($itemInvoice->getItemType() == 'SERVICE') {
  1765.                             //Acumulamos neto e iva para la factura
  1766.                             if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  1767.                                 $subtotal = 0;
  1768.                                 $neto = 0;
  1769.                                 $subtotalService = 0;
  1770.                             } else {
  1771.                                 $subtotalService = $itemInvoice->getSrvPrice();
  1772.                                 $subneto = $itemInvoice->getSrvPrice();
  1773.                                 // Commission
  1774.                                 if ($itemInvoice->getSrvOpCommission()=='1'){
  1775.                                     $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  1776.                                     $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  1777.                                 } else {
  1778.                                     $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  1779.                                     $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  1780.                                 }
  1781.                                 // Over
  1782.                                 if ($itemInvoice->getSrvOpOver()=='1'){
  1783.                                     $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  1784.                                     $subneto = $subneto + $itemInvoice->getSrvOver();
  1785.                                 } else {
  1786.                                     $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  1787.                                     $subneto = $subneto - $itemInvoice->getSrvOver();
  1788.                                 }
  1789.                                 // IVA
  1790.                                 if ($itemInvoice->getSrvOpIva()=='1'){
  1791.                                     $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  1792.                                 } else {
  1793.                                     $subtotalService = $itemInvoice->getSrvPrice();
  1794.                                     $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  1795.                                 }
  1796.                                 switch ($itemInvoice->getSrvServiceCatId()){
  1797.                                     case 1: // Alojamiento
  1798.                                         // el numero de noches $numNoches; precio unitario $subneto
  1799.                                         $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  1800.                                         // La personas no afectan este calculo
  1801.                                         $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  1802.                                         $subnetoUnit = $subneto;
  1803.                                         $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  1804.                                         break;
  1805.                                     case 2: //Actividades
  1806.                                         // El número de personas es considerado en el calculo
  1807.                                         $pax = $itemInvoice->getSrvPax();
  1808.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1809.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1810.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1811.                                         $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  1812.                                         $subnetoUnit = $subneto;
  1813.                                         $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  1814.                                         break;
  1815.                                     case 3: // AV
  1816.                                         $pax = $itemInvoice->getSrvPax();
  1817.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1818.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1819.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1820.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1821.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1822.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1823.                                         $subnetoUnit = $subneto;
  1824.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1825.                                         break;
  1826.                                     case 4: //Creative
  1827.                                         $pax = $itemInvoice->getSrvPax();
  1828.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1829.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1830.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1831.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1832.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1833.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1834.                                         $subnetoUnit = $subneto;
  1835.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1836.                                         break;
  1837.                                     case 5: //Cruise
  1838.                                         $pax = $itemInvoice->getSrvPax();
  1839.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1840.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  1841.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1842.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1843.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1844.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1845.                                         $subnetoUnit = $subneto;
  1846.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1847.                                         break;
  1848.                                     case 6: //Entertaiment
  1849.                                         $pax = $itemInvoice->getSrvPax();
  1850.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1851.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1852.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1853.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1854.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1855.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1856.                                         $subnetoUnit = $subneto;
  1857.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1858.                                         break;
  1859.                                     case 7: // Gifts
  1860.                                         $pax = $itemInvoice->getSrvPax();
  1861.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1862.                                         $days = 1;
  1863.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1864.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1865.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1866.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1867.                                         $subnetoUnit = $subneto;
  1868.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1869.                                         break;
  1870.                                     case 8: //Guide
  1871.                                         $pax = $itemInvoice->getSrvPax();
  1872.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1873.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1874.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1875.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1876.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1877.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1878.                                         $subnetoUnit = $subneto;
  1879.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1880.                                         break;
  1881.                                     case 9: //Itineraries
  1882.                                         $pax = $itemInvoice->getSrvPax();
  1883.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1884.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1885.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1886.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1887.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1888.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1889.                                         $subnetoUnit = $subneto;
  1890.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1891.                                         break;
  1892.                                     case 10: //Lounge  -- No Aplica
  1893.                                         break;
  1894.                                     case 11: //Catering
  1895.                                         $pax = $itemInvoice->getSrvPax();
  1896.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1897.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1898.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1899.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1900.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1901.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1902.                                         $subnetoUnit = $subneto;
  1903.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1904.                                         break;
  1905.                                     case 12: //Others
  1906.                                         $pax = $itemInvoice->getSrvPax();
  1907.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1908.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1909.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1910.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1911.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1912.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1913.                                         $subnetoUnit = $subneto;
  1914.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1915.                                         break;
  1916.                                     case 13: //Transport
  1917.                                         $pax = $itemInvoice->getSrvPax();
  1918.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1919.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1920.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1921.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1922.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1923.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1924.                                         $subnetoUnit = $subneto;
  1925.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1926.                                         break;
  1927.                                     case 14: //Technology
  1928.                                         $pax = $itemInvoice->getSrvPax();
  1929.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1930.                                         $days = 1;
  1931.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  1932.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1933.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1934.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1935.                                         $subnetoUnit = $subneto;
  1936.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1937.                                         break;
  1938.                                     case 15: //Assisstant
  1939.                                         $pax = $itemInvoice->getSrvPax();
  1940.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1941.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1942.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1943.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1944.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1945.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1946.                                         $subnetoUnit = $subneto;
  1947.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1948.                                         break;
  1949.                                     case 16: //DDR
  1950.                                         $pax = $itemInvoice->getSrvPax();
  1951.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1952.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1953.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1954.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1955.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1956.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1957.                                         $subnetoUnit = $subneto;
  1958.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1959.                                         break;
  1960.                                     case 17: //Seguridad
  1961.                                         $pax = $itemInvoice->getSrvPax();
  1962.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1963.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1964.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1965.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1966.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1967.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1968.                                         $subnetoUnit = $subneto;
  1969.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1970.                                         break;
  1971.                                     case 18: //WiFi
  1972.                                         $pax = $itemInvoice->getSrvPax();
  1973.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1974.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1975.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1976.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1977.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1978.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1979.                                         $subnetoUnit = $subneto;
  1980.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1981.                                         break;
  1982.                                     case 19: //Mobiliario
  1983.                                         $pax = $itemInvoice->getSrvPax();
  1984.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1985.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1986.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1987.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1988.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  1989.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1990.                                         $subnetoUnit = $subneto;
  1991.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  1992.                                         break;
  1993.                                     case 20: //Parking
  1994.                                         $pax = $itemInvoice->getSrvPax();
  1995.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  1996.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  1997.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  1998.                                         $unitsServ = $itemInvoice->getSrvUnits();
  1999.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2000.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2001.                                         $subnetoUnit = $subneto;
  2002.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2003.                                         break;
  2004.                                     case 21: //Limpieza
  2005.                                         $pax = $itemInvoice->getSrvPax();
  2006.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2007.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2008.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2009.                                         $unitsServ = $itemInvoice->getSrvUnits();
  2010.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2011.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2012.                                         $subnetoUnit = $subneto;
  2013.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2014.                                         break;
  2015.                                     default: //Others (por default)
  2016.                                         $pax = $itemInvoice->getSrvPax();
  2017.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2018.                                         $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2019.                                         $dateServ = ($days > 1) ? ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y') : ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2020.                                         $unitsServ = $itemInvoice->getSrvUnits();
  2021.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2022.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2023.                                         $subnetoUnit = $subneto;
  2024.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2025.                                         break;
  2026.                                 }
  2027.                                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  2028.                                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  2029.                                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  2030.                                 $valorTotalNet = $valorTotalNet + $neto;
  2031.                                 $valorVat = $valorVat + ($subtotal - $neto);
  2032.                             }
  2033.                         }
  2034.                     }
  2035.                 }
  2036.                 if (!empty($arrayInvoicedItems)) {
  2037.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  2038.                         if ($itemInvoice->getItemType() == 'PAYMENT') {
  2039.                             //Acumulamos neto e iva para la factura
  2040.                             //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  2041.                         }
  2042.                     }
  2043.                 }
  2044.                 //FIN: Creamos los items de la factura
  2045.                 $valorTotal = $valorTotalNet + $valorVat;
  2046.                 //INICIO: Creamos la factura rectificativa
  2047.                 $newInvoice = $em->getRepository(BlvReservationInvoiceRec::class)->findOneById($id);
  2048.                 //FIN: Creamos la factura rectificativa
  2049.                 $data = array();
  2050.                 $data = $this->baseInvoiceDoneReservation($newInvoice->getReservationId(), $id,'R');
  2051.                 $paymentsClient = new ReservationPaymentsClient();
  2052.                 $paymentsClient->setReservationId($id);
  2053.                 $reservaInv = new BlvReservationInvoice();
  2054.                 $reservaInv->setReservationId($id);
  2055.                 $idLounges = array();
  2056.                 $loungesBoolToInvoice = array();
  2057.                 if (array_key_exists('lounge',$data['datasupplier'])) {
  2058.                     foreach ($data['datasupplier']['lounge'] as $item) {
  2059.                         $idLounges[] = $item['id'];
  2060.                         $loungesBoolToInvoice[] = 1;
  2061.                     }
  2062.                     $idLounges = implode(',', $idLounges);
  2063.                     $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  2064.                 } else {
  2065.                     $idLounges = '0';
  2066.                     $loungesBoolToInvoice = '0';
  2067.                 }
  2068.                 $idPayments = array();
  2069.                 $paymentsBoolToInvoice = array();
  2070.                 if (array_key_exists('payment',$data['datasupplier'])) {
  2071.                     foreach ($data['datasupplier']['payment'] as $item) {
  2072.                         $idPayments[] = $item['id'];
  2073.                         $paymentsBoolToInvoice[] = 1;
  2074.                     }
  2075.                     $idPayments = implode(',', $idPayments);
  2076.                     $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  2077.                     // Se han generado dos arreglos, cada uno dividido en 2 strings
  2078.                     // array( [5]=>0, [8]=>1, [10]=>0)  ==>> '5,8,10' y '0,1,0'
  2079.                     // Para manipularlos entre la vista y el JS
  2080.                 } else {
  2081.                     $idPayments = '0';
  2082.                     $paymentsBoolToInvoice = '0';
  2083.                 }
  2084.                 //Es una proforma
  2085.                 $proforma = $em->getRepository(ReservationProforma::class)->findOneByReservationId($id);
  2086.                 $invoiceIdToRec = $newInvoice->getInvoiceToRec();
  2087.                 $data['number'] = $data['number']->getNumber();
  2088.                 $data['balance'] = $data['balance'] *(-1);
  2089.                 $reservaEquis = $em->getRepository(Reservation::class)->findOneById($newInvoice->getReservationId());
  2090.                 if(empty($newInvoice->getClientName()) and empty($newInvoice->getClientAddress()) and empty($newInvoice->getClientDocument())){
  2091.                     $newInvoice = $this->clientDataToInvoice($newInvoice, 'Client', $reservaEquis->getClient(), $newInvoice->getReservationId());
  2092.                 }
  2093.             } else {
  2094.                 // El tipo es una proforma
  2095.                 //INICIO: Creamos los items de la proforma
  2096.                 $arrayInvoicedItems = $em->getRepository(ReservationInvoiceProformaItems::class)->findByProformaId($id);
  2097.                 if (!empty($arrayInvoicedItems)) {
  2098.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  2099.                         if ($itemInvoice->getType() == 'LOUNGE') {
  2100.                             $itemPro = $em->getRepository(ReservationLoungeSimple::class)->findOneById($itemInvoice->getControlId());
  2101.                             //Acumulamos neto e iva para la factura
  2102.                             $valorTotalNet = $valorTotalNet + $itemPro->getServicePrice();
  2103.                             $valorVat = $valorVat + round(($itemPro->getServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  2104.                         }
  2105.                     }
  2106.                 }
  2107.                 if (!empty($arrayInvoicedItems)) {
  2108.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  2109.                         if ($itemInvoice->getType() == 'SERVICE') {
  2110.                             $itemPro = $em->getRepository(ReservationService::class)->findOneById($itemInvoice->getControlId());
  2111.                             //Acumulamos neto e iva para la factura
  2112.                             if (is_null($itemPro->getPrice()) or empty($itemPro->getPrice())){
  2113.                                 $subtotal = 0;
  2114.                                 $neto = 0;
  2115.                                 $subtotalService = 0;
  2116.                             } else {
  2117.                                 $subtotalService = $itemPro->getPrice();
  2118.                                 $subneto = $itemPro->getPrice();
  2119.                                 // Commission
  2120.                                 if ($itemPro->getOpCommission()=='1'){
  2121.                                     $subtotalService = $subtotalService * (1 + ($itemPro->getCommission()/100));
  2122.                                     $subneto = $subneto  * (1 + ($itemPro->getCommission()/100));
  2123.                                 } else {
  2124.                                     $subtotalService = $subtotalService * (1 - ($itemPro->getCommission()/100));
  2125.                                     $subneto = $subneto * (1 - ($itemPro->getCommission()/100));
  2126.                                 }
  2127.                                 // Over
  2128.                                 if ($itemPro->getOpOver()=='1'){
  2129.                                     $subtotalService = $subtotalService + $itemPro->getOver();
  2130.                                     $subneto = $subneto + $itemPro->getOver();
  2131.                                 } else {
  2132.                                     $subtotalService = $subtotalService - $itemPro->getOver();
  2133.                                     $subneto = $subneto - $itemPro->getOver();
  2134.                                 }
  2135.                                 // IVA
  2136.                                 if ($itemPro->getOpIva()=='1'){
  2137.                                     $subtotalService = $subtotalService * (1 + ($itemPro->getIva()/100));
  2138.                                 } else {
  2139.                                     $subtotalService = $itemPro->getPrice();
  2140.                                     $subneto = ($subneto * 100) / (100 + $itemPro->getIva());
  2141.                                 }
  2142.                                 switch ($itemPro->getServiceCatId()){
  2143.                                     case 1: // Alojamiento
  2144.                                         // el numero de noches $numNoches; precio unitario $subneto
  2145.                                         $numNoches = (($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days;
  2146.                                         // La personas no afectan este calculo
  2147.                                         $subtotal = $subtotalService * $numNoches * $itemPro->getUnits();
  2148.                                         $subnetoUnit = $subneto;
  2149.                                         $subneto = $subneto * $numNoches * $itemPro->getUnits();
  2150.                                         break;
  2151.                                     case 2: //Actividades
  2152.                                         // El número de personas es considerado en el calculo
  2153.                                         $pax = $itemPro->getPax();
  2154.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2155.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2156.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2157.                                         $subtotal = $subtotalService * $days * $itemPro->getUnits();
  2158.                                         $subnetoUnit = $subneto;
  2159.                                         $subneto = $subneto * $days * $itemPro->getUnits();
  2160.                                         break;
  2161.                                     case 3: // AV
  2162.                                         $pax = $itemPro->getPax();
  2163.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2164.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2165.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2166.                                         $unitsServ = $itemPro->getUnits();
  2167.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2168.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2169.                                         $subnetoUnit = $subneto;
  2170.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2171.                                         break;
  2172.                                     case 4: //Creative
  2173.                                         $pax = $itemPro->getPax();
  2174.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2175.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2176.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2177.                                         $unitsServ = $itemPro->getUnits();
  2178.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2179.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2180.                                         $subnetoUnit = $subneto;
  2181.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2182.                                         break;
  2183.                                     case 5: //Cruise
  2184.                                         $pax = $itemPro->getPax();
  2185.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2186.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days);
  2187.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2188.                                         $unitsServ = $itemPro->getUnits();
  2189.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2190.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2191.                                         $subnetoUnit = $subneto;
  2192.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2193.                                         break;
  2194.                                     case 6: //Entertaiment
  2195.                                         $pax = $itemPro->getPax();
  2196.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2197.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2198.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2199.                                         $unitsServ = $itemPro->getUnits();
  2200.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2201.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2202.                                         $subnetoUnit = $subneto;
  2203.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2204.                                         break;
  2205.                                     case 7: // Gifts
  2206.                                         $pax = $itemPro->getPax();
  2207.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2208.                                         $days = 1;
  2209.                                         $dateServ = ($itemPro->getDateInAt())->format('d/m/Y');
  2210.                                         $unitsServ = $itemPro->getUnits();
  2211.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2212.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2213.                                         $subnetoUnit = $subneto;
  2214.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2215.                                         break;
  2216.                                     case 8: //Guide
  2217.                                         $pax = $itemPro->getPax();
  2218.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2219.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2220.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2221.                                         $unitsServ = $itemPro->getUnits();
  2222.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2223.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2224.                                         $subnetoUnit = $subneto;
  2225.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2226.                                         break;
  2227.                                     case 9: //Itineraries
  2228.                                         $pax = $itemPro->getPax();
  2229.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2230.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2231.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2232.                                         $unitsServ = $itemPro->getUnits();
  2233.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2234.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2235.                                         $subnetoUnit = $subneto;
  2236.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2237.                                         break;
  2238.                                     case 10: //Lounge  -- No Aplica
  2239.                                         break;
  2240.                                     case 11: //Menu
  2241.                                         $pax = $itemPro->getPax();
  2242.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2243.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2244.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2245.                                         $unitsServ = $itemPro->getUnits();
  2246.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2247.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2248.                                         $subnetoUnit = $subneto;
  2249.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2250.                                         break;
  2251.                                     case 12: //Others
  2252.                                         $pax = $itemPro->getPax();
  2253.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2254.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2255.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2256.                                         $unitsServ = $itemPro->getUnits();
  2257.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2258.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2259.                                         $subnetoUnit = $subneto;
  2260.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2261.                                         break;
  2262.                                     case 13: //Transport
  2263.                                         $pax = $itemPro->getPax();
  2264.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2265.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2266.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2267.                                         $unitsServ = $itemPro->getUnits();
  2268.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2269.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2270.                                         $subnetoUnit = $subneto;
  2271.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2272.                                         break;
  2273.                                     case 14: //Technology
  2274.                                         $pax = $itemPro->getPax();
  2275.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2276.                                         $days = 1;
  2277.                                         $dateServ = ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y');
  2278.                                         $unitsServ = $itemPro->getUnits();
  2279.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2280.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2281.                                         $subnetoUnit = $subneto;
  2282.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2283.                                         break;
  2284.                                     case 15: //Assisstant
  2285.                                         $pax = $itemPro->getPax();
  2286.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2287.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2288.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2289.                                         $unitsServ = $itemPro->getUnits();
  2290.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2291.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2292.                                         $subnetoUnit = $subneto;
  2293.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2294.                                         break;
  2295.                                     case 16: //DDR
  2296.                                         $pax = $itemPro->getPax();
  2297.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2298.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2299.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2300.                                         $unitsServ = $itemPro->getUnits();
  2301.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2302.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2303.                                         $subnetoUnit = $subneto;
  2304.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2305.                                         break;
  2306.                                     case 17: //Seguridad
  2307.                                         $pax = $itemPro->getPax();
  2308.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2309.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2310.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2311.                                         $unitsServ = $itemPro->getUnits();
  2312.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2313.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2314.                                         $subnetoUnit = $subneto;
  2315.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2316.                                         break;
  2317.                                     case 18: //WiFi
  2318.                                         $pax = $itemPro->getPax();
  2319.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2320.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2321.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2322.                                         $unitsServ = $itemPro->getUnits();
  2323.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2324.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2325.                                         $subnetoUnit = $subneto;
  2326.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2327.                                         break;
  2328.                                     case 19: //Mobiliario
  2329.                                         $pax = $itemPro->getPax();
  2330.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2331.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2332.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2333.                                         $unitsServ = $itemPro->getUnits();
  2334.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2335.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2336.                                         $subnetoUnit = $subneto;
  2337.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2338.                                         break;
  2339.                                     case 20: //Parking
  2340.                                         $pax = $itemPro->getPax();
  2341.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2342.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2343.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2344.                                         $unitsServ = $itemPro->getUnits();
  2345.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2346.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2347.                                         $subnetoUnit = $subneto;
  2348.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2349.                                         break;
  2350.                                     case 21: //Limpieza
  2351.                                         $pax = $itemPro->getPax();
  2352.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2353.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2354.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2355.                                         $unitsServ = $itemPro->getUnits();
  2356.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2357.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2358.                                         $subnetoUnit = $subneto;
  2359.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2360.                                         break;
  2361.                                     default: //Others (por default)
  2362.                                         $pax = $itemPro->getPax();
  2363.                                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2364.                                         $days = ((($itemPro->getDateOutAt())->diff($itemPro->getDateInAt()))->days + 1);
  2365.                                         $dateServ = ($days > 1) ? ($itemPro->getDateInAt())->format('d/m/Y'). ' - '.($itemPro->getDateOutAt())->format('d/m/Y') : ($itemPro->getDateInAt())->format('d/m/Y');
  2366.                                         $unitsServ = $itemPro->getUnits();
  2367.                                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2368.                                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2369.                                         $subnetoUnit = $subneto;
  2370.                                         $subneto = $subneto * $days * $unitsServ * $pax;
  2371.                                         break;
  2372.                                 }
  2373.                                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  2374.                                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  2375.                                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  2376.                                 $valorTotalNet = $valorTotalNet + $neto;
  2377.                                 $valorVat = $valorVat + ($subtotal - $neto);
  2378.                             }
  2379.                         }
  2380.                     }
  2381.                 }
  2382.                 if (!empty($arrayInvoicedItems)) {
  2383.                     foreach ($arrayInvoicedItems as $itemInvoice) {
  2384.                         if ($itemInvoice->getType() == 'PAYMENT') {
  2385.                             //Acumulamos neto e iva para la factura
  2386.                             //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  2387.                         }
  2388.                     }
  2389.                 }
  2390.                 //FIN: Creamos los items de la factura
  2391.                 $valorTotal = $valorTotalNet + $valorVat;
  2392.                 //INICIO: Creamos la factura
  2393.                 $newInvoice = $em->getRepository(ReservationProforma::class)->findOneById($id);
  2394.                 //FIN: Creamos la factura
  2395.                 $data = array();
  2396.                 $data = $this->baseInvoiceDoneReservation($newInvoice->getReservationId(), $id,'P');
  2397.                 $paymentsClient = new ReservationPaymentsClient();
  2398.                 $paymentsClient->setReservationId($id);
  2399.                 $reservaInv = new BlvReservationInvoice();
  2400.                 $reservaInv->setReservationId($id);
  2401.                 $idLounges = array();
  2402.                 $loungesBoolToInvoice = array();
  2403.                 if (array_key_exists('lounge',$data['datasupplier'])) {
  2404.                     foreach ($data['datasupplier']['lounge'] as $item) {
  2405.                         $idLounges[] = $item['id'];
  2406.                         $loungesBoolToInvoice[] = 1;
  2407.                     }
  2408.                     $idLounges = implode(',', $idLounges);
  2409.                     $loungesBoolToInvoice = implode(',', $loungesBoolToInvoice);
  2410.                 } else {
  2411.                     $idLounges = '0';
  2412.                     $loungesBoolToInvoice = '0';
  2413.                 }
  2414.                 $idPayments = array();
  2415.                 $paymentsBoolToInvoice = array();
  2416.                 if (array_key_exists('payment',$data['datasupplier'])) {
  2417.                     foreach ($data['datasupplier']['payment'] as $item) {
  2418.                         $idPayments[] = $item['id'];
  2419.                         $paymentsBoolToInvoice[] = 1;
  2420.                     }
  2421.                     $idPayments = implode(',', $idPayments);
  2422.                     $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  2423.                 } else {
  2424.                     $idPayments = '0';
  2425.                     $paymentsBoolToInvoice = '0';
  2426.                 }
  2427.                 //Es una proforma
  2428.                 $proforma = $em->getRepository(ReservationProforma::class)->findOneById($id);
  2429.                 $invoiceIdToRec = null;
  2430.                 $reservaEquis = $em->getRepository(Reservation::class)->findOneById($proforma->getReservationId());
  2431.                 if(true){ $newInvoice = $this->clientDataToInvoice(null, 'Client', $reservaEquis->getClient(), $reservaEquis->getId()); }
  2432.             }
  2433.         }
  2434.         return $this->render('MDS/GreenPatioBundle/blv/services-proforma-print-reservation.html.twig',
  2435.             array(
  2436.                 'id' => $id,
  2437.                 'type' => $data['type'],
  2438.                 'number' => $data['number'],
  2439.                 'invoiceIdToRec' => $invoiceIdToRec,
  2440.                 'prefix' => $data['prefix'],
  2441.                 'date' => $data['date'],
  2442.                 'token' => '',
  2443.                 'company' => $data['company'],
  2444.                 'clients' => $data['clients'],
  2445.                 'datasupplier' => $data['datasupplier'],
  2446.                 'totales_neto' => $data['totales_neto'],
  2447.                 'bases_imponibles' => $data['bases_imponibles'],
  2448.                 'totales' => $data['totales'],
  2449.                 'balance' => $data['balance'],
  2450.                 'currency' => $data['currency'],
  2451.                 'paymentInvoice' => $data['paymentInvoice'],
  2452.                 'clientName' => $newInvoice->getClientName(),
  2453.                 'clientAddress' => $newInvoice->getClientAddress(),
  2454.                 'clientDocument' => $newInvoice->getClientDocument(),
  2455.             )
  2456.         );
  2457.     }
  2458.     private function baseInvoiceSelectedReservation($id, $arrayBools, $number)
  2459.     {
  2460.         $em = $this->getDoctrine()->getManager();
  2461.         $reservation = $em->getRepository(Reservation::class)->findOneById($id);
  2462.         $items = $em->getRepository(ReservationInvoiceProformaItems::class)->findByReservationId($reservation->getId());
  2463.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('2');
  2464.         $client = $em->getRepository(Client::class)->findById($reservation->getClient());
  2465.         if (!empty($client)){
  2466.             $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  2467.             $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  2468.             $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  2469.             $client['city'] = $city; $client['region'] = $region; $client['country'] = $country;
  2470.         } else {
  2471.             $client[0] = new Client();
  2472.             $client[0]->setName('');
  2473.             $client[0]->setTitle('');
  2474.             $client[0]->setIdDocument('');
  2475.             $client[0]->setPopulation('');
  2476.             $client[0]->setRegion('');
  2477.             $client[0]->setCountry('');
  2478.         }
  2479.         // Acumuladores de los calculos
  2480.         $totales_neto_all = 0;
  2481.         $data_iva = array(
  2482.             'iva' => 21,
  2483.             'ivaMontoVeintiUno' => 0,
  2484.             'ivaMontoDiez' => 0,
  2485.             'ivaMontoCero' => 0,
  2486.         );
  2487.         //INICIO: Determinamos el numero de factura o proforma
  2488.         if (substr($number,0,1) == 'I'){
  2489.             // Es una Factura
  2490.             $numberInvoiceOrProforma = substr($number,1);
  2491.             $number = $numberInvoiceOrProforma;
  2492.             $number = $em->getRepository(BlvReservationInvoice::class)->findOneById($number);
  2493.             $number = $number->getNumber();
  2494.             $type = 'Invoice';
  2495.         } else {
  2496.             // Es una Proforma
  2497.             $numberInvoiceOrProforma = substr($number,1);
  2498.             $number = $numberInvoiceOrProforma;
  2499.             $number = $em->getRepository(ReservationProforma::class)->findOneById($number);
  2500.             $number = $number->getPrefix().$number->getReservationId();
  2501.             $type = 'Proforma';
  2502.         }
  2503.         //FIN: Determinamos el numero de factura o proforma
  2504.         // Buscamos las salas reservadas, pagos y servicios para el evento
  2505.         $arrayItems = array();
  2506.         $reservationLounges = array();
  2507.         if (!empty($arrayBools['lounge'])) {
  2508.             foreach ($arrayBools['lounge'] as $key => $item) {
  2509.                 if ($item == 'true') {
  2510.                     $reservationLounges[] = $em->getRepository(ReservationLoungeSimple::class)->findOneById($key);
  2511.                 }
  2512.             }
  2513.         }
  2514.         $payments = array();
  2515.         if (!empty($arrayBools['payment'])) {
  2516.             foreach ($arrayBools['payment'] as $key => $item) {
  2517.                 if ($item == 'true') {
  2518.                     $payments[] = $em->getRepository(ReservationPaymentsClient::class)->findOneById($key);
  2519.                 }
  2520.             }
  2521.         }
  2522.         $services = array();
  2523.         if (!empty($arrayBools['service'])) {
  2524.             foreach ($arrayBools['service'] as $key => $item) {
  2525.                 if ($item == 'true') {
  2526.                     $services[] = $em->getRepository(ReservationService::class)->findOneById($key);
  2527.                 }
  2528.             }
  2529.         }
  2530.         $data_supplier = array();
  2531.         $i = 0;
  2532.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  2533.         $pax = '-';
  2534.         $qty = '1';
  2535.         $lounge = array(
  2536.             'neto' => 0,
  2537.             'sumSubT' => 0,
  2538.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  2539.         $service = array(
  2540.             'neto' => 0,
  2541.             'sumSubT' => 0,
  2542.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  2543.         foreach ($reservationLounges as $item){
  2544.             if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  2545.                 $subtotal = 0;
  2546.                 $neto = 0;
  2547.                 $subnetoMounting = 0;
  2548.                 $subtotalLounge = 0;
  2549.                 $subnetoRemoval = 0;
  2550.             } else {
  2551.                 $subtotalLounge = $item->getServicePrice() * 1.21;
  2552.                 $subneto = $item->getServicePrice();
  2553.                 $subtotal =  $subtotalLounge;
  2554.                 $neto =  $subneto;
  2555.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  2556.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  2557.                 $neto = round($neto,2,PHP_ROUND_HALF_UP);
  2558.                 $arrayItems[] = array(
  2559.                     'type' => 'Lounge',
  2560.                     'name' => $item->getLoungeName(),
  2561.                     'amount' => $neto,
  2562.                     'iva' => $iva,
  2563.                     'total' => $subtotal,
  2564.                 );
  2565.             }
  2566.             // Acumula netos totales e IVA
  2567.             $totales_neto_all = $totales_neto_all + $neto;
  2568.             switch ($iva){
  2569.                 case 21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21); break;
  2570.                 case 10: $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * 0.1); break;
  2571.                 case 0: break;
  2572.                 default: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21); break;
  2573.             }
  2574.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  2575.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  2576.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  2577.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  2578.             // Acumula netos totales e IVA
  2579.             $lounge['neto'] = $lounge['neto'] + $neto;
  2580.             $lounge['sumSubT'] = $lounge['sumSubT'] + $subtotal;
  2581.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  2582.             $lounge['neto'] = round($lounge['neto'],2,PHP_ROUND_HALF_UP);
  2583.             $lounge['sumSubT'] = round($lounge['sumSubT'],2,PHP_ROUND_HALF_UP);
  2584.             $data_supplier['lounge'][$i] = array (
  2585.                 'id' => $item->getId(),
  2586.                 'loungeName' => $item->getLoungeName(),
  2587.                 'idLounge' => $item->getIdLounge(),
  2588.                 'dateStart' => $item->getDateStart(),
  2589.                 'dateEnd' => $item->getDateEnd(),
  2590.                 'servicePrice' => $item->getServicePrice(),
  2591.                 'subtotalLounge' => $subtotalLounge,
  2592.                 'iva' => $iva,
  2593.                 'pax' => $pax,
  2594.                 'qty' => $qty,
  2595.                 'type' => $item->getType(),
  2596.                 'subtotal' => $subtotal,
  2597.             );
  2598.             $i++;
  2599.         }
  2600.         $data_supplier['loungeSubTotal'] = array(
  2601.             'neto' => $lounge['neto'],
  2602.             'sumSubT' => $lounge['sumSubT'],
  2603.         );
  2604.         $payment = array(
  2605.             'neto' => 0,
  2606.             'sumSubT' => 0,
  2607.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  2608.         $i = 0;
  2609.         foreach ($payments as $item){
  2610.             $payment['neto'] = $payment['neto'] + ($item->getAmount()*(-1));
  2611.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  2612.             $payment['neto'] = round($payment['neto'],2,PHP_ROUND_HALF_UP);
  2613.             $payment['sumSubT'] = $payment['sumSubT'] + ($item->getAmountTotal()*(-1));
  2614.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  2615.             $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  2616.             $data_supplier['payment'][$i] = array (
  2617.                 'id' => $item->getId(),
  2618.                 'amount' => $item->getAmount()*(-1),
  2619.                 'amountTotal' => $item->getAmountTotal()*(-1),
  2620.                 'vat' => $item->getVat(),
  2621.                 'datePayAt' => $item->getDatePayAt(),
  2622.                 'wayToPay' => $item->getWayToPay(),
  2623.             );
  2624.             $arrayItems[] = array(
  2625.                 'type' => 'Payment',
  2626.                 'name' => $item->getWayToPay(),
  2627.                 'amount' => $item->getAmount()*(-1),
  2628.                 'total' => $item->getAmountTotal()*(-1),
  2629.                 'iva' => $item->getVat(),
  2630.             );
  2631.             // Acumula netos totales e IVA
  2632.             $totales_neto_all = $totales_neto_all + ($item->getAmount()*(-1));
  2633.             $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + (($item->getAmount()*(-1)) * 0.21);
  2634.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  2635.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  2636.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  2637.             // Acumula netos totales e IVA
  2638.             $i++;
  2639.         }
  2640.         if (!empty($payments)) {
  2641.             $data_supplier['paymentSubTotal'] = array(
  2642.                 'neto' => $payment['neto'],
  2643.                 'sumSubT' => $payment['sumSubT'],
  2644.             );
  2645.         }
  2646.         foreach ($services as $item){
  2647.             if (is_null($item->getPrice()) or empty($item->getPrice())){
  2648.                 $subtotal = 0;
  2649.                 $neto = 0;
  2650.                 $subtotalService = 0;
  2651.             } else {
  2652.                 // Manejar Comision y Over en precio unitario
  2653.                 $priceExtra = (float)$item->getPrice();
  2654.                 $commissionExtra = (float)$item->getCommission();
  2655.                 $subnetoUnitExtra = ($item->getOpCommission() == '1')
  2656.                     ? ($priceExtra + ($priceExtra * $commissionExtra / 100))
  2657.                     : ($priceExtra - ($priceExtra * $commissionExtra / 100));
  2658.                 $subnetoUnitExtra = ($item->getOpOver() == '1') ? ($subnetoUnitExtra + $item->getOver()) : ($subnetoUnitExtra - $item->getOver());
  2659.                 $subneto = $subnetoUnitExtra;
  2660.                 $subtotalService = $subnetoUnitExtra;
  2661.                 // IVA
  2662.                 if ($item->getOpIva()=='1'){
  2663.                     $subtotalService = $subtotalService * (1 + ($item->getIva()/100));
  2664.                 } else {
  2665.                     $subtotalService = $item->getPrice();
  2666.                     $subneto = ($subneto * 100) / (100 + $item->getIva());
  2667.                 }
  2668.                 switch ($item->getServiceCatId()){
  2669.                     case 1: // Alojamiento
  2670.                         // el numero de noches $numNoches; precio unitario $subneto
  2671.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  2672.                         // La personas no afectan este calculo
  2673.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  2674.                         $subnetoUnit = $subneto;
  2675.                         $subneto = $subneto * $numNoches * $item->getUnits();
  2676.                         $data_supplier['service'][$i] = array (
  2677.                             'id' => $item->getId(),
  2678.                             'serviceCatId' => $item->getServiceCatId(),
  2679.                             'serviceName' => $item->getName(),
  2680.                             'serviceType' => 'Hotel',
  2681.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  2682.                             'qty' => $item->getUnits(),
  2683.                             'iva' => $item->getIva(),
  2684.                             'pax' => '-',
  2685.                             'precioUnit' => $subnetoUnit,
  2686.                             'subneto' => $subneto,
  2687.                             'subtotal' => $subtotal,
  2688.                         );
  2689.                         break;
  2690.                     case 2: //Actividades
  2691.                         // El número de personas es considerado en el calculo
  2692.                         $pax = $item->getPax();
  2693.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2694.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2695.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2696.                         $subtotal = $subtotalService * $days * $item->getUnits();
  2697.                         $subnetoUnit = $subneto;
  2698.                         $subneto = $subneto * $days * $item->getUnits();
  2699.                         $data_supplier['service'][$i] = array (
  2700.                             'id' => $item->getId(),
  2701.                             'serviceCatId' => $item->getServiceCatId(),
  2702.                             'serviceName' => $item->getName(),
  2703.                             'serviceType' => 'Actividad',
  2704.                             'date' => $dateServ,
  2705.                             'qty' => $item->getUnits(),
  2706.                             'iva' => $item->getIva(),
  2707.                             'pax' => $item->getPax(),
  2708.                             'precioUnit' => $subnetoUnit,
  2709.                             'subneto' => $subneto,
  2710.                             'subtotal' => $subtotal,
  2711.                         );
  2712.                         break;
  2713.                     case 3: // AV
  2714.                         $pax = $item->getPax();
  2715.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2716.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2717.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2718.                         $unitsServ = $item->getUnits();
  2719.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2720.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2721.                         $subnetoUnit = $subneto;
  2722.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2723.                         $data_supplier['service'][$i] = array (
  2724.                             'id' => $item->getId(),
  2725.                             'serviceCatId' => $item->getServiceCatId(),
  2726.                             'serviceName' => $item->getName(),
  2727.                             'serviceType' => 'AV',
  2728.                             'date' => $dateServ,
  2729.                             'qty' => $unitsServ,
  2730.                             'iva' => $item->getIva(),
  2731.                             'pax' => $item->getPax(),
  2732.                             'precioUnit' => $subnetoUnitExtra,
  2733.                             'subneto' => $subneto,
  2734.                             'subtotal' => $subtotal,
  2735.                         );
  2736.                         break;
  2737.                     case 4: //Creative
  2738.                         $pax = $item->getPax();
  2739.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2740.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2741.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2742.                         $unitsServ = $item->getUnits();
  2743.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2744.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2745.                         $subnetoUnit = $subneto;
  2746.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2747.                         $data_supplier['service'][$i] = array (
  2748.                             'id' => $item->getId(),
  2749.                             'serviceCatId' => $item->getServiceCatId(),
  2750.                             'serviceName' => $item->getName(),
  2751.                             'serviceType' => 'Creativo',
  2752.                             'date' => $dateServ,
  2753.                             'qty' => $unitsServ,
  2754.                             'iva' => $item->getIva(),
  2755.                             'pax' => $item->getPax(),
  2756.                             'precioUnit' => $subnetoUnit,
  2757.                             'subneto' => $subneto,
  2758.                             'subtotal' => $subtotal,
  2759.                         );
  2760.                         break;
  2761.                     case 5: //Cruise
  2762.                         $pax = $item->getPax();
  2763.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2764.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  2765.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2766.                         $unitsServ = $item->getUnits();
  2767.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2768.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2769.                         $subnetoUnit = $subneto;
  2770.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2771.                         $data_supplier['service'][$i] = array (
  2772.                             'id' => $item->getId(),
  2773.                             'serviceCatId' => $item->getServiceCatId(),
  2774.                             'serviceName' => $item->getName(),
  2775.                             'serviceType' => 'Crucero',
  2776.                             'date' => $dateServ,
  2777.                             'qty' => $unitsServ,
  2778.                             'iva' => $item->getIva(),
  2779.                             'pax' => $item->getPax(),
  2780.                             'precioUnit' => $subnetoUnit,
  2781.                             'subneto' => $subneto,
  2782.                             'subtotal' => $subtotal,
  2783.                         );
  2784.                         break;
  2785.                     case 6: //Entertaiment
  2786.                         $pax = $item->getPax();
  2787.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2788.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2789.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2790.                         $unitsServ = $item->getUnits();
  2791.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2792.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2793.                         $subnetoUnit = $subneto;
  2794.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2795.                         $data_supplier['service'][$i] = array (
  2796.                             'id' => $item->getId(),
  2797.                             'serviceCatId' => $item->getServiceCatId(),
  2798.                             'serviceName' => $item->getName(),
  2799.                             'serviceType' => 'Entretenimiento',
  2800.                             'date' => $dateServ,
  2801.                             'qty' => $unitsServ,
  2802.                             'iva' => $item->getIva(),
  2803.                             'pax' => $item->getPax(),
  2804.                             'precioUnit' => $subnetoUnit,
  2805.                             'subneto' => $subneto,
  2806.                             'subtotal' => $subtotal,
  2807.                         );
  2808.                         break;
  2809.                     case 7: // Gifts
  2810.                         $pax = $item->getPax();
  2811.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2812.                         $days = 1;
  2813.                         $dateServ = ($item->getDateInAt())->format('d/m/Y');
  2814.                         $unitsServ = $item->getUnits();
  2815.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2816.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2817.                         $subnetoUnit = $subneto;
  2818.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2819.                         $data_supplier['service'][$i] = array (
  2820.                             'id' => $item->getId(),
  2821.                             'serviceCatId' => $item->getServiceCatId(),
  2822.                             'serviceName' => $item->getName(),
  2823.                             'serviceType' => 'Regalos',
  2824.                             'date' => $dateServ,
  2825.                             'qty' => $unitsServ,
  2826.                             'iva' => $item->getIva(),
  2827.                             'pax' => $item->getPax(),
  2828.                             'precioUnit' => $subnetoUnit,
  2829.                             'subneto' => $subneto,
  2830.                             'subtotal' => $subtotal,
  2831.                         );
  2832.                         break;
  2833.                     case 8: //Guide
  2834.                         $pax = $item->getPax();
  2835.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2836.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2837.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2838.                         $unitsServ = $item->getUnits();
  2839.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2840.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2841.                         $subnetoUnit = $subneto;
  2842.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2843.                         $data_supplier['service'][$i] = array (
  2844.                             'id' => $item->getId(),
  2845.                             'serviceCatId' => $item->getServiceCatId(),
  2846.                             'serviceName' => $item->getName(),
  2847.                             'serviceType' => 'Guía',
  2848.                             'date' => $dateServ,
  2849.                             'qty' => $unitsServ,
  2850.                             'iva' => $item->getIva(),
  2851.                             'pax' => $item->getPax(),
  2852.                             'precioUnit' => $subnetoUnit,
  2853.                             'subneto' => $subneto,
  2854.                             'subtotal' => $subtotal,
  2855.                         );
  2856.                         break;
  2857.                     case 9: //Itineraries
  2858.                         $pax = $item->getPax();
  2859.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2860.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2861.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2862.                         $unitsServ = $item->getUnits();
  2863.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2864.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2865.                         $subnetoUnit = $subneto;
  2866.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2867.                         $data_supplier['service'][$i] = array (
  2868.                             'id' => $item->getId(),
  2869.                             'serviceCatId' => $item->getServiceCatId(),
  2870.                             'serviceName' => $item->getName(),
  2871.                             'serviceType' => 'Itinerarios',
  2872.                             'date' => $dateServ,
  2873.                             'qty' => $unitsServ,
  2874.                             'iva' => $item->getIva(),
  2875.                             'pax' => $item->getPax(),
  2876.                             'precioUnit' => $subnetoUnit,
  2877.                             'subneto' => $subneto,
  2878.                             'subtotal' => $subtotal,
  2879.                         );
  2880.                         break;
  2881.                     case 10: //Lounge  -- No Aplica
  2882.                         break;
  2883.                     case 11: //Menu (Catering)
  2884.                         $pax = $item->getPax();
  2885.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2886.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2887.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2888.                         $unitsServ = $item->getUnits();
  2889.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2890.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2891.                         $subnetoUnit = $subneto;
  2892.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2893.                         $data_supplier['service'][$i] = array (
  2894.                             'id' => $item->getId(),
  2895.                             'serviceCatId' => $item->getServiceCatId(),
  2896.                             'serviceName' => $item->getName(),
  2897.                             'serviceType' => 'Catering',
  2898.                             'date' => $dateServ,
  2899.                             'qty' => $unitsServ,
  2900.                             'iva' => $item->getIva(),
  2901.                             'pax' => $item->getPax(),
  2902.                             'precioUnit' => $subnetoUnitExtra,
  2903.                             'subneto' => $subneto,
  2904.                             'subtotal' => $subtotal,
  2905.                         );
  2906.                         break;
  2907.                     case 12: //Others
  2908.                         $pax = $item->getPax();
  2909.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2910.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2911.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2912.                         $unitsServ = $item->getUnits();
  2913.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2914.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2915.                         $subnetoUnit = $subneto;
  2916.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2917.                         $data_supplier['service'][$i] = array (
  2918.                             'id' => $item->getId(),
  2919.                             'serviceCatId' => $item->getServiceCatId(),
  2920.                             'serviceName' => $item->getName(),
  2921.                             'serviceType' => 'Otros',
  2922.                             'date' => $dateServ,
  2923.                             'qty' => $unitsServ,
  2924.                             'iva' => $item->getIva(),
  2925.                             'pax' => $item->getPax(),
  2926.                             'precioUnit' => $subnetoUnitExtra,
  2927.                             'subneto' => $subneto,
  2928.                             'subtotal' => $subtotal,
  2929.                         );
  2930.                         break;
  2931.                     case 13: //Transport
  2932.                         $pax = $item->getPax();
  2933.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2934.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2935.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2936.                         $unitsServ = $item->getUnits();
  2937.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2938.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2939.                         $subnetoUnit = $subneto;
  2940.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2941.                         $data_supplier['service'][$i] = array (
  2942.                             'id' => $item->getId(),
  2943.                             'serviceCatId' => $item->getServiceCatId(),
  2944.                             'serviceName' => $item->getName(),
  2945.                             'serviceType' => 'Transporte',
  2946.                             'date' => $dateServ,
  2947.                             'qty' => $unitsServ,
  2948.                             'iva' => $item->getIva(),
  2949.                             'pax' => $item->getPax(),
  2950.                             'precioUnit' => $subnetoUnit,
  2951.                             'subneto' => $subneto,
  2952.                             'subtotal' => $subtotal,
  2953.                         );
  2954.                         break;
  2955.                     case 14: //Technology
  2956.                         $pax = $item->getPax();
  2957.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2958.                         $days = 1;
  2959.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  2960.                         $unitsServ = $item->getUnits();
  2961.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2962.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2963.                         $subnetoUnit = $subneto;
  2964.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2965.                         $data_supplier['service'][$i] = array (
  2966.                             'id' => $item->getId(),
  2967.                             'serviceCatId' => $item->getServiceCatId(),
  2968.                             'serviceName' => $item->getName(),
  2969.                             'serviceType' => 'Tecnología',
  2970.                             'date' => $dateServ,
  2971.                             'qty' => $unitsServ,
  2972.                             'iva' => $item->getIva(),
  2973.                             'pax' => $item->getPax(),
  2974.                             'precioUnit' => $subnetoUnit,
  2975.                             'subneto' => $subneto,
  2976.                             'subtotal' => $subtotal,
  2977.                         );
  2978.                         break;
  2979.                     case 15: //Assisstant
  2980.                         $pax = $item->getPax();
  2981.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  2982.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  2983.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  2984.                         $unitsServ = $item->getUnits();
  2985.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  2986.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2987.                         $subnetoUnit = $subneto;
  2988.                         $subneto = $subneto * $days * $unitsServ * $pax;
  2989.                         $data_supplier['service'][$i] = array (
  2990.                             'id' => $item->getId(),
  2991.                             'serviceCatId' => $item->getServiceCatId(),
  2992.                             'serviceName' => $item->getName(),
  2993.                             'serviceType' => 'Asistente',
  2994.                             'date' => $dateServ,
  2995.                             'qty' => $unitsServ,
  2996.                             'iva' => $item->getIva(),
  2997.                             'pax' => $item->getPax(),
  2998.                             'precioUnit' => $subnetoUnitExtra,
  2999.                             'subneto' => $subneto,
  3000.                             'subtotal' => $subtotal,
  3001.                         );
  3002.                         break;
  3003.                     case 16: //DDR
  3004.                         $pax = $item->getPax();
  3005.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3006.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3007.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3008.                         $unitsServ = $item->getUnits();
  3009.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3010.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3011.                         $subnetoUnit = $subneto;
  3012.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3013.                         $data_supplier['service'][$i] = array (
  3014.                             'id' => $item->getId(),
  3015.                             'serviceCatId' => $item->getServiceCatId(),
  3016.                             'serviceName' => $item->getName(),
  3017.                             'serviceType' => 'DDR',
  3018.                             'date' => $dateServ,
  3019.                             'qty' => $unitsServ,
  3020.                             'iva' => $item->getIva(),
  3021.                             'pax' => $item->getPax(),
  3022.                             'precioUnit' => $subnetoUnit,
  3023.                             'subneto' => $subneto,
  3024.                             'subtotal' => $subtotal,
  3025.                         );
  3026.                         break;
  3027.                     case 17: //Seguridad
  3028.                         $pax = $item->getPax();
  3029.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3030.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3031.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3032.                         $unitsServ = $item->getUnits();
  3033.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3034.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3035.                         $subnetoUnit = $subneto;
  3036.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3037.                         $data_supplier['service'][$i] = array (
  3038.                             'id' => $item->getId(),
  3039.                             'serviceCatId' => $item->getServiceCatId(),
  3040.                             'serviceName' => $item->getName(),
  3041.                             'serviceType' => 'Seguridad',
  3042.                             'date' => $dateServ,
  3043.                             'qty' => $unitsServ,
  3044.                             'iva' => $item->getIva(),
  3045.                             'pax' => $item->getPax(),
  3046.                             'precioUnit' => $subnetoUnitExtra,
  3047.                             'subneto' => $subneto,
  3048.                             'subtotal' => $subtotal,
  3049.                         );
  3050.                         break;
  3051.                     case 18: //WiFi
  3052.                         $pax = $item->getPax();
  3053.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3054.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3055.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3056.                         $unitsServ = $item->getUnits();
  3057.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3058.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3059.                         $subnetoUnit = $subneto;
  3060.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3061.                         $data_supplier['service'][$i] = array (
  3062.                             'id' => $item->getId(),
  3063.                             'serviceCatId' => $item->getServiceCatId(),
  3064.                             'serviceName' => $item->getName(),
  3065.                             'serviceType' => 'WiFi',
  3066.                             'date' => $dateServ,
  3067.                             'qty' => $unitsServ,
  3068.                             'iva' => $item->getIva(),
  3069.                             'pax' => $item->getPax(),
  3070.                             'precioUnit' => $subnetoUnitExtra,
  3071.                             'subneto' => $subneto,
  3072.                             'subtotal' => $subtotal,
  3073.                         );
  3074.                         break;
  3075.                     case 19: //Mobiliario
  3076.                         $pax = $item->getPax();
  3077.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3078.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3079.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3080.                         $unitsServ = $item->getUnits();
  3081.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3082.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3083.                         $subnetoUnit = $subneto;
  3084.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3085.                         $data_supplier['service'][$i] = array (
  3086.                             'id' => $item->getId(),
  3087.                             'serviceCatId' => $item->getServiceCatId(),
  3088.                             'serviceName' => $item->getName(),
  3089.                             'serviceType' => 'Mobiliario',
  3090.                             'date' => $dateServ,
  3091.                             'qty' => $unitsServ,
  3092.                             'iva' => $item->getIva(),
  3093.                             'pax' => $item->getPax(),
  3094.                             'precioUnit' => $subnetoUnitExtra,
  3095.                             'subneto' => $subneto,
  3096.                             'subtotal' => $subtotal,
  3097.                         );
  3098.                         break;
  3099.                     case 20: //Parking
  3100.                         $pax = $item->getPax();
  3101.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3102.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3103.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3104.                         $unitsServ = $item->getUnits();
  3105.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3106.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3107.                         $subnetoUnit = $subneto;
  3108.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3109.                         $data_supplier['service'][$i] = array (
  3110.                             'id' => $item->getId(),
  3111.                             'serviceCatId' => $item->getServiceCatId(),
  3112.                             'serviceName' => $item->getName(),
  3113.                             'serviceType' => 'Parking',
  3114.                             'date' => $dateServ,
  3115.                             'qty' => $unitsServ,
  3116.                             'iva' => $item->getIva(),
  3117.                             'pax' => $item->getPax(),
  3118.                             'precioUnit' => $subnetoUnitExtra,
  3119.                             'subneto' => $subneto,
  3120.                             'subtotal' => $subtotal,
  3121.                         );
  3122.                         break;
  3123.                     case 21: //Limpieza
  3124.                         $pax = $item->getPax();
  3125.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3126.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3127.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3128.                         $unitsServ = $item->getUnits();
  3129.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3130.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3131.                         $subnetoUnit = $subneto;
  3132.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3133.                         $data_supplier['service'][$i] = array (
  3134.                             'id' => $item->getId(),
  3135.                             'serviceCatId' => $item->getServiceCatId(),
  3136.                             'serviceName' => $item->getName(),
  3137.                             'serviceType' => 'Limpieza',
  3138.                             'date' => $dateServ,
  3139.                             'qty' => $unitsServ,
  3140.                             'iva' => $item->getIva(),
  3141.                             'pax' => $item->getPax(),
  3142.                             'precioUnit' => $subnetoUnitExtra,
  3143.                             'subneto' => $subneto,
  3144.                             'subtotal' => $subtotal,
  3145.                         );
  3146.                         break;
  3147.                     default: //Others (por default)
  3148.                         $pax = $item->getPax();
  3149.                         if (empty($pax) or $pax == "0") { $pax = 1; }
  3150.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  3151.                         $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  3152.                         $unitsServ = $item->getUnits();
  3153.                         if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3154.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3155.                         $subnetoUnit = $subneto;
  3156.                         $subneto = $subneto * $days * $unitsServ * $pax;
  3157.                         $data_supplier['service'][$i] = array (
  3158.                             'id' => $item->getId(),
  3159.                             'serviceCatId' => $item->getServiceCatId(),
  3160.                             'serviceName' => $item->getName(),
  3161.                             'serviceType' => 'Otros',
  3162.                             'date' => $dateServ,
  3163.                             'qty' => $unitsServ,
  3164.                             'iva' => $item->getIva(),
  3165.                             'pax' => $item->getPax(),
  3166.                             'precioUnit' => $subnetoUnitExtra,
  3167.                             'subneto' => $subneto,
  3168.                             'subtotal' => $subtotal,
  3169.                         );
  3170.                         break;
  3171.                 }
  3172.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  3173.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  3174.                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  3175.                 $arrayItems[] = array(
  3176.                     'type' => 'Service',
  3177.                     'name' => $item->getName(),
  3178.                     'amount' => $neto,
  3179.                     'iva' => $item->getIva(),
  3180.                     'total' => $subtotal,
  3181.                 );
  3182.             }
  3183.             switch ($item->getIva()){
  3184.                 // Acumula IVA
  3185.                 case 21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva()/100)); break;
  3186.                 case 10: $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva()/100)); break;
  3187.                 case 0: break;
  3188.                 default: break;
  3189.             }
  3190.             $totales_neto_all = $totales_neto_all + $neto;
  3191.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3192.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  3193.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  3194.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  3195.             // Acumula netos totales e IVA
  3196.             $service['neto'] = $service['neto'] + $neto;
  3197.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  3198.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3199.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  3200.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  3201.             $i++;
  3202.         }
  3203.         $data_supplier['serviceSubTotal'] = array(
  3204.             'neto' => $service['neto'],
  3205.             'sumSubT' => $service['sumSubT'],
  3206.         );
  3207.         $currency = '€';
  3208.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  3209.         if (!empty($payments)) {
  3210.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  3211.         } else {
  3212.             $amount_pay = 0;
  3213.         }
  3214.         $totales_all = $totales_total - $amount_pay;
  3215.         if ($type == 'Invoice'){
  3216.             $em->clear();   // sin este clear se producia error al guardar
  3217.             $newInvoice = $em->getRepository(BlvReservationInvoice::class)->findOneById($numberInvoiceOrProforma);
  3218.             $newInvoice->setBalance($totales_all);
  3219.             $em->persist($newInvoice);
  3220.             $em->flush();
  3221.         }
  3222.         $data = array(
  3223.             'id' => $id,
  3224.             'type' => $type,
  3225.             'number' => $number,
  3226.             'prefix' => 'LGD-'.(new DateTime('now'))->format('dmy').'-',
  3227.             'date' => new DateTime('now'),
  3228.             'reservation' => $reservation,
  3229.             'token' => $reservation->getAccessKey(),
  3230.             'company' => $company,
  3231.             'clients' => $client,
  3232.             'arrayItems' => $arrayItems,
  3233.             'datasupplier' => $data_supplier,
  3234.             'currency' => $currency,
  3235.             'totales_neto' => $totales_neto_all,
  3236.             'bases_imponibles' => $data_iva,
  3237.             'totales' => $totales_total,
  3238.             'balance' => $totales_all,
  3239.             'paymentInvoice' => $amount_pay,
  3240.         );
  3241.         return $data;
  3242.     }
  3243.     private function baseInvoiceDoneReservation($reservationId, $invoiceId, $typeBase)
  3244.     {
  3245.         $em = $this->getDoctrine()->getManager();
  3246.         $reservation = $em->getRepository(Reservation::class)->findOneById($reservationId);
  3247.         $dateDocument = null;                                                                           //Fecha final del documento
  3248.         if (($typeBase == 'I') or ($typeBase == 'P')) {
  3249.             if ($typeBase == 'I') {
  3250.                 $items = $em->getRepository(BlvReservationInvoiceItems::class)->findBy(array('reservationId' => $reservationId, 'invoiceId' => $invoiceId));
  3251.             } else {
  3252.                 // Es factura ni proforma
  3253.                 $items = $em->getRepository(ReservationInvoiceProformaItems::class)->findBy(array('reservationId' => $reservationId, 'proformaId' => $invoiceId));
  3254.             }
  3255.         } else {
  3256.             // No es factura ni proforma, es una rectificativa
  3257.             $items = $em->getRepository(BlvReservationInvoiceRecItems::class)->findBy(array('reservationId' => $reservationId, 'invoiceRecId' => $invoiceId));
  3258.         }
  3259.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('2');
  3260.         $client = $em->getRepository(Client::class)->findById($reservation->getClient());
  3261.         if (!empty($client)){
  3262.             $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  3263.             $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  3264.             $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  3265.             $client['city'] = $city; $client['region'] = $region; $client['country'] = $country;
  3266.         } else {
  3267.             $client[0] = new Client();
  3268.             $client[0]->setName('');
  3269.             $client[0]->setTitle('');
  3270.             $client[0]->setIdDocument('');
  3271.             $client[0]->setPopulation('');
  3272.             $client[0]->setRegion('');
  3273.             $client[0]->setCountry('');
  3274.         }
  3275.         // Acumuladores de los calculos
  3276.         $totales_neto_all = 0;
  3277.         $data_iva = array(
  3278.             'iva' => 21,
  3279.             'ivaMontoVeintiUno' => 0,
  3280.             'ivaMontoDiez' => 0,
  3281.             'ivaMontoCero' => 0,
  3282.         );
  3283.         //INICIO: Determinamos el numero de factura o proforma
  3284.         if ($typeBase == 'I'){
  3285.             // Es una Factura
  3286.             $number = $em->getRepository(BlvReservationInvoice::class)->findOneById($invoiceId);
  3287.             $dateDocument = $number->getDateAt();
  3288.             $number = $number->getNumber();
  3289.             $type = 'Invoice';
  3290.         } else {
  3291.             if ($typeBase == 'R'){
  3292.                 // Es una factura rectificativa
  3293.                 $number = $em->getRepository(BlvReservationInvoiceRec::class)->findOneById($invoiceId);
  3294.                 $dateDocument = $number->getDateAt();
  3295.                 $type = 'Invoice Rec';
  3296.             } else {
  3297.                 // Es una Proforma
  3298.                 $number = $invoiceId;
  3299.                 $type = 'Proforma';
  3300.             }
  3301.         }
  3302.         //FIN: Determinamos el numero de factura o proforma
  3303.         // Buscamos las salas reservadas, pagos y servicios para el evento
  3304.         $arrayItems = [];
  3305.         $reservationLounges = [];
  3306.         $payments = [];
  3307.         $services = [];
  3308.         if (!empty($items)) {
  3309.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  3310.                 foreach ($items as $item) {
  3311.                     switch ($item->getItemType()) {
  3312.                         case 'LOUNGE': // Salon
  3313.                             $reservationLounges[] = $item;
  3314.                             break;
  3315.                         case 'PAYMENT': // Pago
  3316.                             $payments[] = $item;
  3317.                             break;
  3318.                         case 'SERVICE': // Servicio
  3319.                             $services[] = $item;
  3320.                             break;
  3321.                         default:
  3322.                             break;
  3323.                     }
  3324.                 }
  3325.             } else {
  3326.                 // Es una proforma
  3327.                 foreach ($items as $item) {
  3328.                     switch ($item->getType()) {
  3329.                         case 'LOUNGE': // Salon
  3330.                             $loungePro = $em->getRepository(ReservationLoungeSimple::class)->findOneById($item->getControlId());
  3331.                             if (!empty($loungePro)) {
  3332.                                 $reservationLounges[] = $loungePro;
  3333.                             }
  3334.                             break;
  3335.                         case 'PAYMENT': // Pago
  3336.                             $paymentPro = $em->getRepository(ReservationPaymentsClient::class)->findOneById($item->getControlId());
  3337.                             if (!empty($paymentPro)) {
  3338.                                 $payments[] = $paymentPro;
  3339.                             }
  3340.                             break;
  3341.                         case 'SERVICE': // Servicio
  3342.                             $servicePro = $em->getRepository(ReservationService::class)->findOneById($item->getControlId());
  3343.                             if (!empty($servicePro)) {
  3344.                                 $services[] = $servicePro;
  3345.                             }
  3346.                             break;
  3347.                         default: break;
  3348.                     }
  3349.                 }
  3350.             }
  3351.         }
  3352.         $data_supplier = [];
  3353.         $i = 0;
  3354.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  3355.         $pax = '-';
  3356.         $qty = '1';
  3357.         $lounge = array(
  3358.             'neto' => 0,
  3359.             'sumSubT' => 0,
  3360.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  3361.         $service = array(
  3362.             'neto' => 0,
  3363.             'sumSubT' => 0,
  3364.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  3365.         foreach ($reservationLounges as $item){
  3366.             if (($typeBase == 'I') or $typeBase == 'R') { $iva = $item->getLngIva(); } else { $iva = $item->getIva(); }
  3367.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  3368.                 if (is_null($item->getLngServicePrice()) or empty($item->getLngServicePrice())){
  3369.                     $subtotal = $neto = $subnetoMounting = $subtotalLounge = $subnetoRemoval = 0;
  3370.                 } else {
  3371.                     $subtotalLounge = $item->getLngServicePrice() * (1 + ($iva / 100));
  3372.                     $subneto = $item->getLngServicePrice();
  3373.                     $subtotal =  $subtotalLounge;
  3374.                     $neto =  $subneto;
  3375.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  3376.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  3377.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  3378.                     $arrayItems[] = array(
  3379.                         'type' => 'Lounge',
  3380.                         'name' => $item->getLngLoungeName(),
  3381.                         'amount' => $neto,
  3382.                         'iva' => $iva,
  3383.                         'total' => $subtotal,
  3384.                     );
  3385.                 }
  3386.             } else {
  3387.                 // Es proforma
  3388.                 if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  3389.                     $subtotal = $neto = $subnetoMounting = $subtotalLounge = $subnetoRemoval = 0;
  3390.                 } else {
  3391.                     $subtotalLounge = $item->getServicePrice() * (1 + ($iva / 100));
  3392.                     $subneto = $item->getServicePrice();
  3393.                     $subtotal =  $subtotalLounge;
  3394.                     $neto =  $subneto;
  3395.                     $iva = $item->getIva();
  3396.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  3397.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  3398.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  3399.                     $arrayItems[] = array(
  3400.                         'type' => 'Lounge',
  3401.                         'name' => $item->getLoungeName(),
  3402.                         'amount' => $neto,
  3403.                         'iva' => $iva,
  3404.                         'total' => $subtotal,
  3405.                     );
  3406.                 }
  3407.             }
  3408.             // Acumula netos totales e IVA
  3409.             $totales_neto_all = $totales_neto_all + $neto;
  3410.             switch ($iva){
  3411.                 case 21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21); break;
  3412.                 case 10: $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * 0.1); break;
  3413.                 case 0: break;
  3414.                 default: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21); break;
  3415.             }
  3416.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3417.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  3418.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  3419.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  3420.             // Acumula netos totales e IVA
  3421.             $lounge['neto'] = $lounge['neto'] + $neto;
  3422.             $lounge['sumSubT'] = $lounge['sumSubT'] + $subtotal;
  3423.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3424.             $lounge['neto'] = round($lounge['neto'],2,PHP_ROUND_HALF_UP);
  3425.             $lounge['sumSubT'] = round($lounge['sumSubT'],2,PHP_ROUND_HALF_UP);
  3426.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  3427.                 $data_supplier['lounge'][$i] = array(
  3428.                     'id' => $item->getId(),
  3429.                     'loungeName' => $item->getLngLoungeName(),
  3430.                     'idLounge' => $item->getLngIdLounge(),
  3431.                     'dateStart' => $item->getLngDateStart(),
  3432.                     'dateEnd' => $item->getLngDateEnd(),
  3433.                     'servicePrice' => $item->getLngServicePrice(),
  3434.                     'subtotalLounge' => $subtotalLounge,
  3435.                     'iva' => $iva,
  3436.                     'pax' => $pax,
  3437.                     'qty' => $qty,
  3438.                     'type' => $item->getLngType(),
  3439.                     'subtotal' => $subtotal,
  3440.                 );
  3441.             } else {
  3442.                 // Es proforma
  3443.                 $data_supplier['lounge'][$i] = array(
  3444.                     'id' => $item->getId(),
  3445.                     'loungeName' => $item->getLoungeName(),
  3446.                     'idLounge' => $item->getIdLounge(),
  3447.                     'dateStart' => $item->getDateStart(),
  3448.                     'dateEnd' => $item->getDateEnd(),
  3449.                     'servicePrice' => $item->getServicePrice(),
  3450.                     'subtotalLounge' => $subtotalLounge,
  3451.                     'iva' => $iva,
  3452.                     'pax' => $pax,
  3453.                     'qty' => $qty,
  3454.                     'type' => $item->getType(),
  3455.                     'subtotal' => $subtotal,
  3456.                 );
  3457.             }
  3458.             $i++;
  3459.         }
  3460.         $data_supplier['loungeSubTotal'] = array(
  3461.             'neto' => $lounge['neto'],
  3462.             'sumSubT' => $lounge['sumSubT'],
  3463.         );
  3464.         $payment = array(
  3465.             'neto' => 0,
  3466.             'sumSubT' => 0,
  3467.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  3468.         $i = 0;
  3469.         foreach ($payments as $item){
  3470.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  3471.                 if ($typeBase == 'I') {
  3472.                     $payment['neto'] = $payment['neto'] + ($item->getPayAmount() * (-1));
  3473.                     $payment['sumSubT'] = $payment['sumSubT'] + ($item->getPayAmountTotal() * (-1));
  3474.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3475.                     $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  3476.                     $payment['neto'] = round($payment['neto'], 2, PHP_ROUND_HALF_UP);
  3477.                     // Acumula netos totales e IVA
  3478.                     $totales_neto_all = $totales_neto_all + ($item->getPayAmount() * (-1));
  3479.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($item->getPayAmount() * (-1)*($item->getPayVat()/100));
  3480.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3481.                     $totales_neto_all = round($totales_neto_all, 2, PHP_ROUND_HALF_UP);
  3482.                     $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'], 2, PHP_ROUND_HALF_UP);
  3483.                     // Acumula netos totales e IVA
  3484.                     $data_supplier['payment'][$i] = array(
  3485.                         'id' => $item->getId(),
  3486.                         'amount' => $item->getPayAmount() * (-1),
  3487.                         'amountTotal' => $item->getPayAmountTotal() * (-1),
  3488.                         'vat' => $item->getPayVat(),
  3489.                         'datePayAt' => $item->getPayDatePayAt(),
  3490.                         'wayToPay' => $item->getPayWayToPay(),
  3491.                     );
  3492.                     $arrayItems[] = array(
  3493.                         'type' => 'Payment',
  3494.                         'name' => $item->getPayWayToPay(),
  3495.                         'amount' => $item->getPayAmount() * (-1),
  3496.                         'total' => $item->getPayAmountTotal() * (-1),
  3497.                         'iva' => $item->getPayVat(),
  3498.                     );
  3499.                 } else {
  3500.                     //Es una rectificativa
  3501.                     $payment['neto'] = $payment['neto'] + ($item->getPayAmount());
  3502.                     $payment['sumSubT'] = $payment['sumSubT'] + ($item->getPayAmountTotal());
  3503.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3504.                     $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  3505.                     $payment['neto'] = round($payment['neto'], 2, PHP_ROUND_HALF_UP);
  3506.                     // Acumula netos totales e IVA
  3507.                     $totales_neto_all = $totales_neto_all + ($item->getPayAmount());
  3508.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($item->getPayAmount()*(0.21));
  3509.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3510.                     $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  3511.                     $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  3512.                     // Acumula netos totales e IVA
  3513.                     $data_supplier['payment'][$i] = array(
  3514.                         'id' => $item->getId(),
  3515.                         'amount' => $item->getPayAmount(),
  3516.                         'amountTotal' => $item->getPayAmountTotal(),
  3517.                         'vat' => $item->getPayVat(),
  3518.                         'datePayAt' => $item->getPayDatePayAt(),
  3519.                         'wayToPay' => $item->getPayWayToPay(),
  3520.                     );
  3521.                     $arrayItems[] = array(
  3522.                         'type' => 'Payment',
  3523.                         'name' => $item->getPayWayToPay(),
  3524.                         'amount' => $item->getPayAmount(),
  3525.                         'total' => $item->getPayAmountTotal(),
  3526.                         'iva' => $item->getPayVat(),
  3527.                     );
  3528.                 }
  3529.             } else {
  3530.                 // Es una proforma
  3531.                 $payment['neto'] = $payment['neto'] + ($item->getAmount()*(-1));
  3532.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal()*(-1);
  3533.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3534.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  3535.                 $payment['neto'] = round($payment['neto'], 2, PHP_ROUND_HALF_UP);
  3536.                 // Acumula netos totales e IVA
  3537.                 $totales_neto_all = $totales_neto_all + ($item->getAmount()*(-1));
  3538.                 $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($item->getAmount()*(-0.21));
  3539.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  3540.                 $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  3541.                 $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  3542.                 // Acumula netos totales e IVA
  3543.                 $data_supplier['payment'][$i] = array(
  3544.                     'id' => $item->getId(),
  3545.                     'amount' => $item->getAmount()*(-1),
  3546.                     'amountTotal' => $item->getAmountTotal()*(-1),
  3547.                     'vat' => $item->getVat(),
  3548.                     'datePayAt' => $item->getDatePayAt(),
  3549.                     'wayToPay' => $item->getWayToPay(),
  3550.                 );
  3551.                 $arrayItems[] = array(
  3552.                     'type' => 'Payment',
  3553.                     'name' => $item->getWayToPay(),
  3554.                     'amount' => $item->getAmount()*(-1),
  3555.                     'total' => $item->getAmountTotal()*(-1),
  3556.                     'iva' => $item->getVat(),
  3557.                 );
  3558.             }
  3559.             $i++;
  3560.         }
  3561.         if (!empty($payments)) {
  3562.             $data_supplier['paymentSubTotal'] = array(
  3563.                 'neto' => $payment['neto'],
  3564.                 'sumSubT' => $payment['sumSubT'],
  3565.             );
  3566.         }
  3567.         foreach ($services as $item){
  3568.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  3569.                 if (is_null($item->getSrvPrice())) {
  3570.                     $subtotal = $neto = $subtotalService = 0;
  3571.                 } else {
  3572.                     $subtotalService = $item->getSrvPrice();
  3573.                     $subtotalService = str_replace(',','.',$subtotalService);
  3574.                     $subtotalService = floatval($subtotalService);
  3575.                     $subneto = $item->getSrvPrice();
  3576.                     $subneto = str_replace(',','.',$subneto);
  3577.                     $subneto = floatval($subneto);
  3578.                         // Commission
  3579.                     if ($item->getSrvOpCommission() == '1') {
  3580.                         $subtotalService = $subtotalService * (1 + ($item->getSrvCommission() / 100));
  3581.                         $subneto = $subneto * (1 + ($item->getSrvCommission() / 100));
  3582.                     } else {
  3583.                         $subtotalService = $subtotalService * (1 - ($item->getSrvCommission() / 100));
  3584.                         $subneto = $subneto * (1 - ($item->getSrvCommission() / 100));
  3585.                     }
  3586.                     // Over
  3587.                     if ($item->getSrvOpOver() == '1') {
  3588.                         $subtotalService = $subtotalService + $item->getSrvOver();
  3589.                         $subneto = $subneto + $item->getSrvOver();
  3590.                     } else {
  3591.                         $subtotalService = $subtotalService - $item->getSrvOver();
  3592.                         $subneto = $subneto - $item->getSrvOver();
  3593.                     }
  3594.                     // IVA
  3595.                     if ($item->getSrvOpIva() == '1') {
  3596.                         $subtotalService = $subtotalService * (1 + ($item->getSrvIva() / 100));
  3597.                     } else {
  3598.                         $subtotalService = $item->getSrvPrice();
  3599.                         $subneto = ($subneto * 100) / (100 + $item->getSrvIva());
  3600.                     }
  3601.                     switch ($item->getSrvServiceCatId()) {
  3602.                         case 1: // Alojamiento
  3603.                             // el numero de noches $numNoches; precio unitario $subneto
  3604.                             $numNoches = (($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days;
  3605.                             // La personas no afectan este calculo
  3606.                             $subtotal = $subtotalService * $numNoches * $item->getSrvUnits();
  3607.                             $subnetoUnit = $subneto;
  3608.                             $subneto = $subneto * $numNoches * $item->getSrvUnits();
  3609.                             $data_supplier['service'][$i] = array(
  3610.                                 'id' => $item->getId(),
  3611.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3612.                                 'serviceName' => $item->getSrvName(),
  3613.                                 'serviceType' => 'Hotel',
  3614.                                 'date' => ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y'),
  3615.                                 'qty' => $item->getSrvUnits(),
  3616.                                 'iva' => $item->getSrvIva(),
  3617.                                 'pax' => '-',
  3618.                                 'precioUnit' => $subnetoUnit,
  3619.                                 'subneto' => $subneto,
  3620.                                 'subtotal' => $subtotal,
  3621.                             );
  3622.                             break;
  3623.                         case 2: //Actividades
  3624.                             // El número de personas es considerado en el calculo
  3625.                             $pax = $item->getSrvPax();
  3626.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3627.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3628.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3629.                             $subtotal = $subtotalService * $days * $item->getSrvUnits();
  3630.                             $subnetoUnit = $subneto;
  3631.                             $subneto = $subneto * $days * $item->getSrvUnits();
  3632.                             $data_supplier['service'][$i] = array(
  3633.                                 'id' => $item->getId(),
  3634.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3635.                                 'serviceName' => $item->getSrvName(),
  3636.                                 'serviceType' => 'Actividad',
  3637.                                 'date' => $dateServ,
  3638.                                 'qty' => $item->getSrvUnits(),
  3639.                                 'iva' => $item->getSrvIva(),
  3640.                                 'pax' => $item->getSrvPax(),
  3641.                                 'precioUnit' => $subnetoUnit,
  3642.                                 'subneto' => $subneto,
  3643.                                 'subtotal' => $subtotal,
  3644.                             );
  3645.                             break;
  3646.                         case 3: // AV
  3647.                             $pax = $item->getSrvPax();
  3648.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3649.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3650.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3651.                             $unitsServ = $item->getSrvUnits();
  3652.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3653.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3654.                             $subnetoUnit = $subneto;
  3655.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3656.                             $data_supplier['service'][$i] = array(
  3657.                                 'id' => $item->getId(),
  3658.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3659.                                 'serviceName' => $item->getSrvName(),
  3660.                                 'serviceType' => 'AV',
  3661.                                 'date' => $dateServ,
  3662.                                 'qty' => $unitsServ,
  3663.                                 'iva' => $item->getSrvIva(),
  3664.                                 'pax' => $item->getSrvPax(),
  3665.                                 'precioUnit' => $subnetoUnit,
  3666.                                 'subneto' => $subneto,
  3667.                                 'subtotal' => $subtotal,
  3668.                             );
  3669.                             break;
  3670.                         case 4: //Creative
  3671.                             $pax = $item->getSrvPax();
  3672.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3673.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3674.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3675.                             $unitsServ = $item->getSrvUnits();
  3676.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3677.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3678.                             $subnetoUnit = $subneto;
  3679.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3680.                             $data_supplier['service'][$i] = array(
  3681.                                 'id' => $item->getId(),
  3682.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3683.                                 'serviceName' => $item->getSrvName(),
  3684.                                 'serviceType' => 'Creativo',
  3685.                                 'date' => $dateServ,
  3686.                                 'qty' => $unitsServ,
  3687.                                 'iva' => $item->getSrvIva(),
  3688.                                 'pax' => $item->getSrvPax(),
  3689.                                 'precioUnit' => $subnetoUnit,
  3690.                                 'subneto' => $subneto,
  3691.                                 'subtotal' => $subtotal,
  3692.                             );
  3693.                             break;
  3694.                         case 5: //Cruise
  3695.                             $pax = $item->getSrvPax();
  3696.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3697.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days);
  3698.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3699.                             $unitsServ = $item->getSrvUnits();
  3700.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3701.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3702.                             $subnetoUnit = $subneto;
  3703.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3704.                             $data_supplier['service'][$i] = array(
  3705.                                 'id' => $item->getId(),
  3706.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3707.                                 'serviceName' => $item->getSrvName(),
  3708.                                 'serviceType' => 'Crucero',
  3709.                                 'date' => $dateServ,
  3710.                                 'qty' => $unitsServ,
  3711.                                 'iva' => $item->getSrvIva(),
  3712.                                 'pax' => $item->getSrvPax(),
  3713.                                 'precioUnit' => $subnetoUnit,
  3714.                                 'subneto' => $subneto,
  3715.                                 'subtotal' => $subtotal,
  3716.                             );
  3717.                             break;
  3718.                         case 6: //Entertaiment
  3719.                             $pax = $item->getSrvPax();
  3720.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3721.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3722.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3723.                             $unitsServ = $item->getSrvUnits();
  3724.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3725.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3726.                             $subnetoUnit = $subneto;
  3727.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3728.                             $data_supplier['service'][$i] = array(
  3729.                                 'id' => $item->getId(),
  3730.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3731.                                 'serviceName' => $item->getSrvName(),
  3732.                                 'serviceType' => 'Entretenimiento',
  3733.                                 'date' => $dateServ,
  3734.                                 'qty' => $unitsServ,
  3735.                                 'iva' => $item->getSrvIva(),
  3736.                                 'pax' => $item->getSrvPax(),
  3737.                                 'precioUnit' => $subnetoUnit,
  3738.                                 'subneto' => $subneto,
  3739.                                 'subtotal' => $subtotal,
  3740.                             );
  3741.                             break;
  3742.                         case 7: // Gifts
  3743.                             $pax = $item->getSrvPax();
  3744.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3745.                             $days = 1;
  3746.                             $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  3747.                             $unitsServ = $item->getSrvUnits();
  3748.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3749.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3750.                             $subnetoUnit = $subneto;
  3751.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3752.                             $data_supplier['service'][$i] = array(
  3753.                                 'id' => $item->getId(),
  3754.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3755.                                 'serviceName' => $item->getSrvName(),
  3756.                                 'serviceType' => 'Regalos',
  3757.                                 'date' => $dateServ,
  3758.                                 'qty' => $unitsServ,
  3759.                                 'iva' => $item->getSrvIva(),
  3760.                                 'pax' => $item->getSrvPax(),
  3761.                                 'precioUnit' => $subnetoUnit,
  3762.                                 'subneto' => $subneto,
  3763.                                 'subtotal' => $subtotal,
  3764.                             );
  3765.                             break;
  3766.                         case 8: //Guide
  3767.                             $pax = $item->getSrvPax();
  3768.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3769.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3770.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3771.                             $unitsServ = $item->getSrvUnits();
  3772.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3773.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3774.                             $subnetoUnit = $subneto;
  3775.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3776.                             $data_supplier['service'][$i] = array(
  3777.                                 'id' => $item->getId(),
  3778.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3779.                                 'serviceName' => $item->getSrvName(),
  3780.                                 'serviceType' => 'Guía',
  3781.                                 'date' => $dateServ,
  3782.                                 'qty' => $unitsServ,
  3783.                                 'iva' => $item->getSrvIva(),
  3784.                                 'pax' => $item->getSrvPax(),
  3785.                                 'precioUnit' => $subnetoUnit,
  3786.                                 'subneto' => $subneto,
  3787.                                 'subtotal' => $subtotal,
  3788.                             );
  3789.                             break;
  3790.                         case 9: //Itineraries
  3791.                             $pax = $item->getSrvPax();
  3792.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3793.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3794.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3795.                             $unitsServ = $item->getSrvUnits();
  3796.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3797.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3798.                             $subnetoUnit = $subneto;
  3799.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3800.                             $data_supplier['service'][$i] = array(
  3801.                                 'id' => $item->getId(),
  3802.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3803.                                 'serviceName' => $item->getSrvName(),
  3804.                                 'serviceType' => 'Itinerarios',
  3805.                                 'date' => $dateServ,
  3806.                                 'qty' => $unitsServ,
  3807.                                 'iva' => $item->getSrvIva(),
  3808.                                 'pax' => $item->getSrvPax(),
  3809.                                 'precioUnit' => $subnetoUnit,
  3810.                                 'subneto' => $subneto,
  3811.                                 'subtotal' => $subtotal,
  3812.                             );
  3813.                             break;
  3814.                         case 10: //Lounge  -- No Aplica
  3815.                             break;
  3816.                         case 11: //Menu (Catering)
  3817.                             $pax = $item->getSrvPax();
  3818.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3819.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3820.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  3821.                             $unitsServ = $item->getSrvUnits();
  3822.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3823.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3824.                             $subnetoUnit = $subneto;
  3825.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3826.                             $data_supplier['service'][$i] = array(
  3827.                                 'id' => $item->getId(),
  3828.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3829.                                 'serviceName' => $item->getSrvName(),
  3830.                                 'serviceType' => 'Catering',
  3831.                                 'date' => $dateServ,
  3832.                                 'qty' => $unitsServ,
  3833.                                 'iva' => $item->getSrvIva(),
  3834.                                 'pax' => $item->getSrvPax(),
  3835.                                 'precioUnit' => $subnetoUnit,
  3836.                                 'subneto' => $subneto,
  3837.                                 'subtotal' => $subtotal,
  3838.                             );
  3839.                             break;
  3840.                         case 12: //Others
  3841.                             $pax = $item->getSrvPax();
  3842.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3843.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3844.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3845.                             $unitsServ = $item->getSrvUnits();
  3846.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3847.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3848.                             $subnetoUnit = $subneto;
  3849.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3850.                             $data_supplier['service'][$i] = array(
  3851.                                 'id' => $item->getId(),
  3852.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3853.                                 'serviceName' => $item->getSrvName(),
  3854.                                 'serviceType' => 'Otros',
  3855.                                 'date' => $dateServ,
  3856.                                 'qty' => $unitsServ,
  3857.                                 'iva' => $item->getSrvIva(),
  3858.                                 'pax' => $item->getSrvPax(),
  3859.                                 'precioUnit' => $subnetoUnit,
  3860.                                 'subneto' => $subneto,
  3861.                                 'subtotal' => $subtotal,
  3862.                             );
  3863.                             break;
  3864.                         case 13: //Transport
  3865.                             $pax = $item->getSrvPax();
  3866.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3867.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3868.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3869.                             $unitsServ = $item->getSrvUnits();
  3870.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3871.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3872.                             $subnetoUnit = $subneto;
  3873.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3874.                             $data_supplier['service'][$i] = array(
  3875.                                 'id' => $item->getId(),
  3876.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3877.                                 'serviceName' => $item->getSrvName(),
  3878.                                 'serviceType' => 'Transporte',
  3879.                                 'date' => $dateServ,
  3880.                                 'qty' => $unitsServ,
  3881.                                 'iva' => $item->getSrvIva(),
  3882.                                 'pax' => $item->getSrvPax(),
  3883.                                 'precioUnit' => $subnetoUnit,
  3884.                                 'subneto' => $subneto,
  3885.                                 'subtotal' => $subtotal,
  3886.                             );
  3887.                             break;
  3888.                         case 14: //Technology
  3889.                             $pax = $item->getSrvPax();
  3890.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3891.                             $days = 1;
  3892.                             $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  3893.                             $unitsServ = $item->getSrvUnits();
  3894.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3895.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3896.                             $subnetoUnit = $subneto;
  3897.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3898.                             $data_supplier['service'][$i] = array(
  3899.                                 'id' => $item->getId(),
  3900.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3901.                                 'serviceName' => $item->getSrvName(),
  3902.                                 'serviceType' => 'Tecnología',
  3903.                                 'date' => $dateServ,
  3904.                                 'qty' => $unitsServ,
  3905.                                 'iva' => $item->getSrvIva(),
  3906.                                 'pax' => $item->getSrvPax(),
  3907.                                 'precioUnit' => $subnetoUnit,
  3908.                                 'subneto' => $subneto,
  3909.                                 'subtotal' => $subtotal,
  3910.                             );
  3911.                             break;
  3912.                         case 15: //Assisstant
  3913.                             $pax = $item->getSrvPax();
  3914.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3915.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3916.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3917.                             $unitsServ = $item->getSrvUnits();
  3918.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3919.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3920.                             $subnetoUnit = $subneto;
  3921.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3922.                             $data_supplier['service'][$i] = array(
  3923.                                 'id' => $item->getId(),
  3924.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3925.                                 'serviceName' => $item->getSrvName(),
  3926.                                 'serviceType' => 'Asistente',
  3927.                                 'date' => $dateServ,
  3928.                                 'qty' => $unitsServ,
  3929.                                 'iva' => $item->getSrvIva(),
  3930.                                 'pax' => $item->getSrvPax(),
  3931.                                 'precioUnit' => $subnetoUnit,
  3932.                                 'subneto' => $subneto,
  3933.                                 'subtotal' => $subtotal,
  3934.                             );
  3935.                             break;
  3936.                         case 16: //DDR
  3937.                             $pax = $item->getSrvPax();
  3938.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3939.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3940.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3941.                             $unitsServ = $item->getSrvUnits();
  3942.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3943.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3944.                             $subnetoUnit = $subneto;
  3945.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3946.                             $data_supplier['service'][$i] = array(
  3947.                                 'id' => $item->getId(),
  3948.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3949.                                 'serviceName' => $item->getSrvName(),
  3950.                                 'serviceType' => 'DDR',
  3951.                                 'date' => $dateServ,
  3952.                                 'qty' => $unitsServ,
  3953.                                 'iva' => $item->getSrvIva(),
  3954.                                 'pax' => $item->getSrvPax(),
  3955.                                 'precioUnit' => $subnetoUnit,
  3956.                                 'subneto' => $subneto,
  3957.                                 'subtotal' => $subtotal,
  3958.                             );
  3959.                             break;
  3960.                         case 17: //Seguridad
  3961.                             $pax = $item->getSrvPax();
  3962.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3963.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3964.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3965.                             $unitsServ = $item->getSrvUnits();
  3966.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3967.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3968.                             $subnetoUnit = $subneto;
  3969.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3970.                             $data_supplier['service'][$i] = array(
  3971.                                 'id' => $item->getId(),
  3972.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3973.                                 'serviceName' => $item->getSrvName(),
  3974.                                 'serviceType' => 'Seguridad',
  3975.                                 'date' => $dateServ,
  3976.                                 'qty' => $unitsServ,
  3977.                                 'iva' => $item->getSrvIva(),
  3978.                                 'pax' => $item->getSrvPax(),
  3979.                                 'precioUnit' => $subnetoUnit,
  3980.                                 'subneto' => $subneto,
  3981.                                 'subtotal' => $subtotal,
  3982.                             );
  3983.                             break;
  3984.                         case 18: //WiFi
  3985.                             $pax = $item->getSrvPax();
  3986.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  3987.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  3988.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  3989.                             $unitsServ = $item->getSrvUnits();
  3990.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  3991.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3992.                             $subnetoUnit = $subneto;
  3993.                             $subneto = $subneto * $days * $unitsServ * $pax;
  3994.                             $data_supplier['service'][$i] = array(
  3995.                                 'id' => $item->getId(),
  3996.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  3997.                                 'serviceName' => $item->getSrvName(),
  3998.                                 'serviceType' => 'WiFi',
  3999.                                 'date' => $dateServ,
  4000.                                 'qty' => $unitsServ,
  4001.                                 'iva' => $item->getSrvIva(),
  4002.                                 'pax' => $item->getSrvPax(),
  4003.                                 'precioUnit' => $subnetoUnit,
  4004.                                 'subneto' => $subneto,
  4005.                                 'subtotal' => $subtotal,
  4006.                             );
  4007.                             break;
  4008.                         case 19: //Mobiliario
  4009.                             $pax = $item->getSrvPax();
  4010.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4011.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  4012.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  4013.                             $unitsServ = $item->getSrvUnits();
  4014.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4015.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4016.                             $subnetoUnit = $subneto;
  4017.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4018.                             $data_supplier['service'][$i] = array(
  4019.                                 'id' => $item->getId(),
  4020.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  4021.                                 'serviceName' => $item->getSrvName(),
  4022.                                 'serviceType' => 'Mobiliario',
  4023.                                 'date' => $dateServ,
  4024.                                 'qty' => $unitsServ,
  4025.                                 'iva' => $item->getSrvIva(),
  4026.                                 'pax' => $item->getSrvPax(),
  4027.                                 'precioUnit' => $subnetoUnit,
  4028.                                 'subneto' => $subneto,
  4029.                                 'subtotal' => $subtotal,
  4030.                             );
  4031.                             break;
  4032.                         case 20: //Parking
  4033.                             $pax = $item->getSrvPax();
  4034.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4035.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  4036.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  4037.                             $unitsServ = $item->getSrvUnits();
  4038.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4039.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4040.                             $subnetoUnit = $subneto;
  4041.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4042.                             $data_supplier['service'][$i] = array(
  4043.                                 'id' => $item->getId(),
  4044.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  4045.                                 'serviceName' => $item->getSrvName(),
  4046.                                 'serviceType' => 'Parking',
  4047.                                 'date' => $dateServ,
  4048.                                 'qty' => $unitsServ,
  4049.                                 'iva' => $item->getSrvIva(),
  4050.                                 'pax' => $item->getSrvPax(),
  4051.                                 'precioUnit' => $subnetoUnit,
  4052.                                 'subneto' => $subneto,
  4053.                                 'subtotal' => $subtotal,
  4054.                             );
  4055.                             break;
  4056.                         case 21: //Limpieza
  4057.                             $pax = $item->getSrvPax();
  4058.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4059.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  4060.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  4061.                             $unitsServ = $item->getSrvUnits();
  4062.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4063.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4064.                             $subnetoUnit = $subneto;
  4065.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4066.                             $data_supplier['service'][$i] = array(
  4067.                                 'id' => $item->getId(),
  4068.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  4069.                                 'serviceName' => $item->getSrvName(),
  4070.                                 'serviceType' => 'Limpieza',
  4071.                                 'date' => $dateServ,
  4072.                                 'qty' => $unitsServ,
  4073.                                 'iva' => $item->getSrvIva(),
  4074.                                 'pax' => $item->getSrvPax(),
  4075.                                 'precioUnit' => $subnetoUnit,
  4076.                                 'subneto' => $subneto,
  4077.                                 'subtotal' => $subtotal,
  4078.                             );
  4079.                             break;
  4080.                         default: //Others (por default)
  4081.                             $pax = $item->getSrvPax();
  4082.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4083.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  4084.                             $dateServ = ($days > 1) ? ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y') : ($item->getSrvDateInAt())->format('d/m/Y');
  4085.                             $unitsServ = $item->getSrvUnits();
  4086.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4087.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4088.                             $subnetoUnit = $subneto;
  4089.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4090.                             $data_supplier['service'][$i] = array(
  4091.                                 'id' => $item->getId(),
  4092.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  4093.                                 'serviceName' => $item->getSrvName(),
  4094.                                 'serviceType' => 'Otros',
  4095.                                 'date' => $dateServ,
  4096.                                 'qty' => $unitsServ,
  4097.                                 'iva' => $item->getSrvIva(),
  4098.                                 'pax' => $item->getSrvPax(),
  4099.                                 'precioUnit' => $subnetoUnit,
  4100.                                 'subneto' => $subneto,
  4101.                                 'subtotal' => $subtotal,
  4102.                             );
  4103.                             break;
  4104.                     }
  4105.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  4106.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  4107.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  4108.                     $arrayItems[] = array(
  4109.                         'type' => 'Service',
  4110.                         'name' => $item->getSrvName(),
  4111.                         'amount' => $neto,
  4112.                         'iva' => $item->getSrvIva(),
  4113.                         'total' => $subtotal,
  4114.                     );
  4115.                 }
  4116.                 switch ($item->getSrvIva()) {
  4117.                     // Acumula IVA
  4118.                     case 21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getSrvIva() / 100)); break;
  4119.                     case 10: $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getSrvIva() / 100)); break;
  4120.                     case 0: break;
  4121.                     default: break;
  4122.                 }
  4123.             } else {
  4124.                 // Es una proforma
  4125.                 if (is_null($item->getPrice()) or empty($item->getPrice())) {
  4126.                     $subtotal = 0;
  4127.                     $neto = 0;
  4128.                     $subtotalService = 0;
  4129.                 } else {
  4130.                     // Manejar Comision y Over en precio unitario
  4131.                     $priceExtra = (float)$item->getPrice();
  4132.                     $commissionExtra = (float)$item->getCommission();
  4133.                     $subnetoUnitExtra = ($item->getOpCommission() == '1')
  4134.                         ? ($priceExtra + ($priceExtra * $commissionExtra / 100))
  4135.                         : ($priceExtra - ($priceExtra * $commissionExtra / 100));
  4136.                     $subnetoUnitExtra = ($item->getOpOver() == '1') ? ($subnetoUnitExtra + $item->getOver()) : ($subnetoUnitExtra - $item->getOver());
  4137.                     $subneto = $subnetoUnitExtra;
  4138.                     $subtotalService = $subnetoUnitExtra;
  4139.                     // IVA
  4140.                     if ($item->getOpIva() == '1') {
  4141.                         $subtotalService = $subtotalService * (1 + ($item->getIva() / 100));
  4142.                     } else {
  4143.                         $subtotalService = $item->getPrice();
  4144.                         $subneto = ($subneto * 100) / (100 + $item->getIva());
  4145.                     }
  4146.                     switch ($item->getServiceCatId()) {
  4147.                         case 1: // Alojamiento
  4148.                             // el numero de noches $numNoches; precio unitario $subneto
  4149.                             $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  4150.                             // La personas no afectan este calculo
  4151.                             $subtotal = $subtotalService * $numNoches * $item->getUnits();
  4152.                             $subnetoUnit = $subneto;
  4153.                             $subneto = $subneto * $numNoches * $item->getUnits();
  4154.                             $data_supplier['service'][$i] = array(
  4155.                                 'id' => $item->getId(),
  4156.                                 'serviceCatId' => $item->getServiceCatId(),
  4157.                                 'serviceName' => $item->getName(),
  4158.                                 'serviceType' => 'Hotel',
  4159.                                 'date' => ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y'),
  4160.                                 'qty' => $item->getUnits(),
  4161.                                 'iva' => $item->getIva(),
  4162.                                 'pax' => '-',
  4163.                                 'precioUnit' => $subnetoUnit,
  4164.                                 'subneto' => $subneto,
  4165.                                 'subtotal' => $subtotal,
  4166.                             );
  4167.                             break;
  4168.                         case 2: //Actividades
  4169.                             // El número de personas es considerado en el calculo
  4170.                             $pax = $item->getPax();
  4171.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4172.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4173.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4174.                             $subtotal = $subtotalService * $days * $item->getUnits();
  4175.                             $subnetoUnit = $subneto;
  4176.                             $subneto = $subneto * $days * $item->getUnits();
  4177.                             $data_supplier['service'][$i] = array(
  4178.                                 'id' => $item->getId(),
  4179.                                 'serviceCatId' => $item->getServiceCatId(),
  4180.                                 'serviceName' => $item->getName(),
  4181.                                 'serviceType' => 'Actividad',
  4182.                                 'date' => $dateServ,
  4183.                                 'qty' => $item->getUnits(),
  4184.                                 'iva' => $item->getIva(),
  4185.                                 'pax' => $item->getPax(),
  4186.                                 'precioUnit' => $subnetoUnit,
  4187.                                 'subneto' => $subneto,
  4188.                                 'subtotal' => $subtotal,
  4189.                             );
  4190.                             break;
  4191.                         case 3: // AV
  4192.                             $pax = $item->getPax();
  4193.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4194.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4195.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4196.                             $unitsServ = $item->getUnits();
  4197.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4198.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4199.                             $subnetoUnit = $subneto;
  4200.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4201.                             $data_supplier['service'][$i] = array(
  4202.                                 'id' => $item->getId(),
  4203.                                 'serviceCatId' => $item->getServiceCatId(),
  4204.                                 'serviceName' => $item->getName(),
  4205.                                 'serviceType' => 'AV',
  4206.                                 'date' => $dateServ,
  4207.                                 'qty' => $unitsServ,
  4208.                                 'iva' => $item->getIva(),
  4209.                                 'pax' => $item->getPax(),
  4210. //                                'precioUnit' => $subnetoUnit,
  4211.                                 'precioUnit' => $subnetoUnitExtra,
  4212.                                 'subneto' => $subneto,
  4213.                                 'subtotal' => $subtotal,
  4214.                             );
  4215.                             break;
  4216.                         case 4: //Creative
  4217.                             $pax = $item->getPax();
  4218.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4219.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4220.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4221.                             $unitsServ = $item->getUnits();
  4222.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4223.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4224.                             $subnetoUnit = $subneto;
  4225.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4226.                             $data_supplier['service'][$i] = array(
  4227.                                 'id' => $item->getId(),
  4228.                                 'serviceCatId' => $item->getServiceCatId(),
  4229.                                 'serviceName' => $item->getName(),
  4230.                                 'serviceType' => 'Creativo',
  4231.                                 'date' => $dateServ,
  4232.                                 'qty' => $unitsServ,
  4233.                                 'iva' => $item->getIva(),
  4234.                                 'pax' => $item->getPax(),
  4235.                                 'precioUnit' => $subnetoUnit,
  4236.                                 'subneto' => $subneto,
  4237.                                 'subtotal' => $subtotal,
  4238.                             );
  4239.                             break;
  4240.                         case 5: //Cruise
  4241.                             $pax = $item->getPax();
  4242.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4243.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  4244.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4245.                             $unitsServ = $item->getUnits();
  4246.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4247.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4248.                             $subnetoUnit = $subneto;
  4249.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4250.                             $data_supplier['service'][$i] = array(
  4251.                                 'id' => $item->getId(),
  4252.                                 'serviceCatId' => $item->getServiceCatId(),
  4253.                                 'serviceName' => $item->getName(),
  4254.                                 'serviceType' => 'Crucero',
  4255.                                 'date' => $dateServ,
  4256.                                 'qty' => $unitsServ,
  4257.                                 'iva' => $item->getIva(),
  4258.                                 'pax' => $item->getPax(),
  4259.                                 'precioUnit' => $subnetoUnit,
  4260.                                 'subneto' => $subneto,
  4261.                                 'subtotal' => $subtotal,
  4262.                             );
  4263.                             break;
  4264.                         case 6: //Entertaiment
  4265.                             $pax = $item->getPax();
  4266.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4267.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4268.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4269.                             $unitsServ = $item->getUnits();
  4270.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4271.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4272.                             $subnetoUnit = $subneto;
  4273.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4274.                             $data_supplier['service'][$i] = array(
  4275.                                 'id' => $item->getId(),
  4276.                                 'serviceCatId' => $item->getServiceCatId(),
  4277.                                 'serviceName' => $item->getName(),
  4278.                                 'serviceType' => 'Entretenimiento',
  4279.                                 'date' => $dateServ,
  4280.                                 'qty' => $unitsServ,
  4281.                                 'iva' => $item->getIva(),
  4282.                                 'pax' => $item->getPax(),
  4283.                                 'precioUnit' => $subnetoUnit,
  4284.                                 'subneto' => $subneto,
  4285.                                 'subtotal' => $subtotal,
  4286.                             );
  4287.                             break;
  4288.                         case 7: // Gifts
  4289.                             $pax = $item->getPax();
  4290.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4291.                             $days = 1;
  4292.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  4293.                             $unitsServ = $item->getUnits();
  4294.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4295.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4296.                             $subnetoUnit = $subneto;
  4297.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4298.                             $data_supplier['service'][$i] = array(
  4299.                                 'id' => $item->getId(),
  4300.                                 'serviceCatId' => $item->getServiceCatId(),
  4301.                                 'serviceName' => $item->getName(),
  4302.                                 'serviceType' => 'Regalos',
  4303.                                 'date' => $dateServ,
  4304.                                 'qty' => $unitsServ,
  4305.                                 'iva' => $item->getIva(),
  4306.                                 'pax' => $item->getPax(),
  4307.                                 'precioUnit' => $subnetoUnit,
  4308.                                 'subneto' => $subneto,
  4309.                                 'subtotal' => $subtotal,
  4310.                             );
  4311.                             break;
  4312.                         case 8: //Guide
  4313.                             $pax = $item->getPax();
  4314.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4315.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4316.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4317.                             $unitsServ = $item->getUnits();
  4318.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4319.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4320.                             $subnetoUnit = $subneto;
  4321.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4322.                             $data_supplier['service'][$i] = array(
  4323.                                 'id' => $item->getId(),
  4324.                                 'serviceCatId' => $item->getServiceCatId(),
  4325.                                 'serviceName' => $item->getName(),
  4326.                                 'serviceType' => 'Guía',
  4327.                                 'date' => $dateServ,
  4328.                                 'qty' => $unitsServ,
  4329.                                 'iva' => $item->getIva(),
  4330.                                 'pax' => $item->getPax(),
  4331.                                 'precioUnit' => $subnetoUnit,
  4332.                                 'subneto' => $subneto,
  4333.                                 'subtotal' => $subtotal,
  4334.                             );
  4335.                             break;
  4336.                         case 9: //Itineraries
  4337.                             $pax = $item->getPax();
  4338.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4339.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4340.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4341.                             $unitsServ = $item->getUnits();
  4342.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4343.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4344.                             $subnetoUnit = $subneto;
  4345.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4346.                             $data_supplier['service'][$i] = array(
  4347.                                 'id' => $item->getId(),
  4348.                                 'serviceCatId' => $item->getServiceCatId(),
  4349.                                 'serviceName' => $item->getName(),
  4350.                                 'serviceType' => 'Itinerarios',
  4351.                                 'date' => $dateServ,
  4352.                                 'qty' => $unitsServ,
  4353.                                 'iva' => $item->getIva(),
  4354.                                 'pax' => $item->getPax(),
  4355.                                 'precioUnit' => $subnetoUnit,
  4356.                                 'subneto' => $subneto,
  4357.                                 'subtotal' => $subtotal,
  4358.                             );
  4359.                             break;
  4360.                         case 10: //Lounge  -- No Aplica
  4361.                             break;
  4362.                         case 11: //Catering (Menu)
  4363.                             $pax = $item->getPax();
  4364.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4365.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4366.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4367.                             $unitsServ = $item->getUnits();
  4368.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4369.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4370.                             $subnetoUnit = $subneto;
  4371.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4372.                             $data_supplier['service'][$i] = array(
  4373.                                 'id' => $item->getId(),
  4374.                                 'serviceCatId' => $item->getServiceCatId(),
  4375.                                 'serviceName' => $item->getName(),
  4376.                                 'serviceType' => 'Catering',
  4377.                                 'date' => $dateServ,
  4378.                                 'qty' => $unitsServ,
  4379.                                 'iva' => $item->getIva(),
  4380.                                 'pax' => $item->getPax(),
  4381.                                 'precioUnit' => $subnetoUnitExtra,
  4382.                                 'subneto' => $subneto,
  4383.                                 'subtotal' => $subtotal,
  4384.                             );
  4385.                             break;
  4386.                         case 12: //Others
  4387.                             $pax = $item->getPax();
  4388.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4389.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4390.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4391.                             $unitsServ = $item->getUnits();
  4392.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4393.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4394.                             $subnetoUnit = $subneto;
  4395.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4396.                             $data_supplier['service'][$i] = array(
  4397.                                 'id' => $item->getId(),
  4398.                                 'serviceCatId' => $item->getServiceCatId(),
  4399.                                 'serviceName' => $item->getName(),
  4400.                                 'serviceType' => 'Otros',
  4401.                                 'date' => $dateServ,
  4402.                                 'qty' => $unitsServ,
  4403.                                 'iva' => $item->getIva(),
  4404.                                 'pax' => $item->getPax(),
  4405.                                 'precioUnit' => $subnetoUnitExtra,
  4406.                                 'subneto' => $subneto,
  4407.                                 'subtotal' => $subtotal,
  4408.                             );
  4409.                             break;
  4410.                         case 13: //Transport
  4411.                             $pax = $item->getPax();
  4412.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4413.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4414.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4415.                             $unitsServ = $item->getUnits();
  4416.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4417.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4418.                             $subnetoUnit = $subneto;
  4419.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4420.                             $data_supplier['service'][$i] = array(
  4421.                                 'id' => $item->getId(),
  4422.                                 'serviceCatId' => $item->getServiceCatId(),
  4423.                                 'serviceName' => $item->getName(),
  4424.                                 'serviceType' => 'Transporte',
  4425.                                 'date' => $dateServ,
  4426.                                 'qty' => $unitsServ,
  4427.                                 'iva' => $item->getIva(),
  4428.                                 'pax' => $item->getPax(),
  4429.                                 'precioUnit' => $subnetoUnit,
  4430.                                 'subneto' => $subneto,
  4431.                                 'subtotal' => $subtotal,
  4432.                             );
  4433.                             break;
  4434.                         case 14: //Technology
  4435.                             $pax = $item->getPax();
  4436.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4437.                             $days = 1;
  4438.                             $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  4439.                             $unitsServ = $item->getUnits();
  4440.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4441.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4442.                             $subnetoUnit = $subneto;
  4443.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4444.                             $data_supplier['service'][$i] = array(
  4445.                                 'id' => $item->getId(),
  4446.                                 'serviceCatId' => $item->getServiceCatId(),
  4447.                                 'serviceName' => $item->getName(),
  4448.                                 'serviceType' => 'Tecnología',
  4449.                                 'date' => $dateServ,
  4450.                                 'qty' => $unitsServ,
  4451.                                 'iva' => $item->getIva(),
  4452.                                 'pax' => $item->getPax(),
  4453.                                 'precioUnit' => $subnetoUnit,
  4454.                                 'subneto' => $subneto,
  4455.                                 'subtotal' => $subtotal,
  4456.                             );
  4457.                             break;
  4458.                         case 15: //Assisstant
  4459.                             $pax = $item->getPax();
  4460.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4461.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4462.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4463.                             $unitsServ = $item->getUnits();
  4464.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4465.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4466.                             $subnetoUnit = $subneto;
  4467.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4468.                             $data_supplier['service'][$i] = array(
  4469.                                 'id' => $item->getId(),
  4470.                                 'serviceCatId' => $item->getServiceCatId(),
  4471.                                 'serviceName' => $item->getName(),
  4472.                                 'serviceType' => 'Asistente',
  4473.                                 'date' => $dateServ,
  4474.                                 'qty' => $unitsServ,
  4475.                                 'iva' => $item->getIva(),
  4476.                                 'pax' => $item->getPax(),
  4477.                                 'precioUnit' => $subnetoUnitExtra,
  4478.                                 'subneto' => $subneto,
  4479.                                 'subtotal' => $subtotal,
  4480.                             );
  4481.                             break;
  4482.                         case 16: //DDR
  4483.                             $pax = $item->getPax();
  4484.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4485.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4486.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4487.                             $unitsServ = $item->getUnits();
  4488.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4489.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4490.                             $subnetoUnit = $subneto;
  4491.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4492.                             $data_supplier['service'][$i] = array(
  4493.                                 'id' => $item->getId(),
  4494.                                 'serviceCatId' => $item->getServiceCatId(),
  4495.                                 'serviceName' => $item->getName(),
  4496.                                 'serviceType' => 'DDR',
  4497.                                 'date' => $dateServ,
  4498.                                 'qty' => $unitsServ,
  4499.                                 'iva' => $item->getIva(),
  4500.                                 'pax' => $item->getPax(),
  4501.                                 'precioUnit' => $subnetoUnit,
  4502.                                 'subneto' => $subneto,
  4503.                                 'subtotal' => $subtotal,
  4504.                             );
  4505.                             break;
  4506.                         case 17: //Seguridad
  4507.                             $pax = $item->getPax();
  4508.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4509.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4510.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4511.                             $unitsServ = $item->getUnits();
  4512.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4513.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4514.                             $subnetoUnit = $subneto;
  4515.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4516.                             $data_supplier['service'][$i] = array(
  4517.                                 'id' => $item->getId(),
  4518.                                 'serviceCatId' => $item->getServiceCatId(),
  4519.                                 'serviceName' => $item->getName(),
  4520.                                 'serviceType' => 'Seguridad',
  4521.                                 'date' => $dateServ,
  4522.                                 'qty' => $unitsServ,
  4523.                                 'iva' => $item->getIva(),
  4524.                                 'pax' => $item->getPax(),
  4525.                                 'precioUnit' => $subnetoUnitExtra,
  4526.                                 'subneto' => $subneto,
  4527.                                 'subtotal' => $subtotal,
  4528.                             );
  4529.                             break;
  4530.                         case 18: //WiFi
  4531.                             $pax = $item->getPax();
  4532.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4533.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4534.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4535.                             $unitsServ = $item->getUnits();
  4536.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4537.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4538.                             $subnetoUnit = $subneto;
  4539.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4540.                             $data_supplier['service'][$i] = array(
  4541.                                 'id' => $item->getId(),
  4542.                                 'serviceCatId' => $item->getServiceCatId(),
  4543.                                 'serviceName' => $item->getName(),
  4544.                                 'serviceType' => 'WiFi',
  4545.                                 'date' => $dateServ,
  4546.                                 'qty' => $unitsServ,
  4547.                                 'iva' => $item->getIva(),
  4548.                                 'pax' => $item->getPax(),
  4549.                                 'precioUnit' => $subnetoUnitExtra,
  4550.                                 'subneto' => $subneto,
  4551.                                 'subtotal' => $subtotal,
  4552.                             );
  4553.                             break;
  4554.                         case 19: //Mobiliario
  4555.                             $pax = $item->getPax();
  4556.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4557.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4558.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4559.                             $unitsServ = $item->getUnits();
  4560.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4561.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4562.                             $subnetoUnit = $subneto;
  4563.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4564.                             $data_supplier['service'][$i] = array(
  4565.                                 'id' => $item->getId(),
  4566.                                 'serviceCatId' => $item->getServiceCatId(),
  4567.                                 'serviceName' => $item->getName(),
  4568.                                 'serviceType' => 'Mobiliario',
  4569.                                 'date' => $dateServ,
  4570.                                 'qty' => $unitsServ,
  4571.                                 'iva' => $item->getIva(),
  4572.                                 'pax' => $item->getPax(),
  4573.                                 'precioUnit' => $subnetoUnitExtra,
  4574.                                 'subneto' => $subneto,
  4575.                                 'subtotal' => $subtotal,
  4576.                             );
  4577.                             break;
  4578.                         case 20: //Parking
  4579.                             $pax = $item->getPax();
  4580.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4581.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4582.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4583.                             $unitsServ = $item->getUnits();
  4584.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4585.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4586.                             $subnetoUnit = $subneto;
  4587.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4588.                             $data_supplier['service'][$i] = array(
  4589.                                 'id' => $item->getId(),
  4590.                                 'serviceCatId' => $item->getServiceCatId(),
  4591.                                 'serviceName' => $item->getName(),
  4592.                                 'serviceType' => 'Parking',
  4593.                                 'date' => $dateServ,
  4594.                                 'qty' => $unitsServ,
  4595.                                 'iva' => $item->getIva(),
  4596.                                 'pax' => $item->getPax(),
  4597.                                 'precioUnit' => $subnetoUnitExtra,
  4598.                                 'subneto' => $subneto,
  4599.                                 'subtotal' => $subtotal,
  4600.                             );
  4601.                             break;
  4602.                         case 21: //Limpieza
  4603.                             $pax = $item->getPax();
  4604.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4605.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4606.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4607.                             $unitsServ = $item->getUnits();
  4608.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4609.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4610.                             $subnetoUnit = $subneto;
  4611.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4612.                             $data_supplier['service'][$i] = array(
  4613.                                 'id' => $item->getId(),
  4614.                                 'serviceCatId' => $item->getServiceCatId(),
  4615.                                 'serviceName' => $item->getName(),
  4616.                                 'serviceType' => 'Limpieza',
  4617.                                 'date' => $dateServ,
  4618.                                 'qty' => $unitsServ,
  4619.                                 'iva' => $item->getIva(),
  4620.                                 'pax' => $item->getPax(),
  4621.                                 'precioUnit' => $subnetoUnitExtra,
  4622.                                 'subneto' => $subneto,
  4623.                                 'subtotal' => $subtotal,
  4624.                             );
  4625.                             break;
  4626.                         default: //Others (por default)
  4627.                             $pax = $item->getPax();
  4628.                             if (empty($pax) or $pax == "0") { $pax = 1; }
  4629.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  4630.                             $dateServ = ($days > 1) ? ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y') : ($item->getDateInAt())->format('d/m/Y');
  4631.                             $unitsServ = $item->getUnits();
  4632.                             if (empty($unitsServ) or $unitsServ == "0") { $unitsServ = 1; }
  4633.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4634.                             $subnetoUnit = $subneto;
  4635.                             $subneto = $subneto * $days * $unitsServ * $pax;
  4636.                             $data_supplier['service'][$i] = array(
  4637.                                 'id' => $item->getId(),
  4638.                                 'serviceCatId' => $item->getServiceCatId(),
  4639.                                 'serviceName' => $item->getName(),
  4640.                                 'serviceType' => 'Otros',
  4641.                                 'date' => $dateServ,
  4642.                                 'qty' => $unitsServ,
  4643.                                 'iva' => $item->getIva(),
  4644.                                 'pax' => $item->getPax(),
  4645.                                 'precioUnit' => $subnetoUnitExtra,
  4646.                                 'subneto' => $subneto,
  4647.                                 'subtotal' => $subtotal,
  4648.                             );
  4649.                             break;
  4650.                     }
  4651.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  4652.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  4653.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  4654.                     $arrayItems[] = array(
  4655.                         'type' => 'Service',
  4656.                         'name' => $item->getName(),
  4657.                         'amount' => $neto,
  4658.                         'iva' => $item->getIva(),
  4659.                         'total' => $subtotal,
  4660.                     );
  4661.                 }
  4662.                 switch ($item->getIva()) {
  4663.                     // Acumula IVA
  4664.                     case 21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva() / 100)); break;
  4665.                     case 10: $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva() / 100)); break;
  4666.                     case 0: break;
  4667.                     default: break;
  4668.                 }
  4669.             }
  4670.             $totales_neto_all = $totales_neto_all + $neto;
  4671.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  4672.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  4673.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  4674.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  4675.             // Acumula netos totales e IVA
  4676.             $service['neto'] = $service['neto'] + $neto;
  4677.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  4678.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  4679.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  4680.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  4681.             $i++;
  4682.         }
  4683.         $data_supplier['serviceSubTotal'] = array(
  4684.             'neto' => $service['neto'],
  4685.             'sumSubT' => $service['sumSubT'],
  4686.         );
  4687.         $currency = '€';
  4688.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  4689.         if (!empty($payments)) {
  4690.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  4691.         } else {
  4692.             $amount_pay = 0;
  4693.         }
  4694.         $totales_all = $totales_total - $amount_pay;
  4695.         if (empty($dateDocument)){ $dateDocument = new DateTime('now'); }
  4696.         $data = array(
  4697.             'id' => $reservationId,
  4698.             'type' => $type,
  4699.             'number' => $number,
  4700.             'prefix' => 'GP-'.(new DateTime('now'))->format('dmy').'-',
  4701.             'date' => $dateDocument,
  4702.             'reservation' => $reservation,
  4703.             'token' => $reservation->getAccessKey(),
  4704.             'company' => $company,
  4705.             'clients' => $client,
  4706.             'arrayItems' => $arrayItems,
  4707.             'datasupplier' => $data_supplier,
  4708.             'currency' => $currency,
  4709.             'totales_neto' => $totales_neto_all,
  4710.             'bases_imponibles' => $data_iva,
  4711.             'totales' => $totales_total,
  4712.             'balance' => $totales_all,
  4713.             'paymentInvoice' => $amount_pay,
  4714.         );
  4715.         return $data;
  4716.     }
  4717.     /**
  4718.      * @Route("/blv/invoicerec/{id}",  name="reservations_invoice_rec_blv")
  4719.      * Generar factura rectificativa Bella View
  4720.      */
  4721.     public function generateReservationInvoiceRecAction($id, EntityManagerInterface $em, Request $request)
  4722.     {
  4723.         $invoice = $em->getRepository(BlvReservationInvoice::class)->findOneById($id);
  4724.         if (true){
  4725.             // Verificamos que no exista una factura rectificativa previa sobre la misma factura
  4726.             $previusInvoiceRec = $em->getRepository(BlvReservationInvoiceRec::class)->findOneByInvoiceToRec($id);
  4727.             if (!empty($previusInvoiceRec)){ return $this->redirectToRoute('reservations_viewrec_invoice_blv', array('id' => $previusInvoiceRec->getId())); }
  4728.         }
  4729.         $now = new DateTime();
  4730.         /* Obtengo usuario logueado */
  4731.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4732.         $user_id = $user_logueado->getId();
  4733.         $invoiceRec = new BlvReservationInvoiceRec();
  4734.         $invoiceRec->setInvoiceToRec($id);
  4735.         $invoiceRec->setCreatedAt($now);
  4736.         $invoiceRec->setUpdatedAt($now);
  4737.         $invoiceRec->setCreatedId($user_id);
  4738.         $invoiceRec->setUpdatedId($user_id);
  4739.         $invoiceRec->setDateAt($now);
  4740.         $invoiceRec->setMaster($invoice->getMaster());
  4741.         $invoiceRec->setNumber('REC-'.$invoice->getNumber());
  4742.         $invoiceRec->setPrefix('RL');
  4743.         $invoiceRec->setReservationId($invoice->getReservationId());
  4744.         $invoiceRec->setTotal($invoice->getTotal()*(-1));
  4745.         $invoiceRec->setTotalNet($invoice->getTotalNet()*(-1));
  4746.         $invoiceRec->setType('Invoice Rec');
  4747.         $invoiceRec->setVat($invoice->getVat()*(-1));
  4748.         $invoiceRec->setBalance($invoice->getBalance()*(-1));
  4749.         $invoiceRec->setClientName($invoice->getClientName());
  4750.         $invoiceRec->setClientAddress($invoice->getClientAddress());
  4751.         $invoiceRec->setClientDocument($invoice->getClientDocument());
  4752.         $invoiceRec->setClientType($invoice->getClientType());
  4753.         $em->persist($invoiceRec);
  4754.         $em->flush();
  4755.         $invoiceItems = $em->getRepository(BlvReservationInvoiceItems::class)->findByInvoiceId($id);
  4756.         // Cada elemento se duplica en ItemsRec y se libera asignando ID = 0
  4757.         foreach ($invoiceItems as $item){
  4758.             $itemRec = new BlvReservationInvoiceRecItems();
  4759.             $itemRec->setReservationId($item->getReservationId());
  4760.             $itemRec->setInvoiceId($item->getInvoiceId());
  4761.             $itemRec->setItemType($item->getItemType());
  4762.             $itemRec->setInvoiceRecId($invoiceRec->getId());
  4763.             $itemRec->setLngControlId($item->getLngControlId());
  4764.             $itemRec->setLngLoungeName($item->getLngLoungeName());
  4765.             $itemRec->setLngIdLounge($item->getLngIdLounge());
  4766.             $itemRec->setLngIdLounge($item->getLngIdLounge());
  4767.             $itemRec->setLngDateStart($item->getLngDateStart());
  4768.             $itemRec->setLngDateEnd($item->getLngDateEnd());
  4769.             $itemRec->setLngServicePrice($item->getLngServicePrice()*(-1));
  4770.             $itemRec->setLngPax($item->getLngPax());
  4771.             $itemRec->setLngType($item->getLngType());
  4772.             $itemRec->setLngHourStart($item->getLngHourStart());
  4773.             $itemRec->setLngMinStart($item->getLngMinStart());
  4774.             $itemRec->setLngHourEnd($item->getLngHourEnd());
  4775.             $itemRec->setLngMinEnd($item->getLngMinEnd());
  4776.             $itemRec->setLngIva($item->getLngIva());
  4777.             $itemRec->setLngOpIva($item->getLngOpIva());
  4778.             $itemRec->setSrvControlId($item->getSrvControlId());
  4779.             $itemRec->setSrvSupplierId($item->getSrvSupplierId());
  4780.             $itemRec->setSrvServiceId($item->getSrvServiceId());
  4781.             $itemRec->setSrvServiceCatId($item->getSrvServiceCatId());
  4782.             $itemRec->setSrvServiceCatName($item->getSrvServiceCatName());
  4783.             $itemRec->setSrvPrice($item->getSrvPrice()*(-1));
  4784.             $itemRec->setSrvCurrency($item->getSrvCurrency());
  4785.             $itemRec->setSrvUnits($item->getSrvUnits());
  4786.             $itemRec->setSrvOpCommission($item->getSrvOpCommission());
  4787.             $itemRec->setSrvCommission($item->getSrvCommission());
  4788.             $itemRec->setSrvOpOver($item->getSrvOpOver());
  4789.             $itemRec->setSrvOver($item->getSrvOver()*(-1));
  4790.             $itemRec->setSrvOpIva($item->getSrvOpIva());
  4791.             $itemRec->setSrvIva($item->getSrvIva());
  4792.             $itemRec->setSrvPax($item->getSrvPax());
  4793.             $itemRec->setSrvHour($item->getSrvHour());
  4794.             $itemRec->setSrvDateInAt($item->getSrvDateInAt());
  4795.             $itemRec->setSrvDateOutAt($item->getSrvDateOutAt());
  4796.             $itemRec->setSrvContcolor($item->getSrvContcolor());
  4797.             $itemRec->setSrvRank($item->getSrvRank());
  4798.             $itemRec->setSrvAssistantId($item->getSrvAssistantId());
  4799.             $itemRec->setSrvActivityId($item->getSrvActivityId());
  4800.             $itemRec->setSrvPay($item->getSrvPay());
  4801.             $itemRec->setSrvName($item->getSrvName());
  4802.             $itemRec->setPayControlId($item->getPayControlId());
  4803.             $itemRec->setPayAmount($item->getPayAmount());          // Los pagos han pasado a ser las facturas de deposito, en la tabla su amount se encuentra en positivo, en la rectificativa, este valor lo necesitamos en positivo
  4804.             $itemRec->setPayDatePayAt($item->getPayDatePayAt());
  4805.             $itemRec->setPayWayToPay($item->getPayWayToPay());
  4806.             $itemRec->setPayAmountTotal($item->getPayAmountTotal());    // Los pagos han pasado a ser las facturas de deposito, en la tabla su amount se encuentra en positivo, en la rectificativa, este valor lo necesitamos en positivo
  4807.             $itemRec->setPayVat($item->getPayVat());
  4808.             $itemRec->setCreatedId($user_id);
  4809.             $itemRec->setCreatedAt(new DateTime('now'));
  4810.             $itemRec->setUpdatedId($user_id);
  4811.             $itemRec->setUpdatedAt(new DateTime('now'));
  4812.             $em->persist($itemRec);
  4813.             $em->flush();
  4814.             // Se libera asignando ID = 0
  4815.             $item->setLngControlId(0);
  4816.             $item->setSrvControlId(0);
  4817.             $item->setPayControlId(0);
  4818.             $em->persist($item);
  4819.             $em->flush();
  4820.         }
  4821.         $allInvoices = $em->getRepository(BlvReservationInvoice::class)->findByReservationId($invoice->getReservationId());
  4822.         $allInvoicesRec = $em->getRepository(BlvReservationInvoiceRec::class)->findByReservationId($invoice->getReservationId());
  4823.         foreach ($allInvoicesRec as $item){ array_push($allInvoices, $item); }
  4824.         $reservation = $em->getRepository(Reservation::class)->findOneById($invoice->getReservationId());
  4825.         $client = $em->getRepository(Client::class)->findOneById($reservation->getClient());
  4826.         $data = array();
  4827.         $data['clients'][0] = $client;
  4828.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('2');
  4829.         $data['datasupplier'] = '';
  4830.         $data['totales_neto'] = '';
  4831.         $data['totales'] = '';
  4832.         $data['bases_imponibles'] = '';
  4833.         $data['balance'] = '';
  4834.         $data['paymentInvoice'] = '';
  4835.         $data['currency'] = '';
  4836.         // Una factura solo se puede rectificar una vez
  4837.         $rectf = $em->getRepository(BlvReservationInvoiceRec::class)->findBy( array('number' => 'REC-'.$invoice->getNumber()));
  4838.         $boolToRec = empty($rectf);
  4839.         return $this->redirectToRoute('reservations_viewrec_invoice_blv', array('id' => $invoiceRec->getId()));
  4840.     }
  4841.     /**
  4842.      * @Route("/blv/reservations/invoice/list", name="reservation_invoice_list_blv")
  4843.      * Listar las facturas de Bella View
  4844.      */
  4845.     public function indexAction( EntityManagerInterface $em, Request $request) {
  4846.         $todayTwoYearAgo = new DateTime('now -2 year');
  4847.         $parameters = array( 'dateStart' => $todayTwoYearAgo, );
  4848.         $dql = 'SELECT p
  4849.                 FROM GreenPatioBundle:BlvReservationInvoice p
  4850.                 WHERE p.dateAt > :dateStart';
  4851.         $query = $em->createQuery($dql)->setParameters($parameters);
  4852.         $invoicedBlv = $query->getResult();
  4853.         $parameters = array( 'dateStart' => $todayTwoYearAgo, );
  4854.         $dql = 'SELECT p
  4855.                 FROM GreenPatioBundle:BlvReservationInvoiceRec p
  4856.                 WHERE p.dateAt > :dateStart';
  4857.         $query = $em->createQuery($dql)->setParameters($parameters);
  4858.         $invoicedRec = $query->getResult();
  4859.         $invoiced = [];
  4860.         foreach ($invoicedBlv as $item){ $invoiced['B'.$item->getId()] = $item; }
  4861.         // Agregamos las rectificativas al arreglo de facturas con el indice "R(id)"
  4862.         foreach ($invoicedRec as $idRecArray => $item){ $invoiced['R'.$item->getId()] = $item; }
  4863.         $sumatoriaTotalNet = $sumatoriaTotalVat = $sumatoriaTotal = 0;
  4864.         foreach ($invoiced as $idArray => $item){
  4865.             $reserva = $em->getRepository(Reservation::class)->findOneById($item->getReservationId());
  4866.             $invoiced[$idArray]->setNumber($reserva->getTitle());
  4867.             $client = $em->getRepository(Client::class)->findOneById($reserva->getClient());
  4868.             if(!empty($client)){ $invoiced[$idArray]->setPrefix($client->getName()); } else { $invoiced[$idArray]->setPrefix(''); }
  4869.             $estaEnFactura = false; $estaEnFacturaRectificativa = false;
  4870.             if ($item->getType() == 'Invoice Deposit'){
  4871.                 // Si la factura de deposito se encuentra en una factura sumara 0
  4872.                 // INICIO: Verificamos factura de deposito dentro de alguna factura
  4873.                 $payOfInvoiceDeposit = $em->getRepository(ReservationPaymentsClient::class)->findOneByInvoiceId($item->getId());
  4874.                 if (!empty($payOfInvoiceDeposit)){
  4875.                     // Verificamos si el deposito esta dentro de una factura
  4876.                     $itemInvoiceOfInvoiceDeposit = $em->getRepository(BlvReservationInvoiceItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4877.                 }
  4878.                 if (!empty($itemInvoiceOfInvoiceDeposit)){
  4879.                     $estaEnFactura = true;
  4880.                 } else {
  4881.                     // Verificamos que el control Id no haya sido seteado a 0 por rectificar la factura
  4882.                     if (!empty($payOfInvoiceDeposit)) {
  4883.                         $itemInvoiceOfInvoiceDeposit = $em->getRepository(BlvReservationInvoiceRecItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4884.                         if (!empty($itemInvoiceOfInvoiceDeposit)) {
  4885.                             $estaEnFactura = true;
  4886.                         }
  4887.                     }
  4888.                 }
  4889.                 if (!empty($payOfInvoiceDeposit)){
  4890.                     // Verificamos si el deposito esta dentro de una factura rectificativa
  4891.                     $itemInvoiceOfInvoiceDepositRec = $em->getRepository(BlvReservationInvoiceRecItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4892.                 }
  4893.                 if (!empty($itemInvoiceOfInvoiceDepositRec)){
  4894.                     $estaEnFacturaRectificativa = true;
  4895.                 }
  4896.                 // FIN: Verificamos factura de deposito dentro de alguna factura
  4897.                 $invoicedDepositItems = $em->getRepository(ReservationInvoiceDepositItems::class)->findByControlId($item->getMaster());
  4898.                 foreach ($invoicedDepositItems as $itemDep){
  4899.                         $invoiced[$idArray]->setTotalNet($invoiced[$idArray]->getTotalNet() + $itemDep->getAmount());
  4900.                         $invoiced[$idArray]->setVat($invoiced[$idArray]->getVat() + (($itemDep->getAmount() * $itemDep->getIva())/100));
  4901.                         $invoiced[$idArray]->setTotal($invoiced[$idArray]->getTotal() + ($itemDep->getAmount() + (($itemDep->getAmount() * $itemDep->getIva())/100)));
  4902.                 }
  4903.             }
  4904.             // Sumara cuando ambos esten en false (no esta en factura ni en fact rect) o en true en ambas (fue facturado pero luego rectificado)
  4905.             if ((!$estaEnFactura and !$estaEnFacturaRectificativa) or ($estaEnFactura and $estaEnFacturaRectificativa)) {
  4906.                 $sumatoriaTotalNet = $sumatoriaTotalNet + $item->getTotalNet();
  4907.                 $sumatoriaTotalVat = $sumatoriaTotalVat + $item->getVat();
  4908.                 $sumatoriaTotal = $sumatoriaTotal + $item->getTotal();
  4909.             }
  4910.         }
  4911.         $toXls = $request->request->get('boolToXls');
  4912.         if (!empty($toXls)){
  4913.             $id = 0;
  4914.             // Solicita el servicio de excel
  4915.             $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
  4916.             $phpExcelObject->getProperties()->setCreator("InOut Travel & Events")
  4917.                 ->setLastModifiedBy("InOut Travel & Events")
  4918.                 ->setTitle("InOut Travel & Events")
  4919.                 ->setSubject("InOut Travel & Events")
  4920.                 ->setDescription("InOut Travel & Events, generado usando clases de PHP")
  4921.                 ->setKeywords("office 2005 openxml php")
  4922.                 ->setCategory("Archivo de ejemplo");
  4923.             $fill = new PHPExcel_Style_Fill();
  4924.             $numberFormat = new PHPExcel_Style_NumberFormat();
  4925.             $alignment = new PHPExcel_Style_Alignment();
  4926.             $i = 6;
  4927.             $indiceInicial = 7;
  4928.             $indiceFinal = 7;
  4929.             $activesheet = $phpExcelObject->getActiveSheet();
  4930.             $drawingobject = $this->get('phpexcel')->createPHPExcelWorksheetDrawing();
  4931.             $drawingobject->setName('Image name');
  4932.             $drawingobject->setDescription('Image description');
  4933.             $drawingobject->setPath(getcwd().'/assets/images/logo/logo_green_patio.png');
  4934.             $drawingobject->setHeight(60);
  4935.             $drawingobject->setOffsetY(20);
  4936.             $drawingobject->setCoordinates('Q1');
  4937.             $drawingobject->setWorksheet($activesheet);
  4938.             $phpExcelObject
  4939.                 ->getActiveSheet()
  4940.                 ->getStyle('B3:O3')
  4941.                 ->getFill()
  4942.                 ->setFillType($fill::FILL_SOLID)
  4943.                 ->getStartColor()
  4944.                 ->setRGB('9fc4a6');
  4945.             $phpExcelObject->setActiveSheetIndex(0)
  4946.                 ->setCellValue('C3','LISTADO DE FACTURACION')
  4947.             ;
  4948.             $phpExcelObject->setActiveSheetIndex(0)
  4949.                 ->setCellValue('B'.$i, 'Fecha')
  4950.                 ->setCellValue('D'.$i, 'Número')
  4951.                 ->setCellValue('F'.$i, 'Expediente')
  4952.                 ->setCellValue('H'.$i, 'Nombre del Evento')
  4953.                 ->setCellValue('L'.$i, 'Cliente')
  4954.                 ->setCellValue('O'.$i, 'Tipo')
  4955.                 ->setCellValue('Q'.$i, 'Neto')
  4956.                 ->setCellValue('S'.$i, 'Total')
  4957.             ;
  4958.             $data = [];
  4959.             $data = $invoiced;
  4960.             foreach ($data as $item) {
  4961.                 $time = $item->getDateAt()->format('d/m/Y');
  4962.                 $idExp = ($item->getType() == 'Invoice Rec') ? 'RL'.$item->getId() : 'L'.$item->getId();
  4963.                 $clientTypeText = ($item->getClientType() == 'Client') ? 'Cliente' : 'Proveedor';
  4964.                 $phpExcelObject->setActiveSheetIndex(0)
  4965.                     ->setCellValue('B' . ($i + 1), $time)
  4966.                     ->setCellValue('D' . ($i + 1), $idExp)
  4967.                     ->setCellValue('F' . ($i + 1), $item->getReservationId())
  4968.                     ->setCellValue('H' . ($i + 1), $item->getNumber())
  4969.                     ->setCellValue('L' . ($i + 1), $item->getClientName())
  4970.                     ->setCellValue('O' . ($i + 1), $clientTypeText)
  4971.                     ->setCellValue('Q' . ($i + 1), $item->getTotalNet())
  4972.                     ->setCellValue('S' . ($i + 1), $item->getTotal())
  4973.                 ;
  4974.                 $i++;
  4975.                 $indiceFinal++;
  4976.             }
  4977.             $indiceFinal--;
  4978.             $indicesResta = $indiceFinal - $indiceInicial + 1;
  4979.             $phpExcelObject
  4980.                 ->getActiveSheet()
  4981.                 ->getStyle('Q' . ($indiceFinal + 3) . ':' . 'S' . ($indiceFinal + 3))
  4982.                 ->getFill()
  4983.                 ->setFillType($fill::FILL_SOLID)
  4984.                 ->getStartColor()
  4985.                 ->setRGB('9fc4a6');
  4986.             $phpExcelObject->setActiveSheetIndex(0)
  4987.                 ->setCellValue('Q' . ($indiceFinal + 3), 'Total Neto')
  4988.                 ->setCellValue('S' . ($indiceFinal + 3), 'Total');
  4989.             $phpExcelObject->setActiveSheetIndex(0)
  4990.                 ->setCellValue('Q' . ($indiceFinal + 4), '=SUM(' . 'Q' . $indiceInicial . ' : Q' . $indiceFinal . ')')
  4991.                 ->setCellValue('S' . ($indiceFinal + 4), '=SUM(' . 'S' . $indiceInicial . ' : U' . $indiceFinal . ')');
  4992.             $phpExcelObject->getActiveSheet()->setTitle('Estadísticas por sala');
  4993.             // Define el indice de página al número 1, para abrir esa página al abrir el archivo
  4994.             $phpExcelObject->setActiveSheetIndex(0);
  4995.             // Creamos pagina de Datos en bruto para Esteban Rincon
  4996.             // Add new sheet
  4997.             $objWorkSheet = $phpExcelObject->createSheet(1); //Setting index when creating
  4998.             $phpExcelObject->setActiveSheetIndex(1);
  4999.             $phpExcelObject->getActiveSheet()->setTitle('Datos en bruto');
  5000.             $x = 1;
  5001.             $phpExcelObject->setActiveSheetIndex(1)
  5002.                 ->setCellValue('A'.$x, 'Fecha')
  5003.                 ->setCellValue('B'.$x, 'Número')
  5004.                 ->setCellValue('C'.$x, 'Expediente')
  5005.                 ->setCellValue('D'.$x, 'Nombre del Evento')
  5006.                 ->setCellValue('E'.$x, 'Cliente')
  5007.                 ->setCellValue('F'.$x, 'Tipo')
  5008.                 ->setCellValue('G'.$x, 'Neto')
  5009.                 ->setCellValue('H'.$x, 'Total')
  5010.             ;
  5011.             foreach ($data as $item) {
  5012.                 $time = $item->getDateAt()->format('d/m/Y');
  5013.                 $idExp = ($item->getType() == 'Invoice Rec') ? 'RL'.$item->getId() : 'L'.$item->getId();
  5014.                 $clientTypeText = ($item->getClientType() == 'Client') ? 'Cliente' : 'Proveedor';
  5015.                 $phpExcelObject->setActiveSheetIndex(1)
  5016.                     ->setCellValue('A' . ($x + 1), $time)
  5017.                     ->setCellValue('B' . ($x + 1), $idExp)
  5018.                     ->setCellValue('C' . ($x + 1), $item->getReservationId())
  5019.                     ->setCellValue('D' . ($x + 1), $item->getNumber())
  5020.                     ->setCellValue('E' . ($x + 1), $item->getClientName())
  5021.                     ->setCellValue('F' . ($x + 1), $clientTypeText)
  5022.                     ->setCellValue('G' . ($x + 1), $item->getTotalNet())
  5023.                     ->setCellValue('H' . ($x + 1), $item->getTotal())
  5024.                 ;
  5025.                 $x++;
  5026.             }
  5027.             $phpExcelObject->setActiveSheetIndex(0);
  5028.             // Crea el writer
  5029.             $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
  5030.             // Envia la respuesta del controlador
  5031.             $response = $this->get('phpexcel')->createStreamedResponse($writer);
  5032.             // Agrega los headers requeridos
  5033.             $dispositionHeader = $response->headers->makeDisposition(
  5034.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  5035.                 'InOut_Travel_Events'.'.xlsx'
  5036.             );
  5037.             $response->headers->set('Content-Type', 'application/vnd.ms-excel; charset=utf-8');
  5038.             $response->headers->set('Pragma', 'public');
  5039.             $response->headers->set('Cache-Control', 'maxage=1');
  5040.             $response->headers->set('Content-Disposition', $dispositionHeader);
  5041.             $phpExcelObject->getActiveSheet()->setTitle('Facturación Bella View');
  5042.             // Define el indice de página al número 1, para abrir esa página al abrir el archivo
  5043.             $phpExcelObject->setActiveSheetIndex(0);
  5044.             // Crea el writer
  5045.             $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
  5046.             // Envia la respuesta del controlador
  5047.             $response = $this->get('phpexcel')->createStreamedResponse($writer);
  5048.             // Agrega los headers requeridos
  5049.             $dispositionHeader = $response->headers->makeDisposition(
  5050.                 ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  5051.                 'Green Patio - Listado Facturacion Legends.xlsx'
  5052.             );
  5053.             $response->headers->set('Content-Type', 'application/vnd.ms-excel; charset=utf-8');
  5054.             $response->headers->set('Pragma', 'public');
  5055.             $response->headers->set('Cache-Control', 'maxage=1');
  5056.             $response->headers->set('Content-Disposition', $dispositionHeader);
  5057.             return $response;
  5058.         }
  5059.         return $this->render('MDS/GreenPatioBundle/blv/list-reservations-invoice.html.twig',
  5060.             array(
  5061.                 'invocied' => $invoiced,
  5062.                 'sumatoriaTotalNet' => $sumatoriaTotalNet,
  5063.                 'sumatoriaTotalVat' => $sumatoriaTotalVat,
  5064.                 'sumatoriaTotal' => $sumatoriaTotal,
  5065.             )
  5066.         );
  5067.     }
  5068.     private function clientDataToInvoice($invoice, $typeClient, $idClient, $reservationId){
  5069.         //Recibe una ReservationInvoice y se le agregan los datos del cliente
  5070.         $em = $this->getDoctrine()->getManager();
  5071.         $address = null; $name = '';
  5072.         if (empty($invoice)){
  5073.             // Venimos de un selector y vamos a cotizar
  5074.             $reserva = $em->getRepository(Reservation::class)->findOneById($reservationId);
  5075.             $typeClient = 'Client'; $idClient = $reserva->getClient();
  5076.             $invoice = new BlvReservationInvoice();
  5077.         }
  5078.         if ($typeClient == 'Client'){
  5079.             $client = $em->getRepository(Client::class)->findOneById($idClient); $typeClient = 'Client';
  5080.             if (!empty($client)){ $name = $client->getName(); $address = $client->getAddress() . ' ' . $client->getAddressNumber(); }
  5081.         } else {
  5082.             // Proveedor (Para facturas de comision)
  5083.             $client = $em->getRepository(Supplier::class)->findOneById($idClient); $typeClient = 'Supplier';
  5084.             if (!empty($client)){ $name = $client->getCompany(); $address = $client->getAddress(); }
  5085.         }
  5086.         if(empty($client)){
  5087.             // No se encontro el cliente o proveedor, se utilizará por defecto el de la reserva
  5088.             $reserva = $em->getRepository(Reservation::class)->findOneById($invoice->getReservationId()); $typeClient = 'Client';
  5089.             $client = $em->getRepository(Client::class)->findOneById($reserva->getClient());
  5090.             if (!empty($client)){ $name = $client->getName(); $address = $client->getAddress() . ' ' . $client->getAddressNumber(); }
  5091.         }
  5092.         $region = $em->getRepository(Regions::class)->findOneById($client->getRegion());
  5093.         $country = $em->getRepository(Country::class)->findOneById($client->getCountry());
  5094.         $address = (empty($client->getRegion())) ? $address : $address. ', ' . $region->getRegion();
  5095.         $address = (empty($client->getZipCode())) ? $address : $address. ' - ' . $client->getZipCode();
  5096.         $address = (empty($client->getCountry())) ? $address : $address. ', ' . $country->getCountry();
  5097.         $invoice->setClientAddress($address);
  5098.         $invoice->setClientName($name);
  5099.         $clientDocument = null;
  5100.         $clientDocument = (empty($client->getTypeDocument())) ? $clientDocument : $client->getTypeDocument();
  5101.         $clientDocument = (empty($client->getIdDocument())) ? ' ' : $clientDocument. ': ' . $client->getIdDocument();
  5102.         $invoice->setClientDocument($clientDocument);
  5103.         $invoice->setClientType($typeClient);
  5104.         return $invoice;
  5105.     }
  5106. }