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

include 'menu/header.php';

/*
http://192.168.1.200/orius/bon_de_livraison.php?
limite=0
Sel_y=2017
Sel_m=2
Sel_w=0
Num_Client=0
pages=25
*/
if (!isset($_GET['limite'])) { 
		// Transmis en POST
		$limite=$_POST['limite'];
		if (isset($_POST['Sel_y'])) { $Sel_y=$_POST['Sel_y']; }  else {$Sel_y=date("Y"); }
		if (isset($_POST['Sel_m'])) { $Sel_m=$_POST['Sel_m']; }  else {$Sel_m=''; }
		if (isset($_POST['Sel_w'])) { $Sel_w=$_POST['Sel_w']; } else {$Sel_w=''; }
		if (isset($_POST['Num_Client_sel'])) { $Num_Client_sel=$_POST['Num_Client_sel']; }  else {$Num_Client_sel=''; }
		if (isset($_POST['pages'])) { $NombrePages=$_POST['pages']; } else {$NombrePages=10; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$limite=$_GET['limite'];
		if (isset($_GET['Sel_y'])) { $Sel_y=$_GET['Sel_y']; }  else {$Sel_y=date("Y"); }
		if (isset($_GET['Sel_m'])) { $Sel_m=$_GET['Sel_m']; }  else {$Sel_m=''; }		
		if (isset($_GET['Sel_w'])) { $Sel_w=$_GET['Sel_w']; } else {$Sel_w=''; }
		if (isset($_GET['Num_Client_sel'])) { $Num_Client_sel=$_GET['Num_Client_sel']; }  else {$Num_Client_sel=''; }
		if (isset($_GET['pages'])) { $NombrePages=$_GET['pages']; } else {$NombrePages=10; }
		$valForm="GET" ;
}

$v='select * FROM gestion_br_haut ';
$selectc = 'SELECT count(num_li_br) FROM gestion_br_haut';


if (($Num_Client_sel || $Sel_y || $Sel_w || $Sel_m )>0) {
$v.=' WHERE' ;
$selectc.=' WHERE' ;
} else { 
    $v.=''; 
    $selectc.='';
    }

if ($Sel_w==0) {
$v.='';
$selectc.='';
} else { 
    $v.=' semaine_br='.$Sel_w;
    $selectc.=' semaine_brl='.$Sel_w; 
    }

if ($Num_Client_sel==0) {
$v.='';
$selectc.='';
} else { 
    if (($Num_CatPlu || $Sel_w)>0) {
    $v.=" AND num_client_br='".$Num_Client_sel."'";
    $selectc.=" AND num_client_br='".$Num_Client_sel."'";
    } else {
    $v.=" num_client_br='".$Num_Client_sel."'";
    $selectc.=" num_client_br='".$Num_Client_sel."'";    
    }
}


// selectoion mois
if ($Sel_m >= 1) {
$jourBl = array(1=>31, 2=>28, 3=>31, 4=>30, 5=>31, 6=>30, 7=>31, 8=>31, 9=>30, 10=>31, 11=>30, 12=>31 );
$jourfin = $jourBl[$Sel_m];

$SelDeb = date("Y-m-d", mktime(0,0,0,$Sel_m,01,$Sel_y));
$SelFin = date("Y-m-d", mktime(0,0,0,$Sel_m,$jourBl[$Sel_m],$Sel_y));
} else {

$SelDeb = date("Y-m-d", mktime(0,0,0,01,01,$Sel_y));
$SelFin = date("Y-m-d", mktime(0,0,0,12,31,$Sel_y));
}

if (($Num_Client_sel || $Sel_w)>0) {
$v.=" AND date_br BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND date_br BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" date_br BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" date_br BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}

$v.=" AND IdNumClient='".$IdNumClient."'";
$selectc.=" AND IdNumClient='".$IdNumClient."'";


$v.=" ORDER BY `gestion_br_haut`.`num_li_br` DESC";
$selectc.=" ORDER BY `gestion_br_haut`.`num_li_br` DESC";



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

if ($Sel_w>=1){
	
}


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

if ($Sel_w>=1){
$NombrePages = $total;
}

$v.=' limit '.$limite.','.$NombrePages;
$SelectEA = $v ;
$date = date("d-m-Y");
//ecriture tests
/*
echo "<center><HR size='2' width='500'>";
echo $v."<br>".$SelectEA."<br>".$selectc;
echo "<HR size='2' width='500'>";
echo " $total | sY : $Sel_y | sW : $Sel_w | sM : $Sel_m | Clt : $Num_Client_sel | sem : $semaine | $SelDeb | $SelFin<br>" ;
echo " $limiteprecedente | $limite | $limitesuivante | $NombrePages | $total | $NbPage <br></center>" ;

*/


// TITRE Page //
echo "<center><div class='titre-text'><center>Bon de Retour</div></center><br>" ;

// FILTRES
// menu haut new-supp-modif
echo "<form id='form1' name='form1' method='get' action=''>";
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 :";
// Année 
  $class = '';
  $fyear='2015';
  $lyear='2050';
  $name = 'Sel';

  $yr=date("Y");
  
  $s.="<SELECT style=' width: 65px;' NAME=\"${name}_y\" 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"; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";

// Mois
echo "</td>";
echo "<td>&nbsp;Par mois :<select name='Sel_m' id='Sel_m'>";
echo "<option value='0'>Tous</option>";
$mois = array("","Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre"); 
for ($i=1; $i<=12 ; $i++){

echo "<option value='$i'>$mois[$i]</option>";	
}
echo "</select>";
echo "</td>";

// Semaine    

echo "</td>";
echo "<td>&nbsp;Par semaine :";

  $class = '';
  $fyear='1';
  $lyear='53';
  $name = 'Sel';

  $yr=date("W");
  
  $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>";

// Clients
echo "<td>&nbsp;Par Client : <select style=' width: 150px;' name='Num_Client_sel' id='Num_Client_sel'>";
echo "<option value='0'>Tous</option>";
$myart =new clients;
$select = 'select * FROM clients_base';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $Num_Client=$myart->Num_Client ;
  $Nom_Client=$myart->Nom_Client ;

echo "<option value='$Num_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>";

// Menu
echo "<center><HR size='1' width='500'>";
echo "<table width='800' cellspacing=0 cellpadding=0 align=center border=0 >";
echo "<td><center>";
echo "<a href='saisie_br_haut.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><b></b>";
echo "</center></td></tr>";
echo "</tbody>";
echo "</table><HR size='1' width='500'>";

/*
echo "<center>total : ".$total."| limite  ".$limite."| limite suivante ".$limitesuivante."| limite precedente".$limiteprecedente ."<br>";
echo "<center>num_pf : ".$num_pf."| num_cde  ".$num_cde."| num_bl ".$num_bl."| num_br".$num_br ."| num_fa  ".$num_fa."| num_av ".$num_av."| num_ec".$num_ec ."<br>";
*/
//detail Tableau

echo "<table border='1'  id='customers'>"; 
echo "<tr>";
echo "<th width='50' >Date</div></th>";
echo "<th width='100' >N&deg; BR</th>";
echo "<th width='100' >N&deg; Bl Refs</th>";
echo "<th width='150' >Client</th>";
echo "<th width='75'>Poids Retour</th>";
echo "<th width='75'>Montant Ht</th>";
echo "<th width='50'>Date Retour</th>";	
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";
echo "<th width='20' ></th>";

echo "</tr>";
$TotalPoidlivre = 0 ; 
$TotalMontantHt = 0 ;

$req = mysqli_query($link, $SelectEA);
while ($row = mysqli_fetch_assoc($req)) {
  $num_li_bl = $row["num_li_br"];
  $num_bl_base = $row["num_br"];
  $num_bl_retour = $row["num_bl_ref"];
  $date_bl = $row["date_br"];
  $semaine_bl = $row["semaine_br"];	
  $num_client_bl = $row["num_client_br"];
  $date_livraison = $row["date_retour"];
  $montant_ht = $row["montant_ht"];
  $poids_livre = $row["poids_retour"];
  $num_paie = $row["num_paie_br"];	
  $num_trsp = $row["num_trsp_br"];
  $message_bl = $row["message_br"];
  $Etat = $row["etat_br"];
  $Timestamp = $row["Timestamp"];

$sqlb = "SELECT * FROM clients_base WHERE Num_Client='$num_client_bl'"; 
$reqb = mysqli_query($link, $sqlb);
while ($row = mysqli_fetch_assoc($reqb)) {
        $Nom_Client = $row["Nom_Client"];
    }  
//$Nom_Client = htmlspecialchars($Nom_Client, ENT_QUOTES);
// formation du numéro de BL
$date_bl_e=date("d/m/Y", strtotime($date_bl));
$date_livraison=date("d/m/Y", strtotime($date_livraison));
$Anne_bl=date("y", strtotime($date_bl));
$num_bl_complet ="$Anne_bl-$num_bl-".$num_bl_base ;
$num_bl_ref_retour = $num_bl_retour ;
$num_bl_retour="$Anne_bl-$num_bl-".$num_bl_retour ;
  echo '<tr>';
  echo "<td width='50' style='text-align:center'>$date_bl_e</div></td>";
  echo "<td width='100' style='text-align:center'>$num_bl_complet</td>";
	echo "<td width='100' style='text-align:center'>$num_bl_retour</td>";
	echo "<td width='150' style='text-align:center'>$Nom_Client </td>";
	echo "<td width='75' style='text-align:right'>$poids_livre kg</td>";
	echo "<td width='75' style='text-align:right'>$montant_ht &euro;</td>";
	echo "<td width='50' style='text-align:center'>$date_livraison </td>";
	echo "<td width='20'>
  <a href='ajout_detail_br.php?ordre=1&num_bl_ligne=$num_li_bl&num_bl=$num_bl_base&num_client_entre=$num_client_bl&date_bl=$date_bl&num_bl_ref=$num_bl_ref_retour'>
	<img src='images/table.png' width='20' height='20'></a></td>";
  echo "<td width='20'><a href='maj_haut_br.php?ordre=3&num_bl=$num_bl_base&num_bl_ligne=$num_li_bl'><center><img src='images/redo.png'></center></a></td>";

    echo "<td width='20' height='20'><a class='' href='edition_bl_nc_imp.php?num_entre=$num_li_bl' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></td>";
  echo "<td width='20' height='20'><a class='' href='edition_bl_imp.php?num_entre=$num_li_bl' TARGET=_BLANK><img src='images/money_bag.png' width='20' height='20'></a></td>";	
	switch ($Etat) {
    case 0:
        echo "<td width='20' height='20'><img src='images/Erase.png' width='20' height='20'></a></td>";
        break ;
    case 1:
        echo "<td width='20' height='20'><img src='images/Apply.png' width='20' height='20'></a></td>";
        break ;
    case 2:
        echo "<td width='20' height='20'><img src='images/Erase.png' width='20' height='20'></a></td>";
        break ;
        
 	} 
	echo "<td width='20'><a href='modif_bl_haut.php?ordre=4&num_bl=$num_bl_base'><img src='images/modifie.png' width='20' height='20'></a></td>";
	echo "<td width='20'><a href='delete_bon_haut.php?ordre=3&num_bon=$num_li_bl&num_bl=$num_bl_complet'><img src='images/trash.png' width='20' height='20'></a></td></tr>";

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

if ($Sel_w>0) {
	$sql5 ="SELECT SUM(poids_livre), SUM(montant_ht) FROM gestion_bl_haut";
	$sql5.=" WHERE semaine_bl='".$Sel_w."'";
	$sql5.=" AND IdNumClient='".$IdNumClient."'";  
	$req5=mysqli_query($link, $sql5);  
	while ($row = mysqli_fetch_assoc($req5)) {

	        $poids_total_bl = $row["SUM(poids_livre)"];
	        $montant_total_bl = $row["SUM(montant_ht)"];
	}
	
$poids_total_bl = number_format($poids_total_bl, 2, '.', '')	;	
$montant_total_bl = number_format($montant_total_bl, 2, '.', '')	;
	
echo "<table border='1'  id='customers'>"; 
echo "<tr>";
echo "<th colspan='4' >TOTAL Semaine $SelSem_w :</div></th>";
echo "<th width='75' >$poids_total_bl Kg</th>";
echo "<th width='75' >$montant_total_bl &euro;</th>";
echo "</tr>";
echo "</table>";
}


echo "<br>";

echo "<table border='1' cellpadding='0' cellspacing='0' width='500' height='22'><tr>";
echo "<td width='450' height='20' valign='center'><div align='center'><span class='t-text'>";

if ($Num_Client_sel==0) {
$Num_Client_sel=0 ;
} else {
$Num_Client_seld = substr($Num_Client_sel, 0,2); 
$Num_Client_selg = substr($Num_Client_sel, 3,3); 
$Num_Client_sel=$Num_Client_seld."+".$Num_Client_selg ;
}

//
echo "total enrg = ".$total.
" | Nbpages = ".$NbPage.
" <br> limite suivante= ".$limitesuivante.
" | limite encours= ".$limite.
" | limite precedente= ".$limiteprecedente.
" <br> Anne =".$Sel_y.
" | Semaine =".$Sel_w.
" | Num_Client=".$Num_Client_sel.
" | Nombre=".$NombrePages."<br>" ;	 
//
if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente
    .'&Sel_w='.$Sel_w
    .'&Sel_y='.$Sel_y
	  .'&Num_Client_sel='.$Num_Client_sel
    .'">Page precedente  </a>';
   // echo '<a href="'.$page.'?limite='.$limiteprecedente.'">Page precedente  </a>';
}

$limitePage=0;

for ($a = 1 ; $a <= 10 ; $a++) {
    $limitePage = $NombrePages + $limitePage;
	if (($limite/10)==$a) {
    echo '<a href="'.$page.'?limite='.$limitePage
    .'&Sel_w='.$Sel_w
    .'&Sel_y='.$Sel_y
	  .'&Num_Client_sel='.$Num_Client_sel
    .'"><FONT color="red">'.$a.'</FONT></a> | ';
	} else {
	echo '<a href="'.$page.'?limite='.$limitePage
    .'&Sel_w='.$Sel_w
    .'&Sel_y='.$Sel_y
	  .'&Num_Client_sel='.$Num_Client_sel
    .'">'.$a.'</a> | ';
	}
}

if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
    .'&Sel_w='.$Sel_w
    .'&Sel_y='.$Sel_y
	  .'&Num_Client_sel='.$Num_Client_sel
.'">Page Suivante </a>';
}
echo "</div></td></span>";
echo "<td>";
echo "<form id='form1' name='form1' method='get' action=''>";
echo "<input type='hidden' name='limite' value='$limite'/>";
echo "<input type='hidden' name='Sel_w' value='$Sel_w'/>";
echo "<input type='hidden' name='Sel_y' value='$Sel_y'/>";
echo "<input type='hidden' name='Num_Client_sel' value='$Num_Client_sel'/>";
echo "<SELECT style=' width: 65px;' NAME='pages' onchange='submit()'>";
echo "<OPTION VALUE='0'>*-*</OPTION>\n";

echo "<OPTION VALUE='5'>5</OPTION>\n";
echo "<OPTION VALUE='10'>10</OPTION>\n";
echo "<OPTION VALUE='25'>25</OPTION>\n";
echo "<OPTION VALUE='50'>50</OPTION>\n";
echo "<OPTION VALUE='100'>100</OPTION>\n";
echo "</SELECT></form>";
echo "</td></tr></table>";


include 'menu/footer.php';
?>
