<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
set_time_limit(0);

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog=" select ifNull(FI_DeliveryApp,0) As FI_DeliveryApp, ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(FI_DeliverySalesPrice,0) As FI_DeliverySalesPrice, ".
		  " ifNull(FI_sorderconfirmed,0) As FI_SalesOrderConfirm, ifNull(login_disapprovesorder,0) As Login_DisapproveSorder, ifNull(FI_FrmRestrictWarehouse,0) As FI_FrmRestrictWarehouse ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

$EditHint = '';
$EditMode = '';

$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, 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);
}

//Read From SalesSetup Table
$querySalesSetup="Select SalesTTC, ifNull(Sale_MinPendingQty,0) As Sale_MinPendingQty, ifNull(Sale_MaxExceedQty,0) As Sale_MaxExceedQty From SalesSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_Pack,0) As Module_Pack, ".
			 " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent, ".
			 " ifNull(Module_Delivery,0) As Module_Delivery, ifNull(Module_TransferSorder,0) As Module_TransferSorder  From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);	

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse inner join ".$_SESSION['dbLabel'].".loginstk on warehouse.warehouse_code = loginstk.warehouse_code ".
		 " where user_account='".$_SESSION['useraccount']."' ".
		 " and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '')  ".
		 " order by warehouse.warehouse_code";
$whresult = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($whresult);	  



print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Confirmed Transfer Request</title>
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<script type="text/javascript" language="javascript">

function RefreshPage()
{
  document.search.submit();

}

function clearform()
{
  document.search.PRef.value='';
  document.search.action='Form_TransferLoadRequest.php';
  document.search.submit();

}



function PrintPreview(SorId)
{
	var str = '';

	str = 'Form_TransferSalesOrderPreview.php?SorderId='+SorId;
	window.open(str);
	
	self.close();

}


function LoadForm() 
{
	x=document.search;
	x.PRef.focus();
	
	ChangeLocation();
}

function SetNextPage(startrange,perpage){ 
	document.search.action='Form_TransferLoadRequest.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.action='Form_TransferLoadRequest.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}



function SelectRecord(RecordId){
	
	document.search.PMode.value=1;	
	document.search.action="Form_Transfer_Actions.php?mainmode=copyrequest&TrReqId="+RecordId;
	document.search.submit();


}

function UnConfirmOrder(SorId, SorRef){

	var vMainMode = '';
	var vMsg = '';
	
	vMainMode = 'unconfirmorder';
	vMsg = 'Are you sure you want to Unconfirm order with reference = '+SorRef+' ?';
	
	if (SorId != '') {
		if (confirm (vMsg) )
		{
			document.search.action="Form_Transfer_Actions.php?mainmode="+vMainMode+"&SSaId="+SorId;
			document.search.submit();
		}
	}
	
}

function LoadData(){
	var vSelectedRows = document.search.SelectedRows.value;
	
	if (vSelectedRows != '') {
		var vSaID = document.search.PSaId.value;
		document.search.PMode.value=1;	
		document.search.action="Form_Transfer_Actions.php?mainmode=loadsorder&SSaId="+vSaID+"&SSaDtId="+vSelectedRows;
		document.search.submit();
	} else
		alert ('Please Select an Item.');
	
}


function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
	    var GenId = document.search.GeneratedId.value;
		
		
		opener.document.transfer.transfer_id.value=GenId;
		
	    opener.document.transfer.actionmode.value='submitrecord';
		opener.document.transfer.message.value = document.search.message.value;
		opener.document.transfer.action='Form_Transfer.php';
		opener.document.transfer.submit();
		self.close();
	}	
}

function SelectMaster(vName) {

   
   var MasterCheckName, DetailCheckName, RecName;
   var x, y, z, t;
   
   for (var i=1; i <= document.search.i.value; i++)
  {
    MasterCheckName='M'+i;
	RecName = 'Rec'+MasterCheckName;
	
	x = document.getElementById(MasterCheckName);
	y = document.getElementById(RecName);
	t = document.getElementById(vName);
	
	if (x.checked && x.value == t.value) {
		for (var j=1; j <= y.value; j++)
		{
			DetailCheckName=MasterCheckName+'D'+j;
	
			z = document.getElementById(DetailCheckName);
			z.checked = true;
		}	
	} 
	else if (!(x.checked) &&  x.value == t.value){ 
		for (var j=1; j <= y.value; j++)
		{
			DetailCheckName=MasterCheckName+'D'+j;
	
			z = document.getElementById(DetailCheckName);
			z.checked = false;
		}
	
	}
    
  }
  
  SelectDetail();
  

}

