<?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

include 'menu/header.php';
$txt="";
$date = date("Y-m-d");
$Valid_ok=0;

if (isset($_GET['date_lot_entre']) ){
$date_lot_entre = $_GET['date_lot_entre'];
} else {
$date_lot_entre = $date ;
}

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_trace_entre']) ){
$num_trace_entre = $_GET['num_trace_entre'];
} else {
$num_trace_entre = '';
}
//Cbarre_entre
if (isset($_GET['Cbarre_entre']) ){
$Cbarre_entre = $_GET['Cbarre_entre'];
} else {
$Cbarre_entre = '';
}
if (isset($_GET['dlc_plu_temp']) ){
$dlc_plu_temp = $_GET['dlc_plu_temp'];
} else {
$dlc_plu_temp = '';
}

// titre page
echo "<div class='titre-text'><center>Mouvements de Stock - Entr&eacute;es</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 "<center>" ;
echo "<table border='0' cellpadding='1' cellspacing='1' width='800'><tr>";
echo "<tr>";
echo "<td class='t-text' width='150'><center><label>Date :</label><input type='date' name='date_lot_entre' value='$date_lot_entre' onchange='submit()'>";
echo "</div></td>" ;
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 `decoupe`  ";
	$sql1.=" WHERE IdNumClient='".$IdNumClient."'";
	$sql1.=" GROUP BY num_client ASC ";  
	$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>";

	$SelAnne_y = date("Y", strtotime($date));
	$SelDeb = date("Y-m-d", mktime(0,0,0,01,01,$SelAnne_y));
	$SelFin = date("Y-m-d", mktime(0,0,0,12,31,$SelAnne_y));
	
	$sql = "SELECT num_lot_decoupe, count(IdNumClient) FROM `lot`  ";
	$sql.=" WHERE Num_Client ='".$num_client_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";
	$sql.=" AND date_lot_decoupe BETWEEN '".$SelDeb."' AND '".$SelFin."'";
	$sql.=" GROUP BY date_lot_decoupe DESC, 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=3;     
    }  else {
       echo "<option value='$num_lot'>$num_lot</option> ";     
    }     
  }  
 echo "</select>";
 $txt="";
if ($Valid_ok==3) {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; Tra&ccedil;abilit&eacute;e:</label><select name='num_trace_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_tace_decoupe, count(IdNumClient) FROM lot";
	$sql.= " WHERE num_lot_decoupe='$num_lot_entre' AND (num_cate_decoupe!='1' AND num_cate_decoupe!='4')";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" GROUP BY num_lot_tace_decoupe ";
	$req = mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
        $num_trace = $row["num_lot_tace_decoupe"];
        
       if ($num_trace==$num_trace_entre) {
       echo "<option value='$num_trace' selected>$num_trace</option> "; 
       $Valid_ok=4;      
    }  else {
       echo "<option value='$num_trace'>$num_trace</option> ";     
    }     
  }  
echo "</select>";

if ($Valid_ok==4) {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Apply.png'>";
		echo "</div></td>" ;		
		echo "<td width='17' class='t-text'><img src='images/Apply.png'></td>";
		} else {
		echo"&nbsp;&nbsp;&nbsp;<img src='images/Alert.png'>";
		echo "</div></td>" ;		
		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='750' id='customers'>";
echo "<tr>";
echo "<th width='20'>-</th>" ;
echo "<th width='100'>Date</th>" ;
echo "<th width='50'&gt;N&deg; Plu</th>" ;
echo "<th width='200'>Nom PLu</th>" ;
echo "<th width='100'>dlc</th>" ;
echo "<th width='150'>Poids</th>" ;
echo "<th width='150'>Nb Pi</th>" ;
echo "<th width='50'>D</th>" ;
echo "</tr>";
$li=0 ;
$totalPoids=0;
$totalPi=0;

	$sql="select * FROM temp_entre_stock" ;
	$sql.=" WHERE num_client='".$num_client_entre."'";
	$sql.=" AND num_lot='".$num_lot_entre."'";
	$sql.=" AND num_traca='".$num_trace_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" ORDER BY  `num_ligne` ASC";
	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
	        $num_ligne = $row["num_ligne"];
	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["poids"];
	        $nb_pi_temp = $row["nb_pi"];
	        $date_dlc_temp = $row["date_dlc"];
					$date_sais_temp = $row["date_entre"];
					
			$totalPoids = $totalPoids + $poids_temp ;
			$totalPi = $totalPi + $nb_pi_temp ;
		
					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu_temp."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$nom_plu_temp = $row["PLU"];
						$dlc_plu_temp = $row["dlc_plu"];
						} 
$li=$li+1;								
$poids_temp = number_format($poids_temp, 2, '.', ' ')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', ' ')	;
$totalPi = number_format($totalPi, 0, '.', ' ')	;
$totalPoids = number_format($totalPoids, 2, '.', ' ')	;

