<?
 session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if($submit)
{
if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}
if(!checkdate($mm,$dd,$yy))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}

$ch=mysqli_query($db,"select * from veh_purchase where brcode='$brcode' and (invno='$invno' or da='$da')");

 if(mysqli_num_rows($ch)>0)
 {
  print "<script>alert('Duplicate Entry'); history.back(1);</script>";
  return; 
 }
$rs=mysqli_query($db,"select max(pur_no) from veh_purchase where brcode='$brcode'")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;

$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(strlen($dea)>0)
 $ledger=$dea;
else
 $ledger=$cust;

$invdate=$yy.'-'.$mm.'-'.$dd;
$dt=$invdate;
$purdate=$py.'-'.$pm.'-'.$pd;
$net=($pval+($pval*12.5/100));
$tx=($pval*12.5/100);
$sql="insert into veh_purchase values('$ledger','$pinvno','$purdate','$da','$mcode','$config',
 '$net','$tx','$pval','$disc','$color','$engno','$chasis','$remark','$brcode', '$invdate','$pno',
'sale_ret','-','$loc','$taxp','$emcode','$jvno','$config','old', '0','0','VAT')";
 mysqli_query($db,$sql) or die(mysqli_error());
 
$rr=mysqli_query($db,"select max(ret_num) from veh_salereturn where brcode='$brcode'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  else
   $retno++;
$sql="insert into veh_salereturn values('$retno','$dt','$da','$invno','$mcode','$config', '$netamt','$ledger','$brcode','$tax','$taxp','$amt2','$jvno')";
 mysqli_query($db,$sql) or die(mysqli_error());


 $code=substr($engno,0,3).substr(time(),7,3); 
 $code=str_replace(' ','-',$code);

$entrydate=date("Y-m-d");

$sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da) values('$entrydate','$pno','1','sale_ret','$brcode','$ledger','$da')";
mysqli_query($db,$sql) or die('Err Movement:-'.mysqli_error());


if($taxp=='12.5')
{
$sql="insert into journal2 values('$dt','$jvno','Dr','Output tax 12.5 (Sale Ret)','$tax', '0','Old V Sales Ret INV No: $retno','$brcode','Sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$dt','$jvno','Dr','Sales ret 12.5(vehicle)','$amt2','0','Old V Sales Ret INV No: $retno','$brcode','Sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

$sql="insert into journal2 values('$dt','$jvno','Cr','$ledger','0','$netamt', 'Old  V Sales Ret INV No: $retno','$brcode','sale ret','DA','$da','$retno')";
//print $sql;
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
}
else
{
 $sql="insert into journal2 values('$dt','$jvno','Dr','Sales ret 12.5(vehicle)' ,'$amt2','0','Old  V Sales Ret INV No: $retno','$brcode','sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

$sql="insert into journal2 values('$dt','$jvno','Cr','$ledger','0','$netamt', 'V Sales Ret INV No: $retno','$brcode','sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
}
}

?>
<script>

function valid()
{
 submitOK="true";
 if(document.f1.ledger.selectedIndex<=0 && document.f1.dea.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Customer / Dealer'); return false;
 }

 if(document.f1.ledger.selectedIndex>0 && document.f1.dea.selectedIndex>0)
 {
   submitOK="false";
   alert('Select Customer / Dealer'); return false;
 }


 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.da.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid DA');
 }
 if(document.f1.engno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Engine No.');
 }
 if(document.f1.loc.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Location');
 }

 if(document.f1.chasis.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Chasis');
 }

 if(document.f1.netamt.value.length<=0 || isNaN(document.f1.netamt.value))
 {
   submitOK="false";
   alert('Invalid Net Amount');
 }

 if(isNaN(document.f1.amt2.value) || isNaN(document.f1.tax.value))
 {
   submitOK="false";
   alert('Invalid Amount');
}
if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}

  if(submitOK=="true")
   return true;
  else
   return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<CENTER>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">

