<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['admin_id']))
{
	$admin_id = $_POST['admin_id'];
	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d");
	$ctime = date('h:i:sa');
	
	
	
	$select_product = mysqli_query($con,"select * from tbl_insurance_company where admin_id='$admin_id'");
	if(mysqli_num_rows($select_product)>0)
	{     
		$ftd_deatil=array();
		while($fetch_product=mysqli_fetch_assoc($select_product))
		{
			$ftd_deatil['company_id']=$fetch_product['company_id'];
			$ftd_deatil['company_name']=$fetch_product['company_name'];
			$comp_id=$fetch_product['company_id'];
			$actual_amt=0;
			$select_detail = mysqli_query($con,"select sum(tbl_customer_premium.premium_amt) as final_net_amt from tbl_customer,tbl_customer_premium,tbl_policy_master where tbl_customer.customer_id=tbl_policy_master.customer_id and tbl_customer_premium.customer_id=tbl_customer.customer_id and tbl_customer_premium.policy_no=tbl_policy_master.policy_no and tbl_policy_master.company_id='$comp_id' and tbl_customer_premium.status='Complete' and tbl_customer_premium.complete_date='$cdate' group by tbl_policy_master.company_id");
			while($fetch_data=mysqli_fetch_assoc($select_detail))
			{
				$actual_amt=$fetch_data['final_net_amt'];
			}
			$ftd_deatil['total_amt']=$actual_amt;
			$response['ftd_detail'][]=$ftd_deatil;
		}
	}
	
	$select_product = mysqli_query($con,"select * from tbl_insurance_company where admin_id='$admin_id'");
	if(mysqli_num_rows($select_product)>0)
	{ 	
		$mtd_detail=array();
		$cdate11  = date("m");
		$cdate12  = date("Y");
		if(strlen($cdate11)==1)
			$cdate11="0".$cdate11;
		$cdate1 = $cdate12."-".$cdate11."-01";
		$cdate2 = $cdate12."-".$cdate11."-30";
		$cdate2=date("Y-m-t", strtotime($cdate2));
		while($fetch_product1=mysqli_fetch_assoc($select_product))
		{
			$mtd_detail['company_id']=$fetch_product1['company_id'];
			$mtd_detail['company_name']=$fetch_product1['company_name'];
			$comp_id=$fetch_product1['company_id'];
			$actual_amt=0;
			$select_detail = mysqli_query($con,"select sum(tbl_customer_premium.premium_amt) as final_net_amt from tbl_customer,tbl_customer_premium,tbl_policy_master where tbl_customer.customer_id=tbl_policy_master.customer_id and tbl_customer_premium.customer_id=tbl_customer.customer_id  and tbl_customer_premium.policy_no=tbl_policy_master.policy_no and tbl_policy_master.company_id='$comp_id' and tbl_customer_premium.status='Complete' and tbl_customer_premium.complete_date>='$cdate1' and tbl_customer_premium.complete_date<='$cdate2' group by tbl_policy_master.company_id");
			while($fetch_data=mysqli_fetch_assoc($select_detail))
			{
				$actual_amt=$fetch_data['final_net_amt'];
			}
			$mtd_detail['total_amt']=$actual_amt;
			$response['mtd_detail'][]=$mtd_detail;
		}
	}
		
		
	$select_product = mysqli_query($con,"select * from tbl_insurance_company where admin_id='$admin_id'");
	if(mysqli_num_rows($select_product)>0)
	{ 
		$ytd_detail=array();
		$cdate12  = date("Y");
		$cdate1 = $cdate12."-01-01";
		while($fetch_product2=mysqli_fetch_assoc($select_product))
		{
			$ytd_detail['company_id']=$fetch_product2['company_id'];
			$ytd_detail['company_name']=$fetch_product2['company_name'];
			$comp_id=$fetch_product2['company_id'];
			$actual_amt=0;
			$select_detail = mysqli_query($con,"select sum(tbl_customer_premium.premium_amt) as final_net_amt from tbl_customer,tbl_customer_premium,tbl_policy_master where tbl_customer.customer_id=tbl_policy_master.customer_id and tbl_customer_premium.customer_id=tbl_customer.customer_id and tbl_customer_premium.policy_no=tbl_policy_master.policy_no and tbl_policy_master.company_id='$comp_id' and tbl_customer_premium.status='Complete' and tbl_customer_premium.complete_date>='$cdate1' group by tbl_policy_master.company_id");
			while($fetch_data=mysqli_fetch_assoc($select_detail))
			{
				$actual_amt=$fetch_data['final_net_amt'];
			}
			$ytd_detail['total_amt']=$actual_amt;
			$response['ytd_detail'][]=$ytd_detail;
		}
	}
	
	$response['success']='1';
	$response['details']="Fetch Detail Successful";
}
else
{
	$response['success']='0';
	$response['details']="Required Field Missing!!";
}	
echo json_encode($response);
?>