<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$cot_id=$_GET['cot_id'];                        
?>
<table class="table table-bordered table-hover table-striped">
<tr>
	<th width="7%">CODIGO</th>
    <th width="7%">UM </th>
	<th width="7%">CANTIDAD</th>
	<th><span class="pull-center">DESCRIPCIÓN</span></th>
	<th width="10%"><span class="pull-right">PRECIO U.</span></th>
	<th width="7%"><span class="pull-right">DESC.%</span></th>
	<th width="10%"><span class="pull-right">PRECIO TOTAL</span></th>
	<th width="10%"></th>
</tr>
<?php
	$neto=0;
    $total_descuento=0;
    $total_iva=0;
    $total_retencion=0;
    $sumador_articulos=0;

    $ret=mysqli_query($con,"SELECT SUM(ret_importe) as ret_importe FROM retencion_cotizacion where cotizacion_cot_id='$cot_id' ") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $total_retencion=$rets['ret_importe'];

	$sql=mysqli_query($con, "SELECT * from cotizacion_detalle where cotizacion_cot_id='$cot_id' and  empresa='$empresa'");
	while ($row=mysqli_fetch_array($sql))
	{
    	$id=$row["cotd_id"];
    	$cotd_id=$row["cotd_id"];
        $cotd_referencia=$row['cotd_referencia'];
        $cotd_art_id=$row['cotd_art_id'];
        $cotd_nombre=mb_strtoupper($row['cotd_nombre']);
        $cotd_cantidad=$row['cotd_cantidad'];
        $cotd_precio=$row['cotd_precio'];
        $cotd_iva=$row['cotd_iva'];
        $cotd_descuento=$row['cotd_desc'];
        $cambio=$row['cambio'];

        if ($negocio_neg_id==7)
        {
            if($cambio=='ven'){ $sta='Vendido'; } 
            if($cambio=='cam'){ $sta='Cambio'; } 
            if($cambio=='com'){ $sta='Bonificacion'; }
            if($cambio==null){ $sta=''; }
        }
        else
        {
            $sta='';
        }

        $iva0=$cotd_iva/100; //0.19
        $iva1=1+$iva0; //1.19

        $precio_total_item=$cotd_precio*$cotd_cantidad;//precio normal 1.000.000
        $precio_total_sin_iva_item=($cotd_precio/$iva1)*$cotd_cantidad; //precio sin iva  840.336
        $descuento_item=($cotd_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
        $total_iva_item=($precio_total_sin_iva_item-$descuento_item)*$iva0; // total iva  159.336

        $neto+=$precio_total_sin_iva_item;
        $total_descuento+=$descuento_item;
        $total_iva+=$total_iva_item;
        $sumador_articulos=($neto+$total_iva)-$total_descuento;
?>
<tr>
	<td><?php echo $cotd_referencia;?></td>
     <td> <div class="cajaselect">
            <span class="seleccionado"><?php echo $cotd_unidad=$row['cotd_unidad']; ?></span>
            <ul class="listaselect">
            <?php
                $artpres=mysqli_query($con,"SELECT * FROM articulos_presentacion where pres_art_id='$cotd_art_id' and pres_unidad<>'$cotd_unidad' order by pres_cantidad asc") ;
                while($artpre=mysqli_fetch_array($artpres))
                {   
                    $pres_id=$artpre['pres_id'];
                    ?> 
                <li onclick="presentacion(<?php echo $pres_id;?>,<?php echo $id;?>)">
                    <a href="#" ><?php echo $artpre['pres_unidad']; ?></a>
                </li>
            <?php   }   ?>
            </ul>
            <span class="trianguloinf"></span>
        </div>   
    </td>
	<td><?php echo $cotd_cantidad;?></td>
	<td><?php echo $cotd_nombre;?>
        <span style="color: red;">
            <?php   
            echo $sta;
            $art=mysqli_query($con,"SELECT * FROM cotizacion_sabores_producto WHERE cotd_id='$cotd_id'")or die(mysqli_error($con));
            while ($row1=mysqli_fetch_array($art))
            { 
                echo ' ';
                echo mb_strtoupper($row1['sab_nombre']);
                echo ' -'; 
            } 
            ?>
        </span>
    </td>
	<td><span class="pull-right"><?php echo number_format($cotd_precio,0);?></span></td>
	<td><span class="pull-right"><?php echo $cotd_descuento;?></span></td>
	<td><span class="pull-right"><?php echo number_format($precio_total_item,0);?></span></td>
	<td>
        <a href="#" id="del-<?php echo $row["cotd_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
        <a data-id="<?php echo $id;?>" class="btn btn-edit btn-xs btn-warning"> <i class="btn-icon-only icon-edit"></i></a>
        <?php
            if($negocio_neg_id=='2')
            {
                $can11=mysqli_query($con,"SELECT * FROM sabores_productos where articulo_art_id='$cotd_art_id' ") or die(mysqli_error($con)); 
                $data=mysqli_fetch_array($can11);
                if($data>0){
        ?>
            <a data-id="<?php echo $cotd_id;?>" class="btn btn-sabores_art btn-xs btn-info" title="Sabores"> <i class="btn-icon-only fas fa-concierge-bell"></i></a>
        <?php  }  }?>
        <?php
            $canl=mysqli_query($con,"SELECT * FROM articulos where art_id='$cotd_art_id'") or die(mysqli_error($con)); 
            $datal=mysqli_fetch_array($canl);
            $art_lote=$datal['art_lote'];
            if($art_lote=='si')
            {  ?>
                <a href="#" data-id="<?php echo $id;?>" data-toggle="modal" class="btn btn-form_lote btn-xs btn-primary" title="lote"> <i class="btn-icon-only fas fa-boxes"></i></a>
        <?php   }  ?>

        <script>
        $("#del-"+<?php echo $row["cotd_id"];?>).click(function(e){
        	e.preventDefault();
        	p = confirm("Estas seguro de Eliminar?");
        	if(p){
        		$.get("./php_cotizaciones/z_eliminar.php","cotd_id="+<?php echo $row["cotd_id"];?>,function(data){
        			loadTabla();
        		});
        	}
        });
        </script>
    </td>
</tr>		
<?php } ?>





<?php 
    $sumador_combo=0;
    $sql=mysqli_query($con, "SELECT * from cotizacion_detalle_combo where cot_id='$cot_id'");
    while ($row=mysqli_fetch_array($sql))
    {
        $cdc_id=$row["cdc_id"];
        $combo_id=$row["combo_id"];
        $combo_referencia=$row["combo_referencia"];
        $combo_cantidad=$row["combo_cantidad"];
        $combo_precio=$row["combo_precio"];
        $subtotal=$combo_cantidad*$combo_precio;
        $sumador_combo+=$subtotal;
?>
    <tr>
        <td ><?php echo $combo_referencia;?></td>
        <td ><?php echo $combo_cantidad;?></td>
        <td ><?php echo $row["combo_nombre"];?>
            <span style="color: red;">
            <?php $art=mysqli_query($con,"SELECT * FROM cotizacion_sabores_combo WHERE cdc_id='$cdc_id' ")or die(mysqli_error($con));
                while ($row1=mysqli_fetch_array($art))
                {
                    echo ' ';
                    echo mb_strtoupper($row1['sab_nombre']);
                    echo ' -'; 
                }
            ?>
            </span>
        </td>
        <td ><span class="pull-right"><?php echo number_format($combo_precio,0);?></span></td>
        <td ><span class="pull-right"><?php //echo number_format($vend_descuento,0);?></span></td>
        <td style="text-align: right; "><span class="pull-right"><?php echo number_format($subtotal,0);?></span></td>
        <td> 
            <a href="#" id="delc-<?php echo $cdc_id;?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
            <?php
                if($negocio_neg_id=='2')
                {
                    $can11=mysqli_query($con,"SELECT * FROM sabores_combos where combo_id='$combo_id' ") or die(mysqli_error($con)); 
                    $data=mysqli_fetch_array($can11);
                ?>
                <a data-id="<?php echo $cdc_id;?>" class="btn btn-sabores_combo btn-xs btn-info" title="Sabores"><i class="btn-icon-only fas fa-concierge-bell"></i></a>
            <?php   }   ?>
            <script>
                $("#delc-"+<?php echo $cdc_id;?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro de Eliminar?");
                    if(p){
                        $.get("./php_cotizaciones/z_eliminar_combo.php","cdc_id="+<?php echo $cdc_id;?>,function(data){
                        loadTabla();
                        });
                    }
                });
            </script>
        </td>
    </tr>

<?php   }  ?>
<tr>
	<td colspan=6><span class="pull-right">SUBTOTAL 1 $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($neto+$sumador_combo,0);?></strong></span></td>
	<td></td>
