<?
 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");


//newly added

$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];

$dt=$yy.'-'.$mm.'-'.$dd;
//print $user_level;
//print $dt;

if($user_level<=2)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>3 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 3 days only'); history.back(1); </script>";
 return;
}
}
//


if($submit)
{
$entrydate=$yy.'-'.$mm.'-'.$dd;
$invdate=$y.'-'.$m.'-'.$d;

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- Invoice No or DA'); 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);

$sql="insert into veh_purchase values('$ledger','$invno','$invdate','$da','$mcode','$config',
 '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode',
 '$entrydate','$pno','eich','-','$loc','$taxp','$emcode','$jvno','$config','-', '$cessp','$cessamt','$taxtype')";
 mysqli_query($db,$sql) or die(mysqli_error());

log_entry($brcode,$user,$REMOTE_ADDR,"Veh purchase entry-$da");



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

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

//***
if($taxtype=='VAT')
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase $taxp(vehicle)','$amt2', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

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

  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
}
else
{

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase $taxp(vehicle) CST','$amt2', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

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

  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
}

if($roundoff>0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','$roundoff', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
}
else
{
$roff=abs($roundoff);

$sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','$roff', 'P.Bill No: $invno','$brcode','V pur','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());

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

?>
<script>
 document.location="../portaldetails.php";
</script>

<?
return;

}
?>

<script>
var ar1=[];
var config=[];
function fun1()
{
 var x=document.f1.config.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
   }
 else
  {
    document.f1.mcode.value=ar1[document.f1.config.selectedIndex];
     }
}

function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.da.value.length<=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( document.f1.tax.value.length<=1)
 {
   submitOK="false";
   alert('Invalid tax Amount');
}

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

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

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


var day1=document.f1.dd.value;
var mon1=document.f1.mm.value;
var year1=document.f1.yy.value;
var chdt=day1+"/"+mon1+"/"+year1;

var tempdt=year1+"-"+mon1+"-"+day1;
var tempact=document.f1.act.value;
var tempacf=document.f1.acf.value;


function ValidateDate() 
{
 
    var msg = '';
 
    this.isDate = function(date) 
     {
 
        var explodeData = date.explode("/");
        var day = explodeData[0];
        var month = explodeData[1];
        var year = explodeData[2];
 
        var thisMonth = new Date(year, month, 0);
        var daysInMonth = thisMonth.getDate();
        var pattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
 
        if (!pattern.test(date)) {
            msg = "invalid date format";
        }
        else if (day > daysInMonth) {
            msg = "invalid day";
        }
        else if (month > 12) {
            msg = "invalid month";
        }
 
        if (msg) {
            return false;
        } else {
            return true;
        }
    }
 
    this.getMsg = function() 
    {
 
        return msg;
    }
}

 
 vDate = new ValidateDate();
 
if (vDate.isDate(chdt))

 {

if(tempdt>=tempacf&&tempdt<=tempact)
{

//alert('hii');

}
else
{
submitOK="false";

document.f1.mm.focus();

alert('date should be in between current Financial Year');

}

 //alert("Date is valid");
 }
else 

{
 submitOK="false";

document.f1.dd.focus();
alert("Date is not valid: " + vDate.getMsg()); 
}




 


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>
 td{color:black;font-family:verdana;font-size:11; font-weight: bold;}
 th{color:black;font-family:verdana;font-size:11}
</style>



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

