<?
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");
//include ("calcsparestock.php");
$tbl="sparetmp".$brcode;


$re=mysqli_query($db,"select max(tr_no) from sparetransout where brcode='$brcode'");
$prow=mysqli_fetch_row($re);
if($prow[0]<=0)
 $tr_no=1;
else
 $tr_no=($prow[0]+1);

if(!checkdate($m,$d,$y))
 {
    print "<script> alert('Invalid Date'); document.location='transout.php'; </script>";
    return false;
 }
if(!checkdate($m1,$d1,$y1))
 {
    print "<script> alert('Invalid Entry Date'); document.location='transout.php';  </script>";
    return false;
 }
?>
<center>
<style>
th,td,body{font-family:'arial narrow';letter-spacing:1;font-size:16}
</style>
<?
$trdate=$y.'-'.$m.'-'.$d;
$entrydate=$y1.'-'.$m1.'-'.$d1;

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

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

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

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

$i=0;
foreach($telcqty as $a)
{
 $telcqty[$i]=$a; 
 $i++;
}
if($pv)
{
 $re=mysqli_query($db,"select brname from branch where brcode='$to_branch'")or die('err2'.mysqli_error());
 $rr=mysqli_fetch_row($re);
 $branch_na=$rr[0];
$c=1;
print "Transfer out  #$tr_no Preview<table border=1>";
print "<th>No<th>Part Code <th>Part Name<th>Qty<th>Telc Qty <th>To Branch<th>Amt <th>Location";
for($j=0;$j<$i;$j++)
{

	if($qty[$j]>0||$telcqty[$j]>0)
	{
  $sql="select location from sparelocmast where itemcode='$partcode[$j]' and brcode='$brcode'";
  $locre=mysqli_query($db,$sql)or die(mysqli_error());
  $locrow=mysqli_fetch_array($locre);

$amtres=mysqli_query($db,"select lstprice from spareprmast where partcode='$partcode[$j]' order by effectfrom desc")or die(mysqli_error());
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error());

 print "<tr><td>".$c++."</td><td>$partcode[$j]</td><td>$partname[$j]</td><td align=right>$qty[$j]";
 print "</td><td>$telcqty[$j]&nbsp;</td><td>$branch_na</td><td align=right>$amtrow[0]</td><td>$locrow[0]</td></tr>";
 $tq+=$qty[$j];
 $tamt+=$amtrow[0];
        }
}
 print "<tr><td colspan=3><center>Total</td><td align=right>$tq</td><td>&nbsp;</td><td>&nbsp;</td>";
 print "<td align=right>".putComa($tamt)."</td></tr>";
return;
} // end if prev

/*
for($j1=0;$j1<$i;$j1++)
{
if($qty[$j1]>0)
{
  $chstkre=mysqli_query($db,"select * from $tbl where partcode='$partcode[$j1]'");
  $chstkrow=mysqli_fetch_array($chstkre);
   if($qty[$j1]>$chstkrow[supqty])
    {
      print "Insufficient Quantity ".$partcode[$j1];
      return;
    }
}
}
*/
$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'")or die('err'.mysqli_error());
$jvrow=mysqli_fetch_array($jvres)or die('err1'.mysqli_error());
if($jvrow[0]>0)
 $jvno=($jvrow[0]+1);
else
 $jvno=1;


for($j=0;$j<$i;$j++)
{

	if($qty[$j]>0||$telcqty[$j]>0)
	{

$amtres=mysqli_query($db,"select lstprice from spareprmast where partcode='$partcode[$j]' order by effectfrom desc")or die(mysqli_error());
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error());
		$sql="insert into sparetransout (tr_no, trdate, entrydate, partcode, partname, 	
		pord_no, qty, to_branch, brcode,telcqty,amount,jvno) values ('$tr_no', '$trdate', '$entrydate', 	'$partcode[$j]', '$partname[$j]' , '$pord_no[$j]', '$qty[$j]','$to_branch', '$brcode','$telcqty[$j]','$amtrow[0]','$jvno')";
		mysqli_query($db,$sql) or die('Err sptrans'.mysqli_error());
		$sql="update purorder_spare set sup_qty=(sup_qty+".$qty[$j]."),sup_purno='$tr_no',sup_date='$trdate',trans_type='trans-out' where orderno='$pord_no[$j]' and partcode='$partcode[$j]' and brcode='$to_branch'";
//print $sql;

 		mysqli_query($db,$sql) or die('Err:'.mysqli_error());
      }
	if($telcqty[$j]>0)
	{
		$sql="insert into telconsparetrans values ('$tr_no', '$trdate', '$entrydate', 	'$partcode[$j]', '$partname[$j]' , '$pord_no[$j]', '$telcqty[$j]','$to_branch', '$brcode')";
		mysqli_query($db,$sql) or die(mysqli_error());

	}

}
 $re=mysqli_query($db,"select brname from branch where brcode='$to_branch'")or die('err2'.mysqli_error());
 $rr=mysqli_fetch_row($re);
 $branch_na=$rr[0];
//*************** posting
$sql="select sum(qty*amount) from sparetransout where tr_no='$tr_no' 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','Cr','Spare transfer out','0','$postrow[0]','Spare Transfer:$tr_no','$brcode','Spare trans out','Trans no','$tr_no','$tr_no')");

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

log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans out:$tr_no, Journal:$jvno");
?>
<script>
 document.location='../blank.html';
 window.open('prnsptrout.php?tr_no=<?print $tr_no?>');
</script>