</tr>
<tr>
    <td colspan=6><span class="pull-right" style="color: red;">DESCUENTO $</span></td>
    <td><span class="pull-right" style="color: red;"><strong><?php echo number_format($total_descuento,0);?></strong></span></td>
    <td></td>
</tr>
<tr>
    <td colspan=6><span class="pull-right">SUBTOTAL 2 $</span></td>
    <td><span class="pull-right"><strong><?php echo number_format(($neto+$sumador_combo)-$total_descuento,0);?></strong></span></td>
    <td></td>
</tr>
<tr>
	<td colspan=6 ><span class="pull-right">IMPUESTO $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($total_iva,0);?></strong></span></td>
	<td></td>
</tr>
<tr>
    <td colspan="6"><span class="pull-right"><strong>RETENCION $</strong></span></td>
    <td>
        <span class="pull-right"><strong><?php echo number_format($total_retencion,0);?></strong></span></td>
    <td> 
        <?php
        $ret=mysqli_query($con, "SELECT * FROM retencion_cotizacion where empresa='$empresa' and cotizacion_cot_id='$cot_id'") or die(mysqli_error($con)); 
        while ($rets=mysqli_fetch_array($ret))
        {
            $ret_id=$rets['ret_id'];
            $porcentaje=$rets['porcentaje'];
        ?>
        <h5> (<?php echo $porcentaje;?>%)<a href="#" id="re-<?php echo $rets["ret_id"];?>"><i class="btn-icon-only icon-remove"></i></a></h5>
        <script>
        $("#re-"+<?php echo $rets["ret_id"];?>).click(function(e){
            e.preventDefault();
            p = confirm("Estas seguro?");
            if(p){
            $.get("./php_cotizaciones/z_eliminar_retencion.php","ret_id="+<?php echo $rets["ret_id"];?>,function(data){
              loadTabla();
              loadtotal();
              loadventa();
            });
          }
        });
        </script>
        <?php }?>
    </td>
