<?
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");

$date=$y.'-'.$m.'-'.$d;
$failon=$y1.'-'.$m1.'-'.$d1;
$transdate=$y2.'-'.$m2.'-'.$d2;
$invdate=$y3.'-'.$m3.'-'.$d3;

$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
$jvrow=mysqli_fetch_array($jvres);
$jvno=($jvrow[0]+1);

$tbl="spareinvwar".$brcode;
$re=mysqli_query($db,"select * from $tbl where warno='$warno'");
if(mysqli_num_rows($re)<=0)
{
 print "<script> alert('Invalid Entry-Items could not be found'); history.back(1); </script>";
 return ;
}

if(mysqli_num_rows($re)>0)
{
 while($row=mysqli_fetch_array($re))
 {
 $sql="insert into warsparetransout values('$row[warno]','$row[partcode]', '$row[partname]', '$row[psnqty]' , '$row[telcqty]','$row[price]' , '$tobr', '$date', '$cust', '$msno', '$failon', '$hmr', '$flr', '$transp','$transdate','$sendto', '$finfo', '$cssno','$servord', '$stono', '$preqno','$invno','$invdate', '$stat', '$brcode','$notifno')";
 mysqli_query($db,$sql);
 }
}

//*************** posting
 $re=mysqli_query($db,"select brname from branch where brcode='$tobr'")or die('err2'.mysqli_error());
 $rr=mysqli_fetch_row($re);
 $branch_na=$rr[0];

$sql="select sum(price)*sum(psnqty) from warsparetransout where warno='$warno' 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('$invdate','$jvno','Cr','Spare transfer out (WAR)','0','$postrow[0]','Spare Transfer out WAR:$warno','$brcode','Spare trans out WAR','Trans no','$warno','$warno')");

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

log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans out WAR:$warno, Journal:$jvno");


mysqli_query($db,"update $tbl set trans='y' where warno='$warno'");
print "<font color=green> Data has been saved</font>";
$warno='x';
log_entry($brcode,$user,$REMOTE_ADDR,"War sparetrans out:$warno");
?>
<script> parent.window.close();</script>
