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

$selectc = 'SELECT count(num_decoupe) FROM decoupe';

$v='select * FROM decoupe ';

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_decoupe='.$SelSem_w;
    $selectc.=' semaine_decoupe='.$SelSem_w; 
    }

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

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_decoupe BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND date_decoupe BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" date_decoupe BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" date_decoupe BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}
$selectc.=" AND IdNumClient='".$IdNumClient."'";
$v.=" AND IdNumClient='".$IdNumClient."'";
$v.=' ORDER BY date_decoupe DESC limit '.$limite.','.$nombre;

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

  // on va afficher 5 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 | $NbPage | $Num_CatPlu | $Num_Client | $SelAnne_y | $SelDeb | $SelFin<br></center>" ;
*/

echo "<center>";

// titre page
echo "<div class='titre-text'><center>D&#233;coupe</div></center><br>" ;

// Filtres

echo "<form id='form1' name='form1' method='get' action='decoupe.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 :";

  $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 Cat&eacute;gorie : <select style=' width: 60px;' name='Num_CatPlu' id='Num_CatPlu'>";
echo "<option value='0'>Toutes</option>";
$myart =new categorieplu;
$select = 'select * FROM categorieplu';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $Num_CatPlu=$myart->Num_CatPlu ;
  $CategoriePlu=$myart->CategoriePlu ;
  echo "<option value='$Num_CatPlu'>$Num_CatPlu | $CategoriePlu</option>";
}

echo "</select>";
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';
$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><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 *-*
// Menu
echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
echo "<td>";
echo "<a href='ajout_decoupe.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>";

echo "</td></tr>";
echo "</tbody>";
echo "</table><HR size='1' width='500'>";
// fin menu

// detail page

//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='700' height='22'><tr>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Date</div></td></span>";
echo "<td width='20' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>L</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='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Num Tra&#231;abilit&#233;</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'>Type Plu</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>F</td></span>";	
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>E</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 decoupe;
$result = mysqli_query($link, $SelectEA);
$myart->getnum($result);
while ($myart->z < $myart->num) {
	$myart->readrow($result);
	$num_decoupe=$myart->num_decoupe ;
	$date_decoupe=$myart->date_decoupe ;
	$semaine_decoupe=$myart->semaine_decoupe ;
	$num_client_decoupe=$myart->num_client ;
	$num_lot_decoupe=$myart->num_lot_decoupe ;
	$tracabilite_decoupe=$myart->tracabilite_decoupe ;
	$categorie_decoupe=$myart->categorie_decoupe ;
	$categorie_plu=$myart->categorie_plu ;
	$poid_decoupe=$myart->poid_decoupe ;
	$cloture=$myart->cloture ;
	$observations=$myart->observations ;
  
$sql = "SELECT Let_Client FROM clients WHERE Num_Client='$num_client_decoupe'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Let_Client = $row["Let_Client"];
    }      


$sql = "SELECT CategoriePlu FROM categorieplu WHERE Num_CatPlu='$categorie_plu'";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $categorie_plu_decoupe = $row["CategoriePlu"];
    }     

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

echo "<tr><td width='30' height='20' valign='center'>
		<div align='center'><span class='t-text'>$date_e</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 class='' href='consult_decoupe.php?num_entre=$num_decoupe&ordre=2'><span class='t-text'>$num_lot_decoupe</div></a></td></span>";
echo "<td width='50' height='20' valign='center'><div align='center'>
		<span class='t-text'>$tracabilite_decoupe</div></a></td></span>";
echo "<td width='50' height='20' valign='center'>
		<div align='center'><span class='t-text'>$poid_decoupe</div></td></span>";
echo "<td width='50' height='20' valign='center'>
		<div align='center'><span class='t-text'>$categorie_plu_decoupe</div></td></span>";
echo "<td width='32' height='20' valign='center'>";
			
		if ($cloture==1) {
		echo "<div align='center'><span class='t-text'>
		      <img src='images/coche.gif' width='20' height='20'>
		      </div></td></span>";
		} else {
		echo "<div align='center'><span class='t-text'>
		      <img src='images/croix.gif' width='20' height='20'>
			  </div></td></span>";
		}

echo "<td width='32' height='20' valign='center'>
		<div align='center'><a class='' href='edition_detail_decoupe_imp.php?num_entre=$num_lot_decoupe' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></div></td>";
echo "<td width='32' height='20' valign='center'>
		<div align='center'><a class='' href='#'><img src='images/modifie.png' width='20' height='20'></a></div></td>";
//echo "<td width='32' height='20' valign='center'>
//		<div align='center'><a class='' href='modif_decoupe.php?num_entre=$num_decoupe&ordre=4'><img src='images/modifie.png' width='20' height='20'></a></div></td>";
echo "<td width='32' height='20' valign='center'>
		<div align='center'><a class='' href='delette_decoupe.php?num_entre=$num_decoupe&lot=$num_lot_decoupe&trace=$tracabilite_decoupe'><img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";

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

echo "<table border='1' cellpadding='0' cellspacing='0' 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_sel
    .'&SelAnne_y='.$SelAnne_y
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&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
    .'&SelAnne_y='.$SelAnne_y
    .'&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
    .'&SelAnne_y='.$SelAnne_y
    .'&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
    .'&SelAnne_y='.$SelAnne_y
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
		.'">Page Suivante</a>';
}
echo "</div></td></span>";
echo "";
echo "</tr></table><br>";


include 'menu/footer.php';
?>
