<?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, $rendement, 
$page, $Nom_Client, $NumAbat, $mrs, $total_entre, $total_sortie, $total_entre_cass, $total_sortie_cass, 
$total_entre_cass, $total_sortie_cass, $total_entre_decoupe, $total_sortie_decoupe, $total_entre_stockPf, $total_sortie_stockPf,
$total_entre_transf, $total_sortie_transf, $total_entre_congel, $total_sortie_congel ;
// entete de la page

include 'menu/header_edition.php';

$date = date("d-m-Y");
/*
http://192.168.1.200/horius/edition_recap_etiq.php?ordre=1
SelAnne_y=2019
SelSem_w=48
Num_lot_sel=B47AGNX19
Num_Client=10+100
date_debut=2019-11-25
date_fin=2019-11-30
*/

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

/*
echo "<center><HR size='2' width='500'>";
echo "<HR size='2' width='500'>";
echo " *-* Fiche detail Decoupe *-*<br>" ;
echo "$SelAnne_y| $SelSem_w | $Num_lot_sel | $Num_Client | $date_debut | $date_fin | $date <br>" ;
*/
?>
<BODY onLoad='$("#bcTarget").barcode("<?php echo "20010".$IdNumClient ; ?>", "code128",{barWidth:1 , barHeight:30}); 
	' >
<!--<div id="bcTarget"></div> window.print(); -->
<?php
$v ="SELECT DISTINCT `num_lot`, `num_lot_trace` FROM temp_import_csv";
// Detremination de la requete d'edition
if (($Num_lot_sel || $SelAnne_y || $SelSem_w || $Num_Client )>0) {
$v.=' WHERE' ;
}

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

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

if (empty($Num_lot_sel)) {
$v.='';
} else { 
if (($Num_Client || $SelSem_w)>0) {
    $v.=" AND num_lot='".$Num_lot_sel."'";
    } else {
    $v.=" num_lot='".$Num_lot_sel."'";
    }
}

if ($date_debut ==0) {

$date_debut = date("Y-m-d", mktime(0,0,0,01,01,$SelAnne_y));
$date_fin = date("Y-m-d", mktime(0,0,0,12,31,$SelAnne_y));
}



if (($Num_lot_sel || $SelSem_w || $Num_Client )>0) {
$v.=" AND date_etiq BETWEEN '".$date_debut."' AND '".$date_fin."'";
} else { 
$v.=" date_etiq BETWEEN '".$date_debut."' AND '".$date_fin."'";
}

$v.=" AND IdNumClient='".$IdNumClient."'";
$v.=" ORDER BY `temp_import_csv`.`num_lot` DESC";

/*
echo "<br><center>$v<br>";
*/
echo "<br>";
	
echo "<center>";
echo "<table width='800' border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td width='100'><center><img src='images/$LogoSite' width='80'></center></td>";
echo "<td class='texthaut' width='250'>$SocieteSite<br>$RueSite<br>$CpostalSite $VilleSite
			<br>
			T&eacute;l : $TelephoneSite <br> $MailSite </td>";
echo "<td width='50'><center><img src='images/agrem_$IdNumClient.jpg' width='50'></center></td>";
echo "<td width='20'><center>&nbsp;</center></td>";
echo "<td class='t-text' width='370'><center>Edition R&#233;capitulatif Importation Etiquettes</center><br>";
if ($SelSem_w>0) {echo "Semaine : $SelSem_w <br>";}
if ($Num_lot_sel>0) {echo "Num lot : $Num_lot_sel <br>";}
if ($Num_Client>0) {echo "Num_Client : $Num_Client <br>";}
if (empty($date_debut)) {echo "Entre le $date_debut et $date_fin <br>";} else {echo "Ann&eacute;e : 2019";}
echo "</td>";

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

echo "<center><table class='t-text' width='750' border='1' cellspacing='2' cellpadding='2'>";	  



	$req5=mysqli_query($link, $v);
