<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

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

if($export == 1){
	$filename = "Exp_ListofRequestedItems_" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
}
print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Requested Items </TITLE>

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 13px; 
                  font-family:Times-New-Roman;
                 }
				 
table.master1 th { font-size: 14px; 
                   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 
table.master1 td { font-size: 13px; 
				   font-weight: bold;
                   font-family:Times-New-Roman;
                 }				 

table.detail th { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }				 			   
table.detail td { font-size: 11px; 
				  font-weight: normal;
		          font-family:Times-New-Roman;
                 }			   
				 
.totals { font-size: 12px; 
          font-weight: bold;
          font-family:Times-New-Roman;
        }					  
				 
h4 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 2px;
   }
h5 { border-top-style: solid; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }   
   
</style>

</HEAD>

HERE;



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

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$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");

$querymodule=" Select ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes, ifNull(Module_Pack,0) As Module_Pack, ".
			 " ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_RestrictProjCost,0) As Module_RestrictProjCost, ifNull(Module_RestrictCost,0) As Module_RestrictCost, ".
			 " ifNull(Module_ProjectDt,0) As Module_ProjectDt, ifNull(Module_CostCenterDt,0) As Module_CostCenterDt, ".
			 " ifNull(Module_PRequestConfirm,0) As Module_PRequestConfirm, ifNull(Module_DoubleUnit,0) As Module_DoubleUnit, ifNull(Module_Unit1Method,0) As Module_Unit1Method, ".
			 " ifnull(Module_PurchaseRequestPrice,0) as Module_PurchaseRequestPrice, ifNull(Module_Grn,0) as Module_Grn, ".
			 " ifnull(Module_PrintApprovedRqst,0) as Module_PrintApprovedRqst, ifNull(Module_PorderConfirm,0) As Module_PorderConfirm, ".
			 " ifNull(Module_PaymentLimit,0) as Module_PaymentLimit,module_CheckItem,module_pendingprocess as PendingProcess,PrequestReferenceByProject as RefProject ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
$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, ".
          " ifnull(FI_TransferRequest,0) as FI_TransferRequest, ifnull(FI_StockOutRequest,0) as FI_StockOutRequest
		  ,ifnull(PorderApproved_Level,50) As PurorderApproved_Level  ".
          " ,ifnull(PrequestApproved_Level,50) As PrequestApproved_Level  ".
          " ,ifnull(TransferRequestApproved_Level,50) As TransferRequestApproved_Level  ".
          " ,ifnull(AdjustmentRequestApproved_Level,50) As AdjustmentRequestApproved_Level  ".
		  " ,ifnull(fi_pendingpurchaserequest,0) fi_pendingpurchaserequest".
		  " ,ifnull(fi_pendingtransferrequest,0) fi_pendingtransferrequest".
		  " ,ifnull(fi_pendingadjustmentrequest,0) fi_pendingadjustmentrequest".
		  " ,ifnull(fi_pendingpaymentrequest,0) fi_pendingpaymentrequest".
		  " ,ifnull(fi_pendingpurchaseorder,0) fi_pendingpurchaseorder, ".
		  " ifNull(Login_PReqApprove,0) As Login_PReqApprove, ifNull(Login_PReqReject,0) As Login_PReqReject, ifNull(Login_PReqUndProc,0) As Login_PReqUndProc, ".
		  " ifNull(Login_POrdApprove,0) As Login_POrdApprove, ifNull(Login_POrdReject,0) As Login_POrdReject, ".
		  " ifNull(Login_TReqApprove,0) As Login_TReqApprove, ifNull(Login_TReqReject,0) As Login_TReqReject, ".
		  " ifNull(Login_AReqApprove,0) As Login_AReqApprove, ifNull(Login_AReqReject,0) As Login_AReqReject, ifNull(FI_PRequestConfirmed,0) As FI_PRequestConfirmed, ".
		  " PR_status1,PR_status2,PR_status3,PR_status4,PR_status5,PR_status6,PR_status7,PR_status8,PR_status9,PR_status10,PR_status11,PR_status12,PR_status13,PR_default,PO_default,TR_default,AD_default, PYR_default, ".
		  " TR_status1,TR_status2,TR_status3,AD_status1,AD_status2,AD_status3,PO_status1,PO_status2,PO_status3,PO_status4,PO_status5,PO_status6,PO_status7, ifNull(FI_PorderConfirmed,0) As FI_PorderConfirmed, ".
		  " PYR_status1,PYR_status2,PYR_status3,ifnull(PaymentRqstApproved_Level,50) As PaymentRqstApproved_Level, ifNull(Branch_Manager,0) as Branch_Manager,FI_ItemsApp ".
		  " 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);
}

$querySalesSetup="Select Warehouse As SetupDftWareHouse, ifNull(ShowCostCentLayout,0) As ShowCostCentLayout_Purchase, PurchaseTTC From PurchaseSetup";
$resultSalesSetup=mysql_query($querySalesSetup,$connection) or die(mysql_error());
while($rowSalesSetup=mysql_fetch_array($resultSalesSetup)){
	extract($rowSalesSetup);
}
$queryAdjustSetup="Select ifNull(ShowCostCentLayout,0) As ShowCostCentLayout_Adjustment From AdjustmentSetup ";
$resultAdjustSetup=mysql_query($queryAdjustSetup,$connection) or die(mysql_error());
while($rowAdjustSetup=mysql_fetch_array($resultAdjustSetup)){
	extract($rowAdjustSetup);
}
$querydflwh="select warehouse_code as defaultwarehouse from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and ifNull(default_warehouse,0) = 1 ";

$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}
$queryTrSetup="Select ifNull(ShowCostCentLayout,0) As ShowCostCentLayout_Transfer From TransferSetup ";
$resultTrSetup=mysql_query($queryTrSetup,$connection) or die(mysql_error());
while($rowAdjustSetup=mysql_fetch_array($resultTrSetup)){
	extract($rowAdjustSetup);
}

if($defaultwarehouse == ''){
	$q = "select warehouse from purchasesetup;";
			$rs = mysql_query($q,$connection); 
			$rw = mysql_fetch_row($rs);
			$defaultwarehouse = $rw[0];
}
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

