<?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, 
$page, $Nom_Client, $NumAbat;
// entete de la page

$date = date("Y/m/d");
$timestamp = date('Y-m-d G:i:s');
//$semaine = date("w");
// menu haut new-supp-modif

echo "<center><br>";

/*
http://192.168.1.200/horius/ajout_br_haut.php?ordre=1
Num_client_liv=10+200
CodeTrsp=1
CodePaiement=1
date_bl=2018-08-30
date_livr=2018-08-30
Num_bl_ref=08-56
observations=
ok=Valider
*/
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		if (isset($_POST['ordre'])) { $num_ordre=$_POST['ordre']; }  else {$num_ordre=''; }
		if (isset($_POST['date_bl'])) { $date_bl=$_POST['date_bl']; }  else {$date_livr=''; }
		if (isset($_POST['date_livr'])) { $date_livr=$_POST['date_livr']; }  else {$date_livr=''; }
		if (isset($_POST['Num_bl_ref'])) { $Num_bl_ref=$_POST['Num_bl_ref']; }  else {$Num_bl_ref=''; }		
		if (isset($_POST['Num_client_liv'])) { $Num_client_liv=$_POST['Num_client_liv']; }  else {$Num_client_liv=''; }
		if (isset($_POST['observations'])) { $observations=$_POST['observations']; }  else {$observations=''; }
		if (isset($_POST['CodeTrsp'])) { $CodeTrsp=$_POST['CodeTrsp']; }  else {$CodeTrsp=''; }
		if (isset($_POST['CodePaiement'])) { $CodePaiement=$_POST['CodePaiement']; }  else {$CodePaiement=''; }
		if (isset($_POST['date_bl'])) { $date_bl=$_POST['date_bl']; } else {$date_bl=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		if (isset($_GET['ordre'])) { $num_ordre=$_GET['ordre']; }  else {$num_ordre=''; }
		if (isset($_GET['date_bl'])) { $date_bl=$_GET['date_bl']; }  else {$date_livr=''; }
		if (isset($_GET['date_livr'])) { $date_livr=$_GET['date_livr']; }  else {$date_livr=''; }
		if (isset($_GET['Num_bl_ref'])) { $Num_bl_ref=$_GET['Num_bl_ref']; }  else {$Num_bl_ref=''; }		
		if (isset($_GET['Num_client_liv'])) { $Num_client_liv=$_GET['Num_client_liv']; }  else {$Num_client_liv=''; }
		if (isset($_GET['observations'])) { $observations=$_GET['observations']; }  else {$observations=''; }
		if (isset($_GET['CodeTrsp'])) { $CodeTrsp=$_GET['CodeTrsp']; }  else {$CodeTrsp=''; }
		if (isset($_GET['CodePaiement'])) { $CodePaiement=$_GET['CodePaiement']; }  else {$CodePaiement=''; }
		if (isset($_GET['date_bl'])) { $date_bl=$_GET['date_bl']; } else {$date_bl=''; }
		$valForm="GET" ;
}


// valide donc on ajourt les lignes et efface la table temporaire	
echo "$valForm | Valider<br>";
// process des requettes

$observations = htmlspecialchars($observations, ENT_QUOTES);


$last_num_entre = 0 ;
$sql2 = "SELECT max(num_li_br) FROM gestion_br_haut";  
$req2 = mysqli_query($link, $sql2); 
$row = mysqli_fetch_row($req2);
$last_num_entre_0 = $row[0];
$last_num_entre = $last_num_entre_0 + 1 ;


$jourBl = array('01'=>31, '02'=>28, '03'=>31, '04'=>30, '05'=>31, '06'=>30, '07'=>31, '08'=>31, '09'=>30, '10'=>31, '11'=>30, '12'=>31 );
$moisbl=date("m", strtotime($date_livr));
$SelAnne = date("Y", strtotime($date_livr));
$semaine_bl=date("W", strtotime($date_livr));
$jourfin = $jourBl[$moisbl];

$SelDeb = date("Y-m-d", mktime(0,0,0,$moisbl,01,$SelAnne));
$SelFin = date("Y-m-d", mktime(0,0,0,$moisbl,$jourBl[$moisbl],$SelAnne));

// chercher le dernier num bl par mois
$last_bl = 0 ;
$sql3 = "SELECT * FROM gestion_br_haut ";
$sql3.=" WHERE `IdNumClient`='".$IdNumClient."'";  
//$sql3.=" AND `num_li_bl`='".$last_num_entre_0."'";
$sql3.=" AND date_br BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$req3 = mysqli_query($link, $sql3);
while ($row = mysqli_fetch_assoc($req3)) {
  $num_li_bl = $row["num_li_br"];
  $num_bl_base = $row["num_br"];
echo "$num_li_bl | $num_bl_base <br>" ;
 }
echo "$moisbl | $SelAnne | Jours fin : ".$jourBl[$moisbl]." | $jourfin |<br>" ;
$nbcar=strlen($num_bl_base);
$nbarrestant=$nbcar-3;
$numblfin = substr ($num_bl_base, -$nbarrestant);
$numblfin = $numblfin+1 ;
$numblfin =$moisbl."-".$numblfin;
// Teste ecriture
echo $sql3."<br>";
echo "$num_li_bl | $num_bl_base |nb car : $nbcar|$nbarrestant| numero bl : $numblfin <br>" ;
echo "trsp : $CodeTrsp | Paie : $CodePaiement | obs : $observations <br>";
//

$sql_ajout_bl_haut = "INSERT INTO gestion_br_haut VALUES ( 
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$numblfin."',  
'".$Num_bl_ref."',
'".$date_bl."',
'".$semaine_bl."',
'".$Num_client_liv."',
'".$date_livr."',
'0',
'0',
'0',
'".$CodePaiement."',
'".$CodeTrsp."',
'0',
'0',
'".$observations."',
'".$timestamp."')";

$Req_ajout_bl_haut = mysqli_query($link, $sql_ajout_bl_haut); 

echo "$sql_ajout_bl_haut | $Req_ajout_bl_haut<br>";

//http://192.168.1.200/orius/ajout_detail_bl.php?ordre=1&num_bl_ligne=2&num_bl=2&num_client_entre=10%20100&date_bl=2017-07-18

Header("Location: ajout_detail_br.php?ordre=1&num_bl_ligne=$last_num_entre&num_bl=$numblfin&num_client_entre=$Num_client_liv&date_bl=$date_bl");
?>