<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['admin_id']) && isset($_POST['manager_id']) && isset($_POST['user_type']))
{
	$dis_id = $_POST['manager_id'];
	$admin_id = $_POST['admin_id'];
	$user_type = $_POST['user_type'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$cdate1 = isset($_POST['cdate1']) ? $_POST['cdate1'] : $cdate;
	$cdate2 = isset($_POST['cdate2']) ? $_POST['cdate2'] : $cdate;
	if($user_type=='Manager') 
		$res_product_stock = mysqli_query($con,"SELECT *,tbl_commission.status as comstatus FROM  tbl_commission,tbl_agent,tbl_manager where tbl_commission.manager_id=tbl_manager.manager_id and tbl_commission.agent_id=tbl_agent.agent_id and tbl_manager.manager_id='$dis_id' and tbl_commission.insert_date>='$cdate1' and tbl_commission.insert_date<='$cdate2'");
	else
		$res_product_stock = mysqli_query($con,"SELECT *,tbl_commission.status as comstatus FROM  tbl_commission,tbl_agent,tbl_manager where tbl_commission.manager_id=tbl_manager.manager_id and tbl_commission.agent_id=tbl_agent.agent_id and tbl_manager.admin_id='$admin_id' and tbl_commission.insert_date>='$cdate1' and tbl_commission.insert_date<='$cdate2'");
	if(mysqli_num_rows($res_product_stock)>0)
	{   
		$agent_commission=array();	
		while($fetch_product=mysqli_fetch_assoc($res_product_stock))
		{
			$agent_commission['transaction_id']=$fetch_product['commission_id'];
			$agent_commission['agent_name']=$fetch_product['agent_name']." - ".$fetch_product['agent_phone'];
			$agent_commission['manager_name']=$fetch_product['manager_name']." - ".$fetch_product['manager_phone'];
			$agent_commission['transaction_date']=$fetch_product['insert_date'];
			$agent_commission['transaction_time']=$fetch_product['insert_time'];
			$agent_commission['commission_amount']=$fetch_product['amount'];
			$agent_commission['txn_status']=$fetch_product['comstatus'];
			$agent_commission['reason']=$fetch_product['rejection_reason'];
			$agent_commission['paid_amt']=$fetch_product['paid_amt'];
			$a_id=$fetch_product['agent_id'];
			$adjust_amount_val=0;
			$res = mysqli_query($con,"SELECT * FROM  tbl_agent_commission where agent_id='$a_id' and status='Pending'");
			while($fetch_detail=mysqli_fetch_assoc($res))
			{
				$adjust_amount_val=$adjust_amount_val+$fetch_detail['credit_amt']-$fetch_detail['adjust_amount'];
			}
			$agent_commission['adjust_amount']=$adjust_amount_val;
			$response['agent_commission'][]=$agent_commission;
		}
		$response['success']='1';
		$response['details']="Fetch Detail Successful";
	}
	else
	{
		$response['success']='0';
		$response['details']="No Data Available";
	}
}
else
{
	$response['success']='0';
	$response['details']="Required Field Missing!!";
}	
echo json_encode($response);
?>