<?php
//ini_set("display_errors",1);
require_once ("../../config/Cado.php");
session_start();
require_once ("../almacen/cAlmacen.php");
$oAlmacen = new cAlmacen();
require_once ("../stock/cStock.php");
$oStock = new cStock();
require_once ("../traspaso/cTraspaso.php");
$oTraspaso = new cTraspaso();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once ("../distribucion/cDistribucion.php");
$oDistribucion = new cDistribucion();
require_once ("../venta/cVenta.php");
 $oVenta = new cVenta();
require_once("../formatos/fechas.php");

require_once ("../formatos/formato.php");


$mesactual = date('m');

$y = date('Y');
$m = date('m');
$d = date('d');

$fecactual='01-'.$m.'-'.$y;
list($day,$mon,$year) = explode('-',$fecactual);


$fecantes=date('Y-m-d',mktime(0,0,0,$mon-2,$day,$year));
list($anioini1,$mesini1,$diaini1) = explode('-',$fecantes);

$fecantes=date('Y-m-d',mktime(0,0,0,$mon-1,$day,$year));
list($anioini2,$mesini2,$diaini2) = explode('-',$fecantes);

$fecantes=date('Y-m-d',mktime(0,0,0,$mon,$day,$year));
list($anioini3,$mesini3,$diaini3) = explode('-',$fecantes);




//echo $fecantes.'--'.$fecactual.'--'.$mesini;

			$dts1=$oCatalogo->mostrarUno($_POST['cat_id'],$_POST['lab_id']);
			$dt1 = mysqli_fetch_array($dts1);
			$pro_nom =  $dt1['tb_producto_nom'];
			$emp_nom =  $dt1['tb_empaque_nom'];
			$emp_fac =  $dt1['tb_empaque_fac'];
			
			$dts5 = $oAlmacen->mostrarUno($_POST['almorigen_id']);
			$dt5 = mysqli_fetch_array($dts5);
			$alm_nom = $dt5["tb_almacen_nom"];
			

		$stock = mysqli_fetch_array($oStock->stock_por_presentacion($_POST['cat_id'],$_POST['lab_id'],$_POST['almorigen_id']))['tb_stock_num']*1;
								


						$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;
							}


