<?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.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['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['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>";
}



// *-* fin filtres *-*

// detail page
echo "<form id='interolot' name ='interolot' method='GET' action='".$_SERVER['PHP_SELF']."'>";
echo "<input type='hidden' name='ordre' value='$num_ordre'>"; 
echo "<center>" ;
echo "<table border='0' cellpadding='1' cellspacing='1' width='800'><tr>";
echo "<tr>";
echo "<td class='t-text' width='220'><center><label>N&deg; Client :</label><select name='num_client_entre' required onchange='submit()'></span>";
echo "<option value=''>??</option>";
	$sql1 = "SELECT Num_Client, count(Num_Client) FROM `stock`  ";
	$sql1.=" WHERE EtatES=0";
	$sql1.=" AND IdNumClient='".$IdNumClient."'";
	$sql1.=" GROUP BY Num_Client ";  
	$req1 = mysqli_query($link, $sql1);  
	while ($row = mysqli_fetch_assoc($req1)) {
				$num_client_eleveur = $row["Num_Client"];

	$sql ="SELECT * FROM clients "; 
	$sql.="WHERE Num_Client='$num_client_eleveur' ";
	$sql.="AND `IdNumClient`='".$IdNumClient."'" ;
	  
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_client = $row["Num_Client"];
        $nom_Client = $row["Nom_Client"];
        $let_Client = $row["Let_Client"];
        
       if ($num_client_entre==$num_client) {
       echo "<option value='$num_client' selected>$let_Client | $nom_Client</option> ";
       $let_Clientv=$let_Client ;       
			 $Valid_ok=2;
    }  else {
       echo "<option value='$num_client'>$let_Client | $nom_Client</option> "; 
          
    }     
  } } 
 echo "</select>";
if ($Valid_ok==2) {echo"&nbsp;&nbsp;&nbsp;<img src='images/Apply.png'>";} else {echo"&nbsp;&nbsp;&nbsp;<img src='images/Alert.png'>";}
echo "</div></td>" ;
echo "<td class='t-text' width='200'><center><label>N&deg; Lot :</label><select name='num_lot_entre' required onchange='submit()'></center>";
for ($i=1; $i<=10; $i++) {$txt.="&nbsp;"; }
$txt.="&nbsp;&nbsp;*-*-*&nbsp;&nbsp;" ;
for ($i=1; $i<=10; $i++) {$txt.="&nbsp;"; }
echo "<option value=''>$txt</option>";
	$sql = "SELECT num_lot_decoupe, count(IdNumClient) FROM `lot`  ";
	$sql.=" WHERE Num_Client ='".$num_client_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";
	$sql.=" GROUP BY num_lot_decoupe ";   
	
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_lot = $row["num_lot_decoupe"];
       
       if ($num_lot_entre==$num_lot) {
       echo "<option value='$num_lot' selected>$num_lot</option> ";  
       $Valid_ok=4;     
    }  else {
       echo "<option value='$num_lot'>$num_lot</option> ";     
    }     
  }  
 echo "</select>";
 $txt="";
 if ($Valid_ok==4) {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Apply.png'>";
	} else {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Alert.png'>";
		}
echo "</div></td>" ;
echo "<td class='t-text' width='200'><center><label>N&deg; PLU :</label><select name='num_plu_entre' onchange='submit()'></center>";
for ($i=1; $i<=10; $i++) {$txt.="&nbsp;"; }
$txt.="&nbsp;&nbsp;*-*-*&nbsp;&nbsp;" ;
for ($i=1; $i<=10; $i++) {$txt.="&nbsp;"; }
echo "<option value=''>$txt</option>";
	$sql1 = "SELECT Num_Plu, count(IdNumClient) FROM `stock`  ";
	$sql1.=" WHERE Num_Lot ='".$num_lot_entre."'";
	$sql1.=" AND IdNumClient='".$IdNumClient."'";
	$sql1.=" GROUP BY Num_Plu ";   
	
	$req1 = mysqli_query($link, $sql1);  
	while ($row = mysqli_fetch_assoc($req1)) {
        $num_plu_stock = $row["Num_Plu"];
   
  $sql ="SELECT * FROM plu "; 
	$sql.="WHERE Num_PLU='$num_plu_stock' ";
	$sql.="AND `IdNumClient`='".$IdNumClient."'" ;
	  
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_plu = $row["Num_PLU"];
        $plu = $row["PLU"];
              
       if ($num_plu_entre==$num_plu) {
       echo "<option value='$num_plu' selected>$num_plu | $plu</option> ";  
  $Valid_ok_opt=1;
    }  else {
       echo "<option value='$num_plu'>$num_plu | $plu</option> ";     
    }     
  }  }
 echo "</select>";
 $txt="";
 if ($Valid_ok_opt==1) {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Apply.png'>";
		echo "</div></td>" ;		

	} else {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/key.png'>";
		echo "</div></td>" ;		

		}
/*
if ($Valid_ok==4) {
		echo "<td width='17' class='t-text'><img src='images/Apply.png'></td>";
		} else {
		echo "<td width='17' class='t-text'><img src='images/Erase.png'></td>";
		}
*/
echo "</div></td>" ;
echo "</tr></form>";
echo "</table><br>";

if ($Valid_ok==4) {

echo "<center>" ;
echo "<table width='850' id='customers'>";
echo "<tr>";
echo "<th width='100' rowspan='2'>Date</th>" ;
echo "<th width='50' rowspan='2'>N&deg; Plu</th>" ;
echo "<th width='200' rowspan='2'>Nom PLu</th>" ;
echo "<th width='100' rowspan='2'>Traca</th>" ;
echo "<th width='100' rowspan='2'>dlc</th>" ;
echo "<th width='150' colspan='2'>Entr&eacute;e</th>" ;
echo "<th width='150' colspan='2'>Sortie</th>";
echo "</tr><tr>";
echo "<th width='75'>Poids </th>" ;
echo "<th width='75'>Nb Pi </th>" ;
echo "<th width='75'>Poids </th>" ;
echo "<th width='75'>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` 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` ASC, `Num_Traca` ASC";
}

//echo $sql ;
	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
	        $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"];
		
					$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"];
						} 
								
$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='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='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=5 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=5 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=5 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';
}
?>
