<?php
session_start();
//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("../producto/cProductolote.php");
$oProductolote = new cProductolote();
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'],$_POST['txt_fil_letlab']);
$num_rows= mysqli_num_rows($dts1);
?>
<script type="text/javascript">


function guardar_stock_cantidad(idcat,idmar,fecven,lote_id,lote,stock)
{
	//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,
				lote_id:lote_id,
				lote:lote,
				alm_id: $("#cmb_fil_alm_id").val(),
				stock :stock,
				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 fecven = $("#stock_ini_txtfecven_"+cat_id).val();
			var lote_id = $("#stock_ini_txtlote_"+cat_id).attr('data-lot_id');
			var lote = $("#stock_ini_txtlote_"+cat_id).val();
			var stock = $("#stock_ini_txt_"+cat_id).val();
			
			//alert(lote_id);
			//var val = $(this).val();
			
			guardar_stock_cantidad(cat_id,mar_id,fecven,lote_id,lote,stock);
			
		return false;}
});
	


$('.stock_ini').change(function(){
		$(this).val($(this).val().toUpperCase());
});
	
$('.btn_guardar').click(function(){
	
		
		
		var cat_id = $(this).attr('data-cat_id');
		var mar_id = $(this).attr('data-mar_id');
		var ban = $(this).attr('data-ban');
	
			var fecven = $("#stock_ini_txtfecven_"+cat_id).val();
			var lote_id = $("#stock_ini_txtlote_"+cat_id).attr('data-lot_id');
			var lote = $("#stock_ini_txtlote_"+cat_id).val();
			var stock = $("#stock_ini_txt_"+cat_id).val();
			
			
			guardar_stock_cantidad(cat_id,mar_id,fecven,lote_id,lote,stock);
		
		//	guardar_stock_cantidad(cat_id,mar_id,val,ban,lot);
			
			
		//
		
		
});
	
	
});	
	
	
	
</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;">
	
	
	<a title="VER DESCARGAR Y/O IMPRIMIR PDF" data-toggle="tooltip" data-placement="top"  
