<?php
   
include("include/data.php");
Init_boutique();

// entete de la page

//include 'menu/header.php';

/* delete_bl_temp.php?
ordre=1
CltSelBL%5B%5D=10200%2F
ok=Valider

*/ 
unset($arr);
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['ok'])) { $ok=$_POST['ok']; } else {$ok=''; }
		if (isset($_POST['Sel_w'])) { $Sel_w=$_POST['Sel_w']; } else {$Sel_w=''; }
		foreach($_POST['CltSelBL'] as $valeur)
		{ 
		$i=$i+1 ;
		//echo $valeur.' a &eacute;t&eacute; s&eacute;lectionn&eacute;<br>';	
		$arr[$i] = $valeur ;
			
		}	
		$valForm="POST" ;
		
		
		
		
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['ok'])) { $ok=$_GET['ok']; } else {$ok=''; }
		if (isset($_GET['Sel_w'])) { $Sel_w=$_GET['Sel_w']; } else {$Sel_w=''; }
		$arr = array();
		$i=0;
		foreach($_GET['CltSelBL'] as $valeur)
		{ 
		$i=$i+1 ;
		//echo $valeur.' a &eacute;t&eacute; s&eacute;lectionn&eacute;<br>';	
		$arr[$i] = $valeur ;
			
		}	
		$valForm="GET" ;
}

$b=0;

// TITRE Page //
//echo "<HR size='2' width='500'><div class='titre-text'><center>Cr&#233;ation Bon de livraison apr&#232;s imports</div></center><HR size='2' width='500'>" ;



if ($ok=="Valider") {     // Importation Partielle

$Nb_ValTbl = sizeof ($arr) ;
echo "Nb_ValTbl = $Nb_ValTbl";
if ($Nb_ValTbl>2) {
	$SelCltCltTotal.=" AND ( ";
} else {
	$SelCltCltTotal.="";
}


for ($f=1; $f <= $Nb_ValTbl ; $f++) {
 $SelClt_a=$arr[$f] ;
 $NumClient_final_Deb=substr($SelClt_a, 0, 2);
 $NumClient_final_Fin=substr($SelClt_a, -4,-1);
 $SelClt_a=$NumClient_final_Deb." ".$NumClient_final_Fin;
	
		if ($f==1 and $Nb_ValTbl==1) {$SelCltCltTotal.=" AND `num_client`='".$SelClt_a."'";}
	
	if ($f>=1 and $Nb_ValTbl>$f) {
	$SelCltCltTotal.=" `num_client`='".$SelClt_a."' OR";	
	} elseif ($Nb_ValTbl==$f) {
	$SelCltCltTotal.=" `num_client`='".$SelClt_a."' )";
	}
}



$Nb_etiq_import_Total = 0 ;
	$sql1 = "SELECT count(num_etiq_ligne) FROM temp_import_csv";
	$sql1.=" WHERE `IdNumClient`='".$IdNumClient."'"; 
	if (!empty($Sel_w)){ $sql1.=" AND Num_semaine='".$Sel_w."'";}
	$sql1.= $SelCltCltTotal ;
	$sql1.=" AND `RsltImport`='1'";
	$sql1.=" AND `RsltLot`='1'";
	$sql1.=" AND `RsltStock`='1'";  
	$req1 = mysqli_query($link, $sql1); 
	$row = mysqli_fetch_row($req1);
	$Nb_etiq_import_Total = $row[0];
	

$Nb_clients_import_Total = 0 ;
	$sql1 = "SELECT COUNT(DISTINCT (num_client))  FROM temp_import_csv";
	$sql1.=" WHERE `IdNumClient`='".$IdNumClient."'"; 
	if (!empty($Sel_w)){ $sql1.=" AND Num_semaine='".$Sel_w."'";}
	$sql1.= $SelCltCltTotal ;
	$sql1.=" AND `RsltImport`='1'";
	$sql1.=" AND `RsltLot`='1'";
	$sql1.=" AND `RsltStock`='1'"; 
	$req1 = mysqli_query($link, $sql1); 
	$row = mysqli_fetch_row($req1);
	$Nb_clients_import_Total = $row[0];

echo "<center>Suppression de  $Nb_etiq_import_Total &#233;tiquette(s) pour $Nb_clients_import_Total client(s) s&eacute;l&eacute;ctionn&eacute;(s)</div><HR size='2' width='500'><br>" ;	

//
	$sql2 ="DELETE FROM `temp_import_csv`";
	$sql2.=" WHERE `IdNumClient`='".$IdNumClient."'";
	if (!empty($Sel_w)){ $sql2.=" AND Num_semaine='".$Sel_w."'";} 
	$sql2.= $SelCltCltTotal ;
	$sql2.=" AND `RsltImport`='1'";
	$sql2.=" AND `RsltLot`='1'";
	$sql2.=" AND `RsltStock`='1'";  
	$req2 = mysqli_query($link, $sql2); 
	
echo '<br>'.$SelCltCltTotal.'<br>'.$sql2.'| '.$req2.'<br>';
} else {
//
	$sql2 ="DELETE FROM `temp_import_csv`";
	$sql2.=" WHERE `IdNumClient`='".$IdNumClient."'";
	if (!empty($Sel_w)){ $sql2.=" AND Num_semaine='".$Sel_w."'";} 
	$sql2.=" AND `RsltImport`='1'";
	$sql2.=" AND `RsltLot`='1'";
	$sql2.=" AND `RsltStock`='1'";  
	$req2 = mysqli_query($link, $sql2); 
	
echo '<br>'.$SelCltCltTotal.'<br>'.$sql2.'| '.$req2.'<br>';	
}	

$refere = $_SERVER['HTTP_REFERER'] ;

Header("Location:  $refere") ;
?>
