<?php


include '../db_api.php';





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


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





$url="https://".$_SERVER['HTTP_HOST']."/api/user/profile";


$curl = curl_init($url);


curl_setopt($curl, CURLOPT_URL, $url);


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);




$user_id=$_GET['user_id'];
$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);





$re= json_decode($resp, true);





if ($re['status']==1){





$total=0;
$rev_tot=0;
$prix_zit=0;
$prix_zitoun=0;
$prix_zitoun_hay=0;
$prix_zitoun_nchira=0;
$prix_zit_e=0;
$prix_zit_v=0;
$prix_zit_l=0;
$cout_trensporteur_total=0;




// Perform query


mysqli_set_charset($conn,"utf8mb4");


//parcels


$sql = "SELECT * FROM `parcels`  where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {


$tot2=0;


$sum_nbr_pieds=0;





  while($row = $result->fetch_assoc()) {


	  $tot2+=1;


		$sum_nbr_pieds += $row['nbr_pieds'];


}


} else {


  


}





//recolte

$cout=0; $hay_kg=0;$nchira_kg=0;$hay_chkara=0;$nchira_chkara=0;$hay=0;$nchira=0;
$sql = "SELECT * FROM `agritun_recolte` where user_id='$user_id'";





$result = $conn->query($sql);





if ($result->num_rows > 0) {


$cout_trensporteur_total=0;





  while($row = $result->fetch_assoc()) {	


	$total += intval($row["nbr_pieds"]);


	$cout += $row['cout']; 





	if ($row['q_unite']=="كغ"){


		$hay_kg += $row['hay']; 


		$nchira_kg += $row['nchira']; 


	}


	if ($row['q_unite']=="شكارة"){


		$hay_chkara += $row['hay']; 


		$nchira_chkara += $row['nchira']; 


	}


  }


  


  if ($hay_kg > 0){


	$hay .= $hay_kg." <i style=' color: grey;font-size: 14px; '> كغ </i> ";


}


if ($hay_chkara > 0){


	if ($hay_kg > 0){


		$hay .= "  <i style=' color: grey;font-size: 14px; '> و </i>  ";


	}


	$hay .= $hay_chkara." <i style=' color: grey;font-size: 14px; '> شكارة </i> ";


}





if ($nchira_kg > 0){


	$nchira .= $nchira_kg." <i style=' color: grey;font-size: 14px; '> كغ </i> ";


}


if ($hay_chkara > 0){


	if ($nchira_kg > 0){


		$nchira .= "  <i style=' color: grey;font-size: 14px; '> و </i>  ";


	}


	$nchira .= $nchira_chkara." <i style=' color: grey;font-size: 14px; '> شكارة </i> ";


}





} else {


  


}





//Vente


$sql = "SELECT * FROM `agritun_vente` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$prix_zit=0;$prix_zitoun=0; $prix_zitoun_hay=0; $prix_zitoun_nchira=0;


$prix_zit_e=0;$prix_zit_v=0;$prix_zit_l=0;


$rev_tot=0;





  while($row = $result->fetch_assoc()) {


	$rev_tot += intval($row['montant']);


	if($row['type_vente']=='zit'){


	$prix_zit += intval($row['montant']);


	if($row['qualite']=="extra vierge"){


		$prix_zit_e += intval($row['montant']);


	}else{


		if($row['qualite']=="vierge"){


			$prix_zit_v += intval($row['montant']);


		}else{


			$prix_zit_l += intval($row['montant']);


		}


	}


	}else{


		$prix_zitoun += intval($row['montant']);


		if($row['qualite']=="حي"){


			$prix_zitoun_hay += intval($row['montant']);


		}else{


			$prix_zitoun_nchira += intval($row['montant']);


		}


	}


  }


} else {


  


}








$sql = "SELECT * FROM `agritun_masra_entrant` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$masra_en_hay=0;


$masra_en_nchira=0;





  while($row = $result->fetch_assoc()) {


	if($row['qualite']=='حي'){


		$masra_en_hay += intval($row['quantite']);


	}else{


		$masra_en_nchira += intval($row['quantite']);


	}


  }


} else {


  


}








$sql = "SELECT * FROM `agritun_masra_sortant` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$masra_sor_hay=0;


$masra_sor_nchira=0;


