<?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['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_cate_intrants']))
{
    $num_cate_intrants=0;
    $num_cate_intrants_sel=0;
} else {
    $num_cate_intrants=($_GET['num_cate_intrants']);
    $num_cate_intrants_sel=($_GET['num_cate_intrants']);
}
if (!isset($_GET['pages']))
{ $NombrePages=10;} else { $NombrePages=($_GET['pages']);}

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

$nombre = $NombrePages ;
$limitePage = 0 ;
$PageC = ($limite/$NombrePages) ;

$v='select * FROM intrants ';
$selectc = 'SELECT count(Num_entre_intrant) FROM intrants';
if (($num_cate_intrants || $SelAnne_y || $SelSem_w)>0) {
$v.=' WHERE' ;
$selectc.=' WHERE' ;
} else { 
    $v.=''; 
    $selectc.='';
    }

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

if ($num_cate_intrants==0) {
$v.='';
$selectc.='';
 } else { 
   if ($SelSem_w!=0) {
    $v.=' AND num_cate_intrants ='.$num_cate_intrants; 
    $selectc.=' AND num_cate_intrants ='.$num_cate_intrants; 
     } else {
    $v.=' num_cate_intrants ='.$num_cate_intrants; 
    $selectc.=' num_cate_intrants ='.$num_cate_intrants; 
    }
}


$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_cate_intrants || $SelSem_w)>0) {
$v.=" AND Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}

$v.=" AND IdNumClient='".$IdNumClient."'";
$selectc.=" AND IdNumClient='".$IdNumClient."'";

$v.=' ORDER BY Num_Lot DESC limit '.$limite.','.$nombre;

$SelectEA = $v ;
$date = date("d-m-Y");

// on va afficher 10 résultats par page.
$limitesuivante = $limite + $NombrePages;
$limiteprecedente = $limite - $NombrePages;

$req = mysqli_query($link, $selectc); 
$row = mysqli_fetch_row($req);
$total= $row[0];
$NbPage =floor($total/$NombrePages);

/*
echo "<center><HR size='2' width='500'>";
echo $v."<br>".$SelectEA."<br>".$selectc;
echo "<HR size='2' width='500'>";
echo " $total | $SelSem_w | $semaine | $Num_CatPlu | $Num_Client | $SelAnne_y | $SelDeb | $SelFin<br></center>" ;
*/

// titre page
echo "<div class='titre-text'><center>Saisie Entre Intrants</div></center><br>" ;
echo "<center>";

// FILTRES
// menu haut new-supp-modif
echo "<form id='form1' name='form1' method='get' action='entre_intrants.php'>";
echo "<input type='hidden' name='limite' value='$limite'/>";
echo "<table cellspacing=1 cellpadding=1 align=center border=0 width='800'>";
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='2014';
  $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");
  
// categorie 
  
  $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 Cat&eacute;gorie : <select style=' width: 60px;' name='num_cate_intrants' id='num_cate_intrants'>";
echo "<option value='0'>Toutes</option>";
$myart =new categorie_intrant;
$select = 'select * FROM categorie_intrant';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $num_cate_plu_intran=$myart->num_cate_plu_intran ;
  $categorie_plu_intran=$myart->categorie_plu_intran ;
  echo "<option value='$num_cate_plu_intran'>$num_cate_plu_intran | $categorie_plu_intran</option>";
}

echo "</select>";
echo "</td>";
echo "<td><input type='submit' value='ok'>";
echo "</td>";
echo "</tr>";
echo "</span>";
echo "</table>";
echo "</form></center>";

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

// Menu
echo "<center><HR size='1' width='500'>";
echo "<table width='800' cellspacing=0 cellpadding=0 align=center border=0 >";
echo "<td><center>";
echo "<a href='saisie_entre_intrants.php?ordre=1'><b>Nouveau</b></a> <b> | </b>";
echo "<a href=''><b>Consulter</b></a> <b> | </b>";
echo "<a href=''><b>Supprimer</b></a> <b> | </b>";
echo "<a href=''><b>Modifier</b></a><b></b>";
echo "</center></td></tr>";
echo "</tbody>";
echo "</table><HR size='1' width='500'>";

//detail Tableau
//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='800' height='22'><tr>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Date</div></td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Num Lot</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>PLU</td></span>";	
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Fournisseur</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>categorie</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Poids</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>DLC</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>?</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>V</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>M</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>D</td></span>";
echo "</tr>";

$myart =new intrants ;
$result = mysqli_query($link, $SelectEA);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result) ;
  $Num_entre_intrant=$myart->Num_entre_intrant ;
  $Num_Fournisseur=$myart->Num_Fournisseur ;
  $Date_Entre=$myart->Date_Entre ;
  $Date_DLC=$myart->Date_DLC;	
  $Num_Bl=$myart->Num_Bl ;
  $Poids=$myart->Poids ;
  $num_unite=$myart->num_unite ;
  $Num_Cate_Intrants=$myart->Num_Cate_Intrants ;	
  $Observations=$myart->Observations ;
  $Num_Plu=$myart->Num_Plu ;
  $Num_Lot=$myart->Num_Lot ;
  $etat=$myart->etat ;
 
