<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 

$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 10; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM fac_periodica where fac_estado='Activo' and fac_numero like '%$q%' and empresa='$empresa'");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './V_facturacion_periodica.php';
    $sql="SELECT * FROM fac_periodica where fac_estado='Activo' and empresa='$empresa' and fac_numero like '%$q%' order by fac_id desc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<div class="table-responsive">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th width="8%">N°</th>
                <th width="15%">FECHA DE INICIO</th>
                <th>CLIENTE</th>
                <th width="10%">TOTAL</th>
                <th width="5%">ESTADO</th>
                <th width="15%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php
            while ($reg=mysqli_fetch_array($query)){
            $id=$reg['fac_id'];
            $fac_id=$reg['fac_id'];
            $clie_id=$reg['Cliente_clie_id'];
            $can6=mysqli_query($con,"SELECT * FROM cliente where clie_id='$clie_id' and empresa='$empresa'") or die(mysqli_error($con));  
            $dato6=mysqli_fetch_array($can6);
            $clie_nombre=$dato6['clie_nombre'];

            $sumador_total=0;
            $sumador_combo=0;

            $neto=0;
            $total_descuento=0;
            $total_iva=0;
            $total_retencion=0;
            $sumador_articulos=0;
            $can1=mysqli_query($con,"SELECT * FROM fac_detalle where fac_periodica_fac_id='$fac_id' ") or die(mysqli_error($con));  
            while($dato1=mysqli_fetch_array($can1))
            {
                $facd_referencia=$dato1['facd_referencia'];            
                $facd_nombre=$dato1['facd_nombre']; 
                $vend_cantidad=$dato1['facd_cantidad'];
                $vend_iva=$dato1['facd_iva'];            
                $vend_descuento=$dato1['facd_desc']; 
                $vend_precio=$dato1['facd_precio'];

                $iva0=$vend_iva/100; //0.19
                $iva1=1+$iva0; //1.19

                $precio_total_item=$vend_precio*$vend_cantidad;//precio normal 1.000.000
                $precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
                $descuento_item=($vend_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
                $total_iva_item=$precio_total_sin_iva_item*$iva0; // total iva  159.336

                $neto+=$precio_total_sin_iva_item;
                $total_descuento+=$descuento_item;
                $total_iva+=$total_iva_item;
                $sumador_articulos=($neto+$total_iva)-$total_descuento;
            }
            $sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;
          ?>
            <tr>
                <td><?php  echo $reg['fac_numero']; ?></td>
                <td><?php  echo $reg['fac_inicio_contrato']; ?></td>
                <td><?php  echo $clie_nombre; ?></td>
                <td><p class="pull-right"><?php  echo number_format($sumador_total,0); ?></p></td>
                <td>
                    <?php
                    if($reg['fac_estado']=='Activo'){ ?>
                        <span class="badgeazul" title="Pagada"><i class="btn-icon-only icon-ok"></i>Activo</span>
                    <?php }
                    else
                    { ?>
                        <span class="badgerojo" title="Pagada"><i class="btn-icon-only icon-ok"></i>Inactivo</span>
                    <?php } ?>
                </td>
                <td >
                    <a href="#verdetalle<?php echo $reg['fac_id']; ?>" title="Ver Detalle" class="btn btn-xs btn-info" data-toggle="modal" > 
                    <i class="btn-icon-only icon-eye-open"></i></a>  
                      <?php include ('../php_facturacion_periodica/forms_detalle.php'); ?>

                    <?php if($reg['fac_estado']=='Activo') { ?>
                        <a href="V_facturacion_periodica_modificar.php?fac_id=<?php echo $reg['fac_id']; ?>" class='btn btn-xs btn-success' title='Editar'><i class="btn-icon-only icon-edit"></i></a>

                       
                        <a type="button" class="btn btn-xs  btn-danger e btn-anular" title="anular" data-id="<?php echo $id;?>"><i class="btn-icon-only icon-remove"></i> </a>

                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
            <tr>
                <td colspan=8>
                <span class="pull-right">
                 <?php  echo paginate($reload, $page, $total_pages, $adjacents); ?>
                </span></td>
            </tr>
    </table>
</div>
<?php } } ?>

<script>
    $(".btn-anular").click(function(){
        id = $(this).data("id");
        $.get("./php_facturacion_periodica/form_anular.php","id="+id,function(data){
            $("#form-anular").html(data);
        });
        $('#anular').modal('show');
    });
</script>

<script>
    $(".btn-desactivar").click(function(){
        id = $(this).data("id");
        $.get("./php_facturacion_periodica/forms_desactivar.php","id="+id,function(data){
            $("#form-desactivar").html(data);
        });
        $('#desactivar').modal('show');
    });
</script>