<?php

ini_set('display_errors', 0);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'rapport_vente');
if(!isset($_SESSION['user'])){
    header("Location: ../login.php");
    exit();
}
require_once("../includes/fn_module.php"); 


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

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


								$total_ttc_all= 0;
								$somme_all = 0;
								$reste_all = 0;
								
								$date_jour = time();
								
								
								// echo $date_jour;
								
								$query_jour = "select * FROM  pm_facture where id_client = ".$GET_["id_client"]." order by date_facture ASC"  ;
								// $query_jour = "select *, a.id as id_fact FROM  pm_facture as a, pm_client as b where a.Id_client = b.id order by date_facture ASC"  ;
								// echo $query_jour.";<br>";
								$result_jour = @$db->query($query_jour);
								if($result_jour !== false){
									foreach($result_jour as $row){
										$is_day = "";
										$total_moin = 0 ;
										$total_plus = 0;
										$total = 0;
										$somme = 0;
										$somme_avoir = 0;
										
																				
									$date_entre = $row["date_facture"];
									$ref_bl = $row["ref_bl"];
									$idd = $row["idd"];
									
									$id = $row["id"];
									
									$observations = $row['observation'];
									
																		
									
									
									$date_entre = date('d-m-Y',$date_entre) ;
									// ============ client ============
									// $client = $row['name'];
									
												$query_fr = "SELECT name,echeance FROM pm_client WHERE id = ".$row["id_client"];
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$client = $row_fr['name'];
																// $date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row_fr["echeance"].' days')); 
															}	
									$date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row_fr["echeance"].' days')); 						
									// ============ régelement ============
																					
												$query_opp = "SELECT * ,SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE ref_facture_vente = ".$row["id"];
												// echo $query_opp.";<br>";
												$result_opp = $db->query($query_opp);
															if($result_opp !== false && $db->last_row_count() > 0){
																$row_opp = $result_opp->fetch();																
																$somme = $row_opp['somme'];
																$type = $row_opp['type'];
																$retenue_montant = $row_opp['retenue'];
																
															}
									
											$total_ttc = $row["total_ttc"];
											
											
									if($date_jour >= strtotime($date_ech)){
										$is_day = " style='background-color:#ffb9b9;'";
									}
									// ============ facture_avoir_vente ============
									$query_avoir = "SELECT * ,SUM(total_ttc) as somme_avoir FROM pm_facture_avoir_vente WHERE ref_facture = ".$row["id"];
												// echo $query_opp.";<br>";
									$result_avoir = $db->query($query_avoir);
									if($result_avoir !== false && $db->last_row_count() > 0){
										$row_avoir = $result_avoir->fetch();																
										$somme_avoir = $row_avoir['somme_avoir'];
										$type = "in" ;
										// echo $somme_avoir." ==> ".$row["id"]."<br>";
									}
									// =============================================
									$somme = $somme + $somme_avoir ;
											$reste = $total_ttc-($somme+$retenue_montant);
											
											$reste = number_format($reste,3, '.','');
											// $reste2 = number_format($reste,3, '.',' ');
											
										if((($row_opp['status_paiement'] <> 1)||($reste <> 0))&&($row["id_client"] == $_GET["id_client"])){
											// echo $row["total_ttc"]." ==>> '". $row_opp['status_paiement'] ."'<br>";
												
											
											if($type == "in"){	
												$somme = $somme * (-1);
											}
											
											$reste_all += $reste; 
											$somme_all += $somme;											
											$total_ttc_all += $total_ttc;
											



											//=============
											$is_add = 0 ;
											$totallllll = 0 ;
											$montant_facture = 0 ;
											$restoo = 0 ;
											$query_fac2 = "SELECT *, SUM(montant) as newPay FROM `pm_transaction_product` where ref_facture_vente2 =".$id;
												// echo $query_fac2.";<br>";
												$result_fac2 = @$db->query($query_fac2);
												if($result_fac2  !== false){
													foreach($result_fac2  as $row_fac2 ){
														$is_add = $row_fac2["id"];	 		
														$reste  = $row_fac2["reste"];	 		
														$montant_facture = $row_fac2["montant_facture"];	
														$totallllll = ($row_fac2["newPay"] * (-1))+ $somme ;	
														
														$restoo  = $total_ttc + $totallllll;	
														// echo $id." ==> ".$total_ttc." ==> ".$totallllll." ==> ".$restoo."<br>";															
													}
												}											
											//=============
											
										?>

										<?	
										
										if(($restoo > 0)){	
											$outp[$cont] = $row["id"];
											$cont++;
										}	
											
											
											
										}
											}
										}
										

echo json_encode($outp);

?>