<?php
require '../../conexion.php';
require '../../session.php';
$conexion = conexion::con();
$usu_cod = $_SESSION['usu_cod'];
$cod_suc = $_SESSION['cod_suc'];
$apertura = pg_fetch_all(pg_query($conexion,"select * from v_apertura where usu_cod = $usu_cod and cod_suc = $cod_suc and cierre_fecha is null;"));
?>
<?php if(empty($apertura)){ ?>
<label class="text-danger"><i class="fa fa-exclamation-circle"></i> No se encuentra una apertura de caja...</label>
<?php }else{ ?>
<div class="row">
    <?php //echo var_dump($apertura); ?>
    <div class="panel panel-primary">
        <div class="panel-heading">
            Datos de la Apertura
        </div>
        <div class="panel-body">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>N°</th>
                        <th>Funcionario</th>
                        <th>Fecha Apertura</th>
                        <th>Monto Apertura</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><?= $apertura[0]['id_apecie'];?><input type="hidden" id="vapertura" value="<?= $apertura[0]['id_apecie'];?>"></td>
                        <td><?= $apertura[0]['empleado'];?></td>
                        <td><?= $apertura[0]['fecha_apertura'];?></td>
                        <td><?= $apertura[0]['apertura_monto'];?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="row">
    <div class="form-group">
        <input type="hidden" id="btn-panel-agregar" data-target="#panel-agregar" data-toggle="modal">
        <button class="btn btn-success" onclick="panel_agregar(0);"><i class="fa fa-plus-circle"></i> Agregar Cobro</button>
    </div>
    <div class="panel panel-primary">
        <div class="panel-heading">
            Lista de Cobros
        </div>
        <div class="panel-body">
            <?php $cobros = pg_fetch_all(pg_query($conexion, "SELECT * FROM v_cobros WHERE id_apecie = ".$apertura[0]['id_apecie']." ORDER BY fecha DESC, hora DESC"));?>
            <?php if(empty($cobros)){ ?>
            <label class="text-danger"><i class="fa fa-exclamation-circle"></i> No se han registrado cobros...</label>
            <?php }else{ ?>
            <table class="table table-striped table-bordered table-hover facturas">
                <thead>
                    <tr>
                        <th>Fecha</th>
                        <th>Tipo de Documento</th>
                        <th>N° Documento</th>
                        <th>RUC de</th>
                        <th>A Nombre de</th>
                        <th>Estado</th>
                        <th>Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($cobros as $c){ ?>
                    <tr>
                        <td><?= date("d/m/Y", strtotime($c['fecha']))." ".$c['hora'] ;?></td>
                        <td><?= $c['documento'];?></td>
                        <td><?= $c['nro_documento'];?></td>
                        <td><?= $c['ruc_de'];?></td>
                        <td><?= $c['a_nombre_de'];?></td>
                        <td><?= $c['estado'];?></td>
                        <td>
                            <div class="btn-group">
                                <button onclick="panel_agregar(<?= $c['id_cobro'];?>);" class="btn btn-sm btn-warning" style="color:white"><i class="fa fa-list-alt"></i></button>
                                <?php if($c['estado'] == 'PAGADO'){ ?>
                                 <?php if($c['documento'] != 'RECIBO'){ ?>
                                    <a href="/farmmedi/factura/cobro/imprimir.php/?id_cobro=<?= $c['id_cobro'];?>" target="_blank" class="btn btn-sm  btn-primary"><i class="fa fa-print"></i></a>
                                 <?php } ?>
                                <?php } ?>
                                <?php if($c['estado'] != 'ANULADO'){ ?>
                                <a onclick="anular(<?php echo "'" . $c['id_cobro']."_".$c['nro_documento']. "'"; ?>)"
                                   data-toggle="modal" data-target="#delete"
                                   class="btn btn-sm btn-danger" rel='tooltip' 
                                   data-title="Anular"> <i class="fa fa-remove"></i>
                                </a>                                
                                <?php } ?>
                            </div>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
            <?php } ?>
        </div>
    </div>
</div>
<?php } ?>
<script>
/*=============================================
	=            DataTable            =
	=============================================*/

	$(".facturas").DataTable({
                    "lengthMenu": [[10, 50, 100, -1], [10, 50, 100, 'Todos']],
                    "columnDefs": [ {
                      "targets"  : 'no-sort',
                      "orderable": false,
                      "order": [0, 'asc']
                    }],                    
                    /*"stateSave": true,*/
		"language": {
			"sProcessing":     "Procesando...",
			"sLengthMenu":     "Mostrar _MENU_ registros",
			"sZeroRecords":    "No se encontraron resultados",
			"sEmptyTable":     "Ningún dato disponible en esta tabla",
			"sInfo":           "Mostrando registros del _START_ al _END_ de un total de _TOTAL_",
			"sInfoEmpty":      "Mostrando registros del 0 al 0 de un total de 0",
			"sInfoFiltered":   "(filtrado de un total de _MAX_ registros)",
			"sInfoPostFix":    "",
			"sSearch":         "Buscar:",
			"sUrl":            "",
			"sInfoThousands":  ",",
			"sLoadingRecords": "Cargando...",
			"oPaginate": {
				"sFirst":    "Primero",
				"sLast":     "Último",
				"sNext":     "Siguiente",
				"sPrevious": "Anterior"
			},
			"oAria": {
				"sSortAscending":  ": Activar para ordenar la columna de manera ascendente",
				"sSortDescending": ": Activar para ordenar la columna de manera descendente"
			}
		}
	});    
</script>