<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr  bgColor='c1d7bb'><td align=center>
<table border=1 cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='green'>
<font color='ivory'><STRONG>VEHICLE SALES RETURN (OLD STOCK) (No:

<?
$rs=mysqli_query($db,"select max(ret_num) from veh_salereturn where brcode='$brcode'")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;
print $pno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr>
<td colspan=1 align=left>Return Date  </td><td colspan=2>
	dd <input type=text name='dd' size=2 value='<?print date("d",time())?>'>
	mm <input type=text name='mm' size=2 value='<?print date("m",time())?>'>
	yy <input type=text name='yy' size=4 value='<?print date("Y",time())?>'>
</td>
</tr>

<tr><td>From Customer</td><td colspan=5><select name='ledger'><option></option>
<?
$res=mysqli_query($db,"select led from ledger where grp='Sundry Debtors' and brcode='$brcode' order by led");
while($row=mysqli_fetch_row($res))
{
 print "<option>$row[0]</option>";
}
?>
</select></td></tr>
<tr><td>Dealer </td><td colspan=5>
<select name='dea'>
<option></option>
<?
$res=mysqli_query($db,"select supname from sup_master where branch='$brcode' order by supname");
while($row=mysqli_fetch_row($res))
{
 print "<option>$row[0]</option>";
}
?>
</select>
</td></tr>
<tr>
<td><font color=red>Sale Invoice No </td><td><input type=text name='invno' ></td>
<td>Invoice Date <br><br> Location</td><td colspan=3>dd <input type=text name='d' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y' size=4 value='<?print date("Y",time())?>'>
 <br><br> <select name='loc'>
<option></option>
<?
$re=mysqli_query($db,"select code,lname from loc_master where brcode='$brcode' order by lname");
while($lorow=mysqli_fetch_row($re))
{
 print "<option value='$lorow[0]'>$lorow[1]</option>";
}
?>
</select>

</td></tr>
<tr><td><font color='red'>DA </td><td>
<input type=text name='da'>
</td>
<td>Model Code </td><td><input type=text  name='mcode'></td>
<td>config </td><td><input type=text name='config'></td></tr>

<tr><td><font color='red'>Net Amount</td><td><input type=text name='netamt' onChange=calc()></td>
    <td>Tax %</td><td><select name='taxp' onChange=calc()><option>12.5</option><option>0</option> </select>
 <input type=text name='tax' size=10 readonly value='12.5'></td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly name='amt2' ></td>
</tr>
<tr><td>Discount </td><td><input type=text name='disc'></td><td>Color</td>
<td><input type=text name='color'></td><td><font color='red'>Engine No</td><td><input type=text name='engno' onKeyDown=chkey(this)></td>
</tr>
<tr><td><font color='red'>Chasis No</td><td><input type=text name='chasis' onKeyDown=chkey(this)></td>
<td>Eicher<br>Model Code </td><td><input type=text name='emcode'></td>
<td>Remarks </td><td><textarea name='remark' cols=25></textarea></td></tr>
<tr><td colspan=2>Purchase Date</td><td colspan=3>dd<input type=text name='pd' size=2>
			      dd<input type=text name='pm' size=2>
				yy<input type=text name='py' size=4>
		</td>
</tr>
<tr><td colspan=2>Purchase Amount <br>(Before Tax)</td><td colspan=3><input type=text name='pval'></td></tr>
<tr><td colspan=2>Purchase invno</td><td colspan=3><input type=text name='pinvno'></td></tr>

<tr><td colspan=6 align=right bgcolor='green'> <input type=submit name='submit' value=' Save '>
<input type=button value='  Cancel  ' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
 var pe=document.f1.taxp.selectedIndex;
 if(pe==0)
  taxper=12.5;
 else
   taxper=0;
 net=parseFloat(document.f1.netamt.value);
 amt=(net*100)/(100+parseFloat(taxper))
 document.f1.amt2.value=parseFloat(amt.toFixed(2));

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=(net-amt).toFixed(2);
}
</script>
<style>
