<? 
session_start();
$user = $_SESSION['user'];
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");

$y = $_REQUEST['y'];
$m= $_REQUEST['m'];
$d= $_REQUEST['d'];

$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=$y.'-'.$m.'-'.$d;
//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;
}
}
//


$update = $_REQUEST['update'];
$tax = $_REQUEST['tax'];
$da = $_REQUEST['da'];
$oldda = $_REQUEST['oldda'];
$sale = $_REQUEST['sale'];
$invno= $_REQUEST['invno'];
$oldinvno= $_REQUEST['oldinvno'];
$rep = $_REQUEST['rep'];
$cust= $_REQUEST['cust'];
$taxtype= $_REQUEST['taxtype'];

$formno= $_REQUEST['formno'];
$model= $_REQUEST['model'];
$rep_n= $_REQUEST['rep_n'];
$area= $_REQUEST['area'];
$fin= $_REQUEST['fin'];
$config= $_REQUEST['config'];
$unit= $_REQUEST['unit'];
$disc= $_REQUEST['disc'];
$netamt= $_REQUEST['netamt'];
$taxp= $_REQUEST['taxp'];
$cessp= $_REQUEST['cessp'];
$cessamt= $_REQUEST['cessamt'];
$amt2= $_REQUEST['amt2'];
$color= $_REQUEST['color'];
$engno= $_REQUEST['engno'];
$segment= $_REQUEST['segment'];
$chasis= $_REQUEST['chasis'];
$remark= $_REQUEST['remark'];
$tcsper= $_REQUEST['tcsper'];
$tcsamt= $_REQUEST['tcsamt'];
$cgstp= $_REQUEST['cgstp'];
$sgstp= $_REQUEST['sgstp'];
$cgstamt= $_REQUEST['cgstamt'];
$sgstamt= $_REQUEST['sgstamt'];

