<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
?>
<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
    $suma=0;
	$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_cot_temp where empresa='$empresa' and usuario='$usu_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $total_retencion=$rets['ret_importe'];

	
	$sql=mysqli_query($con,"SELECT * from cotizacion_detalle_temp where usuario='$usu_id' and empresa='$empresa'");
	while ($row=mysqli_fetch_array($sql))
	{
    	$id=$row["cott_id"];
    	$cott_id=$row["cott_id"];
    	$cott_referencia=$row['cott_referencia'];
        $cott_art_id=$row['cott_art_id'];
        $cott_nombre=mb_strtoupper($row['cott_nombre']);
    	$cott_cantidad=$row['cott_cantidad'];
    	$cott_precio=$row['cott_precio'];
    	$cott_iva=$row['cott_iva'];
        $cott_descuento=$row['cott_desc'];
        $cambio=$row['cambio'];
        $suma+=$row['cott_cantidad'];

        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=$cott_iva/100; //0.19
        $iva1=1+$iva0; //1.19

        $precio_total_item=$cott_precio*$cott_cantidad;//precio normal 1.000.000
        $precio_total_sin_iva_item=($cott_precio/$iva1)*$cott_cantidad; //precio sin iva  840.336
        $descuento_item=($cott_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 $cott_referencia;?></td>
    <td> <div class="cajaselect">
            <span class="seleccionado"><?php echo $cott_unidad=$row['cott_unidad']; ?></span>
            <ul class="listaselect">
            <?php
                $artpres=mysqli_query($con,"SELECT * FROM articulos_presentacion where pres_art_id='$cott_art_id' and pres_unidad<>'$cott_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 $cott_cantidad;?>
       
    </td>
	<td><?php echo $cott_nombre;?>
        <span style="color: red;">
            <?php   
            echo $sta;
            $art=mysqli_query($con,"SELECT * FROM cotizacion_sabores_producto_temp WHERE cott_id='$cott_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($cott_precio,0);?></span></td>
	<td><span class="pull-right"><?php echo number_format($cott_descuento,0);?></span></td>
    <td><span class="pull-right"><?php echo number_format($precio_total_item,0);?></span></td>
	<td>
        <a data-id="<?php echo $id;?>" class="btn btn-mod 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='$cott_art_id' ") or die(mysqli_error($con)); 
            $data=mysqli_fetch_array($can11);
        ?>
        <a data-id="<?php echo $cott_id;?>" class="btn btn-sabores_art btn-xs btn-info" title="Sabores"> <i class="btn-icon-only fas fa-concierge-bell"></i></a>
        <?php } ?>
        <a href="#"id="del-<?php echo $row["cott_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>

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


<?php
$sumador_combo=0;
$sql2=mysqli_query($con, "SELECT * from cotizacion_detalle_combo_temp where usuario='$usu_id' and empresa='$empresa'");
while ($row2=mysqli_fetch_array($sql2))
{
    $cdc_id=$row2['cdc_id'];
    $combo_id=$row2['combo_id'];
    $precio=($row2['combo_cantidad']*$row2['combo_precio']);
    $sumador_combo+=$precio;
?>
<tr>
    <td><?php echo $row2['combo_referencia'];?></td>
    <td></td>
    <td><?php echo $row2['combo_cantidad'];?></td>
    <td><?php echo $row2['combo_nombre']; ?>
        <span style="color: red;">
        <?php $art=mysqli_query($con,"SELECT * FROM cotizacion_sabores_combo_temp 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($row2['combo_precio'],0);?></span></td>
    <td><span class="pull-right">0</span></td>
    <td><span class="pull-right"><?php echo number_format($precio,0);?></span></td>
    <td>
        <?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 } ?>
        <a href="#" id="delc-<?php echo $cdc_id;?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
        <script>
            $("#delc-"+<?php echo $cdc_id;?>).click(function(e){
                  e.preventDefault();
                  p = confirm("Estas seguro?");
                  if(p){
                    $.get("./php_cotizaciones/eliminar_combo_temp.php","cdc_id="+<?php echo $cdc_id;?>,function(data){
                      loadTabla();
                    });
                  }
                });
        </script>
    </td>
</tr> 
<?php  }    ?>

<tr>
    <td></td>
    <td> <span class="pull-left"><strong><a title="" class=""><span class="badgeazul"><i class="btn-icon-only "><?php echo $suma; ?></i></span></a></strong></span></td>
    <td colspan=4><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">RETENCION $</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_cot_temp where empresa='$empresa' and usuario='$usu_id'") or die(mysqli_error($con)); 
        while ($rets=mysqli_fetch_array($ret))
        {
            $retencion_ret_id=$rets['retencion_ret_id'];
            $rete=mysqli_query($con,"SELECT * FROM retencion where empresa='$empresa' and ret_id='$retencion_ret_id'") or die(mysqli_error($con));
            $rete=mysqli_fetch_array($rete);
            $ret_porcentaje=$rete['ret_porcentaje'];
        ?>
        <h5> (<?php echo $ret_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/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"><strong>TOTAL $</strong></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>


<script>
  	$(".btn-mod").click(function(){
  		id = $(this).data("id");
  		$.get("./php_cotizaciones/form_modificar_temp.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/form_sabores_productos_temp.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">Agregar 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/form_sabores_combos_temp.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">Agregar Sabores</h4>
            </div>
            <div class="modal-body">
                <div id="form-sabores_combo"></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/editar_presentacion.php?pres_id="+pres_id+'&id='+id,function(data){
    $("#tabla").html(data);
    loadTabla();  
    })
}

</script>