<?php
//ini_set('display_errors',1);
require_once ("../../config/Cado.php");
require_once ("cInventarioinicial.php");
$oInventarioinicial = new cInventarioinicial();
require_once ("../categoria/cCategoria.php");
$oCategoria = new cCategoria();
require_once("../kardex/cKardex.php");
$oKardex = new cKardex();
require_once ("../stock/cStock.php");
$oStock = new cStock();
require_once ("../formatos/formato.php");
if(isset($_POST['cmb_fil_cat_id']) and $_POST['cmb_fil_cat_id']>0)
{
	$cat_id=$_POST['cmb_fil_cat_id'].'';
	
	$dts2=$oCategoria->mostrar_por_idp($_POST['cmb_fil_cat_id']);
	$num_rows2= mysqli_num_rows($dts2);
	if($num_rows2>0){
		while($dt2 = mysqli_fetch_array($dts2)){
			
			$cat_id.=', '.$dt2['tb_categoria_id'];
			
			$dts3=$oCategoria->mostrar_por_idp($dt2['tb_categoria_id']);
			$num_rows3= mysqli_num_rows($dts3);
			if($num_rows3>0){
				while($dt3 = mysqli_fetch_array($dts3)){
					$cat_id.=', '.$dt3['tb_categoria_id'];			
				}
				mysqli_free_result($dts3);
			}//fin nivel 3
		}
		mysqli_free_result($dts2);
	}//fin nivel 2
//echo $cat_id;			
}
$limite = $_POST['cmb_fil_lim'];
//echo $_POST['hdd_fil_cata_id'];
$dts1 = $oInventarioinicial->filtrar($_POST['txt_fil_pro_dat1'],$_POST['txt_fil_pro_dat2'],$_POST['txt_fil_pro_dat3'],
$_POST['txt_fil_pro_dat4'],$limite,$_POST['cmb_fil_alm_id'],$_POST['txt_fil_mar'],$_POST['txt_fil_pro_acc']);
$num_rows= mysqli_num_rows($dts1);
?>
<script type="text/javascript">
function evaluar(idbtn,idmar)
{
	if($("#stock_id"+idbtn+''+idmar).val().length >0)
	{
		$("#btn_guardar"+idbtn+''+idmar).prop( "disabled", true );
		$("#btn_guardar"+idbtn+''+idmar).css({ opacity: 0.5 });
		$("#btn_guardar"+idbtn+''+idmar).prop('onclick',null);
		$("#stock_ini"+idbtn+''+idmar).prop('keydown',null);
		$("#costo_prod_id"+idbtn).prop('readonly', true);
		$("#stock_ini"+idbtn+''+idmar).prop('readonly', true);
		$("#stock_ini"+idbtn+''+idmar).css({"border": "none", "background-color": "transparent"}); 
	}else{
		$("#stock_ini"+idbtn+''+idmar).keydown(function(e){if(e.which==13){guardar_stock_cantidad(idbtn,idmar);}});
	}
}

function guardar_stock_cantidad(idcat,idmar,val,ban)
{
	
	
	fecven = $("#fecven_ini"+idcat+''+idmar).val();
	preven = $("#preven_ini"+idcat+''+idmar).val();
	prevenf = $("#prevenf_ini"+idcat+''+idmar).val();
	
	
	//alert(fecven+'--'+preven+'--'+prevenf);
	
		$.ajax({
			type: "POST",
			url: "../inventarioinicial/inventarioinicial_reg.php",
			async : true,
			dataType: "json",
			data: ({
				action : 'guardar_stock_cantidad',				
				cat_id : idcat,
				mar_id: idmar,
				fecven: fecven,
				preven: preven,
				prevenf: prevenf,
				ban:ban,
				alm_id: $("#cmb_fil_alm_id").val(),
				stock :val,
				hdd_usu_id :$("#hdd_usu_id").val(),
				hdd_emp_id:$("#hdd_emp_id").val()
			}),
			beforeSend: function() {
				
			},
			success: function(data)
			{
				inventarioinicial_tabla();
				toastr["info"](data.inv_msj);	
				
			},
		});
	
}



$(function(){
	
$('.stock_ini').keydown(function(e){
		if (e.which == 13){
			
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var ban = $(this).attr('data-ban');
			var val = $(this).val();
			guardar_stock_cantidad(cat_id,mar_id,val,ban);
			
		return false;}
});


$('.stock_ini').change(function(){
		$(this).val($(this).val().toUpperCase());
});
	
	
});	
	
	
	