if($transtype == 'o') $transname = 'Purchase Order';
if($transtype == 'p') $transname = 'MSR';
if($transtype == 't') $transname = 'Transfer Request';
if($transtype == 'a') $transname = 'Adjustment Request';

if($transtype == 'o' && $itemstatus == 2) $stat = 'LPO Pending Approval';
if($transtype == 'o' && $itemstatus == 3) $stat = 'LPO Approved';
if($transtype == 'o' && $itemstatus == 4) $stat = 'LPO Rejected';
if($transtype == 'o' && $itemstatus == 10) $stat = 'GRN Pending';
if($transtype == 'o' && $itemstatus == 11) $stat = 'GRN Rejected';
if($transtype == 'o' && $itemstatus == 12) $stat = 'GRN Approved';

if($transtype == 'p' && $itemstatus == 1) $stat = 'MSR Working On';
if($transtype == 'p' && $itemstatus == 2) $stat = 'MSR Pending Approval';
if($transtype == 'p' && $itemstatus == 3) $stat = 'MSR Approved';
if($transtype == 'p' && $itemstatus == 4) $stat = 'MSR Rejected';
if($transtype == 'p' && $itemstatus == 5) $stat = 'MSR Transferred';
if($transtype == 'p' && $itemstatus == 7) $stat = 'LPO Pending Approval';
if($transtype == 'p' && $itemstatus == 8) $stat = 'LPO Approved';
if($transtype == 'p' && $itemstatus == 9) $stat = 'LPO Rejected';
if($transtype == 'p' && $itemstatus == 10) $stat = 'GRN Pending';
if($transtype == 'p' && $itemstatus == 11) $stat = 'GRN Rejected';
if($transtype == 'p' && $itemstatus == 12) $stat = 'GRN Approved';
if($transtype == 'p' && $itemstatus == 13) $stat = 'MSR Cancelled by PM';

if($transtype == 't' && $itemstatus == 2) $stat = 'Transfer Pending Approval';
if($transtype == 't' && $itemstatus == 3) $stat = 'Transfer Approved';
if($transtype == 't' && $itemstatus == 4) $stat = 'Transfer Rejected';

if($transtype == 'a' && $itemstatus == 2) $stat = 'Stock Out Pending Approval';
if($transtype == 'a' && $itemstatus == 3) $stat = 'Stock Out Approved';
if($transtype == 'a' && $itemstatus == 4) $stat = 'Stock Out Rejected';

print <<< HERE
<table width="900px" border="0" align="center" style='table-layout:fixed'>
<tr><td>
HERE;
echo "<table width='900px' border='0' align='center' >";
if($export == 1) {
	if($transtype == 'o')$colspandt = "colspan='7'";
	if($transtype == 'p')$colspandt = "colspan='5'";
	if($transtype == 't')$colspandt = "colspan='9'";
	if($transtype == 'a')$colspandt = "colspan='8'";
}
else $colspandt = '';
echo "<tr><td align='right' $colspandt>";
echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
echo "<br>".$dt[1]." ".$dt[2];
echo "</td></tr>";
echo "<tr><td align='center' $colspandt>$transname</td></tr>";
if($itemstatus != '') echo "<tr><td align='center' $colspandt>Status: $stat</td></tr>";
echo "<tr><td><hr></td></tr>";
echo "</table></td></tr>";

