<?
session_start();
include("../opendb.php");
//include("../test.php");
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:steelblue;color:white}
.right{text-align:right}
</style>

<center>


<?
if($next)
{
$pbillno=explode('-',$pbillno);
$i=1;
$sql="select * from sparepurchase where purno='$pbillno[1]' and brcode='$brcode'";
$re=mysqli_query($db,$sql)or die('err'.mysqli_error());
if(mysqli_num_rows($re)<=0)
{
	print "OLD1 ";
	$sql="select * from sparepurchase where purno='$pbillno[1]' and brcode='$brcode'";
	$re=mysqli_query($db,$sql)or die('err'.mysqli_error());
}
$row1=mysqli_fetch_array($re)or die(' fet err'.mysqli_error());
print "<form action='svpurreturn.php' method='post' target='xn'> <input type=hidden name='pbillno' value='$pbillno[0]' >";
?>
</center>
<table>
<tr><td>
Return Date </td><td> dd <input type=text name='d' value='<?print date("d")?>' size=2>
	 mm <input type=text name='m' value='<?print date("m")?>' size=2>
	 yy <input type=text name='y' value='<?print date("Y")?>' size=4></td>
</tr>
<tr><td>P Bill No</td>
<td><input type=text readonly name='pbillno' value='<?print $pbillno[0]?>' size=10>
</td></tr>
<tr><td>Pur No</td>
<td><input type=text readonly name='purno' value='<?print $pbillno[1]?>' size=5>
</td></tr>
<tr><td>Tax Type</td>
<td><input type=text readonly name='taxtype' value='<?print $row1[taxtype]?>' size=5>
</td></tr>

<tr><td>Supplier </td>
<td><input type=text readonly name='sup' value='<?print $row1[suplier]?>' size=50>
</td></tr>
</table>
<center>
<br>
<?
print "<font color=red>Enter return quantity to return the Item</font><br> <br>";
print "<table border=0 cellpadding=3>";
 print "<th>Part Code <th>Part Name";
print "<th>Ret Qty <th>Rate <th>Disc <th>A Price<th>Hand <th>Tax% <th>Tax <th>Cess% <th>Cess <th>Total";
$q="select * from sparepurchase where purno='$pbillno[1]' and brcode='$brcode'";
$re1=mysqli_query($db,$q)or die('err'.mysqli_error());
$i=1;
while($row=mysqli_fetch_array($re1))
{
  print "<tr><td><input type=text name='partcode[]' value='$row[partcode]' size=12></td>";
  print "\n<td><input type=text name='partname[]' value='$row[partname]'></td>";
  print "<td bgcolor='yellow'><input type=text class=right name='retqty[]' id='$i' value='0' size=3 onChange=calc(this.id)></td>";
  print "<td><input type=text class=right name='price[]' id='price$i' value='$row[price]' size=6></td>";
  print "<td><input type=text class=right name='disc[]' id='disc$i' value='$row[disc]' size=6></td>";

  print "<td><input type=text class=right name='aprice[]' id='aprice$i' value='$row[aprice]' size=6></td>";
  print "<td><input type=text class=right name='hand[]' id='hand$i' value='$row[hand]' size=3></td>";

  print "<td><input type=text class=right name='tax[]' id='tax$i' value='$row[tax]' size=6 onChange=calc($i)></td>";
  print "<td><input type=text class=right name='taxamt[]' id='taxamt$i' value='$row[taxamt]' size=6></td>";

  print "<td><input type=text class=right name='cessp[]' id='cessp$i' value='$row[cessp]' size=4 onChange=calc($i)></td>";
  print "<td><input type=text class=right name='cessamt[]' id='cessamt$i' value='$row[cessamt]' size=4></td>";

  print "<td><input type=text class=right name='tot[]' id='tot$i' value='$row[tot]' size=8></td>";

  print "\n</tr>";
 $i++;
}
print "<TR bgcolor='steelblue'><TD colspan=14 align=center> <input type=submit value='Save'><input type=submit value='Preview' name='pv'></td></tr>";

print "<tr><td colspan=1>Total Qty</td><td> <input type=text id='totsupq' size=4></td></tr>";
print "<tr><td colspan=1>Total Amount </td><td><input type=text id='gtamt' size=14></td></tr>";
print "</table>";
?>
<script>
 function calc(x)
 {
 var qtyK=x; 

    if(parseFloat(document.getElementById(qtyK).value)<parseFloat(document.getElementById(x).value)){
     alert('Invalid Quantity');
document.getElementById(x).value='';
document.getElementById(x).focus();
     return;
  }
 var ele='price'+x; 
 var disele='disc'+x;
 var actele='aprice'+x;
 var hndele='hand'+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(x).value);
var hnd=amt*document.getElementById(hndele).value/100;


 var disc=amt*parseFloat(document.getElementById(disele).value)/100;
 var actamt=amt+hnd-disc;
//document.getElementById(disele).value=parseFloat(disc);

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

var hnd=actamt*document.getElementById(hndele).value/100;
var tx=parseFloat(document.getElementById(taxele).value);
var tax=((actamt)*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+tax+cess).toFixed(2);

//alert(tax);
tot=0;
for(j=1;j<<?print $i?>;j++)
 {
   t=document.getElementById(j).value; 
   if(t.length<=0) 
     t=0;
    if(parseFloat(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; 
    qtr=parseFloat(document.getElementById(j).value);
   if(t2.length<=0)
    t2=0;
    if(parseFloat(qtr)>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>
<?
return;
}// end if
?>
</form>

<form action='<?print $PHP_SELF?>' method='post'>
<table>
<tr><td bgcolor='c1d799' align=center colspan=2><h3>SPARE - PURCHASE RETURN</H3></tr></td>
<tr><td> Purchase Bill No </td><td><select name='pbillno' style="text-align:right">
<?

$re=mysqli_query($db,"select distinct pbillno,purno from sparepurchase where brcode='$brcode' order by purno")or die('err'.mysqli_error());
while($row=mysqli_fetch_row($re))
{
 print "\n<option>$row[0]-$row[1]</option>";
}
?>
</select></td></tr>
<tr bgcolor='lightsteelblue'><td colspan=2 align=center>
  <input type=submit value='Next ->' name='next'> </td>
</tr>
</table>