<table cellspacing=1 cellpadding=1 bgcolor='#17233c'>
<tr  bgColor='#ffffff'><td align=center>
<table border=0 cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='#17233c'>
<font color='#ffffff'><STRONG>VEHICLE PURCHASE EICHER(Pur No:

<?
$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;
print $pno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr><td colspan=1>DATE  </td><td >
	 <input type=text name='dd' size=2 value='<?print date("d",time())?>'>
	 <input type=text name='mm' size=2 value='<?print date("m",time())?>'>
	 <input type=text name='yy' size=4 value='<?print date("Y",time())?>'>
</td>
<td><font color='#8f012c'>INVOICE NO </td><td><input type=text readonly name='invno'  value=<?print $invno ?>></td>
<?
$da=explode("-",$dat);
$day=$da[2];
$mon=$da[1];
$yea=$da[0];


?>
<td>INVOICE DATE </td><td colspan=3> <input type=text readonly name='d' size=2 value='<?print $day?>'>
 <input type=text readonly name='m' size=2 value='<?print $mon ?>'>
 <input type=text readonly name='y' size=4 value='<?print $yea ?>'>

</td>

</tr>
<tr>





<td>FROM </td>

<td ><select name='ledger'>
<?
$res=mysqli_query($db,"select led from ledger where grp='Sundry Creditors' and (led like 'Eich%' or led like '%VE COMMERCIAL VEHICLES%') and brcode='$brcode' order by led");
while($row=mysqli_fetch_row($res))
{
 print "<option>$row[0]</option>";
}
?>
</select></td>

</tr>
<tr>
<td><font color='#8f012c'>CONFIG</td><td>
<select name='config' onChange=fun1()>
<option></option>
<?
$result=mysqli_query($db,"select config,mcode from veh_master where brcode='$brcode' order by config")or die(mysqli_error());

$i=1;
while($row=mysqli_fetch_array($result))
{

 print "\n<option>$row[config]</option>";
 print "\n<script> ar1[$i]='$row[mcode]'</script>";
 $i++;
}
?>
</select>
</td>
<?
 $r=mysqli_query($db,"select fertcode,configuration from  eichersap where invno='$invno' and brcode='$brcode'")or die(mysqli_error());
$rr=mysqli_fetch_array($r);

$con=trim($rr[1]);
$con=ltrim(html_entity_decode($con));


$cd=str_replace('~','&nbsp;',$partcode[0]);


?>
<td>MODEL CODE </td><td><input type=text readonly name='mcode' value=<?print $partname[0]?> ></td><td>EICHER<br>MODEL CODE </td><td><input type=text readonly name='emcode' value=<?print $rr[0]?>  ></td>


</tr>
<tr>
<td><font color='#8f012c'>COLOUR</td><td><input type=text name='color'></td><td><font color='#8f012c'>ENGINE NO</td><td><input type=text readonly name='engno'  onKeyDown=chkey(this) value=<?print $eng?> ></td>
<td><font color='#8f012c'>CHASIS NO</td><td><input type=text readonly name='chasis'   onKeyDown=chkey(this) value=<?print $cha?> ></td>
</tr>
<tr>
<td>
 <font color='#8f012c'>LOCATION</font></td><td> <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>
<td><font color='#8f012c'>DA </td><td>
<select readonly name='da' onChange=fun1()>
<option><?print $dan?></option>

</select>
</td>
<td>REMARKS </td><td><textarea readonly name='remark' cols=25><?print $con ?></textarea></td>
</tr>

<?
$bamt=($net*100)/(100+14.5);
$bamt=round($bamt,3);

?>

<tr><td><font color='#8f012c'>AMOUNT <br>BEFORE TAX </td><td><input type=text  name='amt2' value='<?print $bamt?>'onchange=calc() onkeypress="return handleEnter(this, event)" >
</select>
Tax Type <select name='taxtype' > <option>VAT</option><option>CST</option></select>
</td>
    <td>TAX %</td><td><select name='taxp' onChange=calc() onkeypress="return handleEnter(this, event)"><option value='14.50'>14.50</option><option value='14.50'>14.50</option><option value='2'>2</option><option value='1'>1</option><option value='0'>0</option> </select>

 <input type=text name='tax' size=10 readonly value='0' onkeypress="return handleEnter(this, event)">

</td>
<td>ROUND OFF</td><td><input type=text name='roundoff' onkeypress="return handleEnter(this, event)"></td>
</tr>
<tr>
<td>CESS%</td><td>
<?
if($brcode=='AUT009' || $brcode=='AUT103')
print "<input type=text name='cessp' readonly size=2 value='0' onChange=calc()>";
else
print "<input type=text name='cessp' readonly size=2 value='0' onChange=calc()>";

?>
</td>
<td>CESS AMT</td><td><input type=text name='cessamt' readonly size=6 onkeypress="return handleEnter(this, event)"></td>
  
<td>NET AMOUNT</td><td><input type=text readonly name='netamt' onChange=calc() value=<?print $net ?> onkeypress="return handleEnter(this, event)"></td>

</tr>
<tr><td>DISCOUNT </td><td><input type=text name='disc' value='0' onkeypress="return handleEnter(this, event)" ></td>

</tr>
<tr>


</tr>

<tr><td colspan=6 align=right bgcolor='#17233c'> <input type=submit name='submit' value=' Save '>
<input type=button value='  Cancel  ' onClick=document.location='../blank.html'></td></tr>

<?
//
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error());
$acrow=mysqli_fetch_row($acpd);