if($update)
{
$taxamt='$tax';
$sql1="update veh_movement set da='$da' where da='$oldda' and type='sale' and brcode='$brcode'";

 mysqli_query($db,$sql1) or die('veh movement err:'.mysqli_error());

$sql2="update vehopstock set sold='-' where dano='$oldda' and brcode='$brcode'";

 mysqli_query($db,$sql2) or die('veh op stock err:'.mysqli_error());

$sql2="update vehopstock set sold='$invno' where dano='$da' and brcode='$brcode'";

 mysqli_query($db,$sql2) or die('veh op stock err:'.mysqli_error());

//return;
$sql3="update veh_purchase set sale_invoice='-' where da='$oldda' and sale_invoice='$oldinvno' and brcode='$brcode'";

 mysqli_query($db,$sql3) or die('veh purchase err:'.mysqli_error());

$sql3="update veh_purchase set sale_invoice='$invno' where da='$da' and brcode='$brcode'";

 mysqli_query($db,$sql3) or die('veh purchase err:'.mysqli_error());

$sql4="update veh_transin set sale_invoice='$invno' where sale_invoice='$oldinvno' and da='$oldda' and brcode='$brcode'";
 mysqli_query($db,$sql4) or die('veh transin err:'.mysqli_error());

 $invdate=$y.'-'.$m.'-'.$d;
 $rr=mysqli_query($db,"select code from rep_master where repname='$rep'");
 $rep_row=mysqli_fetch_row($rr);
 $rep_n=$rep_row[0];

 $rr12=mysqli_query($db,"select taxtype from veh_invoice where invno='$invno' and da='$oldda' ");
 $rep_row1=mysqli_fetch_row($rr12);



			$find_jv=mysqli_query($db,"select * from veh_invoice where invno='$invno' and brcode='$brcode'");
  			$f_j=mysqli_fetch_row($find_jv);
  			$j_no=$f_j[23];
  			if($j_no >0)
			{
		 		mysqli_query($db,"delete from journal2 where jvno='$j_no' and brcode='$brcode'");
  		    	}
 					
			$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
			$jvrow=mysqli_fetch_row($jvres);
			if($jvrow[0]>0)
 				$jvno=($jvrow[0]+1);
			else
 				$jvno=1;







$cure=mysqli_query($db,"select code,acname from cust_master where code='$cust'");
$cust_row=mysqli_fetch_row($cure);
$cust_name=strtoupper($cust_row[1]);
if($cust_name=='')
{
$cure=mysqli_query($db,"select code,supname from sup_master where code='$cust'");
$cust_row=mysqli_fetch_row($cure);
$cust_name=strtoupper($cust_row[1]);
}



if($taxtype=='INTRASTATE')
{
	$cgstp=$taxp/2;
	$sgstp=$taxp/2;
	$cgstamt=$tax/2;
	$sgstamt=$tax/2;
	$sql="update veh_invoice set cust='$cust',formno='$formno',model_code='$model',rep='$rep_n', area='$area', fin='$fin',invdate='$invdate',da='$da',config='$config',unit='$unit', disc='$disc',netamt='$netamt', taxp='$taxp',tax='$tax',cessp='$cessp',cessamt='$cessamt', amt2='$amt2', color='$color', engno='$engno',segment='$segment', chasis='$chasis',remark='$remark',tcsper = '$tcsper',tcsamt = '$tcsamt',cgstp='$cgstp',sgstp='$sgstp',igstp='0',cgstamt='$cgstamt',sgstamt='$sgstamt',igstamt='0',jvno='$jvno'  where brcode='$brcode' and invno='$invno' and taxtype='$rep_row1[0]'";
	mysqli_query($db,$sql) or die('<br>invoice err:'.mysqli_error());
}
else
{
	$sql="update veh_invoice set cust='$cust',formno='$formno',model_code='$model',rep='$rep_n', area='$area', fin='$fin',invdate='$invdate',da='$da',config='$config',unit='$unit', disc='$disc',netamt='$netamt', taxp='$taxp',tax='$tax',cessp='$cessp',cessamt='$cessamt', amt2='$amt2', color='$color', engno='$engno',segment='$segment', chasis='$chasis',remark='$remark',tcsper = '$tcsper',tcsamt = '$tcsamt',cgstp='0',sgstp='0',igstp='$taxp',cgstamt='0',sgstamt='0',igstamt='$tax',jvno='$jvno'  where brcode='$brcode' and invno='$invno' and taxtype='$rep_row1[0]'";
	mysqli_query($db,$sql) or die('<br>invoice err:'.mysqli_error());
}
echo $sql;
$sql="update veh_movement set date='$invdate' where inv_no='$invno' and da='$da' and type='sale' and brcode='$brcode'";
mysqli_query($db,$sql)or die('movement err:'.mysqli_error());
$cres=mysqli_query($db,"select * from cust_master where code='$cust' and branch='$brcode'");
$cusrow=mysqli_fetch_array($cres);
   




if($taxtype=='INTERSTATE')
{
	$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
	$sql="insert into journal2 values('$invdate','$jvno','Cr','IGST Output $taxp%', '0','$tax','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
	$sql="insert into journal2 values('$invdate','$jvno','Cr','INTERSTATE Sale $taxp%(vehicle)','0','$amt2','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	if($tcsper>0)
        {
		$sql="insert into journal2 values('$invdate','$jvno','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		echo $sql;
	echo "<br>";
		
		mysqli_query($db,$sql) or die("J V Write error 4:".mysqli_error());
	}

}
else
{
	$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
	$cgstamt = $tax /2;
	$sql="insert into journal2 values('$invdate','$jvno','Cr','CGST Output 14%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
	$sql="insert into journal2 values('$invdate','$jvno','Cr','SGST Output 14%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

	$sql="insert into journal2 values('$invdate','$jvno','Cr','Output Cess', '0','$cessamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error cess:".mysqli_error());
	$sql="insert into journal2 values('$invdate','$jvno','Cr','Sales 28%(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	if($tcsper>0)
        {
		$sql="insert into journal2 values('$invdate','$jvno','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		echo $sql;
	echo "<br>";
		mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	}
}








