<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$frmbr=$_REQUEST['frmbr'];
$tr_no=$_REQUEST['tr_no'];
$grr=$_REQUEST['grr'];
date_default_timezone_set('Asia/Kolkata');

$logdt=date("Y-m-d");
$logtime=date("H:i:s");

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;


$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;

$gramt=$_REQUEST['gramt'];

print "<br>";


$amt=$_REQUEST['amt'];

//$gramt=round($gramt);

//$amt=round($amt);

$gramt=ceil($gramt);

$amt=ceil($amt);

print $gramt;
print $amt;

if($trdate=='0000-00-00')
{
 print "<script> alert('Invalid GRN Date'); </script>";
 return;
}

if($grr=='0.00')
{
 print "<script> alert('Invalid GRN No'); </script>";
 return;
}

if($gramt!=$amt)
{
 print "<script> alert('GRN Amount not matching'); </script>";
 return;
}
else
{
    print "<script> alert('GRN Amount matching with zme80fn '); </script>";
// return;
}


    $r=mysqli_query($db,"select * from journal2 where jvno='$row[jvno]' and brcode='$frmbr' ")or die('err'.mysqli_error($db));
    $rw=mysqli_fetch_array($r);
    
      $rt=mysqli_query($db,"select partic from journal2 where jvno='$row[jvno]' and brcode='$frmbr' and  type='Dr' ")or die('err'.mysqli_error($db));
    $rwt=mysqli_fetch_array($rt);
    $brledger1=$rwt[0];
    
    $sq="update journal2 set against='done',tr_no='$jvno' where brcode='$frmbr' and jvno='$row[jvno]' and partic='$brledger1' ";

mysqli_query($db,$sq) or die('advcoll err:'.mysqli_error($db));



$res=mysqli_query($db,"select * from sparetransout where tr_no='$tr_no' and to_branch='$brcode' and brcode='$frmbr' and trfrtype='agt' and jvno > 0")or die('err'.mysqli_error($db));

while($row=mysqli_fetch_array($res))
{
    
   
 $totqty=$row['qty']+$row['telcqty'];
 $partname1=addslashes($row[4]);
$sql="insert into sparetransin values('$tr_no1','$trdate','$entrydate','$row[3]','$partname1','$row[pord_no]','$totqty','$frmbr','$brcode','$row[amount]','$jvno','$grr','$row[saprefno]','$row[taxamt]','$row[aprice]','$row[taxp]','$row[gtot]','agt','0')";
print $sql;
 mysqli_query($db,$sql)or die('err2'.mysqli_error($db));
}
 $sql="update sparetransout set received='Y' where tr_no='$tr_no' and to_branch='$brcode' and brcode='$frmbr' and jvno > 0  and trfrtype='agt'";
 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];

  

print $sql="select sum(aprice),saprefno,sum(taxamt),taxp,sum(gtot) from sparetransin where tr_no='$tr_no1' and brcode='$brcode' and trfrtype='agt' group by taxp";
$postres=mysqli_query($db,$sql) or die('post err'.mysqli_error($db));

    while($postrow=mysqli_fetch_array($postres))
{
  $ledger1='STOCK TRANSFER IN IGST '.$postrow[3];
  $ledger2='INPUT IGST STOCK TRANSFER IN '.$postrow[3];
  
  if($postrow[2]<=0)
  {
  
mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$entrydate','$jvno','Dr','STOCK TRANSFER IN','$postrow[0]','0','Spare Transfer:$tr_no1-$postrow[1]:grno:$grr','$brcode','Spare trans in agt','Trans no','$postrow[1]','$grr','$logtime $user','$logdt')");

}
else
{
  mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$entrydate','$jvno','Dr','$ledger1','$postrow[0]','0','Spare Transfer:$tr_no1-$postrow[1]:grno:$grr','$brcode','Spare trans in agt','Trans no','$postrow[1]','$grr','$logtime $user','$logdt')");  
  
  
    mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$entrydate','$jvno','Dr','$ledger2','$postrow[2]','0','Spare Transfer:$tr_no1-$postrow[1]:grno:$grr','$brcode','Spare trans in agt','Trans no','$postrow[1]','$grr','$logtime $user','$logdt')");  
}


  mysqli_query($db,"insert into journal2(date, jvno, type, partic, dramt, cramt, narr, brcode, j_type, against, no, tr_no,prftcntr,docdt) values('$entrydate','$jvno','Cr','$branch_na','0','$postrow[4]','Spare Transfer:$tr_no1-$postrow[1]:grno:$grr','$brcode','Spare trans in agt','done','$postrow[1]','$grr','$logtime $user','$logdt')");
  
  
    	

}


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-$postrow[1], Journal:$jvno",$db);

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