<?php
include("../connect.php"); 
$response = array();
if(isset($_POST['manager_id']) && isset($_POST['customer_id'])&& isset($_POST['admin_id'])&& isset($_POST['agent_id']) && isset($_POST['policy_no']) && isset($_POST['company_id']) && isset($_POST['policy_date']) && isset($_POST['renewal_date']) && isset($_POST['policy_type']) && isset($_POST['vehicle_id']) && isset($_POST['make_model']) && isset($_POST['idv']) && isset($_POST['reg_no']) && isset($_POST['engine_no'])&& isset($_POST['chassis_no']) && isset($_POST['mfg_year']) && isset($_POST['seating_cap']) && isset($_POST['body_type']) && isset($_POST['fuel_type']) && isset($_POST['hp']) && isset($_POST['policy_term']) && isset($_POST['term_value']) && isset($_POST['NCB'])&& isset($_POST['od']) && isset($_POST['cover']) && isset($_POST['pa']) && isset($_POST['liabilities']) && isset($_POST['passenger_assist']) && isset($_POST['depreciation']) && isset($_POST['calculation_param']) && isset($_POST['net_premium']) && isset($_POST['gst_typeA'])&& isset($_POST['gst_typeB'])&& isset($_POST['gst_typeC']) && isset($_POST['gst_percentageA'])&& isset($_POST['gst_percentageB'])&& isset($_POST['gst_percentageC'])&& isset($_POST['totalA'])&& isset($_POST['totalB'])&& isset($_POST['totalC']) && isset($_POST['premium_amt']) && isset($_POST['previous_policy']))
{
	
	$previous_policy = $_POST['previous_policy'];
	$customer_name = $_POST['customer_name'];
	$phone_no = $_POST['phone_no'];
	$address = $_POST['address'];
	$email = $_POST['email'];
	$manager_id = $_POST['manager_id'];
	$customer_id=$_POST['customer_id'];
	$admin_id=$_POST['admin_id'];
	$unique_id=$_POST['unique_id'];
	$agent_id=$_POST['agent_id'];
	$policy_no=$_POST['policy_no'];
	$company_id=$_POST['company_id'];
	$policy_date=$_POST['policy_date'];
	$renewal_date=$_POST['renewal_date'];
	$policy_type=$_POST['policy_type'];
	$vehicle_id=$_POST['vehicle_id'];
	$make_model=$_POST['make_model'];
	$idv=$_POST['idv'];
	$reg_no=$_POST['reg_no'];
	$engine_no=$_POST['engine_no'];
	$chassis_no=$_POST['chassis_no'];
	$mfg_year=$_POST['mfg_year'];
	$seating_cap=$_POST['seating_cap'];
	$body_type=$_POST['body_type'];
	$fuel_type=$_POST['fuel_type'];
	$hp=$_POST['hp'];
	$policy_term=$_POST['policy_term'];
	$term_value=$_POST['term_value'];
	$NCB=$_POST['NCB'];
	$NCB_value = isset($_POST['NCB_value']) ? $_POST['NCB_value'] : '0';
	$od=$_POST['od'];
	$od_discount=$_POST['od_discount'];
	$od_discount_per=$_POST['od_discount_per'];
	$shortPremium=$_POST['shortPremium'];
	$cover=$_POST['cover'];
	$pa=$_POST['pa'];
	$liabilities=$_POST['liabilities'];
	$passenger_assist=$_POST['passenger_assist'];
	$depreciation=$_POST['depreciation'];
	$totalA=$_POST['totalA'];
	$totalB=$_POST['totalB'];
	$totalC=$_POST['totalC'];
	$calculation_param=$_POST['calculation_param'];
	$net_premium=$_POST['net_premium'];
	$gst_typeA=$_POST['gst_typeA'];
	$gst_typeB=$_POST['gst_typeB'];
	$gst_typeC=$_POST['gst_typeC'];
	$gst_percentageA=$_POST['gst_percentageA'];
	$gst_percentageB=$_POST['gst_percentageB'];
	$gst_percentageC=$_POST['gst_percentageC'];
	$cGstA=$_POST['cGstA'];
	$sGstA=$_POST['sGstA'];
	$iGstA=$_POST['iGstA'];
	
	$cGstB=$_POST['cGstB'];
	$sGstB=$_POST['sGstB'];
	$iGstB=$_POST['iGstB'];
	
	$cGstC=$_POST['cGstC'];
	$sGstC=$_POST['sGstC'];
	$iGstC=$_POST['iGstC'];
	$totalGst=$_POST['totalGst'];

	$premium_amt=$_POST['premium_amt'];

	date_default_timezone_set('Asia/Calcutta');
	$cdate  = date("Y-m-d")." ".date('h:i:sa');
	$t_date  = date("Y-m-d");
	$t_time = date('h:i:sa');
	
	$res_product_stock = mysqli_query($con,"SELECT customer_id FROM  tbl_policy_master where policy_no='$policy_no'");
	if(mysqli_num_rows($res_product_stock)<=0)
	{		

	$query = mysqli_query($con,"INSERT INTO `tbl_policy_master`(`admin_id`, `manager_id`, `customer_id`, `policy_no`, `policy_type`, `company_id`, `policy_date`, `renewal_date`, `vehicle_id`, `make_model`, `idv`, `registration_no`, `engine_no`, `chassis_no`, `manufacturing_year`, `seating_capacity`, `body_type`, `fuel_type`, `hp_cc`, `policy_term`, `term_value`, `NCB`, `NCB_value`, `od`, `od_discount_per`, `od_discount`, `totalA`, `cover`, `pa`, `liabilities`, `totalB`, `passenger_assist`, `depreciation`, `totalC`, `gst_typeA`, `gst_percentageA`, `cGstA`, `sGstA`, `iGstA`, `gst_typeB`, `gst_percentageB`, `cGstB`, `sGstB`, `iGstB`, `gst_typeC`, `gst_percentageC`, `cGstC`, `sGstC`, `iGstC`, `calculation_param`, `net_premium`, `totalGst`, `premium_amt`, `status`, `insert_datetime`, `unique_id`) VALUES ('$admin_id', '$manager_id', '$customer_id', '$policy_no', '$policy_type', '$company_id', '$policy_date', '$renewal_date', '$vehicle_id', '$make_model', '$idv', '$reg_no', '$engine_no', '$chassis_no', '$mfg_year', '$seating_cap', '$body_type', '$fuel_type', '$hp', '$policy_term', '$term_value', '$NCB', '$NCB_value', '$od', '$od_discount_per', '$od_discount', '$totalA', '$cover', '$pa', '$liabilities', '$totalB', '$passenger_assist', '$depreciation', '$totalC', '$gst_typeA', '$gst_percentageA', '$cGstA', '$sGstA', '$iGstA', '$gst_typeB', '$gst_percentageB', '$cGstB', '$sGstB', '$iGstB', '$gst_typeC', '$gst_percentageC', '$cGstC', '$sGstC', '$iGstC', '$calculation_param', '$net_premium', '$totalGst', '$premium_amt', 'Pending', '$cdate', '$unique_id')");

	// $query1 = mysqli_query($con,"INSERT INTO `tbl_customer_premium`(`manager_id`,`agent_id`,`customer_id`,`premium_amt`,`transaction_date`,`transaction_time`,`status`,`policy_no`,`agent_received_amt`,`complete_date`,`complete_time`) VALUES ('$manager_id','$agent_id','$customer_id','$premium_amt','$t_date','$t_time','Complete','$policy_no','$premium_amt','$t_date','$t_time')");	

		$query = mysqli_query($con,"INSERT INTO `tbl_customer_premium`(`manager_id`,`agent_id`,`customer_id`,`premium_amt`,`transaction_date`,`transaction_time`,`status`,`policy_no`,`unique_id`) VALUES ('$manager_id','$agent_id','$customer_id','$premium_amt','$t_date','$t_time','Pending','$policy_no','$unique_id')");

		$query = mysqli_query($con,"UPDATE `tbl_policy_master` SET status = 'Renewal Pending' where policy_no = '$previous_policy'");
		
		$response['success']='1';
		$response['details']="Insert Successful";		
	}
	else
	{
		$response['success']='0';
		$response['details']="Customer Policy already exists";
	}
}
else
{
	$response['success']='0';
	$response['details']="Required field missing";
}	
echo json_encode($response);
?>