function SelectDetail() {
	var MasterCheckName, DetailCheckName, RecName, vSelectedRows;
	var x, y, z;
	
	vSelectedRows = '';
	for (var i=1; i <= document.search.i.value; i++)
	{
		MasterCheckName='M'+i;
		RecName = 'Rec'+MasterCheckName;
		
		x = document.getElementById(MasterCheckName);
		y = document.getElementById(RecName);
		
		
		for (var j=1; j <= y.value; j++)
		{
			DetailCheckName=MasterCheckName+'D'+j;
		
			z = document.getElementById(DetailCheckName);
			
			if (z.checked)
				vSelectedRows = vSelectedRows + ',' + z.value;
		}	
		
		
	}
	
	vSelectedRows = vSelectedRows.substr(1);
	document.search.SelectedRows.value = vSelectedRows;
}

function ShowHideDetails(ReqId, Option, startrange) {
	vSlideDivId = '#displaydetails'+ReqId;
	$(vSlideDivId).slideToggle();
}


</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>

</head>
<body onLoad='LoadForm();'>
<form name='search' method='post' action=''>
<input type='hidden' name='GeneratedId' value='$GeneratedId'>
<input type='hidden' name='SelectedRows' value='$SelectedRows'>

<input type='hidden' name='costcent' value='$costcent'>
<input type='hidden' name='tocostcent' value='$tocostcent'>
<input type='hidden' name='fromwarehouse' value='$fromwarehouse'>
<input type='hidden' name='towarehouse' value='$towarehouse'>
<input type='hidden' name='toproject' value='$toproject'>
<input type='hidden' name='fromproject' value='$fromproject'>
<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='TransType' value='$TransType'>
<input type='hidden' name='documentrefno' value='$documentrefno'>
HERE;

echo "<input type='hidden' name='message' value='".htmlspecialchars($message, ENT_QUOTES)."'>";
echo "<input type='hidden' name='PSaRemark' value='".htmlspecialchars($PSaRemark, ENT_QUOTES)."' > 
	  <input type='hidden' name='PSaDriverName' value='".htmlspecialchars($PSaDriverName, ENT_QUOTES)."' >
	  <input type='hidden' name='PSaVehicleNumber' value='".htmlspecialchars($PSaVehicleNumber, ENT_QUOTES)."' >
	  <input type='hidden' name='PSaDAddress' value='".htmlspecialchars($PSaDAddress, ENT_QUOTES)."' > 
	  <input type='hidden' name='PSaIAddress' value='".htmlspecialchars($PSaIAddress, ENT_QUOTES)."'> ";





echo"<table border='0' cellspacing='1' style='table-layout:fixed; width:835px;'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=65>
	  <col width=70>
	  <col width=120>
	  <col width=120>
	  <col width=100>
	  <col width=100>
	  <col width=200>
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='10'><span class='style16'>List of Confirmed Transfer Request</span></td></tr>
	  <tr bgcolor='#DDDDDD'>
	    <td align='left' colspan='4'><span class='style11'>Search by Reference</span></td>
		<td align='left' colspan='3'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		    <input type='submit' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' valign='center' colspan='3'><span class='style11'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage'>";
              

        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';
			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';	
echo" </tr>
      <tr bgcolor='#DDDDDD'>
	    <td align='left' colspan='4'><span class='style11'>Search by Item</span></td>
		<td align='left' colspan='6'>";
    echo"<select name='lstItems' id='lstItems' style='font-size:11px; width:350px; height:18px;' onchange='RefreshPage();' >";
			echo"<option></option>";
			while($itemf=mysql_fetch_row($resultitems)){
				echo"<option ";
				if ($lstItems==$itemf[0])
					echo " selected ";
				echo" value='$itemf[0]'>$itemf[0] - $itemf[1]</option>";
			}
			
			
	echo"</select></td>";
echo" </tr>	
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='10'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style25'>Reference </span></td>
		<td  height='25' align='center'><span class='style25'>Date </span></td>
		<td  height='25' align='center'><span class='style25'>From Project</span></td>
		<td  height='25' align='center'><span class='style25'>To Project</span></td>";
		
		
			echo"<td  height='25' align='center'><span class='style25'>From Warehouse</span></td>
				 <td  height='25' align='center'><span class='style25'>To Warehouse</span></td>
				 <td  height='25' align='center'><span class='style25'>Remark</span></td>
				 ";
		
		
