<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='31' and modp_nombre='Eliminar Pedido Confirmado'") or die(mysqli_error($con));
$permi = mysqli_fetch_array($per);
$estado_confirmado=$permi['estado'];

$per= mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='31' and modp_nombre='Eliminar Pedido Pendiente' ") or die(mysqli_error($con));
$permi = mysqli_fetch_array($per);
$estado_pendiente=$permi['estado'];

$est=mysqli_query($con,"SELECT * FROM modulo_permiso WHERE mod_id='31' and modp_nombre='Modificar Precio' and usuario='$usu_id'  ") or die(mysqli_error($con));
$est=mysqli_fetch_array($est);
if($est['estado']=='Activo')
{
    $readonly='';
}
else
{
    $readonly='readonly'; 
}

$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_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"];
    $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_total=$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;
}
    ?>
    <input type="hidden" name="pen_id" id="pen_id" value="<?php echo $pen_id;?>">
    <div>
        <div class="col-md-12" style="background-color:#098BCB;padding-top:3px; padding-bottom: 3px;">
           <center><span style="color:white;" ><strong>RESUMEN DE VENTA</strong></span></center>
        </div>
    </div>
    <div class="col-md-12" style="padding-top:3px; padding-bottom: 3px; font-size: 15px;"><strong>SUBTOTAL</strong>
        <span class="pull-right" style="color:green;">
            <strong>$ <?php echo number_format($neto+$neto_combo,0);?></strong>
        </span>
    </div>
    <div class="col-md-12" style="padding-top:3px;background-color:#F3F2F2; padding-bottom: 3px; font-size: 15px;"><strong>DESCUENTO </strong>
        <span class="pull-right" style="color:green;">
            <strong>$ <?php echo number_format($descuentof+$descuento_combo_total,0);?></strong>
        </span>
    </div>
    <div class="col-md-12" style="background-color:black; font-size: 20px;color: white;padding: 5px;">TOTAL
        <span class="pull-right"><strong>$ <?php echo number_format($sumador_total+$sumador_combo,0);?></strong>
        </span>
    </div>
    </br>
   <div id="tabla_abonos"></div>
   
   <div class="modal fade" id="abonar" 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">ABONAR CUENTA</h4>
                </div>
                <div class="modal-body">
                    <div id="form-abonar"></div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

<!-- /.modal detalle tabla -->
    <div class="modal fade" id="tabla_detalle" 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">DETALLE ABONOS</h4>
                </div>
                <div class="modal-body">
                    <div id="detalle"></div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    <br>
<div>
    <form action="" method="post" id="descuentotal" name="descuentotal" class="">
        <div class="input-group">
            <input type="number" min="0" max="99" name="desc" value="0" size="3" class="form-control input-sm" id="desc" placeholder="%" accesskey="p" title="alt + p" autocomplete="off" <?php echo $readonly; ?> />
            <span class="input-group-btn btn-group-sm">
                <button name="btn_desc" id="btn_desc" type="submit" class="btn btn-warning " >Descuento</button>
            </span>
            <input type="hidden" name="pen_id1" id="pen_id1" value="<?php echo $pen_id;?>" >
        </div>
    </form>
</br>

   
    <?php
     	$com=mysqli_query($con, "SELECT * from pedido where pen_id='$pen_id'");
     	$come=mysqli_fetch_array($com);
        $pen_comentario=$come["pen_comentario"];
        $pen_estado=$come["pen_estado"];
    ?>
    <div id="comen"></div>
    <label>Comentarios</label>
    <textarea class="form-control" name="comentario" id="comentario" onkeyup="return comentarios(2,this.value);"><?php echo $pen_comentario;?></textarea>
    </br>

    <?php if ($sumador_total+$sumador_combo>0) 
    { ?>
        <div class="btn-group btn-group-justified btn-group-sm" style="padding-bottom: 5px;">
            <a data-toggle="modal" href="#confirmar" class="btn btn-info">ENVIAR PEDIDO</a>
            <?php   if($negocio_neg_id=='2')  {  ?>
                <?php 
                    $sqlu=mysqli_query($con,"SELECT * FROM ubicacion where empresa='$empresa' and sucursal='$sucursal'");
                    $rowsu=mysqli_num_rows($sqlu);
                    if($rowsu>1) { 
                ?>
                    <a href="#recibo" class="btn btn-default" data-toggle="modal">RECIBO</a>
                <?php } else { ?>
                    <a href="tirillas/pedido.php?pen_id=<?php echo $pen_id;?>&&ubicacion=0" target="_blank" class="btn btn-default" data-toggle="modal">RECIBO</a>
                <?php } ?>
                <?php if($propina>0) { ?>
                <a data-toggle="modal" href="#prefactura" class="btn btn-warning" >PREFACTURA</a>
                <?php } else { ?>
                <a href="tirillas/prefactura.php?pen_id=<?php echo $pen_id;?>&&propi=no" target="_blank" class="btn btn-warning" type="button" >PREFACTURA</a>
                <?php } 
            }   ?>

            <?php if($negocio_neg_id=='6') {  ?>
            <a href="tirillas/boleta.php?pen_id=<?php echo $pen_id;?>" target="_blank" class="btn btn-default">RECIBO</a>
            <?php } ?>

        <?php 
        if($pen_estado=='En Proceso') { ?>
            <?php if ($estado_pendiente=='Activo') { ?>
                <a  href="#cancelar" class="btn btn-danger" data-toggle="modal">CANCELAR</a>
            <?php } ?>
            <?php } else { 
                if ($estado_confirmado=='Activo') { ?>
                <a  href="#cancelar" class="btn btn-danger" data-toggle="modal">CANCELAR</a>
            <?php  } } ?>

        </div>
        <?php  if ($aper_num>'0'){ ?>
            <div class="btn-group btn-group-justified btn-group-sm" style="padding-bottom: 5px;">
                <a data-toggle="modal" class="btn btn-success btn-fact">FACTURAR</a>
            </div>
        <?php } ?>

<?php   }
    else 
    { 
        $sqlu=mysqli_query($con,"SELECT * FROM ubicacion where empresa='$empresa' and sucursal='$sucursal'");
        $rowsu=mysqli_num_rows($sqlu);
        if($rowsu>1)
        { 
    ?>
            <a href="#recibo" class="btn btn-default" data-toggle="modal">RECIBO</a>
        <?php   } else  {  ?>
            <a href="tirillas/pedido.php?pen_id=<?php echo $pen_id;?>&&ubicacion=0" target="_blank" class="btn btn-default" data-toggle="modal">RECIBO</a>
        <?php  }  ?>

        <?php 
        if($pen_estado=='En Proceso'){ 
            if ($estado_pendiente=='Activo') { ?>
            <a  href="#cancelar" class="btn btn-danger" data-toggle="modal">CANCELAR</a>
            <?php } ?>
        <?php } else {
            if ($estado_confirmado=='Activo') { ?>
            <div class="btn-group btn-group-justified btn-group-sm" style="padding-bottom: 5px;">
                <a  href="#cancelar" class="btn btn-danger" data-toggle="modal">CANCELAR</a>
             </div>
            <?php } 
        } ?>
<?php } ?>
</div>    
 
