src/Noahtech/Sistemas/InterjamaBundle/Handler/McCajaHandler.php line 130

Open in your IDE?
  1. <?php
  2. namespace Noahtech\Sistemas\InterjamaBundle\Handler;
  3. use Noahtech\Sistemas\InterjamaBundle\Entity\McCaja;
  4. use Noahtech\Sistemas\InterjamaBundle\Utils\Constants;
  5. use Noahtech\Sistemas\InterjamaBundle\Utils\EmailsMessages;
  6. use Noahtech\Sistemas\InterjamaBundle\Utils\Encrypt;
  7. use DateInterval;
  8. use DateTime;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Noahtech\Sistemas\InterjamaBundle\Utils\PDF;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class McCajaHandler extends BaseHandler {
  16.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManager) {
  17.         $this->container $container;
  18.         $this->entityManager $entityManager;
  19.         $this->repository $entityManager->getRepository(McCaja::class);
  20.     }
  21.     
  22.     public function search($offset$limit$sortField$sortDirection$searchParam) {
  23.         $lp $this->repository->search($offset$limit$sortField$sortDirection$searchParam);
  24.         $cajas $this->toarray($lp->getListado(), 'caja');
  25.         foreach ($cajas as &$caja) {
  26.             $caja['caja_monedas'] = [];
  27.             $monedas $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  28.             foreach ($monedas as $moneda) {
  29.                 array_push($caja['caja_monedas'], $moneda);
  30.             }
  31.         }
  32.         $lp->setListado($cajas);
  33.         return $lp;
  34.     }
  35.     public function getCajaFromRequest(Request $request$operador$tipoint $id null):McCaja {
  36.         $cerrado = ($request->request->get('derrado') ? true false);
  37.         $fechaCreacion =  $request->request->get('fecha_creacion');
  38.         $caja $this->repository->getCajaByTipoCerrada($tipo$id);
  39.         if (COUNT($caja) > 0) {
  40.             throw new HttpException(409"Ya existe una caja abierta. Recuerde que solo puede haber una sola caja abierta.");
  41.         }
  42.         if (is_null($id)) {
  43.             $caja = new McCaja();
  44.             $caja->setFechaCreacion(new DateTime());          
  45.         } else {
  46.             $caja $this->repository->findOneById($id);
  47.             $caja->setFechaActualizacion(new DateTime());            
  48.         }
  49.         $caja->setTipo($tipo);
  50.         $caja->setCerrado($cerrado);
  51.         $caja->setUsuarioOpen($operador);
  52.         return $caja;
  53.     }
  54.     public function save(Request $requestMcCaja $caja) {
  55.         $cajaMonedas $request->request->get('monedas');
  56.         $caja $this->repository->save($caja);
  57.         //Guardo los datos de las monedas para la caja
  58.         if (COUNT($cajaMonedas) > 0) {
  59.             $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->saveOrUpdateCajaMoneda($caja$cajaMonedas);
  60.         }        
  61.         $caja $this->toarray($caja"caja");
  62.         return $caja;
  63.     }
  64.     public function getById($id) {
  65.         $caja $this->repository->findOneById($id);
  66.         $caja $this->toarray($caja"caja");
  67.         $caja['caja_monedas'] = [];
  68.         $monedas $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  69.         foreach ($monedas as $moneda) {
  70.             array_push($caja['caja_monedas'], $moneda);
  71.         }
  72.         return $caja;
  73.     }
  74.     public function update(Request $requestMcCaja $cajaint $id) {
  75.         $cajaMonedas $request->request->get('monedas');
  76.         $cajaMonedasRemover $request->request->get('monedasRemover');
  77.         $caja $this->repository->save($caja);
  78.         // Elimino las monedas que se sacaron en la edicion
  79.         if (!is_null($cajaMonedasRemover)) {
  80.             if (COUNT($cajaMonedasRemover) > 0) {
  81.                 $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->removeCajaMoneda($cajaMonedasRemover);
  82.             }
  83.         }
  84.         // Guardo los datos de las monedas para la caja
  85.         if (!is_null($cajaMonedas)) {
  86.             if (COUNT($cajaMonedas) > 0) {
  87.                 $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->saveOrUpdateCajaMoneda($caja$cajaMonedas);
  88.             }
  89.         }    
  90.         $caja $this->toarray($caja"caja");
  91.         return $caja;
  92.     }
  93.     public function getCajaEstadoFromRequest(Request $request$operador$tipoint $id):McCaja {
  94.         $cerrado = ($request->request->get('cerrado') ? true false);
  95.         $caja $this->repository->findOneById($id);
  96.         if (!$cerrado) {
  97.             $fechaCreacion =  $caja->getFechaCreacion();
  98.             $today = new DateTime();
  99.             if ($today->format('d/m/Y') == $fechaCreacion->format('d/m/Y')) {
  100.                 $cajaAux $this->repository->getCajaByFechaAndTipo($today->format('Y-m-d'), $tipo$id);
  101.                 if (COUNT($cajaAux) > 0) {
  102.                     throw new HttpException(409"Ya existe una caja para la fecha de hoy.");
  103.                 }
  104.             } else {
  105.                 throw new HttpException(409"No se puede crear/modificar una caja para la fecha seleccionada.");
  106.             }
  107.         }
  108.         $caja->setFechaActualizacion(new DateTime());
  109.         $caja->setCerrado($cerrado);
  110.         if ($cerrado) {
  111.             $caja->setUsuarioClose($operador);
  112.         } else {
  113.             $caja->setUsuarioClose(null);
  114.         }
  115.        
  116.         return $caja;
  117.     }
  118.     public function getCajaById($id) {
  119.         return $this->repository->findOneById($id);
  120.     }
  121.     public function  numeroAddZero($nro) {
  122.         $numero "";
  123.         $nro1 strlen((string)$nro);      
  124.         switch($nro1) {
  125.             case 1:
  126.                 $numero "000000" $nro;
  127.             break;
  128.             case 2:
  129.                 $numero "00000" $nro;                
  130.             break;
  131.             case 3:
  132.                 $numero "0000" $nro;                
  133.             break;
  134.             case 4:
  135.                 $numero "000" $nro;                
  136.             break;
  137.             case 5:
  138.                 $numero "00" $nro;                
  139.             break;
  140.             case 6:
  141.                 $numero "0" $nro;                
  142.             break;
  143.             case 7:
  144.                 $numero $nro;                
  145.             break;
  146.         }
  147.         return $numero;
  148.       }
  149.     /*
  150.     public function descargaReciboCajaPdf($id) {
  151.         $caja = $this->getById($id);
  152.         $caja['caja_monedas'] = [];
  153.         $monedas = $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  154.         foreach ($monedas as $moneda) {
  155.             array_push($caja['caja_monedas'], $moneda);
  156.         }
  157.         $numero = $this->numeroAddZero($caja['id']);        
  158.         $fecha = date("d/m/Y H:m", strtotime($caja['fecha_actualizacion']));
  159.         $usuarioClose = $caja['usuario_close']['persona']['nombre'] ." ". $caja['usuario_close']['persona']['apellido'];
  160.         // Obtengo losmovimientos de la caja
  161.         $movimientos = $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McMovimientoHandler")->getAllMovimientosByCaja($id);
  162.         // Calculos de los monto por moneda
  163.         $totales = [];
  164.         foreach ($monedas as $moneda) {
  165.             $nombreMoneda = $moneda['moneda']['nombre'];
  166.             $montoTotalEfectivo = 0;
  167.             $montoTotalTransferencia = 0;
  168.             $montoTotalPorConvenio = 0;
  169.             $montoTotalCuentaCorriente = 0;
  170.             if (COUNT($movimientos) > 0) {
  171.                 foreach ($movimientos as $movimiento) {
  172.                     if ((int)$moneda['moneda']['id'] == (int)$movimiento['moneda']['id']) {
  173.                         if (strtolower($movimiento['modalidad']) == 'contado/efectivo') {
  174.                             if ($movimiento['tipo'] == 'Ingreso') {
  175.                                 $montoTotalEfectivo = $montoTotalEfectivo + $movimiento['monto'];
  176.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  177.                                 $montoTotalEfectivo = $montoTotalEfectivo - $movimiento['monto'];
  178.                             }
  179.                         }
  180.                         if (strtolower($movimiento['modalidad']) == 'transferencia') {
  181.                             if ($movimiento['tipo'] == 'Ingreso') {
  182.                                 $montoTotalTransferencia = $montoTotalTransferencia + $movimiento['monto'];
  183.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  184.                                 $montoTotalTransferencia = $montoTotalTransferencia - $movimiento['monto'];
  185.                             }
  186.                         }
  187.                         if (strtolower($movimiento['modalidad']) == 'pago por convenio') {
  188.                             if ($movimiento['tipo'] == 'Ingreso') {
  189.                                 $montoTotalPorConvenio = $montoTotalPorConvenio + $movimiento['monto'];
  190.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  191.                                 $montoTotalPorConvenio = $montoTotalPorConvenio - $movimiento['monto'];
  192.                             }
  193.                         }
  194.                         if (strtolower($movimiento['modalidad']) == 'cuenta corriente') {
  195.                             if ($movimiento['tipo'] == 'Ingreso') {
  196.                                 $montoTotalCuentaCorriente = $montoTotalCuentaCorriente + $movimiento['monto'];
  197.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  198.                                 $montoTotalCuentaCorriente = $montoTotalCuentaCorriente - $movimiento['monto'];
  199.                             }
  200.                         }
  201.                     } 
  202.                 }
  203.             }
  204.             $totales[] = array(
  205.                 'nombreMoneda' => $nombreMoneda,
  206.                 'montoTotalEfectivo' => $montoTotalEfectivo,
  207.                 'montoTotalTransferencia' => $montoTotalTransferencia,
  208.                 'montoTotalPorConvenio' => $montoTotalPorConvenio,
  209.                 'montoTotalCuentaCorriente' => $montoTotalCuentaCorriente
  210.             );
  211.         }
  212.         
  213.         // create new PDF document
  214.         $pdf = new PDF();       
  215.         $pdf->SetTitle("Comprobante Caja Cerrada");
  216.         $pdf->SetSubject("Comprobante Caja Cerrada");
  217.         $pdf->setFontSubsetting(true);
  218.         // set default header data
  219.         $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
  220.         $pdf->SetFont('helvetica', '', 11, '', true);
  221.         $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  222.         $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  223.         $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  224.         $pdf->setPrintFooter(false);
  225.         // set auto page breaks
  226.         $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  227.         // set image scale factor
  228.         $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  229.         $pdf->AddPage('P', 'A4');        
  230.         $html = <<<EOD
  231.         <h1> Comprobante - Cierre de Caja</h1>
  232.         <table table border="0.5">
  233.             <tr>
  234.                 <th style="background-color: grey;"> Caja NĀŗ</th>
  235.                 <td><b> {$numero}</b></td>
  236.             </tr>
  237.             <tr>
  238.                 <th style="background-color: grey;"> Tipo</th>
  239.                 <td><b> {$caja['tipo']}</b></td>
  240.             </tr>
  241.             <tr>
  242.                 <th style="background-color: grey;"> Fecha</th>
  243.                 <td><b> {$fecha}</b></td>
  244.             </tr>
  245.             <tr>
  246.                 <th style="background-color: grey;"> Usuario responsable</th>
  247.                 <td><b> {$usuarioClose}</b></td>
  248.             </tr>
  249.         </table>
  250.         <br/>
  251.         <br/>
  252.         <br/>
  253.         <table border="0.5">
  254.             <tr style="background-color: grey;">
  255.                 <th> Fecha</th>
  256.                 <th> Monto</th>
  257.                 <th> Moneda</th>
  258.                 <th> Modalidad</th>
  259.                 <th> Transacción</th>
  260.             </tr>
  261.         EOD;
  262.         if (COUNT($movimiento) > 0) {
  263.             foreach ($movimientos as $movimiento) {
  264.                 $fecha = date("d/m/Y H:m", strtotime($movimiento['fecha_creacion']));
  265.                 $html .= <<<EOD
  266.                     <tr>
  267.                         <td> {$fecha}</td>
  268.                         <td> $ {$movimiento['monto']}</td>
  269.                         <td> {$movimiento['moneda']['nombre']}</td>
  270.                         <td> {$movimiento['modalidad']}</td>
  271.                         <td> {$movimiento['tipo']}</td>
  272.                     </tr>
  273.                 EOD;
  274.             }
  275.         }
  276.         $html .= <<<EOD
  277.             </table>
  278.         <br/>
  279.         <br/>
  280.         <br/>
  281.         <table border="0.5">
  282.             <tr style="background-color: grey;">
  283.                 <th> Moneda</th>
  284.                 <th> Total Efectivo/Contado</th>
  285.                 <th> Total Transferencia</th>
  286.                 <th> Total Por Convenio</th>
  287.                 <th> Total Cuenta Corriente</th>
  288.             </tr>
  289.         EOD;
  290.         if (COUNT($totales) > 0) {
  291.             foreach ($totales as $total) {
  292.                 $fecha = date("d/m/Y H:m", strtotime($movimiento['fecha_creacion']));
  293.                 $html .= <<<EOD
  294.                     <tr>
  295.                         <td> {$total['nombreMoneda']}</td>
  296.                         <td> $ {$total['montoTotalEfectivo']}</td>
  297.                         <td> $ {$total['montoTotalTransferencia']}</td>
  298.                         <td> $ {$total['montoTotalPorConvenio']}</td>
  299.                         <td> $ {$total['montoTotalCuentaCorriente']}</td>
  300.                     </tr>
  301.                 EOD;
  302.             }
  303.         }
  304.         $html .= <<<EOD
  305.             </table>
  306.         <br/>
  307.         <br/>
  308.         <br/>
  309.         <table border="0.5">
  310.             <tr style="background-color: grey;">
  311.                 <th> Moneda</th>
  312.                 <th> Monto inicial</th>
  313.                 <th> Monto final</th>
  314.             </tr>
  315.         EOD;
  316.         foreach ($caja['caja_monedas'] as $moneda) {
  317.             $html .= <<<EOD
  318.                 <tr>
  319.                     <td> {$moneda['moneda']['nombre']}</td>
  320.                     <td> $ {$moneda['monto_inicial']}</td>
  321.                     <td> $ {$moneda['monto_final']}</td>
  322.                 </tr>
  323.             EOD;
  324.         }
  325.         $html .= <<<EOD
  326.             </table>
  327.         EOD;
  328.         $pdf->writeHTML($html, true, false, false, false, '');
  329.         return new Response($pdf->Output("Comprobante", "S"), 200, array(
  330.             'Content-Type' => 'application/pdf',
  331.             'Content-Disposition' => 'attachment; filename="Caja-Comprobante.pdf"'
  332.                 )
  333.         );
  334.     
  335.     }
  336.     */
  337.   public function descargaReciboCajaPdf($id) {
  338.     $caja $this->getById($id);
  339.     $caja['caja_monedas'] = [];
  340.     $monedas $this->container
  341.         ->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")
  342.         ->getAllMonedasByCaja((int)$caja['id']);
  343.     foreach ($monedas as $moneda) {
  344.         $caja['caja_monedas'][] = $moneda;
  345.     }
  346.     $numero $this->numeroAddZero($caja['id']);
  347.     $fecha date("d/m/Y H:i"strtotime($caja['fecha_actualizacion']));
  348.     $usuarioClose $caja['usuario_close']['persona']['nombre'] ." "$caja['usuario_close']['persona']['apellido'];
  349.     // Movimientos
  350.     $movimientos $this->container
  351.         ->get("Noahtech\Sistemas\InterjamaBundle\Handler\McMovimientoHandler")
  352.         ->getAllMovimientosByCaja($id);
  353.     // ======================================
  354.     //     TOTALES GENERALES (YA EXISTENTES)
  355.     // ======================================
  356.     $totalesIngresos = [];
  357.     foreach ($monedas as $moneda) {
  358.         $idMoneda = (int)$moneda['moneda']['id'];
  359.         $totalesIngresos[$idMoneda] = [
  360.             'moneda' => $moneda['moneda']['nombre'],
  361.             'tramite' => [
  362.                 'efectivo' => 0,
  363.                 'transferencia' => 0,
  364.                 'convenio' => 0,
  365.                 'cuenta_corriente' => 0
  366.             ],
  367.             'mov_ingreso' => [
  368.                 'efectivo' => 0,
  369.                 'transferencia' => 0,
  370.                 'convenio' => 0,
  371.                 'cuenta_corriente' => 0
  372.             ],
  373.             'mov_egreso' => [
  374.                 'efectivo' => 0,
  375.                 'transferencia' => 0,
  376.                 'convenio' => 0,
  377.                 'cuenta_corriente' => 0
  378.             ]
  379.         ];
  380.     }
  381.     // ======================================
  382.     //   NUEVO: DETALLE POR TIPO_DETALLE
  383.     // ======================================
  384.     $detalleMovIng = [];  // ingresos por tipo_detalle
  385.     $detalleMovEgr = [];  // egresos por tipo_detalle
  386.     foreach ($movimientos as $mov) {
  387.         $idMoneda = (int)$mov['moneda']['id'];
  388.         $nombreMoneda $mov['moneda']['nombre'];
  389.         if (!isset($totalesIngresos[$idMoneda])) continue;
  390.         $esTramite = isset($mov['precarga']['id']);
  391.         $esIngreso = ($mov['tipo'] == 'Ingreso');
  392.         $esEgreso  = ($mov['tipo'] == 'Egreso' || $mov['tipo'] == 'Anulacion');
  393.         // Normalizar modalidad
  394.         $mod strtolower($mov['modalidad']);
  395.         if ($mod == 'contado/efectivo'$mod 'efectivo';
  396.         if ($mod == 'pago por convenio'$mod 'convenio';
  397.         if ($mod == 'cuenta corriente'$mod 'cuenta_corriente';
  398.         // ==========================
  399.         //   TOTALES GENERALES
  400.         // ==========================
  401.         if ($esTramite) {
  402.             if ($esIngreso$totalesIngresos[$idMoneda]['tramite'][$mod] += $mov['monto'];
  403.             if ($esEgreso)  $totalesIngresos[$idMoneda]['tramite'][$mod] -= $mov['monto'];
  404.             continue;
  405.         }
  406.         // MOVIMIENTO SIN TRAMITE
  407.         if ($esIngreso) {
  408.             $totalesIngresos[$idMoneda]['mov_ingreso'][$mod] += $mov['monto'];
  409.         }
  410.         if ($esEgreso) {
  411.             $totalesIngresos[$idMoneda]['mov_egreso'][$mod] += $mov['monto'];
  412.         }
  413.         // ====================================
  414.         //   NUEVO: CLASIFICACIƓN POR tipo_detalle
  415.         // ====================================
  416.         $tipo trim($mov['tipo_detalle'] ?? "Sin detalle");
  417.         // Inicializar moneda dentro del tipo_detalle
  418.         if ($esIngreso) {
  419.             if (!isset($detalleMovIng[$tipo][$nombreMoneda])) {
  420.                 $detalleMovIng[$tipo][$nombreMoneda] = 0;
  421.             }
  422.             $detalleMovIng[$tipo][$nombreMoneda] += $mov['monto'];
  423.         }
  424.         if ($esEgreso) {
  425.             if (!isset($detalleMovEgr[$tipo][$nombreMoneda])) {
  426.                 $detalleMovEgr[$tipo][$nombreMoneda] = 0;
  427.             }
  428.             $detalleMovEgr[$tipo][$nombreMoneda] += $mov['monto'];
  429.         }
  430.     }
  431.     // ======================================
  432.     //         GENERACIƓN DEL PDF
  433.     // ======================================
  434.     $pdf = new PDF();
  435.     $pdf->SetTitle("Comprobante Caja Cerrada");
  436.     $pdf->SetSubject("Comprobante Caja Cerrada");
  437.     $pdf->setFontSubsetting(true);
  438.     $pdf->SetFont('helvetica'''11''true);
  439.     $pdf->SetMargins(5,5,5);
  440.     $pdf->setPrintFooter(false);
  441.     $pdf->AddPage('P''A4');
  442.     // ======================================
  443.     //   CABECERA
  444.     // ======================================
  445.     $html = <<<EOD
  446.     <h1>Comprobante - Cierre de Caja</h1>
  447.     <table border="0.5">
  448.     <tr><th style="background-color: grey;"> Caja NĀŗ</th><td><b> {$numero}</b></td></tr>
  449.     <tr><th style="background-color: grey;"> Fecha</th><td><b> {$fecha}</b></td></tr>
  450.     <tr><th style="background-color: grey;"> Usuario responsable</th><td><b> {$usuarioClose}</b></td></tr>
  451.     </table>
  452.     <br/>
  453.     EOD;
  454.     // ======================================
  455.     //   TABLAS ORIGINALES (LAS MANTENEMOS)
  456.     // ======================================
  457.     // Ingresos por trĆ”mites
  458.     $html .= "<h2>Ingresos por TrĆ”mites</h2>";
  459.     $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Moneda</th><th> Efectivo</th><th> Transferencia</th><th> Por Convenio</th><th> Cuenta Corriente</th></tr>';
  460.     foreach ($totalesIngresos as $t) {
  461.         $html .= "<tr>
  462.             <td> {$t['moneda']}</td>
  463.             <td> {$t['tramite']['efectivo']}</td>
  464.             <td> {$t['tramite']['transferencia']}</td>
  465.             <td> {$t['tramite']['convenio']}</td>
  466.             <td> {$t['tramite']['cuenta_corriente']}</td>
  467.         </tr>";
  468.     }
  469.     $html .= "</table>";
  470.     // Ingresos por movimientos
  471.     $html .= "<h2>Ingresos por Movimientos</h2>";
  472.     $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Moneda</th><th> Efectivo</th><th> Transferencia</th><th> Por Convenio</th><th> Cuenta Corriente</th></tr>';
  473.     foreach ($totalesIngresos as $t) {
  474.         $html .= "<tr>
  475.             <td> {$t['moneda']}</td>
  476.             <td> {$t['mov_ingreso']['efectivo']}</td>
  477.             <td> {$t['mov_ingreso']['transferencia']}</td>
  478.             <td> {$t['mov_ingreso']['convenio']}</td>
  479.             <td> {$t['mov_ingreso']['cuenta_corriente']}</td>
  480.         </tr>";
  481.     }
  482.     $html .= "</table>";
  483.     // Egresos por movimientos
  484.     $html .= "<h2>Egresos por Movimientos</h2>";
  485.     $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Moneda</th><th> Efectivo</th><th> Transferencia</th><th> Por Convenio</th><th> Cuenta Corriente</th></tr>';
  486.     foreach ($totalesIngresos as $t) {
  487.         $html .= "<tr>
  488.             <td> {$t['moneda']}</td>
  489.             <td> {$t['mov_egreso']['efectivo']}</td>
  490.             <td> {$t['mov_egreso']['transferencia']}</td>
  491.             <td> {$t['mov_egreso']['convenio']}</td>
  492.             <td> {$t['mov_egreso']['cuenta_corriente']}</td>
  493.         </tr>";
  494.     }
  495.     $html .= "</table>";
  496.     // ======================================
  497.     //   NUEVAS TABLAS POR tipo_detalle
  498.     // ======================================
  499.     if (COUNT($detalleMovIng) > 0) {
  500.         // ---- Ingresos clasificados
  501.         $html .= "<h2>Ingresos por Movimientos (detalle por servicio)</h2>";
  502.         $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Tipo</th><th> Moneda</th><th> Monto</th></tr>';
  503.         foreach ($detalleMovIng as $tipo => $arr) {
  504.             foreach ($arr as $mon => $monto) {
  505.                 $html .= "<tr><td> {$tipo}</td><td> {$mon}</td><td> {$monto}</td></tr>";
  506.             }
  507.         }
  508.         $html .= "</table>";
  509.     }
  510.     if (COUNT($detalleMovEgr) > 0) {
  511.         // ---- Egresos clasificados
  512.         $html .= "<h2>Egresos por Movimientos (detalle por concepto)</h2>";
  513.         $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Tipo</th><th> Moneda</th><th> Monto</th></tr>';
  514.         foreach ($detalleMovEgr as $tipo => $arr) {
  515.             foreach ($arr as $mon => $monto) {
  516.                 $html .= "<tr><td> {$tipo}</td><td> {$mon}</td><td> {$monto}</td></tr>";
  517.             }
  518.         }
  519.         $html .= "</table>";
  520.     }
  521.     
  522.     // ======================================
  523.     //   Totales inicial / final
  524.     // ======================================
  525.     $html .= "<h2>Totales por Moneda</h2>";
  526.     $html .= '<table border="0.5"><tr style="background-color: grey;"><th> Moneda</th><th> Inicial</th><th> Final</th></tr>';
  527.     foreach ($caja['caja_monedas'] as $m) {
  528.         $html .= "<tr>
  529.             <td> {$m['moneda']['nombre']}</td>
  530.             <td> {$m['monto_inicial']}</td>
  531.             <td> {$m['monto_final']}</td>
  532.         </tr>";
  533.     }
  534.     $html .= "</table>";
  535.     // SALIDA FINAL
  536.     $pdf->writeHTML($htmltruefalsefalsefalse'');
  537.     return new Response(
  538.         $pdf->Output("Comprobante""S"),
  539.         200,
  540.         [
  541.             'Content-Type' => 'application/pdf',
  542.             'Content-Disposition' => 'attachment; filename=\"Caja-Comprobante.pdf\"'
  543.         ]
  544.     );
  545. }
  546.     public function saveOperacionCaja($caja$tipo$monto$modalidad$monedaId) {
  547.         if ($modalidad == 'Contado/Efectivo') {
  548.             $cajaMoneda $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), $monedaId);
  549.             if ($tipo == 'Egreso') {
  550.                 if ($cajaMoneda->getMontoFinal() > 0) {
  551.                     if ($cajaMoneda->getMontoFinal() >= $monto) {
  552.                         $montoFinal $cajaMoneda->getMontoFinal();
  553.                         $cajaMoneda->setMontoFinal($montoFinal $monto);
  554.                     } else {
  555.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  556.                     }
  557.                 } else {
  558.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  559.                 }
  560.             } else if($tipo == 'Ingreso' || $tipo == 'Ingreso-Devolución') {
  561.                 $montoFinal $cajaMoneda->getMontoFinal();
  562.                 $cajaMoneda->setMontoFinal($montoFinal $monto);
  563.             }
  564.             $cajaMoneda->setFechaActualizacion(new DateTime());
  565.             return $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->save($cajaMoneda);
  566.         }
  567.     }
  568.     public function saveOperacionDivisasCaja($caja$tipo$tipoDetalle$monto$modalidad$monedaIdfloat $cotizacion) {
  569.         if ($modalidad == 'Contado/efectivo') {
  570.             $cajaMonedaNacional $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), 1);
  571.             $cajaMonedaExtranjero $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), $monedaId);
  572.             //Si se compra moneda extranjera, validamos si tenemos los pesos argentinos para poder comprar.
  573.             if ($tipoDetalle == 'Compra') {
  574.                 if ($cajaMonedaNacional->getMontoFinal() > 0) {
  575.                     if ($cajaMonedaNacional->getMontoFinal() >= $cotizacion) {
  576.                         //Egreso de la caja nacional
  577.                         $montoFinalNacional $cajaMonedaNacional->getMontoFinal();
  578.                         $cajaMonedaNacional->setMontoFinal($montoFinalNacional $cotizacion);
  579.                         //Ingreso de la caja extranjera
  580.                         $montoFinalExtranjero $cajaMonedaExtranjero->getMontoFinal();
  581.                         $cajaMonedaExtranjero->setMontoFinal($montoFinalExtranjero $monto);
  582.                     } else {
  583.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  584.                     }
  585.                 } else {
  586.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  587.                 }
  588.             } else {
  589.                 //Si se vende moneda extranjera, validamos si tenemos la cantidad a vender.
  590.                 if ($cajaMonedaExtranjero->getMontoFinal() > 0) {
  591.                     if ($cajaMonedaExtranjero->getMontoFinal() >= $monto) {
  592.                         //Egreso de la caja extranjera
  593.                         $montoFinalExtranjero $cajaMonedaExtranjero->getMontoFinal();
  594.                         $cajaMonedaExtranjero->setMontoFinal($montoFinalExtranjero $monto);
  595.                         //Ingreso de la caja nacional
  596.                         $montoFinalNacional $cajaMonedaNacional->getMontoFinal();
  597.                         $cajaMonedaNacional->setMontoFinal($montoFinalNacional $cotizacion);
  598.                     } else {
  599.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  600.                     }
  601.                 } else {
  602.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  603.                 }
  604.             }
  605.         }
  606.     }
  607.     public function getCajaAbierta($tipo) {
  608.         $caja $this->repository->getCajaAbierta($tipo);
  609.         if (!is_null($caja)) {
  610.             $caja =  $this->toarray($caja'caja');
  611.             $caja['caja_monedas'] = [];
  612.             $monedas $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  613.             foreach ($monedas as $moneda) {
  614.                 array_push($caja['caja_monedas'], $moneda);
  615.             }
  616.         } else {
  617.            $caja = [];
  618.         }
  619.         return $caja;
  620.     }
  621. }