
<? 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<=4)
{
$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;
}

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


$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(refno) from veh_purchase where brcode='$brcode' and taxtype = '$taxtype'")or die(mysqli_error());
$p_row1=mysqli_fetch_row($rs);
if(mysqli_num_rows($rs)<=0)
	$refno=1;
else
	$refno=$p_row1[0]+1;
$cgstp = $taxp/2;
$sgstp = $taxp/2;
$cgstamt = $tax/2;
$sgstamt = $tax/2;
$sql="insert into veh_purchase values('$ledger','$invno','$invdate','$da','$mcode',
'$config', '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode',
'$entrydate', '$pno','other','-','$loc','$taxp','$emcode', '$jvno','$config', '-','$cessp','$cessamt','$taxtype','$refno','$cgstp','$cgstamt','$sgstp','$sgstamt','$igstp','$igstamt','')";
 mysqli_query($db,$sql) or die('Purch Err:'.mysqli_error());

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

/*$sq="update purorder setpurno='$pno',purdate='$invdate' where dano='$da' and brcode='$brcode'";
mysqli_query($db,$sq) or die(mysqli_error());
*/

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

/*$sql="insert into vehopstock (code,mcode,chno,engno,dano,pinv,pdate,brcode) values('$code','$mcode','$chasis','$engno','$da','$invno','$invdate','$brcode')";
 mysqli_query($db,$sql) or die(mysqli_error());
*/
$sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type) values('$entrydate','$invno','1','pur','$brcode','eich')";
mysqli_query($db,$sql) or die(mysqli_error());

if($taxtype=='INTRASTATE')
{
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase GST $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 CGST $cgstp','$cgstamt', '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 SGST $cgstp','$sgstamt', '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());

 $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());

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());
}

/*
if($taxp=='12.5')
{ 
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase 12.5(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 12.5','$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());

 $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());
}
if($taxp=='14')
{ 
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase 14(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 14','$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','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());
}
*/
}
else if($taxtype=='CST')
{
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase $taxp% (vehicle)$taxtype','$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% $taxtype','$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','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());

}
/*
$jvd=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$difrow=mysqli_fetch_array($jvd);
$dif=$difrow[0];
$x=abs($dif);
if($dif<0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','$x', '0','Round ofSINV No: $invno','$brcode','V sale','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
else if($dif>0)
{
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','$x','Round of adjusted SINV No: $invno','$brcode','V sale','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}
*/
 print "<font color=green><b>Data has been saved</b></font>";
}
?>

<script>
function calc()
{

/*
var taxper=parseFloat(document.f1.taxp.value);
//document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 

 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 net=parseFloat(document.f1.netamt.value);
 if(cessper==0 && taxper==2)
	{ amt=(net*100)/102; }
 else if(cessper==1)
	 amt=(net*100)/112.625; 
 else if(cessper==0)
	 amt=(net*100)/114; 

 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 cessper=parseFloat(document.f1.cessp.value).toFixed(0);

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

*/
if(document.f1.taxp.value==14.50)
document.f1.cessp.value=0;
else
{
document.f1.cessp.value=0;
}

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


}


function calc11()
{
 var pe=document.f1.taxp.value; 
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 if(pe==0)
  taxper=28;
 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*pe)/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 ar1=[];
function fun1()
{
 var x=document.f1.config.selectedIndex;
 if(x<=0)
   document.f1.mcode.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.chasis.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Chasis');
 }
 if(document.f1.mcode.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Chasis');
 }
 if(document.f1.loc.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Location');
 }

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




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")
   return true;
  else
   return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}
function setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=2;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

 }
 else
 {
  document.f1.cessp.value=1;
    document.f1.taxp.value=12.5;
 }


 calc();
}