/*
$sql="update journal2 set date='$invdate',partic='$cusrow[acname]',dramt='$netamt',cramt='0',no='$da' where jvno='$jvno' and type='Dr'  and brcode='$brcode' and partic!='Round Off'";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="update journal2 set date='$invdate',cramt='$tax',no='$da' where jvno='$jvno' and type='Cr' and (partic='Output tax 14.5' or partic = 'Output tax 14.5') and brcode='$brcode'";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$sql="update journal2 set date='$invdate',cramt='$amt2',no='$da' where jvno='$jvno' and type='Cr' and brcode='$brcode' and (partic='Sales 14.5(vehicle)' or partic = 'Sales 14.5(vehicle)')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

$sql="update journal2 set cramt='$cessamt',date='$invdate',no='$da' where partic='Output Cess' and jvno='$jvno' and brcode='$brcode'";
mysqli_query($db,$sql) or die("J V Write error cess:".mysqli_error());
$sql="delete from journal2 where jvno='$jvno' and brcode='$brcode' and partic='Tax Collected at Source (TCS) @1%'";
mysqli_query($db,$sql);
if($tcsper>0)
{
	$sql="insert into journal2 values('$invdate','$jvno','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
}

$sql="delete from journal2 where jvno='$jvno' and brcode='$brcode' and partic='round off'";
mysqli_query($db,$sql);
*/
//*****************
$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('$invdate','$jvno','Dr','Round Off','$x', '0','Round ofSINV No: $invno','$brcode','V sale','DA','$da','$invno')";
	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
else if($dif>0)
{
 	$sql="insert into journal2 values('$invdate','$jvno','Cr','Round Off','0','$x','Round of adjusted SINV No: $invno','$brcode','V sale','DA','$da','$invno')";
 	echo $sql;
	echo "<br>";
	mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}


//*********************

print "<script> document.location='seainvoice.php'; </script>";

$sql="update crnote set da='$da' where sale_invoice='$invno' and brcode='$brcode'";
mysqli_query($db,$sql) or die('Crnote error:'.mysqli_error());
log_entry($brcode,$user,$REMOTE_ADDR,"Edit veh invoice-da=$da, invno=$invno");
}
$da=str_replace('*',' ',$da);
$sql="select * from veh_invoice where da='$da' and invno='$invno' and brcode='$brcode'";
$result=mysqli_query($db,$sql) or die(mysqli_error());
$inv_row=mysqli_fetch_array($result);

?>
<style>
body,td,th{font-family:'arial narrow';letter-spacing:1;font-size:14}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<script>
var adr=[];
var ar1=[];
var config=[];
var config2=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];

function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
    document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
    document.f1.color.value=color[document.f1.da.selectedIndex];
     document.f1.engno.value=engno[document.f1.da.selectedIndex];
     document.f1.chasis.value=chasis[document.f1.da.selectedIndex];
    document.f1.config.value=config2[document.f1.da.selectedIndex];
/*
  if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
   }
*/
}
function fun2()
{
document.f1.config.value=config2[document.f1.mcode.selectedIndex];
}
function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.cust.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Customer');
 }
 if(document.f1.formno.selectedIndex<0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
 if(document.f1.segment.selectedIndex<0)
 {
   submitOK="false";
   alert('Invalid Segment No.');
 }


var day1=document.f1.d.value;
var mon1=document.f1.m.value;
var year1=document.f1.y.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.m.focus();

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

}

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

