<?
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
include("../util/exqty.php");
?>
<script>
 function validqty(x)
 {
   var qtyele=x;
   var stkele='stk'+x;
   var ordele='ordqty'+x;
   var ordq=parseFloat(document.getElementById(ordele).value);
   var supq=parseFloat(document.getElementById(qtyele).value);
   if(supq>ordq)
   { 
     alert('Cannot supply more than ordered');
      document.getElementById(qtyele).focus();
     document.getElementById(qtyele).value=0;
      document.getElementById(qtyele).focus();

     return false;

   }
   exstock=parseFloat(document.getElementById(stkele).value);
   if(parseFloat(document.getElementById(qtyele).value)>exstock)
   {
     alert('Insufficient Stock');
      document.getElementById(qtyele).focus();
     document.getElementById(qtyele).value=0;
      document.getElementById(qtyele).focus();

     return false;
   }
}
</script>

<title>Spare Transfer Out </title>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
	th{background-color:steelblue;color:white}
	.right{text-align:right}
</style>
<script>
 function valid()
 {
  if(document.f1.d.value.length<=0)
   {
     alert('Invalid Date');
	   return false;
   }
   if(confirm('Save ?'))
     {return true;}
   else
     {return false;}
 }
</script>

<center>
<h3>SPARE TRANSFER OUT</H3>
</CENTER>
<?
if($show)
{
//include("calcsparestock.php");
if($brn=='All')
{
$sql="select a.brcode,b.brname from purorder_spare a, branch b where a.orderto='$brcode' and a.brcode=b.brcode and a.qty>a.sup_qty and a.closed='N' group by a.brcode";
}
else
{
$sql="select a.brcode,b.brname from purorder_spare a, branch b where a.orderto='$brcode' and a.brcode=b.brcode and a.qty>a.sup_qty and a.brcode='$brn' and a.closed='N' group by a.brcode";
}

$result=mysqli_query($db,$sql)or die(mysqli_error());

while($row1=mysqli_fetch_array($result))
{
 print "<fieldset><legend>Sale Order From: <b>$row1[1]</b></legend>";
?>
<center>
<form name='f1' action='svsparetrout.php' method=post onSubmit="return valid()" target='n'>
<input type=hidden name='to_branch' value='<?print $row1[0]?>'>
<table>
<tr><td>
Transfer Date </td><td>: dd <input type=text name='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>
Entry Date </td><td>: dd <input type=text name='d1' value='<?print date("d")?>' size=2>
	 mm <input type=text name='m1' value='<?print date("m")?>' size=2>
	 yy <input type=text name='y1' value='<?print date("Y")?>' size=4>
</tr></td>
</table>
<br>
<table border=1 cellspacing=1 cellpadding=3 >
<th>No.<th>Part Code <th>Partname<th>Ord <br>No<th>Order <br>Qty<th>Sup <br>Qty
<th>ExQty <th>PSN <BR>Trans Qty
<th>Telcon <br>Stock
<th>Ord date
<?
$ss1="select distinct(partcode) from purorder_spare where brcode='$row1[0]' and orderto='$brcode' and qty>sup_qty and closed='N' order by orderno desc";
$ree=mysqli_query($db,$ss1);
$i=1;
//$tbl="sparetmp".$brcode.$user;
while($row2=mysqli_fetch_array($ree))
{


$s="select * from purorder_spare where brcode='$row1[0]' and orderto='$brcode' and qty>sup_qty and closed='N' and partcode='$row2[0]'order by orderno desc limit 1";
$re=mysqli_query($db,$s);

//$tbl="sparetmp".$brcode.$user;
while($row=mysqli_fetch_array($re))
{
/* $stkres=mysqli_query($db,"select supqty from $tbl where partcode='$row[partcode]'");
 $stkrow=mysqli_fetch_array($stkres);
 if($stkrow[0]<=0)
  $stk=0;
 else
  $stk=$stkrow[0];
*/
$ex_stk=exqty1($row[partcode],$brcode,$user);
 if($ex_stk<=0)
  $stk=0;
 else
  $stk=$ex_stk;
print "<tr><td>".$i."<td><input type=text readonly name='partcode[]' value='$row[partcode]' size=9> </td>";
print "\n<td><textarea name='partname[]' rows=1 readonly>$row[partname]</textarea></td>";
print "\n<td><center><input type=text class=right readonly name='pord_no[]' value='$row[orderno]' size=3></td>";
print "\n<td align=center><input type=text lass=right readonly id='ordqty$i' value='".($row[qty])."' size=4></td>";
print "\n<td align=center>".($row[sup_qty])."</td>";
print "<td><input type=text name='exqty' readonly size=4 id='stk$i' value='$stk'></td>";

print "\n<td><center><input type=text name='qty[]' id='$i' class=right size=4 onKeyUp='validqty(this.id)'></td>";
if($brcode=='ERA153')
 print "\n<td bgcolor='yellow'><center><input type=text readonly name='telcqty[]' class=right size=4 ></td>";
else
 print "\n<td><center><input type=text readonly style='background-color:silver' name='telcqty[]' class=right size=4></td>";

print "<td>".datetohtml($row[date])." </td>";
print "</tr>";
$i++;
}
}
print "<tr><th colspan=10><input type=submit value='Save' name='sv'> ".
"<input type=submit value='Preview' name='pv'> ";
print "</table>\n</form></fieldset>\n<br>";
}
}// endif show

?>
<center>

<form action='<?print $PHP_SELF?>'>
<table border=0>
<tr><td>Branch</td><td>
<select name='brn'>
<?
 $brr=mysqli_query($db,"select brcode,brname from branch order by brname");
 while($brrr=mysqli_fetch_array($brr))
  {
    print "<option value='$brrr[0]'>$brrr[1]</option>";
  }
 print "<option>All</option>";
?>
</select></td></tr>
<tr><td colspan=2 align=center bgcolor='steelblue'><input type=submit value='Show' name='show'></td></tr>
</table>
</form>