href="../../files/inventarioinicial/inventarioinicial_impcpe_doc.php?dat1=<?php echo $_POST['txt_fil_pro_dat1']; ?>
&dat2=<?php echo $_POST['txt_fil_pro_dat2']; ?>&dat3=<?php echo $_POST['txt_fil_pro_dat3']; ?>
&dat4=<?php echo $_POST['txt_fil_pro_dat4']; ?>&limit=<?php echo $limite; ?>&alm_id=<?php echo $_POST['cmb_fil_alm_id']; ?>
&lab=<?php echo $_POST['txt_fil_mar']; ?>&acc=<?php echo $_POST['txt_fil_pro_acc']; ?>&letlab=<?php echo $_POST['txt_fil_letlab']; ?>" target="_blank" class="btn btn-sm" 
style="background-color:#3B71CA;color:#fff;" title="DESCARGAR E IMPRIMIR PDF"><i class="fas fa-file-pdf"></i> DESCARGAR E IMPRIMIR </a>		 
				
				
					<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>COD. INTERNO</th>
								<th>NOMBRE</th>
								<th>PRESENTACION / EMPAQUE</th>
								<th>LABORATORIO / CATEGORIA</th>
								<th>UNIDAD</th>
								
								<th>COD. BAR.</th>
								<th>FEC. VENCIMIENTO</th>
								<th nowrap align="right">NUM. LOTE</th>
								<th align="right">STOCK ACTUAL</th>
								<th nowrap align="center">STOCK INI.</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;
							
							if($stock<$emp_fac){
								$cane = $cane;
								$canf = $stock;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							// echo $cane.'--'.$canf;
							
							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;
							}else if($cane==0 && $canf>0){
								
								$stock = 'F'.$canf;
							}
							
							
					$dtspl = $oProductolote->mostrarUltimoLote($dt1['tb_catalogo_id']);		
					$numrowspl = mysqli_num_rows($dtspl);
					
					$dtp = mysqli_fetch_array($dtspl);
					$lote = $dtp['tb_productolote_nom'];
					$lot_id = $dtp['tb_productolote_id'];
					
					
							
								?>
								<tr  id="td_fila_<?php echo $dt1['tb_catalogo_id']?>">
								<td><?php echo $dt1['tb_producto_codint']?></td>
									<td nowrap >
									<?php echo $dt1['tb_producto_nom']?>
								<?php if($_SESSION['usuariogrupo_id']==1 || $_SESSION['usuariogrupo_id']==2){?>
									
								<span class="btn btn-success btn-sm" title="Agregar producto a la orden de compra" data-toggle="tooltip" data-placement="top" onclick="agregar_productoordencompra('<?php echo $dt1['tb_catalogo_id']; ?>','<?php echo $dt1['tb_marca_id']; ?>');"  >
									<i class=" fas fa-hand-pointer"></i>
									</span>
									
									<span class="btn btn-primary btn-sm" title="Ver todos almacenes" data-toggle="tooltip" data-placement="top"   onclick="ver_almacenes('<?php echo $dt1['tb_catalogo_id']; ?>','<?php echo $dt1['tb_marca_id']; ?>','<?php echo $_POST['cmb_fil_alm_id']; ?>');"  >
									<i class="fas fa-store"></i>
									</span>
									<?php }?>
								
								</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><?php echo $dt1['tb_productomarca_codbar']?></td>
									
								
									<td  id="td_fecven_<?php echo $dt1['tb_catalogo_id']?>" class="" nowrap align="right">
																		
										<input type="text" data-index="<?php echo $indexfocus ?>" 
										style="text-align:right;border: 1px solid #3B71CA;"  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'?> id="stock_ini_txtfecven_<?php echo $dt1['tb_catalogo_id']; ?>"
										name="fecven_ini_<?php echo $dt1['tb_catalogo_id']?>"  autocomplete="off"
										
										value="<?php echo mostrarFecha($dt1['tb_productomarca_fecven']); ?>">
										
										
									</td>
									
									
								
									
								<!-- LOTEEEE -->
									
									<td  width="150" id="td_stockini_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
										
																				
										<input type="text" style="text-align:right;border: 1px solid #3B71CA;" data-cat_id ="<?php echo $dt1['tb_catalogo_id']; ?>"
										data-mar_id ="<?php echo $dt1['tb_marca_id']?>" data-lot_id ="<?php echo $lot_id; ?>" 
										size="7" height="15"  autocomplete="off"
										data-ban = <?php echo '3'?> id="stock_ini_txtlote_<?php echo $dt1['tb_catalogo_id']; ?>"
										class="inputs form-control stock_ini " value="<?php echo $lote; ?>">
										
										
									</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 .'</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>
									
									<!-- STOCK INICIAL -->
									<td  id="td_stockini_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
										
																				
										<input type="text" style="text-align:right;border: 1px solid #DC4C64;" data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?> size="7" height="15" autocomplete="off"
										data-ban = <?php echo '2'?>  id="stock_ini_txt_<?php echo $dt1['tb_catalogo_id']; ?>"
										class="inputs form-control stock_ini " value="<?php echo '' ?>">
										
										
									</td>   



									<td  id="td_stockini_<?php echo $dt1['tb_catalogo_id']?>" nowrap align="right">
										
																				
										<span id="" class="btn btn-sm btn-primary btn_guardar " data-cat_id = <?php echo $dt1['tb_catalogo_id']; ?>
										data-mar_id = <?php echo $dt1['tb_marca_id']?> data-ban = <?php echo '2'?>
										data-nombrecampo="#stock_ini_txt_<?php echo $dt1['tb_catalogo_id']; ?>" ><i class="fas fa-save"></i></span>
										
										
									</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>