{
 submitOK="false";

document.f1.d.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 setadr()
{
  var x= document.f1.cust.selectedIndex ; 
    document.f1.addr.value=adr[document.f1.cust.selectedIndex];
}
function setModel()
{
  var x= document.f1.formno.selectedIndex ; 
     document.f1.model.value=model[document.f1.formno.selectedIndex];
     document.f1.rep.value=rep[document.f1.formno.selectedIndex];
     document.f1.area.value=area[document.f1.formno.selectedIndex];
//alert(model[document.f1.formno.selectedIndex]);
}
 

</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=black>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>CUSTOMER DETAILS</TD></TR>
<TR><td align=center bgcolor='ivory'>
<table>
<tr><td><font color=red>Customer</td><td colspan=4> <select name='cust' onChange='setadr()'>
<?

$re=mysqli_query($db,"select * from cust_master where code='$inv_row[cust]' and branch='$brcode'")or die(mysqli_error());
 $cust_row=mysqli_fetch_row($re);
?>
<option value='<?print $inv_row['cust']?>'><?print $cust_row[1]?></option>
<?
$i=1;
$re=mysqli_query($db,"select * from cust_master where branch='$brcode'");
while($row=mysqli_fetch_array($re))
{
 print "\n<option value='$row[code]'>$row[acname]</option>";
 print "\n<script>adr[$i]='$row[addr1] - $row[city] - $row[dist] - $row[stat] - $row[pin] - $row[phone1]' </script>";
 $i++;
}
?>
</select> </td></tr>
<tr>
<td>Address </td><td><textarea name='addr' cols=40><?print $cust_row[2]?></textarea></td>
<td><font color=red>Form No</td><td>
<input type=hidden name ='oldformno' value='<?print $inv_row['formno']?>'>
<input type=hidden name ='jvno' value='<?print $inv_row['jvno']?>'>

 <select name='formno' onChange='setModel()'>


<option><?print $inv_row['formno']?></option>
<?
//$rr=mysqli_query($db,"select a.formno,a.model,b.repname,b.dist from orderform a,rep_master b where a.rname=b.code and a.sale_invno='-' and a.brcode='$brcode'")or die(mysqli_error());


$rr=mysqli_query($db,"select a.formno,a.model,b.repname,b.dist from orderform a,rep_master b where a.rname=b.code and a.dt>='2017-10-01' and a.formno not in (select formno from veh_invoice where brcode='$brcode') and a.brcode='$brcode'")or die(mysqli_error());





 $i=0;
 print "\n<script> model[$i]='$inv_row[model_code]' </script>";
 print "\n<script> rep[$i]='$inv_row[rep]' </script>";
 print "\n<script> area[$i]='$inv_row[area]' </script>";
$i++;
while($row=mysqli_fetch_row($rr))
{
 print "\n<option>$row[0]</option>";
 print "\n<script> model[$i]='$row[1]' </script>";
 print "\n<script> rep[$i]='$row[2]' </script>";
 print "\n<script> area[$i]='$row[3]' </script>";

 $i++;
}
?>
</select> 
<br>Model Code<input type=text readonly name='model' value='<?print $inv_row['model_code']?>'>
</td></tr>
<? 
 $rp=mysqli_query($db,"select code,repname from rep_master where code='$inv_row[rep]'")or die(mysqli_error());
 $rp_row=mysqli_fetch_row($rp);
?>
<tr><td>Rep<td><input type=text readonly name='rep' value='<?print $rp_row[1]?>'></td> 
 <td>Area </td><td><input type=text name='area' value='<?print $inv_row['area']?>' readonly></td>
</tr>
<tr><td>
<font color=red>Financer</font> </td><td colspan=4> <select name='fin'> 
<option> <?print $inv_row['fin']?></option>
<?
 $re=mysqli_query($db,"select code,finname from fin_master where branch='$brcode'");
  while($row=mysqli_fetch_row($re))
  {
   print "<option $row[0]>$row[1]</option>";
  }
?>
</select>
</table>
 </td></tr>
</table>




<br>
<CENTER>
<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>EDIT VEHICLE SALES  (Inv No:<?print $invno?>)
<input type=hidden name='oldinvno' value='<?print $invno?>'>
</TD></TR>
<tr>

</tr>
<tr>
<td>Invoice No </td><td><input type=text  name='invno' value='<?print $invno?>' size=8 ></td>
<?
$dt=explode('-',$inv_row['invdate']);
$d=$dt[2];
$m=$dt[1];
$y=$dt[0];
?>
<td>Invoice Date </td><td>dd <input type=text name='d' size=2 value='<?print $d?>'>
mm <input type=text name='m' size=2 value='<?print $m?>'>
yy <input type=text name='y' size=4 value='<?print $y?>''>
</td>

<td>Tax Type</td><td><select name='taxtype'>
<option value='INTRASTATE' <?php if ($inv_row['taxtype'] == 'INTRASTATE') echo ' selected="selected"'; ?>>INTRASTATE</option>
<option value='INTERSTATE' <?php if ($inv_row['taxtype'] == 'INTERSTATE') echo ' selected="selected"'; ?>>INTERSTATE</option>
</select>
</td>

</tr>
<tr><td>DA </td><td>
<input type=hidden name ='oldda' value='<?print $inv_row['da']?>'>

<select name='da' onChange=fun1()>
<option><?print $inv_row['da']?></option>
<?
$i=0;
 print "\n<script> ar1[$i]='$inv_row[model_code]';\n config[$i]='$inv_row[config]'; \n";
 print "color[$i]='$inv_row[color]'; \n engno[$i]='$inv_row[engno]'; \n chasis[$i]='$inv_row[chasis]'; </script>";
$i++;
$result=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[da]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]'; </script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and sold='-'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[dano]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chno]'; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td colspan=4><select name='mcode' onChange=fun2()>
<option> <?print $inv_row['model_code']?> </option>
<?

