<?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,
$Poid_entre_total,  $Poid_saisie_total, $Poid_Chaud_total, $Poid_Froid_total, 
$page, $Nom_Client, $NumAbat, $mrs ;
// entete de la page

include 'menu/header_edition.php';
/*
http://192.168.1.100/horius/edition_abattoirs_imp.php?
ordre=5
SelAnne_y=2017
SelSem_w=46
Num_CatPlu=0
Num_Client=0
date_debut=2017-11-01
date_fin=2017-11-30
*/

$num_abatt = 0;
$num_CatePlu = 0 ;
$cheche = 0 ;
$Poid_Chaud_total_Cate = 0 ;
$Poid_Froid_total_Cate = 0 ;
$Poid_saisie_total_Cate = 0 ;
$Poid_entre_total_Cate = 0 ;

if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['SelAnne_y'])) { $SelAnne_y=$_POST['SelAnne_y']; }  else {$SelAnne_y='0'; }
		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['date_debut'])) { $date_debut=$_POST['date_debut']; } else {$date_debut='0'; }
		if (isset($_POST['date_fin'])) { $date_fin=$_POST['date_fin']; } else {$date_fin='0'; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['SelAnne_y'])) { $SelAnne_y=$_GET['SelAnne_y']; }  else {$SelAnne_y='0'; }
		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['date_debut'])) { $date_debut=$_GET['date_debut']; } else {$date_debut='0'; }
		if (isset($_GET['date_fin'])) { $date_fin=$_GET['date_fin']; } else {$date_fin='0'; }
		$valForm="GET" ;
}

$v='';
if (($Num_Client || $Num_CatPlu || $SelAnne_y || $SelSem_w)>0) {
$v.=' WHERE' ;
} else { 
    $v.=''; 
    }

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

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

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

if ($date_debut==0 || $date_fin==0 ) {
$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));
$SelDate = 1;
} else {
$SelDeb = $date_debut;
$Date_Debut=date("d/m/Y", strtotime($date_debut));
$SelFin = $date_fin;
$Date_Fin=date("d/m/Y", strtotime($date_fin));
$SelDate = 2;
}
if (!$SelSem_w==0) {$SelDate = 3;}

if (($Num_Client || $Num_CatPlu || $SelSem_w)>0) {
$v.=" AND Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" Date_Entre BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}
$SelTotaux=$v ;
$v.=" AND IdNumClient='".$IdNumClient."'";
$v.=' ORDER BY Num_categoriePlu ASC , Date_Entre DESC';

$selectc = 'SELECT count(Num_entre) FROM entreabat'.$v;
$v1='select * FROM entreabat '.$v;
$SelectEA = $v1 ;

/*
// test variables
echo "<center><HR size='2' width='500'>";
echo $SelTotaux."<br> v : ".$v."<br> EA : ".$SelectEA."<br> Selectc :".$selectc;
echo "<HR size='2' width='500'>";
echo " <br>" ;
echo " $SelSem_w | $semaine | $Num_CatPlu | $Num_Client |$SelDate <br>" ;
// fin test variables 
*/
echo "<center>";
echo "<table width='600' border='0' cellpadding='2' cellspacing='2'>";
echo "<tr>";
echo "<td height='80' width='100'><center><img src='images/$LogoSite' width='80'></center></td>";
echo "<td class='texthaut' width='500'>$SocieteSite<br>$RueSite<br>$CpostalSite $VilleSite
			<br>
			T&eacute;l : $TelephoneSite <br> $MailSite </td>";
echo "<td><center><img src='images/agrem_$IdNumClient.jpg' width='50'></center></td>";
echo "<td height='80' width='100'><center><div id='bcTarget'></center></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
// Debut Page
			echo "<table width='800' border='0' cellpadding='1' cellspacing='1'>";
			echo "<tr>";
			echo "<td colspan='2' height='30' class='t-text-titre'><center>FICHE ENTREE CARCASSES</center></td>";
			echo "</tr>";