echo" </tr>";
		



	
if (empty($PRef))   
   $PRef = 'Null';
else
   $PRef = "'".$PRef."'"; 

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
   
   
   
$querysearch=" Select Distinct transferrequest.transferrequest_ID as transferrequest_ID, transferrequest.transferrequest_Reference
			, Date_Format(transferrequest.transferrequest_Date,'%d-%m-%Y') As transferrequest_Date, transferrequest.Transferrequest_Remark,".
			 " Project.Project_Name As ProjectName ,Transferrequest.Transferrequest_WarehouseFrom as Transferrequest_WarehouseFrom,Transferrequest.Transferrequest_WarehouseTo as Transferrequest_WarehouseTo".
			 " ,Project2.Project_Name As ProjectName2 ,transferrequest.Project_Code1 as Project_Code1,transferrequest.Project_Code2 as Project_Code2".
			 " ,ifNull(Temp_transferrequest.transferrequest_ID, -1) As TemppurchaserequestId from transferrequest".
			 " Left Join Project on transferrequest.Project_Code1 = Project.Project_Code ".
			 " Left Join Project as project2 on transferrequest.Project_Code2 = project2.Project_Code ".
			 " Left Join Temp_transferrequest on Temp_transferrequest.transferrequest_Id = transferrequest.transferrequest_Id ";		 
			 
			 
			 
if ($projectexist >	0)
  $querysearch= $querysearch." inner join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on transferrequest.project_code1 like concat(Q1.project_code,'%') ";
							 
if ($costcentexist > 0)
  $querysearch= $querysearch." inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
							 "			   where loginstk.user_account='".$_SESSION['useraccount']."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on transferrequest.costcent_code like concat(Q2.costcent_code,'%') ";							 

				 