?>
<tr><td><input type=hidden name='acf' value='<?print $acrow[0]?>'></td>
<td><input type=hidden name='act' value='<?print $acrow[1]?>'></td>
</tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
 /*var pe=document.f1.taxp.value; 
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 if(pe==0)
  taxper=12.5;
 else
   taxper=0;
 net=parseFloat(document.f1.netamt.value);
 x=(100+parseFloat(cessper)); 
 zz=(100+parseFloat(pe)); 
  if(cessper>0)
  {
    yy=pe*cessper/100;
    zz=zz+yy;
  }

 amt=(net*100)/zz;
 tax=(amt*parseFloat(document.f1.taxp.value))/100;
 document.f1.amt2.value=parseFloat(amt.toFixed(2));

// tax=(parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value))/100;

 document.f1.tax.value=tax.toFixed(2);

 var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);
 var gtot=parseFloat(document.f1.cessamt.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.amt2.value);
  var diff=net-gtot;
 document.f1.amt2.value=(parseFloat(document.f1.amt2.value)+parseFloat(diff)).toFixed(2);
*/

if(document.f1.taxp.value==12.5)
document.f1.cessp.value=1;
else
{
document.f1.cessp.value=0;
}

var b_amt=(parseFloat(document.f1.netamt.value)*100)/(parseFloat(document.f1.taxp.value)+100);
document.f1.amt2.value=b_amt.toFixed(2);

var tax_amt=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
document.f1.tax.value=tax_amt.toFixed(2);

var ces_amt=parseFloat(document.f1.tax.value)*parseFloat(document.f1.cessp.value)/100;
document.f1.cessamt.value=ces_amt.toFixed(2);


//var net_amt=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value);
//document.f1.netamt.value=net_amt.toFixed(0);

var gt=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value);
 var round=parseFloat(document.f1.netamt.value)-gt;
//alert(round.toFixed(2));

document.f1.roundoff.value=round.toFixed(2);


}
</script>

<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      

</script>


<style>
th{background-color:brown}
</style>
<form action="<?print $PHP_SELF?>">
Search DA <input type=text name='sea'> <input type=submit value='Search' name='search'>
</form>
<?
if($search)
 $sql="select * from veh_purchase where  brcode='$brcode' and da='$sea'";
else
 { $sql="select * from veh_purchase where  brcode='$brcode' and sale_invoice='-' and type='eich' order by pur_no desc limit 50";
    print "<font color=blue>Showing last 50 purchases</font>";
 }

$result=mysqli_query($db,$sql);
 if(mysqli_num_rows($result)<=0)
 {
   print "<font color='#983b58'>Record not found</font>";
   return;
 }
print "<table bgcolor='white'> <th>Pur No <th>P Inv No. <th>P.Inv Date <th>DA <th>M.Code <th>Config";
print "<th>Engine <th>Chasis <th>Action";
while($row=mysqli_fetch_array($result))
{
 if($row[type]=='trans')
  print "<tr><td>$row[pur_no](TR)</td>";
 else
  print "<tr><td>$row[pur_no]</td>";
 print "<td><a href='vmpur.php?code=$row[pur_no]&da=$row[da]'>$row[invno]</a></td><td>".datetohtml($row[invdate])."</td><td>$row[da]</td>";
 print "<td>$row[mcode]</td><td>$row[config]</td><td>$row[engno]</td><td>$row[chasis]</td>";
if($row[sale_invoice]=='-' && $level>=3)
  print "<td align=center valign=center><input type=button value='Edit' onClick=javascript:edit('$row[da]','$row[pur_no]');> <a href=javascript:del('$row[da]','$row[pur_no]','$row[jvno]') title='delete'><img src='../img/delete2.gif' border=0></a></td></tr>";
else 
 print "<td align=center valign=center><input type=button value='Edit' onClick=javascript:edit('$row[da]','$row[pur_no]');></tr>";
}
?>
<script>
function del(da,pno,jvno)
{
if(confirm("Do you want to delete Da "+da+" ?"))
 document.location="delpveh.php?da="+da+"&pno="+pno+"&jvno="+jvno;

}
function edit(da,pno)
{
 document.location="editpveh.php?da="+da+"&pno="+pno;
}

</script>


