<?php
session_start();
require_once('conexion.php');
require_once('include/sesiones.php');
if (isset($_SESSION['clie_id']))
{
  $cliente=$_SESSION['clie_id'];
?>
<!DOCTYPE html>
<html lang="en">
    <head>
	   <?php include ('head.php'); ?>
    </head>
<body>
<?php
include ('menu.php');
?> 
<div class="main">
    <div class="col-md-2"> 
        <?php include ('include/menu_ventas.php');?>
	</div> <!-- /span6 -->
    <div class="col-md-10">  
        <div class="widget stacked">
            <div class="widget-content">
                 <div class="panel panel-default">
                <div class="panel-body">
                   <div class="col-md-6"> <h1 class="panel-title pull-left" style="font-size:20px;padding-top: 0px;"><i class="fas fa-credit-card fa-2x" ></i> COMPRAS</h1>
                   </div>
                    <?php 
                    $totalpagar=0;
                    $registros=mysqli_query($con,"SELECT * FROM ventas where Cliente_clie_id='$cliente' and ven_estado='sin pagar' and empresa='$empresa'") or die(mysqli_error($con));
                    while ($reg=mysqli_fetch_array($registros))
                    {
                        $ven_id=$reg['ven_id'];
                        $ven_estado=$reg['ven_estado'];
                        $ven_forma_de_pago=$reg['ven_forma_de_pago'];
                        $Cliente_clie_id=$reg['Cliente_clie_id'];
                        $sumador_combo=0;
                        $sumador_total=0;

                        $neto=0;
                        $total_descuento=0;
                        $total_iva=0;
                        $total_retencion=0;
                        $sumador_articulos=0;


                        $ret=mysqli_query($con, "SELECT * FROM retencion_venta where ventas_ven_id='$ven_id'") or die(mysqli_error($con)); 
                        $rets=mysqli_fetch_array($ret);
                        if(isset($rets['porcentaje']))
                        {
                            $ret_porcentaje=$rets['porcentaje'];
                        }
                        else
                        {
                            $ret_porcentaje=0;
                        }


                        include('include/total_venta.php');

                        $total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
                        $sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;

                        $sql=mysqli_query($con, "SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id' and forma_pago<>'credito' ");
                        $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; 
                        }

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

                        if($ven_forma_de_pago=="Credito" or $ven_forma_de_pago=="Combinado"  )
                            {
                                $total_abono; 
                            }
                        else 
                            {
                                $total_abono='0';
                            }

                            
                                $totalpagar+=$saldo;
                            ?>
                       
                    <?php } ?> 
            <div class="col-md-6" style="font-size:20px;padding-top: 10px;color: red;"><span>TOTAL SALDO PENDIENTE:</span><strong> <?php echo number_format($totalpagar,0); ?></strong></div> 
            </div>
            </div>
                <div class="widget stacked">
                    <div class="widget-content">
                        <section id="buttons">
                            <div class="col-md-6"></div>
                            <div class="col-md-3">
                                <input id="ser" type="hidden" class="form-control" autocomplete="off" placeholder="Serial de Articulo" onKeyUp='load(1);' >
                            </div>
                            <div class="col-md-3">
                                <input  class="form-control" type="text"  id="q" placeholder="Buscador" onKeyUp='load(1);' autofocus />
                            </div>
                        </section>
                    </div>
                </div>
                <div class="table-responsive">
                    <center> <span id="loader"></span></center>
                    <div id="resultados"></div><!-- Carga los datos ajax -->
                    <div class='outer_div'></div><!-- Carga los datos ajax -->
                </div>
            </div> <!-- /widget-content -->
        </div> <!-- /widget --> 
    </div> <!-- /span9 -->
</div> <!-- /main -->
<div class="footer" style="
        width: 100%;
        height:40px;
  position: fixed;
  bottom: 0;
  z-index:1000;">
    
    <div class="container">
        <div class="row">
            <div id="footer-copyright" class="col-md-5">
            2017-21 VICPOS.COM.
            </div> <!-- /span6 -->
            <div id="footer-copyright" class="col-md-6">
            Desarrollado por <a href="http://vicpos.com" target="_blank">Vicpos.com</a>
            </div> <!-- /.span6 -->
            <div id="footer-terms" class="col-md-1">
            <p>V 3</p>
            </div> <!-- /.span6 -->
        </div> <!-- /row -->
    </div> <!-- /container -->
</div> <!-- /footer -->
<script type="text/javascript" src="php_ventas/ajax.js"></script>

  </body>
</html>
<?php

}
else
{
    echo '<script>location.href = "index.php";</script>';
}
?>