<?
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;
}
$savecount=$_REQUEST['savecount'];

if($savecount==0)
{
$partcode =$_REQUEST['partcode'];
$partname =$_REQUEST['partname'];
$pord_no =$_REQUEST['pord_no'];
$qty =$_REQUEST['qty'];
$telcqty =$_REQUEST['telcqty'];
$dsoqty =$_REQUEST['dsoqty'];

$m=$_REQUEST['m'];
$d=$_REQUEST['d'];
$y =$_REQUEST['y'];

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

$qty=explode('||',$qty);
$partcode=explode('||',$partcode);
$partname =explode('||',$partname);
$pord_no =explode('||',$pord_no);
$telcqty =explode('||',$telcqty);
$dsoqty =explode('||',$dsoqty);



include("../opendb.php");
include("../test.php");
include("../util/exqty.php");

//include ("calcsparestock.php");
$tbl="sparetmp".$brcode;
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
/*
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;
 }
 */
?>
<link href="../css/printline.css" type="text/css" rel="stylesheet" media="print,screen"  />

<center>
<style>
th,td,body{font-family:'arial narrow';letter-spacing:1;font-size:16}
</style>
<?
$trdate=$_REQUEST['trdate'];
$entrydate=$_REQUEST['entrydate'];
//$entrydate=$y1.'-'.$m1.'-'.$d1;
//print_r($qty);


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

$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($telcqty as $a)
{
 $telcqty[$i]=$a; 
 $i++;
}

$i=0;
//print_r($qty);
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}
$to_branch=$_REQUEST['to_branch'];
$pv=$_REQUEST['pv'];
print $i;
if($pv==1)
{				
 	$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 [Pick List]<table border=1>";
	print "<th>No<th>Part Code <th>Part Name<th>Qty <th>To Branch<th>MRP Value <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($db));
  			$locrow=mysqli_fetch_array($locre);
			if($kar[0]=='Kerala') 
			{
				$amtres=mysqli_query($db,"select ndpcost,lstprice from spareprmast where partcode='$partcode[$j]' and brcode='ERA153' order by effectfrom desc")or die(mysqli_error($db));
				$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error($db));
				$m_mrp=round($amtrow[1]*114.5/100);
			}
			else
			{
				$amtres=mysqli_query($db,"select ndpcost,lstprice from spareprmast where partcode='$partcode[$j]' and brcode='BAN682' order by effectfrom desc")or die(mysqli_error($db));
				$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error($db));
				$prres1=mysqli_query($db,"select kar_tax_sales from partmaster where partcode='$partcode[$j]' ");
				$prrow1=mysqli_fetch_row($prres1);
				$m_mrp=$amtrow[1]*(100+$prrow1[0])/100;
				$m_mrp=round($m_mrp);
			}
			print "<tr><td>".$c++."</td><td>$partcode[$j]</td><td>$partname[$j]</td><td align=right>$qty[$j]";
			print "</td><td>$branch_na</td><td align=right>$m_mrp</td><td>$locrow[0]</td></tr>";
			$tq+=$qty[$j];
			$tamt+=$m_mrp;
		}
	}
	print "<tr><td colspan=3><center>Total</td><td align=right>$tq</td>";
	print "" ?><td align=center colspan=3>[Printed by <?print $user?> on <?print date("d-m-Y  h:i:s a")?>]</td><?"</tr>";
	print "</table>";
	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;
    }
}
}
*/
//for($h=0;$h<$i;$h++)
//{
//$exstk=exqty1($partcode[$h],$brcode,$user);
//if($qty[$h]>0)
//{
 //if($exstk<=0)
//{ 
// print "insuf.stock";

//}
//}
//}



/*
$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);
*/
for($j=0;$j<$i;$j++)
//print $j;

{
//echo $j."-".$qty[$j]."asfas";
//echo "<br>";
//exit;

	if($qty[$j]>0||$telcqty[$j]>0)
	{
	
$exstk=exqty1($partcode[$j],$brcode,$user);
if($exstk>0)
{
if($kar[0]=='Kerala') 
{
$amtres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$partcode[$j]'  and brcode='ERA153' order by effectfrom desc")or die(mysqli_error($db));
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error($db));
}
else
{
$amtres=mysqli_query($db,"select ndpcost from spareprmast where partcode='$partcode[$j]'  and brcode='BAN682' order by effectfrom desc")or die(mysqli_error($db));
$amtrow=mysqli_fetch_row($amtres)or die(mysqli_error($db));

}
if($j==0)
{
$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);
$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;
}
//print $j;
return;
if($tr_no>0)
{
		$mpartname = addslashes($partname[$j]);
		$sql="insert into sparetransout (tr_no, trdate, entrydate, partcode, partname, 	
		pord_no, qty, to_branch, brcode,telcqty,amount,jvno,saprefno) values ('$tr_no', '$trdate', '$entrydate', '$partcode[$j]','$mpartname' ,'$pord_no[$j]','$qty[$j]','$to_branch','$brcode','0.00','$amtrow[0]','$jvno','111')";
		//echo $sql;
		//echo "<br>";
		mysqli_query($db,$sql) or die('Err sptrans'.mysqli_error($db));
		$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'";

//echo $sql;
		//echo "<br>";
 		mysqli_query($db,$sql) or die('Err:'.mysqli_error($db));
}
else
{
   echo "<script>alert('Trans no missing'); document.location='transout.php?brn=$to_branch&&shw=1';</script>";
}
      }
if($tr_no>0)
{
	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($db));

	}
}

}
}
 $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];
//*************** 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($db));

$postrow=mysqli_fetch_array($postres);
if($tr_no>0)
{
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')");

print "<script> window.open('prnsptrout.php?tr_no=$tr_no')</script>";

log_entry($brcode,$user,$REMOTE_ADDR,"sparetrans out:$tr_no, Journal:$jvno");
$savecount=1;
echo "<script>alert('Transfer out has been saved'); document.location='transout.php';</script>";
}
else
{
   echo "<script>alert('Trans no missing'); document.location='transout.php?brn=$to_branch&&shw=1';</script>";
}
?>


<?
}
else
{
echo "Duplication";
 echo "<script> document.location='transout.php';</script>";
  
}