</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=0 cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='green'>
<font color='ivory'><STRONG>VEHICLE PURCHASE (OTHER THAN EICHER)(Pur No:

<?
$rs=mysqli_query($db,"select max(pur_no) from veh_purchase where ledger not like 'eich%' and brcode='$brcode' and taxtype='$taxtype'")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>From </td><td colspan=5><select name='ledger'>
<?
$res=mysqli_query($db,"select led from ledger where grp='Sundry Creditors' and led not like 'Eich%' and brcode='$brcode' order by led");
while($row=mysqli_fetch_row($res))
{
 print "<option>$row[0]</option>";
}
?>
</select>
Tax Type <select name='taxtype' onChange='setttype()'> <option>INTRASTATE</option><option>INTERSTATE</option></select>
</td></tr>
<tr>
<td colspan=1>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><font color=red>Invoice No </td><td colspan=1><input type=text name='invno' ></td>
<td>Invoice Date </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())?>'>
<tr><td>
 Location </td><td colspan=3><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' size=15>


</td>

<td>Config</td><td colspan=3>
<!-- <input type=text name='model' size=10>-->
<select name='config' onChange=fun1()><option></option>
    <?
     $i=1;
     $res=mysqli_query($db,"select * from veh_master where brcode='$brcode' order by config");
     while($row=mysqli_fetch_row($res))
     {
       print "\n<option>$row[2]</option>"; 
       print "\n<script>ar1[$i]='".strtoupper($row[1])."'</script>";
	$i++;
     }
    ?>
  </select>
</td></tr>
<tr><td></td><td></td><td colspan=3>
 M.Code <input type=text name='mcode' size=20></td>
</tr>




<tr>
<td><font color='red'>Amount <br>Before Tax </td><td><input type=text onchange=calc() name='amt2' ></td>


<?
if($brcode=='AUT009'||$brcode=='AUT103')
{
?>
    <td>Tax %</td><td><select name='taxp' onChange=calc()><option value='28.00'>28.00</option></select>
 <input type=text name='tax' size=10 readonly value='14'></td><td>Round Off</td><td><input type=text name='roundoff'></td></tr>
<?
}
else
{
?>
    <td>Tax %</td><td><select name='taxp' onChange=calc()><option value='28.00'>28.00</option></select>
 <input type=text name='tax' size=10 readonly value='0'></td><td>Round Off</td><td><input type=text name='roundoff'></td>
</tr>
<?
}
?>


<tr>
<td>Cess %</td><td>
<?
if($brcode=='AUT009' )
print "<input type=text name='cessp' readonly size=2 value='0'>";
else
print "<input type=text name='cessp' readonly size=2 value='1' onChange=calc()>";
//9747238199 rijil
?>
</td>
<td>Cess Amt</td><td><input type=text name='cessamt' readonly size=6 ></td>
    
<td>Net Amount</td><td><input type=text name='netamt' readonly onChange=calc()></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=6 align=right bgcolor='green'> <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>

<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 type='other' order by pur_no desc limit 50";
 }

$result=mysqli_query($db,$sql);
 if(mysqli_num_rows($result)<=0)
 {
   print "<font color=red>Record not found</font>";
   return;
 }

print "<font color=blue>Showing last 50 purchases</font>";
print "<table border=1> <th>Pur No <th>P Inv No. <th>P.Inv Date <th>DA <th>M.Code <th>Config";
print "<th>Engine <th>Chasis <th>Delete";
while($row=mysqli_fetch_array($result))
{
 print "\n<tr><td>$row[pur_no]</td><td><a href='vmpur.php?code=$row[pur_no]'>$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>";
 print "\n<td align=center><a href=javascript:del('$row[da]','$row[pur_no]','$row[jvno]')><img src='../img/delete2.gif' border=0></a></td></tr>";
}
?>
<script>
function del(da,pno,jvs)
{
if(confirm("Do you want to delete Da "+da+' '+jvs+" ?"))
 document.location="delpveh.php?da="+da+"&pno="+pno+"&jvno="+jvs;
}
</script>


