<?php
//ini_set('display_errors',1);

require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();

if($_POST['com_id']=="")
{
session_start();
require_once ("../../config/Cado.php");
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once ("../formatos/formato.php");
if(isset($_SESSION['compra_car']))
{
	$num_rows=count($_SESSION['compra_car']);
	if($num_rows==0)$num_rows="";
	foreach($_SESSION['compra_car'] as $indice=>$cantidad){
		$total_cantidad+=$cantidad;
	}
}
else
{
	$num_rows="0";
}

$com_igvval  = $_POST['igv'];
$com_igvsub  = "1.".$_POST['igv'];
$com_igvigv  = "0.".$_POST['igv'];

?>
 


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

<table cellspacing="1" id="tabla_compra_car" class="table table-bordered table-striped">
	<thead>
		<tr>
			<th >CÓDIGO</th>
			<th >ARTÍCULO</th>
			<th >MARCA</th>
			<th>EMPAQUE</th>
			<th align="right">CAN</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>
			<th>&nbsp;</th>
		</tr>
	</thead>

	<?php if($num_rows>0){ ?>
	<tbody>
	<?php
	foreach($_SESSION['compra_car'] as $indice=>$cantidad)
	{
			
		$dts1=$oCatalogo->mostrarUno($_SESSION['compra_catid'][$indice],$_SESSION['compra_linea_mar'][$indice]);
		$dt1 = mysqli_fetch_array($dts1);

		$can=$cantidad;
		
		list($cane,$canf) = explode('F',$can);
		
		//echo $cane.'--'.$canf;
		if($cane==""){$can = $canf;}
		if($canf==""){ $can = $cane;}
	
		
		
		//$can = $cane;
		
		$des_uni = $_SESSION['compra_linea_des'][$indice];
		$valor_uni=$_SESSION['compra_linea_precom'][$indice]-$des_uni;
		
		$igv_uni = $valor_uni * $com_igvigv;
		$pre_uni=$valor_uni+$igv_uni;
			
		$val_tot = ($valor_uni) * $can;
		$des_tot = $des_uni * $can;
		$igv_tot = ($igv_uni) * $can;
		$pre_tot = ($pre_uni) * $can;
			

		$val_tot = (abs($valor_uni)) * $can;
		$des_tot = ($des_uni) * $can;
		$igv_tot = (abs($igv_uni)) * $can;
		$pre_tot = (abs($pre_uni)) * $can;
		
			
			$total_valorventa +=$val_tot;
			$total_igv	+=$igv_tot;
			$total	+=$pre_tot;
			
			
			
			$rscod = $oProductomarcacodigo->mostrarUno($dt1['tb_catalogo_id'],'1');
			$dtcod = mysqli_fetch_array($rscod);
			
			$codpri = $dtcod['tb_productomarcacodigo_cod'];
			

	?>

		<tr >
			
			<td class="" ><?php echo $codpri; ?></td>
			<td  style="dark-space: nowrap; font-size: 9pt; font-weight: bold;"><?php echo $dt1['tb_producto_nom']?></td>
			<td class="" ><?php echo $dt1['tb_marca_nom']?></td>
			<td><?php echo $dt1['tb_empaque_nom']?></td>

			<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo $cantidad ?>" data-cantidad="<?php echo $cantidad ?>" 
				data-can="<?php echo $can ?>" 
				data-original="<?php echo $cantidad ?>" readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>"
				data-tipdes="<?php echo $tipdes ?>" class="text-dark" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" style="text-align: right;">
			</td>

			<td align="right" width="80" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-preven="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-original="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-tipdes="<?php echo $tipdes ?>" 
				data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 
				data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="text-dark" style="text-align: right;">
			</td>
			<!--
			<td align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold">
							<input type="text" value="<?php echo formato_money(abs($des_uni)) ; ?>" 
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							data-tipdes="<?php echo $tipdes ?>"
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-original="<?php echo formato_money(abs($des_uni)) ?>" readonly 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="text-dark" style="text-align: right;">
			</td>
			-->
			<td class="" align="right"><?php echo formato_money(abs($igv_uni))?></td>
						
			<td align="right" width="80" class="editable" style="font-size:9pt; font-weight:bold">
							<input type="text" class="text-dark" value="<?php echo formato_money(abs($pre_uni)) ; ?>" 
							data-original="<?php echo formato_money(abs($pre_uni)) ?>" 
							data-cantidad="<?php echo $cantidad ?>" data-tip = "preuni" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							readonly 
							style="text-align: right;">
			</td>
			
			
			<td  align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold" >
			
		
			<input type="text" class="text-orange" value="<?php echo formato_money(abs($val_tot)) ; ?>" 
							data-original="<?php echo formato_money(abs($val_tot)) ?>" 
							data-cantidad="<?php echo $cantidad ?>" data-tip = "valtot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							readonly 
							style="text-align: right;">
			
			
			</td>
			<!--
			<td width="80" align="right"> <strong class="text-orange"><?php echo formato_money(abs($des_tot))?> </strong> </td>
			-->
			<td align="right" width="70" style="font-size: 9pt; font-weight: bold;"> 
			<strong class="text-orange"> <?php echo formato_money(abs($igv_tot))?> </strong> </td>
			
			<td align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold">
							<input type="text" class="text-orange" value="<?php echo formato_money(abs($pre_tot)) ; ?>" 
							data-original="<?php echo formato_money(abs($pre_tot)) ?>" 
							data-cantidad="<?php echo $cantidad ?>" data-tip = "pretot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							readonly 
							style="text-align: right;">
			</td>
			

			<td align="center" nowrap="nowrap">
			<span style="cursor:pointer;" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" class="btn_eliminar btn btn-danger btn-sm " ><i class="fas fa-trash"></i></span>
		</tr>
	<?php
		mysqli_free_result($dts1);
	}	
	?>
	</tbody>
	<?php
	}
	else
	{
	?>
		<tr>
			<td colspan="14">&nbsp;</td>
		</tr>
	<?php
	}
	?>
</table>
	
<?php echo $num_rows." Registros.";?> 


<br />
<br />

</div> 

 <input type="hidden" name="txt_com_opeina" autocomplete="off"  class="form-control" id="txt_com_opeina" value="<?php echo formato_money($total_valorventa)?>" style="text-align:right;font-weight: bold;" >
 <input type="hidden" name="txt_com_opeexo" autocomplete="off"  class="form-control" id="txt_com_opeexo" value="<?php echo formato_money($total_valorventa)?>" style="text-align:right;font-weight: bold;" >
 <input type="hidden" name="txt_com_valven" autocomplete="off"  class="form-control" id="txt_com_valven" value="<?php echo formato_money($total_valorventa)?>" style="text-align:right;font-weight: bold;" >


		<div class="col-lg-12 col-sm-12 col-12">
							<div class="row">

							<div class="col-lg-2 col-sm-6 col-6">
								<label>Operacion Gravada:</label>
								<input name="txt_com_opegra" type="text"class="form-control" id="txt_com_opegra" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_valorventa)?>"  > 							
							</div>
						
							<div class="col-lg-2 col-sm-6  col-6">
								<label>IGV:</label>
								<input name="txt_com_igv" type="text" class="form-control" id="txt_com_igv" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_igv)?>"   > 
							</div>
							<div class="col-lg-2 col-sm-6  col-6">
								<label>DESCUENTO:</label>
								<input name="txt_com_des" type="text" class="form-control" id="txt_com_des" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($des_tot)?>"   > 
							</div>
							<div class="col-lg-2 col-sm-6  col-6">
								<label>TOTAL:</label>
								<input name="txt_com_tot" type="text" class="form-control" id="txt_com_tot" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total)?>" > 
							</div>
							
							</div>
		</div>
					
					
					



