<?php
include("include/data.php");
Init_boutique();
global $base, $MySQL_Host, $MySQL_User, $MySQL_Passw, $myart, $A_NumArt,$i, $result, 
$NbLigne, $mygroupeart, $mysousgroupeart, $semaine, $limitesuivante, $s,
$limiteprecedente, $limite, $total, $page, $Nom_Client, $semaine,$txt ;

// entete de la page

/*
http://192.168.1.200/orius/stock_inter.php?ordre=0&num_client_entre=7&num_lot_entre=C38AGNX17&num_plu_entre=203
*/

include 'menu/header_edition.php';
$txt="";
$date = date("Y-m-d");
$Valid_ok=0;
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		if (isset($_POST['ordre'])) { $num_ordre=$_POST['ordre']; }  else {$num_ordre='0'; }
		if (isset($_POST['num_client_entre'])) { $num_client_entre=$_POST['num_client_entre']; }  else {$num_client_entre=''; }
		if (isset($_POST['Sel_y'])) { $Sel_y=$_POST['Sel_y']; }  else {$Sel_y=date("Y"); }
		if (isset($_POST['Sel_w'])) { $Sel_w=$_POST['Sel_w']; }  else {$Sel_w=''; }
		if (isset($_POST['num_lot_entre'])) { $num_lot_entre=$_POST['num_lot_entre']; }  else {$num_lot_entre=''; }
		if (isset($_POST['num_plu_entre'])) { $num_plu_entre=$_POST['num_plu_entre']; }  else {$num_plu_entre=''; }
		
		$valForm="POST" ;
} else {
		// Transmis en GET
		if (isset($_GET['ordre'])) { $num_ordre=$_GET['ordre']; }  else {$num_ordre='0'; }
		if (isset($_GET['num_client_entre'])) { $num_client_entre=$_GET['num_client_entre']; }  else {$num_client_entre=''; }
		if (isset($_GET['Sel_y'])) { $Sel_y=$_GET['Sel_y']; }  else {$Sel_y=date("Y"); }
		if (isset($_GET['Sel_w'])) { $Sel_w=$_GET['Sel_w']; }  else {$Sel_w=''; }
		if (isset($_GET['num_lot_entre'])) { $num_lot_entre=$_GET['num_lot_entre']; }  else {$num_lot_entre=''; }
		if (isset($_GET['num_plu_entre'])) { $num_plu_entre=$_GET['num_plu_entre']; }  else {$num_plu_entre=''; }
		$valForm="GET" ;
}

// titre page
if (!empty($num_lot_entre)){
 echo "<div class='titre-text'><center>Mouvements de Stock - Int&eacute;rogations Lot N&deg; $num_lot_entre </div>" ;
echo "<HR size='1' width='500'></center>";
} else {
echo "<div class='titre-text'><center>Mouvements de Stock - Int&eacute;rogations</div>" ;
echo "<HR size='1' width='500'></center>";
}



echo "<center>" ;
echo "<table class='t-text' width='900' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr>";
echo "<th width='30' rowspan='2'>Date</th>" ;
echo "<th width='30' rowspan='2'>N&deg; Plu</th>" ;
echo "<th width='150' rowspan='2'>Nom PLu</th>" ;
echo "<th width='50' rowspan='2'>Traca</th>" ;
echo "<th width='30' rowspan='2'>dlc</th>" ;
echo "<th width='250' rowspan='2'>observations</th>" ;
echo "<th width='100' colspan='2'>Entr&eacute;e</th>" ;
echo "<th width='100' colspan='2'>Sortie</th>";
echo "</tr><tr>";
echo "<th width='50'>Poids </th>" ;
echo "<th width='50'>Nb Pi </th>" ;
echo "<th width='50'>Poids </th>" ;
echo "<th width='50'>Nb Pi </th>" ;
echo "</tr>";

$totalPoids_e=0;
$totalPi_e=0;
$totalPoids_s=0;
$totalPi_s=0;

if (!empty($num_plu_entre)) {
		$sql="select * FROM stock" ;
	$sql.=" WHERE Num_client='".$num_client_entre."'";
	$sql.=" AND Num_Lot='".$num_lot_entre."'";
	$sql.=" AND Num_Plu='".$num_plu_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" ORDER BY `Date_Etiq` ASC, `Num_Traca` ASC";
}else{
	$sql="select * FROM stock" ;
	$sql.=" WHERE Num_client='".$num_client_entre."'";
	$sql.=" AND Num_Lot='".$num_lot_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" ORDER BY `Date_Etiq` ASC, `Num_Traca` ASC";
}

//echo $sql ;
	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
			$IdNumStock = $row["IdNumStock"];	
	        $Trace = $row["Num_Traca"];		
		    $Date_Mvt = $row["Date_Etiq"];
	        $Date_Dlc = $row["DLC_Etiq"];
	        $num_plu = $row["Num_Plu"];
	        $Poids = $row["Poids"];
	        $nb_pi = $row["Nb_Piece"];
	        $etat_mvt = $row["EtatES"];
					$Observations = $row["Observations"];
					
					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
