<?php
session_start();
require_once 'includes/database.php';
include 'includes/funcs.php';
$db=new MySQLDatabase();
$db->open_connection();
$user = $_SESSION['reusername'];

if(isset($_REQUEST['cname'])){	
$cname = strtoupper($_REQUEST['cname']);
$insurer = strtoupper($_REQUEST['insurer']);
$instel = strtoupper($_REQUEST['instel']);	
$insaddr = strtoupper(mysqli_real_escape_string($db->connection, $_REQUEST['insaddr']));
$idno = $_REQUEST['idno'];
$pinno = $_REQUEST['pinno'];
$icat = strtoupper($_REQUEST['icat']);
$isco = strtoupper($_REQUEST['isco']);
$vehmod = strtoupper($_REQUEST['vehmod']);
$regno = strtoupper($_REQUEST['regno']);
$chasis = strtoupper(mysqli_real_escape_string($db->connection, $_REQUEST['chasis']));
$vcolor = strtoupper($_REQUEST['vcolor']);
$vbody = strtoupper($_REQUEST['vbody']);
$yom = strtoupper($_REQUEST['yom']);
$rating = strtoupper($_REQUEST['rating']);
$engno= strtoupper($_REQUEST['engno']);
$idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);
$expdate = substr($_REQUEST['expdate'],6,4)."-".substr($_REQUEST['expdate'],3,2)."-".substr($_REQUEST['expdate'] ,0,2);
$inam = $_REQUEST['inam'];
$supp = $_REQUEST['supp'];	
$plllab = $_REQUEST['plllab'];
$pllam= $_REQUEST['pllam'];
$cpvt= $_REQUEST['cpvt'];
$cexp= $_REQUEST['cexp'];
$ppvt= $_REQUEST['ppvt'];
$pexp= $_REQUEST['pexp'];
$cpvtr= $_REQUEST['cpvtr'];
$cexpr= $_REQUEST['cexpr'];
$ppvtr= $_REQUEST['ppvtr'];
$pexpr= $_REQUEST['pexpr'];
$ctotprem = $_REQUEST['ctotprem'];
$ptotprem = $_REQUEST['ptotprem'];

if($pllam ==='') { $pllam = 0; };	
$agpno = mysqli_real_escape_string($db->connection, $_REQUEST['agpno']);	$agpno1 = mysqli_real_escape_string($db->connection, $_REQUEST['agpno1']);
$agpno2 = mysqli_real_escape_string($db->connection, $_REQUEST['agpno2']);	$agpno3 = mysqli_real_escape_string($db->connection, $_REQUEST['agpno3']);
$pno = mysqli_real_escape_string($db->connection, $_REQUEST['pno']);		
$insprem = $_REQUEST['insprem'];	
$insrate = $_REQUEST['insrate'];
$proprem = $_REQUEST['proprem'];	
$prorate = $_REQUEST['prorate'];	$inst1= intval($_REQUEST['inst1']);		$inst2= intval($_REQUEST['inst2']);		$inst3= intval($_REQUEST['inst3']);
$rmks = trim($_REQUEST['rmks']);
$proval = $_REQUEST['proval'];		$clevy = round((floatval($insprem)+floatval($cpvt)+floatval($cexp)+floatval($pllam))*0.45/100);	$plevy = round((floatval($proprem)+floatval($ppvt)+floatval($pexp)+floatval($pllam))*0.45/100);
$csduty = 40;	$psduty = 40; 	$comm = round((floatval($proprem)+floatval($ppvt)+floatval($pexp)+floatval($pllam))*0.1);
$amount = (intval($insprem) + intval($cpvt) + intval($cexp)) + $clevy + $csduty + intval($pllam);
$proam  = (intval($proprem)+ intval($ppvt) + intval($pexp)) + $plevy + $psduty + intval($pllam) - $comm;
																		// add ins															
if($_REQUEST['e'] === '0') {
$insno = getnewno("insno");
$query = "INSERT INTO insrecs (cname, idno, pinno, insno, idate, expdate, supp, vehmod, vcolor, vbody, yom, rating, engno, isco, regno, chasis, rmks, inam, insurer, insprem, proam, amount, instel, insaddr, insrate, icat, pno, agpno, agpno1, agpno2, agpno3, proval, prorate, proprem, clevy, csduty, plevy, psduty, inst1, inst2, inst3, plllab, pllam, cpvt, cpvtr, ppvt, ppvtr, cexp, cexpr, pexp, pexpr, ctotprem, ptotprem, us, ts) VALUES
 ('".$cname."', '".$idno."', '".$pinno."', ".$insno.", '".$idate."', '".$expdate."', ".$supp.", '".$vehmod."', '".$vcolor."', '".$vbody."', '".$yom."', '".$rating."', '".$engno."', '".$isco."','".$regno."', '".$chasis."', '".$rmks."', ".$inam.", '".$insurer."', ".$insprem.", ".$proam.", ".$amount.", '".$instel."', '".$insaddr."', ".$insrate.", '".$icat."', '".$pno."', '".$agpno."', '".$agpno1."', '".$agpno2."', '".$agpno3."', ".$proval.", ".$prorate.", ".$proprem.", ".$clevy.", ".$csduty.", ".$plevy.", ".$psduty.", ".$inst1.", ".$inst2.", ".$inst3.", '".$plllab."', ".$pllam.", ".$cpvt.", ".$cpvtr.", ".$ppvt.", ".$ppvtr.", ".$cexp.", ".$cexpr.", ".$pexp.", ".$pexpr.", ".$ctotprem.", ".$ptotprem.", '".$user."', '".date("Y-m-d H:i:s")."');";
}
else {
$insno = $_REQUEST['insno'];
$query = "UPDATE insrecs SET cname = '$cname', idno='$idno', pinno = '$pinno', idate = '$idate', isco = '$isco', supp = ".$supp.", vehmod = '$vehmod', vcolor = '$vcolor', vbody = '$vbody', yom= '$yom', rating='$rating', engno='$engno', regno = '$regno', chasis = '$chasis', insurer = '$insurer', instel = '$instel', insaddr ='$insaddr', insprem = '$insprem', icat = '$icat', insrate = '$insrate', inam = '$inam', rmks = '$rmks', pno = '$pno', agpno = '$agpno', agpno1 = '$agpno1', agpno2 = '$agpno2', agpno3 = '$agpno3', inst1='$inst1', inst2='$inst2', inst3='$inst3', proval = '$proval', proprem = '$proprem', prorate = '$prorate', expdate = '$expdate', proam='$proam', clevy='$clevy', csduty='$csduty', plevy='$plevy', psduty='$psduty', amount='$amount', plllab = '$plllab', pllam = '$pllam',  cpvt='$cpvt', cpvtr='$cpvtr', ppvt='$ppvt', ppvtr='$ppvtr', cexp='$cexp', cexpr='$cexpr', pexp='$pexp', pexpr='$pexpr', ctotprem='$ctotprem', ptotprem='$ptotprem', comm='$comm'  WHERE insno = '$insno'";
 	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
 $query = "UPDATE insinvoices SET cname = '$cname',  supp = ".$supp." WHERE insno = '$insno'";	
}
//echo $query;
$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
$db->close_connection();
echo ($_REQUEST['e'] === '0') ? ('Insurance Booked with No. '.$insno.' !') :('OK');  
}


?>