<?php


require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();

require_once ("../formatos/formato.php");

$dts= $oVenta->mostrarUno($_POST['ven_id']);
$dt = mysqli_fetch_array($dts);
  $fec	=mostrarFecha($dt['tb_venta_fec']);
  $doc_id	=$dt['tb_documento_id'];
  $numdoc	=$dt['tb_venta_numdoc'];
  $cli_id	=$dt['tb_cliente_id'];

  $valven	=$dt['tb_venta_valven'];
  $igv	=$dt['tb_venta_igv'];
  $des	=$dt['tb_venta_des'];
  $tot	=$dt['tb_venta_tot'];
  $igvval	=$dt['tb_venta_igvval'];
  $igvsub	=$dt['tb_venta_igvsub'];
  $igvigv	=$dt['tb_venta_igvigv'];
  $estsun	=$dt['tb_venta_estsun'];
  
mysqli_free_result($dts);

$dts1=$oVenta->mostrar_venta_detalle($_POST['ven_id']);
$num_rows= mysqli_num_rows($dts1);

?>
 
 
 <style>
	#tabla_venta_detalle td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_venta_detalle td.editable,#tabla_venta_detalle td.editable *{
		cursor: text !important;
		height: 100%; 
		border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_venta_detalle td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_venta_detalle td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_venta_detalle td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
	}
	#tabla_venta_detalle td input[readonly]{
		background: transparent;
	}