<script type="text/javascript" src="../cotizacion/NumberFormat.js"></script>

<script> 




$("#txt_com_tot").keydown(function(e){
			if (e.which == 13){
				verpago_total();
				return false;
				}
			});
			
	$("#txt_com_opegra").keydown(function(e){
			if (e.which == 13){
				verpago_gra();
				return false;
				}
			});

function verpago_total(){
	
		tot =  $("#txt_com_tot").val() ;
		valven = tot / 1.18;
		gra = tot / 1.18;
		igv =  tot*(0.18)/1.18;
	
	
				$("#txt_com_opegra").val(new NumberFormat(gra).toFormatted());
				
				$("#txt_com_valven").val(new NumberFormat(valven).toFormatted());
				$("#txt_com_igv").val(new NumberFormat(igv).toFormatted());				
				$("#txt_compag_mon").val(new NumberFormat(tot).toFormatted());
				$("#txt_com_tot").val(new NumberFormat(tot).toFormatted()); 
				
				$('#tabproveedor .nav-tabs a:last').tab('show'); 
}

function verpago_gra(){
	
		
	
		gra = $("#txt_com_opegra").val();
		igv =  gra*(18)/100;
		
				$("#txt_com_opegra").val(new NumberFormat(gra).toFormatted());				
				$("#txt_com_valven").val(new NumberFormat(gra).toFormatted());
				$("#txt_com_igv").val(new NumberFormat(igv).toFormatted());			
				
				tot = parseFloat(igv) + parseFloat(gra);
	
				
				$("#txt_compag_mon").val(new NumberFormat(tot).toFormatted());
				$("#txt_com_tot").val(new NumberFormat(tot).toFormatted()); 
				
				$('#tabproveedor .nav-tabs a:last').tab('show'); 
}


