<?php


header('Access-Control-Allow-Origin: *');


header('Content-Type: application/json');





$url="https://".$_SERVER['HTTP_HOST'].":443/api/agritun_masra_sortant/all?filter=&field=&start=&limit=&filters[0][co][4][fl]=masra&filters[0][co][4][op]=equal&filters[0][co][4][vl]=".$_GET['id_masra']."&filters[0][co][4][lg]=and&sort_field=&sort_order=ASC";





$curl = curl_init($url);


curl_setopt($curl, CURLOPT_URL, $url);


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);





$headers = array(


   "X-Api-Key: ".$_GET['X-Api-Key'],


   "X-Token: ".$_GET['X-Token'],


);


curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);


//for debug only!


curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);


curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);





$resp = curl_exec($curl);


curl_close($curl);





$re= json_decode($resp, true);





$tot=$re['total']; 


$qte_zit=0;


$qte_zitoun=0;


$cout=0;


$hay_sor=0;


$nchira_sor=0;


$zit_hay=0;


$zit_nchira=0;


for ($i=0;$i<$tot;$i++){


$cout += $re['data']['agritun_masra_sortant'][$i]['cout']; 


$qte_zitoun += $re['data']['agritun_masra_sortant'][$i]['qte_zitoun']; 


$qte_zit += $re['data']['agritun_masra_sortant'][$i]['qte_zit']; 


if($re['data']['agritun_masra_sortant'][$i]['qualite_zit']=="حي"){


	$hay_sor +=$re['data']['agritun_masra_sortant'][$i]['qte_zitoun'];


	$zit_hay+= $re['data']['agritun_masra_sortant'][$i]['qte_zit'];


}else{


	$nchira_sor += $re['data']['agritun_masra_sortant'][$i]['qte_zitoun'];


	$zit_nchira+= $re['data']['agritun_masra_sortant'][$i]['qte_zit'];


}





}











$url1="https://".$_SERVER['HTTP_HOST'].":443/api/agritun_paiement/all?filter=&field=&start=&limit=&filters[0][co][4][fl]=beneficiaire&filters[0][co][4][op]=equal&filters[0][co][4][vl]=M_".$_GET['id_masra']."&filters[0][co][4][lg]=and&sort_field=&sort_order=ASC";





$curl1 = curl_init($url1);


curl_setopt($curl1, CURLOPT_URL, $url1);


curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);





$headers = array(


   "X-Api-Key: ".$_GET['X-Api-Key'],


   "X-Token: ".$_GET['X-Token'],


);


curl_setopt($curl1, CURLOPT_HTTPHEADER, $headers);


//for debug only!


curl_setopt($curl1, CURLOPT_SSL_VERIFYHOST, false);


curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);





$resp1 = curl_exec($curl1);


curl_close($curl1);





$re1= json_decode($resp1, true);


$tot1=$re1['total']; 


$paiements=0;


for ($j=0;$j<$tot1;$j++){


$paiements += $re1['data']['agritun_paiement'][$j]['montant']; 


	


}





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.'"}';


?>