if($transtype == 'p'){
echo "<tr><td><table width='900px' border='1' align='center'>
	              <col width=50>
				  <col width=50>
				  <col width=200>
				  <col width=75>
				  <col width=75>";
echo "<tr>";
echo "<th height='25' align='left'>Ref.</th>
			<th height='25' align='left'>Date</th>";
if ($Module_Project == 1)
				echo"<th align='left'>Project</th>";
if ($ShowCostCentLayout_Purchase == 1)			
				echo"<th align='left'>Costcenter</th>";
		echo"<th height='25' align='left'>Created By</th>";
		echo"<th height='25' align='left'>Ordered By</th>";
echo"</tr>";
echo "</table></td></tr>";



	$querysearch=" select distinct prequest.PRqst_id as preqst_id, prequest.prqst_reference as prqst_reference,date_format(prequest.prqst_date,'%d/%m/%Y') as prqst_date,
				prequest.project_code as prequestproject_code,prequest.costcent_code as prequestcostcent_code,prequestdt.project_code as project_code,prequestdt.costcent_code as costcent_code ,
				prequest.username as user_name ,costcent.costcent_name as costcent_name,project.project_name as project_name,costcent1.costcent_name prequestdtcostcent_name,
				project1.project_name as prequestdtproject_name , prequest.prqst_requestBy as UserOrder, PRqst_Priority
				from prequest ";
					
	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 (($Module_ProjectParent=1 and prequest.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and prequest.project_code = Q1.project_code )) ";
						
	$querysearch= $querysearch." left join project on project.project_code = prequest.project_code
								left join costcent on costcent.costcent_code = prequest.costcent_code
								,prequestdt left join items on items.item_code=prequestdt.item_code
								left join project project1 on project1.project_code = prequestdt.project_code
								left join costcent costcent1 on costcent1.costcent_code = prequestdt.costcent_code ";

	$querysearch= $querysearch." where prequest.prqst_id = prequestdt.prqst_id  
								 and if('$itemcode[0]'='',1=1,prequestdt.item_code='$itemcode[0]')
								 and if('$PRef'='',1=1,prequest.prqst_reference='$PRef')	
								 and date_format(prequest.prqst_date,'%Y-%m-%d') between ifnull('". explodedate($fromdate)."', date_format(prequest.prqst_date,'%Y-%m-%d')) and ifnull('". explodedate($todate)."', date_format(prequest.prqst_date,'%Y-%m-%d')) ";
								
	if ($PProjectCode != '')
		$querysearch= $querysearch." And prequest.project_code like concat('".$PProjectCode."','%') ";
	if ($PCostCentCode != '')
		$querysearch= $querysearch." And prequest.costcent_code like concat('".$PCostCentCode."','%') ";
	
	if ($PRequestedBy != '')
		$querysearch= $querysearch." And prequest.prqst_requestBy like concat('".$PRequestedBy."','%') ";
									 
	if($itemstatus != '') {
		if($itemstatus == '1') {
			$querysearch = $querysearch." and (ifnull(Prqst_Confirmed,0) = 0 and ifnull(Prqst_Cancelled,0) = 0)";
		} 
		else if($itemstatus == '2') {
			$querysearch = $querysearch." and ifnull(Prqst_Confirmed,0) = 1 and ((ifnull(PRqstDt_status,'x') = 'x' and 1 = 1) or (ifnull(PRqstDt_status,'x') = 'a' and PRqstDt_approvedlevel > $PrequestApproved_Level)) ";
		} 
		else if($itemstatus == '3') {
			$querysearch = $querysearch." and ifnull(Prqst_Confirmed,0) = 1 and (ifnull(PRqstDt_status,'x') = 'a' or ifnull(PRqstDt_status,'x') = 'u') and PRqstDt_approvedlevel <= $PrequestApproved_Level ";
		}
		else if($itemstatus == '4') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'r' ";
		}
		else if($itemstatus == '5') {
			$querysearch = $querysearch." and PRqstDt_Quantity=0 ";
		}
		else if($itemstatus == '6') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and not exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id) ";
		}
		else if($itemstatus == '7') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'x') ";
		} 
		else if($itemstatus == '8') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' ) ";
		}
		else if($itemstatus == '9') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'r' ) ";
		}
		else if($itemstatus == '10') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) != 1 and not exists (select 1 from GrnPurchasesDt where GrnPurchasesDt.RelatedDt_Id = porderdt.PorderDt_id and GrnPurchasesDt.Journal_Type = 'PI')) ";
		}
		else if($itemstatus == '11') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) != 1 and exists ( select 1 from GrnPurchasesDt where GrnPurchasesDt.RelatedDt_Id = porderdt.PorderDt_id and GrnPurchasesDt.Journal_Type = 'PI' )) ";
		}
		else if($itemstatus == '12') {
			$querysearch = $querysearch." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt_grnrejected,0) = 1 ) ";
		}
		else if($itemstatus == '13') {
			$querysearch = $querysearch." and ifnull(Prqst_Cancelled,0) = 1  ";
		}
	}
	
					
	if($priority != '') {
		$querysearch = $querysearch." and ifnull(PRqst_Priority,-1) = '".$priority."' ";
	}
				
	$querysearch = $querysearch." order by prequest.prqst_date"; 
	$resultsearch=mysql_query($querysearch,$connection);
	echo "<tr><td><table width='900px' border='0' align='left'> 				  
	              <col width=50>
				  <col width=50>
				  <col width=200>
				  <col width=75>
				  <col width=75>";
	while($rows=mysql_fetch_array($resultsearch)){
	extract($rows);
		    
			//echo"<tr><td ><font size='3'>$prqst_reference</font></td>	";
			//echo"<td ><font size='3'>$prqst_date</font></td>";
			//echo"<td ><font size='3'>$prequestproject_code</font></td>";
			
			echo"<tr><td ><span class='style1' ><font size='2'>$prqst_reference</font></span></td>";
			echo"<td ><span class='style1' align=left><font size='2'>$prqst_date</font></span></td>";
			echo"<td ><span class='style1'><font size='2'>$project_name</font></span></td>";
			
			
			
			echo"<td ><span class='style1'><font size='2'>$user_name</font></span></td>";
			echo"<td ><span class='style1'><font size='2'>$UserOrder</font></span></td>";
			echo"</tr>";
			
			
		if($checkDt == 1){		
			echo'<tr>';	
			echo"<td colspan='6' valign='top'>";
			echo"<table  border=1 width:900px;'  >
				  <col width=200>
				  <col width=400>
				  <col width=400>
				  <col width=50>
				  <col width=50>";
$subquery = "select prequestdt.item_code as item_code,items.item_description as item_description,
			PRqstDt_OQuantity,PRqstDt_CQuantity,PRqstDt_Quantity,PRqstDt_id,
			ifnull(QtyExists(prequestdt.item_code , '".$defaultwarehouse."', prequest.prqst_date, 0, 'PQ', '', ''),0) as qtyexists,
			if(ifnull(Prqst_Cancelled,0)=1, 'Cancelled',if(ifnull(Prqst_Confirmed,0)=0, 'Working On',if(PRqstDt_Quantity=0,'Transferred', if(ifnull(PRqstDt_status,'x')='x', 'Pending Approval', if(ifnull(PRqstDt_status,'x')='a', 'Approved', if(ifnull(PRqstDt_status,'x')='r', 'Rejected', '')))))) as PRqstDtStatus
			,Items.Item_UnitSalesDesc as Item_UnitSalesDesc, warehouse_code,prequestdt.item_description as newitem
			from prequestdt
			left join prequest on prequest.PRqst_id = prequestdt.PRqst_id
			left join items on items.item_code = prequestdt.item_code
			where prequestdt.PRqst_id=$preqst_id";

	if($itemstatus != '') {
		if($itemstatus == '1') {
			$subquery = $subquery." and (ifnull(Prqst_Confirmed,0) = 0 and ifnull(Prqst_Cancelled,0) = 0)";
		} 
		else if($itemstatus == '2') {
			$subquery = $subquery." and ifnull(Prqst_Confirmed,0) = 1 and ((ifnull(PRqstDt_status,'x') = 'x' and 1 = 1) or (ifnull(PRqstDt_status,'x') = 'a' and PRqstDt_approvedlevel > $PrequestApproved_Level)) ";
		} 
		else if($itemstatus == '3') {
			$subquery = $subquery." and ifnull(Prqst_Confirmed,0) = 1 and (ifnull(PRqstDt_status,'x') = 'a' or ifnull(PRqstDt_status,'x') = 'u') and PRqstDt_approvedlevel <= $PrequestApproved_Level ";
		}
		else if($itemstatus == '4') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'r' ";
		}
		else if($itemstatus == '5') {
			$subquery = $subquery." and PRqstDt_Quantity=0 ";
		}
		else if($itemstatus == '6') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and not exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id) ";
		}
		else if($itemstatus == '7') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'x') ";
		} 
		else if($itemstatus == '8') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' ) ";
		}
		else if($itemstatus == '9') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'r' ) ";
		}
		else if($itemstatus == '10') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) != 1 and not exists (select 1 from GrnPurchasesDt where GrnPurchasesDt.RelatedDt_Id = porderdt.PorderDt_id and GrnPurchasesDt.Journal_Type = 'PI')) ";
		}
		else if($itemstatus == '11') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt.porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) != 1 and exists ( select 1 from GrnPurchasesDt where GrnPurchasesDt.RelatedDt_Id = porderdt.PorderDt_id and GrnPurchasesDt.Journal_Type = 'PI' )) ";
		}
		else if($itemstatus == '12') {
			$subquery = $subquery." and ifnull(PRqstDt_status,'x') = 'u' and exists (select 1 from porderdt where porderdt.relateddt_id = prequestdt.PRqstDt_id and ifnull(porderdt_grnrejected,0) = 1 ) ";
		}
		else if($itemstatus == '13') {
			$subquery = $subquery." and ifnull(Prqst_Cancelled,0) = 1  ";
		}
	}


			//echo $subquery;