$xx=mysqli_query($db,"select * from veh_master where mcode='$inv_row[model_code]' and brcode='$brcode'");
$kkk=mysqli_fetch_row($xx);

 print "\n<script>config2[0]='$kkk[2]';</script>";
$rr=mysqli_query($db,"select * from veh_master where brcode='$brcode'");
$k=1;

while($rrow=mysqli_fetch_array($rr))
{
 print "<option value='$rrow[1]'>$rrow[1]-$rrow[2]</option>";
 print "\n<script>config2[$k]='$rrow[2]';</script>";
 $k++;
}
?>

</td></tr>
<tr><td>config </td><td colspan=5><input type=text name='config' size=93 value='<?print $inv_row['config']?>'></td></tr>
<tr><td><font color=red>Unit Price </td><td>
	<input type=text name='unit' size=12 onChange=calc() value="<?print $inv_row['amt2']?>"> </td>
    <td>Discount </td><td><input type=text name='disc' SIZE=8 onChange=calc() value="<?print $inv_row['disc']?>"></td><td>Amount <br>Before Tax </td><td><input type=text   name='amt2' onChange=calc() value="<?print $inv_row['amt2']?>"></td>
    </tr>
<tr>
<?
if($brcode=='AUT009'|| $brcode=='AUT103')
{
?>   
 <td>Tax %</td><td><input type=text readonly name='taxp' size=5 value='14.5'>
 <input type=text name='tax' readonly value='<?print $inv_row['tax']?>' size=8></td>
<?
}
else
{
?>
 <td>Tax %</td><td><input type=text readonly name='taxp' size=5 value='28'>
 <input type=text name='tax' readonly value='<?print $inv_row['tax']?>' size=8></td>
<?
}
?>
<td>Cess % 
<?
if($brcode=='AUT009'|| $brcode=='AUT103')
{
?>

<input type=text name='cessp' value='0' readonly size=1 onChange=calc()>

<?
}
else
{
?>

<input type=text name='cessp' value='0' size=1 onChange=calc()>

<?
}

?>

Cess Amt <input type=text name='cessamt' readonly size=8 value='<?print $inv_row['cessamt']?>'>
</td>
<td>
TCS % <input type=text name='tcsper' value='<?print $inv_row['tcsper']?>'  size=4 onChange=calc()>
TCS Amount <input type=text name='tcsamt' readonly  size=10 value='<?print $inv_row['tcsamt']?>'>

</td>
    <td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calc() value="<?print $inv_row['netamt']?>"></td>
</tr>
<tr><td>Color</td>
<td><input type=text name='color' value='<?print $inv_row['color']?>'></td><td>Engine No</td><td><input type=text readonly name='engno' value='<?print $inv_row['engno']?>'></td>
</td></tr>
<tr><td><font color=red>Segment </td><td colspan=6><select name='segment'>
<option><?print $inv_row['segment']?></option>
<?
 $re=mysqli_query($db,"select * from seg_master");
 while($row=mysqli_fetch_row($re))
 {
   print "<option>$row[1]</option>";
 }