<div id="recibo" 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">×</button>
                <h4 class="modal-title">Imprimir Recibo</h4>
            </div>
            <div class="modal-body">
                <a href="tirillas/pedido.php?pen_id=<?php echo $pen_id;?>&&ubicacion=0" target="_blank" class="icon-btn" >
                    <p style="padding: 10px; text-align: left;"><i class="fa-sharp fa-regular fa-print fa-2xl"></i><strong> TODO</strong></p>
                </a>
                <?php 
                $sql=mysqli_query($con,"SELECT * FROM ubicacion where empresa='$empresa' and sucursal='$sucursal'");
                while ($reg=mysqli_fetch_array($sql))
                {    
                    $ubi_id=$reg['ubi_id']; 
                    $ubi=mysqli_query($con, "SELECT * from pedido_detalle pd, articulos a where pd.pedido_pen_id='$pen_id' and pd.pend_estado='facturado' and pd.pend_archivado='no' and a.art_id=pd.pend_art_id and a.ubicacion_ubi_id='$ubi_id' limit 1");

                     $ubicom=mysqli_query($con, "SELECT * from pedido_detalle_combo pdc, combos c where pdc.pedido_pen_id='$pen_id'  and pdc.pend_estado='facturado' and pdc.pend_archivado='no' and pdc.combo_id=c.com_id and c.com_ubicacion=$ubi_id limit 1 ");

                    $conta=mysqli_num_rows($ubi);
                    $contacom=mysqli_num_rows($ubicom);
                    if ($conta>=1 || $contacom>=1) 
                   {
                ?>            
                    <a href="tirillas/pedido.php?pen_id=<?php echo $pen_id;?>&&ubicacion=<?php echo $ubi_id;?>" target="_blank" class="icon-btn" >
                        <p style="padding: 10px; text-align: left;"><i class="fa-sharp fa-regular fa-print fa-2xl"> </i> <strong> <?php echo $reg['ubi_nombre'];?></strong></p>
                    </a>
                <?php  }  } ?>
            </div><!-- /.modal-content -->
        </div>
    </div><!-- /.modal-dialog -->
</div>



<script>


function loadabonos(){
    var id=$("#pen_id").val();
    $.get("./php_pedidos/tabla_total_abono.php?id="+id,function(data){
    $("#tabla_abonos").html(data);
    })
}
loadabonos();


$(".btn-fact").click(function(){
    var pen_id=$("#pen_id").val();
    $.get("./php_pedidos/form_facturar.php?pen_id="+pen_id,function(data){
    $("#form-fact").html(data);
  });
  $('#cobrar').modal('show');
});



</script>


<script>
$("#descuentotal").submit(function(event) {
$('#btn_desc').attr("disabled", true);
var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_pedidos/descuento.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
      success: function(datos){
      $("#tabla").html(datos);
      $('#btn_desc').attr("disabled", false);
      loadTabla();
      loadtotal();
      }
  });
  event.preventDefault();
})



function comentarios(campo,comentario){
    var pen_id=$("#pen_id").val();
    var parametros = {"action":"update","campo":campo,"comentario":comentario,"pen_id":pen_id}; 
    $.ajax({
    type: "POST",
    url: "./php_pedidos/comentario.php",
    data: parametros,
    success: function(datos){
    $("#comen").html(datos);
    }
  });
}  
</script>
<style>
.middle {
  width: 100%;
  text-align: center;
  /* Made by */
}
.middle h1 {
  font-family: "Dax", sans-serif;
  color: #ccc;
}
.middle input[type="radio"] {
  display: none;
}
.middle input[type="radio"]:checked + .box {
  background-color: #009e90;
}
.middle input[type="radio"]:checked + .box span {
  color: white;
  transform: translateY(70px);
}
.middle input[type="radio"]:checked + .box span:before {
  transform: translateY(0px);
  opacity: 1;
}
.middle .box {
  width: 131px;
  height: 60px;
  background-color: #ccc;
  transition: all 250ms ease;
  will-change: transition;
  display: inline-block;
  text-align: center;
  cursor: pointer;
  position: relative;
  font-family: "Dax", sans-serif;
  font-weight: 900;
  padding-top: 19px;
}

.middle .box span {
  left: 0;
  right: 0;
}
</style>