switch ($SelDate) { 	
	case 1: 
			echo "<tr class='texthaut'>";
			echo "<th width='200' scope='col'>Ann&eacute;e</th>";
			echo "<th width='200' height='30'>$SelAnne_y</th>";
			echo "</th></tr>";
			echo "</table>";		  
	break; 	
	case 2: 
			echo "<tr class='texthaut'>";
			echo "<th width='200' scope='col'>Intraval de date : </th>";
			echo "<th width='200' height='30'>entre le : ".$Date_Debut." et le ".$Date_Fin."</th>";
			echo "</th></tr>";
			echo "</table>";		    
	break; 	
	case 3: 
			echo "<tr class='texthaut'>";
			echo "<th width='200' scope='col'>Semaine :</th>";
			echo "<th width='200' height='30'>$SelSem_w / $SelAnne_y</th>";
			echo "</th></tr>";
			echo "</table>";	  
	break; 	
	}

$myartentreabat =new entreabat;
$resultentreabat =mysqli_query($link,$SelectEA);
$myartentreabat->getnum($resultentreabat);
while ($myartentreabat->z < $myartentreabat->num) {
  $myartentreabat->readrow($resultentreabat);
  $Num_entre=$myartentreabat->Num_entre ;
  $Num_Abattoir=$myartentreabat->Num_Abattoir ;
  $Date_Entreb=$myartentreabat->Date_Entre ;
  $DataAbatb=$myartentreabat->DataAbat ;
  $DateDecoupb=$myartentreabat->DateDecoupe ;
  $SemaineAbat=$myartentreabat->SemaineAbat ;
  $NumIdentAn=$myartentreabat->NumIdentAn ;
  $Num_Abatage=$myartentreabat->Num_Abatage ;
  $Poid=$myartentreabat->Poid ;
  $Poid_Chaud=$myartentreabat->Poid_Chaud ;
  $Num_categoriePlu=$myartentreabat->Num_categoriePlu ;
  $Observations=$myartentreabat->Observations ;
  $Num_Client=$myartentreabat->Num_Client ;
  $Num_Plu=$myartentreabat->Num_Plu ;
  $Num_Lot=$myartentreabat->Num_Lot ;
  $NumTicket=$myartentreabat->NumTicket ;
  $Poid_saisie=$myartentreabat->Poid_saisie ;
  $Poid_entre=$myartentreabat->Poid_entre ;

$Date_Entre=date("d/m/Y", strtotime($Date_Entreb));
$DataAbat=date("d/m/Y", strtotime($DataAbatb));
$DateDecoup=date("d/m/Y", strtotime($DateDecoupb));
$Poid = number_format($Poid, 2, '.', ' ')	;
$Poid_Chaud = number_format($Poid_Chaud, 2, '.', ' ')	;
$Poid_saisie = number_format($Poid_saisie, 2, '.', ' ')	;
$Poid_entre = number_format($Poid_entre, 2, '.', ' ')	;

if ($num_CatePlu==$Num_categoriePlu) {
   
//echo "Categorie PLU : $Num_categoriePlu | Categorie PLU test : $num_CatePlu<br>";

if ($num_abatt==$Num_Abattoir){
//echo "Abattoirs vrai : $num_abatt | Abattoirs test : $Num_Abattoir<br>";
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='text'>";
$myartclients =new clients;
$selectclients = "select * FROM clients WHERE Num_Client='$Num_Client'";
$resultatclients = mysqli_query($link,$selectclients);
$myartclients->getnum($resultatclients);
while ($myartclients->z < $myartclients->num) {
  $myartclients->readrow($resultatclients);
  $Num_Clientf=$myartclients->Num_Client ;
  $Let_Client=$myartclients->Let_Client ;
  $Nom_Client=$myartclients->Nom_Client ;
echo "<td width='40' scope='col'>$Let_Client</td>";
}
echo "<td width='100' scope='col'>$Num_Lot</td>";
echo "<td width='70' scope='col'>$DataAbat</td>";
echo "<td width='70' scope='col'>$Date_Entre</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Chaud Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_saisie Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_entre Kg</td>";
echo "<td width='200' scope='col'>$Observations</td>";
echo "</tr>";
//echo "</table>";
} else {
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='text'>";
echo "<th width='200' height='30' scope='col'>Abattoir : $Num_Abattoir</th>";
// echo $num_abatt." | ".$Num_Abattoir ;
$myarticle =new abattoir;
$selectabat = "select * FROM abattoir WHERE id_abat='$Num_Abattoir'";
$resultabat =mysqli_query($link,$selectabat);
$myarticle->getnum($resultabat);
while ($myarticle->z < $myarticle->num) {
  $myarticle->readrow($resultabat);
  $id_abat=$myarticle->id_abat ;
  $Num_Abattoirf=$myarticle->Num_Abattoir ;
  $Nom_abattoir=$myarticle->Nom_abattoir ;
  $Ville_Abat=$myarticle->Ville_Abat;
 
echo "<th width='200' height='30' scope='col'>$Num_Abattoirf</th>";
echo "<th width='200' height='30' scope='col'>$Nom_abattoir</th>";
}

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

echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='haut'>";
echo "<th width='40' scope='col'>clt</th>";
echo "<th width='100' scope='col'>N&deg; Lot</th>";
echo "<th width='70' scope='col'>Date Abattage</th>";
echo "<th width='70' scope='col'>Date Entr&eacute;e</th>";
echo "<th width='80' scope='col'>Poids Chaud</th>";
echo "<th width='80' scope='col'>Poids Froid</th>";
echo "<th width='80' scope='col'>saisies</th>";
echo "<th width='80' scope='col'>Poids Entr&eacute;</th>";
echo "<th width='200' scope='col'>Observations</th>";
echo "</tr>";

$num_abatt=$Num_Abattoir;


echo "<tr class='text'>";
$myartclients =new clients;
$selectclients = "select * FROM clients WHERE Num_Client='$Num_Client'";
$resultatclients = mysqli_query($link,$selectclients);
$myartclients->getnum($resultatclients);
while ($myartclients->z < $myartclients->num) {
  $myartclients->readrow($resultatclients);
  $Num_Clientf=$myartclients->Num_Client ;
  $Let_Client=$myartclients->Let_Client ;
  $Nom_Client=$myartclients->Nom_Client ;
echo "<td>$Let_Client</td>";
}
echo "<td width='100' scope='col'>$Num_Lot</td>";
echo "<td width='70' scope='col'>$DataAbat</td>";
echo "<td width='70' scope='col'>$Date_Entre</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Chaud Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_saisie Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_entre Kg</td>";
echo "<td width='200' scope='col'>$Observations</td>";
echo "</tr>";

}

} else {
    
if ($cheche == 1 ) {
//echo "<br>";
//echo "<table width='800' border='3' cellpadding='1' cellspacing='1'>";
$SqlSelTotaux="select SUM(`Poid`) as PoidTotal, SUM(`Poid_Chaud`)as PoidChaudTotal, SUM(`Poid_saisie`)as PoidSaisieTotal, SUM(`Poid_entre`)as PoidEntreTotal FROM entreabat".$SelTotaux ;
$SqlSelTotaux.=" AND Num_categoriePlu=".$num_CatePlu; 
//echo "<br>".$SqlSelTotaux."<BR>";
//echo "<br>Categorie PLU : $Num_categoriePlu | Categorie PLU test : $num_CatePlu<br>";
$req = mysqli_query($link, $SqlSelTotaux);
while ($row = mysqli_fetch_assoc($req)) {
	$Poid_Chaud_total[$Num_categoriePlu] = $row["PoidChaudTotal"];
	$Poid_Froid_total[$Num_categoriePlu] = $row["PoidTotal"];
	$Poid_saisie_total[$Num_categoriePlu] = $row["PoidSaisieTotal"];
	$Poid_entre_total[$Num_categoriePlu] = $row["PoidEntreTotal"];
	}  
$Poid_Chaud_total[$Num_categoriePlu] = number_format($Poid_Chaud_total[$Num_categoriePlu], 2, '.', ' ')	;
$Poid_Froid_total[$Num_categoriePlu] = number_format($Poid_Froid_total[$Num_categoriePlu], 2, '.', ' ')	;
$Poid_saisie_total[$Num_categoriePlu] = number_format($Poid_saisie_total[$Num_categoriePlu], 2, '.', ' ')	;
$Poid_entre_total[$Num_categoriePlu] = number_format($Poid_entre_total[$Num_categoriePlu], 2, '.', ' ')	;

echo "<tr class='text'>";;
echo "<td colspan='4' >TOTAL :</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Chaud_total[$Num_categoriePlu] Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Froid_total[$Num_categoriePlu] Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_saisie_total[$Num_categoriePlu] Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_entre_total[$Num_categoriePlu] Kg</td>";
echo "<td width='200' scope='col'>Total : $num_CatePlu | $CategoriePlu</td>";
echo "</tr>";
echo "</table>";

 } else {
  $cheche = 1 ;   
 } 

$num_CatePlu=$Num_categoriePlu ;
echo "<br>";
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='texthaut'>";
echo "<th width='200' height='30' scope='col'>Categorie :</th>";
echo "<th width='200' scope='col'>";
$myartcateplu =new categorieplu;
$selectcateplu = "select * FROM categorieplu WHERE Num_CatPlu='".$Num_categoriePlu."'";
//echo $selectcateplu."<br>";
$resultcateplu = mysqli_query($link,$selectcateplu);
$myartcateplu->getnum($resultcateplu);
while ($myartcateplu->z < $myartcateplu->num) {
  $myartcateplu->readrow($resultcateplu);
  $Num_CatPluf=$myartcateplu->Num_CatPlu ;
  $CategoriePlu=$myartcateplu->CategoriePlu ;
  
//if  ($Num_CatPluf==$Num_categoriePlu) 
echo $Num_CatPluf." | ".$CategoriePlu;
}
echo "</th></tr>";
echo "</table>";

//echo "Categorie PLU : $Num_categoriePlu | faux ";
//echo "Categorie PLU test : $num_CatePlu<br>";


if ($num_abatt==$Num_Abattoir){
//    echo $num_abatt." | ".$Num_Abattoir ;
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='text'>";
$myartclients =new clients;
$selectclients = "select * FROM clients WHERE Num_Client='$Num_Client'";
$resultatclients = mysqli_query($link,$selectclients);
$myartclients->getnum($resultatclients);
while ($myartclients->z < $myartclients->num) {
  $myartclients->readrow($resultatclients);
  $Num_Clientf=$myartclients->Num_Client ;
  $Let_Client=$myartclients->Let_Client ;
  $Nom_Client=$myartclients->Nom_Client ;
echo "<td width='40' scope='col'>$Let_Client</td>";
}
echo "<td width='100' scope='col'>$Num_Lot</td>";
echo "<td width='70' scope='col'>$DataAbat</td>";
echo "<td width='70' scope='col'>$Date_Entre</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Chaud Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_saisie Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_entre Kg</td>";
echo "<td width='200' scope='col'>$Observations</td>";
echo "</tr>";
//echo "</table>";

} else {

//echo "<br>";
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='text'>";
echo "<th width='200' height='30' scope='col'>Abattoir : $Num_Abattoir</th>";
// echo $num_abatt." | ".$Num_Abattoir ;
$myarticle =new abattoir;
$selectabat = "select * FROM abattoir WHERE id_abat='$Num_Abattoir'";
$resultabat = mysqli_query($link,$selectabat);
$myarticle->getnum($resultabat);
while ($myarticle->z < $myarticle->num) {
  $myarticle->readrow($resultabat);
  $id_abat=$myarticle->id_abat ;
  $Num_Abattoirf=$myarticle->Num_Abattoir ;
  $Nom_abattoir=$myarticle->Nom_abattoir ;
  $Ville_Abat=$myarticle->Ville_Abat;
 
echo "<th width='200' height='30' scope='col'>$Num_Abattoirf</th>";
echo "<th width='200' height='30' scope='col'>$Nom_abattoir</th>";
}

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


echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='haut'>";
echo "<th width='40' scope='col'>clt</th>";
echo "<th width='100' scope='col'>N&deg; Lot</th>";
echo "<th width='70' scope='col'>Date Abattage</th>";
echo "<th width='70' scope='col'>Date Entr&eacute;e</th>";
echo "<th width='80' scope='col'>Poids Chaud</th>";
echo "<th width='80' scope='col'>Poids Froid</th>";
echo "<th width='80' scope='col'>saisies</th>";
echo "<th width='80' scope='col'>Poids Entr&eacute;</th>";
echo "<th width='200' scope='col'>Observations</th>";

echo "</tr>";

$num_abatt=$Num_Abattoir;


echo "<tr class='text'>";
$myartclients =new clients;
$selectclients = "select * FROM clients WHERE Num_Client='$Num_Client'";
$resultatclients = mysqli_query($link,$selectclients);
$myartclients->getnum($resultatclients);
while ($myartclients->z < $myartclients->num) {
  $myartclients->readrow($resultatclients);
  $Num_Clientf=$myartclients->Num_Client ;
  $Let_Client=$myartclients->Let_Client ;
  $Nom_Client=$myartclients->Nom_Client ;
echo "<td width='40' scope='col'>$Let_Client</td>";
}
echo "<td width='100' scope='col'>$Num_Lot</td>";
echo "<td width='70' scope='col'>$DataAbat</td>";
echo "<td width='70' scope='col'>$Date_Entre</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_Chaud Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_saisie Kg</td>";
echo "<td width='80' scope='col' class='textpoids'>$Poid_entre Kg</td>";
echo "<td width='200' scope='col'>$Observations</td>";
echo "</tr>";
//echo "</table>";

}
}
}
$sql = "select SUM(`Poid`) as PoidTotal, SUM(`Poid_Chaud`)as PoidChaudTotal, SUM(`Poid_saisie`)as PoidSaisieTotal, SUM(`Poid_entre`)as PoidEntreTotal FROM entreabat".$v;
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
	$Poid_Chaud_total = $row["PoidChaudTotal"];
	$Poid_Froid_total = $row["PoidTotal"];
	$Poid_saisie_total = $row["PoidSaisieTotal"];
	$Poid_entre_total = $row["PoidEntreTotal"];
	}  
