<?
session_start();
$brcode = $_SESSION['brcode'];
$user   = $_SESSION['user'];
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($db));
$r=mysqli_fetch_row($us);
$level = $_SESSION['level'];
$user_level=$r[0];


if($user_level<=4)
{
$sq="select to_days(curdate())-to_days('$dt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$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($_REQUEST['submit'])
{

$mm = $_REQUEST['mm'];
$dd = $_REQUEST['dd'];
$yy = $_REQUEST['yy'];
$m = $_REQUEST['m'];
$d = $_REQUEST['d'];
$y = $_REQUEST['y'];
$da = $_REQUEST['da'];
$dt=$yy.'-'.$mm.'-'.$dd;
//print $user_level;
//print $dt;
$invno = $_REQUEST['invno'];
$sapno=$_REQUEST['sapno'];
$taxtype = $_REQUEST['taxtype'];
$ledger = $_REQUEST['ledger'];
$invno = $_REQUEST['invno'];
$da = $_REQUEST['da'];
$mcode = $_REQUEST['mcode'];
$config = $_REQUEST['config'];
$netamt = $_REQUEST['netamt'];
$tax = $_REQUEST['tax'];
$amt2 = $_REQUEST['amt2'];
$disc= $_REQUEST['disc'];
$color= $_REQUEST['color'];
$engno= $_REQUEST['engno'];
$chasis= $_REQUEST['chasis'];
$remark= $_REQUEST['remark'];
$loc = $_REQUEST['loc'];
$taxp= $_REQUEST['taxp'];
$emcode= $_REQUEST['emcode'];
$config= $_REQUEST['config'];
$cessp= $_REQUEST['cessp'];
$cessamt= $_REQUEST['cessamt'];
$refno= $_REQUEST['refno'];
$mcgstp= $_REQUEST['mcgstp'];
$mcgstamt= $_REQUEST['mcgstamt'];
$msgstp= $_REQUEST['msgstp'];
$msgstamt= $_REQUEST['msgstamt'];
$migstp= $_REQUEST['migstp'];
$migstamt= $_REQUEST['migstamt'];
$hsncode= $_REQUEST['hsncode'];
$tcsp= $_REQUEST['tcsp'];
$tcsp = round($tcsp,3);
$tcsamt= $_REQUEST['tcsamt'];   
    
    if($refno<='')
    $refno=0;
$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; 
 }

$ch=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and (pinv='$invno' or dano='$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($db));
 $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);
  
$resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");
 $rowdn=mysqli_fetch_row($resdn);
 if($rowdn[0]<=0)
 {
    $jvnodn=1;
 }
 else
 {
    $jvnodn=($rowdn[0]+1);
 }  
$mcgstp = 0;
$mcgstamt = 0;
$msgstp = 0;
$msgstamt = 0;
$migstp = 0;
$migstamt = 0;

if($taxtype=='INTERSTATE')
{
	$entrydate= date("Y",time()).'-'.date("m",time()).'-'.date("d",time());
	$rs=mysqli_query($db,"select max(refno) from veh_purchase where brcode='$brcode' and taxtype = '$taxtype'")or die(mysqli_error($db));
	$p_row1=mysqli_fetch_row($rs);
	if(mysqli_num_rows($rs)<=0)
		$refno=1;
	else
		$refno=$p_row1[0]+1;
	$migstp = $taxp;
	$migstamt = $tax;
}
else
{
	$mcgstp = $taxp/2;
	$mcgstamt = $tax/2;
	$msgstp = $taxp/2;
	$msgstamt = $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','eich','-','$loc','$taxp','$emcode','$jvno','$config','-', '$cessp','$cessamt','$taxtype','$refno',$mcgstp,$mcgstamt,$msgstp,$msgstamt,$migstp,$migstamt,'$hsncode','$tcsp','$tcsamt','$sapno')";
 //echo $sql;
mysqli_query($db,$sql) or die(mysqli_error($db));

log_entry($brcode,$user,"$jvno|Veh purchase|Add","Veh purchase entry-$da",$db);

$sqldn="insert into psnindia_psndbdn.veh_purchase values('$ledger','$invno','$invdate','$da','$mcode','$config',
 '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode',
 '$entrydate','$pno','eich','-','$loc','$taxp','$emcode','$jvnodn','$config','-', '$cessp','$cessamt','$taxtype','$refno',$mcgstp,$mcgstamt,$msgstp,$msgstamt,$migstp,$migstamt,'$hsncode','$tcsp','$tcsamt')";
 //echo $sql;
mysqli_query($db,$sqldn) or die(mysqli_error($db));














//$sql="https://psnautos.in/psn/purchaseinsert.php?ledger=$ledger&invno=$invno&invdate=$invdate&da=$da&mcode=$mcode&config=$config&netamt=$netamt&tax=$tax&amt2=$amt2&disc=$disc&color=$color&engno=$engno&chasis=$chasis&remark=$remark&brcode=$brcode&entrydate=$entrydate&pno=$pno&loc=$loc&taxp=$taxp&emcode=$emcode&jvno=$jvno&config=$config&cessp=$cessp&cessamt=$cessamt&taxtype=$taxtype&refno=$refno&cgstp=$mcgstp&cgstamt=$mcgstamt&sgstp=$msgstp&sgstamt=$msgstamt&igstp=$migstp&igstamt=$migstamt&hsncode=$hsncode&tcsp=$tcsp&tcsamt=$tcsamt";



/*//$result = file_get_contents($sql);
//$sql="https://psnautos.in/psn/purchaseinsert.php";
    $ch = curl_init($sql); 
    curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);  
	if ($output === FALSE) 
	{
	    die(curl_error($ch));
	}
	curl_close($ch); 
print $result;
//print file_get_contents($sql);*/
//print $sql."Done";
//exit;

$sq="update purorder set purno='$pno',purdate='$entrydate' where dano='$da' and brcode='$brcode'";
mysqli_query($db,$sq) or die(mysqli_error($db));

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

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

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

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

  	$sql="insert into journal2 values('$entrydate','$jvno','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
	
	$sql="insert into journal2 values('$entrydate','$jvno','Dr','TCS Amount Receivable @ $tcsp%','$tcsamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
	
	
	
	
	$sqldn1="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','Purchase $taxp(vehicle)','$amt2', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn1) or die("J V Write error 1:".mysqli_error($db));

	$sqldn2="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','CGST $taxper','$tax_amt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn2) or die("J V Write error 2:".mysqli_error($db));

	$sqldn3="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','SGST $taxper','$tax_amt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn3) or die("J V Write error 2:".mysqli_error($db));

  	$sqldn4="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn4) or die("J V Write error 2cess:".mysqli_error($db));
	
	$sqldn5="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','TCS Amount Receivable @ $tcsp%','$tcsamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn5) or die("J V Write error 2cess:".mysqli_error($db));
	
	
	
	
	
	
	
}
else
{
	$sql="insert into journal2 values('$entrydate','$jvno','Dr','Purchase $taxp(vehicle) IGST','$amt2', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error($db));

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

  	$sql="insert into journal2 values('$entrydate','$jvno','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
	$sql="insert into journal2 values('$entrydate','$jvno','Dr','TCS Amount Receivable @ $tcsp%','$tcsamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
	
	
	
	
	$sqldn1="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','Purchase $taxp(vehicle) IGST','$amt2', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn1) or die("J V Write error 1:".mysqli_error($db));

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

  	$sqldn3="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','Input Cess','$cessamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn3) or die("J V Write error 2cess:".mysqli_error($db));
	$sqldn4="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','TCS Amount Receivable @ $tcsp%','$tcsamt', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
	mysqli_query($db,$sqldn4) or die("J V Write error 2cess:".mysqli_error($db));
	
	
	
	
	
}
//print $sql;
$roundoff = $_REQUEST['roundoff'];
if($roundoff>0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','$roundoff', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
$sqldn5="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Dr','Round Off','$roundoff', '0','P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
mysqli_query($db,$sqldn5) or die("J V Write error 2cess:".mysqli_error($db));
}
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','$da')";
mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error($db));
$sqldn6="insert into psnindia_psndbdn.journal2 values('$entrydate','$jvnodn','Cr','Round Off','0','$roff', 'P.Bill No: $invno','$brcode','V pur','DA','$da','$da')";
mysqli_query($db,$sqldn6) or die("J V Write error 2cess:".mysqli_error($db));

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

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

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

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

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


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

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

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

 $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($db));
}
*/





