<?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,
$limiteprecedente, $limite, $total, $page, $Nom_Client, $semaine, $IdNumClient ;
// entete de la page

include 'menu/header.php';
/*
http://192.168.1.200/horius/saisie_abattoir.php?
ordre=1
limite=0
SelAnne_y=2018
SelSem_w=10
Num_CatPlu=0
Num_Client=0
*/
if (!isset($_GET['ordre'])) {
	// Transmis en POST
	$ordre=$_POST['ordre'];
	if (isset($_POST['SelAnne_y'])) { $SelAnne_y=$_POST['SelAnne_y']; }  else {$SelAnne_y = date("Y"); }
	if (isset($_POST['SelSem_w'])) { $SelSem_w=$_POST['SelSem_w']; }  else {$SelSem_w=0; }
	if (isset($_POST['Num_CatPlu'])) { $Num_CatPlu=$_POST['Num_CatPlu']; } else {$Num_CatPlu = 0; }
	if (isset($_POST['Num_Client'])) { $Num_Client=$_POST['Num_Client']; } else {$Num_Client = 0; }
	if (isset($_POST['pages'])) { $NombrePages=$_POST['pages']; } else {$NombrePages=10; }
	if (isset($_POST['limite'])) { $limite=$_POST['limite']; } else {$limite=0; }
	$valForm="POST" ;
} else {
	// Transmis en GET
	$ordre=$_GET['ordre'];
	if (isset($_GET['SelAnne_y'])) { $SelAnne_y=$_GET['SelAnne_y']; }  else {$SelAnne_y = date("Y"); }
	if (isset($_GET['SelSem_w'])) { $SelSem_w=$_GET['SelSem_w']; }  else {$SelSem_w=0; }
	if (isset($_GET['Num_CatPlu'])) { $Num_CatPlu=$_GET['Num_CatPlu']; } else {$Num_CatPlu = 0; }
	if (isset($_GET['Num_Client'])) { $Num_Client=$_GET['Num_Client']; } else {$Num_Client = 0; }
	if (isset($_GET['pages'])) { $NombrePages=$_GET['pages']; } else {$NombrePages = 10; }
	if (isset($_GET['limite'])) { $limite=$_GET['limite']; } else {$limite = 0; }
	$valForm="GET" ;
}

    $SelAnne_y_sel = $SelAnne_y ;
	$SelSem_w_sel = $SelSem_w;
	$Num_CatPlu_sel = $Num_CatPlu ;
	$Num_Client_sel = $Num_Client ;

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

$v='select * FROM entreabat ';
$selectc = 'SELECT count(Num_entre) FROM entreabat';
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.=' SemaineAbat='.$SelSem_w;
    $selectc.=' SemaineAbat='.$SelSem_w;
    }

