<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
include("test.php");
include("util/exqty.php");

$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
?>
<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="includes/style1.css" type="text/css" />
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;

}
</style>
<style>
	th{background-color:steelblue;color:white}
	.right{text-align:right}
</style>
<? include("includes/top.php"); ?>
<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(isset($_POST[show]))
{
//include("calcsparestock.php");
$brn=$_REQUEST['brn'];
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";
}
//echo $sql;
$result=mysqli_query($db,$sql)or die(mysqli_error($db));

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()">
<input type=hidden name='to_branch' value='<?print $row1[0]?>'>
<table>
<tr><td>
Transfer 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>
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>
<?
print "<th colspan=15><h3>Ref No.<input type=text name='refno'  size=30></th>";
?>
</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>Rate<th>Branch <br>Stock<th>Minimum <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;

 $prres=mysqli_query($db,"select * from spareprmast where partcode='$row[partcode]' order by effectfrom desc");
 $prrow=mysqli_fetch_array($prres);

 $prres22=mysqli_query($db,"select qty from minstock where partcode='$row[partcode]' and brcode='$row1[0]'");
 $prrow22=mysqli_fetch_array($prres22);

$qtyy=exqty1($row[partcode],$row1[0],$user);

 if(mysqli_num_rows($prres)<=0)
 {
  print "<tr><td colspan=10 align=center><font color=red>$row[partcode] - Price could not be found1111</font></td></tr>";
}

 if($prrow[ndpcost]==0)
 {
  print "<tr><td colspan=10 align=center><font color=red>$row[partcode] - Price could not be found</font></td></tr>";
}

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>";
print "\n<td><center><input type=text name='qty[]' id='$i' class=right value='".($row[qty])."' size=4 ></td>";

print "\n<td><center><input type=text style='background-color:silver' name='rate[]'
value='".($row[rate])."' class=right size=4></td>";

 print "\n<td><center><input type=text readonly style='background-color:silver' name='currqty[]' value='$qtyy' class=right size=4></td>";
 print "\n<td><center><input type=text readonly style='background-color:silver' name='minqty[]' value='$prrow22[0]' 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?>' method='POST'>
<table border=0>
<tr><td>Branch</td><td>
<select name='brn'>
<?
 $brr=mysqli_query($db,"select brcode,brname from branch where brcode!='$brcode' order by brname asc");
 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>