<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];

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

include("../opendb.php");
include("../test.php");
//include ("calcsparestock.php");
$tbl="sparetmp".$brcode;


$re=mysqli_query($db,"select max(tr_no) from sparetransoutexcess 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;
 }
 $saprefno=$_REQUEST['saprefno'];
 if($saprefno=='')
 {
     print "<script> alert('Invalid Sap No'); 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;

$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$pord_no=$_REQUEST['pord_no'];
$qty =$_REQUEST['qty'];
$price =$_REQUEST['price'];

$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($price as $a)
{
 $price[$i]=$a; 
 $i++;
}
$pv=$_REQUEST['pv'];
$to_branch=$_REQUEST['to_branch'];
if($pv)
{
 $re=mysqli_query($db,"select brname from branch where brcode='$to_branch'")or die('err2'.mysqli_error($db));
 $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||$price[$j]>0)
	{
  $sql="select location from sparelocmast where itemcode='$partcode[$j]' and brcode='$brcode'";
  $locre=mysqli_query($db,$sql);
  $locrow=mysqli_fetch_array($locre);

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

 print "<tr><td>".$c++."</td><td>$partcode[$j]</td><td>$partname[$j]</td><td align=right>$qty[$j]";
 print "</td><td>$price[$j]&nbsp;</td><td>$branch_na</td><td align=right>$mndp</td><td>$locrow[0]</td></tr>";
 $tq+=$qty[$j];
 $tamt+=$mndp;
        }
}
 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


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


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

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


$rec=mysqli_query($db,"select partcode from sparetransoutexcess where brcode='$brcode' and partcode='$partcode[$j]' and saprefno='$saprefno' ");
$prowc=mysqli_fetch_row($rec);

if($prowc[0]>0) 
{
  print "<script> alert('Duplicate partcode for same chasis');   </script>"; 
  $sqdel="delete from sparetransoutexcess where brcode='$brcode' and tr_no='$tr_no' and jvno='$jvno'";
  	mysqli_query($db,$sqdel) or die('Err:'.mysqli_error($db));
  	return;
}
		$sql1="insert into sparetransoutexcess (tr_no, trdate, entrydate, partcode, partname, 	
		pord_no, qty, to_branch, brcode,amount,jvno,saprefno,grdate) values ('$tr_no', '$trdate', '$entrydate', '$partcode[$j]', '$partname[$j]' , '$pord_no[$j]', '$qty[$j]','$to_branch', '$brcode','$price[$j]','$jvno','$saprefno','0000-00-00')";
		mysqli_query($db,$sql1) or die('Err sptrans'.mysqli_error($db));
		
		$sql2="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,$sql2) or die('Err:'.mysqli_error($db));
     }
	

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

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

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