if ($Num_CatPlu==0) {
$v.='';
$selectc.='';
 } else {
   if ($SelSem_w!=0) {
    $v.=' AND Num_categoriePlu='.$Num_CatPlu;
    $selectc.=' AND Num_categoriePlu='.$Num_CatPlu;
     } else {
    $v.=' Num_categoriePlu='.$Num_CatPlu;
    $selectc.=' Num_categoriePlu='.$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_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 Date_Entre DESC limit '.$limite.','.$nombre;

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

// on va afficher x 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);

// Filtres de travail
/*
echo "<center><HR size='2' width='500'>";
echo "v : ".$v."<br>Select C : ".$selectc;
echo "<HR size='2' width='500'>";
echo " Total enrg : $total | Nbpages : $NbPage | $SelSem_w | $semaine | $PageC <br>
				plu : $Num_CatPlu plu sel : $Num_CatPlu_sel | Semaine sel : $SelSem_w|$SelSem_w | Client : $Num_Client | $SelAnne_y | $SelDeb | $SelFin<br></center>" ;
*/

// titre page
echo "<div class='titre-text'><center>Entr&#233es Abattoirs</div></center><br>" ;

// Filtres

echo "<form id='form1' name='form1' method='get' action='saisie_abattoir.php'>";
echo "<input type='hidden' name='ordre' value=$ordre>";
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 : ";

	$s = '';
  $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 "total : ".$total."| limite  ".$limite."<br> limite suivante ".$limitesuivante."| limite precedente".$limiteprecedente ."<br>";

echo "<div class='CorpsPage'>";
echo "<center><HR size='1' width='500'>";
echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
echo "<td>";
echo "<a href='saisie_ajout_abattoir.php'><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 "</td></tr>";
echo "</tbody>";
echo "</table><HR size='1' width='500'>";


//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='50' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Num Ident</td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Nom Clt</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='100' height='20' valign='center' bgcolor='#CCCCCC'><div align='center'><span class='t-text'>Cat&eacute;gorie</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='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 entreabat;
$result = mysqli_query($link, $v);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $Num_categoriePlu=$myart->Num_categoriePlu;
  $Num_entre=$myart->Num_entre ;
  $Date_Entre=$myart->Date_Entre ;
  $Num_Client=$myart->Num_Client ;
  $Poid=$myart->Poid_entre ;
  $NumIdentAn=$myart->NumIdentAn ;
  $Num_Lot=$myart->Num_Lot ;

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

$sql = "select * FROM categorieplu where Num_CatPlu='$Num_categoriePlu'";
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $nom_cate_plu = $row["CategoriePlu"];
    }

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


    echo "<tr><td width='30' height='20' valign='center'><div align='center'><span class='t-text'>$date_e</div></td></span>";
	echo "<td width='50' height='20' valign='center'><div align='center'><span class='t-text'>$NumIdentAn</div></td></span>";
	echo "<td width='100' height='20' valign='center'><div align='center'><span class='t-text'>$Nom_Client</div></td></span>";
	echo "<td width='50' height='20' valign='center'><div align='center'><a class='' href='consult_abattoir.php?Num_entre=$Num_entre'><span class='t-text'>$Num_Lot</div></a></td></span>";
	echo "<td width='100' height='20' valign='center'><div align='center'><span class='t-text'>$nom_cate_plu</div></td></span>";
	echo "<td width='50' height='20' valign='center'><div align='center'><span class='t-text'>$Poid</div></td></span>";
	echo "<td width='20' height='20' valign='center'><div align='center'><a href='#' onclick='javascript:Popup(\"edition_abattoir_fiche.php?Num_entre=$Num_entre\",900,400,\"location=0,menubar=0,scrollbars=0,statusbar=0\")'><img src='images/list.png' width='20' height='20'></a></div></td>";
	echo "<td width='20' height='20' valign='center'><div align='center'><a href='#' onclick='javascript:Popup(\"edition_abattoir.php?Num_entre=$Num_entre\",800,500,\"location=0,menubar=0,scrollbars=0,statusbar=0\")'><img src='images/print.png' width='20' height='20'></a></div></td>";
	echo "<td width='20' height='20' valign='center'><div align='center'><a class='' href='Modif_abattoir.php?Num_entre=$Num_entre'><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='delette_abattoir.php?Num_entre=$Num_entre'><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='700' 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
	.'&ordre='.$ordre
    .'&SelAnne_y='.$SelAnne_y_sel
	.'&SelSem_w='.$SelSem_w_sel
	.'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
    .'">Page precedente  </a>';
   // echo '<a href="'.$page.'?limite='.$limiteprecedente.'">Page precedente  </a>';
}

// saisie_abattoir.php?limite=0&SelSem_w=0&Num_CatPlu=0&Num_Client=0

// 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
		.'&ordre='.$ordre
		.'&SelAnne_y='.$SelAnne_y_sel
		.'&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
		 .'&ordre='.$ordre
		 .'&SelAnne_y='.$SelAnne_y_sel
		 .'&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
	.'&ordre='.$ordre
	.'&SelAnne_y='.$SelAnne_y_sel
	.'&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><br>";

include 'menu/footer.php';
?>
