<?
session_start();
include("../opendb.php");
include("../test.php");
if($save)
{
$pinvdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;

$sql="insert into veh_purchase (tono,pinvno,pinvdate, config,amt,taxp,taxamt, amt2 ,msno ,engno ,entrydate, brcode,model,sup,transtype) values('$tono','$pinvno','$pinvdate', '$config', '$amt', '$taxp', '$taxamt', '$amt2' ,'$msno' ,'$engno','$entrydate', '$brcode', '$model', '$sup', 'purchase')";
mysqli_query($db,$sql) or die('Insert Err:'.mysqli_error());

$sql="update purorder set pinvoice='$pinvno' where tono='$tono'";
mysqli_query($db,$sql) or die('Upd error:'.mysqli_error());

$jvr=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
 $j_row=mysqli_fetch_row($jvr);
  if($j_row[0]<=0)
   $jvno=1;
  else
  $jvno=($j_row[0]+1);


if($taxp=='12.5')
{ 
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase 12.5(vehicle)','$amt', '0','P.Bill No: $pinvno','$brcode','V pur','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Input tax 12.5','$taxamt', '0','P.Bill No: $pinvno','$brcode','V pur','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$entrydate','$jvno','Cr','$sup','0','$amt2','P.Bill No: $pinvno','$brcode','V pur','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
}

print "<script> document.location='veh_purch1.php' </script>";
return;
}
$re=mysqli_query($db,"select * from purorder where tono='$tono'");
$row=mysqli_fetch_array($re);
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
tr{background-color:white}
th{background-color:steelblue;color:white}
</style>
<script>
 function calctax()
  {
     document.f1.taxamt.value=(parseFloat(document.f1.amt.value)*parseFloat(document.f1.taxp.value)/100).toFixed(2);
   document.f1.amt2.value=parseFloat(document.f1.amt.value)+parseFloat(document.f1.taxamt.value);
   document.f1.msno.focus();
  }
function valid()
{

 if(document.f1.tono.value.length<=0)
 {
   alert("Invalid Telcon Order No");
   document.f1.tono.focus();
   return false;
 }
 if(document.f1.pinvno.value.length<=0)
 {
   alert("Invalid Purchase invoice no");
   document.f1.pinvno.focus();
   return false;
 }
 if(document.f1.d.value.length<=0)
 {
   alert("Invalid Purchase invoice date");
   document.f1.d.focus();
   return false;
 }
 if(document.f1.d1.value.length<=0)
 {
   alert("Invalid Entry date");
   document.f1.d1.focus();
   return false;
 }

 if(document.f1.amt.value.length<=0)
 {
   alert("Invalid Amount");
   document.f1.amt.focus();
   return false;
 }
 if(isNaN(document.f1.amt.value))
 {
   alert("Invalid Amount");
   document.f1.pinvno.focus();
   return false;
 }
 if(document.f1.msno.value.length<=0)
 {
   alert("Invalid Machine Serial NO.");
   document.f1.msno.focus();
   return false;
 }
 if(document.f1.engno.value.length<=0)
 {
   alert("Invalid Engine NO.");
   document.f1.engno.focus();
   return false;
 }

}
</script>

<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>
<form name='f1' action='veh_purch2.php' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3>
<th colspan=6><font size=3>VEHICLE PURCHASE</th>
<tr><td> Supplier </td><td colspan=2><select name='sup'>
<?
$sure=mysqli_query($db,"select * from sup_master");
while($surow=mysqli_fetch_array($sure))
{
 print "<option>$surow[supname]</option>";
}
?>
</option>
</td>
<td>Location</td><td colspan=2><select name='location'>
<option></option>
<?
 $locres=mysqli_query($db,"select lname from loc_master where brcode='$brcode'")or die(mysqli_error());
  while($lorow=mysqli_fetch_array($locres))
  {
   print "<option>$lorow[0]</option>";
  }
?>
</tr>
<tr><td>Tel Ord No </td><td><input type=text name='tono' readonly size=4 value='<?print $tono?>'></td>
    <td>P.Inv No.</td><td><input type=text name='pinvno' ></td>
    <td>Inv Date</td><td>dd<input type=text name='d' size=2>
mm<input type=text name='m' size=2 value='<?print date('m')?>'>
yy<input type=text name='y' size=4 value='<?print date('Y')?>'>
</td></tr>
<tr>
<td>Entry Date</td><td>dd<input type=text name='d1' size=2 value='<?print date('d')?>'>
mm<input type=text name='m1' size=2 value='<?print date('m')?>'>
yy<input type=text name='y1' size=4 value='<?print date('Y')?>'>
</td>
<td>Model</td><td>
<select name='model'>
<option><?print $row[model]?></option>
<?
$re=mysqli_query($db,"select * from configmaster order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[1]</option>";
}
?>
</select> </td></tr>
<tr><td>Config</td><td colspan=4><select name='config'>
<option><?print $row[config]?></option>
<?
$re=mysqli_query($db,"select * from configmaster order by model");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[2]</option>";
}
?>

</select>
</td></tr>
<tr><td>Amount </td><td><input type=text name='amt' onChange='javascript:calctax()'></td>
	<td>Tax %</td><td><input type=text name='taxp' value='12.5'></td>
        <td>Tax Amt </td><td  align=right><input type=text name='taxamt'></td></td>
</tr>
<tr><td>Amt Payable </td><td><input type=text name='amt2' readonly>
    <td>M Ser No </td><td><input type=text name='msno'></td>
	<td>Engine No </td><td align=right><input type=text name='engno'></td>
</tr>
<th colspan=6><input type=submit value='Save' name='save'> <input type=button value='Cancel' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
</td></tr>
</table>


