<?php
// ini_set('display_errors', 1);

require_once("../../common/lib.php");
require_once("../../common/define.php");

require_once("../includes/functions.php");

if(!isset($_SESSION['user'])){
    header("Location: login.php");
    exit();
}

$id = $_GET["id"];

$outp = array();
$cont = 0;

								$total_ttc_all= 0;
								$somme_all = 0;
								$reste_all = 0;
								$reste_all_in = 0;
								$reste_all_out = 0;
								
								$observation = array();
								
								
								$query_jour = "select *, a.id as id_fact, a.observation as obs FROM  pm_facture_achat as a, pm_fournisseurs as b where a.fournisseur = b.id AND a.id =".$id." order by reglement ASC, date_entre ASC"  ;
								// echo $query_jour;
								$result_jour = @$db->query($query_jour);
								if($result_jour !== false){
									foreach($result_jour as $row){
										$total_moin = 0 ;
										$total_plus = 0;
										$total = 0;
										$somme = 0;
										$somme_in = 0;
										$somme_out = 0;
										
									$date_entre = $row["date_entre"];
									$ref_bl = $row["ref_bl"];
									$total_ttc = $row["total_ttc"];
									
									$observation[$row["id_fact"]] = $row["obs"];
									
														
									$date_entre = date('d-m-Y',$date_entre) ;
									// ============ Fournisseur ============
									$fournisseur = $row['name'];
									$fournisseur_id = $row['id'];
									$date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row["reglement"].' days')); 

									// ============ régelement ============
												
												//========================
												$query_in = "SELECT SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE type = 'in' AND ref_facture_achat = ".$row["id_fact"];												
												$result_in = $db->query($query_in);
												if($result_in !== false && $db->last_row_count() > 0){
													$row_in = $result_in->fetch();																
													$somme_in += $row_in['somme'];
													$retenue_montant_in = $row_in['retenue'];
												}
												//========================
			
												// echo $somme_in."<br>";
												//========================
												$total_in = $somme_in + $retenue_montant_in ;
												//=== OUT ===
												$query_out = "SELECT SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE type = 'out' AND ref_facture_achat = ".$row["id_fact"];
												$result_out = $db->query($query_out);
												if($result_out !== false && $db->last_row_count() > 0){
													$row_out = $result_out->fetch();																
													$somme_out += $row_out['somme'];
													$retenue_montant_out = $row_out['retenue'];
												}
												//========================
												$query_out2 = "SELECT SUM(montant) as somme FROM pm_transaction_product WHERE ref_facture_achat2 = ".$row["id_fact"];
												// echo $query_in2;
												$result_out2 = $db->query($query_out2);
												if($result_out2 !== false && $db->last_row_count() > 0){
													$row_out2 = $result_out2->fetch();																
													$somme_out += $row_out2['somme'];
													
												}									
												//============= Facture Avoir =================
												$total_avoir = 0;
												$query_avoir = "SELECT *, SUM(total_ttc) as total_avoir FROM `pm_facture_avoir` where ref_bl =".$row["id_fact"];
												// echo $query_avoir.";<br>";
												$result_avoir = @$db->query($query_avoir);
												if($result_avoir !== false){
													foreach($result_avoir as $row_avoir){
														$total_avoir = $row_avoir["total_avoir"];	 		
													}
												}
												//=============================================
											$total_out = $somme_out + $retenue_montant_out + $total_avoir ;
											$somme = $total_in - $total_out ;
											$reste = ($total_ttc+$total_in)-$total_out;	
											
											$reste = number_format($reste,3, '.','');
											
											$id_facture = get_info_table_codition("pm_facture_achat","id"," ref_bl ='".$ref_bl."'");
											
											if($reste <> 0 && $id_facture == $id){	
											
											$somme_all += $somme;							 
											$total_ttc_all += $total_ttc;
											
											if($reste > 0){
												$reste_all_in += $total_in;
											}else{
												$reste_all_out += $total_out;
											}
											$reste_all += $reste_all_in;
											
																					
											

																				
												$outp["date_ech"] = $date_ech;
												$outp["reste"] = $reste;
												$outp["total_reglement"] = number_format($somme,3, '.','');
												$outp["total_ttc"] = number_format($total_ttc,3, '.','');
												


											}
											}
										}
	echo json_encode($outp);
	

?>