</tr>
<tr>
	<td colspan=6 ><span class="pull-right">TOTAL $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($sumador_articulos+$sumador_combo-$total_retencion,0);?></strong></span></td>
	<td></td>
</tr>
</table>
  <!-- Modal -->
<script>
$(".btn-edit").click(function(){
	id = $(this).data("id");
	$.get("./php_cotizaciones/z_form_modificar.php","id="+id,function(data){
		$("#form-edit").html(data);
	});
	$('#editModal').modal('show');
});
</script>
<div class="modal fade" id="editModal" 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">Actualizar</h4>
            </div>
            <div class="modal-body">
                <div id="form-edit"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal --> 


<script>
$(".btn-sabores_art").click(function()
{
    id = $(this).data("id");
    $.get("./php_cotizaciones/z_form_sabores_productos.php","id="+id,function(data){
        $("#form-sabores_art").html(data);
    });
    $('#sabores_art_Modal').modal('show');
});
</script>
<div class="modal fade" id="sabores_art_Modal" 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">Sabores</h4>
            </div>
            <div class="modal-body">
                <div id="form-sabores_art"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
$(".btn-sabores_combo").click(function(){
    id = $(this).data("id");
    $.get("./php_cotizaciones/z_form_sabores_combos.php","id="+id,function(data){
        $("#form-sabores_combo").html(data);
    });
    $('#sabores_combo_modal').modal('show');
});
</script>
<div class="modal fade" id="sabores_combo_modal" 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">Sabores</h4>
            </div>
            <div class="modal-body">
                <div id="form-sabores_combo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