// Array Parameter Data
/*
$data = [
  'ledger'=>'$ledger', 
  'invno'=>'$invno',
  ];
  */
  

  






$data = array(
      'ledger'=>$ledger, 
  'invno'=>$invno,
	'invdate' => $invdate,
	'da' => $da,
	'mcode' => $mcode,
	'config' => $config,
	'netamt' => $netamt,
	'tax' => $tax,
	'amt2' => $amt2,
	'disc' => $disc,
	'color' => $color,
	'engno' => $engno,
	'chasis' => $chasis,
	'entrydate' => $entrydate,
	'pno' => $pno,
	'loc' => $loc,
	'taxp' => $taxp,
	'emcode' => $emcode,
	'config' => $config,
	'cessp' => $cessp,
	'cessamt' => $cessamt,
	'taxtype' => $taxtype,
	'refno' => $refno,
	'hsncode' => $hsncode,
	'tcsp' => $tcsp,
	'tcsamt' => $tcsamt
	);
	
	/*
	$invdate=$_REQUEST['invdate'];
$da=$_REQUEST['da'];
$mcode=$_REQUEST['mcode'];
$config=$_REQUEST['config'];
$netamt=$_REQUEST['netamt'];
$tax=$_REQUEST['tax'];
$amt2=$_REQUEST['amt2'];
$disc=$_REQUEST['disc'];
$color=$_REQUEST['color'];
$engno=$_REQUEST['engno'];
$chasis=$_REQUEST['chasis'];
$remark=$_REQUEST['remark'];
$brcode=$_REQUEST['brcode'];
$entrydate=$_REQUEST['entrydate'];
$pno=$_REQUEST['pno'];
$loc=$_REQUEST['loc'];
$taxp=$_REQUEST['taxp'];
$emcode=$_REQUEST['emcode'];
$jvno=$_REQUEST['jvno'];
$config=$_REQUEST['config'];
$cessp=$_REQUEST['cessp'];
$cessamt=$_REQUEST['cessamt'];
$taxtype=$_REQUEST['taxtype'];
$refno=$_REQUEST['refno'];
$mcgstp=$_REQUEST['cgstp'];
$mcgstamt=$_REQUEST['cgstamt'];
$msgstp=$_REQUEST['sgstp'];
$msgstamt=$_REQUEST['sgstamt'];
$migstp=$_REQUEST['igstp'];
$migstamt=$_REQUEST['igstamt'];
$hsncode=$_REQUEST['hsncode'];
$tcsp=$_REQUEST['tcsp'];
$tcsamt=$_REQUEST['tcsamt'];
*/	
	
	
	
	
	
	
	
	
	
	
	
	
  $str = http_build_query($data);
