<?php
include("../connect.php");
$response = array();
if(isset($_POST['manager_id']) && isset($_POST['admin_id']) && isset($_POST['user_type']) && isset($_POST['agent_id']))
{
	$manager_id = $_POST['manager_id'];
	$admin_id = $_POST['admin_id'];
	$user_type = $_POST['user_type'];
	$agent_id = $_POST['agent_id'];

	if($user_type=='Admin') 
		$select_policy = mysqli_query($con,"SELECT tbl_policy_master.*,tbl_customer_premium.policy_no,tbl_customer_premium.agent_id FROM `tbl_customer_premium`,`tbl_policy_master` where tbl_policy_master.policy_no = tbl_customer_premium.policy_no and tbl_customer_premium.agent_id = '$agent_id' and tbl_policy_master.admin_id = '$admin_id' and tbl_policy_master.status in ('Active','Renewal Pending')");
	else
		$select_policy = mysqli_query($con,"SELECT tbl_policy_master.*,tbl_customer_premium.policy_no,tbl_customer_premium.agent_id FROM `tbl_customer_premium`,`tbl_policy_master` where tbl_policy_master.policy_no = tbl_customer_premium.policy_no and tbl_customer_premium.agent_id = '$agent_id' and tbl_policy_master.manager_id = '$manager_id'  and tbl_policy_master.status in ('Active','Renewal Pending')");

$policies=array();
	if(mysqli_num_rows($select_policy)>0)
	{     
		while($fetch_policies=mysqli_fetch_assoc($select_policy))
		{
			$policies['admin_id'] = $fetch_policies['admin_id'];
			$policies['manager_id'] = $fetch_policies['manager_id'];
			$policies['customer_id']=$fetch_policies['customer_id'];
			$policies['id']=$fetch_policies['id'];
			$policies['policy_no']=$fetch_policies['policy_no'];
			$policies['policy_date']=$fetch_policies['policy_date'];
			$policies['renewal_date']=$fetch_policies['renewal_date'];
			$policies['company_id']=$fetch_policies['company_id'];
			$policies['policy_type']=$fetch_policies['policy_type'];
			$policies['vehicle_id']=$fetch_policies['vehicle_id'];

			$query = mysqli_query($con,"SELECT * FROM tbl_insurance_company,tbl_policy_type,tbl_vehicle_master where tbl_insurance_company.company_id='".$fetch_policies['company_id']."' and tbl_policy_type.policy_id = '".$fetch_policies['policy_type']."' and tbl_vehicle_master.vehicle_id = '".$fetch_policies['vehicle_id']."'");
			if(mysqli_num_rows($query)>0){     
				while($fetch_query=mysqli_fetch_assoc($query)){
					$policies['company']=$fetch_query['company_name'];
					$policies['policy']=$fetch_query['policy_name'];
					$policies['vehicle']=$fetch_query['vehicle_type'];
				}
			}

			$query = mysqli_query($con,"SELECT tbl_agent.agent_name,tbl_agent.agent_phone FROM `tbl_policy_master` ,tbl_customer_premium,tbl_agent where tbl_policy_master.policy_no=tbl_customer_premium.policy_no and tbl_customer_premium.agent_id=tbl_agent.agent_id and tbl_policy_master.policy_no='".$fetch_policies['policy_no']."'");
			if(mysqli_num_rows($query)>0){     
				$fetch_query=mysqli_fetch_assoc($query);
					$policies['agent_name']=$fetch_query['agent_name'];
					$policies['agent_phone']=$fetch_query['agent_phone'];				
			}

			$policies['make_model']=$fetch_policies['make_model'];
			$policies['idv']=$fetch_policies['idv'];
			$policies['registration_no']=$fetch_policies['registration_no'];
			$policies['engine_no']=$fetch_policies['engine_no'];
			$policies['chassis_no']=$fetch_policies['chassis_no'];
			$policies['manufacturing_year']=$fetch_policies['manufacturing_year'];
			$policies['seating_capacity']=$fetch_policies['seating_capacity'];
			$policies['body_type']=$fetch_policies['body_type'];
			$policies['fuel_type']=$fetch_policies['fuel_type'];
			$policies['hp_cc']=$fetch_policies['hp_cc'];
			$policies['policy_term']=$fetch_policies['policy_term'];
			$policies['term_value']=$fetch_policies['term_value'];
			$policies['NCB']=$fetch_policies['NCB'];
			$policies['NCB_value']=$fetch_policies['NCB_value'];
			$policies['od']=$fetch_policies['od'];
			$policies['od_discount_per']=$fetch_policies['od_discount_per'];
			$policies['od_discount']=$fetch_policies['od_discount'];
			$policies['cover']=$fetch_policies['cover'];
			$policies['pa']=$fetch_policies['pa'];
			$policies['liabilities']=$fetch_policies['liabilities'];
			$policies['passenger_assist']=$fetch_policies['passenger_assist'];
			$policies['depreciation']=$fetch_policies['depreciation'];
			$policies['totalA']=$fetch_policies['totalA'];
			$policies['totalB']=$fetch_policies['totalB'];
			$policies['totalC']=$fetch_policies['totalC'];
			$policies['calculation_param']=$fetch_policies['calculation_param'];
			$policies['net_premium']=$fetch_policies['net_premium'];
			$policies['gst_typeA']=$fetch_policies['gst_typeA'];
			$policies['gst_typeB']=$fetch_policies['gst_typeB'];
			$policies['gst_typeC']=$fetch_policies['gst_typeC'];
			$policies['gst_percentageA']=$fetch_policies['gst_percentageA'];
			$policies['gst_percentageB']=$fetch_policies['gst_percentageB'];
			$policies['gst_percentageC']=$fetch_policies['gst_percentageC'];
			$policies['cGstA']=$fetch_policies['cGstA'];
			$policies['sGstA']=$fetch_policies['sGstA'];
			$policies['iGstA']=$fetch_policies['iGstA'];
			
			$policies['cGstB']=$fetch_policies['cGstB'];
			$policies['sGstB']=$fetch_policies['sGstB'];
			$policies['iGstB']=$fetch_policies['iGstB'];
			
			$policies['cGstC']=$fetch_policies['cGstC'];
			$policies['sGstC']=$fetch_policies['sGstC'];
			$policies['iGstC']=$fetch_policies['iGstC'];
			
			$policies['totalGst']=$fetch_policies['totalGst'];
			$policies['premium_amt']=$fetch_policies['premium_amt'];
			$policies['status']=$fetch_policies['status'];
			$response['policies'][]=$policies;
		}
	}
	else
	{
		$response['policies'][]=$policies;
	}


	$response['success']=1;	
	$response['details']="Data Success";
}
else
{
	$response['success']=0;
	$response['details']="Required fields missing";
}
echo json_encode($response);
?> 