<?php if($num_rows>0){ ?>
$("#hdd_com_numite").val(<?php echo $num_rows?>)
$("#hdd_com_numite").change(); 
//$('#cmb_mone_id').attr('disabled','disabled');

$('#txt_comapp_igv').attr('disabled','disabled');
$('#tabproveedor .nav-tabs a:last').tab('show'); 
	
<?php  }else {?>
$('#txt_comapp_igv').removeAttr("disabled");
//$('#cmb_mone_id').removeAttr("disabled");
	<?php 
}?>


	$('#tabla_compra_car td input').keydown(function(ev) {
		if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			 ev.preventDefault();
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var cantidad = $(this).parent().parent().find('input[data-cantidad]').val();
			var can = $(this).parent().parent().find('input[data-cantidad]').attr('data-can');
			var preven = $(this).parent().parent().find('input[data-preven]').val();
			//var des = $(this).parent().parent().find('input[data-des]').val();
			var des = 0;
			var tipdes = $(this).attr('data-tipdes');
			var tip = $(this).attr('data-tip');
			var valor = $(this).val();
		
			if(tip=="pretot"){
				
			preuni = valor/can;	
			valuni = preuni / <?php echo $com_igvsub; ?>;
			
			agregar_producto('agregar', cat_id, cantidad, valuni,mar_id,des);
			
			}else if(tip=="preuni"){
				
			preuni = valor;	
			valuni = preuni / <?php echo $com_igvsub; ?>;
			
			agregar_producto('agregar', cat_id, cantidad, valuni,mar_id,des);
			
			}else if(tip=="valtot"){

			valuni =  valor/can;
			
			agregar_producto('agregar', cat_id, cantidad, valuni,mar_id,des);
			
			}else{
				
				agregar_producto('agregar', cat_id, cantidad, preven,mar_id,des);
			}
			
			//$('html, body').animate({scrollTop:900}, 'slow');
			
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		}
	});

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

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

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

	 
			$('.btn_editar').click(function(event) {
				event.preventDefault();
				var $input = $(this).parent().parent().find('input[data-cantidad]');
				var $td = $input.parent().addClass('editando');
				$input.removeAttr('readonly').focus().select();
			});

			/* $('#txt_com_valven, #txt_com_igv, #txt_com_tot').blur(function(){
				verpago();				
			}); */
			
			$("#txt_com_valven").keydown(function(e){
			if (e.which == 13){
				$("#txt_com_igv").focus();
				//verpago();
				return false;
				}
			});
			
			$("#txt_com_igv").keydown(function(e){
			if (e.which == 13){
				$("#txt_com_tot").focus();
			//	verpago();
				return false;
				}
			});
			
			$("#txt_com_tot").keydown(function(e){
			if (e.which == 13){
				$("#txt_com_tot").focus();
				//verpago();
				return false;
				}
			});
	 

	$('.btn_eliminar').click(function(event) {
				var cat_id = $(this).parent().parent().find('input').attr('data-cat_id');
				var mar_id = $(this).attr('data-mar_id');
				indice = cat_id+''+mar_id;

			//	alert(indice);
				if(confirm('¿Seguro de quitar el item?')){
					agregar_producto('quitar', indice);
				}
			});


		function cambiartides(t){
			var cat_id = $(t).attr('data-cat_id');
			var cantidad = $(t).parent().parent().find('input[data-cantidad]').val();
			var preven = $(t).parent().parent().find('input[data-preven]').val();
			var des = $(t).parent().parent().find('input[data-des]').val();
			var tipdes = $(t).val();
			var mar_id = $(t).attr('data-mar_id');
			
			agregar_producto('agregar', cat_id, cantidad, preven,mar_id,des,tipdes);

		}