//print $data; 
$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://psnautos.in/psn/purchaseinsert.php');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	curl_close($ch);








/*
curl_setopt_array($curl, [
  CURLOPT_URL => "https://psnautos.in/psn/purchaseinsert.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => $data,
  CURLOPT_HTTPHEADER => [
    "Content-Type: application/json",
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
*/

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $output;
}








}
?>

<script>
var ar1=[];
var config=[];
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=0;
    document.f1.taxp.value=14.5;
 }
}
function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
   }
 else
  {
    document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
  }
}

function valid()
{
 submitOK="true";
 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');
}



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>
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 PURCHASE EICHER(Pur No:

<?
$rs=mysqli_query($db,"select max(pur_no) from veh_purchase where brcode='$brcode'")or die(mysqli_error($db));
 $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=2><select name='ledger'>
<?

//select * from sup_master where branch='$brcode'
//$res=mysqli_query($db,"select supname from sup_master where (supname like 'Eich%' or supname like '%VE COMMERCIAL VEHICLES%') and branch='$brcode' order by supname");

$res=mysqli_query($db,"select led from ledger where grp='Sundry Creditors' and brcode='$brcode' order by led");
while($row=mysqli_fetch_row($res))
{
 	print "<option>$row[0]</option>";
}

?>
</select></td>
<td colspan=1 align=right>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><input type=text name='invno'  value=<?print $invno ?>></td>
<td>SAP No </td><td><input type=text name='sapno' id='sapno'></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())?>'>
</td>
</tr>
<tr>
<td> <font color=red>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>

