<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$level=$_SESSION['level'];
$code=$_REQUEST['code'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");
$place=$_REQUEST['place'];
$conv=$_REQUEST['conv'];
$rpkm=$_REQUEST['rpkm'];
$starkm=$_REQUEST['starkm'];
$endkm=$_REQUEST['endkm'];
$totalkm=$_REQUEST['totalkm'];
$totamt=$_REQUEST['totamt'];
$da=$_REQUEST['da'];
$hotel=$_REQUEST['hotel'];
$ocharge=$_REQUEST['ocharge'];
$remark=$_REQUEST['remark'];
$gtot=$_REQUEST['gtot'];

if(isset($_POST['sv']))
{
$trdate=$y.'-'.$m.'-'.$d;
$code=substr($place,0,3).date('is');
$sql="create table dabill(code varchar(20),trdate date,place varchar(200),conv varchar(100), rpkm double(12,2),starkm double(12,2),endkm double(12,2) ,totalkm double(12,2),totamt double(12,2), da double(12,2),hotel double(12,2) ,ocharge double(12,2), remark varchar(250),gtot double(12,2),brcode varchar(100),user varchar(100))";
mysqli_query($db,$sql) ;
$sql="insert into dabill values('$code','$trdate','$place','$conv', '$rpkm','$starkm','$endkm' ,'$totalkm','$totamt', '$da','$hotel' ,'$ocharge', '$remark','$gtot','$brcode', '$user')";

mysqli_query($db,$sql)or die(mysqli_error($db));
print "Record has been saved";
}

if(isset($_POST['del']))
{
 mysqli_query($db,"delete from dabill where code='$code' and user='$user'");
}
?>
<?php include('top.php'); ?>
<style>
tr{font-family:arial;font-size:13;background-color:white}
TH{COLOR:WHITE;BACKGROUND-COLOR:STEELBLUE}
input,textarea{border:solid;border-width:1px;border-color:black}
</style>
<script>
function validkm()
{
 var diff=parseFloat(document.f1.endkm.value)-parseFloat(document.f1.startkm.value)
 document.f1.totalkm.value=diff;
if(diff>0)
{
var totamt=parseFloat(diff)*parseFloat(document.f1.rpkm.value);
 document.f1.totamt.value=totamt;
}
totamt=parseFloat(document.f1.totamt.value)
var grtot=totamt+parseFloat(document.f1.da.value)+parseFloat(document.f1.hotel.value)+parseFloat(document.f1.ocharge.value);
document.f1.gtot.value=grtot;
}
function valid()
{
 if(document.f1.place.value.length<=0)
 {
   alert('Invalid Place');
	document.f1.place.focus();
   return false
 }

 if(parseFloat(document.f1.totalkm.value)<0)
 {
   alert('Invalid Starting/Ending Kilometer');
	document.f1.endkm.focus();
   return false
 }
 if(parseFloat(document.f1.gtot.value)<=0)
 {
   alert('Invalid entry');
   return false
 }
if(confirm('Do you want to save?'))
 return true;
else
 return false;
}
</script>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<center>
<form method='post' name='f1' action='<?print $PHP_SELF?>' onSubmit='return valid()'>
<table cellspacing=1 cellpadding=3 bgcolor='gray'>
<th colspan=2>T.A Bill</th>
<tr><td  bgcolor='silver'>Date of Travel</td><td> 
	dd <input type=text name='d' size=2 maxlength=2 value='<?print date('d')?>'>
	mm <input type=text name='m' size=2 maxlength=2 value='<?print date('m')?>'>
	yy <input type=text name='y' size=4 maxlength=4 value='<?print date('Y')?>'>
     </td>
</tr>
<tr><td bgcolor='silver'>Place Visited</td><td><input type=text name='place' size=40></td></tr>
<tr><td bgcolor='silver'>Mode of conveyance</td>
	<td><select name='conv'>
	<option>Car</option>
	<option>Bus</option>
	<option>Bike</option>
	<option>Train</option></select></td>
</tr>

<tr><td bgcolor='silver'>Rate per KM</td>
<td> 
	<input type=text name='rpkm' value='0' size=5 onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>Starting KM</td>
<td> 
	<input type=text name='startkm' size=5 value='0' onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>Ending KM</td>
<td> 
	<input type=text name='endkm' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>Total KM</td>
<td> 
	<input type=text name='totalkm' READONLY size=5>
</td></tr>
<tr><td bgcolor='silver'>Total Amount</td>
<td> 
	<input type=text name='totamt' size=5 onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>DA</td>
<td> 
	<input type=text name='da' size=5  value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>Hotel</td>
<td> 
	<input type=text name='hotel' size=5 value='0' onChange=validkm()>
</td></tr>

<tr><td bgcolor='silver'>Other Charges</td>
<td> 
	<input type=text name='ocharge' size=5 value='0' onChange=validkm()>
</td></tr>
<tr><td bgcolor='silver'>Remark</td>
<td> 
	<textarea name='remark' rows=3 cols=46></textarea>
</td></tr>
<tr><td bgcolor='silver'>Grand Total</td>
<td> 
	<input type=text name='gtot' size=5 value='0' READONLY>
</td></tr>
<th colspan=2><input type=submit value='Save' name='sv'></th>
</table>
</form>
<table cellspacing=1 cellpadding=3 bgcolor='gray'>
<th>Date <th>Place <th>Total Amount <th>DA <th>Hotel <th>Other <br>Charges <th>G Tot <th>Remark
<th>Del
<?
$re=mysqli_query($db,"select * from dabill where brcode='$brcode' and user='$user'");
while($row=mysqli_fetch_array($re))
{
print "\n<tr><form method='post'>\n<input type=hidden name='code' value='$row[code]'>\n<td>".datetohtml($row[trdate])."</td><td>$row[place]</td>";
print "<td align=right>$row[totamt]</td><td align=right>$row[da]</td>";
print "<td align=right>$row[hotel]</td><td align=right>$row[ocharge]</td>";
print "<td  align=right>$row[gtot]</td><td>$row[remark]</td>";
print "<td><input type=submit value='Del' name='del'></form></tr>";
}
?>




