src/Controller/higotrigo/HtInvoiceController.php line 728

Open in your IDE?
  1. <?php
  2. namespace App\Controller\higotrigo;
  3. use App\Entity\Client;
  4. use App\Entity\HtProforma;
  5. use App\Entity\HtProformaItems;
  6. use App\Entity\Supplier;
  7. use App\Entity\Country;
  8. use App\Entity\HtExtra;
  9. use App\Entity\HtFile;
  10. use App\Entity\HtInvoice;
  11. use App\Entity\HtInvoiceItems;
  12. use App\Entity\HtItem;
  13. use App\Entity\HtMenu;
  14. use App\Service\HtService;
  15. use App\Form\HtInvoiceType;
  16. use App\Repository\HtInvoiceRepository;
  17. use Google\Service\Dfareporting\Resource\Countries;
  18. use Google\Service\MigrationCenterAPI\DateTime;
  19. use Monolog\DateTimeImmutable;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use App\Entity\SettingsCompany;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. /**
  28.  * @Route("/higotrigo/ht/invoice")
  29.  */
  30. class HtInvoiceController extends AbstractController
  31. {
  32.     /**
  33.      * @Route("/", name="app_ht_invoice_index", methods={"GET"})
  34.      */
  35.     public function index(HtInvoiceRepository $htInvoiceRepository): Response
  36.     {
  37.         $htInvoices $htInvoiceRepository->findAll();
  38.         foreach ($htInvoices as $invoice) {
  39.             // Formatear el campo vatTotalGroup
  40.             $invoice->setVatTotalGroup($this->formatVatTotalGroup($invoice->getVatTotalGroup()));
  41.         }
  42.         return $this->render('higotrigo/ht_invoice/index.html.twig', [
  43.             'ht_invoices' => $htInvoices,
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/new", name="app_ht_invoice_new", methods={"GET", "POST"})
  48.      */
  49.     public function new(Request $requestHtInvoiceRepository $htInvoiceRepository): Response
  50.     {
  51.         $htInvoice = new HtInvoice();
  52.         $form $this->createForm(HtInvoiceType::class, $htInvoice);
  53.         $form->handleRequest($request);
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.             $htInvoiceRepository->add($htInvoicetrue);
  56.             return $this->redirectToRoute('app_ht_invoice_index', [], Response::HTTP_SEE_OTHER);
  57.         }
  58.         return $this->renderForm('higotrigo/ht_invoice/new.html.twig', [
  59.             'ht_invoice' => $htInvoice,
  60.             'form' => $form,
  61.         ]);
  62.     }
  63.     /**
  64.      * @Route("/{id}", name="app_ht_invoice_show", methods={"GET"})
  65.      */
  66.     public function show(HtInvoice $htInvoice): Response
  67.     {
  68.         $em $this->getDoctrine()->getManager();
  69.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  70.         $currency '€';
  71.         $boolToRec false;
  72.         return $this->render('higotrigo/ht_invoice/show.html.twig', [
  73.             'ht_invoice' => $htInvoice,
  74.             'company' => $company,
  75.             'currency' => $currency,
  76.             'boolToRec' => $boolToRec,
  77.         ]);
  78.     }
  79.     /**
  80.      * @Route("/{id}/edit", name="app_ht_invoice_edit", methods={"GET", "POST"})
  81.      */
  82.     public function edit(Request $requestHtInvoice $htInvoiceHtInvoiceRepository $htInvoiceRepository): Response
  83.     {
  84.         $form $this->createForm(HtInvoiceType::class, $htInvoice);
  85.         $form->handleRequest($request);
  86.         if ($form->isSubmitted() && $form->isValid()) {
  87.             $htInvoiceRepository->add($htInvoicetrue);
  88.             return $this->redirectToRoute('app_ht_invoice_index', [], Response::HTTP_SEE_OTHER);
  89.         }
  90.         return $this->renderForm('higotrigo/ht_invoice/edit.html.twig', [
  91.             'ht_invoice' => $htInvoice,
  92.             'form' => $form,
  93.         ]);
  94.     }
  95.     /**
  96.      * @Route("/{id}", name="app_ht_invoice_delete", methods={"POST"})
  97.      */
  98.     public function delete(Request $requestHtInvoice $htInvoiceHtInvoiceRepository $htInvoiceRepository): Response
  99.     {
  100.         if ($this->isCsrfTokenValid('delete'.$htInvoice->getId(), $request->request->get('_token'))) {
  101.             $htInvoiceRepository->remove($htInvoicetrue);
  102.         }
  103.         return $this->redirectToRoute('app_ht_invoice_index', [], Response::HTTP_SEE_OTHER);
  104.     }
  105.     /**
  106.      * @Route("/selinvoice/{id}",  name="ht_select_invoice")
  107.      * Seleccionar los elementos que pasaran a la proforma o factura
  108.      */
  109.     public function selectItemsInvoiceAction(HtFile $htFileEntityManagerInterface $emRequest $request)
  110.     {
  111.         $beneficio $request->query->get('bn');
  112.         // hfile => htitem => htMenu => htExtra ; htMenu => htMenuEscandallo => escandallo
  113.         $data = array();
  114.         $data['file'] = $htFile;
  115.         $allHtItems $htFile->getHtItems();
  116.         $allHtMenus = array();
  117.         $allHtExtrasinFile = array();
  118.         $allHtEscandallos = array();
  119.         $beneficioItem 0;
  120.         $indiceBeneficio = -1;
  121.         foreach ($allHtItems as $item) {
  122.             $indiceBeneficio ++;
  123.             $data['items'][$item->getId()]['item'] = $item;
  124.             $data['items'][$item->getId()]['item_beneficio'] = $beneficio[$indiceBeneficio];
  125.             $allHtExtras $item->getHtExtras();
  126.             foreach ($allHtExtras as $htSinlgeExtra){
  127.                 $htExtraIsInvoiced $em->getRepository(HtInvoiceItems::class)->findBy(array('itemType' => 'htExtra''itemIdOrigin' => $htSinlgeExtra->getId()));
  128.                 $boolIsInvoiced = ((count($htExtraIsInvoiced)) % == 0) ? false true;
  129.                 $data['items'][$item->getId()]['extras'][] = $htSinlgeExtra;
  130.                 if ($htSinlgeExtra->isAppearInvoice()) {
  131.                     $extra_price_no_vat $htSinlgeExtra->isOpOverCommission() ? $htSinlgeExtra->getPrice() * ($htSinlgeExtra->getOverCommission() /100) : ($htSinlgeExtra->getPrice() + $htSinlgeExtra->getOverCommission());
  132.                     $extra_price $extra_price_no_vat * (+ ($htSinlgeExtra->getVat()/100));
  133.                     $allHtExtrasinFile[] = array(
  134.                         'enabledToInvoice' => !$boolIsInvoiced,
  135.                         'extra' => $htSinlgeExtra,
  136.                         'extra_price_no_vat' => ($boolIsInvoiced) ? 0$extra_price_no_vat,
  137.                         'extra_price' => ($boolIsInvoiced) ? 0$extra_price,
  138.                     );
  139.                 }
  140.             }
  141.             $htMenus $item->getHtMenus();
  142.             if (!empty($htMenus)){
  143.                 foreach ($htMenus as $htMenu) {
  144.                     $allHtMenus[] = $htMenu;
  145.                     $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['menu_info'] = $htMenu;
  146.                     $htExtras $htMenu->getHtExtras();
  147.                     if (!empty($htExtras)) {
  148.                         foreach ($htExtras as $elem) {
  149.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['extras'][] = $elem;
  150.                             $allHtExtras[] = $elem;
  151.                         }
  152.                     }
  153.                     $escandallo $htMenu->getEscandallo();
  154.                     if (!empty($escandallo)) {
  155.                         $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] = null;
  156.                         foreach ($escandallo as $escan) {
  157.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos'][] = $escan;
  158.                             $allHtEscandallos[] = $escan;
  159.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] = (empty($data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'])) ? ($escan->getNombre()) : ($data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] . ', ' $escan->getNombre());
  160.                         }
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  166.         $data['client'] = $em->getRepository(Client::class)->findOneById($htFile->getClient());
  167.         $data_iva = array();
  168.         $data_iva['ivaMontoVeintiUno'] = 0;
  169.         $data_iva['ivaMontoDiez'] = 0;
  170.         $data_iva['ivaMontoCero'] = 0;
  171.         $netoSum 0$totalSum 0;
  172.         $indiceBeneficio = -1;
  173.         if (array_key_exists("items"$data)) {
  174.             foreach ($data['items'] as $itemFac) {
  175.                 if (array_key_exists("menus"$itemFac)) {
  176.                     $indiceBeneficio++;
  177.                     foreach ($itemFac['menus'] as $htMenu) {
  178.                         $productName $htMenu['menu']['menu_info']->getName() . ' ( ' $htMenu['menu']['escandallos_names'] . ' )';
  179.                         $servicePrice = (($htMenu['menu']['menu_info']->getTotalPrice() * 100) / (100 $htMenu['menu']['menu_info']->getIva()));
  180.                         $subTotalPrice round((($htMenu['menu']['menu_info']->getTotalPrice() * 100) / (100 $htMenu['menu']['menu_info']->getIva())), 2) * $itemFac['item']->getPax();
  181.                         $subtotalProduct $subTotalPrice * (+ ($htMenu['menu']['menu_info']->getIva() / 100));
  182.                         $ivaAmount = ($htMenu['menu']['menu_info']->getIva() * $subTotalPrice) / 100;
  183.                         $htMenuIsInvoiced $em->getRepository(HtInvoiceItems::class)->findBy(array('itemType' => 'htMenu''itemIdOrigin' => $htMenu['menu']['menu_info']->getId()));
  184.                         $boolIsInvoiced = ((count($htMenuIsInvoiced)) % == 0) ? false true;
  185.                         if (!$boolIsInvoiced) {
  186.                             $netoSum += $subTotalPrice;
  187.                             $totalSum += $subtotalProduct;
  188.                             switch ($htMenu['menu']['menu_info']->getIva()) {
  189.                                 case 10$data_iva['ivaMontoDiez'] += $ivaAmount; break;
  190.                                 case 21$data_iva['ivaMontoVeintiUno'] += $ivaAmount; break;
  191.                                 default: break;
  192.                             }
  193.                         }
  194.                         $data['product'][] = array(
  195.                             'id' => $htMenu['menu']['menu_info']->getId(),     // Id del Menu
  196.                             'idHtItem' => $itemFac['item']->getId(),            // Id del Servicio (HtItem) que contiene el Menu
  197.                             'enabledToInvoice' => !$boolIsInvoiced,
  198.                             'productName' => $productName,
  199.                             'type' => null,
  200.                             'servicePrice' => $servicePrice,
  201.                             'qty' => $itemFac['item']->getPax(),
  202.                             'days' => $itemFac['item']->getDateStart()->format('d/m/Y'),
  203.                             'subTotalPrice' => ($boolIsInvoiced) ? $subTotalPrice,
  204.                             'iva' => $htMenu['menu']['menu_info']->getIva(),
  205.                             'ivaAmount' => $ivaAmount,
  206.                             'subtotalProduct' => ($boolIsInvoiced) ? $subtotalProduct,
  207.                             'beneficio' => $beneficio[$indiceBeneficio],
  208.                             'productSubTotal' => array(
  209.                                 'neto' => null,
  210.                                 'sumSubT' => null,
  211.                             ),
  212.                         );
  213.                     }
  214.                 }
  215.             }
  216.         }
  217.         $netoAllSum $netoSum;
  218.         $totalSumItems $totalSum;
  219.         //  SERVICIOS ADICIONALES
  220.         $data_extras = array();
  221.         if (!empty($allHtExtrasinFile)){ $data_extras['allHtExtrasinFile'] = $allHtExtrasinFile; }
  222.         $data_extras_sum = array('extra_price_no_vat' => 0'extra_price' => 0,);
  223.         foreach ($allHtExtrasinFile as $item){
  224.             $data_extras_sum['extra_price_no_vat'] += $item['extra_price_no_vat'];
  225.             $data_extras_sum['extra_price'] += $item['extra_price'];
  226.             $montoIva $item['extra_price'] - $item['extra_price_no_vat'];
  227.             switch ($item['extra']->getVat()) {
  228.                 case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  229.                 case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  230.                 default: break;
  231.             }
  232.             $netoAllSum += $item['extra_price_no_vat'];
  233.             $totalSum += $item['extra_price'];
  234.         }
  235.         $clientType 'client';
  236.         $clientAddress = (array_key_exists("client"$data) and !empty($data['client'])) ? $data['client']->getAddress() : 'No se ha podido determinar la dirección';
  237.         $country = (array_key_exists("client"$data) and !empty($data['client'])) ? $em->getRepository(Country::class)->findOneById($data['client']->getCountry()) : $em->getRepository(Country::class)->findOneById(62);
  238.         $country $country->getCountry();
  239.         if ($country == 'Spain'){ $country 'España'; }
  240.         $clientAddress $clientAddress ', '$country;
  241.         $clientDocument $data['client']->getIdDocument();
  242.         return $this->render('higotrigo/ht_invoice/invoice-select-items.html.twig',
  243.             array(
  244.                 'id' => $htFile->getId(),
  245.                 'htFile' => $htFile,
  246.                 'prefix' => null,
  247.                 'date' => null,
  248.                 'company' => $data['company'],
  249.                 'clientAddress' => $clientAddress,
  250.                 'clientType' => $clientType,
  251.                 'clientDocument' => $clientDocument,
  252.                 'client' => $data['client'],
  253.                 'clients' => null,
  254.                 'datasupplier' => $data,
  255.                 'data_extras' => $data_extras,
  256.                 'data_extras_sum' => $data_extras_sum,
  257.                 'currency' => '€',
  258.                 'totales_coniva_items' => $totalSumItems,
  259.                 'totales_coniva' => $totalSum,
  260.                 'totales_neto_items' => $netoSum,
  261.                 'totales_neto' => $netoAllSum,
  262.                 'bases_imponibles' => $data_iva,
  263.             )
  264.         );
  265.     }
  266.     /**
  267.      * @Route("/selinvoicesp/{id}",  name="ht_select_invoice_special")
  268.      * Vista previa para selecionar a quien se le va a facturar (proveedor - factura de comisión)
  269.      */
  270.     public function generateSpecialInvoiceAction($idEntityManagerInterface $emRequest $request)
  271.     {
  272.         //Buscamos los proveedores
  273.         $suppliers $em->getRepository(Supplier::class)->findAll();
  274.         $arrayClients = array();
  275.         foreach ($suppliers as $item){
  276.             $arrayClients[] = array(
  277.                 'nombre' => $item->getCompany(),
  278.                 'id' => $item->getId(),
  279.                 'tipo' => 'PROVEEDOR',
  280.                 'indice' => 'S'.$item->getId(),
  281.             );
  282.         }
  283.         return $this->render('higotrigo/ht_invoice/invoice-select-client-supplier.html.twig',
  284.             array(
  285.                 'id' => $id,
  286.                 'clients' => $arrayClients,
  287.             )
  288.         );
  289.     }
  290.     /**
  291.      * @Route("/selinvoicecom/{id}",  name="ht_select_commision_invoice")
  292.      * Seleccionar los elementos que pasaran a la factura de comision
  293.      */
  294.     public function selectItemsCommisionInvoiceAction($idHtFile $htFileHtService $htServiceEntityManagerInterface $emRequest $request)
  295.     {
  296.         $supId $request->request->get('reservation');
  297.         $supId $supId['client'];
  298.         $supId substr($supId,1);
  299.         $supplier $em->getRepository(Supplier::class)->findOneById($supId);
  300.         // El beneficio será del 100% por ser factura de comisión
  301.         $beneficio $request->query->get('bn');
  302.         // hfile => htitem => htMenu => htExtra ; htMenu => htMenuEscandallo => escandallo
  303.         $data = array();
  304.         $data['file'] = $em->getRepository(HtFile::class)->findOneById($id);
  305.         $htFile $data['file'];
  306.         $allHtItems $htFile->getHtItems();
  307.         $allHtMenus = array(); $allHtExtrasinFile = array(); $allHtEscandallos = array();
  308.         $beneficioItem 0;
  309.         $indiceBeneficio = -1;
  310.         $dataBen $htService->CalcTotals($htFile);
  311.         foreach ($allHtItems as $item) {
  312.             $indiceBeneficio ++;
  313.             $data['items'][$item->getId()]['item'] = $item;
  314.             $data['items'][$item->getId()]['item_beneficio'] = $dataBen['htItem'][$item->getId()]['precioNeto'] - $dataBen['htItem'][$item->getId()]['coste'];
  315.             $allHtExtras $item->getHtExtras();
  316.             foreach ($allHtExtras as $htSinlgeExtra){
  317.                 $htExtraIsInvoiced $em->getRepository(HtInvoiceItems::class)->findBy(array('itemType' => 'htExtra''itemIdOrigin' => $htSinlgeExtra->getId()));
  318.                 $boolIsInvoiced = ((count($htExtraIsInvoiced)) % == 0) ? false true;
  319.                 $data['items'][$item->getId()]['extras'][] = $htSinlgeExtra;
  320.                 if ($htSinlgeExtra->isAppearInvoice()) {
  321.                     $extra_price_no_vat $htSinlgeExtra->isOpOverCommission() ? $htSinlgeExtra->getPrice() * ($htSinlgeExtra->getOverCommission() /100) : ($htSinlgeExtra->getPrice() + $htSinlgeExtra->getOverCommission());
  322.                     $extra_price $extra_price_no_vat * (+ ($htSinlgeExtra->getVat()/100));
  323.                     $allHtExtrasinFile[] = array(
  324.                         'enabledToInvoice' => !$boolIsInvoiced,
  325.                         'extra' => $htSinlgeExtra,
  326.                         'extra_price_no_vat' => ($boolIsInvoiced) ? 0$extra_price_no_vat,
  327.                         'extra_price' => ($boolIsInvoiced) ? 0$extra_price,
  328.                     );
  329.                 }
  330.             }
  331.             $htMenus $item->getHtMenus();
  332.             if (!empty($htMenus)){
  333.                 foreach ($htMenus as $htMenu) {
  334.                     $allHtMenus[] = $htMenu;
  335.                     $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['menu_info'] = $htMenu;
  336.                     $htExtras $htMenu->getHtExtras();
  337.                     if (!empty($htExtras)) {
  338.                         foreach ($htExtras as $elem) {
  339.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['extras'][] = $elem;
  340.                             $allHtExtras[] = $elem;
  341.                         }
  342.                     }
  343.                     $escandallo $htMenu->getEscandallo();
  344.                     if (!empty($escandallo)) {
  345.                         $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] = null;
  346.                         foreach ($escandallo as $escan) {
  347.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos'][] = $escan;
  348.                             $allHtEscandallos[] = $escan;
  349.                             $data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] = (empty($data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'])) ? ($escan->getNombre()) : ($data['items'][$item->getId()]['menus'][$htMenu->getId()]['menu']['escandallos_names'] . ', ' $escan->getNombre());
  350.                         }
  351.                     }
  352.                 }
  353.             }
  354.         }
  355.         $data['company'] = $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  356.         $data['client'] = $supplier;
  357.         $data_iva = array();
  358.         $data_iva['ivaMontoVeintiUno'] = 0;
  359.         $data_iva['ivaMontoDiez'] = 0;
  360.         $data_iva['ivaMontoCero'] = 0;
  361.         $netoSum 0$totalSum 0;
  362.         $indiceBeneficio = -1;
  363.         if (array_key_exists("items"$data)) {
  364.             foreach ($data['items'] as $itemFac) {
  365.                 if (array_key_exists("menus"$itemFac)) {
  366.                     $indiceBeneficio++;
  367.                     foreach ($itemFac['menus'] as $htMenu) {
  368.                         $productName $htMenu['menu']['menu_info']->getName() . ' ( ' $htMenu['menu']['escandallos_names'] . ' )';
  369.                         $servicePrice = (($htMenu['menu']['menu_info']->getTotalPrice() * 100) / (100 $htMenu['menu']['menu_info']->getIva()));
  370.                         $subTotalPrice round((($htMenu['menu']['menu_info']->getTotalPrice() * 100) / (100 $htMenu['menu']['menu_info']->getIva())), 2) * $itemFac['item']->getPax();
  371.                         $subtotalProduct $subTotalPrice * (+ ($htMenu['menu']['menu_info']->getIva() / 100));
  372.                         $ivaAmount = ($htMenu['menu']['menu_info']->getIva() * $subTotalPrice) / 100;
  373.                         $htMenuIsInvoiced $em->getRepository(HtInvoiceItems::class)->findBy(array('itemType' => 'htMenu''itemIdOrigin' => $htMenu['menu']['menu_info']->getId()));
  374.                         $boolIsInvoiced = ((count($htMenuIsInvoiced)) % == 0) ? false true;
  375.                         if (!$boolIsInvoiced) {
  376.                             $netoSum += $subTotalPrice;
  377.                             $totalSum += $subtotalProduct;
  378.                             switch ($htMenu['menu']['menu_info']->getIva()) {
  379.                                 case 10$data_iva['ivaMontoDiez'] += $ivaAmount; break;
  380.                                 case 21$data_iva['ivaMontoVeintiUno'] += $ivaAmount; break;
  381.                                 default: break;
  382.                             }
  383.                         }
  384.                         $data['product'][] = array(
  385.                             'id' => $htMenu['menu']['menu_info']->getId(),     // Id del Menu
  386.                             'idHtItem' => $itemFac['item']->getId(),            // Id del Servicio (HtItem) que contiene el Menu
  387.                             'enabledToInvoice' => !$boolIsInvoiced,
  388.                             'productName' => $productName,
  389.                             'type' => null,
  390.                             'servicePrice' => $servicePrice,
  391.                             'qty' => $itemFac['item']->getPax(),
  392.                             'days' => $itemFac['item']->getDateStart()->format('d/m/Y'),
  393.                             'subTotalPrice' => ($boolIsInvoiced) ? $subTotalPrice,
  394.                             'iva' => $htMenu['menu']['menu_info']->getIva(),
  395.                             'ivaAmount' => $ivaAmount,
  396.                             'subtotalProduct' => ($boolIsInvoiced) ? $subtotalProduct,
  397. //                        'beneficio' => $beneficio[$indiceBeneficio],
  398.                             'beneficio' => $dataBen['htItem'][$itemFac['item']->getId()]['precioNeto'] - $dataBen['htItem'][$itemFac['item']->getId()]['coste'],                      // Se lleva al 100% antes de impuestos ya que es factura de comision
  399.                         );
  400.                     }
  401.                 }
  402.             }
  403.         }
  404.         $netoAllSum $netoSum;
  405.         $totalSumItems $totalSum;
  406.         //  SERVICIOS ADICIONALES
  407.         $data_extras = array();
  408.         if (!empty($allHtExtrasinFile)){ $data_extras['allHtExtrasinFile'] = $allHtExtrasinFile; }
  409.         $data_extras_sum = array('extra_price_no_vat' => 0'extra_price' => 0,);
  410.         foreach ($allHtExtrasinFile as $item){
  411.             $data_extras_sum['extra_price_no_vat'] += $item['extra_price_no_vat'];
  412.             $data_extras_sum['extra_price'] += $item['extra_price'];
  413.             $montoIva $item['extra_price'] - $item['extra_price_no_vat'];
  414.             switch ($item['extra']->getVat()) {
  415.                 case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  416.                 case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  417.                 default: break;
  418.             }
  419.             $netoAllSum += $item['extra_price_no_vat'];
  420.             $totalSum += $item['extra_price'];
  421.         }
  422.         $clientType 'supplier';
  423.         $clientAddress = (array_key_exists("client"$data) and !empty($data['client'])) ? $data['client']->getAddress() : 'No se ha podido determinar la dirección';
  424.         $country = (array_key_exists("client"$data) and !empty($data['client'])) ? $em->getRepository(Country::class)->findOneById($data['client']->getCountry()) : $em->getRepository(Country::class)->findOneById(62);
  425.         $country $country->getCountry();
  426.         if ($country == 'Spain'){ $country 'España'; }
  427.         $clientAddress $clientAddress ', '$country;
  428.         $clientDocument $data['client']->getIdDocument();
  429.         return $this->render('higotrigo/ht_invoice/invoice-select-items.html.twig',
  430.             array(
  431.                 'id' => $htFile->getId(),
  432.                 'htFile' => $htFile,
  433.                 'prefix' => null,
  434.                 'date' => null,
  435.                 'company' => $data['company'],
  436.                 'clientAddress' => $clientAddress,
  437.                 'clientDocument' => $clientDocument,
  438.                 'clientType' => $clientType,
  439.                 'client' => $data['client'],
  440.                 'clients' => null,
  441.                 'datasupplier' => $data,
  442.                 'data_extras' => $data_extras,
  443.                 'data_extras_sum' => $data_extras_sum,
  444.                 'currency' => '€',
  445.                 'totales_coniva_items' => $totalSumItems,
  446.                 'totales_coniva' => $totalSum,
  447.                 'totales_neto_items' => $netoSum,
  448.                 'totales_neto' => $netoAllSum,
  449.                 'bases_imponibles' => $data_iva,
  450.             )
  451.         );
  452.     }
  453.     /**
  454.      * @Route("/createinvoice/{id}",  name="ht_create_invoice")
  455.      * Generar factura en el expediente ID
  456.      */
  457.     public function createInvoiceHtAction($idEntityManagerInterface $emRequest $request)
  458.     {
  459.         $req $request->request->get('invoiced_item');
  460.         $req_extra $request->request->get('invoiced_extra');
  461.         $htFile $em->getRepository(HtFile::class)->findOneById($id);
  462.         $beneficio_total 0;
  463.         // El break es necesario ya que el beneficio fue concebido para ser por servicios, luego con el cambio de multi menus en un servicio se debe sumar solamente una vez
  464.         foreach ($req as $itemServ){
  465.             foreach ($itemServ as $item) {
  466.                 if ($item['boolToInvoice'] == 'true') { $beneficio_total += $item['benefit']; break; }
  467.             }
  468.         }
  469.         $boolSeleccionados false;
  470.         // Si no hay elemento seleccionados se vuelve a la pantalla de seleccion
  471.         if ($boolSeleccionados){ return $this->redirectToRoute('ht_select_invoice', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER); }
  472.         // Se genera la factura
  473.         // buscamos por el prefijo de cada linea de facturacion para saber el ultimo ID
  474.         $invoiceLineGp $em->getRepository(HtInvoice::class)->findByPrefix('GP');
  475.         $invoiceLineCvr $em->getRepository(HtInvoice::class)->findByPrefix('CV');
  476.         $invoiceLineOther $em->getRepository(HtInvoice::class)->findByPrefix('F');
  477.         $lastGp end($invoiceLineGp);
  478.         $lastCvr end($invoiceLineCvr);
  479.         $lastOther end($invoiceLineOther);
  480.         // Buscamos los HtIemts del HtFile para saber la sala del servicio para determinar la línea de facturación ($prefix)
  481.         $htItems $em->getRepository(HtItem::class)->findByHtFile($htFile);
  482.         $lounge $htItems[0]->getLoungeGp();
  483.         $prefix = (empty($lounge) ? 'F' : (($lounge->getId() < 22) ? 'GP' 'CV'));
  484.         switch ($prefix) {
  485.             case 'F'$idNum = (empty($lastOther)) ? $lastOther->getIdNum() + 1; break;
  486.             case 'CV'$idNum = (empty($lastCvr)) ? $lastCvr->getIdNum() + 1; break;
  487.             default:
  488.                 // Por defecto Green Patio.
  489.                 // Esteban Rincon comenta que en caso de un expediente tener salas de GP y CVR deberiamos tener un expediente por linea de facturación
  490.                 $idNum = (empty($lastGp)) ? $lastGp->getIdNum() + 1;
  491.                 break;
  492.         }
  493.         $client = (empty($htFile->getClient())) ? null $htFile->getClient();
  494.         // Si no hay cliente no se puede facturar, se vuelve a la pantalla de seleccion
  495.         if (empty($client)){ return $this->redirectToRoute('ht_select_invoice', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER); }
  496.         $dateAt = new DateTimeImmutable('now');
  497.         $type 'Invoice';
  498.         $totalNet 0$vatTotalGroup 0$total 0;
  499.         $clientName $client->getName();
  500.         $clientAddress $request->request->get('clientAddress');
  501.         $clientDocument = empty($client->getIdDocument()) ? ' ' $client->getIdDocument();
  502.         $clientType $request->request->get('clientType');
  503.         if ($clientType == 'supplier'){
  504.             $type 'commission invoice';
  505.             $clientName $request->request->get('clientName');
  506.             $clientDocument $request->request->get('clientDocument');
  507.         }
  508.         $invoice = new HtInvoice();
  509.         $invoice->setIdNum($idNum);
  510.         $invoice->setPrefix($prefix);
  511.         $invoice->setDateAt($dateAt);
  512.         $invoice->setType($type);
  513.         $invoice->setTotalNet($totalNet);
  514.         $invoice->setVatTotalGroup($vatTotalGroup);
  515.         $invoice->setTotal($total);
  516.         $invoice->setClientName($clientName);
  517.         $invoice->setClientAddress($clientAddress);
  518.         $invoice->setClientDocument($clientDocument);
  519.         $invoice->setClientType($clientType);
  520.         $invoice->setHtFile($htFile);
  521.         $invoice->setBenefit($beneficio_total);
  522.         $em->persist($invoice);
  523.         $em->flush();
  524.         $data_iva = array(); $data_iva['ivaMontoVeintiUno'] = 0$data_iva['ivaMontoDiez'] = 0;
  525.         // Se crean los Menus en los HtInvoiceItems
  526.         foreach ($req as $itemServ){
  527.             foreach ($itemServ as $item) {
  528.                 if ($item['boolToInvoice'] == 'true') {
  529.                     $htMenu $em->getRepository(HtMenu::class)->findOneByHtItem($item['idHtService']);
  530.                     $itemType 'htMenu';
  531.                     $itemIdOrigin $htMenu->getId();
  532.                     $itemName $item['name'];
  533.                     $htItem $em->getRepository(HtItem::class)->findOneById($htMenu->getHtItem());
  534.                     $dateStart = ($htItem->getDateStart())->format('Y-m-d');
  535.                     $hourEnd = ($htItem->getHourEnd())->format('Y-m-d');
  536.                     $dateTimeString $dateStart ' ' $hourEnd;
  537.                     $dateEnd = new DateTimeImmutable($dateTimeString);
  538.                     $iva $htMenu->getIva();
  539.                     switch ($iva) {
  540.                         case 10$data_iva['ivaMontoDiez'] += ($item['subTotalPrice'] * 0.1); break;
  541.                         case 21$data_iva['ivaMontoVeintiUno'] += ($item['subTotalPrice'] * 0.21); break;
  542.                         default: break;
  543.                     }
  544.                     $itemCommission 0;                                                                // Solo htExtra maneja el concepto de over o comision.
  545.                     $itemOver 0;                                                                      // Solo htExtra maneja el concepto de over o comision.
  546.                     $costeDirecto $item['subTotalPrice'] - $item['benefit'];
  547.                     $totalNet += round($item['subTotalPrice'], 2);
  548.                     $total += round($item['subTotalPrice'], 2) + (round($item['subTotalPrice'], 2) * ($iva 100));
  549.                     $xPax = empty($item['qty']) ? $item['qty'];
  550.                     $htInvItem = new HtInvoiceItems();
  551.                     $htInvItem->setInvoice($invoice);
  552.                     $htInvItem->setHtFile($htFile);
  553.                     $htInvItem->setItemType($itemType);
  554.                     $htInvItem->setItemIdOrigin($itemIdOrigin);
  555.                     $htInvItem->setItemName($itemName);
  556.                     $htInvItem->setItemDateStart($htItem->getDateStart());
  557.                     $htInvItem->setItemDateEnd($dateEnd);
  558.                     $htInvItem->setItemPrice(round($item['servicePrice'], 2));
  559.                     $htInvItem->setItemUnits(1);
  560.                     $htInvItem->setItemPax($xPax);
  561.                     $htInvItem->setItemKgLt(1);
  562.                     $htInvItem->setItemRank(1);
  563.                     $htInvItem->setItemLocation(0);
  564.                     $htInvItem->setItemIva($iva);
  565.                     $htInvItem->setItemOpIva(0);            // +
  566.                     $htInvItem->setItemCommission($itemCommission);
  567.                     $htInvItem->setItemOpCommission(0);
  568.                     $htInvItem->setItemOver($itemOver);
  569.                     $htInvItem->setItemOpOver(0);
  570.                     $htInvItem->setItemSubTotalPrice(round($item['subTotalPrice'], 2));
  571.                     $htInvItem->setItemCosteDirecto(round($costeDirecto2));
  572.                     $htInvItem->setItemBeneficio(round($item['benefit'], 2));
  573.                     $em->persist($htInvItem);
  574.                     $em->flush();
  575.                 }
  576.             }
  577.         }
  578.         // Se crean los Extras en los HtInvoiceItems
  579.         if (!empty($req_extra)) {
  580.             foreach ($req_extra as $item) {
  581.                 if($item['boolToInvoice'] == 'true') {
  582.                     $htExtra $em->getRepository(HtExtra::class)->findOneById($item['idHtExtra']);
  583.                     if ($htExtra->isAppearInvoice()) {
  584.                         $itemType 'htExtra';
  585.                         $itemIdOrigin $htExtra->getId();
  586.                         $itemName $htExtra->getConcept();
  587.                         $htItem $em->getRepository(HtItem::class)->findOneById($htExtra->getHtItem());
  588.                         $dateStart = ($htItem->getDateStart())->format('Y-m-d');
  589.                         $hourEnd = ($htItem->getHourEnd())->format('Y-m-d');
  590.                         $dateTimeString $dateStart ' ' $hourEnd;
  591.                         $dateEnd = new DateTimeImmutable($dateTimeString);
  592.                         $iva $htExtra->getVat();
  593.                         $itemCommission = (($htExtra->isOpOverCommission() == 0) ? $htExtra->getOverCommission());
  594.                         $itemOver = (($htExtra->isOpOverCommission() == 1)  ? $htExtra->getOverCommission());
  595.                         $costeDirecto 0;
  596.                         $benefit $itemOver + ($item['price'] * ($itemCommission 100));
  597.                         $totalPrice $item['price'] + $benefit;
  598.                         switch ($iva) {
  599.                             case 10$data_iva['ivaMontoDiez'] += ($totalPrice 0.1); break;
  600.                             case 21$data_iva['ivaMontoVeintiUno'] += ($totalPrice 0.21); break;
  601.                             default: break;
  602.                         }
  603.                         $totalNet += round($totalPrice2);
  604.                         $total += round($totalPrice2) + (round($totalPrice2) * ($iva 100));
  605.                         $htInvItem = new HtInvoiceItems();
  606.                         $htInvItem->setInvoice($invoice);
  607.                         $htInvItem->setHtFile($htFile);
  608.                         $htInvItem->setItemType($itemType);
  609.                         $htInvItem->setItemIdOrigin($itemIdOrigin);
  610.                         $htInvItem->setItemName($itemName);
  611.                         $htInvItem->setItemDateStart($htItem->getDateStart());
  612.                         $htInvItem->setItemDateEnd($dateEnd);
  613.                         $htInvItem->setItemPrice(round($item['price'], 2));
  614.                         $htInvItem->setItemUnits(1);
  615.                         $htInvItem->setItemPax(1);
  616.                         $htInvItem->setItemKgLt(1);
  617.                         $htInvItem->setItemRank(1);
  618.                         $htInvItem->setItemLocation(0);
  619.                         $htInvItem->setItemIva($iva);
  620.                         $htInvItem->setItemOpIva(0);            // +
  621.                         $htInvItem->setItemCommission($itemCommission);
  622.                         $htInvItem->setItemOpCommission(0);
  623.                         $htInvItem->setItemOver($itemOver);
  624.                         $htInvItem->setItemOpOver(0);
  625.                         $htInvItem->setItemSubTotalPrice(round($totalPrice2));
  626.                         $htInvItem->setItemCosteDirecto(round($costeDirecto2));
  627.                         $htInvItem->setItemBeneficio(round($benefit2));
  628.                         $em->persist($htInvItem);
  629.                         $em->flush();
  630.                     }
  631.                 }
  632.             }
  633.         }
  634.         $string_data_iva '[21]'.round($data_iva['ivaMontoVeintiUno'],2).'[21]'.'[10]'.round($data_iva['ivaMontoDiez'],2).'[10]';
  635.         // Se actualiza la factura con los datos globales
  636.         $invoice->setTotalNet($totalNet);
  637.         $invoice->setVatTotalGroup($string_data_iva);
  638.         $invoice->setTotal(round($total,2));
  639.         $em->persist($invoice);
  640.         $em->flush();
  641.         return $this->redirectToRoute('ht_view_invoice', ['id' => $invoice->getPrefix(). $invoice->getIdNum()], Response::HTTP_SEE_OTHER);
  642.     }
  643.     /**
  644.      * @Route("/viewinvoice/{id}",  name="ht_view_invoice")
  645.      * Ver la factura con el ID
  646.      */
  647.     public function showInvoiceHtAction($idEntityManagerInterface $emRequest $request)
  648.     {
  649.         $idNum = (substr($id01) == 'F') ? substr($id1) : substr($id2);      // F1, GP1, CV1
  650.         $prefix = (substr($id01) == 'F') ? 'F' substr($id02);
  651.         $user $this->getUser();
  652.         $parameters = array( 'idNum' => $idNum'prefix' => $prefix, );
  653.         $dql 'SELECT p
  654.                 FROM App\Entity\HtInvoice p
  655.                 WHERE p.idNum = :idNum AND p.prefix = :prefix';
  656.         $query $em->createQuery($dql)->setParameters($parameters);
  657.         $invoice $query->getResult();
  658.         $invoice = (!empty($invoice)) ? $invoice[0] : null;
  659. //        $id = $invoice->getPrefix() . $invoice->getIdNum();
  660.         // Si no consigue la factura redirige al listado de expedientes
  661.         if (empty($invoice)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  662.         $datAt = (!empty($invoice)) ? $invoice->getDateAt() : null;
  663.         $totales_neto = (!empty($invoice)) ? $invoice->getTotalNet() : 0;
  664.         $totales_iva = (!empty($invoice)) ? $invoice->getVatTotalGroup() : 0;
  665.         $totales = (!empty($invoice)) ? $invoice->getTotal() : 0;
  666.         $htInvoiceItems $em->getRepository(HtInvoiceItems::class)->findByInvoice($invoice);
  667.         $htFile $em->getRepository(HtFile::class)->findOneById(($invoice->getHtFile())->getId());
  668.         $clientName = (!empty($invoice)) ? $invoice->getClientName() : 'No se pudo determinar el cliente';
  669.         $clientAddress = (!empty($invoice)) ? $invoice->getClientAddress() : 'No se pudo determinar la dirección';
  670.         $clientDocument = (!empty($invoice)) ? $invoice->getClientDocument() : 'No se pudo determinar';
  671.         $type $invoice->getType();
  672.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  673.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  674.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  675.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  676.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  677.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  678.         foreach ($htInvoiceItems as $key => $item){
  679.             if ($item->getItemType()=='htMenu') {
  680.                 $datasupplier['invoice_item'][] = array(
  681.                     'id' => $item->getId(),
  682.                     'itemName' => $item->getItemName(),
  683.                     'itemPrice' => $item->getItemPrice(),
  684.                     'itemPax' => $item->getItemPax(),
  685.                     'itemDateStart' => $item->getItemDateStart(),
  686.                     'itemDateEnd' => $item->getItemDateEnd(),
  687.                     'itemIva' => $item->getItemIva(),
  688.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  689.                 );
  690.                 $netoSumItems += $item->getItemSubTotalPrice();
  691.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  692.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  693.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  694.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  695.                 switch ($item->getItemIva()) {
  696.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  697.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  698.                     default: break;
  699.                 }
  700.             }
  701.             if ($item->getItemType()=='htExtra') {
  702.                 $datasupplier['invoice_extra'][] = array(
  703.                     'id' => $item->getId(),
  704.                     'itemType' => $item->getItemType(),
  705.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  706.                     'itemName' => $item->getItemName(),
  707.                     'itemPrice' => $item->getItemSubTotalPrice(),
  708.                     'itemPax' => $item->getItemPax(),
  709.                     'itemDateStart' => $item->getItemDateStart(),
  710.                     'itemDateEnd' => $item->getItemDateEnd(),
  711.                     'itemIva' => $item->getItemIva(),
  712.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  713.                 );
  714.                 $netoSumItems += $item->getItemSubTotalPrice();
  715.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  716.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  717.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  718.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  719.                 switch ($item->getItemIva()) {
  720.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  721.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  722.                     default: break;
  723.                 }
  724.             }
  725.         }
  726.         $allInvoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  727.         $invoiceRec $em->getRepository(HtInvoice::class)->findByInvoiceToRec($id);
  728.         $boolToRec = ($invoice->getType() == 'Invoice Rec') ? false : empty($invoiceRec);
  729.         return $this->render('higotrigo/ht_invoice/show-invoice.html.twig',
  730.             array(
  731.                 'id' => $htFile->getId(),
  732.                 'cid' => '',
  733.                 'fid' => $id,
  734.                 'invoice' => $invoice,
  735.                 'allInvoices' => $allInvoices,
  736.                 'proformas' => null,
  737.                 'numberadmin' => '',
  738.                 'type' => $type,
  739.                 'number' => $id,
  740.                 'prefix' => $prefix,
  741.                 'date' => $datAt,
  742.                 'reservation' => $htFile,
  743.                 'company' => $company,
  744.                 'user' => $user,
  745.                 'boolToRec' => $boolToRec,
  746.                 'invoicedeposititems' => '',
  747.                 'datasupplier' => $datasupplier,
  748.                 'totales_neto' => $netoSumItems,
  749.                 'totales_iva' => $totales_iva,
  750.                 'totales' => $totalSumItems,
  751.                 'bases_imponibles' => $data_iva,
  752.                 'paymentInvoice' => null,
  753.                 'currency' => '€',
  754.                 'clientName' => $clientName,
  755.                 'clientAddress' => $clientAddress,
  756.                 'clientDocument' => $clientDocument,
  757.             )
  758.         );
  759.     }
  760.     /**
  761.      * @Route("/viewinvoicerec/{id}",  name="ht_view_invoice_rec")
  762.      * Ver la factura rectificativa con el ID
  763.      */
  764.     public function showInvoiceRecHtAction($idEntityManagerInterface $emRequest $request)
  765.     {
  766.         $idNum = (substr($id02) == 'RF') ? substr($id2) : substr($id3);      // RF1, RGP1, RCV1
  767.         $prefix = (substr($id02) == 'RF') ? 'RF' substr($id03);
  768.         $user $this->getUser();
  769.         $parameters = array( 'idNum' => $idNum'prefix' => $prefix, );
  770.         $dql 'SELECT p
  771.                 FROM App\Entity\HtInvoice p
  772.                 WHERE p.idNum = :idNum AND p.prefix = :prefix';
  773.         $query $em->createQuery($dql)->setParameters($parameters);
  774.         $invoice $query->getResult();
  775.         $invoice = (!empty($invoice)) ? $invoice[0] : null;
  776.         // Si no consigue la factura redirige al listado de expedientes
  777.         if (empty($invoice)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  778.         $datAt = (!empty($invoice)) ? $invoice->getDateAt() : null;
  779.         $totales_neto = (!empty($invoice)) ? $invoice->getTotalNet() : 0;
  780.         $totales_iva = (!empty($invoice)) ? $invoice->getVatTotalGroup() : 0;
  781.         $totales = (!empty($invoice)) ? $invoice->getTotal() : 0;
  782.         $htInvoiceItems $em->getRepository(HtInvoiceItems::class)->findByInvoice($invoice);
  783.         $htFile $em->getRepository(HtFile::class)->findOneById(($invoice->getHtFile())->getId());
  784.         $clientName = (!empty($invoice)) ? $invoice->getClientName() : 'No se pudo determinar el cliente';
  785.         $clientAddress = (!empty($invoice)) ? $invoice->getClientAddress() : 'No se pudo determinar la dirección';
  786.         $clientDocument = (!empty($invoice)) ? $invoice->getClientDocument() : 'No se pudo determinar';
  787.         $type $invoice->getType();
  788.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  789.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  790.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  791.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  792.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  793.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  794.         foreach ($htInvoiceItems as $key => $item){
  795.             if ($item->getItemType()=='htMenu') {
  796.                 $datasupplier['invoice_item'][] = $item;
  797.                 $netoSumItems += $item->getItemSubTotalPrice();
  798.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  799.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  800.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  801.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  802.                 switch ($item->getItemIva()) {
  803.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  804.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  805.                     default: break;
  806.                 }
  807.             }
  808.             if ($item->getItemType()=='htExtra') {
  809. //                $datasupplier['invoice_extra'][] = $item;
  810.                 $datasupplier['invoice_extra'][] = array(
  811.                     'id' => $item->getId(),
  812.                     'itemType' => $item->getItemType(),
  813.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  814.                     'itemName' => $item->getItemName(),
  815.                     'itemPrice' => $item->getItemSubTotalPrice(),
  816.                     'itemPax' => $item->getItemPax(),
  817.                     'itemDateStart' => $item->getItemDateStart(),
  818.                     'itemDateEnd' => $item->getItemDateEnd(),
  819.                     'itemIva' => $item->getItemIva(),
  820.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  821.                 );
  822.                 $netoSumItems += $item->getItemSubTotalPrice();
  823.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  824.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  825.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  826.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  827.                 switch ($item->getItemIva()) {
  828.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  829.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  830.                     default: break;
  831.                 }
  832.             }
  833.         }
  834.         $allInvoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  835.         return $this->render('higotrigo/ht_invoice/show-invoice.html.twig',
  836.             array(
  837.                 'id' => $htFile->getId(),
  838.                 'cid' => '',
  839.                 'fid' => $id,
  840.                 'invoice' => $invoice,
  841.                 'invoiceIdToRec' => $invoice->getInvoiceToRec(),
  842.                 'allInvoices' => $allInvoices,
  843.                 'proformas' => null,
  844.                 'boolToRec' => false,
  845.                 'numberadmin' => '',
  846.                 'type' => $type,
  847.                 'number' => $id,
  848.                 'prefix' => $prefix,
  849.                 'date' => $datAt,
  850.                 'reservation' => $htFile,
  851.                 'company' => $company,
  852.                 'user' => $user,
  853.                 'invoicedeposititems' => '',
  854.                 'datasupplier' => $datasupplier,
  855.                 'totales_neto' => $netoSumItems,
  856.                 'totales_iva' => $totales_iva,
  857.                 'totales' => $totalSumItems,
  858.                 'bases_imponibles' => $data_iva,
  859.                 'paymentInvoice' => null,
  860.                 'currency' => '€',
  861.                 'clientName' => $clientName,
  862.                 'clientAddress' => $clientAddress,
  863.                 'clientDocument' => $clientDocument,
  864.             )
  865.         );
  866.     }
  867.     /**
  868.      * @Route("/htexternal/viewprintinvoice/{id}",  name="ht_view_print_invoice")
  869.      * Ver la factura con el ID en modalidad de impresion
  870.      */
  871.     public function printInvoiceHtAction($idEntityManagerInterface $emRequest $request)
  872.     {
  873.         $idNum = (substr($id01) == 'F') ? substr($id1) : substr($id2);      // F1, GP1, CV1
  874.         $prefix = (substr($id01) == 'F') ? 'F' substr($id02);
  875.         $user $this->getUser();
  876.         $parameters = array( 'idNum' => $idNum'prefix' => $prefix, );
  877.         $dql 'SELECT p
  878.                 FROM App\Entity\HtInvoice p
  879.                 WHERE p.idNum = :idNum AND p.prefix = :prefix';
  880.         $query $em->createQuery($dql)->setParameters($parameters);
  881.         $invoice $query->getResult();
  882.         $invoice = (!empty($invoice)) ? $invoice[0] : null;
  883.         // Si no consigue la factura redirige al listado de expedientes
  884.         if (empty($invoice)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  885.         $datAt = (!empty($invoice)) ? $invoice->getDateAt() : null;
  886.         $totales_neto = (!empty($invoice)) ? $invoice->getTotalNet() : 0;
  887.         $totales_iva = (!empty($invoice)) ? $invoice->getVatTotalGroup() : 0;
  888.         $totales = (!empty($invoice)) ? $invoice->getTotal() : 0;
  889.         $htInvoiceItems $em->getRepository(HtInvoiceItems::class)->findByInvoice($invoice);
  890.         $htFile $em->getRepository(HtFile::class)->findOneById(($invoice->getHtFile())->getId());
  891.         $clientName = (!empty($invoice)) ? $invoice->getClientName() : 'No se pudo determinar el cliente';
  892.         $clientAddress = (!empty($invoice)) ? $invoice->getClientAddress() : 'No se pudo determinar la dirección';
  893.         $clientDocument = (!empty($invoice)) ? $invoice->getClientDocument() : 'No se pudo determinar';
  894.         $type $invoice->getType();
  895.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  896.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  897.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  898.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  899.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  900.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  901.         foreach ($htInvoiceItems as $key => $item){
  902.             if ($item->getItemType()=='htMenu') {
  903.                 $datasupplier['invoice_item'][] = array(
  904.                     'id' => $item->getId(),
  905.                     'itemName' => $item->getItemName(),
  906.                     'itemPrice' => $item->getItemPrice(),
  907.                     'itemPax' => $item->getItemPax(),
  908.                     'itemDateStart' => $item->getItemDateStart(),
  909.                     'itemDateEnd' => $item->getItemDateEnd(),
  910.                     'itemIva' => $item->getItemIva(),
  911.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  912.                 );
  913.                 $netoSumItems += $item->getItemSubTotalPrice();
  914.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  915.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  916.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  917.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  918.                 switch ($item->getItemIva()) {
  919.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  920.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  921.                     default: break;
  922.                 }
  923.             }
  924.             if ($item->getItemType()=='htExtra') {
  925. //                $datasupplier['invoice_extra'][] = $item;
  926.                 $datasupplier['invoice_extra'][] = array(
  927.                     'id' => $item->getId(),
  928.                     'itemType' => $item->getItemType(),
  929.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  930.                     'itemName' => $item->getItemName(),
  931.                     'itemPrice' => $item->getItemSubTotalPrice(),
  932.                     'itemPax' => $item->getItemPax(),
  933.                     'itemDateStart' => $item->getItemDateStart(),
  934.                     'itemDateEnd' => $item->getItemDateEnd(),
  935.                     'itemIva' => $item->getItemIva(),
  936.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  937.                 );
  938.                 $netoSumItems += $item->getItemSubTotalPrice();
  939.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  940.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  941.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  942.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  943.                 switch ($item->getItemIva()) {
  944.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  945.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  946.                     default: break;
  947.                 }
  948.             }
  949.         }
  950.         return $this->render('higotrigo/ht_invoice/invoice-print.html.twig',
  951.             array(
  952.                 'id' => $htFile->getId(),
  953.                 'cid' => '',
  954.                 'fid' => $id,
  955.                 'invoice' => $invoice,
  956.                 'proformas' => null,
  957.                 'boolToRec' => true,
  958.                 'numberadmin' => '',
  959.                 'type' => $type,
  960.                 'number' => $id,
  961.                 'prefix' => $prefix,
  962.                 'date' => $datAt,
  963.                 'reservation' => $htFile,
  964.                 'company' => $company,
  965.                 'user' => $user,
  966.                 'invoicedeposititems' => '',
  967.                 'datasupplier' => $datasupplier,
  968.                 'totales_neto' => $netoSumItems,
  969.                 'totales_iva' => $totales_iva,
  970.                 'totales' => $totalSumItems,
  971.                 'bases_imponibles' => $data_iva,
  972.                 'paymentInvoice' => null,
  973.                 'currency' => '€',
  974.                 'clientName' => $clientName,
  975.                 'clientAddress' => $clientAddress,
  976.                 'clientDocument' => $clientDocument,
  977.             )
  978.         );
  979.     }
  980.     /**
  981.      * @Route("/htexternal/viewprintinvoicerec/{id}", name="ht_view_print_invoicerec")
  982.      * Ver la factura rectificativa con el ID en modalidad de impresion
  983.      */
  984.     public function printInvoiceRecHtAction($idEntityManagerInterface $emRequest $request)
  985.     {
  986.         $idNum = (substr($id02) == 'RF') ? substr($id2) : substr($id3);      // RF1, RGP1, RCV1
  987.         $prefix = (substr($id02) == 'RF') ? 'RF' substr($id03);
  988.         $user $this->getUser();
  989.         $parameters = array( 'idNum' => $idNum'prefix' => $prefix, );
  990.         $dql 'SELECT p
  991.                 FROM App\Entity\HtInvoice p
  992.                 WHERE p.idNum = :idNum AND p.prefix = :prefix';
  993.         $query $em->createQuery($dql)->setParameters($parameters);
  994.         $invoice $query->getResult();
  995.         $invoice = (!empty($invoice)) ? $invoice[0] : null;
  996.         // Si no consigue la factura redirige al listado de expedientes
  997.         if (empty($invoice)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  998.         $datAt = (!empty($invoice)) ? $invoice->getDateAt() : null;
  999.         $totales_neto = (!empty($invoice)) ? $invoice->getTotalNet() : 0;
  1000.         $totales_iva = (!empty($invoice)) ? $invoice->getVatTotalGroup() : 0;
  1001.         $totales = (!empty($invoice)) ? $invoice->getTotal() : 0;
  1002.         $htInvoiceItems $em->getRepository(HtInvoiceItems::class)->findByInvoice($invoice);
  1003.         $htFile $em->getRepository(HtFile::class)->findOneById(($invoice->getHtFile())->getId());
  1004.         $clientName = (!empty($invoice)) ? $invoice->getClientName() : 'No se pudo determinar el cliente';
  1005.         $clientAddress = (!empty($invoice)) ? $invoice->getClientAddress() : 'No se pudo determinar la dirección';
  1006.         $clientDocument = (!empty($invoice)) ? $invoice->getClientDocument() : 'No se pudo determinar';
  1007.         $type $invoice->getType();
  1008.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  1009.         $invoiceIdToRec $invoice->getInvoiceToRec();
  1010.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  1011.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  1012.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  1013.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  1014.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  1015.         foreach ($htInvoiceItems as $key => $item){
  1016.             if ($item->getItemType()=='htMenu') {
  1017.                 $datasupplier['invoice_item'][] = array(
  1018.                     'id' => $item->getId(),
  1019.                     'itemName' => $item->getItemName(),
  1020.                     'itemPrice' => $item->getItemPrice(),
  1021.                     'itemPax' => $item->getItemPax(),
  1022.                     'itemDateStart' => $item->getItemDateStart(),
  1023.                     'itemDateEnd' => $item->getItemDateEnd(),
  1024.                     'itemIva' => $item->getItemIva(),
  1025.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1026.                 );
  1027.                 $netoSumItems += $item->getItemSubTotalPrice();
  1028.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  1029.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1030.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1031.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1032.                 switch ($item->getItemIva()) {
  1033.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1034.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1035.                     default: break;
  1036.                 }
  1037.             }
  1038.             if ($item->getItemType()=='htExtra') {
  1039. //                $datasupplier['invoice_extra'][] = $item;
  1040.                 $datasupplier['invoice_extra'][] = array(
  1041.                     'id' => $item->getId(),
  1042.                     'itemType' => $item->getItemType(),
  1043.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  1044.                     'itemName' => $item->getItemName(),
  1045.                     'itemPrice' => $item->getItemSubTotalPrice(),
  1046.                     'itemPax' => $item->getItemPax(),
  1047.                     'itemDateStart' => $item->getItemDateStart(),
  1048.                     'itemDateEnd' => $item->getItemDateEnd(),
  1049.                     'itemIva' => $item->getItemIva(),
  1050.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1051.                 );
  1052.                 $netoSumItems += $item->getItemSubTotalPrice();
  1053.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  1054.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1055.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1056.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1057.                 switch ($item->getItemIva()) {
  1058.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1059.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1060.                     default: break;
  1061.                 }
  1062.             }
  1063.         }
  1064.         return $this->render('higotrigo/ht_invoice/invoice-print.html.twig',
  1065.             array(
  1066.                 'id' => $htFile->getId(),
  1067.                 'cid' => '',
  1068.                 'fid' => $id,
  1069.                 'invoice' => $invoice,
  1070.                 'proformas' => null,
  1071.                 'boolToRec' => true,
  1072.                 'numberadmin' => '',
  1073.                 'type' => $type,
  1074.                 'number' => $id,
  1075.                 'prefix' => $prefix,
  1076.                 'date' => $datAt,
  1077.                 'reservation' => $htFile,
  1078.                 'company' => $company,
  1079.                 'user' => $user,
  1080.                 'invoicedeposititems' => '',
  1081.                 'datasupplier' => $datasupplier,
  1082.                 'totales_neto' => $netoSumItems,
  1083.                 'totales_iva' => $totales_iva,
  1084.                 'totales' => $totalSumItems,
  1085.                 'bases_imponibles' => $data_iva,
  1086.                 'paymentInvoice' => null,
  1087.                 'currency' => '€',
  1088.                 'clientName' => $clientName,
  1089.                 'clientAddress' => $clientAddress,
  1090.                 'clientDocument' => $clientDocument,
  1091.                 'invoiceIdToRec' => $invoiceIdToRec,
  1092.             )
  1093.         );
  1094.     }
  1095.     /**
  1096.      * @Route("/createrecinvoice/{id}",  name="ht_create_rec_invoice")
  1097.      * Generar factura rectificativa de la factura ID
  1098.      */
  1099.     public function createRecInvoiceHtAction($idEntityManagerInterface $emRequest $request)
  1100.     {
  1101.         $idNum = (substr($id01) == 'F') ? substr($id1) : substr($id2);      // F1, GP1, CV1
  1102.         $prefix = (substr($id01) == 'F') ? 'F' substr($id02);
  1103.         $parameters = array( 'idNum' => $idNum'prefix' => $prefix, );
  1104.         $dql 'SELECT p
  1105.                 FROM App\Entity\HtInvoice p
  1106.                 WHERE p.idNum = :idNum AND p.prefix = :prefix';
  1107.         $query $em->createQuery($dql)->setParameters($parameters);
  1108.         $invoice $query->getResult();
  1109.         $invoice = (!empty($invoice)) ? $invoice[0] : null;
  1110.         // Si no consigue la factura redirige al listado de expedientes
  1111.         if (empty($invoice)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  1112.         // Buscamos el ID para la nueva rectificativa
  1113.         $parameters = array( 'prefix' => 'R'.$prefix, );
  1114.         $dql 'SELECT p
  1115.                 FROM App\Entity\HtInvoice p
  1116.                 WHERE p.prefix = :prefix';
  1117.         $query $em->createQuery($dql)->setParameters($parameters);
  1118.         $invoiceRec $query->getResult();
  1119.         $invoiceRec = (!empty($invoiceRec)) ? end($invoiceRec) : null;
  1120.         $idNewRec = empty($invoiceRec) ? : ($invoiceRec->getIdNum() + 1) ;
  1121.         $dateAt = new DateTimeImmutable('now');
  1122.         $invoiceItems $em->getRepository(HtInvoiceItems::class)->findByInvoice($invoice);
  1123.         // Creamos la factura rectificativa
  1124.         $oldIvas $invoice->getVatTotalGroup();
  1125.         $newIvas $this->transformarString($oldIvas);
  1126.         $invoiceRec = new HtInvoice();
  1127.         $invoiceRec->setIdNum($idNewRec);
  1128.         $invoiceRec->setPrefix('R'$prefix);
  1129.         $invoiceRec->setDateAt($dateAt);
  1130.         $invoiceRec->setType('Invoice Rec');
  1131.         $invoiceRec->setClientName($invoice->getClientName());
  1132.         $invoiceRec->setClientAddress($invoice->getClientAddress());
  1133.         $invoiceRec->setClientDocument($invoice->getClientDocument());
  1134.         $invoiceRec->setClientType($invoice->getClientType());
  1135.         $invoiceRec->setHtFile($invoice->getHtFile());
  1136.         $invoiceRec->setBenefit((-1) * $invoice->getBenefit());
  1137.         $invoiceRec->setTotalNet((-1) * $invoice->getTotalNet());
  1138.         $invoiceRec->setVatTotalGroup($newIvas);
  1139.         $invoiceRec->setTotal((-1) * $invoice->getTotal());
  1140.         $invoiceRec->setInvoiceToRec($id);
  1141.         $em->persist($invoiceRec);
  1142.         $em->flush();
  1143.         // Creamos los Items de la factura rectificativa
  1144.         foreach ($invoiceItems as $item){
  1145.             $htInvItem = new HtInvoiceItems();
  1146.             $htInvItem->setInvoice($invoiceRec);
  1147.             $htInvItem->setHtFile($item->getHtFile());
  1148.             $htInvItem->setItemType($item->getItemType());
  1149.             $htInvItem->setItemIdOrigin($item->getItemIdOrigin());
  1150.             $htInvItem->setItemName($item->getItemName());
  1151.             $htInvItem->setItemDateStart($item->getItemDateStart());
  1152.             $htInvItem->setItemDateEnd($item->getItemDateEnd());
  1153.             $htInvItem->setItemPrice((-1) * $item->getItemPrice());
  1154.             $htInvItem->setItemUnits($item->getItemUnits());
  1155.             $htInvItem->setItemPax($item->getItemPax());
  1156.             $htInvItem->setItemKgLt($item->getItemKgLt());
  1157.             $htInvItem->setItemRank($item->getItemRank());
  1158.             $htInvItem->setItemLocation($item->getItemLocation());
  1159.             $htInvItem->setItemIva($item->getItemIva());
  1160.             $htInvItem->setItemOpIva($item->isItemOpIva());
  1161.             $htInvItem->setItemCommission((-1) * $item->getItemCommission());
  1162.             $htInvItem->setItemOpCommission($item->isItemOpCommission());
  1163.             $htInvItem->setItemOver((-1) * $item->getItemOver());
  1164.             $htInvItem->setItemOpOver($item->isItemOpOver());
  1165.             $htInvItem->setItemSubTotalPrice((-1) * $item->getItemSubTotalPrice());
  1166.             $htInvItem->setItemCosteDirecto((-1) * $item->getItemCosteDirecto());
  1167.             $htInvItem->setItemBeneficio((-1) * $item->getItemBeneficio());
  1168.             $em->persist($htInvItem);
  1169.             $em->flush();
  1170.         }
  1171.         return $this->redirectToRoute('ht_view_invoice_rec', ['id' => $invoiceRec->getPrefix(). $invoiceRec->getIdNum()], Response::HTTP_SEE_OTHER);
  1172.     }
  1173.     /**
  1174.      * @Route("/createproforma/{id}",  name="ht_create_proforma")
  1175.      * Generar proforma en el expediente ID
  1176.      */
  1177.     public function createProformaHtAction($idEntityManagerInterface $emRequest $request)
  1178.     {
  1179.         $req $request->request->get('invoiced_item');
  1180.         $clientName $request->request->get('clientName');
  1181.         $req_extra $request->request->get('invoiced_extra');
  1182.         $htFile $em->getRepository(HtFile::class)->findOneById($id);
  1183.         $beneficio_total 0;
  1184.         foreach ($req as $itemServ){
  1185.             foreach ($itemServ as $item) {
  1186.                 if ($item['boolToInvoice'] == 'true') {
  1187.                     $beneficio_total += $item['benefit'];
  1188.                 }
  1189.             }
  1190.         }
  1191.         // Se genera la proforma
  1192.         // buscamos por el prefijo de cada linea de facturacion para saber el ultimo ID
  1193.         $invoiceLineGp $em->getRepository(HtInvoice::class)->findByPrefix('GP');
  1194.         $invoiceLineCvr $em->getRepository(HtInvoice::class)->findByPrefix('CV');
  1195.         $invoiceLineOther $em->getRepository(HtInvoice::class)->findByPrefix('F');
  1196.         $lastGp end($invoiceLineGp);
  1197.         $lastCvr end($invoiceLineCvr);
  1198.         $lastOther end($invoiceLineOther);
  1199.         // Buscamos los HtIemts del HtFile para saber la sala del servicio para determinar la línea de facturación ($prefix)
  1200.         $htItems $em->getRepository(HtItem::class)->findByHtFile($htFile);
  1201.         $lounge $htItems[0]->getLoungeGp();
  1202.         $prefix = (empty($lounge) ? 'F' : (($lounge->getId() < 22) ? 'GP' 'CV'));
  1203.         switch ($prefix) {
  1204.             case 'F'$idNum = (empty($lastOther)) ? $lastOther->getIdNum() + 1; break;
  1205.             case 'CV'$idNum = (empty($lastCvr)) ? $lastCvr->getIdNum() + 1; break;
  1206.             default:
  1207.                 // Por defecto Green Patio.
  1208.                 // Esteban Rincon comenta que en caso de un expediente tener salas de GP y CVR deberiamos tener un expediente por linea de facturación
  1209.                 $idNum = (empty($lastGp)) ? $lastGp->getIdNum() + 1;
  1210.                 break;
  1211.         }
  1212.         $client = (empty($htFile->getClient())) ? null $htFile->getClient();
  1213.         // Si no hay cliente no se puede facturar, se vuelve a la pantalla de seleccion
  1214.         if (empty($client)){ return $this->redirectToRoute('ht_select_invoice', ['id' => $htFile->getId()], Response::HTTP_SEE_OTHER); }
  1215.         $dateAt = new DateTimeImmutable('now');
  1216.         $type 'Proforma';
  1217.         $totalNet 0$vatTotalGroup 0$total 0;
  1218.         if (empty($clientName)){ $clientName $client->getName(); }
  1219.         $clientAddress $request->request->get('clientAddress');
  1220.         $clientDocument $request->request->get('clientDocument');
  1221.         if (empty($clientDocument)){ $clientDocument = empty($client->getIdDocument()) ? ' ' $client->getIdDocument(); }
  1222.         $clientType $request->request->get('clientType');
  1223.         if ($clientType == 'supplier'){
  1224.             $type 'Proforma de Factura de Comision';
  1225.             $clientName $request->request->get('clientName');
  1226.             $clientDocument $request->request->get('clientDocument');
  1227.         }
  1228.         $proforma = new HtProforma();
  1229.         $proforma->setIdNum($idNum);
  1230.         $proforma->setPrefix($prefix);
  1231.         $proforma->setDateAt($dateAt);
  1232.         $proforma->setType($type);
  1233.         $proforma->setTotalNet($totalNet);
  1234.         $proforma->setVatTotalGroup($vatTotalGroup);
  1235.         $proforma->setTotal($total);
  1236.         $proforma->setClientName($clientName);
  1237.         $proforma->setClientAddress($clientAddress);
  1238.         $proforma->setClientDocument($clientDocument);
  1239.         $proforma->setClientType($clientType);
  1240.         $proforma->setHtFile($htFile->getId());
  1241.         $proforma->setBenefit($beneficio_total);
  1242.         $em->persist($proforma);
  1243.         $em->flush();
  1244.         $data_iva = array(); $data_iva['ivaMontoVeintiUno'] = 0$data_iva['ivaMontoDiez'] = 0;
  1245.         // Se crean los Menus en los HtInvoiceItems
  1246.         foreach ($req as $itemServ){
  1247.             foreach ($itemServ as $item) {
  1248.                 if ($item['boolToInvoice'] == 'true') {
  1249.                     $htMenu $em->getRepository(HtMenu::class)->findOneByHtItem($item['idHtService']);
  1250.                     $itemType 'htMenu';
  1251.                     $itemIdOrigin $htMenu->getId();
  1252.                     $itemName $item['name'];
  1253.                     $htItem $em->getRepository(HtItem::class)->findOneById($htMenu->getHtItem());
  1254.                     $dateStart = ($htItem->getDateStart())->format('Y-m-d');
  1255.                     $hourEnd = ($htItem->getHourEnd())->format('Y-m-d');
  1256.                     $dateTimeString $dateStart ' ' $hourEnd;
  1257.                     $dateEnd = new DateTimeImmutable($dateTimeString);
  1258.                     $iva $htMenu->getIva();
  1259.                     switch ($iva) {
  1260.                         case 10$data_iva['ivaMontoDiez'] += ($item['subTotalPrice'] * 0.1); break;
  1261.                         case 21$data_iva['ivaMontoVeintiUno'] += ($item['subTotalPrice'] * 0.21); break;
  1262.                         default: break;
  1263.                     }
  1264.                     $itemCommission 0;                                                                // Solo htExtra maneja el concepto de over o comision.
  1265.                     $itemOver 0;                                                                      // Solo htExtra maneja el concepto de over o comision.
  1266.                     $costeDirecto $item['subTotalPrice'] - $item['benefit'];
  1267.                     $totalNet += round($item['subTotalPrice'], 2);
  1268.                     $total += round($item['subTotalPrice'], 2) + (round($item['subTotalPrice'], 2) * ($iva 100));
  1269.                     $htInvItem = new HtProformaItems();
  1270.                     $htInvItem->setProforma($proforma);
  1271.                     $htInvItem->setHtFile($htFile->getId());
  1272.                     $htInvItem->setItemType($itemType);
  1273.                     $htInvItem->setItemIdOrigin($itemIdOrigin);
  1274.                     $htInvItem->setItemName($itemName);
  1275.                     $htInvItem->setItemDateStart($htItem->getDateStart());
  1276.                     $htInvItem->setItemDateEnd($dateEnd);
  1277.                     $htInvItem->setItemPrice(round($item['servicePrice'], 2));
  1278.                     $htInvItem->setItemUnits(1);
  1279.                     $htInvItem->setItemPax($item['qty']);
  1280.                     $htInvItem->setItemKgLt(1);
  1281.                     $htInvItem->setItemRank(1);
  1282.                     $htInvItem->setItemLocation(0);
  1283.                     $htInvItem->setItemIva($iva);
  1284.                     $htInvItem->setItemOpIva(0);            // +
  1285.                     $htInvItem->setItemCommission($itemCommission);
  1286.                     $htInvItem->setItemOpCommission(0);
  1287.                     $htInvItem->setItemOver($itemOver);
  1288.                     $htInvItem->setItemOpOver(0);
  1289.                     $htInvItem->setItemSubTotalPrice(round($item['subTotalPrice'], 2));
  1290.                     $htInvItem->setItemCosteDirecto(round($costeDirecto2));
  1291.                     $htInvItem->setItemBeneficio(round($item['benefit'], 2));
  1292.                     $em->persist($htInvItem);
  1293.                     $em->flush();
  1294.                 }
  1295.             }
  1296.         }
  1297.         // Se crean los Extras en los HtInvoiceItems
  1298.         if (!empty($req_extra)) {
  1299.             foreach ($req_extra as $item) {
  1300.                 if($item['boolToInvoice'] == 'true') {
  1301.                     $htExtra $em->getRepository(HtExtra::class)->findOneById($item['idHtExtra']);
  1302.                     if ($htExtra->isAppearInvoice()) {
  1303.                         $itemType 'htExtra';
  1304.                         $itemIdOrigin $htExtra->getId();
  1305.                         $itemName $htExtra->getConcept();
  1306.                         $htItem $em->getRepository(HtItem::class)->findOneById($htExtra->getHtItem());
  1307.                         $dateStart = ($htItem->getDateStart())->format('Y-m-d');
  1308.                         $hourEnd = ($htItem->getHourEnd())->format('Y-m-d');
  1309.                         $dateTimeString $dateStart ' ' $hourEnd;
  1310.                         $dateEnd = new DateTimeImmutable($dateTimeString);
  1311.                         $iva $htExtra->getVat();
  1312.                         $itemCommission = (($htExtra->isOpOverCommission() == 0) ? $htExtra->getOverCommission());
  1313.                         $itemOver = (($htExtra->isOpOverCommission() == 1)  ? $htExtra->getOverCommission());
  1314.                         $costeDirecto 0;
  1315.                         $benefit $itemOver + ($item['price'] * ($itemCommission 100));
  1316.                         $totalPrice $item['price'] + $benefit;
  1317.                         switch ($iva) {
  1318.                             case 10$data_iva['ivaMontoDiez'] += ($totalPrice 0.1); break;
  1319.                             case 21$data_iva['ivaMontoVeintiUno'] += ($totalPrice 0.21); break;
  1320.                             default: break;
  1321.                         }
  1322.                         $totalNet += round($totalPrice2);
  1323.                         $total += round($totalPrice2) + (round($totalPrice2) * ($iva 100));
  1324.                         $htInvItem = new HtProformaItems();
  1325.                         $htInvItem->setProforma($proforma);
  1326.                         $htInvItem->setHtFile($htFile->getId());
  1327.                         $htInvItem->setItemType($itemType);
  1328.                         $htInvItem->setItemIdOrigin($itemIdOrigin);
  1329.                         $htInvItem->setItemName($itemName);
  1330.                         $htInvItem->setItemDateStart($htItem->getDateStart());
  1331.                         $htInvItem->setItemDateEnd($dateEnd);
  1332.                         $htInvItem->setItemPrice(round($item['price'], 2));
  1333.                         $htInvItem->setItemUnits(1);
  1334.                         $htInvItem->setItemPax(1);
  1335.                         $htInvItem->setItemKgLt(1);
  1336.                         $htInvItem->setItemRank(1);
  1337.                         $htInvItem->setItemLocation(0);
  1338.                         $htInvItem->setItemIva($iva);
  1339.                         $htInvItem->setItemOpIva(0);            // +
  1340.                         $htInvItem->setItemCommission($itemCommission);
  1341.                         $htInvItem->setItemOpCommission(0);
  1342.                         $htInvItem->setItemOver($itemOver);
  1343.                         $htInvItem->setItemOpOver(0);
  1344.                         $htInvItem->setItemSubTotalPrice(round($totalPrice2));
  1345.                         $htInvItem->setItemCosteDirecto(round($costeDirecto2));
  1346.                         $htInvItem->setItemBeneficio(round($benefit2));
  1347.                         $em->persist($htInvItem);
  1348.                         $em->flush();
  1349.                     }
  1350.                 }
  1351.             }
  1352.         }
  1353.         $string_data_iva '[21]'.round($data_iva['ivaMontoVeintiUno'],2).'[21]'.'[10]'.round($data_iva['ivaMontoDiez'],2).'[10]';
  1354.         // Se actualiza la factura con los datos globales
  1355.         $proforma->setTotalNet($totalNet);
  1356.         $proforma->setVatTotalGroup($string_data_iva);
  1357.         $proforma->setTotal(round($total,2));
  1358.         $em->persist($proforma);
  1359.         $em->flush();
  1360.         return $this->redirectToRoute('ht_view_proforma', ['id' => $proforma->getId()], Response::HTTP_SEE_OTHER);
  1361.     }
  1362.     /**
  1363.      * @Route("/viewproforma/{id}",  name="ht_view_proforma")
  1364.      * Ver la proforma número ID
  1365.      */
  1366.     public function showProformaHtAction($idEntityManagerInterface $emRequest $request)
  1367.     {
  1368.         $user $this->getUser();
  1369.         $proforma $em->getRepository(HtProforma::class)->findOneById($id);
  1370.         // Si no consigue la proforma redirige al listado de expedientes
  1371.         if (empty($proforma)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  1372.         $datAt = (!empty($proforma)) ? $proforma->getDateAt() : null;
  1373.         $totales_neto = (!empty($proforma)) ? $proforma->getTotalNet() : 0;
  1374.         $totales_iva = (!empty($proforma)) ? $proforma->getVatTotalGroup() : 0;
  1375.         $totales = (!empty($proforma)) ? $proforma->getTotal() : 0;
  1376.         $htProformaItems $em->getRepository(HtProformaItems::class)->findByProforma($proforma);
  1377.         $htFile $em->getRepository(HtFile::class)->findOneById($proforma->getHtFile());
  1378.         $clientName = (!empty($proforma)) ? $proforma->getClientName() : 'No se pudo determinar el cliente';
  1379.         $clientAddress = (!empty($proforma)) ? $proforma->getClientAddress() : 'No se pudo determinar la dirección';
  1380.         $clientDocument = (!empty($proforma)) ? $proforma->getClientDocument() : 'No se pudo determinar';
  1381.         $type $proforma->getType();
  1382.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  1383.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  1384.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  1385.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  1386.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  1387.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  1388.         foreach ($htProformaItems as $key => $item){
  1389.             if ($item->getItemType()=='htMenu') {
  1390.                 $datasupplier['invoice_item'][] = array(
  1391.                     'id' => $item->getId(),
  1392.                     'itemName' => $item->getItemName(),
  1393.                     'itemPrice' => $item->getItemPrice(),
  1394.                     'itemPax' => $item->getItemPax(),
  1395.                     'itemDateStart' => $item->getItemDateStart(),
  1396.                     'itemDateEnd' => $item->getItemDateEnd(),
  1397.                     'itemIva' => $item->getItemIva(),
  1398.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1399.                 );
  1400.                 $netoSumItems += $item->getItemSubTotalPrice();
  1401.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  1402.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1403.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1404.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1405.                 switch ($item->getItemIva()) {
  1406.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1407.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1408.                     default: break;
  1409.                 }
  1410.             }
  1411.             if ($item->getItemType()=='htExtra') {
  1412.                 $datasupplier['invoice_extra'][] = array(
  1413.                     'id' => $item->getId(),
  1414.                     'itemType' => $item->getItemType(),
  1415.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  1416.                     'itemName' => $item->getItemName(),
  1417.                     'itemPrice' => $item->getItemSubTotalPrice(),
  1418.                     'itemPax' => $item->getItemPax(),
  1419.                     'itemDateStart' => $item->getItemDateStart(),
  1420.                     'itemDateEnd' => $item->getItemDateEnd(),
  1421.                     'itemIva' => $item->getItemIva(),
  1422.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1423.                 );
  1424.                 $netoSumItems += $item->getItemSubTotalPrice();
  1425.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  1426.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1427.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1428.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1429.                 switch ($item->getItemIva()) {
  1430.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1431.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1432.                     default: break;
  1433.                 }
  1434.             }
  1435.         }
  1436.         $allInvoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  1437.         $boolToRec false;
  1438.         $proformas $em->getRepository(HtProforma::class)->findByHtFile($htFile->getId());
  1439.         $fid $proforma->getId();
  1440.         return $this->render('higotrigo/ht_invoice/show-invoice.html.twig',
  1441.             array(
  1442.                 'id' => $htFile->getId(),
  1443.                 'cid' => '',
  1444.                 'fid' => $fid,
  1445.                 'invoice' => $proforma,
  1446.                 'allInvoices' => $allInvoices,
  1447.                 'proformas' => $proformas,
  1448.                 'numberadmin' => '',
  1449.                 'type' => $type,
  1450.                 'number' => $proforma->getId(),
  1451.                 'prefix' => $proforma->getPrefix(),
  1452.                 'date' => $datAt,
  1453.                 'reservation' => $htFile,
  1454.                 'company' => $company,
  1455.                 'user' => $user,
  1456.                 'boolToRec' => $boolToRec,
  1457.                 'invoicedeposititems' => '',
  1458.                 'datasupplier' => $datasupplier,
  1459.                 'totales_neto' => $netoSumItems,
  1460.                 'totales_iva' => $totales_iva,
  1461.                 'totales' => $totalSumItems,
  1462.                 'bases_imponibles' => $data_iva,
  1463.                 'paymentInvoice' => null,
  1464.                 'currency' => '€',
  1465.                 'clientName' => $clientName,
  1466.                 'clientAddress' => $clientAddress,
  1467.                 'clientDocument' => $clientDocument,
  1468.             )
  1469.         );
  1470.     }
  1471.     /**
  1472.      * @Route("/htexternal/viewprintproforma/{id}",  name="ht_view_print_proforma")
  1473.      * Ver la proforma del expediente ID en modalidad de impresion
  1474.      */
  1475.     public function printProformaHtAction($idEntityManagerInterface $emRequest $request)
  1476.     {
  1477.         $proforma $em->getRepository(HtProforma::class)->findOneById($id);
  1478.         // Si no consigue la proforma redirige al listado de expedientes
  1479.         if (empty($proforma)){ return $this->redirectToRoute('app_ht_file_index', [], Response::HTTP_SEE_OTHER); }
  1480.         $datAt = (!empty($proforma)) ? $proforma->getDateAt() : null;
  1481.         $totales_neto = (!empty($proforma)) ? $proforma->getTotalNet() : 0;
  1482.         $totales_iva = (!empty($proforma)) ? $proforma->getVatTotalGroup() : 0;
  1483.         $totales = (!empty($proforma)) ? $proforma->getTotal() : 0;
  1484.         $htProformaItems $em->getRepository(HtProformaItems::class)->findByProforma($proforma);
  1485.         $htFile $em->getRepository(HtFile::class)->findOneById($id);
  1486.         $clientName = (!empty($proforma)) ? $proforma->getClientName() : 'No se pudo determinar el cliente';
  1487.         $clientAddress = (!empty($proforma)) ? $proforma->getClientAddress() : 'No se pudo determinar la dirección';
  1488.         $clientDocument = (!empty($proforma)) ? $proforma->getClientDocument() : 'No se pudo determinar';
  1489.         $type $proforma->getType();
  1490.         $company $em->getRepository(SettingsCompany::class)->findOneByPriority('5');
  1491.         $bases_imponibles = array('21'=> 0,'10'=> 0,'0'=> 0,);
  1492.         $datasupplier = array(); $netoSumItems 0$totalSumItems 0;
  1493.         $data_iva = array('ivaMontoDiez' => 0'ivaMontoVeintiUno' => 0);
  1494.         $datasupplier['invoice_item_net_sum'] = 0$datasupplier['invoice_item_sum'] = 0;
  1495.         $datasupplier['invoice_extra_net_sum'] = 0$datasupplier['invoice_extra_sum'] = 0;
  1496.         foreach ($htProformaItems as $key => $item){
  1497.             if ($item->getItemType()=='htMenu') {
  1498.                 $datasupplier['invoice_item'][] = array(
  1499.                     'id' => $item->getId(),
  1500.                     'itemName' => $item->getItemName(),
  1501.                     'itemPrice' => $item->getItemPrice(),
  1502.                     'itemPax' => $item->getItemPax(),
  1503.                     'itemDateStart' => $item->getItemDateStart(),
  1504.                     'itemDateEnd' => $item->getItemDateEnd(),
  1505.                     'itemIva' => $item->getItemIva(),
  1506.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1507.                 );
  1508.                 $netoSumItems += $item->getItemSubTotalPrice();
  1509.                 $datasupplier['invoice_item_net_sum'] += $item->getItemSubTotalPrice();
  1510.                 $datasupplier['invoice_item_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1511.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1512.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1513.                 switch ($item->getItemIva()) {
  1514.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1515.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1516.                     default: break;
  1517.                 }
  1518.             }
  1519.             if ($item->getItemType()=='htExtra') {
  1520.                 $datasupplier['invoice_extra'][] = array(
  1521.                     'id' => $item->getId(),
  1522.                     'itemType' => $item->getItemType(),
  1523.                     'itemIdOrigin' => $item->getItemIdOrigin(),
  1524.                     'itemName' => $item->getItemName(),
  1525.                     'itemPrice' => $item->getItemSubTotalPrice(),
  1526.                     'itemPax' => $item->getItemPax(),
  1527.                     'itemDateStart' => $item->getItemDateStart(),
  1528.                     'itemDateEnd' => $item->getItemDateEnd(),
  1529.                     'itemIva' => $item->getItemIva(),
  1530.                     'itemSubTotalPrice' => round($item->getItemSubTotalPrice() * (+ ($item->getItemIva()/100)),2),
  1531.                 );
  1532.                 $netoSumItems += $item->getItemSubTotalPrice();
  1533.                 $datasupplier['invoice_extra_net_sum'] += $item->getItemSubTotalPrice();
  1534.                 $datasupplier['invoice_extra_sum'] += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1535.                 $totalSumItems += $item->getItemSubTotalPrice() * (+ ($item->getItemIva() / 100));
  1536.                 $montoIva $item->getItemSubTotalPrice() * ($item->getItemIva() / 100);
  1537.                 switch ($item->getItemIva()) {
  1538.                     case 10$data_iva['ivaMontoDiez'] += $montoIva; break;
  1539.                     case 21$data_iva['ivaMontoVeintiUno'] += $montoIva; break;
  1540.                     default: break;
  1541.                 }
  1542.             }
  1543.         }
  1544.         $allInvoices $em->getRepository(HtInvoice::class)->findByHtFile($htFile);
  1545.         $boolToRec false;
  1546.         $fid $proforma->getPrefix() . $proforma->getIdNum();
  1547.         return $this->render('higotrigo/ht_invoice/proforma-print.html.twig',
  1548.             array(
  1549.                 'id' => $id,
  1550.                 'fid' => $fid,
  1551.                 'type' => 'Proforma',
  1552.                 'number' => $id,
  1553.                 'date' => $datAt,
  1554.                 'company' => $company,
  1555.                 'invoicedeposititems' => '',
  1556.                 'datasupplier' => $datasupplier,
  1557.                 'totales_neto' => $netoSumItems,
  1558.                 'totales' => $totalSumItems,
  1559.                 'bases_imponibles' => $data_iva,
  1560.                 'currency' => '€',
  1561.                 'clientName' => $clientName,
  1562.                 'clientAddress' => $clientAddress,
  1563.                 'clientDocument' => $clientDocument,
  1564.             )
  1565.         );
  1566.     }
  1567.     private function transformarString($input) {
  1568.         // Expresión regular para encontrar números entre los delimitadores [21] y [10]
  1569.         $pattern '/\[21\](\d+\.?\d*)\[21\]|\[10\](\d+\.?\d*)\[10\]/';
  1570.         // Función de reemplazo que multiplica los números por -1
  1571.         $callback = function ($matches) {
  1572.             if (isset($matches[1]) && $matches[1] !== '') { return '[21]' . (-$matches[1]) . '[21]'; } elseif (isset($matches[2]) && $matches[2] !== '') { return '[10]' . (-$matches[2]) . '[10]'; }
  1573.             return $matches[0]; // No debería llegar aquí
  1574.         };
  1575.         // Realizar el reemplazo en el string original
  1576.         $output preg_replace_callback($pattern$callback$input);
  1577.         return $output;
  1578.     }
  1579.     /**
  1580.      * Formatea el campo vatTotalGroup a una cadena como "157.00 (21%)"
  1581.      */
  1582.     private function formatVatTotalGroup($vatTotalGroup): string
  1583.     {
  1584.         // Regex para extraer valores, porcentajes y manejar valores negativos
  1585.         preg_match_all('/\[(\d+)\](-?\d+(\.\d+)?)/'$vatTotalGroup$matches);
  1586.         $formattedVat '';
  1587.         foreach ($matches[1] as $index => $percentage) {
  1588.             // Asegurarse de que los números se formateen correctamente
  1589.             $amount number_format((float)$matches[2][$index], 2',''.');
  1590.             $formattedVat .= "$amount ({$percentage}%)<br>";
  1591.         }
  1592.         return $formattedVat;
  1593.     }
  1594. }