Tax Type <select name='taxtype' onChange='setttype()'> <option>INTRASTATE</option><option>INTERSTATE</option></select>
 </td><td>
Vehicle/Body </td><td><select name='remark'> <option>VEHICLE</option><option>BODY</option></select>
</td></tr>
<tr><td><font color='red'>DA </td><td>
<select name='da' onChange=fun1()>
<option></option>
<?
$result=mysqli_query($db,"select code,ordno,dano,mcode,config from purorder where purno ='-' and brcode='$brcode'")or die(mysqli_error($db));

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

 print "\n<option>$row[dano]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'</script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode'></td>
<td>config </td><td><input type=text readonly name='config'></td></tr>
<tr><td><font color='red'>Amount <br>Before Tax </td><td><input type=text  name='amt2' onchange=calc() ></td>
<td>Tax %</td><td><select name='taxp' onChange=calc()>
<?
$result1=mysqli_query($db,"select tax_igst from taxmaster order by tax_igst");
while($row=mysqli_fetch_array($result1))
{
	print "\n<option value='$row[0]'>$row[0]</option>";
}
//<option value='14.50'>14.50</option><option value='5.50'>5.50</option><option value='1'>1</option><option value='2'>2</option><option value='0'>0</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>TCS %</td><td><input type=text name='tcsp' size=2 value='0' onChange=calc()></td>
<td>TCS Amount</td><td><input type=text name='tcsamt' readonly size=6 ></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' size=2 value='0' onChange=calc()>";

?>
</td>
<td>Cess Amt</td><td><input type=text name='cessamt' readonly size=6 ></td>
    
<td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calc() value=<?print $amt ?>></td>

</tr>
<tr><td>Discount </td><td><input type=text name='disc' value='0' ></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) value=<?print $eng?> ></td>
</tr>
<tr><td><font color='red'>Chasis No</td><td><input type=text name='chasis'   onKeyDown=chkey(this) value=<?print $cha?> ></td>
<td>Eicher<br>Model Code </td><td><input type=text name='emcode'></td>
<td>HSN Code </td><td><input type=text name='hsncode'></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($db));
$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 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.amt2.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 tcs_amt=gt*parseFloat(document.f1.tcsp.value)/100;
    document.f1.tcsamt.value=tcs_amt.toFixed(2);
    var net_amt=gt+tcs_amt;
    document.f1.netamt.value=net_amt.toFixed(0);
    var round=parseFloat(document.f1.netamt.value)-net_amt;
    //alert(round.toFixed(2));
    document.f1.roundoff.value=round.toFixed(2);
}
</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>
<?
$search = $_REQUEST['search'];
$sea = $_REQUEST['sea'];
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=red>Record not found</font>";
   return;
 }
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>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>".implode('-', array_reverse(explode('-', $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>