$subres = mysql_query($subquery,$connection);

		while($subrows=mysql_fetch_row($subres)){
			
		echo "<tr><td><font size='2'>$subrows[0]</font></td>";
		echo "<td><font size='2'>$subrows[1]</font></td>";
		echo "<td><font size='2'>$subrows[10]</font></td>";
		echo "<td><font size='2'>$subrows[4]</font></td>";
		echo "<td><font size='2'>$subrows[8]</font></td>";
		
	}
				  

	
echo "</table></td></tr>";
		}
//echo "<tr height='0'><td></td></tr>";
	}
	echo "</td></tr></table>";
}


if($transtype == 'o'){
echo "<tr><td><table width='900px' border='1' align='center'>

	              <col width=30>
				  <col width=70>
				  <col width=50>
				  <col width=225>
				  <col width=200>
				  <col width=25>
				  <col width=100>";
    
	echo "<tr>";
	echo "<th height='25' align='left'>Ref.</th>
	      <th height='25' align='left'>Date</th>
	      <th height='25' align='left'>Delivery  </th>
		  <th height='25' align='left'>Project</th>
	      <th height='25' align='left'>Supplier</th>
		  <th height='25' align='left'>Cur.</th>
		  <th height='25' align='left'>Total Price</th>";
  
echo"</tr>";
echo "</table></td></tr>";

if($checkDt == 1){
	echo'<tr>';	
			echo"<td colspan='18' valign='top'>";
			echo"<table  border=1 width:900px;'  >
				 <col width=200>
				  <col width=400>
				  <col width=75>
				  <col width=50>
				  <col width=50> 
				  <col width=100>
				  <col width=100>";
		//echo "<th align='left'><font size='2'>Item Code</font></th>";
			
			echo"<th align='left'><font size='2'>Item Code</font></th>
			     <th align='left'><font size='2'>Description</font></th>
			    <th align='left'><font size='2'>Quantity</font></th>
				<th align='left'><font size='2'>unit</font></th>
				<th align='left'><font size='2'>Cur.</font></th>
				<th align='left'><font size='2'>Price</font></th>
				<th align='left'><font size='2'>Total</font></th>";
echo "</table></td></tr>";
			

}

	$querysearch=" select distinct porder.porder_reference as porder_reference,date_format(porder.porder_date,'%d/%m/%Y') as porderdate,
				porder.porder_total as porder_total,porder.porder_id as porder_id,porder.project_code as porderproject_code,porder.costcent_code as pordercostcent_code,
				null as qtyexists,porder.user_name as user_name,project.project_name porderproject_name,costcent.costcent_name as pordercostcent_name ,
				date_format(porder.porder_requestdate,'%d/%m/%Y') as requestdate, (Porder.Porder_Total + IfNull(Porder.Porder_Vat,0) - IfNull(Porder.Porder_DiscountAmount,0) + (ifNull(Porder.Porder_Tax1,0) + ifNull(Porder.Porder_Tax2,0) + ifNull(Porder.Porder_Tax3,0) + ifNull(Porder.Porder_Tax4,0) + ifNull(Porder.Porder_Tax5,0))) As nettotal ,
				suppliers.Ledger_name as suppliername, Porder.porder_downperc as DownPer,
				Porder.porder_downamount as DownAmount,
				Porder.Porder_RequestBy as RequestedUser,porder.currency_code as PorderCur
				from porder ";
					
	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 (($Module_ProjectParent=1 and porder.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and porder.project_code = Q1.project_code )) ";
	}
						
						
	$querysearch= $querysearch." left join project on project.project_code = porder.project_code
								left join costcent on costcent.costcent_code = porder.costcent_code
								left join prequest  on prequest.prqst_id = porder.PRqst_id
								left join GrnPurchases on GrnPurchases.Porder_Ref = porder.porder_reference and GrnPurchases.Journal_Type = 'PI'
								left join suppliers on suppliers.ledger_number = porder.Supplier_code
								,porderdt left join items on items.item_code=porderdt.item_code
								left join project project1 on project1.project_code = porderdt.project_code
								left join costcent costcent1 on costcent1.costcent_code = porderdt.costcent_code ";
				
	$querysearch= $querysearch." where porder.porder_id = porderdt.porder_id 
								 and if('$itemcode[0]'='',1=1,porderdt.item_code='$itemcode[0]')
								 and if('$PRef'='',1=1,porder.porder_reference='$PRef')	
								 and date_format(porder.porder_date,'%Y-%m-%d') between ifnull('". explodedate($fromdate)."', date_format(porder.porder_date,'%Y-%m-%d')) and ifnull('". explodedate($todate)."', date_format(porder.porder_date,'%Y-%m-%d')) ";
						
	if ($PProjectCode != '')
		$querysearch= $querysearch." And porder.project_code like concat('".$PProjectCode."','%') ";
	if ($PCostCentCode != '')
		$querysearch= $querysearch." And porder.costcent_code like concat('".$PCostCentCode."','%') "; 
	if ($PRequestedBy != '')
		$querysearch= $querysearch." And Porder.Porder_RequestBy like concat('".$PRequestedBy."','%') ";
	
	if($itemstatus != '') {
		if($itemstatus == '1') {
			$querysearch = $querysearch." and ifnull(Porder_Confirmed,0) = 0 ";
		}
		else if($itemstatus == '2' && $Module_PorderConfirm != 1) {
			$querysearch = $querysearch." and ((ifnull(porderdt_approvedstatus,'x') = 'x' and 1 = 1) or (ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel > $PurorderApproved_Level)) ";
		} 
		else if($itemstatus == '2' && $Module_PorderConfirm == 1) {
			$querysearch = $querysearch." and ifnull(Porder_Confirmed,0) = 1 and ((ifnull(porderdt_approvedstatus,'x') = 'x' and 1 = 1) or (ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel > $PurorderApproved_Level)) ";
		} 
		else if($itemstatus == '3' && $Module_PorderConfirm != 1) {
			$querysearch = $querysearch." and ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel <= $PurorderApproved_Level ";
		}
		else if($itemstatus == '3' && $Module_PorderConfirm == 1) {
			$querysearch = $querysearch." and ifnull(Porder_Confirmed,0) = 1 and ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel <= $PurorderApproved_Level ";
		}
		else if($itemstatus == '4') {
			$querysearch = $querysearch." and ifnull(porderdt_approvedstatus,'x') = 'r' ";
		}
		else if($itemstatus == '12') {
			$querysearch = $querysearch." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 1 ";
		}
		else if($itemstatus == '10') {
			$querysearch = $querysearch." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 0 and not exists (select 1 from GrnPurchases where GrnPurchases.Porder_Ref = porder.Porder_Reference and GrnPurchases.Journal_Type = 'PI') ";
		}
		else if($itemstatus == '11') {
			$querysearch = $querysearch." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 0 and exists (select 1 from GrnPurchases where GrnPurchases.Porder_Ref = porder.Porder_Reference and GrnPurchases.Journal_Type = 'PI' ) ";
		}
	}

	
	$querysearch= $querysearch." order by porder.porder_date"; 
	$resultsearch=mysql_query($querysearch,$connection);
	echo "<tr><td><table width='900px' border='0' align='center'> 
	              <col width=30>
				  <col width=70>
				  <col width=50>
				  <col width=225>
				  <col width=200>
				  <col width=25>
				  <col width=100>";
    