$t_q_e=0;$t_q_v=0;$t_q_l=0;


$masra_sor_qt_zit_hay=0;


$masra_sor_qt_zit_nchira=0;


$masra_sor_cout=0;





  while($row = $result->fetch_assoc()) {


	$masra_sor_cout += intval($row['cout']);


	if($row['qualite']=='extra vierge'){


		$t_q_e +=intval($row['qte_zit']);


	}else{


		if($row['qualite']=='vierge'){


			$t_q_v +=intval($row['qte_zit']);


		}else{


			$t_q_l +=intval($row['qte_zit']);


		}


	}


	if($row['qualite_zit']=='حي'){


	$masra_sor_hay += intval($row['qte_zitoun']);


	$masra_sor_qt_zit_hay += intval($row['qte_zit']);


	}else{


		$masra_sor_nchira += intval($row['qte_zitoun']);


		$masra_sor_qt_zit_nchira +=intval($row['qte_zit']);


	}


  }


} else {


  


}








//paiement


$sum_total_autre=0;


$sql = "SELECT * FROM `agritun_paiement` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$sum_total_eq=0;$sum_total_tr=0;$sum_total_mr=0; $p_total=0;





  while($row = $result->fetch_assoc()) {


	$p_total += intval($row['montant']);


	if($row['service']=='equipe'){


	$sum_total_eq += intval($row['montant']);


	}else{


		if($row['service']=='masra'){


			$sum_total_mr += intval($row['montant']);


		}else{


			if($row['service']=='transporteur'){


			$sum_total_tr += intval($row['montant']);


			}else{


				$sum_total_autre += intval($row['montant']);


			}


		}


	}  }


} else {


  


}





//agritun_transport


$sql = "SELECT * FROM `agritun_transport` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$cout_trensporteur_total=0;





  while($row = $result->fetch_assoc()) {


$cout_trensporteur_total += intval($row['cout']);


}


} else {


  


}








$sql = "SELECT * FROM `agritun_achat` where user_id='$user_id'";


$result = $conn->query($sql);


if ($result->num_rows > 0) {





$achat=0;





  while($row = $result->fetch_assoc()) {


	$achat += intval($row['total']);





  }


} else {


  


}





$conn -> close();








$total_total_cout=$cout_trensporteur_total+$masra_sor_cout+$cout+$achat;





$rev=intval($rev_tot)-intval($total_total_cout);








echo '{"total":"'.$total.'","sum_nbr_pieds":"'.$sum_nbr_pieds.'","hay":"'.$hay.'","nchira":"'.$nchira.'","cout":"'.$cout.'","nbr_parcels":"'.$tot2.'","prix_zit":"'.$prix_zit.'","prix_zitoun":"'.$prix_zitoun.'","prix_zitoun_hay":"'.$prix_zitoun_hay.'","prix_zitoun_nchira":"'.$prix_zitoun_nchira.'","prix_zit_e":"'.$prix_zit_e.'","prix_zit_v":"'.$prix_zit_v.'","prix_zit_l":"'.$prix_zit_l.'","masra_e_h":"'.$masra_en_hay.'","masra_e_n":"'.$masra_en_nchira.'","masra_sor_n":"'.$masra_sor_nchira.'","masra_sor_h":"'.$masra_sor_hay.'","m_s_e":"'.$t_q_e.'","m_s_v":"'.$t_q_v.'","m_s_l":"'.$t_q_l.'","m_s_z_h":"'.$masra_sor_qt_zit_hay.'","m_s_z_n":"'.$masra_sor_qt_zit_nchira.'","p_eq":"'.$sum_total_eq.'","p_masra":"'.$sum_total_mr.'","p_transporteur":"'.$sum_total_tr.'","cout_masra":"'.$masra_sor_cout.'","cout_trans":"'.$cout_trensporteur_total.'","p_total":"'.$p_total.'","cout_total_total":"'.$total_total_cout.'","rev_total_total":"'.$rev_tot.'","hay_kg":"'.$hay_kg.'","hay_chkara":"'.$hay_chkara.'","nchira_kg":"'.$nchira_kg.'","nchira_chkara":"'.$nchira_chkara.'","achat":"'.$achat.'","sum_total_autre":"'.$sum_total_autre.'"}';








}


?>