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

// entete de la page

include 'menu/header.php';

if (!isset($_GET['SelAnne_y']))
{
    $SelAnne_y=date("Y");
    $SelAnne_y_sel=date("Y");
} else {
    $SelAnne_y=htmlentities($_GET['SelAnne_y']);
    $SelAnne_y_sel=htmlentities($_GET['SelAnne_y']);
}

if (!isset($_GET['lot_txt']))
{
    $lot_txt="";
    $lot_txt_sel="";
} else {
    $lot_txt=htmlentities($_GET['lot_txt']);
    $lot_txt_sel=htmlentities($_GET['lot_txt']);
}


if (!isset($_GET['SelSem_w']))
{
    $SelSem_w=0;
    $SelSem_w_sel=0;
} else {
    $SelSem_w=htmlentities($_GET['SelSem_w']);
    $SelSem_w_sel=htmlentities($_GET['SelSem_w']);
}

if (!isset($_GET['Num_CatPlu']))
{
    $Num_CatPlu=0;
    $Num_CatPlu_sel=0;
} else {
    $Num_CatPlu=($_GET['Num_CatPlu']);
    $Num_CatPlu_sel=($_GET['Num_CatPlu']);
}

if (!isset($_GET['Num_Client']))
{
    $Num_Client=0;
    $Num_Client_sel=0;
} else {
    $Num_Client=($_GET['Num_Client']);
    $Num_Client_sel=($_GET['Num_Client']);
}

if (!isset($limite)) $limite = 0;
$nombre = 10;
$limitePage = 0 ;

$v='select * FROM stock ';
$selectc = 'SELECT count(Num_ligne_qtv) FROM quantitative_haut';
if (($Num_Client || $Num_CatPlu || $SelAnne_y || $SelSem_w)>0) {
$v.=' WHERE' ;
$selectc.=' WHERE' ;
} else { 
    $v.=''; 
    $selectc.='';
    }

if ($SelSem_w==0) {
$v.='';
$selectc.='';
} else { 
    $v.=' Semaine='.$SelSem_w;
    $selectc.=' Semaine='.$SelSem_w; 
    }

if ($Num_Client==0) {
$v.='';
$selectc.='';
} else { 
    if (($Num_CatPlu || $SelSem_w)>0) {
    $v.=' AND num_client='.$Num_Client;
    $selectc.=' AND num_client='.$Num_Client;
    } else {
    $v.=' num_client='.$Num_Client;
    $selectc.=' num_client='.$Num_Client;    
    }
}



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

