<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$cot_id=$_SESSION['cot_id'];

if (isset($_POST['art_id'])){  $art_id=$_POST['art_id'];}                        
if (isset($_POST['art_referencia'])){  $art_referencia=$_POST['art_referencia'];}
if (isset($_POST['cantidad'])){ $cantidad=$_POST['cantidad'];}
if (isset($_POST['precio_venta'])){  $precio_venta=$_POST['precio_venta'];}
if (isset($_POST['art_iva'])){ $art_iva=$_POST['art_iva'];}
if (isset($_POST['descuento'])){$descuento=$_POST['descuento'];}
if (isset($_POST['nombre'])){  $nombre=$_POST['nombre'];}
if (isset($_POST['detallado'])){ $detallado=$_POST['detallado'];}

	
	
if (!empty($art_referencia) and !empty($cantidad) and !empty($precio_venta) and !empty($art_iva))
{

$sqll=mysqli_query($con,"SELECT cotd_art_id FROM dis_cotizacion_detalle
 WHERE cotd_art_id='$art_id'  and dis_cotizacion_cot_id='$cot_id'") ;
        if($roww=mysqli_fetch_array($sqll))
            { 
                $can7=mysqli_query($con,"SELECT * FROM dis_cotizacion_detalle WHERE cotd_art_id='$art_id' and dis_cotizacion_cot_id='$cot_id'") or die(mysqli_error($con));  
                    while($dato=mysqli_fetch_array($can7))
                    {
                    	
                        $cotd_art_id=$dato['cotd_art_id'];            
                        $c=$dato['cotd_cantidad'];
                        $suma=$c+$cantidad;
                       
                    }
                
               $registros=mysqli_query($con,"UPDATE dis_cotizacion_detalle Set cotd_cantidad='$suma',cotd_precio='$precio_venta' Where cotd_art_id='$art_id'  and dis_cotizacion_cot_id='$cot_id'") or die(mysqli_error($con));

            }
            else
            {   
            	if($detallado=='no')
            	{
            		$sql=mysqli_query($con,"INSERT INTO dis_cotizacion_detalle (cotd_art_id,cotd_referencia,cotd_nombre,cotd_cantidad,cotd_iva,cotd_desc,cotd_precio,detallado,dis_cotizacion_cot_id) 
                	VALUES ('$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','no','$cot_id')") or  die(mysqli_error($con));
           
            	}
            	else
            	{
            		$sql=mysqli_query($con,"INSERT INTO cotizacion_detalle (cotd_art_id,cotd_referencia,cotd_nombre,cotd_cantidad,cotd_iva,cotd_desc,cotd_precio,detallado,dis_cotizacion_cot_id) 
                	VALUES ('$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','si','$cot_id')") or  die(mysqli_error($con));
           
            	}		
            }

}
?>





<table class="table table-bordered table-hover table-striped">
<tr>
	<th width="7%">REFERENCIA</th>
	<th width="7%">CANT.</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 T</span></th>
	<th width="9%"></th>
</tr>
<?php
	$descuentof=0;
  	$descuento=0;
  	$neto=0;
  	$sumador_total=0;
  	$iva_total=0;




	$sql=mysqli_query($con, "SELECT * from  dis_cotizacion_detalle where dis_cotizacion_cot_id='$cot_id'  ");
	while ($row=mysqli_fetch_array($sql))
	{
	$id=$row["cotd_id"];
	$cotd_id=$row["cotd_id"];
	$cotd_referencia=$row['cotd_referencia'];
	$cotd_nombre=$row['cotd_nombre'];
	$cotd_cantidad=$row['cotd_cantidad'];
	$cotd_precio=$row['cotd_precio'];
	$cotd_iva=$row['cotd_iva'];
	$cotd_descuento=$row['cotd_desc'];

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

  	$precio_u_sin_iva=$cotd_precio/$iva1;
  	$precio_unidad=$precio_u_sin_iva;

  	$precio_t_sin_iva=$precio_unidad*$cotd_cantidad;

  	$descuento=($cotd_descuento/100)*$precio_t_sin_iva;

  	$precio_descuento_t=$precio_t_sin_iva-$descuento;

  	$total_iva_u=$precio_descuento_t*$iva0;

  	$iva_total+=$total_iva_u;

  	$neto+=$precio_t_sin_iva;

  	$descuentof+=$descuento;

  	$sumador_total=($neto+$iva_total)-$descuentof;



	
?>
		<tr>
			<td><?php echo $cotd_referencia;?></td>
			<td><?php echo $cotd_cantidad;?></td>
			<?php if ($clie_cc=='1091665283' || $clie_cc=='1091670119') {  ?>
			<td>
				<a href="#" style="text-decoration: none;color: gray" data-id="<?php echo $id;?>" class="btn-mod_des"><strong> <?php echo $cotd_nombre;?></strong></a> 
			</td>	
			<?php } else { ?>
 			<td><?php echo $cotd_nombre;?></td>
			<?php } ?>

			<td><span class="pull-right"><?php echo number_format($precio_unidad,0);?></span></td>
			
			<td><span class="pull-right"><?php echo $cotd_descuento;?></span></td>
			<td><span class="pull-right"><?php echo number_format($precio_t_sin_iva,0);?></span></td>
			<td ><a data-id="<?php echo $id;?>" class="btn btn-edit btn-xs btn-warning"> <i class="btn-icon-only icon-edit"></i></a>
		<a href="#" id="del-<?php echo $row["cotd_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
		<script>
		$("#del-"+<?php echo $row["cotd_id"];?>).click(function(e){
			e.preventDefault();
			p = confirm("Estas seguro?");
			if(p){
				$.get("./php_cotizacion/z_eliminar.php","cotd_id="+<?php echo $row["cotd_id"];?>,function(data){
					loadTabla();
				});
			}
		});
		</script></td>
		</tr>		
		<?php } ?>
<tr>
	<td colspan=5 > <span class="pull-right">NETO $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($neto,0);?></strong></span></td>
	<td></td>
</tr>
<tr>
	<td colspan=5 ><span class="pull-right">DESCUENTO $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($descuentof,0);?></strong></span></td>
	<td></td>
</tr>
<tr>
	<td colspan=5 ><span class="pull-right">IVA $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($iva_total,0);?></strong></span></td>
	<td></td>
</tr>

<tr>
	<td colspan=5 ><span class="pull-right">TOTAL $</span></td>
	<td><span class="pull-right"><strong><?php echo number_format($sumador_total,0);?></strong></span></td>
	<td></td>
</tr>
</table>

  <!-- Modal -->
  <script>
  	$(".btn-edit").click(function(){
  		id = $(this).data("id");
  		$.get("./php_cotizacion/z_formulario.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-mod_des").click(function(){
  id = $(this).data("id");
  $.get("./php_cotizacion/z_modificar_descripcion.php","id="+id,function(data){
    $("#form-modifi_art").html(data);
  });
  $('#descripcion_art_Modal').modal('show');
});
</script>
<div class="modal fade" id="descripcion_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">Modificar descripción</h4>
            </div>
            <div class="modal-body">
                <div id="form-modifi_art"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

