<?php
session_start();
require_once('conexion.php');
require_once('include/sesiones.php');
if (isset($_SESSION['usu_nombre']))
{
    $regis=mysqli_query($con,"SELECT * from permiso where modulo_mod_id='25' and usuario_usu_id='$usu_id'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
    if($reg['per_visualizar']=="Activo")
        {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include ('head.php'); ?>
</head>
<body>
<?php
$inicio="";
$M_inventario="";
$M_contactos="";
$M_compras="";
$M_ventas="";
$M_pedidos="";
$M_pos="";
$R_reportes="active";

$ventas="active";
$caja="";
$compra="";
$contactos="";
$inventario="";
$utilidad="";

include ('menu.php');
?> 
    
<div class="main">
    <div class="col-md-2">
        <?php include ('include/menu_reportes.php');?>
    </div>
    <div class="col-md-10">
        <div class="widget stacked">
            <div class="widget-content">
                <div class="widget stacked">
                    <div class="widget-content">
                        <?php include ('php_reportes/menu_ventas.php');?>  
                    </div>
                </div>   
                <div class="pull-right" style="padding-bottom:5px">
                    <?php 
                        $cliente=$_GET['cliente'];
                        $hasta=$_GET['hasta'];
                        $sucursal_form=$_GET['sucursal_form'];
                    ?>
                    <a href="libraries/pdf/pdf_reportes/ventas_cxc.php?cliente=<?php echo $cliente; ?>&hasta=<?php echo $hasta;?>&sucursal_form=<?php echo $sucursal_form;?>" target="_blank" class="btn btn-danger"><i class="fa-solid fa-file-pdf"></i> DESCARGAR PDF</a>
                    <a href="libraries/pdf/pdf_reportes/ventas_cxc_detallado.php?cliente=<?php echo $cliente; ?>&hasta=<?php echo $hasta;?>&sucursal_form=<?php echo $sucursal_form;?>" target="_blank" class="btn btn-danger"><i class="fa-solid fa-file-pdf"></i> DESCARGAR PDF DETALLADO</a>
                    <!--  <a href="libraries/php_excel/excel_ventas_cxc.php?cliente=<?php echo $_GET['cliente'];?>" target="_blank" class="btn btn-success" ><i class="fa-solid fa-file-excel"></i> DESCARGAR EXCEL</a>-->
                </div> 
              
                <div class="widget stacked widget-table action-table">
                    <div class="widget-header" >
                        <h3>VENTAS POR COBRAR </h3>  
                    </div> <!-- /widget-header -->      
                    <div class="widget-content">
                        <div class="table-responsive">

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <td style="width: 8%">N° FACTURA</td>
            <td style="width: 8%">FECHA</td>
            <td>CLIENTE</td>
            <td style="width: 10%">DIAS</td>
            <td style="width: 10%">TOTAL</td>
            <td style="width: 7%">ABONOS</td>
            <td style="width: 7%">DEVOLUCION</td>
            <td style="width: 7%">SALDO</td>
            <td style="width: 8%"></td>
      </tr>
    </thead>
<?php
if($cliente==0)
{
     $registros=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta' and empresa='$empresa' and ven_estado='sin pagar' ") or die(mysqli_error($con));
}
else
{
    $registros=mysqli_query($con,"SELECT * FROM ventas where ven_fecha<='$hasta' and Cliente_clie_id='$cliente' and empresa='$empresa' and ven_estado='sin pagar' ") or die(mysqli_error($con));
}


$total_cliente=0;
$total_abonos=0;
$total_saldo=0;
$total_devoluciones=0;
$sumador_saldo=0;   
$saldo_vencido=0;
while ($reg=mysqli_fetch_array($registros))
{  
    $id=$reg['ven_id'];
    $ven_id=$reg['ven_id'];
    $ven_fecha=$reg['ven_fecha'];
    $ven_dias=$reg['ven_dias'];

    $vence = strtotime($ven_fecha."+ ".$ven_dias." days");
    $vence_fecha=date("Y-m-d",$vence) . "\n";
    $fecha=date('Y-m-d ');

    $hasta=date("Y-m-d",$vence) . "\n";
    $hoy=$fecha;
    $dias = (strtotime($hasta)-strtotime($hoy))/86400;
    $dias = abs($dias);

    if ($hasta >= $hoy){ $dias = '-' .floor($dias);}
    else{ $dias = '+' .floor($dias); }

    $sumador_combo=0;
    $sumador_total=0;
    $neto=0;
    $total_descuento=0;
    $total_iva=0;
    $sumador_articulos=0;

    $vencido=0;
    $total_retencion=0;
    $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_venta where empresa='$empresa' and ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $total_retencion=$rets['ret_importe'];

    include('include/total_venta.php');

    $sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;
    $total_cliente+=$sumador_total;

    //abono
    $sql=mysqli_query($con, "SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id' ");
    $sql=mysqli_fetch_array($sql);
    $total_abono=$sql['total'];

    $devo_total=0;
    $sql1=mysqli_query($con, "SELECT * from devolucion where empresa='$empresa' and ventas_ven_id='$ven_id'");
    while ($row=mysqli_fetch_array($sql1))
    {
        $dev_precio=$row['dev_precio'];
        $devo_total+=$dev_precio; 
    }

    $saldo_t=$sumador_total-$total_abono-$devo_total;
    if ($saldo_t<=0) 
    {
        $saldo='0';
    }
    else
    {
        $saldo=$saldo_t;
    }

    $ven_fecha=$reg['ven_fecha'];
    $ven_dias=$reg['ven_dias'];

    $vence = strtotime($ven_fecha."+ ".$ven_dias." days");
    date("Y-m-d",$vence) . "\n";
    $fecha=date('Y-m-d ');

    $hasta=date("Y-m-d",$vence) . "\n";
    $hoy=$fecha;
    $dias = (strtotime($hasta)-strtotime($hoy))/86400;
    $dias = abs($dias);

    if($hasta >= $hoy)
    {
        $dias = '-' .floor($dias);
    }
    else
    {
        $dias = '+' .floor($dias);
        $vencido+=$saldo;
    }
    $saldo=($sumador_total-$total_abono)-$devo_total;
    $total_abonos+=$total_abono;
    $total_saldo+=$saldo;
?>

    <tr>
        <td><?php echo $reg['ven_n_venta']; ?></td>
        <td><?php echo $reg['ven_fecha']; ?></td>
        <td><?php echo $reg['ven_nombre']; ?></td>
        <td><?php echo $dias; ?></td>
        <td style="text-align: right;"><?php echo number_format($sumador_total,0);?></td>
        <td style="text-align: right;"><?php echo number_format($total_abono,0); ?></td>
        <td style="text-align: right;"><?php echo number_format($devo_total,0); ?></td>
        <td style="text-align: right;"><?php echo number_format($saldo-$devo_total,0);?></td>
        <td>
            <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>
            <?php include ('include/boton_venta2.php'); ?>
        </td>
    </tr>
 <?php   
    $sumador_saldo+=$saldo;
    $saldo_vencido+= $vencido;
} 
?> 
        <tr>
            <td colspan="3"></td>
            <td style="width: 8%"><strong>TOTAL</strong></td>
            <td style="width: 10%;text-align: right;"><strong><?php echo number_format($total_cliente,0);?></strong></td>
            <td style="width: 10%;text-align: right;"><strong><?php echo number_format($total_abonos,0);?></strong></td>
            <td style="width: 10%;text-align: right;"><strong><?php echo number_format($total_devoluciones,0);?></strong></td>
            <td style="width: 10%;text-align: right;"><strong><?php echo number_format($total_saldo,0);?></strong></td>
            <td style="width: 10%;text-align: right;"></td>
        </tr>
    </table>
                        </div>
                    </div> <!-- /widget-content -->
                </div> <!-- /widget -->
            </div>
        </div></br>
   </div> <!-- /span6 -->
</div> <!-- /main -->

<div class="modal fade" id="detalle" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <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">Detalle de Venta</h4>
            </div>
            <div class="modal-body">
                <div id="form-detalle"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<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>
<?php
    include ('php_caja/caja_abrir.php');
    include ('script.php');
?>
</body>
</html>
<?php
}
else
{
   echo '<script>alert("No tienes permiso para ver esta pagina")</script>';
   echo '<script> setTimeout("history.back(1)", 0); </script>';
}
}
else
{
    echo '<script>location.href = "index.php";</script>';
}
?>