<html xmlns="http://www.w3.org/1999/xhtml">
<?php 

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");

mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

if ($transfer_id!='')
	{
		$UpdCost = "update temp_transfer set costcenter_code = '".$costcent."',tocostcenter_code  =  '".$tocostcent."' where transfer_id = ".$transfer_id;
		$result=mysql_query($UpdCost,$connection) or die(mysql_error());					
	}

$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


$transferfromwarehouse = $fromwarehouse;
$transfertowarehouse = $towarehouse;


if ($towarehouse!='')
$towarehouse1= $fromwarehouse;

if ($loadfromwarehouse=='')
$loadfromwarehouse = $fromwarehouse;
//echo $towarehouse1;

$query="select warehouse_code from ".$dbLabel.".loginstk where user_account = '".$HTTP_SESSION_VARS['useraccount']."'";
$result=mysql_query($query,$connection)  or die(mysql_error());;
$warehouserow=mysql_fetch_array($result);
$user_warehousecode = $warehouserow[0];
//select transferdt.item_code,items.item_description,transferdt_quantity from transferdt,transfer,items


//Read From Module Table
$querymodule=" Select module_serialno  From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

	
if ($transfer_id=='' )
				{
					$PTransferId = get_nextsequence('transfer_id','',$connection);	

											$querylog=" select ifNull(FI_Transfer,0) As FI_Transfer ,login_belongstogrp ".
								  " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
						$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
						while($rows=mysql_fetch_array($resultlog)){
						extract($rows);				
						}
					
					
					$query = "insert into temp_transfer(Transfer_Id,Transfer_Date,Transfer_Remark,Project_code1,Project_code2,Transfer_WarehouseFrom,Transfer_WarehouseTo,Driver_Name,Vehicle_No,Logged_Code,costcenter_code,Tocostcenter_code,Transfer_BelongsToGroup,user_account,Document_RefNo,Transfer_Type)
								values
							(".$PTransferId."
							,'".displayParadoxDate(insertDate($transfer_date))."','".$transfer_remark."','".$fromproject."','".$toproject."','".$fromwarehouse."','".$towarehouse."','".$driver_name."','".$vehicle_no."','".$HTTP_SESSION_VARS['useraccount']."','".$costcent."','".$tocostcent."',".$login_belongstogrp.",'".$HTTP_SESSION_VARS['useraccount']."','".$documentrefno."','".$TransType."')";
				
					//echo $query;
					
					$result=mysql_query($query,$connection) or die(mysql_error());
						if ($result)
							$transfer_id = $PTransferId;
				}
				
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" language="javascript">

function openitempage(transferid,TransType){
//submitpage();
var fromwarehouse= document.loaditems.transferfromwarehouse.value;
var towarehouse= document.loaditems.transfertowarehouse.value;
var toproject = document.loaditems.toproject.value;
var fromproject = document.loaditems.fromproject.value;
var transfer_date = document.loaditems.transfer_date.value;
var costcent = document.loaditems.costcent.value;
var tocostcent = document.loaditems.tocostcent.value;


var td = document.loaditems.td.value;
if (td=='')
td=0;
	fromwarehouse = fromwarehouse.replace('&','%26');
	towarehouse = towarehouse.replace('&','%26');


	str1= 'Form_TransferItems.php?item=1&transfer_id='+transferid+'&fromwarehouse='+fromwarehouse+'&towarehouse='+towarehouse+'&fromproject='+fromproject+'&toproject='+toproject+"&costcent="+costcent+"&tocostcent="+tocostcent+'&process=load'+'&td='+td+'&transfer_date='+transfer_date+'&TransType='+TransType;
	  width = screen.width - 200;
	  height = screen.height - 250;
	  window.open(str1,"", "width=" + width + ",height=" + height + ",screenX=0,left=150,screenY=0,top=0,status=no,menubar=no,scrollbars=yes,resizable=yes");

}
function ClearForm()
{
document.loaditems.submit();
}
function checkwarehouse(){
var transferwarehousefrom = document.loaditems.transferfromwarehouse.value;
var transferwarehouseto = document.loaditems.transfertowarehouse.value;
var loadwarehousefrom = document.loaditems.loadfromwarehouse.value;
var x = document.loaditems;

if ((loadwarehousefrom != transferwarehousefrom) && (loadwarehousefrom != transferwarehouseto))
	{
		alert('Warehouse must be warehouse from or to of the current transfer');
		x.loadfromwarehouse.focus();
	}	
else
document.loaditems.submit();	

}

