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

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

$cntres=mysqli_query($db,"select max(cntr) from sparetrinwar where brcode='$brcode'");
$cntrow=mysqli_fetch_row($cntres);
if($cntrow[0]<=0)
 $cnt=1;
else
 $cnt=($cntrow[0]+1);

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

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

$i=0;
foreach($stono as $a)
{
 if(strlen($a)<=0)
  $a=0;
 $stono[$i]=$a; 
 $i++;
}


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

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

$i=0;
foreach($invdate as $a)
{
 $invdate[$i]=parsedate($a); 
 $i++;
}
$i=0;
foreach($stat as $a)
{
 $stat[$i]=($a); 
 $i++;
}
$i=0;
foreach($psnqty as $a)
{
 $psnqty[$i]=($a); 
 $i++;
}
$i=0;
foreach($recqty as $a)
{
 $recqty[$i]=($a); 
 $i++;
}
$i=0;
foreach($servord as $a)
{
 $servord[$i]=($a); 
 $i++;
}

$dt=date("Y-m-d");
$entrydate=$ey.'-'.$em.'-'.$ed;
for($j=0;$j<$i;$j++)
{
$amtres=mysqli_query($db,"select price from warsparetransout where partcode='$partcode[$j]' and warno='$warno' and brcode='$brcde'");
$amtrow=mysqli_fetch_row($amtres);
 $amt=($amtrow[0]*$recqty[$j]);
	  $sql="insert into sparetrinwar values('$warno','$partcode[$j]','$partname[$j]','$preqno[$j]','$invno[$j]','$invdate[$j]','$frombr','$user','$dt','$brcode','$msno','$stono[$j]','$psnqty[$j]','$amt','$recqty[$j]','$cnt','$cust','$servord[$j]','$jvno')";

          mysqli_query($db,$sql) or die(mysqli_error());
	$sql="update warsparetransout set preqno='$preqno[$j]',invno='$invno[$j]',invdate='$invdate[$j]',stat='$stat[$j]',stono='$stono[$j]' where warno='$warno' and partcode='$partcode[$j]'";
       mysqli_query($db,$sql)or die(mysqli_error());
}
//*************** posting

$sql="select sum(amount) from sparetrinwar where warno='$warno' and brcode='$brcode' and cntr='$cnt'";
$postres=mysqli_query($db,$sql) or die('post err'.mysqli_error());
$postrow=mysqli_fetch_array($postres);
if($postrow[0]>0)
{
mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Dr','Spare transfer in(WAR)','$postrow[0]','0','Spare Transfer:$warno','$brcode','Spare trans in(WAR)','Trans no','$warno','$warno')");

mysqli_query($db,"insert into journal2 values('$entrydate','$jvno','Cr','$frombr','0','$postrow[0]','Spare Transfer(WAR):$warno','$brcode','Spare trans in(WAR)','Trans no','$warno','$warno')");
log_entry($brcode,$user,$REMOTE_ADDR,"spareTrans in war:$warno, Journal:$jvno");
}
?>
<center>
<input type=button value='Done. Close this window' onClick=window.close()>