$sql = "SELECT Nom_Fournisseur FROM fournisseurs WHERE Num_fournisseur='$Num_Fournisseur'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $nom_prestataire = $row["Nom_Fournisseur"];
    } 
// echo "<br>".$sql;

$sql = "SELECT nom_unite_ini FROM unitee WHERE num_unite='$num_unite'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $nom_untite = $row["nom_unite_ini"];
    } 

// echo "<br>".$sql;

$sql = "SELECT categorie_plu_intran FROM categorie_intrant WHERE num_cate_plu_intran='$Num_Cate_Intrants'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $categorie_plu_intran = $row["categorie_plu_intran"];
    } 

//echo "<br>".$sql;

$sql = "SELECT nom_plu_intrant FROM plu_intrant WHERE num_plu_intrant='$Num_Plu'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $nom_plu_intrant = $row["nom_plu_intrant"];
    } 
	
//
//echo "<br>".$sql."<br>" ;
//echo "<br>prestataire : ".$nom_prestataire." Categorie dechets :".$categorie_dechets."<br>" ;
//
$date_e=date("d/m/Y", strtotime($Date_Entre));
$date_dlc=date("d/m/Y", strtotime($Date_DLC));

/*
//echo  date("d/m/Y", strtotime($date_enleve_dechets))." ".$date_e;
num_entre=26&ordre=2
*/
  
    echo "<tr><td width='50' height='20' valign='center'>
	<div align='center'><span class='t-text'>$date_e</div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'><a class='' href='consult_intrants.php?num_entre=$Num_entre_intrant&ordre=2'>$Num_Lot</a></div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$nom_plu_intrant</div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$nom_prestataire</div></a></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$categorie_plu_intran</div></td></span>";
	echo "<td width='50' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Poids $nom_untite</div></td></span>";
	echo "<td width='50' height='20' valign='center'>" ;
	echo "<div align='center'><span class='t-text'>$date_dlc</a></div></td></span>";
	echo "<td width='20' height='20' valign='center'>
	<div align='center'><a class='' href='detail_intrants.php?ordre=4&num_lot=$Num_Lot'>
	<img src='images/list.png' width='20' height='20'></a></div></td>";
  
	switch ($etat) {
    case 0:
        echo "<td width='20' height='20' valign='center'><div align='center'><img src='images/coche.gif' width='20' height='20'></a></div></td>";
        break ;
    case 1:
        echo "<td width='20' height='20' valign='center'><div align='center'><img src='images/croix.gif' width='20' height='20'></a></div></td>";
        break ;
 } 
	echo "<td width='20' height='20' valign='center'>
	<div align='center'><a class='' href='modif_intrants.php?num_entre=$Num_entre_intrant&ordre=4'>
	<img src='images/modifie.png' width='20' height='20'></a></div></td>";
	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>";

echo "<br>";

echo "<table border='0' 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='.$limitePage
    .'&SelSem_w='.$SelSem_w_sel
    .'&SelAnne_y='.$SelAnne_y
    .'&num_cate_intrants='.$num_cate_intrants
    .'">Page precedente  </a>';
   // echo '<a href="'.$page.'?limite='.$limiteprecedente.'">Page precedente  </a>';
}


// Nombre dintervale entre deux
$range = 5;
 
// affiche les intervales entre pages
$initial_num = $PageC - $range;
$condition_limit_num = ($PageC + $range)  + 1;

for ($a = $initial_num ; $a <= $condition_limit_num; $a++) {
// 0 devant 123456789   
   $pagenum = $PageC < 10 ? $pagenum = "0".$PageC : $pagenum = $PageC; 
   $pagenuma = $a < 10 ? $pagenuma = "0".$a : $pagenuma = $a;
 
  if ($a > 0 && $a <= $NbPage ) {
  	
  $limitePage = $NombrePages*$a ;
  
    if (($limite/10)==$a) {
    echo '<a href="'.$page.'?limite='.$limitePage
   .'&SelSem_w='.$SelSem_w_sel
    .'&SelAnne_y='.$SelAnne_y
    .'&num_cate_intrants='.$num_cate_intrants
    .'"><FONT color="red">'.$pagenum.'</FONT></a> | ';
    } else {
     echo '<a href="'.$page.'?limite='.$limitePage
   .'&SelSem_w='.$SelSem_w_sel
    .'&SelAnne_y='.$SelAnne_y
    .'&num_cate_intrants='.$num_cate_intrants
    .'">'.$pagenuma.'</a> | ';
  	}	
	}	
}
if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
.'&SelSem_w='.$SelSem_w_sel
.'&SelAnne_y='.$SelAnne_y
.'&num_cate_intrants='.$num_cate_intrants
.'">Page Suivante</a>';
}
echo "</div></td></span>";
echo "";
echo "</tr></table>";


include 'menu/footer.php';
?>