function addserialcode(delfromload,submitbutton,TransType){

var fromwarehouse= document.loaditems.fromwarehouse.value;
var towarehouse= document.loaditems.towarehouse.value;

var fromproject= document.loaditems.fromproject.value;
var toproject= document.loaditems.toproject.value;
var actionmode= document.loaditems.actionmode.value;

var transfer_id= document.loaditems.transfer_id.value;
var transfer_date= document.loaditems.transfer_date.value;
var driver_name = document.loaditems.driver_name.value;
var vehicle_no = document.loaditems.vehicle_no.value;
var costcent = document.loaditems.costcent.value;
var tocostcent = document.loaditems.tocostcent.value;
	
	
	fromwarehouse = fromwarehouse.replace('&','%26');
	towarehouse = towarehouse.replace('&','%26');

if ((event.keyCode == 13) || (submitbutton==1))
{
document.loaditems.action="Form_loaditems.php?transfer_id="+transfer_id+"&costcent="+costcent+"&tocostcent="+tocostcent+"&fromwarehouse="+fromwarehouse+"&towarehouse="+towarehouse+"&actionmode=insert"+"&delfromload=insert&fromproject="+fromproject+"&toproject="+toproject+"&actionmodeload=insert&driver_name="+driver_name+"&vehicle_no="+vehicle_no+"&transfer_date="+transfer_date+"&TransType="+TransType;
document.loaditems.submit();
}

}

function removeitem(transferdtid){
  str1= 'd_fromtemp.php?transferdtid='+transferdtid;
  width = screen.width - 999;
  height = screen.height - 999;
  window.open(str1,"", "width=" + width + ",height=" + height + ",screenX=0,left=250,screenY=0,top=0,status=no,menubar=no,scrollbars=yes,resizable=yes");
	document.loaditems.showitems.click();
 }

function selecttransfer(checkboxname,transfer,id){
if (checkboxname.checked==true)
transfer.value=1;
else
transfer.value=0;

if (checkboxname.checked==true)
document.loaditems.selectedtransfer.value=id;
else
document.loaditems.selectedtransfer.value='';
}

function submitpage(){

document.loaditems.submit();
}

function confirmtransfer(transfer_id,ref,date,costcent,tocostcent,fromwarehouse,towarehouse,actionmode,delfromload,fromproject,toproject,driver_name,vehicle_no)
{

var LoadItemsForm=document.loaditems.selectedtransfer.value;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
				{ 	
						var data = xmlhttp.responseText.split("[BRK]");
						fromwarehouse = fromwarehouse.replace('&','%26');
						towarehouse = towarehouse.replace('&','%26');

						
						opener.document.transfer.transfer_id.value=transfer_id;
						opener.document.transfer.actionmode.value='submitrecord';
						opener.document.transfer.submit();
						self.close();
						

				}
		}
  }

xmlhttp.open("GET","Form_Transfer_Actions.php?LoadItemsForm="+LoadItemsForm+"&transfer_id="+transfer_id+"&fromload=1&actionmode=addtempdt",true);
xmlhttp.send();




}


</script>
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>
<title>TRANSFER</title>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
HERE;
if ($module_serialno==1)
	echo "<body  OnLoad=\"javascript:document.getElementById('serialno').focus()\">";
else
	echo "<body>";

print <<<HERE
<form name='loaditems' method='post'>
<input type='hidden' name='towarehouse1' value='$towarehouse1'>
<input type='hidden' name='fromproject' value='$fromproject'>
<input type='hidden' name='toproject' value='$toproject'>
<input type='hidden' name='transfertowarehouse' value='$transfertowarehouse'>
<input type='hidden' name='transferfromwarehouse' value='$transferfromwarehouse'>

<input type='hidden' name='fromwarehouse' value='$fromwarehouse'>
<input type='hidden' name='towarehouse' value='$towarehouse'>


