<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['agent_id']))
{
	$agent_id = $_POST['agent_id'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime = date('h:i:sa');
	$cdate1 = isset($_POST['cdate1']) ? $_POST['cdate1'] : $cdate;
	$cdate2 = isset($_POST['cdate2']) ? $_POST['cdate2'] : $cdate;
	$total_short_cash=0;
	$response['amount']=array();

	$select_premium = mysqli_query($con,"select * from tbl_customer_premium,tbl_policy_master where tbl_customer_premium.agent_id='$agent_id' and tbl_customer_premium.transaction_date>='$cdate1' and tbl_customer_premium.transaction_date<='$cdate2' and tbl_customer_premium.policy_no=tbl_policy_master.policy_no and tbl_customer_premium.status='Complete' ORDER by transaction_id");

	$amt=0;
	$com=0;
	if(mysqli_num_rows($select_premium)>0)
	{
		while($fetch_details=mysqli_fetch_assoc($select_premium))
		{

			$vehicle_id=$fetch_details['vehicle_id'];
			$policy_no=$fetch_details['policy_no'];
			$premium=$fetch_details['agent_received_amt'];
			$policy_date=$fetch_details['transaction_date'];
			$totalA=$fetch_details['totalA'];
			$totalB=$fetch_details['totalB'];
			$totalC=$fetch_details['totalC'];

			$query = mysqli_query($con,"SELECT * from tbl_commission_type where tbl_commission_type.vehicle_id='$vehicle_id' order by tbl_commission_type.commission_id");

			if(mysqli_num_rows($query)>0)
			{
				while($fetch_percentage=mysqli_fetch_assoc($query)){

					$percentage=$fetch_percentage['percentage'];
					$percentage2=$fetch_percentage['percentage2'];
					$type=$fetch_percentage['type'];
					$start_date=$fetch_percentage['start_date'];
					$end_date=$fetch_percentage['end_date'];	
					if(strtotime($start_date) <= strtotime($policy_date) && strtotime($end_date) >= strtotime($policy_date)){
						if($type=='OD')
						{	
							$per1=$percentage;
							$per2="0";
							$com=($totalA+$totalC)*((float)$percentage/100);
							$amt =(float)$amt+(float)$com;

						}
						elseif($type=='TP')
						{
							$per2=$percentage2;
							$per1="0";
							$com =($totalB)*((float)$percentage2/100)+($totalC)*((float)$percentage/100);
							$amt =(float)$amt+(float)$com;
						}
						elseif($type=='Both')
						{
							$per2=$percentage2;
							$per1=$percentage;
							$com=($totalA)*((float)$percentage/100)+($totalB)*((float)$percentage2/100)+($totalC)*((float)$percentage/100);
							$amt =(float)$amt+(float)$com;
						}

						$amount['commission']= $com;
						$amount['od']= $per1;
						$amount['tp']= $per2;
						$amount['policy_number']= $policy_no;

						$query2=mysqli_query($con,"SELECT company_name from tbl_insurance_company where company_id=(Select company_id from tbl_policy_master where policy_no='$policy_no') ");
						if(mysqli_num_rows($query2)>0){
							$fetch_qur2=mysqli_fetch_assoc($query2);
							$amount['company_name']= $fetch_qur2['company_name'];
						}


						$qur = mysqli_query($con,"Select tbl_vehicle_master.vehicle_type,tbl_agent_commission.credit_amt from tbl_vehicle_master,tbl_agent_commission WHERE tbl_vehicle_master.vehicle_id = '$vehicle_id' and tbl_agent_commission.policy_no='$policy_no'");
						// $qur = mysqli_query($con,"Select tbl_vehicle_master.vehicle_type from tbl_vehicle_master WHERE tbl_vehicle_master.vehicle_id = '$vehicle_id'");
						if(mysqli_num_rows($qur)>0){
							$fetch_qur=mysqli_fetch_assoc($qur);
							$amount['vehicle_type']= $fetch_qur['vehicle_type'];
							$amount['short_cash']= $fetch_qur['credit_amt'];
							$total_short_cash = $total_short_cash+$fetch_qur['credit_amt'];
						}else{
							$amount['vehicle_type']= "";
							$amount['short_cash']= "0";
							$total_short_cash = 0;
						}
						$amount['vehicle_id']= $vehicle_id;
						$amount['premium_amount']= $premium;
						
						$response['amount'][]=$amount;
					}
				}	
			}

		}
	}
	$response['agent_id']=$agent_id;
	$response['total_commission']=number_format($amt,2,'.','');
	$response['total_short_cash']=strval($total_short_cash);
	$response['total_amount']=strval(number_format($amt,2,'.','')-$total_short_cash);
	$response['success']='1';
	$response['details']="Fetch Detail Successful";
}
else
{
	$response['success']='0';
	$response['details']="Required Field Missing!!";
}	
echo json_encode($response);
?>