</style> 

 
<div class="table-responsive">
<table cellspacing="1" id="tabla_venta_detalle" class="table table-bordered table-striped"> 
 
    <thead>
        <tr>
        <th class="" >CÓDIGO</th>
      <!--  <th class="" >ESTADO</th>
        -->
		<th style="white-space: nowrap;">ARTÍCULO</th>
        <th class="" style="white-space: nowrap;" >CATEGORÍA | LABORATORIO</th>
        <th>EMPAQUE</th>
        <th align="right">CANT</th>
		<th align="right" >V. UNI.</th>		

		<th align="right" >IGV UNI.</th>
		<th align="right" nowrap >P. UNI.</th>
		<th align="right" >VAL. TOTAL</th>
	
		<th align="right" >IGV TOTAL.</th>
		<th align="right" >PRECIO TOTAL.</th>
		
        </tr>
    </thead>
    <tbody>
    <?php
		while($dt1 = mysqli_fetch_array($dts1))
    {
		$can = $dt1['tb_ventadetalle_can'] * 1;
		$can1 = $dt1['tb_ventadetalle_can'] * 1;
		$pre_uni=$dt1['tb_ventadetalle_preuni'];
		$des_uni = $dt1['tb_ventadetalle_des'];
		
			$dts2=$oCatalogo->mostrarUno($dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
			$dt2 = mysqli_fetch_array($dts2);
			
			$arttip_nom =  $dt2['tb_articulotipo_nom'];
			$emp_fac =  $dt2['tb_empaque_fac'];
			
			if($can>=$emp_fac){
				$can = $can/$emp_fac;
				$canvis =$can;
			}else{
				$can = $can;
				$canvis = 'F'.$can;

			}
		
		$valor_uni = $pre_uni / $igvsub;
		$valor_uni = formato_money4($valor_uni);
		$igv_uni = $valor_uni * $igvigv;
			
		$val_tot = $valor_uni * $can;
		$des_tot = $des_uni * $can;
		$igv_tot = $igv_uni * $can;
		$pre_tot = $val_tot + $igv_tot;
		
		
		$est = $dt1['tb_ventadetalle_est'];
		
			

			
			
		?>
        <tr style="font-weight:bold" >
          <td><?php echo $dt1['tb_catalogo_cod'];?></td>
        <!--  <td>
		  <?php if($est==1){
			echo '<strong class="text-blue">ENTREGADO</strong>';  
		  }else{?>
		  <select class="form-control cmb_estado text-red" onchange="cambiar_estado('<?php echo $dt1['tb_ventadetalle_id']; ?>');" >
		  <option value="1">ENTREGADO</option>
		  <option value="2" selected >POR ENTREGAR</option>		  
		  </select>
		  <?php } ?>
		  </td>-->
          <td><?php  echo '<strong>'.$dt1['tb_ventadetalle_nom'].'</strong>';?></td>
          <td><?php echo $dt1['tb_categoria_nom'].' | '.$dt1['tb_marca_nom'];?></td>
          <td><?php echo $dt1['tb_empaque_nom']?></td>
		  <td align="right"><?php echo $canvis;?></td>
		  
          <td class="" align="right"><?php echo formato_money4($valor_uni);?></td>

          <td class="" align="right"><?php echo formato_money($igv_uni);?></td>
        	<?php if($estsun==1){
			?>
			<td class="" align="right"><?php echo formato_money4($pre_uni);?></td>
			<?php 
			}else{?>
		  <td nowrap align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo formato_money4($pre_uni) ?>"  data-original="<?php echo formato_money($pre_uni) ?>" 
				data-vendet_id="<?php echo $dt1['tb_ventadetalle_id']; ?>" readonly class="" style="text-align: right;font-size:1rem;">
			</td>
			<?php }?>
		  
          <td class="" align="right"><?php echo formato_money($val_tot);?></td>

          <td class="" align="right"><?php echo formato_money($igv_tot);?></td>
          <td class="" align="right"><?php echo formato_money($pre_tot);?></td>
        </tr>
    <?php						
   
		$totpre += $pre_tot;
		$totigv += $igv_tot;
		$totval += $val_tot;
	
	}
	
	
    mysqli_free_result($dts1);
    ?>
    </tbody>
	
	 <tr style="font-weight:bold" >
          <td colspan="4"></td>
          <td colspan="4" align="center">
		  <!--	<span class="btn btn-primary btn-sm" id="btn_acttot">ACTUALIZAR TOTALES</span>
		  -->
		  </td>
        
         
		  
          <td class="" align="right"><?php echo formato_money($totval);?></td>

          <td class="" align="right"><?php echo formato_money($totigv);?></td>
          <td class="" align="right"><?php echo formato_money($totpre);?></td>
        </tr>
	
	
</table>

<?php 
  if($num_rows=="" or $num_rows==0)echo $num_rows.' Ningún registro';
  if($num_rows==1)echo $num_rows.' registro';
  if($num_rows>=2)echo $num_rows.' registros';
?>

<br/>
</div> 
  
  
<div class="table-responsive">


<table>

		<tr>
					<td>
						<div style="width:150px;" >
								<label>Operacion Gravada:</label>
								<input type="text"  autocomplete="off"  class="form-control text-orange" value="<?php echo formato_money($valven); ?>" style="text-align:right;font-weight: bold;" readonly >				
							</div>
					</td>	
					<td style="width:5%">
					</td>
					<td>
							<div >
								<label>IGV:</label>
						<input type="text" autocomplete="off" class="form-control text-orange" value="<?php echo formato_money($igv)?>"  style="text-align:right;font-weight: bold;" readonly> 
							</div>
					</td>
					<td style="width:5%">
					</td>							
					<td>
							<div >
								<label>TOTAL:</label>
								<input type="text"  autocomplete="off" class="form-control text-orange" value="<?php echo formato_money($tot)?>"  style="text-align:right;font-weight: bold;" readonly >
							</div>
							
					</td>	
			</tr>
			</table>
</div>			


<script type="text/javascript">
  
  function cambiar_estado(detid){
  
		$.ajax({
			type: "POST",
			url: "../venta/venta_reg.php",
			async:true,
			dataType: "json",                      
			data: ({
				action_venta:	'cambiar_estado',
				detid:	detid
			}),
			beforeSend: function() {
				
			},
			success: function(data){
				toastr["success"](data.msj);	
			},
			complete: function(){			
				venta_detalle_tabla_vis(<?php echo $_POST["ven_id"]?>);
			}
		});     
  
  }
  
  
  
  
  
  
  
  
  
  
  $(function() {

  
		$('#tabla_venta_detalle td input').keydown(function(ev) {
			if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			
			var vendet_id = $(this).attr('data-vendet_id');
			var preuni = $(this).val();
			
			//alert(preuni);
		
				actualizar_precio('actualizarprecio', vendet_id,preuni);
			
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});

		$('#tabla_venta_detalle td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});

		$('#tabla_venta_detalle td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});

		$('#tabla_venta_detalle td input').keydown(function(ev) {
			if(ev.keyCode==27){
				$(this).val($(this).attr('data-original'));
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
			}
		});



	}); 
	
	
	
  
  
  
  
  
  
  
  
  </script>
  
  
  
  
  
  
  
  
  
  
  