<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='transfer_id' value='$transfer_id'>
<input type='hidden' name='transfer_remark' value='$transfer_remark'>
<input type='hidden' name='transfer_date' value='$transfer_date'>
<input type='hidden' name='driver_name' value='$driver_name'>
<input type='hidden' name='vehicle_no' value='$vehicle_no'>
<input type='hidden' name='costcent' value='$costcent'>
<input type='hidden' name='tocostcent' value='$tocostcent'>
<input type='hidden' name='selectedtransfer' id='selectedtransfer' value='$selectedtransfer'>

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><th height="15" colspan="9" align="left" valign="top"><span class="style3"> </span></th></tr>
<tr><th height="15" colspan="9" align="left" valign="top"><span class="style3">Load </span></th></tr>


HERE;

$query="select q1.transfer_id,
transfer.Transfer_Reference,date_format(transfer.Transfer_Date,'%d-%m-%Y'),
transfer.Transfer_WarehouseFrom   , sum(q1.diff) , transfer.Transfer_WarehouseTo
from(
select a.transfer_id,a.item_code,a.TransferDt_Quantity,sum(ifnull(b.transferdt_quantity,0)),a.TransferDt_Quantity- sum(ifnull(b.transferdt_quantity,0)) diff
from transferdt a left join transferdt b
on a.transferdt_id=b.relateddt_id
group by a.transfer_id , a.item_code,a.TransferDt_Quantity) q1 , transfer
where transfer.transfer_id = q1.transfer_id
and Transfer_WarehouseFrom <> ''";

//if ($user_warehousecode!='')
//$query= $query . " and transfer_warehouseto = '".$user_warehousecode."'";
if ($towarehouse!='' and $reference=='')
$query= $query . " and transfer_warehouseto = '".$loadfromwarehouse."'";

if ($reference!='' )
$query= $query . " and transfer.transfer_reference  = '".$reference."'";


$query = $query . " group by transfer.Transfer_Reference,date_format(transfer.Transfer_Date,'%d-%m-%Y'),transfer.Transfer_WarehouseFrom , q1.transfer_id
having sum(q1.diff > 0)";


$result=mysql_query($query,$connection)  or die(mysql_error());

echo"<tr><td>&nbsp;</td></tr>";

echo'<tr bgcolor="#F7F7F7">
    <td><table><td  class="style29">Warehouse:&nbsp;</td>
    <td><input name="loadfromwarehouse" type="text" onblur="checkwarehouse()" id="loadfromwarehouse" value="'.$loadfromwarehouse.'" readonly/></td></tr>
	
	<tr>
	<td  class="style29">Reference:&nbsp;</td>
    <td><input name="reference" type="text" id="reference" value="'.$reference.'"/></td>
	</tr>
    </table></td>
  </tr><tr><td>';

echo"<div style='width:900px;height:120px;overflow:auto;'>";
echo"<table border=0>";

echo"<tr><td>&nbsp;</td></tr>";
echo"<tr bgcolor='#F7F7F7'><td width='5'>&nbsp;</td><td  width='100'>Reference No</td><td  width='250'>From WareHouse</td><td  width='250'>To WareHouse</td><td  width='345'>Date</td>";
$i=0;
while($rows=mysql_fetch_array($result)){
$i=$i+1;
$checkbox=0;
$transferrefcheck = 'transferrefcheck'.$i;
$transferref = 'transferref'.$i;
echo"<tr bgcolor='#F7F7F7'> 
<td><input type='hidden' name='$transferrefcheck' value='".$$transferrefcheck."'>
<input type='checkbox' name='".$i."' value='0' ";
$$transferref = $rows[0];
if ($$transferrefcheck==1)
 echo "checked";
 echo" onclick=selecttransfer(this,$transferrefcheck,$rows[0]) ></td><td>$rows[1]<input type='hidden'  name='$transferref' value='".$$transferref."' style='border-width:0;background:#F7F7F7'></td><td>$rows[3]</td><td>$rows[5]</td><td>$rows[2]</td>
</tr>";

}
echo"</table></div></td></tr>";
echo"<tr><td>&nbsp;</td></tr>";
$style="style='background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;'";