</script>


<?php

}else{
	
require_once ("../../config/Cado.php");
require_once ("cCompra.php");
$oCompra = new cCompra();
require_once ("../kardex/cKardex.php");
$oKardex = new cKardex();

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

	$dts= $oCompra->mostrarUno($_POST['com_id']);
	$dt = mysqli_fetch_array($dts);
		$fec	=mostrarFecha($dt['tb_compra_fec']);
		$doc_id	=$dt['tb_documento_id'];
		$numdoc	=$dt['tb_compra_numdoc'];
		$pro_id	=$dt['tb_proveedor_id'];
		$subtot	=$dt['tb_compra_subtot'];
		$des	=$dt['tb_compra_des'];
		$descal	=$dt['tb_compra_descal'];
		$fle	=$dt['tb_compra_fle'];
		$tipfle	=$dt['tb_compra_tipfle'];
		$ajupos	=$dt['tb_compra_ajupos'];
		$ajuneg	=$dt['tb_compra_ajuneg'];
		$opeina	=$dt['tb_compra_opeina'];
		$opeexo	=$dt['tb_compra_opeexo'];
		$opegra	=$dt['tb_compra_opegra'];
		$valven	=$dt['tb_compra_valven'];
		$igv	=$dt['tb_compra_igv'];
		$tot	=$dt['tb_compra_tot'];
		
		$tipper	=$dt['tb_compra_tipper'];
		$per	=$dt['tb_compra_per'];
		$com_igvval	=$dt['tb_compra_igvval'];
		$com_igvsub =$dt['tb_compra_igvsub'];
		$com_igvigv =$dt['tb_compra_igvigv'];
		$per	=$dt['tb_compra_per'];
		
		$alm_id	=$dt['tb_almacen_id'];
		$est	=$dt['tb_compra_est'];
	mysqli_free_result($dts);

$dts1=$oCompra->mostrar_compra_detalle($_POST['com_id']);
$num_rows= mysqli_num_rows($dts1);

if($num_rows>0){
?>


<script type="text/javascript">

	$(function() {
		$('#txt_com_des,#txt_com_descal,#txt_com_fle,#cmb_com_tipfle,#txt_com_subtot,#txt_com_ajupos,#txt_com_ajuneg,#txt_com_valven,#txt_com_igv,#txt_com_tot').attr('disabled', 'disabled');
	});
 
</script>



<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>
		
<div class="table-responsive">

	   
<table cellspacing="1" id="tabla_compra_detalle" class="table table-bordered table-striped"> 
    <thead>
        <tr >
        
		<th class="" >COD. BAR.</th>
        <th class="" >CÓDIGO</th>
        <th style="dark-space: nowrap;">ARTÍCULO</th>
        <th class="" style="dark-space: nowrap;" >MARCA</th>
        <th>EMPAQUE</th>
        <th align="right">CAN</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 * $com_igvigv;
		$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']; */
		
		
		
			$rskd = $oKardex->mostrar_kardexdetalle_x_modulo('4',$_POST['com_id'],$dt1['tb_catalogo_id'],$dt1['tb_marca_id']);
			$dtkd = mysqli_fetch_array($rskd);
			
			$kardet_id = $dtkd['tb_kardexdetalle_id']; 
			
	?>
	
      <tr >
          <td class=" hidden-sm" ><strong class="text-primary"> <?php echo $dt1['tb_compradetalle_codbar'];?> </strong></td>
		   <td><?php echo $dt1['tb_catalogo_cod']; ?></td>
		   
          <td style="dark-space: nowrap;"><?php  echo '<strong>'.$dt1['tb_producto_nom'].'</strong>';?></td>
          <td class="" style="dark-space: nowrap;"><?php echo $dt1['tb_marca_nom'];?></td>
          <td><?php echo $dt1['tb_empaque_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>
		  
		  <?php if($pre_uni>0){
			 ?>
			  <td class="" align="right"><?php echo formato_money($pre_uni);?></td>
			 <?php 
		  }else {?>
	
			<td align="right" width="80" class="editable" style="font-size:9pt; font-weight:bold">
							<input type="text" class="text-red" value="<?php echo formato_money(abs($pre_uni)) ; ?>" 
							data-original="<?php echo formato_money(abs($pre_uni)) ?>" 						
							data-kardet_id="<?php echo $kardet_id ?>" 						
							data-comdet_id="<?php echo $dt1['tb_compradetalle_id'];?>" 
							data-cantidad="<?php echo $can;?>"							
							readonly data-tip = 'preuni'
							style="text-align: right;">
			</td>
		  <?php }?>
		  
		  
          <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>      
         
		  <?php if($pre_tot>0){
			 ?>
			  <td class="" align="right"><?php echo formato_money($pre_tot);?></td>
			 <?php 
		  }else {?>
	
			<td align="right" width="80" class="editable" style="font-size:9pt; font-weight:bold">
							<input type="text" class="text-red" value="<?php echo formato_money(abs($pre_tot)) ; ?>" 
							data-original="<?php echo formato_money(abs($pre_tot)) ?>" 	data-tip = 'pretot'					
							readonly 
							data-kardet_id="<?php echo $kardet_id ?>"
							data-comdet_id="<?php echo $dt1['tb_compradetalle_id'];?>"
							data-cantidad="<?php echo $can;?>"
							style="text-align: right;">
			</td>
		  <?php }?>
		 
		 
      </tr>
    <?php
    }
    mysqli_free_result($dts1);
    ?>
  </tbody>
</table>



<script type="text/javascript">
	
<?php if($num_rows>0){
	?>
	$("#fieldset_producto").hide();
	<?php 
}?>

</script>






<script type="text/javascript" src="../cotizacion/NumberFormat.js"></script>

<script> 

<?php if( $num_rows > 0){ ?>
$("#hdd_com_numite").val(<?php echo $num_rows?>)
$("#hdd_com_numite").change(); 
//$('#cmb_mone_id').attr('disabled','disabled');
<?php  }else {?>
//$('#cmb_mone_id').removeAttr("disabled");
	<?php 
}?>

var nFilas = $("#hdd_com_numite").val();
  if(nFilas>0){
  	$('#tabproveedor .nav-tabs a:last').tab('show'); 
  }




	$('#tabla_compra_detalle td input').keydown(function(ev) {
		if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			 ev.preventDefault();
			
			var tip = $(this).attr('data-tip');
			var kardet_id = $(this).attr('data-kardet_id');
			var comdetid = $(this).attr('data-comdet_id');
			var cantidad = $(this).attr('data-cantidad');
			var valor = $(this).val();
		
			//alert(tip);
		
			if(tip=="pretot"){
				
			preuni = valor/cantidad;	
			valuni = preuni / 1.18;
			
			agregar_producto_editar('actualizardetalle',comdetid, kardet_id, cantidad, valuni);
			
			}else if(tip=="preuni"){
				
			preuni = valor;	
			valuni = preuni / 1.18;
			
			agregar_producto_editar('actualizardetalle',comdetid, kardet_id, cantidad, valuni);
			
			}else{
				// agregar_producto('agregar', cat_id, cantidad, preven,mar_id,des);
			}
			
			
			
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		}
	});

	$('#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('data-original'));
		$(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');
		}
	});


