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

//SUMADOR 
$pen_id=$_GET['pen_id'];
$sqlpen=mysqli_query($con, "SELECT * from pedido where pen_id='$pen_id' ");
$rowpen=mysqli_fetch_array($sqlpen);
$pen_propina=$rowpen["pen_propina"];

$neto=0;
$descuentof=0;
$sumador_articulos=0;
$sumador_total=0;
$sql=mysqli_query($con, "SELECT * from pedido_detalle where pedido_pen_id='$pen_id' and pend_estado='facturado'");
while ($row=mysqli_fetch_array($sql))
{
    $pend_id=$row["pend_id"];
    $pend_art_id=$row["pend_art_id"];
    $vent_cantidad=$row['pend_cantidad'];
    $vent_precio=$row['pend_precio'];
    $vent_descuento=$row['pend_descuento'];

    $total_sab=0;
    $suma_sab=0;
    $sql1=mysqli_query($con, "SELECT * FROM sabores_pedido_temp where pend_id='$pend_id' ");
    while ($sab=mysqli_fetch_array($sql1))
    {
    $sab_precio=$sab["precio"]*$sab['cantidad']*$vent_cantidad;
    $suma_sab+=$sab_precio;

    }

    $total_sab=$suma_sab;


    $vent_precio_total=$vent_precio*$vent_cantidad+$total_sab;
    $descuento=($vent_descuento/100)*$vent_precio_total;
    $precio_descuento_t=$vent_precio_total-$descuento;
    $neto+=$vent_precio_total;
    $descuentof+=$descuento;
    $sumador_articulos=($neto)-$descuentof;
}

  
$descuento_combo_total=0;
$neto_combo=0;
$sumador_combo=0;
$sql2=mysqli_query($con, "SELECT * from pedido_detalle_combo where pedido_pen_id='$pen_id' and pend_estado='facturado' ");
while ($row2=mysqli_fetch_array($sql2))
{
    $pedcom_cantidad=$row2['pedcom_cantidad'];
    $pend_desc=$row2['pend_desc'];
    $pedcom_precio=$row2['pedcom_precio'];
    $precio_total=$pedcom_cantidad*$pedcom_precio;
    $descuento_combo_total+=($pend_desc/100)*$precio_total;
    $neto_combo+=$precio_total;
    $sumador_combo=$neto_combo-$descuento_combo_total;
}



$tarjer=mysqli_query($con,"SELECT *, sum(ped_abono) as total_suma from pedido_abono_temp where pedido_pen_id='$pen_id' ");
$tarjer=mysqli_fetch_array($tarjer);
$total_suma_abono=$tarjer['total_suma'];


$sumador_total=($sumador_articulos+$sumador_combo)-$total_suma_abono;




//CLIENTE
$cli1=mysqli_query($con,"SELECT * from pedido where pen_id='$pen_id'") or die(mysqli_error($con)); 
$cli1=mysqli_fetch_array($cli1);
$clie_id=$cli1['clie_id'];

$cli2=mysqli_query($con,"SELECT * from cliente where clie_id='$clie_id'") or die(mysqli_error($con)); 
$cli2=mysqli_fetch_array($cli2);
$clie_nombre=$cli2['clie_nombre'];
$clie_nota_credito=$cli2['clie_nota_credito'];
$clie_cupo_credito=$cli2['clie_cupo_credito'];
$clie_facturador=$cli2['facturador'];

if($clie_facturador=='si') 
{  
    $checked_e='checked'; 
    $checked_p=''; 
    $display_p='none'; 
    $display_e='block';
} 
else 
{ 
    $checked_p='checked'; 
    $checked_e=''; 
    $display_p='block'; 
    $display_e='none'; 
}
?>
<?php
$subtotal=$sumador_total+$sumador_combo;
$total_propina=$subtotal*($propina/100);
?>
<script type="text/javascript">
    $("#form_propina").submit(function(event) {
$('#btn_propina').attr("disabled", true);
var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_pedidos/propina.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
      success: function(datos){
      $("#tabla").html(datos);
      $('#btn_propina').attr("disabled", false);
      loadTabla();
      loadtotal();
      form_facturar();

      }
  });
  event.preventDefault();
})

</script>
<?php if($propina>0){ ?>
<div id="div_propina">
    <form action="" method="post" id="form_propina" name="form_propina" class="">
        <div class="input-group">
            <?php if($pen_propina<=0) {?>
            <input type="number" min="0" name="propina" value="<?php echo $total_propina;?>" class="form-control input-sm" id="propina" autocomplete="off" />
            <?php } else { ?>
            <input type="number" min="0" name="propina" value="<?php echo $pen_propina;?>" class="form-control input-sm" id="propina" autocomplete="off" />
            <?php }  ?>
            <span class="input-group-btn btn-group-sm">
                <button name="btn_propina" id="btn_propina" type="submit" class="btn btn-primary">Aplicar Propina</button>
            </span>
            <input type="hidden" name="pen_id2" id="pen_id2" value="<?php echo $pen_id;?>" >
        </div>
    </form>
    </br>
</div>
<?php } ?>


<div id="facturar"></div>
<script>
function form_facturar(){
    var pen_id=$("#pen_id").val();
    $.get("./php_pedidos/form_facturar2.php?pen_id="+pen_id,function(data){
      $("#facturar").html(data);
    })
}
form_facturar();
</script>