<?php
//ini_set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
require_once ("cDistribucion.php");
$oDistribucion = new cDistribucion();
require_once ("../formatos/formato.php");
$limite = $_POST['cmb_fil_lim'];
//echo $_POST['hdd_fil_cata_id'];
require_once ("../almacen/cAlmacen.php");
$oAlmacen = new cAlmacen();
require_once ("../stock/cStock.php");
$oStock = new cStock();


unset($_SESSION["cat_id"]);
unset($_SESSION["lab_id"]);
unset($_SESSION["can"]);
unset($_SESSION["almori_id"]);
unset($_SESSION["almdes_id"]);

$dts1 = $oDistribucion->filtrar($_POST['txt_fil_pro_dat1'],$_POST['txt_fil_pro_dat2'],$_POST['txt_fil_pro_dat3'],
$_POST['txt_fil_pro_dat4'],$limite,$_POST['txt_fil_mar'],$_POST['txt_fil_pro_acc'],$_POST['cmb_fil_cat_id']);
$num_rows= mysqli_num_rows($dts1);

?>

<section class="content" style="" >
	<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">	
	
					<table id="tabla_distribucion" class="table table-bordered table-striped">
						
		 <thead>
        <tr>
          <th>CO. INTERNO</th>
          <th>NOMBRE</th>
		  <th align="right">LABORATORIO/CATEGORIA</th>
          <th>PRESENTACION/EMPAQUE</th>
          <th nowrap title="Fecha">FECHA VENCIMIENTO</th>
        <th nowrap title="Fecha">STOCK ACTUAL</th>
          </tr>
        </thead>
						<?php if($num_rows>=1){ ?>
						<tbody>	
						
						
						<?php 
						
						while($dt1 = mysqli_fetch_array($dts1)) {
						
					//	if($_POST['hdd_alm_ori']>0) $stock = mysqli_fetch_array($oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_POST['hdd_alm_ori']))['tb_stock_num']*1; 
						
						
						if($_POST['hdd_alm_ori']>0) {
							
							$stockkardex = mysqli_fetch_array($oStock->sumatipokardex_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_POST['hdd_alm_ori']))['total']*1; 
								
								$dtssto = $oStock->stock_por_presentacion($dt1['tb_catalogo_id'],$dt1['tb_marca_id'],$_POST['hdd_alm_ori']);
								$dtsto = mysqli_fetch_array($dtssto);
								$stock = $dtsto['tb_stock_num']*1; 
								$stock_id = $dtsto['tb_stock_id']; 
							
							if($stockkardex!=$stock){
								$oStock->modificar($stock_id,$stockkardex);
								$stock =$stockkardex; 
							}
							
						}
						
						
							
							$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;
							}
								
							

						
						?>
						
					<tr  id="td_fila_<?php echo $dt1['tb_catalogo_id']?>">

					<td><?php echo $dt1['tb_producto_codint']?></td>
					<td> <span style="cursor:pointer" data-lab_id="<?php echo $dt1['tb_marca_id'];?>"  data-cat_id="<?php echo $dt1['tb_catalogo_id'];?>" class="span_nombre"> <?php echo $dt1['tb_producto_nom']?></span>  </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 align="right"> <?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 style="border:red">
					
					
					
					
						</div>
					</div>
				</div>
			</div>
		</div>		
		</div>
	</section>
	
	
<script type="text/javascript">



	$(function() {

			$(".span_nombre").click(function(){
		    almori_id = $("#hdd_alm_ori").val();
			cat_id = $(this).attr('data-cat_id');
			lab_id = $(this).attr('data-lab_id');
			//alert(""+cat_id+""+lab_id);
			
			$.ajax({
			type: "POST",
			url: "../distribucion/distribucion_tablasucursal.php",
			async:true,
			dataType: "html",                      
			data: ({	
				cat_id:cat_id,
				lab_id:lab_id,
				almorigen_id:almori_id
			}),
			beforeSend: function() {		
				$('#div_distribucionsucursales_tabla').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_distribucionsucursales_tabla').html(html);
			},
			complete: function(){			
				$('#div_distribucionsucursales_tabla').removeClass("ui-state-disabled");
			}
			});
		});
		
		
		

	});



</script>
	
	
	