while($rows=mysql_fetch_array($resultsearch)){
	extract($rows);
	
//	if(empty($totalpaidamount)) $totalpaidamount = 0;
//		$totalremainamount = $nettotal - $totalpaidamount;
		
//		  echo"<tr><td ><font size='3'>$porder_reference</font></td>	";

           echo "<tr>";
		   echo"<td ><font size='2'>$porder_reference</font></td>	";
		   echo"<td ><font size='2'>$porderdate</font></td>	";
		   echo"<td ><font size='2'>$requestdate</font></td>	";
		   echo"<td ><font size='2'>$porderproject_name</font></td>	";
		   echo"<td ><font size='2'>$suppliername</font></td>	";
		   echo"<td ><font size='2'>$PorderCur</font></td>	";
		   echo"<td ><font size='2'>".number_format($nettotal, 2)."</font></td>	";
           echo"</tr>";
		
		/*	echo "<tr>";
	        echo "<th height='25' align='left'>$porder_reference</th>
	              <th height='25' align='left'>$porderdate</th>
	              <th height='25' align='left'>$requestdate</th>
		          <th height='25' align='left'>$porderproject_code</th>
	              <th height='25' align='left'>$suppliername</th>
		          <th height='25' align='left'>$PorderCur</th>
		          <th height='25' align='left'>".number_format($nettotal, 2)."</th>";
  
            echo"</tr>";
			*/
		
		
		
		
		if($checkDt == 1){	
			echo'<tr>';	
			echo"<td colspan='18' valign='top'>";
			echo"<table  border=1 width:900px;'  >
				 <col width=200>
				  <col width=400>
				  <col width=75>
				  <col width=50>
				  <col width=50> 
				  <col width=100>
				  <col width=100>";
				  
$subquery = "select porderdt.item_code,items.item_description,
			if(ifnull(Porder_Confirmed,0)=0, 'Working On', if(ifnull(porderdt_grnrejected,0)=1, 'GRN Rejected', if(ifnull(porderdt_approvedstatus,'x')='x', 'Pending Approval', if(ifnull(porderdt_approvedstatus,'x')='r', 'Rejected', '')))),
			porderdt.warehouse_code,porderDt_Quantity,Items.Item_UnitSalesDesc,porder.currency_code, porderdt_price,(ifnull(porderdt_price,0)*ifnull(porderdt_quantity,0))
			from porderdt
			left join porder on porder.porder_id = porderdt.porder_id
			left join items on items.item_code = porderdt.item_code
			where porderdt.porder_id=$porder_id"; //echo $subquery;
			
			
	if($itemstatus != '') {
		if($itemstatus == '1') {
			$subquery = $subquery." and ifnull(Porder_Confirmed,0) = 0 ";
		}
		else if($itemstatus == '2' && $Module_PorderConfirm != 1) {
			$subquery = $subquery." and ((ifnull(porderdt_approvedstatus,'x') = 'x' and 1 = 1) or (ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel > $PurorderApproved_Level)) ";
		} 
		else if($itemstatus == '2' && $Module_PorderConfirm == 1) {
			$subquery = $subquery." and ifnull(Porder_Confirmed,0) = 1 and ((ifnull(porderdt_approvedstatus,'x') = 'x' and 1 = 1) or (ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel > $PurorderApproved_Level)) ";
		} 
		else if($itemstatus == '3' && $Module_PorderConfirm != 1) {
			$subquery = $subquery." and ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel <= $PurorderApproved_Level ";
		}
		else if($itemstatus == '3' && $Module_PorderConfirm == 1) {
			$subquery = $subquery." and ifnull(Porder_Confirmed,0) = 1 and ifnull(porderdt_approvedstatus,'x') = 'a' and porderdt_approvedlevel <= $PurorderApproved_Level ";
		}
		else if($itemstatus == '4') {
			$subquery = $subquery." and ifnull(porderdt_approvedstatus,'x') = 'r' ";
		}
		else if($itemstatus == '12') {
			$subquery = $subquery." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 1 ";
		}
		else if($itemstatus == '10') {
			$subquery = $subquery." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 0 and not exists (select 1 from GrnPurchases where GrnPurchases.Porder_Ref = porder.Porder_Reference and GrnPurchases.Journal_Type = 'PI') ";
		}
		else if($itemstatus == '11') {
			$subquery = $subquery." and ifnull(porderdt_approvedstatus,'x') = 'a' and ifnull(porderdt_grnrejected,0) = 0 and exists (select 1 from GrnPurchases where GrnPurchases.Porder_Ref = porder.Porder_Reference and GrnPurchases.Journal_Type = 'PI' ) ";
		}
	}

			
			
			
			
			
			
$subres = mysql_query($subquery,$connection);

		while($subrows=mysql_fetch_row($subres)){
			$$val = $subrows[0];
		$i_lpurp = 0;
				$sub_query = " SELECT if($PurchaseTTC = 1, PurchaseDt_PriceTTCBase1, PurchaseDt_PriceBase1), ifNull(PurchaseDt_DiscountPer,0), Purchase_Date FROM PurchasesDt ".
							 " WHERE Item_code = '".$$val."' And ifNull(PurchaseDt_Price,0) > 0 ".
							 " Order By Purchase_Date Desc Limit 1 "; 
				$sub_result = mysql_query($sub_query,$connection);
				$sub_row = mysql_fetch_row($sub_result);
				$i_lpurp = $sub_row[0];
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$i_lpurp = ConvertCurrency($Base1, $txtCurrCode, insertMySQLDate($txtDate), $i_lpurp, $connection);
				if(!isset($i_lpurp)) $i_lpurp = 0;
				$txtLastPurPrice = $i_lpurp;
				
		echo "<tr><td><font size='2'>$subrows[0]</font></td>";
		echo "<td><font size='2'>$subrows[1]</font></td>";
		echo "<td><font size='2'>$subrows[4]</font></td>";
		echo "<td><font size='2'>$subrows[5]</font></td>";
		echo "<td><font size='2'>$subrows[6]</font></td>";
		echo "<td><font size='2' >".number_format($subrows[7], 2)."</font></td>";
		echo "<td><font size='2' >".number_format($subrows[8], 2)."</font></td></tr>";
	
	}
				  

	
echo "</table></td></tr>";
		}
echo "<tr height='10'><td></td></tr>";
	}
	echo "</td></tr></table>";
}