?>
<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;">
	
					
 
		<table class="table table-bordered table-striped">
		<thead>
		<tr style="background-color:#FFCDD2;color:#000;" >
          <th><?php echo  $pro_nom; ?></th>
          <th><?php echo  $emp_nom; ?></th>
          <th><?php echo "ORIGEN: ". $alm_nom; ?></th>
          <th>STOCK ACTUAL</th>
          <th><?php echo $stock; ?></th>
        </tr>	 
        </thead>	 
        </tABLE>
		
		<div class="table-responsive " style="overflow-y: scroll;" >	
					
					
		<div  class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="height:5px;" > </div>
		

					<table class="table table-bordered table-striped">
					 <thead>
			 
        <tr>
          <th>ALMACEN</th>
          
          
		  <?php
		   for($i=1;$i<=3;$i++){
			 
			 if($i==1){
				 $mes = $mesini1; 
			 }
			 else if($i==2){
				 $mes = $mesini2; 
			 }
			 else if($i==3){
				 $mes = $mesini3; 
			 }
			 
			?>
			<th><?php echo nombre_mes($mes);?></th>
			<?php 			
		  }
		  
		  ?>
		  
		  <th>STOCK ACTUAL</th>
		  <th>TRANSITO</th>
		  
		  
          <th></th>
          </tr>
        </thead>
					<?php 
					
					foreach($_SESSION["alm_id"] as $indice=>$alm_id){
						
						$dts5 = $oAlmacen->mostrarUno($alm_id);
						$dt5 = mysqli_fetch_array($dts5);
						$alm_nom = $dt5["tb_almacen_nom"];
						$punven_id = $dt5["tb_puntoventa_id"];
					
			
		                $stockkardex = mysqli_fetch_array($oStock->sumatipokardex_por_presentacion($_POST['cat_id'],$_POST['lab_id'],$alm_id))['total']*1; 
				
						
						$dtssto = $oStock->stock_por_presentacion($_POST['cat_id'],$_POST['lab_id'],$alm_id);
						$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; 
						}


						
				//	$stock = mysqli_fetch_array($oStock->stock_por_presentacion($_POST['cat_id'],$_POST['lab_id'],$alm_id))['tb_stock_num']*1; 
								
							$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;
							}	
									
						//$barori = $_POST['almorigen_id'];

			$dts6 = $oDistribucion->mostrarporAlmacenes($_POST['almorigen_id'],$alm_id,$_POST['cat_id'],$_POST['lab_id']);
			$dt6 = mysqli_fetch_array($dts6);
			$discar_can = $dt6["tb_distribucioncarrito_can"];
			
			$dts6 = $oTraspaso->verTransito($_POST['cat_id'],$_POST['lab_id'],$_POST['almorigen_id'],$alm_id,'1');
			$dt6 = mysqli_fetch_array($dts6);
			$can_trans = $dt6["cantidadtransito"] * 1;
			
			$can = $can_trans / $emp_fac;
							$canf = 0;
							$cane = 0;
							//echo $emp_fac.'--';
							if($can_trans<$emp_fac){
								$cane = $cane;
								$canf = $can_trans;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							
							if($cane>0 && $canf>0){
								$canemp = $cane * $emp_fac;
								$canf = $can_trans - $canemp;
								$can_trans = $cane.'F'.$canf;
							}else if($cane>0 && $canf==""){
								$can_trans = $cane;
							}else if($cane=="" && $canf>0){
								
								$can_trans = 'F'.$canf;
							}else if($cane=="0" && $canf>0){
								
								$can_trans = 'F'.$canf;
							}	
									
									
						
					?>
						
					<tr>
					<td><?php echo $alm_nom ?></td>
					
					
					<?php
					   for($i=1;$i<=3;$i++){
						  
						  $fec1 = '01-'.$i.'-'.$y;
						  $fec2 = '31-'.$i.'-'.$y;
						  
						   if($i==1){
							   
							    $fec1 = '01-'.$mesini1.'-'.$anioini1;
								$fec2 = '31-'.$mesini1.'-'.$anioini1;
							 }
							 else if($i==2){
								  $fec1 = '01-'.$mesini2.'-'.$anioini2;
								$fec2 = '31-'.$mesini2.'-'.$anioini2;
							 }
							 else if($i==3){
								 $fec1 = '01-'.$mesini3.'-'.$anioini3;
								$fec2 = '31-'.$mesini3.'-'.$anioini3;
							 }
					  
						  $i = $i * 1;
						$dtsv = $oVenta->ventas_rotacion($_POST['cat_id'],$_POST['lab_id'],fecha_mysql($fec1),fecha_mysql($fec2),$punven_id);
						$dtv = mysqli_fetch_array($dtsv);
						$canven = $dtv['can']*1;
							$can = $canven / $emp_fac;
							$canf = 0;
							$cane = 0;
							//echo $emp_fac.'--';
							if($canven<$emp_fac){
								$cane = $cane;
								$canf = $canven;
							}else{
								list($cane,$canf) = explode('.',$can);
							}
							
							
							if($cane>0 && $canf>0){
								$canemp = $cane * $emp_fac;
								$canf = $canven - $canemp;
								$canven = $cane.'F'.$canf;
							}else if($cane>0 && $canf==""){
								$canven = $cane;
							}else if($cane=="" && $canf>0){
								
								$canven = 'F'.$canf;
							}else if($cane=="0" && $canf>0){
								
								$canven = 'F'.$canf;
							}


						
						  
						?>
						<th><?php echo $canven;?></th>
						<?php 			
					  }
					  
					  ?>
					
					<td><?php echo $stock ?></td>
					<td><?php echo $can_trans ?></td>
					
					<td width="200" >
					<input type="text" value="<?php if($discar_can!="" ) echo $discar_can*1 ?>" placeholder="Ingrese cantidad"  class="form-control txt_cantidad" 
					data-cat_id="<?php echo $_POST['cat_id'] ?>" data-almdes_id="<?php echo $alm_id?>" 
				    data-lab_id="<?php echo $_POST['lab_id']?>" />
					</td>
					</tr>
					<?php
					}
					
					?>
					
					</table  >
</div>
</div>
</div>
</div>
</div>
</div>
</section >


<script type="text/javascript">



	$(function() {

		
    $('.txt_cantidad').keydown(function(e){
		if (e.which == 13){
			almori_id = $(this).attr("data-almori_id");
			almdes_id = $(this).attr("data-almdes_id");
			lab_id = $(this).attr("data-lab_id");
			cat_id = $(this).attr("data-cat_id");
			can = $(this).val();
			
			$.ajax({
			type: "POST",
			url: "../distribucion/distribucion_reg.php",
			async:true,
			dataType: "json",                      
			data: ({
				almori_id:'<?php echo $_POST['almorigen_id'] ?>',
				almdes_id:almdes_id,
				lab_id:lab_id,
				cat_id:cat_id,
				can:can,
				action:"agregarcantidad"
			}),
			beforeSend: function(){
				
			},
			success: function(data){
				if(data.est==0){
					
				toastr["error"](data.msj);
				}else {
					toastr["success"](data.msj);
				}
				
			}
		});
			return false;}
	});


	});



</script>
	
	
	