<script>
$(".btn-form_lote").click(function(){
    id = $(this).data("id");
    $.get("./php_cotizaciones/z_form_lote.php","id="+id,function(data){
        $("#form-lote_tabla").html(data);
    });
    $('#lote_Modal').modal('show');
});
</script>
<div class="modal fade" id="lote_Modal" 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">Lote</h4>
            </div>
            <div class="modal-body">
                <div id="form-lote_tabla"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<style type="text/css">
.cajaselect {
  background: none repeat scroll 0 0 #4cbeff;
      cursor: pointer;
  position: relative;
}
ul.listaselect {
  background: none repeat scroll 0 0 #fff;
  border: 1px solid #dedede;
  display: none;
  left: -1px;
  margin-left: 0;
  margin-top: 32px;
  padding-left: 0;
  position: absolute;
  text-indent: 15px;
  top: 0;
  width: 100%;
  z-index: 100;

}
ul.listaselect li {
  border-bottom: 1px solid #efefef;
  cursor: pointer;
  display: block;
  line-height: 15px;
  list-style: outside none none;
  margin: 0;
  padding: 0.5em 0.3em;

}
ul.listaselect li a {
  color: #333;
  text-decoration: none;
}
ul.listaselect li a:hover {
  color: #999797;
  text-decoration: none;
}
ul.SelectProductos li:last-child {
  border-bottom: medium none;
}
.seleccionado {
  color: white;
  display: block;
  font-weight: 400;
  line-height: 2.5;
  text-indent: 8px;
  z-index: 100;

}
.trianguloinf {
  border-left: 9px solid rgba(0, 0, 0, 0);
  border-right: 9px solid rgba(0, 0, 0, 0);
  border-top: 13px solid #cfecfc;
  height: 0;
  position: absolute;
  right: 10px;
  top: 10px;
  width: 0;
}
.triangulosup {
  border-bottom: 13px solid #cfecfc;
  border-left: 9px solid rgba(0, 0, 0, 0);
  border-right: 9px solid rgba(0, 0, 0, 0);
  height: 0;
  position: absolute;
  right: 10px;
  top: 10px;
  width: 0;
}
</style>

<script type="text/javascript">
$(document).ready(function() {
function clickcaja(e) {
    var lista = $(this).find("ul"),
    triangulo = $(this).find("span:last-child");
    e.preventDefault();
    //lista.is(":hidden") ? $(this).find("ul").show() : $(this).find("ul").hide();
    $(this).find("ul").toggle();
    if(lista.is(":hidden")) {
        triangulo.removeClass("triangulosup").addClass("trianguloinf");
    }
    else {
        triangulo.removeClass("trianguloinf").addClass("triangulosup");
    }
}
function clickli(e) {
    var texto = $(this).text(),
    seleccionado = $(this).parent().prev(),
    lista = $(this).closest("ul"),
    triangulo = $(this).parent().next();
    e.preventDefault();
    e.stopPropagation();  
    seleccionado.text(texto);
    lista.hide();
    triangulo.removeClass("triangulosup").addClass("trianguloinf");
}
$(".cajaselect").click(clickcaja);
$(".cajaselect").on("click", "li", clickli);
});


function presentacion(pres_id,id)
{
    $.get("./php_cotizaciones/z_editar_presentacion.php?pres_id="+pres_id+'&id='+id,function(data){
    $("#tabla").html(data);
    loadTabla();  
    })
}

</script>