<?php $controller = 'POS';
    $action = 'print';
    include $_SERVER["DOCUMENT_ROOT"].'/app/include/bootstrap.php';?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link href="<?php echo ROOT_HOST?>/public/bootstrap/css/bootstrap.min.css" rel="stylesheet">     
</head><?php
#INFORMACION EMPRESA 
$empresa = new EmpresaEntity();
$empresa->setOptions($empresa->getById(1)); 

$nombreEmpresa  = $empresa->getNombre();
$direccion      = $empresa->getDireccion;
$ciudadEstadoZC = $empresa->getCiudad().', '.$empresa->getEstado().' '.$empresa->getCodigoPostal();
$telefono       = $empresa->getTelefono();  

#INFORMACION CIERRE DE CAJA
$entity = new AperturaCierreCajaEntity();
$cierreData = $entity->getById($_GET['idCierreCaja']);
$entity->setOptions($cierreData);
$data = $entity->getOptions();
$caja = $entity->getCajaName();
$cajero = $entity->getUsuarioName();
$fechaCierre = strftime('%d/%m/%y %H:%M:%S',strtotime($entity->getFechaCierre()));
$montoCierre = $entity->getMontoCierre();
$efectivoInicial = $entity->getEfectivoInicial();
$efectivoFinal = $entity->getEfectivoFinal();
$tarjetasFinal = $entity->getTarjetasFinal();
$transferenciasFinal = $entity->getElectronicoFinal();
$chequesFinal = $entity->getChequesFinal();

#FALTANTE O SOBRANTE$entity = new VentaEntity();
$ventaEntity = new VentaEntity();
$montoVentasTotal = $ventaEntity->getTotalVentasByIdApertura($_GET['idCierreCaja']);
$ventasPorFormaDePago = $ventaEntity->getDataVentaParaArqueo($_GET['idCierreCaja']);

$repoGeneric = new GenericRepository();
$totalRetiros = round($repoGeneric->getSumTotalRetiros($_GET['idCierreCaja']),2);
$montoTotalCierre = $entity->getMontoCierre()  + $totalRetiros;


$donacion = 0;
$ventasEfectivo = 0;
$arrayVentasPorFormaDePago = array();
if($ventasPorFormaDePago){
    foreach($ventasPorFormaDePago as $row){
        if($row['formaPagoId'] == 9){$donacion = round($row['monto'],2);}
        if($row['formaPagoId'] == 3){$ventasEfectivo = round($row['monto'],2);}
        
        $arrayVentasPorFormaDePago[$row['formaPagoId']] = $row;
    }
}

$formaMetodoPagoRepo = New FormaMetodoPagoRepository();
$formasPago = $formaMetodoPagoRepo->getListFormasPagoParaCierre();

foreach($formasPago as $key => $name){
    if(!key_exists($key, $arrayVentasPorFormaDePago)){
        $arrayVentasPorFormaDePago[] = array('formaPago'=>$name,'monto'=>0);
    }
}

$totalQueDeberiaHaberEnEfectivo = abs((float)$ventasEfectivo -(float)$totalRetiros);
$faltanteOSobrante = (float)$totalQueDeberiaHaberEnEfectivo - (float)$efectivoFinal;
if($faltanteOSobrante > 0){
    $titleDiferencia = 'Faltante';
}elseif($faltanteOSobrante < 0){
    $titleDiferencia = 'Sobrante';
}else{
    $titleDiferencia = 'Balance';
}

