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



// *-* 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='950'><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'><center><label>Par ann&eacute;e :</label>";
// Année 
  $fyear='2017';
  $lyear='2050';
  $name = 'Sel_y';

  $yr=$Sel_y;
  
  $s.="<SELECT style=' width: 65px;' NAME=\"${name}\" required onchange='submit()'>\n";
  $s.="<OPTION VALUE='0' SELECTED>Toutes</OPTION>\n";
  $s.="<OPTION VALUE=$yr>$yr</OPTION>\n";
  $sclt=""; 
  for ($i=$fyear;$i<=$lyear;$i++)
  { 
  $i2="$i"; 
  if ($i2==$yr) 
  $sclt=" SELECTED"; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";
echo "</td>";
//semaine
echo "<td class='t-text'><center><label>Par semaine :</label>";


  $DSem='1';
  $FSem='53';
  $name='Sel_w';

  $yr=$Sel_w ;
  
  $s="<SELECT style=' width: 65px;' NAME=\"${name}\" required onchange='submit()'>\n";
  $s.="<OPTION VALUE='0' SELECTED>Toutes</OPTION>\n";
  $s.="<OPTION VALUE=$yr>$yr</OPTION>\n";
  $sclt=""; 
  for ($i=$DSem;$i<=$FSem;$i++)
  { 
  $i2="$i"; 
  if ($i2==$yr) 
  $sclt=" SELECTED"; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";
echo "</td>";

$SelDeb = date("Y-m-d", mktime(0,0,0,01,01,$Sel_y));
$SelFin = date("Y-m-d", mktime(0,0,0,12,31,$Sel_y));

// Num Lot

echo "<td class='t-text' width='180'><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, count(IdNumClient) FROM `stock`  ";
	$sql.=" WHERE Num_Client ='".$num_client_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";
	
	if ($Sel_w>=1) {
	$sql.=" AND Semaine='".$Sel_w."'";
	}

	$sql.=" AND Date_Etiq BETWEEN '".$SelDeb."' AND '".$SelFin."'";
	$sql.=" GROUP BY Num_Lot ";   

	
	
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_lot = $row["Num_Lot"];
       
       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>";
// echo "<br>$sql<br>";
 $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>" ;

// PLU

echo "<td class='t-text' width='300'><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"];
        $plu_c = $row["Plu_complement"];
              
       if ($num_plu_entre==$num_plu) {
       	
       echo "<option value='$num_plu' selected>$num_plu | $plu $plu_c</option> ";  
  $Valid_ok_opt=1;
    }  else {
       echo "<option value='$num_plu'>$num_plu | $plu $plu_c</option> ";     
    }     
  }  }
 echo "</select>";
 $txt="";
 if ($Valid_ok_opt==1) {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Apply.png'>";


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

		}