</script>










<input type="hidden" name="numrow_items" id="numrow_items" value="<?php echo $num_rows;?>">
<?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> 

<?php }else{
	
	
	

session_start();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();

if(isset($_SESSION['compra_car']))
{
	$num_rows=count($_SESSION['compra_car']);
	if($num_rows==0)$num_rows="";
	
	foreach($_SESSION['compra_car'] as $indice=>$cantidad){
		$total_cantidad+=$cantidad;
	}
}
else
{
	$num_rows="0";
}
?>
 


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

<table cellspacing="1" id="tabla_compra_car" class="table table-bordered table-striped">
	<thead>
		<tr>
			<th >CÓDIGO</th>
			<th >ARTÍCULO</th>
			<th >MARCA</th>
			<th>UNIDAD</th>
			<th align="right">CAN</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>
			<th>&nbsp;</th>
		</tr>
	</thead>

	<?php if($num_rows>0){ ?>
	<tbody>
	<?php
	foreach($_SESSION['compra_car'] as $indice=>$cantidad)
	{
		

		$dts1=$oCatalogo->mostrarUno($_SESSION['compra_catid'][$indice],$_SESSION['compra_linea_mar'][$indice]);
		$dt1 = mysqli_fetch_array($dts1);

		$can=$cantidad;
		
		
		list($cane,$canf) = explode('F',$can);
		
		echo $cane.'--'.$canf;
		
		
		
		$valor_uni=$_SESSION['compra_linea_precom'][$indice];
		//echo $valor_uni;
		
		$des_uni = $_SESSION['compra_linea_des'][$indice];	
		$val_unicondes = $valor_uni-$des_uni;
		$igv_uni = $val_unicondes *0.18;
		
		//echo 'igv-'.$igv_uni;
		
		$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;
			

		$val_tot = (abs($valor_uni)) * $can;
		$des_tot = ($des_uni) * $can;
		$igv_tot = (abs($igv_uni)) * $can;
		$pre_tot = (abs($pre_uni)) * $can;
		
			
			$total_valorventa +=$val_tot;
			$total_igv	+=$igv_tot;
			$total	+=$pre_tot;
			
			
			$rscod = $oProductomarcacodigo->mostrarUno($dt1['tb_catalogo_id'],'1');
			$dtcod = mysqli_fetch_array($rscod);
			
			$codpri = $dtcod['tb_productomarcacodigo_cod'];
			
	?>
		<tr title="<?php echo 'VALOR DE VENTA: '.formato_money($valor_venta).' - '.'IGV: '.formato_money($igvvis);?>">
			<td class=" hidden-sm" ><?php echo $codpri; ?></td>
			<td  style="dark-space: nowrap; font-size: 9pt; font-weight: bold;"><?php echo $dt1['tb_producto_nom']?></td>
			<td class=" hidden-sm" ><?php echo $dt1['tb_marca_nom']?></td>
			<td><?php echo $dt1['tb_unidad_nom']?></td>

			<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo $cantidad ?>" data-cantidad="<?php echo $cantidad ?>" 
				data-original="<?php echo $cantidad ?>" readonly data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>"
				data-tipdes="<?php echo $tipdes ?>" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" class="text-dark" style="text-align: right;">
			</td>

			<td align="right" width="80" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-preven="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-original="<?php echo formato_money(abs($valor_uni)) ?>" 
				data-tipdes="<?php echo $tipdes ?>" 
				data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 
				data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="text-dark" style="text-align: right;">
			</td>
			
			<td align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold">
							<input type="text" value="<?php echo formato_money(abs($des_uni)) ; ?>" 
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							data-tipdes="<?php echo $tipdes ?>"
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-original="<?php echo formato_money(abs($des_uni)) ?>" readonly 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" class="text-dark" style="text-align: right;">
			</td>
			
			<td class="text-dark" align="right"><?php echo formato_money(abs($igv_uni))?></td>
			
			<td align="right" width="80" class="text-dark" style="font-size:9pt; font-weight:bold">
				<?php echo formato_money(abs($pre_uni)) ?>
			</td>
			
			<td  align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold" >
			
		
			<input type="text" class="text-orange" value="<?php echo formato_money(abs($val_tot)) ; ?>" 
							data-original="<?php echo formato_money(abs($val_tot)) ?>" 
							data-cantidad="<?php echo $cantidad ?>" data-tip = "valtot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							readonly 
							style="text-align: right;">
			
			
			</td>
			<td class="" width="80" align="right"> <strong class="text-orange"><?php echo formato_money(abs($des_tot))?> </strong> </td>
			<td align="right" width="70" style="font-size: 9pt; font-weight: bold;"> 
			<strong class="text-orange"> <?php echo formato_money(abs($igv_tot))?> </strong> </td>
			
			<td align="right" width="100" class="editable" style="font-size:10pt; font-weight:bold">
							<input type="text" class="text-orange" value="<?php echo formato_money(abs($pre_tot)) ; ?>" 
							data-original="<?php echo formato_money(abs($pre_tot)) ?>" 
							data-cantidad="<?php echo $cantidad ?>" data-tip = "pretot" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							data-des="<?php echo formato_money(abs($des_uni)) ?>"
							readonly 
							style="text-align: right;">
			</td>
			

			<td align="center" nowrap="nowrap">
			<span title="Eliminar" data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" class="btn_eliminar btn btn-danger btn-xs" ><i class="fas fa-trash"></i></span>
				
		</tr>
	<?php
		mysqli_free_result($dts1);
	}	
	?>
	</tbody>
	<?php
	}
	else
	{
	?>
		<tr>
			<td colspan="14">&nbsp;</td>
		</tr>
	<?php
	}
	?>

</table>
		<table>
		<tr>
		<td>
				<div style="width:150px;" >
								<label>Operacion Gravada:</label>
								<input type="text" name="txt_com_opegra_vis" autocomplete="off"  class="form-control" id="txt_com_opegra_vis" value="<?php echo formato_money($total_valorventa); ?>" style="text-align:right;font-weight: bold;" >				
							</div>
						</td>	
						<td style="width:5%">
						</td>
						<td>
							<div >
								<label>IGV:</label>
						<input type="text" name="txt_com_igv_vis"  autocomplete="off" class="form-control border-bottom" id="txt_com_igv_vis" value="<?php echo formato_money($total_igv)?>"  style="text-align:right;font-weight: bold;" > 
							</div>
							</td>
				<td style="width:5%">
						</td>							
							<td>
							<div >
								<label>TOTAL:</label>
								<input type="text" name="txt_com_tot_vis" autocomplete="off" class="form-control" id="txt_com_tot_vis" value="<?php echo formato_money($total)?>"  style="text-align:right;font-weight: bold;"  >
							</div>
							
						</td>	
							</tr>
			</table>		

<input type="hidden" name="txt_com_tot_hdd" autocomplete="off"  class="form-control" id="txt_com_tot_hdd" value="<?php echo formato_money($tot) ?>" >
<input type="hidden" name="txt_com_tot_vis_hdd" autocomplete="off"  class="form-control" id="txt_com_tot_vis_hdd" value="<?php echo formato_money($total) ?>" >
 

<?php echo $num_rows." Registros.";?> 


<br />
<br />

</div> 


<script type="text/javascript" src="../cotizacion/NumberFormat.js"></script>

<script> 

	<?php if($total>=$tot){
		?>
	$("#fieldset_producto").hide(100);
		<?php 
	}else{
	?>
	$("#fieldset_producto").show(100);
	<?php 
	}?>

	$("#txt_com_tot_vis,#txt_com_igv_vis,#txt_com_opegra_vis").attr('disabled', 'disabled');

	$('#tabla_compra_car td input').keydown(function(ev) {
		if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			 ev.preventDefault();
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var tip = $(this).attr('data-tip');
			var cantidad = $(this).parent().parent().find('input[data-cantidad]').val();
			var preven = $(this).parent().parent().find('input[data-preven]').val();
			var des = $(this).parent().parent().find('input[data-des]').val();
			var tipdes = $(this).attr('data-tipdes');
		var valor = $(this).val();
		
			if(tip=="pretot"){
				
			
			preuni = valor/cantidad;	
			valuni = preuni / <?php echo $com_igvsub; ?>;
			
			//alert(valuni);
			
			agregar_producto('agregar', cat_id, cantidad, valuni, mar_id,des);
			
			}else if(tip=="valtot"){
				
			
			valuni =  valor/cantidad;
			
			agregar_producto('agregar', cat_id, cantidad, valuni, mar_id,des);
			
			}else{
				agregar_producto('agregar', cat_id, cantidad, preven, mar_id,des);
			}
			
			
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
			
			
			return false;
		}
	});

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

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

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

	 
			$('.btn_editar').click(function(event) {
				event.preventDefault();
				var $input = $(this).parent().parent().find('input[data-cantidad]');
				var $td = $input.parent().addClass('editando');
				$input.removeAttr('readonly').focus().select();
			});

			/* $('#txt_com_valven, #txt_com_igv, #txt_com_tot').blur(function(){
				verpago();				
			}); */
		
			

	$('.btn_eliminar').click(function(event) {
				var cat_id = $(this).parent().parent().find('input').attr('data-cat_id');
				var mar_id = $(this).attr('data-mar_id');
				indice = cat_id+''+mar_id;

			//	alert(indice);
				if(confirm('¿Seguro de quitar el item?')){
					agregar_producto('quitar', indice);
				}
			});


		function cambiartides(t){
			var cat_id = $(t).attr('data-cat_id');
			var cantidad = $(t).parent().parent().find('input[data-cantidad]').val();
			var preven = $(t).parent().parent().find('input[data-preven]').val();
			var des = $(t).parent().parent().find('input[data-des]').val();
			var tipdes = $(t).val();
			var mar_id = $(t).attr('data-mar_id');
			
			agregar_producto('agregar', cat_id, cantidad, preven, mar_id,des,tipdes);

		}



</script>



<?php 


} ?>


<div class="table-responsive">


			<table>
				<tr>
					<td>
							<div  >
								<label>Operacion Gravada:</label>
								<input type="text"  autocomplete="off"  class="form-control text-red" value="<?php echo formato_money($opegra); ?>" 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-red" 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-red" value="<?php echo formato_money($tot)?>"  style="text-align:right;font-weight: bold;" readonly >
							</div>
					</td>	
				</tr>
			</table>
</div>			

 <input type="hidden" name="txt_com_tot" autocomplete="off"  id="txt_com_tot" value="<?php echo formato_money($tot)?>"  style="text-align:right;font-weight: bold;"  >
        
 
<?php 	
}
?>