<?
session_start();
$brcode = $_SESSION['brcode'];
$invno = $_REQUEST['invno'];
$invtype = $_REQUEST['invtype'];
$vatorcst = $_REQUEST['vatorcst'];
include("../opendb.php");
include("../test.php");
?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
.right{text-align:right};
</style>

<?
$re=mysqli_query($db,"select * from spareinvoice where invno='$invno' and invtype='$invtype' and vatorcst='$vatorcst' and brcode='$brcode'");
if(mysqli_num_rows($re)<=0)
{
  print "<script> alert('Invoice does not exist in $invtype'); history.back(1); </script>";
  return;
}
$row=mysqli_fetch_array($re);
print "<form action='svsalereturn.php' method='post'>";
print "<table border=1 cellspacing=1 cellpadding=3 ><th colspan=2><H3> SPARE SALES RETURN</h3></TH>";
print "<tr><td>Invoice No. </td><td>$row[invno]</td></tr>";
print "<tr><td>Invoice Type </td><td>$row[invtype]</td></tr>";
print "<tr><td>Payment Type </td><td>$row[paytype]</td></tr>";
print "<tr><td>Customer </td><td>$row[customer]</td></tr>";
print "<tr><td>Address</td><td>$row[addr]</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>

<input type=hidden name='invno' value='<?print $invno?>'>
<input type=hidden name='invtype' value='<?print $invtype?>'>
<input type=hidden name='customer' value='<?print $row[customer]?>'>
<input type=hidden name='addr' value='<?print $row[addr]?>'>
<input type=hidden name='paytype' value='<?print $row[paytype]?>'>
<input type=hidden name='vatorcst' value='<?print $vatorcst?>'>
<?
$re1=mysqli_query($db,"select * from spareinvoice where invno='$invno' and invtype='$invtype' and brcode='$brcode'");
print "<table cellpadding=3 cellspacing=1> <th>No. <th>Part Code <th>Part Name";
print "<th>Qty <th>Ret Qty <th>Price <th>Disc <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($row1=mysqli_fetch_array($re1))
{
 print "\n<tr><td>".$i."</td><td><input type=text name='partcode[]' value='$row1[partcode]' size=10> </td>";
 print "<td><input type=text name='partname[]' value='$row1[partname]' size=30> </td>";
 print "\n<td><input type=text class=right name='qty[]' value='$row1[qty]' id='qty$i' size=5></td>";
 //print "\n<td align=center bgcolor='silver'><input type=text name='retqty[]' id=$i size=5 onChange='chkqty(this)'> </td>";
 //print "<td><input type=text class=right name='price[]' value='$row1[price]' size=10> </td>";
 //print "<td><input type=text class=right name='disc[]' value='$row1[disc]' size=10> </td>";

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

 print "<td align=center><input class=right type=text name='price[]' id='price$i' size=8 value='$row1[price]'></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  readonly name='aprice[]' id='ap$i' size=8></td>";
 print "<td align=center><input class=right type=text  readonly 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  readonly name='tax[]' id='tax$i' size=4 value='3'></td>";
else
 print "<td align=center><input class=right type=text  name='tax[]' id='tax$i' size=4 value='$row1[tax]'></td>";
//**********************
 print "<td align=center><input class=right type=text   name='taxamt[]' id='taxamt$i' size=8 value='0'></td>";
 print "<td align=center><input class=right type=text  name='cessp[]' id='cessp$i' size=2 value='1' onChange='calc($i)'></td>";
 print "<td align=center><input class=right type=text  readonly name='cessamt[]' id='cessamt$i' size=5 value='0'></td>";

 print "<td align=center><input class=right type=text  readonly name='tot[]' id='tot$i' size=12></td></tr>";
$i++;
}
print "\n<tr><td colspan=4 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=5 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></td><td><input type=text id='gtamt' size=8></td></tr>";

print "<tr><th colspan=14><input type=submit value='Save' name='save'></th></tr> </table>";
?>
</form>
<script>
function chkqty(x)
 {
   var qtyK='qty'+x.id; 

    if(parseFloat(document.getElementById(qtyK).value)<parseFloat(document.getElementById(x.id).value)){
     alert('Invalid Quantity');
document.getElementById(x.id).value='';
document.getElementById(x.id).focus();
     return;
}
 }


 function calc(x)
 {
 var qtyK='qty'+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='dis'+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(x).value);
 var disc=amt*parseFloat(document.getElementById(disele).value)/100;
 var actamt=amt-disc;
//document.getElementById(disele).value=parseFloat(disc);

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++)
 {
   t=document.getElementById(j).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>