if (($Num_Client || $Num_CatPlu || $SelSem_w)>0) {
$v.=" AND Date BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND Date BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" Date BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" Date BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}

if (empty ($lot_txt) ) {
$v.='';
$selectc.='';
} else { 
    $v.=" AND  Num_Lot LIKE '%".$lot_txt."%'";
    $selectc.=" AND  Num_Lot LIKE '%".$lot_txt."%'";
    }

$v.=" AND IdNumClient='".$IdNumClient."'";
$v.=' ORDER BY Date DESC limit '.$limite.','.$nombre;

$SelectEA = $v ;
$date = date("d-m-Y");
/*
echo "<center><HR size='2' width='500'>";
echo $v."<br>".$SelectEA;
echo "<HR size='2' width='500'>";
echo " $total | $SelSem_w | $semaine | $Num_CatPlu | $Num_Client | $SelAnne_y | $SelDeb | $SelFin<br></center>" ;
echo "<HR size='2' width='500'>";
*/

echo "<center>";


  // on va afficher 5 résultats par page.
$limitesuivante = $limite + $nombre;
$limiteprecedente = $limite - $nombre;
$selectc = 'SELECT count(Num_ligne_qtv) FROM quantitative_haut';
$req = mysqli_query($link, $selectc); 
$row = mysqli_fetch_row($req);
$total= $row[0];
$NbPage =floor($total/10);

// titre page
echo "<div class='titre-text'><center>Mouvements de Stock</div></center><br>" ;

// Filtres

echo "<form id='form1' name='form1' method='get' action=''>";
echo "<input type='hidden' name='limite' value='$limite'/>";
Echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
Echo "<tr><td colspan=5><span class='t-text'><center>FILTRES<br><HR size='1' width='500'></center></td></tr>";
echo "<tr class='text'>";
echo "<td>Par ann&eacute;e :";

  $class = '';
  $fyear='2011';
  $lyear='2050';
  $name = 'SelAnne';

  $yr=date("Y");
  // Année 
  
  $s.="<SELECT NAME=\"${name}_y\" class='$class'>\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>";
echo "<td>Par semaine :";

  $class = '';
  $fyear='1';
  $lyear='53';
  $name = 'SelSem';

  $yr=date("W");
  // Année 
  
  $s="<SELECT style=' width: 65px;' NAME=\"${name}_w\" class='$class'>\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";
  $sclt=""; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";
echo "</td>";

echo "<td>Par Client : <select style=' width: 60px;' name='Num_Client' id='Num_Client'>";
echo "<option value='0'>Tous</option>";
$myart =new clients;
$select = 'select * FROM clients';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $Num_Client=$myart->Num_Client ;
  $Let_Client=$myart->Let_Client ;
  $Nom_Client=$myart->Nom_Client ;
echo "<option value='$Num_Client'>$Num_Client | $Let_Client | $Nom_Client</option>";
}
echo "</select>";
echo "</td>";
echo "<td>N&deg; Lot : <input type='text' name='lot_txt' id='lot_txt' placeholder='Ex : H04415114' size='30' maxlength='10'>";
echo "</td>";
echo "<td><input type='submit' value='ok'>";
echo "</td>";
Echo "</tr>";
Echo "</span>";
Echo "</table>";
echo "</form></center>";
echo "<center><HR size='1' width='500'>";

//echo "total : ".$total."| limite  ".$limite."<br> limite suivante ".$limitesuivante."| limite precedente".$limiteprecedente ."<br>";

// *-* fin filtres *-*

// detail page

//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='800' height='22'><tr>";
echo "<td width='50' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Date</div></td></span>";
echo "<td width='20' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>L</td></span>";
echo "<td width='100' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Num lot</td></span>";
echo "<td width='50' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Num Tra&#231;abilit&#233;</td></span>";
echo "<td width='100' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>PLU</td></span>";
echo "<td width='50' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Pi</td></span>";
echo "<td width='150' colspan='2'  height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Poids</td></span>";
echo "<td width='50' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>DLC</td></span>";
echo "<td width='150' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Obserevations</td></span>";	
echo "<td width='32' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>V</td></span>";
echo "<td width='32' rowspan='2' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>D</td></span>";
echo "</tr>";
echo "<tr>";
echo "<td valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Entr&eacute;e</span></div></td>";
echo "<td valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Sortie</span></div></td>";
echo "</tr>";


$myart =new stock;
$result = mysqli_query($link,$SelectEA);
$myart->getnum($result);
while ($myart->z < $myart->num) {
	$myart->readrow($result);
	$IdNumStock=$myart->IdNumStock ;
  $IdNumClient=$myart->IdNumClient ;
  $Date=$myart->Date ; 
  $Semaine=$myart->Semaine ;
  $Num_Client=$myart->Num_Client ;
  $Num_Lot = $myart->Num_Lot ;
  $Num_Traca=$myart->Num_Traca ; 
  $Num_Plu=$myart->Num_Plu ;	
  $Date_Etiq=$myart->Date_Etiq ;    
  $DLC_Etiq=$myart->DLC_Etiq ; 
  $Poids=$myart->Poids ;
  $Nb_Piece	=$myart->Nb_Piece ;
  $EtatES=$myart->EtatES ;    
  $Solde=$myart->Solde ; 
  $Observations=$myart->Observations ;	
  $Timestamp=$myart->Timestamp ; 

$sql = "SELECT Let_Client FROM clients WHERE Num_Client='$Num_Client'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Let_Client = $row["Let_Client"];
    } 

