<?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, $req,
$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_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($_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 fabrications_haut ';
$selectc = 'SELECT count(Num_Fabric) FROM fabrications_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_Fabric='.$SelSem_w;
    $selectc.=' Semaine_Fabric='.$SelSem_w; 
    }

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

if ($Num_Client==0) {
$v.='';
$selectc.='';
} else { 
    if (($Num_CatPlu || $SelSem_w)>0) {
    $v.=' AND Client_Fabric='.$Num_Client;
    $selectc.=' AND Client_Fabric='.$Num_Client;
    } else {
    $v.=' Client_Fabric='.$Num_Client;
    $selectc.=' Client_Fabric='.$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_Fabric BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND Date_Fabric BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" Date_Fabric BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" Date_Fabric BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}
$v.=" AND IdNumClient='".$IdNumClient."'";
$selectc.=" AND IdNumClient='".$IdNumClient."'";

$v.=' ORDER BY Date_Fabric DESC limit '.$limite.','.$NombrePages;

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

/*
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>" ;
*/

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

// TITRE Page //
echo "<center><div class='titre-text'><center>Saisie des Fabrications</div></center><br>" ;

// FILTRES
// menu haut new-supp-modif
echo "<form id='form1' name='form1' method='get' action='fabrications.php'>";
echo "<input type='hidden' name='limite' value='0'/>";
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 :";
// Année 
  $class = '';
  $fyear='2015';
  $lyear='2050';
  $name = 'SelAnne';

  $yr=date("Y");
  
  $s.="<SELECT NAME=\"${name}_y\" 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"; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";

// Semaine    

echo "</td>";
echo "<td>&nbsp;Par semaine :";

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

  $yr=date("W");
  
  $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";

// categorie   

echo "</td>";
echo "<td>&nbsp;Par Cat&eacute;gorie : <select style=' width: 120px;' name='Num_CatPlu' id='Num_CatPlu'>";
echo "<option value='0'>Toutes</option>";
$myart =new categorie_fabrications;
$select = 'select * FROM categorie_fabrications';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $num_cate_plu_intran=$myart->num_cate_fabric ;
  $categorie_plu_intran=$myart->categorie_fabrication ;
  
  echo "<option value='$num_cate_plu_intran'>$num_cate_plu_intran | $categorie_plu_intran</option>";
}

echo "</select>";
echo "<td>&nbsp;Par Client : <select style=' width: 150px;' name='Num_Client' id='Num_Client'>";
echo "<option value='0'>Tous</option>";
$myart =new clients;
$select = 'select * FROM clients';
$select.=" WHERE `IdNumClient`='".$IdNumClient."'";
$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>";
echo "<td>&nbsp;&nbsp;<input type='submit' value='ok'>";
echo "</td>";
echo "</tr>";
echo "</span>";
echo "</table>";
echo "</form></center>";

/*
echo "<center><br>";
echo "total enrg: ".$total." Total Pages : ".$NbPage." Page : ".$PageC."<br>limite  ".$limite."<br> limite suivante : ".$limitesuivante." | limite precedente : ".$limiteprecedente ."<br>";
echo "</center>";
*/

// 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_fabrications.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='70' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Date</div></td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Clt</td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Cat</td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Num Lot</td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Tracabilit&#233;e</td></span>";
echo "<td width='150' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>PLU</td></span>";	
echo "<td width='70' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Poids</td></span>";
echo "<td width='70' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>DLC</td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>D</td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>V</td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>M</td></span>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>S</td></span>";
echo "</tr>";

