<?php
ini_set('display_errors', 0);

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

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

$id = $_GET["id"];

$query = "SELECT * from pm_facture_achat where fournisseur <> ".$id;
// echo $query;
$result = @$db->query($query);
$outp = array();
$cont = 0;
if($result !== false){
	foreach($result as $row){
		
		$outp[$cont] = $row["id"];
		$cont++;		
	} 
}	
$query_payer = "SELECT a.id , b.montant, b.retenue_montant, a.total_ttc, b.retenue  from pm_facture_achat as a, pm_transaction as b where a.fournisseur = b.fournisseur AND b.ref_facture_achat = a.id AND b.fournisseur =".$id;	
// echo $query_payer;
$result_payer = @$db->query($query_payer);
if($result_payer !== false){
	foreach($result_payer as $row_payer){
		if($row_payer["retenue_montant"]){
			$retenue_montant = $row_payer["retenue_montant"];
		}else{
			$retenue_montant = 0;
		}
		$retenu = ($row_payer["total_ttc"] * $row_payer["retenue"] ) /100 ; 
		$total_payee = floatval($row_payer["total_ttc"] - ($row_payer["montant"] + $row_payer["retenue_montant"]));
		// echo "<br>".$row_payer["id"]." >> ".$total_payee ." >> ".$row_payer["total_ttc"] ." - ". ($row_payer["montant"] + $row_payer["retenue_montant"])."<br>";
		
		$val_1 = intval ($row_payer["total_ttc"]) ;
		$val_2 = intval ($row_payer["montant"] + $row_payer["retenue_montant"]) ;
		$if_payer = $val_1 - $val_2 ;
		// echo "<br>".$row_payer["id"]." >>".$if_payer." >> ".$row_payer["total_ttc"] ." - ". ($row_payer["montant"] + $row_payer["retenue_montant"])."<br>";
		// if($total_payee <= 0){	
		if($if_payer <= 0){	
			$outp[$cont] = $row_payer["id"];
			$cont++;
		}	
	} 
}		
	
	echo json_encode($outp);
	





?>