<?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)));
    $ser = mysqli_real_escape_string($con,(strip_tags($_REQUEST['ser'], ENT_QUOTES)));
    $buscar = mb_strtoupper($q);

    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20; //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 ventas  where  empresa='$empresa' and sucursal='$sucursal' and  (ven_n_venta like '%$q%' OR ven_nombre like '%$q%' OR ven_cc like '%$q%' OR ven_guia like '%$q%') ");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './P_pos.php';
    if ($ser<>null) {
        
        $sql="SELECT * , DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM seriales s, ventas v where v.empresa='$empresa' and s.ser_codigo like '%$ser%' and v.ven_id=s.venta_detalle_ventas_ven_id  order by v.ven_id desc LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
    }
    else
    {
        $sql="SELECT * , DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM ventas where empresa='$empresa' and sucursal='$sucursal'  and (ven_n_venta like '%$q%' OR ven_nombre like '%$q%' OR ven_cc like '%$q%' OR ven_guia like '%$q%') order by ven_id desc LIMIT $offset,$per_page";
        $query = mysqli_query($con, $sql);
    }
    if ($numrows>0){
?>
<table class="table table-bordered table-hover table-striped" style="font-size: 9pt;">
    <thead>
        <tr>
            <td width="1%"></td>
            <th>N°</th>
            <th width="10%">FECHA</th>
            <th>CLIENTE</th>
            <th width="9%">F.DE PAGO</th>
            <th>TOTAL</th>
            <th>ABONOS</th>
            <th>DEVOLUCION</th>
            <th>SALDO</th>
            <th width="15%"></th>
        </tr>
    </thead>
    <tbody class="buscar">
    <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $id=$reg['ven_id'];
                $ven_id=$reg['ven_id'];
                $apertura=$reg['apertura'];
                $ven_n_venta=$reg['ven_n_venta'];
                $ven_fecha=$reg['ven_fecha'];
                $ven_hora=$reg['ven_hora'];
                $clie_nombre=$reg['ven_nombre'];
                $Cliente_clie_id=$reg['Cliente_clie_id'];
                $ven_estado=$reg['ven_estado'];
                $ven_forma_de_pago=$reg['ven_forma_de_pago'];


                $clie=mysqli_query($con,"SELECT * FROM cliente where clie_id='$Cliente_clie_id'") or die(mysqli_error($con)); 
                $cli=mysqli_fetch_array($clie);
                $facturador_cli=$cli['facturador'];

                include '../php_pos/saldodeventas.php';
                ?>
        <tr>
            <td><?php echo $estados;?></td>
            <td><a href="P_pos_consulta.php?ven_id=<?php echo $reg['ven_id']; ?>" style="text-decoration:none;color: #000"><?php echo $reg['ven_prefijo']; ?><?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $ven_n_venta )); ?></a></td>
            <td><?php echo $ven_fecha;?> - <?php echo $ven_hora; ?></td>
            <td>
                <?php  if($facturador_cli=='si')
                {
                    echo '<i class="btn-icon-only fa fa-desktop alert-danger" aria-hidden="true" title="facturador electronico"></i>';
                } else 
                {
                    echo '<i class="btn-icon-only alert-danger" aria-hidden="true">&nbsp;&nbsp;&nbsp;&nbsp;</i>';
                }
                ?>
                <?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $clie_nombre )); ?>
            </td>
            <td><?php echo $ven_forma_de_pago; ?> </td>
            <td><p class="pull-right"><?php echo number_format($sumador_total,0); ?></p></td>
            <td><p class="pull-right"><?php echo number_format($total_abono,0);  ?></p> </td>
            <td><p class="pull-right"><?php echo number_format($devo_total,0); ?></p></td>
            <td><p class="pull-right"><?php echo number_format($saldo,0); ?></p></td>
            <td>
                <div class="btn-group pull-right">
                    <?php
                    if($reg['ven_estado']<>"anulada") { ?>
                    <?php  
                        if($reg['estado_dian']<>"Aprobada" && $reg['ven_estado']<>"anulada" ) { ?>
                        <a data-id="<?php echo $reg['ven_id'];?>" title="Editar" class="btn btn-xs btn-default btn-editar" data-toggle="modal"><i class="btn-icon-only icon-edit"></i></a> 
                    <?php  } ?>
                    <?php 
                        if($facturador=='si') {
                            if($reg['ven_tipo']<>"electronica") { ?>
                                <a href="#" class="btn btn-xs btn-danger btn-dian" title="Enviar Dian" data-id="<?php echo $reg['ven_id'];?>"><i class="btn-icon-only fa fa-upload"></i></a>
                    <?php } } ?>
                    <?php if($negocio_neg_id=='1') { ?>
                        <a href="V_ventas_serial.php?ven_id=<?php echo $reg['ven_id']; ?>" class="btn btn-ser btn-xs btn-info" title="Serial"> <i class="btn-icon-only icon-barcode"></i></a>
                    <?php } ?>
                    <?php include ('../include/boton_venta2.php'); ?>
                    <?php } ?>
                    <a href="#" data-id="<?php echo $id;?>" title="Ver Detalle" class="btn btn-xs btn-info btn_detalle" data-toggle="modal" > 
                    <i class="btn-icon-only icon-eye-open"></i></a>
                </div> 
            </td>
        </tr>
            <?php }?>
        <tr>
            <td colspan=10><span class="pull-right">
                <?php echo paginate($reload, $page, $total_pages, $adjacents);?></span>
            </td>
        </tr>
    </table>
<?php } } ?>

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


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

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

<script>
    $(".btn_detalle").click(function(){
        id = $(this).data("id");
        $.get("./php_pos/forms_detalle.php","id="+id,function(data){
            $("#form-detalle").html(data);
        });
        $('#detalle').modal('show');
    });
</script>


  
 <div class="modal fade" id="correo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Correo </h4>
            </div>
            <div class="modal-body">
                <div id="form-correo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