$myart =new fabrications_haut ;
$result = mysqli_query($link, $SelectEA);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result) ;
  $Num_Fabric =$myart->Num_Fabric ;
  $Num_plu_Fabric =$myart->Num_plu_Fabric ;
  $Cate_Fabric =$myart->Cate_Fabric ;
  $Client_Fabric =$myart->Client_Fabric ;
  $Client_Fabric =$myart->Client_Fabric ;	
  $Date_Fabric =$myart->Date_Fabric ;
  $DLC_Fabric =$myart->DLC_Fabric ;
  $Num_Lot_Fabric =$myart->Num_Lot_Fabric ;
  $Traca_Fabric =$myart->Traca_Fabric ;	
  $Poid_Fabric =$myart->Poid_Fabric ;
  $Unite_Fabric =$myart->Unite_Fabric ;
  $Observation =$myart->Observation ;
  $Etat = $myart->Etat ;
  $Timestamp =$myart->Timestamp ;

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

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


$sql = "SELECT Let_cate_fabric FROM categorie_fabrications WHERE num_cate_fabric='$Cate_Fabric'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Nom_Cate_Fabric = $row["Let_cate_fabric"];
} 

$sql = "SELECT PLU FROM plu WHERE Num_PLU='$Num_plu_Fabric'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Nom_Plu_Fabric = $row["PLU"];
    } 

//
//echo "<br>".$sql."<br>" ;
//echo "<br>prestataire : ".$nom_prestataire." Categorie dechets :".$categorie_dechets."<br>" ;
//
$date_e=date("d/m/Y", strtotime($Date_Fabric));
$date_dlc=date("d/m/Y", strtotime($DLC_Fabric));

/*
//echo  date("d/m/Y", strtotime($date_enleve_dechets))." ".$date_e;
num_entre=26&ordre=2
<a class='' href='consult_intrants.php?num_entre=$Num_entre_intrant&ordre=2'></a>
*/
  
    echo "<tr><td width='70' height='20' valign='center'>
	<div align='center'><span class='t-text'>$date_e</div></td></span>";
	echo "<td width='30' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Let_Client</div></a></td></span>";
	echo "<td width='30' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Nom_Cate_Fabric</div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'><a class='' href='detail_fabrications.php?num_entre=$Num_Fabric&ordre=2'>$Num_Lot_Fabric</a></div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Traca_Fabric</a></div></td></span>";
	echo "<td width='150' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Nom_Plu_Fabric</div></td></span>";
	echo "<td width='70' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Poid_Fabric $Nom_Untite_Fabric</div></td></span>";
	echo "<td width='70' height='20' valign='center'>" ;
	echo "<div align='center'><span class='t-text'>$date_dlc</a></div></td></span>";
	echo "<td width='30' height='20' valign='center'>
	<div align='center'><a class='' href='saisie_fabrications_detail.php?ordre=1&Num_Fabric=$Num_Fabric'>
	<img src='images/table.png' width='20' height='20'></a></div></td>";
	switch ($Etat) {
    case 1:
        echo "<td width='30' height='20' valign='center'><div align='center'><img src='images/coche.gif' width='20' height='20'></a></div></td>";
        break ;
    case 0:
        echo "<td width='30' height='20' valign='center'><div align='center'><img src='images/croix.gif' width='20' height='20'></a></div></td>";
        break ;
 } 
	echo "<td width='30' height='20' valign='center'>
	<div align='center'><a class='' href='modif_fabrications_haut.php?num_entre=$Num_Fabric&ordre=4'>
	<img src='images/modifie.png' width='20' height='20'></a></div></td>";
	echo "<td width='30' height='20' valign='center'>
	<div align='center'><a class='' href='delette_fabrications.php?num_entre=$Num_Fabric'>
	<img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";

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

echo "<br>";
// Navigation
echo "<table border='0' cellpadding='2' cellspacing='2' width='600' 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
    .'&SelSem_w='.$SelSem_w
	.'&Num_CatPlu='.$Num_CatPlu
    .'&Num_Client='.$Num_Client_sel
    .'">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
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
    .'"><FONT color="red">'.$pagenum.'</FONT></a> | ';
    } else {
     echo '<a href="'.$page.'?limite='.$limitePage
    .'&SelSem_w='.$SelSem_w_sel
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
    .'">'.$pagenuma.'</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';
?>