//	echo "<br>$sql5<br>";
	while ($row = mysqli_fetch_assoc($req5)) {
		 $num_lot_dist=  $row["num_lot"];
		 $num_trace_dist=  $row["num_lot_trace"];
	echo "<center><table class='t-text' width='750' border='1' cellspacing='2' cellpadding='2'>";	 
	echo "<tr><th>Num Lot : $num_lot_dist - $num_trace_dist</th><th><img src='images/Apply.png'></th><th></th><th></th><th></th></tr>";
	 
	$sql61="SELECT `num_client`, `date_etiq`,`dlc_etiq`, `num_lot`, `num_lot_trace`, num_plu, SUM(poids), COUNT(`num_plu`)";
	$sql61.=" FROM temp_import_csv";
	$sql61.=" WHERE IdNumClient='".$IdNumClient."'";
	$sql61.=" AND `num_lot`='".$num_lot_dist."'";
	$sql61.=" AND `num_lot_trace`='".$num_trace_dist."'";
	$sql61.=" GROUP BY  `num_client`,`date_etiq`, `dlc_etiq`, `num_lot`, `num_lot_trace`, `num_plu`" ;
	$sql61.=" ORDER BY `temp_import_csv`.`num_lot` DESC, `temp_import_csv`.`num_plu` ASC";
	$req61=mysqli_query($link, $sql61); 
	
	$poids_total_temp = 0 ;
  $nb_pi_total_temp = 0 ;
	 
	//echo "<br>$sql61<br>";
	while ($row = mysqli_fetch_assoc($req61)) {
	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["SUM(poids)"];
	        $nb_pi_temp = $row["COUNT(`num_plu`)"];
	        $num_lot_entre=  $row["num_lot"];
					$num_trace_entre=  $row["num_lot_trace"];
					$date_lot_entre  =  $row["date_etiq"];
					$date_dlc_temp	=  $row["dlc_etiq"];
					$NumClient	=  $row["num_client"];
					
							$sql661="select * FROM plu" ;
							$sql661.=" WHERE Num_PLU='".$num_plu_temp."'";
							$sql661.=" AND IdNumClient='".$IdNumClient."'";  
							$req661=mysqli_query($link, $sql661);  
							while ($row = mysqli_fetch_assoc($req661)) { 
								$nom_plu_temp = $row["PLU"];
								} 
								
							$sql662 = "SELECT Nom_Client FROM clients_base WHERE Num_Client='$NumClient'";  
							$req662 = mysqli_query($link, $sql662);
							while ($row = mysqli_fetch_assoc($req662)) {$Nom_Client = $row["Nom_Client"];}
					
					
$a++;							
$poids_temp = number_format($poids_temp, 3, '.', ' ')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', ' ')	;
$date_lot_entre=date("d/m/Y", strtotime($date_lot_entre));
$date_dlc_temp=date("d/m/Y", strtotime($date_dlc_temp));

$poids_total_temp = $poids_total_temp + $poids_temp ;
$nb_pi_total_temp = $nb_pi_total_temp + $nb_pi_temp ;
	
echo "<tr><th>$a</th><th>$num_plu_temp |  $nom_plu_temp</th><th>$NumClient | $Nom_Client </th><th>$poids_temp Kg | $nb_pi_temp pi</th><th><img src='images/Apply.png'></th</tr>";
}
$poids_total_temp = number_format($poids_total_temp, 3, '.', ' ')	;
$nb_pi_total_temp = number_format($nb_pi_total_temp, 0, '.', ' ')	;


echo "<tr><th></th><th></th><th>TOTAL </th><th>$poids_total_temp Kg | $nb_pi_total_temp pi</th><th></th</tr>";
echo "</table></center>";	
echo "<br>";
}

echo "<br>";
echo "<table width='800' border='0' cellpadding='1' cellspacing='1'>";
echo "<tr>";
echo '<td width="400"><center><div id="bcTarget"></div></center></td>';
echo "<td width='400'><center>Edition du $date</td>";
echo "</tr>";
echo "</table>";

?>