if($transtype == 't'){
echo "<tr><td><table width='900px' border='0' align='center'><col width=30><col width=30><col width=90>";
	if ($Module_Project == 1) echo "<col width=200><col width=200>";
	if ($ShowCostCentLayout_Transfer == 1) echo "<col width=90><col width=90>";
	
echo "<tr>";
	echo "<th height='25' align='left'>Ref.</th><th height='25' align='center'>MSR</th>
			<th height='25' align='center'>Date</th>";
	if ($Module_Project == 1){
				echo"<th align='center'>From Project</th>";
				echo"<th align='center'>To Project</th>";
			}
			if ($ShowCostCentLayout_Transfer == 1){			
				echo"<th align='center'>From Costcenter</th>";
				echo"<th align='center'>To Costcenter</th>";
			}
echo"</tr>";
echo "</table></td></tr>";
if($checkDt == 1){
	echo'<tr>';	
			echo"<td colspan='18' valign='top'>";
			echo"<table  width:900px;'  >
				    <col width=30>
				  <col width=300>
				  <col width=120>
				  <col width=120>
				  <col width=80>
				  <col width=80>
				  <col width=80>
				  <col width=80>
				  <col width=100>";
		echo "<th></th><th align='left'><font size='2'>Item Code \ Description</font></th>";
			echo "<th align='center'><font size='2'>From Warehouse</font></th><th align='center'><font size='2'>To Warehouse</font></th>";
			echo"<th align='center'><font size='2'>Ord.Qty</font></th>
				<th align='center'><font size='2'>Canc.Qty</font></th>
				<th align='center'><font size='2'>Qty</font></th>
				<th align='center'><font size='2'>Unit</font></th>
				<th align='center'><font size='2'>Status</font></th>";
echo "</table></td></tr>";
			

}

	$querysearch_transfer=" select distinct transferrequest.transferrequest_reference as transferrequest_reference,date_format(transferrequest.transferrequest_date,'%d/%m/%Y') as transferrequest_date,
						transferrequest.transferrequest_id ,
						transferrequest.Project_code1,transferrequest.Project_code2,transferrequest.Costcenter_Code,transferrequest.ToCostcenter_Code ,
						project.project_name transferproject_name1	,project2.project_name transferproject_name2 ,costcent.costcent_name as transfercostcent_name1	,
						costcent2.costcent_name as transferdtcostcent_name2, prequest.PRqst_reference
				from transferrequest
				left join project on project.project_code = transferrequest.project_code1
				left join project project2 on project2.project_code = transferrequest.project_code2
				left join costcent on costcent.costcent_code = transferrequest.costcenter_code
				left join costcent costcent2 on costcent2.costcent_code = transferrequest.ToCostcenter_Code ";
				
			
	 $querysearch_transfer= $querysearch_transfer."	left join prequest  on prequest.prqst_id = transferrequest.prqst_id
				,transferrequestdt 
				,items
				where transferrequest.transferrequest_id = transferrequestdt.transferrequest_id  
				and items.item_code=transferrequestdt.item_code
				and if('$itemcode[0]'='',1=1,transferrequestdt.item_code='$itemcode[0]')
				and if('$PRef'='',1=1,transferrequest.transferrequest_reference='$PRef')
				and date_format(transferrequest.transferrequest_date,'%Y-%m-%d') between ifnull('". explodedate($fromdate)."', date_format(transferrequest.transferrequest_date,'%Y-%m-%d')) and ifnull('". explodedate($todate)."', date_format(transferrequest.transferrequest_date,'%Y-%m-%d'))";
				
	if ($PProjectCode != '')
		$querysearch_transfer= $querysearch_transfer." And transferrequest.project_code1 like concat('".$PProjectCode."','%') ";
	if ($PCostCentCode != '')
		$querysearch_transfer= $querysearch_transfer." And transferrequest.costcenter_code like concat('".$PCostCentCode."','%') ";
	
	if($itemstatus != '') {
		if($itemstatus == '2') {
			$querysearch_transfer = $querysearch_transfer." and ((ifnull(transferrequestdt_status,'x') = 'x' and 1 = 1) or (ifnull(transferrequestdt_status,'x') = 'a' and TransferrequestDt_approvedlevel > $TransferRequestApproved_Level)) ";
		} 
		else if($itemstatus == '3') {
			$querysearch_transfer = $querysearch_transfer." and ifnull(transferrequestdt_status,'x') = 'a' and TransferrequestDt_approvedlevel <= $TransferRequestApproved_Level ";
		}
		else if($itemstatus == '4') {
			$querysearch_transfer = $querysearch_transfer." and ifnull(transferrequestdt_status,'x') = 'r' ";
		}
	}
				
	$querysearch_transfer= $querysearch_transfer." Order By transferrequest.transferrequest_date  "; 
	$resultsearch=mysql_query($querysearch_transfer,$connection);
	echo "<tr><td><table width='900px' border='0' align='center'><col width=30><col width=30><col width=90>";
	if ($Module_Project == 1) echo "<col width=200><col width=200>";
	if ($ShowCostCentLayout_Purchase == 1) echo "<col width=90><col width=90>";
	while($rows=mysql_fetch_array($resultsearch)){
	extract($rows);
	
		echo"<tr><td ><font size='3'>$transferrequest_reference</font></td>	";
		
			echo"	<td ><font size='3'>$PRqst_reference</font></td>";
			echo "	<td><font size='3'>$transferrequest_date</font></td>";	
			if ($Module_Project == 1) {
				echo"<td><font size='3'>$Project_code1</font></td>
					 <td><font size='3'>$Project_code2</font></td>";
			}
			if ($ShowCostCentLayout_Transfer == 1) {
				echo"<td><font size='3'>$Costcenter_Code</font></td>
					 <td><font size='3'>$ToCostcenter_Code</font></td>";
			}
			echo "</tr>";
			if($export == 1) $colnb = "colspan='9'";
			else $colnb = "colspan='18'";
		if($checkDt == 1){	
			echo'<tr>';	
			echo"<td $colnb valign='top' style='border: 1px solid black;'>";
			echo"<table  width:900px;'  >
				 <col width=30>
				  <col width=300>
				  <col width=120>
				  <col width=120>
				  <col width=80>
				  <col width=80>
				  <col width=80>
				  <col width=80>
				  <col width=100>";
$subquery = "select transferrequestdt.item_code,items.item_description,TransferrequestDt_WarehouseFrom,TransferrequestDt_WarehouseTO,transferrequestdt_Quantity, TransferrequestDt_Price,Item_UnitSalesDesc,
			if(ifnull(transferrequestdt_status,'x')='x', 'Pending Approval', if(ifnull(transferrequestdt_status,'x')='a', 'Approved', if(ifnull(transferrequestdt_status,'x')='r', 'Rejected', '')))
			from transferrequestdt
			left join items on items.item_code = transferrequestdt.item_code
			where transferrequestdt.transferrequest_id = $transferrequest_id;"; //echo $subquery;
$subres = mysql_query($subquery,$connection);

		while($subrows=mysql_fetch_row($subres)){
		
				
		echo "<tr><td></td><td><font size='2'>$subrows[0]/$subrows[1]</font></td>";
		echo "<td><font size='2'>$subrows[2]</font></td>";
		echo "<td><font size='2'>$subrows[3]</font></td>";
		echo "<td><font size='2'>$subrows[4]</font></td>";
		echo "<td><font size='2'>$subrows[5]</font></td>";
		echo "<td><font size='2'>$subrows[6]</font></td>";
		echo "<td><font size='2'>$subrows[7]</font></td></tr>";
	
	}
				  

	
echo "</table></td></tr>";
		}
echo "<tr height='10'><td></td></tr>";
	}
	echo "</td></tr></table>";
}