echo"<tr bgcolor='#F7F7F7' ><td colspan=2 align=center>
	<table border=0 width='100%'><tr>";

if ($module_serialno==1)	
echo"<td width='60'>Serial No</td>
	<td ><input type='text' name='serialno' onkeydown=addserialcode('".$delfromload."','0',\"$TransType\") value='$serialno'></td>
	<td width='60'><input type='button' onclick=addserialcode('".$delfromload."','1',\"$TransType\")   name='serialnobut' value='Submit' $style></td>
	";
else	
echo"<td width='60'>&nbsp;</td>
	<td >&nbsp;</td>
	<td width='60'>&nbsp;</td>";

	
echo"<td  width='100' align='center'><input type='button' id='addnewitembut' name='button'  
						value='Add Item' 
					 onclick='openitempage($transfer_id,\"$TransType\")' $style>
				</td>
				
	<td width='500' align='center'>
	<input type='button' onclick='submitpage()' name='showitems' value='Show Items'  
	 $style title='Show items'>
	&nbsp;<input type='button' name='' value='Confirm' 
	onclick=\"confirmtransfer('".$transfer_id."','".$ref."','".$date."','".htmlentities($costcent)."','".htmlentities($tocostcent)."','".htmlentities($transferfromwarehouse)."','".htmlentities($transfertowarehouse)."','".$actionmode."','".$delfromload."','".htmlentities($fromproject)."','".htmlentities($toproject)."','".htmlentities($driver_name)."','".$vehicle_no."')\" style='background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;'  title='Confirm'>
	";
			 
	echo"</td></tr></table>
	</td>
	</tr>";

	echo"<tr><td>&nbsp;</td></tr>";
echo"<tr><td>";
echo"<div style='width:900px;height:150px;overflow:auto;'>";
echo"<table width='800'> ";//table of items

{ 
			$query="SELECT serialno_code ,serialno_used ,item_code ,serial_id FROM purchasesserialno  
			where serialno_code ='".$serialno."'
			and warehouse_code = '".$fromwarehouse."'
			and (journal_type = 'PI' or journal_type = 'AI'   or journal_type = 'TR'  or journal_type = 'DR'  or journal_type = 'PD' or journal_type = 'SR')";
			//echo $query;
			
			$result=mysql_query($query,$connection)  or die(mysql_error());
			$row=mysql_fetch_array($result);
			$item_code=$row[2];
			$relateddt = $row[3];
			$numofres=mysql_num_rows($result);
				if ($numofres==0 or $numofres=='')
					$serialerror = "Serial Not found in (".$fromwarehouse.") warehouse";
				else
					{
						if ($row[1]==1)
						{
						$qry="SELECT serialno_code ,serialno_used ,item_code,serial_id FROM purchasesserialno  
									where serialno_code ='".$serialno."'
									and warehouse_code = '".$fromwarehouse."'
									and (journal_type = 'PI' or journal_type = 'AI'   or journal_type = 'TR' or journal_type = 'DR'  or journal_type = 'PD' or journal_type = 'SR')
									and serialno_used=0";
								//echo $qry;		
								$resultsr=mysql_query($qry,$connection)  ;
								$row2=mysql_fetch_array($resultsr);
								$relateddt = $row2[3];
								$numofres=mysql_num_rows($resultsr); 							
								
									if ($numofres==0 or $numofres=='')
									{	
										if ($transfer_id!='')
											{	
												$q = "select serialno_code,RelatedSerial_Id from purchasesserialno where transferdt_id in
														(select transferdt_id from transferdt where transfer_id = ".$transfer_id.")
														and serialno_code=".$serialno;
														
														$resultq=mysql_query($q,$connection) ;
														$rowq2=mysql_fetch_array($resultq);
														$rowq=mysql_num_rows($resultq);
														if ($rowq=='' or $rowq==0)
														$serialerror = "Serial is used in (".$fromwarehouse.") warehouse";
														else
														$relateddt = $rowq2[1];
														
											}
									else
										$serialerror = "Serial is used in (".$fromwarehouse.") warehouse";
									
								}
						}		
						else // final else for quantity
							{
							$query0 = "select QtyExistsWSorder_temp('".$row[2]."','".$fromwarehouse."','".displayParadoxDate(insertDate($transfer_date))." 23:59:59',0,'TR',null,null,0)";
							//echo $query0;
							//exit;
									$result0=mysql_query($query0,$connection)  or die(mysql_error());							
									$row0=mysql_fetch_array($result0);
									
									if ($serialerror=='')
									if ($row0[0]<=0)
										$serialerror = 'Quantity Does Not Exist';
									else //check if it is entered before in temp table
										{
										$num=0;
										$query="select serialno_code from temp_purchasesserialno 
										where serialno_code = '".$serialno."'
										and journal_type= 'TR'";
										$result=mysql_query($query,$connection)  or die(mysql_error());
										$num=mysql_num_rows($result);
										if ($num!=0)
											$serialerror = 'Serial Entered Before.';
										}
							}
					}
		
		}
if ($module_serialno==1) 
echo' <tr  ><td>Items With Serial</td><td><font color=red>'.$serialerror.'</font></tD></tr>';

echo' <tr  bgcolor="#CCD8E6">
    <td width="130" class="style27">Item Code </td>
    <td width="400" class="style27">Item Description </td>
    <td width="50" class="style27">Qty</td>
    <td width="50" class="style27">Qty Check</td>
    <td width="100" class="style27">WH From</td>
    <td width="90" class="style27">WH To</td>
    </tr>';
	
	//echo $i;
	//$query = "delete from ".$HTTP_SESSION_VARS['tabletd'];
	
	$delresult=mysql_query($query,$connection)  or die(mysql_error());	
	
	$query = "select  transferdt.item_code,items.item_description,transferdt_quantity,transferdt.transferdt_id,transfer.transfer_id 
				,TransferDt_Price ,TransferDt_WarehouseFrom , TransferDt_WarehouseTo ,purchasesserialno.serialno_code
				from transferdt,transfer,items,purchasesserialno
					 where transferdt.item_code=items.item_code
					 and transferdt.transfer_id = transfer.transfer_id
					 and purchasesserialno.transferdt_id = transferdt.transferdt_id
					 and transfer.transfer_id in (";
			$td='';		 
		for ($j=1;$j<=$i;$j++)
		{	
			$transferrefcheck= 'transferrefcheck'.$j;
			if ($$transferrefcheck==1)
				{
				$transferref = 'transferref'.$j;
				//echo "t: ".$$transferref."<br>";
				$query = $query.$$transferref.",";
				$td=$td.$$transferref.",";
				//$insert = "insert into ".$HTTP_SESSION_VARS['table']."(transfer_id) values(".$$transferref.")";
				//echo $insert."<br>";
				//$insresult=mysql_query($insert,$connection)  or die(mysql_error());;	
				}
			
		}
	$query = $query."0)
	and purchasesserialno.serialno_code = '".$serialno."'";
	//echo $query;
	$result=mysql_query($query,$connection)  or die(mysql_error());
	
	$row=mysql_fetch_array($result);
		//echo $rows[1];
		if ($row[1]!='')
		{   			
				$exists=1;
				$p_transfer_id=$row[4];
				$p_transferdt_id=$row[3];
				$p_item_code=$row[0];
				$p_item_description=$row[1];
				$p_transferdt_quantity=$row[2];
				$p_TransferDt_Price=$row[5];
				$p_TransferDt_WarehouseFrom=$row[6];
				$p_TransferDt_WarehouseTo=$row[7];
				$p_relateddt_id=$row[3];
				$p_serialno_code = $row[8];
				
				if ($p_TransferDt_Price=='')
					$p_TransferDt_Price = 0;
		}
if ($td!='')
		$td=$td."0";
echo"<input type='hidden' name='td' value='$td'>";		
		//echo $serialno;
		
	 
		if ($actionmodeload=='insert' and $serialerror=='')
		{
		
		if ($exists==1)
			{ 
				$countquery="select count(serialno_code) as isexists 
							from temp_purchasesserialno where serialno_code = '".$serialno."'
							 and transferdt_id in (select transferdt_id from temp_transferdt where transfer_id = ".$transfer_id.") 
							 ";
				
				$countresult=mysql_query($countquery,$connection)  or die(mysql_error());	
				while($crows=mysql_fetch_array($countresult)){
					extract($crows);
					}
				
				if ($isexists==0 or $isexists=='')
					{ 
						$checkTransferdt = "select transferdt_id from temp_transferdt where item_code = '".$p_item_code."' 
											and transfer_id  = ".$transfer_id;
						
						$check=mysql_query($checkTransferdt,$connection)  or die(mysql_error());												
						$row=mysql_fetch_array($check);
						$PTransferDtId = $row[0];
						if ($row[0]==0 or $row[0]=='')	
							{
							
							$query = "select BASE1 from setup limit 1";
									$result1=mysql_query($query,$connection)	or die( mysql_error());
										while($row=mysql_fetch_array($result1)){
										extract($row);
										}
	
							$query="select curdt_parity from curdt where currency_code='".$BASE1."' order by currency_code desc";
							$result1=mysql_query($query,$connection)	or die( mysql_error());
							while($row=mysql_fetch_array($result1)){
							extract($row);
							}
	
							$p_TransferDt_Price2=$p_TransferDt_Price/$curdt_parity;
							
							$PTransferDtId = get_nextsequence('transferdt_id','',$connection);	
							if (empty($relateddt_id))
								$relateddt_id = 'null';
							$insTrDt = "insert into temp_transferdt
										(transferdt_id,transfer_id,transfer_date,item_code,transferdt_warehousefrom,
										transferdt_warehouseto,transferdt_quantity,transferdt_price,transferdt_pricebase1,
										transferdt_pricebase2,project_code1,project_code2,costcent_code,tocostcent_code,transferdt_remark,cur_code,
										relateddt_id,logged_code)
										values(".$PTransferDtId."
										,".$transfer_id."
										,'".displayParadoxDate(insertDate($transfer_date))."'
										,'".$p_item_code."'
										,'".$transferfromwarehouse."'
										,'".$transfertowarehouse."'
										,'0'
										,".$p_TransferDt_Price."
										,".$p_TransferDt_Price."
										,".$p_TransferDt_Price2."
										,'".$fromproject."'
										,'".$toproject."'
										,'".$costcent."'
										,'".$tocostcent."'
										,'".$transferdt_remark."'
										,'".$BASE1."'						
										,".$relateddt_id."
										,'".$HTTP_SESSION_VARS['useraccount']."'
										)";
										//echo $insTrDt;exit;

								$result=mysql_query($insTrDt,$connection)  or die(mysql_error());
								}
						if ($relateddt=='')
							$relateddt = 'null';
		
						$serial_id = get_nextsequence('serial_id','',$connection);	
						$insert = "insert into temp_purchasesserialno
						(Serial_ID,journal_type,serialno_code,item_code,transferdt_id,serialno_quantity,Warehouse_code,logged_code,flagunique,PurchaseDt_ID,RelatedSerial_Id) 
						values(".$serial_id.",'TR','".$p_serialno_code."','".$p_item_code."',".$PTransferDtId.",1,'".$transfertowarehouse."','".$HTTP_SESSION_VARS['useraccount']."',".$PTransferDtId.",".$PTransferDtId.",".$relateddt.")";
						$insresult=mysql_query($insert,$connection)  or die(mysql_error());	
						if ($insresult)
							{
							$Upd = "update temp_transferdt set TransferDt_Quantity  = TransferDt_Quantity + 1 where transferdt_id = ".$PTransferDtId;
							$result=mysql_query($Upd,$connection) or die(mysql_error());					
							}
					}
			}
			}
			
		////// list of items
		$query = "select  transferdt.item_code,items.item_description,
		transferdt.TransferDt_Quantity- sum(ifnull(b.transferdt_quantity,0)) as transferdt_quantity,transferdt.transferdt_id,
		transfer.transfer_id 
				,transferdt.TransferDt_Price ,transferdt.TransferDt_WarehouseFrom , transferdt.TransferDt_WarehouseTo 
				from transfer,items,transferdt 	 
				left join transferdt B on B.relateddt_id=transferdt.transferdt_id
					 where transferdt.item_code=items.item_code
					 and transferdt.transfer_id = transfer.transfer_id
					  and transfer.transfer_id in (";
		for ($j=1;$j<=$i;$j++)
		{	
			$transferrefcheck= 'transferrefcheck'.$j;
			if ($$transferrefcheck==1)
				{
				$transferref = 'transferref'.$j;
				$query = $query.$$transferref.",";		
				}
		}
	$query = $query."0)
	
	group by transferdt.transferdt_id
	order by transferdt.item_code";
	//echo $query;exit;
	$result=mysql_query($query,$connection)  or die(mysql_error());
	while($row=mysql_fetch_array($result))
		{
				$query2="select count(*) from temp_purchasesserialno where 
				transferdt_id in (select transferdt_id from temp_transferdt where transfer_id = ".$transfer_id.")
				and item_code = '".$row[0]."'";
				//echo $query;
				$j=1;
				$result2=mysql_query($query2,$connection)  or die(mysql_error());	
				$numrows=mysql_fetch_array($result2);
				
		
				echo"<tr bgcolor='#D8D8D8'>
				<td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$numrows[0]</td><td>$row[6]</td><td>$row[7]</td></tr>";
	
		}
		
	echo"</div>";
	

echo"</table></td></tr><tr><td>&nbsp;</td></tr><tr><td>";//table of items
	
	//if ($actionmodeload=='insert')
		{
		echo"<div style='width:900px;height:300px;overflow:auto;'>";
		echo"<table border=0 width='100%'>";
if ($module_serialno==1){
	echo"<tr><td></td><td></td><td align='right'>
	<span class='style25'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='ClearForm();'>
	";    
		
        	 if ($perpage==5)
			$s5 = 'selected';
		else if ($perpage==10)
			$s10 = 'selected';	
		else if ($perpage==15)
			$s15 = 'selected';	
		else if ($perpage==20)
			$s20 = 'selected';	
		else if ($perpage==100)
			$s100 = 'selected';	
		else if ($perpage==200)
			$s200 = 'selected';
		else if ($perpage==300)
			$s300 = 'selected';
		else if ($perpage=='all')
			$sall = 'selected';

			
				
        echo'<option '.$s5.' value="5">5</option>
			 <option '.$s10.' value="10">10</option>
			 <option '.$s15.' value="15">15</option>
			 <option '.$s20.' value="20">20</option>
			 <option '.$s100.' value="100">100</option>
			 <option '.$s200.' value="200">200</option>
			 <option '.$s300.' value="300">300</option>
			 <option '.$sall.' value="all">All</option>
			 </select></span></td></tr>';

			 

		echo"<tr  bgcolor='#CCD8E6'><tD></tD><td width='200'  class='style27'>Serial No</td><td class='style27'>Item Code</td></tr>";
		
		$query="select serialno_code  as serialno_code , item_code as item_code from temp_purchasesserialno where 
				transferdt_id in (select transferdt_id from temp_transferdt where transfer_id = ".$transfer_id.")			
				order by serial_id desc ";
				//echo $query;
				if  ($perpage=='')
					$perpage = 5;
				if ($perpage!='all')
				$query = $query . " limit $perpage ";
				
				
				$j=1;
				$result=mysql_query($query,$connection)  or die(mysql_error());	
				while($rows=mysql_fetch_array($result)){
					$ct+=1;

				if($ct%2==0)
					$Color="style='background-color:".$grid_color1."'";
				else			
					$Color="style='background-color:".$grid_color2."'";	
			
				echo "<tr ".$Color." >";
					echo"<td width='5%'>$j</td><td>$rows[0]</td><td>$rows[1]</td></tr>";
					$j=$j+1;
					
					}	
		
		
		echo"</table>";
		echo"</div>";
		}
}

		
echo"</td></tr></table>";//last table
echo"</form>";
$serialno='';


if ($module_serialno==1)
print <<<HERE
<script>
document.loaditems.serialno.value='';

</script>
HERE;


function displayParadoxDate($data){
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2);
}
function insertDate($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}
function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}
function selectData($conn, $query){
	$result = mysql_query($query, $conn);
	$cur = mysql_fetch_row($result);		
	return $cur;
}
function updateData($conn, $query, $commit){
	if (mysql_query($query, $conn)){				
		return true;
	}else{
		return false;
	}
}
?>