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.     public function descargaReciboCajaPdf($id) {
  150.         $caja $this->getById($id);
  151.         $caja['caja_monedas'] = [];
  152.         $monedas $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  153.         foreach ($monedas as $moneda) {
  154.             array_push($caja['caja_monedas'], $moneda);
  155.         }
  156.         $numero $this->numeroAddZero($caja['id']);        
  157.         $fecha date("d/m/Y H:m"strtotime($caja['fecha_actualizacion']));
  158.         $usuarioClose $caja['usuario_close']['persona']['nombre'] ." "$caja['usuario_close']['persona']['apellido'];
  159.         // Obtengo losmovimientos de la caja
  160.         $movimientos $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McMovimientoHandler")->getAllMovimientosByCaja($id);
  161.         // Calculos de los monto por moneda
  162.         $totales = [];
  163.         foreach ($monedas as $moneda) {
  164.             $nombreMoneda $moneda['moneda']['nombre'];
  165.             $montoTotalEfectivo 0;
  166.             $montoTotalTransferencia 0;
  167.             $montoTotalPorConvenio 0;
  168.             $montoTotalCuentaCorriente 0;
  169.             if (COUNT($movimientos) > 0) {
  170.                 foreach ($movimientos as $movimiento) {
  171.                     if ((int)$moneda['moneda']['id'] == (int)$movimiento['moneda']['id']) {
  172.                         if (strtolower($movimiento['modalidad']) == 'contado/efectivo') {
  173.                             if ($movimiento['tipo'] == 'Ingreso') {
  174.                                 $montoTotalEfectivo $montoTotalEfectivo $movimiento['monto'];
  175.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  176.                                 $montoTotalEfectivo $montoTotalEfectivo $movimiento['monto'];
  177.                             }
  178.                         }
  179.                         if (strtolower($movimiento['modalidad']) == 'transferencia') {
  180.                             if ($movimiento['tipo'] == 'Ingreso') {
  181.                                 $montoTotalTransferencia $montoTotalTransferencia $movimiento['monto'];
  182.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  183.                                 $montoTotalTransferencia $montoTotalTransferencia $movimiento['monto'];
  184.                             }
  185.                         }
  186.                         if (strtolower($movimiento['modalidad']) == 'pago por convenio') {
  187.                             if ($movimiento['tipo'] == 'Ingreso') {
  188.                                 $montoTotalPorConvenio $montoTotalPorConvenio $movimiento['monto'];
  189.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  190.                                 $montoTotalPorConvenio $montoTotalPorConvenio $movimiento['monto'];
  191.                             }
  192.                         }
  193.                         if (strtolower($movimiento['modalidad']) == 'cuenta corriente') {
  194.                             if ($movimiento['tipo'] == 'Ingreso') {
  195.                                 $montoTotalCuentaCorriente $montoTotalCuentaCorriente $movimiento['monto'];
  196.                             } else if ($movimiento['tipo'] == 'Egreso' || $movimiento['tipo'] == 'Anulacion') {
  197.                                 $montoTotalCuentaCorriente $montoTotalCuentaCorriente $movimiento['monto'];
  198.                             }
  199.                         }
  200.                     } 
  201.                 }
  202.             }
  203.             $totales[] = array(
  204.                 'nombreMoneda' => $nombreMoneda,
  205.                 'montoTotalEfectivo' => $montoTotalEfectivo,
  206.                 'montoTotalTransferencia' => $montoTotalTransferencia,
  207.                 'montoTotalPorConvenio' => $montoTotalPorConvenio,
  208.                 'montoTotalCuentaCorriente' => $montoTotalCuentaCorriente
  209.             );
  210.         }
  211.         
  212.         // create new PDF document
  213.         $pdf = new PDF();       
  214.         $pdf->SetTitle("Comprobante Caja Cerrada");
  215.         $pdf->SetSubject("Comprobante Caja Cerrada");
  216.         $pdf->setFontSubsetting(true);
  217.         // set default header data
  218.         $pdf->SetHeaderData(PDF_HEADER_LOGOPDF_HEADER_LOGO_WIDTHPDF_HEADER_TITLEPDF_HEADER_STRING);
  219.         $pdf->SetFont('helvetica'''11''true);
  220.         $pdf->SetMargins(PDF_MARGIN_LEFTPDF_MARGIN_TOPPDF_MARGIN_RIGHT);
  221.         $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  222.         $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  223.         $pdf->setPrintFooter(false);
  224.         // set auto page breaks
  225.         $pdf->SetAutoPageBreak(TRUEPDF_MARGIN_BOTTOM);
  226.         // set image scale factor
  227.         $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  228.         $pdf->AddPage('P''A4');        
  229.         $html = <<<EOD
  230.         <h1> Comprobante - Cierre de Caja</h1>
  231.         <table table border="0.5">
  232.             <tr>
  233.                 <th style="background-color: grey;"> Caja Nº</th>
  234.                 <td><b> {$numero}</b></td>
  235.             </tr>
  236.             <tr>
  237.                 <th style="background-color: grey;"> Tipo</th>
  238.                 <td><b> {$caja['tipo']}</b></td>
  239.             </tr>
  240.             <tr>
  241.                 <th style="background-color: grey;"> Fecha</th>
  242.                 <td><b> {$fecha}</b></td>
  243.             </tr>
  244.             <tr>
  245.                 <th style="background-color: grey;"> Usuario responsable</th>
  246.                 <td><b> {$usuarioClose}</b></td>
  247.             </tr>
  248.         </table>
  249.         <br/>
  250.         <br/>
  251.         <br/>
  252.         <table border="0.5">
  253.             <tr style="background-color: grey;">
  254.                 <th> Fecha</th>
  255.                 <th> Monto</th>
  256.                 <th> Moneda</th>
  257.                 <th> Modalidad</th>
  258.                 <th> Transacción</th>
  259.             </tr>
  260.         EOD;
  261.         if (COUNT($movimiento) > 0) {
  262.             foreach ($movimientos as $movimiento) {
  263.                 $fecha date("d/m/Y H:m"strtotime($movimiento['fecha_creacion']));
  264.                 $html .= <<<EOD
  265.                     <tr>
  266.                         <td> {$fecha}</td>
  267.                         <td> $ {$movimiento['monto']}</td>
  268.                         <td> {$movimiento['moneda']['nombre']}</td>
  269.                         <td> {$movimiento['modalidad']}</td>
  270.                         <td> {$movimiento['tipo']}</td>
  271.                     </tr>
  272.                 EOD;
  273.             }
  274.         }
  275.         $html .= <<<EOD
  276.             </table>
  277.         <br/>
  278.         <br/>
  279.         <br/>
  280.         <table border="0.5">
  281.             <tr style="background-color: grey;">
  282.                 <th> Moneda</th>
  283.                 <th> Total Efectivo/Contado</th>
  284.                 <th> Total Transferencia</th>
  285.                 <th> Total Por Convenio</th>
  286.                 <th> Total Cuenta Corriente</th>
  287.             </tr>
  288.         EOD;
  289.         if (COUNT($totales) > 0) {
  290.             foreach ($totales as $total) {
  291.                 $fecha date("d/m/Y H:m"strtotime($movimiento['fecha_creacion']));
  292.                 $html .= <<<EOD
  293.                     <tr>
  294.                         <td> {$total['nombreMoneda']}</td>
  295.                         <td> $ {$total['montoTotalEfectivo']}</td>
  296.                         <td> $ {$total['montoTotalTransferencia']}</td>
  297.                         <td> $ {$total['montoTotalPorConvenio']}</td>
  298.                         <td> $ {$total['montoTotalCuentaCorriente']}</td>
  299.                     </tr>
  300.                 EOD;
  301.             }
  302.         }
  303.         $html .= <<<EOD
  304.             </table>
  305.         <br/>
  306.         <br/>
  307.         <br/>
  308.         <table border="0.5">
  309.             <tr style="background-color: grey;">
  310.                 <th> Moneda</th>
  311.                 <th> Monto inicial</th>
  312.                 <th> Monto final</th>
  313.             </tr>
  314.         EOD;
  315.         foreach ($caja['caja_monedas'] as $moneda) {
  316.             $html .= <<<EOD
  317.                 <tr>
  318.                     <td> {$moneda['moneda']['nombre']}</td>
  319.                     <td> $ {$moneda['monto_inicial']}</td>
  320.                     <td> $ {$moneda['monto_final']}</td>
  321.                 </tr>
  322.             EOD;
  323.         }
  324.         $html .= <<<EOD
  325.             </table>
  326.         EOD;
  327.         $pdf->writeHTML($htmltruefalsefalsefalse'');
  328.         return new Response($pdf->Output("Comprobante""S"), 200, array(
  329.             'Content-Type' => 'application/pdf',
  330.             'Content-Disposition' => 'attachment; filename="Caja-Comprobante.pdf"'
  331.                 )
  332.         );
  333.     
  334.     }
  335.     public function saveOperacionCaja($caja$tipo$monto$modalidad$monedaId) {
  336.         if ($modalidad == 'Contado/Efectivo') {
  337.             $cajaMoneda $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), $monedaId);
  338.             if ($tipo == 'Egreso') {
  339.                 if ($cajaMoneda->getMontoFinal() > 0) {
  340.                     if ($cajaMoneda->getMontoFinal() >= $monto) {
  341.                         $montoFinal $cajaMoneda->getMontoFinal();
  342.                         $cajaMoneda->setMontoFinal($montoFinal $monto);
  343.                     } else {
  344.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  345.                     }
  346.                 } else {
  347.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  348.                 }
  349.             } else if($tipo == 'Ingreso' || $tipo == 'Ingreso-Devolución') {
  350.                 $montoFinal $cajaMoneda->getMontoFinal();
  351.                 $cajaMoneda->setMontoFinal($montoFinal $monto);
  352.             }
  353.             $cajaMoneda->setFechaActualizacion(new DateTime());
  354.             return $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->save($cajaMoneda);
  355.         }
  356.     }
  357.     public function saveOperacionDivisasCaja($caja$tipo$tipoDetalle$monto$modalidad$monedaIdfloat $cotizacion) {
  358.         if ($modalidad == 'Contado/efectivo') {
  359.             $cajaMonedaNacional $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), 1);
  360.             $cajaMonedaExtranjero $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getByCajaAndMoneda($caja->getId(), $monedaId);
  361.             //Si se compra moneda extranjera, validamos si tenemos los pesos argentinos para poder comprar.
  362.             if ($tipoDetalle == 'Compra') {
  363.                 if ($cajaMonedaNacional->getMontoFinal() > 0) {
  364.                     if ($cajaMonedaNacional->getMontoFinal() >= $cotizacion) {
  365.                         //Egreso de la caja nacional
  366.                         $montoFinalNacional $cajaMonedaNacional->getMontoFinal();
  367.                         $cajaMonedaNacional->setMontoFinal($montoFinalNacional $cotizacion);
  368.                         //Ingreso de la caja extranjera
  369.                         $montoFinalExtranjero $cajaMonedaExtranjero->getMontoFinal();
  370.                         $cajaMonedaExtranjero->setMontoFinal($montoFinalExtranjero $monto);
  371.                     } else {
  372.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  373.                     }
  374.                 } else {
  375.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  376.                 }
  377.             } else {
  378.                 //Si se vende moneda extranjera, validamos si tenemos la cantidad a vender.
  379.                 if ($cajaMonedaExtranjero->getMontoFinal() > 0) {
  380.                     if ($cajaMonedaExtranjero->getMontoFinal() >= $monto) {
  381.                         //Egreso de la caja extranjera
  382.                         $montoFinalExtranjero $cajaMonedaExtranjero->getMontoFinal();
  383.                         $cajaMonedaExtranjero->setMontoFinal($montoFinalExtranjero $monto);
  384.                         //Ingreso de la caja nacional
  385.                         $montoFinalNacional $cajaMonedaNacional->getMontoFinal();
  386.                         $cajaMonedaNacional->setMontoFinal($montoFinalNacional $cotizacion);
  387.                     } else {
  388.                         throw new HttpException(409"El monto de egreso es mayor al saldo de la caja.");
  389.                     }
  390.                 } else {
  391.                     throw new HttpException(409"La caja no cuenta con el saldo suficiente.");
  392.                 }
  393.             }
  394.         }
  395.     }
  396.     public function getCajaAbierta($tipo) {
  397.         $caja $this->repository->getCajaAbierta($tipo);
  398.         if (!is_null($caja)) {
  399.             $caja =  $this->toarray($caja'caja');
  400.             $caja['caja_monedas'] = [];
  401.             $monedas $this->container->get("Noahtech\Sistemas\InterjamaBundle\Handler\McCajaMonedaHandler")->getAllMonedasByCaja((int)$caja['id']);
  402.             foreach ($monedas as $moneda) {
  403.                 array_push($caja['caja_monedas'], $moneda);
  404.             }
  405.         } else {
  406.            $caja = [];
  407.         }
  408.         return $caja;
  409.     }
  410. }