<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$frmbr=$_REQUEST['frmbr'];
$tr_no=$_REQUEST['tr_no'];
print 'from branch='.$frmbr.' trans no='.$tr_no;

if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
$sq="select jvno from sparetransinexcess where brcode='$brcode' and troutno='$tr_no' and from_branch='$frmbr'";
$chkre=mysqli_query($db,$sq);
$chkrow=mysqli_fetch_array($chkre);
if($chkrow[0]>0)
{
 print "<script> alert('Duplicate posting'); </script>";
 return;
}
$re=mysqli_query($db,"select max(tr_no) from sparetransinexcess where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $tr_no1=1;
else
 $tr_no1=($prow[0]+1);

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


$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];

$d1=$_REQUEST['d1'];
$m1=$_REQUEST['m1'];
$y1=$_REQUEST['y1'];

$trdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;
$res=mysqli_query($db,"select * from sparetransoutexcess where tr_no='$tr_no' and to_branch='$brcode' and brcode='$frmbr'")or die('err'.mysqli_error($db));

while($row=mysqli_fetch_array($res))
{
 $totqty=$row['qty']+$row['telcqty'];
 
$sql="insert into sparetransinexcess values('$tr_no1','$trdate','$entrydate','$row[3]','$row[4]','$row[pord_no]','$totqty','$frmbr','$brcode','$row[amount]','$jvno','$tr_no','$row[saprefno]','0','0','0','0','$row[worktype]')";

 mysqli_query($db,$sql)or die('err2'.mysqli_error($db));
}
 $sql="update sparetransoutexcess set received='Y' where tr_no='$tr_no' and to_branch='$brcode' and brcode='$frmbr'";
 mysqli_query($db,$sql)or die('err3'.mysqli_error($db));

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

$sql="select sum(qty*amount) from sparetransinexcess where tr_no='$tr_no1' and brcode='$brcode'";
$postres=mysqli_query($db,$sql) or die('post err'.mysqli_error($db));
$postrow=mysqli_fetch_array($postres);

mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Dr','STOCK TRANSFER IN ACCESSORIES','$postrow[0]','0','Spare Transfer:$tr_no1','$brcode','acs trans in','Trans no','$tr_no1','$tr_no1')");

mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no) values('$entrydate','$jvno','Cr','$branch_na','0','$postrow[0]','Spare Transfer:$tr_no1','$brcode','acs trans in','Trans no','$tr_no1','$tr_no1')");

print "<script> window.open('prntransin.php?trno=$tr_no1')</script>";
$ip_address = "$jvno|Spare Trans In|Add";
log_entry($brcode,$user,$ip_address,"sparetrans in:$tr_no1, Journal:$jvno",$db);

?>
<script> alert('Transfer in has been saved'); 
 document.location='transinexcess.php'
</script>
