<?
session_start();
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 sparetransin 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 sparetransin 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;

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

while($row=mysqli_fetch_array($res))
{
 $totqty=$row[qty]+$row[telcqty];
 
$sql="insert into sparetransin values('$tr_no1','$trdate','$entrydate','$row[3]','$row[4]','$row[pord_no]','$totqty','$frmbr','$brcode','$row[amount]','$jvno','$tr_no')";
 mysqli_query($db,$sql)or die('err2'.mysqli_error());
}
 $sql="update productiondb.sparetransout set received='Y' where tr_no='$tr_no' and to_branch='$brcode' and brcode='$frmbr'";
 mysqli_query($db,$sql)or die('err3'.mysqli_error());

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

$sql="select sum(qty*amount) from sparetransin where tr_no='$tr_no1' 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','Dr','Spare transfer in','$postrow[0]','0','Spare Transfer:$tr_no1','$brcode','Spare trans in','Trans no','$tr_no1','$tr_no1')");

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

print "<script> window.open('prntransin.php?trno=$tr_no1')</script>";
log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans in:$tr_no1, Journal:$jvno");

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