src/MDS/DevelupBundle/Controller/InvoiceController.php line 858

Open in your IDE?
  1. <?php
  2. namespace App\MDS\DevelupBundle\Controller;
  3. use App\Entity\Cities;
  4. use App\Entity\Client;
  5. use App\Entity\Configuration;
  6. use App\Entity\Country;
  7. use App\Entity\Regions;
  8. use App\Entity\SettingsCompany;
  9. use App\Entity\SupplierTechnology;
  10. use App\Entity\User;
  11. use App\MDS\DevelupBundle\Entity\MdvBriefings;
  12. use App\MDS\DevelupBundle\Entity\MdvBudgetPending;
  13. use App\MDS\DevelupBundle\Entity\MdvBudgetToProforma;
  14. use App\MDS\DevelupBundle\Entity\MdvDocInvoiceItems;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use DateTime;
  19. use App\MDS\DevelupBundle\Entity\MdvFiles;
  20. use App\MDS\DevelupBundle\Entity\MdvProduct;
  21. use App\MDS\DevelupBundle\Entity\MdvProductFile;
  22. use App\MDS\DevelupBundle\Entity\MdvPaymentsClient;
  23. use App\MDS\DevelupBundle\Entity\MdvDocProformaDeposit;
  24. use App\MDS\DevelupBundle\Entity\MdvDocInvoice;
  25. use App\MDS\DevelupBundle\Entity\MdvDocInvoiceRec;
  26. use App\MDS\DevelupBundle\Entity\MdvDocProformaItems;
  27. use App\MDS\DevelupBundle\Entity\MdvDocProforma;
  28. use App\MDS\DevelupBundle\Entity\MdvDocInvoiceRecItems;
  29. use App\MDS\DevelupBundle\Entity\MdvServices;
  30. use App\MDS\DevelupBundle\Entity\MdvTelegramUser;
  31. use App\MDS\EventsBundle\Entity\ProposalAgents;
  32. use App\MDS\EventsBundle\Entity\ProposalSupplierServices;
  33. use Swift_Mailer;
  34. use Swift_Message;
  35. use Swift_SmtpTransport;
  36. use Symfony\Contracts\Translation\TranslatorInterface;
  37. class InvoiceController extends AbstractController
  38. {
  39.     private $translator;
  40.     public function __construct(TranslatorInterface $translator) {
  41.         $this->translator = $translator;
  42.     }
  43.     
  44.     /**
  45.      * @Route("/listpaydep/{id}",  name="mdv_list_payments_deposits")
  46.      * Listar pagos y depositos de un expediente
  47.      */
  48.     public function listPaymentsDespositsAction( $id, Request $request){
  49.         $em = $this->getDoctrine()->getManager();
  50.         $paymentsInFile = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  51.         $proformaDepositsInFile = $em->getRepository(MdvDocProformaDeposit::class)->findByFileId($id);
  52.         $proformas = array();
  53.         $allProformas = $em->getRepository(MdvDocProforma::class)->findByFileId($id);
  54.         foreach ($allProformas as $item){
  55.             $data = $this->baseInvoiceDoneFileTwo($id, $item->getId(),'P');
  56.             $bases = 0;
  57.             if (is_numeric($data['totales']) and is_numeric($data['totales_neto'])){$bases = $data['totales'] - $data['totales_neto'];}
  58.             $proformas[] = array(
  59.                 'id' => $data['number'],
  60.                 'date' => $data['date'],
  61.                 'neto' => $data['totales_neto'],
  62.                 'bases_imponibles' => $bases,
  63.                 'total' => $data['totales'],
  64.             );
  65.         }
  66.         return $this->render('MDS/DevelupBundle/Develup/list-payments-deposits.html.twig',
  67.             array(
  68.                 'id' => $id,
  69.                 'proformas' => $proformas,
  70.                 'payments' => $paymentsInFile,
  71.                 'allInvoiceDeposit' => null,
  72.                 'proformaDepositsInFile' => $proformaDepositsInFile,
  73.             )
  74.         );
  75.     }
  76.     /**
  77.      * @Route("/paymentadd/{id}",  name="mdv_add_payment")
  78.      * Agregar pago a un expediente
  79.      */
  80.     public function addPaymentsAction( $id, Request $request)
  81.     {
  82.         $em = $this->getDoctrine()->getManager();
  83.         $newRequest = $request->request->get('invoicepay');
  84.         /* Obtengo usuario logueado */
  85.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  86.         $user_id = $user_logueado->getId();
  87.         if(!empty($newRequest['amount']) and !empty($newRequest['datePayAt']) and !empty($newRequest['wayToPay'])){
  88.             if (is_numeric($newRequest['amount'])){
  89.                 $pay = new MdvPaymentsClient();
  90.                 $pay->setAmount($newRequest['amount']);
  91.                 $pay->setConcept($newRequest['concept']);
  92.                 $pay->setDatePayAt(new \DateTime($newRequest['datePayAt']));
  93.                 $pay->setWayToPay($newRequest['wayToPay']);
  94.                 $pay->setFileId($id);
  95.                 $pay->setUpdatedId($user_id);
  96.                 $pay->setUpdatedAt(new \DateTime("now"));
  97.                 $pay->setCreatedId($user_id);
  98.                 $pay->setCreatedAt(new \DateTime("now"));
  99.                 $em->persist($pay);
  100.                 $em->flush();
  101.             }
  102.         }
  103.         return $this->redirectToRoute('mdv_list_payments_deposits',
  104.             array(
  105.                 'id' => $id
  106.             )
  107.         );
  108.     }
  109.     /**
  110.      * @Route("/paymentdelete/{id}",  name="mdv_delete_payment")
  111.      * Eliminar pago de un expediente
  112.      */
  113.     public function deletePaymentsAction( $id, Request $request)
  114.     {
  115.         $em = $this->getDoctrine()->getManager();
  116.         $payment = $em->getRepository(MdvPaymentsClient::class)->findOneById($id);
  117.         $fileId = $payment->getFileId();
  118.         $em->remove($payment);
  119.         $em->flush();
  120.         return $this->redirectToRoute('mdv_list_payments_deposits',
  121.             array(
  122.                 'id' => $fileId
  123.             )
  124.         );
  125.     }
  126.     /**
  127.      * @Route("/newproformadeposit/{id}",  name="mdv_new_proforma_deposit")
  128.      * Crear nueva proforma de deposito en un expediente
  129.      */
  130.     public function newProformaDepositAction( $id, Request $request)
  131.     {
  132.         $em = $this->getDoctrine()->getManager();
  133.         $newRequest = $request->request->get('invoicedep');
  134.         /* Obtengo usuario logueado */
  135.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  136.         $user_id = $user_logueado->getId();
  137.         $proforma = new MdvDocProformaDeposit();
  138.         if(!empty($newRequest['amount']) and !empty($newRequest['name']) and !empty($newRequest['iva'])){
  139.             if (is_numeric($newRequest['amount']) and is_numeric($newRequest['iva'])){
  140.                 $proforma->setAmount($newRequest['amount']);
  141.                 $proforma->setName($newRequest['name']);
  142.                 $proforma->setFileId($id);
  143.                 $proforma->setIva($newRequest['iva']);
  144.                 $proforma->setTotal($newRequest['amount'] + ($newRequest['amount']*($newRequest['iva']/100)));
  145.                 $proforma->setUpdatedId($user_id);
  146.                 $proforma->setUpdatedAt(new \DateTime("now"));
  147.                 $proforma->setCreatedId($user_id);
  148.                 $proforma->setCreatedAt(new \DateTime("now"));
  149.                 $em->persist($proforma);
  150.                 $em->flush();
  151.             }
  152.         } else {
  153.             // Falta algun dato
  154.             return $this->redirectToRoute('mdv_list_payments_deposits', array( 'id' => $id));
  155.         }
  156.         return $this->redirectToRoute('mdv_view_proforma_deposit',
  157.             array(
  158.                 'id' => $proforma->getId()
  159.             )
  160.         );
  161.     }
  162.     /**
  163.      * @Route("/viewproformadeposit/{id}",  name="mdv_view_proforma_deposit")
  164.      * Ver una proforma de deposito
  165.      */
  166.     public function viewProformaDepositAction( $id, Request $request)
  167.     {
  168.         $docType = 'Proforma de Deposito';
  169.         $em = $this->getDoctrine()->getManager();
  170.         $proforma =  $em->getRepository(MdvDocProformaDeposit::class)->findOneById($id);
  171.         $id = $proforma->getFileId();
  172.         /* Obtengo usuario logueado */
  173.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  174.         $user_id = $user_logueado->getId();
  175.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  176.         // Buscamos el cliente
  177.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  178.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  179.         if (empty($client)){
  180.             $client[0] = new Client();
  181.             $client[0]->setName('');
  182.             $client[0]->setTitle('');
  183.             $client[0]->setIdDocument('');
  184.             $client[0]->setPopulation('');
  185.             $client[0]->setRegion('');
  186.             $client[0]->setCountry('');
  187.         } else {
  188.             if (is_numeric($client[0]->getPopulation())){
  189.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  190.                 $client[0]->setPopulation($city->getCity());
  191.             }
  192.             if (is_numeric($client[0]->getRegion())){
  193.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  194.                 $client[0]->setRegion($region->getRegion());
  195.             }
  196.             if (is_numeric($client[0]->getCountry())){
  197.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  198.                 $client[0]->setCountry($country->getCountry());
  199.             }
  200.         }
  201.         // Buscamos el cliente
  202.         $bases_imponibles['ivaMontoVeintiUno'] = $proforma->getAmount()*0.21;
  203.         $allProformas =  $em->getRepository(MdvDocProformaDeposit::class)->findByFileId($id);
  204.         // Falta agregar en $allProformas las proformas de facturas, hasta ahora solo estan las proformas de deposito  //6*6*6
  205.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  206.             array(
  207.                 'id' => $id,
  208.                 'docType' => $docType,
  209.                 'proforma' => $proforma,
  210.                 'company' => $company,
  211.                 'clients' => $client,
  212.                 'date' => $proforma->getCreatedAt(),
  213.                 'number' => $proforma->getId(),
  214.                 'invoice' => null,
  215.                 'fid' => $proforma->getId(),
  216.                 'boolToRec' => false,
  217.                 'proformas' => $allProformas,
  218.                 'currency' => '€',
  219.                 'totales_neto' => $proforma->getAmount(),
  220.                 'totales' => $proforma->getTotal(),
  221.                 'balance' => $proforma->getTotal(),
  222.                 'bases_imponibles' => $bases_imponibles,
  223.                 'services' => null,
  224.                 'facturas' => null,
  225.                 'paymentNotIvoiced' => null,
  226.                 'paymentsAll' => null,
  227.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  228.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  229.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  230.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  231.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  232.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  233.             ));
  234.     }
  235.     /**
  236.      * @Route("/dpexternal/printproformadeposit/{id}",  name="mdv_print_proforma_deposit")
  237.      * Imprimir una proforma de deposito
  238.      */
  239.     public function printProformaDepositAction( $id, Request $request)
  240.     {
  241.         $docType = 'Proforma de Deposito';
  242.         $em = $this->getDoctrine()->getManager();
  243.         $proforma =  $em->getRepository(MdvDocProformaDeposit::class)->findOneById($id);
  244.         $id = $proforma->getFileId();
  245.         /* Obtengo usuario logueado */
  246. //        $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  247. //        $user_id = $user_logueado->getId();
  248.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  249.         // Buscamos el cliente
  250.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  251.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  252.         if (empty($client)){
  253.             $client[0] = new Client();
  254.             $client[0]->setName('');
  255.             $client[0]->setTitle('');
  256.             $client[0]->setIdDocument('');
  257.             $client[0]->setPopulation('');
  258.             $client[0]->setRegion('');
  259.             $client[0]->setCountry('');
  260.         } else {
  261.             if (is_numeric($client[0]->getPopulation())){
  262.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  263.                 $client[0]->setPopulation($city->getCity());
  264.             }
  265.             if (is_numeric($client[0]->getRegion())){
  266.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  267.                 $client[0]->setRegion($region->getRegion());
  268.             }
  269.             if (is_numeric($client[0]->getCountry())){
  270.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  271.                 $client[0]->setCountry($country->getCountry());
  272.             }
  273.         }
  274.         // Buscamos el cliente
  275.         $bases_imponibles['ivaMontoVeintiUno'] = $proforma->getAmount()*0.21;
  276.         $allProformas =  $em->getRepository(MdvDocProformaDeposit::class)->findByFileId($id);
  277.         // Falta agregar en $allProformas las proformas de facturas, hasta ahora solo estan las proformas de deposito  //6*6*6
  278.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  279.             array(
  280.                 'id' => $id,
  281.                 'docType' => $docType,
  282.                 'proforma' => $proforma,
  283.                 'company' => $company,
  284.                 'clients' => $client,
  285.                 'date' => $proforma->getCreatedAt(),
  286.                 'number' => $proforma->getId(),
  287.                 'invoice' => null,
  288.                 'fid' => $proforma->getId(),
  289.                 'boolToRec' => false,
  290.                 'proformas' => $allProformas,
  291.                 'currency' => '€',
  292.                 'totales_neto' => $proforma->getAmount(),
  293.                 'totales' => $proforma->getTotal(),
  294.                 'balance' => $proforma->getTotal(),
  295.                 'bases_imponibles' => $bases_imponibles,
  296.                 'services' => null,
  297.                 'facturas' => null,
  298.                 'paymentNotIvoiced' => null,
  299.                 'paymentsAll' => null,
  300.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  301.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  302.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  303.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  304.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  305.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  306.             ));
  307.     }
  308.     /**
  309.      * @Route("/newinvoicedeposit/{id}",  name="mdv_new_invoice_deposit")
  310.      * Crear nueva factura de deposito en un expediente
  311.      */
  312.     public function newInvoiceDepositAction( $id, Request $request)
  313.     {
  314.         $docType = 'Factura de Deposito';
  315.         $em = $this->getDoctrine()->getManager();
  316.         $proforma =  $em->getRepository(MdvDocProformaDeposit::class)->findOneById($id);
  317.         $id = $proforma->getFileId();
  318.         /* Obtengo usuario logueado */
  319.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  320.         $user_id = $user_logueado->getId();
  321.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  322.         // Buscamos el cliente
  323.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  324.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  325.         if (empty($client)){
  326.             $client[0] = new Client();
  327.             $client[0]->setName('');
  328.             $client[0]->setTitle('');
  329.             $client[0]->setIdDocument('');
  330.             $client[0]->setPopulation('');
  331.             $client[0]->setRegion('');
  332.             $client[0]->setCountry('');
  333.         } else {
  334.             if (is_numeric($client[0]->getPopulation())){
  335.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  336.                 $client[0]->setPopulation($city->getCity());
  337.             }
  338.             if (is_numeric($client[0]->getRegion())){
  339.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  340.                 $client[0]->setRegion($region->getRegion());
  341.             }
  342.             if (is_numeric($client[0]->getCountry())){
  343.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  344.                 $client[0]->setCountry($country->getCountry());
  345.             }
  346.         }
  347.         // Buscamos el cliente
  348.         // Generamos la factura con la proforma especificada
  349.         $invoice = new MdvDocInvoice();
  350.         $invoice->setFileId($proforma->getFileId());
  351.         $invoice->setDateAt(new \DateTime("now"));
  352.         $invoice->setName($proforma->getName());
  353.         $invoice->setTotalNet($proforma->getAmount());
  354.         $invoice->setIva($proforma->getIva());
  355.         $invoice->setVat(($proforma->getAmount() * $proforma->getIva())/100);
  356.         $totalInv = $invoice->getTotalNet() + $invoice->getVat();
  357.         $invoice->setTotal($totalInv);
  358.         $invoice->setMaster(null);
  359.         $invoice->setNumber(null);
  360.         $invoice->setPrefix(null);
  361.         $invoice->setBalance($totalInv*(-1));
  362.         $invoice->setType('Factura de Deposito');
  363.         $invoice->setUpdatedId($user_id);
  364.         $invoice->setUpdatedAt(new \DateTime("now"));
  365.         $invoice->setCreatedId($user_id);
  366.         $invoice->setCreatedAt(new \DateTime("now"));
  367.         $em->persist($invoice);
  368.         $em->flush();
  369.         $bases_imponibles['ivaMontoVeintiUno'] = $proforma->getAmount()*0.21;
  370.         $allInvoices =  $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  371.         $allProformas = null;
  372.         // Una factura solo se puede rectificar una vez
  373.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('invoiceToRec' => $invoice->getId()));
  374.         $boolToRec = empty($rectf);
  375.         return $this->redirectToRoute('mdv_view_invoice_deposit',
  376.             array(
  377.                 'id' => $invoice->getId()
  378.             )
  379.         );
  380.     }
  381.     /**
  382.      * @Route("/viewinvoicedeposit/{id}",  name="mdv_view_invoice_deposit")
  383.      * Ver una factura de deposito en un expediente
  384.      */
  385.     public function viewInvoiceDepositAction( $id, Request $request)
  386.     {
  387.         $docType = 'Factura de Deposito';
  388.         $em = $this->getDoctrine()->getManager();
  389.         $invoice =  $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  390.         $id = $invoice->getFileId();
  391.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  392.         // Buscamos el cliente
  393.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  394.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  395.         if (empty($client)){
  396.             $client[0] = new Client();
  397.             $client[0]->setName('');
  398.             $client[0]->setTitle('');
  399.             $client[0]->setIdDocument('');
  400.             $client[0]->setPopulation('');
  401.             $client[0]->setRegion('');
  402.             $client[0]->setCountry('');
  403.         } else {
  404.             if (is_numeric($client[0]->getPopulation())){
  405.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  406.                 $client[0]->setPopulation($city->getCity());
  407.             }
  408.             if (is_numeric($client[0]->getRegion())){
  409.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  410.                 $client[0]->setRegion($region->getRegion());
  411.             }
  412.             if (is_numeric($client[0]->getCountry())){
  413.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  414.                 $client[0]->setCountry($country->getCountry());
  415.             }
  416.         }
  417.         // Buscamos el cliente
  418.         $bases_imponibles['ivaMontoVeintiUno'] = $invoice->getTotalNet()*0.21;
  419.         $allInvoices =  $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  420.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($id);
  421.         foreach ($allInvoicesRec as $item) {
  422.             array_push($allInvoices, $item);
  423.         }
  424.         $allProformas = null;
  425.         // Una factura solo se puede rectificar una vez
  426.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('invoiceToRec' => $invoice->getId()));
  427.         $boolToRec = empty($rectf);
  428.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  429.             array(
  430.                 'id' => $id,
  431.                 'docType' => $docType,
  432.                 'proforma' => null,
  433.                 'company' => $company,
  434.                 'clients' => $client,
  435.                 'date' => $invoice->getDateAt(),
  436.                 'number' => $invoice->getId(),
  437.                 'invoice' => $invoice,
  438.                 'fid' => $invoice->getId(),
  439.                 'boolToRec' => $boolToRec,
  440.                 'proformas' => $allProformas,
  441.                 'facturas' => $allInvoices,
  442.                 'currency' => '€',
  443.                 'totales_neto' => $invoice->getTotalNet(),
  444.                 'totales' => $invoice->getTotal(),
  445.                 'balance' => $invoice->getTotal(),
  446.                 'bases_imponibles' => $bases_imponibles,
  447.                 'services' => null,
  448. //                'facturas' => null,
  449.                 'paymentNotIvoiced' => null,
  450.                 'paymentsAll' => null,
  451.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  452.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  453.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  454.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  455.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  456.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  457.             ));
  458.     }
  459.     /**
  460.      * @Route("/dpexternal/printinvoicedeposit/{id}",  name="mdv_print_invoice_deposit")
  461.      * Imprimir una factura de deposito
  462.      */
  463.     public function printInvoiceDepositAction( $id, Request $request)
  464.     {
  465.         $docType = 'Factura de Deposito';
  466.         $em = $this->getDoctrine()->getManager();
  467.         $invoice =  $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  468.         $id = $invoice->getFileId();
  469.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  470.         // Buscamos el cliente
  471.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  472.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  473.         if (empty($client)){
  474.             $client[0] = new Client();
  475.             $client[0]->setName('');
  476.             $client[0]->setTitle('');
  477.             $client[0]->setIdDocument('');
  478.             $client[0]->setPopulation('');
  479.             $client[0]->setRegion('');
  480.             $client[0]->setCountry('');
  481.         } else {
  482.             if (is_numeric($client[0]->getPopulation())){
  483.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  484.                 $client[0]->setPopulation($city->getCity());
  485.             }
  486.             if (is_numeric($client[0]->getRegion())){
  487.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  488.                 $client[0]->setRegion($region->getRegion());
  489.             }
  490.             if (is_numeric($client[0]->getCountry())){
  491.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  492.                 $client[0]->setCountry($country->getCountry());
  493.             }
  494.         }
  495.         // Buscamos el cliente
  496.         $bases_imponibles['ivaMontoVeintiUno'] = $invoice->getTotalNet()*0.21;
  497.         $allInvoices =  $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  498.         $allProformas = null;
  499.         // Una factura solo se puede rectificar una vez
  500.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('invoiceToRec' => $invoice->getId()));
  501.         $boolToRec = empty($rectf);
  502.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  503.             array(
  504.                 'id' => $id,
  505.                 'docType' => $docType,
  506.                 'proforma' => null,
  507.                 'company' => $company,
  508.                 'clients' => $client,
  509.                 'date' => $invoice->getDateAt(),
  510.                 'number' => $invoice->getId(),
  511.                 'invoice' => $invoice,
  512.                 'fid' => $invoice->getId(),
  513.                 'boolToRec' => $boolToRec,
  514.                 'proformas' => $allProformas,
  515.                 'facturas' => $allInvoices,
  516.                 'currency' => '€',
  517.                 'totales_neto' => $invoice->getTotalNet(),
  518.                 'totales' => $invoice->getTotal(),
  519.                 'balance' => $invoice->getTotal(),
  520.                 'bases_imponibles' => $bases_imponibles,
  521.                 'services' => null,
  522. //                'facturas' => null,
  523.                 'paymentNotIvoiced' => null,
  524.                 'paymentsAll' => null,
  525.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  526.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  527.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  528.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  529.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  530.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  531.             ));
  532.     }
  533.     /**
  534.      * @Route("/newinvoicedepositrec/{id}",  name="mdv_invoice_deposit_rec")
  535.      * Crear nueva factura rectificativa de deposito
  536.      */
  537.     public function newInvoiceDepositRecAction( $id, Request $request)
  538.     {
  539.         $docType = 'Factura Rectificativa de Deposito';
  540.         $em = $this->getDoctrine()->getManager();
  541.         if (true){
  542.             // Verificamos que no exista una factura rectificativa previa sobre la misma factura
  543.             $previusInvoiceRec = $em->getRepository(MdvDocInvoiceRec::class)->findOneByInvoiceToRec($id);
  544.             if (!empty($previusInvoiceRec)){ return $this->redirectToRoute('mdv_view_invoice_deposit_rec', array('id' => $previusInvoiceRec->getId())); }
  545.         }
  546.         $invoice =  $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  547.         $id = $invoice->getFileId();
  548.         /* Obtengo usuario logueado */
  549.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  550.         $user_id = $user_logueado->getId();
  551.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  552.         // Buscamos el cliente
  553.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  554.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  555.         if (empty($client)){
  556.             $client[0] = new Client();
  557.             $client[0]->setName('');
  558.             $client[0]->setTitle('');
  559.             $client[0]->setIdDocument('');
  560.             $client[0]->setPopulation('');
  561.             $client[0]->setRegion('');
  562.             $client[0]->setCountry('');
  563.         } else {
  564.             if (is_numeric($client[0]->getPopulation())){
  565.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  566.                 $client[0]->setPopulation($city->getCity());
  567.             }
  568.             if (is_numeric($client[0]->getRegion())){
  569.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  570.                 $client[0]->setRegion($region->getRegion());
  571.             }
  572.             if (is_numeric($client[0]->getCountry())){
  573.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  574.                 $client[0]->setCountry($country->getCountry());
  575.             }
  576.         }
  577.         // Buscamos el cliente
  578.         // Generamos la factura con la proforma especificada
  579.         $invoiceRec = new MdvDocInvoiceRec();
  580.         $invoiceRec->setFileId($invoice->getFileId());
  581.         $invoiceRec->setInvoiceToRec($invoice->getId());
  582.         $invoiceRec->setDateAt(new \DateTime("now"));
  583.         $invoiceRec->setName($invoice->getName());
  584.         $invoiceRec->setTotalNet($invoice->getTotalNet()*(-1));
  585.         $invoiceRec->setIva($invoice->getIva());
  586.         $invoiceRec->setVat((($invoice->getTotalNet() * $invoice->getIva())/100)*(-1));
  587.         $totalInv = $invoice->getTotalNet() + $invoice->getVat();
  588.         $invoiceRec->setTotal($totalInv * (-1));
  589.         $invoiceRec->setMaster(null);
  590.         $invoiceRec->setNumber(null);
  591.         $invoiceRec->setPrefix(null);
  592.         $invoiceRec->setBalance($totalInv);
  593.         $invoiceRec->setType('Factura de Deposito');
  594.         $invoiceRec->setUpdatedId($user_id);
  595.         $invoiceRec->setUpdatedAt(new \DateTime("now"));
  596.         $invoiceRec->setCreatedId($user_id);
  597.         $invoiceRec->setCreatedAt(new \DateTime("now"));
  598.         $em->persist($invoiceRec);
  599.         $em->flush();
  600.         return $this->redirectToRoute('mdv_view_invoice_deposit_rec', array('id' => $invoiceRec->getId()));
  601.     }
  602.     /**
  603.      * @Route("/viewinvoicedepositrec/{id}",  name="mdv_view_invoice_deposit_rec")
  604.      * Ver una factura rectificativa de deposito
  605.      */
  606.     public function viewInvoiceDepositRecAction( $id, Request $request)
  607.     {
  608.         $docType = 'Factura Rectificativa de Deposito';
  609.         $em = $this->getDoctrine()->getManager();
  610.         $invoice =  $em->getRepository(MdvDocInvoiceRec::class)->findOneById($id);
  611.         $id = $invoice->getFileId();
  612.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  613.         // Buscamos el cliente
  614.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  615.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  616.         if (empty($client)){
  617.             $client[0] = new Client();
  618.             $client[0]->setName('');
  619.             $client[0]->setTitle('');
  620.             $client[0]->setIdDocument('');
  621.             $client[0]->setPopulation('');
  622.             $client[0]->setRegion('');
  623.             $client[0]->setCountry('');
  624.         } else {
  625.             if (is_numeric($client[0]->getPopulation())){
  626.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  627.                 $client[0]->setPopulation($city->getCity());
  628.             }
  629.             if (is_numeric($client[0]->getRegion())){
  630.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  631.                 $client[0]->setRegion($region->getRegion());
  632.             }
  633.             if (is_numeric($client[0]->getCountry())){
  634.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  635.                 $client[0]->setCountry($country->getCountry());
  636.             }
  637.         }
  638.         // Buscamos el cliente
  639.         $bases_imponibles['ivaMontoVeintiUno'] = $invoice->getTotalNet()*0.21;
  640.         $allInvoices =  $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  641.         $allProformas = null;
  642.         // Una factura solo se puede rectificar una vez
  643. //        $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('invoiceToRec' => $invoice->getId()));
  644.         $boolToRec = false;
  645.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  646.             array(
  647.                 'id' => $id,
  648.                 'docType' => $docType,
  649.                 'proforma' => null,
  650.                 'company' => $company,
  651.                 'clients' => $client,
  652.                 'date' => $invoice->getDateAt(),
  653.                 'number' => $invoice->getId(),
  654.                 'invoice' => $invoice,
  655.                 'fid' => $invoice->getId(),
  656.                 'boolToRec' => $boolToRec,
  657.                 'proformas' => $allProformas,
  658.                 'facturas' => $allInvoices,
  659.                 'currency' => '€',
  660.                 'totales_neto' => $invoice->getTotalNet(),
  661.                 'totales' => $invoice->getTotal(),
  662.                 'balance' => $invoice->getTotal(),
  663.                 'bases_imponibles' => $bases_imponibles,
  664.                 'services' => null,
  665. //                'facturas' => null,
  666.                 'paymentNotIvoiced' => null,
  667.                 'paymentsAll' => null,
  668.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  669.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  670.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  671.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  672.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  673.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  674.             ));
  675.     }
  676.     /**
  677.      * @Route("/dpexternal/printinvoicedepositrec/{id}",  name="mdv_print_invoice_deposit_rec")
  678.      * Imprimir una factura rectificativa de deposito
  679.      */
  680.     public function printInvoiceDepositRecAction( $id, Request $request)
  681.     {
  682.         $docType = 'Factura Rectificativa de Deposito';
  683.         $em = $this->getDoctrine()->getManager();
  684.         $invoice =  $em->getRepository(MdvDocInvoiceRec::class)->findOneById($id);
  685.         $id = $invoice->getFileId();
  686.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  687.         // Buscamos el cliente
  688.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  689.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  690.         if (empty($client)){
  691.             $client[0] = new Client();
  692.             $client[0]->setName('');
  693.             $client[0]->setTitle('');
  694.             $client[0]->setIdDocument('');
  695.             $client[0]->setPopulation('');
  696.             $client[0]->setRegion('');
  697.             $client[0]->setCountry('');
  698.         } else {
  699.             if (is_numeric($client[0]->getPopulation())){
  700.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  701.                 $client[0]->setPopulation($city->getCity());
  702.             }
  703.             if (is_numeric($client[0]->getRegion())){
  704.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  705.                 $client[0]->setRegion($region->getRegion());
  706.             }
  707.             if (is_numeric($client[0]->getCountry())){
  708.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  709.                 $client[0]->setCountry($country->getCountry());
  710.             }
  711.         }
  712.         // Buscamos el cliente
  713.         $bases_imponibles['ivaMontoVeintiUno'] = $invoice->getTotalNet()*0.21;
  714.         $allInvoices =  $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  715.         $allProformas = null;
  716.         // Una factura solo se puede rectificar una vez
  717. //        $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('invoiceToRec' => $invoice->getId()));
  718.         $boolToRec = false;
  719.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  720.             array(
  721.                 'id' => $id,
  722.                 'docType' => $docType,
  723.                 'proforma' => null,
  724.                 'company' => $company,
  725.                 'clients' => $client,
  726.                 'date' => $invoice->getDateAt(),
  727.                 'number' => $invoice->getId(),
  728.                 'invoice' => $invoice,
  729.                 'fid' => $invoice->getId(),
  730.                 'boolToRec' => $boolToRec,
  731.                 'proformas' => $allProformas,
  732.                 'facturas' => $allInvoices,
  733.                 'currency' => '€',
  734.                 'totales_neto' => $invoice->getTotalNet(),
  735.                 'totales' => $invoice->getTotal(),
  736.                 'balance' => $invoice->getTotal(),
  737.                 'bases_imponibles' => $bases_imponibles,
  738.                 'services' => null,
  739. //                'facturas' => null,
  740.                 'paymentNotIvoiced' => null,
  741.                 'paymentsAll' => null,
  742.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  743.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  744.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  745.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  746.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  747.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  748.             ));
  749.     }
  750.     /**
  751.      * @Route("/selinvoice/{id}",  name="mdv_select_invoice")
  752.      * Seleccionar los items que pasaran a la proforma o factura
  753.      */
  754.     public function selectItemsInvoiceAction($id, Request $request)
  755.     {
  756.         $em = $this->getDoctrine()->getManager();
  757.         $paymentsClient = new MdvPaymentsClient();
  758.         $paymentsClient->setFileId($id);
  759.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  760.         $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  761.         $allProformas = $em->getRepository(MdvDocProforma::class)->findAll();
  762.         if (!empty($allProformas)){
  763.             $number = end($allProformas)->getId() + 1;
  764.         } else {
  765.             // Primer registro de proformas
  766.             $number = 1;
  767.         }
  768.         $type = 'Proforma';
  769.         $docType = 'Proforma';
  770.         $date = new DateTime('now');
  771.         $prefix = "MDV-".$date->format('dmy')."-";
  772.         $fileInv = new MdvDocInvoice();
  773.         $fileInv->setFileId($id);
  774.         $data = $this->baseInvoiceFile($id, $type, $number, $prefix, $date);
  775.         $token = $data['token'];
  776.         $idProducts = array();
  777.         if (array_key_exists('product',$data['datasupplier'])) {
  778.             foreach ($data['datasupplier']['product'] as $key => $item) {
  779.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneByPrdControlId($item['id']);
  780.                 if (empty($existe)){
  781.                     $data['datasupplier']['product'][$key]['enabledToInvoice'] = true;
  782.                 } else {
  783.                     $data['datasupplier']['product'][$key]['enabledToInvoice'] = false;
  784.                 }
  785.             }
  786.         }
  787.         if (array_key_exists('service',$data['datasupplier'])) {
  788.             foreach ($data['datasupplier']['service'] as $key => $item) {
  789.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneBySrvControlId($item['id']);
  790.                 if (empty($existe)){
  791.                     $data['datasupplier']['service'][$key]['enabledToInvoice'] = true;
  792.                 } else {
  793.                     $data['datasupplier']['service'][$key]['enabledToInvoice'] = false;
  794.                 }
  795.             }
  796.         }
  797.         $idPayments = array();
  798.         if (array_key_exists('payment',$data['datasupplier'])) {
  799.             foreach ($data['datasupplier']['payment'] as $key => $item) {
  800.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneByPayControlId($item['id']);
  801.                 if (empty($existe)){
  802.                     $data['datasupplier']['payment'][$key]['enabledToInvoice'] = true;
  803.                 } else {
  804.                     $data['datasupplier']['payment'][$key]['enabledToInvoice'] = false;
  805.                 }
  806.             }
  807.         }
  808.         /* Obtengo usuario logueado */
  809.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  810.         $user_id = $user_logueado->getId();
  811.         return $this->render('MDS/DevelupBundle/Develup/invoice-select-items.html.twig',
  812.             array(
  813.                 'id' => $id,
  814.                 'idLounges' => $idProducts,
  815.                 'file' => $file,
  816.                 'idPayments' => $idPayments,
  817.                 'type' => $data['type'],
  818.                 'docType' => $docType,
  819.                 'number' => $data['number'],
  820.                 'prefix' => $data['prefix'],
  821.                 'date' => $data['date'],
  822.                 'token' => $token,
  823.                 'company' => $data['company'],
  824.                 'clients' => $data['clients'],
  825.                 'datasupplier' => $data['datasupplier'],
  826.                 'currency' => $data['currency'],
  827.                 'totales_neto' => $data['totales_neto'],
  828.                 'bases_imponibles' => $data['bases_imponibles'],
  829.                 'totales' => $data['totales'],
  830.                 'balance' => $data['balance'],
  831.                 'paymentInvoice' => $data['paymentInvoice'],
  832.                 'payments' => $payments,
  833.                 'user_id' => $user_id,
  834.             )
  835.         );
  836.     }
  837.     /**
  838.      * @Route("/selproforma/{id}",  name="mdv_select_proforma")
  839.      * Seleccionar los items que pasaran a la proforma o factura a partir de los datos de una proforma
  840.      */
  841.     public function selectItemsProformaAction($id, Request $request)
  842.     {
  843.         $em = $this->getDoctrine()->getManager();
  844.         //Se cambia el ID de proforma a ID file
  845.         $proforma = $em->getRepository(MdvDocProforma::class)->findOneById($id);
  846.         $id = $proforma->getFileId();
  847.         $proformaId = $proforma->getId();
  848.         $paymentsClient = new MdvPaymentsClient();
  849.         $paymentsClient->setFileId($id);
  850.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  851.         $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  852.         $allProformas = $em->getRepository(MdvDocProforma::class)->findAll();
  853.         if (!empty($allProformas)){
  854.             $number = end($allProformas)->getId() + 1;
  855.         } else {
  856.             // Primer registro de proformas
  857.             $number = 1;
  858.         }
  859.         $type = 'Proforma';
  860.         $docType = 'Proforma';
  861.         $date = new DateTime('now');
  862.         $prefix = "MDV-".$date->format('dmy')."-";
  863.         $fileInv = new MdvDocInvoice();
  864.         $fileInv->setFileId($id);
  865.         $data = $this->baseProformaSelect($id, $type, $number, $prefix, $date, $proformaId);
  866.         $token = $data['token'];
  867.         $idProducts = array();
  868.         if (array_key_exists('product',$data['datasupplier'])) {
  869.             foreach ($data['datasupplier']['product'] as $key => $item) {
  870.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneByPrdControlId($item['id']);
  871.                 if (empty($existe)){
  872.                     $data['datasupplier']['product'][$key]['enabledToInvoice'] = true;
  873.                 } else {
  874.                     $data['datasupplier']['product'][$key]['enabledToInvoice'] = false;
  875.                 }
  876.             }
  877.         }
  878.         if (array_key_exists('service',$data['datasupplier'])) {
  879.             foreach ($data['datasupplier']['service'] as $key => $item) {
  880.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneBySrvControlId($item['id']);
  881.                 if (empty($existe)){
  882.                     $data['datasupplier']['service'][$key]['enabledToInvoice'] = true;
  883.                 } else {
  884.                     $data['datasupplier']['service'][$key]['enabledToInvoice'] = false;
  885.                 }
  886.             }
  887.         }
  888.         $idPayments = array();
  889.         if (array_key_exists('payment',$data['datasupplier'])) {
  890.             foreach ($data['datasupplier']['payment'] as $key => $item) {
  891.                 $existe = $em->getRepository(MdvDocInvoiceItems::class)->findOneByPayControlId($item['id']);
  892.                 if (empty($existe)){
  893.                     $data['datasupplier']['payment'][$key]['enabledToInvoice'] = true;
  894.                 } else {
  895.                     $data['datasupplier']['payment'][$key]['enabledToInvoice'] = false;
  896.                 }
  897.             }
  898.         }
  899.         /* Obtengo usuario logueado */
  900.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  901.         $user_id = $user_logueado->getId();
  902.         return $this->render('MDS/DevelupBundle/Develup/invoice-select-items.html.twig',
  903.             array(
  904.                 'id' => $id,
  905.                 'idLounges' => $idProducts,
  906.                 'file' => $file,
  907.                 'idPayments' => $idPayments,
  908.                 'type' => $data['type'],
  909.                 'docType' => $docType,
  910.                 'number' => $data['number'],
  911.                 'prefix' => $data['prefix'],
  912.                 'date' => $data['date'],
  913.                 'token' => $token,
  914.                 'company' => $data['company'],
  915.                 'clients' => $data['clients'],
  916.                 'datasupplier' => $data['datasupplier'],
  917.                 'currency' => $data['currency'],
  918.                 'totales_neto' => $data['totales_neto'],
  919.                 'bases_imponibles' => $data['bases_imponibles'],
  920.                 'totales' => $data['totales'],
  921.                 'balance' => $data['balance'],
  922.                 'paymentInvoice' => $data['paymentInvoice'],
  923.                 'payments' => $payments,
  924.                 'user_id' => $user_id,
  925.             )
  926.         );
  927.     }
  928.     /**
  929.      * @Route("/newmultiproforma/", name="mdv_new_multi_proforma")
  930.      * Crear nueva proforma con los elementos seleccionados y retornar al selector
  931.      */
  932.     public function generateNewMultiProformaAction(Request $request)
  933.     {
  934.         $em = $this->getDoctrine()->getManager();
  935.         // Se genera y muestra en nueva pantalla la nueva proforma
  936.         $arrayBools = array(
  937.             'product' => null,
  938.             'service' => null,
  939.             'payment' => null,
  940.         );
  941.         $arrayBools['product'] = $request->request->get('multi_lounge_hidden_bool');
  942.         $arrayBools['service'] = $request->request->get('multi_service_hidden_bool');
  943.         $arrayBools['payment'] = $request->request->get('multi_payment_hidden_bool');
  944.         $id = $request->request->get('multi_file_id');
  945.         $proforma_number = $request->request->get('multi_proforma_number');
  946.         $proforma_prefix = $request->request->get('multi_proforma_prefix') . $proforma_number . '-' . $id;
  947.         //INICIO: buscamos el ID de la factura
  948.         $boolMakeInvoice = false;  // Control para verificar si se debe hacer (PROFORMA) o todos los indicadores (sala, servicios y pagos) estaban en falso
  949.         //FIN: buscamos el ID de la factura
  950.         /* Obtengo usuario logueado */
  951.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  952.         $user_id = $user_logueado->getId();
  953.         //Acumuladores de datos globales de la factura
  954.         $valorTotalNet = 0;
  955.         $valorVat = 0;
  956.         //INICIO: Creamos los items de la proforma
  957.         if (!empty($arrayBools['product'])) {
  958.             foreach ($arrayBools['product'] as $key => $item) {
  959.                 if ($item == 'true') {
  960.                     $boolMakeInvoice = true;
  961.                     $product = $em->getRepository(MdvProductFile::class)->findOneById($key);
  962.                     $boolKeyChange = false;
  963.                     //INICIO: Creamos el nuevo ProductFile para evitar cruce de datos entre proformas
  964.                     // Si el producto esta contenido en una proforma debemos crear un nuevo porducto
  965.                     $productInProforma = $em->getRepository(MdvDocProformaItems::class)->findOneByControlId($key);
  966.                     if (!empty($productInProforma)){
  967.                         //INICIO: Si el producto esta sincronizado con un proposal debemos crear un servicio nuevo en el proposal
  968.                         $boolKeyChange = true;
  969.                         $newProductFile = new MdvProductFile();
  970.                         //INICIO: Duplicamos en Eventos
  971.                         if (!empty($product->getOriginId())) {
  972.                             $serviceEvent = $em->getRepository(ProposalSupplierServices::class)->findOneById($product->getOriginId());
  973.                             if (!empty($serviceEvent)){
  974.                                 $newServiceEvent = new ProposalSupplierServices();
  975.                                 $newServiceEvent->setServiceIdFather($serviceEvent->getServiceIdFather());
  976.                                 $newServiceEvent->setControlId($serviceEvent->getControlId());
  977.                                 $newServiceEvent->setProposalId($serviceEvent->getProposalId());
  978.                                 $newServiceEvent->setDestinationId($serviceEvent->getDestinationId());
  979.                                 $newServiceEvent->setSupplierId($serviceEvent->getSupplierId());
  980.                                 $newServiceEvent->setIdeaId($serviceEvent->getIdeaId());
  981.                                 $newServiceEvent->setActivityId($serviceEvent->getActivityId());
  982.                                 $newServiceEvent->setServiceId($serviceEvent->getServiceId());
  983.                                 $newServiceEvent->setServiceCatId($serviceEvent->getServiceCatId());
  984.                                 $newServiceEvent->setServiceCatName($serviceEvent->getServiceCatName());
  985.                                 $newServiceEvent->setName($serviceEvent->getName());
  986.                                 $newServiceEvent->setPrice($serviceEvent->getPrice());
  987.                                 $newServiceEvent->setCurrency($serviceEvent->getCurrency());
  988.                                 $newServiceEvent->setUnits($serviceEvent->getUnits());
  989.                                 $newServiceEvent->setCommission($serviceEvent->getCommission());
  990.                                 $newServiceEvent->setOver($serviceEvent->getOver());
  991.                                 $newServiceEvent->setIva($serviceEvent->getIva());
  992.                                 $newServiceEvent->setPax($serviceEvent->getPax());
  993.                                 $newServiceEvent->setHour($serviceEvent->getHour());
  994.                                 $newServiceEvent->setDateInAt($serviceEvent->getDateInAt());
  995.                                 $newServiceEvent->setDateOutAt($serviceEvent->getDateOutAt());
  996.                                 $newServiceEvent->setDirectPayment($serviceEvent->getDirectPayment());
  997.                                 $newServiceEvent->setStatus($serviceEvent->getStatus());
  998.                                 $newServiceEvent->setCreatedAt(new DateTime('now'));
  999.                                 $newServiceEvent->setCreatedId($user_id);
  1000.                                 $newServiceEvent->setUpdatedAt(new DateTime('now'));
  1001.                                 $newServiceEvent->setUpdatedId($user_id);
  1002.                                 $newServiceEvent->setStatusinternal($serviceEvent->getStatusinternal());
  1003.                                 $newServiceEvent->setPreCommission($serviceEvent->getPreCommission());
  1004.                                 $newServiceEvent->setPreIva($serviceEvent->getPreIva());
  1005.                                 $newServiceEvent->setDateBlockLimit($serviceEvent->getDateBlockLimit());
  1006.                                 $newServiceEvent->setOpCommission($serviceEvent->getOpCommission());
  1007.                                 $newServiceEvent->setOpOver($serviceEvent->getOpOver());
  1008.                                 $newServiceEvent->setOpIva($serviceEvent->getOpIva());
  1009.                                 $newServiceEvent->setBreakdown($serviceEvent->getBreakdown());
  1010.                                 $newServiceEvent->setIsFather($serviceEvent->getIsFather());
  1011.                                 $newServiceEvent->setRank($serviceEvent->getRank());
  1012.                                 $newServiceEvent->setOriginalopIva($serviceEvent->getOriginalopIva());
  1013.                                 $newServiceEvent->setOriginalIva($serviceEvent->getOriginalIva());
  1014.                                 $newServiceEvent->setOriginalPrice($serviceEvent->getOriginalPrice());
  1015.                                 $newServiceEvent->setStatusRec($serviceEvent->getStatusRec());   //setStatusRecommended
  1016.                                 $newServiceEvent->setAssistantId($serviceEvent->getAssistantId());
  1017.                                 $em->persist($newServiceEvent);
  1018.                                 $em->flush();
  1019.                             }
  1020.                             if (!empty($serviceEvent)){
  1021.                                 $newProductFile->setOriginId($newServiceEvent->getId());
  1022.                             } else {
  1023.                                 $newProductFile->setOriginId(null);
  1024.                             }
  1025.                         }
  1026.                         //FIN: Duplicamos en Eventos
  1027.                         //FIN: Si el producto esta sincronizado con un proposal debemos crear un servicio nuevo en el proposal
  1028.                         $newProductFile->setProductName($product->getProductName());
  1029.                         $newProductFile->setProductId($product->getProductId());
  1030.                         $newProductFile->setDateStart($product->getDateStart());
  1031.                         $newProductFile->setDateEnd($product->getDateEnd());
  1032.                         $newProductFile->setServicePrice($product->getServicePrice());
  1033.                         $newProductFile->setFileId($product->getFileId());
  1034.                         $newProductFile->setPax($product->getPax());
  1035.                         $newProductFile->setType($product->getType());
  1036.                         $newProductFile->setHourStart($product->getHourStart());
  1037.                         $newProductFile->setMinStart($product->getMinStart());
  1038.                         $newProductFile->setHourEnd($product->getHourEnd());
  1039.                         $newProductFile->setMinEnd($product->getMinEnd());
  1040.                         $newProductFile->setDescription($product->getDescription());
  1041.                         $newProductFile->setServiceIdProposal($product->getServiceIdProposal());
  1042.                         $newProductFile->setServiceIdFather($product->getServiceIdFather());
  1043.                         $newProductFile->setControlId($product->getControlId());
  1044.                         $newProductFile->setProposalId($product->getProposalId());
  1045.                         $newProductFile->setDestinationId($product->getDestinationId());
  1046.                         $newProductFile->setIdeaId($product->getIdeaId());
  1047.                         $newProductFile->setActivityId($product->getActivityId());
  1048.                         $newProductFile->setSupplierId($product->getSupplierId());
  1049.                         $newProductFile->setAssistantId($product->getAssistantId());
  1050.                         $newProductFile->setServiceId($product->getServiceId());
  1051.                         $newProductFile->setServiceCatId($product->getServiceCatId());
  1052.                         $newProductFile->setServiceCatName($product->getServiceCatName());
  1053.                         $newProductFile->setName($product->getName());
  1054.                         $newProductFile->setPrice($product->getPrice());
  1055.                         $newProductFile->setCurrency($product->getCurrency());
  1056.                         $newProductFile->setUnits($product->getUnits());
  1057.                         $newProductFile->setOpCommission($product->getOpCommission());
  1058.                         $newProductFile->setCommission($product->getCommission());
  1059.                         $newProductFile->setOpOver($product->getOpOver());
  1060.                         $newProductFile->setOver($product->getOver());
  1061.                         $newProductFile->setOpIva($product->getOpIva());
  1062.                         $newProductFile->setIva($product->getIva());
  1063.                         $newProductFile->setHour($product->getHour());
  1064.                         $newProductFile->setDateInAt($product->getDateInAt());
  1065.                         $newProductFile->setDateOutAt($product->getDateOutAt());
  1066.                         $newProductFile->setContcolor($product->getContcolor());
  1067.                         $newProductFile->setRank($product->getRank());
  1068.                         $newProductFile->setRenovate($product->getRenovate());
  1069.                         $newProductFile->setPay($product->getPay());
  1070.                         $newProductFile->setSubTotalPrice($product->getSubTotalPrice());
  1071.                         $newProductFile->setDays($product->getDays());
  1072.                         $newProductFile->setCreatedAt(new DateTime('now'));
  1073.                         $newProductFile->setCreatedId($user_id);
  1074.                         $newProductFile->setUpdatedAt(new DateTime('now'));
  1075.                         $newProductFile->setUpdatedId($user_id);
  1076.                         $em->persist($newProductFile);
  1077.                         $em->flush();
  1078.                     }
  1079.                     //FIN: Creamos el nuevo ProductFile para evitar cruce de datos entre proformas
  1080.                     $itemProforma = new MdvDocProformaItems();
  1081.                     $itemProforma->setType('PRODUCT');
  1082.                     $itemProforma->setFileId($id);
  1083.                     $itemProforma->setProformaId($proforma_number);
  1084.                     if ($boolKeyChange){
  1085.                         $itemProforma->setControlId($newProductFile->getId());
  1086.                     } else {
  1087.                         $itemProforma->setControlId($key);
  1088.                     }
  1089.                     $em->persist($itemProforma);
  1090.                     $em->flush();
  1091.                     //Acumulamos neto e iva para la factura (PROFORMA)
  1092. //                    $valorTotalNet = $valorTotalNet + $product->getServicePrice();
  1093.                     $valorTotalNet = $valorTotalNet + $product->getSubTotalPrice();
  1094. //                    $valorVat = $valorVat + round(($product->getServicePrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  1095.                     $valorVat = $valorVat + round(($product->getSubTotalPrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  1096.                 }
  1097.             }
  1098.         }
  1099.         if (!empty($arrayBools['service'])) {
  1100.             foreach ($arrayBools['service'] as $key => $item) {
  1101.                 if ($item == 'true') {
  1102.                     $boolMakeInvoice = true;
  1103.                     $servicio = $em->getRepository(MdvServices::class)->findOneById($key);
  1104.                     $itemProforma = new MdvDocProformaItems();
  1105.                     $itemProforma->setType('SERVICE');
  1106.                     $itemProforma->setFileId($id);
  1107.                     $itemProforma->setProformaId($proforma_number);
  1108.                     $itemProforma->setControlId($key);
  1109.                     $em->persist($itemProforma);
  1110.                     $em->flush();
  1111.                     //Acumulamos neto e iva para la factura
  1112.                     if (is_null($servicio->getPrice()) or empty($servicio->getPrice())) {
  1113.                         $subtotal = 0;
  1114.                         $neto = 0;
  1115.                         $subtotalService = 0;
  1116.                     } else {
  1117.                         $subtotalService = $servicio->getPrice();
  1118.                         $subneto = $servicio->getPrice();
  1119.                         // Commission
  1120.                         if ($servicio->getOpCommission() == '1') {
  1121.                             $subtotalService = $subtotalService * (1 + ($servicio->getCommission() / 100));
  1122.                             $subneto = $subneto * (1 + ($servicio->getCommission() / 100));
  1123.                         } else {
  1124.                             $subtotalService = $subtotalService * (1 - ($servicio->getCommission() / 100));
  1125.                             $subneto = $subneto * (1 - ($servicio->getCommission() / 100));
  1126.                         }
  1127.                         // Over
  1128.                         if ($servicio->getOpOver() == '1') {
  1129.                             $subtotalService = $subtotalService + $servicio->getOver();
  1130.                             $subneto = $subneto + $servicio->getOver();
  1131.                         } else {
  1132.                             $subtotalService = $subtotalService - $servicio->getOver();
  1133.                             $subneto = $subneto - $servicio->getOver();
  1134.                         }
  1135.                         // IVA
  1136.                         if ($servicio->getOpIva() == '1') {
  1137.                             $subtotalService = $subtotalService * (1 + ($servicio->getIva() / 100));
  1138.                         } else {
  1139.                             $subtotalService = $servicio->getPrice();
  1140.                             $subneto = ($subneto * 100) / (100 + $servicio->getIva());
  1141.                         }
  1142.                         switch ($servicio->getServiceCatId()) {
  1143.                             case 1: // Alojamiento
  1144.                                 // el numero de noches $numNoches; precio unitario $subneto
  1145.                                 $numNoches = (($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days;
  1146.                                 // La personas no afectan este calculo
  1147.                                 $subtotal = $subtotalService * $numNoches * $servicio->getUnits();
  1148.                                 $subnetoUnit = $subneto;
  1149.                                 $subneto = $subneto * $numNoches * $servicio->getUnits();
  1150.                                 break;
  1151.                             case 2: //Actividades
  1152.                                 // El número de personas es considerado en el calculo
  1153.                                 $pax = $servicio->getPax();
  1154.                                 if (empty($pax) or $pax == "0") {
  1155.                                     $pax = 1;
  1156.                                 }
  1157.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1158.                                 if ($days > 1) {
  1159.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1160.                                 } else {
  1161.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1162.                                 }
  1163.                                 $subtotal = $subtotalService * $days * $servicio->getUnits();
  1164.                                 $subnetoUnit = $subneto;
  1165.                                 $subneto = $subneto * $days * $servicio->getUnits();
  1166.                                 break;
  1167.                             case 3: // AV
  1168.                                 $pax = $servicio->getPax();
  1169.                                 if (empty($pax) or $pax == "0") {
  1170.                                     $pax = 1;
  1171.                                 }
  1172.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1173.                                 if ($days > 1) {
  1174.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1175.                                 } else {
  1176.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1177.                                 }
  1178.                                 $unitsServ = $servicio->getUnits();
  1179.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1180.                                     $unitsServ = 1;
  1181.                                 }
  1182.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1183.                                 $subnetoUnit = $subneto;
  1184.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1185.                                 break;
  1186.                             case 4: //Creative
  1187.                                 $pax = $servicio->getPax();
  1188.                                 if (empty($pax) or $pax == "0") {
  1189.                                     $pax = 1;
  1190.                                 }
  1191.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1192.                                 if ($days > 1) {
  1193.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1194.                                 } else {
  1195.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1196.                                 }
  1197.                                 $unitsServ = $servicio->getUnits();
  1198.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1199.                                     $unitsServ = 1;
  1200.                                 }
  1201.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1202.                                 $subnetoUnit = $subneto;
  1203.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1204.                                 break;
  1205.                             case 5: //Cruise
  1206.                                 $pax = $servicio->getPax();
  1207.                                 if (empty($pax) or $pax == "0") {
  1208.                                     $pax = 1;
  1209.                                 }
  1210.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days);
  1211.                                 if ($days > 1) {
  1212.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1213.                                 } else {
  1214.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1215.                                 }
  1216.                                 $unitsServ = $servicio->getUnits();
  1217.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1218.                                     $unitsServ = 1;
  1219.                                 }
  1220.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1221.                                 $subnetoUnit = $subneto;
  1222.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1223.                                 break;
  1224.                             case 6: //Entertaiment
  1225.                                 $pax = $servicio->getPax();
  1226.                                 if (empty($pax) or $pax == "0") {
  1227.                                     $pax = 1;
  1228.                                 }
  1229.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1230.                                 if ($days > 1) {
  1231.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1232.                                 } else {
  1233.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1234.                                 }
  1235.                                 $unitsServ = $servicio->getUnits();
  1236.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1237.                                     $unitsServ = 1;
  1238.                                 }
  1239.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1240.                                 $subnetoUnit = $subneto;
  1241.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1242.                                 break;
  1243.                             case 7: // Gifts
  1244.                                 $pax = $servicio->getPax();
  1245.                                 if (empty($pax) or $pax == "0") {
  1246.                                     $pax = 1;
  1247.                                 }
  1248.                                 $days = 1;
  1249.                                 if ($days > 1) {
  1250.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1251.                                 } else {
  1252.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1253.                                 }
  1254.                                 $unitsServ = $servicio->getUnits();
  1255.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1256.                                     $unitsServ = 1;
  1257.                                 }
  1258.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1259.                                 $subnetoUnit = $subneto;
  1260.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1261.                                 break;
  1262.                             case 8: //Guide
  1263.                                 $pax = $servicio->getPax();
  1264.                                 if (empty($pax) or $pax == "0") {
  1265.                                     $pax = 1;
  1266.                                 }
  1267.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1268.                                 if ($days > 1) {
  1269.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1270.                                 } else {
  1271.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1272.                                 }
  1273.                                 $unitsServ = $servicio->getUnits();
  1274.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1275.                                     $unitsServ = 1;
  1276.                                 }
  1277.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1278.                                 $subnetoUnit = $subneto;
  1279.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1280.                                 break;
  1281.                             case 9: //Itineraries
  1282.                                 $pax = $servicio->getPax();
  1283.                                 if (empty($pax) or $pax == "0") {
  1284.                                     $pax = 1;
  1285.                                 }
  1286.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1287.                                 if ($days > 1) {
  1288.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1289.                                 } else {
  1290.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1291.                                 }
  1292.                                 $unitsServ = $servicio->getUnits();
  1293.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1294.                                     $unitsServ = 1;
  1295.                                 }
  1296.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1297.                                 $subnetoUnit = $subneto;
  1298.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1299.                                 break;
  1300.                             case 10: //Lounge  -- No Aplica  6*6*6 en GreenPatio no, pero en Develup ???
  1301.                                 break;
  1302.                             case 11: //Menu
  1303.                                 $pax = $servicio->getPax();
  1304.                                 if (empty($pax) or $pax == "0") {
  1305.                                     $pax = 1;
  1306.                                 }
  1307.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1308.                                 if ($days > 1) {
  1309.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1310.                                 } else {
  1311.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1312.                                 }
  1313.                                 $unitsServ = $servicio->getUnits();
  1314.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1315.                                     $unitsServ = 1;
  1316.                                 }
  1317.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1318.                                 $subnetoUnit = $subneto;
  1319.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1320.                                 break;
  1321.                             case 12: //Others
  1322.                                 $pax = $servicio->getPax();
  1323.                                 if (empty($pax) or $pax == "0") {
  1324.                                     $pax = 1;
  1325.                                 }
  1326.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1327.                                 if ($days > 1) {
  1328.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1329.                                 } else {
  1330.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1331.                                 }
  1332.                                 $unitsServ = $servicio->getUnits();
  1333.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1334.                                     $unitsServ = 1;
  1335.                                 }
  1336.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1337.                                 $subnetoUnit = $subneto;
  1338.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1339.                                 break;
  1340.                             case 13: //Transport
  1341.                                 $pax = $servicio->getPax();
  1342.                                 if (empty($pax) or $pax == "0") {
  1343.                                     $pax = 1;
  1344.                                 }
  1345.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1346.                                 if ($days > 1) {
  1347.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1348.                                 } else {
  1349.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1350.                                 }
  1351.                                 $unitsServ = $servicio->getUnits();
  1352.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1353.                                     $unitsServ = 1;
  1354.                                 }
  1355.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1356.                                 $subnetoUnit = $subneto;
  1357.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1358.                                 break;
  1359.                             case 14: //Technology
  1360.                                 $pax = $servicio->getPax();
  1361.                                 if (empty($pax) or $pax == "0") {
  1362.                                     $pax = 1;
  1363.                                 }
  1364.                                 $days = 1;
  1365.                                 $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1366.                                 $unitsServ = $servicio->getUnits();
  1367.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1368.                                     $unitsServ = 1;
  1369.                                 }
  1370.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1371.                                 $subnetoUnit = $subneto;
  1372.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1373.                                 break;
  1374.                             case 15: //Assisstant
  1375.                                 $pax = $servicio->getPax();
  1376.                                 if (empty($pax) or $pax == "0") {
  1377.                                     $pax = 1;
  1378.                                 }
  1379.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1380.                                 if ($days > 1) {
  1381.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1382.                                 } else {
  1383.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1384.                                 }
  1385.                                 $unitsServ = $servicio->getUnits();
  1386.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1387.                                     $unitsServ = 1;
  1388.                                 }
  1389.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1390.                                 $subnetoUnit = $subneto;
  1391.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1392.                                 break;
  1393.                             case 16: //DDR
  1394.                                 $pax = $servicio->getPax();
  1395.                                 if (empty($pax) or $pax == "0") {
  1396.                                     $pax = 1;
  1397.                                 }
  1398.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1399.                                 if ($days > 1) {
  1400.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1401.                                 } else {
  1402.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1403.                                 }
  1404.                                 $unitsServ = $servicio->getUnits();
  1405.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1406.                                     $unitsServ = 1;
  1407.                                 }
  1408.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1409.                                 $subnetoUnit = $subneto;
  1410.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1411.                                 break;
  1412.                             default:
  1413.                                 break;
  1414.                         }
  1415.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  1416.                         $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  1417.                         $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  1418.                         $valorTotalNet = $valorTotalNet + $neto;
  1419.                         $valorVat = $valorVat + ($subtotal - $neto);
  1420.                     }
  1421.                 }
  1422.             }
  1423.         }
  1424.         if (!empty($arrayBools['payment'])) {
  1425.             foreach ($arrayBools['payment'] as $key => $item) {
  1426.                 if ($item == 'true') {
  1427.                     $boolMakeInvoice = true;
  1428.                     $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  1429.                     $itemProforma = new MdvDocProformaItems();
  1430.                     $itemProforma->setType('PAYMENT');
  1431.                     $itemProforma->setFileId($id);
  1432.                     $itemProforma->setProformaId($proforma_number);
  1433.                     $itemProforma->setControlId($key);
  1434.                     $em->persist($itemProforma);
  1435.                     $em->flush();
  1436.                 }
  1437.             }
  1438.         }
  1439.         //FIN: Creamos los items de la proforma
  1440.         $valorTotal = $valorTotalNet + $valorVat;
  1441.         //INICIO: Creamos la proforma
  1442.         $newProforma = new MdvDocProforma();
  1443.         $newProforma->setPrefix($proforma_prefix);
  1444.         $newProforma->setDateAt(new DateTime('now'));
  1445.         $newProforma->setFileId($id);
  1446.         $newProforma->setCreatedAt(new DateTime('now'));
  1447.         $newProforma->setCreatedId($user_id);
  1448.         $newProforma->setUpdatedAt(new DateTime('now'));
  1449.         $newProforma->setUpdatedId($user_id);
  1450.         $newProforma->setItems(null);
  1451.         $newProforma->setAccessKey(null);
  1452.         if ($boolMakeInvoice) {
  1453.             // Solo se debe crear proforma si hay un elemento
  1454.             $em->persist($newProforma);
  1455.             $em->flush();
  1456.         } else {
  1457.             // Se llama a la vista de editar el expediente
  1458.             return $this->redirectToRoute('mdv_edit_file', array('id' => $id));
  1459.         }
  1460.         //FIN: Creamos la proforma
  1461.         return $this->redirectToRoute('mdv_select_invoice',array('id' => $id));
  1462.     }
  1463.     /**
  1464.      * @Route("/newproforma/", name="mdv_new_proforma")
  1465.      * Crear nueva proforma con los elementos seleccionados
  1466.      */
  1467.     public function generateNewProformaAction(Request $request)
  1468.     {
  1469.         $em = $this->getDoctrine()->getManager();
  1470.         // Se genera y muestra en nueva pantalla la nueva proforma
  1471.         $arrayBools = array(
  1472.             'product' => null,
  1473.             'service' => null,
  1474.             'payment' => null,
  1475.         );
  1476.         $arrayBools['product'] = $request->request->get('lounge_hidden_bool');
  1477.         $arrayBools['service'] = $request->request->get('service_hidden_bool');
  1478.         $arrayBools['payment'] = $request->request->get('payment_hidden_bool');
  1479.         $id = $request->request->get('file_id');
  1480.         $proforma_number = $request->request->get('proforma_number');
  1481.         $proforma_prefix = $request->request->get('proforma_prefix') . $proforma_number . '-' . $id;
  1482.         //INICIO: buscamos el ID de la factura
  1483.         $boolMakeInvoice = false;  // Control para verificar si se debe hacer (PROFORMA) o todos los indicadores (sala, servicios y pagos) estaban en falso
  1484.         //FIN: buscamos el ID de la factura
  1485.         /* Obtengo usuario logueado */
  1486.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  1487.         $user_id = $user_logueado->getId();
  1488.         //Acumuladores de datos globales de la factura
  1489.         $valorTotalNet = 0;
  1490.         $valorVat = 0;
  1491.         if (!empty($arrayBools['product'])) {
  1492.             foreach ($arrayBools['product'] as $key => $item) {
  1493.                 if ($item == 'true') {
  1494.                     $boolMakeInvoice = true;
  1495.                     $product = $em->getRepository(MdvProductFile::class)->findOneById($key);
  1496.                     $boolKeyChange = false;
  1497.                     //INICIO: Creamos el nuevo ProductFile para evitar cruce de datos entre proformas
  1498.                     // Si el producto esta contenido en una proforma debemos crear un nuevo porducto
  1499.                     $productInProforma = $em->getRepository(MdvDocProformaItems::class)->findOneByControlId($key);
  1500.                     if (!empty($productInProforma)){
  1501.                         //INICIO: Si el producto esta sincronizado con un proposal debemos crear un servicio nuevo en el proposal
  1502.                         $boolKeyChange = true;
  1503.                         $newProductFile = new MdvProductFile();
  1504.                         //INICIO: Duplicamos en Eventos
  1505.                         if (!empty($product->getOriginId())) {
  1506.                             $serviceEvent = $em->getRepository(ProposalSupplierServices::class)->findOneById($product->getOriginId());
  1507.                             if (!empty($serviceEvent)){
  1508.                                 $newServiceEvent = new ProposalSupplierServices();
  1509.                                 $newServiceEvent->setServiceIdFather($serviceEvent->getServiceIdFather());
  1510.                                 $newServiceEvent->setControlId($serviceEvent->getControlId());
  1511.                                 $newServiceEvent->setProposalId($serviceEvent->getProposalId());
  1512.                                 $newServiceEvent->setDestinationId($serviceEvent->getDestinationId());
  1513.                                 $newServiceEvent->setSupplierId($serviceEvent->getSupplierId());
  1514.                                 $newServiceEvent->setIdeaId($serviceEvent->getIdeaId());
  1515.                                 $newServiceEvent->setActivityId($serviceEvent->getActivityId());
  1516.                                 $newServiceEvent->setServiceId($serviceEvent->getServiceId());
  1517.                                 $newServiceEvent->setServiceCatId($serviceEvent->getServiceCatId());
  1518.                                 $newServiceEvent->setServiceCatName($serviceEvent->getServiceCatName());
  1519.                                 $newServiceEvent->setName($serviceEvent->getName());
  1520.                                 $newServiceEvent->setPrice($serviceEvent->getPrice());
  1521.                                 $newServiceEvent->setCurrency($serviceEvent->getCurrency());
  1522.                                 $newServiceEvent->setUnits($serviceEvent->getUnits());
  1523.                                 $newServiceEvent->setCommission($serviceEvent->getCommission());
  1524.                                 $newServiceEvent->setOver($serviceEvent->getOver());
  1525.                                 $newServiceEvent->setIva($serviceEvent->getIva());
  1526.                                 $newServiceEvent->setPax($serviceEvent->getPax());
  1527.                                 $newServiceEvent->setHour($serviceEvent->getHour());
  1528.                                 $newServiceEvent->setDateInAt($serviceEvent->getDateInAt());
  1529.                                 $newServiceEvent->setDateOutAt($serviceEvent->getDateOutAt());
  1530.                                 $newServiceEvent->setDirectPayment($serviceEvent->getDirectPayment());
  1531.                                 $newServiceEvent->setStatus($serviceEvent->getStatus());
  1532.                                 $newServiceEvent->setCreatedAt(new DateTime('now'));
  1533.                                 $newServiceEvent->setCreatedId($user_id);
  1534.                                 $newServiceEvent->setUpdatedAt(new DateTime('now'));
  1535.                                 $newServiceEvent->setUpdatedId($user_id);
  1536.                                 $newServiceEvent->setStatusinternal($serviceEvent->getStatusinternal());
  1537.                                 $newServiceEvent->setPreCommission($serviceEvent->getPreCommission());
  1538.                                 $newServiceEvent->setPreIva($serviceEvent->getPreIva());
  1539.                                 $newServiceEvent->setDateBlockLimit($serviceEvent->getDateBlockLimit());
  1540.                                 $newServiceEvent->setOpCommission($serviceEvent->getOpCommission());
  1541.                                 $newServiceEvent->setOpOver($serviceEvent->getOpOver());
  1542.                                 $newServiceEvent->setOpIva($serviceEvent->getOpIva());
  1543.                                 $newServiceEvent->setBreakdown($serviceEvent->getBreakdown());
  1544.                                 $newServiceEvent->setIsFather($serviceEvent->getIsFather());
  1545.                                 $newServiceEvent->setRank($serviceEvent->getRank());
  1546.                                 $newServiceEvent->setOriginalopIva($serviceEvent->getOriginalopIva());
  1547.                                 $newServiceEvent->setOriginalIva($serviceEvent->getOriginalIva());
  1548.                                 $newServiceEvent->setOriginalPrice($serviceEvent->getOriginalPrice());
  1549.                                 $newServiceEvent->setStatusRec($serviceEvent->getStatusRec());   //setStatusRecommended
  1550.                                 $newServiceEvent->setAssistantId($serviceEvent->getAssistantId());
  1551.                                 $em->persist($newServiceEvent);
  1552.                                 $em->flush();
  1553.                             }
  1554.                             if (!empty($serviceEvent)){
  1555.                                 $newProductFile->setOriginId($newServiceEvent->getId());
  1556.                             } else {
  1557.                                 $newProductFile->setOriginId(null);
  1558.                             }
  1559.                         }
  1560.                         //FIN: Duplicamos en Eventos
  1561.                         //FIN: Si el producto esta sincronizado con un proposal debemos crear un servicio nuevo en el proposal
  1562.                         $newProductFile->setProductName($product->getProductName());
  1563.                         $newProductFile->setProductId($product->getProductId());
  1564.                         $newProductFile->setDateStart($product->getDateStart());
  1565.                         $newProductFile->setDateEnd($product->getDateEnd());
  1566.                         $newProductFile->setServicePrice($product->getServicePrice());
  1567.                         $newProductFile->setFileId($product->getFileId());
  1568.                         $newProductFile->setPax($product->getPax());
  1569.                         $newProductFile->setType($product->getType());
  1570.                         $newProductFile->setHourStart($product->getHourStart());
  1571.                         $newProductFile->setMinStart($product->getMinStart());
  1572.                         $newProductFile->setHourEnd($product->getHourEnd());
  1573.                         $newProductFile->setMinEnd($product->getMinEnd());
  1574.                         $newProductFile->setDescription($product->getDescription());
  1575.                         $newProductFile->setServiceIdProposal($product->getServiceIdProposal());
  1576.                         $newProductFile->setServiceIdFather($product->getServiceIdFather());
  1577.                         $newProductFile->setControlId($product->getControlId());
  1578.                         $newProductFile->setProposalId($product->getProposalId());
  1579.                         $newProductFile->setDestinationId($product->getDestinationId());
  1580.                         $newProductFile->setIdeaId($product->getIdeaId());
  1581.                         $newProductFile->setActivityId($product->getActivityId());
  1582.                         $newProductFile->setSupplierId($product->getSupplierId());
  1583.                         $newProductFile->setAssistantId($product->getAssistantId());
  1584.                         $newProductFile->setServiceId($product->getServiceId());
  1585.                         $newProductFile->setServiceCatId($product->getServiceCatId());
  1586.                         $newProductFile->setServiceCatName($product->getServiceCatName());
  1587.                         $newProductFile->setName($product->getName());
  1588.                         $newProductFile->setPrice($product->getPrice());
  1589.                         $newProductFile->setCurrency($product->getCurrency());
  1590.                         $newProductFile->setUnits($product->getUnits());
  1591.                         $newProductFile->setOpCommission($product->getOpCommission());
  1592.                         $newProductFile->setCommission($product->getCommission());
  1593.                         $newProductFile->setOpOver($product->getOpOver());
  1594.                         $newProductFile->setOver($product->getOver());
  1595.                         $newProductFile->setOpIva($product->getOpIva());
  1596.                         $newProductFile->setIva($product->getIva());
  1597.                         $newProductFile->setHour($product->getHour());
  1598.                         $newProductFile->setDateInAt($product->getDateInAt());
  1599.                         $newProductFile->setDateOutAt($product->getDateOutAt());
  1600.                         $newProductFile->setContcolor($product->getContcolor());
  1601.                         $newProductFile->setRank($product->getRank());
  1602.                         $newProductFile->setRenovate($product->getRenovate());
  1603.                         $newProductFile->setPay($product->getPay());
  1604.                         $newProductFile->setSubTotalPrice($product->getSubTotalPrice());
  1605.                         $newProductFile->setDays($product->getDays());
  1606.                         $newProductFile->setCreatedAt(new DateTime('now'));
  1607.                         $newProductFile->setCreatedId($user_id);
  1608.                         $newProductFile->setUpdatedAt(new DateTime('now'));
  1609.                         $newProductFile->setUpdatedId($user_id);
  1610.                         $em->persist($newProductFile);
  1611.                         $em->flush();
  1612.                     }
  1613.                     //FIN: Creamos el nuevo ProductFile para evitar cruce de datos entre proformas
  1614.                     $itemProforma = new MdvDocProformaItems();
  1615.                     $itemProforma->setType('PRODUCT');
  1616.                     $itemProforma->setFileId($id);
  1617.                     $itemProforma->setProformaId($proforma_number);
  1618.                     if ($boolKeyChange){
  1619.                         $itemProforma->setControlId($newProductFile->getId());
  1620.                     } else {
  1621.                         $itemProforma->setControlId($key);
  1622.                     }
  1623.                     $em->persist($itemProforma);
  1624.                     $em->flush();
  1625.                     //Acumulamos neto e iva para la factura (PROFORMA)
  1626. //                    $valorTotalNet = $valorTotalNet + $product->getServicePrice();
  1627.                     $valorTotalNet = $valorTotalNet + $product->getSubTotalPrice();
  1628. //                    $valorVat = $valorVat + round(($product->getServicePrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  1629.                     $valorVat = $valorVat + round(($product->getSubTotalPrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  1630.                 }
  1631.             }
  1632.         }
  1633.         if (!empty($arrayBools['service'])) {
  1634.             foreach ($arrayBools['service'] as $key => $item) {
  1635.                 if ($item == 'true') {
  1636.                     $boolMakeInvoice = true;
  1637.                     $servicio = $em->getRepository(MdvServices::class)->findOneById($key);
  1638.                     $itemProforma = new MdvDocProformaItems();
  1639.                     $itemProforma->setType('SERVICE');
  1640.                     $itemProforma->setFileId($id);
  1641.                     $itemProforma->setProformaId($proforma_number);
  1642.                     $itemProforma->setControlId($key);
  1643.                     $em->persist($itemProforma);
  1644.                     $em->flush();
  1645.                     //Acumulamos neto e iva para la factura
  1646.                     if (is_null($servicio->getPrice()) or empty($servicio->getPrice())) {
  1647.                         $subtotal = 0;
  1648.                         $neto = 0;
  1649.                         $subtotalService = 0;
  1650.                     } else {
  1651.                         $subtotalService = $servicio->getPrice();
  1652.                         $subneto = $servicio->getPrice();
  1653.                         // Commission
  1654.                         if ($servicio->getOpCommission() == '1') {
  1655.                             $subtotalService = $subtotalService * (1 + ($servicio->getCommission() / 100));
  1656.                             $subneto = $subneto * (1 + ($servicio->getCommission() / 100));
  1657.                         } else {
  1658.                             $subtotalService = $subtotalService * (1 - ($servicio->getCommission() / 100));
  1659.                             $subneto = $subneto * (1 - ($servicio->getCommission() / 100));
  1660.                         }
  1661.                         // Over
  1662.                         if ($servicio->getOpOver() == '1') {
  1663.                             $subtotalService = $subtotalService + $servicio->getOver();
  1664.                             $subneto = $subneto + $servicio->getOver();
  1665.                         } else {
  1666.                             $subtotalService = $subtotalService - $servicio->getOver();
  1667.                             $subneto = $subneto - $servicio->getOver();
  1668.                         }
  1669.                         // IVA
  1670.                         if ($servicio->getOpIva() == '1') {
  1671.                             $subtotalService = $subtotalService * (1 + ($servicio->getIva() / 100));
  1672.                         } else {
  1673.                             $subtotalService = $servicio->getPrice();
  1674.                             $subneto = ($subneto * 100) / (100 + $servicio->getIva());
  1675.                         }
  1676.                         switch ($servicio->getServiceCatId()) {
  1677.                             case 1: // Alojamiento
  1678.                                 // el numero de noches $numNoches; precio unitario $subneto
  1679.                                 $numNoches = (($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days;
  1680.                                 // La personas no afectan este calculo
  1681.                                 $subtotal = $subtotalService * $numNoches * $servicio->getUnits();
  1682.                                 $subnetoUnit = $subneto;
  1683.                                 $subneto = $subneto * $numNoches * $servicio->getUnits();
  1684.                                 break;
  1685.                             case 2: //Actividades
  1686.                                 // El número de personas es considerado en el calculo
  1687.                                 $pax = $servicio->getPax();
  1688.                                 if (empty($pax) or $pax == "0") {
  1689.                                     $pax = 1;
  1690.                                 }
  1691.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1692.                                 if ($days > 1) {
  1693.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1694.                                 } else {
  1695.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1696.                                 }
  1697.                                 $subtotal = $subtotalService * $days * $servicio->getUnits();
  1698.                                 $subnetoUnit = $subneto;
  1699.                                 $subneto = $subneto * $days * $servicio->getUnits();
  1700.                                 break;
  1701.                             case 3: // AV
  1702.                                 $pax = $servicio->getPax();
  1703.                                 if (empty($pax) or $pax == "0") {
  1704.                                     $pax = 1;
  1705.                                 }
  1706.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1707.                                 if ($days > 1) {
  1708.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1709.                                 } else {
  1710.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1711.                                 }
  1712.                                 $unitsServ = $servicio->getUnits();
  1713.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1714.                                     $unitsServ = 1;
  1715.                                 }
  1716.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1717.                                 $subnetoUnit = $subneto;
  1718.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1719.                                 break;
  1720.                             case 4: //Creative
  1721.                                 $pax = $servicio->getPax();
  1722.                                 if (empty($pax) or $pax == "0") {
  1723.                                     $pax = 1;
  1724.                                 }
  1725.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1726.                                 if ($days > 1) {
  1727.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1728.                                 } else {
  1729.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1730.                                 }
  1731.                                 $unitsServ = $servicio->getUnits();
  1732.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1733.                                     $unitsServ = 1;
  1734.                                 }
  1735.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1736.                                 $subnetoUnit = $subneto;
  1737.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1738.                                 break;
  1739.                             case 5: //Cruise
  1740.                                 $pax = $servicio->getPax();
  1741.                                 if (empty($pax) or $pax == "0") {
  1742.                                     $pax = 1;
  1743.                                 }
  1744.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days);
  1745.                                 if ($days > 1) {
  1746.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1747.                                 } else {
  1748.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1749.                                 }
  1750.                                 $unitsServ = $servicio->getUnits();
  1751.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1752.                                     $unitsServ = 1;
  1753.                                 }
  1754.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1755.                                 $subnetoUnit = $subneto;
  1756.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1757.                                 break;
  1758.                             case 6: //Entertaiment
  1759.                                 $pax = $servicio->getPax();
  1760.                                 if (empty($pax) or $pax == "0") {
  1761.                                     $pax = 1;
  1762.                                 }
  1763.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1764.                                 if ($days > 1) {
  1765.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1766.                                 } else {
  1767.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1768.                                 }
  1769.                                 $unitsServ = $servicio->getUnits();
  1770.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1771.                                     $unitsServ = 1;
  1772.                                 }
  1773.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1774.                                 $subnetoUnit = $subneto;
  1775.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1776.                                 break;
  1777.                             case 7: // Gifts
  1778.                                 $pax = $servicio->getPax();
  1779.                                 if (empty($pax) or $pax == "0") {
  1780.                                     $pax = 1;
  1781.                                 }
  1782.                                 $days = 1;
  1783.                                 if ($days > 1) {
  1784.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1785.                                 } else {
  1786.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1787.                                 }
  1788.                                 $unitsServ = $servicio->getUnits();
  1789.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1790.                                     $unitsServ = 1;
  1791.                                 }
  1792.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1793.                                 $subnetoUnit = $subneto;
  1794.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1795.                                 break;
  1796.                             case 8: //Guide
  1797.                                 $pax = $servicio->getPax();
  1798.                                 if (empty($pax) or $pax == "0") {
  1799.                                     $pax = 1;
  1800.                                 }
  1801.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1802.                                 if ($days > 1) {
  1803.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1804.                                 } else {
  1805.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1806.                                 }
  1807.                                 $unitsServ = $servicio->getUnits();
  1808.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1809.                                     $unitsServ = 1;
  1810.                                 }
  1811.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1812.                                 $subnetoUnit = $subneto;
  1813.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1814.                                 break;
  1815.                             case 9: //Itineraries
  1816.                                 $pax = $servicio->getPax();
  1817.                                 if (empty($pax) or $pax == "0") {
  1818.                                     $pax = 1;
  1819.                                 }
  1820.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1821.                                 if ($days > 1) {
  1822.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1823.                                 } else {
  1824.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1825.                                 }
  1826.                                 $unitsServ = $servicio->getUnits();
  1827.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1828.                                     $unitsServ = 1;
  1829.                                 }
  1830.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1831.                                 $subnetoUnit = $subneto;
  1832.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1833.                                 break;
  1834.                             case 10: //Lounge  -- No Aplica
  1835.                                 break;
  1836.                             case 11: //Menu
  1837.                                 $pax = $servicio->getPax();
  1838.                                 if (empty($pax) or $pax == "0") {
  1839.                                     $pax = 1;
  1840.                                 }
  1841.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1842.                                 if ($days > 1) {
  1843.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1844.                                 } else {
  1845.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1846.                                 }
  1847.                                 $unitsServ = $servicio->getUnits();
  1848.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1849.                                     $unitsServ = 1;
  1850.                                 }
  1851.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1852.                                 $subnetoUnit = $subneto;
  1853.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1854.                                 break;
  1855.                             case 12: //Others
  1856.                                 $pax = $servicio->getPax();
  1857.                                 if (empty($pax) or $pax == "0") {
  1858.                                     $pax = 1;
  1859.                                 }
  1860.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1861.                                 if ($days > 1) {
  1862.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1863.                                 } else {
  1864.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1865.                                 }
  1866.                                 $unitsServ = $servicio->getUnits();
  1867.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1868.                                     $unitsServ = 1;
  1869.                                 }
  1870.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1871.                                 $subnetoUnit = $subneto;
  1872.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1873.                                 break;
  1874.                             case 13: //Transport
  1875.                                 $pax = $servicio->getPax();
  1876.                                 if (empty($pax) or $pax == "0") {
  1877.                                     $pax = 1;
  1878.                                 }
  1879.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1880.                                 if ($days > 1) {
  1881.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1882.                                 } else {
  1883.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1884.                                 }
  1885.                                 $unitsServ = $servicio->getUnits();
  1886.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1887.                                     $unitsServ = 1;
  1888.                                 }
  1889.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1890.                                 $subnetoUnit = $subneto;
  1891.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1892.                                 break;
  1893.                             case 14: //Technology
  1894.                                 $pax = $servicio->getPax();
  1895.                                 if (empty($pax) or $pax == "0") {
  1896.                                     $pax = 1;
  1897.                                 }
  1898.                                 $days = 1;
  1899.                                 $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1900.                                 $unitsServ = $servicio->getUnits();
  1901.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1902.                                     $unitsServ = 1;
  1903.                                 }
  1904.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1905.                                 $subnetoUnit = $subneto;
  1906.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1907.                                 break;
  1908.                             case 15: //Assisstant
  1909.                                 $pax = $servicio->getPax();
  1910.                                 if (empty($pax) or $pax == "0") {
  1911.                                     $pax = 1;
  1912.                                 }
  1913.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1914.                                 if ($days > 1) {
  1915.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1916.                                 } else {
  1917.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1918.                                 }
  1919.                                 $unitsServ = $servicio->getUnits();
  1920.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1921.                                     $unitsServ = 1;
  1922.                                 }
  1923.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1924.                                 $subnetoUnit = $subneto;
  1925.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1926.                                 break;
  1927.                             case 16: //DDR
  1928.                                 $pax = $servicio->getPax();
  1929.                                 if (empty($pax) or $pax == "0") {
  1930.                                     $pax = 1;
  1931.                                 }
  1932.                                 $days = ((($servicio->getDateOutAt())->diff($servicio->getDateInAt()))->days + 1);
  1933.                                 if ($days > 1) {
  1934.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y') . ' - ' . ($servicio->getDateOutAt())->format('d/m/Y');
  1935.                                 } else {
  1936.                                     $dateServ = ($servicio->getDateInAt())->format('d/m/Y');
  1937.                                 }
  1938.                                 $unitsServ = $servicio->getUnits();
  1939.                                 if (empty($unitsServ) or $unitsServ == "0") {
  1940.                                     $unitsServ = 1;
  1941.                                 }
  1942.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  1943.                                 $subnetoUnit = $subneto;
  1944.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  1945.                                 break;
  1946.                             default:
  1947.                                 break;
  1948.                         }
  1949.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  1950.                         $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  1951.                         $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  1952.                         $valorTotalNet = $valorTotalNet + $neto;
  1953.                         $valorVat = $valorVat + ($subtotal - $neto);
  1954.                     }
  1955.                 }
  1956.             }
  1957.         }
  1958.         if (!empty($arrayBools['payment'])) {
  1959.             foreach ($arrayBools['payment'] as $key => $item) {
  1960.                 if ($item == 'true') {
  1961.                     $boolMakeInvoice = true;
  1962.                     $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  1963.                     $itemProforma = new MdvDocProformaItems();
  1964.                     $itemProforma->setType('PAYMENT');
  1965.                     $itemProforma->setFileId($id);
  1966.                     $itemProforma->setProformaId($proforma_number);
  1967.                     $itemProforma->setControlId($key);
  1968.                     $em->persist($itemProforma);
  1969.                     $em->flush();
  1970.                 }
  1971.             }
  1972.         }
  1973.         //FIN: Creamos los items de la proforma
  1974.         $valorTotal = $valorTotalNet + $valorVat;
  1975.         //INICIO: Creamos la proforma
  1976.         $newProforma = new MdvDocProforma();
  1977. //        $newProforma->setPrefix("GPP-".date('dmy')."-".$id);
  1978.         $newProforma->setPrefix($proforma_prefix);
  1979.         $newProforma->setDateAt(new DateTime('now'));
  1980.         $newProforma->setFileId($id);
  1981.         $newProforma->setCreatedAt(new DateTime('now'));
  1982.         $newProforma->setCreatedId($user_id);
  1983.         $newProforma->setUpdatedAt(new DateTime('now'));
  1984.         $newProforma->setUpdatedId($user_id);
  1985.         $newProforma->setItems(null);
  1986.         $newProforma->setAccessKey(null);
  1987.         if ($boolMakeInvoice) {
  1988.             // Solo se debe crear proforma si hay un elemento
  1989.             $em->persist($newProforma);
  1990.             $em->flush();
  1991.         } else {
  1992.             // Se llama a la vista de editar el expediente
  1993.             return $this->redirectToRoute('mdv_edit_file', array('id' => $id));
  1994.         }
  1995.         //FIN: Creamos la proforma
  1996.         return $this->redirectToRoute('mdv_view_proforma', array( 'id' => $newProforma->getId()));
  1997.     }
  1998.     /**
  1999.      * @Route("/discardproforma/{id}", name="mdv_discard_proforma")
  2000.      * Descartar la proforma especificada en el ID
  2001.      */
  2002.     public function discardProformaAction( $id, Request $request)
  2003.     {
  2004.         $em = $this->getDoctrine()->getManager();
  2005.         $proforma =  $em->getRepository(MdvDocProforma::class)->findOneById($id);
  2006.         $fileId = $proforma->getFileId();
  2007.         //Eliminamos los productos dentro de la proforma
  2008.         $proformaItems =  $em->getRepository(MdvDocProformaItems::class)->findByProformaId($id);
  2009.         foreach ($proformaItems as $item){
  2010.             $em->remove($item);
  2011.             $em->flush();
  2012.         }
  2013.         //No se puede eliminar el registro porque es necesario mantener la numeración de las proformas si se desean generar correctamente
  2014.         $proforma->setFileId(0);
  2015.         $em->persist($proforma);
  2016.         $em->flush();
  2017.         return $this->redirectToRoute('mdv_edit_file', array('id' => $fileId));
  2018.     }
  2019.     /**
  2020.      * @Route("/viewproforma/{id}",  name="mdv_view_proforma")
  2021.      * Ver una proforma
  2022.      */
  2023.     public function viewProformaAction( $id, Request $request)
  2024.     {
  2025.         $docType = 'Proforma';
  2026.         $em = $this->getDoctrine()->getManager();
  2027.         $proforma =  $em->getRepository(MdvDocProforma::class)->findOneById($id);
  2028.         $id = $proforma->getFileId();
  2029.         /* Obtengo usuario logueado */
  2030.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  2031.         $user_id = $user_logueado->getId();
  2032.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  2033.         // Buscamos el cliente
  2034.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  2035.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  2036.         if (empty($client)){
  2037.             $client[0] = new Client();
  2038.             $client[0]->setName('');
  2039.             $client[0]->setTitle('');
  2040.             $client[0]->setIdDocument('');
  2041.             $client[0]->setPopulation('');
  2042.             $client[0]->setRegion('');
  2043.             $client[0]->setCountry('');
  2044.         } else {
  2045.             if (is_numeric($client[0]->getPopulation())){
  2046.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  2047.                 $client[0]->setPopulation($city->getCity());
  2048.             }
  2049.             if (is_numeric($client[0]->getRegion())){
  2050.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  2051.                 $client[0]->setRegion($region->getRegion());
  2052.             }
  2053.             if (is_numeric($client[0]->getCountry())){
  2054.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  2055.                 $client[0]->setCountry($country->getCountry());
  2056.             }
  2057.         }
  2058.         // Buscamos el cliente
  2059.         $data = $this->baseInvoiceDoneDocument($file->getId(), $proforma->getId(), 'P');
  2060.         $allProformas =  $em->getRepository(MdvDocProforma::class)->findByFileId($id);
  2061.         // Falta agregar en $allProformas las proformas de facturas, hasta ahora solo estan las proformas de deposito  //6*6*6
  2062.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  2063.             array(
  2064.                 'id' => $id,
  2065.                 'docType' => $docType,
  2066.                 'proforma' => $proforma,
  2067.                 'company' => $company,
  2068.                 'clients' => $client,
  2069.                 'date' => $proforma->getDateAt(),
  2070.                 'number' => $proforma->getId(),
  2071.                 'invoice' => null,
  2072.                 'fid' => $proforma->getId(),
  2073.                 'boolToRec' => false,
  2074.                 'proformas' => $allProformas,
  2075.                 'currency' => '€',
  2076.                 'totales_neto' => $data['totales_neto'],
  2077.                 'totales' => $data['totales'],
  2078.                 'balance' => $data['balance'],
  2079.                 'bases_imponibles' => $data['bases_imponibles'],
  2080.                 'datasupplier' => $data['datasupplier'],
  2081.                 'services' => null,
  2082.                 'facturas' => null,
  2083.                 'paymentNotIvoiced' => null,
  2084.                 'paymentsAll' => null,
  2085.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  2086.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  2087.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  2088.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  2089.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  2090.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  2091.             ));
  2092.     }
  2093.     /**
  2094.      * @Route("/dpexternal/printproforma/{id}",  name="mdv_print_proforma")
  2095.      * Imprimir una proforma
  2096.      */
  2097.     public function printProformaAction( $id, Request $request)
  2098.     {
  2099.         $docType = 'Proforma';
  2100.         $em = $this->getDoctrine()->getManager();
  2101.         $proforma =  $em->getRepository(MdvDocProforma::class)->findOneById($id);
  2102.         $id = $proforma->getFileId();
  2103.         /* Obtengo usuario logueado */
  2104. //        $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  2105. //        $user_id = $user_logueado->getId();
  2106.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  2107.         // Buscamos el cliente
  2108.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  2109.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  2110.         if (empty($client)){
  2111.             $client[0] = new Client();
  2112.             $client[0]->setName('');
  2113.             $client[0]->setTitle('');
  2114.             $client[0]->setIdDocument('');
  2115.             $client[0]->setPopulation('');
  2116.             $client[0]->setRegion('');
  2117.             $client[0]->setCountry('');
  2118.         } else {
  2119.             if (is_numeric($client[0]->getPopulation())){
  2120.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  2121.                 $client[0]->setPopulation($city->getCity());
  2122.             }
  2123.             if (is_numeric($client[0]->getRegion())){
  2124.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  2125.                 $client[0]->setRegion($region->getRegion());
  2126.             }
  2127.             if (is_numeric($client[0]->getCountry())){
  2128.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  2129.                 $client[0]->setCountry($country->getCountry());
  2130.             }
  2131.         }
  2132.         // Buscamos el cliente
  2133.         $data = $this->baseInvoiceDoneDocument($file->getId(), $proforma->getId(), 'P');
  2134.         $allProformas =  $em->getRepository(MdvDocProformaDeposit::class)->findByFileId($id);
  2135.         // Falta agregar en $allProformas las proformas de facturas, hasta ahora solo estan las proformas de deposito  //6*6*6
  2136.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  2137.             array(
  2138.                 'id' => $id,
  2139.                 'docType' => $docType,
  2140.                 'proforma' => $proforma,
  2141.                 'company' => $company,
  2142.                 'clients' => $client,
  2143.                 'date' => $proforma->getDateAt(),
  2144.                 'number' => $proforma->getId(),
  2145.                 'invoice' => null,
  2146.                 'fid' => $proforma->getId(),
  2147.                 'boolToRec' => false,
  2148.                 'proformas' => $allProformas,
  2149.                 'currency' => '€',
  2150.                 'totales_neto' => $data['totales_neto'],
  2151.                 'totales' => $data['totales'],
  2152.                 'balance' => $data['balance'],
  2153.                 'bases_imponibles' => $data['bases_imponibles'],
  2154.                 'datasupplier' => $data['datasupplier'],
  2155.                 'services' => null,
  2156.                 'facturas' => null,
  2157.                 'paymentNotIvoiced' => null,
  2158.                 'paymentsAll' => null,
  2159.                 'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  2160.                 'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  2161.                 'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  2162.                 'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  2163.                 'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  2164.                 'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  2165.             ));
  2166.     }
  2167.     /**
  2168.      * @Route("/newinvoice/", name="mdv_new_invoice")
  2169.      * Crear nueva factura con los elementos seleccionados
  2170.      */
  2171.     public function generateNewInvoiceAction(Request $request)
  2172.     {
  2173.         $em = $this->getDoctrine()->getManager();
  2174.         // Se genera y muestra en nueva pantalla la nueva proforma
  2175.         $arrayBools = array(
  2176.             'product' => null,
  2177.             'service' => null,
  2178.             'payment' => null,
  2179.         );
  2180.         $arrayBools['product'] = $request->request->get('inv_lounge_hidden_bool');
  2181.         $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  2182.         $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  2183.         $id = $request->request->get('inv_file_id');
  2184.         //INICIO: buscamos el ID de la factura
  2185.         $allFacturas = $em->getRepository(MdvDocInvoice::class)->findAll();
  2186.         if (!empty($allFacturas)){
  2187.             $invoiceId = end($allFacturas)->getId() + 1;
  2188.         } else {
  2189.             $invoiceId = 1;
  2190.         }
  2191.         $boolMakeInvoice = false;  // Control para verificar si se debe hacer factura o todos los indicadores (sala, servicios y pagos) estaban en falso
  2192.         //FIN: buscamos el ID de la factura
  2193.         /* Obtengo usuario logueado */
  2194.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  2195.         $user_id = $user_logueado->getId();
  2196.         //Acumuladores de datos globales de la factura
  2197.         $valorTotalNet = 0;
  2198.         $valorVat = 0;
  2199.         //INICIO: Creamos los items de la factura
  2200.         if (!empty($arrayBools['product'])) {
  2201.             foreach ($arrayBools['product'] as $key => $item) {
  2202.                 if ($item == 'true') {
  2203.                     $boolMakeInvoice = true;
  2204.                     $sala = $em->getRepository(MdvProductFile::class)->findOneById($key);
  2205.                     $itemInvoice = new MdvDocInvoiceItems();
  2206.                     $itemInvoice->setFileId($id);
  2207.                     $itemInvoice->setInvoiceId($invoiceId);
  2208.                     $itemInvoice->setItemType('PRODUCT');
  2209.                     $itemInvoice->setPrdControlId($key);
  2210.                     $itemInvoice->setPrdName($sala->getProductName());
  2211.                     $itemInvoice->setPrdProductId($sala->getProductId());
  2212.                     $itemInvoice->setPrdProductId($sala->getProductId());
  2213.                     $itemInvoice->setPrdDateStart($sala->getDateStart());
  2214.                     $itemInvoice->setPrdDateEnd($sala->getDateEnd());
  2215.                     $itemInvoice->setPrdServicePrice($sala->getServicePrice());
  2216.                     $itemInvoice->setPrdSubTotalPrice($sala->getSubTotalPrice());
  2217.                     $itemInvoice->setPrdQty($sala->getUnits());
  2218.                     $itemInvoice->setPrdPax($sala->getPax());
  2219. //                    $itemInvoice->setItemType($sala->getType());
  2220.                     $itemInvoice->setCreatedId($user_id);
  2221.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  2222.                     $itemInvoice->setUpdatedId($user_id);
  2223.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  2224.                     $itemInvoice->setPrdHourStart($sala->getHourStart());
  2225.                     $itemInvoice->setPrdMinStart($sala->getMinStart());
  2226.                     $itemInvoice->setPrdHourEnd($sala->getHourEnd());
  2227.                     $itemInvoice->setPrdMinEnd($sala->getMinEnd());
  2228.                     $itemInvoice->setPrdIva($sala->getIva());
  2229.                     $em->persist($itemInvoice);
  2230.                     $em->flush();
  2231.                     //Acumulamos neto e iva para la factura
  2232. //                    $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdServicePrice();
  2233.                     $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdSubTotalPrice();
  2234. //                    $valorVat = $valorVat + round(($itemInvoice->getPrdServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  2235.                     $valorVat = $valorVat + round(($itemInvoice->getPrdSubTotalPrice() * 0.21),2,PHP_ROUND_HALF_UP);
  2236.                 }
  2237.             }
  2238.         }
  2239.         if (!empty($arrayBools['service'])) {
  2240.             foreach ($arrayBools['service'] as $key => $item) {
  2241.                 if ($item == 'true') {
  2242.                     $boolMakeInvoice = true;
  2243.                     $servicio = $em->getRepository(MdvServices::class)->findOneById($key);
  2244.                     $itemInvoice = new MdvDocInvoiceItems();
  2245.                     $itemInvoice->setFileId($id);
  2246.                     $itemInvoice->setInvoiceId($invoiceId);
  2247.                     $itemInvoice->setItemType('SERVICE');
  2248.                     $itemInvoice->setSrvControlId($key);
  2249.                     $itemInvoice->setSrvSupplierId($servicio->getSupplierId());
  2250.                     $itemInvoice->setSrvServiceId($servicio->getServiceId());
  2251.                     $itemInvoice->setSrvServiceCatId($servicio->getServiceCatId());
  2252.                     $itemInvoice->setSrvServiceCatName($servicio->getServiceCatName());
  2253.                     $itemInvoice->setSrvPrice($servicio->getPrice());
  2254.                     $itemInvoice->setSrvCurrency($servicio->getCurrency());
  2255.                     $itemInvoice->setSrvUnits($servicio->getUnits());
  2256.                     $itemInvoice->setSrvOpCommission($servicio->getOpCommission());
  2257.                     $itemInvoice->setSrvCommission($servicio->getCommission());
  2258.                     $itemInvoice->setSrvOpOver($servicio->getOpOver());
  2259.                     $itemInvoice->setSrvOver($servicio->getOver());
  2260.                     $itemInvoice->setSrvOpIva($servicio->getOpIva());
  2261.                     $itemInvoice->setSrvIva($servicio->getIva());
  2262.                     $itemInvoice->setSrvPax($servicio->getPax());
  2263.                     $itemInvoice->setSrvHour($servicio->getHour());
  2264.                     $itemInvoice->setSrvDateInAt($servicio->getDateInAt());
  2265.                     $itemInvoice->setSrvDateOutAt($servicio->getDateOutAt());
  2266.                     $itemInvoice->setSrvContcolor($servicio->getContcolor());
  2267.                     $itemInvoice->setSrvRank($servicio->getRank());
  2268.                     $itemInvoice->setSrvAssistantId($servicio->getAssistantId());
  2269.                     $itemInvoice->setSrvActivityId($servicio->getActivityId());
  2270.                     $itemInvoice->setSrvPay($servicio->getPay());
  2271.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  2272.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  2273.                     $itemInvoice->setCreatedId($user_id);
  2274.                     $itemInvoice->setUpdatedId($user_id);
  2275.                     $em->persist($itemInvoice);
  2276.                     $em->flush();
  2277.                     //Acumulamos neto e iva para la factura
  2278.                     if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  2279.                         $subtotal = 0;
  2280.                         $neto = 0;
  2281.                         $subtotalService = 0;
  2282.                     } else {
  2283.                         $subtotalService = $itemInvoice->getSrvPrice();
  2284.                         $subneto = $itemInvoice->getSrvPrice();
  2285.                         // Commission
  2286.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  2287.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  2288.                             $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  2289.                         } else {
  2290.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  2291.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  2292.                         }
  2293.                         // Over
  2294.                         if ($itemInvoice->getSrvOpOver()=='1'){
  2295.                             $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  2296.                             $subneto = $subneto + $itemInvoice->getSrvOver();
  2297.                         } else {
  2298.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  2299.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  2300.                         }
  2301.                         // IVA
  2302.                         if ($itemInvoice->getSrvOpIva()=='1'){
  2303.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  2304.                         } else {
  2305.                             $subtotalService = $itemInvoice->getSrvPrice();
  2306.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  2307.                         }
  2308.                         switch ($itemInvoice->getSrvServiceCatId()){
  2309.                             case 1: // Alojamiento
  2310.                                 // el numero de noches $numNoches; precio unitario $subneto
  2311.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  2312.                                 // La personas no afectan este calculo
  2313.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  2314.                                 $subnetoUnit = $subneto;
  2315.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  2316.                                 break;
  2317.                             case 2: //Actividades
  2318.                                 // El número de personas es considerado en el calculo
  2319.                                 $pax = $itemInvoice->getSrvPax();
  2320.                                 if (empty($pax) or $pax == "0") {
  2321.                                     $pax = 1;
  2322.                                 }
  2323.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2324.                                 if ($days > 1){
  2325.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2326.                                 } else {
  2327.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2328.                                 }
  2329.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  2330.                                 $subnetoUnit = $subneto;
  2331.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  2332.                                 break;
  2333.                             case 3: // AV
  2334.                                 $pax = $itemInvoice->getSrvPax();
  2335.                                 if (empty($pax) or $pax == "0") {
  2336.                                     $pax = 1;
  2337.                                 }
  2338.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2339.                                 if ($days > 1){
  2340.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2341.                                 } else {
  2342.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2343.                                 }
  2344.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2345.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2346.                                     $unitsServ = 1;
  2347.                                 }
  2348.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2349.                                 $subnetoUnit = $subneto;
  2350.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2351.                                 break;
  2352.                             case 4: //Creative
  2353.                                 $pax = $itemInvoice->getSrvPax();
  2354.                                 if (empty($pax) or $pax == "0") {
  2355.                                     $pax = 1;
  2356.                                 }
  2357.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2358.                                 if ($days > 1){
  2359.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2360.                                 } else {
  2361.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2362.                                 }
  2363.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2364.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2365.                                     $unitsServ = 1;
  2366.                                 }
  2367.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2368.                                 $subnetoUnit = $subneto;
  2369.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2370.                                 break;
  2371.                             case 5: //Cruise
  2372.                                 $pax = $itemInvoice->getSrvPax();
  2373.                                 if (empty($pax) or $pax == "0") {
  2374.                                     $pax = 1;
  2375.                                 }
  2376.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  2377.                                 if ($days > 1){
  2378.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2379.                                 } else {
  2380.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2381.                                 }
  2382.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2383.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2384.                                     $unitsServ = 1;
  2385.                                 }
  2386.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2387.                                 $subnetoUnit = $subneto;
  2388.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2389.                                 break;
  2390.                             case 6: //Entertaiment
  2391.                                 $pax = $itemInvoice->getSrvPax();
  2392.                                 if (empty($pax) or $pax == "0") {
  2393.                                     $pax = 1;
  2394.                                 }
  2395.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2396.                                 if ($days > 1){
  2397.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2398.                                 } else {
  2399.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2400.                                 }
  2401.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2402.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2403.                                     $unitsServ = 1;
  2404.                                 }
  2405.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2406.                                 $subnetoUnit = $subneto;
  2407.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2408.                                 break;
  2409.                             case 7: // Gifts
  2410.                                 $pax = $itemInvoice->getSrvPax();
  2411.                                 if (empty($pax) or $pax == "0") {
  2412.                                     $pax = 1;
  2413.                                 }
  2414.                                 $days = 1;
  2415.                                 if ($days > 1){
  2416.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2417.                                 } else {
  2418.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2419.                                 }
  2420.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2421.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2422.                                     $unitsServ = 1;
  2423.                                 }
  2424.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2425.                                 $subnetoUnit = $subneto;
  2426.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2427.                                 break;
  2428.                             case 8: //Guide
  2429.                                 $pax = $itemInvoice->getSrvPax();
  2430.                                 if (empty($pax) or $pax == "0") {
  2431.                                     $pax = 1;
  2432.                                 }
  2433.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2434.                                 if ($days > 1){
  2435.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2436.                                 } else {
  2437.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2438.                                 }
  2439.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2440.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2441.                                     $unitsServ = 1;
  2442.                                 }
  2443.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2444.                                 $subnetoUnit = $subneto;
  2445.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2446.                                 break;
  2447.                             case 9: //Itineraries
  2448.                                 $pax = $itemInvoice->getSrvPax();
  2449.                                 if (empty($pax) or $pax == "0") {
  2450.                                     $pax = 1;
  2451.                                 }
  2452.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2453.                                 if ($days > 1){
  2454.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2455.                                 } else {
  2456.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2457.                                 }
  2458.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2459.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2460.                                     $unitsServ = 1;
  2461.                                 }
  2462.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2463.                                 $subnetoUnit = $subneto;
  2464.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2465.                                 break;
  2466.                             case 10: //Lounge  -- No Aplica
  2467.                                 break;
  2468.                             case 11: //Menu
  2469.                                 $pax = $itemInvoice->getSrvPax();
  2470.                                 if (empty($pax) or $pax == "0") {
  2471.                                     $pax = 1;
  2472.                                 }
  2473.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2474.                                 if ($days > 1){
  2475.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2476.                                 } else {
  2477.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2478.                                 }
  2479.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2480.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2481.                                     $unitsServ = 1;
  2482.                                 }
  2483.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2484.                                 $subnetoUnit = $subneto;
  2485.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2486.                                 break;
  2487.                             case 12: //Others
  2488.                                 $pax = $itemInvoice->getSrvPax();
  2489.                                 if (empty($pax) or $pax == "0") {
  2490.                                     $pax = 1;
  2491.                                 }
  2492.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2493.                                 if ($days > 1){
  2494.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2495.                                 } else {
  2496.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2497.                                 }
  2498.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2499.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2500.                                     $unitsServ = 1;
  2501.                                 }
  2502.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2503.                                 $subnetoUnit = $subneto;
  2504.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2505.                                 break;
  2506.                             case 13: //Transport
  2507.                                 $pax = $itemInvoice->getSrvPax();
  2508.                                 if (empty($pax) or $pax == "0") {
  2509.                                     $pax = 1;
  2510.                                 }
  2511.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2512.                                 if ($days > 1){
  2513.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2514.                                 } else {
  2515.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2516.                                 }
  2517.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2518.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2519.                                     $unitsServ = 1;
  2520.                                 }
  2521.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2522.                                 $subnetoUnit = $subneto;
  2523.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2524.                                 break;
  2525.                             case 14: //Technology
  2526.                                 $pax = $itemInvoice->getSrvPax();
  2527.                                 if (empty($pax) or $pax == "0") {
  2528.                                     $pax = 1;
  2529.                                 }
  2530.                                 $days = 1;
  2531.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2532.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2533.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2534.                                     $unitsServ = 1;
  2535.                                 }
  2536.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2537.                                 $subnetoUnit = $subneto;
  2538.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2539.                                 break;
  2540.                             case 15: //Assisstant
  2541.                                 $pax = $itemInvoice->getSrvPax();
  2542.                                 if (empty($pax) or $pax == "0") {
  2543.                                     $pax = 1;
  2544.                                 }
  2545.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2546.                                 if ($days > 1){
  2547.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2548.                                 } else {
  2549.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2550.                                 }
  2551.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2552.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2553.                                     $unitsServ = 1;
  2554.                                 }
  2555.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2556.                                 $subnetoUnit = $subneto;
  2557.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2558.                                 break;
  2559.                             case 16: //DDR
  2560.                                 $pax = $itemInvoice->getSrvPax();
  2561.                                 if (empty($pax) or $pax == "0") {
  2562.                                     $pax = 1;
  2563.                                 }
  2564.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2565.                                 if ($days > 1){
  2566.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2567.                                 } else {
  2568.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2569.                                 }
  2570.                                 $unitsServ = $itemInvoice->getSrvUnits();
  2571.                                 if (empty($unitsServ) or $unitsServ == "0") {
  2572.                                     $unitsServ = 1;
  2573.                                 }
  2574.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2575.                                 $subnetoUnit = $subneto;
  2576.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  2577.                                 break;
  2578.                             default:
  2579.                                 break;
  2580.                         }
  2581.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  2582.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  2583.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  2584.                         $valorTotalNet = $valorTotalNet + $neto;
  2585.                         $valorVat = $valorVat + ($subtotal - $neto);
  2586.                     }
  2587.                 }
  2588.             }
  2589.         }
  2590.         if (!empty($arrayBools['payment'])) {
  2591.             foreach ($arrayBools['payment'] as $key => $item) {
  2592.                 if ($item == 'true') {
  2593.                     $boolMakeInvoice = true;
  2594.                     $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  2595.                     $itemInvoice = new MdvDocInvoiceItems();
  2596.                     $itemInvoice->setFileId($id);
  2597.                     $itemInvoice->setInvoiceId($invoiceId);
  2598.                     $itemInvoice->setItemType('PAYMENT');
  2599.                     $itemInvoice->setPayControlId($key);
  2600.                     $itemInvoice->setPayAmount($pago->getAmount());
  2601.                     $itemInvoice->setPayVat($pago->getVat());
  2602.                     $itemInvoice->setPayAmountTotal($pago->getAmountTotal());
  2603.                     $itemInvoice->setPayDatePayAt($pago->getDatePayAt());
  2604.                     $itemInvoice->setPayWayToPay($pago->getWayToPay());
  2605.                     $itemInvoice->setCreatedAt(new DateTime('now'));
  2606.                     $itemInvoice->setUpdatedAt(new DateTime('now'));
  2607.                     $itemInvoice->setCreatedId($user_id);
  2608.                     $itemInvoice->setUpdatedId($user_id);
  2609.                     $em->persist($itemInvoice);
  2610.                     $em->flush();
  2611.                     //Acumulamos neto e iva para la factura
  2612.                     //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  2613.                 }
  2614.             }
  2615.         }
  2616.         //FIN: Creamos los items de la factura
  2617.         $valorTotal = $valorTotalNet + $valorVat;
  2618.         //INICIO: Creamos la factura
  2619.         $newInvoice = new MdvDocInvoice();
  2620.         $newInvoice->setNumber("MDV-".date('dmy')."-".$id);
  2621.         $newInvoice->setDateAt(new DateTime('now'));
  2622.         $newInvoice->setType('Invoice');
  2623.         $newInvoice->setFileId($id);
  2624.         $newInvoice->setTotalNet($valorTotalNet);
  2625.         $newInvoice->setVat($valorVat);
  2626.         $newInvoice->setTotal($valorTotal);
  2627.         $newInvoice->setCreatedAt(new DateTime('now'));
  2628.         $newInvoice->setCreatedId($user_id);
  2629.         $newInvoice->setUpdatedAt(new DateTime('now'));
  2630.         $newInvoice->setUpdatedId($user_id);
  2631.         $newInvoice->setMaster('master');
  2632.         if ($boolMakeInvoice){
  2633.             // Solo se debe crear factura si hay un elemento
  2634.             $em->persist($newInvoice);
  2635.             $em->flush();
  2636.             $newInvoice->setNumber($newInvoice->getNumber().'-'.$newInvoice->getId());
  2637.             $em->persist($newInvoice);
  2638.             $em->flush();
  2639.         } else {
  2640.             // Se llama a la vista de facturas del expediente
  2641.             return $this->redirectToRoute('mdv_view_invoices',array('id' => $id));
  2642.         }
  2643.         //FIN: Creamos la factura
  2644.         $data = array();
  2645.         $data = $this->baseInvoiceSelectedFile($id, $arrayBools,'I'.$newInvoice->getId());
  2646.         $paymentsClient = new MdvPaymentsClient();
  2647.         $paymentsClient->setFileId($id);
  2648.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  2649.         $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  2650.         $services = $em->getRepository(MdvServices::class)->findByFileId($id);
  2651.         $proforma = $em->getRepository(MdvDocProforma::class)->findOneByFileId($id);
  2652.         $reservaInv = new MdvDocInvoice();
  2653.         $reservaInv->setFileId($id);
  2654.         $idProducts = array();
  2655.         $productsBoolToInvoice = array();
  2656.         if (array_key_exists('lounge',$data['datasupplier'])) {
  2657.             foreach ($data['datasupplier']['lounge'] as $item) {
  2658.                 $idProducts[] = $item['id'];
  2659.                 $productsBoolToInvoice[] = 1;
  2660.             }
  2661.             $idProducts = implode(',', $idProducts);
  2662.             $productsBoolToInvoice = implode(',', $productsBoolToInvoice);
  2663.         } else {
  2664.             $idProducts = '0';
  2665.             $productsBoolToInvoice = '0';
  2666.         }
  2667.         $idPayments = array();
  2668.         $paymentsBoolToInvoice = array();
  2669.         if (array_key_exists('payment',$data['datasupplier'])) {
  2670.             foreach ($data['datasupplier']['payment'] as $item) {
  2671.                 $idPayments[] = $item['id'];
  2672.                 $paymentsBoolToInvoice[] = 1;
  2673.             }
  2674.             $idPayments = implode(',', $idPayments);
  2675.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  2676.             // Se han generado dos arreglos, cada uno dividido en 2 strings
  2677.             // array( [5]=>0, [8]=>1, [10]=>0)  ==>> '5,8,10' y '0,1,0'
  2678.             // Para manipularlos entre la vista y el JS
  2679.         } else {
  2680.             $idPayments = '0';
  2681.             $paymentsBoolToInvoice = '0';
  2682.         }
  2683.         // Una factura solo se puede rectificar una vez
  2684.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('number' => 'REC-'.$newInvoice->getNumber()));
  2685.         $boolToRec = empty($rectf);
  2686.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  2687.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($id);
  2688.         foreach ($allInvoicesRec as $item){
  2689.             array_push($allInvoices, $item);
  2690.         }
  2691.         $allProformas = $em->getRepository(MdvDocProforma::class)->findByFileId($id);
  2692.         if(empty($data['type'])){$data['type'] = 'Invoice';}
  2693.         if (array_key_exists('paymentSubTotal', $data['datasupplier'])){
  2694.             $paymentInvoice = $data['datasupplier']['paymentSubTotal']['sumSubT'];
  2695.         } else {
  2696.             $paymentInvoice = 0;
  2697.         }
  2698.         return $this->redirectToRoute('mdv_view_invoice',
  2699.             array(
  2700.                 'id' => $newInvoice->getId()
  2701.             )
  2702.         );
  2703.     }
  2704.     /**
  2705.      * @Route("/newinvoicefromproforma/{id}", name="mdv_new_invoice_from_proforma")
  2706.      * Crear nueva factura con los elementos seleccionados en una proforma
  2707.      */
  2708.     public function generateNewInvoiceFromProformaAction($id, Request $request)
  2709.     {
  2710.         $em = $this->getDoctrine()->getManager();
  2711.         $proformaOrigen = $em->getRepository(MdvDocProforma::class)->findOneById($id);
  2712.         //INICIO: Buscamos los elementos de la proforma y verificamos disponibilidad
  2713.         $proformaOrigenItems = $em->getRepository(MdvDocProformaItems::class)->findByProformaId($id);
  2714.         $algunItemEstaFacturado = false;
  2715.         foreach ($proformaOrigenItems as $item){
  2716.             if ($item->getType() == 'PRODUCT'){
  2717.                 $estaFacturado = $em->getRepository(MdvDocInvoiceItems::class)->findByPrdControlId($item->getControlId());
  2718.                 if (!empty($estaFacturado)){$algunItemEstaFacturado = true;}
  2719.             }
  2720.             if ($item->getType() == 'PAYMENT'){
  2721.                 $estaFacturado = $em->getRepository(MdvDocInvoiceItems::class)->findByPayControlId($item->getControlId());
  2722.                 if (!empty($estaFacturado)){$algunItemEstaFacturado = true;}
  2723.             }
  2724.             if ($item->getType() == 'SERVICE'){
  2725.                 $estaFacturado = $em->getRepository(MdvDocInvoiceItems::class)->findByPayControlId($item->getControlId());
  2726.                 if (!empty($estaFacturado)){$algunItemEstaFacturado = true;}
  2727.             }
  2728.         }
  2729.         //FIN: Buscamos los elementos de la proforma y verificamos disponibilidad
  2730.         if ($algunItemEstaFacturado){
  2731.             $id = $proformaOrigen->getFileId();
  2732.             return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  2733.                 array(
  2734.                     'id' => $id,
  2735.                     'docType' => 'Error Elementos ya Facturados',
  2736.                     'proforma' => '',
  2737.                     'company' => '',
  2738.                     'clients' => '',
  2739.                     'date' => '',
  2740.                     'number' => '',
  2741.                     'invoice' => null,
  2742.                     'fid' => '',
  2743.                     'boolToRec' => false,
  2744.                     'proformas' => '',
  2745.                     'currency' => '',
  2746.                     'totales_neto' => '',
  2747.                     'totales' => '',
  2748.                     'balance' => '',
  2749.                     'bases_imponibles' => '',
  2750.                     'services' => null,
  2751.                     'facturas' => null,
  2752.                     'paymentNotIvoiced' => null,
  2753.                     'paymentsAll' => null,
  2754.                     'totales_global_servicios_con_iva' => 0, //$data['totales_global_servicios_con_iva'],
  2755.                     'totales_global_servicios_neto' => 0, //$data['totales_global_servicios_neto'],
  2756.                     'totales_global_servicios_iva' => 0, //$data['totales_global_servicios_iva'],
  2757.                     'sumatoria_totales_global_con_iva' => 0, //$data['sumatoria_totales_global_con_iva'],
  2758.                     'sumatoria_totales_global_neto' => 0, //$data['sumatoria_totales_global_neto'],
  2759.                     'sumatoria_totales_global_iva' => 0, //$data['sumatoria_totales_global_iva'],
  2760.                 ));
  2761.         } else {
  2762.             // Se genera y muestra en nueva pantalla la nueva proforma
  2763.             $arrayBools = array(
  2764.                 'product' => null,
  2765.                 'service' => null,
  2766.                 'payment' => null,
  2767.             );
  2768.             foreach ($proformaOrigenItems as $item){
  2769.                 if ($item->getType() == 'PRODUCT'){ $arrayBools['product'][$item->getControlId()] = 'true'; }
  2770.                 if ($item->getType() == 'SERVICE'){ $arrayBools['service'][$item->getControlId()] = 'true'; }
  2771.                 if ($item->getType() == 'PAYMENT'){ $arrayBools['payment'][$item->getControlId()] = 'true'; }
  2772.             }
  2773. //            $arrayBools['product'] = $request->request->get('inv_lounge_hidden_bool');
  2774. //            $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  2775. //            $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  2776.             $id = $proformaOrigen->getFileId();
  2777.             //INICIO: buscamos el ID de la factura
  2778.             $allFacturas = $em->getRepository(MdvDocInvoice::class)->findAll();
  2779.             if (!empty($allFacturas)) {
  2780.                 $invoiceId = end($allFacturas)->getId() + 1;
  2781.             } else {
  2782.                 $invoiceId = 1;
  2783.             }
  2784.             $boolMakeInvoice = false;  // Control para verificar si se debe hacer factura o todos los indicadores (sala, servicios y pagos) estaban en falso
  2785.             //FIN: buscamos el ID de la factura
  2786.             /* Obtengo usuario logueado */
  2787.             $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  2788.             $user_id = $user_logueado->getId();
  2789.             //Acumuladores de datos globales de la factura
  2790.             $valorTotalNet = 0;
  2791.             $valorVat = 0;
  2792.             //INICIO: Creamos los items de la factura
  2793.             if (!empty($arrayBools['product'])) {
  2794.                 foreach ($arrayBools['product'] as $key => $item) {
  2795.                     if ($item == 'true') {
  2796.                         $boolMakeInvoice = true;
  2797.                         $sala = $em->getRepository(MdvProductFile::class)->findOneById($key);
  2798.                         $itemInvoice = new MdvDocInvoiceItems();
  2799.                         $itemInvoice->setFileId($id);
  2800.                         $itemInvoice->setInvoiceId($invoiceId);
  2801.                         $itemInvoice->setItemType('PRODUCT');
  2802.                         $itemInvoice->setPrdControlId($key);
  2803.                         $itemInvoice->setPrdName($sala->getProductName());
  2804.                         $itemInvoice->setPrdProductId($sala->getProductId());
  2805.                         $itemInvoice->setPrdProductId($sala->getProductId());
  2806.                         $itemInvoice->setPrdDateStart($sala->getDateStart());
  2807.                         $itemInvoice->setPrdDateEnd($sala->getDateEnd());
  2808.                         $itemInvoice->setPrdServicePrice($sala->getServicePrice());
  2809.                         $itemInvoice->setPrdSubTotalPrice($sala->getSubTotalPrice());
  2810.                         $itemInvoice->setPrdQty($sala->getUnits());
  2811.                         $itemInvoice->setPrdPax($sala->getPax());
  2812.                         $itemInvoice->setCreatedId($user_id);
  2813.                         $itemInvoice->setCreatedAt(new DateTime('now'));
  2814.                         $itemInvoice->setUpdatedId($user_id);
  2815.                         $itemInvoice->setUpdatedAt(new DateTime('now'));
  2816.                         $itemInvoice->setPrdHourStart($sala->getHourStart());
  2817.                         $itemInvoice->setPrdMinStart($sala->getMinStart());
  2818.                         $itemInvoice->setPrdHourEnd($sala->getHourEnd());
  2819.                         $itemInvoice->setPrdMinEnd($sala->getMinEnd());
  2820.                         $itemInvoice->setPrdIva($sala->getIva());
  2821.                         $em->persist($itemInvoice);
  2822.                         $em->flush();
  2823.                         //Acumulamos neto e iva para la factura
  2824. //                        $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdServicePrice();
  2825.                         $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdSubTotalPrice();
  2826. //                        $valorVat = $valorVat + round(($itemInvoice->getPrdServicePrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  2827.                         $valorVat = $valorVat + round(($itemInvoice->getPrdSubTotalPrice() * 0.21), 2, PHP_ROUND_HALF_UP);
  2828.                     }
  2829.                 }
  2830.             }
  2831.             if (!empty($arrayBools['service'])) {
  2832.                 foreach ($arrayBools['service'] as $key => $item) {
  2833.                     if ($item == 'true') {
  2834.                         $boolMakeInvoice = true;
  2835.                         $servicio = $em->getRepository(MdvServices::class)->findOneById($key);
  2836.                         $itemInvoice = new MdvDocInvoiceItems();
  2837.                         $itemInvoice->setFileId($id);
  2838.                         $itemInvoice->setInvoiceId($invoiceId);
  2839.                         $itemInvoice->setItemType('SERVICE');
  2840.                         $itemInvoice->setSrvControlId($key);
  2841.                         $itemInvoice->setSrvSupplierId($servicio->getSupplierId());
  2842.                         $itemInvoice->setSrvServiceId($servicio->getServiceId());
  2843.                         $itemInvoice->setSrvServiceCatId($servicio->getServiceCatId());
  2844.                         $itemInvoice->setSrvServiceCatName($servicio->getServiceCatName());
  2845.                         $itemInvoice->setSrvPrice($servicio->getPrice());
  2846.                         $itemInvoice->setSrvCurrency($servicio->getCurrency());
  2847.                         $itemInvoice->setSrvUnits($servicio->getUnits());
  2848.                         $itemInvoice->setSrvOpCommission($servicio->getOpCommission());
  2849.                         $itemInvoice->setSrvCommission($servicio->getCommission());
  2850.                         $itemInvoice->setSrvOpOver($servicio->getOpOver());
  2851.                         $itemInvoice->setSrvOver($servicio->getOver());
  2852.                         $itemInvoice->setSrvOpIva($servicio->getOpIva());
  2853.                         $itemInvoice->setSrvIva($servicio->getIva());
  2854.                         $itemInvoice->setSrvPax($servicio->getPax());
  2855.                         $itemInvoice->setSrvHour($servicio->getHour());
  2856.                         $itemInvoice->setSrvDateInAt($servicio->getDateInAt());
  2857.                         $itemInvoice->setSrvDateOutAt($servicio->getDateOutAt());
  2858.                         $itemInvoice->setSrvContcolor($servicio->getContcolor());
  2859.                         $itemInvoice->setSrvRank($servicio->getRank());
  2860.                         $itemInvoice->setSrvAssistantId($servicio->getAssistantId());
  2861.                         $itemInvoice->setSrvActivityId($servicio->getActivityId());
  2862.                         $itemInvoice->setSrvPay($servicio->getPay());
  2863.                         $itemInvoice->setCreatedAt(new DateTime('now'));
  2864.                         $itemInvoice->setUpdatedAt(new DateTime('now'));
  2865.                         $itemInvoice->setCreatedId($user_id);
  2866.                         $itemInvoice->setUpdatedId($user_id);
  2867.                         $em->persist($itemInvoice);
  2868.                         $em->flush();
  2869.                         //Acumulamos neto e iva para la factura
  2870.                         if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())) {
  2871.                             $subtotal = 0;
  2872.                             $neto = 0;
  2873.                             $subtotalService = 0;
  2874.                         } else {
  2875.                             $subtotalService = $itemInvoice->getSrvPrice();
  2876.                             $subneto = $itemInvoice->getSrvPrice();
  2877.                             // Commission
  2878.                             if ($itemInvoice->getSrvOpCommission() == '1') {
  2879.                                 $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission() / 100));
  2880.                                 $subneto = $subneto * (1 + ($itemInvoice->getSrvCommission() / 100));
  2881.                             } else {
  2882.                                 $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission() / 100));
  2883.                                 $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission() / 100));
  2884.                             }
  2885.                             // Over
  2886.                             if ($itemInvoice->getSrvOpOver() == '1') {
  2887.                                 $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  2888.                                 $subneto = $subneto + $itemInvoice->getSrvOver();
  2889.                             } else {
  2890.                                 $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  2891.                                 $subneto = $subneto - $itemInvoice->getSrvOver();
  2892.                             }
  2893.                             // IVA
  2894.                             if ($itemInvoice->getSrvOpIva() == '1') {
  2895.                                 $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva() / 100));
  2896.                             } else {
  2897.                                 $subtotalService = $itemInvoice->getSrvPrice();
  2898.                                 $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  2899.                             }
  2900.                             switch ($itemInvoice->getSrvServiceCatId()) {
  2901.                                 case 1: // Alojamiento
  2902.                                     // el numero de noches $numNoches; precio unitario $subneto
  2903.                                     $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  2904.                                     // La personas no afectan este calculo
  2905.                                     $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  2906.                                     $subnetoUnit = $subneto;
  2907.                                     $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  2908.                                     break;
  2909.                                 case 2: //Actividades
  2910.                                     // El número de personas es considerado en el calculo
  2911.                                     $pax = $itemInvoice->getSrvPax();
  2912.                                     if (empty($pax) or $pax == "0") {
  2913.                                         $pax = 1;
  2914.                                     }
  2915.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2916.                                     if ($days > 1) {
  2917.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2918.                                     } else {
  2919.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2920.                                     }
  2921.                                     $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  2922.                                     $subnetoUnit = $subneto;
  2923.                                     $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  2924.                                     break;
  2925.                                 case 3: // AV
  2926.                                     $pax = $itemInvoice->getSrvPax();
  2927.                                     if (empty($pax) or $pax == "0") {
  2928.                                         $pax = 1;
  2929.                                     }
  2930.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2931.                                     if ($days > 1) {
  2932.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2933.                                     } else {
  2934.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2935.                                     }
  2936.                                     $unitsServ = $itemInvoice->getSrvUnits();
  2937.                                     if (empty($unitsServ) or $unitsServ == "0") {
  2938.                                         $unitsServ = 1;
  2939.                                     }
  2940.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2941.                                     $subnetoUnit = $subneto;
  2942.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  2943.                                     break;
  2944.                                 case 4: //Creative
  2945.                                     $pax = $itemInvoice->getSrvPax();
  2946.                                     if (empty($pax) or $pax == "0") {
  2947.                                         $pax = 1;
  2948.                                     }
  2949.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2950.                                     if ($days > 1) {
  2951.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2952.                                     } else {
  2953.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2954.                                     }
  2955.                                     $unitsServ = $itemInvoice->getSrvUnits();
  2956.                                     if (empty($unitsServ) or $unitsServ == "0") {
  2957.                                         $unitsServ = 1;
  2958.                                     }
  2959.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2960.                                     $subnetoUnit = $subneto;
  2961.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  2962.                                     break;
  2963.                                 case 5: //Cruise
  2964.                                     $pax = $itemInvoice->getSrvPax();
  2965.                                     if (empty($pax) or $pax == "0") {
  2966.                                         $pax = 1;
  2967.                                     }
  2968.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  2969.                                     if ($days > 1) {
  2970.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2971.                                     } else {
  2972.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2973.                                     }
  2974.                                     $unitsServ = $itemInvoice->getSrvUnits();
  2975.                                     if (empty($unitsServ) or $unitsServ == "0") {
  2976.                                         $unitsServ = 1;
  2977.                                     }
  2978.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2979.                                     $subnetoUnit = $subneto;
  2980.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  2981.                                     break;
  2982.                                 case 6: //Entertaiment
  2983.                                     $pax = $itemInvoice->getSrvPax();
  2984.                                     if (empty($pax) or $pax == "0") {
  2985.                                         $pax = 1;
  2986.                                     }
  2987.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  2988.                                     if ($days > 1) {
  2989.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  2990.                                     } else {
  2991.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  2992.                                     }
  2993.                                     $unitsServ = $itemInvoice->getSrvUnits();
  2994.                                     if (empty($unitsServ) or $unitsServ == "0") {
  2995.                                         $unitsServ = 1;
  2996.                                     }
  2997.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  2998.                                     $subnetoUnit = $subneto;
  2999.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3000.                                     break;
  3001.                                 case 7: // Gifts
  3002.                                     $pax = $itemInvoice->getSrvPax();
  3003.                                     if (empty($pax) or $pax == "0") {
  3004.                                         $pax = 1;
  3005.                                     }
  3006.                                     $days = 1;
  3007.                                     if ($days > 1) {
  3008.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3009.                                     } else {
  3010.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3011.                                     }
  3012.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3013.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3014.                                         $unitsServ = 1;
  3015.                                     }
  3016.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3017.                                     $subnetoUnit = $subneto;
  3018.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3019.                                     break;
  3020.                                 case 8: //Guide
  3021.                                     $pax = $itemInvoice->getSrvPax();
  3022.                                     if (empty($pax) or $pax == "0") {
  3023.                                         $pax = 1;
  3024.                                     }
  3025.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3026.                                     if ($days > 1) {
  3027.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3028.                                     } else {
  3029.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3030.                                     }
  3031.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3032.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3033.                                         $unitsServ = 1;
  3034.                                     }
  3035.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3036.                                     $subnetoUnit = $subneto;
  3037.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3038.                                     break;
  3039.                                 case 9: //Itineraries
  3040.                                     $pax = $itemInvoice->getSrvPax();
  3041.                                     if (empty($pax) or $pax == "0") {
  3042.                                         $pax = 1;
  3043.                                     }
  3044.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3045.                                     if ($days > 1) {
  3046.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3047.                                     } else {
  3048.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3049.                                     }
  3050.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3051.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3052.                                         $unitsServ = 1;
  3053.                                     }
  3054.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3055.                                     $subnetoUnit = $subneto;
  3056.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3057.                                     break;
  3058.                                 case 10: //Lounge  -- No Aplica
  3059.                                     break;
  3060.                                 case 11: //Menu
  3061.                                     $pax = $itemInvoice->getSrvPax();
  3062.                                     if (empty($pax) or $pax == "0") {
  3063.                                         $pax = 1;
  3064.                                     }
  3065.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3066.                                     if ($days > 1) {
  3067.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3068.                                     } else {
  3069.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3070.                                     }
  3071.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3072.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3073.                                         $unitsServ = 1;
  3074.                                     }
  3075.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3076.                                     $subnetoUnit = $subneto;
  3077.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3078.                                     break;
  3079.                                 case 12: //Others
  3080.                                     $pax = $itemInvoice->getSrvPax();
  3081.                                     if (empty($pax) or $pax == "0") {
  3082.                                         $pax = 1;
  3083.                                     }
  3084.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3085.                                     if ($days > 1) {
  3086.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3087.                                     } else {
  3088.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3089.                                     }
  3090.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3091.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3092.                                         $unitsServ = 1;
  3093.                                     }
  3094.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3095.                                     $subnetoUnit = $subneto;
  3096.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3097.                                     break;
  3098.                                 case 13: //Transport
  3099.                                     $pax = $itemInvoice->getSrvPax();
  3100.                                     if (empty($pax) or $pax == "0") {
  3101.                                         $pax = 1;
  3102.                                     }
  3103.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3104.                                     if ($days > 1) {
  3105.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3106.                                     } else {
  3107.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3108.                                     }
  3109.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3110.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3111.                                         $unitsServ = 1;
  3112.                                     }
  3113.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3114.                                     $subnetoUnit = $subneto;
  3115.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3116.                                     break;
  3117.                                 case 14: //Technology
  3118.                                     $pax = $itemInvoice->getSrvPax();
  3119.                                     if (empty($pax) or $pax == "0") {
  3120.                                         $pax = 1;
  3121.                                     }
  3122.                                     $days = 1;
  3123.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3124.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3125.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3126.                                         $unitsServ = 1;
  3127.                                     }
  3128.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3129.                                     $subnetoUnit = $subneto;
  3130.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3131.                                     break;
  3132.                                 case 15: //Assisstant
  3133.                                     $pax = $itemInvoice->getSrvPax();
  3134.                                     if (empty($pax) or $pax == "0") {
  3135.                                         $pax = 1;
  3136.                                     }
  3137.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3138.                                     if ($days > 1) {
  3139.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3140.                                     } else {
  3141.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3142.                                     }
  3143.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3144.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3145.                                         $unitsServ = 1;
  3146.                                     }
  3147.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3148.                                     $subnetoUnit = $subneto;
  3149.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3150.                                     break;
  3151.                                 case 16: //DDR
  3152.                                     $pax = $itemInvoice->getSrvPax();
  3153.                                     if (empty($pax) or $pax == "0") {
  3154.                                         $pax = 1;
  3155.                                     }
  3156.                                     $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3157.                                     if ($days > 1) {
  3158.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3159.                                     } else {
  3160.                                         $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3161.                                     }
  3162.                                     $unitsServ = $itemInvoice->getSrvUnits();
  3163.                                     if (empty($unitsServ) or $unitsServ == "0") {
  3164.                                         $unitsServ = 1;
  3165.                                     }
  3166.                                     $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3167.                                     $subnetoUnit = $subneto;
  3168.                                     $subneto = $subneto * $days * $unitsServ * $pax;
  3169.                                     break;
  3170.                                 default:
  3171.                                     break;
  3172.                             }
  3173.                             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  3174.                             $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  3175.                             $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  3176.                             $valorTotalNet = $valorTotalNet + $neto;
  3177.                             $valorVat = $valorVat + ($subtotal - $neto);
  3178.                         }
  3179.                     }
  3180.                 }
  3181.             }
  3182.             if (!empty($arrayBools['payment'])) {
  3183.                 foreach ($arrayBools['payment'] as $key => $item) {
  3184.                     if ($item == 'true') {
  3185.                         $boolMakeInvoice = true;
  3186.                         $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  3187.                         $itemInvoice = new MdvDocInvoiceItems();
  3188.                         $itemInvoice->setFileId($id);
  3189.                         $itemInvoice->setInvoiceId($invoiceId);
  3190.                         $itemInvoice->setItemType('PAYMENT');
  3191.                         $itemInvoice->setPayControlId($key);
  3192.                         $itemInvoice->setPayAmount($pago->getAmount());
  3193.                         $itemInvoice->setPayVat($pago->getVat());
  3194.                         $itemInvoice->setPayAmountTotal($pago->getAmountTotal());
  3195.                         $itemInvoice->setPayDatePayAt($pago->getDatePayAt());
  3196.                         $itemInvoice->setPayWayToPay($pago->getWayToPay());
  3197.                         $itemInvoice->setCreatedAt(new DateTime('now'));
  3198.                         $itemInvoice->setUpdatedAt(new DateTime('now'));
  3199.                         $itemInvoice->setCreatedId($user_id);
  3200.                         $itemInvoice->setUpdatedId($user_id);
  3201.                         $em->persist($itemInvoice);
  3202.                         $em->flush();
  3203.                         //Acumulamos neto e iva para la factura
  3204.                         //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  3205.                     }
  3206.                 }
  3207.             }
  3208.             //FIN: Creamos los items de la factura
  3209.             $valorTotal = $valorTotalNet + $valorVat;
  3210.             //INICIO: Creamos la factura
  3211.             $newInvoice = new MdvDocInvoice();
  3212.             $newInvoice->setNumber('MDV-' . date('dmy') . "-" . $id);
  3213.             $newInvoice->setDateAt(new DateTime('now'));
  3214.             $newInvoice->setType('Invoice');
  3215.             $newInvoice->setFileId($id);
  3216.             $newInvoice->setTotalNet($valorTotalNet);
  3217.             $newInvoice->setVat($valorVat);
  3218.             $newInvoice->setTotal($valorTotal);
  3219.             $newInvoice->setCreatedAt(new DateTime('now'));
  3220.             $newInvoice->setCreatedId($user_id);
  3221.             $newInvoice->setUpdatedAt(new DateTime('now'));
  3222.             $newInvoice->setUpdatedId($user_id);
  3223.             $newInvoice->setMaster('master');
  3224.             if ($boolMakeInvoice) {
  3225.                 // Solo se debe crear factura si hay un elemento
  3226.                 $em->persist($newInvoice);
  3227.                 $em->flush();
  3228.                 $newInvoice->setNumber($newInvoice->getNumber() . '-' . $newInvoice->getId());
  3229.                 $em->persist($newInvoice);
  3230.                 $em->flush();
  3231.             } else {
  3232.                 // Se llama a la vista de facturas del expediente
  3233.                 return $this->redirectToRoute('mdv_view_invoices', array('id' => $id));
  3234.             }
  3235.             //FIN: Creamos la factura
  3236.             $data = array();
  3237.             $data = $this->baseInvoiceSelectedFile($id, $arrayBools, 'I' . $newInvoice->getId());
  3238.             $paymentsClient = new MdvPaymentsClient();
  3239.             $paymentsClient->setFileId($id);
  3240.             $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  3241.             $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  3242.             $services = $em->getRepository(MdvServices::class)->findByFileId($id);
  3243.             $proforma = $em->getRepository(MdvDocProforma::class)->findOneByFileId($id);
  3244.             $reservaInv = new MdvDocInvoice();
  3245.             $reservaInv->setFileId($id);
  3246.             $idProducts = array();
  3247.             $productsBoolToInvoice = array();
  3248.             if (array_key_exists('lounge', $data['datasupplier'])) {
  3249.                 foreach ($data['datasupplier']['lounge'] as $item) {
  3250.                     $idProducts[] = $item['id'];
  3251.                     $productsBoolToInvoice[] = 1;
  3252.                 }
  3253.                 $idProducts = implode(',', $idProducts);
  3254.                 $productsBoolToInvoice = implode(',', $productsBoolToInvoice);
  3255.             } else {
  3256.                 $idProducts = '0';
  3257.                 $productsBoolToInvoice = '0';
  3258.             }
  3259.             $idPayments = array();
  3260.             $paymentsBoolToInvoice = array();
  3261.             if (array_key_exists('payment', $data['datasupplier'])) {
  3262.                 foreach ($data['datasupplier']['payment'] as $item) {
  3263.                     $idPayments[] = $item['id'];
  3264.                     $paymentsBoolToInvoice[] = 1;
  3265.                 }
  3266.                 $idPayments = implode(',', $idPayments);
  3267.                 $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  3268.                 // Se han generado dos arreglos, cada uno dividido en 2 strings
  3269.                 // array( [5]=>0, [8]=>1, [10]=>0)  ==>> '5,8,10' y '0,1,0'
  3270.                 // Para manipularlos entre la vista y el JS
  3271.             } else {
  3272.                 $idPayments = '0';
  3273.                 $paymentsBoolToInvoice = '0';
  3274.             }
  3275.             // Una factura solo se puede rectificar una vez
  3276.             $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy(array('number' => 'REC-' . $newInvoice->getNumber()));
  3277.             $boolToRec = empty($rectf);
  3278.             $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($id);
  3279.             $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($id);
  3280.             foreach ($allInvoicesRec as $item) {
  3281.                 array_push($allInvoices, $item);
  3282.             }
  3283.             $allProformas = $em->getRepository(MdvDocProforma::class)->findByFileId($id);
  3284.             if (empty($data['type'])) {
  3285.                 $data['type'] = 'Invoice';
  3286.             }
  3287.             if (array_key_exists('paymentSubTotal', $data['datasupplier'])) {
  3288.                 $paymentInvoice = $data['datasupplier']['paymentSubTotal']['sumSubT'];
  3289.             } else {
  3290.                 $paymentInvoice = 0;
  3291.             }
  3292.             return $this->redirectToRoute('mdv_view_invoice',
  3293.                 array(
  3294.                     'id' => $newInvoice->getId()
  3295.                 )
  3296.             );
  3297.         }
  3298.     }
  3299.     
  3300.     /**
  3301.      * @Route("/viewinvoice/{id}",  name="mdv_view_invoice")
  3302.      * Ver una factura
  3303.      */
  3304.     public function viewNewInvoiceAction($id, Request $request)
  3305.     {
  3306.         $em = $this->getDoctrine()->getManager();
  3307.         // Se genera y muestra en nueva pantalla la nueva proforma
  3308.         $arrayBools = array(
  3309.             'product' => null,
  3310.             'service' => null,
  3311.             'payment' => null,
  3312.         );
  3313.         $arrayBools['product'] = $request->request->get('inv_lounge_hidden_bool');
  3314.         $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  3315.         $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  3316.         /* Obtengo usuario logueado */
  3317.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  3318.         $user_id = $user_logueado->getId();
  3319.         //Acumuladores de datos globales de la factura
  3320.         $valorTotalNet = 0;
  3321.         $valorVat = 0;
  3322.         //INICIO: Creamos los items de la factura
  3323.         $arrayInvoicedItems = $em->getRepository(MdvDocInvoiceItems::class)->findByInvoiceId($id);
  3324.         if (!empty($arrayInvoicedItems)) {
  3325.             foreach ($arrayInvoicedItems as $itemInvoice) {
  3326.                 if ($itemInvoice->getItemType() == 'PRODUCT') {
  3327.                     //Acumulamos neto e iva para la factura
  3328. //                    $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdServicePrice();
  3329.                     $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdSubTotalPrice();
  3330. //                    $valorVat = $valorVat + round(($itemInvoice->getPrdServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  3331.                     $valorVat = $valorVat + round(($itemInvoice->getPrdSubTotalPrice() * 0.21),2,PHP_ROUND_HALF_UP);
  3332.                 }
  3333.             }
  3334.         }
  3335.         if (!empty($arrayInvoicedItems)) {
  3336.             foreach ($arrayInvoicedItems as $itemInvoice) {
  3337.                 if ($itemInvoice->getItemType() == 'SERVICE') {
  3338.                     //Acumulamos neto e iva para la factura
  3339.                     if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  3340.                         $subtotal = 0;
  3341.                         $neto = 0;
  3342.                         $subtotalService = 0;
  3343.                     } else {
  3344.                         $subtotalService = $itemInvoice->getSrvPrice();
  3345.                         $subneto = $itemInvoice->getSrvPrice();
  3346.                         // Commission
  3347.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  3348.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  3349.                             $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  3350.                         } else {
  3351.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  3352.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  3353.                         }
  3354.                         // Over
  3355.                         if ($itemInvoice->getSrvOpOver()=='1'){
  3356.                             $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  3357.                             $subneto = $subneto + $itemInvoice->getSrvOver();
  3358.                         } else {
  3359.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  3360.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  3361.                         }
  3362.                         // IVA
  3363.                         if ($itemInvoice->getSrvOpIva()=='1'){
  3364.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  3365.                         } else {
  3366.                             $subtotalService = $itemInvoice->getSrvPrice();
  3367.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  3368.                         }
  3369.                         switch ($itemInvoice->getSrvServiceCatId()){
  3370.                             case 1: // Alojamiento
  3371.                                 // el numero de noches $numNoches; precio unitario $subneto
  3372.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  3373.                                 // La personas no afectan este calculo
  3374.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  3375.                                 $subnetoUnit = $subneto;
  3376.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  3377.                                 break;
  3378.                             case 2: //Actividades
  3379.                                 // El número de personas es considerado en el calculo
  3380.                                 $pax = $itemInvoice->getSrvPax();
  3381.                                 if (empty($pax) or $pax == "0") {
  3382.                                     $pax = 1;
  3383.                                 }
  3384.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3385.                                 if ($days > 1){
  3386.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3387.                                 } else {
  3388.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3389.                                 }
  3390.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  3391.                                 $subnetoUnit = $subneto;
  3392.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  3393.                                 break;
  3394.                             case 3: // AV
  3395.                                 $pax = $itemInvoice->getSrvPax();
  3396.                                 if (empty($pax) or $pax == "0") {
  3397.                                     $pax = 1;
  3398.                                 }
  3399.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3400.                                 if ($days > 1){
  3401.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3402.                                 } else {
  3403.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3404.                                 }
  3405.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3406.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3407.                                     $unitsServ = 1;
  3408.                                 }
  3409.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3410.                                 $subnetoUnit = $subneto;
  3411.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3412.                                 break;
  3413.                             case 4: //Creative
  3414.                                 $pax = $itemInvoice->getSrvPax();
  3415.                                 if (empty($pax) or $pax == "0") {
  3416.                                     $pax = 1;
  3417.                                 }
  3418.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3419.                                 if ($days > 1){
  3420.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3421.                                 } else {
  3422.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3423.                                 }
  3424.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3425.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3426.                                     $unitsServ = 1;
  3427.                                 }
  3428.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3429.                                 $subnetoUnit = $subneto;
  3430.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3431.                                 break;
  3432.                             case 5: //Cruise
  3433.                                 $pax = $itemInvoice->getSrvPax();
  3434.                                 if (empty($pax) or $pax == "0") {
  3435.                                     $pax = 1;
  3436.                                 }
  3437.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  3438.                                 if ($days > 1){
  3439.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3440.                                 } else {
  3441.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3442.                                 }
  3443.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3444.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3445.                                     $unitsServ = 1;
  3446.                                 }
  3447.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3448.                                 $subnetoUnit = $subneto;
  3449.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3450.                                 break;
  3451.                             case 6: //Entertaiment
  3452.                                 $pax = $itemInvoice->getSrvPax();
  3453.                                 if (empty($pax) or $pax == "0") {
  3454.                                     $pax = 1;
  3455.                                 }
  3456.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3457.                                 if ($days > 1){
  3458.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3459.                                 } else {
  3460.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3461.                                 }
  3462.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3463.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3464.                                     $unitsServ = 1;
  3465.                                 }
  3466.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3467.                                 $subnetoUnit = $subneto;
  3468.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3469.                                 break;
  3470.                             case 7: // Gifts
  3471.                                 $pax = $itemInvoice->getSrvPax();
  3472.                                 if (empty($pax) or $pax == "0") {
  3473.                                     $pax = 1;
  3474.                                 }
  3475.                                 $days = 1;
  3476.                                 if ($days > 1){
  3477.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3478.                                 } else {
  3479.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3480.                                 }
  3481.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3482.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3483.                                     $unitsServ = 1;
  3484.                                 }
  3485.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3486.                                 $subnetoUnit = $subneto;
  3487.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3488.                                 break;
  3489.                             case 8: //Guide
  3490.                                 $pax = $itemInvoice->getSrvPax();
  3491.                                 if (empty($pax) or $pax == "0") {
  3492.                                     $pax = 1;
  3493.                                 }
  3494.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3495.                                 if ($days > 1){
  3496.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3497.                                 } else {
  3498.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3499.                                 }
  3500.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3501.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3502.                                     $unitsServ = 1;
  3503.                                 }
  3504.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3505.                                 $subnetoUnit = $subneto;
  3506.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3507.                                 break;
  3508.                             case 9: //Itineraries
  3509.                                 $pax = $itemInvoice->getSrvPax();
  3510.                                 if (empty($pax) or $pax == "0") {
  3511.                                     $pax = 1;
  3512.                                 }
  3513.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3514.                                 if ($days > 1){
  3515.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3516.                                 } else {
  3517.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3518.                                 }
  3519.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3520.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3521.                                     $unitsServ = 1;
  3522.                                 }
  3523.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3524.                                 $subnetoUnit = $subneto;
  3525.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3526.                                 break;
  3527.                             case 10: //Lounge  -- No Aplica
  3528.                                 break;
  3529.                             case 11: //Menu
  3530.                                 $pax = $itemInvoice->getSrvPax();
  3531.                                 if (empty($pax) or $pax == "0") {
  3532.                                     $pax = 1;
  3533.                                 }
  3534.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3535.                                 if ($days > 1){
  3536.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3537.                                 } else {
  3538.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3539.                                 }
  3540.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3541.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3542.                                     $unitsServ = 1;
  3543.                                 }
  3544.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3545.                                 $subnetoUnit = $subneto;
  3546.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3547.                                 break;
  3548.                             case 12: //Others
  3549.                                 $pax = $itemInvoice->getSrvPax();
  3550.                                 if (empty($pax) or $pax == "0") {
  3551.                                     $pax = 1;
  3552.                                 }
  3553.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3554.                                 if ($days > 1){
  3555.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3556.                                 } else {
  3557.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3558.                                 }
  3559.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3560.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3561.                                     $unitsServ = 1;
  3562.                                 }
  3563.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3564.                                 $subnetoUnit = $subneto;
  3565.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3566.                                 break;
  3567.                             case 13: //Transport
  3568.                                 $pax = $itemInvoice->getSrvPax();
  3569.                                 if (empty($pax) or $pax == "0") {
  3570.                                     $pax = 1;
  3571.                                 }
  3572.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3573.                                 if ($days > 1){
  3574.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3575.                                 } else {
  3576.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3577.                                 }
  3578.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3579.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3580.                                     $unitsServ = 1;
  3581.                                 }
  3582.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3583.                                 $subnetoUnit = $subneto;
  3584.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3585.                                 break;
  3586.                             case 14: //Technology
  3587.                                 $pax = $itemInvoice->getSrvPax();
  3588.                                 if (empty($pax) or $pax == "0") {
  3589.                                     $pax = 1;
  3590.                                 }
  3591.                                 $days = 1;
  3592.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3593.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3594.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3595.                                     $unitsServ = 1;
  3596.                                 }
  3597.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3598.                                 $subnetoUnit = $subneto;
  3599.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3600.                                 break;
  3601.                             case 15: //Assisstant
  3602.                                 $pax = $itemInvoice->getSrvPax();
  3603.                                 if (empty($pax) or $pax == "0") {
  3604.                                     $pax = 1;
  3605.                                 }
  3606.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3607.                                 if ($days > 1){
  3608.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3609.                                 } else {
  3610.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3611.                                 }
  3612.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3613.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3614.                                     $unitsServ = 1;
  3615.                                 }
  3616.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3617.                                 $subnetoUnit = $subneto;
  3618.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3619.                                 break;
  3620.                             case 16: //DDR
  3621.                                 $pax = $itemInvoice->getSrvPax();
  3622.                                 if (empty($pax) or $pax == "0") {
  3623.                                     $pax = 1;
  3624.                                 }
  3625.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3626.                                 if ($days > 1){
  3627.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3628.                                 } else {
  3629.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3630.                                 }
  3631.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3632.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3633.                                     $unitsServ = 1;
  3634.                                 }
  3635.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3636.                                 $subnetoUnit = $subneto;
  3637.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3638.                                 break;
  3639.                             default:
  3640.                                 break;
  3641.                         }
  3642.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  3643.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  3644.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  3645.                         $valorTotalNet = $valorTotalNet + $neto;
  3646.                         $valorVat = $valorVat + ($subtotal - $neto);
  3647.                     }
  3648.                 }
  3649.             }
  3650.         }
  3651.         if (!empty($arrayInvoicedItems)) {
  3652.             foreach ($arrayInvoicedItems as $itemInvoice) {
  3653.                 if ($itemInvoice->getItemType() == 'PAYMENT') {
  3654. //                    $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  3655. //
  3656. //                    $itemInvoice = new ReservationInvoiceItems();
  3657. //                    $itemInvoice->setReservationId($id);
  3658. //                    $itemInvoice->setInvoiceId($invoiceId);
  3659. //                    $itemInvoice->setItemType('PAYMENT');
  3660. //                    $itemInvoice->setPayControlId($key);
  3661. //                    $itemInvoice->setPayAmount($pago->getAmount());
  3662. //                    $itemInvoice->setPayDatePayAt($pago->getDatePayAt());
  3663. //                    $itemInvoice->setPayWayToPay($pago->getWayToPay());
  3664. //                    $itemInvoice->setCreatedAt(new DateTime('now'));
  3665. //                    $itemInvoice->setUpdatedAt(new DateTime('now'));
  3666. //                    $itemInvoice->setCreatedId($user_id);
  3667. //                    $itemInvoice->setUpdatedId($user_id);
  3668. //
  3669. //                    $em->persist($itemInvoice);
  3670. //                    $em->flush();
  3671.                     //Acumulamos neto e iva para la factura
  3672.                     //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  3673.                 }
  3674.             }
  3675.         }
  3676.         //FIN: Creamos los items de la factura
  3677.         $valorTotal = $valorTotalNet + $valorVat;
  3678.         //INICIO: Buscamos la factura
  3679.         $newInvoice = $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  3680.         //FIN: Buscamos la factura
  3681.         $data = array();
  3682.         $data = $this->baseInvoiceDoneFile($newInvoice->getFileId(), $id,'I');
  3683.         $paymentsClient = new MdvPaymentsClient();
  3684.         $paymentsClient->setFileId($id);
  3685.         $fileInv = new MdvDocInvoice();
  3686.         $fileInv->setFileId($id);
  3687.         $idProducts = array();
  3688.         $productsBoolToInvoice = array();
  3689.         if (array_key_exists('product',$data['datasupplier'])) {
  3690.             foreach ($data['datasupplier']['product'] as $item) {
  3691.                 $idProducts[] = $item['id'];
  3692.                 $productsBoolToInvoice[] = 1;
  3693.             }
  3694.             $idProducts = implode(',', $idProducts);
  3695.             $productsBoolToInvoice = implode(',', $productsBoolToInvoice);
  3696.         } else {
  3697.             $idProducts = '0';
  3698.             $productsBoolToInvoice = '0';
  3699.         }
  3700.         $idPayments = array();
  3701.         $paymentsBoolToInvoice = array();
  3702.         if (array_key_exists('payment',$data['datasupplier'])) {
  3703.             foreach ($data['datasupplier']['payment'] as $item) {
  3704.                 $idPayments[] = $item['id'];
  3705.                 $paymentsBoolToInvoice[] = 1;
  3706.             }
  3707.             $idPayments = implode(',', $idPayments);
  3708.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  3709.             // Se han generado dos arreglos, cada uno dividido en 2 strings
  3710.             // array( [5]=>0, [8]=>1, [10]=>0)  ==>> '5,8,10' y '0,1,0'
  3711.             // Para manipularlos entre la vista y el JS
  3712.         } else {
  3713.             $idPayments = '0';
  3714.             $paymentsBoolToInvoice = '0';
  3715.         }
  3716.         // Una factura solo se puede rectificar una vez
  3717.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('number' => 'REC-'.$newInvoice->getNumber()));
  3718.         $boolToRec = empty($rectf);
  3719.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($newInvoice->getFileId());
  3720.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($newInvoice->getFileId());
  3721.         foreach ($allInvoicesRec as $item){
  3722.             array_push($allInvoices, $item);
  3723.         }
  3724.         $allProformas = $em->getRepository(MdvDocProforma::class)->findByFileId($newInvoice->getFileId());
  3725.         if(empty($data['type'])){$data['type'] = 'Invoice';}
  3726.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  3727.             array(
  3728.                 'id' => $newInvoice->getFileId(),
  3729.                 'cid' => '',
  3730.                 'fid' => $id,
  3731.                 'invoice' => $newInvoice,
  3732.                 'facturas' => $allInvoices,
  3733.                 'proformas' => $allProformas,
  3734.                 'boolToRec' => $boolToRec,
  3735.                 'numberadmin' => '',
  3736.                 'type' => $data['type'],
  3737.                 'docType' => 'Factura',
  3738.                 'number' => $newInvoice->getId(),
  3739.                 'prefix' => $data['prefix'],
  3740.                 'date' => $data['date'],
  3741.                 'token' =>  $data['token'],
  3742.                 'file' => $data['file'],
  3743.                 'company' => $data['company'],
  3744.                 'clients' => $data['clients'],
  3745.                 'user' => $user_logueado,
  3746.                 'invoicedeposititems' => '',
  3747.                 'arrayItems' => $data['arrayItems'],
  3748.                 'datasupplier' => $data['datasupplier'],
  3749.                 'totales_neto' => $data['totales_neto'],
  3750.                 'totales_iva' => '',
  3751.                 'totales' => $data['totales'],
  3752.                 'bases_imponibles' => $data['bases_imponibles'],
  3753.                 'balance' => $data['balance'],
  3754.                 'paymentInvoice' => $data['paymentInvoice'],
  3755.                 'currency' => $data['currency'],
  3756.             )
  3757.         );
  3758.     }
  3759.     /**
  3760.      * @Route("/dpexternal/printinvoice/{id}", name="mdv_print_invoice")
  3761.      * Imprimir una factura
  3762.      */
  3763.     public function printInvoiceAction( $id, Request $request)
  3764.     {
  3765.         $em = $this->getDoctrine()->getManager();
  3766.         // Se genera y muestra en nueva pantalla la nueva proforma
  3767.         $arrayBools = array(
  3768.             'product' => null,
  3769.             'service' => null,
  3770.             'payment' => null,
  3771.         );
  3772.         $arrayBools['product'] = $request->request->get('inv_lounge_hidden_bool');
  3773.         $arrayBools['service'] = $request->request->get('inv_service_hidden_bool');
  3774.         $arrayBools['payment'] = $request->request->get('inv_payment_hidden_bool');
  3775.         /* Obtengo usuario logueado */
  3776. //        $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  3777. //        $user_id = $user_logueado->getId();
  3778.         //Acumuladores de datos globales de la factura
  3779.         $valorTotalNet = 0;
  3780.         $valorVat = 0;
  3781.         //INICIO: Creamos los items de la factura
  3782.         $arrayInvoicedItems = $em->getRepository(MdvDocInvoiceItems::class)->findByInvoiceId($id);
  3783.         if (!empty($arrayInvoicedItems)) {
  3784.             foreach ($arrayInvoicedItems as $itemInvoice) {
  3785.                 if ($itemInvoice->getItemType() == 'PRODUCT') {
  3786.                     //Acumulamos neto e iva para la factura
  3787.                     $valorTotalNet = $valorTotalNet + $itemInvoice->getPrdServicePrice();
  3788.                     $valorVat = $valorVat + round(($itemInvoice->getPrdServicePrice() * 0.21),2,PHP_ROUND_HALF_UP);
  3789.                 }
  3790.             }
  3791.         }
  3792.         if (!empty($arrayInvoicedItems)) {
  3793.             foreach ($arrayInvoicedItems as $itemInvoice) {
  3794.                 if ($itemInvoice->getItemType() == 'SERVICE') {
  3795.                     //Acumulamos neto e iva para la factura
  3796.                     if (is_null($itemInvoice->getSrvPrice()) or empty($itemInvoice->getSrvPrice())){
  3797.                         $subtotal = 0;
  3798.                         $neto = 0;
  3799.                         $subtotalService = 0;
  3800.                     } else {
  3801.                         $subtotalService = $itemInvoice->getSrvPrice();
  3802.                         $subneto = $itemInvoice->getSrvPrice();
  3803.                         // Commission
  3804.                         if ($itemInvoice->getSrvOpCommission()=='1'){
  3805.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvCommission()/100));
  3806.                             $subneto = $subneto  * (1 + ($itemInvoice->getSrvCommission()/100));
  3807.                         } else {
  3808.                             $subtotalService = $subtotalService * (1 - ($itemInvoice->getSrvCommission()/100));
  3809.                             $subneto = $subneto * (1 - ($itemInvoice->getSrvCommission()/100));
  3810.                         }
  3811.                         // Over
  3812.                         if ($itemInvoice->getSrvOpOver()=='1'){
  3813.                             $subtotalService = $subtotalService + $itemInvoice->getSrvOver();
  3814.                             $subneto = $subneto + $itemInvoice->getSrvOver();
  3815.                         } else {
  3816.                             $subtotalService = $subtotalService - $itemInvoice->getSrvOver();
  3817.                             $subneto = $subneto - $itemInvoice->getSrvOver();
  3818.                         }
  3819.                         // IVA
  3820.                         if ($itemInvoice->getSrvOpIva()=='1'){
  3821.                             $subtotalService = $subtotalService * (1 + ($itemInvoice->getSrvIva()/100));
  3822.                         } else {
  3823.                             $subtotalService = $itemInvoice->getSrvPrice();
  3824.                             $subneto = ($subneto * 100) / (100 + $itemInvoice->getSrvIva());
  3825.                         }
  3826.                         switch ($itemInvoice->getSrvServiceCatId()){
  3827.                             case 1: // Alojamiento
  3828.                                 // el numero de noches $numNoches; precio unitario $subneto
  3829.                                 $numNoches = (($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days;
  3830.                                 // La personas no afectan este calculo
  3831.                                 $subtotal = $subtotalService * $numNoches * $itemInvoice->getSrvUnits();
  3832.                                 $subnetoUnit = $subneto;
  3833.                                 $subneto = $subneto * $numNoches * $itemInvoice->getSrvUnits();
  3834.                                 break;
  3835.                             case 2: //Actividades
  3836.                                 // El número de personas es considerado en el calculo
  3837.                                 $pax = $itemInvoice->getSrvPax();
  3838.                                 if (empty($pax) or $pax == "0") {
  3839.                                     $pax = 1;
  3840.                                 }
  3841.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3842.                                 if ($days > 1){
  3843.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3844.                                 } else {
  3845.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3846.                                 }
  3847.                                 $subtotal = $subtotalService * $days * $itemInvoice->getSrvUnits();
  3848.                                 $subnetoUnit = $subneto;
  3849.                                 $subneto = $subneto * $days * $itemInvoice->getSrvUnits();
  3850.                                 break;
  3851.                             case 3: // AV
  3852.                                 $pax = $itemInvoice->getSrvPax();
  3853.                                 if (empty($pax) or $pax == "0") {
  3854.                                     $pax = 1;
  3855.                                 }
  3856.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3857.                                 if ($days > 1){
  3858.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3859.                                 } else {
  3860.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3861.                                 }
  3862.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3863.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3864.                                     $unitsServ = 1;
  3865.                                 }
  3866.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3867.                                 $subnetoUnit = $subneto;
  3868.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3869.                                 break;
  3870.                             case 4: //Creative
  3871.                                 $pax = $itemInvoice->getSrvPax();
  3872.                                 if (empty($pax) or $pax == "0") {
  3873.                                     $pax = 1;
  3874.                                 }
  3875.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3876.                                 if ($days > 1){
  3877.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3878.                                 } else {
  3879.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3880.                                 }
  3881.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3882.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3883.                                     $unitsServ = 1;
  3884.                                 }
  3885.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3886.                                 $subnetoUnit = $subneto;
  3887.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3888.                                 break;
  3889.                             case 5: //Cruise
  3890.                                 $pax = $itemInvoice->getSrvPax();
  3891.                                 if (empty($pax) or $pax == "0") {
  3892.                                     $pax = 1;
  3893.                                 }
  3894.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days);
  3895.                                 if ($days > 1){
  3896.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3897.                                 } else {
  3898.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3899.                                 }
  3900.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3901.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3902.                                     $unitsServ = 1;
  3903.                                 }
  3904.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3905.                                 $subnetoUnit = $subneto;
  3906.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3907.                                 break;
  3908.                             case 6: //Entertaiment
  3909.                                 $pax = $itemInvoice->getSrvPax();
  3910.                                 if (empty($pax) or $pax == "0") {
  3911.                                     $pax = 1;
  3912.                                 }
  3913.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3914.                                 if ($days > 1){
  3915.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3916.                                 } else {
  3917.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3918.                                 }
  3919.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3920.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3921.                                     $unitsServ = 1;
  3922.                                 }
  3923.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3924.                                 $subnetoUnit = $subneto;
  3925.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3926.                                 break;
  3927.                             case 7: // Gifts
  3928.                                 $pax = $itemInvoice->getSrvPax();
  3929.                                 if (empty($pax) or $pax == "0") {
  3930.                                     $pax = 1;
  3931.                                 }
  3932.                                 $days = 1;
  3933.                                 if ($days > 1){
  3934.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3935.                                 } else {
  3936.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3937.                                 }
  3938.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3939.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3940.                                     $unitsServ = 1;
  3941.                                 }
  3942.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3943.                                 $subnetoUnit = $subneto;
  3944.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3945.                                 break;
  3946.                             case 8: //Guide
  3947.                                 $pax = $itemInvoice->getSrvPax();
  3948.                                 if (empty($pax) or $pax == "0") {
  3949.                                     $pax = 1;
  3950.                                 }
  3951.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3952.                                 if ($days > 1){
  3953.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3954.                                 } else {
  3955.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3956.                                 }
  3957.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3958.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3959.                                     $unitsServ = 1;
  3960.                                 }
  3961.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3962.                                 $subnetoUnit = $subneto;
  3963.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3964.                                 break;
  3965.                             case 9: //Itineraries
  3966.                                 $pax = $itemInvoice->getSrvPax();
  3967.                                 if (empty($pax) or $pax == "0") {
  3968.                                     $pax = 1;
  3969.                                 }
  3970.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3971.                                 if ($days > 1){
  3972.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3973.                                 } else {
  3974.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3975.                                 }
  3976.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3977.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3978.                                     $unitsServ = 1;
  3979.                                 }
  3980.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  3981.                                 $subnetoUnit = $subneto;
  3982.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  3983.                                 break;
  3984.                             case 10: //Lounge  -- No Aplica
  3985.                                 break;
  3986.                             case 11: //Menu
  3987.                                 $pax = $itemInvoice->getSrvPax();
  3988.                                 if (empty($pax) or $pax == "0") {
  3989.                                     $pax = 1;
  3990.                                 }
  3991.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  3992.                                 if ($days > 1){
  3993.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  3994.                                 } else {
  3995.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  3996.                                 }
  3997.                                 $unitsServ = $itemInvoice->getSrvUnits();
  3998.                                 if (empty($unitsServ) or $unitsServ == "0") {
  3999.                                     $unitsServ = 1;
  4000.                                 }
  4001.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4002.                                 $subnetoUnit = $subneto;
  4003.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4004.                                 break;
  4005.                             case 12: //Others
  4006.                                 $pax = $itemInvoice->getSrvPax();
  4007.                                 if (empty($pax) or $pax == "0") {
  4008.                                     $pax = 1;
  4009.                                 }
  4010.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  4011.                                 if ($days > 1){
  4012.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  4013.                                 } else {
  4014.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  4015.                                 }
  4016.                                 $unitsServ = $itemInvoice->getSrvUnits();
  4017.                                 if (empty($unitsServ) or $unitsServ == "0") {
  4018.                                     $unitsServ = 1;
  4019.                                 }
  4020.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4021.                                 $subnetoUnit = $subneto;
  4022.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4023.                                 break;
  4024.                             case 13: //Transport
  4025.                                 $pax = $itemInvoice->getSrvPax();
  4026.                                 if (empty($pax) or $pax == "0") {
  4027.                                     $pax = 1;
  4028.                                 }
  4029.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  4030.                                 if ($days > 1){
  4031.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  4032.                                 } else {
  4033.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  4034.                                 }
  4035.                                 $unitsServ = $itemInvoice->getSrvUnits();
  4036.                                 if (empty($unitsServ) or $unitsServ == "0") {
  4037.                                     $unitsServ = 1;
  4038.                                 }
  4039.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4040.                                 $subnetoUnit = $subneto;
  4041.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4042.                                 break;
  4043.                             case 14: //Technology
  4044.                                 $pax = $itemInvoice->getSrvPax();
  4045.                                 if (empty($pax) or $pax == "0") {
  4046.                                     $pax = 1;
  4047.                                 }
  4048.                                 $days = 1;
  4049.                                 $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  4050.                                 $unitsServ = $itemInvoice->getSrvUnits();
  4051.                                 if (empty($unitsServ) or $unitsServ == "0") {
  4052.                                     $unitsServ = 1;
  4053.                                 }
  4054.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4055.                                 $subnetoUnit = $subneto;
  4056.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4057.                                 break;
  4058.                             case 15: //Assisstant
  4059.                                 $pax = $itemInvoice->getSrvPax();
  4060.                                 if (empty($pax) or $pax == "0") {
  4061.                                     $pax = 1;
  4062.                                 }
  4063.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  4064.                                 if ($days > 1){
  4065.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  4066.                                 } else {
  4067.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  4068.                                 }
  4069.                                 $unitsServ = $itemInvoice->getSrvUnits();
  4070.                                 if (empty($unitsServ) or $unitsServ == "0") {
  4071.                                     $unitsServ = 1;
  4072.                                 }
  4073.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4074.                                 $subnetoUnit = $subneto;
  4075.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4076.                                 break;
  4077.                             case 16: //DDR
  4078.                                 $pax = $itemInvoice->getSrvPax();
  4079.                                 if (empty($pax) or $pax == "0") {
  4080.                                     $pax = 1;
  4081.                                 }
  4082.                                 $days = ((($itemInvoice->getSrvDateOutAt())->diff($itemInvoice->getSrvDateInAt()))->days + 1);
  4083.                                 if ($days > 1){
  4084.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y'). ' - '.($itemInvoice->getSrvDateOutAt())->format('d/m/Y');
  4085.                                 } else {
  4086.                                     $dateServ = ($itemInvoice->getSrvDateInAt())->format('d/m/Y');
  4087.                                 }
  4088.                                 $unitsServ = $itemInvoice->getSrvUnits();
  4089.                                 if (empty($unitsServ) or $unitsServ == "0") {
  4090.                                     $unitsServ = 1;
  4091.                                 }
  4092.                                 $subtotal = $subtotalService * $days * $unitsServ * $pax;
  4093.                                 $subnetoUnit = $subneto;
  4094.                                 $subneto = $subneto * $days * $unitsServ * $pax;
  4095.                                 break;
  4096.                             default:
  4097.                                 break;
  4098.                         }
  4099.                         // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  4100.                         $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  4101.                         $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  4102.                         $valorTotalNet = $valorTotalNet + $neto;
  4103.                         $valorVat = $valorVat + ($subtotal - $neto);
  4104.                     }
  4105.                 }
  4106.             }
  4107.         }
  4108.         if (!empty($arrayInvoicedItems)) {
  4109.             foreach ($arrayInvoicedItems as $itemInvoice) {
  4110.                 if ($itemInvoice->getItemType() == 'PAYMENT') {
  4111. //                    $pago = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  4112. //
  4113. //                    $itemInvoice = new ReservationInvoiceItems();
  4114. //                    $itemInvoice->setReservationId($id);
  4115. //                    $itemInvoice->setInvoiceId($invoiceId);
  4116. //                    $itemInvoice->setItemType('PAYMENT');
  4117. //                    $itemInvoice->setPayControlId($key);
  4118. //                    $itemInvoice->setPayAmount($pago->getAmount());
  4119. //                    $itemInvoice->setPayDatePayAt($pago->getDatePayAt());
  4120. //                    $itemInvoice->setPayWayToPay($pago->getWayToPay());
  4121. //                    $itemInvoice->setCreatedAt(new DateTime('now'));
  4122. //                    $itemInvoice->setUpdatedAt(new DateTime('now'));
  4123. //                    $itemInvoice->setCreatedId($user_id);
  4124. //                    $itemInvoice->setUpdatedId($user_id);
  4125. //
  4126. //                    $em->persist($itemInvoice);
  4127. //                    $em->flush();
  4128.                     //Acumulamos neto e iva para la factura
  4129.                     //$valorTotalNet = $valorTotalNet - $itemInvoice->getPayAmount();    // El pago no puede afectar el neto de la factura
  4130.                 }
  4131.             }
  4132.         }
  4133.         //FIN: Creamos los items de la factura
  4134.         $valorTotal = $valorTotalNet + $valorVat;
  4135.         //INICIO: Buscamos la factura
  4136.         $newInvoice = $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  4137.         //FIN: Buscamos la factura
  4138.         $data = array();
  4139.         $data = $this->baseInvoiceDoneFile($newInvoice->getFileId(), $id,'I');
  4140.         $paymentsClient = new MdvPaymentsClient();
  4141.         $paymentsClient->setFileId($id);
  4142.         $fileInv = new MdvDocInvoice();
  4143.         $fileInv->setFileId($id);
  4144.         $idProducts = array();
  4145.         $productsBoolToInvoice = array();
  4146.         if (array_key_exists('product',$data['datasupplier'])) {
  4147.             foreach ($data['datasupplier']['product'] as $item) {
  4148.                 $idProducts[] = $item['id'];
  4149.                 $productsBoolToInvoice[] = 1;
  4150.             }
  4151.             $idProducts = implode(',', $idProducts);
  4152.             $productsBoolToInvoice = implode(',', $productsBoolToInvoice);
  4153.         } else {
  4154.             $idProducts = '0';
  4155.             $productsBoolToInvoice = '0';
  4156.         }
  4157.         $idPayments = array();
  4158.         $paymentsBoolToInvoice = array();
  4159.         if (array_key_exists('payment',$data['datasupplier'])) {
  4160.             foreach ($data['datasupplier']['payment'] as $item) {
  4161.                 $idPayments[] = $item['id'];
  4162.                 $paymentsBoolToInvoice[] = 1;
  4163.             }
  4164.             $idPayments = implode(',', $idPayments);
  4165.             $paymentsBoolToInvoice = implode(',', $paymentsBoolToInvoice);
  4166.             // Se han generado dos arreglos, cada uno dividido en 2 strings
  4167.             // array( [5]=>0, [8]=>1, [10]=>0)  ==>> '5,8,10' y '0,1,0'
  4168.             // Para manipularlos entre la vista y el JS
  4169.         } else {
  4170.             $idPayments = '0';
  4171.             $paymentsBoolToInvoice = '0';
  4172.         }
  4173.         // Una factura solo se puede rectificar una vez
  4174.         $rectf = $em->getRepository(MdvDocInvoiceRec::class)->findBy( array('number' => 'REC-'.$newInvoice->getNumber()));
  4175.         $boolToRec = empty($rectf);
  4176.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($newInvoice->getFileId());
  4177.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($newInvoice->getFileId());
  4178.         foreach ($allInvoicesRec as $item){
  4179.             array_push($allInvoices, $item);
  4180.         }
  4181.         $allProformas = $em->getRepository(MdvDocProforma::class)->findByFileId($newInvoice->getFileId());
  4182.         if(empty($data['type'])){$data['type'] = 'Invoice';}
  4183.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  4184.             array(
  4185.                 'id' => $newInvoice->getFileId(),
  4186.                 'cid' => '',
  4187.                 'fid' => $id,
  4188.                 'invoice' => $newInvoice,
  4189.                 'facturas' => $allInvoices,
  4190.                 'proformas' => $allProformas,
  4191.                 'boolToRec' => $boolToRec,
  4192.                 'numberadmin' => '',
  4193.                 'type' => $data['type'],
  4194.                 'docType' => 'Factura',
  4195.                 'number' => $newInvoice->getId(),
  4196.                 'prefix' => $data['prefix'],
  4197.                 'date' => $data['date'],
  4198.                 'token' =>  $data['token'],
  4199.                 'file' => $data['file'],
  4200.                 'company' => $data['company'],
  4201.                 'clients' => $data['clients'],
  4202. //                'user' => $user_logueado,
  4203.                 'invoicedeposititems' => '',
  4204.                 'arrayItems' => $data['arrayItems'],
  4205.                 'datasupplier' => $data['datasupplier'],
  4206.                 'totales_neto' => $data['totales_neto'],
  4207.                 'totales_iva' => '',
  4208.                 'totales' => $data['totales'],
  4209.                 'bases_imponibles' => $data['bases_imponibles'],
  4210.                 'balance' => $data['balance'],
  4211.                 'paymentInvoice' => $data['paymentInvoice'],
  4212.                 'currency' => $data['currency'],
  4213.             )
  4214.         );
  4215.     }
  4216.     /**
  4217.      * @Route("/newinvoicerec/{id}", name="mdv_new_invoice_rec")
  4218.      * Crear una factura rectificativa
  4219.      */
  4220.     public function generateNewInvoiceRecAction($id, Request $request)
  4221.     {
  4222.         $em = $this->getDoctrine()->getManager();
  4223.         $invoice = $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  4224.         if (true){
  4225.             // Verificamos que no exista una factura rectificativa previa sobre la misma factura
  4226.             $previusInvoiceRec = $em->getRepository(MdvDocInvoiceRec::class)->findOneByInvoiceToRec($id);
  4227.             if (!empty($previusInvoiceRec)){ return $this->redirectToRoute('mdv_view_invoice_rec', array('id' => $previusInvoiceRec->getId())); }
  4228.         }
  4229.         $now = new DateTime();
  4230.         /* Obtengo usuario logueado */
  4231.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4232.         $user_id = $user_logueado->getId();
  4233.         $invoiceRec = new MdvDocInvoiceRec();
  4234.         $invoiceRec->setInvoiceToRec($id);
  4235.         $invoiceRec->setCreatedAt($now);
  4236.         $invoiceRec->setUpdatedAt($now);
  4237.         $invoiceRec->setCreatedId($user_id);
  4238.         $invoiceRec->setUpdatedId($user_id);
  4239.         $invoiceRec->setDateAt($now);
  4240.         $invoiceRec->setMaster($invoice->getMaster());
  4241.         $invoiceRec->setNumber('REC-'.$invoice->getNumber());
  4242.         $invoiceRec->setPrefix('R');
  4243.         $invoiceRec->setFileId($invoice->getFileId());
  4244.         $invoiceRec->setTotal($invoice->getTotal()*(-1));
  4245.         $invoiceRec->setTotalNet($invoice->getTotalNet()*(-1));
  4246.         $invoiceRec->setType('Invoice Rec');
  4247.         $invoiceRec->setVat($invoice->getVat()*(-1));
  4248.         $invoiceRec->setBalance($invoice->getBalance()*(-1));
  4249.         $em->persist($invoiceRec);
  4250.         $em->flush();
  4251.         $invoiceItems = $em->getRepository(MdvDocInvoiceItems::class)->findByInvoiceId($id);
  4252.         // Cada elemento se duplica en ItemsRec y se libera asignando ID = 0
  4253.         foreach ($invoiceItems as $item){
  4254.             $itemRec = new MdvDocInvoiceRecItems();
  4255.             $itemRec->setFileId($item->getFileId());
  4256.             $itemRec->setInvoiceId($item->getInvoiceId());
  4257.             $itemRec->setItemType($item->getItemType());
  4258.             $itemRec->setInvoiceRecId($invoiceRec->getId());
  4259.             $itemRec->setPrdControlId($item->getPrdControlId());
  4260.             $itemRec->setPrdName($item->getPrdName());
  4261.             $itemRec->setPrdProductId($item->getPrdProductId());
  4262. //            $itemRec->setPrdIdLounge($item->getPrdIdLounge());
  4263.             $itemRec->setPrdDateStart($item->getPrdDateStart());
  4264.             $itemRec->setPrdDateEnd($item->getPrdDateEnd());
  4265.             $itemRec->setPrdServicePrice($item->getPrdServicePrice()*(-1));
  4266.             $itemRec->setPrdSubTotalPrice($item->getPrdSubTotalPrice()*(-1));
  4267.             $itemRec->setPrdQty($item->getPrdQty());
  4268.             $itemRec->setPrdPax($item->getPrdPax());
  4269.             $itemRec->setPrdType($item->getPrdType());
  4270.             $itemRec->setPrdHourStart($item->getPrdHourStart());
  4271.             $itemRec->setPrdMinStart($item->getPrdMinStart());
  4272.             $itemRec->setPrdHourEnd($item->getPrdHourEnd());
  4273.             $itemRec->setPrdMinEnd($item->getPrdMinEnd());
  4274.             $itemRec->setSrvControlId($item->getSrvControlId());
  4275.             $itemRec->setSrvSupplierId($item->getSrvSupplierId());
  4276.             $itemRec->setSrvServiceId($item->getSrvServiceId());
  4277.             $itemRec->setSrvServiceCatId($item->getSrvServiceCatId());
  4278.             $itemRec->setSrvServiceCatName($item->getSrvServiceCatName());
  4279.             $itemRec->setSrvPrice($item->getSrvPrice()*(-1));
  4280.             $itemRec->setSrvCurrency($item->getSrvCurrency());
  4281.             $itemRec->setSrvUnits($item->getSrvUnits());
  4282.             $itemRec->setSrvOpCommission($item->getSrvOpCommission());
  4283.             $itemRec->setSrvCommission($item->getSrvCommission());
  4284.             $itemRec->setSrvOpOver($item->getSrvOpOver());
  4285.             $itemRec->setSrvOver($item->getSrvOver());
  4286.             $itemRec->setSrvOpIva($item->getSrvOpIva());
  4287.             $itemRec->setSrvIva($item->getSrvIva());
  4288.             $itemRec->setSrvPax($item->getSrvPax());
  4289.             $itemRec->setSrvHour($item->getSrvHour());
  4290.             $itemRec->setSrvDateInAt($item->getSrvDateInAt());
  4291.             $itemRec->setSrvDateOutAt($item->getSrvDateOutAt());
  4292.             $itemRec->setSrvContcolor($item->getSrvContcolor());
  4293.             $itemRec->setSrvRank($item->getSrvRank());
  4294.             $itemRec->setSrvAssistantId($item->getSrvAssistantId());
  4295.             $itemRec->setSrvActivityId($item->getSrvActivityId());
  4296.             $itemRec->setSrvPay($item->getSrvPay());
  4297.             $itemRec->setPayControlId($item->getPayControlId());
  4298.             $itemRec->setPayAmount($item->getPayAmount()*(-1));
  4299.             $itemRec->setPayDatePayAt($item->getPayDatePayAt());
  4300.             $itemRec->setPayWayToPay($item->getPayWayToPay());
  4301.             $itemRec->setPayAmountTotal($item->getPayAmountTotal()*(-1));
  4302.             $itemRec->setPayVat($item->getPayVat());
  4303.             $itemRec->setCreatedId($user_id);
  4304.             $itemRec->setCreatedAt(new DateTime('now'));
  4305.             $itemRec->setUpdatedId($user_id);
  4306.             $itemRec->setUpdatedAt(new DateTime('now'));
  4307.             $em->persist($itemRec);
  4308.             $em->flush();
  4309.             // Se libera asignando ID = 0
  4310.             $item->setPrdControlId(0);
  4311.             $item->setSrvControlId(0);
  4312.             $item->setPayControlId(0);
  4313.             $em->persist($item);
  4314.             $em->flush();
  4315.         }
  4316.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($invoice->getFileId());
  4317.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($invoice->getFileId());
  4318.         foreach ($allInvoicesRec as $item){
  4319.             array_push($allInvoices, $item);
  4320.         }
  4321.         $file = $em->getRepository(MdvFiles::class)->findOneById($invoice->getFileId());
  4322.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  4323.         $data = array();
  4324.         $data = $this->baseInvoiceDoneFile($invoiceRec->getFileId(), $invoiceRec->getId(),'R');
  4325.         $data['clients'][0] = $client;
  4326.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  4327.         // Una factura solo se puede rectificar una vez
  4328. //        $rectf = $em->getRepository('DevelupBundle:MdvInvoiceRec')->findBy( array('number' => 'REC-'.$invoice->getNumber()));
  4329.         $boolToRec = false;
  4330.         return $this->redirectToRoute('mdv_view_invoice_rec',
  4331.             array(
  4332.                 'id' => $invoiceRec->getId()
  4333.             )
  4334.         );
  4335.     }
  4336.     /**
  4337.      * @Route("/viewinvoicerec/{id}", name="mdv_view_invoice_rec")
  4338.      * Ver una factura rectificativa
  4339.      */
  4340.     public function viewInvoiceRecAction($id, Request $request)
  4341.     {
  4342.         $em = $this->getDoctrine()->getManager();
  4343.         $invoiceRec = $em->getRepository(MdvDocInvoiceRec::class)->findOneById($id);
  4344.         /* Obtengo usuario logueado */
  4345.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4346.         $user_id = $user_logueado->getId();
  4347.         $invoiceItems = $em->getRepository(MdvDocInvoiceRecItems::class)->findByInvoiceId($id);
  4348.         // Cada elemento se duplica en ItemsRec y se libera asignando ID = 0
  4349.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($invoiceRec->getFileId());
  4350.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($invoiceRec->getFileId());
  4351.         foreach ($allInvoicesRec as $item){
  4352.             array_push($allInvoices, $item);
  4353.         }
  4354.         $file = $em->getRepository(MdvFiles::class)->findOneById($invoiceRec->getFileId());
  4355.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  4356.         if (empty($client)){
  4357.             $client[0] = new Client();
  4358.             $client[0]->setName('');
  4359.             $client[0]->setTitle('');
  4360.             $client[0]->setIdDocument('');
  4361.             $client[0]->setPopulation('');
  4362.             $client[0]->setRegion('');
  4363.             $client[0]->setCountry('');
  4364.         } else {
  4365.             if (is_numeric($client[0]->getPopulation())){
  4366.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  4367.                 $client[0]->setPopulation($city->getCity());
  4368.             }
  4369.             if (is_numeric($client[0]->getRegion())){
  4370.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  4371.                 $client[0]->setRegion($region->getRegion());
  4372.             }
  4373.             if (is_numeric($client[0]->getCountry())){
  4374.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  4375.                 $client[0]->setCountry($country->getCountry());
  4376.             }
  4377.         }
  4378.         $data = array();
  4379.         $data = $this->baseInvoiceDoneFile($invoiceRec->getFileId(), $id,'R');
  4380.         $data['clients'] = $client;
  4381.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  4382.         // Una factura solo se puede rectificar una vez
  4383. //        $rectf = $em->getRepository('DevelupBundle:MdvInvoiceRec')->findBy( array('number' => 'REC-'.$invoice->getNumber()));
  4384.         $boolToRec = false;
  4385.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep.html.twig',
  4386.             array(
  4387.                 'id' => $invoiceRec->getFileId(),
  4388.                 'cid' => '',
  4389.                 'fid' => $invoiceRec->getId(),
  4390.                 'invoice' => $invoiceRec,
  4391.                 'facturas' => $allInvoices,
  4392.                 'boolToRec' => $boolToRec,
  4393.                 'numberadmin' => '',
  4394.                 'type' => 'Invoice Rec',
  4395.                 'docType' => 'Factura Rectificativa',
  4396.                 'number' => $invoiceRec->getId(),
  4397.                 'prefix' => '',
  4398.                 'date' => $invoiceRec->getDateAt(),
  4399.                 'invoiceIdToRec' => $invoiceRec->getInvoiceToRec(),
  4400.                 'token' => '',
  4401.                 'file' => $file,
  4402.                 'company' => $data['company'],
  4403.                 'clients' => $data['clients'],
  4404.                 'user' => $user_logueado,
  4405.                 'invoicedeposititems' => '',
  4406.                 'datasupplier' => $data['datasupplier'],
  4407.                 'totales_neto' => $data['totales_neto'],
  4408.                 'totales_iva' => '',
  4409.                 'totales' => $data['totales'],
  4410.                 'bases_imponibles' => $data['bases_imponibles'],
  4411.                 'balance' => $data['balance'],
  4412.                 'paymentInvoice' => $data['paymentInvoice'],
  4413.                 'currency' => $data['currency'],
  4414.             )
  4415.         );
  4416.     }
  4417.     /**
  4418.      * @Route("/dpexternal/printinvoicerec/{id}", name="mdv_print_invoice_rec")
  4419.      * Imprimir una factura rectificativa
  4420.      */
  4421.     public function printInvoiceRecAction($id, Request $request)
  4422.     {
  4423.         $em = $this->getDoctrine()->getManager();
  4424.         $invoiceRec = $em->getRepository(MdvDocInvoiceRec::class)->findOneById($id);
  4425.         $invoiceItems = $em->getRepository(MdvDocInvoiceRecItems::class)->findByInvoiceId($id);
  4426.         $allInvoices = $em->getRepository(MdvDocInvoice::class)->findByFileId($invoiceRec->getFileId());
  4427.         $allInvoicesRec = $em->getRepository(MdvDocInvoiceRec::class)->findByFileId($invoiceRec->getFileId());
  4428.         foreach ($allInvoicesRec as $item){
  4429.             array_push($allInvoices, $item);
  4430.         }
  4431.         $file = $em->getRepository(MdvFiles::class)->findOneById($invoiceRec->getFileId());
  4432.         $client = $em->getRepository(Client::class)->findOneById($file->getClient());
  4433.         $data = array();
  4434.         $data = $this->baseInvoiceDoneFile($invoiceRec->getFileId(), $id,'R');
  4435.         $data['clients'][0] = $client;
  4436.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  4437.         // Una factura solo se puede rectificar una vez
  4438. //        $rectf = $em->getRepository('DevelupBundle:MdvInvoiceRec')->findBy( array('number' => 'REC-'.$invoice->getNumber()));
  4439.         $boolToRec = false;
  4440.         $user_logueado = $this->getUser();
  4441.         return $this->render('MDS/DevelupBundle/Develup/invoice-proforma-invdep-print.html.twig',
  4442.             array(
  4443.                 'id' => $invoiceRec->getFileId(),
  4444.                 'cid' => '',
  4445.                 'fid' => $invoiceRec->getId(),
  4446.                 'invoice' => $invoiceRec,
  4447.                 'facturas' => $allInvoices,
  4448.                 'boolToRec' => $boolToRec,
  4449.                 'numberadmin' => '',
  4450.                 'type' => 'Invoice Rec',
  4451.                 'docType' => 'Factura Rectificativa',
  4452.                 'number' => $invoiceRec->getId(),
  4453.                 'prefix' => '',
  4454.                 'date' => $invoiceRec->getDateAt(),
  4455.                 'invoiceIdToRec' => $invoiceRec->getInvoiceToRec(),
  4456.                 'token' => '',
  4457.                 'file' => $file,
  4458.                 'company' => $data['company'],
  4459.                 'clients' => $data['clients'],
  4460.                 'user' => $user_logueado,
  4461.                 'invoicedeposititems' => '',
  4462.                 'datasupplier' => $data['datasupplier'],
  4463.                 'totales_neto' => $data['totales_neto'],
  4464.                 'totales_iva' => '',
  4465.                 'totales' => $data['totales'],
  4466.                 'bases_imponibles' => $data['bases_imponibles'],
  4467.                 'balance' => $data['balance'],
  4468.                 'paymentInvoice' => $data['paymentInvoice'],
  4469.                 'currency' => $data['currency'],
  4470.             )
  4471.         );
  4472.     }
  4473.     /**
  4474.      * @Route("/listinvoice/", name="mdv_list_invoices")
  4475.      * Listar facturas
  4476.      */
  4477.     public function listInvoicesAction( Request $request){
  4478.         $em = $this->getDoctrine()->getManager();
  4479.         $invoiced = $em->getRepository(MdvDocInvoice::class)->findAll();
  4480.         $invoicedRec = $em->getRepository(MdvDocInvoiceRec::class)->findAll();
  4481.         // Agregamos las rectificativas al arreglo de facturas con el indice "R(id)"
  4482.         foreach ($invoicedRec as $idRecArray => $item){
  4483.             $invoiced['R'.$item->getId()] = $item;
  4484.         }
  4485.         $sumatoriaTotalNet = 0;
  4486.         $sumatoriaTotalVat = 0;
  4487.         $sumatoriaTotal = 0;
  4488.         foreach ($invoiced as $idArray => $item){
  4489.             $file = $em->getRepository(MdvFiles::class)->findOneById($item->getFileId());
  4490.             $invoiced[$idArray]->setNumber(is_null($file)? 0 : $file->getTitle());
  4491.             $client = is_null($file)? '' : $em->getRepository(Client::class)->findOneById($file->getClient());
  4492.             if(!empty($client)){
  4493.                 $invoiced[$idArray]->setPrefix($client->getName());
  4494.             } else {
  4495.                 $invoiced[$idArray]->setPrefix('');
  4496.             }
  4497.             $estaEnFactura = false;
  4498.             $estaEnFacturaRectificativa = false;
  4499.             if ($item->getType() == 'Invoice Deposit'){
  4500.                 // Si la factura de deposito se encuentra en una factura sumara 0
  4501.                 // INICIO: Verificamos factura de deposito dentro de alguna factura
  4502.                 $payOfInvoiceDeposit = $em->getRepository(MdvPaymentsClient::class)->findOneByInvoiceId($item->getId());
  4503.                 if (!empty($payOfInvoiceDeposit)){
  4504.                     // Verificamos si el deposito esta dentro de una factura
  4505.                     $itemInvoiceOfInvoiceDeposit = $em->getRepository(MdvDocInvoiceItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4506.                 }
  4507.                 if (!empty($itemInvoiceOfInvoiceDeposit)){
  4508.                     $estaEnFactura = true;
  4509.                 } else {
  4510.                     // Verificamos que el control Id no haya sido seteado a 0 por rectificar la factura
  4511.                     if (!empty($payOfInvoiceDeposit)) {
  4512.                         $itemInvoiceOfInvoiceDeposit = $em->getRepository(MdvDocInvoiceRecItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4513.                         if (!empty($itemInvoiceOfInvoiceDeposit)) { $estaEnFactura = true; }
  4514.                     }
  4515.                 }
  4516.                 if (!empty($payOfInvoiceDeposit)){
  4517.                     // Verificamos si el deposito esta dentro de una factura rectificativa
  4518.                     $itemInvoiceOfInvoiceDepositRec = $em->getRepository(MdvDocInvoiceRecItems::class)->findOneByPayControlId($payOfInvoiceDeposit->getId());
  4519.                 }
  4520.                 if (!empty($itemInvoiceOfInvoiceDepositRec)){
  4521.                     $estaEnFacturaRectificativa = true;
  4522.                 }
  4523.                 // FIN: Verificamos factura de deposito dentro de alguna factura
  4524.                 $invoicedDepositItems = $em->getRepository(MdvDocInvoiceItems::class)->findByControlId($item->getMaster());
  4525.                 foreach ($invoicedDepositItems as $itemDep){
  4526.                     $invoiced[$idArray]->setTotalNet($invoiced[$idArray]->getTotalNet() + $itemDep->getAmount());
  4527.                     $invoiced[$idArray]->setVat($invoiced[$idArray]->getVat() + (($itemDep->getAmount() * $itemDep->getIva())/100));
  4528.                     $invoiced[$idArray]->setTotal($invoiced[$idArray]->getTotal() + ($itemDep->getAmount() + (($itemDep->getAmount() * $itemDep->getIva())/100)));
  4529.                 }
  4530.             }
  4531.             // Sumara cuando ambos esten en false (no esta en factura ni en fact rect) o en true en ambas (fue facturado pero luego rectificado)
  4532.             if ((!$estaEnFactura and !$estaEnFacturaRectificativa) or ($estaEnFactura and $estaEnFacturaRectificativa)) {
  4533.                 $sumatoriaTotalNet = $sumatoriaTotalNet + $item->getTotalNet();
  4534.                 $sumatoriaTotalVat = $sumatoriaTotalVat + $item->getVat();
  4535.                 $sumatoriaTotal = $sumatoriaTotal + $item->getTotal();
  4536.             }
  4537.         }
  4538.         return $this->render('MDS/DevelupBundle/Develup/invoice-list.html.twig',
  4539.             array(
  4540.                 'invocied' => $invoiced,
  4541.                 'sumatoriaTotalNet' => $sumatoriaTotalNet,
  4542.                 'sumatoriaTotalVat' => $sumatoriaTotalVat,
  4543.                 'sumatoriaTotal' => $sumatoriaTotal,
  4544.             )
  4545.         );
  4546.     }
  4547.     /**
  4548.      * @Route("/invoiceispaid/{id}", name="mdv_invoices_is_paid")
  4549.      * Confirmar y deshacer el pago de la factura
  4550.      */
  4551.     public function invoiceIsPaidAction($id, Request $request)
  4552.     {
  4553.         $em = $this->getDoctrine()->getManager();
  4554.         $invoice = $em->getRepository(MdvDocInvoice::class)->findOneById($id);
  4555.         if(!empty($invoice)){
  4556.             if($invoice->getIsPaid()){ $invoice->setIsPaid(false); } else { $invoice->setIsPaid(true); }
  4557.             try {
  4558.                 $em->persist($invoice);
  4559.                 $em->flush();
  4560.                 $event = 'Se ha actualizado la factura';
  4561.                 $successMessage = $this->translator->trans($event);
  4562.                 $this->addFlash('mensajecontact', $successMessage);
  4563.             } catch (\Exception $e) {
  4564.                 $event = 'An error occurred: ' . $e->getMessage() . ' | transport';
  4565.                 /* Para el usuario */
  4566.                 $errorMessage = $this->translator->trans($event);
  4567.                 $this->addFlash('mensajecontacterror', $errorMessage);
  4568.             }
  4569.         }
  4570.         return $this->redirectToRoute('mdv_list_invoices');
  4571.     }
  4572.     /**
  4573.      * @Route("/listpendingbud/", name="mdv_list_pending_bud")
  4574.      * Listar presupuestos pendientes para aprobar y los pendientes por facturar
  4575.      */
  4576.     public function listPendingBudgetsAction( Request $request){
  4577.         $em = $this->getDoctrine()->getManager();
  4578.         $pendingBuds = $em->getRepository(MdvBudgetPending::class)->findAll();
  4579.         $pBuds = array();
  4580.         $budInPro = array();
  4581.         // Creamos la agrupacion por proposal
  4582.         foreach ($pendingBuds as $item){
  4583.             if (!(array_key_exists($item->getProposalId(),$pBuds))){
  4584.                 if (!empty($item->getOriginId())){    // Los vacios solo contienen briefings
  4585.                     $pBuds[$item->getProposalId()] = null;
  4586.                 }
  4587.             }
  4588.         }
  4589.         // Metemos los items en el arreglo clasificado por proposal
  4590.         foreach ($pendingBuds as $item){
  4591.             if (!empty($item->getOriginId())) {    // Los vacios solo contienen briefings
  4592.                 $pBuds[$item->getProposalId()][] = $item;
  4593.             }
  4594.         }
  4595.         ksort($pBuds);
  4596.         $keysPbuds = array_keys($pBuds);
  4597.         $budToProforma = $em->getRepository(MdvBudgetToProforma::class)->findBy(
  4598.             array(
  4599.                 'status' => 'Confirmed',                                         // Pending -> Tosend -> Confirmed -> Invoiced
  4600.             )
  4601.         );
  4602.         $budToInvoice = array();
  4603.         foreach ($budToProforma as $item){
  4604.             if (!array_key_exists($item->getFileId(), $budToInvoice)){
  4605.                 $budToInvoice[$item->getFileId()] = null;
  4606.             }
  4607.         }
  4608.         foreach ($budToProforma as $item){
  4609.             $budToInvoice[$item->getFileId()]['file'] = $em->getRepository(MdvFiles::class)->findOneById($item->getFileId());
  4610.             // INICIO: Agrupamos las proformas y dentro ponemos los items de cada una
  4611.             //Obtenemos el Id de la proforma que contiene el producto
  4612.             $idPro =  $em->getRepository(MdvDocProformaItems::class)->findOneByControlId($item->getProductFileId());
  4613.             $idPro = $idPro->getProformaId();
  4614.             // Guardamos la info del agente
  4615.             $budToInvoice[$item->getFileId()]['agente'] = $item->getUserConfirmedFullName();
  4616.             $budToInvoice[$item->getFileId()]['idAgente'] = $item->getUserConfirmedId();
  4617.             //Buscamos el elemento que va dentro de la proforma
  4618.             $elemento = $em->getRepository(MdvProductFile::class)->findOneById($item->getProductFileId());
  4619.             $budToInvoice[$item->getFileId()]['proformas'][$idPro][] = $elemento;
  4620.             // FIN: Agrupamos las proformas y dentro ponemos los items de cada una
  4621.         }
  4622.         ksort($budToInvoice);
  4623.         //INICIO: Buscamos los budgets que ya se han metido en proformas
  4624.         $budInProforma = $em->getRepository(MdvBudgetToProforma::class)->findBy(
  4625.             array(
  4626.                 'status' => 'Tosend',                                         // Pending -> Tosend -> Confirmed -> Invoiced
  4627.             )
  4628.         );
  4629.         $budInPro= array();
  4630.         foreach ($budInProforma as $item){
  4631.             $budInPro[$item->getFileId()]['file'] = $em->getRepository(MdvFiles::class)->findOneById($item->getFileId());
  4632.             // INICIO: Agrupamos las proformas y dentro ponemos los items de cada una
  4633.             //Obtenemos el Id de la proforma que contiene el producto
  4634.             $idPro =  $em->getRepository(MdvDocProformaItems::class)->findOneByControlId($item->getProductFileId());
  4635.             $idPro = $idPro->getProformaId();
  4636.             // Guardamos la info del agente
  4637.             $budInPro[$item->getFileId()]['agente'] = $item->getUserConfirmedFullName();
  4638.             $budInPro[$item->getFileId()]['idAgente'] = $item->getUserConfirmedId();
  4639.             //Buscamos el elemento que va dentro de la proforma
  4640.             $elemento = $em->getRepository(MdvProductFile::class)->findOneById($item->getProductFileId());
  4641.             if (!empty($elemento)) {
  4642.                 $budInPro[$item->getFileId()]['proformas'][$idPro][] = $elemento;
  4643.                 if (empty($budInPro[$item->getFileId()]['proformasMontos'][$idPro])) {
  4644.                     $budInPro[$item->getFileId()]['proformasMontos'][$idPro] = $elemento->getServicePrice() * 1.21;
  4645.                 } else {
  4646.                     $budInPro[$item->getFileId()]['proformasMontos'][$idPro] = $budInPro[$item->getFileId()]['proformasMontos'][$idPro] + $elemento->getServicePrice() * 1.21;
  4647.                 }
  4648.             }
  4649.             // FIN: Agrupamos las proformas y dentro ponemos los items de cada una
  4650.         }
  4651.         ksort($budInPro);
  4652.         //Verificamos que no haya expedientes sin elementos (proformas)
  4653.         foreach ($budInPro as $key => $item){
  4654.             if(!array_key_exists('proformas',$item)){
  4655.                 if (sizeof($budInPro)==1){
  4656.                     unset($budInPro,$key);
  4657.                     $budInPro = array();
  4658.                 } else {
  4659.                     unset($budInPro,$key);
  4660.                 }
  4661.             }
  4662.         }
  4663.         //FIN: Buscamos los budgets que ya se han metido en proformas
  4664.         //Buscamos los budgets que solo contienen Briefing
  4665.         $briefings = $em->getRepository(MdvBudgetPending::class)->findBy(
  4666.             array(
  4667.                 'serviceId' => null,                                         // Pending -> Tosend -> Confirmed -> Invoiced
  4668.             )
  4669.         );
  4670.         return $this->render('MDS/DevelupBundle/Develup/list-budgets-pending.html.twig',
  4671.             array(
  4672.                 'pendingBudgets' => $pBuds,
  4673.                 'keysPendingBudgets' => $keysPbuds,
  4674.                 'budToInvoice' => $budToInvoice,
  4675.                 'budInProforma' => $budInPro,
  4676.                 'briefings' => $briefings,
  4677.             )
  4678.         );
  4679.     }
  4680.     /**
  4681.      * @Route("/budgetadd/", name="mdv_list_budget_add")
  4682.      * Crear los presupuestos (proformas) y los expedientes (si es necesario)
  4683.      */
  4684.     public function budgetsAddAction( Request $request){
  4685.         $services = $request->request->get('services_hidden');
  4686.         $logTelegram = false;
  4687.         //Modificar precios en Eventos (si es necesario)
  4688.         foreach ($services as $item){
  4689.             $em = $this->getDoctrine()->getManager();
  4690.             $pendingBud = $em->getRepository(MdvBudgetPending::class)->findOneById($item['id']);
  4691.             //INICIO: Creamos el expediente de Develup (en caso de ser necesario)
  4692.             $file = $em->getRepository(MdvFiles::class)->findOneByIdProposal($pendingBud->getProposalId());
  4693.             if (empty($file)){
  4694.                 $file = new MdvFiles();
  4695.                 if (array_key_exists('proposalTitle',$item)){
  4696.                     $file->setTitle($pendingBud->getProposalId().' -- '.$item['proposalTitle']);
  4697.                 } else {
  4698.                     if (array_key_exists('title',$item)){
  4699.                         $file->setTitle($item['title']);
  4700.                     } else {
  4701.                         $file->setTitle($pendingBud->getProposalId().' - ');
  4702.                     }
  4703.                 }
  4704.                 $file->setClient('262');                                            // 262 es In Out Travel & Events
  4705.                 $file->setPriority(null);
  4706.                 $file->setSupplier(null);
  4707.                 $file->setStatus('Confirmed');
  4708.                 $file->setDaysBlock(null);
  4709.                 $file->setAdvancePayment(null);
  4710.                 $file->setIdProposal($pendingBud->getProposalId());
  4711.                 $file->setCateringName(null);
  4712.                 $file->setBoolCatering(null);
  4713.                 $file->setDateStart(null);
  4714.                 $file->setDateEnd(null);
  4715.                 $file->setPax(null);
  4716.                 $file->setDeposit(null);
  4717.                 $file->setAccessKey(null);
  4718.                 $file->setDescription(null);
  4719.                 $file->setCreatedAt(new DateTime('now'));
  4720.                 $file->setCreatedId(5);                                             // Usuario de Jorge de Freita
  4721.                 $file->setUpdatedAt(new DateTime('now'));
  4722.                 $file->setUpdatedId(5);                                             // Usuario de Jorge de Freita
  4723.                 $em->persist($file);
  4724.                 $em->flush();
  4725.                 //Actualizamos los Briefings que esten en FileId null con el recien generado fileId en base al proposalId
  4726.                 $allBriefings = $em->getRepository(MdvBriefings::class)->findByProposalId($file->getIdProposal());
  4727.                 foreach ($allBriefings as $itemBrief){
  4728.                     if (empty($itemBrief->getFileId())) {
  4729.                         $itemBrief->setFileId($file->getId());
  4730.                         $em->persist($itemBrief);
  4731.                         $em->flush();
  4732.                     }
  4733.                 }
  4734.             }
  4735.             //FIN: Creamos el expediente de Develup (en caso de ser necesario)
  4736.             //INICIO: Creamos el producto dentro del expediente
  4737.             $mdvProductSelected = $em->getRepository(MdvProduct::class)->findOneById($pendingBud->getServiceId());
  4738.             /* Obtengo usuario logueado */
  4739.             $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4740.             $user_id = $user_logueado->getId();
  4741.             $newProductFile = new MdvProductFile();
  4742.             $newProductFile->setProductName($mdvProductSelected->getName());
  4743.             $newProductFile->setProductId($mdvProductSelected->getId());
  4744.             $newProductFile->setOriginId($pendingBud->getOriginId());
  4745.             if (!empty($item['price'])){
  4746.                 $newProductFile->setServicePrice($item['price']);
  4747.                 if (!($item['price'] == $mdvProductSelected->getPrice())) {
  4748.                     // Se ha modificado el precio en el presupuesto y se debe cambiar en el origen (Proposal de eventos)
  4749.                     $itemOrigin = $em->getRepository(ProposalSupplierServices::class)->findOneById($pendingBud->getOriginId());
  4750.                     if (!empty($itemOrigin)) {
  4751.                         $itemOrigin->setPrice($item['price']);
  4752.                         $em->persist($itemOrigin);
  4753.                         $em->flush();
  4754.                     }
  4755.                 }
  4756.             } else {
  4757.                 // Si el precio viene vacio tomamos el valor por defecto del producto
  4758.                 $newProductFile->setServicePrice($mdvProductSelected->getPrice());
  4759.             }
  4760.             //Unidades
  4761.             if (!empty($item['units'])){
  4762.                 $newProductFile->setUnits($item['units']);
  4763.                 if (!($item['units'] == 1)) {
  4764.                     // Se ha modificado el numero de unidades en el presupuesto y se debe cambiar en el origen (Proposal de eventos)
  4765.                     $itemOrigin = $em->getRepository(ProposalSupplierServices::class)->findOneById($pendingBud->getOriginId());
  4766.                     if (!empty($itemOrigin)) {
  4767.                         $itemOrigin->setUnits($item['units']);
  4768.                         $em->persist($itemOrigin);
  4769.                         $em->flush();
  4770.                     }
  4771.                 }
  4772.             } else {
  4773.                 // Si las unidades viene vacio tomamos el valor por defecto del producto
  4774.                 $newProductFile->setUnits('1');
  4775.             }
  4776.             $newProductFile->setSubTotalPrice($newProductFile->getServicePrice()*$newProductFile->getUnits());
  4777.             //Ajustamos el precio quitando el IVA
  4778. //            $tempPrice = $newProductFile->getServicePrice()*100;
  4779. //            $tempPrice = bcdiv($tempPrice,'121',2);         // Divide entre 121 manteniendo 2 decimales sin redondeo
  4780. //            $newProductFile->setServicePrice($tempPrice);
  4781.             $newProductFile->setDescription($mdvProductSelected->getDescription());
  4782.             $newProductFile->setFileId($file->getId());
  4783.             if(true){ // No recibimos fechas // (!empty($newRequest['dateStart'])) {
  4784.                 $newProductFile->setDateStart(new DateTime('now')); //$newRequest['dateStart'].' '.$newRequest['hourStart']));
  4785.             }
  4786.             if (true){ // No recibimos fechas // (!empty($newRequest['dateEnd'])) {
  4787.                 $newProductFile->setDateEnd(new DateTime('now')); // (new \DateTime($newRequest['dateStart'].' '.$newRequest['hourEnd']));
  4788.             }
  4789.             $newProductFile->setCreatedId($user_id);
  4790.             $newProductFile->setUpdatedId($user_id);
  4791.             $newProductFile->setCreatedAt(new DateTime('now'));
  4792.             $newProductFile->setUpdatedAt(new DateTime('now'));
  4793.             $em->persist($newProductFile);
  4794.             $em->flush();
  4795.             $logTelegram = true;
  4796.             //FIN: Creamos el producto dentro del expediente
  4797.             //INICIO: Creamos una orden de hacer proforma
  4798.             $order = new MdvBudgetToProforma();
  4799.             $order->setStatus('Pending');
  4800.             $order->setFileId($file->getId());
  4801.             $order->setProductFileId($newProductFile->getId());
  4802.             $order->setUserConfirmedId($pendingBud->getUserConfirmedId());
  4803.             $order->setUserConfirmedFullName($pendingBud->getUserConfirmedFullName());
  4804.             $order->setProposalTitle($pendingBud->getProposalTitle());
  4805.             $em->persist($order);
  4806.             $em->flush();
  4807.             //FIN: Creamos una orden de hacer proforma
  4808.             // Se elimina el presupuesto pendiente
  4809.             $em->remove($pendingBud);
  4810.             $em->flush();
  4811.         }
  4812.         //INICIO: Creamos el presupuesto (proforma)
  4813.         $budToPro = $em->getRepository(MdvBudgetToProforma::class)->findByStatus('Pending');
  4814.         /* Obtengo usuario logueado */
  4815. //        $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4816. //        $user_id = $user_logueado->getId();
  4817. //        $arrayIdPro = array();          // $arrayIdPro['X'] = 'Y'; al agente 'Y' se le informa del presupuesto 'X'
  4818.         //INICIO: Se agrupan por expediente (fileId)
  4819.         $proformas = array();
  4820.         foreach ($budToPro as $item){
  4821.             $proformas[$item->getFileId()][] = $item;
  4822.         }
  4823.         //FIN: Se agrupan por expediente (fileId)
  4824.         if (!empty($budToPro)){
  4825.             foreach ($proformas as $key => $elemento){
  4826.                 //INICIO: Creamos las proformas
  4827.                 //INICIO: Calculamos el ID de la proforma
  4828.                 $numberToPrefix = $em->getRepository(MdvDocProforma::class)->findAll();
  4829.                 if (empty($numberToPrefix)){
  4830.                     $numberToPrefix = 0;
  4831.                 } else {
  4832.                     $numberToPrefix = end($numberToPrefix)->getId();
  4833.                 }
  4834.                 $numberToPrefix++;
  4835.                 //FIN: Calculamos el ID de la proforma
  4836.                 $newProforma = new MdvDocProforma();
  4837.                 $newProforma->setPrefix('MDV-' . (new DateTime('now'))->format('dmy') . '-' . $numberToPrefix . '-' . $key);
  4838.                 $newProforma->setDateAt(new DateTime('now'));
  4839.                 $newProforma->setFileId($key);
  4840.                 $newProforma->setCreatedId($user_id);
  4841.                 $newProforma->setUpdatedId($user_id);
  4842.                 $newProforma->setCreatedAt(new DateTime('now'));
  4843.                 $newProforma->setUpdatedAt(new DateTime('now'));
  4844.                 $em->persist($newProforma);
  4845.                 $em->flush();
  4846.                 //FIN: Creamos las proformas
  4847.                 foreach ($elemento as $item) {
  4848.                     //$key es el id del expediente
  4849. //                    $arrayIdPro[$numberToPrefix] = $item->getUserConfirmedId();             // $arrayIdPro['X'] = 'Y'; al agente 'Y' se le informa del presupuesto 'X'
  4850.                     //INICIO: Creamos los elemento que iran dentro de las proformas
  4851.                     $itemProforma = new MdvDocProformaItems();
  4852.                     $itemProforma->setType('PRODUCT');
  4853.                     $itemProforma->setFileId($key);
  4854.                     $itemProforma->setProformaId($newProforma->getId());
  4855.                     $itemProforma->setControlId($item->getProductFileId());
  4856.                     $em->persist($itemProforma);
  4857.                     $em->flush();
  4858.                     //INICIO: Confirmamos el BudToProforma
  4859.                     $item->setStatus('Tosend');              // El elemento se ha pasado a la proforma, solo falta enviar
  4860.                     $item->setProformaId($newProforma->getId());
  4861.                     $em->persist($item);
  4862.                     $em->flush();
  4863.                     //FIN: Confirmamos el BudToProforma
  4864.                     //FIN: Creamos los elemento que iran dentro de las proformas
  4865.                 }
  4866.             }
  4867.         }
  4868.         //INICIO: Notificamos al agente por Telegram
  4869.         if ($logTelegram){
  4870.             //Buscamos todos los agentes del proposal
  4871.             $proposalAgents = $em->getRepository(ProposalAgents::class)->findOneByIdProp($file->getIdProposal());
  4872.             //Buscamos el Destino
  4873. //            $proposalDestino = $em->getRepository(Destination::class)->findOneById($control->getDestinoId());
  4874.             //Buscamos el agente de Develup
  4875.             $userData = $em->getRepository(User::class)->findOneById($user_id);
  4876.             if (!empty($proposalAgents)){
  4877.                 if ((!empty($proposalAgents->getAgOne())) and (!($proposalAgents->getAgOne() == 0))){
  4878.                     $this->sendTelegram($proposalAgents->getAgOne(),'1. PRESUPUESTO APROBADO DESDE DEVELUP __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  4879.                     //d('entro en 1',$proposalAgents->getAgOne(), $proposalDestino->getTitle());
  4880.                 }
  4881.                 if ((!empty($proposalAgents->getAgTwo())) and (!($proposalAgents->getAgTwo() == 0))){
  4882.                     $this->sendTelegram($proposalAgents->getAgTwo(),'2. PRESUPUESTO APROBADO DESDE DEVELUP __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  4883.                     //d('entro en 2',$proposalAgents->getAgTwo(), $proposalDestino->getTitle());
  4884.                 }
  4885.                 if ((!empty($proposalAgents->getAgThree())) and (!($proposalAgents->getAgThree() == 0))){
  4886.                     $this->sendTelegram($proposalAgents->getAgThree(),'3. PRESUPUESTO APROBADO DESDE DEVELUP __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  4887.                     //d('entro en 3', $proposalAgents->getAgThree(), $proposalDestino->getTitle());
  4888.                 }
  4889.                 if ((!empty($proposalAgents->getAgFour())) and (!($proposalAgents->getAgFour() == 0))){
  4890.                     $this->sendTelegram($proposalAgents->getAgFour(),'4. PRESUPUESTO APROBADO DESDE DEVELUP __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  4891.                     //d('entro en 4', $proposalAgents->getAgFour(), $proposalDestino->getTitle());
  4892.                 }
  4893.             }
  4894.         }
  4895.         //FIN: Notificamos al agente por Telegram
  4896.         //FIN: Creamos el presupuesto (proforma)
  4897.         return $this->redirectToRoute('mdv_list_pending_bud');
  4898.     }
  4899.     /**
  4900.      * @Route("/budgetsend/", name="mdv_budget_send")
  4901.      * Envio automatico de los presupuestos forzado por usuario
  4902.      */
  4903.     public function budgetSendAction( Request $request){
  4904.         $em = $this->getDoctrine()->getManager();
  4905.         $budToPro = $em->getRepository(MdvBudgetToProforma::class)->findByStatus('Tosend');
  4906.         $logTelegram = false;
  4907.         /* Obtengo usuario logueado */
  4908.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  4909.         $user_id = $user_logueado->getId();
  4910.         $arrayIdPro = array();          // $arrayIdPro['X'] = 'Y'; al agente 'Y' se le informa del presupuesto 'X'
  4911.         //INICIO: Se agrupan por expediente (fileId)
  4912.         $proformas = array();
  4913.         foreach ($budToPro as $item){
  4914.             $proformas[$item->getFileId()][] = $item;
  4915.             $fileDevelup = $item->getFileId();
  4916.         }
  4917.         //FIN: Se agrupan por expediente (fileId)
  4918.         if (!empty($budToPro)){
  4919.             foreach ($proformas as $key => $elemento){
  4920.                 //INICIO: Creamos las proformas
  4921.                 //INICIO: Calculamos el ID de la proforma
  4922. //                $numberToPrefix = $em->getRepository(MdvDocProforma::class)->findAll();
  4923. //                if (empty($numberToPrefix)){
  4924. //                    $numberToPrefix = 0;
  4925. //                } else {
  4926. //                    $numberToPrefix = end($numberToPrefix)->getId();
  4927. //                }
  4928. //                $numberToPrefix++;
  4929. //                $numberToPrefix++;
  4930.                 //FIN: Calculamos el ID de la proforma
  4931. //d($proformas, $elemento);exit();
  4932. //                $newProforma = new MdvDocProforma();
  4933. //                $newProforma->setPrefix('MDV-' . (new DateTime('now'))->format('dmy') . '-' . $numberToPrefix . '-' . $key);
  4934. //                $newProforma->setDateAt(new DateTime('now'));
  4935. //                $newProforma->setFileId($key);
  4936. //
  4937. //                $newProforma->setCreatedId($user_id);
  4938. //                $newProforma->setUpdatedId($user_id);
  4939. //                $newProforma->setCreatedAt(new DateTime('now'));
  4940. //                $newProforma->setUpdatedAt(new DateTime('now'));
  4941. //
  4942. //                $em->persist($newProforma);
  4943. //                $em->flush();
  4944.                 //FIN: Creamos las proformas
  4945.                 foreach ($elemento as $item) {
  4946.                     //$key es el id del expediente
  4947.                     if (!empty($item->getProformaId())) {
  4948.                         $arrayIdPro[$item->getProformaId()] = $item->getUserConfirmedId();             // $arrayIdPro['X'] = 'Y'; al agente 'Y' se le informa del presupuesto 'X'
  4949.                     }
  4950.                     //INICIO: Creamos los elemento que iran dentro de las proformas
  4951. //                    $itemProforma = new MdvDocProformaItems();
  4952. //                    $itemProforma->setType('PRODUCT');
  4953. //                    $itemProforma->setFileId($key);
  4954. //                    $itemProforma->setProformaId($newProforma->getId());
  4955. //                    $itemProforma->setControlId($item->getProductFileId());
  4956. //
  4957. //                    $em->persist($itemProforma);
  4958. //                    $em->flush();
  4959.                     //INICIO: Confirmamos el BudToProforma
  4960.                     $item->setStatus('Confirmed');              // El elemento se ha pasado a confirmado porque se enviara por correo
  4961.                     $em->persist($item);
  4962.                     $em->flush();
  4963.                     //FIN: Confirmamos el BudToProforma
  4964.                     //FIN: Creamos los elemento que iran dentro de las proformas
  4965.                 }
  4966.             }
  4967.         }
  4968.         //INICIO: Notificamos de todos los presupuestos (proformas) que se han creado
  4969.         foreach ($arrayIdPro as $key => $item){
  4970.             $logTelegram = true;
  4971.             //Buscamos los agentes a notificar
  4972.             // $arrayIdPro['X'] = 'Y'; al agente 'Y' se le informa del presupuesto 'X'
  4973.             $agent = $em->getRepository(User::class)->findOneById($item);
  4974.             $agentMail = $agent->getEmail();
  4975.             $mailAgent = $agentMail;
  4976. //            $mailAgent = 'gustavo.ayala@develup.solutions';
  4977.             //Se prepara el correo con los agentes a notificar
  4978.             $firmGmail = $agent->getFirmGmail();
  4979.             $data = array(
  4980.                 'body' => 'Presupuesto Develup #' . $key  . ', ' . '<br><a href="http://' . $request->server->get('HTTP_HOST') . '/pdf/develupproforma/' . $key . '">Ver el PDF del presupuesto</a><br><br>Notificación de nuevo presupuesto: Este presupuesto ha sido creado de forma automatica.' . '<br><br>',
  4981.                 'firm' => $firmGmail,
  4982.             );
  4983.             // EJECUTAR ENVIO DE ALERTA PARA EL AGENTE
  4984.             $transporter = new Swift_SmtpTransport();
  4985.             $transporter->setHost('smtp.gmail.com')
  4986.                 ->setEncryption('ssl')//ssl / tls
  4987.                 ->setPort(465)// 465 / 587
  4988.                 ->setUsername('desarrollo@develup.solutions')
  4989.                 ->setPassword('utvh hzoi wfdo ztjs');
  4990.             $mailer = new Swift_Mailer($transporter);
  4991.             $message = new Swift_Message();
  4992.             $message->setSubject('Presupuesto #' . $key . ', Notificación de nuevo presupuesto')
  4993.                 ->setSender($agentMail)
  4994.                 ->setFrom(array("desarrollo@develup.solutions" => "System Mante 3.0"))
  4995.                 ->setReplyTo($agentMail)
  4996.                 ->setTo($mailAgent)
  4997.                 ->setBody(
  4998.                     $this->renderView(
  4999.                         'mail/structure-mail.html.twig',
  5000.                         array('data' => $data)
  5001.                     ),
  5002.                     'text/html'
  5003.                 );
  5004.             $mailer->send($message);
  5005.             //INICIO: Notificamos al agente por Telegram
  5006.             if ($logTelegram){
  5007.                 //Buscamos el id proposal a partir del id de la proforma
  5008.                 $prof = $em->getRepository(MdvDocProforma::class)->findOneById($key);
  5009.                 $file = $em->getRepository(MdvFiles::class)->findOneById($prof->getFileId());
  5010.                 //Buscamos todos los agentes del proposal
  5011.                 $proposalAgents = $em->getRepository(ProposalAgents::class)->findOneByIdProp($file->getIdProposal());
  5012.                 //Buscamos el Destino
  5013. //            $proposalDestino = $em->getRepository(Destination::class)->findOneById($control->getDestinoId());
  5014.                 //Buscamos el agente de Develup
  5015.                 $userData = $em->getRepository(User::class)->findOneById($user_id);
  5016.                 if (!empty($proposalAgents)){
  5017.                     if ((!empty($proposalAgents->getAgOne())) and (!($proposalAgents->getAgOne() == 0))){
  5018.                         $this->sendTelegram($proposalAgents->getAgOne(),'1. PRESUPUESTO DE DEVELUP ENVIADO POR CORREO __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  5019.                         //d('entro en 1',$proposalAgents->getAgOne(), $proposalDestino->getTitle());
  5020.                     }
  5021.                     if ((!empty($proposalAgents->getAgTwo())) and (!($proposalAgents->getAgTwo() == 0))){
  5022.                         $this->sendTelegram($proposalAgents->getAgTwo(),'2. PRESUPUESTO DE DEVELUP ENVIADO POR CORREO __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  5023.                         //d('entro en 2',$proposalAgents->getAgTwo(), $proposalDestino->getTitle());
  5024.                     }
  5025.                     if ((!empty($proposalAgents->getAgThree())) and (!($proposalAgents->getAgThree() == 0))){
  5026.                         $this->sendTelegram($proposalAgents->getAgThree(),'3. PRESUPUESTO DE DEVELUP ENVIADO POR CORREO __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  5027.                         //d('entro en 3', $proposalAgents->getAgThree(), $proposalDestino->getTitle());
  5028.                     }
  5029.                     if ((!empty($proposalAgents->getAgFour())) and (!($proposalAgents->getAgFour() == 0))){
  5030.                         $this->sendTelegram($proposalAgents->getAgFour(),'4. PRESUPUESTO DE DEVELUP ENVIADO POR CORREO __ ID: '.$file->getIdProposal().' __ PROVEEDOR: Develup __ CONTACTO: '.$userData->getName().' '.$userData->getLastName());
  5031.                         //d('entro en 4', $proposalAgents->getAgFour(), $proposalDestino->getTitle());
  5032.                     }
  5033.                 }
  5034.             }
  5035.             //FIN: Notificamos al agente por Telegram
  5036.         }
  5037.         //FIN: Notificamos de todos los presupuestos (proformas) que se han creado
  5038.         return $this->redirectToRoute('mdv_list_pending_bud');
  5039. //        return new JsonResponse('Done');
  5040.     }
  5041.     /**
  5042.      * @Route("/budgetdel/{id}", name="mdv_list_budget_del")
  5043.      * Eliminar el producto de la solicitud de presupuesto
  5044.      */
  5045.     public function budgetsDeleteAction($id, Request $request){
  5046.         $em = $this->getDoctrine()->getManager();
  5047.         $budget = $em->getRepository(MdvBudgetPending::class)->findOneById($id);
  5048.         $proposal_services_supplier = $em->getRepository(ProposalSupplierServices::class)->findOneById($budget->getOriginId());
  5049.         // Se elimina el servicio de In Out
  5050.         if (!empty($proposal_services_supplier)) {
  5051.             $em->remove($proposal_services_supplier);
  5052.             $em->flush();
  5053.         }
  5054.         // Se elimina el servicio del presupuesto
  5055.         $em->remove($budget);
  5056.         $em->flush();
  5057.         return $this->redirectToRoute('mdv_list_pending_bud');
  5058.     }
  5059.     /**
  5060.      * @Route("/cargaservicios/", name="mdv_carga_servicios")
  5061.      * actualizar servicios develup desde inout events (Controlador inaccesible)
  5062.      */
  5063.     public function cargaServiciosAddAction( Request $request){
  5064.         $em = $this->getDoctrine()->getManager();
  5065.         $services = $em->getRepository(SupplierTechnology::class)->findAll();
  5066.         /* Obtengo usuario logueado */
  5067.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  5068.         $user_id = $user_logueado->getId();
  5069.         foreach ($services as $item){
  5070.             $temp = new MdvProduct();
  5071.             $temp->setName($item->getTitle());
  5072.             $temp->setPrice($item->getPrice());
  5073.             $temp->setType($item->getType());
  5074.             $temp->setCreatedId($user_id);
  5075.             $temp->setUpdatedId($user_id);
  5076.             $temp->setCreatedAt(new DateTime('now'));
  5077.             $temp->setUpdatedAt(new DateTime('now'));
  5078.             $em->persist($temp);
  5079.             $em->flush();
  5080.         }
  5081.         d($services);exit();
  5082.     }
  5083.     /**
  5084.      * @Route("/newinvoicefrombudget/{id}", name="mdv_new_invoice_from_budget")
  5085.      * Crear nueva factura desde un presupuesto (proforma)
  5086.      */
  5087.     public function generateNewInvoiceFromBudgetAction($id, Request $request){
  5088.         // Antes de redirigir a la funcion que crea presupuesto a partir de proformas se deben modificar las tablas de los presupuestos
  5089.         $em = $this->getDoctrine()->getManager();
  5090.         //  Buscamos los elementos de la proforma
  5091.         $elemProf = $em->getRepository(MdvDocProformaItems::class)->findByProformaId($id);
  5092.         $fileId = $elemProf[0]->getFileId();
  5093.         //  Buscamos los elementos de budgetToProforma para cambiar su status a Invoiced
  5094.         // los que pertenezcan a la proforma y a Discard los que pertenezcan a otra proforma
  5095.         $elemBudToProf = $em->getRepository(MdvBudgetToProforma::class)->findByFileId($fileId);
  5096.         foreach ($elemBudToProf as $key => $item){
  5097.             //con los $item busco en $elemBudToProf para poner invoiced
  5098.             foreach ($elemProf as $elem){
  5099.                 if ($elem->getControlId() == $item->getProductFileId()){
  5100.                     // Se debe poner en Invoiced
  5101.                     $item->setStatus('Invoiced');
  5102.                 }
  5103.             }
  5104.             // Si al terminar de revisar el status sigue en Confirmado, lo pasaremos
  5105.             // a descartado, es un elemento de un presupuesto que no fue aprobado
  5106.             // mientras que otro presupuesto si fue aprobado y paso a facturacion.
  5107.             // Al estar descartado no se muestra mas el presupuesto en los listados
  5108.             // aunque la proforma sigue existiendo.
  5109.             if ($item->getStatus() == 'Confirmed'){
  5110.                 $item->setStatus('Discarded');
  5111.             }
  5112.             $em->persist($item);
  5113.             $em->flush();
  5114.         }
  5115.         return $this->redirectToRoute('mdv_new_invoice_from_proforma',
  5116.             array(
  5117.                 'id' => $id
  5118.             )
  5119.         );
  5120.     }
  5121.     /**
  5122.      * @Route("/budgetdiscard/{id}", name="mdv_budget_discard")
  5123.      * Crear nueva factura desde un presupuesto (proforma)
  5124.      */
  5125.     public function discardBudgetAction($id, Request $request){
  5126.         $em = $this->getDoctrine()->getManager();
  5127.         //  Buscamos los elementos de la proforma
  5128.         $elemProf = $em->getRepository(MdvDocProformaItems::class)->findByProformaId($id);
  5129.         foreach ($elemProf as $item){
  5130.             //  Buscamos los elementos de budgetToProforma para cambiar su status a Discarded
  5131.             $elemBudToProf = $em->getRepository(MdvBudgetToProforma::class)->findOneByProductFileId($item->getControlId());
  5132.             $elemBudToProf->setStatus('Discarded');
  5133.             $em->persist($item);
  5134.             $em->flush();
  5135.         }
  5136.         return $this->redirectToRoute('mdv_list_pending_bud');
  5137.     }
  5138.     /**
  5139.      * @Route("/updateinvoicedate/", name="mdv_update_invoice_date")
  5140.      * Actualizar servicios develup desde inout events (Controlador inaccesible)
  5141.      */
  5142.     public function updateInvoiceDateAction( Request $request){
  5143.         $em = $this->getDoctrine()->getManager();
  5144.         $invoiceId = $request->request->get('invoice_id');
  5145.         $invoiceNewDate = $request->request->get('new_invoice_date');
  5146.         /* Obtengo usuario logueado */
  5147.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  5148.         $user_id = $user_logueado->getId();
  5149.         $invoice = $em->getRepository(MdvDocInvoice::class)->findOneById($invoiceId);
  5150.         $newDate = new DateTime($invoiceNewDate);
  5151.         $invoice->setDateAt($newDate);
  5152.         // Modificamos el number para que se adapte a la nueva fecha
  5153.         $invoice->setNumber('MDV-' . $newDate->format('dmy') . substr($invoice->getNumber(),10));
  5154.         $invoice->setUpdatedId($user_id);
  5155.         $invoice->setUpdatedAt(new DateTime('now'));
  5156.         $em->persist($invoice);
  5157.         $em->flush();
  5158.         return $this->redirectToRoute('mdv_list_invoices');
  5159.     }
  5160.     /**
  5161.      * @Route("/budgetbriefdiscard/{id}", name="mdv_budget_briefing_discard")
  5162.      * Eliminar un budget que es de tipo Briefing
  5163.      */
  5164.     public function budgetBriefDiscardAction($id, Request $request){
  5165.         $em = $this->getDoctrine()->getManager();
  5166.         $pendingBud = $em->getRepository(MdvBudgetPending::class)->findOneById($id);
  5167.         // Se elimina el presupuesto pendiente
  5168.         $em->remove($pendingBud);
  5169.         $em->flush();
  5170.         return $this->redirectToRoute('mdv_list_pending_bud');
  5171.     }
  5172.     // Replicar cambios entre FileController, InvoiceController y ProductsFilesController
  5173.     private function calculosFile($id, $arrayBools, $number, $type)
  5174.     {
  5175.         // $id Id del Expediente (File)
  5176.         // $arrayBool arreglo de logicos
  5177.         // $number  *********************
  5178.         // $type tipo de calculos a realizar (edicion de expediente, factura, factura de deposito, proforma)
  5179.         $em = $this->getDoctrine()->getManager();
  5180.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  5181.         $items = $em->getRepository(MdvProductFile::class)->findByFileId($file->getId());
  5182.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  5183.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  5184.         if (!empty($client)){
  5185.             $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  5186.             $client[0]->setPopulation($city);
  5187.             $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  5188.             $client[0]->setRegion($region);
  5189.             $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  5190.             $client[0]->setCountry($country);
  5191.         } else {
  5192.             $client[0] = new Client();
  5193.             $client[0]->setName('');
  5194.             $client[0]->setTitle('');
  5195.             $client[0]->setIdDocument('');
  5196.             $client[0]->setPopulation('');
  5197.             $client[0]->setRegion('');
  5198.             $client[0]->setCountry('');
  5199.         }
  5200.         // Acumuladores de los calculos
  5201.         $totales_neto_all = 0;
  5202.         $data_iva = array(
  5203.             'iva' => 21,
  5204.             'ivaMontoVeintiUno' => 0,
  5205.             'ivaMontoDiez' => 0,
  5206.             'ivaMontoCero' => 0,
  5207.         );
  5208.         //INICIO: Determinamos el numero de factura o proforma
  5209.         if (substr($number,0,1) == 'I'){
  5210.             // Es una Factura
  5211.             $numberInvoiceOrProforma = substr($number,1);
  5212.             $number = $numberInvoiceOrProforma;
  5213. //            $number = $em->getRepository('DevelupBundle:FileInvoice')->findOneById($number);
  5214. //            $number = $number->getNumber();
  5215.             $type = 'Invoice';
  5216.         } else {
  5217.             // Es una Proforma
  5218.             $numberInvoiceOrProforma = substr($number,1);
  5219.             $number = $numberInvoiceOrProforma;
  5220. //            $number = $em->getRepository('DevelupBundle:FileProforma')->findOneById($number);
  5221. //            $number = $number->getPrefix().$number->getReservationId();
  5222.             $type = 'Proforma';
  5223.         }
  5224.         //FIN: Determinamos el numero de factura o proforma
  5225.         // Buscamos las salas reservadas, pagos y servicios para el evento
  5226.         $arrayItems = array();
  5227.         $fileProducts = array();
  5228.         if (!empty($arrayBools['product'])) {
  5229.             foreach ($arrayBools['product'] as $key => $item) {
  5230.                 if ($item == 'true') {
  5231. //                    $fileProducts[] = $em->getRepository('DevelupBundle:ReservationLoungeSimple')->findOneById($key);
  5232.                 }
  5233.             }
  5234.         }
  5235.         $payments = array();
  5236.         if (!empty($arrayBools['payment'])) {
  5237.             foreach ($arrayBools['payment'] as $key => $item) {
  5238.                 if ($item == 'true') {
  5239. //                    $payments[] = $em->getRepository('DevelupBundle:ReservationPaymentsClient')->findOneById($key);
  5240.                 }
  5241.             }
  5242.         }
  5243.         $services = array();
  5244.         if (!empty($arrayBools['service'])) {
  5245.             foreach ($arrayBools['service'] as $key => $item) {
  5246.                 if ($item == 'true') {
  5247. //                    $services[] = $em->getRepository('DevelupBundle:ReservationService')->findOneById($key);
  5248.                 }
  5249.             }
  5250.         }
  5251.         $data_supplier = array();
  5252.         $i = 0;
  5253.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  5254.         $pax = '1';
  5255.         $qty = '1';
  5256.         $product = array(
  5257.             'neto' => 0,
  5258.             'sumSubT' => 0,
  5259.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  5260.         $service = array(
  5261.             'neto' => 0,
  5262.             'sumSubT' => 0,
  5263.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  5264.         if (empty($items)){
  5265.             $data_supplier['product'][0] = null;
  5266.         }
  5267.         foreach ($items as $item){
  5268.             if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  5269.                 $subtotal = 0;
  5270.                 $neto = 0;
  5271.                 $subtotalProduct = 0;
  5272.             } else {
  5273.                 $subtotalProduct = $item->getServicePrice() * 1.21;
  5274.                 $subneto = $item->getServicePrice();
  5275.                 $subtotal =  $subtotalProduct;
  5276.                 $neto =  $subneto;
  5277.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  5278.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  5279.                 $neto = round($neto,2,PHP_ROUND_HALF_UP);
  5280.                 $arrayItems[] = array(
  5281.                     'type' => 'Product',
  5282.                     'name' => $item->getProductName(),
  5283.                     'amount' => $neto,
  5284.                     'iva' => '21',
  5285.                     'total' => $subtotal,
  5286.                 );
  5287.             }
  5288.             // Acumula netos totales e IVA
  5289.             $totales_neto_all = $totales_neto_all + $neto;
  5290.             $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21);
  5291.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  5292.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  5293.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  5294.             // Acumula netos totales e IVA
  5295.             $product['neto'] = $product['neto'] + $neto;
  5296.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  5297.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  5298.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  5299.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  5300.             $data_supplier['product'][$i] = array (
  5301.                 'id' => $item->getId(),
  5302.                 'productName' => $item->getProductName(),
  5303.                 'description' => $item->getDescription(),
  5304.                 'productId' => $item->getProductId(),
  5305.                 'dateStart' => $item->getDateStart(),
  5306.                 'dateEnd' => $item->getDateEnd(),
  5307.                 'servicePrice' => $item->getServicePrice(),
  5308.                 'subtotalProduct' => $subtotalProduct,
  5309.                 'iva' => $iva,
  5310.                 'pax' => $pax,
  5311.                 'qty' => $qty,
  5312.                 'type' => $item->getType(),
  5313.                 'subtotal' => $subtotal,
  5314.             );
  5315.             $i++;
  5316.         }
  5317.         $data_supplier['productSubTotal'] = array(
  5318.             'neto' => $product['neto'],
  5319.             'sumSubT' => $product['sumSubT'],
  5320.         );
  5321.         $payment = array(
  5322.             'sumSubT' => 0,
  5323.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  5324.         $i = 0;
  5325.         foreach ($payments as $item){
  5326.             $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  5327.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  5328.             $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  5329.             $data_supplier['payment'][$i] = array (
  5330.                 'id' => $item->getId(),
  5331.                 'amount' => $item->getAmount(),
  5332.                 'amountTotal' => $item->getAmountTotal(),
  5333.                 'vat' => $item->getVat(),
  5334.                 'datePayAt' => $item->getDatePayAt(),
  5335.                 'wayToPay' => $item->getWayToPay(),
  5336.             );
  5337.             $arrayItems[] = array(
  5338.                 'type' => 'Payment',
  5339.                 'name' => $item->getWayToPay(),
  5340.                 'amount' => $item->getAmount(),
  5341.                 'total' => $item->getAmountTotal(),
  5342.                 'iva' => $item->getVat(),
  5343. //                    'iva' => '',
  5344. //                    'total' => $item->getAmount(),
  5345.             );
  5346.             $i++;
  5347.         }
  5348.         if (!empty($payments)) {
  5349.             $data_supplier['paymentSubTotal'] = array(
  5350.                 'sumSubT' => $payment['sumSubT'],
  5351.             );
  5352.         }
  5353.         foreach ($services as $item){
  5354.             if (is_null($item->getPrice()) or empty($item->getPrice())){
  5355.                 $subtotal = 0;
  5356.                 $neto = 0;
  5357.                 $subtotalService = 0;
  5358.             } else {
  5359.                 $subtotalService = $item->getPrice();
  5360.                 $subneto = $item->getPrice();
  5361.                 // Commission
  5362.                 if ($item->getOpCommission()=='1'){
  5363.                     $subtotalService = $subtotalService * (1 + ($item->getCommission()/100));
  5364.                     $subneto = $subneto  * (1 + ($item->getCommission()/100));
  5365.                 } else {
  5366.                     $subtotalService = $subtotalService * (1 - ($item->getCommission()/100));
  5367.                     $subneto = $subneto * (1 - ($item->getCommission()/100));
  5368.                 }
  5369.                 // Over
  5370.                 if ($item->getOpOver()=='1'){
  5371.                     $subtotalService = $subtotalService + $item->getOver();
  5372.                     $subneto = $subneto + $item->getOver();
  5373.                 } else {
  5374.                     $subtotalService = $subtotalService - $item->getOver();
  5375.                     $subneto = $subneto - $item->getOver();
  5376.                 }
  5377.                 // IVA
  5378.                 if ($item->getOpIva()=='1'){
  5379.                     $subtotalService = $subtotalService * (1 + ($item->getIva()/100));
  5380.                 } else {
  5381.                     $subtotalService = $item->getPrice();
  5382.                     $subneto = ($subneto * 100) / (100 + $item->getIva());
  5383.                 }
  5384.                 switch ($item->getServiceCatId()){
  5385.                     case 1: // Alojamiento
  5386.                         // el numero de noches $numNoches; precio unitario $subneto
  5387.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  5388.                         // La personas no afectan este calculo
  5389.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  5390.                         $subnetoUnit = $subneto;
  5391.                         $subneto = $subneto * $numNoches * $item->getUnits();
  5392.                         $data_supplier['service'][$i] = array (
  5393.                             'id' => $item->getId(),
  5394.                             'serviceCatId' => $item->getServiceCatId(),
  5395.                             'serviceName' => $item->getName(),
  5396.                             'serviceType' => 'Hotel',
  5397.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  5398.                             'qty' => $item->getUnits(),
  5399.                             'iva' => $item->getIva(),
  5400.                             'pax' => '-',
  5401.                             'precioUnit' => $subnetoUnit,
  5402.                             'subneto' => $subneto,
  5403.                             'subtotal' => $subtotal,
  5404.                         );
  5405.                         break;
  5406.                     case 2: //Actividades
  5407.                         // El número de personas es considerado en el calculo
  5408.                         $pax = $item->getPax();
  5409.                         if (empty($pax) or $pax == "0") {
  5410.                             $pax = 1;
  5411.                         }
  5412.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5413.                         if ($days > 1){
  5414.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5415.                         } else {
  5416.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5417.                         }
  5418.                         $subtotal = $subtotalService * $days * $item->getUnits();
  5419.                         $subnetoUnit = $subneto;
  5420.                         $subneto = $subneto * $days * $item->getUnits();
  5421.                         $data_supplier['service'][$i] = array (
  5422.                             'id' => $item->getId(),
  5423.                             'serviceCatId' => $item->getServiceCatId(),
  5424.                             'serviceName' => $item->getName(),
  5425.                             'serviceType' => 'Actividad',
  5426.                             'date' => $dateServ,
  5427.                             'qty' => $item->getUnits(),
  5428.                             'iva' => $item->getIva(),
  5429.                             'pax' => $item->getPax(),
  5430.                             'precioUnit' => $subnetoUnit,
  5431.                             'subneto' => $subneto,
  5432.                             'subtotal' => $subtotal,
  5433.                         );
  5434.                         break;
  5435.                     case 3: // AV
  5436.                         $pax = $item->getPax();
  5437.                         if (empty($pax) or $pax == "0") {
  5438.                             $pax = 1;
  5439.                         }
  5440.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5441.                         if ($days > 1){
  5442.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5443.                         } else {
  5444.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5445.                         }
  5446.                         $unitsServ = $item->getUnits();
  5447.                         if (empty($unitsServ) or $unitsServ == "0") {
  5448.                             $unitsServ = 1;
  5449.                         }
  5450.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5451.                         $subnetoUnit = $subneto;
  5452.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5453.                         $data_supplier['service'][$i] = array (
  5454.                             'id' => $item->getId(),
  5455.                             'serviceCatId' => $item->getServiceCatId(),
  5456.                             'serviceName' => $item->getName(),
  5457.                             'serviceType' => 'AV',
  5458.                             'date' => $dateServ,
  5459.                             'qty' => $unitsServ,
  5460.                             'iva' => $item->getIva(),
  5461.                             'pax' => $item->getPax(),
  5462.                             'precioUnit' => $subnetoUnit,
  5463.                             'subneto' => $subneto,
  5464.                             'subtotal' => $subtotal,
  5465.                         );
  5466.                         break;
  5467.                     case 4: //Creative
  5468.                         $pax = $item->getPax();
  5469.                         if (empty($pax) or $pax == "0") {
  5470.                             $pax = 1;
  5471.                         }
  5472.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5473.                         if ($days > 1){
  5474.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5475.                         } else {
  5476.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5477.                         }
  5478.                         $unitsServ = $item->getUnits();
  5479.                         if (empty($unitsServ) or $unitsServ == "0") {
  5480.                             $unitsServ = 1;
  5481.                         }
  5482.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5483.                         $subnetoUnit = $subneto;
  5484.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5485.                         $data_supplier['service'][$i] = array (
  5486.                             'id' => $item->getId(),
  5487.                             'serviceCatId' => $item->getServiceCatId(),
  5488.                             'serviceName' => $item->getName(),
  5489.                             'serviceType' => 'Creativo',
  5490.                             'date' => $dateServ,
  5491.                             'qty' => $unitsServ,
  5492.                             'iva' => $item->getIva(),
  5493.                             'pax' => $item->getPax(),
  5494.                             'precioUnit' => $subnetoUnit,
  5495.                             'subneto' => $subneto,
  5496.                             'subtotal' => $subtotal,
  5497.                         );
  5498.                         break;
  5499.                     case 5: //Cruise
  5500.                         $pax = $item->getPax();
  5501.                         if (empty($pax) or $pax == "0") {
  5502.                             $pax = 1;
  5503.                         }
  5504.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  5505.                         if ($days > 1){
  5506.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5507.                         } else {
  5508.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5509.                         }
  5510.                         $unitsServ = $item->getUnits();
  5511.                         if (empty($unitsServ) or $unitsServ == "0") {
  5512.                             $unitsServ = 1;
  5513.                         }
  5514.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5515.                         $subnetoUnit = $subneto;
  5516.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5517.                         $data_supplier['service'][$i] = array (
  5518.                             'id' => $item->getId(),
  5519.                             'serviceCatId' => $item->getServiceCatId(),
  5520.                             'serviceName' => $item->getName(),
  5521.                             'serviceType' => 'Crucero',
  5522.                             'date' => $dateServ,
  5523.                             'qty' => $unitsServ,
  5524.                             'iva' => $item->getIva(),
  5525.                             'pax' => $item->getPax(),
  5526.                             'precioUnit' => $subnetoUnit,
  5527.                             'subneto' => $subneto,
  5528.                             'subtotal' => $subtotal,
  5529.                         );
  5530.                         break;
  5531.                     case 6: //Entertaiment
  5532.                         $pax = $item->getPax();
  5533.                         if (empty($pax) or $pax == "0") {
  5534.                             $pax = 1;
  5535.                         }
  5536.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5537.                         if ($days > 1){
  5538.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5539.                         } else {
  5540.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5541.                         }
  5542.                         $unitsServ = $item->getUnits();
  5543.                         if (empty($unitsServ) or $unitsServ == "0") {
  5544.                             $unitsServ = 1;
  5545.                         }
  5546.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5547.                         $subnetoUnit = $subneto;
  5548.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5549.                         $data_supplier['service'][$i] = array (
  5550.                             'id' => $item->getId(),
  5551.                             'serviceCatId' => $item->getServiceCatId(),
  5552.                             'serviceName' => $item->getName(),
  5553.                             'serviceType' => 'Entretenimiento',
  5554.                             'date' => $dateServ,
  5555.                             'qty' => $unitsServ,
  5556.                             'iva' => $item->getIva(),
  5557.                             'pax' => $item->getPax(),
  5558.                             'precioUnit' => $subnetoUnit,
  5559.                             'subneto' => $subneto,
  5560.                             'subtotal' => $subtotal,
  5561.                         );
  5562.                         break;
  5563.                     case 7: // Gifts
  5564.                         $pax = $item->getPax();
  5565.                         if (empty($pax) or $pax == "0") {
  5566.                             $pax = 1;
  5567.                         }
  5568. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5569.                         $days = 1;
  5570.                         if ($days > 1){
  5571.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5572.                         } else {
  5573.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5574.                         }
  5575.                         $unitsServ = $item->getUnits();
  5576.                         if (empty($unitsServ) or $unitsServ == "0") {
  5577.                             $unitsServ = 1;
  5578.                         }
  5579.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5580.                         $subnetoUnit = $subneto;
  5581.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5582.                         $data_supplier['service'][$i] = array (
  5583.                             'id' => $item->getId(),
  5584.                             'serviceCatId' => $item->getServiceCatId(),
  5585.                             'serviceName' => $item->getName(),
  5586.                             'serviceType' => 'Regalos',
  5587.                             'date' => $dateServ,
  5588.                             'qty' => $unitsServ,
  5589.                             'iva' => $item->getIva(),
  5590.                             'pax' => $item->getPax(),
  5591.                             'precioUnit' => $subnetoUnit,
  5592.                             'subneto' => $subneto,
  5593.                             'subtotal' => $subtotal,
  5594.                         );
  5595.                         break;
  5596.                     case 8: //Guide
  5597.                         $pax = $item->getPax();
  5598.                         if (empty($pax) or $pax == "0") {
  5599.                             $pax = 1;
  5600.                         }
  5601.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5602.                         if ($days > 1){
  5603.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5604.                         } else {
  5605.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5606.                         }
  5607.                         $unitsServ = $item->getUnits();
  5608.                         if (empty($unitsServ) or $unitsServ == "0") {
  5609.                             $unitsServ = 1;
  5610.                         }
  5611.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5612.                         $subnetoUnit = $subneto;
  5613.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5614.                         $data_supplier['service'][$i] = array (
  5615.                             'id' => $item->getId(),
  5616.                             'serviceCatId' => $item->getServiceCatId(),
  5617.                             'serviceName' => $item->getName(),
  5618.                             'serviceType' => 'Regalos',
  5619.                             'date' => $dateServ,
  5620.                             'qty' => $unitsServ,
  5621.                             'iva' => $item->getIva(),
  5622.                             'pax' => $item->getPax(),
  5623.                             'precioUnit' => $subnetoUnit,
  5624.                             'subneto' => $subneto,
  5625.                             'subtotal' => $subtotal,
  5626.                         );
  5627.                         break;
  5628.                     case 9: //Itineraries
  5629.                         $pax = $item->getPax();
  5630.                         if (empty($pax) or $pax == "0") {
  5631.                             $pax = 1;
  5632.                         }
  5633.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5634.                         if ($days > 1){
  5635.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5636.                         } else {
  5637.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5638.                         }
  5639.                         $unitsServ = $item->getUnits();
  5640.                         if (empty($unitsServ) or $unitsServ == "0") {
  5641.                             $unitsServ = 1;
  5642.                         }
  5643.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5644.                         $subnetoUnit = $subneto;
  5645.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5646.                         $data_supplier['service'][$i] = array (
  5647.                             'id' => $item->getId(),
  5648.                             'serviceCatId' => $item->getServiceCatId(),
  5649.                             'serviceName' => $item->getName(),
  5650.                             'serviceType' => 'Itinerarios',
  5651.                             'date' => $dateServ,
  5652.                             'qty' => $unitsServ,
  5653.                             'iva' => $item->getIva(),
  5654.                             'pax' => $item->getPax(),
  5655.                             'precioUnit' => $subnetoUnit,
  5656.                             'subneto' => $subneto,
  5657.                             'subtotal' => $subtotal,
  5658.                         );
  5659.                         break;
  5660.                     case 10: //Lounge  -- No Aplica
  5661. //                        $pax = $item->getPax();
  5662. //                        if (empty($pax) or $pax == "0") {
  5663. //                            $pax = 1;
  5664. //                        }
  5665. //
  5666. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5667. //                        if ($days > 1){
  5668. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5669. //                        } else {
  5670. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5671. //                        }
  5672. //
  5673. //                        $unitsServ = $item->getUnits();
  5674. //                        if (empty($unitsServ) or $unitsServ == "0") {
  5675. //                            $unitsServ = 1;
  5676. //                        }
  5677. //
  5678. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5679. //                        $subnetoUnit = $subneto;
  5680. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  5681. //
  5682. //                        $data_supplier['service'][$i] = array (
  5683. //                            'id' => $item->getId(),
  5684. //                            'serviceCatId' => $item->getServiceCatId(),
  5685. //                            'serviceName' => $item->getName(),
  5686. //                            'serviceType' => 'Itinerarios',
  5687. //                            'date' => $dateServ,
  5688. //                            'qty' => $unitsServ,
  5689. //                            'iva' => $item->getIva(),
  5690. //                            'pax' => $item->getPax(),
  5691. //                            'precioUnit' => $subnetoUnit,
  5692. //                            'subneto' => $subneto,
  5693. //                            'subtotal' => $subtotal,
  5694. //                        );
  5695.                         break;
  5696.                     case 11: //Menu
  5697.                         $pax = $item->getPax();
  5698.                         if (empty($pax) or $pax == "0") {
  5699.                             $pax = 1;
  5700.                         }
  5701.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5702.                         if ($days > 1){
  5703.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5704.                         } else {
  5705.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5706.                         }
  5707.                         $unitsServ = $item->getUnits();
  5708.                         if (empty($unitsServ) or $unitsServ == "0") {
  5709.                             $unitsServ = 1;
  5710.                         }
  5711.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5712.                         $subnetoUnit = $subneto;
  5713.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5714.                         $data_supplier['service'][$i] = array (
  5715.                             'id' => $item->getId(),
  5716.                             'serviceCatId' => $item->getServiceCatId(),
  5717.                             'serviceName' => $item->getName(),
  5718.                             'serviceType' => 'Menú',
  5719.                             'date' => $dateServ,
  5720.                             'qty' => $unitsServ,
  5721.                             'iva' => $item->getIva(),
  5722.                             'pax' => $item->getPax(),
  5723.                             'precioUnit' => $subnetoUnit,
  5724.                             'subneto' => $subneto,
  5725.                             'subtotal' => $subtotal,
  5726.                         );
  5727.                         break;
  5728.                     case 12: //Others
  5729.                         $pax = $item->getPax();
  5730.                         if (empty($pax) or $pax == "0") {
  5731.                             $pax = 1;
  5732.                         }
  5733.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5734.                         if ($days > 1){
  5735.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5736.                         } else {
  5737.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5738.                         }
  5739.                         $unitsServ = $item->getUnits();
  5740.                         if (empty($unitsServ) or $unitsServ == "0") {
  5741.                             $unitsServ = 1;
  5742.                         }
  5743.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5744.                         $subnetoUnit = $subneto;
  5745.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5746.                         $data_supplier['service'][$i] = array (
  5747.                             'id' => $item->getId(),
  5748.                             'serviceCatId' => $item->getServiceCatId(),
  5749.                             'serviceName' => $item->getName(),
  5750.                             'serviceType' => 'Otros',
  5751.                             'date' => $dateServ,
  5752.                             'qty' => $unitsServ,
  5753.                             'iva' => $item->getIva(),
  5754.                             'pax' => $item->getPax(),
  5755.                             'precioUnit' => $subnetoUnit,
  5756.                             'subneto' => $subneto,
  5757.                             'subtotal' => $subtotal,
  5758.                         );
  5759.                         break;
  5760.                     case 13: //Transport
  5761.                         $pax = $item->getPax();
  5762.                         if (empty($pax) or $pax == "0") {
  5763.                             $pax = 1;
  5764.                         }
  5765.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5766.                         if ($days > 1){
  5767.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5768.                         } else {
  5769.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5770.                         }
  5771.                         $unitsServ = $item->getUnits();
  5772.                         if (empty($unitsServ) or $unitsServ == "0") {
  5773.                             $unitsServ = 1;
  5774.                         }
  5775.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5776.                         $subnetoUnit = $subneto;
  5777.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5778.                         $data_supplier['service'][$i] = array (
  5779.                             'id' => $item->getId(),
  5780.                             'serviceCatId' => $item->getServiceCatId(),
  5781.                             'serviceName' => $item->getName(),
  5782.                             'serviceType' => 'Transporte',
  5783.                             'date' => $dateServ,
  5784.                             'qty' => $unitsServ,
  5785.                             'iva' => $item->getIva(),
  5786.                             'pax' => $item->getPax(),
  5787.                             'precioUnit' => $subnetoUnit,
  5788.                             'subneto' => $subneto,
  5789.                             'subtotal' => $subtotal,
  5790.                         );
  5791.                         break;
  5792.                     case 14: //Technology
  5793.                         $pax = $item->getPax();
  5794.                         if (empty($pax) or $pax == "0") {
  5795.                             $pax = 1;
  5796.                         }
  5797. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5798.                         $days = 1;
  5799. //                        if ($days > 1){
  5800. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5801. //                        } else {
  5802. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5803. //                        }
  5804.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5805.                         $unitsServ = $item->getUnits();
  5806.                         if (empty($unitsServ) or $unitsServ == "0") {
  5807.                             $unitsServ = 1;
  5808.                         }
  5809.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5810.                         $subnetoUnit = $subneto;
  5811.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5812.                         $data_supplier['service'][$i] = array (
  5813.                             'id' => $item->getId(),
  5814.                             'serviceCatId' => $item->getServiceCatId(),
  5815.                             'serviceName' => $item->getName(),
  5816.                             'serviceType' => 'Tecnología',
  5817.                             'date' => $dateServ,
  5818.                             'qty' => $unitsServ,
  5819.                             'iva' => $item->getIva(),
  5820.                             'pax' => $item->getPax(),
  5821.                             'precioUnit' => $subnetoUnit,
  5822.                             'subneto' => $subneto,
  5823.                             'subtotal' => $subtotal,
  5824.                         );
  5825.                         break;
  5826.                     case 15: //Assisstant
  5827.                         $pax = $item->getPax();
  5828.                         if (empty($pax) or $pax == "0") {
  5829.                             $pax = 1;
  5830.                         }
  5831.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5832.                         if ($days > 1){
  5833.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5834.                         } else {
  5835.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5836.                         }
  5837.                         $unitsServ = $item->getUnits();
  5838.                         if (empty($unitsServ) or $unitsServ == "0") {
  5839.                             $unitsServ = 1;
  5840.                         }
  5841.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5842.                         $subnetoUnit = $subneto;
  5843.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5844.                         $data_supplier['service'][$i] = array (
  5845.                             'id' => $item->getId(),
  5846.                             'serviceCatId' => $item->getServiceCatId(),
  5847.                             'serviceName' => $item->getName(),
  5848.                             'serviceType' => 'Asistente',
  5849.                             'date' => $dateServ,
  5850.                             'qty' => $unitsServ,
  5851.                             'iva' => $item->getIva(),
  5852.                             'pax' => $item->getPax(),
  5853.                             'precioUnit' => $subnetoUnit,
  5854.                             'subneto' => $subneto,
  5855.                             'subtotal' => $subtotal,
  5856.                         );
  5857.                         break;
  5858.                     case 16: //DDR
  5859.                         $pax = $item->getPax();
  5860.                         if (empty($pax) or $pax == "0") {
  5861.                             $pax = 1;
  5862.                         }
  5863.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  5864.                         if ($days > 1){
  5865.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  5866.                         } else {
  5867.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  5868.                         }
  5869.                         $unitsServ = $item->getUnits();
  5870.                         if (empty($unitsServ) or $unitsServ == "0") {
  5871.                             $unitsServ = 1;
  5872.                         }
  5873.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  5874.                         $subnetoUnit = $subneto;
  5875.                         $subneto = $subneto * $days * $unitsServ * $pax;
  5876.                         $data_supplier['service'][$i] = array (
  5877.                             'id' => $item->getId(),
  5878.                             'serviceCatId' => $item->getServiceCatId(),
  5879.                             'serviceName' => $item->getName(),
  5880.                             'serviceType' => 'DDR',
  5881.                             'date' => $dateServ,
  5882.                             'qty' => $unitsServ,
  5883.                             'iva' => $item->getIva(),
  5884.                             'pax' => $item->getPax(),
  5885.                             'precioUnit' => $subnetoUnit,
  5886.                             'subneto' => $subneto,
  5887.                             'subtotal' => $subtotal,
  5888.                         );
  5889.                         break;
  5890.                     default:
  5891.                         break;
  5892.                 }
  5893.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  5894.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  5895.                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  5896.                 $arrayItems[] = array(
  5897.                     'type' => 'Service',
  5898.                     'name' => $item->getName(),
  5899.                     'amount' => $neto,
  5900.                     'iva' => $item->getIva(),
  5901.                     'total' => $subtotal,
  5902.                 );
  5903.             }
  5904.             switch ($item->getIva()){
  5905.                 // Acumula IVA
  5906.                 case 21:
  5907.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva()/100));
  5908.                     break;
  5909.                 case 10:
  5910.                     $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva()/100));
  5911.                     break;
  5912.                 case 0:
  5913.                     break;
  5914.                 default:
  5915.                     break;
  5916.             }
  5917.             $totales_neto_all = $totales_neto_all + $neto;
  5918.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  5919.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  5920.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  5921.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  5922.             // Acumula netos totales e IVA
  5923.             $service['neto'] = $service['neto'] + $neto;
  5924.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  5925.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  5926.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  5927.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  5928.             $i++;
  5929.         }
  5930.         $data_supplier['serviceSubTotal'] = array(
  5931.             'neto' => $service['neto'],
  5932.             'sumSubT' => $service['sumSubT'],
  5933.         );
  5934.         $currency = '€';
  5935.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  5936.         if (!empty($payments)) {
  5937.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  5938.         } else {
  5939.             $amount_pay = 0;
  5940.         }
  5941.         $totales_all = $totales_total - $amount_pay;
  5942.         if ($type == 'Invoice'){
  5943.             $em->clear();   // sin este clear se producia error al guardar
  5944.             $newInvoice = $em->getRepository(MdvDocInvoice::class)->findOneById($numberInvoiceOrProforma);
  5945.             $newInvoice->setBalance($totales_all);
  5946.             $em->persist($newInvoice);
  5947.             $em->flush();
  5948.         }
  5949.         $data = array(
  5950.             'id' => $id,
  5951.             'type' => $type,
  5952.             'number' => $number,
  5953.             'prefix' => 'GPF-'.(new DateTime('now'))->format('dmy').'-',
  5954.             'date' => new DateTime('now'),
  5955.             'file' => $file,
  5956. //            'token' => $file->getAccessKey(),
  5957.             'company' => $company,
  5958.             'clients' => $client,
  5959.             'arrayItems' => $arrayItems,
  5960.             'datasupplier' => $data_supplier,
  5961.             'currency' => $currency,
  5962.             'totales_neto' => $totales_neto_all,
  5963.             'bases_imponibles' => $data_iva,
  5964.             'totales' => $totales_total,
  5965.             'balance' => $totales_all,
  5966.             'paymentInvoice' => $amount_pay,
  5967.         );
  5968.         return $data;
  5969.     }
  5970.     private function baseInvoiceFile($id, $type, $number, $prefix, $date)
  5971.     {
  5972.         $em = $this->getDoctrine()->getManager();
  5973.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  5974.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  5975.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  5976.         if (empty($client)){
  5977.             $client[0] = new Client();
  5978.             $client[0]->setName('');
  5979.             $client[0]->setTitle('');
  5980.             $client[0]->setIdDocument('');
  5981.             $client[0]->setPopulation('');
  5982.             $client[0]->setRegion('');
  5983.             $client[0]->setCountry('');
  5984.         } else {
  5985.             if (is_numeric($client[0]->getPopulation())){
  5986.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  5987.                 $client[0]->setPopulation($city->getCity());
  5988.             }
  5989.             if (is_numeric($client[0]->getRegion())){
  5990.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  5991.                 $client[0]->setRegion($region->getRegion());
  5992.             }
  5993.             if (is_numeric($client[0]->getCountry())){
  5994.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  5995.                 $client[0]->setCountry($country->getCountry());
  5996.             }
  5997.         }
  5998.         // Acumuladores de los calculos
  5999.         $totales_neto_all = 0;
  6000.         $data_iva = array(
  6001.             'iva' => 21,
  6002.             'ivaMontoVeintiUno' => 0,
  6003.             'ivaMontoDiez' => 0,
  6004.             'ivaMontoCero' => 0,
  6005.         );
  6006.         // Buscamos las salas reservadas, pagos y servicios para el evento
  6007.         $productsInFile = $em->getRepository(MdvProductFile::class)->findByFileId($id);
  6008.         $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  6009.         $services = $em->getRepository(MdvServices::class)->findByFileId($id);
  6010.         $data_supplier = array();
  6011.         $i = 0;
  6012.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  6013.         $pax = '1';
  6014.         $qty = '1';
  6015.         $product = array(
  6016.             'neto' => 0,
  6017.             'sumSubT' => 0,
  6018.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  6019.         $service = array(
  6020.             'neto' => 0,
  6021.             'sumSubT' => 0,
  6022.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  6023.         foreach ($productsInFile as $item){
  6024.             // Verificamos que el producto no se encuentre ya en una factura
  6025.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findByPrdControlId($item->getId());
  6026.             $days = 1;
  6027.             $factorIva = (empty($item->getIva()) and !($item->getIva() == 0)) ? 21 : $item->getIva();
  6028.             $factorIva = $factorIva / 100;
  6029.             if (!((empty($item->getDateInAt()) or empty($item->getDateOutAt())))){
  6030.                 $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6031.             }
  6032.             $pax = $item->getPax();
  6033.             $qty = $item->getUnits();
  6034.             if (!empty($item->getDays())){ $days = $item->getDays();}
  6035.             if (is_null($item->getServicePrice()) or empty($item->getServicePrice()) or !empty($existe)){
  6036.                 $subtotal = 0;
  6037.                 $neto = 0;
  6038.                 $subtotalProduct = 0;
  6039.             } else {
  6040.                 $subtotalProduct = $item->getSubTotalPrice() * (1 + $factorIva);
  6041.                 $subneto = $item->getSubTotalPrice();
  6042.                 $subtotal =  $subtotalProduct;
  6043.                 $neto =  $subneto;
  6044.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  6045.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  6046.                 $neto = round($neto,2,PHP_ROUND_HALF_UP);
  6047.             }
  6048.             // Acumula netos totales e IVA
  6049.             $totales_neto_all = $totales_neto_all + $neto;
  6050. //            $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21);
  6051.             switch ($factorIva){
  6052.                 case 0.21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * $factorIva); break;
  6053.                 case 0: $data_iva['ivaMontoCero'] = 0; break;
  6054.             }
  6055.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  6056.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  6057.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  6058.             // Acumula netos totales e IVA
  6059.             $product['neto'] = $product['neto'] + $neto;
  6060.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  6061.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  6062.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  6063.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  6064.             $data_supplier['product'][$i] = array (
  6065.                 'id' => $item->getId(),
  6066.                 'productName' => $item->getProductName(),
  6067.                 'productId' => $item->getProductId(),
  6068.                 'dateStart' => $item->getDateStart(),
  6069.                 'dateEnd' => $item->getDateEnd(),
  6070.                 'servicePrice' => $item->getServicePrice(),
  6071.                 'subTotalPrice' => $item->getSubTotalPrice(),
  6072.                 'subtotalProduct' => $subtotalProduct,
  6073.                 'iva' => ($factorIva * 100),
  6074.                 'pax' => $pax,
  6075.                 'qty' => $qty,
  6076.                 'days' => $days,
  6077.                 'type' => $item->getType(),
  6078.                 'subtotal' => $subtotal,
  6079.             );
  6080.             $i++;
  6081.         }
  6082.         $data_supplier['productSubTotal'] = array(
  6083.             'neto' => $product['neto'],
  6084.             'sumSubT' => $product['sumSubT'],
  6085.         );
  6086.         $payment = array(
  6087.             'sumSubT' => 0,
  6088.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  6089.         $i = 0;
  6090.         foreach ($payments as $item){
  6091.             // Verificamos que el pago no se encuentre ya en una factura
  6092.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findByPayControlId($item->getId());
  6093.             if (!empty($existe)){
  6094.                 $payment['sumSubT'] = $payment['sumSubT'] + 0;
  6095.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  6096.                 $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  6097.             } else {
  6098.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  6099.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  6100.                 $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  6101.             }
  6102.             $data_supplier['payment'][$i] = array (
  6103.                 'id' => $item->getId(),
  6104.                 'amount' => $item->getAmount(),
  6105.                 'datePayAt' => $item->getDatePayAt(),
  6106.                 'wayToPay' => $item->getWayToPay(),
  6107.                 'amountTotal' => $item->getAmountTotal(),
  6108.                 'vat' => $item->getVat(),
  6109.             );
  6110.             $i++;
  6111.         }
  6112.         if (!empty($payments)) {
  6113.             $data_supplier['paymentSubTotal'] = array(
  6114.                 'sumSubT' => $payment['sumSubT'],
  6115.             );
  6116.         }
  6117.         foreach ($services as $item){
  6118.             // Verificamos que el servicio no se encuentre ya en una factura
  6119.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findBySrvControlId($item->getId());
  6120.             if (is_null($item->getPrice()) or empty($item->getPrice()) or !empty($existe)){
  6121.                 $subtotal = 0;
  6122.                 $neto = 0;
  6123.                 $subtotalService = 0;
  6124.                 $subneto = 0;
  6125.                 switch ($item->getServiceCatId()){
  6126.                     case 1: // Alojamiento
  6127.                         // el numero de noches $numNoches; precio unitario $subneto
  6128.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  6129.                         // La personas no afectan este calculo
  6130.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  6131.                         $subnetoUnit = $subneto;
  6132.                         $subneto = $subneto * $numNoches * $item->getUnits();
  6133.                         $data_supplier['service'][$i] = array (
  6134.                             'id' => $item->getId(),
  6135.                             'serviceCatId' => $item->getServiceCatId(),
  6136.                             'serviceName' => $item->getName(),
  6137.                             'serviceType' => 'Hotel',
  6138.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  6139.                             'qty' => $item->getUnits(),
  6140.                             'iva' => $item->getIva(),
  6141.                             'pax' => '-',
  6142.                             'precioUnit' => $subnetoUnit,
  6143.                             'subneto' => $subneto,
  6144.                             'subtotal' => $subtotal,
  6145.                         );
  6146.                         break;
  6147.                     case 2: //Actividades
  6148.                         // El número de personas es considerado en el calculo
  6149.                         $pax = $item->getPax();
  6150.                         if (empty($pax) or $pax == "0") {
  6151.                             $pax = 1;
  6152.                         }
  6153.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6154.                         if ($days > 1){
  6155.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6156.                         } else {
  6157.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6158.                         }
  6159.                         $subtotal = $subtotalService * $days * $item->getUnits();
  6160.                         $subnetoUnit = $subneto;
  6161.                         $subneto = $subneto * $days * $item->getUnits();
  6162.                         $data_supplier['service'][$i] = array (
  6163.                             'id' => $item->getId(),
  6164.                             'serviceCatId' => $item->getServiceCatId(),
  6165.                             'serviceName' => $item->getName(),
  6166.                             'serviceType' => 'Actividad',
  6167.                             'date' => $dateServ,
  6168.                             'qty' => $item->getUnits(),
  6169.                             'iva' => $item->getIva(),
  6170.                             'pax' => $item->getPax(),
  6171.                             'precioUnit' => $subnetoUnit,
  6172.                             'subneto' => $subneto,
  6173.                             'subtotal' => $subtotal,
  6174.                         );
  6175.                         break;
  6176.                     case 3: // AV
  6177.                         $pax = $item->getPax();
  6178.                         if (empty($pax) or $pax == "0") {
  6179.                             $pax = 1;
  6180.                         }
  6181.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6182.                         if ($days > 1){
  6183.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6184.                         } else {
  6185.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6186.                         }
  6187.                         $unitsServ = $item->getUnits();
  6188.                         if (empty($unitsServ) or $unitsServ == "0") {
  6189.                             $unitsServ = 1;
  6190.                         }
  6191.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6192.                         $subnetoUnit = $subneto;
  6193.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6194.                         $data_supplier['service'][$i] = array (
  6195.                             'id' => $item->getId(),
  6196.                             'serviceCatId' => $item->getServiceCatId(),
  6197.                             'serviceName' => $item->getName(),
  6198.                             'serviceType' => 'AV',
  6199.                             'date' => $dateServ,
  6200.                             'qty' => $unitsServ,
  6201.                             'iva' => $item->getIva(),
  6202.                             'pax' => $item->getPax(),
  6203.                             'precioUnit' => $subnetoUnit,
  6204.                             'subneto' => $subneto,
  6205.                             'subtotal' => $subtotal,
  6206.                         );
  6207.                         break;
  6208.                     case 4: //Creative
  6209.                         $pax = $item->getPax();
  6210.                         if (empty($pax) or $pax == "0") {
  6211.                             $pax = 1;
  6212.                         }
  6213.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6214.                         if ($days > 1){
  6215.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6216.                         } else {
  6217.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6218.                         }
  6219.                         $unitsServ = $item->getUnits();
  6220.                         if (empty($unitsServ) or $unitsServ == "0") {
  6221.                             $unitsServ = 1;
  6222.                         }
  6223.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6224.                         $subnetoUnit = $subneto;
  6225.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6226.                         $data_supplier['service'][$i] = array (
  6227.                             'id' => $item->getId(),
  6228.                             'serviceCatId' => $item->getServiceCatId(),
  6229.                             'serviceName' => $item->getName(),
  6230.                             'serviceType' => 'Creativo',
  6231.                             'date' => $dateServ,
  6232.                             'qty' => $unitsServ,
  6233.                             'iva' => $item->getIva(),
  6234.                             'pax' => $item->getPax(),
  6235.                             'precioUnit' => $subnetoUnit,
  6236.                             'subneto' => $subneto,
  6237.                             'subtotal' => $subtotal,
  6238.                         );
  6239.                         break;
  6240.                     case 5: //Cruise
  6241.                         $pax = $item->getPax();
  6242.                         if (empty($pax) or $pax == "0") {
  6243.                             $pax = 1;
  6244.                         }
  6245.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  6246.                         if ($days > 1){
  6247.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6248.                         } else {
  6249.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6250.                         }
  6251.                         $unitsServ = $item->getUnits();
  6252.                         if (empty($unitsServ) or $unitsServ == "0") {
  6253.                             $unitsServ = 1;
  6254.                         }
  6255.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6256.                         $subnetoUnit = $subneto;
  6257.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6258.                         $data_supplier['service'][$i] = array (
  6259.                             'id' => $item->getId(),
  6260.                             'serviceCatId' => $item->getServiceCatId(),
  6261.                             'serviceName' => $item->getName(),
  6262.                             'serviceType' => 'Crucero',
  6263.                             'date' => $dateServ,
  6264.                             'qty' => $unitsServ,
  6265.                             'iva' => $item->getIva(),
  6266.                             'pax' => $item->getPax(),
  6267.                             'precioUnit' => $subnetoUnit,
  6268.                             'subneto' => $subneto,
  6269.                             'subtotal' => $subtotal,
  6270.                         );
  6271.                         break;
  6272.                     case 6: //Entertaiment
  6273.                         $pax = $item->getPax();
  6274.                         if (empty($pax) or $pax == "0") {
  6275.                             $pax = 1;
  6276.                         }
  6277.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6278.                         if ($days > 1){
  6279.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6280.                         } else {
  6281.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6282.                         }
  6283.                         $unitsServ = $item->getUnits();
  6284.                         if (empty($unitsServ) or $unitsServ == "0") {
  6285.                             $unitsServ = 1;
  6286.                         }
  6287.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6288.                         $subnetoUnit = $subneto;
  6289.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6290.                         $data_supplier['service'][$i] = array (
  6291.                             'id' => $item->getId(),
  6292.                             'serviceCatId' => $item->getServiceCatId(),
  6293.                             'serviceName' => $item->getName(),
  6294.                             'serviceType' => 'Entretenimiento',
  6295.                             'date' => $dateServ,
  6296.                             'qty' => $unitsServ,
  6297.                             'iva' => $item->getIva(),
  6298.                             'pax' => $item->getPax(),
  6299.                             'precioUnit' => $subnetoUnit,
  6300.                             'subneto' => $subneto,
  6301.                             'subtotal' => $subtotal,
  6302.                         );
  6303.                         break;
  6304.                     case 7: // Gifts
  6305.                         $pax = $item->getPax();
  6306.                         if (empty($pax) or $pax == "0") {
  6307.                             $pax = 1;
  6308.                         }
  6309.                         $days = 1;
  6310.                         if ($days > 1){
  6311.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6312.                         } else {
  6313.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6314.                         }
  6315.                         $unitsServ = $item->getUnits();
  6316.                         if (empty($unitsServ) or $unitsServ == "0") {
  6317.                             $unitsServ = 1;
  6318.                         }
  6319.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6320.                         $subnetoUnit = $subneto;
  6321.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6322.                         $data_supplier['service'][$i] = array (
  6323.                             'id' => $item->getId(),
  6324.                             'serviceCatId' => $item->getServiceCatId(),
  6325.                             'serviceName' => $item->getName(),
  6326.                             'serviceType' => 'Regalos',
  6327.                             'date' => $dateServ,
  6328.                             'qty' => $unitsServ,
  6329.                             'iva' => $item->getIva(),
  6330.                             'pax' => $item->getPax(),
  6331.                             'precioUnit' => $subnetoUnit,
  6332.                             'subneto' => $subneto,
  6333.                             'subtotal' => $subtotal,
  6334.                         );
  6335.                         break;
  6336.                     case 8: //Guide
  6337.                         $pax = $item->getPax();
  6338.                         if (empty($pax) or $pax == "0") {
  6339.                             $pax = 1;
  6340.                         }
  6341.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6342.                         if ($days > 1){
  6343.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6344.                         } else {
  6345.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6346.                         }
  6347.                         $unitsServ = $item->getUnits();
  6348.                         if (empty($unitsServ) or $unitsServ == "0") {
  6349.                             $unitsServ = 1;
  6350.                         }
  6351.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6352.                         $subnetoUnit = $subneto;
  6353.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6354.                         $data_supplier['service'][$i] = array (
  6355.                             'id' => $item->getId(),
  6356.                             'serviceCatId' => $item->getServiceCatId(),
  6357.                             'serviceName' => $item->getName(),
  6358.                             'serviceType' => 'Regalos',
  6359.                             'date' => $dateServ,
  6360.                             'qty' => $unitsServ,
  6361.                             'iva' => $item->getIva(),
  6362.                             'pax' => $item->getPax(),
  6363.                             'precioUnit' => $subnetoUnit,
  6364.                             'subneto' => $subneto,
  6365.                             'subtotal' => $subtotal,
  6366.                         );
  6367.                         break;
  6368.                     case 9: //Itineraries
  6369.                         $pax = $item->getPax();
  6370.                         if (empty($pax) or $pax == "0") {
  6371.                             $pax = 1;
  6372.                         }
  6373.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6374.                         if ($days > 1){
  6375.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6376.                         } else {
  6377.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6378.                         }
  6379.                         $unitsServ = $item->getUnits();
  6380.                         if (empty($unitsServ) or $unitsServ == "0") {
  6381.                             $unitsServ = 1;
  6382.                         }
  6383.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6384.                         $subnetoUnit = $subneto;
  6385.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6386.                         $data_supplier['service'][$i] = array (
  6387.                             'id' => $item->getId(),
  6388.                             'serviceCatId' => $item->getServiceCatId(),
  6389.                             'serviceName' => $item->getName(),
  6390.                             'serviceType' => 'Itinerarios',
  6391.                             'date' => $dateServ,
  6392.                             'qty' => $unitsServ,
  6393.                             'iva' => $item->getIva(),
  6394.                             'pax' => $item->getPax(),
  6395.                             'precioUnit' => $subnetoUnit,
  6396.                             'subneto' => $subneto,
  6397.                             'subtotal' => $subtotal,
  6398.                         );
  6399.                         break;
  6400.                     case 10: //Lounge  -- No Aplica
  6401. //                        $pax = $item->getPax();
  6402. //                        if (empty($pax) or $pax == "0") {
  6403. //                            $pax = 1;
  6404. //                        }
  6405. //
  6406. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6407. //                        if ($days > 1){
  6408. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6409. //                        } else {
  6410. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6411. //                        }
  6412. //
  6413. //                        $unitsServ = $item->getUnits();
  6414. //                        if (empty($unitsServ) or $unitsServ == "0") {
  6415. //                            $unitsServ = 1;
  6416. //                        }
  6417. //
  6418. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6419. //                        $subnetoUnit = $subneto;
  6420. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  6421. //
  6422. //                        $data_supplier['service'][$i] = array (
  6423. //                            'id' => $item->getId(),
  6424. //                            'serviceCatId' => $item->getServiceCatId(),
  6425. //                            'serviceName' => $item->getName(),
  6426. //                            'serviceType' => 'Itinerarios',
  6427. //                            'date' => $dateServ,
  6428. //                            'qty' => $unitsServ,
  6429. //                            'iva' => $item->getIva(),
  6430. //                            'pax' => $item->getPax(),
  6431. //                            'precioUnit' => $subnetoUnit,
  6432. //                            'subneto' => $subneto,
  6433. //                            'subtotal' => $subtotal,
  6434. //                        );
  6435.                         break;
  6436.                     case 11: //Menu
  6437.                         $pax = $item->getPax();
  6438.                         if (empty($pax) or $pax == "0") {
  6439.                             $pax = 1;
  6440.                         }
  6441.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6442.                         if ($days > 1){
  6443.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6444.                         } else {
  6445.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6446.                         }
  6447.                         $unitsServ = $item->getUnits();
  6448.                         if (empty($unitsServ) or $unitsServ == "0") {
  6449.                             $unitsServ = 1;
  6450.                         }
  6451.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6452.                         $subnetoUnit = $subneto;
  6453.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6454.                         $data_supplier['service'][$i] = array (
  6455.                             'id' => $item->getId(),
  6456.                             'serviceCatId' => $item->getServiceCatId(),
  6457.                             'serviceName' => $item->getName(),
  6458.                             'serviceType' => 'Menú',
  6459.                             'date' => $dateServ,
  6460.                             'qty' => $unitsServ,
  6461.                             'iva' => $item->getIva(),
  6462.                             'pax' => $item->getPax(),
  6463.                             'precioUnit' => $subnetoUnit,
  6464.                             'subneto' => $subneto,
  6465.                             'subtotal' => $subtotal,
  6466.                         );
  6467.                         break;
  6468.                     case 12: //Others
  6469.                         $pax = $item->getPax();
  6470.                         if (empty($pax) or $pax == "0") {
  6471.                             $pax = 1;
  6472.                         }
  6473.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6474.                         if ($days > 1){
  6475.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6476.                         } else {
  6477.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6478.                         }
  6479.                         $unitsServ = $item->getUnits();
  6480.                         if (empty($unitsServ) or $unitsServ == "0") {
  6481.                             $unitsServ = 1;
  6482.                         }
  6483.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6484.                         $subnetoUnit = $subneto;
  6485.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6486.                         $data_supplier['service'][$i] = array (
  6487.                             'id' => $item->getId(),
  6488.                             'serviceCatId' => $item->getServiceCatId(),
  6489.                             'serviceName' => $item->getName(),
  6490.                             'serviceType' => 'Otros',
  6491.                             'date' => $dateServ,
  6492.                             'qty' => $unitsServ,
  6493.                             'iva' => $item->getIva(),
  6494.                             'pax' => $item->getPax(),
  6495.                             'precioUnit' => $subnetoUnit,
  6496.                             'subneto' => $subneto,
  6497.                             'subtotal' => $subtotal,
  6498.                         );
  6499.                         break;
  6500.                     case 13: //Transport
  6501.                         $pax = $item->getPax();
  6502.                         if (empty($pax) or $pax == "0") {
  6503.                             $pax = 1;
  6504.                         }
  6505.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6506.                         if ($days > 1){
  6507.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6508.                         } else {
  6509.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6510.                         }
  6511.                         $unitsServ = $item->getUnits();
  6512.                         if (empty($unitsServ) or $unitsServ == "0") {
  6513.                             $unitsServ = 1;
  6514.                         }
  6515.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6516.                         $subnetoUnit = $subneto;
  6517.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6518.                         $data_supplier['service'][$i] = array (
  6519.                             'id' => $item->getId(),
  6520.                             'serviceCatId' => $item->getServiceCatId(),
  6521.                             'serviceName' => $item->getName(),
  6522.                             'serviceType' => 'Transporte',
  6523.                             'date' => $dateServ,
  6524.                             'qty' => $unitsServ,
  6525.                             'iva' => $item->getIva(),
  6526.                             'pax' => $item->getPax(),
  6527.                             'precioUnit' => $subnetoUnit,
  6528.                             'subneto' => $subneto,
  6529.                             'subtotal' => $subtotal,
  6530.                         );
  6531.                         break;
  6532.                     case 14: //Technology
  6533.                         $pax = $item->getPax();
  6534.                         if (empty($pax) or $pax == "0") {
  6535.                             $pax = 1;
  6536.                         }
  6537.                         $days = 1;
  6538.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6539.                         $unitsServ = $item->getUnits();
  6540.                         if (empty($unitsServ) or $unitsServ == "0") {
  6541.                             $unitsServ = 1;
  6542.                         }
  6543.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6544.                         $subnetoUnit = $subneto;
  6545.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6546.                         $data_supplier['service'][$i] = array (
  6547.                             'id' => $item->getId(),
  6548.                             'serviceCatId' => $item->getServiceCatId(),
  6549.                             'serviceName' => $item->getName(),
  6550.                             'serviceType' => 'Tecnología',
  6551.                             'date' => $dateServ,
  6552.                             'qty' => $unitsServ,
  6553.                             'iva' => $item->getIva(),
  6554.                             'pax' => $item->getPax(),
  6555.                             'precioUnit' => $subnetoUnit,
  6556.                             'subneto' => $subneto,
  6557.                             'subtotal' => $subtotal,
  6558.                         );
  6559.                         break;
  6560.                     case 15: //Assisstant
  6561.                         $pax = $item->getPax();
  6562.                         if (empty($pax) or $pax == "0") {
  6563.                             $pax = 1;
  6564.                         }
  6565.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6566.                         if ($days > 1){
  6567.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6568.                         } else {
  6569.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6570.                         }
  6571.                         $unitsServ = $item->getUnits();
  6572.                         if (empty($unitsServ) or $unitsServ == "0") {
  6573.                             $unitsServ = 1;
  6574.                         }
  6575.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6576.                         $subnetoUnit = $subneto;
  6577.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6578.                         $data_supplier['service'][$i] = array (
  6579.                             'id' => $item->getId(),
  6580.                             'serviceCatId' => $item->getServiceCatId(),
  6581.                             'serviceName' => $item->getName(),
  6582.                             'serviceType' => 'Asistente',
  6583.                             'date' => $dateServ,
  6584.                             'qty' => $unitsServ,
  6585.                             'iva' => $item->getIva(),
  6586.                             'pax' => $item->getPax(),
  6587.                             'precioUnit' => $subnetoUnit,
  6588.                             'subneto' => $subneto,
  6589.                             'subtotal' => $subtotal,
  6590.                         );
  6591.                         break;
  6592.                     case 16: //DDR
  6593.                         $pax = $item->getPax();
  6594.                         if (empty($pax) or $pax == "0") {
  6595.                             $pax = 1;
  6596.                         }
  6597.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6598.                         if ($days > 1){
  6599.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6600.                         } else {
  6601.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6602.                         }
  6603.                         $unitsServ = $item->getUnits();
  6604.                         if (empty($unitsServ) or $unitsServ == "0") {
  6605.                             $unitsServ = 1;
  6606.                         }
  6607.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6608.                         $subnetoUnit = $subneto;
  6609.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6610.                         $data_supplier['service'][$i] = array (
  6611.                             'id' => $item->getId(),
  6612.                             'serviceCatId' => $item->getServiceCatId(),
  6613.                             'serviceName' => $item->getName(),
  6614.                             'serviceType' => 'DDR',
  6615.                             'date' => $dateServ,
  6616.                             'qty' => $unitsServ,
  6617.                             'iva' => $item->getIva(),
  6618.                             'pax' => $item->getPax(),
  6619.                             'precioUnit' => $subnetoUnit,
  6620.                             'subneto' => $subneto,
  6621.                             'subtotal' => $subtotal,
  6622.                         );
  6623.                         break;
  6624.                     default:
  6625.                         break;
  6626.                 }
  6627.             } else {
  6628.                 $subtotalService = $item->getPrice();
  6629.                 $subneto = $item->getPrice();
  6630.                 // Commission
  6631.                 if ($item->getOpCommission()=='1'){
  6632.                     $subtotalService = $subtotalService * (1 + ($item->getCommission()/100));
  6633.                     $subneto = $subneto  * (1 + ($item->getCommission()/100));
  6634.                 } else {
  6635.                     $subtotalService = $subtotalService * (1 - ($item->getCommission()/100));
  6636.                     $subneto = $subneto * (1 - ($item->getCommission()/100));
  6637.                 }
  6638.                 // Over
  6639.                 if ($item->getOpOver()=='1'){
  6640.                     $subtotalService = $subtotalService + $item->getOver();
  6641.                     $subneto = $subneto + $item->getOver();
  6642.                 } else {
  6643.                     $subtotalService = $subtotalService - $item->getOver();
  6644.                     $subneto = $subneto - $item->getOver();
  6645.                 }
  6646.                 // IVA
  6647.                 if ($item->getOpIva()=='1'){
  6648.                     $subtotalService = $subtotalService * (1 + ($item->getIva()/100));
  6649.                 } else {
  6650.                     $subtotalService = $item->getPrice();
  6651.                     $subneto = ($subneto * 100) / (100 + $item->getIva());
  6652.                 }
  6653.                 switch ($item->getServiceCatId()){
  6654.                     case 1: // Alojamiento
  6655.                         // el numero de noches $numNoches; precio unitario $subneto
  6656.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  6657.                         // La personas no afectan este calculo
  6658.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  6659.                         $subnetoUnit = $subneto;
  6660.                         $subneto = $subneto * $numNoches * $item->getUnits();
  6661.                         $data_supplier['service'][$i] = array (
  6662.                             'id' => $item->getId(),
  6663.                             'serviceCatId' => $item->getServiceCatId(),
  6664.                             'serviceName' => $item->getName(),
  6665.                             'serviceType' => 'Hotel',
  6666.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  6667.                             'qty' => $item->getUnits(),
  6668.                             'iva' => $item->getIva(),
  6669.                             'pax' => '-',
  6670.                             'precioUnit' => $subnetoUnit,
  6671.                             'subneto' => $subneto,
  6672.                             'subtotal' => $subtotal,
  6673.                         );
  6674.                         break;
  6675.                     case 2: //Actividades
  6676.                         // El número de personas es considerado en el calculo
  6677.                         $pax = $item->getPax();
  6678.                         if (empty($pax) or $pax == "0") {
  6679.                             $pax = 1;
  6680.                         }
  6681.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6682.                         if ($days > 1){
  6683.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6684.                         } else {
  6685.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6686.                         }
  6687.                         $subtotal = $subtotalService * $days * $item->getUnits();
  6688.                         $subnetoUnit = $subneto;
  6689.                         $subneto = $subneto * $days * $item->getUnits();
  6690.                         $data_supplier['service'][$i] = array (
  6691.                             'id' => $item->getId(),
  6692.                             'serviceCatId' => $item->getServiceCatId(),
  6693.                             'serviceName' => $item->getName(),
  6694.                             'serviceType' => 'Actividad',
  6695.                             'date' => $dateServ,
  6696.                             'qty' => $item->getUnits(),
  6697.                             'iva' => $item->getIva(),
  6698.                             'pax' => $item->getPax(),
  6699.                             'precioUnit' => $subnetoUnit,
  6700.                             'subneto' => $subneto,
  6701.                             'subtotal' => $subtotal,
  6702.                         );
  6703.                         break;
  6704.                     case 3: // AV
  6705.                         $pax = $item->getPax();
  6706.                         if (empty($pax) or $pax == "0") {
  6707.                             $pax = 1;
  6708.                         }
  6709.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6710.                         if ($days > 1){
  6711.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6712.                         } else {
  6713.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6714.                         }
  6715.                         $unitsServ = $item->getUnits();
  6716.                         if (empty($unitsServ) or $unitsServ == "0") {
  6717.                             $unitsServ = 1;
  6718.                         }
  6719.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6720.                         $subnetoUnit = $subneto;
  6721.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6722.                         $data_supplier['service'][$i] = array (
  6723.                             'id' => $item->getId(),
  6724.                             'serviceCatId' => $item->getServiceCatId(),
  6725.                             'serviceName' => $item->getName(),
  6726.                             'serviceType' => 'AV',
  6727.                             'date' => $dateServ,
  6728.                             'qty' => $unitsServ,
  6729.                             'iva' => $item->getIva(),
  6730.                             'pax' => $item->getPax(),
  6731.                             'precioUnit' => $subnetoUnit,
  6732.                             'subneto' => $subneto,
  6733.                             'subtotal' => $subtotal,
  6734.                         );
  6735.                         break;
  6736.                     case 4: //Creative
  6737.                         $pax = $item->getPax();
  6738.                         if (empty($pax) or $pax == "0") {
  6739.                             $pax = 1;
  6740.                         }
  6741.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6742.                         if ($days > 1){
  6743.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6744.                         } else {
  6745.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6746.                         }
  6747.                         $unitsServ = $item->getUnits();
  6748.                         if (empty($unitsServ) or $unitsServ == "0") {
  6749.                             $unitsServ = 1;
  6750.                         }
  6751.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6752.                         $subnetoUnit = $subneto;
  6753.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6754.                         $data_supplier['service'][$i] = array (
  6755.                             'id' => $item->getId(),
  6756.                             'serviceCatId' => $item->getServiceCatId(),
  6757.                             'serviceName' => $item->getName(),
  6758.                             'serviceType' => 'Creativo',
  6759.                             'date' => $dateServ,
  6760.                             'qty' => $unitsServ,
  6761.                             'iva' => $item->getIva(),
  6762.                             'pax' => $item->getPax(),
  6763.                             'precioUnit' => $subnetoUnit,
  6764.                             'subneto' => $subneto,
  6765.                             'subtotal' => $subtotal,
  6766.                         );
  6767.                         break;
  6768.                     case 5: //Cruise
  6769.                         $pax = $item->getPax();
  6770.                         if (empty($pax) or $pax == "0") {
  6771.                             $pax = 1;
  6772.                         }
  6773.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  6774.                         if ($days > 1){
  6775.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6776.                         } else {
  6777.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6778.                         }
  6779.                         $unitsServ = $item->getUnits();
  6780.                         if (empty($unitsServ) or $unitsServ == "0") {
  6781.                             $unitsServ = 1;
  6782.                         }
  6783.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6784.                         $subnetoUnit = $subneto;
  6785.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6786.                         $data_supplier['service'][$i] = array (
  6787.                             'id' => $item->getId(),
  6788.                             'serviceCatId' => $item->getServiceCatId(),
  6789.                             'serviceName' => $item->getName(),
  6790.                             'serviceType' => 'Crucero',
  6791.                             'date' => $dateServ,
  6792.                             'qty' => $unitsServ,
  6793.                             'iva' => $item->getIva(),
  6794.                             'pax' => $item->getPax(),
  6795.                             'precioUnit' => $subnetoUnit,
  6796.                             'subneto' => $subneto,
  6797.                             'subtotal' => $subtotal,
  6798.                         );
  6799.                         break;
  6800.                     case 6: //Entertaiment
  6801.                         $pax = $item->getPax();
  6802.                         if (empty($pax) or $pax == "0") {
  6803.                             $pax = 1;
  6804.                         }
  6805.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6806.                         if ($days > 1){
  6807.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6808.                         } else {
  6809.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6810.                         }
  6811.                         $unitsServ = $item->getUnits();
  6812.                         if (empty($unitsServ) or $unitsServ == "0") {
  6813.                             $unitsServ = 1;
  6814.                         }
  6815.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6816.                         $subnetoUnit = $subneto;
  6817.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6818.                         $data_supplier['service'][$i] = array (
  6819.                             'id' => $item->getId(),
  6820.                             'serviceCatId' => $item->getServiceCatId(),
  6821.                             'serviceName' => $item->getName(),
  6822.                             'serviceType' => 'Entretenimiento',
  6823.                             'date' => $dateServ,
  6824.                             'qty' => $unitsServ,
  6825.                             'iva' => $item->getIva(),
  6826.                             'pax' => $item->getPax(),
  6827.                             'precioUnit' => $subnetoUnit,
  6828.                             'subneto' => $subneto,
  6829.                             'subtotal' => $subtotal,
  6830.                         );
  6831.                         break;
  6832.                     case 7: // Gifts
  6833.                         $pax = $item->getPax();
  6834.                         if (empty($pax) or $pax == "0") {
  6835.                             $pax = 1;
  6836.                         }
  6837.                         $days = 1;
  6838.                         if ($days > 1){
  6839.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6840.                         } else {
  6841.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6842.                         }
  6843.                         $unitsServ = $item->getUnits();
  6844.                         if (empty($unitsServ) or $unitsServ == "0") {
  6845.                             $unitsServ = 1;
  6846.                         }
  6847.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6848.                         $subnetoUnit = $subneto;
  6849.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6850.                         $data_supplier['service'][$i] = array (
  6851.                             'id' => $item->getId(),
  6852.                             'serviceCatId' => $item->getServiceCatId(),
  6853.                             'serviceName' => $item->getName(),
  6854.                             'serviceType' => 'Regalos',
  6855.                             'date' => $dateServ,
  6856.                             'qty' => $unitsServ,
  6857.                             'iva' => $item->getIva(),
  6858.                             'pax' => $item->getPax(),
  6859.                             'precioUnit' => $subnetoUnit,
  6860.                             'subneto' => $subneto,
  6861.                             'subtotal' => $subtotal,
  6862.                         );
  6863.                         break;
  6864.                     case 8: //Guide
  6865.                         $pax = $item->getPax();
  6866.                         if (empty($pax) or $pax == "0") {
  6867.                             $pax = 1;
  6868.                         }
  6869.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6870.                         if ($days > 1){
  6871.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6872.                         } else {
  6873.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6874.                         }
  6875.                         $unitsServ = $item->getUnits();
  6876.                         if (empty($unitsServ) or $unitsServ == "0") {
  6877.                             $unitsServ = 1;
  6878.                         }
  6879.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6880.                         $subnetoUnit = $subneto;
  6881.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6882.                         $data_supplier['service'][$i] = array (
  6883.                             'id' => $item->getId(),
  6884.                             'serviceCatId' => $item->getServiceCatId(),
  6885.                             'serviceName' => $item->getName(),
  6886.                             'serviceType' => 'Regalos',
  6887.                             'date' => $dateServ,
  6888.                             'qty' => $unitsServ,
  6889.                             'iva' => $item->getIva(),
  6890.                             'pax' => $item->getPax(),
  6891.                             'precioUnit' => $subnetoUnit,
  6892.                             'subneto' => $subneto,
  6893.                             'subtotal' => $subtotal,
  6894.                         );
  6895.                         break;
  6896.                     case 9: //Itineraries
  6897.                         $pax = $item->getPax();
  6898.                         if (empty($pax) or $pax == "0") {
  6899.                             $pax = 1;
  6900.                         }
  6901.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6902.                         if ($days > 1){
  6903.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6904.                         } else {
  6905.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6906.                         }
  6907.                         $unitsServ = $item->getUnits();
  6908.                         if (empty($unitsServ) or $unitsServ == "0") {
  6909.                             $unitsServ = 1;
  6910.                         }
  6911.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6912.                         $subnetoUnit = $subneto;
  6913.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6914.                         $data_supplier['service'][$i] = array (
  6915.                             'id' => $item->getId(),
  6916.                             'serviceCatId' => $item->getServiceCatId(),
  6917.                             'serviceName' => $item->getName(),
  6918.                             'serviceType' => 'Itinerarios',
  6919.                             'date' => $dateServ,
  6920.                             'qty' => $unitsServ,
  6921.                             'iva' => $item->getIva(),
  6922.                             'pax' => $item->getPax(),
  6923.                             'precioUnit' => $subnetoUnit,
  6924.                             'subneto' => $subneto,
  6925.                             'subtotal' => $subtotal,
  6926.                         );
  6927.                         break;
  6928.                     case 10: //Lounge  -- No Aplica
  6929. //                        $pax = $item->getPax();
  6930. //                        if (empty($pax) or $pax == "0") {
  6931. //                            $pax = 1;
  6932. //                        }
  6933. //
  6934. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6935. //                        if ($days > 1){
  6936. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6937. //                        } else {
  6938. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6939. //                        }
  6940. //
  6941. //                        $unitsServ = $item->getUnits();
  6942. //                        if (empty($unitsServ) or $unitsServ == "0") {
  6943. //                            $unitsServ = 1;
  6944. //                        }
  6945. //
  6946. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6947. //                        $subnetoUnit = $subneto;
  6948. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  6949. //
  6950. //                        $data_supplier['service'][$i] = array (
  6951. //                            'id' => $item->getId(),
  6952. //                            'serviceCatId' => $item->getServiceCatId(),
  6953. //                            'serviceName' => $item->getName(),
  6954. //                            'serviceType' => 'Itinerarios',
  6955. //                            'date' => $dateServ,
  6956. //                            'qty' => $unitsServ,
  6957. //                            'iva' => $item->getIva(),
  6958. //                            'pax' => $item->getPax(),
  6959. //                            'precioUnit' => $subnetoUnit,
  6960. //                            'subneto' => $subneto,
  6961. //                            'subtotal' => $subtotal,
  6962. //                        );
  6963.                         break;
  6964.                     case 11: //Menu
  6965.                         $pax = $item->getPax();
  6966.                         if (empty($pax) or $pax == "0") {
  6967.                             $pax = 1;
  6968.                         }
  6969.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  6970.                         if ($days > 1){
  6971.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  6972.                         } else {
  6973.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  6974.                         }
  6975.                         $unitsServ = $item->getUnits();
  6976.                         if (empty($unitsServ) or $unitsServ == "0") {
  6977.                             $unitsServ = 1;
  6978.                         }
  6979.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  6980.                         $subnetoUnit = $subneto;
  6981.                         $subneto = $subneto * $days * $unitsServ * $pax;
  6982.                         $data_supplier['service'][$i] = array (
  6983.                             'id' => $item->getId(),
  6984.                             'serviceCatId' => $item->getServiceCatId(),
  6985.                             'serviceName' => $item->getName(),
  6986.                             'serviceType' => 'Menú',
  6987.                             'date' => $dateServ,
  6988.                             'qty' => $unitsServ,
  6989.                             'iva' => $item->getIva(),
  6990.                             'pax' => $item->getPax(),
  6991.                             'precioUnit' => $subnetoUnit,
  6992.                             'subneto' => $subneto,
  6993.                             'subtotal' => $subtotal,
  6994.                         );
  6995.                         break;
  6996.                     case 12: //Others
  6997.                         $pax = $item->getPax();
  6998.                         if (empty($pax) or $pax == "0") {
  6999.                             $pax = 1;
  7000.                         }
  7001.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7002.                         if ($days > 1){
  7003.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7004.                         } else {
  7005.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7006.                         }
  7007.                         $unitsServ = $item->getUnits();
  7008.                         if (empty($unitsServ) or $unitsServ == "0") {
  7009.                             $unitsServ = 1;
  7010.                         }
  7011.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7012.                         $subnetoUnit = $subneto;
  7013.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7014.                         $data_supplier['service'][$i] = array (
  7015.                             'id' => $item->getId(),
  7016.                             'serviceCatId' => $item->getServiceCatId(),
  7017.                             'serviceName' => $item->getName(),
  7018.                             'serviceType' => 'Otros',
  7019.                             'date' => $dateServ,
  7020.                             'qty' => $unitsServ,
  7021.                             'iva' => $item->getIva(),
  7022.                             'pax' => $item->getPax(),
  7023.                             'precioUnit' => $subnetoUnit,
  7024.                             'subneto' => $subneto,
  7025.                             'subtotal' => $subtotal,
  7026.                         );
  7027.                         break;
  7028.                     case 13: //Transport
  7029.                         $pax = $item->getPax();
  7030.                         if (empty($pax) or $pax == "0") {
  7031.                             $pax = 1;
  7032.                         }
  7033.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7034.                         if ($days > 1){
  7035.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7036.                         } else {
  7037.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7038.                         }
  7039.                         $unitsServ = $item->getUnits();
  7040.                         if (empty($unitsServ) or $unitsServ == "0") {
  7041.                             $unitsServ = 1;
  7042.                         }
  7043.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7044.                         $subnetoUnit = $subneto;
  7045.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7046.                         $data_supplier['service'][$i] = array (
  7047.                             'id' => $item->getId(),
  7048.                             'serviceCatId' => $item->getServiceCatId(),
  7049.                             'serviceName' => $item->getName(),
  7050.                             'serviceType' => 'Transporte',
  7051.                             'date' => $dateServ,
  7052.                             'qty' => $unitsServ,
  7053.                             'iva' => $item->getIva(),
  7054.                             'pax' => $item->getPax(),
  7055.                             'precioUnit' => $subnetoUnit,
  7056.                             'subneto' => $subneto,
  7057.                             'subtotal' => $subtotal,
  7058.                         );
  7059.                         break;
  7060.                     case 14: //Technology
  7061.                         $pax = $item->getPax();
  7062.                         if (empty($pax) or $pax == "0") {
  7063.                             $pax = 1;
  7064.                         }
  7065.                         $days = 1;
  7066.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7067.                         $unitsServ = $item->getUnits();
  7068.                         if (empty($unitsServ) or $unitsServ == "0") {
  7069.                             $unitsServ = 1;
  7070.                         }
  7071.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7072.                         $subnetoUnit = $subneto;
  7073.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7074.                         $data_supplier['service'][$i] = array (
  7075.                             'id' => $item->getId(),
  7076.                             'serviceCatId' => $item->getServiceCatId(),
  7077.                             'serviceName' => $item->getName(),
  7078.                             'serviceType' => 'Tecnología',
  7079.                             'date' => $dateServ,
  7080.                             'qty' => $unitsServ,
  7081.                             'iva' => $item->getIva(),
  7082.                             'pax' => $item->getPax(),
  7083.                             'precioUnit' => $subnetoUnit,
  7084.                             'subneto' => $subneto,
  7085.                             'subtotal' => $subtotal,
  7086.                         );
  7087.                         break;
  7088.                     case 15: //Assisstant
  7089.                         $pax = $item->getPax();
  7090.                         if (empty($pax) or $pax == "0") {
  7091.                             $pax = 1;
  7092.                         }
  7093.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7094.                         if ($days > 1){
  7095.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7096.                         } else {
  7097.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7098.                         }
  7099.                         $unitsServ = $item->getUnits();
  7100.                         if (empty($unitsServ) or $unitsServ == "0") {
  7101.                             $unitsServ = 1;
  7102.                         }
  7103.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7104.                         $subnetoUnit = $subneto;
  7105.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7106.                         $data_supplier['service'][$i] = array (
  7107.                             'id' => $item->getId(),
  7108.                             'serviceCatId' => $item->getServiceCatId(),
  7109.                             'serviceName' => $item->getName(),
  7110.                             'serviceType' => 'Asistente',
  7111.                             'date' => $dateServ,
  7112.                             'qty' => $unitsServ,
  7113.                             'iva' => $item->getIva(),
  7114.                             'pax' => $item->getPax(),
  7115.                             'precioUnit' => $subnetoUnit,
  7116.                             'subneto' => $subneto,
  7117.                             'subtotal' => $subtotal,
  7118.                         );
  7119.                         break;
  7120.                     case 16: //DDR
  7121.                         $pax = $item->getPax();
  7122.                         if (empty($pax) or $pax == "0") {
  7123.                             $pax = 1;
  7124.                         }
  7125.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7126.                         if ($days > 1){
  7127.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7128.                         } else {
  7129.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7130.                         }
  7131.                         $unitsServ = $item->getUnits();
  7132.                         if (empty($unitsServ) or $unitsServ == "0") {
  7133.                             $unitsServ = 1;
  7134.                         }
  7135.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7136.                         $subnetoUnit = $subneto;
  7137.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7138.                         $data_supplier['service'][$i] = array (
  7139.                             'id' => $item->getId(),
  7140.                             'serviceCatId' => $item->getServiceCatId(),
  7141.                             'serviceName' => $item->getName(),
  7142.                             'serviceType' => 'DDR',
  7143.                             'date' => $dateServ,
  7144.                             'qty' => $unitsServ,
  7145.                             'iva' => $item->getIva(),
  7146.                             'pax' => $item->getPax(),
  7147.                             'precioUnit' => $subnetoUnit,
  7148.                             'subneto' => $subneto,
  7149.                             'subtotal' => $subtotal,
  7150.                         );
  7151.                         break;
  7152.                     default:
  7153.                         break;
  7154.                 }
  7155.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  7156.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  7157.                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  7158.             }
  7159.             switch ($item->getIva()){
  7160.                 // Acumula IVA
  7161.                 case 21:
  7162.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva()/100));
  7163.                     break;
  7164.                 case 10:
  7165.                     $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva()/100));
  7166.                     break;
  7167.                 case 0:
  7168.                     break;
  7169.                 default:
  7170.                     break;
  7171.             }
  7172.             $totales_neto_all = $totales_neto_all + $neto;
  7173.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7174.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  7175.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  7176.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  7177.             // Acumula netos totales e IVA
  7178.             $service['neto'] = $service['neto'] + $neto;
  7179.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  7180.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7181.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  7182.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  7183.             $i++;
  7184.         }
  7185.         $data_supplier['serviceSubTotal'] = array(
  7186.             'neto' => $service['neto'],
  7187.             'sumSubT' => $service['sumSubT'],
  7188.         );
  7189.         $currency = '€';
  7190.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  7191.         if (!empty($payments)) {
  7192.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  7193.         } else {
  7194.             $amount_pay = 0;
  7195.         }
  7196.         $totales_all = $totales_total - $amount_pay;
  7197.         $data = array(
  7198.             'id' => $id,
  7199.             'type' => $type,
  7200.             'number' => $number,
  7201.             'prefix' => $prefix,
  7202.             'date' => $date,
  7203.             'file' => $file,
  7204.             'token' =>'', // $file->getAccessKey(),
  7205.             'company' => $company,
  7206.             'clients' => $client,
  7207.             'datasupplier' => $data_supplier,
  7208.             'currency' => $currency,
  7209.             'totales_neto' => $totales_neto_all,
  7210.             'bases_imponibles' => $data_iva,
  7211.             'totales' => $totales_total,
  7212.             'balance' => $totales_all,
  7213.             'paymentInvoice' => $amount_pay,
  7214.         );
  7215.         return $data;
  7216.     }
  7217.     private function baseInvoiceSelectedFile($id, $arrayBools, $number)
  7218.     {
  7219.         $em = $this->getDoctrine()->getManager();
  7220.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  7221.         $items = $em->getRepository(MdvDocProformaItems::class)->findByFileId($file->getId());
  7222.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  7223.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  7224.         if (empty($client)){
  7225.             $client[0] = new Client();
  7226.             $client[0]->setName('');
  7227.             $client[0]->setTitle('');
  7228.             $client[0]->setIdDocument('');
  7229.             $client[0]->setPopulation('');
  7230.             $client[0]->setRegion('');
  7231.             $client[0]->setCountry('');
  7232.         } else {
  7233.             if (is_numeric($client[0]->getPopulation())){
  7234.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  7235.                 $client[0]->setPopulation($city->getCity());
  7236.             }
  7237.             if (is_numeric($client[0]->getRegion())){
  7238.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  7239.                 $client[0]->setRegion($region->getRegion());
  7240.             }
  7241.             if (is_numeric($client[0]->getCountry())){
  7242.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  7243.                 $client[0]->setCountry($country->getCountry());
  7244.             }
  7245.         }
  7246.         // Acumuladores de los calculos
  7247.         $totales_neto_all = 0;
  7248.         $data_iva = array(
  7249.             'iva' => 21,
  7250.             'ivaMontoVeintiUno' => 0,
  7251.             'ivaMontoDiez' => 0,
  7252.             'ivaMontoCero' => 0,
  7253.         );
  7254.         //INICIO: Determinamos el numero de factura o proforma
  7255.         if (substr($number,0,1) == 'I'){
  7256.             // Es una Factura
  7257.             $numberInvoiceOrProforma = substr($number,1);
  7258.             $number = $numberInvoiceOrProforma;
  7259.             $number = $em->getRepository(MdvDocInvoice::class)->findOneById($number);
  7260.             $number = $number->getNumber();
  7261.             $type = 'Invoice';
  7262.         } else {
  7263.             // Es una Proforma
  7264.             $numberInvoiceOrProforma = substr($number,1);
  7265.             $number = $numberInvoiceOrProforma;
  7266.             $number = $em->getRepository(MdvDocProforma::class)->findOneById($number);
  7267.             $number = $number->getPrefix().$number->getFileId();
  7268.             $type = 'Proforma';
  7269.         }
  7270.         //FIN: Determinamos el numero de factura o proforma
  7271.         // Buscamos las salas reservadas, pagos y servicios para el evento
  7272.         $arrayItems = array();
  7273.         $fileProducts = array();
  7274.         if (!empty($arrayBools['product'])) {
  7275.             foreach ($arrayBools['product'] as $key => $item) {
  7276.                 if ($item == 'true') {
  7277.                     $fileProducts[] = $em->getRepository(MdvProductFile::class)->findOneById($key);
  7278.                 }
  7279.             }
  7280.         }
  7281.         $payments = array();
  7282.         if (!empty($arrayBools['payment'])) {
  7283.             foreach ($arrayBools['payment'] as $key => $item) {
  7284.                 if ($item == 'true') {
  7285.                     $payments[] = $em->getRepository(MdvPaymentsClient::class)->findOneById($key);
  7286.                 }
  7287.             }
  7288.         }
  7289.         $services = array();
  7290.         if (!empty($arrayBools['service'])) {
  7291.             foreach ($arrayBools['service'] as $key => $item) {
  7292.                 if ($item == 'true') {
  7293.                     $services[] = $em->getRepository(MdvServices::class)->findOneById($key);
  7294.                 }
  7295.             }
  7296.         }
  7297.         $data_supplier = array();
  7298.         $i = 0;
  7299.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  7300.         $pax = '1';
  7301.         $qty = '1';
  7302.         $product = array(
  7303.             'neto' => 0,
  7304.             'sumSubT' => 0,
  7305.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  7306.         $service = array(
  7307.             'neto' => 0,
  7308.             'sumSubT' => 0,
  7309.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  7310.         foreach ($fileProducts as $item){
  7311.             $factorIva = empty($item->getIva()) ? 21 : $item->getIva();
  7312.             $factorIva = $factorIva / 100;
  7313.             if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  7314.                 $subtotal = 0;
  7315.                 $neto = 0;
  7316.                 $subtotalProduct = 0;
  7317.             } else {
  7318.                 $subtotalProduct = $item->getSubTotalPrice() * (1 + $factorIva);
  7319.                 $subneto = $item->getSubTotalPrice();
  7320.                 $subtotal =  $subtotalProduct;
  7321.                 $neto =  $subneto;
  7322.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  7323.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  7324.                 $neto = round($neto,2,PHP_ROUND_HALF_UP);
  7325.                 $arrayItems[] = array(
  7326.                     'type' => 'Product',
  7327.                     'name' => $item->getProductName(),
  7328.                     'amount' => $neto,
  7329.                     'iva' => ($factorIva * 100),
  7330.                     'total' => $subtotal,
  7331.                 );
  7332.             }
  7333.             // Acumula netos totales e IVA
  7334.             $totales_neto_all = $totales_neto_all + $neto;
  7335. //            $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21);
  7336.             switch ($factorIva){
  7337.                 case 0.21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * $factorIva); break;
  7338.                 case 0: $data_iva['ivaMontoCero'] = 0; break;
  7339.             }
  7340.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7341.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  7342.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  7343.             // Acumula netos totales e IVA
  7344.             $product['neto'] = $product['neto'] + $neto;
  7345.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  7346.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7347.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  7348.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  7349.             $data_supplier['product'][$i] = array (
  7350.                 'id' => $item->getId(),
  7351.                 'productName' => $item->getProductName(),
  7352.                 'productId' => $item->getProductId(),
  7353.                 'dateStart' => $item->getDateStart(),
  7354.                 'dateEnd' => $item->getDateEnd(),
  7355.                 'servicePrice' => $item->getServicePrice(),
  7356.                 'subtotalProduct' => $subtotalProduct,
  7357.                 'iva' => ($factorIva * 100),
  7358.                 'pax' => $pax,
  7359.                 'qty' => $qty,
  7360.                 'type' => $item->getType(),
  7361.                 'subtotal' => $subtotal,
  7362.             );
  7363.             $i++;
  7364.         }
  7365.         $data_supplier['productSubTotal'] = array(
  7366.             'neto' => $product['neto'],
  7367.             'sumSubT' => $product['sumSubT'],
  7368.         );
  7369.         $payment = array(
  7370.             'sumSubT' => 0,
  7371.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  7372.         $i = 0;
  7373.         foreach ($payments as $item){
  7374.             $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  7375.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7376.             $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  7377.             $data_supplier['payment'][$i] = array (
  7378.                 'id' => $item->getId(),
  7379.                 'amount' => $item->getAmount(),
  7380.                 'amountTotal' => $item->getAmountTotal(),
  7381.                 'vat' => $item->getVat(),
  7382.                 'datePayAt' => $item->getDatePayAt(),
  7383.                 'wayToPay' => $item->getWayToPay(),
  7384.             );
  7385.             $arrayItems[] = array(
  7386.                 'type' => 'Payment',
  7387.                 'name' => $item->getWayToPay(),
  7388.                 'amount' => $item->getAmount(),
  7389.                 'total' => $item->getAmountTotal(),
  7390.                 'iva' => $item->getVat(),
  7391. //                    'iva' => '',
  7392. //                    'total' => $item->getAmount(),
  7393.             );
  7394.             $i++;
  7395.         }
  7396.         if (!empty($payments)) {
  7397.             $data_supplier['paymentSubTotal'] = array(
  7398.                 'sumSubT' => $payment['sumSubT'],
  7399.             );
  7400.         }
  7401.         foreach ($services as $item){
  7402.             if (is_null($item->getPrice()) or empty($item->getPrice())){
  7403.                 $subtotal = 0;
  7404.                 $neto = 0;
  7405.                 $subtotalService = 0;
  7406.             } else {
  7407.                 $subtotalService = $item->getPrice();
  7408.                 $subneto = $item->getPrice();
  7409.                 // Commission
  7410.                 if ($item->getOpCommission()=='1'){
  7411.                     $subtotalService = $subtotalService * (1 + ($item->getCommission()/100));
  7412.                     $subneto = $subneto  * (1 + ($item->getCommission()/100));
  7413.                 } else {
  7414.                     $subtotalService = $subtotalService * (1 - ($item->getCommission()/100));
  7415.                     $subneto = $subneto * (1 - ($item->getCommission()/100));
  7416.                 }
  7417.                 // Over
  7418.                 if ($item->getOpOver()=='1'){
  7419.                     $subtotalService = $subtotalService + $item->getOver();
  7420.                     $subneto = $subneto + $item->getOver();
  7421.                 } else {
  7422.                     $subtotalService = $subtotalService - $item->getOver();
  7423.                     $subneto = $subneto - $item->getOver();
  7424.                 }
  7425.                 // IVA
  7426.                 if ($item->getOpIva()=='1'){
  7427.                     $subtotalService = $subtotalService * (1 + ($item->getIva()/100));
  7428.                 } else {
  7429.                     $subtotalService = $item->getPrice();
  7430.                     $subneto = ($subneto * 100) / (100 + $item->getIva());
  7431.                 }
  7432.                 switch ($item->getServiceCatId()){
  7433.                     case 1: // Alojamiento
  7434.                         // el numero de noches $numNoches; precio unitario $subneto
  7435.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  7436.                         // La personas no afectan este calculo
  7437.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  7438.                         $subnetoUnit = $subneto;
  7439.                         $subneto = $subneto * $numNoches * $item->getUnits();
  7440.                         $data_supplier['service'][$i] = array (
  7441.                             'id' => $item->getId(),
  7442.                             'serviceCatId' => $item->getServiceCatId(),
  7443.                             'serviceName' => $item->getName(),
  7444.                             'serviceType' => 'Hotel',
  7445.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  7446.                             'qty' => $item->getUnits(),
  7447.                             'iva' => $item->getIva(),
  7448.                             'pax' => '-',
  7449.                             'precioUnit' => $subnetoUnit,
  7450.                             'subneto' => $subneto,
  7451.                             'subtotal' => $subtotal,
  7452.                         );
  7453.                         break;
  7454.                     case 2: //Actividades
  7455.                         // El número de personas es considerado en el calculo
  7456.                         $pax = $item->getPax();
  7457.                         if (empty($pax) or $pax == "0") {
  7458.                             $pax = 1;
  7459.                         }
  7460.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7461.                         if ($days > 1){
  7462.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7463.                         } else {
  7464.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7465.                         }
  7466.                         $subtotal = $subtotalService * $days * $item->getUnits();
  7467.                         $subnetoUnit = $subneto;
  7468.                         $subneto = $subneto * $days * $item->getUnits();
  7469.                         $data_supplier['service'][$i] = array (
  7470.                             'id' => $item->getId(),
  7471.                             'serviceCatId' => $item->getServiceCatId(),
  7472.                             'serviceName' => $item->getName(),
  7473.                             'serviceType' => 'Actividad',
  7474.                             'date' => $dateServ,
  7475.                             'qty' => $item->getUnits(),
  7476.                             'iva' => $item->getIva(),
  7477.                             'pax' => $item->getPax(),
  7478.                             'precioUnit' => $subnetoUnit,
  7479.                             'subneto' => $subneto,
  7480.                             'subtotal' => $subtotal,
  7481.                         );
  7482.                         break;
  7483.                     case 3: // AV
  7484.                         $pax = $item->getPax();
  7485.                         if (empty($pax) or $pax == "0") {
  7486.                             $pax = 1;
  7487.                         }
  7488.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7489.                         if ($days > 1){
  7490.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7491.                         } else {
  7492.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7493.                         }
  7494.                         $unitsServ = $item->getUnits();
  7495.                         if (empty($unitsServ) or $unitsServ == "0") {
  7496.                             $unitsServ = 1;
  7497.                         }
  7498.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7499.                         $subnetoUnit = $subneto;
  7500.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7501.                         $data_supplier['service'][$i] = array (
  7502.                             'id' => $item->getId(),
  7503.                             'serviceCatId' => $item->getServiceCatId(),
  7504.                             'serviceName' => $item->getName(),
  7505.                             'serviceType' => 'AV',
  7506.                             'date' => $dateServ,
  7507.                             'qty' => $unitsServ,
  7508.                             'iva' => $item->getIva(),
  7509.                             'pax' => $item->getPax(),
  7510.                             'precioUnit' => $subnetoUnit,
  7511.                             'subneto' => $subneto,
  7512.                             'subtotal' => $subtotal,
  7513.                         );
  7514.                         break;
  7515.                     case 4: //Creative
  7516.                         $pax = $item->getPax();
  7517.                         if (empty($pax) or $pax == "0") {
  7518.                             $pax = 1;
  7519.                         }
  7520.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7521.                         if ($days > 1){
  7522.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7523.                         } else {
  7524.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7525.                         }
  7526.                         $unitsServ = $item->getUnits();
  7527.                         if (empty($unitsServ) or $unitsServ == "0") {
  7528.                             $unitsServ = 1;
  7529.                         }
  7530.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7531.                         $subnetoUnit = $subneto;
  7532.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7533.                         $data_supplier['service'][$i] = array (
  7534.                             'id' => $item->getId(),
  7535.                             'serviceCatId' => $item->getServiceCatId(),
  7536.                             'serviceName' => $item->getName(),
  7537.                             'serviceType' => 'Creativo',
  7538.                             'date' => $dateServ,
  7539.                             'qty' => $unitsServ,
  7540.                             'iva' => $item->getIva(),
  7541.                             'pax' => $item->getPax(),
  7542.                             'precioUnit' => $subnetoUnit,
  7543.                             'subneto' => $subneto,
  7544.                             'subtotal' => $subtotal,
  7545.                         );
  7546.                         break;
  7547.                     case 5: //Cruise
  7548.                         $pax = $item->getPax();
  7549.                         if (empty($pax) or $pax == "0") {
  7550.                             $pax = 1;
  7551.                         }
  7552.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  7553.                         if ($days > 1){
  7554.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7555.                         } else {
  7556.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7557.                         }
  7558.                         $unitsServ = $item->getUnits();
  7559.                         if (empty($unitsServ) or $unitsServ == "0") {
  7560.                             $unitsServ = 1;
  7561.                         }
  7562.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7563.                         $subnetoUnit = $subneto;
  7564.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7565.                         $data_supplier['service'][$i] = array (
  7566.                             'id' => $item->getId(),
  7567.                             'serviceCatId' => $item->getServiceCatId(),
  7568.                             'serviceName' => $item->getName(),
  7569.                             'serviceType' => 'Crucero',
  7570.                             'date' => $dateServ,
  7571.                             'qty' => $unitsServ,
  7572.                             'iva' => $item->getIva(),
  7573.                             'pax' => $item->getPax(),
  7574.                             'precioUnit' => $subnetoUnit,
  7575.                             'subneto' => $subneto,
  7576.                             'subtotal' => $subtotal,
  7577.                         );
  7578.                         break;
  7579.                     case 6: //Entertaiment
  7580.                         $pax = $item->getPax();
  7581.                         if (empty($pax) or $pax == "0") {
  7582.                             $pax = 1;
  7583.                         }
  7584.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7585.                         if ($days > 1){
  7586.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7587.                         } else {
  7588.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7589.                         }
  7590.                         $unitsServ = $item->getUnits();
  7591.                         if (empty($unitsServ) or $unitsServ == "0") {
  7592.                             $unitsServ = 1;
  7593.                         }
  7594.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7595.                         $subnetoUnit = $subneto;
  7596.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7597.                         $data_supplier['service'][$i] = array (
  7598.                             'id' => $item->getId(),
  7599.                             'serviceCatId' => $item->getServiceCatId(),
  7600.                             'serviceName' => $item->getName(),
  7601.                             'serviceType' => 'Entretenimiento',
  7602.                             'date' => $dateServ,
  7603.                             'qty' => $unitsServ,
  7604.                             'iva' => $item->getIva(),
  7605.                             'pax' => $item->getPax(),
  7606.                             'precioUnit' => $subnetoUnit,
  7607.                             'subneto' => $subneto,
  7608.                             'subtotal' => $subtotal,
  7609.                         );
  7610.                         break;
  7611.                     case 7: // Gifts
  7612.                         $pax = $item->getPax();
  7613.                         if (empty($pax) or $pax == "0") {
  7614.                             $pax = 1;
  7615.                         }
  7616. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7617.                         $days = 1;
  7618.                         if ($days > 1){
  7619.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7620.                         } else {
  7621.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7622.                         }
  7623.                         $unitsServ = $item->getUnits();
  7624.                         if (empty($unitsServ) or $unitsServ == "0") {
  7625.                             $unitsServ = 1;
  7626.                         }
  7627.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7628.                         $subnetoUnit = $subneto;
  7629.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7630.                         $data_supplier['service'][$i] = array (
  7631.                             'id' => $item->getId(),
  7632.                             'serviceCatId' => $item->getServiceCatId(),
  7633.                             'serviceName' => $item->getName(),
  7634.                             'serviceType' => 'Regalos',
  7635.                             'date' => $dateServ,
  7636.                             'qty' => $unitsServ,
  7637.                             'iva' => $item->getIva(),
  7638.                             'pax' => $item->getPax(),
  7639.                             'precioUnit' => $subnetoUnit,
  7640.                             'subneto' => $subneto,
  7641.                             'subtotal' => $subtotal,
  7642.                         );
  7643.                         break;
  7644.                     case 8: //Guide
  7645.                         $pax = $item->getPax();
  7646.                         if (empty($pax) or $pax == "0") {
  7647.                             $pax = 1;
  7648.                         }
  7649.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7650.                         if ($days > 1){
  7651.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7652.                         } else {
  7653.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7654.                         }
  7655.                         $unitsServ = $item->getUnits();
  7656.                         if (empty($unitsServ) or $unitsServ == "0") {
  7657.                             $unitsServ = 1;
  7658.                         }
  7659.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7660.                         $subnetoUnit = $subneto;
  7661.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7662.                         $data_supplier['service'][$i] = array (
  7663.                             'id' => $item->getId(),
  7664.                             'serviceCatId' => $item->getServiceCatId(),
  7665.                             'serviceName' => $item->getName(),
  7666.                             'serviceType' => 'Regalos',
  7667.                             'date' => $dateServ,
  7668.                             'qty' => $unitsServ,
  7669.                             'iva' => $item->getIva(),
  7670.                             'pax' => $item->getPax(),
  7671.                             'precioUnit' => $subnetoUnit,
  7672.                             'subneto' => $subneto,
  7673.                             'subtotal' => $subtotal,
  7674.                         );
  7675.                         break;
  7676.                     case 9: //Itineraries
  7677.                         $pax = $item->getPax();
  7678.                         if (empty($pax) or $pax == "0") {
  7679.                             $pax = 1;
  7680.                         }
  7681.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7682.                         if ($days > 1){
  7683.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7684.                         } else {
  7685.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7686.                         }
  7687.                         $unitsServ = $item->getUnits();
  7688.                         if (empty($unitsServ) or $unitsServ == "0") {
  7689.                             $unitsServ = 1;
  7690.                         }
  7691.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7692.                         $subnetoUnit = $subneto;
  7693.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7694.                         $data_supplier['service'][$i] = array (
  7695.                             'id' => $item->getId(),
  7696.                             'serviceCatId' => $item->getServiceCatId(),
  7697.                             'serviceName' => $item->getName(),
  7698.                             'serviceType' => 'Itinerarios',
  7699.                             'date' => $dateServ,
  7700.                             'qty' => $unitsServ,
  7701.                             'iva' => $item->getIva(),
  7702.                             'pax' => $item->getPax(),
  7703.                             'precioUnit' => $subnetoUnit,
  7704.                             'subneto' => $subneto,
  7705.                             'subtotal' => $subtotal,
  7706.                         );
  7707.                         break;
  7708.                     case 10: //Lounge  -- No Aplica
  7709. //                        $pax = $item->getPax();
  7710. //                        if (empty($pax) or $pax == "0") {
  7711. //                            $pax = 1;
  7712. //                        }
  7713. //
  7714. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7715. //                        if ($days > 1){
  7716. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7717. //                        } else {
  7718. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7719. //                        }
  7720. //
  7721. //                        $unitsServ = $item->getUnits();
  7722. //                        if (empty($unitsServ) or $unitsServ == "0") {
  7723. //                            $unitsServ = 1;
  7724. //                        }
  7725. //
  7726. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7727. //                        $subnetoUnit = $subneto;
  7728. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  7729. //
  7730. //                        $data_supplier['service'][$i] = array (
  7731. //                            'id' => $item->getId(),
  7732. //                            'serviceCatId' => $item->getServiceCatId(),
  7733. //                            'serviceName' => $item->getName(),
  7734. //                            'serviceType' => 'Itinerarios',
  7735. //                            'date' => $dateServ,
  7736. //                            'qty' => $unitsServ,
  7737. //                            'iva' => $item->getIva(),
  7738. //                            'pax' => $item->getPax(),
  7739. //                            'precioUnit' => $subnetoUnit,
  7740. //                            'subneto' => $subneto,
  7741. //                            'subtotal' => $subtotal,
  7742. //                        );
  7743.                         break;
  7744.                     case 11: //Menu
  7745.                         $pax = $item->getPax();
  7746.                         if (empty($pax) or $pax == "0") {
  7747.                             $pax = 1;
  7748.                         }
  7749.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7750.                         if ($days > 1){
  7751.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7752.                         } else {
  7753.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7754.                         }
  7755.                         $unitsServ = $item->getUnits();
  7756.                         if (empty($unitsServ) or $unitsServ == "0") {
  7757.                             $unitsServ = 1;
  7758.                         }
  7759.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7760.                         $subnetoUnit = $subneto;
  7761.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7762.                         $data_supplier['service'][$i] = array (
  7763.                             'id' => $item->getId(),
  7764.                             'serviceCatId' => $item->getServiceCatId(),
  7765.                             'serviceName' => $item->getName(),
  7766.                             'serviceType' => 'Menú',
  7767.                             'date' => $dateServ,
  7768.                             'qty' => $unitsServ,
  7769.                             'iva' => $item->getIva(),
  7770.                             'pax' => $item->getPax(),
  7771.                             'precioUnit' => $subnetoUnit,
  7772.                             'subneto' => $subneto,
  7773.                             'subtotal' => $subtotal,
  7774.                         );
  7775.                         break;
  7776.                     case 12: //Others
  7777.                         $pax = $item->getPax();
  7778.                         if (empty($pax) or $pax == "0") {
  7779.                             $pax = 1;
  7780.                         }
  7781.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7782.                         if ($days > 1){
  7783.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7784.                         } else {
  7785.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7786.                         }
  7787.                         $unitsServ = $item->getUnits();
  7788.                         if (empty($unitsServ) or $unitsServ == "0") {
  7789.                             $unitsServ = 1;
  7790.                         }
  7791.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7792.                         $subnetoUnit = $subneto;
  7793.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7794.                         $data_supplier['service'][$i] = array (
  7795.                             'id' => $item->getId(),
  7796.                             'serviceCatId' => $item->getServiceCatId(),
  7797.                             'serviceName' => $item->getName(),
  7798.                             'serviceType' => 'Otros',
  7799.                             'date' => $dateServ,
  7800.                             'qty' => $unitsServ,
  7801.                             'iva' => $item->getIva(),
  7802.                             'pax' => $item->getPax(),
  7803.                             'precioUnit' => $subnetoUnit,
  7804.                             'subneto' => $subneto,
  7805.                             'subtotal' => $subtotal,
  7806.                         );
  7807.                         break;
  7808.                     case 13: //Transport
  7809.                         $pax = $item->getPax();
  7810.                         if (empty($pax) or $pax == "0") {
  7811.                             $pax = 1;
  7812.                         }
  7813.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7814.                         if ($days > 1){
  7815.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7816.                         } else {
  7817.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7818.                         }
  7819.                         $unitsServ = $item->getUnits();
  7820.                         if (empty($unitsServ) or $unitsServ == "0") {
  7821.                             $unitsServ = 1;
  7822.                         }
  7823.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7824.                         $subnetoUnit = $subneto;
  7825.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7826.                         $data_supplier['service'][$i] = array (
  7827.                             'id' => $item->getId(),
  7828.                             'serviceCatId' => $item->getServiceCatId(),
  7829.                             'serviceName' => $item->getName(),
  7830.                             'serviceType' => 'Transporte',
  7831.                             'date' => $dateServ,
  7832.                             'qty' => $unitsServ,
  7833.                             'iva' => $item->getIva(),
  7834.                             'pax' => $item->getPax(),
  7835.                             'precioUnit' => $subnetoUnit,
  7836.                             'subneto' => $subneto,
  7837.                             'subtotal' => $subtotal,
  7838.                         );
  7839.                         break;
  7840.                     case 14: //Technology
  7841.                         $pax = $item->getPax();
  7842.                         if (empty($pax) or $pax == "0") {
  7843.                             $pax = 1;
  7844.                         }
  7845. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7846.                         $days = 1;
  7847. //                        if ($days > 1){
  7848. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7849. //                        } else {
  7850. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7851. //                        }
  7852.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7853.                         $unitsServ = $item->getUnits();
  7854.                         if (empty($unitsServ) or $unitsServ == "0") {
  7855.                             $unitsServ = 1;
  7856.                         }
  7857.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7858.                         $subnetoUnit = $subneto;
  7859.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7860.                         $data_supplier['service'][$i] = array (
  7861.                             'id' => $item->getId(),
  7862.                             'serviceCatId' => $item->getServiceCatId(),
  7863.                             'serviceName' => $item->getName(),
  7864.                             'serviceType' => 'Tecnología',
  7865.                             'date' => $dateServ,
  7866.                             'qty' => $unitsServ,
  7867.                             'iva' => $item->getIva(),
  7868.                             'pax' => $item->getPax(),
  7869.                             'precioUnit' => $subnetoUnit,
  7870.                             'subneto' => $subneto,
  7871.                             'subtotal' => $subtotal,
  7872.                         );
  7873.                         break;
  7874.                     case 15: //Assisstant
  7875.                         $pax = $item->getPax();
  7876.                         if (empty($pax) or $pax == "0") {
  7877.                             $pax = 1;
  7878.                         }
  7879.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7880.                         if ($days > 1){
  7881.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7882.                         } else {
  7883.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7884.                         }
  7885.                         $unitsServ = $item->getUnits();
  7886.                         if (empty($unitsServ) or $unitsServ == "0") {
  7887.                             $unitsServ = 1;
  7888.                         }
  7889.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7890.                         $subnetoUnit = $subneto;
  7891.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7892.                         $data_supplier['service'][$i] = array (
  7893.                             'id' => $item->getId(),
  7894.                             'serviceCatId' => $item->getServiceCatId(),
  7895.                             'serviceName' => $item->getName(),
  7896.                             'serviceType' => 'Asistente',
  7897.                             'date' => $dateServ,
  7898.                             'qty' => $unitsServ,
  7899.                             'iva' => $item->getIva(),
  7900.                             'pax' => $item->getPax(),
  7901.                             'precioUnit' => $subnetoUnit,
  7902.                             'subneto' => $subneto,
  7903.                             'subtotal' => $subtotal,
  7904.                         );
  7905.                         break;
  7906.                     case 16: //DDR
  7907.                         $pax = $item->getPax();
  7908.                         if (empty($pax) or $pax == "0") {
  7909.                             $pax = 1;
  7910.                         }
  7911.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  7912.                         if ($days > 1){
  7913.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  7914.                         } else {
  7915.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  7916.                         }
  7917.                         $unitsServ = $item->getUnits();
  7918.                         if (empty($unitsServ) or $unitsServ == "0") {
  7919.                             $unitsServ = 1;
  7920.                         }
  7921.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  7922.                         $subnetoUnit = $subneto;
  7923.                         $subneto = $subneto * $days * $unitsServ * $pax;
  7924.                         $data_supplier['service'][$i] = array (
  7925.                             'id' => $item->getId(),
  7926.                             'serviceCatId' => $item->getServiceCatId(),
  7927.                             'serviceName' => $item->getName(),
  7928.                             'serviceType' => 'DDR',
  7929.                             'date' => $dateServ,
  7930.                             'qty' => $unitsServ,
  7931.                             'iva' => $item->getIva(),
  7932.                             'pax' => $item->getPax(),
  7933.                             'precioUnit' => $subnetoUnit,
  7934.                             'subneto' => $subneto,
  7935.                             'subtotal' => $subtotal,
  7936.                         );
  7937.                         break;
  7938.                     default:
  7939.                         break;
  7940.                 }
  7941.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  7942.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  7943.                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  7944.                 $arrayItems[] = array(
  7945.                     'type' => 'Service',
  7946.                     'name' => $item->getName(),
  7947.                     'amount' => $neto,
  7948.                     'iva' => $item->getIva(),
  7949.                     'total' => $subtotal,
  7950.                 );
  7951.             }
  7952.             switch ($item->getIva()){
  7953.                 // Acumula IVA
  7954.                 case 21:
  7955.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva()/100));
  7956.                     break;
  7957.                 case 10:
  7958.                     $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva()/100));
  7959.                     break;
  7960.                 case 0:
  7961.                     break;
  7962.                 default:
  7963.                     break;
  7964.             }
  7965.             $totales_neto_all = $totales_neto_all + $neto;
  7966.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7967.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  7968.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  7969.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  7970.             // Acumula netos totales e IVA
  7971.             $service['neto'] = $service['neto'] + $neto;
  7972.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  7973.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  7974.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  7975.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  7976.             $i++;
  7977.         }
  7978.         $data_supplier['serviceSubTotal'] = array(
  7979.             'neto' => $service['neto'],
  7980.             'sumSubT' => $service['sumSubT'],
  7981.         );
  7982.         $currency = '€';
  7983.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  7984.         if (!empty($payments)) {
  7985.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  7986.         } else {
  7987.             $amount_pay = 0;
  7988.         }
  7989.         $totales_all = $totales_total - $amount_pay;
  7990.         if ($type == 'Invoice'){
  7991.             $em->clear();   // sin este clear se producia error al guardar
  7992.             $newInvoice = $em->getRepository(MdvDocInvoice::class)->findOneById($numberInvoiceOrProforma);
  7993.             $newInvoice->setBalance($totales_all);
  7994.             $em->persist($newInvoice);
  7995.             $em->flush();
  7996.         }
  7997.         $data = array(
  7998.             'id' => $id,
  7999.             'type' => $type,
  8000.             'number' => $number,
  8001.             'prefix' => $number,
  8002.             'date' => new DateTime('now'),
  8003.             'file' => $file,
  8004.             'company' => $company,
  8005.             'clients' => $client,
  8006.             'arrayItems' => $arrayItems,
  8007.             'datasupplier' => $data_supplier,
  8008.             'currency' => $currency,
  8009.             'totales_neto' => $totales_neto_all,
  8010.             'bases_imponibles' => $data_iva,
  8011.             'totales' => $totales_total,
  8012.             'balance' => $totales_all,
  8013.             'paymentInvoice' => $amount_pay,
  8014.         );
  8015.         return $data;
  8016.     }
  8017.     private function baseInvoiceDoneDocument($fileId, $invoiceId, $typeBase)
  8018.     {
  8019.         $em = $this->getDoctrine()->getManager();
  8020.         $file = $em->getRepository(MdvFiles::class)->findOneById($fileId);
  8021.         $dateDocument = null;                                                                           //Fecha final del documento
  8022.         if (($typeBase == 'I') or ($typeBase == 'P')) {
  8023.             if ($typeBase == 'I') {
  8024.                 // Es una factura
  8025.                 $items = $em->getRepository(MdvDocInvoiceItems::class)->findBy(array('fileId' => $fileId, 'invoiceId' => $invoiceId));
  8026.             } else {
  8027.                 // Es una proforma
  8028.                 $items = $em->getRepository(MdvDocProformaItems::class)->findBy(array('fileId' => $fileId, 'proformaId' => $invoiceId));
  8029.             }
  8030.         } else {
  8031.             // No es factura ni proforma, es una rectificativa
  8032.             $items = $em->getRepository(MdvDocInvoiceRecItems::class)->findBy(array('fileId' => $fileId, 'invoiceRecId' => $invoiceId));
  8033.         }
  8034.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('2');
  8035.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  8036.         if (empty($client)){
  8037.             $client[0] = new Client();
  8038.             $client[0]->setName('');
  8039.             $client[0]->setTitle('');
  8040.             $client[0]->setIdDocument('');
  8041.             $client[0]->setPopulation('');
  8042.             $client[0]->setRegion('');
  8043.             $client[0]->setCountry('');
  8044.         } else {
  8045.             if (is_numeric($client[0]->getPopulation())){
  8046.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  8047.                 $client[0]->setPopulation($city->getCity());
  8048.             }
  8049.             if (is_numeric($client[0]->getRegion())){
  8050.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  8051.                 $client[0]->setRegion($region->getRegion());
  8052.             }
  8053.             if (is_numeric($client[0]->getCountry())){
  8054.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  8055.                 $client[0]->setCountry($country->getCountry());
  8056.             }
  8057.         }
  8058.         // Acumuladores de los calculos
  8059.         $totales_neto_all = 0;
  8060.         $data_iva = array(
  8061.             'iva' => 21,
  8062.             'ivaMontoVeintiUno' => 0,
  8063.             'ivaMontoDiez' => 0,
  8064.             'ivaMontoCero' => 0,
  8065.         );
  8066.         //INICIO: Determinamos el numero de factura o proforma
  8067.         if ($typeBase == 'I'){
  8068.             // Es una Factura
  8069. //            $numberInvoiceOrProforma = $invoiceId;
  8070. //            $number = $invoiceId;
  8071.             $number = $em->getRepository(MdvDocInvoice::class)->findOneById($invoiceId);
  8072.             $dateDocument = $number->getDateAt();
  8073.             $number = $number->getNumber();
  8074.             $type = 'Invoice';
  8075.         } else {
  8076.             if ($typeBase == 'R'){
  8077.                 // Es una factura rectificativa
  8078.                 $number = $em->getRepository(MdvDocInvoiceRec::class)->findOneById($invoiceId);
  8079.                 $dateDocument = $number->getDateAt();
  8080.                 $type = 'Invoice Rec';
  8081.             } else {
  8082.                 // Es una Proforma
  8083. //                $number = $em->getRepository(MdvDocProforma::class)->findOneById($invoiceId);
  8084. //                $number = $number->getNumber();
  8085.                 $number = $invoiceId;
  8086.                 $type = 'Proforma';
  8087.             }
  8088.         }
  8089.         //FIN: Determinamos el numero de factura o proforma
  8090.         // Buscamos las salas reservadas, pagos y servicios para el evento
  8091.         $arrayItems = array();
  8092.         $fileProducts = array();
  8093.         $payments = array();
  8094.         $services = array();
  8095.         if (!empty($items)) {
  8096.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  8097.                 foreach ($items as $item) {
  8098.                     switch ($item->getItemType()) {
  8099.                         case 'PRODUCT': // Salon
  8100.                             $fileProducts[] = $item;
  8101.                             break;
  8102.                         case 'PAYMENT': // Pago
  8103.                             $payments[] = $item;
  8104.                             break;
  8105.                         case 'SERVICE': // Servicio
  8106.                             $services[] = $item;
  8107.                             break;
  8108.                         default:
  8109.                             break;
  8110.                     }
  8111.                 }
  8112.             } else {
  8113.                 // Es una proforma
  8114.                 foreach ($items as $item) {
  8115.                     switch ($item->getType()) {
  8116.                         case 'PRODUCT': // Producto
  8117.                             $productPro = $em->getRepository(MdvProductFile::class)->findOneById($item->getControlId());
  8118.                             if (!empty($productPro)) {
  8119.                                 $fileProducts[] = $productPro;
  8120.                             }
  8121.                             break;
  8122.                         case 'PAYMENT': // Pago
  8123.                             $paymentPro = $em->getRepository(MdvPaymentsClient::class)->findOneById($item->getControlId());
  8124.                             if (!empty($paymentPro)) {
  8125.                                 $payments[] = $paymentPro;
  8126.                             }
  8127.                             break;
  8128.                         case 'SERVICE': // Servicio
  8129.                             $servicePro = $em->getRepository(MdvServices::class)->findOneById($item->getControlId());
  8130.                             if (!empty($servicePro)) {
  8131.                                 $services[] = $servicePro;
  8132.                             }
  8133.                             break;
  8134.                         default:
  8135.                             break;
  8136.                     }
  8137.                 }
  8138.             }
  8139.         }
  8140.         $data_supplier = array();
  8141.         $i = 0;
  8142.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  8143.         $pax = '1';
  8144.         $qty = '1';
  8145.         $product = array(
  8146.             'neto' => 0,
  8147.             'sumSubT' => 0,
  8148.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  8149.         $service = array(
  8150.             'neto' => 0,
  8151.             'sumSubT' => 0,
  8152.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  8153.         foreach ($fileProducts as $item){
  8154.             $days = ((($item->getDateEnd())->diff($item->getDateStart()))->days + 1);
  8155.             $subtotalProduct = 0;
  8156.             $subtotalLounge = 0;
  8157.             $factorIva = empty($item->getIva()) ? 21 : $item->getIva();
  8158.             $factorIva = $factorIva / 100;
  8159.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  8160.                 if (is_null($item->getSubTotalPrice()) or empty($item->getSubTotalPrice())){
  8161.                     $subtotal = 0;
  8162.                     $neto = 0;
  8163.                     $subnetoMounting = 0;
  8164.                     $subtotalLounge = 0;
  8165.                     $subnetoRemoval = 0;
  8166.                 } else {
  8167.                     $subtotalLounge = $item->getSubTotalPrice() * (1 + $factorIva);
  8168.                     $subneto = $item->getSubTotalPrice();
  8169.                     $subtotal =  $subtotalLounge;
  8170.                     $neto =  $subneto;
  8171.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  8172.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  8173.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  8174.                     $arrayItems[] = array(
  8175.                         'type' => 'Product',
  8176.                         'name' => $item->getLngLoungeName(),
  8177.                         'amount' => $item->getServicePrice(),
  8178.                         'amountSubTotal' => $neto,
  8179.                         'iva' => ($factorIva * 100),
  8180.                         'total' => $subtotal,
  8181.                     );
  8182.                 }
  8183.             } else {
  8184.                 // Es proforma
  8185.                 if (is_null($item->getSubTotalPrice()) or empty($item->getSubTotalPrice())){
  8186.                     $subtotal = 0;
  8187.                     $neto = 0;
  8188.                     $subtotalProduct = 0;
  8189.                 } else {
  8190.                     $subtotalProduct = $item->getSubTotalPrice() * (1 + $factorIva);
  8191.                     $subneto = $item->getSubTotalPrice();
  8192.                     $subtotal =  $subtotalProduct;
  8193.                     $neto =  $subneto;
  8194.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  8195.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  8196.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  8197.                     $arrayItems[] = array(
  8198.                         'type' => 'Product',
  8199.                         'name' => $item->getProductName(),
  8200.                         'amount' => $item->getServicePrice(),
  8201.                         'amountSubTotal' => $neto,
  8202.                         'iva' => ($factorIva * 100),
  8203.                         'total' => $subtotal,
  8204.                     );
  8205.                 }
  8206.             }
  8207.             // Acumula netos totales e IVA
  8208.             $totales_neto_all = $totales_neto_all + $neto;
  8209.             switch ($factorIva){
  8210.                 case 0.21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * $factorIva); break;
  8211.                 case 0: $data_iva['ivaMontoCero'] = 0; break;
  8212.             }
  8213.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  8214.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  8215.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  8216.             // Acumula netos totales e IVA
  8217.             $product['neto'] = $product['neto'] + $neto;
  8218.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  8219.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  8220.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  8221.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  8222.             $pax = $item->getPax();
  8223.             $qty = $item->getUnits();
  8224.             if (!empty($item->getDays())){ $days = $item->getDays(); }
  8225.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  8226.                 $data_supplier['product'][$i] = array(
  8227.                     'id' => $item->getId(),
  8228.                     'loungeName' => $item->getLngLoungeName(),
  8229.                     'idLounge' => $item->getLngIdLounge(),
  8230.                     'dateStart' => $item->getLngDateStart(),
  8231.                     'dateEnd' => $item->getLngDateEnd(),
  8232.                     'servicePrice' => $item->getServicePrice(),
  8233.                     'serviceSubTotalPrice' => $item->getSubTotalPrice(),
  8234.                     'subtotalLounge' => $subtotalLounge,
  8235.                     'iva' => ($factorIva * 100),
  8236.                     'pax' => $pax,
  8237.                     'qty' => $qty,
  8238.                     'days' => $days,
  8239.                     'type' => $item->getLngType(),
  8240.                     'subtotal' => $subtotal,
  8241.                 );
  8242.             } else {
  8243.                 // Es proforma
  8244.                 $data_supplier['product'][$i] = array(
  8245.                     'id' => $item->getId(),
  8246.                     'productName' => $item->getProductName(),
  8247.                     'productId' => $item->getProductId(),
  8248.                     'dateStart' => $item->getDateStart(),
  8249.                     'dateEnd' => $item->getDateEnd(),
  8250.                     'servicePrice' => $item->getServicePrice(),
  8251.                     'serviceSubTotalPrice' => $item->getSubTotalPrice(),
  8252.                     'subtotalProduct' => $subtotalProduct,
  8253.                     'iva' => ($factorIva * 100),
  8254.                     'pax' => $pax,
  8255.                     'qty' => $qty,
  8256.                     'days' => $days,
  8257.                     'type' => $item->getType(),
  8258.                     'subtotal' => $subtotal,
  8259.                 );
  8260.             }
  8261.             $i++;
  8262.         }
  8263.         $data_supplier['productSubTotal'] = array(
  8264.             'neto' => $product['neto'],
  8265.             'sumSubT' => $product['sumSubT'],
  8266.         );
  8267.         $payment = array(
  8268.             'sumSubT' => 0,
  8269.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  8270.         $i = 0;
  8271.         foreach ($payments as $item){
  8272.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  8273.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getPayAmountTotal();
  8274.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  8275.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  8276.                 $data_supplier['payment'][$i] = array(
  8277.                     'id' => $item->getId(),
  8278.                     'amount' => $item->getPayAmount(),
  8279.                     'amountTotal' => $item->getPayAmountTotal(),
  8280.                     'vat' => $item->getPayVat(),
  8281.                     'datePayAt' => $item->getPayDatePayAt(),
  8282.                     'wayToPay' => $item->getPayWayToPay(),
  8283.                 );
  8284.                 $arrayItems[] = array(
  8285.                     'type' => 'Payment',
  8286.                     'name' => $item->getPayWayToPay(),
  8287.                     'amount' => $item->getPayAmount(),
  8288.                     'total' => $item->getPayAmountTotal(),
  8289.                     'iva' => $item->getPayVat(),
  8290. //                    'iva' => '',
  8291. //                    'total' => $item->getPayAmount(),
  8292.                 );
  8293.             } else {
  8294.                 // Es una proforma
  8295.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  8296.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  8297.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  8298.                 $data_supplier['payment'][$i] = array(
  8299.                     'id' => $item->getId(),
  8300.                     'amount' => $item->getAmount(),
  8301.                     'amountTotal' => $item->getAmountTotal(),
  8302.                     'vat' => $item->getVat(),
  8303.                     'datePayAt' => $item->getDatePayAt(),
  8304.                     'wayToPay' => $item->getWayToPay(),
  8305.                 );
  8306.                 $arrayItems[] = array(
  8307.                     'type' => 'Payment',
  8308.                     'name' => $item->getWayToPay(),
  8309.                     'amount' => $item->getAmount(),
  8310.                     'total' => $item->getAmountTotal(),
  8311.                     'iva' => $item->getVat(),
  8312. //                    'iva' => '',
  8313. //                    'total' => $item->getAmount(),
  8314.                 );
  8315.             }
  8316.             $i++;
  8317.         }
  8318.         if (!empty($payments)) {
  8319.             $data_supplier['paymentSubTotal'] = array(
  8320.                 'sumSubT' => $payment['sumSubT'],
  8321.             );
  8322.         }
  8323.         foreach ($services as $item){
  8324.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  8325.                 if (is_null($item->getSrvPrice()) or empty($item->getSrvPrice())) {
  8326.                     $subtotal = 0;
  8327.                     $neto = 0;
  8328.                     $subtotalService = 0;
  8329.                 } else {
  8330.                     $subtotalService = $item->getSrvPrice();
  8331.                     $subneto = $item->getSrvPrice();
  8332.                     // Commission
  8333.                     if ($item->getSrvOpCommission() == '1') {
  8334.                         $subtotalService = $subtotalService * (1 + ($item->getSrvCommission() / 100));
  8335.                         $subneto = $subneto * (1 + ($item->getSrvCommission() / 100));
  8336.                     } else {
  8337.                         $subtotalService = $subtotalService * (1 - ($item->getSrvCommission() / 100));
  8338.                         $subneto = $subneto * (1 - ($item->getSrvCommission() / 100));
  8339.                     }
  8340.                     // Over
  8341.                     if ($item->getSrvOpOver() == '1') {
  8342.                         $subtotalService = $subtotalService + $item->getSrvOver();
  8343.                         $subneto = $subneto + $item->getSrvOver();
  8344.                     } else {
  8345.                         $subtotalService = $subtotalService - $item->getSrvOver();
  8346.                         $subneto = $subneto - $item->getSrvOver();
  8347.                     }
  8348.                     // IVA
  8349.                     if ($item->getSrvOpIva() == '1') {
  8350.                         $subtotalService = $subtotalService * (1 + ($item->getSrvIva() / 100));
  8351.                     } else {
  8352.                         $subtotalService = $item->getSrvPrice();
  8353.                         $subneto = ($subneto * 100) / (100 + $item->getSrvIva());
  8354.                     }
  8355.                     switch ($item->getSrvServiceCatId()) {
  8356.                         case 1: // Alojamiento
  8357.                             // el numero de noches $numNoches; precio unitario $subneto
  8358.                             $numNoches = (($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days;
  8359.                             // La personas no afectan este calculo
  8360.                             $subtotal = $subtotalService * $numNoches * $item->getSrvUnits();
  8361.                             $subnetoUnit = $subneto;
  8362.                             $subneto = $subneto * $numNoches * $item->getSrvUnits();
  8363.                             $data_supplier['service'][$i] = array(
  8364.                                 'id' => $item->getId(),
  8365.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8366.                                 'serviceName' => $item->getSrvName(),
  8367.                                 'serviceType' => 'Hotel',
  8368.                                 'date' => ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y'),
  8369.                                 'qty' => $item->getSrvUnits(),
  8370.                                 'iva' => $item->getSrvIva(),
  8371.                                 'pax' => '-',
  8372.                                 'precioUnit' => $subnetoUnit,
  8373.                                 'subneto' => $subneto,
  8374.                                 'subtotal' => $subtotal,
  8375.                             );
  8376.                             break;
  8377.                         case 2: //Actividades
  8378.                             // El número de personas es considerado en el calculo
  8379.                             $pax = $item->getSrvPax();
  8380.                             if (empty($pax) or $pax == "0") {
  8381.                                 $pax = 1;
  8382.                             }
  8383.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8384.                             if ($days > 1) {
  8385.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8386.                             } else {
  8387.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8388.                             }
  8389.                             $subtotal = $subtotalService * $days * $item->getSrvUnits();
  8390.                             $subnetoUnit = $subneto;
  8391.                             $subneto = $subneto * $days * $item->getSrvUnits();
  8392.                             $data_supplier['service'][$i] = array(
  8393.                                 'id' => $item->getId(),
  8394.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8395.                                 'serviceName' => $item->getSrvName(),
  8396.                                 'serviceType' => 'Actividad',
  8397.                                 'date' => $dateServ,
  8398.                                 'qty' => $item->getSrvUnits(),
  8399.                                 'iva' => $item->getSrvIva(),
  8400.                                 'pax' => $item->getSrvPax(),
  8401.                                 'precioUnit' => $subnetoUnit,
  8402.                                 'subneto' => $subneto,
  8403.                                 'subtotal' => $subtotal,
  8404.                             );
  8405.                             break;
  8406.                         case 3: // AV
  8407.                             $pax = $item->getSrvPax();
  8408.                             if (empty($pax) or $pax == "0") {
  8409.                                 $pax = 1;
  8410.                             }
  8411.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8412.                             if ($days > 1) {
  8413.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8414.                             } else {
  8415.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8416.                             }
  8417.                             $unitsServ = $item->getSrvUnits();
  8418.                             if (empty($unitsServ) or $unitsServ == "0") {
  8419.                                 $unitsServ = 1;
  8420.                             }
  8421.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8422.                             $subnetoUnit = $subneto;
  8423.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8424.                             $data_supplier['service'][$i] = array(
  8425.                                 'id' => $item->getId(),
  8426.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8427.                                 'serviceName' => $item->getSrvName(),
  8428.                                 'serviceType' => 'AV',
  8429.                                 'date' => $dateServ,
  8430.                                 'qty' => $unitsServ,
  8431.                                 'iva' => $item->getSrvIva(),
  8432.                                 'pax' => $item->getSrvPax(),
  8433.                                 'precioUnit' => $subnetoUnit,
  8434.                                 'subneto' => $subneto,
  8435.                                 'subtotal' => $subtotal,
  8436.                             );
  8437.                             break;
  8438.                         case 4: //Creative
  8439.                             $pax = $item->getSrvPax();
  8440.                             if (empty($pax) or $pax == "0") {
  8441.                                 $pax = 1;
  8442.                             }
  8443.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8444.                             if ($days > 1) {
  8445.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8446.                             } else {
  8447.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8448.                             }
  8449.                             $unitsServ = $item->getSrvUnits();
  8450.                             if (empty($unitsServ) or $unitsServ == "0") {
  8451.                                 $unitsServ = 1;
  8452.                             }
  8453.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8454.                             $subnetoUnit = $subneto;
  8455.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8456.                             $data_supplier['service'][$i] = array(
  8457.                                 'id' => $item->getId(),
  8458.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8459.                                 'serviceName' => $item->getSrvName(),
  8460.                                 'serviceType' => 'Creativo',
  8461.                                 'date' => $dateServ,
  8462.                                 'qty' => $unitsServ,
  8463.                                 'iva' => $item->getSrvIva(),
  8464.                                 'pax' => $item->getSrvPax(),
  8465.                                 'precioUnit' => $subnetoUnit,
  8466.                                 'subneto' => $subneto,
  8467.                                 'subtotal' => $subtotal,
  8468.                             );
  8469.                             break;
  8470.                         case 5: //Cruise
  8471.                             $pax = $item->getSrvPax();
  8472.                             if (empty($pax) or $pax == "0") {
  8473.                                 $pax = 1;
  8474.                             }
  8475.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days);
  8476.                             if ($days > 1) {
  8477.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8478.                             } else {
  8479.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8480.                             }
  8481.                             $unitsServ = $item->getSrvUnits();
  8482.                             if (empty($unitsServ) or $unitsServ == "0") {
  8483.                                 $unitsServ = 1;
  8484.                             }
  8485.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8486.                             $subnetoUnit = $subneto;
  8487.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8488.                             $data_supplier['service'][$i] = array(
  8489.                                 'id' => $item->getId(),
  8490.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8491.                                 'serviceName' => $item->getSrvName(),
  8492.                                 'serviceType' => 'Crucero',
  8493.                                 'date' => $dateServ,
  8494.                                 'qty' => $unitsServ,
  8495.                                 'iva' => $item->getSrvIva(),
  8496.                                 'pax' => $item->getSrvPax(),
  8497.                                 'precioUnit' => $subnetoUnit,
  8498.                                 'subneto' => $subneto,
  8499.                                 'subtotal' => $subtotal,
  8500.                             );
  8501.                             break;
  8502.                         case 6: //Entertaiment
  8503.                             $pax = $item->getSrvPax();
  8504.                             if (empty($pax) or $pax == "0") {
  8505.                                 $pax = 1;
  8506.                             }
  8507.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8508.                             if ($days > 1) {
  8509.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8510.                             } else {
  8511.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8512.                             }
  8513.                             $unitsServ = $item->getSrvUnits();
  8514.                             if (empty($unitsServ) or $unitsServ == "0") {
  8515.                                 $unitsServ = 1;
  8516.                             }
  8517.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8518.                             $subnetoUnit = $subneto;
  8519.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8520.                             $data_supplier['service'][$i] = array(
  8521.                                 'id' => $item->getId(),
  8522.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8523.                                 'serviceName' => $item->getSrvName(),
  8524.                                 'serviceType' => 'Entretenimiento',
  8525.                                 'date' => $dateServ,
  8526.                                 'qty' => $unitsServ,
  8527.                                 'iva' => $item->getSrvIva(),
  8528.                                 'pax' => $item->getSrvPax(),
  8529.                                 'precioUnit' => $subnetoUnit,
  8530.                                 'subneto' => $subneto,
  8531.                                 'subtotal' => $subtotal,
  8532.                             );
  8533.                             break;
  8534.                         case 7: // Gifts
  8535.                             $pax = $item->getSrvPax();
  8536.                             if (empty($pax) or $pax == "0") {
  8537.                                 $pax = 1;
  8538.                             }
  8539. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  8540.                             $days = 1;
  8541.                             if ($days > 1) {
  8542.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8543.                             } else {
  8544.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8545.                             }
  8546.                             $unitsServ = $item->getSrvUnits();
  8547.                             if (empty($unitsServ) or $unitsServ == "0") {
  8548.                                 $unitsServ = 1;
  8549.                             }
  8550.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8551.                             $subnetoUnit = $subneto;
  8552.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8553.                             $data_supplier['service'][$i] = array(
  8554.                                 'id' => $item->getId(),
  8555.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8556.                                 'serviceName' => $item->getSrvName(),
  8557.                                 'serviceType' => 'Regalos',
  8558.                                 'date' => $dateServ,
  8559.                                 'qty' => $unitsServ,
  8560.                                 'iva' => $item->getSrvIva(),
  8561.                                 'pax' => $item->getSrvPax(),
  8562.                                 'precioUnit' => $subnetoUnit,
  8563.                                 'subneto' => $subneto,
  8564.                                 'subtotal' => $subtotal,
  8565.                             );
  8566.                             break;
  8567.                         case 8: //Guide
  8568.                             $pax = $item->getSrvPax();
  8569.                             if (empty($pax) or $pax == "0") {
  8570.                                 $pax = 1;
  8571.                             }
  8572.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8573.                             if ($days > 1) {
  8574.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8575.                             } else {
  8576.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8577.                             }
  8578.                             $unitsServ = $item->getSrvUnits();
  8579.                             if (empty($unitsServ) or $unitsServ == "0") {
  8580.                                 $unitsServ = 1;
  8581.                             }
  8582.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8583.                             $subnetoUnit = $subneto;
  8584.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8585.                             $data_supplier['service'][$i] = array(
  8586.                                 'id' => $item->getId(),
  8587.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8588.                                 'serviceName' => $item->getSrvName(),
  8589.                                 'serviceType' => 'Regalos',
  8590.                                 'date' => $dateServ,
  8591.                                 'qty' => $unitsServ,
  8592.                                 'iva' => $item->getSrvIva(),
  8593.                                 'pax' => $item->getSrvPax(),
  8594.                                 'precioUnit' => $subnetoUnit,
  8595.                                 'subneto' => $subneto,
  8596.                                 'subtotal' => $subtotal,
  8597.                             );
  8598.                             break;
  8599.                         case 9: //Itineraries
  8600.                             $pax = $item->getSrvPax();
  8601.                             if (empty($pax) or $pax == "0") {
  8602.                                 $pax = 1;
  8603.                             }
  8604.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8605.                             if ($days > 1) {
  8606.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8607.                             } else {
  8608.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8609.                             }
  8610.                             $unitsServ = $item->getSrvUnits();
  8611.                             if (empty($unitsServ) or $unitsServ == "0") {
  8612.                                 $unitsServ = 1;
  8613.                             }
  8614.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8615.                             $subnetoUnit = $subneto;
  8616.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8617.                             $data_supplier['service'][$i] = array(
  8618.                                 'id' => $item->getId(),
  8619.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8620.                                 'serviceName' => $item->getSrvName(),
  8621.                                 'serviceType' => 'Itinerarios',
  8622.                                 'date' => $dateServ,
  8623.                                 'qty' => $unitsServ,
  8624.                                 'iva' => $item->getSrvIva(),
  8625.                                 'pax' => $item->getSrvPax(),
  8626.                                 'precioUnit' => $subnetoUnit,
  8627.                                 'subneto' => $subneto,
  8628.                                 'subtotal' => $subtotal,
  8629.                             );
  8630.                             break;
  8631.                         case 10: //Lounge  -- No Aplica
  8632. //                        $pax = $item->getPax();
  8633. //                        if (empty($pax) or $pax == "0") {
  8634. //                            $pax = 1;
  8635. //                        }
  8636. //
  8637. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  8638. //                        if ($days > 1){
  8639. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  8640. //                        } else {
  8641. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  8642. //                        }
  8643. //
  8644. //                        $unitsServ = $item->getUnits();
  8645. //                        if (empty($unitsServ) or $unitsServ == "0") {
  8646. //                            $unitsServ = 1;
  8647. //                        }
  8648. //
  8649. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8650. //                        $subnetoUnit = $subneto;
  8651. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  8652. //
  8653. //                        $data_supplier['service'][$i] = array (
  8654. //                            'id' => $item->getId(),
  8655. //                            'serviceCatId' => $item->getServiceCatId(),
  8656. //                            'serviceName' => $item->getName(),
  8657. //                            'serviceType' => 'Itinerarios',
  8658. //                            'date' => $dateServ,
  8659. //                            'qty' => $unitsServ,
  8660. //                            'iva' => $item->getIva(),
  8661. //                            'pax' => $item->getPax(),
  8662. //                            'precioUnit' => $subnetoUnit,
  8663. //                            'subneto' => $subneto,
  8664. //                            'subtotal' => $subtotal,
  8665. //                        );
  8666.                             break;
  8667.                         case 11: //Menu
  8668.                             $pax = $item->getSrvPax();
  8669.                             if (empty($pax) or $pax == "0") {
  8670.                                 $pax = 1;
  8671.                             }
  8672.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8673.                             if ($days > 1) {
  8674.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8675.                             } else {
  8676.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8677.                             }
  8678.                             $unitsServ = $item->getSrvUnits();
  8679.                             if (empty($unitsServ) or $unitsServ == "0") {
  8680.                                 $unitsServ = 1;
  8681.                             }
  8682.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8683.                             $subnetoUnit = $subneto;
  8684.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8685.                             $data_supplier['service'][$i] = array(
  8686.                                 'id' => $item->getId(),
  8687.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8688.                                 'serviceName' => $item->getSrvName(),
  8689.                                 'serviceType' => 'Menú',
  8690.                                 'date' => $dateServ,
  8691.                                 'qty' => $unitsServ,
  8692.                                 'iva' => $item->getSrvIva(),
  8693.                                 'pax' => $item->getSrvPax(),
  8694.                                 'precioUnit' => $subnetoUnit,
  8695.                                 'subneto' => $subneto,
  8696.                                 'subtotal' => $subtotal,
  8697.                             );
  8698.                             break;
  8699.                         case 12: //Others
  8700.                             $pax = $item->getSrvPax();
  8701.                             if (empty($pax) or $pax == "0") {
  8702.                                 $pax = 1;
  8703.                             }
  8704.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8705.                             if ($days > 1) {
  8706.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8707.                             } else {
  8708.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8709.                             }
  8710.                             $unitsServ = $item->getSrvUnits();
  8711.                             if (empty($unitsServ) or $unitsServ == "0") {
  8712.                                 $unitsServ = 1;
  8713.                             }
  8714.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8715.                             $subnetoUnit = $subneto;
  8716.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8717.                             $data_supplier['service'][$i] = array(
  8718.                                 'id' => $item->getId(),
  8719.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8720.                                 'serviceName' => $item->getSrvName(),
  8721.                                 'serviceType' => 'Otros',
  8722.                                 'date' => $dateServ,
  8723.                                 'qty' => $unitsServ,
  8724.                                 'iva' => $item->getSrvIva(),
  8725.                                 'pax' => $item->getSrvPax(),
  8726.                                 'precioUnit' => $subnetoUnit,
  8727.                                 'subneto' => $subneto,
  8728.                                 'subtotal' => $subtotal,
  8729.                             );
  8730.                             break;
  8731.                         case 13: //Transport
  8732.                             $pax = $item->getSrvPax();
  8733.                             if (empty($pax) or $pax == "0") {
  8734.                                 $pax = 1;
  8735.                             }
  8736.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8737.                             if ($days > 1) {
  8738.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8739.                             } else {
  8740.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8741.                             }
  8742.                             $unitsServ = $item->getSrvUnits();
  8743.                             if (empty($unitsServ) or $unitsServ == "0") {
  8744.                                 $unitsServ = 1;
  8745.                             }
  8746.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8747.                             $subnetoUnit = $subneto;
  8748.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8749.                             $data_supplier['service'][$i] = array(
  8750.                                 'id' => $item->getId(),
  8751.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8752.                                 'serviceName' => $item->getSrvName(),
  8753.                                 'serviceType' => 'Transporte',
  8754.                                 'date' => $dateServ,
  8755.                                 'qty' => $unitsServ,
  8756.                                 'iva' => $item->getSrvIva(),
  8757.                                 'pax' => $item->getSrvPax(),
  8758.                                 'precioUnit' => $subnetoUnit,
  8759.                                 'subneto' => $subneto,
  8760.                                 'subtotal' => $subtotal,
  8761.                             );
  8762.                             break;
  8763.                         case 14: //Technology
  8764.                             $pax = $item->getSrvPax();
  8765.                             if (empty($pax) or $pax == "0") {
  8766.                                 $pax = 1;
  8767.                             }
  8768. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  8769.                             $days = 1;
  8770. //                        if ($days > 1){
  8771. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  8772. //                        } else {
  8773. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  8774. //                        }
  8775.                             $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8776.                             $unitsServ = $item->getSrvUnits();
  8777.                             if (empty($unitsServ) or $unitsServ == "0") {
  8778.                                 $unitsServ = 1;
  8779.                             }
  8780.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8781.                             $subnetoUnit = $subneto;
  8782.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8783.                             $data_supplier['service'][$i] = array(
  8784.                                 'id' => $item->getId(),
  8785.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8786.                                 'serviceName' => $item->getSrvName(),
  8787.                                 'serviceType' => 'Tecnología',
  8788.                                 'date' => $dateServ,
  8789.                                 'qty' => $unitsServ,
  8790.                                 'iva' => $item->getSrvIva(),
  8791.                                 'pax' => $item->getSrvPax(),
  8792.                                 'precioUnit' => $subnetoUnit,
  8793.                                 'subneto' => $subneto,
  8794.                                 'subtotal' => $subtotal,
  8795.                             );
  8796.                             break;
  8797.                         case 15: //Assisstant
  8798.                             $pax = $item->getSrvPax();
  8799.                             if (empty($pax) or $pax == "0") {
  8800.                                 $pax = 1;
  8801.                             }
  8802.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8803.                             if ($days > 1) {
  8804.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8805.                             } else {
  8806.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8807.                             }
  8808.                             $unitsServ = $item->getSrvUnits();
  8809.                             if (empty($unitsServ) or $unitsServ == "0") {
  8810.                                 $unitsServ = 1;
  8811.                             }
  8812.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8813.                             $subnetoUnit = $subneto;
  8814.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8815.                             $data_supplier['service'][$i] = array(
  8816.                                 'id' => $item->getId(),
  8817.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8818.                                 'serviceName' => $item->getSrvName(),
  8819.                                 'serviceType' => 'Asistente',
  8820.                                 'date' => $dateServ,
  8821.                                 'qty' => $unitsServ,
  8822.                                 'iva' => $item->getSrvIva(),
  8823.                                 'pax' => $item->getSrvPax(),
  8824.                                 'precioUnit' => $subnetoUnit,
  8825.                                 'subneto' => $subneto,
  8826.                                 'subtotal' => $subtotal,
  8827.                             );
  8828.                             break;
  8829.                         case 16: //DDR
  8830.                             $pax = $item->getSrvPax();
  8831.                             if (empty($pax) or $pax == "0") {
  8832.                                 $pax = 1;
  8833.                             }
  8834.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  8835.                             if ($days > 1) {
  8836.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  8837.                             } else {
  8838.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  8839.                             }
  8840.                             $unitsServ = $item->getSrvUnits();
  8841.                             if (empty($unitsServ) or $unitsServ == "0") {
  8842.                                 $unitsServ = 1;
  8843.                             }
  8844.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8845.                             $subnetoUnit = $subneto;
  8846.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8847.                             $data_supplier['service'][$i] = array(
  8848.                                 'id' => $item->getId(),
  8849.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  8850.                                 'serviceName' => $item->getSrvName(),
  8851.                                 'serviceType' => 'DDR',
  8852.                                 'date' => $dateServ,
  8853.                                 'qty' => $unitsServ,
  8854.                                 'iva' => $item->getSrvIva(),
  8855.                                 'pax' => $item->getSrvPax(),
  8856.                                 'precioUnit' => $subnetoUnit,
  8857.                                 'subneto' => $subneto,
  8858.                                 'subtotal' => $subtotal,
  8859.                             );
  8860.                             break;
  8861.                         default:
  8862.                             break;
  8863.                     }
  8864.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  8865.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  8866.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  8867.                     $arrayItems[] = array(
  8868.                         'type' => 'Service',
  8869.                         'name' => $item->getSrvName(),
  8870.                         'amount' => $neto,
  8871.                         'iva' => $item->getSrvIva(),
  8872.                         'total' => $subtotal,
  8873.                     );
  8874.                 }
  8875.                 switch ($item->getSrvIva()) {
  8876.                     // Acumula IVA
  8877.                     case 21:
  8878.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getSrvIva() / 100));
  8879.                         break;
  8880.                     case 10:
  8881.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getSrvIva() / 100));
  8882.                         break;
  8883.                     case 0:
  8884.                         break;
  8885.                     default:
  8886.                         break;
  8887.                 }
  8888.             } else {
  8889.                 // Es una proforma
  8890.                 if (is_null($item->getPrice()) or empty($item->getPrice())) {
  8891.                     $subtotal = 0;
  8892.                     $neto = 0;
  8893.                     $subtotalService = 0;
  8894.                 } else {
  8895.                     $subtotalService = $item->getPrice();
  8896.                     $subneto = $item->getPrice();
  8897.                     // Commission
  8898.                     if ($item->getOpCommission() == '1') {
  8899.                         $subtotalService = $subtotalService * (1 + ($item->getCommission() / 100));
  8900.                         $subneto = $subneto * (1 + ($item->getCommission() / 100));
  8901.                     } else {
  8902.                         $subtotalService = $subtotalService * (1 - ($item->getCommission() / 100));
  8903.                         $subneto = $subneto * (1 - ($item->getCommission() / 100));
  8904.                     }
  8905.                     // Over
  8906.                     if ($item->getOpOver() == '1') {
  8907.                         $subtotalService = $subtotalService + $item->getOver();
  8908.                         $subneto = $subneto + $item->getOver();
  8909.                     } else {
  8910.                         $subtotalService = $subtotalService - $item->getOver();
  8911.                         $subneto = $subneto - $item->getOver();
  8912.                     }
  8913.                     // IVA
  8914.                     if ($item->getOpIva() == '1') {
  8915.                         $subtotalService = $subtotalService * (1 + ($item->getIva() / 100));
  8916.                     } else {
  8917.                         $subtotalService = $item->getPrice();
  8918.                         $subneto = ($subneto * 100) / (100 + $item->getIva());
  8919.                     }
  8920.                     switch ($item->getServiceCatId()) {
  8921.                         case 1: // Alojamiento
  8922.                             // el numero de noches $numNoches; precio unitario $subneto
  8923.                             $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  8924.                             // La personas no afectan este calculo
  8925.                             $subtotal = $subtotalService * $numNoches * $item->getUnits();
  8926.                             $subnetoUnit = $subneto;
  8927.                             $subneto = $subneto * $numNoches * $item->getUnits();
  8928.                             $data_supplier['service'][$i] = array(
  8929.                                 'id' => $item->getId(),
  8930.                                 'serviceCatId' => $item->getServiceCatId(),
  8931.                                 'serviceName' => $item->getName(),
  8932.                                 'serviceType' => 'Hotel',
  8933.                                 'date' => ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y'),
  8934.                                 'qty' => $item->getUnits(),
  8935.                                 'iva' => $item->getIva(),
  8936.                                 'pax' => '-',
  8937.                                 'precioUnit' => $subnetoUnit,
  8938.                                 'subneto' => $subneto,
  8939.                                 'subtotal' => $subtotal,
  8940.                             );
  8941.                             break;
  8942.                         case 2: //Actividades
  8943.                             // El número de personas es considerado en el calculo
  8944.                             $pax = $item->getPax();
  8945.                             if (empty($pax) or $pax == "0") {
  8946.                                 $pax = 1;
  8947.                             }
  8948.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  8949.                             if ($days > 1) {
  8950.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  8951.                             } else {
  8952.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  8953.                             }
  8954.                             $subtotal = $subtotalService * $days * $item->getUnits();
  8955.                             $subnetoUnit = $subneto;
  8956.                             $subneto = $subneto * $days * $item->getUnits();
  8957.                             $data_supplier['service'][$i] = array(
  8958.                                 'id' => $item->getId(),
  8959.                                 'serviceCatId' => $item->getServiceCatId(),
  8960.                                 'serviceName' => $item->getName(),
  8961.                                 'serviceType' => 'Actividad',
  8962.                                 'date' => $dateServ,
  8963.                                 'qty' => $item->getUnits(),
  8964.                                 'iva' => $item->getIva(),
  8965.                                 'pax' => $item->getPax(),
  8966.                                 'precioUnit' => $subnetoUnit,
  8967.                                 'subneto' => $subneto,
  8968.                                 'subtotal' => $subtotal,
  8969.                             );
  8970.                             break;
  8971.                         case 3: // AV
  8972.                             $pax = $item->getPax();
  8973.                             if (empty($pax) or $pax == "0") {
  8974.                                 $pax = 1;
  8975.                             }
  8976.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  8977.                             if ($days > 1) {
  8978.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  8979.                             } else {
  8980.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  8981.                             }
  8982.                             $unitsServ = $item->getUnits();
  8983.                             if (empty($unitsServ) or $unitsServ == "0") {
  8984.                                 $unitsServ = 1;
  8985.                             }
  8986.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  8987.                             $subnetoUnit = $subneto;
  8988.                             $subneto = $subneto * $days * $unitsServ * $pax;
  8989.                             $data_supplier['service'][$i] = array(
  8990.                                 'id' => $item->getId(),
  8991.                                 'serviceCatId' => $item->getServiceCatId(),
  8992.                                 'serviceName' => $item->getName(),
  8993.                                 'serviceType' => 'AV',
  8994.                                 'date' => $dateServ,
  8995.                                 'qty' => $unitsServ,
  8996.                                 'iva' => $item->getIva(),
  8997.                                 'pax' => $item->getPax(),
  8998.                                 'precioUnit' => $subnetoUnit,
  8999.                                 'subneto' => $subneto,
  9000.                                 'subtotal' => $subtotal,
  9001.                             );
  9002.                             break;
  9003.                         case 4: //Creative
  9004.                             $pax = $item->getPax();
  9005.                             if (empty($pax) or $pax == "0") {
  9006.                                 $pax = 1;
  9007.                             }
  9008.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9009.                             if ($days > 1) {
  9010.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9011.                             } else {
  9012.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9013.                             }
  9014.                             $unitsServ = $item->getUnits();
  9015.                             if (empty($unitsServ) or $unitsServ == "0") {
  9016.                                 $unitsServ = 1;
  9017.                             }
  9018.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9019.                             $subnetoUnit = $subneto;
  9020.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9021.                             $data_supplier['service'][$i] = array(
  9022.                                 'id' => $item->getId(),
  9023.                                 'serviceCatId' => $item->getServiceCatId(),
  9024.                                 'serviceName' => $item->getName(),
  9025.                                 'serviceType' => 'Creativo',
  9026.                                 'date' => $dateServ,
  9027.                                 'qty' => $unitsServ,
  9028.                                 'iva' => $item->getIva(),
  9029.                                 'pax' => $item->getPax(),
  9030.                                 'precioUnit' => $subnetoUnit,
  9031.                                 'subneto' => $subneto,
  9032.                                 'subtotal' => $subtotal,
  9033.                             );
  9034.                             break;
  9035.                         case 5: //Cruise
  9036.                             $pax = $item->getPax();
  9037.                             if (empty($pax) or $pax == "0") {
  9038.                                 $pax = 1;
  9039.                             }
  9040.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  9041.                             if ($days > 1) {
  9042.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9043.                             } else {
  9044.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9045.                             }
  9046.                             $unitsServ = $item->getUnits();
  9047.                             if (empty($unitsServ) or $unitsServ == "0") {
  9048.                                 $unitsServ = 1;
  9049.                             }
  9050.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9051.                             $subnetoUnit = $subneto;
  9052.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9053.                             $data_supplier['service'][$i] = array(
  9054.                                 'id' => $item->getId(),
  9055.                                 'serviceCatId' => $item->getServiceCatId(),
  9056.                                 'serviceName' => $item->getName(),
  9057.                                 'serviceType' => 'Crucero',
  9058.                                 'date' => $dateServ,
  9059.                                 'qty' => $unitsServ,
  9060.                                 'iva' => $item->getIva(),
  9061.                                 'pax' => $item->getPax(),
  9062.                                 'precioUnit' => $subnetoUnit,
  9063.                                 'subneto' => $subneto,
  9064.                                 'subtotal' => $subtotal,
  9065.                             );
  9066.                             break;
  9067.                         case 6: //Entertaiment
  9068.                             $pax = $item->getPax();
  9069.                             if (empty($pax) or $pax == "0") {
  9070.                                 $pax = 1;
  9071.                             }
  9072.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9073.                             if ($days > 1) {
  9074.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9075.                             } else {
  9076.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9077.                             }
  9078.                             $unitsServ = $item->getUnits();
  9079.                             if (empty($unitsServ) or $unitsServ == "0") {
  9080.                                 $unitsServ = 1;
  9081.                             }
  9082.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9083.                             $subnetoUnit = $subneto;
  9084.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9085.                             $data_supplier['service'][$i] = array(
  9086.                                 'id' => $item->getId(),
  9087.                                 'serviceCatId' => $item->getServiceCatId(),
  9088.                                 'serviceName' => $item->getName(),
  9089.                                 'serviceType' => 'Entretenimiento',
  9090.                                 'date' => $dateServ,
  9091.                                 'qty' => $unitsServ,
  9092.                                 'iva' => $item->getIva(),
  9093.                                 'pax' => $item->getPax(),
  9094.                                 'precioUnit' => $subnetoUnit,
  9095.                                 'subneto' => $subneto,
  9096.                                 'subtotal' => $subtotal,
  9097.                             );
  9098.                             break;
  9099.                         case 7: // Gifts
  9100.                             $pax = $item->getPax();
  9101.                             if (empty($pax) or $pax == "0") {
  9102.                                 $pax = 1;
  9103.                             }
  9104. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9105.                             $days = 1;
  9106.                             if ($days > 1) {
  9107.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9108.                             } else {
  9109.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9110.                             }
  9111.                             $unitsServ = $item->getUnits();
  9112.                             if (empty($unitsServ) or $unitsServ == "0") {
  9113.                                 $unitsServ = 1;
  9114.                             }
  9115.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9116.                             $subnetoUnit = $subneto;
  9117.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9118.                             $data_supplier['service'][$i] = array(
  9119.                                 'id' => $item->getId(),
  9120.                                 'serviceCatId' => $item->getServiceCatId(),
  9121.                                 'serviceName' => $item->getName(),
  9122.                                 'serviceType' => 'Regalos',
  9123.                                 'date' => $dateServ,
  9124.                                 'qty' => $unitsServ,
  9125.                                 'iva' => $item->getIva(),
  9126.                                 'pax' => $item->getPax(),
  9127.                                 'precioUnit' => $subnetoUnit,
  9128.                                 'subneto' => $subneto,
  9129.                                 'subtotal' => $subtotal,
  9130.                             );
  9131.                             break;
  9132.                         case 8: //Guide
  9133.                             $pax = $item->getPax();
  9134.                             if (empty($pax) or $pax == "0") {
  9135.                                 $pax = 1;
  9136.                             }
  9137.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9138.                             if ($days > 1) {
  9139.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9140.                             } else {
  9141.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9142.                             }
  9143.                             $unitsServ = $item->getUnits();
  9144.                             if (empty($unitsServ) or $unitsServ == "0") {
  9145.                                 $unitsServ = 1;
  9146.                             }
  9147.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9148.                             $subnetoUnit = $subneto;
  9149.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9150.                             $data_supplier['service'][$i] = array(
  9151.                                 'id' => $item->getId(),
  9152.                                 'serviceCatId' => $item->getServiceCatId(),
  9153.                                 'serviceName' => $item->getName(),
  9154.                                 'serviceType' => 'Regalos',
  9155.                                 'date' => $dateServ,
  9156.                                 'qty' => $unitsServ,
  9157.                                 'iva' => $item->getIva(),
  9158.                                 'pax' => $item->getPax(),
  9159.                                 'precioUnit' => $subnetoUnit,
  9160.                                 'subneto' => $subneto,
  9161.                                 'subtotal' => $subtotal,
  9162.                             );
  9163.                             break;
  9164.                         case 9: //Itineraries
  9165.                             $pax = $item->getPax();
  9166.                             if (empty($pax) or $pax == "0") {
  9167.                                 $pax = 1;
  9168.                             }
  9169.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9170.                             if ($days > 1) {
  9171.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9172.                             } else {
  9173.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9174.                             }
  9175.                             $unitsServ = $item->getUnits();
  9176.                             if (empty($unitsServ) or $unitsServ == "0") {
  9177.                                 $unitsServ = 1;
  9178.                             }
  9179.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9180.                             $subnetoUnit = $subneto;
  9181.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9182.                             $data_supplier['service'][$i] = array(
  9183.                                 'id' => $item->getId(),
  9184.                                 'serviceCatId' => $item->getServiceCatId(),
  9185.                                 'serviceName' => $item->getName(),
  9186.                                 'serviceType' => 'Itinerarios',
  9187.                                 'date' => $dateServ,
  9188.                                 'qty' => $unitsServ,
  9189.                                 'iva' => $item->getIva(),
  9190.                                 'pax' => $item->getPax(),
  9191.                                 'precioUnit' => $subnetoUnit,
  9192.                                 'subneto' => $subneto,
  9193.                                 'subtotal' => $subtotal,
  9194.                             );
  9195.                             break;
  9196.                         case 10: //Lounge  -- No Aplica
  9197. //                        $pax = $item->getPax();
  9198. //                        if (empty($pax) or $pax == "0") {
  9199. //                            $pax = 1;
  9200. //                        }
  9201. //
  9202. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9203. //                        if ($days > 1){
  9204. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  9205. //                        } else {
  9206. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9207. //                        }
  9208. //
  9209. //                        $unitsServ = $item->getUnits();
  9210. //                        if (empty($unitsServ) or $unitsServ == "0") {
  9211. //                            $unitsServ = 1;
  9212. //                        }
  9213. //
  9214. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9215. //                        $subnetoUnit = $subneto;
  9216. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  9217. //
  9218. //                        $data_supplier['service'][$i] = array (
  9219. //                            'id' => $item->getId(),
  9220. //                            'serviceCatId' => $item->getServiceCatId(),
  9221. //                            'serviceName' => $item->getName(),
  9222. //                            'serviceType' => 'Itinerarios',
  9223. //                            'date' => $dateServ,
  9224. //                            'qty' => $unitsServ,
  9225. //                            'iva' => $item->getIva(),
  9226. //                            'pax' => $item->getPax(),
  9227. //                            'precioUnit' => $subnetoUnit,
  9228. //                            'subneto' => $subneto,
  9229. //                            'subtotal' => $subtotal,
  9230. //                        );
  9231.                             break;
  9232.                         case 11: //Menu
  9233.                             $pax = $item->getPax();
  9234.                             if (empty($pax) or $pax == "0") {
  9235.                                 $pax = 1;
  9236.                             }
  9237.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9238.                             if ($days > 1) {
  9239.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9240.                             } else {
  9241.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9242.                             }
  9243.                             $unitsServ = $item->getUnits();
  9244.                             if (empty($unitsServ) or $unitsServ == "0") {
  9245.                                 $unitsServ = 1;
  9246.                             }
  9247.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9248.                             $subnetoUnit = $subneto;
  9249.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9250.                             $data_supplier['service'][$i] = array(
  9251.                                 'id' => $item->getId(),
  9252.                                 'serviceCatId' => $item->getServiceCatId(),
  9253.                                 'serviceName' => $item->getName(),
  9254.                                 'serviceType' => 'Menú',
  9255.                                 'date' => $dateServ,
  9256.                                 'qty' => $unitsServ,
  9257.                                 'iva' => $item->getIva(),
  9258.                                 'pax' => $item->getPax(),
  9259.                                 'precioUnit' => $subnetoUnit,
  9260.                                 'subneto' => $subneto,
  9261.                                 'subtotal' => $subtotal,
  9262.                             );
  9263.                             break;
  9264.                         case 12: //Others
  9265.                             $pax = $item->getPax();
  9266.                             if (empty($pax) or $pax == "0") {
  9267.                                 $pax = 1;
  9268.                             }
  9269.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9270.                             if ($days > 1) {
  9271.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9272.                             } else {
  9273.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9274.                             }
  9275.                             $unitsServ = $item->getUnits();
  9276.                             if (empty($unitsServ) or $unitsServ == "0") {
  9277.                                 $unitsServ = 1;
  9278.                             }
  9279.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9280.                             $subnetoUnit = $subneto;
  9281.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9282.                             $data_supplier['service'][$i] = array(
  9283.                                 'id' => $item->getId(),
  9284.                                 'serviceCatId' => $item->getServiceCatId(),
  9285.                                 'serviceName' => $item->getName(),
  9286.                                 'serviceType' => 'Otros',
  9287.                                 'date' => $dateServ,
  9288.                                 'qty' => $unitsServ,
  9289.                                 'iva' => $item->getIva(),
  9290.                                 'pax' => $item->getPax(),
  9291.                                 'precioUnit' => $subnetoUnit,
  9292.                                 'subneto' => $subneto,
  9293.                                 'subtotal' => $subtotal,
  9294.                             );
  9295.                             break;
  9296.                         case 13: //Transport
  9297.                             $pax = $item->getPax();
  9298.                             if (empty($pax) or $pax == "0") {
  9299.                                 $pax = 1;
  9300.                             }
  9301.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9302.                             if ($days > 1) {
  9303.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9304.                             } else {
  9305.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9306.                             }
  9307.                             $unitsServ = $item->getUnits();
  9308.                             if (empty($unitsServ) or $unitsServ == "0") {
  9309.                                 $unitsServ = 1;
  9310.                             }
  9311.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9312.                             $subnetoUnit = $subneto;
  9313.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9314.                             $data_supplier['service'][$i] = array(
  9315.                                 'id' => $item->getId(),
  9316.                                 'serviceCatId' => $item->getServiceCatId(),
  9317.                                 'serviceName' => $item->getName(),
  9318.                                 'serviceType' => 'Transporte',
  9319.                                 'date' => $dateServ,
  9320.                                 'qty' => $unitsServ,
  9321.                                 'iva' => $item->getIva(),
  9322.                                 'pax' => $item->getPax(),
  9323.                                 'precioUnit' => $subnetoUnit,
  9324.                                 'subneto' => $subneto,
  9325.                                 'subtotal' => $subtotal,
  9326.                             );
  9327.                             break;
  9328.                         case 14: //Technology
  9329.                             $pax = $item->getPax();
  9330.                             if (empty($pax) or $pax == "0") {
  9331.                                 $pax = 1;
  9332.                             }
  9333. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9334.                             $days = 1;
  9335. //                        if ($days > 1){
  9336. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  9337. //                        } else {
  9338. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9339. //                        }
  9340.                             $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9341.                             $unitsServ = $item->getUnits();
  9342.                             if (empty($unitsServ) or $unitsServ == "0") {
  9343.                                 $unitsServ = 1;
  9344.                             }
  9345.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9346.                             $subnetoUnit = $subneto;
  9347.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9348.                             $data_supplier['service'][$i] = array(
  9349.                                 'id' => $item->getId(),
  9350.                                 'serviceCatId' => $item->getServiceCatId(),
  9351.                                 'serviceName' => $item->getName(),
  9352.                                 'serviceType' => 'Tecnología',
  9353.                                 'date' => $dateServ,
  9354.                                 'qty' => $unitsServ,
  9355.                                 'iva' => $item->getIva(),
  9356.                                 'pax' => $item->getPax(),
  9357.                                 'precioUnit' => $subnetoUnit,
  9358.                                 'subneto' => $subneto,
  9359.                                 'subtotal' => $subtotal,
  9360.                             );
  9361.                             break;
  9362.                         case 15: //Assisstant
  9363.                             $pax = $item->getPax();
  9364.                             if (empty($pax) or $pax == "0") {
  9365.                                 $pax = 1;
  9366.                             }
  9367.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9368.                             if ($days > 1) {
  9369.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9370.                             } else {
  9371.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9372.                             }
  9373.                             $unitsServ = $item->getUnits();
  9374.                             if (empty($unitsServ) or $unitsServ == "0") {
  9375.                                 $unitsServ = 1;
  9376.                             }
  9377.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9378.                             $subnetoUnit = $subneto;
  9379.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9380.                             $data_supplier['service'][$i] = array(
  9381.                                 'id' => $item->getId(),
  9382.                                 'serviceCatId' => $item->getServiceCatId(),
  9383.                                 'serviceName' => $item->getName(),
  9384.                                 'serviceType' => 'Asistente',
  9385.                                 'date' => $dateServ,
  9386.                                 'qty' => $unitsServ,
  9387.                                 'iva' => $item->getIva(),
  9388.                                 'pax' => $item->getPax(),
  9389.                                 'precioUnit' => $subnetoUnit,
  9390.                                 'subneto' => $subneto,
  9391.                                 'subtotal' => $subtotal,
  9392.                             );
  9393.                             break;
  9394.                         case 16: //DDR
  9395.                             $pax = $item->getPax();
  9396.                             if (empty($pax) or $pax == "0") {
  9397.                                 $pax = 1;
  9398.                             }
  9399.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  9400.                             if ($days > 1) {
  9401.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  9402.                             } else {
  9403.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  9404.                             }
  9405.                             $unitsServ = $item->getUnits();
  9406.                             if (empty($unitsServ) or $unitsServ == "0") {
  9407.                                 $unitsServ = 1;
  9408.                             }
  9409.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9410.                             $subnetoUnit = $subneto;
  9411.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9412.                             $data_supplier['service'][$i] = array(
  9413.                                 'id' => $item->getId(),
  9414.                                 'serviceCatId' => $item->getServiceCatId(),
  9415.                                 'serviceName' => $item->getName(),
  9416.                                 'serviceType' => 'DDR',
  9417.                                 'date' => $dateServ,
  9418.                                 'qty' => $unitsServ,
  9419.                                 'iva' => $item->getIva(),
  9420.                                 'pax' => $item->getPax(),
  9421.                                 'precioUnit' => $subnetoUnit,
  9422.                                 'subneto' => $subneto,
  9423.                                 'subtotal' => $subtotal,
  9424.                             );
  9425.                             break;
  9426.                         default:
  9427.                             break;
  9428.                     }
  9429.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  9430.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  9431.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  9432.                     $arrayItems[] = array(
  9433.                         'type' => 'Service',
  9434.                         'name' => $item->getName(),
  9435.                         'amount' => $neto,
  9436.                         'iva' => $item->getIva(),
  9437.                         'total' => $subtotal,
  9438.                     );
  9439.                 }
  9440.                 switch ($item->getIva()) {
  9441.                     // Acumula IVA
  9442.                     case 21:
  9443.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva() / 100));
  9444.                         break;
  9445.                     case 10:
  9446.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva() / 100));
  9447.                         break;
  9448.                     case 0:
  9449.                         break;
  9450.                     default:
  9451.                         break;
  9452.                 }
  9453.             }
  9454.             $totales_neto_all = $totales_neto_all + $neto;
  9455.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9456.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  9457.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  9458.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  9459.             // Acumula netos totales e IVA
  9460.             $service['neto'] = $service['neto'] + $neto;
  9461.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  9462.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9463.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  9464.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  9465.             $i++;
  9466.         }
  9467.         $data_supplier['serviceSubTotal'] = array(
  9468.             'neto' => $service['neto'],
  9469.             'sumSubT' => $service['sumSubT'],
  9470.         );
  9471.         $currency = '€';
  9472.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  9473.         if (!empty($payments)) {
  9474.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  9475.         } else {
  9476.             $amount_pay = 0;
  9477.         }
  9478.         $totales_all = $totales_total - $amount_pay;
  9479.         if (empty($dateDocument)){
  9480.             $dateDocument = new DateTime('now');
  9481.         }
  9482.         $data = array(
  9483.             'id' => $fileId,
  9484.             'type' => $type,
  9485.             'number' => $number,
  9486.             'prefix' => 'MDV-'.(new DateTime('now'))->format('dmy').'-'.$fileId,
  9487.             'date' => $dateDocument,
  9488.             'file' => $file,
  9489.             'company' => $company,
  9490.             'clients' => $client,
  9491.             'arrayItems' => $arrayItems,
  9492.             'datasupplier' => $data_supplier,
  9493.             'currency' => $currency,
  9494.             'totales_neto' => $totales_neto_all,
  9495.             'bases_imponibles' => $data_iva,
  9496.             'totales' => $totales_total,
  9497.             'balance' => $totales_all,
  9498.             'paymentInvoice' => $amount_pay,
  9499.         );
  9500.         return $data;
  9501.     }
  9502.     private function baseInvoiceDoneFile($fileId, $invoiceId, $typeBase)
  9503.     {
  9504.         $em = $this->getDoctrine()->getManager();
  9505.         $file = $em->getRepository(MdvFiles::class)->findOneById($fileId);
  9506.         $dateDocument = null;                                                                           //Fecha final del documento
  9507.         $subneto = 0;
  9508.         if (($typeBase == 'I') or ($typeBase == 'P')) {
  9509.             if ($typeBase == 'I') {
  9510.                 $items = $em->getRepository(MdvDocInvoiceItems::class)->findBy(array('fileId' => $fileId, 'invoiceId' => $invoiceId));
  9511.             } else {
  9512.                 // Es factura ni proforma
  9513.                 $items = $em->getRepository(MdvDocProformaItems::class)->findBy(array('fileId' => $fileId, 'proformaId' => $invoiceId));
  9514.             }
  9515.         } else {
  9516.             // No es factura ni proforma, es una rectificativa
  9517.             $items = $em->getRepository(MdvDocInvoiceRecItems::class)->findBy(array('fileId' => $fileId, 'invoiceRecId' => $invoiceId));
  9518.         }
  9519.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  9520.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  9521.         if (empty($client)){
  9522.             $client[0] = new Client();
  9523.             $client[0]->setName('');
  9524.             $client[0]->setTitle('');
  9525.             $client[0]->setIdDocument('');
  9526.             $client[0]->setPopulation('');
  9527.             $client[0]->setRegion('');
  9528.             $client[0]->setCountry('');
  9529.         } else {
  9530.             if (is_numeric($client[0]->getPopulation())){
  9531.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  9532.                 $client[0]->setPopulation($city->getCity());
  9533.             }
  9534.             if (is_numeric($client[0]->getRegion())){
  9535.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  9536.                 $client[0]->setRegion($region->getRegion());
  9537.             }
  9538.             if (is_numeric($client[0]->getCountry())){
  9539.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  9540.                 $client[0]->setCountry($country->getCountry());
  9541.             }
  9542.         }
  9543.         // Acumuladores de los calculos
  9544.         $totales_neto_all = 0;
  9545.         $data_iva = array(
  9546.             'iva' => 21,
  9547.             'ivaMontoVeintiUno' => 0,
  9548.             'ivaMontoDiez' => 0,
  9549.             'ivaMontoCero' => 0,
  9550.         );
  9551.         //INICIO: Determinamos el numero de factura o proforma
  9552.         if ($typeBase == 'I'){
  9553.             // Es una Factura
  9554.             $number = $em->getRepository(MdvDocInvoice::class)->findOneById($invoiceId);
  9555.             $dateDocument = $number->getDateAt();
  9556.             $number = $number->getNumber();
  9557.             $type = 'Invoice';
  9558.         } else {
  9559.             if ($typeBase == 'R'){
  9560.                 // Es una factura rectificativa
  9561.                 $number = $em->getRepository(MdvDocInvoiceRec::class)->findOneById($invoiceId);
  9562.                 $dateDocument = $number->getDateAt();
  9563.                 $type = 'Invoice Rec';
  9564.             } else {
  9565.                 // Es una Proforma
  9566.                 $number = $invoiceId;
  9567.                 $type = 'Proforma';
  9568.             }
  9569.         }
  9570.         //FIN: Determinamos el numero de factura o proforma
  9571.         // Buscamos los productos, pagos y servicios
  9572.         $arrayItems = array();
  9573.         $fileProducts = array();
  9574.         $payments = array();
  9575.         $services = array();
  9576.         if (!empty($items)) {
  9577.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  9578.                 foreach ($items as $item) {
  9579.                     switch ($item->getItemType()) {
  9580.                         case 'PRODUCT': // Producto
  9581.                             $fileProducts[] = $item;
  9582.                             break;
  9583.                         case 'PAYMENT': // Pago
  9584.                             $payments[] = $item;
  9585.                             break;
  9586.                         case 'SERVICE': // Servicio
  9587.                             $services[] = $item;
  9588.                             break;
  9589.                         default:
  9590.                             break;
  9591.                     }
  9592.                 }
  9593.             } else {
  9594.                 // Es una proforma
  9595.                 foreach ($items as $item) {
  9596.                     switch ($item->getType()) {
  9597.                         case 'PRODUCT': // Producto
  9598.                             $productPro = $em->getRepository(MdvProductFile::class)->findOneById($item->getControlId());
  9599.                             if (!empty($productPro)) {
  9600.                                 $fileProducts[] = $productPro;
  9601.                             }
  9602.                             break;
  9603.                         case 'PAYMENT': // Pago
  9604.                             $paymentPro = $em->getRepository(MdvPaymentsClient::class)->findOneById($item->getControlId());
  9605.                             if (!empty($paymentPro)) {
  9606.                                 $payments[] = $paymentPro;
  9607.                             }
  9608.                             break;
  9609.                         case 'SERVICE': // Servicio
  9610.                             $servicePro = $em->getRepository(MdvServices::class)->findOneById($item->getControlId());
  9611.                             if (!empty($servicePro)) {
  9612.                                 $services[] = $servicePro;
  9613.                             }
  9614.                             break;
  9615.                         default:
  9616.                             break;
  9617.                     }
  9618.                 }
  9619.             }
  9620.         }
  9621.         $data_supplier = array();
  9622.         $i = 0;
  9623.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  9624.         $pax = '1';
  9625.         $qty = '1';
  9626.         $product = array(
  9627.             'neto' => 0,
  9628.             'sumSubT' => 0,
  9629.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  9630.         $service = array(
  9631.             'neto' => 0,
  9632.             'sumSubT' => 0,
  9633.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  9634.         foreach ($fileProducts as $item){
  9635.             $days = ((($item->getPrdDateEnd())->diff($item->getPrdDateStart()))->days + 1);
  9636.             $factorIva = $item->getPrdIva() === null ? 21 : $item->getPrdIva();
  9637.             $factorIva = $factorIva / 100;
  9638.             if (!is_null($item->getPrdQty()) or !empty($item->getPrdQty())){
  9639.                 $qty = $item->getPrdQty();
  9640.             }
  9641.             if (!empty($item->getPrdDays())) { $days = $item->getPrdDays(); }
  9642.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  9643.                 if (is_null($item->getPrdServicePrice()) or empty($item->getPrdServicePrice())){
  9644.                     $subtotal = 0;
  9645.                     $neto = 0;
  9646.                     $subtotalProduct = 0;
  9647.                 } else {
  9648.                     $subtotalProduct = $item->getPrdSubTotalPrice() * (1 + $factorIva);
  9649.                     $subneto = $item->getPrdSubTotalPrice();
  9650.                     $subtotal =  $subtotalProduct;
  9651.                     $neto =  $subneto;
  9652.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  9653.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  9654.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  9655.                     $arrayItems[] = array(
  9656.                         'type' => 'Product',
  9657.                         'name' => $item->getPrdName(),
  9658.                         'amount' => $neto,
  9659.                         'iva' => ($factorIva * 100),
  9660.                         'total' => $subtotal,
  9661.                     );
  9662.                 }
  9663.             } else {
  9664.                 // Es proforma
  9665.                 if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  9666.                     $subtotal = 0; $neto = 0; $subtotalProduct = 0;
  9667.                 } else {
  9668.                     $subtotalProduct = $item->getServicePrice() * (1 + $factorIva);
  9669.                     $subneto = $item->getServicePrice();
  9670.                     $subtotal =  $subtotalProduct;
  9671.                     $neto =  $subneto;
  9672.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  9673.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  9674.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  9675.                     $arrayItems[] = array(
  9676.                         'type' => 'Product',
  9677.                         'name' => $item->getProductName(),
  9678.                         'amount' => $neto,
  9679.                         'iva' => ($factorIva * 100),
  9680.                         'total' => $subtotal,
  9681.                     );
  9682.                 }
  9683.             }
  9684.             // Acumula netos totales e IVA
  9685.             $totales_neto_all = $totales_neto_all + $neto;
  9686.             switch ($factorIva){
  9687.                 case 0.21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * $factorIva); break;
  9688.                 case 0: $data_iva['ivaMontoCero'] = 0; break;
  9689.             }
  9690.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9691.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  9692.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  9693.             // Acumula netos totales e IVA
  9694.             $product['neto'] = $product['neto'] + $neto;
  9695.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  9696.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9697.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  9698.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  9699.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  9700.                 $data_supplier['product'][$i] = array(
  9701.                     'id' => $item->getId(),
  9702.                     'productName' => $item->getPrdName(),
  9703.                     'productId' => $item->getPrdProductId(),
  9704.                     'dateStart' => $item->getPrdDateStart(),
  9705.                     'dateEnd' => $item->getPrdDateEnd(),
  9706.                     'servicePrice' => $item->getPrdServicePrice(),
  9707.                     'serviceSubTotalPrice' => $subneto,     //antes de IVA
  9708.                     'subtotalProduct' => $subtotalProduct,
  9709.                     'iva' => ($factorIva * 100),
  9710.                     'pax' => $pax,
  9711.                     'qty' => $qty,
  9712.                     'days' => $days,
  9713.                     'type' => $item->getPrdType(),
  9714.                     'subtotal' => $subtotal,
  9715.                 );
  9716.             } else {
  9717.                 // Es proforma
  9718.                 $data_supplier['product'][$i] = array(
  9719.                     'id' => $item->getId(),
  9720.                     'productName' => $item->getProductName(),
  9721.                     'productId' => $item->getProductId(),
  9722.                     'dateStart' => $item->getDateStart(),
  9723.                     'dateEnd' => $item->getDateEnd(),
  9724.                     'servicePrice' => $item->getServicePrice(),
  9725.                     'subtotalProduct' => $subtotalProduct,
  9726.                     'iva' => ($factorIva * 100),
  9727.                     'pax' => $pax,
  9728.                     'qty' => $qty,
  9729.                     'days' => $days,
  9730.                     'type' => $item->getType(),
  9731.                     'subtotal' => $subtotal,
  9732.                 );
  9733.             }
  9734.             $i++;
  9735.         }
  9736.         $data_supplier['productSubTotal'] = array(
  9737.             'neto' => $product['neto'],
  9738.             'sumSubT' => $product['sumSubT'],
  9739.         );
  9740.         $payment = array(
  9741.             'sumSubT' => 0,
  9742.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  9743.         $i = 0;
  9744.         foreach ($payments as $item){
  9745.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  9746.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getPayAmountTotal();
  9747.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9748.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  9749.                 $data_supplier['payment'][$i] = array(
  9750.                     'id' => $item->getId(),
  9751.                     'amount' => $item->getPayAmount(),
  9752.                     'amountTotal' => $item->getPayAmountTotal(),
  9753.                     'vat' => $item->getPayVat(),
  9754.                     'datePayAt' => $item->getPayDatePayAt(),
  9755.                     'wayToPay' => $item->getPayWayToPay(),
  9756.                 );
  9757.                 $arrayItems[] = array(
  9758.                     'type' => 'Payment',
  9759.                     'name' => $item->getPayWayToPay(),
  9760.                     'amount' => $item->getPayAmount(),
  9761.                     'total' => $item->getPayAmountTotal(),
  9762.                     'iva' => $item->getPayVat(),
  9763.                 );
  9764.             } else {
  9765.                 // Es una proforma
  9766.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  9767.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  9768.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  9769.                 $data_supplier['payment'][$i] = array(
  9770.                     'id' => $item->getId(),
  9771.                     'amount' => $item->getAmount(),
  9772.                     'amountTotal' => $item->getAmountTotal(),
  9773.                     'vat' => $item->getVat(),
  9774.                     'datePayAt' => $item->getDatePayAt(),
  9775.                     'wayToPay' => $item->getWayToPay(),
  9776.                 );
  9777.                 $arrayItems[] = array(
  9778.                     'type' => 'Payment',
  9779.                     'name' => $item->getWayToPay(),
  9780.                     'amount' => $item->getAmount(),
  9781.                     'total' => $item->getAmountTotal(),
  9782.                     'iva' => $item->getVat(),
  9783.                 );
  9784.             }
  9785.             $i++;
  9786.         }
  9787.         if (!empty($payments)) {
  9788.             $data_supplier['paymentSubTotal'] = array(
  9789.                 'sumSubT' => $payment['sumSubT'],
  9790.             );
  9791.         }
  9792.         foreach ($services as $item){
  9793.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  9794.                 if (is_null($item->getSrvPrice()) or empty($item->getSrvPrice())) {
  9795.                     $subtotal = 0;
  9796.                     $neto = 0;
  9797.                     $subtotalService = 0;
  9798.                 } else {
  9799.                     $subtotalService = $item->getSrvPrice();
  9800.                     $subneto = $item->getSrvPrice();
  9801.                     // Commission
  9802.                     if ($item->getSrvOpCommission() == '1') {
  9803.                         $subtotalService = $subtotalService * (1 + ($item->getSrvCommission() / 100));
  9804.                         $subneto = $subneto * (1 + ($item->getSrvCommission() / 100));
  9805.                     } else {
  9806.                         $subtotalService = $subtotalService * (1 - ($item->getSrvCommission() / 100));
  9807.                         $subneto = $subneto * (1 - ($item->getSrvCommission() / 100));
  9808.                     }
  9809.                     // Over
  9810.                     if ($item->getSrvOpOver() == '1') {
  9811.                         $subtotalService = $subtotalService + $item->getSrvOver();
  9812.                         $subneto = $subneto + $item->getSrvOver();
  9813.                     } else {
  9814.                         $subtotalService = $subtotalService - $item->getSrvOver();
  9815.                         $subneto = $subneto - $item->getSrvOver();
  9816.                     }
  9817.                     // IVA
  9818.                     if ($item->getSrvOpIva() == '1') {
  9819.                         $subtotalService = $subtotalService * (1 + ($item->getSrvIva() / 100));
  9820.                     } else {
  9821.                         $subtotalService = $item->getSrvPrice();
  9822.                         $subneto = ($subneto * 100) / (100 + $item->getSrvIva());
  9823.                     }
  9824.                     switch ($item->getSrvServiceCatId()) {
  9825.                         case 1: // Alojamiento
  9826.                             // el numero de noches $numNoches; precio unitario $subneto
  9827.                             $numNoches = (($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days;
  9828.                             // La personas no afectan este calculo
  9829.                             $subtotal = $subtotalService * $numNoches * $item->getSrvUnits();
  9830.                             $subnetoUnit = $subneto;
  9831.                             $subneto = $subneto * $numNoches * $item->getSrvUnits();
  9832.                             $data_supplier['service'][$i] = array(
  9833.                                 'id' => $item->getId(),
  9834.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9835.                                 'serviceName' => $item->getSrvName(),
  9836.                                 'serviceType' => 'Hotel',
  9837.                                 'date' => ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y'),
  9838.                                 'qty' => $item->getSrvUnits(),
  9839.                                 'iva' => $item->getSrvIva(),
  9840.                                 'pax' => '-',
  9841.                                 'precioUnit' => $subnetoUnit,
  9842.                                 'subneto' => $subneto,
  9843.                                 'subtotal' => $subtotal,
  9844.                             );
  9845.                             break;
  9846.                         case 2: //Actividades
  9847.                             // El número de personas es considerado en el calculo
  9848.                             $pax = $item->getSrvPax();
  9849.                             if (empty($pax) or $pax == "0") {
  9850.                                 $pax = 1;
  9851.                             }
  9852.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  9853.                             if ($days > 1) {
  9854.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  9855.                             } else {
  9856.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  9857.                             }
  9858.                             $subtotal = $subtotalService * $days * $item->getSrvUnits();
  9859.                             $subnetoUnit = $subneto;
  9860.                             $subneto = $subneto * $days * $item->getSrvUnits();
  9861.                             $data_supplier['service'][$i] = array(
  9862.                                 'id' => $item->getId(),
  9863.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9864.                                 'serviceName' => $item->getSrvName(),
  9865.                                 'serviceType' => 'Actividad',
  9866.                                 'date' => $dateServ,
  9867.                                 'qty' => $item->getSrvUnits(),
  9868.                                 'iva' => $item->getSrvIva(),
  9869.                                 'pax' => $item->getSrvPax(),
  9870.                                 'precioUnit' => $subnetoUnit,
  9871.                                 'subneto' => $subneto,
  9872.                                 'subtotal' => $subtotal,
  9873.                             );
  9874.                             break;
  9875.                         case 3: // AV
  9876.                             $pax = $item->getSrvPax();
  9877.                             if (empty($pax) or $pax == "0") {
  9878.                                 $pax = 1;
  9879.                             }
  9880.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  9881.                             if ($days > 1) {
  9882.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  9883.                             } else {
  9884.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  9885.                             }
  9886.                             $unitsServ = $item->getSrvUnits();
  9887.                             if (empty($unitsServ) or $unitsServ == "0") {
  9888.                                 $unitsServ = 1;
  9889.                             }
  9890.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9891.                             $subnetoUnit = $subneto;
  9892.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9893.                             $data_supplier['service'][$i] = array(
  9894.                                 'id' => $item->getId(),
  9895.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9896.                                 'serviceName' => $item->getSrvName(),
  9897.                                 'serviceType' => 'AV',
  9898.                                 'date' => $dateServ,
  9899.                                 'qty' => $unitsServ,
  9900.                                 'iva' => $item->getSrvIva(),
  9901.                                 'pax' => $item->getSrvPax(),
  9902.                                 'precioUnit' => $subnetoUnit,
  9903.                                 'subneto' => $subneto,
  9904.                                 'subtotal' => $subtotal,
  9905.                             );
  9906.                             break;
  9907.                         case 4: //Creative
  9908.                             $pax = $item->getSrvPax();
  9909.                             if (empty($pax) or $pax == "0") {
  9910.                                 $pax = 1;
  9911.                             }
  9912.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  9913.                             if ($days > 1) {
  9914.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  9915.                             } else {
  9916.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  9917.                             }
  9918.                             $unitsServ = $item->getSrvUnits();
  9919.                             if (empty($unitsServ) or $unitsServ == "0") {
  9920.                                 $unitsServ = 1;
  9921.                             }
  9922.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9923.                             $subnetoUnit = $subneto;
  9924.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9925.                             $data_supplier['service'][$i] = array(
  9926.                                 'id' => $item->getId(),
  9927.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9928.                                 'serviceName' => $item->getSrvName(),
  9929.                                 'serviceType' => 'Creativo',
  9930.                                 'date' => $dateServ,
  9931.                                 'qty' => $unitsServ,
  9932.                                 'iva' => $item->getSrvIva(),
  9933.                                 'pax' => $item->getSrvPax(),
  9934.                                 'precioUnit' => $subnetoUnit,
  9935.                                 'subneto' => $subneto,
  9936.                                 'subtotal' => $subtotal,
  9937.                             );
  9938.                             break;
  9939.                         case 5: //Cruise
  9940.                             $pax = $item->getSrvPax();
  9941.                             if (empty($pax) or $pax == "0") {
  9942.                                 $pax = 1;
  9943.                             }
  9944.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days);
  9945.                             if ($days > 1) {
  9946.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  9947.                             } else {
  9948.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  9949.                             }
  9950.                             $unitsServ = $item->getSrvUnits();
  9951.                             if (empty($unitsServ) or $unitsServ == "0") {
  9952.                                 $unitsServ = 1;
  9953.                             }
  9954.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9955.                             $subnetoUnit = $subneto;
  9956.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9957.                             $data_supplier['service'][$i] = array(
  9958.                                 'id' => $item->getId(),
  9959.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9960.                                 'serviceName' => $item->getSrvName(),
  9961.                                 'serviceType' => 'Crucero',
  9962.                                 'date' => $dateServ,
  9963.                                 'qty' => $unitsServ,
  9964.                                 'iva' => $item->getSrvIva(),
  9965.                                 'pax' => $item->getSrvPax(),
  9966.                                 'precioUnit' => $subnetoUnit,
  9967.                                 'subneto' => $subneto,
  9968.                                 'subtotal' => $subtotal,
  9969.                             );
  9970.                             break;
  9971.                         case 6: //Entertaiment
  9972.                             $pax = $item->getSrvPax();
  9973.                             if (empty($pax) or $pax == "0") {
  9974.                                 $pax = 1;
  9975.                             }
  9976.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  9977.                             if ($days > 1) {
  9978.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  9979.                             } else {
  9980.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  9981.                             }
  9982.                             $unitsServ = $item->getSrvUnits();
  9983.                             if (empty($unitsServ) or $unitsServ == "0") {
  9984.                                 $unitsServ = 1;
  9985.                             }
  9986.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  9987.                             $subnetoUnit = $subneto;
  9988.                             $subneto = $subneto * $days * $unitsServ * $pax;
  9989.                             $data_supplier['service'][$i] = array(
  9990.                                 'id' => $item->getId(),
  9991.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  9992.                                 'serviceName' => $item->getSrvName(),
  9993.                                 'serviceType' => 'Entretenimiento',
  9994.                                 'date' => $dateServ,
  9995.                                 'qty' => $unitsServ,
  9996.                                 'iva' => $item->getSrvIva(),
  9997.                                 'pax' => $item->getSrvPax(),
  9998.                                 'precioUnit' => $subnetoUnit,
  9999.                                 'subneto' => $subneto,
  10000.                                 'subtotal' => $subtotal,
  10001.                             );
  10002.                             break;
  10003.                         case 7: // Gifts
  10004.                             $pax = $item->getSrvPax();
  10005.                             if (empty($pax) or $pax == "0") {
  10006.                                 $pax = 1;
  10007.                             }
  10008. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10009.                             $days = 1;
  10010.                             if ($days > 1) {
  10011.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10012.                             } else {
  10013.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10014.                             }
  10015.                             $unitsServ = $item->getSrvUnits();
  10016.                             if (empty($unitsServ) or $unitsServ == "0") {
  10017.                                 $unitsServ = 1;
  10018.                             }
  10019.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10020.                             $subnetoUnit = $subneto;
  10021.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10022.                             $data_supplier['service'][$i] = array(
  10023.                                 'id' => $item->getId(),
  10024.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10025.                                 'serviceName' => $item->getSrvName(),
  10026.                                 'serviceType' => 'Regalos',
  10027.                                 'date' => $dateServ,
  10028.                                 'qty' => $unitsServ,
  10029.                                 'iva' => $item->getSrvIva(),
  10030.                                 'pax' => $item->getSrvPax(),
  10031.                                 'precioUnit' => $subnetoUnit,
  10032.                                 'subneto' => $subneto,
  10033.                                 'subtotal' => $subtotal,
  10034.                             );
  10035.                             break;
  10036.                         case 8: //Guide
  10037.                             $pax = $item->getSrvPax();
  10038.                             if (empty($pax) or $pax == "0") {
  10039.                                 $pax = 1;
  10040.                             }
  10041.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10042.                             if ($days > 1) {
  10043.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10044.                             } else {
  10045.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10046.                             }
  10047.                             $unitsServ = $item->getSrvUnits();
  10048.                             if (empty($unitsServ) or $unitsServ == "0") {
  10049.                                 $unitsServ = 1;
  10050.                             }
  10051.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10052.                             $subnetoUnit = $subneto;
  10053.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10054.                             $data_supplier['service'][$i] = array(
  10055.                                 'id' => $item->getId(),
  10056.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10057.                                 'serviceName' => $item->getSrvName(),
  10058.                                 'serviceType' => 'Regalos',
  10059.                                 'date' => $dateServ,
  10060.                                 'qty' => $unitsServ,
  10061.                                 'iva' => $item->getSrvIva(),
  10062.                                 'pax' => $item->getSrvPax(),
  10063.                                 'precioUnit' => $subnetoUnit,
  10064.                                 'subneto' => $subneto,
  10065.                                 'subtotal' => $subtotal,
  10066.                             );
  10067.                             break;
  10068.                         case 9: //Itineraries
  10069.                             $pax = $item->getSrvPax();
  10070.                             if (empty($pax) or $pax == "0") {
  10071.                                 $pax = 1;
  10072.                             }
  10073.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10074.                             if ($days > 1) {
  10075.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10076.                             } else {
  10077.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10078.                             }
  10079.                             $unitsServ = $item->getSrvUnits();
  10080.                             if (empty($unitsServ) or $unitsServ == "0") {
  10081.                                 $unitsServ = 1;
  10082.                             }
  10083.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10084.                             $subnetoUnit = $subneto;
  10085.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10086.                             $data_supplier['service'][$i] = array(
  10087.                                 'id' => $item->getId(),
  10088.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10089.                                 'serviceName' => $item->getSrvName(),
  10090.                                 'serviceType' => 'Itinerarios',
  10091.                                 'date' => $dateServ,
  10092.                                 'qty' => $unitsServ,
  10093.                                 'iva' => $item->getSrvIva(),
  10094.                                 'pax' => $item->getSrvPax(),
  10095.                                 'precioUnit' => $subnetoUnit,
  10096.                                 'subneto' => $subneto,
  10097.                                 'subtotal' => $subtotal,
  10098.                             );
  10099.                             break;
  10100.                         case 10: //Lounge  -- No Aplica
  10101. //                        $pax = $item->getPax();
  10102. //                        if (empty($pax) or $pax == "0") {
  10103. //                            $pax = 1;
  10104. //                        }
  10105. //
  10106. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10107. //                        if ($days > 1){
  10108. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  10109. //                        } else {
  10110. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10111. //                        }
  10112. //
  10113. //                        $unitsServ = $item->getUnits();
  10114. //                        if (empty($unitsServ) or $unitsServ == "0") {
  10115. //                            $unitsServ = 1;
  10116. //                        }
  10117. //
  10118. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10119. //                        $subnetoUnit = $subneto;
  10120. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  10121. //
  10122. //                        $data_supplier['service'][$i] = array (
  10123. //                            'id' => $item->getId(),
  10124. //                            'serviceCatId' => $item->getServiceCatId(),
  10125. //                            'serviceName' => $item->getName(),
  10126. //                            'serviceType' => 'Itinerarios',
  10127. //                            'date' => $dateServ,
  10128. //                            'qty' => $unitsServ,
  10129. //                            'iva' => $item->getIva(),
  10130. //                            'pax' => $item->getPax(),
  10131. //                            'precioUnit' => $subnetoUnit,
  10132. //                            'subneto' => $subneto,
  10133. //                            'subtotal' => $subtotal,
  10134. //                        );
  10135.                             break;
  10136.                         case 11: //Menu
  10137.                             $pax = $item->getSrvPax();
  10138.                             if (empty($pax) or $pax == "0") {
  10139.                                 $pax = 1;
  10140.                             }
  10141.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10142.                             if ($days > 1) {
  10143.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10144.                             } else {
  10145.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10146.                             }
  10147.                             $unitsServ = $item->getSrvUnits();
  10148.                             if (empty($unitsServ) or $unitsServ == "0") {
  10149.                                 $unitsServ = 1;
  10150.                             }
  10151.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10152.                             $subnetoUnit = $subneto;
  10153.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10154.                             $data_supplier['service'][$i] = array(
  10155.                                 'id' => $item->getId(),
  10156.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10157.                                 'serviceName' => $item->getSrvName(),
  10158.                                 'serviceType' => 'Menú',
  10159.                                 'date' => $dateServ,
  10160.                                 'qty' => $unitsServ,
  10161.                                 'iva' => $item->getSrvIva(),
  10162.                                 'pax' => $item->getSrvPax(),
  10163.                                 'precioUnit' => $subnetoUnit,
  10164.                                 'subneto' => $subneto,
  10165.                                 'subtotal' => $subtotal,
  10166.                             );
  10167.                             break;
  10168.                         case 12: //Others
  10169.                             $pax = $item->getSrvPax();
  10170.                             if (empty($pax) or $pax == "0") {
  10171.                                 $pax = 1;
  10172.                             }
  10173.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10174.                             if ($days > 1) {
  10175.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10176.                             } else {
  10177.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10178.                             }
  10179.                             $unitsServ = $item->getSrvUnits();
  10180.                             if (empty($unitsServ) or $unitsServ == "0") {
  10181.                                 $unitsServ = 1;
  10182.                             }
  10183.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10184.                             $subnetoUnit = $subneto;
  10185.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10186.                             $data_supplier['service'][$i] = array(
  10187.                                 'id' => $item->getId(),
  10188.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10189.                                 'serviceName' => $item->getSrvName(),
  10190.                                 'serviceType' => 'Otros',
  10191.                                 'date' => $dateServ,
  10192.                                 'qty' => $unitsServ,
  10193.                                 'iva' => $item->getSrvIva(),
  10194.                                 'pax' => $item->getSrvPax(),
  10195.                                 'precioUnit' => $subnetoUnit,
  10196.                                 'subneto' => $subneto,
  10197.                                 'subtotal' => $subtotal,
  10198.                             );
  10199.                             break;
  10200.                         case 13: //Transport
  10201.                             $pax = $item->getSrvPax();
  10202.                             if (empty($pax) or $pax == "0") {
  10203.                                 $pax = 1;
  10204.                             }
  10205.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10206.                             if ($days > 1) {
  10207.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10208.                             } else {
  10209.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10210.                             }
  10211.                             $unitsServ = $item->getSrvUnits();
  10212.                             if (empty($unitsServ) or $unitsServ == "0") {
  10213.                                 $unitsServ = 1;
  10214.                             }
  10215.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10216.                             $subnetoUnit = $subneto;
  10217.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10218.                             $data_supplier['service'][$i] = array(
  10219.                                 'id' => $item->getId(),
  10220.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10221.                                 'serviceName' => $item->getSrvName(),
  10222.                                 'serviceType' => 'Transporte',
  10223.                                 'date' => $dateServ,
  10224.                                 'qty' => $unitsServ,
  10225.                                 'iva' => $item->getSrvIva(),
  10226.                                 'pax' => $item->getSrvPax(),
  10227.                                 'precioUnit' => $subnetoUnit,
  10228.                                 'subneto' => $subneto,
  10229.                                 'subtotal' => $subtotal,
  10230.                             );
  10231.                             break;
  10232.                         case 14: //Technology
  10233.                             $pax = $item->getSrvPax();
  10234.                             if (empty($pax) or $pax == "0") {
  10235.                                 $pax = 1;
  10236.                             }
  10237. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10238.                             $days = 1;
  10239. //                        if ($days > 1){
  10240. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  10241. //                        } else {
  10242. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10243. //                        }
  10244.                             $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10245.                             $unitsServ = $item->getSrvUnits();
  10246.                             if (empty($unitsServ) or $unitsServ == "0") {
  10247.                                 $unitsServ = 1;
  10248.                             }
  10249.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10250.                             $subnetoUnit = $subneto;
  10251.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10252.                             $data_supplier['service'][$i] = array(
  10253.                                 'id' => $item->getId(),
  10254.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10255.                                 'serviceName' => $item->getSrvName(),
  10256.                                 'serviceType' => 'Tecnología',
  10257.                                 'date' => $dateServ,
  10258.                                 'qty' => $unitsServ,
  10259.                                 'iva' => $item->getSrvIva(),
  10260.                                 'pax' => $item->getSrvPax(),
  10261.                                 'precioUnit' => $subnetoUnit,
  10262.                                 'subneto' => $subneto,
  10263.                                 'subtotal' => $subtotal,
  10264.                             );
  10265.                             break;
  10266.                         case 15: //Assisstant
  10267.                             $pax = $item->getSrvPax();
  10268.                             if (empty($pax) or $pax == "0") {
  10269.                                 $pax = 1;
  10270.                             }
  10271.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10272.                             if ($days > 1) {
  10273.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10274.                             } else {
  10275.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10276.                             }
  10277.                             $unitsServ = $item->getSrvUnits();
  10278.                             if (empty($unitsServ) or $unitsServ == "0") {
  10279.                                 $unitsServ = 1;
  10280.                             }
  10281.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10282.                             $subnetoUnit = $subneto;
  10283.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10284.                             $data_supplier['service'][$i] = array(
  10285.                                 'id' => $item->getId(),
  10286.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10287.                                 'serviceName' => $item->getSrvName(),
  10288.                                 'serviceType' => 'Asistente',
  10289.                                 'date' => $dateServ,
  10290.                                 'qty' => $unitsServ,
  10291.                                 'iva' => $item->getSrvIva(),
  10292.                                 'pax' => $item->getSrvPax(),
  10293.                                 'precioUnit' => $subnetoUnit,
  10294.                                 'subneto' => $subneto,
  10295.                                 'subtotal' => $subtotal,
  10296.                             );
  10297.                             break;
  10298.                         case 16: //DDR
  10299.                             $pax = $item->getSrvPax();
  10300.                             if (empty($pax) or $pax == "0") {
  10301.                                 $pax = 1;
  10302.                             }
  10303.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  10304.                             if ($days > 1) {
  10305.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  10306.                             } else {
  10307.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  10308.                             }
  10309.                             $unitsServ = $item->getSrvUnits();
  10310.                             if (empty($unitsServ) or $unitsServ == "0") {
  10311.                                 $unitsServ = 1;
  10312.                             }
  10313.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10314.                             $subnetoUnit = $subneto;
  10315.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10316.                             $data_supplier['service'][$i] = array(
  10317.                                 'id' => $item->getId(),
  10318.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  10319.                                 'serviceName' => $item->getSrvName(),
  10320.                                 'serviceType' => 'DDR',
  10321.                                 'date' => $dateServ,
  10322.                                 'qty' => $unitsServ,
  10323.                                 'iva' => $item->getSrvIva(),
  10324.                                 'pax' => $item->getSrvPax(),
  10325.                                 'precioUnit' => $subnetoUnit,
  10326.                                 'subneto' => $subneto,
  10327.                                 'subtotal' => $subtotal,
  10328.                             );
  10329.                             break;
  10330.                         default:
  10331.                             break;
  10332.                     }
  10333.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  10334.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  10335.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  10336.                     $arrayItems[] = array(
  10337.                         'type' => 'Service',
  10338.                         'name' => $item->getSrvName(),
  10339.                         'amount' => $neto,
  10340.                         'iva' => $item->getSrvIva(),
  10341.                         'total' => $subtotal,
  10342.                     );
  10343.                 }
  10344.                 switch ($item->getSrvIva()) {
  10345.                     // Acumula IVA
  10346.                     case 21:
  10347.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getSrvIva() / 100));
  10348.                         break;
  10349.                     case 10:
  10350.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getSrvIva() / 100));
  10351.                         break;
  10352.                     case 0:
  10353.                         break;
  10354.                     default:
  10355.                         break;
  10356.                 }
  10357.             } else {
  10358.                 // Es una proforma
  10359.                 if (is_null($item->getPrice()) or empty($item->getPrice())) {
  10360.                     $subtotal = 0;
  10361.                     $neto = 0;
  10362.                     $subtotalService = 0;
  10363.                 } else {
  10364.                     $subtotalService = $item->getPrice();
  10365.                     $subneto = $item->getPrice();
  10366.                     // Commission
  10367.                     if ($item->getOpCommission() == '1') {
  10368.                         $subtotalService = $subtotalService * (1 + ($item->getCommission() / 100));
  10369.                         $subneto = $subneto * (1 + ($item->getCommission() / 100));
  10370.                     } else {
  10371.                         $subtotalService = $subtotalService * (1 - ($item->getCommission() / 100));
  10372.                         $subneto = $subneto * (1 - ($item->getCommission() / 100));
  10373.                     }
  10374.                     // Over
  10375.                     if ($item->getOpOver() == '1') {
  10376.                         $subtotalService = $subtotalService + $item->getOver();
  10377.                         $subneto = $subneto + $item->getOver();
  10378.                     } else {
  10379.                         $subtotalService = $subtotalService - $item->getOver();
  10380.                         $subneto = $subneto - $item->getOver();
  10381.                     }
  10382.                     // IVA
  10383.                     if ($item->getOpIva() == '1') {
  10384.                         $subtotalService = $subtotalService * (1 + ($item->getIva() / 100));
  10385.                     } else {
  10386.                         $subtotalService = $item->getPrice();
  10387.                         $subneto = ($subneto * 100) / (100 + $item->getIva());
  10388.                     }
  10389.                     switch ($item->getServiceCatId()) {
  10390.                         case 1: // Alojamiento
  10391.                             // el numero de noches $numNoches; precio unitario $subneto
  10392.                             $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  10393.                             // La personas no afectan este calculo
  10394.                             $subtotal = $subtotalService * $numNoches * $item->getUnits();
  10395.                             $subnetoUnit = $subneto;
  10396.                             $subneto = $subneto * $numNoches * $item->getUnits();
  10397.                             $data_supplier['service'][$i] = array(
  10398.                                 'id' => $item->getId(),
  10399.                                 'serviceCatId' => $item->getServiceCatId(),
  10400.                                 'serviceName' => $item->getName(),
  10401.                                 'serviceType' => 'Hotel',
  10402.                                 'date' => ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y'),
  10403.                                 'qty' => $item->getUnits(),
  10404.                                 'iva' => $item->getIva(),
  10405.                                 'pax' => '-',
  10406.                                 'precioUnit' => $subnetoUnit,
  10407.                                 'subneto' => $subneto,
  10408.                                 'subtotal' => $subtotal,
  10409.                             );
  10410.                             break;
  10411.                         case 2: //Actividades
  10412.                             // El número de personas es considerado en el calculo
  10413.                             $pax = $item->getPax();
  10414.                             if (empty($pax) or $pax == "0") {
  10415.                                 $pax = 1;
  10416.                             }
  10417.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10418.                             if ($days > 1) {
  10419.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10420.                             } else {
  10421.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10422.                             }
  10423.                             $subtotal = $subtotalService * $days * $item->getUnits();
  10424.                             $subnetoUnit = $subneto;
  10425.                             $subneto = $subneto * $days * $item->getUnits();
  10426.                             $data_supplier['service'][$i] = array(
  10427.                                 'id' => $item->getId(),
  10428.                                 'serviceCatId' => $item->getServiceCatId(),
  10429.                                 'serviceName' => $item->getName(),
  10430.                                 'serviceType' => 'Actividad',
  10431.                                 'date' => $dateServ,
  10432.                                 'qty' => $item->getUnits(),
  10433.                                 'iva' => $item->getIva(),
  10434.                                 'pax' => $item->getPax(),
  10435.                                 'precioUnit' => $subnetoUnit,
  10436.                                 'subneto' => $subneto,
  10437.                                 'subtotal' => $subtotal,
  10438.                             );
  10439.                             break;
  10440.                         case 3: // AV
  10441.                             $pax = $item->getPax();
  10442.                             if (empty($pax) or $pax == "0") {
  10443.                                 $pax = 1;
  10444.                             }
  10445.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10446.                             if ($days > 1) {
  10447.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10448.                             } else {
  10449.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10450.                             }
  10451.                             $unitsServ = $item->getUnits();
  10452.                             if (empty($unitsServ) or $unitsServ == "0") {
  10453.                                 $unitsServ = 1;
  10454.                             }
  10455.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10456.                             $subnetoUnit = $subneto;
  10457.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10458.                             $data_supplier['service'][$i] = array(
  10459.                                 'id' => $item->getId(),
  10460.                                 'serviceCatId' => $item->getServiceCatId(),
  10461.                                 'serviceName' => $item->getName(),
  10462.                                 'serviceType' => 'AV',
  10463.                                 'date' => $dateServ,
  10464.                                 'qty' => $unitsServ,
  10465.                                 'iva' => $item->getIva(),
  10466.                                 'pax' => $item->getPax(),
  10467.                                 'precioUnit' => $subnetoUnit,
  10468.                                 'subneto' => $subneto,
  10469.                                 'subtotal' => $subtotal,
  10470.                             );
  10471.                             break;
  10472.                         case 4: //Creative
  10473.                             $pax = $item->getPax();
  10474.                             if (empty($pax) or $pax == "0") {
  10475.                                 $pax = 1;
  10476.                             }
  10477.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10478.                             if ($days > 1) {
  10479.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10480.                             } else {
  10481.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10482.                             }
  10483.                             $unitsServ = $item->getUnits();
  10484.                             if (empty($unitsServ) or $unitsServ == "0") {
  10485.                                 $unitsServ = 1;
  10486.                             }
  10487.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10488.                             $subnetoUnit = $subneto;
  10489.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10490.                             $data_supplier['service'][$i] = array(
  10491.                                 'id' => $item->getId(),
  10492.                                 'serviceCatId' => $item->getServiceCatId(),
  10493.                                 'serviceName' => $item->getName(),
  10494.                                 'serviceType' => 'Creativo',
  10495.                                 'date' => $dateServ,
  10496.                                 'qty' => $unitsServ,
  10497.                                 'iva' => $item->getIva(),
  10498.                                 'pax' => $item->getPax(),
  10499.                                 'precioUnit' => $subnetoUnit,
  10500.                                 'subneto' => $subneto,
  10501.                                 'subtotal' => $subtotal,
  10502.                             );
  10503.                             break;
  10504.                         case 5: //Cruise
  10505.                             $pax = $item->getPax();
  10506.                             if (empty($pax) or $pax == "0") {
  10507.                                 $pax = 1;
  10508.                             }
  10509.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  10510.                             if ($days > 1) {
  10511.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10512.                             } else {
  10513.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10514.                             }
  10515.                             $unitsServ = $item->getUnits();
  10516.                             if (empty($unitsServ) or $unitsServ == "0") {
  10517.                                 $unitsServ = 1;
  10518.                             }
  10519.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10520.                             $subnetoUnit = $subneto;
  10521.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10522.                             $data_supplier['service'][$i] = array(
  10523.                                 'id' => $item->getId(),
  10524.                                 'serviceCatId' => $item->getServiceCatId(),
  10525.                                 'serviceName' => $item->getName(),
  10526.                                 'serviceType' => 'Crucero',
  10527.                                 'date' => $dateServ,
  10528.                                 'qty' => $unitsServ,
  10529.                                 'iva' => $item->getIva(),
  10530.                                 'pax' => $item->getPax(),
  10531.                                 'precioUnit' => $subnetoUnit,
  10532.                                 'subneto' => $subneto,
  10533.                                 'subtotal' => $subtotal,
  10534.                             );
  10535.                             break;
  10536.                         case 6: //Entertaiment
  10537.                             $pax = $item->getPax();
  10538.                             if (empty($pax) or $pax == "0") {
  10539.                                 $pax = 1;
  10540.                             }
  10541.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10542.                             if ($days > 1) {
  10543.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10544.                             } else {
  10545.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10546.                             }
  10547.                             $unitsServ = $item->getUnits();
  10548.                             if (empty($unitsServ) or $unitsServ == "0") {
  10549.                                 $unitsServ = 1;
  10550.                             }
  10551.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10552.                             $subnetoUnit = $subneto;
  10553.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10554.                             $data_supplier['service'][$i] = array(
  10555.                                 'id' => $item->getId(),
  10556.                                 'serviceCatId' => $item->getServiceCatId(),
  10557.                                 'serviceName' => $item->getName(),
  10558.                                 'serviceType' => 'Entretenimiento',
  10559.                                 'date' => $dateServ,
  10560.                                 'qty' => $unitsServ,
  10561.                                 'iva' => $item->getIva(),
  10562.                                 'pax' => $item->getPax(),
  10563.                                 'precioUnit' => $subnetoUnit,
  10564.                                 'subneto' => $subneto,
  10565.                                 'subtotal' => $subtotal,
  10566.                             );
  10567.                             break;
  10568.                         case 7: // Gifts
  10569.                             $pax = $item->getPax();
  10570.                             if (empty($pax) or $pax == "0") {
  10571.                                 $pax = 1;
  10572.                             }
  10573. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10574.                             $days = 1;
  10575.                             if ($days > 1) {
  10576.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10577.                             } else {
  10578.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10579.                             }
  10580.                             $unitsServ = $item->getUnits();
  10581.                             if (empty($unitsServ) or $unitsServ == "0") {
  10582.                                 $unitsServ = 1;
  10583.                             }
  10584.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10585.                             $subnetoUnit = $subneto;
  10586.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10587.                             $data_supplier['service'][$i] = array(
  10588.                                 'id' => $item->getId(),
  10589.                                 'serviceCatId' => $item->getServiceCatId(),
  10590.                                 'serviceName' => $item->getName(),
  10591.                                 'serviceType' => 'Regalos',
  10592.                                 'date' => $dateServ,
  10593.                                 'qty' => $unitsServ,
  10594.                                 'iva' => $item->getIva(),
  10595.                                 'pax' => $item->getPax(),
  10596.                                 'precioUnit' => $subnetoUnit,
  10597.                                 'subneto' => $subneto,
  10598.                                 'subtotal' => $subtotal,
  10599.                             );
  10600.                             break;
  10601.                         case 8: //Guide
  10602.                             $pax = $item->getPax();
  10603.                             if (empty($pax) or $pax == "0") {
  10604.                                 $pax = 1;
  10605.                             }
  10606.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10607.                             if ($days > 1) {
  10608.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10609.                             } else {
  10610.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10611.                             }
  10612.                             $unitsServ = $item->getUnits();
  10613.                             if (empty($unitsServ) or $unitsServ == "0") {
  10614.                                 $unitsServ = 1;
  10615.                             }
  10616.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10617.                             $subnetoUnit = $subneto;
  10618.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10619.                             $data_supplier['service'][$i] = array(
  10620.                                 'id' => $item->getId(),
  10621.                                 'serviceCatId' => $item->getServiceCatId(),
  10622.                                 'serviceName' => $item->getName(),
  10623.                                 'serviceType' => 'Regalos',
  10624.                                 'date' => $dateServ,
  10625.                                 'qty' => $unitsServ,
  10626.                                 'iva' => $item->getIva(),
  10627.                                 'pax' => $item->getPax(),
  10628.                                 'precioUnit' => $subnetoUnit,
  10629.                                 'subneto' => $subneto,
  10630.                                 'subtotal' => $subtotal,
  10631.                             );
  10632.                             break;
  10633.                         case 9: //Itineraries
  10634.                             $pax = $item->getPax();
  10635.                             if (empty($pax) or $pax == "0") {
  10636.                                 $pax = 1;
  10637.                             }
  10638.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10639.                             if ($days > 1) {
  10640.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10641.                             } else {
  10642.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10643.                             }
  10644.                             $unitsServ = $item->getUnits();
  10645.                             if (empty($unitsServ) or $unitsServ == "0") {
  10646.                                 $unitsServ = 1;
  10647.                             }
  10648.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10649.                             $subnetoUnit = $subneto;
  10650.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10651.                             $data_supplier['service'][$i] = array(
  10652.                                 'id' => $item->getId(),
  10653.                                 'serviceCatId' => $item->getServiceCatId(),
  10654.                                 'serviceName' => $item->getName(),
  10655.                                 'serviceType' => 'Itinerarios',
  10656.                                 'date' => $dateServ,
  10657.                                 'qty' => $unitsServ,
  10658.                                 'iva' => $item->getIva(),
  10659.                                 'pax' => $item->getPax(),
  10660.                                 'precioUnit' => $subnetoUnit,
  10661.                                 'subneto' => $subneto,
  10662.                                 'subtotal' => $subtotal,
  10663.                             );
  10664.                             break;
  10665.                         case 10: //Lounge  -- No Aplica
  10666. //                        $pax = $item->getPax();
  10667. //                        if (empty($pax) or $pax == "0") {
  10668. //                            $pax = 1;
  10669. //                        }
  10670. //
  10671. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10672. //                        if ($days > 1){
  10673. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  10674. //                        } else {
  10675. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10676. //                        }
  10677. //
  10678. //                        $unitsServ = $item->getUnits();
  10679. //                        if (empty($unitsServ) or $unitsServ == "0") {
  10680. //                            $unitsServ = 1;
  10681. //                        }
  10682. //
  10683. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10684. //                        $subnetoUnit = $subneto;
  10685. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  10686. //
  10687. //                        $data_supplier['service'][$i] = array (
  10688. //                            'id' => $item->getId(),
  10689. //                            'serviceCatId' => $item->getServiceCatId(),
  10690. //                            'serviceName' => $item->getName(),
  10691. //                            'serviceType' => 'Itinerarios',
  10692. //                            'date' => $dateServ,
  10693. //                            'qty' => $unitsServ,
  10694. //                            'iva' => $item->getIva(),
  10695. //                            'pax' => $item->getPax(),
  10696. //                            'precioUnit' => $subnetoUnit,
  10697. //                            'subneto' => $subneto,
  10698. //                            'subtotal' => $subtotal,
  10699. //                        );
  10700.                             break;
  10701.                         case 11: //Menu
  10702.                             $pax = $item->getPax();
  10703.                             if (empty($pax) or $pax == "0") {
  10704.                                 $pax = 1;
  10705.                             }
  10706.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10707.                             if ($days > 1) {
  10708.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10709.                             } else {
  10710.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10711.                             }
  10712.                             $unitsServ = $item->getUnits();
  10713.                             if (empty($unitsServ) or $unitsServ == "0") {
  10714.                                 $unitsServ = 1;
  10715.                             }
  10716.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10717.                             $subnetoUnit = $subneto;
  10718.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10719.                             $data_supplier['service'][$i] = array(
  10720.                                 'id' => $item->getId(),
  10721.                                 'serviceCatId' => $item->getServiceCatId(),
  10722.                                 'serviceName' => $item->getName(),
  10723.                                 'serviceType' => 'Menú',
  10724.                                 'date' => $dateServ,
  10725.                                 'qty' => $unitsServ,
  10726.                                 'iva' => $item->getIva(),
  10727.                                 'pax' => $item->getPax(),
  10728.                                 'precioUnit' => $subnetoUnit,
  10729.                                 'subneto' => $subneto,
  10730.                                 'subtotal' => $subtotal,
  10731.                             );
  10732.                             break;
  10733.                         case 12: //Others
  10734.                             $pax = $item->getPax();
  10735.                             if (empty($pax) or $pax == "0") {
  10736.                                 $pax = 1;
  10737.                             }
  10738.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10739.                             if ($days > 1) {
  10740.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10741.                             } else {
  10742.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10743.                             }
  10744.                             $unitsServ = $item->getUnits();
  10745.                             if (empty($unitsServ) or $unitsServ == "0") {
  10746.                                 $unitsServ = 1;
  10747.                             }
  10748.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10749.                             $subnetoUnit = $subneto;
  10750.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10751.                             $data_supplier['service'][$i] = array(
  10752.                                 'id' => $item->getId(),
  10753.                                 'serviceCatId' => $item->getServiceCatId(),
  10754.                                 'serviceName' => $item->getName(),
  10755.                                 'serviceType' => 'Otros',
  10756.                                 'date' => $dateServ,
  10757.                                 'qty' => $unitsServ,
  10758.                                 'iva' => $item->getIva(),
  10759.                                 'pax' => $item->getPax(),
  10760.                                 'precioUnit' => $subnetoUnit,
  10761.                                 'subneto' => $subneto,
  10762.                                 'subtotal' => $subtotal,
  10763.                             );
  10764.                             break;
  10765.                         case 13: //Transport
  10766.                             $pax = $item->getPax();
  10767.                             if (empty($pax) or $pax == "0") {
  10768.                                 $pax = 1;
  10769.                             }
  10770.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10771.                             if ($days > 1) {
  10772.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10773.                             } else {
  10774.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10775.                             }
  10776.                             $unitsServ = $item->getUnits();
  10777.                             if (empty($unitsServ) or $unitsServ == "0") {
  10778.                                 $unitsServ = 1;
  10779.                             }
  10780.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10781.                             $subnetoUnit = $subneto;
  10782.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10783.                             $data_supplier['service'][$i] = array(
  10784.                                 'id' => $item->getId(),
  10785.                                 'serviceCatId' => $item->getServiceCatId(),
  10786.                                 'serviceName' => $item->getName(),
  10787.                                 'serviceType' => 'Transporte',
  10788.                                 'date' => $dateServ,
  10789.                                 'qty' => $unitsServ,
  10790.                                 'iva' => $item->getIva(),
  10791.                                 'pax' => $item->getPax(),
  10792.                                 'precioUnit' => $subnetoUnit,
  10793.                                 'subneto' => $subneto,
  10794.                                 'subtotal' => $subtotal,
  10795.                             );
  10796.                             break;
  10797.                         case 14: //Technology
  10798.                             $pax = $item->getPax();
  10799.                             if (empty($pax) or $pax == "0") {
  10800.                                 $pax = 1;
  10801.                             }
  10802. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10803.                             $days = 1;
  10804. //                        if ($days > 1){
  10805. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  10806. //                        } else {
  10807. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10808. //                        }
  10809.                             $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10810.                             $unitsServ = $item->getUnits();
  10811.                             if (empty($unitsServ) or $unitsServ == "0") {
  10812.                                 $unitsServ = 1;
  10813.                             }
  10814.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10815.                             $subnetoUnit = $subneto;
  10816.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10817.                             $data_supplier['service'][$i] = array(
  10818.                                 'id' => $item->getId(),
  10819.                                 'serviceCatId' => $item->getServiceCatId(),
  10820.                                 'serviceName' => $item->getName(),
  10821.                                 'serviceType' => 'Tecnología',
  10822.                                 'date' => $dateServ,
  10823.                                 'qty' => $unitsServ,
  10824.                                 'iva' => $item->getIva(),
  10825.                                 'pax' => $item->getPax(),
  10826.                                 'precioUnit' => $subnetoUnit,
  10827.                                 'subneto' => $subneto,
  10828.                                 'subtotal' => $subtotal,
  10829.                             );
  10830.                             break;
  10831.                         case 15: //Assisstant
  10832.                             $pax = $item->getPax();
  10833.                             if (empty($pax) or $pax == "0") {
  10834.                                 $pax = 1;
  10835.                             }
  10836.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10837.                             if ($days > 1) {
  10838.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10839.                             } else {
  10840.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10841.                             }
  10842.                             $unitsServ = $item->getUnits();
  10843.                             if (empty($unitsServ) or $unitsServ == "0") {
  10844.                                 $unitsServ = 1;
  10845.                             }
  10846.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10847.                             $subnetoUnit = $subneto;
  10848.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10849.                             $data_supplier['service'][$i] = array(
  10850.                                 'id' => $item->getId(),
  10851.                                 'serviceCatId' => $item->getServiceCatId(),
  10852.                                 'serviceName' => $item->getName(),
  10853.                                 'serviceType' => 'Asistente',
  10854.                                 'date' => $dateServ,
  10855.                                 'qty' => $unitsServ,
  10856.                                 'iva' => $item->getIva(),
  10857.                                 'pax' => $item->getPax(),
  10858.                                 'precioUnit' => $subnetoUnit,
  10859.                                 'subneto' => $subneto,
  10860.                                 'subtotal' => $subtotal,
  10861.                             );
  10862.                             break;
  10863.                         case 16: //DDR
  10864.                             $pax = $item->getPax();
  10865.                             if (empty($pax) or $pax == "0") {
  10866.                                 $pax = 1;
  10867.                             }
  10868.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  10869.                             if ($days > 1) {
  10870.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  10871.                             } else {
  10872.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  10873.                             }
  10874.                             $unitsServ = $item->getUnits();
  10875.                             if (empty($unitsServ) or $unitsServ == "0") {
  10876.                                 $unitsServ = 1;
  10877.                             }
  10878.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  10879.                             $subnetoUnit = $subneto;
  10880.                             $subneto = $subneto * $days * $unitsServ * $pax;
  10881.                             $data_supplier['service'][$i] = array(
  10882.                                 'id' => $item->getId(),
  10883.                                 'serviceCatId' => $item->getServiceCatId(),
  10884.                                 'serviceName' => $item->getName(),
  10885.                                 'serviceType' => 'DDR',
  10886.                                 'date' => $dateServ,
  10887.                                 'qty' => $unitsServ,
  10888.                                 'iva' => $item->getIva(),
  10889.                                 'pax' => $item->getPax(),
  10890.                                 'precioUnit' => $subnetoUnit,
  10891.                                 'subneto' => $subneto,
  10892.                                 'subtotal' => $subtotal,
  10893.                             );
  10894.                             break;
  10895.                         default:
  10896.                             break;
  10897.                     }
  10898.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  10899.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  10900.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  10901.                     $arrayItems[] = array(
  10902.                         'type' => 'Service',
  10903.                         'name' => $item->getName(),
  10904.                         'amount' => $neto,
  10905.                         'iva' => $item->getIva(),
  10906.                         'total' => $subtotal,
  10907.                     );
  10908.                 }
  10909.                 switch ($item->getIva()) {
  10910.                     // Acumula IVA
  10911.                     case 21:
  10912.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva() / 100));
  10913.                         break;
  10914.                     case 10:
  10915.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva() / 100));
  10916.                         break;
  10917.                     case 0:
  10918.                         break;
  10919.                     default:
  10920.                         break;
  10921.                 }
  10922.             }
  10923.             $totales_neto_all = $totales_neto_all + $neto;
  10924.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  10925.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  10926.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  10927.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  10928.             // Acumula netos totales e IVA
  10929.             $service['neto'] = $service['neto'] + $neto;
  10930.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  10931.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  10932.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  10933.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  10934.             $i++;
  10935.         }
  10936.         $data_supplier['serviceSubTotal'] = array(
  10937.             'neto' => $service['neto'],
  10938.             'sumSubT' => $service['sumSubT'],
  10939.         );
  10940.         $currency = '€';
  10941.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  10942.         if (!empty($payments)) {
  10943.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  10944.         } else {
  10945.             $amount_pay = 0;
  10946.         }
  10947.         $totales_all = $totales_total - $amount_pay;
  10948.         if (empty($dateDocument)){
  10949.             $dateDocument = new DateTime('now');
  10950.         }
  10951.         $data = array(
  10952.             'id' => $fileId,
  10953.             'type' => $type,
  10954.             'number' => $number,
  10955.             'prefix' => 'MDV-'.(new DateTime('now'))->format('dmy').'-'.$invoiceId,
  10956.             'date' => $dateDocument,
  10957.             'file' => $file,
  10958.             'token' => $file->getAccessKey(),
  10959.             'company' => $company,
  10960.             'clients' => $client,
  10961.             'arrayItems' => $arrayItems,
  10962.             'datasupplier' => $data_supplier,
  10963.             'currency' => $currency,
  10964.             'totales_neto' => $totales_neto_all,
  10965.             'bases_imponibles' => $data_iva,
  10966.             'totales' => $totales_total,
  10967.             'balance' => $totales_all,
  10968.             'paymentInvoice' => $amount_pay,
  10969.         );
  10970.         return $data;
  10971.     }
  10972.     private function baseInvoiceDoneFileTwo($fileId, $invoiceId, $typeBase)
  10973.     {
  10974.         $em = $this->getDoctrine()->getManager();
  10975.         $file = $em->getRepository(MdvFiles::class)->findOneById($fileId);
  10976.         $dateDocument = null;                                                                           //Fecha final del documento
  10977.         $subneto = 0;
  10978.         if (($typeBase == 'I') or ($typeBase == 'P')) {
  10979.             if ($typeBase == 'I') {
  10980.                 $items = $em->getRepository(MdvDocInvoiceItems::class)->findBy(array('fileId' => $fileId, 'invoiceId' => $invoiceId));
  10981.             } else {
  10982.                 // Es factura ni proforma
  10983.                 $items = $em->getRepository(MdvDocProformaItems::class)->findBy(array('fileId' => $fileId, 'proformaId' => $invoiceId));
  10984.             }
  10985.         } else {
  10986.             // No es factura ni proforma, es una rectificativa
  10987.             $items = $em->getRepository(MdvDocInvoiceRecItems::class)->findBy(array('fileId' => $fileId, 'invoiceRecId' => $invoiceId));
  10988.         }
  10989.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('3');
  10990.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  10991.         if (empty($client)){
  10992.             $client[0] = new Client();
  10993.             $client[0]->setName('');
  10994.             $client[0]->setTitle('');
  10995.             $client[0]->setIdDocument('');
  10996.             $client[0]->setPopulation('');
  10997.             $client[0]->setRegion('');
  10998.             $client[0]->setCountry('');
  10999.         } else {
  11000.             if (is_numeric($client[0]->getPopulation())){
  11001.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  11002.                 $client[0]->setPopulation($city->getCity());
  11003.             }
  11004.             if (is_numeric($client[0]->getRegion())){
  11005.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  11006.                 $client[0]->setRegion($region->getRegion());
  11007.             }
  11008.             if (is_numeric($client[0]->getCountry())){
  11009.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  11010.                 $client[0]->setCountry($country->getCountry());
  11011.             }
  11012.         }
  11013.         // Acumuladores de los calculos
  11014.         $totales_neto_all = 0;
  11015.         $data_iva = array(
  11016.             'iva' => 21,
  11017.             'ivaMontoVeintiUno' => 0,
  11018.             'ivaMontoDiez' => 0,
  11019.             'ivaMontoCero' => 0,
  11020.         );
  11021.         //INICIO: Determinamos el numero de factura o proforma
  11022.         if ($typeBase == 'I'){
  11023.             // Es una Factura
  11024.             $number = $em->getRepository(MdvDocInvoice::class)->findOneById($invoiceId);
  11025.             $dateDocument = $number->getDateAt();
  11026.             $number = $number->getNumber();
  11027.             $type = 'Invoice';
  11028.         } else {
  11029.             if ($typeBase == 'R'){
  11030.                 // Es una factura rectificativa
  11031.                 $number = $em->getRepository(MdvDocInvoiceRec::class)->findOneById($invoiceId);
  11032.                 $dateDocument = $number->getDateAt();
  11033.                 $type = 'Invoice Rec';
  11034.             } else {
  11035.                 // Es una Proforma
  11036.                 $number = $invoiceId;
  11037.                 $type = 'Proforma';
  11038.             }
  11039.         }
  11040.         //FIN: Determinamos el numero de factura o proforma
  11041.         // Buscamos los productos, pagos y servicios
  11042.         $arrayItems = array();
  11043.         $fileProducts = array();
  11044.         $payments = array();
  11045.         $services = array();
  11046.         if (!empty($items)) {
  11047.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  11048.                 foreach ($items as $item) {
  11049.                     switch ($item->getItemType()) {
  11050.                         case 'PRODUCT': // Producto
  11051.                             $fileProducts[] = $item;
  11052.                             break;
  11053.                         case 'PAYMENT': // Pago
  11054.                             $payments[] = $item;
  11055.                             break;
  11056.                         case 'SERVICE': // Servicio
  11057.                             $services[] = $item;
  11058.                             break;
  11059.                         default:
  11060.                             break;
  11061.                     }
  11062.                 }
  11063.             } else {
  11064.                 // Es una proforma
  11065.                 foreach ($items as $item) {
  11066.                     switch ($item->getType()) {
  11067.                         case 'PRODUCT': // Producto
  11068.                             $productPro = $em->getRepository(MdvProductFile::class)->findOneById($item->getControlId());
  11069.                             if (!empty($productPro)) {
  11070.                                 $fileProducts[] = $productPro;
  11071.                             }
  11072.                             break;
  11073.                         case 'PAYMENT': // Pago
  11074.                             $paymentPro = $em->getRepository(MdvPaymentsClient::class)->findOneById($item->getControlId());
  11075.                             if (!empty($paymentPro)) {
  11076.                                 $payments[] = $paymentPro;
  11077.                             }
  11078.                             break;
  11079.                         case 'SERVICE': // Servicio
  11080.                             $servicePro = $em->getRepository(MdvServices::class)->findOneById($item->getControlId());
  11081.                             if (!empty($servicePro)) {
  11082.                                 $services[] = $servicePro;
  11083.                             }
  11084.                             break;
  11085.                         default:
  11086.                             break;
  11087.                     }
  11088.                 }
  11089.             }
  11090.         }
  11091.         $data_supplier = array();
  11092.         $i = 0;
  11093.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  11094.         $pax = '1';
  11095.         $qty = '1';
  11096.         $product = array(
  11097.             'neto' => 0,
  11098.             'sumSubT' => 0,
  11099.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  11100.         $service = array(
  11101.             'neto' => 0,
  11102.             'sumSubT' => 0,
  11103.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  11104.         foreach ($fileProducts as $item){
  11105.             $factorIva = empty($item->getIva()) ? 21 : $item->getIva();
  11106.             $factorIva = $factorIva / 100;
  11107.             if (!is_null($item->getUnits()) or !empty($item->getUnits())){
  11108.                 $qty = $item->getUnits();
  11109.             }
  11110.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  11111.                 if (is_null($item->getPrdServicePrice()) or empty($item->getPrdServicePrice())){
  11112.                     $subtotal = 0;
  11113.                     $neto = 0;
  11114.                     $subtotalProduct = 0;
  11115.                 } else {
  11116.                     $subtotalProduct = $item->getPrdSubTotalPrice() * (1 + $factorIva);
  11117.                     $subneto = $item->getPrdSubTotalPrice();
  11118.                     $subtotal =  $subtotalProduct;
  11119.                     $neto =  $subneto;
  11120.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  11121.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  11122.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  11123.                     $arrayItems[] = array(
  11124.                         'type' => 'Product',
  11125.                         'name' => $item->getPrdName(),
  11126.                         'amount' => $neto,
  11127.                         'iva' => ($factorIva * 100),
  11128.                         'total' => $subtotal,
  11129.                     );
  11130.                 }
  11131.             } else {
  11132.                 // Es proforma
  11133.                 if (is_null($item->getServicePrice()) or empty($item->getServicePrice())){
  11134.                     $subtotal = 0;
  11135.                     $neto = 0;
  11136.                     $subtotalProduct = 0;
  11137.                 } else {
  11138.                     $subtotalProduct = $item->getServicePrice() * (1 + $factorIva);
  11139.                     $subneto = $item->getServicePrice();
  11140.                     $subtotal =  $subtotalProduct;
  11141.                     $neto =  $subneto;
  11142.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  11143.                     $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  11144.                     $neto = round($neto,2,PHP_ROUND_HALF_UP);
  11145.                     $arrayItems[] = array(
  11146.                         'type' => 'Product',
  11147.                         'name' => $item->getProductName(),
  11148.                         'amount' => $neto,
  11149.                         'iva' => ($factorIva * 100),
  11150.                         'total' => $subtotal,
  11151.                     );
  11152.                 }
  11153.             }
  11154.             // Acumula netos totales e IVA
  11155.             $totales_neto_all = $totales_neto_all + $neto;
  11156.             switch ($factorIva){
  11157.                 case 0.21: $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * $factorIva); break;
  11158.                 case 0: $data_iva['ivaMontoCero'] = 0; break;
  11159.             }
  11160.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  11161.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  11162.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  11163.             // Acumula netos totales e IVA
  11164.             $product['neto'] = $product['neto'] + $neto;
  11165.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  11166.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  11167.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  11168.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  11169.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  11170.                 $data_supplier['product'][$i] = array(
  11171.                     'id' => $item->getId(),
  11172.                     'productName' => $item->getPrdName(),
  11173.                     'productId' => $item->getPrdProductId(),
  11174.                     'dateStart' => $item->getPrdDateStart(),
  11175.                     'dateEnd' => $item->getPrdDateEnd(),
  11176.                     'servicePrice' => $item->getPrdServicePrice(),
  11177.                     'serviceSubTotalPrice' => $subneto,     //antes de IVA
  11178.                     'subtotalProduct' => $subtotalProduct,
  11179.                     'iva' => ($factorIva * 100),
  11180.                     'pax' => $pax,
  11181.                     'qty' => $qty,
  11182.                     'type' => $item->getPrdType(),
  11183.                     'subtotal' => $subtotal,
  11184.                 );
  11185.             } else {
  11186.                 // Es proforma
  11187.                 $data_supplier['product'][$i] = array(
  11188.                     'id' => $item->getId(),
  11189.                     'productName' => $item->getProductName(),
  11190.                     'productId' => $item->getProductId(),
  11191.                     'dateStart' => $item->getDateStart(),
  11192.                     'dateEnd' => $item->getDateEnd(),
  11193.                     'servicePrice' => $item->getServicePrice(),
  11194.                     'subtotalProduct' => $subtotalProduct,
  11195.                     'iva' => ($factorIva * 100),
  11196.                     'pax' => $pax,
  11197.                     'qty' => $qty,
  11198.                     'type' => $item->getType(),
  11199.                     'subtotal' => $subtotal,
  11200.                 );
  11201.             }
  11202.             $i++;
  11203.         }
  11204.         $data_supplier['productSubTotal'] = array(
  11205.             'neto' => $product['neto'],
  11206.             'sumSubT' => $product['sumSubT'],
  11207.         );
  11208.         $payment = array(
  11209.             'sumSubT' => 0,
  11210.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  11211.         $i = 0;
  11212.         foreach ($payments as $item){
  11213.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  11214.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getPayAmountTotal();
  11215.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  11216.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  11217.                 $data_supplier['payment'][$i] = array(
  11218.                     'id' => $item->getId(),
  11219.                     'amount' => $item->getPayAmount(),
  11220.                     'amountTotal' => $item->getPayAmountTotal(),
  11221.                     'vat' => $item->getPayVat(),
  11222.                     'datePayAt' => $item->getPayDatePayAt(),
  11223.                     'wayToPay' => $item->getPayWayToPay(),
  11224.                 );
  11225.                 $arrayItems[] = array(
  11226.                     'type' => 'Payment',
  11227.                     'name' => $item->getPayWayToPay(),
  11228.                     'amount' => $item->getPayAmount(),
  11229.                     'total' => $item->getPayAmountTotal(),
  11230.                     'iva' => $item->getPayVat(),
  11231.                 );
  11232.             } else {
  11233.                 // Es una proforma
  11234.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  11235.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  11236.                 $payment['sumSubT'] = round($payment['sumSubT'], 2, PHP_ROUND_HALF_UP);
  11237.                 $data_supplier['payment'][$i] = array(
  11238.                     'id' => $item->getId(),
  11239.                     'amount' => $item->getAmount(),
  11240.                     'amountTotal' => $item->getAmountTotal(),
  11241.                     'vat' => $item->getVat(),
  11242.                     'datePayAt' => $item->getDatePayAt(),
  11243.                     'wayToPay' => $item->getWayToPay(),
  11244.                 );
  11245.                 $arrayItems[] = array(
  11246.                     'type' => 'Payment',
  11247.                     'name' => $item->getWayToPay(),
  11248.                     'amount' => $item->getAmount(),
  11249.                     'total' => $item->getAmountTotal(),
  11250.                     'iva' => $item->getVat(),
  11251.                 );
  11252.             }
  11253.             $i++;
  11254.         }
  11255.         if (!empty($payments)) {
  11256.             $data_supplier['paymentSubTotal'] = array(
  11257.                 'sumSubT' => $payment['sumSubT'],
  11258.             );
  11259.         }
  11260.         foreach ($services as $item){
  11261.             if (($typeBase == 'I') or ($typeBase == 'R')) {
  11262.                 if (is_null($item->getSrvPrice()) or empty($item->getSrvPrice())) {
  11263.                     $subtotal = 0;
  11264.                     $neto = 0;
  11265.                     $subtotalService = 0;
  11266.                 } else {
  11267.                     $subtotalService = $item->getSrvPrice();
  11268.                     $subneto = $item->getSrvPrice();
  11269.                     // Commission
  11270.                     if ($item->getSrvOpCommission() == '1') {
  11271.                         $subtotalService = $subtotalService * (1 + ($item->getSrvCommission() / 100));
  11272.                         $subneto = $subneto * (1 + ($item->getSrvCommission() / 100));
  11273.                     } else {
  11274.                         $subtotalService = $subtotalService * (1 - ($item->getSrvCommission() / 100));
  11275.                         $subneto = $subneto * (1 - ($item->getSrvCommission() / 100));
  11276.                     }
  11277.                     // Over
  11278.                     if ($item->getSrvOpOver() == '1') {
  11279.                         $subtotalService = $subtotalService + $item->getSrvOver();
  11280.                         $subneto = $subneto + $item->getSrvOver();
  11281.                     } else {
  11282.                         $subtotalService = $subtotalService - $item->getSrvOver();
  11283.                         $subneto = $subneto - $item->getSrvOver();
  11284.                     }
  11285.                     // IVA
  11286.                     if ($item->getSrvOpIva() == '1') {
  11287.                         $subtotalService = $subtotalService * (1 + ($item->getSrvIva() / 100));
  11288.                     } else {
  11289.                         $subtotalService = $item->getSrvPrice();
  11290.                         $subneto = ($subneto * 100) / (100 + $item->getSrvIva());
  11291.                     }
  11292.                     switch ($item->getSrvServiceCatId()) {
  11293.                         case 1: // Alojamiento
  11294.                             // el numero de noches $numNoches; precio unitario $subneto
  11295.                             $numNoches = (($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days;
  11296.                             // La personas no afectan este calculo
  11297.                             $subtotal = $subtotalService * $numNoches * $item->getSrvUnits();
  11298.                             $subnetoUnit = $subneto;
  11299.                             $subneto = $subneto * $numNoches * $item->getSrvUnits();
  11300.                             $data_supplier['service'][$i] = array(
  11301.                                 'id' => $item->getId(),
  11302.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11303.                                 'serviceName' => $item->getSrvName(),
  11304.                                 'serviceType' => 'Hotel',
  11305.                                 'date' => ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y'),
  11306.                                 'qty' => $item->getSrvUnits(),
  11307.                                 'iva' => $item->getSrvIva(),
  11308.                                 'pax' => '-',
  11309.                                 'precioUnit' => $subnetoUnit,
  11310.                                 'subneto' => $subneto,
  11311.                                 'subtotal' => $subtotal,
  11312.                             );
  11313.                             break;
  11314.                         case 2: //Actividades
  11315.                             // El número de personas es considerado en el calculo
  11316.                             $pax = $item->getSrvPax();
  11317.                             if (empty($pax) or $pax == "0") {
  11318.                                 $pax = 1;
  11319.                             }
  11320.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11321.                             if ($days > 1) {
  11322.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11323.                             } else {
  11324.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11325.                             }
  11326.                             $subtotal = $subtotalService * $days * $item->getSrvUnits();
  11327.                             $subnetoUnit = $subneto;
  11328.                             $subneto = $subneto * $days * $item->getSrvUnits();
  11329.                             $data_supplier['service'][$i] = array(
  11330.                                 'id' => $item->getId(),
  11331.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11332.                                 'serviceName' => $item->getSrvName(),
  11333.                                 'serviceType' => 'Actividad',
  11334.                                 'date' => $dateServ,
  11335.                                 'qty' => $item->getSrvUnits(),
  11336.                                 'iva' => $item->getSrvIva(),
  11337.                                 'pax' => $item->getSrvPax(),
  11338.                                 'precioUnit' => $subnetoUnit,
  11339.                                 'subneto' => $subneto,
  11340.                                 'subtotal' => $subtotal,
  11341.                             );
  11342.                             break;
  11343.                         case 3: // AV
  11344.                             $pax = $item->getSrvPax();
  11345.                             if (empty($pax) or $pax == "0") {
  11346.                                 $pax = 1;
  11347.                             }
  11348.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11349.                             if ($days > 1) {
  11350.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11351.                             } else {
  11352.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11353.                             }
  11354.                             $unitsServ = $item->getSrvUnits();
  11355.                             if (empty($unitsServ) or $unitsServ == "0") {
  11356.                                 $unitsServ = 1;
  11357.                             }
  11358.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11359.                             $subnetoUnit = $subneto;
  11360.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11361.                             $data_supplier['service'][$i] = array(
  11362.                                 'id' => $item->getId(),
  11363.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11364.                                 'serviceName' => $item->getSrvName(),
  11365.                                 'serviceType' => 'AV',
  11366.                                 'date' => $dateServ,
  11367.                                 'qty' => $unitsServ,
  11368.                                 'iva' => $item->getSrvIva(),
  11369.                                 'pax' => $item->getSrvPax(),
  11370.                                 'precioUnit' => $subnetoUnit,
  11371.                                 'subneto' => $subneto,
  11372.                                 'subtotal' => $subtotal,
  11373.                             );
  11374.                             break;
  11375.                         case 4: //Creative
  11376.                             $pax = $item->getSrvPax();
  11377.                             if (empty($pax) or $pax == "0") {
  11378.                                 $pax = 1;
  11379.                             }
  11380.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11381.                             if ($days > 1) {
  11382.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11383.                             } else {
  11384.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11385.                             }
  11386.                             $unitsServ = $item->getSrvUnits();
  11387.                             if (empty($unitsServ) or $unitsServ == "0") {
  11388.                                 $unitsServ = 1;
  11389.                             }
  11390.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11391.                             $subnetoUnit = $subneto;
  11392.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11393.                             $data_supplier['service'][$i] = array(
  11394.                                 'id' => $item->getId(),
  11395.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11396.                                 'serviceName' => $item->getSrvName(),
  11397.                                 'serviceType' => 'Creativo',
  11398.                                 'date' => $dateServ,
  11399.                                 'qty' => $unitsServ,
  11400.                                 'iva' => $item->getSrvIva(),
  11401.                                 'pax' => $item->getSrvPax(),
  11402.                                 'precioUnit' => $subnetoUnit,
  11403.                                 'subneto' => $subneto,
  11404.                                 'subtotal' => $subtotal,
  11405.                             );
  11406.                             break;
  11407.                         case 5: //Cruise
  11408.                             $pax = $item->getSrvPax();
  11409.                             if (empty($pax) or $pax == "0") {
  11410.                                 $pax = 1;
  11411.                             }
  11412.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days);
  11413.                             if ($days > 1) {
  11414.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11415.                             } else {
  11416.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11417.                             }
  11418.                             $unitsServ = $item->getSrvUnits();
  11419.                             if (empty($unitsServ) or $unitsServ == "0") {
  11420.                                 $unitsServ = 1;
  11421.                             }
  11422.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11423.                             $subnetoUnit = $subneto;
  11424.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11425.                             $data_supplier['service'][$i] = array(
  11426.                                 'id' => $item->getId(),
  11427.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11428.                                 'serviceName' => $item->getSrvName(),
  11429.                                 'serviceType' => 'Crucero',
  11430.                                 'date' => $dateServ,
  11431.                                 'qty' => $unitsServ,
  11432.                                 'iva' => $item->getSrvIva(),
  11433.                                 'pax' => $item->getSrvPax(),
  11434.                                 'precioUnit' => $subnetoUnit,
  11435.                                 'subneto' => $subneto,
  11436.                                 'subtotal' => $subtotal,
  11437.                             );
  11438.                             break;
  11439.                         case 6: //Entertaiment
  11440.                             $pax = $item->getSrvPax();
  11441.                             if (empty($pax) or $pax == "0") {
  11442.                                 $pax = 1;
  11443.                             }
  11444.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11445.                             if ($days > 1) {
  11446.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11447.                             } else {
  11448.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11449.                             }
  11450.                             $unitsServ = $item->getSrvUnits();
  11451.                             if (empty($unitsServ) or $unitsServ == "0") {
  11452.                                 $unitsServ = 1;
  11453.                             }
  11454.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11455.                             $subnetoUnit = $subneto;
  11456.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11457.                             $data_supplier['service'][$i] = array(
  11458.                                 'id' => $item->getId(),
  11459.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11460.                                 'serviceName' => $item->getSrvName(),
  11461.                                 'serviceType' => 'Entretenimiento',
  11462.                                 'date' => $dateServ,
  11463.                                 'qty' => $unitsServ,
  11464.                                 'iva' => $item->getSrvIva(),
  11465.                                 'pax' => $item->getSrvPax(),
  11466.                                 'precioUnit' => $subnetoUnit,
  11467.                                 'subneto' => $subneto,
  11468.                                 'subtotal' => $subtotal,
  11469.                             );
  11470.                             break;
  11471.                         case 7: // Gifts
  11472.                             $pax = $item->getSrvPax();
  11473.                             if (empty($pax) or $pax == "0") {
  11474.                                 $pax = 1;
  11475.                             }
  11476. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11477.                             $days = 1;
  11478.                             if ($days > 1) {
  11479.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11480.                             } else {
  11481.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11482.                             }
  11483.                             $unitsServ = $item->getSrvUnits();
  11484.                             if (empty($unitsServ) or $unitsServ == "0") {
  11485.                                 $unitsServ = 1;
  11486.                             }
  11487.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11488.                             $subnetoUnit = $subneto;
  11489.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11490.                             $data_supplier['service'][$i] = array(
  11491.                                 'id' => $item->getId(),
  11492.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11493.                                 'serviceName' => $item->getSrvName(),
  11494.                                 'serviceType' => 'Regalos',
  11495.                                 'date' => $dateServ,
  11496.                                 'qty' => $unitsServ,
  11497.                                 'iva' => $item->getSrvIva(),
  11498.                                 'pax' => $item->getSrvPax(),
  11499.                                 'precioUnit' => $subnetoUnit,
  11500.                                 'subneto' => $subneto,
  11501.                                 'subtotal' => $subtotal,
  11502.                             );
  11503.                             break;
  11504.                         case 8: //Guide
  11505.                             $pax = $item->getSrvPax();
  11506.                             if (empty($pax) or $pax == "0") {
  11507.                                 $pax = 1;
  11508.                             }
  11509.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11510.                             if ($days > 1) {
  11511.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11512.                             } else {
  11513.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11514.                             }
  11515.                             $unitsServ = $item->getSrvUnits();
  11516.                             if (empty($unitsServ) or $unitsServ == "0") {
  11517.                                 $unitsServ = 1;
  11518.                             }
  11519.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11520.                             $subnetoUnit = $subneto;
  11521.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11522.                             $data_supplier['service'][$i] = array(
  11523.                                 'id' => $item->getId(),
  11524.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11525.                                 'serviceName' => $item->getSrvName(),
  11526.                                 'serviceType' => 'Regalos',
  11527.                                 'date' => $dateServ,
  11528.                                 'qty' => $unitsServ,
  11529.                                 'iva' => $item->getSrvIva(),
  11530.                                 'pax' => $item->getSrvPax(),
  11531.                                 'precioUnit' => $subnetoUnit,
  11532.                                 'subneto' => $subneto,
  11533.                                 'subtotal' => $subtotal,
  11534.                             );
  11535.                             break;
  11536.                         case 9: //Itineraries
  11537.                             $pax = $item->getSrvPax();
  11538.                             if (empty($pax) or $pax == "0") {
  11539.                                 $pax = 1;
  11540.                             }
  11541.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11542.                             if ($days > 1) {
  11543.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11544.                             } else {
  11545.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11546.                             }
  11547.                             $unitsServ = $item->getSrvUnits();
  11548.                             if (empty($unitsServ) or $unitsServ == "0") {
  11549.                                 $unitsServ = 1;
  11550.                             }
  11551.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11552.                             $subnetoUnit = $subneto;
  11553.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11554.                             $data_supplier['service'][$i] = array(
  11555.                                 'id' => $item->getId(),
  11556.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11557.                                 'serviceName' => $item->getSrvName(),
  11558.                                 'serviceType' => 'Itinerarios',
  11559.                                 'date' => $dateServ,
  11560.                                 'qty' => $unitsServ,
  11561.                                 'iva' => $item->getSrvIva(),
  11562.                                 'pax' => $item->getSrvPax(),
  11563.                                 'precioUnit' => $subnetoUnit,
  11564.                                 'subneto' => $subneto,
  11565.                                 'subtotal' => $subtotal,
  11566.                             );
  11567.                             break;
  11568.                         case 10: //Lounge  -- No Aplica
  11569. //                        $pax = $item->getPax();
  11570. //                        if (empty($pax) or $pax == "0") {
  11571. //                            $pax = 1;
  11572. //                        }
  11573. //
  11574. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11575. //                        if ($days > 1){
  11576. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  11577. //                        } else {
  11578. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11579. //                        }
  11580. //
  11581. //                        $unitsServ = $item->getUnits();
  11582. //                        if (empty($unitsServ) or $unitsServ == "0") {
  11583. //                            $unitsServ = 1;
  11584. //                        }
  11585. //
  11586. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11587. //                        $subnetoUnit = $subneto;
  11588. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  11589. //
  11590. //                        $data_supplier['service'][$i] = array (
  11591. //                            'id' => $item->getId(),
  11592. //                            'serviceCatId' => $item->getServiceCatId(),
  11593. //                            'serviceName' => $item->getName(),
  11594. //                            'serviceType' => 'Itinerarios',
  11595. //                            'date' => $dateServ,
  11596. //                            'qty' => $unitsServ,
  11597. //                            'iva' => $item->getIva(),
  11598. //                            'pax' => $item->getPax(),
  11599. //                            'precioUnit' => $subnetoUnit,
  11600. //                            'subneto' => $subneto,
  11601. //                            'subtotal' => $subtotal,
  11602. //                        );
  11603.                             break;
  11604.                         case 11: //Menu
  11605.                             $pax = $item->getSrvPax();
  11606.                             if (empty($pax) or $pax == "0") {
  11607.                                 $pax = 1;
  11608.                             }
  11609.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11610.                             if ($days > 1) {
  11611.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11612.                             } else {
  11613.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11614.                             }
  11615.                             $unitsServ = $item->getSrvUnits();
  11616.                             if (empty($unitsServ) or $unitsServ == "0") {
  11617.                                 $unitsServ = 1;
  11618.                             }
  11619.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11620.                             $subnetoUnit = $subneto;
  11621.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11622.                             $data_supplier['service'][$i] = array(
  11623.                                 'id' => $item->getId(),
  11624.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11625.                                 'serviceName' => $item->getSrvName(),
  11626.                                 'serviceType' => 'Menú',
  11627.                                 'date' => $dateServ,
  11628.                                 'qty' => $unitsServ,
  11629.                                 'iva' => $item->getSrvIva(),
  11630.                                 'pax' => $item->getSrvPax(),
  11631.                                 'precioUnit' => $subnetoUnit,
  11632.                                 'subneto' => $subneto,
  11633.                                 'subtotal' => $subtotal,
  11634.                             );
  11635.                             break;
  11636.                         case 12: //Others
  11637.                             $pax = $item->getSrvPax();
  11638.                             if (empty($pax) or $pax == "0") {
  11639.                                 $pax = 1;
  11640.                             }
  11641.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11642.                             if ($days > 1) {
  11643.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11644.                             } else {
  11645.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11646.                             }
  11647.                             $unitsServ = $item->getSrvUnits();
  11648.                             if (empty($unitsServ) or $unitsServ == "0") {
  11649.                                 $unitsServ = 1;
  11650.                             }
  11651.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11652.                             $subnetoUnit = $subneto;
  11653.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11654.                             $data_supplier['service'][$i] = array(
  11655.                                 'id' => $item->getId(),
  11656.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11657.                                 'serviceName' => $item->getSrvName(),
  11658.                                 'serviceType' => 'Otros',
  11659.                                 'date' => $dateServ,
  11660.                                 'qty' => $unitsServ,
  11661.                                 'iva' => $item->getSrvIva(),
  11662.                                 'pax' => $item->getSrvPax(),
  11663.                                 'precioUnit' => $subnetoUnit,
  11664.                                 'subneto' => $subneto,
  11665.                                 'subtotal' => $subtotal,
  11666.                             );
  11667.                             break;
  11668.                         case 13: //Transport
  11669.                             $pax = $item->getSrvPax();
  11670.                             if (empty($pax) or $pax == "0") {
  11671.                                 $pax = 1;
  11672.                             }
  11673.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11674.                             if ($days > 1) {
  11675.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11676.                             } else {
  11677.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11678.                             }
  11679.                             $unitsServ = $item->getSrvUnits();
  11680.                             if (empty($unitsServ) or $unitsServ == "0") {
  11681.                                 $unitsServ = 1;
  11682.                             }
  11683.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11684.                             $subnetoUnit = $subneto;
  11685.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11686.                             $data_supplier['service'][$i] = array(
  11687.                                 'id' => $item->getId(),
  11688.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11689.                                 'serviceName' => $item->getSrvName(),
  11690.                                 'serviceType' => 'Transporte',
  11691.                                 'date' => $dateServ,
  11692.                                 'qty' => $unitsServ,
  11693.                                 'iva' => $item->getSrvIva(),
  11694.                                 'pax' => $item->getSrvPax(),
  11695.                                 'precioUnit' => $subnetoUnit,
  11696.                                 'subneto' => $subneto,
  11697.                                 'subtotal' => $subtotal,
  11698.                             );
  11699.                             break;
  11700.                         case 14: //Technology
  11701.                             $pax = $item->getSrvPax();
  11702.                             if (empty($pax) or $pax == "0") {
  11703.                                 $pax = 1;
  11704.                             }
  11705. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11706.                             $days = 1;
  11707. //                        if ($days > 1){
  11708. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  11709. //                        } else {
  11710. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11711. //                        }
  11712.                             $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11713.                             $unitsServ = $item->getSrvUnits();
  11714.                             if (empty($unitsServ) or $unitsServ == "0") {
  11715.                                 $unitsServ = 1;
  11716.                             }
  11717.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11718.                             $subnetoUnit = $subneto;
  11719.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11720.                             $data_supplier['service'][$i] = array(
  11721.                                 'id' => $item->getId(),
  11722.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11723.                                 'serviceName' => $item->getSrvName(),
  11724.                                 'serviceType' => 'Tecnología',
  11725.                                 'date' => $dateServ,
  11726.                                 'qty' => $unitsServ,
  11727.                                 'iva' => $item->getSrvIva(),
  11728.                                 'pax' => $item->getSrvPax(),
  11729.                                 'precioUnit' => $subnetoUnit,
  11730.                                 'subneto' => $subneto,
  11731.                                 'subtotal' => $subtotal,
  11732.                             );
  11733.                             break;
  11734.                         case 15: //Assisstant
  11735.                             $pax = $item->getSrvPax();
  11736.                             if (empty($pax) or $pax == "0") {
  11737.                                 $pax = 1;
  11738.                             }
  11739.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11740.                             if ($days > 1) {
  11741.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11742.                             } else {
  11743.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11744.                             }
  11745.                             $unitsServ = $item->getSrvUnits();
  11746.                             if (empty($unitsServ) or $unitsServ == "0") {
  11747.                                 $unitsServ = 1;
  11748.                             }
  11749.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11750.                             $subnetoUnit = $subneto;
  11751.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11752.                             $data_supplier['service'][$i] = array(
  11753.                                 'id' => $item->getId(),
  11754.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11755.                                 'serviceName' => $item->getSrvName(),
  11756.                                 'serviceType' => 'Asistente',
  11757.                                 'date' => $dateServ,
  11758.                                 'qty' => $unitsServ,
  11759.                                 'iva' => $item->getSrvIva(),
  11760.                                 'pax' => $item->getSrvPax(),
  11761.                                 'precioUnit' => $subnetoUnit,
  11762.                                 'subneto' => $subneto,
  11763.                                 'subtotal' => $subtotal,
  11764.                             );
  11765.                             break;
  11766.                         case 16: //DDR
  11767.                             $pax = $item->getSrvPax();
  11768.                             if (empty($pax) or $pax == "0") {
  11769.                                 $pax = 1;
  11770.                             }
  11771.                             $days = ((($item->getSrvDateOutAt())->diff($item->getSrvDateInAt()))->days + 1);
  11772.                             if ($days > 1) {
  11773.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y') . ' - ' . ($item->getSrvDateOutAt())->format('d/m/Y');
  11774.                             } else {
  11775.                                 $dateServ = ($item->getSrvDateInAt())->format('d/m/Y');
  11776.                             }
  11777.                             $unitsServ = $item->getSrvUnits();
  11778.                             if (empty($unitsServ) or $unitsServ == "0") {
  11779.                                 $unitsServ = 1;
  11780.                             }
  11781.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11782.                             $subnetoUnit = $subneto;
  11783.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11784.                             $data_supplier['service'][$i] = array(
  11785.                                 'id' => $item->getId(),
  11786.                                 'serviceCatId' => $item->getSrvServiceCatId(),
  11787.                                 'serviceName' => $item->getSrvName(),
  11788.                                 'serviceType' => 'DDR',
  11789.                                 'date' => $dateServ,
  11790.                                 'qty' => $unitsServ,
  11791.                                 'iva' => $item->getSrvIva(),
  11792.                                 'pax' => $item->getSrvPax(),
  11793.                                 'precioUnit' => $subnetoUnit,
  11794.                                 'subneto' => $subneto,
  11795.                                 'subtotal' => $subtotal,
  11796.                             );
  11797.                             break;
  11798.                         default:
  11799.                             break;
  11800.                     }
  11801.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  11802.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  11803.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  11804.                     $arrayItems[] = array(
  11805.                         'type' => 'Service',
  11806.                         'name' => $item->getSrvName(),
  11807.                         'amount' => $neto,
  11808.                         'iva' => $item->getSrvIva(),
  11809.                         'total' => $subtotal,
  11810.                     );
  11811.                 }
  11812.                 switch ($item->getSrvIva()) {
  11813.                     // Acumula IVA
  11814.                     case 21:
  11815.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getSrvIva() / 100));
  11816.                         break;
  11817.                     case 10:
  11818.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getSrvIva() / 100));
  11819.                         break;
  11820.                     case 0:
  11821.                         break;
  11822.                     default:
  11823.                         break;
  11824.                 }
  11825.             } else {
  11826.                 // Es una proforma
  11827.                 if (is_null($item->getPrice()) or empty($item->getPrice())) {
  11828.                     $subtotal = 0;
  11829.                     $neto = 0;
  11830.                     $subtotalService = 0;
  11831.                 } else {
  11832.                     $subtotalService = $item->getPrice();
  11833.                     $subneto = $item->getPrice();
  11834.                     // Commission
  11835.                     if ($item->getOpCommission() == '1') {
  11836.                         $subtotalService = $subtotalService * (1 + ($item->getCommission() / 100));
  11837.                         $subneto = $subneto * (1 + ($item->getCommission() / 100));
  11838.                     } else {
  11839.                         $subtotalService = $subtotalService * (1 - ($item->getCommission() / 100));
  11840.                         $subneto = $subneto * (1 - ($item->getCommission() / 100));
  11841.                     }
  11842.                     // Over
  11843.                     if ($item->getOpOver() == '1') {
  11844.                         $subtotalService = $subtotalService + $item->getOver();
  11845.                         $subneto = $subneto + $item->getOver();
  11846.                     } else {
  11847.                         $subtotalService = $subtotalService - $item->getOver();
  11848.                         $subneto = $subneto - $item->getOver();
  11849.                     }
  11850.                     // IVA
  11851.                     if ($item->getOpIva() == '1') {
  11852.                         $subtotalService = $subtotalService * (1 + ($item->getIva() / 100));
  11853.                     } else {
  11854.                         $subtotalService = $item->getPrice();
  11855.                         $subneto = ($subneto * 100) / (100 + $item->getIva());
  11856.                     }
  11857.                     switch ($item->getServiceCatId()) {
  11858.                         case 1: // Alojamiento
  11859.                             // el numero de noches $numNoches; precio unitario $subneto
  11860.                             $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  11861.                             // La personas no afectan este calculo
  11862.                             $subtotal = $subtotalService * $numNoches * $item->getUnits();
  11863.                             $subnetoUnit = $subneto;
  11864.                             $subneto = $subneto * $numNoches * $item->getUnits();
  11865.                             $data_supplier['service'][$i] = array(
  11866.                                 'id' => $item->getId(),
  11867.                                 'serviceCatId' => $item->getServiceCatId(),
  11868.                                 'serviceName' => $item->getName(),
  11869.                                 'serviceType' => 'Hotel',
  11870.                                 'date' => ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y'),
  11871.                                 'qty' => $item->getUnits(),
  11872.                                 'iva' => $item->getIva(),
  11873.                                 'pax' => '-',
  11874.                                 'precioUnit' => $subnetoUnit,
  11875.                                 'subneto' => $subneto,
  11876.                                 'subtotal' => $subtotal,
  11877.                             );
  11878.                             break;
  11879.                         case 2: //Actividades
  11880.                             // El número de personas es considerado en el calculo
  11881.                             $pax = $item->getPax();
  11882.                             if (empty($pax) or $pax == "0") {
  11883.                                 $pax = 1;
  11884.                             }
  11885.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11886.                             if ($days > 1) {
  11887.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  11888.                             } else {
  11889.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11890.                             }
  11891.                             $subtotal = $subtotalService * $days * $item->getUnits();
  11892.                             $subnetoUnit = $subneto;
  11893.                             $subneto = $subneto * $days * $item->getUnits();
  11894.                             $data_supplier['service'][$i] = array(
  11895.                                 'id' => $item->getId(),
  11896.                                 'serviceCatId' => $item->getServiceCatId(),
  11897.                                 'serviceName' => $item->getName(),
  11898.                                 'serviceType' => 'Actividad',
  11899.                                 'date' => $dateServ,
  11900.                                 'qty' => $item->getUnits(),
  11901.                                 'iva' => $item->getIva(),
  11902.                                 'pax' => $item->getPax(),
  11903.                                 'precioUnit' => $subnetoUnit,
  11904.                                 'subneto' => $subneto,
  11905.                                 'subtotal' => $subtotal,
  11906.                             );
  11907.                             break;
  11908.                         case 3: // AV
  11909.                             $pax = $item->getPax();
  11910.                             if (empty($pax) or $pax == "0") {
  11911.                                 $pax = 1;
  11912.                             }
  11913.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11914.                             if ($days > 1) {
  11915.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  11916.                             } else {
  11917.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11918.                             }
  11919.                             $unitsServ = $item->getUnits();
  11920.                             if (empty($unitsServ) or $unitsServ == "0") {
  11921.                                 $unitsServ = 1;
  11922.                             }
  11923.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11924.                             $subnetoUnit = $subneto;
  11925.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11926.                             $data_supplier['service'][$i] = array(
  11927.                                 'id' => $item->getId(),
  11928.                                 'serviceCatId' => $item->getServiceCatId(),
  11929.                                 'serviceName' => $item->getName(),
  11930.                                 'serviceType' => 'AV',
  11931.                                 'date' => $dateServ,
  11932.                                 'qty' => $unitsServ,
  11933.                                 'iva' => $item->getIva(),
  11934.                                 'pax' => $item->getPax(),
  11935.                                 'precioUnit' => $subnetoUnit,
  11936.                                 'subneto' => $subneto,
  11937.                                 'subtotal' => $subtotal,
  11938.                             );
  11939.                             break;
  11940.                         case 4: //Creative
  11941.                             $pax = $item->getPax();
  11942.                             if (empty($pax) or $pax == "0") {
  11943.                                 $pax = 1;
  11944.                             }
  11945.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  11946.                             if ($days > 1) {
  11947.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  11948.                             } else {
  11949.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11950.                             }
  11951.                             $unitsServ = $item->getUnits();
  11952.                             if (empty($unitsServ) or $unitsServ == "0") {
  11953.                                 $unitsServ = 1;
  11954.                             }
  11955.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11956.                             $subnetoUnit = $subneto;
  11957.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11958.                             $data_supplier['service'][$i] = array(
  11959.                                 'id' => $item->getId(),
  11960.                                 'serviceCatId' => $item->getServiceCatId(),
  11961.                                 'serviceName' => $item->getName(),
  11962.                                 'serviceType' => 'Creativo',
  11963.                                 'date' => $dateServ,
  11964.                                 'qty' => $unitsServ,
  11965.                                 'iva' => $item->getIva(),
  11966.                                 'pax' => $item->getPax(),
  11967.                                 'precioUnit' => $subnetoUnit,
  11968.                                 'subneto' => $subneto,
  11969.                                 'subtotal' => $subtotal,
  11970.                             );
  11971.                             break;
  11972.                         case 5: //Cruise
  11973.                             $pax = $item->getPax();
  11974.                             if (empty($pax) or $pax == "0") {
  11975.                                 $pax = 1;
  11976.                             }
  11977.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  11978.                             if ($days > 1) {
  11979.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  11980.                             } else {
  11981.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  11982.                             }
  11983.                             $unitsServ = $item->getUnits();
  11984.                             if (empty($unitsServ) or $unitsServ == "0") {
  11985.                                 $unitsServ = 1;
  11986.                             }
  11987.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  11988.                             $subnetoUnit = $subneto;
  11989.                             $subneto = $subneto * $days * $unitsServ * $pax;
  11990.                             $data_supplier['service'][$i] = array(
  11991.                                 'id' => $item->getId(),
  11992.                                 'serviceCatId' => $item->getServiceCatId(),
  11993.                                 'serviceName' => $item->getName(),
  11994.                                 'serviceType' => 'Crucero',
  11995.                                 'date' => $dateServ,
  11996.                                 'qty' => $unitsServ,
  11997.                                 'iva' => $item->getIva(),
  11998.                                 'pax' => $item->getPax(),
  11999.                                 'precioUnit' => $subnetoUnit,
  12000.                                 'subneto' => $subneto,
  12001.                                 'subtotal' => $subtotal,
  12002.                             );
  12003.                             break;
  12004.                         case 6: //Entertaiment
  12005.                             $pax = $item->getPax();
  12006.                             if (empty($pax) or $pax == "0") {
  12007.                                 $pax = 1;
  12008.                             }
  12009.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12010.                             if ($days > 1) {
  12011.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12012.                             } else {
  12013.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12014.                             }
  12015.                             $unitsServ = $item->getUnits();
  12016.                             if (empty($unitsServ) or $unitsServ == "0") {
  12017.                                 $unitsServ = 1;
  12018.                             }
  12019.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12020.                             $subnetoUnit = $subneto;
  12021.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12022.                             $data_supplier['service'][$i] = array(
  12023.                                 'id' => $item->getId(),
  12024.                                 'serviceCatId' => $item->getServiceCatId(),
  12025.                                 'serviceName' => $item->getName(),
  12026.                                 'serviceType' => 'Entretenimiento',
  12027.                                 'date' => $dateServ,
  12028.                                 'qty' => $unitsServ,
  12029.                                 'iva' => $item->getIva(),
  12030.                                 'pax' => $item->getPax(),
  12031.                                 'precioUnit' => $subnetoUnit,
  12032.                                 'subneto' => $subneto,
  12033.                                 'subtotal' => $subtotal,
  12034.                             );
  12035.                             break;
  12036.                         case 7: // Gifts
  12037.                             $pax = $item->getPax();
  12038.                             if (empty($pax) or $pax == "0") {
  12039.                                 $pax = 1;
  12040.                             }
  12041. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12042.                             $days = 1;
  12043.                             if ($days > 1) {
  12044.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12045.                             } else {
  12046.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12047.                             }
  12048.                             $unitsServ = $item->getUnits();
  12049.                             if (empty($unitsServ) or $unitsServ == "0") {
  12050.                                 $unitsServ = 1;
  12051.                             }
  12052.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12053.                             $subnetoUnit = $subneto;
  12054.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12055.                             $data_supplier['service'][$i] = array(
  12056.                                 'id' => $item->getId(),
  12057.                                 'serviceCatId' => $item->getServiceCatId(),
  12058.                                 'serviceName' => $item->getName(),
  12059.                                 'serviceType' => 'Regalos',
  12060.                                 'date' => $dateServ,
  12061.                                 'qty' => $unitsServ,
  12062.                                 'iva' => $item->getIva(),
  12063.                                 'pax' => $item->getPax(),
  12064.                                 'precioUnit' => $subnetoUnit,
  12065.                                 'subneto' => $subneto,
  12066.                                 'subtotal' => $subtotal,
  12067.                             );
  12068.                             break;
  12069.                         case 8: //Guide
  12070.                             $pax = $item->getPax();
  12071.                             if (empty($pax) or $pax == "0") {
  12072.                                 $pax = 1;
  12073.                             }
  12074.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12075.                             if ($days > 1) {
  12076.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12077.                             } else {
  12078.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12079.                             }
  12080.                             $unitsServ = $item->getUnits();
  12081.                             if (empty($unitsServ) or $unitsServ == "0") {
  12082.                                 $unitsServ = 1;
  12083.                             }
  12084.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12085.                             $subnetoUnit = $subneto;
  12086.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12087.                             $data_supplier['service'][$i] = array(
  12088.                                 'id' => $item->getId(),
  12089.                                 'serviceCatId' => $item->getServiceCatId(),
  12090.                                 'serviceName' => $item->getName(),
  12091.                                 'serviceType' => 'Regalos',
  12092.                                 'date' => $dateServ,
  12093.                                 'qty' => $unitsServ,
  12094.                                 'iva' => $item->getIva(),
  12095.                                 'pax' => $item->getPax(),
  12096.                                 'precioUnit' => $subnetoUnit,
  12097.                                 'subneto' => $subneto,
  12098.                                 'subtotal' => $subtotal,
  12099.                             );
  12100.                             break;
  12101.                         case 9: //Itineraries
  12102.                             $pax = $item->getPax();
  12103.                             if (empty($pax) or $pax == "0") {
  12104.                                 $pax = 1;
  12105.                             }
  12106.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12107.                             if ($days > 1) {
  12108.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12109.                             } else {
  12110.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12111.                             }
  12112.                             $unitsServ = $item->getUnits();
  12113.                             if (empty($unitsServ) or $unitsServ == "0") {
  12114.                                 $unitsServ = 1;
  12115.                             }
  12116.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12117.                             $subnetoUnit = $subneto;
  12118.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12119.                             $data_supplier['service'][$i] = array(
  12120.                                 'id' => $item->getId(),
  12121.                                 'serviceCatId' => $item->getServiceCatId(),
  12122.                                 'serviceName' => $item->getName(),
  12123.                                 'serviceType' => 'Itinerarios',
  12124.                                 'date' => $dateServ,
  12125.                                 'qty' => $unitsServ,
  12126.                                 'iva' => $item->getIva(),
  12127.                                 'pax' => $item->getPax(),
  12128.                                 'precioUnit' => $subnetoUnit,
  12129.                                 'subneto' => $subneto,
  12130.                                 'subtotal' => $subtotal,
  12131.                             );
  12132.                             break;
  12133.                         case 10: //Lounge  -- No Aplica
  12134. //                        $pax = $item->getPax();
  12135. //                        if (empty($pax) or $pax == "0") {
  12136. //                            $pax = 1;
  12137. //                        }
  12138. //
  12139. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12140. //                        if ($days > 1){
  12141. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12142. //                        } else {
  12143. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12144. //                        }
  12145. //
  12146. //                        $unitsServ = $item->getUnits();
  12147. //                        if (empty($unitsServ) or $unitsServ == "0") {
  12148. //                            $unitsServ = 1;
  12149. //                        }
  12150. //
  12151. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12152. //                        $subnetoUnit = $subneto;
  12153. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  12154. //
  12155. //                        $data_supplier['service'][$i] = array (
  12156. //                            'id' => $item->getId(),
  12157. //                            'serviceCatId' => $item->getServiceCatId(),
  12158. //                            'serviceName' => $item->getName(),
  12159. //                            'serviceType' => 'Itinerarios',
  12160. //                            'date' => $dateServ,
  12161. //                            'qty' => $unitsServ,
  12162. //                            'iva' => $item->getIva(),
  12163. //                            'pax' => $item->getPax(),
  12164. //                            'precioUnit' => $subnetoUnit,
  12165. //                            'subneto' => $subneto,
  12166. //                            'subtotal' => $subtotal,
  12167. //                        );
  12168.                             break;
  12169.                         case 11: //Menu
  12170.                             $pax = $item->getPax();
  12171.                             if (empty($pax) or $pax == "0") {
  12172.                                 $pax = 1;
  12173.                             }
  12174.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12175.                             if ($days > 1) {
  12176.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12177.                             } else {
  12178.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12179.                             }
  12180.                             $unitsServ = $item->getUnits();
  12181.                             if (empty($unitsServ) or $unitsServ == "0") {
  12182.                                 $unitsServ = 1;
  12183.                             }
  12184.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12185.                             $subnetoUnit = $subneto;
  12186.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12187.                             $data_supplier['service'][$i] = array(
  12188.                                 'id' => $item->getId(),
  12189.                                 'serviceCatId' => $item->getServiceCatId(),
  12190.                                 'serviceName' => $item->getName(),
  12191.                                 'serviceType' => 'Menú',
  12192.                                 'date' => $dateServ,
  12193.                                 'qty' => $unitsServ,
  12194.                                 'iva' => $item->getIva(),
  12195.                                 'pax' => $item->getPax(),
  12196.                                 'precioUnit' => $subnetoUnit,
  12197.                                 'subneto' => $subneto,
  12198.                                 'subtotal' => $subtotal,
  12199.                             );
  12200.                             break;
  12201.                         case 12: //Others
  12202.                             $pax = $item->getPax();
  12203.                             if (empty($pax) or $pax == "0") {
  12204.                                 $pax = 1;
  12205.                             }
  12206.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12207.                             if ($days > 1) {
  12208.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12209.                             } else {
  12210.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12211.                             }
  12212.                             $unitsServ = $item->getUnits();
  12213.                             if (empty($unitsServ) or $unitsServ == "0") {
  12214.                                 $unitsServ = 1;
  12215.                             }
  12216.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12217.                             $subnetoUnit = $subneto;
  12218.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12219.                             $data_supplier['service'][$i] = array(
  12220.                                 'id' => $item->getId(),
  12221.                                 'serviceCatId' => $item->getServiceCatId(),
  12222.                                 'serviceName' => $item->getName(),
  12223.                                 'serviceType' => 'Otros',
  12224.                                 'date' => $dateServ,
  12225.                                 'qty' => $unitsServ,
  12226.                                 'iva' => $item->getIva(),
  12227.                                 'pax' => $item->getPax(),
  12228.                                 'precioUnit' => $subnetoUnit,
  12229.                                 'subneto' => $subneto,
  12230.                                 'subtotal' => $subtotal,
  12231.                             );
  12232.                             break;
  12233.                         case 13: //Transport
  12234.                             $pax = $item->getPax();
  12235.                             if (empty($pax) or $pax == "0") {
  12236.                                 $pax = 1;
  12237.                             }
  12238.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12239.                             if ($days > 1) {
  12240.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12241.                             } else {
  12242.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12243.                             }
  12244.                             $unitsServ = $item->getUnits();
  12245.                             if (empty($unitsServ) or $unitsServ == "0") {
  12246.                                 $unitsServ = 1;
  12247.                             }
  12248.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12249.                             $subnetoUnit = $subneto;
  12250.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12251.                             $data_supplier['service'][$i] = array(
  12252.                                 'id' => $item->getId(),
  12253.                                 'serviceCatId' => $item->getServiceCatId(),
  12254.                                 'serviceName' => $item->getName(),
  12255.                                 'serviceType' => 'Transporte',
  12256.                                 'date' => $dateServ,
  12257.                                 'qty' => $unitsServ,
  12258.                                 'iva' => $item->getIva(),
  12259.                                 'pax' => $item->getPax(),
  12260.                                 'precioUnit' => $subnetoUnit,
  12261.                                 'subneto' => $subneto,
  12262.                                 'subtotal' => $subtotal,
  12263.                             );
  12264.                             break;
  12265.                         case 14: //Technology
  12266.                             $pax = $item->getPax();
  12267.                             if (empty($pax) or $pax == "0") {
  12268.                                 $pax = 1;
  12269.                             }
  12270. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12271.                             $days = 1;
  12272. //                        if ($days > 1){
  12273. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12274. //                        } else {
  12275. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12276. //                        }
  12277.                             $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12278.                             $unitsServ = $item->getUnits();
  12279.                             if (empty($unitsServ) or $unitsServ == "0") {
  12280.                                 $unitsServ = 1;
  12281.                             }
  12282.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12283.                             $subnetoUnit = $subneto;
  12284.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12285.                             $data_supplier['service'][$i] = array(
  12286.                                 'id' => $item->getId(),
  12287.                                 'serviceCatId' => $item->getServiceCatId(),
  12288.                                 'serviceName' => $item->getName(),
  12289.                                 'serviceType' => 'Tecnología',
  12290.                                 'date' => $dateServ,
  12291.                                 'qty' => $unitsServ,
  12292.                                 'iva' => $item->getIva(),
  12293.                                 'pax' => $item->getPax(),
  12294.                                 'precioUnit' => $subnetoUnit,
  12295.                                 'subneto' => $subneto,
  12296.                                 'subtotal' => $subtotal,
  12297.                             );
  12298.                             break;
  12299.                         case 15: //Assisstant
  12300.                             $pax = $item->getPax();
  12301.                             if (empty($pax) or $pax == "0") {
  12302.                                 $pax = 1;
  12303.                             }
  12304.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12305.                             if ($days > 1) {
  12306.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12307.                             } else {
  12308.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12309.                             }
  12310.                             $unitsServ = $item->getUnits();
  12311.                             if (empty($unitsServ) or $unitsServ == "0") {
  12312.                                 $unitsServ = 1;
  12313.                             }
  12314.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12315.                             $subnetoUnit = $subneto;
  12316.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12317.                             $data_supplier['service'][$i] = array(
  12318.                                 'id' => $item->getId(),
  12319.                                 'serviceCatId' => $item->getServiceCatId(),
  12320.                                 'serviceName' => $item->getName(),
  12321.                                 'serviceType' => 'Asistente',
  12322.                                 'date' => $dateServ,
  12323.                                 'qty' => $unitsServ,
  12324.                                 'iva' => $item->getIva(),
  12325.                                 'pax' => $item->getPax(),
  12326.                                 'precioUnit' => $subnetoUnit,
  12327.                                 'subneto' => $subneto,
  12328.                                 'subtotal' => $subtotal,
  12329.                             );
  12330.                             break;
  12331.                         case 16: //DDR
  12332.                             $pax = $item->getPax();
  12333.                             if (empty($pax) or $pax == "0") {
  12334.                                 $pax = 1;
  12335.                             }
  12336.                             $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12337.                             if ($days > 1) {
  12338.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y') . ' - ' . ($item->getDateOutAt())->format('d/m/Y');
  12339.                             } else {
  12340.                                 $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12341.                             }
  12342.                             $unitsServ = $item->getUnits();
  12343.                             if (empty($unitsServ) or $unitsServ == "0") {
  12344.                                 $unitsServ = 1;
  12345.                             }
  12346.                             $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12347.                             $subnetoUnit = $subneto;
  12348.                             $subneto = $subneto * $days * $unitsServ * $pax;
  12349.                             $data_supplier['service'][$i] = array(
  12350.                                 'id' => $item->getId(),
  12351.                                 'serviceCatId' => $item->getServiceCatId(),
  12352.                                 'serviceName' => $item->getName(),
  12353.                                 'serviceType' => 'DDR',
  12354.                                 'date' => $dateServ,
  12355.                                 'qty' => $unitsServ,
  12356.                                 'iva' => $item->getIva(),
  12357.                                 'pax' => $item->getPax(),
  12358.                                 'precioUnit' => $subnetoUnit,
  12359.                                 'subneto' => $subneto,
  12360.                                 'subtotal' => $subtotal,
  12361.                             );
  12362.                             break;
  12363.                         default:
  12364.                             break;
  12365.                     }
  12366.                     // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  12367.                     $subtotal = round($subtotal, 2, PHP_ROUND_HALF_UP);
  12368.                     $neto = round($subneto, 2, PHP_ROUND_HALF_UP);
  12369.                     $arrayItems[] = array(
  12370.                         'type' => 'Service',
  12371.                         'name' => $item->getName(),
  12372.                         'amount' => $neto,
  12373.                         'iva' => $item->getIva(),
  12374.                         'total' => $subtotal,
  12375.                     );
  12376.                 }
  12377.                 switch ($item->getIva()) {
  12378.                     // Acumula IVA
  12379.                     case 21:
  12380.                         $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva() / 100));
  12381.                         break;
  12382.                     case 10:
  12383.                         $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva() / 100));
  12384.                         break;
  12385.                     case 0:
  12386.                         break;
  12387.                     default:
  12388.                         break;
  12389.                 }
  12390.             }
  12391.             $totales_neto_all = $totales_neto_all + $neto;
  12392.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12393.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  12394.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  12395.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  12396.             // Acumula netos totales e IVA
  12397.             $service['neto'] = $service['neto'] + $neto;
  12398.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  12399.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12400.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  12401.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  12402.             $i++;
  12403.         }
  12404.         $data_supplier['serviceSubTotal'] = array(
  12405.             'neto' => $service['neto'],
  12406.             'sumSubT' => $service['sumSubT'],
  12407.         );
  12408.         $currency = '€';
  12409.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  12410.         if (!empty($payments)) {
  12411.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  12412.         } else {
  12413.             $amount_pay = 0;
  12414.         }
  12415.         $totales_all = $totales_total - $amount_pay;
  12416.         if (empty($dateDocument)){
  12417.             $dateDocument = new DateTime('now');
  12418.         }
  12419.         $data = array(
  12420.             'id' => $fileId,
  12421.             'type' => $type,
  12422.             'number' => $number,
  12423.             'prefix' => 'MDV-'.(new DateTime('now'))->format('dmy').'-'.$invoiceId,
  12424.             'date' => $dateDocument,
  12425.             'file' => $file,
  12426.             'token' => $file->getAccessKey(),
  12427.             'company' => $company,
  12428.             'clients' => $client,
  12429.             'arrayItems' => $arrayItems,
  12430.             'datasupplier' => $data_supplier,
  12431.             'currency' => $currency,
  12432.             'totales_neto' => $totales_neto_all,
  12433.             'bases_imponibles' => $data_iva,
  12434.             'totales' => $totales_total,
  12435.             'balance' => $totales_all,
  12436.             'paymentInvoice' => $amount_pay,
  12437.         );
  12438.         return $data;
  12439.     }
  12440.     private function sendTelegram ( $id, $text )
  12441.     {
  12442.         $em = $this->getDoctrine()->getManager();
  12443.         $telegUser = $em->getRepository(MdvTelegramUser::class)->findOneByUserId($id);
  12444.         if (empty($telegUser)){return true;}
  12445.         $parameters = array(
  12446.             'chat_id' => $telegUser->getChatId(),
  12447.             'text' => $text,
  12448.         );
  12449.         $bot_token = $telegUser->getBotToken();
  12450.         $url = "https://api.telegram.org/bot$bot_token/sendMessage";
  12451.         if (!$curl = curl_init()){
  12452.             exit();
  12453.         }
  12454.         curl_setopt($curl,CURLOPT_POST,true);
  12455.         curl_setopt($curl,CURLOPT_POSTFIELDS,$parameters);
  12456.         curl_setopt($curl,CURLOPT_URL,$url);
  12457.         curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
  12458.         $output = curl_exec($curl);
  12459.         curl_close($curl);
  12460.         return true;
  12461.     }
  12462.     private function baseProformaSelect($id, $type, $number, $prefix, $date, $proformaId)
  12463.     {
  12464.         $em = $this->getDoctrine()->getManager();
  12465.         $file = $em->getRepository(MdvFiles::class)->findOneById($id);
  12466.         $company = $em->getRepository(SettingsCompany::class)->findOneByPriority('4');
  12467.         $client = $em->getRepository(Client::class)->findById($file->getClient());
  12468.         if (empty($client)){
  12469.             $client[0] = new Client();
  12470.             $client[0]->setName('');
  12471.             $client[0]->setTitle('');
  12472.             $client[0]->setIdDocument('');
  12473.             $client[0]->setPopulation('');
  12474.             $client[0]->setRegion('');
  12475.             $client[0]->setCountry('');
  12476.         } else {
  12477.             if (is_numeric($client[0]->getPopulation())){
  12478.                 $city = ($em->getRepository(Cities::class)->findOneById($client[0]->getPopulation()));
  12479.                 $client[0]->setPopulation($city->getCity());
  12480.             }
  12481.             if (is_numeric($client[0]->getRegion())){
  12482.                 $region = ($em->getRepository(Regions::class)->findOneById($client[0]->getRegion()));
  12483.                 $client[0]->setRegion($region->getRegion());
  12484.             }
  12485.             if (is_numeric($client[0]->getCountry())){
  12486.                 $country = ($em->getRepository(Country::class)->findOneById($client[0]->getCountry()));
  12487.                 $client[0]->setCountry($country->getCountry());
  12488.             }
  12489.         }
  12490.         // Acumuladores de los calculos
  12491.         $totales_neto_all = 0;
  12492.         $data_iva = array(
  12493.             'iva' => 21,
  12494.             'ivaMontoVeintiUno' => 0,
  12495.             'ivaMontoDiez' => 0,
  12496.             'ivaMontoCero' => 0,
  12497.         );
  12498.         // Buscamos las salas reservadas, pagos y servicios para el evento
  12499.         // Buscamos los productos de la proforma
  12500.         $productsInProforma = $em->getRepository(MdvDocProformaItems::class)->findByProformaId($proformaId);
  12501.         $productsInFile = array();
  12502.         foreach ($productsInProforma as $item){
  12503.             $producto = $em->getRepository(MdvProductFile::class)->findOneById($item->getControlId());
  12504.             if (!empty($producto)){ $productsInFile[] = $producto; }
  12505.         }
  12506. //        $productsInFile = $em->getRepository(MdvProductFile::class)->findByFileId($id);
  12507.         $payments = $em->getRepository(MdvPaymentsClient::class)->findByFileId($id);
  12508.         $services = $em->getRepository(MdvServices::class)->findByFileId($id);
  12509.         $data_supplier = array();
  12510.         $i = 0;
  12511.         $iva = '21';            // Esteban Rincon: "Por Ley de localización del impuesto, siempre será un 21%"
  12512.         $pax = '1';
  12513.         $qty = '1';
  12514.         $product = array(
  12515.             'neto' => 0,
  12516.             'sumSubT' => 0,
  12517.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  12518.         $service = array(
  12519.             'neto' => 0,
  12520.             'sumSubT' => 0,
  12521.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  12522.         foreach ($productsInFile as $item){
  12523. //            $days = '1';
  12524. //            $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12525.             $days = ((($item->getDateEnd())->diff($item->getDateStart()))->days + 1);
  12526.             // Verificamos que el producto no se encuentre ya en una factura
  12527.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findByPrdControlId($item->getId());
  12528.             $pax = $item->getPax();
  12529.             $qty = $item->getUnits();
  12530.             if (!empty($item->getDays())){ $days = $item->getDays();}
  12531.             if (is_null($item->getServicePrice()) or empty($item->getServicePrice()) or !empty($existe)){
  12532.                 $subtotal = 0;
  12533.                 $neto = 0;
  12534.                 $subtotalProduct = 0;
  12535.             } else {
  12536. //                $subtotalProduct = $item->getServicePrice() * 1.21;
  12537.                 $subtotalProduct = $item->getSubTotalPrice() * 1.21;
  12538. //                $subneto = $item->getServicePrice();
  12539.                 $subneto = $item->getSubTotalPrice();
  12540.                 $subtotal =  $subtotalProduct;
  12541.                 $neto =  $subneto;
  12542.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  12543.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  12544.                 $neto = round($neto,2,PHP_ROUND_HALF_UP);
  12545.             }
  12546.             // Acumula netos totales e IVA
  12547.             $totales_neto_all = $totales_neto_all + $neto;
  12548.             $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * 0.21);
  12549.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12550.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  12551.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  12552.             // Acumula netos totales e IVA
  12553.             $product['neto'] = $product['neto'] + $neto;
  12554.             $product['sumSubT'] = $product['sumSubT'] + $subtotal;
  12555.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12556.             $product['neto'] = round($product['neto'],2,PHP_ROUND_HALF_UP);
  12557.             $product['sumSubT'] = round($product['sumSubT'],2,PHP_ROUND_HALF_UP);
  12558.             $data_supplier['product'][$i] = array (
  12559.                 'id' => $item->getId(),
  12560.                 'productName' => $item->getProductName(),
  12561.                 'productId' => $item->getProductId(),
  12562.                 'dateStart' => $item->getDateStart(),
  12563.                 'dateEnd' => $item->getDateEnd(),
  12564.                 'servicePrice' => $item->getServicePrice(),
  12565.                 'subTotalPrice' => $item->getSubTotalPrice(),
  12566.                 'subtotalProduct' => $subtotalProduct,
  12567.                 'iva' => $iva,
  12568.                 'pax' => $pax,
  12569.                 'qty' => $qty,
  12570.                 'days' => $days,
  12571.                 'type' => $item->getType(),
  12572.                 'subtotal' => $subtotal,
  12573.             );
  12574.             $i++;
  12575.         }
  12576.         $data_supplier['productSubTotal'] = array(
  12577.             'neto' => $product['neto'],
  12578.             'sumSubT' => $product['sumSubT'],
  12579.         );
  12580.         $payment = array(
  12581.             'sumSubT' => 0,
  12582.         ); // Acumula sumatoria de netos y sumantoria de subtotales
  12583.         $i = 0;
  12584.         foreach ($payments as $item){
  12585.             // Verificamos que el pago no se encuentre ya en una factura
  12586.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findByPayControlId($item->getId());
  12587.             if (!empty($existe)){
  12588.                 $payment['sumSubT'] = $payment['sumSubT'] + 0;
  12589.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12590.                 $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  12591.             } else {
  12592.                 $payment['sumSubT'] = $payment['sumSubT'] + $item->getAmountTotal();
  12593.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  12594.                 $payment['sumSubT'] = round($payment['sumSubT'],2,PHP_ROUND_HALF_UP);
  12595.             }
  12596.             $data_supplier['payment'][$i] = array (
  12597.                 'id' => $item->getId(),
  12598.                 'amount' => $item->getAmount(),
  12599.                 'datePayAt' => $item->getDatePayAt(),
  12600.                 'wayToPay' => $item->getWayToPay(),
  12601.                 'amountTotal' => $item->getAmountTotal(),
  12602.                 'vat' => $item->getVat(),
  12603.             );
  12604.             $i++;
  12605.         }
  12606.         if (!empty($payments)) {
  12607.             $data_supplier['paymentSubTotal'] = array(
  12608.                 'sumSubT' => $payment['sumSubT'],
  12609.             );
  12610.         }
  12611.         foreach ($services as $item){
  12612.             // Verificamos que el servicio no se encuentre ya en una factura
  12613.             $existe = $em->getRepository(MdvDocInvoiceItems::class)->findBySrvControlId($item->getId());
  12614.             if (is_null($item->getPrice()) or empty($item->getPrice()) or !empty($existe)){
  12615.                 $subtotal = 0;
  12616.                 $neto = 0;
  12617.                 $subtotalService = 0;
  12618.                 $subneto = 0;
  12619.                 switch ($item->getServiceCatId()){
  12620.                     case 1: // Alojamiento
  12621.                         // el numero de noches $numNoches; precio unitario $subneto
  12622.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  12623.                         // La personas no afectan este calculo
  12624.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  12625.                         $subnetoUnit = $subneto;
  12626.                         $subneto = $subneto * $numNoches * $item->getUnits();
  12627.                         $data_supplier['service'][$i] = array (
  12628.                             'id' => $item->getId(),
  12629.                             'serviceCatId' => $item->getServiceCatId(),
  12630.                             'serviceName' => $item->getName(),
  12631.                             'serviceType' => 'Hotel',
  12632.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  12633.                             'qty' => $item->getUnits(),
  12634.                             'iva' => $item->getIva(),
  12635.                             'pax' => '-',
  12636.                             'precioUnit' => $subnetoUnit,
  12637.                             'subneto' => $subneto,
  12638.                             'subtotal' => $subtotal,
  12639.                         );
  12640.                         break;
  12641.                     case 2: //Actividades
  12642.                         // El número de personas es considerado en el calculo
  12643.                         $pax = $item->getPax();
  12644.                         if (empty($pax) or $pax == "0") {
  12645.                             $pax = 1;
  12646.                         }
  12647.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12648.                         if ($days > 1){
  12649.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12650.                         } else {
  12651.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12652.                         }
  12653.                         $subtotal = $subtotalService * $days * $item->getUnits();
  12654.                         $subnetoUnit = $subneto;
  12655.                         $subneto = $subneto * $days * $item->getUnits();
  12656.                         $data_supplier['service'][$i] = array (
  12657.                             'id' => $item->getId(),
  12658.                             'serviceCatId' => $item->getServiceCatId(),
  12659.                             'serviceName' => $item->getName(),
  12660.                             'serviceType' => 'Actividad',
  12661.                             'date' => $dateServ,
  12662.                             'qty' => $item->getUnits(),
  12663.                             'iva' => $item->getIva(),
  12664.                             'pax' => $item->getPax(),
  12665.                             'precioUnit' => $subnetoUnit,
  12666.                             'subneto' => $subneto,
  12667.                             'subtotal' => $subtotal,
  12668.                         );
  12669.                         break;
  12670.                     case 3: // AV
  12671.                         $pax = $item->getPax();
  12672.                         if (empty($pax) or $pax == "0") {
  12673.                             $pax = 1;
  12674.                         }
  12675.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12676.                         if ($days > 1){
  12677.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12678.                         } else {
  12679.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12680.                         }
  12681.                         $unitsServ = $item->getUnits();
  12682.                         if (empty($unitsServ) or $unitsServ == "0") {
  12683.                             $unitsServ = 1;
  12684.                         }
  12685.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12686.                         $subnetoUnit = $subneto;
  12687.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12688.                         $data_supplier['service'][$i] = array (
  12689.                             'id' => $item->getId(),
  12690.                             'serviceCatId' => $item->getServiceCatId(),
  12691.                             'serviceName' => $item->getName(),
  12692.                             'serviceType' => 'AV',
  12693.                             'date' => $dateServ,
  12694.                             'qty' => $unitsServ,
  12695.                             'iva' => $item->getIva(),
  12696.                             'pax' => $item->getPax(),
  12697.                             'precioUnit' => $subnetoUnit,
  12698.                             'subneto' => $subneto,
  12699.                             'subtotal' => $subtotal,
  12700.                         );
  12701.                         break;
  12702.                     case 4: //Creative
  12703.                         $pax = $item->getPax();
  12704.                         if (empty($pax) or $pax == "0") {
  12705.                             $pax = 1;
  12706.                         }
  12707.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12708.                         if ($days > 1){
  12709.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12710.                         } else {
  12711.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12712.                         }
  12713.                         $unitsServ = $item->getUnits();
  12714.                         if (empty($unitsServ) or $unitsServ == "0") {
  12715.                             $unitsServ = 1;
  12716.                         }
  12717.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12718.                         $subnetoUnit = $subneto;
  12719.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12720.                         $data_supplier['service'][$i] = array (
  12721.                             'id' => $item->getId(),
  12722.                             'serviceCatId' => $item->getServiceCatId(),
  12723.                             'serviceName' => $item->getName(),
  12724.                             'serviceType' => 'Creativo',
  12725.                             'date' => $dateServ,
  12726.                             'qty' => $unitsServ,
  12727.                             'iva' => $item->getIva(),
  12728.                             'pax' => $item->getPax(),
  12729.                             'precioUnit' => $subnetoUnit,
  12730.                             'subneto' => $subneto,
  12731.                             'subtotal' => $subtotal,
  12732.                         );
  12733.                         break;
  12734.                     case 5: //Cruise
  12735.                         $pax = $item->getPax();
  12736.                         if (empty($pax) or $pax == "0") {
  12737.                             $pax = 1;
  12738.                         }
  12739.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  12740.                         if ($days > 1){
  12741.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12742.                         } else {
  12743.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12744.                         }
  12745.                         $unitsServ = $item->getUnits();
  12746.                         if (empty($unitsServ) or $unitsServ == "0") {
  12747.                             $unitsServ = 1;
  12748.                         }
  12749.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12750.                         $subnetoUnit = $subneto;
  12751.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12752.                         $data_supplier['service'][$i] = array (
  12753.                             'id' => $item->getId(),
  12754.                             'serviceCatId' => $item->getServiceCatId(),
  12755.                             'serviceName' => $item->getName(),
  12756.                             'serviceType' => 'Crucero',
  12757.                             'date' => $dateServ,
  12758.                             'qty' => $unitsServ,
  12759.                             'iva' => $item->getIva(),
  12760.                             'pax' => $item->getPax(),
  12761.                             'precioUnit' => $subnetoUnit,
  12762.                             'subneto' => $subneto,
  12763.                             'subtotal' => $subtotal,
  12764.                         );
  12765.                         break;
  12766.                     case 6: //Entertaiment
  12767.                         $pax = $item->getPax();
  12768.                         if (empty($pax) or $pax == "0") {
  12769.                             $pax = 1;
  12770.                         }
  12771.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12772.                         if ($days > 1){
  12773.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12774.                         } else {
  12775.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12776.                         }
  12777.                         $unitsServ = $item->getUnits();
  12778.                         if (empty($unitsServ) or $unitsServ == "0") {
  12779.                             $unitsServ = 1;
  12780.                         }
  12781.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12782.                         $subnetoUnit = $subneto;
  12783.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12784.                         $data_supplier['service'][$i] = array (
  12785.                             'id' => $item->getId(),
  12786.                             'serviceCatId' => $item->getServiceCatId(),
  12787.                             'serviceName' => $item->getName(),
  12788.                             'serviceType' => 'Entretenimiento',
  12789.                             'date' => $dateServ,
  12790.                             'qty' => $unitsServ,
  12791.                             'iva' => $item->getIva(),
  12792.                             'pax' => $item->getPax(),
  12793.                             'precioUnit' => $subnetoUnit,
  12794.                             'subneto' => $subneto,
  12795.                             'subtotal' => $subtotal,
  12796.                         );
  12797.                         break;
  12798.                     case 7: // Gifts
  12799.                         $pax = $item->getPax();
  12800.                         if (empty($pax) or $pax == "0") {
  12801.                             $pax = 1;
  12802.                         }
  12803.                         $days = 1;
  12804.                         if ($days > 1){
  12805.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12806.                         } else {
  12807.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12808.                         }
  12809.                         $unitsServ = $item->getUnits();
  12810.                         if (empty($unitsServ) or $unitsServ == "0") {
  12811.                             $unitsServ = 1;
  12812.                         }
  12813.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12814.                         $subnetoUnit = $subneto;
  12815.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12816.                         $data_supplier['service'][$i] = array (
  12817.                             'id' => $item->getId(),
  12818.                             'serviceCatId' => $item->getServiceCatId(),
  12819.                             'serviceName' => $item->getName(),
  12820.                             'serviceType' => 'Regalos',
  12821.                             'date' => $dateServ,
  12822.                             'qty' => $unitsServ,
  12823.                             'iva' => $item->getIva(),
  12824.                             'pax' => $item->getPax(),
  12825.                             'precioUnit' => $subnetoUnit,
  12826.                             'subneto' => $subneto,
  12827.                             'subtotal' => $subtotal,
  12828.                         );
  12829.                         break;
  12830.                     case 8: //Guide
  12831.                         $pax = $item->getPax();
  12832.                         if (empty($pax) or $pax == "0") {
  12833.                             $pax = 1;
  12834.                         }
  12835.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12836.                         if ($days > 1){
  12837.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12838.                         } else {
  12839.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12840.                         }
  12841.                         $unitsServ = $item->getUnits();
  12842.                         if (empty($unitsServ) or $unitsServ == "0") {
  12843.                             $unitsServ = 1;
  12844.                         }
  12845.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12846.                         $subnetoUnit = $subneto;
  12847.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12848.                         $data_supplier['service'][$i] = array (
  12849.                             'id' => $item->getId(),
  12850.                             'serviceCatId' => $item->getServiceCatId(),
  12851.                             'serviceName' => $item->getName(),
  12852.                             'serviceType' => 'Regalos',
  12853.                             'date' => $dateServ,
  12854.                             'qty' => $unitsServ,
  12855.                             'iva' => $item->getIva(),
  12856.                             'pax' => $item->getPax(),
  12857.                             'precioUnit' => $subnetoUnit,
  12858.                             'subneto' => $subneto,
  12859.                             'subtotal' => $subtotal,
  12860.                         );
  12861.                         break;
  12862.                     case 9: //Itineraries
  12863.                         $pax = $item->getPax();
  12864.                         if (empty($pax) or $pax == "0") {
  12865.                             $pax = 1;
  12866.                         }
  12867.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12868.                         if ($days > 1){
  12869.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12870.                         } else {
  12871.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12872.                         }
  12873.                         $unitsServ = $item->getUnits();
  12874.                         if (empty($unitsServ) or $unitsServ == "0") {
  12875.                             $unitsServ = 1;
  12876.                         }
  12877.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12878.                         $subnetoUnit = $subneto;
  12879.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12880.                         $data_supplier['service'][$i] = array (
  12881.                             'id' => $item->getId(),
  12882.                             'serviceCatId' => $item->getServiceCatId(),
  12883.                             'serviceName' => $item->getName(),
  12884.                             'serviceType' => 'Itinerarios',
  12885.                             'date' => $dateServ,
  12886.                             'qty' => $unitsServ,
  12887.                             'iva' => $item->getIva(),
  12888.                             'pax' => $item->getPax(),
  12889.                             'precioUnit' => $subnetoUnit,
  12890.                             'subneto' => $subneto,
  12891.                             'subtotal' => $subtotal,
  12892.                         );
  12893.                         break;
  12894.                     case 10: //Lounge  -- No Aplica
  12895. //                        $pax = $item->getPax();
  12896. //                        if (empty($pax) or $pax == "0") {
  12897. //                            $pax = 1;
  12898. //                        }
  12899. //
  12900. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12901. //                        if ($days > 1){
  12902. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12903. //                        } else {
  12904. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12905. //                        }
  12906. //
  12907. //                        $unitsServ = $item->getUnits();
  12908. //                        if (empty($unitsServ) or $unitsServ == "0") {
  12909. //                            $unitsServ = 1;
  12910. //                        }
  12911. //
  12912. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12913. //                        $subnetoUnit = $subneto;
  12914. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  12915. //
  12916. //                        $data_supplier['service'][$i] = array (
  12917. //                            'id' => $item->getId(),
  12918. //                            'serviceCatId' => $item->getServiceCatId(),
  12919. //                            'serviceName' => $item->getName(),
  12920. //                            'serviceType' => 'Itinerarios',
  12921. //                            'date' => $dateServ,
  12922. //                            'qty' => $unitsServ,
  12923. //                            'iva' => $item->getIva(),
  12924. //                            'pax' => $item->getPax(),
  12925. //                            'precioUnit' => $subnetoUnit,
  12926. //                            'subneto' => $subneto,
  12927. //                            'subtotal' => $subtotal,
  12928. //                        );
  12929.                         break;
  12930.                     case 11: //Menu
  12931.                         $pax = $item->getPax();
  12932.                         if (empty($pax) or $pax == "0") {
  12933.                             $pax = 1;
  12934.                         }
  12935.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12936.                         if ($days > 1){
  12937.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12938.                         } else {
  12939.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12940.                         }
  12941.                         $unitsServ = $item->getUnits();
  12942.                         if (empty($unitsServ) or $unitsServ == "0") {
  12943.                             $unitsServ = 1;
  12944.                         }
  12945.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12946.                         $subnetoUnit = $subneto;
  12947.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12948.                         $data_supplier['service'][$i] = array (
  12949.                             'id' => $item->getId(),
  12950.                             'serviceCatId' => $item->getServiceCatId(),
  12951.                             'serviceName' => $item->getName(),
  12952.                             'serviceType' => 'Menú',
  12953.                             'date' => $dateServ,
  12954.                             'qty' => $unitsServ,
  12955.                             'iva' => $item->getIva(),
  12956.                             'pax' => $item->getPax(),
  12957.                             'precioUnit' => $subnetoUnit,
  12958.                             'subneto' => $subneto,
  12959.                             'subtotal' => $subtotal,
  12960.                         );
  12961.                         break;
  12962.                     case 12: //Others
  12963.                         $pax = $item->getPax();
  12964.                         if (empty($pax) or $pax == "0") {
  12965.                             $pax = 1;
  12966.                         }
  12967.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  12968.                         if ($days > 1){
  12969.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  12970.                         } else {
  12971.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  12972.                         }
  12973.                         $unitsServ = $item->getUnits();
  12974.                         if (empty($unitsServ) or $unitsServ == "0") {
  12975.                             $unitsServ = 1;
  12976.                         }
  12977.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  12978.                         $subnetoUnit = $subneto;
  12979.                         $subneto = $subneto * $days * $unitsServ * $pax;
  12980.                         $data_supplier['service'][$i] = array (
  12981.                             'id' => $item->getId(),
  12982.                             'serviceCatId' => $item->getServiceCatId(),
  12983.                             'serviceName' => $item->getName(),
  12984.                             'serviceType' => 'Otros',
  12985.                             'date' => $dateServ,
  12986.                             'qty' => $unitsServ,
  12987.                             'iva' => $item->getIva(),
  12988.                             'pax' => $item->getPax(),
  12989.                             'precioUnit' => $subnetoUnit,
  12990.                             'subneto' => $subneto,
  12991.                             'subtotal' => $subtotal,
  12992.                         );
  12993.                         break;
  12994.                     case 13: //Transport
  12995.                         $pax = $item->getPax();
  12996.                         if (empty($pax) or $pax == "0") {
  12997.                             $pax = 1;
  12998.                         }
  12999.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13000.                         if ($days > 1){
  13001.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13002.                         } else {
  13003.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13004.                         }
  13005.                         $unitsServ = $item->getUnits();
  13006.                         if (empty($unitsServ) or $unitsServ == "0") {
  13007.                             $unitsServ = 1;
  13008.                         }
  13009.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13010.                         $subnetoUnit = $subneto;
  13011.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13012.                         $data_supplier['service'][$i] = array (
  13013.                             'id' => $item->getId(),
  13014.                             'serviceCatId' => $item->getServiceCatId(),
  13015.                             'serviceName' => $item->getName(),
  13016.                             'serviceType' => 'Transporte',
  13017.                             'date' => $dateServ,
  13018.                             'qty' => $unitsServ,
  13019.                             'iva' => $item->getIva(),
  13020.                             'pax' => $item->getPax(),
  13021.                             'precioUnit' => $subnetoUnit,
  13022.                             'subneto' => $subneto,
  13023.                             'subtotal' => $subtotal,
  13024.                         );
  13025.                         break;
  13026.                     case 14: //Technology
  13027.                         $pax = $item->getPax();
  13028.                         if (empty($pax) or $pax == "0") {
  13029.                             $pax = 1;
  13030.                         }
  13031.                         $days = 1;
  13032.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13033.                         $unitsServ = $item->getUnits();
  13034.                         if (empty($unitsServ) or $unitsServ == "0") {
  13035.                             $unitsServ = 1;
  13036.                         }
  13037.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13038.                         $subnetoUnit = $subneto;
  13039.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13040.                         $data_supplier['service'][$i] = array (
  13041.                             'id' => $item->getId(),
  13042.                             'serviceCatId' => $item->getServiceCatId(),
  13043.                             'serviceName' => $item->getName(),
  13044.                             'serviceType' => 'Tecnología',
  13045.                             'date' => $dateServ,
  13046.                             'qty' => $unitsServ,
  13047.                             'iva' => $item->getIva(),
  13048.                             'pax' => $item->getPax(),
  13049.                             'precioUnit' => $subnetoUnit,
  13050.                             'subneto' => $subneto,
  13051.                             'subtotal' => $subtotal,
  13052.                         );
  13053.                         break;
  13054.                     case 15: //Assisstant
  13055.                         $pax = $item->getPax();
  13056.                         if (empty($pax) or $pax == "0") {
  13057.                             $pax = 1;
  13058.                         }
  13059.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13060.                         if ($days > 1){
  13061.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13062.                         } else {
  13063.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13064.                         }
  13065.                         $unitsServ = $item->getUnits();
  13066.                         if (empty($unitsServ) or $unitsServ == "0") {
  13067.                             $unitsServ = 1;
  13068.                         }
  13069.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13070.                         $subnetoUnit = $subneto;
  13071.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13072.                         $data_supplier['service'][$i] = array (
  13073.                             'id' => $item->getId(),
  13074.                             'serviceCatId' => $item->getServiceCatId(),
  13075.                             'serviceName' => $item->getName(),
  13076.                             'serviceType' => 'Asistente',
  13077.                             'date' => $dateServ,
  13078.                             'qty' => $unitsServ,
  13079.                             'iva' => $item->getIva(),
  13080.                             'pax' => $item->getPax(),
  13081.                             'precioUnit' => $subnetoUnit,
  13082.                             'subneto' => $subneto,
  13083.                             'subtotal' => $subtotal,
  13084.                         );
  13085.                         break;
  13086.                     case 16: //DDR
  13087.                         $pax = $item->getPax();
  13088.                         if (empty($pax) or $pax == "0") {
  13089.                             $pax = 1;
  13090.                         }
  13091.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13092.                         if ($days > 1){
  13093.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13094.                         } else {
  13095.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13096.                         }
  13097.                         $unitsServ = $item->getUnits();
  13098.                         if (empty($unitsServ) or $unitsServ == "0") {
  13099.                             $unitsServ = 1;
  13100.                         }
  13101.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13102.                         $subnetoUnit = $subneto;
  13103.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13104.                         $data_supplier['service'][$i] = array (
  13105.                             'id' => $item->getId(),
  13106.                             'serviceCatId' => $item->getServiceCatId(),
  13107.                             'serviceName' => $item->getName(),
  13108.                             'serviceType' => 'DDR',
  13109.                             'date' => $dateServ,
  13110.                             'qty' => $unitsServ,
  13111.                             'iva' => $item->getIva(),
  13112.                             'pax' => $item->getPax(),
  13113.                             'precioUnit' => $subnetoUnit,
  13114.                             'subneto' => $subneto,
  13115.                             'subtotal' => $subtotal,
  13116.                         );
  13117.                         break;
  13118.                     default:
  13119.                         break;
  13120.                 }
  13121.             } else {
  13122.                 $subtotalService = $item->getPrice();
  13123.                 $subneto = $item->getPrice();
  13124.                 // Commission
  13125.                 if ($item->getOpCommission()=='1'){
  13126.                     $subtotalService = $subtotalService * (1 + ($item->getCommission()/100));
  13127.                     $subneto = $subneto  * (1 + ($item->getCommission()/100));
  13128.                 } else {
  13129.                     $subtotalService = $subtotalService * (1 - ($item->getCommission()/100));
  13130.                     $subneto = $subneto * (1 - ($item->getCommission()/100));
  13131.                 }
  13132.                 // Over
  13133.                 if ($item->getOpOver()=='1'){
  13134.                     $subtotalService = $subtotalService + $item->getOver();
  13135.                     $subneto = $subneto + $item->getOver();
  13136.                 } else {
  13137.                     $subtotalService = $subtotalService - $item->getOver();
  13138.                     $subneto = $subneto - $item->getOver();
  13139.                 }
  13140.                 // IVA
  13141.                 if ($item->getOpIva()=='1'){
  13142.                     $subtotalService = $subtotalService * (1 + ($item->getIva()/100));
  13143.                 } else {
  13144.                     $subtotalService = $item->getPrice();
  13145.                     $subneto = ($subneto * 100) / (100 + $item->getIva());
  13146.                 }
  13147.                 switch ($item->getServiceCatId()){
  13148.                     case 1: // Alojamiento
  13149.                         // el numero de noches $numNoches; precio unitario $subneto
  13150.                         $numNoches = (($item->getDateOutAt())->diff($item->getDateInAt()))->days;
  13151.                         // La personas no afectan este calculo
  13152.                         $subtotal = $subtotalService * $numNoches * $item->getUnits();
  13153.                         $subnetoUnit = $subneto;
  13154.                         $subneto = $subneto * $numNoches * $item->getUnits();
  13155.                         $data_supplier['service'][$i] = array (
  13156.                             'id' => $item->getId(),
  13157.                             'serviceCatId' => $item->getServiceCatId(),
  13158.                             'serviceName' => $item->getName(),
  13159.                             'serviceType' => 'Hotel',
  13160.                             'date' => ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y'),
  13161.                             'qty' => $item->getUnits(),
  13162.                             'iva' => $item->getIva(),
  13163.                             'pax' => '-',
  13164.                             'precioUnit' => $subnetoUnit,
  13165.                             'subneto' => $subneto,
  13166.                             'subtotal' => $subtotal,
  13167.                         );
  13168.                         break;
  13169.                     case 2: //Actividades
  13170.                         // El número de personas es considerado en el calculo
  13171.                         $pax = $item->getPax();
  13172.                         if (empty($pax) or $pax == "0") {
  13173.                             $pax = 1;
  13174.                         }
  13175.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13176.                         if ($days > 1){
  13177.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13178.                         } else {
  13179.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13180.                         }
  13181.                         $subtotal = $subtotalService * $days * $item->getUnits();
  13182.                         $subnetoUnit = $subneto;
  13183.                         $subneto = $subneto * $days * $item->getUnits();
  13184.                         $data_supplier['service'][$i] = array (
  13185.                             'id' => $item->getId(),
  13186.                             'serviceCatId' => $item->getServiceCatId(),
  13187.                             'serviceName' => $item->getName(),
  13188.                             'serviceType' => 'Actividad',
  13189.                             'date' => $dateServ,
  13190.                             'qty' => $item->getUnits(),
  13191.                             'iva' => $item->getIva(),
  13192.                             'pax' => $item->getPax(),
  13193.                             'precioUnit' => $subnetoUnit,
  13194.                             'subneto' => $subneto,
  13195.                             'subtotal' => $subtotal,
  13196.                         );
  13197.                         break;
  13198.                     case 3: // AV
  13199.                         $pax = $item->getPax();
  13200.                         if (empty($pax) or $pax == "0") {
  13201.                             $pax = 1;
  13202.                         }
  13203.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13204.                         if ($days > 1){
  13205.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13206.                         } else {
  13207.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13208.                         }
  13209.                         $unitsServ = $item->getUnits();
  13210.                         if (empty($unitsServ) or $unitsServ == "0") {
  13211.                             $unitsServ = 1;
  13212.                         }
  13213.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13214.                         $subnetoUnit = $subneto;
  13215.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13216.                         $data_supplier['service'][$i] = array (
  13217.                             'id' => $item->getId(),
  13218.                             'serviceCatId' => $item->getServiceCatId(),
  13219.                             'serviceName' => $item->getName(),
  13220.                             'serviceType' => 'AV',
  13221.                             'date' => $dateServ,
  13222.                             'qty' => $unitsServ,
  13223.                             'iva' => $item->getIva(),
  13224.                             'pax' => $item->getPax(),
  13225.                             'precioUnit' => $subnetoUnit,
  13226.                             'subneto' => $subneto,
  13227.                             'subtotal' => $subtotal,
  13228.                         );
  13229.                         break;
  13230.                     case 4: //Creative
  13231.                         $pax = $item->getPax();
  13232.                         if (empty($pax) or $pax == "0") {
  13233.                             $pax = 1;
  13234.                         }
  13235.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13236.                         if ($days > 1){
  13237.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13238.                         } else {
  13239.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13240.                         }
  13241.                         $unitsServ = $item->getUnits();
  13242.                         if (empty($unitsServ) or $unitsServ == "0") {
  13243.                             $unitsServ = 1;
  13244.                         }
  13245.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13246.                         $subnetoUnit = $subneto;
  13247.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13248.                         $data_supplier['service'][$i] = array (
  13249.                             'id' => $item->getId(),
  13250.                             'serviceCatId' => $item->getServiceCatId(),
  13251.                             'serviceName' => $item->getName(),
  13252.                             'serviceType' => 'Creativo',
  13253.                             'date' => $dateServ,
  13254.                             'qty' => $unitsServ,
  13255.                             'iva' => $item->getIva(),
  13256.                             'pax' => $item->getPax(),
  13257.                             'precioUnit' => $subnetoUnit,
  13258.                             'subneto' => $subneto,
  13259.                             'subtotal' => $subtotal,
  13260.                         );
  13261.                         break;
  13262.                     case 5: //Cruise
  13263.                         $pax = $item->getPax();
  13264.                         if (empty($pax) or $pax == "0") {
  13265.                             $pax = 1;
  13266.                         }
  13267.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days);
  13268.                         if ($days > 1){
  13269.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13270.                         } else {
  13271.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13272.                         }
  13273.                         $unitsServ = $item->getUnits();
  13274.                         if (empty($unitsServ) or $unitsServ == "0") {
  13275.                             $unitsServ = 1;
  13276.                         }
  13277.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13278.                         $subnetoUnit = $subneto;
  13279.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13280.                         $data_supplier['service'][$i] = array (
  13281.                             'id' => $item->getId(),
  13282.                             'serviceCatId' => $item->getServiceCatId(),
  13283.                             'serviceName' => $item->getName(),
  13284.                             'serviceType' => 'Crucero',
  13285.                             'date' => $dateServ,
  13286.                             'qty' => $unitsServ,
  13287.                             'iva' => $item->getIva(),
  13288.                             'pax' => $item->getPax(),
  13289.                             'precioUnit' => $subnetoUnit,
  13290.                             'subneto' => $subneto,
  13291.                             'subtotal' => $subtotal,
  13292.                         );
  13293.                         break;
  13294.                     case 6: //Entertaiment
  13295.                         $pax = $item->getPax();
  13296.                         if (empty($pax) or $pax == "0") {
  13297.                             $pax = 1;
  13298.                         }
  13299.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13300.                         if ($days > 1){
  13301.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13302.                         } else {
  13303.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13304.                         }
  13305.                         $unitsServ = $item->getUnits();
  13306.                         if (empty($unitsServ) or $unitsServ == "0") {
  13307.                             $unitsServ = 1;
  13308.                         }
  13309.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13310.                         $subnetoUnit = $subneto;
  13311.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13312.                         $data_supplier['service'][$i] = array (
  13313.                             'id' => $item->getId(),
  13314.                             'serviceCatId' => $item->getServiceCatId(),
  13315.                             'serviceName' => $item->getName(),
  13316.                             'serviceType' => 'Entretenimiento',
  13317.                             'date' => $dateServ,
  13318.                             'qty' => $unitsServ,
  13319.                             'iva' => $item->getIva(),
  13320.                             'pax' => $item->getPax(),
  13321.                             'precioUnit' => $subnetoUnit,
  13322.                             'subneto' => $subneto,
  13323.                             'subtotal' => $subtotal,
  13324.                         );
  13325.                         break;
  13326.                     case 7: // Gifts
  13327.                         $pax = $item->getPax();
  13328.                         if (empty($pax) or $pax == "0") {
  13329.                             $pax = 1;
  13330.                         }
  13331.                         $days = 1;
  13332.                         if ($days > 1){
  13333.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13334.                         } else {
  13335.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13336.                         }
  13337.                         $unitsServ = $item->getUnits();
  13338.                         if (empty($unitsServ) or $unitsServ == "0") {
  13339.                             $unitsServ = 1;
  13340.                         }
  13341.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13342.                         $subnetoUnit = $subneto;
  13343.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13344.                         $data_supplier['service'][$i] = array (
  13345.                             'id' => $item->getId(),
  13346.                             'serviceCatId' => $item->getServiceCatId(),
  13347.                             'serviceName' => $item->getName(),
  13348.                             'serviceType' => 'Regalos',
  13349.                             'date' => $dateServ,
  13350.                             'qty' => $unitsServ,
  13351.                             'iva' => $item->getIva(),
  13352.                             'pax' => $item->getPax(),
  13353.                             'precioUnit' => $subnetoUnit,
  13354.                             'subneto' => $subneto,
  13355.                             'subtotal' => $subtotal,
  13356.                         );
  13357.                         break;
  13358.                     case 8: //Guide
  13359.                         $pax = $item->getPax();
  13360.                         if (empty($pax) or $pax == "0") {
  13361.                             $pax = 1;
  13362.                         }
  13363.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13364.                         if ($days > 1){
  13365.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13366.                         } else {
  13367.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13368.                         }
  13369.                         $unitsServ = $item->getUnits();
  13370.                         if (empty($unitsServ) or $unitsServ == "0") {
  13371.                             $unitsServ = 1;
  13372.                         }
  13373.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13374.                         $subnetoUnit = $subneto;
  13375.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13376.                         $data_supplier['service'][$i] = array (
  13377.                             'id' => $item->getId(),
  13378.                             'serviceCatId' => $item->getServiceCatId(),
  13379.                             'serviceName' => $item->getName(),
  13380.                             'serviceType' => 'Regalos',
  13381.                             'date' => $dateServ,
  13382.                             'qty' => $unitsServ,
  13383.                             'iva' => $item->getIva(),
  13384.                             'pax' => $item->getPax(),
  13385.                             'precioUnit' => $subnetoUnit,
  13386.                             'subneto' => $subneto,
  13387.                             'subtotal' => $subtotal,
  13388.                         );
  13389.                         break;
  13390.                     case 9: //Itineraries
  13391.                         $pax = $item->getPax();
  13392.                         if (empty($pax) or $pax == "0") {
  13393.                             $pax = 1;
  13394.                         }
  13395.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13396.                         if ($days > 1){
  13397.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13398.                         } else {
  13399.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13400.                         }
  13401.                         $unitsServ = $item->getUnits();
  13402.                         if (empty($unitsServ) or $unitsServ == "0") {
  13403.                             $unitsServ = 1;
  13404.                         }
  13405.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13406.                         $subnetoUnit = $subneto;
  13407.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13408.                         $data_supplier['service'][$i] = array (
  13409.                             'id' => $item->getId(),
  13410.                             'serviceCatId' => $item->getServiceCatId(),
  13411.                             'serviceName' => $item->getName(),
  13412.                             'serviceType' => 'Itinerarios',
  13413.                             'date' => $dateServ,
  13414.                             'qty' => $unitsServ,
  13415.                             'iva' => $item->getIva(),
  13416.                             'pax' => $item->getPax(),
  13417.                             'precioUnit' => $subnetoUnit,
  13418.                             'subneto' => $subneto,
  13419.                             'subtotal' => $subtotal,
  13420.                         );
  13421.                         break;
  13422.                     case 10: //Lounge  -- No Aplica
  13423. //                        $pax = $item->getPax();
  13424. //                        if (empty($pax) or $pax == "0") {
  13425. //                            $pax = 1;
  13426. //                        }
  13427. //
  13428. //                        $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13429. //                        if ($days > 1){
  13430. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13431. //                        } else {
  13432. //                            $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13433. //                        }
  13434. //
  13435. //                        $unitsServ = $item->getUnits();
  13436. //                        if (empty($unitsServ) or $unitsServ == "0") {
  13437. //                            $unitsServ = 1;
  13438. //                        }
  13439. //
  13440. //                        $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13441. //                        $subnetoUnit = $subneto;
  13442. //                        $subneto = $subneto * $days * $unitsServ * $pax;
  13443. //
  13444. //                        $data_supplier['service'][$i] = array (
  13445. //                            'id' => $item->getId(),
  13446. //                            'serviceCatId' => $item->getServiceCatId(),
  13447. //                            'serviceName' => $item->getName(),
  13448. //                            'serviceType' => 'Itinerarios',
  13449. //                            'date' => $dateServ,
  13450. //                            'qty' => $unitsServ,
  13451. //                            'iva' => $item->getIva(),
  13452. //                            'pax' => $item->getPax(),
  13453. //                            'precioUnit' => $subnetoUnit,
  13454. //                            'subneto' => $subneto,
  13455. //                            'subtotal' => $subtotal,
  13456. //                        );
  13457.                         break;
  13458.                     case 11: //Menu
  13459.                         $pax = $item->getPax();
  13460.                         if (empty($pax) or $pax == "0") {
  13461.                             $pax = 1;
  13462.                         }
  13463.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13464.                         if ($days > 1){
  13465.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13466.                         } else {
  13467.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13468.                         }
  13469.                         $unitsServ = $item->getUnits();
  13470.                         if (empty($unitsServ) or $unitsServ == "0") {
  13471.                             $unitsServ = 1;
  13472.                         }
  13473.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13474.                         $subnetoUnit = $subneto;
  13475.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13476.                         $data_supplier['service'][$i] = array (
  13477.                             'id' => $item->getId(),
  13478.                             'serviceCatId' => $item->getServiceCatId(),
  13479.                             'serviceName' => $item->getName(),
  13480.                             'serviceType' => 'Menú',
  13481.                             'date' => $dateServ,
  13482.                             'qty' => $unitsServ,
  13483.                             'iva' => $item->getIva(),
  13484.                             'pax' => $item->getPax(),
  13485.                             'precioUnit' => $subnetoUnit,
  13486.                             'subneto' => $subneto,
  13487.                             'subtotal' => $subtotal,
  13488.                         );
  13489.                         break;
  13490.                     case 12: //Others
  13491.                         $pax = $item->getPax();
  13492.                         if (empty($pax) or $pax == "0") {
  13493.                             $pax = 1;
  13494.                         }
  13495.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13496.                         if ($days > 1){
  13497.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13498.                         } else {
  13499.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13500.                         }
  13501.                         $unitsServ = $item->getUnits();
  13502.                         if (empty($unitsServ) or $unitsServ == "0") {
  13503.                             $unitsServ = 1;
  13504.                         }
  13505.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13506.                         $subnetoUnit = $subneto;
  13507.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13508.                         $data_supplier['service'][$i] = array (
  13509.                             'id' => $item->getId(),
  13510.                             'serviceCatId' => $item->getServiceCatId(),
  13511.                             'serviceName' => $item->getName(),
  13512.                             'serviceType' => 'Otros',
  13513.                             'date' => $dateServ,
  13514.                             'qty' => $unitsServ,
  13515.                             'iva' => $item->getIva(),
  13516.                             'pax' => $item->getPax(),
  13517.                             'precioUnit' => $subnetoUnit,
  13518.                             'subneto' => $subneto,
  13519.                             'subtotal' => $subtotal,
  13520.                         );
  13521.                         break;
  13522.                     case 13: //Transport
  13523.                         $pax = $item->getPax();
  13524.                         if (empty($pax) or $pax == "0") {
  13525.                             $pax = 1;
  13526.                         }
  13527.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13528.                         if ($days > 1){
  13529.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13530.                         } else {
  13531.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13532.                         }
  13533.                         $unitsServ = $item->getUnits();
  13534.                         if (empty($unitsServ) or $unitsServ == "0") {
  13535.                             $unitsServ = 1;
  13536.                         }
  13537.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13538.                         $subnetoUnit = $subneto;
  13539.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13540.                         $data_supplier['service'][$i] = array (
  13541.                             'id' => $item->getId(),
  13542.                             'serviceCatId' => $item->getServiceCatId(),
  13543.                             'serviceName' => $item->getName(),
  13544.                             'serviceType' => 'Transporte',
  13545.                             'date' => $dateServ,
  13546.                             'qty' => $unitsServ,
  13547.                             'iva' => $item->getIva(),
  13548.                             'pax' => $item->getPax(),
  13549.                             'precioUnit' => $subnetoUnit,
  13550.                             'subneto' => $subneto,
  13551.                             'subtotal' => $subtotal,
  13552.                         );
  13553.                         break;
  13554.                     case 14: //Technology
  13555.                         $pax = $item->getPax();
  13556.                         if (empty($pax) or $pax == "0") {
  13557.                             $pax = 1;
  13558.                         }
  13559.                         $days = 1;
  13560.                         $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13561.                         $unitsServ = $item->getUnits();
  13562.                         if (empty($unitsServ) or $unitsServ == "0") {
  13563.                             $unitsServ = 1;
  13564.                         }
  13565.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13566.                         $subnetoUnit = $subneto;
  13567.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13568.                         $data_supplier['service'][$i] = array (
  13569.                             'id' => $item->getId(),
  13570.                             'serviceCatId' => $item->getServiceCatId(),
  13571.                             'serviceName' => $item->getName(),
  13572.                             'serviceType' => 'Tecnología',
  13573.                             'date' => $dateServ,
  13574.                             'qty' => $unitsServ,
  13575.                             'iva' => $item->getIva(),
  13576.                             'pax' => $item->getPax(),
  13577.                             'precioUnit' => $subnetoUnit,
  13578.                             'subneto' => $subneto,
  13579.                             'subtotal' => $subtotal,
  13580.                         );
  13581.                         break;
  13582.                     case 15: //Assisstant
  13583.                         $pax = $item->getPax();
  13584.                         if (empty($pax) or $pax == "0") {
  13585.                             $pax = 1;
  13586.                         }
  13587.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13588.                         if ($days > 1){
  13589.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13590.                         } else {
  13591.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13592.                         }
  13593.                         $unitsServ = $item->getUnits();
  13594.                         if (empty($unitsServ) or $unitsServ == "0") {
  13595.                             $unitsServ = 1;
  13596.                         }
  13597.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13598.                         $subnetoUnit = $subneto;
  13599.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13600.                         $data_supplier['service'][$i] = array (
  13601.                             'id' => $item->getId(),
  13602.                             'serviceCatId' => $item->getServiceCatId(),
  13603.                             'serviceName' => $item->getName(),
  13604.                             'serviceType' => 'Asistente',
  13605.                             'date' => $dateServ,
  13606.                             'qty' => $unitsServ,
  13607.                             'iva' => $item->getIva(),
  13608.                             'pax' => $item->getPax(),
  13609.                             'precioUnit' => $subnetoUnit,
  13610.                             'subneto' => $subneto,
  13611.                             'subtotal' => $subtotal,
  13612.                         );
  13613.                         break;
  13614.                     case 16: //DDR
  13615.                         $pax = $item->getPax();
  13616.                         if (empty($pax) or $pax == "0") {
  13617.                             $pax = 1;
  13618.                         }
  13619.                         $days = ((($item->getDateOutAt())->diff($item->getDateInAt()))->days + 1);
  13620.                         if ($days > 1){
  13621.                             $dateServ = ($item->getDateInAt())->format('d/m/Y'). ' - '.($item->getDateOutAt())->format('d/m/Y');
  13622.                         } else {
  13623.                             $dateServ = ($item->getDateInAt())->format('d/m/Y');
  13624.                         }
  13625.                         $unitsServ = $item->getUnits();
  13626.                         if (empty($unitsServ) or $unitsServ == "0") {
  13627.                             $unitsServ = 1;
  13628.                         }
  13629.                         $subtotal = $subtotalService * $days * $unitsServ * $pax;
  13630.                         $subnetoUnit = $subneto;
  13631.                         $subneto = $subneto * $days * $unitsServ * $pax;
  13632.                         $data_supplier['service'][$i] = array (
  13633.                             'id' => $item->getId(),
  13634.                             'serviceCatId' => $item->getServiceCatId(),
  13635.                             'serviceName' => $item->getName(),
  13636.                             'serviceType' => 'DDR',
  13637.                             'date' => $dateServ,
  13638.                             'qty' => $unitsServ,
  13639.                             'iva' => $item->getIva(),
  13640.                             'pax' => $item->getPax(),
  13641.                             'precioUnit' => $subnetoUnit,
  13642.                             'subneto' => $subneto,
  13643.                             'subtotal' => $subtotal,
  13644.                         );
  13645.                         break;
  13646.                     default:
  13647.                         break;
  13648.                 }
  13649.                 // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP),
  13650.                 $subtotal = round($subtotal,2,PHP_ROUND_HALF_UP);
  13651.                 $neto = round($subneto,2,PHP_ROUND_HALF_UP);
  13652.             }
  13653.             switch ($item->getIva()){
  13654.                 // Acumula IVA
  13655.                 case 21:
  13656.                     $data_iva['ivaMontoVeintiUno'] = $data_iva['ivaMontoVeintiUno'] + ($neto * ($item->getIva()/100));
  13657.                     break;
  13658.                 case 10:
  13659.                     $data_iva['ivaMontoDiez'] = $data_iva['ivaMontoDiez'] + ($neto * ($item->getIva()/100));
  13660.                     break;
  13661.                 case 0:
  13662.                     break;
  13663.                 default:
  13664.                     break;
  13665.             }
  13666.             $totales_neto_all = $totales_neto_all + $neto;
  13667.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  13668.             $totales_neto_all = round($totales_neto_all,2,PHP_ROUND_HALF_UP);
  13669.             $data_iva['ivaMontoVeintiUno'] = round($data_iva['ivaMontoVeintiUno'],2,PHP_ROUND_HALF_UP);
  13670.             $data_iva['ivaMontoDiez'] = round($data_iva['ivaMontoDiez'],2,PHP_ROUND_HALF_UP);
  13671.             // Acumula netos totales e IVA
  13672.             $service['neto'] = $service['neto'] + $neto;
  13673.             $service['sumSubT'] = $service['sumSubT'] + $subtotal;
  13674.             // Se lleva a 2 decimales round($totales_neto_antes,2,PHP_ROUND_HALF_UP)
  13675.             $service['neto'] = round($service['neto'],2,PHP_ROUND_HALF_UP);
  13676.             $service['sumSubT'] = round($service['sumSubT'],2,PHP_ROUND_HALF_UP);
  13677.             $i++;
  13678.         }
  13679.         $data_supplier['serviceSubTotal'] = array(
  13680.             'neto' => $service['neto'],
  13681.             'sumSubT' => $service['sumSubT'],
  13682.         );
  13683.         $currency = '€';
  13684.         $totales_total = $totales_neto_all + $data_iva['ivaMontoVeintiUno'] + $data_iva['ivaMontoDiez'];
  13685.         if (!empty($payments)) {
  13686.             $amount_pay = $data_supplier['paymentSubTotal']['sumSubT'];
  13687.         } else {
  13688.             $amount_pay = 0;
  13689.         }
  13690.         $totales_all = $totales_total - $amount_pay;
  13691.         $data = array(
  13692.             'id' => $id,
  13693.             'type' => $type,
  13694.             'number' => $number,
  13695.             'prefix' => $prefix,
  13696.             'date' => $date,
  13697.             'file' => $file,
  13698.             'token' =>'', // $file->getAccessKey(),
  13699.             'company' => $company,
  13700.             'clients' => $client,
  13701.             'datasupplier' => $data_supplier,
  13702.             'currency' => $currency,
  13703.             'totales_neto' => $totales_neto_all,
  13704.             'bases_imponibles' => $data_iva,
  13705.             'totales' => $totales_total,
  13706.             'balance' => $totales_all,
  13707.             'paymentInvoice' => $amount_pay,
  13708.         );
  13709.         return $data;
  13710.     }
  13711. };