if($transtype == 'a'){
echo "<tr><td><table width='900px' border='0' align='center'><col width=30><col width=90>";
	if ($Module_Project == 1) echo "<col width=200>";
	if ($ShowCostCentLayout_Adjustment == 1) echo "<col width=90>";
	
echo "<tr>";
	echo "<th height='25' align='left'>Ref.</th><th height='25' align='center'>Date</th>";
	if ($Module_Project == 1) echo"<th align='center'>Project</th>";
	if ($ShowCostCentLayout_Adjustment == 1) echo"<th align='center'>Costcenter</th>";
				
echo"</tr>";
echo "</table></td></tr>";
if($checkDt == 1){
	echo'<tr>';	
			echo"<td colspan='18' valign='top' >";
			echo"<table  width:900px;'  >
				    <col width=30>
				  <col width=300>
				  <col width=120>";
				  if ($Module_Project == 1) echo "<col width=150>";
			if ($ShowCostCentLayout_Adjustment == 1)  echo "<col width=90>";
				 echo "<col width=80>
				  <col width=80>
				  <col width=80>";
		echo "<tr><th></th><th align='left'><font size='2'>Item Code \ Description</font></th>";
		echo "<th align='center'><font size='2'>Warehouse</font></th>";
			if ($Module_Project == 1) echo"<th align='center'><font size='2'>Project</font></th>";
			if ($ShowCostCentLayout_Adjustment == 1) echo"<th align='center'><font size='2'>Costcenter</font></th>";
		echo "<th align='center'><font size='2'>Qty</font></th>
				<th align='center'><font size='2'>Unit</font></th>
				<th align='center'><font size='2'>Status</font></th></tr>";
echo "</table></td></tr>";
			

}

	$querysearch_adjs=" select distinct adjustmentrequest.adjustmentrequest_reference as adjustmentrequest_reference ,date_format(adjustmentrequest.adjustmentrequest_date,'%d/%m/%Y') as
					adjustmentrequest_date,adjustmentrequest.adjustmentrequest_id as adjustmentrequest_id ,
					adjustmentrequest.Project_Code as HeaderProject_Code,adjustmentrequest.Costcent_Code as HeaderCostcent_Code
				from adjustmentrequest ";
	if ($projectexist >	0)
		$querysearch_adjs= $querysearch_adjs." 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 (($Module_ProjectParent=1 and adjustmentrequest.project_code  like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and adjustmentrequest.project_code  = Q1.project_code )) ";
						
	$querysearch_adjs= $querysearch_adjs." left join project on project.project_code = adjustmentrequest.project_code
				left join costcent on costcent.costcent_code = adjustmentrequest.costcent_code
				,adjustmentrequestdt
				left join project project1 on project1.project_code = adjustmentrequestdt.project_code ";
				
			
	$querysearch_adjs= $querysearch_adjs."left join costcent costcent1 on costcent1.costcent_code = adjustmentrequestdt.costcent_code
				,items
				where adjustmentrequest.adjustmentrequest_id = adjustmentrequestdt.adjustmentrequest_id  
				and items.item_code=adjustmentrequestdt.item_code
				and if('$itemcode[0]'='',1=1,adjustmentrequestdt.item_code='$itemcode[0]')
				and if('$PRef'='',1=1,adjustmentrequest.adjustmentrequest_reference='$PRef')
				and date_format(adjustmentrequest.adjustmentrequest_date,'%Y-%m-%d') between ifnull('". explodedate($fromdate)."', date_format(adjustmentrequest.adjustmentrequest_date,'%Y-%m-%d')) and ifnull('". explodedate($todate)."', date_format(adjustmentrequest.adjustmentrequest_date,'%Y-%m-%d')) ";

	if($itemstatus != '') {
		if($itemstatus == '2') {
			$querysearch_adjs = $querysearch_adjs." and ((ifnull(adjustmentrequestdt_status,'x') = 'x' and 1 = 1) or (ifnull(adjustmentrequestdt_status,'x') = 'a' and adjustmentrequestdt_approvedlevel > $AdjustmentRequestApproved_Level)) ";
		} 
		else if($itemstatus == '3') {
			$querysearch_adjs = $querysearch_adjs." and ifnull(adjustmentrequestdt_status,'x') = 'a' and adjustmentrequestdt_approvedlevel <= $AdjustmentRequestApproved_Level ";
		}
		else if($itemstatus == '4') {
			$querysearch_adjs = $querysearch_adjs." and ifnull(adjustmentrequestdt_status,'x') = 'r' ";
		}
	}
	
	if ($PProjectCode != '')
		$querysearch_adjs= $querysearch_adjs." And adjustmentrequest.project_code like concat('".$PProjectCode."','%') ";
	if ($PCostCentCode != '')
		$querysearch_adjs= $querysearch_adjs." And adjustmentrequest.costcent_code like concat('".$PCostCentCode."','%') ";
		
	$querysearch_adjs= $querysearch_adjs." Order By adjustmentrequest.adjustmentrequest_date  "; 
	$resultsearch=mysql_query($querysearch_adjs,$connection);
	echo "<tr><td><table width='900px' border='0' align='center'><col width=30><col width=90>";
	if ($Module_Project == 1) echo "<col width=200>";
	if ($ShowCostCentLayout_Adjustment == 1) echo "<col width=90>";
	while($rows=mysql_fetch_array($resultsearch)){
	extract($rows);
	
		echo"<tr><td ><font size='3'>$adjustmentrequest_reference</font></td>	";
		
			echo"	<td ><font size='3'>$adjustmentrequest_date</font></td>";
			if ($Module_Project == 1) {
				echo"<td><font size='3'>$HeaderProject_Code</font></td>";
			}
			if ($ShowCostCentLayout_Adjustment == 1) {
				echo"<td><font size='3'>$HeaderCostcent_Code</font></td>";
			}
			echo "</tr>";
			if($export == 1) $colnb = "colspan='8'";
			else $colnb = "colspan='18'";
		if($checkDt == 1){	
			echo'<tr>';	
			echo"<td $colnb valign='top' style='border: 1px solid black;'>";
			echo"<table  width:900px;'  >
				      <col width=30>
				  <col width=300>
				  <col width=120>";
				  if ($Module_Project == 1) echo "<col width=150>";
			if ($ShowCostCentLayout_Adjustment == 1)  echo "<col width=90>";
				 echo "<col width=80>
				  <col width=80>
				  <col width=80>";
	
$subquery = "select adjustmentrequestdt.item_code, item_description, warehouse_code,adjustmentrequestdt.Project_Code,adjustmentrequestdt.costcent_code, adjustmentRequestdt_quantity, Item_UnitSalesDesc,
			if(ifnull(adjustmentrequestdt_status,'x')='x', 'Pending Approval', if(ifnull(adjustmentrequestdt_status,'x')='a', 'Approved', if(ifnull(adjustmentrequestdt_status,'x')='r', 'Rejected', '')))
			from adjustmentrequestdt
			left join adjustmentrequest on adjustmentrequest.adjustmentrequest_id=adjustmentrequestdt.adjustmentrequest_id
			left join items on items.item_code = adjustmentrequestdt.item_code
			where adjustmentrequestdt.adjustmentrequest_id=$adjustmentrequest_id;"; //echo $subquery;
$subres = mysql_query($subquery,$connection);

		while($subrows=mysql_fetch_row($subres)){
		
				
		echo "<tr><td></td><td><font size='2'>$subrows[0]/$subrows[1]</font></td>";
		echo "<td><font size='2'>$subrows[2]</font></td>";
	 if ($Module_Project == 1)	echo "<td><font size='2'>$subrows[3]</font></td>";
	if ($ShowCostCentLayout_Adjustment == 1) echo "<td><font size='2'>$subrows[4]</font></td>";
		echo "<td><font size='2'>$subrows[5]</font></td>";
		echo "<td><font size='2'>$subrows[6]</font></td>";
		echo "<td><font size='2'>$subrows[7]</font></td></tr>";
	
	}
				  

	
echo "</table></td></tr>";
		}
echo "<tr height='10'><td></td></tr>";
	}
	echo "</td></tr></table>";
}

echo "</table>";










function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
function explodedate($date)
{
	$trim = explode('/',$date);
	$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
	return $trim;
}
function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result = selectData($connection, $query);
	//echo $query;
	Return	$result[0];

}

function insertMySQLDate($data){
	$data = insertDate($data);
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2)." ".strftime("%H").":".strftime("%M").":".strftime("%S");;
}
function insertDate($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}
function selectData($conn, $query){
	$result = mysql_query($query, $conn);
	$cur = mysql_fetch_row($result);		
	return $cur;
}
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

?>