$querysearch= $querysearch.											 
			 " Where transferrequest.transferrequest_Reference = ifNull($PRef, transferrequest.transferrequest_Reference) ".
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(transferrequest.transferrequest_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',transferrequest.transferrequest_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(transferrequest.transferrequest_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ".
             " And  exists ( select 1 from transferrequestdt where transferrequestdt.transferrequest_id = transferrequest.transferrequest_id and TransferrequestDt_approvedlevel = 1 and ifnull(TransferrequestDt_status,'r') = 'a' limit 1) ".
			 " And ifNull(transferrequest.TransferRequest_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";

$querysearch= $querysearch." and not exists (select 1 from transfer where transfer.transferrequest_id = transferrequest.transferrequest_id ) ";			 
$querysearch= $querysearch." Order By transferrequest.transferrequest_Reference Desc ";
			 
//echo $querysearch; exit;

if ($PMode != '1') {

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='' || $PRef != 'Null')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	

$i = 0;

while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;
	$i = $i + 1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
	
	
	if ($TemppurchaserequestId != -1) { $EditHint = 'Exists in Pending'; $EditMode = ' disabled '; }
	else { $EditHint = 'Select Record'; $EditMode = ''; }
	

	
	echo'<tr '.$Color.'>';
			
		if (!empty($PSaClientCode)) {
				
		    $SelMaster  = 'M'.$i;
			$$SelMaster = $Sorder_ID;
			   
			echo"<td align='center'><span class='style15'><input type='checkbox' name='$SelMaster' id='$SelMaster' value='".$$SelMaster."' onclick='SelectMaster(this.name);' /></span></td>";
			echo'<td align="center"><span class="style15">&nbsp;</span></td>';
			
		} else
		{
			echo'<td align="center"><span class="style15">&nbsp;</span></td>';
			//echo'<td align="center"><span class="style15"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$transferrequest_ID.');"  title="'.$PrevHint.'"  style="width: 15px; height: 15px; " '.$SorPrevMode.'></span></td>';
		
			if ($TemppurchaserequestId != -1) 
				echo'<tD></tD>';
			else
				echo'<td align="center"><span class="style15"><img src="img/button_edit.png" onMouseOver="style.cursor=\'hand\'" onclick="SelectRecord('.$transferrequest_ID.');" title="'.$EditHint.'" '.$EditMode.'></span></td>';	
				
			echo'<td align="center"><span class="style5"><img src="img/details.gif" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$transferrequest_ID.', \''.'S'.'\','.$startrange.');" title="Show Details" style="width:15px; height:15px; "></span></td>';	
		}

			
			
		echo'<td align="left"><span class="style15">'.$transferrequest_Reference.'</span></td>
			 <td align="center"><span class="style15">'.$transferrequest_Date.'</span></td>
			 <td align="center"><span class="style15">'.$Project_Code1.'</span></td>
			 <td align="left"><span class="style15">'.$Project_Code2.'</span></td>';
			 
	
			echo'<td align="left"><span class="style15">'.$Transferrequest_WarehouseFrom.'</span></td>
				 <td align="left"><span class="style15">'.$Transferrequest_WarehouseTo.'</span></td>
				 <td align="left"><span class="style15">'.$Transferrequest_Remark.'</span></td>
				 	 ';
	
	
	echo'</tr>';
		
		if (!empty($PSaClientCode)) {
		
			echo" <tr><td colspan='10'><table border='0' style='table-layout:fixed'>
				   <col width=50>
				   <col width=123>
				   <col width=110>
				   <col width=347>
				   <col width=90>
				   <col width=50>
				   ";
				   
			$querydt = " Select SorderDt.SorderDt_id, SorderDt.Item_CodePacked, SorderDt.Item_code, Item_Description, SorderDt.Warehouse_Code, ".
					   " SorderDt.SorderDt_Remark, (ifNull(SorderDt.SorderDt_Quantity,0) - (ifNull(Q1.QuantityDelivered,0) + ifNull(Q2.QuantityDeliveredTemp,0))) As QuantityRemaining,  ".
					   " Round(SorderDt.SorderDt_Price, ".$DecimalBase."), SorderDt.SorderDt_PriceBase1, SorderDt.SroderDt_PriceBase2, ".
					   " Round(SorderDt.SorderDt_Discount, ".$DecimalBase."),  ".
					   " Round(((SorderDt.SorderDt_Quantity*SorderDt.SorderDt_Price)-((SorderDt.SorderDt_Quantity*SorderDt.SorderDt_Price*ifNull(SorderDt.SorderDt_Discount,0))/100)), ".$DecimalBase.") As Total, ".
					   " Round(SorderDt.SorderDt_PriceTTC, ".$DecimalBase."), ".
					   " Round(((SorderDt.SorderDt_Quantity*SorderDt.SorderDt_PriceTTC)-((SorderDt.SorderDt_Quantity*SorderDt.SorderDt_PriceTTC*ifNull(SorderDt.SorderDt_Discount,0))/100)), ".$DecimalBase.") As TotalTTC ".
					   " From SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
					   " Left Join ( Select DeliveryDt.RelatedDt_ID, DeliveryDt.Item_code, DeliveryDt.Item_CodePacked, Sum(ifNull(DeliveryDt.DeliveryDt_Quantity,0)) As QuantityDelivered ".
					   "             From DeliveryDt ".
					   "             Where (DeliveryDt.RelatedDt_ID != '' or DeliveryDt.RelatedDt_ID Is Not Null)  ".
					   "             And DeliveryDt.Delivery_Id != '".$PSaId."' ".
					   "             Group By DeliveryDt.RelatedDt_ID, DeliveryDt.Item_code, DeliveryDt.Item_CodePacked  ".
					   "            ) Q1 on Q1.Item_code = SorderDt.Item_code  and Q1.RelatedDt_ID = SorderDt.SorderDt_id ".
					   " Left Join ( Select Temp_DeliveryDt.RelatedDt_ID, Temp_DeliveryDt.Item_code, Temp_DeliveryDt.Item_CodePacked, Sum(ifNull(Temp_DeliveryDt.DeliveryDt_Quantity,0)) As QuantityDeliveredTemp ".
					   "             From Temp_DeliveryDt ".
					   "             Where (Temp_DeliveryDt.RelatedDt_ID != '' or Temp_DeliveryDt.RelatedDt_ID Is Not Null) ".
					   "             Group By Temp_DeliveryDt.RelatedDt_ID, Temp_DeliveryDt.Item_code, Temp_DeliveryDt.Item_CodePacked ".
					   "            ) Q2 on Q2.Item_code = SorderDt.Item_code  and Q2.RelatedDt_ID = SorderDt.SorderDt_id ".
					   " Where SorderDt.Sorder_ID = ".$Sorder_ID.
					   " And (('".$lstItems."' != '' and Sorderdt.Item_Code = '".$lstItems."') or ('".$lstItems."' = '' and 1 = 1)) ".
					   " And (ifNull(SorderDt.SorderDt_Quantity,0) - (ifNull(Q1.QuantityDelivered,0) + ifNull(Q2.QuantityDeliveredTemp,0))) > 0 ".
					   " Order By SorderDt.SorderDt_ID Desc ";
			
			//echo "<br>".$querydt;
			
			$j = 0;
			$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
			while($rowsdt=mysql_fetch_array($resultdt)){
			    
				$ct+=1;
				$j = $j + 1;
				
				if($ct%2==0)
					$Color="style='background-color:".$grid_color1."'";
				else			
					$Color="style='background-color:".$grid_color2."'";	
						
				$SelDetail  = $SelMaster.'D'.$j;
				$$SelDetail = $rowsdt[0];
			   
				
				echo "<tr ".$Color." >";
				echo"   <td align='center'><span class='style15'><input type='checkbox' name='$SelDetail' id='$SelDetail' value='".$$SelDetail."' onclick='SelectDetail();' /></span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[1]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[2]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[3]."</span></td>";
				echo "	<td align='left'><span class='style15'>".$rowsdt[4]."</span></td>";
				echo "	<td align='right'><span class='style15'>".$rowsdt[6]."</span></td>";
				echo "</tr>";
								
			} 		

			$RecName  = 'Rec'.$SelMaster;
			
			echo "<input type='hidden' name='$RecName' id='$RecName' value='$j'>";	  
			echo "</table></td></tr>";
			
		} else {
			$sub_query = " Select transferrequestdt.TransferrequestDt_id, transferrequestdt.Item_CodePacked, transferrequestdt.Item_code, Item_Description, ".
						 " transferrequestdt.TransferrequestDt_WarehouseFrom, transferrequestdt.TransferrequestDt_WarehouseTo, Item_UnitSalesDesc, TransferrequestDt_Quantity  ".
						 " From transferrequestdt Left Join Items on transferrequestdt.Item_code = Items.Item_code ".
						 " Where transferrequestdt.transferrequest_ID = ".$transferrequest_ID;

			$sub_result=mysql_query($sub_query,$connection) or die(mysql_error($connection));	
			$sub_totalrows = mysql_num_rows($sub_result);
				
			if ($sub_totalrows > 0) {
				$SlideDivId = 'displaydetails'.$transferrequest_ID;
					
				echo "<tr ".$Color.">";
				echo "<td colspan='10' valign='top' align='left'><div id='$SlideDivId' class='div-search-details' style='display:none; margin-left:60px;'>";
				echo "<table border='0' style='table-layout:fixed; width:720px;' cellpadding='0' cellspacing='1'>";
				echo "<tr><td width='140px'><span class='style4'>Item Code</span></td>
						  <td width='240px'><span class='style4'>Description</span></td> 
						  <td width='60px' align='center'><span class='style4'>Unit1</span></td>
						  <td width='80px' align='center'><span class='style4'>Qty</span></td>
						  <td width='100px'><span class='style4'>From Warehouse</span></td> 
						  <td width='100px'><span class='style4'>To Warehouse</span></td> ";
				echo" </tr>";
				while($sub_rows=mysql_fetch_array($sub_result)){
					echo "<tr><td align='left' valign='top' class='style9'>".$sub_rows[2]."</td>
							  <td align='left' valign='top' class='style9'>".$sub_rows[3]."</td>
							  <td align='center' valign='top' class='style9'>".$sub_rows[6]."</td>
							  <td align='center' valign='top' class='style9'>".$sub_rows[7]."</td>
							  <td align='left' valign='top' class='style9'>".$sub_rows[4]."</td>
							  <td align='left' valign='top' class='style9'>".$sub_rows[5]."</td>";
					echo "</tr>";
						
				}
					
				echo "</table></div></td>";
				echo "</tr>";
			}
		}
}
							
echo "<input type='hidden' name='i' value='$i'>";

if (!empty($PSaClientCode)) {
	echo"   <tr bgcolor='#DDDDDD'><td colspan='10' align='center'><input type='button' name='btnLoad' value='Load' onclick='LoadData();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td></tr>";
}			
		
echo"   <tr><td colspan='10'>&nbsp;</td></tr>
		<tr><td colspan='10'><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"<table>";
		
}		

echo"</td></tr></table>";
	


          

		  

print <<<HERE

</form>
</body>

HERE;

?>