$sql = "SELECT * FROM plu WHERE Num_PLU='$Num_Plu'";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $nom_plu = $row["PLU"];
        $Dlc_plu = $row["dlc_plu"];
    } 

$Date_Etiq=date("d/m/Y", strtotime($Date_Etiq));
$DLC_Etiq=date("d/m/Y", strtotime($DLC_Etiq));


//
//echo  date("d/m/Y", strtotime($Date_Entre))." | ".$date_e." |=> ". $date_jours." | ";
//

echo "<tr><td width='50' height='20' valign='center'><div align='center'><span class='t-text'>$Date_Etiq</div></td></span>";
echo "<td width='20 height='20' valign='center'><div align='center'><span class='t-text'>$Let_Client</div></td></span>";
echo "<td width='100' height='20' valign='center'><div align='center'><a><span class='t-text'>$Num_Lot </div></a></td></span>";
echo "<td width='50' height='20' valign='center'><div align='center'><span class='t-text'>$Num_Traca</div></a></td></span>";
echo "<td width='150' height='20' valign='center'><div align='center'><span class='t-text'>$nom_plu</div></td></span>";
echo "<td width='20' height='20' valign='center'><div align='center'><span class='t-text'>$Nb_Piece</div></td></span>";

		switch ($EtatES) { 	
 		case 0 :
echo "<td width='75' height='20' valign='center'><div align='center'><span class='t-text'>$Poids Kg</div></td></span>";
echo "<td width='75' height='20' valign='center'><div align='center'><span class='t-text'>*-*</div></td></span>";
	
		break;
		case 1 :
echo "<td width='75' height='20' valign='center'><div align='center'><span class='t-text'>*-*</div></td></span>";
echo "<td width='75' height='20' valign='center'><div align='center'><span class='t-text'>$Poids Kg</div></td></span>";
		
		break;
		}

if ($date>$DLC_Etiq AND $Solde==0){
echo "<td width='50' height='20' valign='center'><div align='center'><span class='t-textRge'>$DLC_Etiq</div></td></span>";
} else {
echo "<td width='50' height='20' valign='center'><div align='center'><span class='t-textVert'>$DLC_Etiq</div></td></span>";

}

echo "<td width='150' height='20' valign='center'><div align='center'><span class='t-text'>$Observations </div></td></span>";		
	switch ($Solde) { 	
 		case 0 :
 		echo "<td width='20' height='20' valign='center'><div align='center'><span class='t-text'>
		      <img src='images/croix.gif' width='20' height='20'></div></td></span>";
		break ;
		case 1 :
		echo "<td width='20' height='20' valign='center'><div align='center'><span class='t-text'>
		      <img src='images/coche.gif' width='20' height='20'></div></td></span>";
		
		break ;
		}
echo "<td width='20' height='20' valign='center'><div align='center'><a class='' href=''><img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";
} 
echo "</tr></table><br>";

// limites
echo "<table border='1' cellpadding='0' cellspacing='0' width='500' height='22'><tr>";
echo "<td width='120' height='20' valign='center'><div align='center'><span class='t-text'>";
	 
if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente.'">Page precedente  </a>';
}

for ($a = 1; $a <= 10; $a++) {
    $limitePage = $nombre + $limitePage;
    echo '<a href="'.$page.'?limite='.$limitePage
    .'&SelSem_w='.$SelSem_w_sel
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
    .'">'.$a.'</a> | ';
}

if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
.'&SelSem_w='.$SelSem_w_sel
.'&Num_CatPlu='.$Num_CatPlu_sel
.'&Num_Client='.$Num_Client_sel
.'">Page Suivante</a>';
}
echo "</div></td></span>";
echo "";
echo "</tr></table>";


include 'menu/footer.php';

?>
