<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("../compra/cCompra.php");
$oCompra = new cCompra();
require_once ("../formatos/formato.php");
	$dts= $oCompra->mostrarUno($_POST['com_id']);
	$dt = mysqli_fetch_array($dts);
	$fec	=mostrarFecha($dt['tb_compra_fec']);
	$doc	=$dt['tb_documento_abr'];
	$numdoc	=$dt['tb_compra_numdoc'];
	
	
$dts1=$oCompra->mostrar_compra_detalle($_POST['com_id']);
$num_rows= mysqli_num_rows($dts1);
?>
<section class="content">
	<div class="container-fluid">
	<div class="row">
	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
		    <div class="card">
				
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:5px;">
					<div class="table-responsive">
		
		<strong style="font-size:2rem;" >DETALLES DE LA COMPRA : </strong> <strong style="font-size:2rem;" class="text-red" ><?php echo $doc.' '.$numdoc; ?></strong>
		
		<style>
	#tabla_compra_detalle td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_compra_detalle td.editable,#tabla_compra_detalle td.editable *{
		cursor: text !important;
		height: 100%; 
		 border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_compra_detalle td.editando{
		background-color: rgba(18, 180, 243, 0.05);
			outline: 2px solid #35A4F3;
	}
	#tabla_compra_detalle td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_compra_detalle td input:focus{
		background: transparent;
			height: 100%;
			width: 100%;
			outline: none;
			border: none;
	}
	#tabla_compra_detalle td input[readonly]{
		background: transparent;
	}
</style>
            
<table cellspacing="1" id="tabla_compra_detalle" class="table table-bordered table-striped"> 
    <thead>
        <tr >
        
		<th class="" >CANTIDAD</th>
		<th class="" >COD. BAR.</th>
        <th class="" >CÓDIGO</th>
        <th style="white-space: nowrap;">ARTÍCULO</th>
        <th style="white-space: nowrap;" >MARCA</th>
        <th>UNIDAD</th>
        <th align="right">CANT</th>
		<th align="right" >V. UNI.</th>		
		<th align="right" >DES. UNI.</th>
		<th align="right" >IGV UNI.</th>
		<th align="right" >P. UNI.</th>
		<th align="right" >VAL. TOTAL</th>
		<th align="right" >DESC. 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_compradetalle_can'];
		$valor_uni=$dt1['tb_compradetalle_preuni'];
		$des_uni = $dt1['tb_compradetalle_des'];
		$val_unicondes = $valor_uni-$des_uni;
		$igv_uni = $val_unicondes *0.18;
		$pre_uni=($valor_uni-$des_uni)+$igv_uni;
			
		$val_tot = $valor_uni * $can;
		$des_tot = $des_uni * $can;
		$igv_tot = $igv_uni * $can;
		$pre_tot = $pre_uni * $can;
			
		/* 	$rscod = $oProductomarcacodigo->mostrarUno($dt1['tb_catalogo_id'],'1');
			$dtcod = mysqli_fetch_array($rscod);
			
			$codpri = $dtcod['tb_productomarcacodigo_cod']; */
		
		
	?>
	<!--
	title="<?php echo ' PRECIO DE '.$dt1['tb_unidad_nom'].' ES '.$dt1['tb_compradetalle_preuni']; ?>"
      -->
      <tr >
        <td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
			<input type="text" value="<?php echo $cantidad ?>" data-valuni="<?php echo $valor_uni ?>" readonly 
			data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>"
			data-can="<?php echo $can ?>" class="text-white" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
		</td>
		  <td class="" ><strong class="text-primary"> <?php echo $dt1['tb_compradetalle_codbar'];?> </strong></td>
		  <td><?php echo $dt1['tb_catalogo_cod']; ?></td>
		   
          <td style="white-space: nowrap;"><?php  echo '<strong>'.$dt1['tb_producto_nom'].'</strong>';?></td>
          <td class="" style="white-space: nowrap;"><?php echo $dt1['tb_marca_nom'];?></td>
          <td><?php echo $dt1['tb_unidad_nom']?></td>
          <td align="right"><?php echo $can;?></td>
		  
          <td class="" align="right"><?php echo formato_money($valor_uni);?></td>
          <td class="" align="right"><?php echo formato_money($des_uni);?></td>
          <td class="" align="right"><?php echo formato_money($igv_uni);?></td>
          <td class="" align="right"><?php echo formato_money($pre_uni);?></td>
          <td class="" align="right"><?php echo formato_money($val_tot);?></td>
          <td class="" align="right"><?php echo formato_money($des_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
    }
    mysqli_free_result($dts1);
    ?>
  </tbody>
</table>
          
		  </div>
        </div>
		
	<div  id="div_itemsadevol" ></div>
		
	
	<div style="height:15px;" class="col-lg-12 col-md-12 col-xs-12 " ></div>
	
	<span class="btn btn-primary" style="display:none;cursor:pointer" onclick="devolucion_form('insertar',<?php echo $_POST['com_id']; ?>)" id="btn_generar" > GENERAR</span>
	
	
        </div>
        </div>
      </div>
    </div>
  </section>
  
   <script type="text/javascript">
  $(function() {  
  
  $('#tabla_compra_detalle td input').keydown(function(ev) {
		if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			
			var catid = $(this).attr('data-cat_id');
			var marid = $(this).attr('data-mar_id');
			var valuni = $(this).attr('data-valuni');
			var canc = $(this).attr('data-can');
			var can = $(this).val();
			
			agregar_producto('agregar',catid,marid,can, valuni,canc);
			
			$(this).val('');
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');			
			return false;
		}
	});
	$('#tabla_compra_detalle td input').parent().click(function(ev) {
		var $input = $(this).find('input');
		var $td = $input.parent().addClass('editando');
		$input.removeAttr('readonly').focus().select();
	});
	$('#tabla_compra_detalle td input').blur(function(event) {
		$(this).val('');
		$(this).attr('readonly', 'true');
		$(this).parent().removeClass('editando');
	});
	$('#tabla_compra_detalle td input').keydown(function(ev) {
		if(ev.keyCode==27){
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		}
	});
 
  
    $("#tabla_compra").DataTable( {
      paging: true,
	  searching: false,
	  ordering:false,
	  "columnDefs": [
        { "orderable": true, "targets": [0] }
      ],
    });
  }); 
</script>  