<?php

session_start();
if($_SESSION['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($_SESSION['dbName'],$connection) or die("Could not find DB");

							
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

//Read From Login Table
$querylog=" select ifNull(Login_ModifyAllPending,0) As Login_ModifyAllPending, ifNull(FC_CashRegisterDel,0) As FC_CashRegisterDel, ifNull(FC_CashRegisterMod,0) As FC_CashRegisterMod, 
			ifnull(Retail_Cancel,0) as Retail_Cancel".
          " 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);
}

$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;	
$queryCashRegister=" Select Upper(CashRegister_Name) As CashRegister_Name, Currency_Code As StpCurrCode, CostCenter_Code As StpCostCentCode, Project_Code As StpProjectCode, ".
                   " ifNull(CashRegister_BarCode,0) As CashRegisterBarCode, ifNull(CashRegister_SerialNo,0) As CashRegisterSerialNo, ifNull(CashRegister_Discount,0) As CashRegisterDiscount, ".
				   " CashRegisterTTC, Client_Account As StpClientAccount, ifNull(CashRegister_SlipPrinter,0) As CashRegister_SlipPrinter, ifNull(CashRegister_SlipRePrinter,0) As CashRegister_SlipRePrinter, ".
				   " ifNull(CashRegister_AutoPost,0) As CashRegister_AutoPost, ifNull(CashReg_Vendor,0) As CashReg_Vendor, ".
				   " ifNull(CashRegister_DisplayLed,0) As CashRegister_DisplayLed, ifNull(CashRegister_DispCOM,0) As CashRegister_DispCOM, ".
				   " ifNull(CashRegister_PrintBarcode,0) As CashRegister_PrintBarcode, ifNull(CashReg_ForcingVendor,0) As CashReg_ForcingVendor, ".
				   " ifNull(CashRegister_LedDescLength,0) As CashRegister_LedDescLength, ifNull(CashRegister_Printer,0) As CashRegister_Printer, ".
				   " ifNull(Module_CashRegisterFastItem,0) As Module_CashRegisterFastItem, ifNull(Module_RestaurantDelivery,0) As Module_RestaurantDelivery, ".
				   " ifNull(Module_CashRegisterStandardView,0) As Module_CashRegisterStandardView, ifNull(Module_CashRegisterTable,0) As Module_CashRegisterTable, ".
				   " ifNull(Module_CashRegAutoItemsList,0) As Module_CashRegAutoItemsList, ifNull(CashRegister_ShowTaxNonTax,0) as CashRegister_ShowTaxNonTax,  ".
				   " ifNull(CashRegister_Drawer,0) As CashRegister_Drawer, ifNull(CashRegister_SlipKitchen,0) As CashRegister_SlipKitchen, ".
				   " ifNull(CashRegister_SlipCopy,1) As CashRegister_SlipCopy ".
				   " ,ifNull(cashregister_logout,1) As cashregister_logout ".
				   " ,ifNull(cashregister_retailtype,1) As cashregister_retailtype ,ifNull(cashregister_printPending,1) As cashregister_printPending ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
				 //echo$queryCashRegister;
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerstk  ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];


$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$searchby   = $_POST['searchby']; if ($searchby == ""){$searchby = $_GET["searchby"];}
$orderby    = $_POST['orderby']; if ($orderby == ""){$orderby = $_GET["orderby"];}
$perpage    = $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange = $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
HERE;


if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}

print <<<HERE
<title>Search Pending Invoices</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.search.keyword.value='';
  document.search.action='Form_RetailSearchPending.php';
  document.search.submit();

}

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

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

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