#Cncelaciones
$cancelaciones = $ventaEntity->getVentasCanceladasByIdApertura($_GET['idCierreCaja']);
?>
<div id="printArea" style="margin:auto;font-size: 12px">
    <div style="margin: 30px;margin-bottom: 10px">
            <img src="../../public/img/logo_login.png" width="50%">
    </div>  
    <div style="text-align: center">
        <h4><?php echo $nombreEmpresa;?></h4>
        <p class="m-a-0"><?php echo $direccion; ?></p>
        <p class="m-a-0"><?php echo $ciudadEstadoZC; ?></p>
        <p class="m-a-0"><?php echo $telefono;?></p>
    </div> 
    <div class="text-center m-t-1">    
        <p class="m-a-0">Caja #: <?php echo $caja?></p>
        <p class="m-a-0">Fecha: <?php echo $fechaCierre;?></p>
        <p class="m-a-0">Hecho por: <?php echo $cajero;?></p>
    </div> 
    <div class="m-t-1">    
        <h4 class="m-a-0 text-center w-100">Cierre de caja</h4>
        <style>.table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th{padding:2px}</style>
        <table class="m-t-1 table table-condensed tableTicket" style="width:60%;margin:auto">
        <tbody>
            <tr style="border:0px;">
               <td class="text-left" style="border:0px;">Caja inicial</td>
               <td class="text-right" style="border:0px;"><?php echo number_format(round($efectivoInicial,2,PHP_ROUND_HALF_UP),2) ?></td>  
            </tr>
            <tr style="border:0px;"><td style="height: 15px;border:0px;"></td><td style="border:0px;"></tr>
            <tr style="border:0px;">
                <td class="text-left" style="border:0px;">Ventas efectivo</td>
                <td class="text-right" style="border:0px;"><?php echo number_format(round($ventasEfectivo,2,PHP_ROUND_HALF_UP),2) ?></td>
            </tr>            
            <tr style="border:0px;padding-top: 20px">
                <td class="text-left" style="border:0px;">Retiros</td>
                <td class="text-right" style="border:0px;">- <?php echo number_format(round($totalRetiros,2,PHP_ROUND_HALF_UP),2) ?></td>
            </tr>
            <tr style="border:0px;">
                <td class="text-left" style="border:0px;">Total</td>
                <td class="text-right" style="border:0px;"><?php echo number_format(round($totalQueDeberiaHaberEnEfectivo,2,PHP_ROUND_HALF_UP),2) ?></td>
            </tr>
            <tr style="border:0px;">
                <td class="text-left" style="border:0px;">Efectivo final</td>
                <td class="text-right" style="border:0px;"><?php echo number_format(round($efectivoFinal,2,PHP_ROUND_HALF_UP),2) ?></td>
            </tr>
            <tr style="border:0px;border-top: 1px solid #000">
                <td class="text-left" style="border:0px;"><?php echo $titleDiferencia ?></td>
                <td class="text-right" style="border:0px;"><?php echo number_format(abs($faltanteOSobrante),2); ?></td>
            </tr>
        </tbody>
        </table>
         <table class="m-t-1 table table-condensed tableTicket" style="width:60%;margin:auto;margin-top: 20px">
            <tbody>
            <tr style="border:0px;">
                <td class="text-left" style="border:0px;">Ventas</td>
                <td class="text-right" style="border:0px;"></td>
            </tr>
            <?php
            if($arrayVentasPorFormaDePago){
                foreach($arrayVentasPorFormaDePago as $row){?>
                    <tr style='border:0px;'>
                    <td style='border:0px;'><?php echo $row['formaPago'];?></td>
                    <td class='text-right' style='border:0px;'><?php echo number_format($row['monto'],2)?></td>  
                    </tr><?php
                }
            }?>
            <tr style="border:0px;">
                <td class="text-left" style="border:0px;border-top: 1px solid #000;">Total</td>
                <td class="text-right" style="border:0px;border-top: 1px solid #000;"><?php echo number_format(round($montoVentasTotal,2,PHP_ROUND_HALF_UP),2) ?></td>
            </tr><?php
            if($cancelaciones){?>
                <tr style="border:0px;padding-top: 50px">
                <td colspan="2" style="border:0px;border-bottom: 1px solid #000;padding-top: 10px;text-align: center">Cancelaciones</td>
                </tr>
                <tr>
                    <td style="border:0px;border-bottom: 1px solid #000;text-align: center">Venta</td>
                    <td style="border:0px;border-bottom: 1px solid #000;text-align: right">Total</td>
                </tr>
                    <?php            
                    foreach($cancelaciones as $cancelacion){?>
                        <tr style="border:0px;">
                            <td style="border:0px;text-align: center"><?php echo $cancelacion['num_venta']; ?></td>
                            <td style="border:0px;text-align: right"><?php echo $cancelacion['total_venta']; ?></td>
                        </tr>
                    <?php                 
                    }
                }else{?>
                        <tr style="border:0px;padding-top: 50px">
                            <td colspan="2" style="border:0px;border-bottom: 1px solid #000;padding-top: 10px;">Cancelaciones: 0</td>
                        </tr>
                <?php
            }?>                    
        </tbody>
        </table>
		<table style="width:60%;margin:auto;margin-top: 40px">
            <tr>
                <td colspan="2" style="border:0px;border-bottom: 1px solid #000;text-align: left">Merma</td>                
            </tr>
            <tr>
                <td style="border:0px;text-align: left">Merma Lbs</td>
                <td style="border:0px;text-align: right"><?php echo number_format((float)$cierreData['waste_lbs']) ?></td>
            </tr>
            <tr>
                <td style="border:0px;text-align: left">Mermas pza</td>
                <td style="border:0px;text-align: right"><?php echo number_format((float)$cierreData['waste_pza']) ?></td>
            </tr>
        </table>
    </div>     
</div>
    <script type="text/javascript">
        self.print();
    </script>
    <?php //if(isset($_GET['back'])){echo "<script>setTimeout(document.location = '{$_GET['back']}',500);</script>";}?>
</body>
</html>