<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');

if (isset($_POST['art_id'])){ echo$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=utf8_decode($_POST['nombre']);}
if (isset($_POST['detallado'])){ $detallado=$_POST['detallado'];}
if (isset($_POST['combo'])){ $combo=$_POST['combo'];}

	
if (!empty($cantidad)  )
{

	if($detallado=='no')
    {
		$sqll=mysqli_query($con,"SELECT cott_art_id FROM dis_cotizacion_detalle_temp WHERE cott_art_id='$art_id'and distribuidor='$distribuidor' and detallado='no'") ;
        if($roww=mysqli_fetch_array($sqll))
            { 
                $can7=mysqli_query($con,"SELECT * FROM dis_cotizacion_detalle_temp WHERE cott_art_id='$art_id' and distribuidor='$distribuidor'and detallado='no'") or die(mysqli_error($con));  
                    while($dato=mysqli_fetch_array($can7))
                    {
                    	
                        $cott_art_id=$dato['cott_art_id'];            
                        $c=$dato['cott_cantidad'];
                        $suma=$c+$cantidad;
                       
                    }
                
               $registros=mysqli_query($con,"UPDATE dis_cotizacion_detalle_temp Set cott_cantidad='$suma',cott_precio='$precio_venta' Where cott_art_id='$art_id' and distribuidor='$distribuidor' and detallado='no'") or die(mysqli_error($con));

            }
            else
            {   
               $sql=mysqli_query($con,"INSERT INTO dis_cotizacion_detalle_temp (cott_art_id,cott_referencia,cott_nombre,cott_cantidad,cott_iva,cott_desc,cott_precio,detallado,distribuidor) 
                VALUES ('$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','$detallado','$distribuidor')") or  die(mysqli_error($con));
            }
    }  
    else
    {
    	$sqll=mysqli_query($con,"SELECT cott_art_id FROM dis_cotizacion_detalle_temp WHERE cott_art_id='$art_id' and distribuidor='$distribuidor' and detallado='si'") ;
        if($roww=mysqli_fetch_array($sqll))
            { 
                $can7=mysqli_query($con,"SELECT * FROM dis_cotizacion_detalle_temp WHERE cott_art_id='$art_id' and distribuidor='$distribuidor'  and detallado='si'") or die(mysqli_error($con));  
                    while($dato=mysqli_fetch_array($can7))
                    {
                    	
                        $cott_art_id=$dato['cott_art_id'];            
                        $c=$dato['cott_cantidad'];
                        $suma=$c+$cantidad;
                    }
               $registros=mysqli_query($con,"UPDATE dis_cotizacion_detalle_temp Set cott_cantidad='$suma',cott_precio='$precio_venta' Where cott_art_id='$art_id' and distribuidor='$distribuidor'  and detallado='si'") or die(mysqli_error($con));
            }
            else
            {   
               $sql=mysqli_query($con,"INSERT INTO dis_cotizacion_detalle_temp (cott_art_id,cott_referencia,cott_nombre,cott_cantidad,cott_iva,cott_desc,cott_precio,usuario,detallado,distribuidor) 
                VALUES ('$art_id','$art_referencia','$nombre','$cantidad','$art_iva','$descuento','$precio_venta','$detallado','$distribuidor')") or  die(mysqli_error($con));
            }
    }     

}
?>

<table class="table table-bordered table-hover table-striped">
<tr>
	<th width="7%">CODIGO</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
	$neto=0;
  $descuentof=0;
  $sumador_total=0;
  $iva_total=0;
  $total_ret=0;

   

	
	$sql=mysqli_query($con, "SELECT * from  dis_cotizacion_detalle_temp where distribuidor='$distribuidor' ");
	while ($row=mysqli_fetch_array($sql))
	{
	$id=$row["cott_id"];
	$cott_id=$row["cott_id"];
	$cott_referencia=$row['cott_referencia'];
  $cott_nombre=utf8_encode($row['cott_nombre']);
	$cott_cantidad=$row['cott_cantidad'];
	$cott_precio=$row['cott_precio'];
	$cott_iva=$row['cott_iva'];
	$cott_descuento=$row['cott_desc'];

	$iva0=$cott_iva/100; //0.19
  $iva1=1+$iva0; //1.19
  
  $precio_u_sin_iva=$cott_precio/$iva1;
  $precio_unidad=$precio_u_sin_iva;

  $precio_t_sin_iva=$precio_unidad*$cott_cantidad;


  $descuento=($cott_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 $cott_referencia;?></td>
			<td><?php echo $cott_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 $cott_nombre;?></strong></a> 
			</td>	
			<?php } else { ?>
 			<td><?php echo $cott_nombre;?></td>
			<?php } ?>
			
			<td><span class="pull-right"><?php echo number_format($precio_unidad,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_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["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?");
			if(p){
				$.get("./php_cotizacion/eliminar.php","cott_id="+<?php echo $row["cott_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-$total_ret,0);?></strong></span></td>
	<td></td>
</tr>

</table>



  <!-- Modal -->
  <script>
  	$(".btn-edit").click(function(){
  		id = $(this).data("id");
  		$.get("./php_cotizacion/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/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 -->

