<?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_fa_haut ';
$selectc = 'SELECT count(rowid) FROM gestion_fa_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_fa='.$Sel_w;
    $selectc.=' semaine_fa='.$Sel_w; 
    }

if ($Num_Client_sel==0) {
$v.='';
$selectc.='';
} else { 
    if (($Num_CatPlu || $Sel_w)>0) {
    $v.=" AND num_client_fa='".$Num_Client_sel."'";
    $selectc.=" AND num_client_fa='".$Num_Client_sel."'";
    } else {
    $v.=" num_client_fa='".$Num_Client_sel."'";
    $selectc.=" num_client_fa='".$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_fa BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND date_fa BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" date_fa BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" date_fa BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}

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


$v.=" ORDER BY `gestion_fa_haut`.`rowid` DESC";
$selectc.=" ORDER BY `gestion_fa_haut`.`rowid` DESC";

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


//if (!isset($_GET['pages'])) { $NombrePages=10;} else { $NombrePages=($_GET['pages']);}
$limitePage = 0 ;

// 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);

$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>Facturation</div></center><br>" ;

// FILTRES
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>";

$sql = "SELECT * FROM clients_base WHERE IdNumClient='".$IdNumClient."'";
$sql.=" ORDER BY `clients_base`.`Num_Client` ASC" ;   
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $Nom_Client = $row["Nom_Client"];
  		$Num_Client = $row["Num_Client"];
  

echo "<option value='$Num_Client'>$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='900' cellspacing=1 cellpadding=1 align=center border=0 >";
echo "<td><center>";
echo "<a href='modif_fa_haut.php?ordre=1' TARGET=_BLANK><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='20'>N&deg; Ligne</th>";
echo "<th width='60'>Date Facture</th>";
echo "<th width='100'>N&deg; Facture</th>";
echo "<th width='150'>Client</th>";
echo "<th width='75'>Montant HT</th>";
echo "<th width='75'>Montant TVA</th>";
echo "<th width='75'>Montant TTC</th>";
echo "<th width='75'>Acompte</th>";
echo "<th width='75'>Solde</th>";
echo "<th width='50'>Date Echeance</th>";	
echo "<th width='20' ></th>";
echo "<th width='20' >V</th>";
echo "<th width='20' >I</th>";
echo "<th width='20' >S</th>";
echo "<th width='20' >V</th>";
echo "<th width='20' >C</th>";
echo "<th width='20' >M</th>";
echo "<th width='20' >D</th>";

echo "</tr>";
$TotalPoidlivre = 0 ; 
$TotalMontantHt = 0 ;
$a=0;
$req = mysqli_query($link, $SelectEA);
while ($row = mysqli_fetch_assoc($req)) {
	$rowid = $row["rowid"];
	$num_client_fa = $row["num_client_fa"];
	$date_fa = $row["date_fa"];
	$semaine_fa = $row["semaine_fa"];	
	$facnumber = $row["facnumber"];
	$total_ht = $row["total_ht"];
	$total_ttc = $row["total_ttc"];
	$total_tva = $row["total_tva"];
	$acompte = $row["acompte"];
	$Solde_ttc = $row["Solde_ttc"];
	$fa_etat = $row["fa_etat"];
	$fa_paie = $row["fa_paie"];
	$fa_cpt = $row["fa_cpt"];
	$fa_ed = $row["fa_ed"];
	$date_ech = $row["date_ech"];
	$Timestamp = $row["Timestamp"];

$a++;
$sqlb = "SELECT * FROM clients_base WHERE Num_Client='$num_client_fa'";
$sqlb.=" AND IdNumClient='".$IdNumClient."'";   
$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_fa = DateFra($date_fa);
$date_ech = DateFra($date_ech);

echo '<tr>';
echo "<td width='20' style='text-align:center'>$a</div></td>";
echo "<td width='60' style='text-align:center'>$date_fa</div></td>";
echo "<td width='100' style='text-align:center'>$facnumber</td>";
echo "<td width='150' style='text-align:center'>$Nom_Client </td>";
echo "<td width='75' style='text-align:right'>$total_ht &euro;</td>";
echo "<td width='75' style='text-align:right'>$total_tva &euro;</td>";
echo "<td width='75' style='text-align:right'>$total_ttc &euro;</td>";
echo "<td width='75' style='text-align:right'>$acompte &euro;</td>";
echo "<td width='75' style='text-align:right'>$Solde_ttc &euro;</td>";
echo "<td width='60' style='text-align:center'>$date_ech </td>";

echo "<td width='20'><a href='ajout_detail_fa.php?ordre=4&num_fa_ligne=$rowid&facnumber=$facnumber&num_client_fa=$num_client_fa' TARGET=_BLANK>
		<img src='images/table.png' width='20' height='20'></a></td>";
echo "<td width='20'><a href=''><center><img src='images/redo.png'></center></a></td>";
switch ($fa_paie) {
    case 0:
        echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/Erase.png' width='20' height='20'></a></td>";
        break ;
    case 1:
       echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></td>";	
        break ;
         
 	} 		
switch ($fa_ed) {
    case 0:
        echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/Erase.png' width='20' height='20'></a></td>";
        break ;
    case 1:
       echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></td>";	
        break ;
 	} 
switch ($fa_cpt) {
    case 0:
        echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/Erase.png' width='20' height='20'></a></td>";
        break ;
    case 1:
       echo "<td width='20' height='20'><a class='' href='' TARGET=_BLANK><img src='images/money_bag.png' width='20' height='20'></a></td>";	
        break ;
         
 	} 
switch ($fa_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 ;
         
 	}

echo "<td width='20'><a href='modif_fa_haut.php?ordre=4&num_fa_ligne=$rowid&facnumber=$facnumber&num_client_fa=$num_client_fa' TARGET=_BLANK'><img src='images/modifie.png' width='20' height='20'></a></td>";
echo "<td width='20'><a href=''><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 date_bl BETWEEN '".$SelDeb."' AND '".$SelFin."'";
	$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>";

// bas de page

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'>";
/*
echo "total enrg = ".$total.
" | Nbpages = ".$NbPage.
" | limite suivante= ".$limitesuivante.
" | limite encours= ".$limite.
" | limite precedente= ".$limiteprecedente.
" <br> Anne =".$SelAnne_y.
" | Semaine =".$SelSem_w.
" | Num_CatPlu=".$Num_CatPlu.
" | Num_Client=".$Num_Client_sel.
" | Nombre=".$NombrePages."<br>" ;	 
*/
if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente
    .'&Sel_w='.$Sel_w
    .'&Sel_m='.$Sel_m
	.'&Sel_y='.$Sel_y
    .'&Num_Client_sel='.$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


for ($a = 1 ; $a <= 10 ; $a++) {
    $limitePage = $NombrePages + $limitePage;
	if (($limite/10)==$a) {
    echo '<a href="'.$page.'?limite='.$limitePage
    .'&Sel_w='.$Sel_w
    .'&Sel_m='.$Sel_m
	.'&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_m='.$Sel_m
	.'&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_m='.$Sel_m
	.'&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='Sel_m' value='$Sel_m'/>";
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';
?>