$date_sais_temp = date('d-m-Y', strtotime($date_sais_temp)) ;
$date_dlc_temp = date('d-m-Y', strtotime($date_dlc_temp)) ;
echo "<tr>";
echo "<td width='20' style='text-align:center'>$li</td>" ;
echo "<td width='50' style='text-align:center'>$date_sais_temp</td>" ;
echo "<td width='50' style='text-align:center'>$num_plu_temp</td>" ;
echo "<td width='200' style='text-align:left'>$nom_plu_temp</td>" ;
echo "<td width='50' style='text-align:center'>$date_dlc_temp</td>" ;
echo "<td width='150' style='text-align:right'>$poids_temp Kg</td>" ;
echo "<td width='150' style='text-align:right'>$nb_pi_temp Pi</td>" ;
echo "<td width='50' style='text-align:center'><a href='delete_ligne_stock_temp.php?ligne=$num_ligne'><img src='images/Erase.png'></td>" ;
echo "</tr>" ;
	}					 


echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=5 align='right'>Total :</td>" ;
echo "<td width='150' align='right'>$totalPoids Kg</td>" ;
echo "<td width='150' align='right'>$totalPi Pi</td>" ;
echo "<td width='50' align='center'>&nbsp; </td>" ;        
echo "</tr>" ;
echo "</table>";        
echo "<br>";        
// http://192.168.1.200/orius/stock_entre.php?date_lot_entre=2017-07-14&num_client_entre=8&num_lot_entre=H24141817&num_trace_entre=165T
echo "<form id='ajoutLigne' name ='ajoutLigne' method='GET' action='".$_SERVER['PHP_SELF']."'>";
echo "<input type='hidden' name='date_lot_entre' value='$date_lot_entre'/>";  
echo "<input type='hidden' name='num_client_entre' value='$num_client_entre'/>";  
echo "<input type='hidden' name='num_lot_entre' value='$num_lot_entre'/>";  
echo "<input type='hidden' name='num_trace_entre' value='$num_trace_entre'/>";  
echo "<center>" ;
echo "<table border='0' cellpadding='1' cellspacing='1' width='700'><tr>";
echo "<tr>";
if ($Cbarre_entre>0){
echo "<td class='t-text' align='center' width='200'><label>Code Barre :</label><input type='text' name='Cbarre_entre' value='$Cbarre_entre'></td>";
} else {
echo "<td class='t-text' align='center' width='200'><label>Code Barre :</label><input type='text' name='Cbarre_entre' value='$Cbarre_entre' autofocus onchange='submit()'></td>";	
}

echo "</form>";
echo "<form id='ajoutLigne' name ='ajoutLigne' method='GET' action='ajout_entre_stock_temp.php'>";
echo "<input type='hidden' name='date_lot_entre' value='$date_lot_entre'/>";  
echo "<input type='hidden' name='num_client_entre' value='$num_client_entre'/>";  
echo "<input type='hidden' name='num_lot_entre' value='$num_lot_entre'/>";  
echo "<input type='hidden' name='num_trace_entre' value='$num_trace_entre'/>";  
echo "<input type='hidden' name='dlc_plu_temp' value='$dlc_plu_temp'/>"; 
$Cb_plu = CodeBarrePlu ($Cbarre_entre) ;
$Cb_Poids = CodeBarrePoids ($Cbarre_entre) ;

echo "<td class='t-text' align='center' width='150'><label>Plu :</label><select name='num_plu_saisie' style='width:200px;' required>";
echo "<option value=''>*-*</option>";
					$sql="select * FROM plu" ;
					$sql.=" WHERE IdNumClient='".$IdNumClient."'"; 
					$sql.=" GROUP BY Num_PLU "; 
					$req=mysqli_query($link, $sql);  
					while ($row = mysqli_fetch_assoc($req)) {
									$Num_plu_saisi = $row["Num_PLU"];						
					        $Nom_plu_saisi = $row["PLU"];
			
			 if ($Cb_plu ==$Num_plu_saisi) {
       echo "<option value='$Num_plu_saisi' selected>$Num_plu_saisi | $Nom_plu_saisi</option> "; 
       $Valid_ok=4;      
    	}  else {
       echo "<option value='$Num_plu_saisi'>$Num_plu_saisi | $Nom_plu_saisi</option> ";      
    	}		             
					} 			
 echo "</select></td>";
echo "<td class='t-text' align='center' width='200'><label>Poids</label><input type='text' name='poids_saisie' value='$Cb_Poids' size='5' required>&nbsp;&nbsp;Kg</td>";
echo "<td class='t-text' align='center' width='100'><label>Nb Pi</label><input type='text' name='pi_saisie' autofocus required onchange='submit()'></td>";
echo "<td class='t-text' align='center' width='20'><input type='submit' value='+'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<form id='IcrStock' name ='IcrStock' method='GET' action='ajout_entre_stock.php'>";
echo "<input type='hidden' name='date_lot_entre' value='$date_lot_entre'/>";  
echo "<input type='hidden' name='num_client_entre' value='$num_client_entre'/>";  
echo "<input type='hidden' name='num_lot_entre' value='$num_lot_entre'/>";  
echo "<input type='hidden' name='num_trace_entre' value='$num_trace_entre'/>";  
echo "<br><br><input type='submit' value='Ajout entre Stock'><br><br>";
echo "</form>";
include 'menu/footer.php';
	} else {

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