//echo "<br>".$sql1 ;
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$nom_plu = $row["PLU"];
						} 
					$nbcar=strlen($Observations);
					$nbarrestant=$nbcar-9;
					$numbl = substr ($Observations, -$nbarrestant);
	
								$sql2 = "SELECT num_client_bl FROM gestion_bl_haut WHERE num_bl='$numbl'";  
								$req2 = mysqli_query($link, $sql2);
								while ($row = mysqli_fetch_assoc($req2)) {$num_client_bl = $row["num_client_bl"];} 
	
								$sql3 = "SELECT Nom_Client FROM clients_base WHERE Num_Client='$num_client_bl'";  
								$req3 = mysqli_query($link, $sql3);
								while ($row = mysqli_fetch_assoc($req3)) {$NomClt = $row["Nom_Client"];}
				
					
								
$Poids = number_format($Poids, 2, '.', ' ')	;
$nb_pi = number_format($nb_pi, 0, '.', ' ')	;

$Date_Mvt = date('d/m/Y', strtotime($Date_Mvt)) ;
$Date_Dlc = date('d/m/Y', strtotime($Date_Dlc)) ;
echo "<tr>";
echo "<td width='100' style='text-align:center'>$Date_Mvt </td>" ;
echo "<td width='50' style='text-align:center'>$num_plu</td>" ;
echo "<td width='200' style='text-align:left'>$nom_plu</td>" ;
echo "<td width='100' style='text-align:left'>$Trace </td>" ;
echo "<td width='100' style='text-align:center'>$Date_Dlc</td>" ;




switch ($etat_mvt) { 	
	case 0: 		   
echo "<td width='100' style='text-align:center'>$observations</td>" ;	
echo "<td width='150' style='text-align:right'>$Poids Kg</td>" ;
echo "<td width='150' style='text-align:right'>$nb_pi pi</td>" ;
echo "<td width='150' style='text-align:right'></td>" ;
echo "<td width='150' style='text-align:right'></td>" ;
$totalPoids_e = $totalPoids_e + $Poids ;
$totalPi_e = $totalPi_e + $nb_pi ;
$totalPoids_e = number_format($totalPoids_e, 2, '.', ' ')	;
$totalPi_e = number_format($totalPi_e, 0, '.', ' ')	;
break;

	case 1:
echo "<td width='100' style='text-align:center'>$Observations<br>$NomClt</td>" ;	
echo "<td width='150' style='text-align:right'></td>";
echo "<td width='150' style='text-align:right'></td>" ;
echo "<td width='150' style='text-align:right'>$Poids Kg</td>" ;
echo "<td width='150' style='text-align:right'>$nb_pi pi</td>" ;
$totalPoids_s = $totalPoids_s + $Poids ;
$totalPi_s = $totalPi_s + $nb_pi ;
$totalPoids_s = number_format($totalPoids_s, 2, '.', ' ')	;
$totalPi_s = number_format($totalPi_s, 0, '.', ' ')	;
	break;
	 }
echo "</tr>" ;
	}					 


echo "<tr style='background-color:grey;'>" ;
echo "<td colspan=6 align='left'>Total :</td>" ;
echo "<td width='150' align='right'>$totalPoids_e Kg</td>" ;
echo "<td width='150' align='right'>$totalPi_e Pi</td>" ;
echo "<td width='150' align='right'>$totalPoids_s Kg</td>" ;
echo "<td width='150' align='right'>$totalPi_s Pi</td>" ;
echo "</tr>" ;

$Solde_Poids=$totalPoids_e-$totalPoids_s ;
$Solde_Pi=$totalPi_e-$totalPi_s ;
$Solde_Poids = number_format($Solde_Poids, 2, '.', ' ')	;
$Solde_Pi = number_format($Solde_Pi, 0, '.', ' ')	;

if ($Solde_Poids>=0) {
echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=6 align='left'>Solde :</td>" ;
echo "<td width='150' align='right'></td>" ;
echo "<td width='150' align='right'></td>" ;
echo "<td width='150' align='right'>$Solde_Poids Kg</td>" ;
echo "<td width='150' align='right'>$Solde_Pi Pi</td>" ;
} ELSE {
echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=6 align='left'>Solde :</td>" ;
echo "<td width='150' align='right'>$Solde_Poids Kg</td>" ;
echo "<td width='150' align='right'>$Solde_Pi Pi</td>" ;
echo "<td width='150' align='right'></td>" ;
echo "<td width='150' align='right'></td>" ;	
}


echo "</tr>" ;
echo "</table>";        
echo "<br>"; 


include 'menu/footer.php';

?>
