<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include '../db_api.php';
// Perform query
mysqli_set_charset($conn,"utf8mb4");

//agritun_masra_sortant

$sql = "SELECT * FROM `agritun_masra_sortant` where `masra`='".$_GET['id_masra']."'";
$result = $conn->query($sql);
$qte_zit=0;
$qte_zitoun=0;
$cout=0;
$hay_sor=0;
$nchira_sor=0;
$zit_hay=0;
$zit_nchira=0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
	$cout += $row['cout']; 
	$qte_zitoun += $row['qte_zitoun']; 
	$qte_zit += $row['qte_zit']; 
	if($row['qualite_zit']=="حي"){
		$hay_sor +=$row['qte_zitoun'];
		$zit_hay+= $row['qte_zit'];
	}else{
		$nchira_sor += $row['qte_zitoun'];
		$zit_nchira+= $row['qte_zit'];
	}
}
}


//agritun_masra_sortant

$sql = "SELECT * FROM `agritun_paiement` where `beneficiaire`='M_".$_GET['id_masra']."'";
$result = $conn->query($sql);
$paiements=0;

if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) {
		$paiements += $row['montant']; 
	}
}

$conn -> close();


echo '{"cout":"'.$cout.'","paiements":"'.$paiements.'","qte_zitoun":"'.$qte_zitoun.'","qte_zit":"'.$qte_zit.'","hay_sor":"'.$hay_sor.'","nchira_sor":"'.$nchira_sor.'","zit_nchira":"'.$zit_nchira.'","zit_hay":"'.$zit_hay.'"}';

?>