?>
</select></td>
</tr>
<tr><td>Chasis No</td><td><input type=text readonly name='chasis' value='<?print $inv_row['chasis']?>'></td>
<td>Remarks </td><td colspan=3><textarea name='remark' cols=55><?print $inv_row['remark']?></textarea></td></tr>
<tr><td colspan=6 align=center bgcolor='green'> 

<input type=submit  value=' UPDATE ' name='update'>
<input type=button value=' CANCEL ' onClick=document.location='seainvoice.php'>
</table>
</form>
</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>
</center>
<br>
<script>
function calc()
{
var taxper=parseFloat(document.f1.taxp.value);
document.f1.amt2.value=document.f1.unit.value-document.f1.disc.value;
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
//net=parseFloat(document.f1.netamt.value);

//amt=((net*100)/(100+taxper));
/*
if(net >= 1000000)
{
	if(document.f1.tcsper.value>0)
	{
	    ces_p = 0;
	    if(parseFloat(document.f1.cessp.value)>0)
	    {
	        ces_p = 1.01;
	        amt=(net/(1+(28.95748895+ces_p)/100));
	    }
	    else
	    {
	        amt=(net/(1+(28.95997785)/100));
	    //amt=(net/(1+(29.28+ces_p)/100));
	        //amt=(net/(1+(28.95748895+ces_p)/100));
	    }
	    //alert(amt);
	}
	else
	{
		//alert(parseFloat(document.f1.cessp.value));
		amt=(net/(1+(parseFloat(document.f1.taxp.value)+parseFloat(document.f1.cessp.value))/100));
			
		document.f1.tcsper.value=0;
	}
}
else
{
	amt=(net/(1+(parseFloat(document.f1.taxp.value)+parseFloat(document.f1.cessp.value))/100));
	document.f1.tcsper.value=0;
}
*/
//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.amt2.value*cessper)/100);
document.f1.cessamt.value=ces_amt.toFixed(2);
var tcsamt=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+ces_amt)*parseFloat(document.f1.tcsper.value)/100;
document.f1.tcsamt.value=tcsamt.toFixed(2);
document.f1.netamt.value=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value)+parseFloat(document.f1.tcsamt.value)).toFixed(2);
document.f1.netamt.value=Math.round(document.f1.netamt.value);

}

function calc2()
{
    document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
	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);
	tcsper1 = parseFloat(document.f1.tcsper.value);
	//amt=((net*100)/(100+taxper));
	if(net >= 1000000)
	{
	    ces_p = 0;
	    if(parseFloat(document.f1.cessp.value)>0)
	    {
	    ces_p = 1.01;
	    amt=(net/(1+(28.95748895+ces_p)/100));
	    }
	    else
	    {
	        amt=(net/(1+(28.95997785)/100));
	        
	    }
		//amt=(net/1.2928);
		//amt=((net*0.0075)*(1+taxper))/1.0075;
		
	}
	else
	{
		amt=(net/(1+(parseFloat(document.f1.taxp.value/100))));
		document.f1.tcsper.value=0;
	}
	if(tcsper1<=0)
	{
		amt=(net/(1+(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 cessper=parseFloat(document.f1.cessp.value).toFixed(0);
	//var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
	var ces_amt=parseFloat((document.f1.amt2.value*cessper)/100);
	document.f1.cessamt.value=ces_amt.toFixed(2);
	var tcsamt=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value))*parseFloat(document.f1.tcsper.value)/100;
	document.f1.tcsamt.value=tcsamt.toFixed(2);
}

function calc_tcs()
{
	var tcsamt=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value))*parseFloat(document.f1.tcsper.value)/100;
	document.f1.tcsamt.value=tcsamt.toFixed(2);
	//document.f1.netamt.value=(parseFloat(document.f1.netamt.value)+tcsamt);
}
</script>