/*
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"&nbsp;&nbsp;&nbsp;&nbsp;<a class='' href='edition_mvt_stock.php?ordre=1&num_client_entre=$num_client_entre&Sel_y=$Sel_y&Sel_w=$Sel_w&num_lot_entre=$num_lot_entre&num_plu_entre=$num_plu_entre' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></div></td>";
echo "</div></td>" ;
echo "</tr></form>";
echo "</table><br>";

if ($Valid_ok==4) {

echo "<center>" ;
echo "<table width='950' id='customers'>";
echo "<tr>";
echo "<th width='50' rowspan='2'>Date</th>" ;
echo "<th width='50' rowspan='2'>N&deg; Plu</th>" ;
echo "<th width='300' rowspan='2'>Nom PLu</th>" ;
echo "<th width='50' rowspan='2'>Traca</th>" ;
echo "<th width='50' rowspan='2'>dlc</th>" ;
echo "<th width='150' rowspan='2'>observations</th>" ;
echo "<th width='150' colspan='2'>Entr&eacute;e</th>" ;
echo "<th width='150' colspan='2'>Sortie</th>";
echo "<th width='20' rowspan='2'>D</th>" ;
echo "<th width='20' rowspan='2'>C</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_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"];
	        $solde = $row["Solde"];
	        $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"];
						$plu_c = $row["Plu_complement"];
						} 
					$nbcar=strlen($Observations);
					$nbarrestant=$nbcar-9;
					$numbl = substr ($Observations, -$nbarrestant);
	$Selbl_y=date('Y',strtotime($Date_Mvt));
	$SelAnnDebBl = date("Y-m-d", mktime(0,0,0,01,01,$Selbl_y));
	$SelAnnFinBl = date("Y-m-d", mktime(0,0,0,12,31,$Selbl_y));
	
	
								$sql2 = "SELECT * FROM gestion_bl_haut WHERE num_bl='$numbl' AND date_bl BETWEEN '".$SelAnnDebBl."' AND '".$SelAnnFinBl."'";  
								$req2 = mysqli_query($link, $sql2);
								while ($row = mysqli_fetch_assoc($req2)) {
									$num_client_bl = $row["num_client_bl"];
									$num_ligne_bl = $row["num_li_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='50' style='text-align:center'>$Date_Mvt </td>" ;
echo "<td width='50' style='text-align:center'>$num_plu</td>" ;
echo "<td width='300' style='text-align:left'>$nom_plu $plu_c</td>" ;
echo "<td width='50' style='text-align:left'>$Trace </td>" ;
echo "<td width='50' style='text-align:center'>$Date_Dlc</td>" ;




switch ($etat_mvt) { 	
	case 0: 		   
echo "<td width='150' style='text-align:center'>$observations</td>" ;	
echo "<td width='75' style='text-align:right'>$Poids Kg</td>" ;
echo "<td width='75' style='text-align:right'>$nb_pi pi</td>" ;
echo "<td width='75' style='text-align:right'></td>" ;
echo "<td width='75' style='text-align:right'></td>" ;
echo "<td width='20' style='text-align:center'><a href='delete_mvt.php?ordre=3&IdNumStock=$IdNumStock'><img src='images/trash.png' width='20' height='20'></a></td>" ;
if ($solde==0) {
echo "<td width='20' style='text-align:center'><img src='images/fermer.png' width='20' height='20'></a></td>" ;	
} else {
echo "<td width='20' style='text-align:center'><img src='images/ok.png' width='20' height='20'></a></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:center'><a href='' onclick='javascript:Popup(\"detail_bl_liste.php?ordre=1&num_bl=$numbl&numligne=$num_ligne_bl\",1024,600,\"location=0,menubar=0,scrollbars=0,statusbar=0\")'>$Observations</a><br>$NomClt</td>" ;	
echo "<td width='75' style='text-align:right'></td>";
echo "<td width='75' style='text-align:right'></td>" ;
echo "<td width='75' style='text-align:right'>$Poids Kg</td>" ;
echo "<td width='75' style='text-align:right'>$nb_pi pi</td>" ;
echo "<td width='30' style='text-align:center'></td>" ;
if ($solde==0) {
echo "<td width='20' style='text-align:center'><img src='images/fermer.png' width='20' height='20'></a></td>" ;	
} else {
echo "<td width='20' style='text-align:center'><img src='images/ok.png' width='20' height='20'></a></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='75' align='right'>$totalPoids_e Kg</td>" ;
echo "<td width='75' align='right'>$totalPi_e Pi</td>" ;
echo "<td width='75' align='right'>$totalPoids_s Kg</td>" ;
echo "<td width='75' align='right'>$totalPi_s Pi</td>" ;
echo "<td width='30' style='text-align:center'></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='75' align='right'></td>" ;
echo "<td width='75' align='right'></td>" ;
echo "<td width='75' align='right'>$Solde_Poids Kg</td>" ;
echo "<td width='75' align='right'>$Solde_Pi Pi</td>" ;
echo "<td width='30' style='text-align:center'></td>" ;
} ELSE {
echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=6 align='left'>Solde :</td>" ;
echo "<td width='75' align='right'>$Solde_Poids Kg</td>" ;
echo "<td width='75' align='right'>$Solde_Pi Pi</td>" ;
echo "<td width='75' align='right'></td>" ;
echo "<td width='75' align='right'></td>" ;	
echo "<td width='30' style='text-align:center'></td>" ;
}


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



//saisie Entre/sortie Stock
if ($solde==1) {

echo "<form id='ModifStock' name ='ModifStock' method='GET' action='ajout_ligne_stock.php'>";
echo "<input type='hidden' name='ordre' value='1'/>"; 
echo "<input type='hidden' name='num_eleveur' value='$num_client_entre'/>";
echo "<input type='hidden' name='num_lot_entre' value='$num_lot_entre'/>";

echo "<table width='950' id='customers1' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr>";
echo "<th width='20' rowspan='2'>Sortie</th>" ;
echo "<th width='20' rowspan='2'>Entre</th>" ;
echo "<th width='50' rowspan='2'>Date</th>" ;
echo "<th width='350' rowspan='2'>Nom PLu</th>" ;
echo "<th width='75' rowspan='2'>Traca</th>" ;
echo "<th width='150' colspan='2'>Mouvement</th>" ;
echo "<th width='30' rowspan='2'></th>" ;
echo "</tr><tr>";
echo "<th width='75'>Poids </th>" ;
echo "<th width='75'>Nb Pi </th>" ;
echo "</tr>";
echo "<td class='t-text' align='center' width='20'><label>-</label><input type='radio' name='position' value='m'></td>";
echo "<td class='t-text' align='center' width='20'><label>+</label><input type='radio' name='position' value='p' checked></td>";
echo "<td widtd='50' rowspan='2'><input  name='Date_Mvt' type='date'></td>" ;
echo "<td widtd='350' rowspan='2'><select name='num_plu_mvt'></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 * FROM plu "; 
	$sql.="WHERE `IdNumClient`='".$IdNumClient."'" ;
	  
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_plu = $row["Num_PLU"];
        $plu = $row["PLU"];
              
       if ($num_plu_mvt==$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><br>observations :<input type='text' name='observations'size='37'></td>" ;
echo "<td widtd='50' rowspan='2'><input type='text' name='traca_mvt'size='3'></td>" ;
echo "<td widtd='75 'align='center'><input type='text' name='poids_mvt' value='$poids_mvt' size='3'>&nbsp;Kg</td>" ;
echo "<td widtd='75' align='center'><input type='text' name='NbPi_mvt' value='$NbPi_mvt' size='3'>&nbsp;Pi</td>" ;
echo "<td class='t-text' align='center' width='10'><input type='submit' value='+'></td>";
echo "</table>";  
} 



include 'menu/footer.php';
}
?>