$Poid_Chaud_total = number_format($Poid_Chaud_total, 2, '.', ' ')	;
$Poid_Froid_total = number_format($Poid_Froid_total, 2, '.', ' ')	;
$Poid_saisie_total = number_format($Poid_saisie_total, 2, '.', ' ')	;
$Poid_entre_total = number_format($Poid_entre_total, 2, '.', ' ')	;
echo "</table>";
echo "<br><br>";
echo "<table width='800' border='1' cellpadding='1' cellspacing='1'>";
echo "<tr class='haut'>";
echo "<th width='40' scope='col'>clt</th>";
echo "<th width='100' scope='col'>N&deg; Lot</th>";
echo "<th width='70' scope='col'>Date Abattage</th>";
echo "<th width='70' scope='col'>Date Entr&eacute;e</th>";
echo "<th width='80' scope='col'>Poids Chaud</th>";
echo "<th width='80' scope='col'>Poids Froid</th>";
echo "<th width='80' scope='col'>saisies</th>";
echo "<th width='80' scope='col'>Poids Entr&eacute;</th>";
echo "<th width='200' scope='col'>Observations</th>";
echo "</tr><tr class='text'>";
echo "<td colspan='4' >TOTAL GENERAL:</td>";
echo "<td width='80' class='textpoids' scope='col'>$Poid_Chaud_total Kg</td>";
echo "<td width='80' class='textpoids' scope='col'>$Poid_Froid_total Kg</td>";
echo "<td width='80' class='textpoids' scope='col'>$Poid_saisie_total Kg</td>";
echo "<td width='80' class='textpoids' scope='col'>$Poid_entre_total Kg</td>";
echo "<td width='200' scope='col'>*-*-*</td>";
echo "</tr>";
echo "</table>";
echo "<br><br>";
echo "<center><a href='#' onclick='window.close()'>Fermer</a></center>";

?>