<?
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");
if($sv)
{
$re=mysqli_query($db,"select max(tr_no) from sparetransout where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $tr_no=1;
else
 $tr_no=($prow[0]+1);


if(!checkdate($m,$d,$y))
 {
    print "<script> alert('Invalid Date'); document.location='autotrans.php'; </script>";
    return false;
 }
if(!checkdate($m1,$d1,$y1))
 {
    print "<script> alert('Invalid Entry Date'); document.location='autotrans.php';  </script>";
    return false;
 }

$trdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;

$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 
 $i++;
}

$i=0;
foreach($partname as $a)
{
 $partname1[$i]=$a; 
 $i++;
}

$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}

$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'")or die('err'.mysqli_error());
$jvrow=mysqli_fetch_array($jvres)or die('err1'.mysqli_error());
if($jvrow[0]>0)
 $jvno=($jvrow[0]+1);
else
 $jvno=1;


for($j=0;$j<$i;$j++)
{

	if($qty[$j]>0)
	{

$amtres=mysqli_query($db,"select lstprice from spareprmast where partcode='$partcode[$j]' order by effectfrom desc")or die(mysqli_error());
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error());
		$sql="insert into sparetransout (tr_no, trdate, entrydate, partcode, partname, 	
		pord_no, qty, to_branch, brcode,telcqty,amount,jvno) values ('$tr_no', '$trdate', '$entrydate', 	'$partcode[$j]', '$partname[$j]' , '$pord_no[$j]', '$qty[$j]','$to_branch', '$brcode','$telcqty[$j]','$amtrow[0]','$jvno')";
		mysqli_query($db,$sql) or die('Err sptrans'.mysqli_error());
      }

}
 $re=mysqli_query($db,"select brname from branch where brcode='$to_branch'")or die('err2'.mysqli_error());
 $rr=mysqli_fetch_row($re);
 $branch_na=$rr[0];
//*************** posting
$sql="select sum(qty*amount) from sparetransout where tr_no='$tr_no' and brcode='$brcode'";
$postres=mysqli_query($db,$sql) or die('post err'.mysqli_error());

$postrow=mysqli_fetch_array($postres);
mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Cr','Spare transfer out','0','$postrow[0]','Spare Transfer:$tr_no','$brcode','Spare trans out','Trans no','$tr_no','$tr_no')");

mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Dr','$branch_na','$postrow[0]','0','Spare Transfer out:$tr_no','$brcode','Spare trans out','Trans no','$tr_no','$tr_no')");

log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans out:$tr_no, Journal:$jvno");
?>
<script>
 window.open('prnsptrout.php?tr_no=<?print $tr_no?>');
</script>
<?
print "<center><input type=button value='Done, Close this window' onClick=window.close()>";
return;
}// end if sv
include ("calcsparestock.php");
?>
<script>
function valid()
 {
   if(document.f1.d.value.length<=0||parseFloat(document.f1.d.value)>31)
   {
     alert('Invalid Date');
     document.f1.d.focus();
     return false;
    }
    if(confirm('Do you want to save?'))
     return true;
    else
     return false;
 }
function chkqty(x)
{
 var q='qty'+x;
 exqty=parseFloat(document.getElementById(x).value);
 qty=parseFloat(document.getElementById(q).value);
 if(qty>exqty)
 {
  alert('Invalid Quantity');
  document.getElementById(q).value=0;
  document.getElementById(q).focus();
 }
}
</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
th{background-color:lightsteelblue;color:black}
</style>
<center>
<form  name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table width=100%>
<th STYLE='HEIGHT:29'>TRANSFER TO ERANAKULAM</TH>
</TABLE>

<input type=hidden name='to_branch' value='ERA153'>
<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>Ex <br>Qty<TH>Price <th>Trans <br>Qty
<?
$tbl="sparetmp".$brcode.$user;

$re=mysqli_query($db,"select * from $tbl where supqty>0 order by partcode");
$i=1;
while($row=mysqli_fetch_array($re))
{
print "\n<tr><td>".$i."</td><td><input type=text name='partcode[]' value='$row[partcode]'></td>";
print "<td><textarea name='partname[]' rows=1>$row[partname]</textarea></td>";
print "<td><input type=text name='exqty' id='$i' size=5 readonly value='$row[supqty]' style='text-align:right'></td>";
  $prres=mysqli_query($db,"select lstprice from spareprmast where partcode='$row[0]' order by effectfrom desc limit 1")or die(mysqli_error());
  $prrow=mysqli_fetch_row($prres);
print "<td align=right>$prrow[0]</td>";

print "<td><input type=text name='qty[]' id='qty$i' size=5 style='text-align:right' onChange='chkqty($i)' value='$row[supqty]'></td>";
print "</tr>";
$i++;
}
?>
<tr><th colspan=6>
<input type=submit value='Transter Items' name='sv'>
<input type=button value='Cancel' onClick=window.close()>
</th></tr>
</table>
</form>