function DeleteRecord(RetId, RetRef){

	if (RetId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+RetRef+" ?") )
		{
			document.search.action="Form_Retail_Actions.php?mainmode=deletepending&SRetId="+RetId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(RetId){
		
	opener.document.retail.txtId.value=RetId;
	opener.document.retail.mainaction.value='edit';
	opener.document.retail.onhold.value='1';
	opener.document.retail.actionmode.value='';
	opener.document.retail.action='Form_Retail.php';
	opener.document.retail.submit();
	self.close();
	
}

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

function SubmitForm()
{	

  //document.search.keyword.value='';
  document.search.action='Form_RetailSearchPending.php?startrange=0';
  document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_RetailSearchPending.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_RetailSearchPending.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function RefreshPage()
{
  document.search.action='Form_RetailSearchPending.php?startrange=0';	
  document.search.submit();

}

function OpenKeyboard(field){
	VirtualKeyboard.attachInput(field);
	VirtualKeyboard.open(field,'td');
}

function PrintRetailTemp(RetId, ReportName, SlipCopy,LastRetail) {
	var x = document.search;
	
	var vMainMode = '';
	
	var str = '';
	if (SlipCopy == '') SlipCopy = 1;
	
	str = ReportName+'?TempRep=1&RetailId='+RetId;
	
	if(LastRetail==1 && RetId !='')
return;
	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 = $.parseJSON(xmlhttp.responseText);
			var msg = data[0].message;
			var AccessDenied = data[0].AccessDenied;
			
			if (msg !=''){
				alert(msg);
			} else {
				if (AccessDenied != 1) {
					for (var i=1; i<=SlipCopy; i++) {
						SlipTitle = 'Form_RetailSlip'+i;
						window.open(str, SlipTitle, 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	
					}	
				} else {
					alert('Access Denied!.');
				}	
			}									
		}			
	}			
			
	xmlhttp.open("GET","Form_Retail_Actions.php?mode=updateuseraccess&mainmode="+vMainMode+"&SRetId="+RetId+"&LastRetail="+LastRetail);
	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;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: Black ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body OnLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
HERE;


$PRef = GetSQLValueString($keyword, 'text');

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
	
$querysearch=" Select Distinct Retail.Sale_ID, Retail.Sale_Reference, Date_Format(Retail.Sale_Date,'%d/%m/%Y %h:%i %p') As Sale_Date, ".
             " Retail.Currency_code, Retail.Client_Code As ClientCode, Clients.Ledger_name As ClientName, Retail.CashRegister_Name, ".
			 " Round((ifNull(Retail.Sale_Taxable,0) + ifNull(Retail.Sale_NoneTaxable,0) + ifNull(Retail.Sale_VAT,0) - ifNull(Retail.Sale_DiscountAmount,0) + ".
			 " (ifNull(Retail.Sale_Tax1,0) + ifNull(Retail.Sale_Tax2,0) + ifNull(Retail.Sale_Tax3,0) + ifNull(Retail.Sale_Tax4,0) + ifNull(Retail.Sale_Tax5,0))), ".$DecimalBase.") As NetTotal,  ".
			 " Retail.Journal_Type As RetailType, ProjectDt.ProjectDt_Name As ProjectDtName, guest_name as guestname,Customer.Customer_Name As CustomerName, Retail.Sale_Remark   ".
			 " From Temp_Retail As Retail Left Join Clients on Retail.Client_Code = Clients.Ledger_Number ".
			 " Left Join ProjectDt on Retail.ProjectDt_Id = ProjectDt.ProjectDt_Id ".
			 " Left Join Customer on Retail.Customer_Code = Customer.Customer_Code ";
			 
$querysearch= $querysearch.											 
			 " Where ifNull(Sale_Reference,-1) = ifNull($PRef, ifNull(Sale_Reference,-1)) ".
			 " And (($Login_ModifyAllPending = 0 and Logged_Code = '".$_SESSION['useraccount']."') or ($Login_ModifyAllPending = 1)) ";
			 // " And (($Login_ModifyAllPending = 0 and Logged_Code = '".$_SESSION['useraccount']."') or ($Login_ModifyAllPending = 1 and 1 = 1)) ";

			 
$querysearch= $querysearch." Order By Retail.Sale_Date Desc, Sale_Reference ";
			 
//echo $querysearch;			 

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


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed'  width='992px'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:930px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Pending Invoices
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"<table border='0' cellspacing='1' style='table-layout:fixed'>
	  <col width=20>
	  <col width=20>
	  <col width=100>
	  <col width=150>
	  <col width=50>
	  <col width=100>
	  <col width=210>
	  <col width=60>
	  <col width=120>
	
	  <tr class='Search-header-bg'>
	    <td align='left' colspan='3'><span class='style4'>Search by Reference</span></td>
		<td align='left' colspan='7'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30'
		 /></span>
		  <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='clearform();return false;' title='Cancel'><span><span>Cancel</span></span></button>
		</td>";
		
echo"</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

  echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:930px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\">";
              
			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>
			<td  style="width:74px;border-right:0px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>";
	echo"<div style='overflow:scroll;height:400px;width:930px;'>";
	echo"<table border='0'  cellpadding='0' cellspacing='1' style='width:960px;'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=80>
	  <col width=130>
	  <col width=80>
	  <col width=50>
	  <col width=60>
	  <col width=120>
	  <col width=100>
	  <col width=300>";
	  
	echo"<tr class='Search-Label-bg'>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Hold Ref</span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>
		<td  height='25' align='center'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Type</span></td>
		<td  height='25' align='center'><span class='style4'>Currency</span></td>
		<td  height='25' align='center'><span class='style4'>Total</span></td>
		<td  height='25' align='center'><span class='style4'>Table Name</span></td>
		<td  height='25' align='center'><span class='style4'>Customer</span></td>
	  </tr>";
		

	

	

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

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
		

	echo'<tr '.$Color.'>
			<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$Sale_ID.');" title="Edit Record" ></span></td>';
			
	if ($Retail_Cancel == 1)		
		echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$Sale_ID.', \''.$Sale_Reference.'\');" title="Delete Record" ></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';	
		
	echo'<td align="center"><span class="style5"><img src="img/details.gif" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Sale_ID.', \''.'S'.'\','.$startrange.');" title="Show Details" style="width:15px; height:15px; "></span></td>';
	if ($cashregister_printPending == 1 ) {
	echo "<td align='center'><span class='style5'><img src='img/print.png' onMouseOver=\"style.cursor='hand'\" onclick=\"PrintRetailTemp('$Sale_ID','Form_RetailSlip.php','$CashRegister_SlipCopy');\" title='Edit Record' style='width:16px;height:16px;'></span></td>";	
	} else 
	echo "<td align='center'></td>";			

	echo'	<td align="left"><span class="style5">'.$Sale_Remark.'</span></td>
			<td align="center"><span class="style5">'.$Sale_Date.'</span></td>
			<td align="left"><span class="style5">'.$Sale_Reference.'</span></td>
			<td align="center"><span class="style5">'.$RetailType.'</span></td>
			<td align="center"><span class="style5">'.$Currency_code.'</span></td>
			<td align="right"><span class="style5">'.number_format($NetTotal, $DecimalBase).'</span></td>
			<td align="Center"><span class="style5">'.$ProjectDtName.'</span></td>
			<td align="Center"><span class="style5">'.$guestname.'</span></td>
		</tr>';

		
		
		$querydt = " Select RetailDt.SaleDt_id, RetailDt.Item_Barcode, RetailDt.Item_code, Items.Item_Description, RetailDt.Warehouse_Code, ".
				   " SaleDt_Quantity, if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(SaleDt_Price, ".$DecimalBase."), 0), SaleDt_PriceBase1, SaleDt_PriceBase2, ".
				   " if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(SaleDt_DiscountPer, ".$DecimalBase."),0),  ".
				   " if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(((SaleDt_Quantity*SaleDt_Price)-((SaleDt_Quantity*SaleDt_Price*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase."), 0) As Total, ".
				   " if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(SaleDt_PriceTTC, ".$DecimalBase."), 0), ".
				   " if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(((SaleDt_Quantity*SaleDt_PriceTTC)-((SaleDt_Quantity*SaleDt_PriceTTC*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase."), 0) As TotalTTC, ".
				   " ifNull(Items.Item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, ".
				   " RetailDt.Item_CodePacked, ifNull(Items1.Item_withserialno,0) As ItemPackWithSerialNo, RetailDt.Vendor_Code, if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, Round(RetailDt.SaleDt_ActualPrice, ".$DecimalBase."), 0), ".
				   " if(ifNull(RetailDt.SaleDt_ItemStatus,0)!=3, ifNull(RetailDt.Price_Offer,0), 0) As Price_Offer, ifNull(Items.Item_Weight,0), RetailDt.Slip_Number, RetailDt.Warehouse_Code, ".
				   " ifNull(RetailDt.SaleDt_ItemStatus,0), SalesDt_Remark, saledt_printed ".
				   " From Temp_RetailDt As RetailDt ".
				   " Left Join Items on RetailDt.Item_code = Items.Item_code ".
				   " Left Join Items As Items1 on RetailDt.Item_CodePacked = Items1.Item_code ".
				   " Left Join Temp_Purchasesserialno on RetailDt.SaleDt_ID = Temp_Purchasesserialno.RetailDt_ID ".
				   " And Temp_Purchasesserialno.Journal_Type In ('DI','DR') ".
				   " Where RetailDt.Sale_ID = ".$Sale_ID.
				   " Group By  RetailDt.SaleDt_id ".
				   " Order By ifNull(RelatedDt_Id, RetailDt.SaleDt_ID) Desc, Sale_Date ";

	$sub_result=mysql_query($querydt,$connection) or die(mysql_error($connection));	
	$sub_totalrows = mysql_num_rows($sub_result);
		
	if ($sub_totalrows > 0) {
		$SlideDivId = 'displaydetails'.$Sale_ID;
			
		echo "<tr ".$Color.">";
		echo "<td colspan='18' valign='top' align='left'><div id='$SlideDivId' class='div-search-details' style='display:visible; margin-left:100px;'>";
		echo "<table border='0' style='table-layout:fixed; width:320px;' cellpadding='0' cellspacing='1'>";
		echo "<tr>";
		echo"<th align='left' class='bg-header style27'>Item Code</th>";	
		echo"	<th align='left' class='bg-header style27'>Item Description</th>";
		echo"  	<th align='center' class='bg-header style27'>Qty</th>";
		
		
		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='left' valign='top' class='style9'>".$sub_rows[5]."</td>";	
			
  			echo "</tr>";
				
		}
			
		echo "</table></div></td>";
		echo "</tr>";
	}
		
		}
	
			
		
echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";					
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:1040px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:0px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='74px' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='380px'>&nbsp;";
		
		//include("toolbar.php");
		echo"</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";

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


          

		  

print <<<HERE
</form>
</body>

HERE;

?>