<?php
require_once ("../../config/Cado.php");
require_once ("../producto/cProducto.php");
$oProducto = new cProducto();
require_once ("../producto/cPresentacion.php");
$oPresentacion = new cPresentacion();
require_once ("../almacen/cAlmacen.php");
$oAlmacen = new cAlmacen();
require_once("cStock.php");
$oStock = new cStock();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once ("../formatos/formato.php");




	$dts1=$oCatalogo->mostrarUno($_POST['cat_id'],$_POST['mar_id']);
	$dt1 = mysqli_fetch_array($dts1);
	$pro_codint = $dt1['tb_producto_codint'];
	$pro_nom = $dt1['tb_producto_nom'];
	$mar_nom = $dt1['tb_marca_nom'];
	$emp_fac = $dt1['tb_empaque_fac'];
	
	$dts=$oAlmacen->filtrar('');
	$num_rows= mysqli_num_rows($dts);



?>
<script type="text/javascript">
	


$(function() {
	
	
	
	
});
</script>
<style>
	div#cuadro_sto_form { margin: 0 0; }
	div#cuadro_sto_form table { margin: 0.1em 0; border-collapse: collapse; width: 100%; }
	div#cuadro_sto_form table td, div#cuadro_sto_form table th { border: 1px solid #eee; padding: 2px 3px; }
</style>

<div class="col-lg-12 col-sm-12 col-12"> 
	<div class="modal-header">		
		<h4 class="modal-title" id="titlemodal">Información de Almacenes</h4> 
	</div>


<div class="col-lg-12 col-sm-12 col-12"> 
		<label style="color:#3B71CA;font-weight:arial;" >PRODUCTO:</label>
		<span style="color:#000;font-size:1.5rem;" >  <?php echo $pro_nom?> </span>
</div>



<div class="col-lg-12 col-sm-12 col-12"> 
		<label style="color:#DC4C64;font-weight:arial;" >CODIGO INTERNO:</label>
		<span  style="color:#000;font-size:1.5rem;" >  <?php echo $pro_codint?> </span>
</div>


<div class="col-lg-12 col-sm-12 col-12"> 
		<label  style="color:#DC4C64;font-weight:arial;" >MARCA:</label>
		 <span style="color:#000;font-size:1.5rem;" >  <?php echo $mar_nom?> </span>
</div>


<div class="col-lg-12 col-sm-12 col-12"> 			
		
		<table id="tabla_producto" class="table">
						
						
						
						<?php  if($num_rows>=1){
							 while($dt = mysqli_fetch_array($dts)){
								 
								 
								//$stock = mysqli_fetch_array($oStock->stock_por_presentacion($_POST['cat_id'],$_POST['mar_id'],$dt['tb_almacen_id']))['tb_stock_num']*1;
								
								$stockkardex = mysqli_fetch_array($oStock->sumatipokardex_por_presentacion($_POST['cat_id'],$_POST['mar_id'],$dt['tb_almacen_id']))['total']*1; 
								
								$dtssto = $oStock->stock_por_presentacion($_POST['cat_id'],$_POST['mar_id'],$dt['tb_almacen_id']);
								$dtsto = mysqli_fetch_array($dtssto);
								$stock = $dtsto['tb_stock_num']*1; 
								$stock_id = $dtsto['tb_stock_id']; 
							
							//echo $stockkardex .''.$stock.' | ';
							
							if($stockkardex>0){
							if($stockkardex!=$stock){
								$oStock->modificar($stock_id,$stockkardex);
								$stock =$stockkardex; 
							//	echo '1-';
							}
							}
							
								
								$stocktotal += $stock;
							
								
							$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;
							}else if($cane=="0" && $canf>0){
								
								$stock = 'F'.$canf;
							}
							
							?>
							
								<tr style="<?php if($_POST["alm_id"] == $dt['tb_almacen_id']) echo 'border: 1px solid red;background-color:#1565C0;color:#fff;'; ?>">
									<td><?php echo $dt['tb_almacen_nom'];?></td>
									<td><h4><?php echo $stock;?></h4>
									<?php 
									if($stockkardex<0){
										?>
										<span style="color:red;"><?php echo $stockkardex; ?></span>
										<?php
									}
									?>
									
									</td>
								</tr>
							
							<?php 
							 }
							  $cantot = $stocktotal / $emp_fac;
							$canf = 0;
							$cane = 0;
							//echo $emp_fac.'--';
							if($stocktotal<$emp_fac){
								$cane = $cane;
								$canf = $stocktotal;
							}else{
								list($cane,$canf) = explode('.',$cantot);
							}
							
							
							if($cane>0 && $canf>0){
								$canemp = $cane * $emp_fac;
								$canf = $stocktotal - $canemp;
								$stocktotal = $cane.'F'.$canf;
							}else if($cane>0 && $canf==""){
								$stocktotal = $cane;
							}else if($cane=="" && $canf>0){
								
								$stocktotal = 'F'.$canf;
							}else if($cane=="0" && $canf>0){
								
								$stocktotal = 'F'.$canf;
							}
							
							
							?>
							 
							 
							 
							 
								<tr>
									<td style="color:#DC4C64" >TOTAL:</td>
									<td style="color:#14A44D" ><h4><?php echo $stocktotal;?></h4></td>
								</tr>
							 
						<?php	
							
							
						}else{
						?>
						<tr>
							<td>SIN ALMACENES</td>
						</tr>
						<?php
						}?>
						
							
		</table>
		
</div>


<div class="box-footer float-right">
					<button type="button" data-dismiss="modal" class="btn btn-default">Cancelar</button>
</div>

</div>




	
	