<?php
//ini_set('display_errors',1);
require_once ("../../config/Cado.php");
require_once ("cGestionxstock.php");
$oGestionxstock = new cGestionxstock();
require_once("../producto/cProductolote.php");
$oProductolote = new cProductolote();
require_once ("../formatos/formato.php");
$limite = $_POST['cmb_fil_lim'];
//echo $_POST['hdd_fil_cata_id'];
$dts1 = $oGestionxstock->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['cmb_fil_cat_id']);
$num_rows= mysqli_num_rows($dts1);

?>
<style>
	#tabla_gestion_precios td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_gestion_precios td.editando{
			background-color: rgba(18, 180, 243, 0.05);
			outline: 2px solid #35A4F3;
	}
	#tabla_gestion_precios td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_gestion_precios td input:focus{
		background: transparent;
			height: 100%;
			width: 100%;
			outline: none;
			border: none;
	}
	#tabla_gestion_precios td input[readonly]{
		background: transparent;
	}
 
	</style>
<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">	
					
					<span style="cursor:pointer" id="btn_excel" class="btn btn-success"> <i class="fas fa-file-excel"></i> DESCARGAR EXCEL</span>
		
					
					
					<table id="tabla_gestion_precios" class="table table-bordered table-striped">
						<thead>
							<tr>
							    <th>COD. INTERNO</th>
							    <th>LOTE</th>
								<th>NOMBRE</th>
								<th>LABORATOTIO / CATEGORIA</th>
								<th>PRESENTACION / EMPAQUE</th>
								<th>FEC. VENCIMIENTO</th>
								<th align="right">STOCK ACTUAL</th>
							</tr>
						</thead>
						<?php if($num_rows>=1){ ?>
						<tbody>	
						
						<?php 
						
						while($dt1 = mysqli_fetch_array($dts1)) {
						if($_POST['cmb_fil_alm_id']>0) $stock = $dt1['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'];
					
						
						?>
					
					<tr  id="td_fila_<?php echo $dt1['tb_catalogo_id']?>">
					<td><?php echo $dt1['tb_producto_codint']?></td>
					<td style="font-weight:bold" nowrap=""  > <strong class="" style="padding:5px;background-color:#3F51B5;color:#fff;" > <?php echo $lote?> </strong> </td>
								
					<td><?php echo $dt1['tb_producto_nom']?></td>
					<td><?php echo $dt1['tb_marca_nom'].' / '.$dt1['tb_categoria_nom']?></td>								
					<td><?php echo $dt1['tb_presentacion_nom'].' / '.$dt1['tb_empaque_nom']?></td>								
									
					<td align="right"> <?php echo mostrarFecha($dt1['tb_productomarca_fecven'])?></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>
					</tr>
								
								<?php } 
								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>
	
	
<script type="text/javascript">




function abrirEnPestana(url) {
		var a = document.createElement("a");
		a.target = "_blank";
		a.href = url;
		a.click();
}

$("#btn_excel").click(function(){
			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"]; ?>';
			lit = '<?php echo $limite; ?>';	
			alm_id = '<?php echo $_POST["cmb_fil_alm_id"]; ?>';
			mar = '<?php echo $_POST["txt_fil_mar"]; ?>';	
			acc = '<?php echo $_POST["txt_fil_pro_acc"]; ?>';	
			cat_id = '<?php echo $_POST["cmb_fil_cat_id"]; ?>';	
			abrirEnPestana('../gestionxstock/gestionxstock_excel.php?dat1='+dat1+'&dat2='+dat2+'&dat3='+dat3+'&dat4='+dat4+'&alm_id='+alm_id+'&mar='+mar+'&acc='+acc+'&cat_id='+cat_id);
});



</script>
	
	
	