</script>
<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">
				<?php 
				/* echo $_POST['hdd_fil_cata_id'].'-'.$_POST['txt_fil_pro_codbar'].'-'.$cat_id.'-'.$_POST['cmb_fil_mar_id'].'-'.$_POST['cmb_fil_pro_est'].'-'.$_POST['cmb_fil_arttip_id'].'-'.$limite ; */
				?>
					<table id="tabla_producto" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>NOMBRE</th>
								<th>PRESENTACION / EMPAQUE</th>
								<th>MARCA / CATEGORIA</th>
								<th>UNIDAD</th>
								<th>FEC. VENCIMIENTO</th>
								<th>PRE. VENTA</th>
								<th>PRE. VENTA F</th>
								<th align="right">STOCK ACTUAL</th>
								<th nowroap align="center">STOCK INICIAL</th>
							</tr>
						</thead>
						<?php if($num_rows>=1){ ?>
						<tbody>	
							<?php while($dt1 = mysqli_fetch_array($dts1)) {
							if($_POST['cmb_fil_alm_id']>0)$stock = mysqli_fetch_array($oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_POST['cmb_fil_alm_id']))['tb_stock_num']*1; 
							
							
							$emp_fac = $dt1['tb_empaque_fac'];
							$can = $stock / $emp_fac;
							$canf = 0;
							$cane = 0;
							//echo $emp_fac.'--';
							if($stock<$emp_fac){
								$cane = $cane;
								$canf = $stock;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							
							if($cane>0 && $canf>0){
								$canemp = $cane * $emp_fac;
								$canf = $stock - $canemp;
								$stock = $cane.'F'.$canf;
							}else if($cane>0 && $canf==""){
								$stock = $cane;
							}else if($cane=="" && $canf>0){
								
								$stock = 'F'.$canf;
							}
							
							
							
								?>
								<tr  id="td_fila_<?php echo $dt1['tb_catalogo_id']?>">
									<td><?php echo $dt1['tb_producto_nom']?></td>
									<td><?php echo $dt1['tb_presentacion_nom'].' / '.$dt1['tb_empaque_nom']?></td>
									<td><?php echo $dt1['tb_marca_nom'].' / '.$dt1['tb_categoria_nom']?></td>
								
									<td><?php echo $dt1['tb_unidad_abr']?></td>
									
								
									<td  id="td_fecven_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
																		
										<input type="text" data-index="<?php echo $indexfocus ?>" 
										style="text-align:right"  size="15" class="form-control stock_ini" 
										data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?>
										data-ban = <?php echo '1'?>
										name="fecven_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>"  autocomplete="off"
										id="fecven_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
										value="<?php echo mostrarFecha($dt1['tb_productomarca_fecven']); ?>">
										
										
									</td>
									
									<td  id="td_preven_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
																	
										<input type="text" data-index="<?php echo $indexfocus ?>" 
										style="text-align:right"  size="10" class="form-control stock_ini" 
										name="preven_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
										data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-ban = <?php echo '1'?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?>
										id="preven_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" autocomplete="off"
							
										value="<?php echo formato_money($dt1['tb_productomarca_preven']); ?>">
										
									
										
									</td>  

									<td  id="td_prevenf_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
																	
										<input type="text" data-index="<?php echo $indexfocus ?>" 
										data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?>
										data-ban = <?php echo '1'?>
										style="text-align:right"   size="10" class="form-control stock_ini" 
										name="prevenf_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" 
										id="prevenf_ini<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" autocomplete="off"
										value="<?php echo formato_money($dt1['tb_productomarca_prevenf']); ?>">
										
									</td>              
									
									
									<td id="td_stockactual_<?php echo $dt1['tb_catalogo_id']?>"  align="right">
									<?php 
									if($stock==""){}else{ if($stock<=10){ echo '<strong style="color:red;font-size:2rem;">'.$stock .'</strong>'; }else{ echo '<strong style="">'.$stock*1 .'</strong>'; } } ?>
									<input type="hidden" name="stock_id<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" id="stock_id<?php echo $dt1['tb_catalogo_id'].$dt1['tb_marca_id']?>" value="<?php echo  $stock?>">
										
									</td>
									
									
									
									<td  id="td_stockini_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
										
																				
										<input type="text" style="text-align:right;" data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?> size="7" height="15"
										data-ban = <?php echo '2'?>
										class="inputs form-control stock_ini " value="<?php echo '' ?>">
										
										
									</td>   


									
								</tr>
								
								<?php $indexfocus ++; } mysqli_free_result($dts1); ?>
							</tbody>
							<?php } ?>
							<tr class="even">
								<td colspan="13"><?php echo $num_rows.' registros'?></td>
							</tr>
						</table>
					</div>
				</div>
				</div>
			</div>
		</div>
	</div>
</section>