<?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%">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 * FROM retencion_cotizacion where cotizacion_cot_id='$cot_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
            
    $ret_porcentaje=isset($rets['porcentaje']);
	$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><?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_reservas/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_reservas/z_eliminar_combo.php","cdc_id="+<?php echo $cdc_id;?>,function(data){
                        loadTabla();
                        });
                    }
                });
            </script>
        </td>
    </tr>

<?php   } 
    $total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
?>
<tr>
	<td colspan=5><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=5><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=5><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=5 ><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="5"><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_reservas/z_eliminar_retencion.php","ret_id="+<?php echo $rets["ret_id"];?>,function(data){
              loadTabla();
              loadtotal();
              loadventa();
            });
          }
        });
        </script>
        <?php }?>
    </td>
</tr>
<tr>
	<td colspan=5 ><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_reservas/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_reservas/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_reservas/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_reservas/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 -->