<?
session_start();
include("../opendb.php");
include("../test.php");
?>
<script>
 var pname=[];
var pr=[];
 function setname(x)
 {
   var pc=document.getElementById(x).selectedIndex;
   var pn='pname'+x;
   var prs='price'+x;
   document.getElementById(pn).value=pname[pc];
   document.getElementById(prs).value=pr[pc];
 }
</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:steelblue;color:white}
.right{text-align:right};
</style>

<?
print "<form action='svspreturn_old.php' method='post' target='xn'>";
print "<table border=1 cellspacing=1 cellpadding=3 ><th colspan=2><H3> SPARE PURCHASE RETURN</h3></TH>";
print "<tr><td>Invoice No. </td><td><input type=text name='invno'></td></tr>";
print "<tr><td>Invoice Type </td><td><select name='invtype'><option>8B</option><option>8A</option>
<option>8</option>
</select></td></tr>";
print "<tr><td>VAT/CST</td><td><select name='vatorcst'><option>$vatorcst</option></select></td></tr>";
print "<tr><td>Customer </td><td><select name='customer'>";
$cres=mysqli_query($db,"select supname from sup_master  order by supname")or die(mysqli_error($db));
while($cusrow=mysqli_fetch_row($cres))
 print "<option>$cusrow[0]</option>";
print "</select>";
print "</td></tr>";
print "<tr><td>Address</td><td><textarea name='addr'></textarea></td></tr>";
print "<tr bgcolor='yellow'><td>Entry Date</td><td>dd<input type=text name='d' size=2 maxlength=2 value='".date("d")."'> mm<input type=text name='m' size=2 maxlength=2 value='".date("m")."'> yy<input type=text name='y' size=4 maxlength=4 value='".date("Y")."'></td></tr>";

?>
</table>
<br>
<center>

<?
print "<table cellpadding=3 cellspacing=1> <th>No. <th>Part Code <th>Part Name";
print " <th>Ret Qty <th>Price <th> A Price<th>Hand Chrge"; 

if($vatorcst=='CST')
   print "<th>CST%";
else
 print "<th>VAT%";

print "<th>Tax Amt<th>Cess%<th>Cess Amt<th>Total";
$i=1;
while($i<4)
{
 	print "\n<tr><td>".$i."</td><td><select name='partcode[]' id='$i' onChange='setname(this.id)'>";
 	$parres=mysqli_query($db,"select partcode,partname from partmaster order by partcode");
	$k=0;
  	while($prtrow=mysqli_fetch_array($parres))
  	{	 
		print "<option>$prtrow[0]</option>";
    		print "\n<script>pname[$k]='$prtrow[1]';</script>";

		$parres1=mysqli_query($db,"select lstprice from spareprmast where  partcode='$prtrow[0]'");
		while($prtrow1=mysqli_fetch_array($parres1))
  		{   print "\n<script>pr[$k]='$prtrow1[lstprice]';</script>"; }
$k++;
 	}

	print "</select> </td>";
 	print "<td><input type=text name='partname[]' value='$row1[partname]' size=30 id='pname$i'> </td>";

	print "\n<td align=center><input type=text name='retqty[]' class=right id='rq$i' size=4 		onChange='calc($i)'></td>";

 	print "<td align=center><input class=right type=text  value='0' name='price[]' id='price$i' size=8 	value='$row1[price]' onChange='calc($i)'></td>";
 //print "<td align=center><input class=right type=text name='disc[]' id='dis$i' size=6 value='$row1[disc]'></td>";


	print "<td align=center><input class=right type=text   name='aprice[]' id='ap$i' size=8></td>";
	 print "<td align=center><input class=right type=text   name='hand[]' id='hnd$i' size=8 	value='$row1[hand]'></td>";
//********* vat or cst
	if($vatorcst=='CST')
 	print "<td align=center><input class=right type=text    name='tax[]' id='tax$i' size=4 		value='3' onChange='calc($i)'></td>";
	else
	 print "<td align=center><input class=right type=text   name='tax[]' id='tax$i' size=4 value='12.5' onChange='calc($i)'></td>";
//**********************
 	print "<td align=center><input class=right type=text   name='taxamt[]' id='taxamt$i' 		size=8 value='0'></td>";
if($vatorcst=='CST') 
print "<td align=center><input class=right type=text value='0'  name='cessp[]' id='cessp$i' size=2 value='1' onChange='calc($i)'></td>";
else
print "<td align=center><input class=right type=text  value='1' name='cessp[]' id='cessp$i' size=2 value='1' onChange='calc($i)'></td>";

 print "<td align=center><input class=right type=text   name='cessamt[]' id='cessamt$i' size=5 value='0'></td>";

 print "<td align=center><input class=right type=text   name='tot[]' id='tot$i' size=12></td></tr>";
$i++;
}
print "\n<tr><td colspan=3 bgcolor=silver> <center><b>Total </td><td align=center bgcolor=silver><input type=text name='totsupqty[]' id='totsupqty' size=8></td>";
print "\n<td colspan=4 align=center bgcolor=silver></td><td align=center bgcolor=silver >";
print "<input type=text class=right id='tottx' size=8></td>";
print "<td></td><td><input type=text id='gtamt' size=8></td></tr>";

print "<tr><th colspan=13><input type=submit value='Save' name='save'><input type=submit value='Preview' name='pv'></th></tr> </table>";
?>
</form>
<script>
 function calc(x)
 {
 var qtyK='rq'+x; 

 var ele='price'+x; 
 var actele='ap'+x;
 var hndele='hnd'+x;
 var taxele='tax'+x;
 var totele='tot'+x;
 var txamtele='taxamt'+x;
 var totsupq='totsupqty';
 var cesspele='cessp'+x;
 var cessamtele='cessamt'+x;

 var tx='tottx';
 var amt=parseFloat(document.getElementById(ele).value)*parseFloat(document.getElementById(qtyK).value);

actamt=amt;

document.getElementById(actele).value=parseFloat(actamt);

var hnd=actamt*document.getElementById(hndele).value/100;
var tx=parseFloat(document.getElementById(taxele).value);
var tax=((actamt+hnd)*tx)/100;
document.getElementById(txamtele).value=tax.toFixed(2);
cess=(tax*parseFloat(document.getElementById(cesspele).value))/100;
document.getElementById(cessamtele).value=parseFloat(cess).toFixed(2);
document.getElementById(totele).value=(actamt+hnd+tax+cess).toFixed(2);

//alert(tax);
tot=0;
for(j=1;j<<?print $i?>;j++)
 {
   rqv='rq'+j;
   t=document.getElementById(rqv).value; 
   if(t.length<=0) 
     t=0;
   tot=parseFloat(tot)+parseFloat(t);
}

tottax1=0;
ttamt=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j; 
 var totvar='tot'+j;

   t=document.getElementById(tx).value; 
   t2=document.getElementById(totvar).value; 
   if(t2.length<=0)
    t2=0;
   ttamt=parseFloat(ttamt)+parseFloat(t2);

   if(t.length<=0) 
     t=0;
   tottax1=parseFloat(tottax1)+parseFloat(t);
}

document.getElementById(totsupq).value=parseFloat(tot).toFixed(2);
document.getElementById('tottx').value=parseFloat(tottax1).toFixed(2);
document.getElementById('gtamt').value=parseFloat(ttamt).toFixed(2);


 }
</script>

