<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
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>
<?
$next=$_REQUEST['next'];

if($next)
{
$pbillno=$_REQUEST['pbillno'];
$pbillno=explode('_',$pbillno);
$i=1;
$sql="select * from item_purchase_veh where purinvoice='$pbillno[0]' and pno='$pbillno[1]' and brcode='$brcode'";

$re=mysqli_query($db,$sql)or die('err'.mysqli_error($db));


/*
if(mysqli_num_rows($re)<=0)
{
print "OLD";
$sql="select * from telcondb1.sparepurchase where pbillno='$pbillno[0]' and purno='$pbillno[1]' and brcode='$brcode'";
$re=mysqli_query($db,$sql)or die('err'.mysqli_error($db));
}
*/
$row1=mysqli_fetch_array($re)or die(' fet err'.mysqli_error($db));
$reps=mysqli_query($db,"select supname,web from sup_master where icode='$row1[sup]' ");
$prows=mysqli_fetch_row($reps);
print "<form action='svpurreturnitem.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=30>
</td></tr>

<tr><td>Supplier </td>
<td><input type=text readonly name='sup' value='<?print $prows[0]?>' size=50>
</td><td><input type=hidden readonly name='supac' value='<?print $prows[1]?>' >
</td></tr>
<tr><td>Ref:</td>
<td><input type=text  name='ref' value='0' size=30>
</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 item_purchase_veh where purinvoice='$pbillno[0]' and pno='$pbillno[1]' and brcode='$brcode'";
$re1=mysqli_query($db,$q)or die('err'.mysqli_error($db));
$i=1;
while($row=mysqli_fetch_array($re1))
{
    $rep=mysqli_query($db,"select partname from partmaster where partcode='$row[item_code]' ");
$prow=mysqli_fetch_row($rep);
$apr=$row['net']/$row['qty'];
  print "<tr><td><input type=text name='partcode[]' value='$row[item_code]' size=12></td>";
  print "\n<td><input type=text name='partname[]' value='$prow[0]'></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='$apr' 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=$apr size=6></td>";
  print "<td><input type=text class=right name='hand[]' id='hand$i' value='0' size=3></td>";

  print "<td><input type=text class=right name='tax[]' id='tax$i' value='$row[taxp]' 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[gtot]' size=8></td>";

  print "\n</tr>";
 $i++;
}
print "<TR bgcolor='steelblue'><TD colspan=14 align=center> <input type=submit value='Save' name='sv'><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>ITEM - 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 purinvoice,pno from item_purchase_veh where brcode='$brcode' order by pno")or die('err'.mysqli_error($db));
while($row=mysqli_fetch_row($re))
{
 print "\n<option>$row[0]_$row[1]</option>";
}
/*
$re1=mysqli_query($db,"select distinct pbillno,purno from telcondb1.sparepurchase where brcode='$brcode' order by purno")or die('err2'.mysqli_error($db));
while($row=mysqli_fetch_row($re1))
{
 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>