<?php
include ('connect.php');
session_start(); 
$ver = $_SESSION["admin_id"];
$admin_id = $_SESSION["admin_id"];
$dis_id = $_SESSION["manager_id"];
$user_type = $_SESSION["user_type"];
$user_session = $_SESSION["user_session"];


$csv_filename = 'PolicyReport_'.date('Y-m-d').'.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$csv_filename);
$fp = fopen('php://output', 'w');

fputcsv($fp, array('Customer_Name', 'Customer_Phone', 'Customer_Address','Company_Name','Agent_Name	','Agent_Phone_No.','Policy_Number','Policy_Date','Policy_Term','Registration_Number','Net_Premium','Total_GST','Total_Premium'));

$res=mysqli_query($con,"SELECT * FROM `tbl_policy_master` ,tbl_customer,tbl_vehicle_master where tbl_policy_master.customer_id=tbl_customer.customer_id and tbl_vehicle_master.vehicle_id=tbl_policy_master.vehicle_id and tbl_policy_master.admin_id='$ver' and tbl_policy_master.status in ('Active','Renewal Pending')");
if(mysqli_num_rows($res)>0)
{     
	while($fetch=mysqli_fetch_assoc($res))
	{
		extract($fetch);
		$policy_no=$fetch['policy_no'];
		$query_company=mysqli_query($con,"SELECT * FROM `tbl_policy_master` ,tbl_insurance_company where tbl_policy_master.company_id=tbl_insurance_company.company_id and tbl_policy_master.policy_no='$policy_no'");
		$fetch11=mysqli_fetch_assoc($query_company);
		$query1=mysqli_query($con,"SELECT * 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='$policy_no'");
		$fetch1=mysqli_fetch_assoc($query1);
		
		
		fputcsv($fp, array($fetch['customer_name'], $fetch['phone_no'], $fetch['address'], $fetch11['company_name'],$fetch1['agent_name'],$fetch1['agent_phone'],$fetch['policy_no'],$fetch['policy_date'], $fetch['policy_term']." ".$fetch['term_value'],$registration_no,$net_premium,$totalGst,$premium_amt));
	}
}
exit;
?>