<?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='24' and usuario_usu_id='$usu_id'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
        if($reg['per_visualizar']=="Activo")
        {
        if ($aper_num>'0')
            {
?>
<!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="active";
$R_reportes="";

$ventas="active";
$remisiones="";
$cajas="";
$apartadas="";
$orden="";
$movimientos="";
$periodica=""; 
$cocina="";
$mesas=""; 
include ('menu.php');

$ven_id=$_GET["ven_id"];

$sumador_combo=0;
$sumador_total=0;

$neto=0;
$total_descuento=0;
$total_iva=0;
$total_retencion=0;
$sumador_articulos=0;
include('include/total_venta.php');

$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; 
}


$sumador_total=($sumador_articulos+$sumador_combo)-$devo_total;
?> 
<style type="text/css">
.list-horizontal{width:99%;margin:0 auto;min-width:400px}
.list-horizontal .item-25{width:24.5%}
.list-horizontal .item-20{width:20%}
.list-horizontal .item-18{width:18%}
.list-horizontal .item-15{width:15%}
.list-horizontal .item{display:inline-block;border-right:2px solid #f4f4f4;height:80px}
.list-horizontal .item:last-child{border-right:0}
.list-horizontal .item .title{font-size:13px;font-family:Roboto;font-weight:400;line-height:16px;text-align:center;display:block;margin-top:14px;margin-bottom:12px;color:#767681}
.list-horizontal .item .value{font-family:Roboto;font-size:20px;font-weight:700;line-height:24px;text-align:center;display:block}
.list-horizontal .item .value:not(.text-red){color:#585860}
.list-horizontal .item .value.highlight{border-radius:5px;width:120px;font-size:15px;margin:0 auto;font-weight:500;padding:5px}
</style>
<div class="main">
    <div class="col-md-2">
        <?php include ('include/menu_pos.php');?>       
    </div> <!-- /span6 -->
    <div class="col-md-10"> 
        <div class="widget stacked">
            <div class="widget-header">
                <i class="icon-file-text"></i>
                <h3>NOTA CREDITO</h3>
            </div> <!-- /.widget-header -->
            <div class="widget-content">
                <div id="resultados"></div>

                 <!-- <div class="widget-content">
                    <section class="float-card list-horizontal ">
                        <section class="item item-20">
                            <span class="title">Valor total</span>
                            <span class="value">$5,000</span>
                        </section>

                        <section id="" class="item item-18 ">
                            <span class="title">Retenido</span>
                            <span class="value text-red">$0</span>
                        </section>
                        
                        <section class="item item-18">
                            <span class="title">Notas crédito</span>
                            <span class="value text-red">$0</span>
                        </section>

                        <section id="" class="item item-20 ">
                            <span class="title">Cobrado</span>
                            <span class="value text-green">$5,000</span>
                        </section>

                        <section id="" class="item item-20 ">
                            <span class="title">Por cobrar</span>
                            <span class="value text-red">$0</span>
                        </section>
                    </section> 
                </div></br>-->


                <form role="form" id="datos_cotizacion">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Cliente</label>            
                            <?php
                                $can=mysqli_query($con,"SELECT * FROM cliente c, ventas v where c.clie_id=v.Cliente_clie_id and v.ven_id='$ven_id' and v.empresa='$empresa'") or die(mysqli_error($con)); 
                                $dato=mysqli_fetch_array($can);
                                $ven_n_venta=$dato['ven_n_venta'];
                                $ven_estado=$dato['ven_estado'];

                                $clie_id=$dato['clie_id'];
                                $clie_nombre=$dato['clie_nombre'];
                                $clie_telefono=$dato['clie_telefono'];
                                $clie_correo=$dato['clie_correo'];
                                $clie_nota_credito=$dato['clie_nota_credito'];
                                $clie_disponible=$dato['clie_disponible'];
                            ?> 
                            <input type="text" value="<?php echo $clie_nombre; ?>"  id="cot_nombre" class="form-control input-sm" readonly>
                        </div> 
                    </div><!-- /input-group --> 
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Fecha</label>
                            <input type="text"  value="<?php echo date('d-m-Y');?>" class="form-control input-sm" readonly>
                        </div> <!-- /.form-group -->
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Venta Nº</label>
                            <input type="text"  value="<?php echo $ven_n_venta;?>" class="form-control input-sm" readonly>
                        </div> <!-- /.form-group -->
                    </div>
                   
                    <div class="col-md-11"></br></div>
                </form> <!-- /.form -->
                <div class="widget">
                    <div class="col-md-12">
                        <div class="form-group pull-right">
                            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#tabla">
                                <span class="btn-icon-only icon-plus "></span> Devolución productos
                            </button>
                            <?php if ($ven_estado=='sin pagar'){ ?>
                            <button type="button" class="btn btn-warning" data-toggle="modal" data-target="#descuento">
                                <i class="shortcut-icon fas fa-percent"></i> Aplicar Descuentos
                            </button>
                           <?php } ?>
                        </div>
                        <div id="tabla" class="modal fade " tabindex="-1" role="dialog">
                            <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">Devolucion</h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="table-responsive">
                                            <div id="tabla_resultados"></div><!-- Carga los datos ajax -->
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
                                        </div>
                                    </div>
                                </div><!-- /.modal-content -->
                            </div><!-- /.modal-dialog -->
                        </div> <!-- /.form-group -->

                        <div id="descuento" class="modal fade " tabindex="-1" role="dialog">
                            <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">Descuento</h4>
                                    </div>
                                    <form id="form_descuento" name="form_descuento" method="post">
                                        <div class="modal-body">
                                            <div id="resul_descuento"></div>
                                            <div class="form-group">
                                                <label>Neto</label>
                                                <input type="hidden" value="<?php echo $ven_id;?>" name="ven_id" id="ven_id" > 

                                                <input type="number" id="neto" name="neto" value="<?php echo $sumador_total;?>" readonly  class="form-control input-sm" >
                                            </div> <!-- /.form-group -->
                                            <div class="form-group">
                                                <label>% Descuento</label>
                                                <input type="number" id="porcen" name="porcen" value="" min="1" max="100" onkeyup="Calcular();" class="form-control input-sm" >
                                            </div> <!-- /.form-group -->
                                             <div class="form-group">
                                                <label>Valor Descuento</label>
                                                <input type="number" id="valor_desc" name="valor_desc" value="" class="form-control input-sm" >
                                            </div> <!-- /.form-group -->
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                                                <button type="submit" name="btn_descuento" class="btn btn-danger" id="btn_descuento">Aplicar</button>
                                            </div>
                                        </div>
                                    </form>
                                </div><!-- /.modal-content -->
                            </div><!-- /.modal-dialog -->
                        </div> <!-- /.form-group -->
                        </br></br>
                        <div class="table-responsive">
                            <center> <span id="loader"></span></center>
                            <div id='div_historial'></div><!-- Carga los datos ajax -->
                            <input type="button" value="volver atrás" name="Atras" class="btn btn-danger" onclick="history.back()" />
                        </div>
                    </div>
                </div>
            </div> <!-- /.table-responsive -->
        </div> <!-- /.widget-content --></br></br></br>
    </div> <!-- /.widget --> 
</div> <!-- /container -->
<input type="hidden" value="<?php echo $ven_id;?>" name="ven_id" id="ven_id" > 
<div class="modal fade" id="devolucion" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header" style="background-color: red;">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Seleccione Cantidad</h4>
            </div>
            <div class="modal-body">
                <div id="form-devolucion"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
function Calcular() {
    neto = document.form_descuento.neto.value;
    porcen = document.form_descuento.porcen.value;
    valor=neto*(porcen/100);
    document.form_descuento.valor_desc.value = valor;
}

function load()
{
    var ven_id=$("#ven_id").val();
    $.get("php_pos/tabla_devolucion.php?ven_id="+ven_id,function(data){
    $("#tabla_resultados").html(data);
    })
}
load();

function loadtabla()
{
    var ven_id=$("#ven_id").val();
    $.get("php_pos/tabla_historial.php?ven_id="+ven_id,function(data){
    $("#div_historial").html(data);
    })
}
loadtabla();

$("#form_descuento").submit(function( event ) {
$('#btn_descuento').attr("disabled", true);
var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_pos/registrar_descuento.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#resul_descuento').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resul_descuento").html(datos);
        $('#btn_descuento').attr("disabled", false);
        loadtabla();
      }
  });
  event.preventDefault();
})
</script>
<?php
    include ('php_caja/caja_abrir.php');
    include ('script.php');
}
else
    {
        echo '<script>alert("primero debes Abrir caja")</script>';
        echo '<script> setTimeout("history.back(1)", 0); </script>';
    }
}
    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>';
}
?>