<?php

function ShowCallingRecords($connection,$arrayparams){


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
 
		
	echo"<tr height = 600 valign = top><td id='search-content1'></td><td>";

echo '  <table width="450" border="0" cellspacing="1" align="center">';

if ($startrange1=='') $startrange1 = 0;
if ($perpage1=='')	 $perpage1 = 50;





			
$sub_query= " Select concat(substring(callerid_date,-2,2),'/',substring(callerid_date,1,2)) callerid_date,concat(substring(callerid_time,1,2),':',substring(callerid_time,3,4)) callerid_time
			,Customer.Customer_Code As Ledger_Number, Customer.Customer_Name As Ledger_Name, ".
			" caller_id_status,Concat(Customer_Name, ' Tel#: ', Customer_Telephone1) As CustomerRemark, Customer.Customer_Telephone1,callerid_id ,".
			" Concat(Customer_Name, if(ifnull(Customer_Telephone1,'') != '', concat('<br>Tel#: ', Customer_Telephone1), ''), if(ifnull(Customer_Address,'') != '', concat('<br>Address: ', Customer_Address), '')) As CustomerRemark2 ".
			" From CallerId left join Customer on CallerId.callerid_number = Customer.Customer_Telephone1".
			" Where (ifnull(Customer.Customer_Code,'') like '%".$PLedgerNo1."%' or ifnull(Customer.Customer_Name,'') like '%".$PLedgerNo1."%' or ifnull(Customer.Customer_Telephone1,'') like '%".$PLedgerNo1."%' ) ".
			" And Caller_id_Status  = 0 ".
			" Order By caller_id_status asc";
		
//echo $sub_query;
$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
$totalrows1 = mysql_num_rows($sub_result);

if($totalrows1=='' or $totalrows1==0){
echo "[BRK]";
echo "0";
exit;
}
		
$sub_query = $sub_query." limit ".$startrange1.",".$perpage1;

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_rows=mysql_fetch_array($sub_result)){


if($ct=='')
echo"<tr bgcolor='#E5E5E5'>
      <td width='20'>&nbsp;</td>
	  <td width='20'>&nbsp;</td>
      <td width='80'><span class='style4'>Date </span></td>
      <td width='80'><span class='style4'>Time </span></td>
	  <td width='150'><span class='style4'>Customer</span></td>
	  <td width='150'><span class='style4'>Phone #</span></td>
    </tr>";

	
	extract($sub_rows);

	$ct+=1;

	if($caller_id_status==1)
		$Color="style='background-color:#01DFA5'";
	else			
		$Color="style='background-color:#F5A9BC'";		

    $desc=ereg_replace("'","%27",$Ledger_Name);
	$CustomerRemark2=ereg_replace("'","%27",$CustomerRemark2);

	if($caller_id_status==1)
		$onclick='  ';
		else
		$onclick=' onclick=\'getphone('.$callerid_id.')\' ';
	
	echo"<tr $Color>";
	echo " <td $onclick align='center' valign='center'><span class='style15'></span></td>";
	echo " <td $onclick  align='center' valign='center'><span class='style15'></span></td>";
	echo"  <td  $onclick align='left'><span class='style5'>$callerid_date</span></td>";
	echo"  <td  $onclick align='left'><span class='style5'>$callerid_time</span></td>";
	echo"  <td  $onclick align='left'><span class='style5'>$Ledger_Name</span></td>";
	echo"  <td  $onclick align='left'><span class='style5'>$Customer_Telephone1</span></td>";
    echo"</tr>";
	
}	

echo"<tr><td colspan='5'>&nbsp;</td></tr>
	 <tr height='30'><td colspan=5><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
$diff1= $startrange1 - $perpage1;
		
if (($diff1) >= 0 ){
	$prvstartrange1 = $startrange1 - $perpage1;
	echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='pointer'\"  onclick=SetPrvPage1('".$prvstartrange1."','".$perpage1."','CallingRecords')>";
}
else
	echo "&nbsp;";
		
echo"</td><td width='150' align='center'>";
		
if ($startrange1 <= $totalrows1){
	$startrange1 = $startrange1 + $perpage1;
	if  ($startrange1 < $totalrows1)
		echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='pointer'\" onclick=SetNextPage1('".$startrange1."','".$perpage1."','CallingRecords')>";
} else
	echo "&nbsp;";
			
echo"</td><td width='170'>&nbsp;</td></tr>";
echo"</table>";
echo"</td></tr>";




print <<<HERE
  </table></td><td id='search-content3' ></td></tr>
  <tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:600px;' valign='top'>
HERE;
		//include("toolbar.php");
		echo"</td>
		<td id='search-bottom3' style='height:34px; width:31px;'>";

	
print <<<HERE
&nbsp;</td></tr>
		
   </table>
HERE;

}

function get_nextsequence($SeqField , $ProjCostCent ,$connection)

{
	//mysql_query("START TRANSACTION",$connection);
	$query = "Select ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject , ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter From Module ";
	$result= mysql_query($query,$connection) or die( mysql_error());
	while($rowmodule=mysql_fetch_array($result)){
		extract($rowmodule);
	}
	
	$query = "Select Count(*) From Sequence";

	if ($ProjCostCent != '' && $Module_ReferenceByCostcenter == 1)
		$query = $query."  Where '".$ProjCostCent."' like Concat(Sequence.CostCenter,'%') ";
	else if ($ProjCostCent != '' && $Module_ReferenceByProject == 1)
		$query = $query."  Where '".$ProjCostCent."' Like Concat(Sequence.Project,'%') ";	
	//echo $query;exit;	
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	$row=mysql_fetch_row($result1);
	$Countt = $row[0];
	 
	 
	if ($Countt==0)
	{
		if ($ProjCostCent != '' && $Module_ReferenceByCostcenter == 1)
			$query = "Insert Into Sequence (".$SeqField.",CostCenter) Values (1,'".$ProjCostCent."')";
		else if ($ProjCostCent != '' && $Module_ReferenceByProject == 1)	
			$query = "Insert Into Sequence (".$SeqField.",Project) Values (1,'".$ProjCostCent."')";
		else	
			$query = "Insert Into Sequence (".$SeqField.") Values (1)";
			
		$result1=mysql_query($query,$connection)	or die( mysql_error());
	}	
	else
	{
		if ($ProjCostCent == '') 
		{
			$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where Sequence.CostCenter is null and Sequence.Project is null";
			$result1=mysql_query($query,$connection)	or die( mysql_error());
		}
		else
		{
			if ($Module_ReferenceByCostcenter == 1)
				$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where '".$ProjCostCent."' like Concat(Sequence.CostCenter,'%') ";
			else if ($Module_ReferenceByProject == 1)
				$query = "Update Sequence Set ".$SeqField."=LAST_INSERT_ID(ifnull(".$SeqField.",0)+1) Where '".$ProjCostCent."' Like Concat(Sequence.Project,'%')";
			
			$result1=mysql_query($query,$connection)	or die( mysql_error());
		}
		
	}
	
		
	$query = " SELECT LAST_INSERT_ID()";
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	$row=mysql_fetch_row($result1);
	$NextValue = $row[0];
	if ($NextValue =='')
		$NextValue = 1 ;
 		//mysql_query("COMMIT",$connection);
	return $NextValue;
}

function get_nextsequence_cashtype($FieldName , $CashTypeCode, $JournalType ,$connection)
{
	//mysql_query("START TRANSACTION",$connection);
 

	$query = " Update CashType Set ".$FieldName."=LAST_INSERT_ID(ifnull(".$FieldName.",0)+1) Where CashType_Code = '".$CashTypeCode."' ".
	         " And Journal_Type = '".$JournalType."'";
	$result= mysql_query($query,$connection)	or die( mysql_error());
	
	
	 $query = " SELECT LAST_INSERT_ID()";
	 $result=mysql_query($query,$connection)	or die( mysql_error());
	 $row=mysql_fetch_row($result);
	 $NextValue = $row[0];
	 if ($NextValue =='')
		$NextValue = 1 ;
		
		//mysql_query("COMMIT",$connection);
		return $NextValue;
}

function get_nextsequence_sordertype($FieldName , $SorderTypeCode,$connection)
{
	//mysql_query("START TRANSACTION",$connection);
 

	$query = " Update SorderType Set ".$FieldName."=LAST_INSERT_ID(ifnull(".$FieldName.",0)+1) Where Sorder_Type = '".$SorderTypeCode."'";
	$result= mysql_query($query,$connection)	or die( mysql_error());
	
	
	 $query = " SELECT LAST_INSERT_ID()";
	 $result=mysql_query($query,$connection)	or die( mysql_error());
	 $row=mysql_fetch_row($result);
	 $NextValue = $row[0];
	 if ($NextValue =='')
		$NextValue = 1 ;
		
		//mysql_query("COMMIT",$connection);
		return $NextValue;
}

function get_nextfolio($month ,$connection)
{
$query = "Select Count(*) From jfolio where jfolio.journal_date=".$month;
$result=mysql_query($query,$connection)	or die( mysql_error());
$row=mysql_fetch_row($result1);
$Countt = $row[0];

if($Countt=='0') {
	
	$query ="Insert Into jfolio (journal_seq,Journal_Date) Values (1,$month)";

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

}
else {
	if(empty($month))
	return -1;

else {
	$query="Update jfolio Set journal_seq=LAST_INSERT_ID(ifnull(journal_seq,0)+1) Where jfolio.Journal_Date =".$month;
	//echo$query;
	$result1=mysql_query($query,$connection)	or die( mysql_error());
	}
}

$query = " SELECT LAST_INSERT_ID()";
 $result1=mysql_query($query,$connection)	or die( mysql_error());
 $row=mysql_fetch_row($result1);
 $NextValue = $row[0];
 if ($NextValue =='')
    $NextValue = 1 ;
 	

	return $NextValue;
}

function get_nextreference($client_groupcode,$connection){

	$query ="Select Count(*) From ledgergrp where ledgergrp.LedgerGrp_Code='".$client_groupcode."'";
	$result=mysql_query($query,$connection);
	$row=mysql_fetch_row($result);
	$Countt = $row[0];
	
	
	if($Countt=='0'){
		
		$insert_ledgergrp="insert into ledgergrp(LedgerGrp_Code,LedgerGrp_reference)
							values('".$client_groupcode."','00001')";
		//echo$insert_ledgergrp;
		$result1=mysql_query($insert_ledgergrp,$connection);
		
	}
	else{
	
		$query="Update ledgergrp Set LedgerGrp_reference=LAST_INSERT_ID(ifnull(LedgerGrp_reference,0)+1) Where ledgergrp.LedgerGrp_Code =".$client_groupcode;
		//echo$query;
		$result1=mysql_query($query,$connection);
		
	}
	$query = " SELECT LAST_INSERT_ID()";
	$result1=mysql_query($query,$connection);
	$row=mysql_fetch_row($result1);
	$NextValue = $row[0];
	
	if ($NextValue =='')
		$NextValue = 1 ;
 

	return $NextValue;
}

function GetSQLValueString($theValue, $theType) 
{	
  $theValue = trim($theValue);	
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}


function export_data($connection,$num,$query,$ourFileName,$ourFileHandle,$count){

//echo$query;

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

$numrow=mysql_num_rows($resultdata);

if($numrow=='0')
return 5;


else{

$countrow=1;
while($rows=mysql_fetch_array($resultdata)){




//echo$count."<br>";

if(($count=='3') && ($countrow=='3')){
//echo$countrow;
break;

}

for($i=0;$i<$num;$i=$i+3){


$stringData=$rows[$i];
//echo$stringData;
$c=strlen($stringData);
//echo$c;

if($c=='0'){

$t=$i+2;
$type=$rows[$t];

if($type=='3' or $type=='1' or $type=='2'){
$l=$i+1;
$len=$rows[$l];
for($j=1;$j<=$len;$j++){



fwrite($ourFileHandle,'0');
}
}

else{
$l=$i+1;
$len=$rows[$l];
for($j=1;$j<=$len;$j++){


fwrite($ourFileHandle,$stringData);
fwrite($ourFileHandle,' ');
}

}

}

else{

$l=$i+1;
$len=$rows[$l];
//echo$len;

$t=$i+2;
$type=$rows[$t];



if($type=='0'){
fwrite($ourFileHandle,$stringData);

for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,' ');

}

}

if($type=='1'){


for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,'0');

}
fwrite($ourFileHandle,$stringData);
}

else if($type=='2'){

$rows[i]=number_format($rows[i], 2, '.', '');
//echo$rows[i]."<br>";

$rows[i]=explode('.',$rows[i]);


$c=strlen($rows[i]);




for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,' ');

}
fwrite($ourFileHandle,$stringData);
}

else if($type=='3'){
fwrite($ourFileHandle,$stringData);
for($j=$c;$j<$len;$j++){

fwrite($ourFileHandle,'0');

}
}
}}

$countrow=$countrow+1;
}
}
}


///// function for all data
function export_alldata($gci,$date1,$date2,$date3,$ourFileHandle){

$gcilen=strlen($gci);


$max=7-$gcilen;
for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,'0');
}
fwrite($ourFileHandle,$gci);


$date1len=strlen($date1);
if($date1len=='0'){

for($i=1;$i<=4;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date1);

$date1len=strlen($date2);
if($date1len=='0'){

for($i=1;$i<=2;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date2);


$date1len=strlen($date3);
if($date1len=='0'){

for($i=1;$i<=2;$i++){
fwrite($ourFileHandle,'0');
}
}

else
fwrite($ourFileHandle,$date3);


}

if (!function_exists('explodeword')) {
	function explodeword($date)
	{

		$trim = explode('-',$date);
		$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

		return $trim;
	}
}

if (!function_exists('explodeword2')) {
	function explodeword2($date)
	{

		$trim = explode('/',$date);
		$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

		return $trim;
	}
}
if (!function_exists('explodewordSlash')) {
	function explodewordSlash($date)
	{

		$trim = explode('/',$date);
		$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

		return $trim;
	}

}

	
//Return Date in YYYYMMDD format. Supplied Format: DD/MM/YYYY
function formatDateNoTime($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}
/**
*	Return Number of Days in Each Month
*	Done by Nadine
**/

function getDaysInMonth($monthNbr,$year){
	if($monthNbr >= 1 && $monthNbr <= 12){
		if (in_array($monthNbr, array(1,3,5,7,8,10,12)))
			return 31;
		else if (in_array($monthNbr, array(4,6,9,11) ))
			return 30;
		else if ($monthNbr == 2){
			if((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)))
				return 29;
			return 28;
		} 
	}
	return 0;
}

function ShowReportHeader($title,$PageLayout, $connection) {
	
	//Read From Setup Table							
	$querysetup= " select Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail,  ".
				 " ifNull(Setup_LogoWidth, 80) as SetupLogoWidth, ifNull(Setup_LogoHeight, 50) as SetupLogoHeight, ".
				  " ifNull(show_companylogo,0) as show_companylogo, ifNull(show_companyname,0) as show_companyname, ".
				  " ifNull(show_printdatetime,0) as show_printdatetime, ifNull(show_user,0) as show_user, ".
				  " ifNull(show_companymemo, 0) as show_companymemo ".
					" From Setup ";
					
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	
	while($rowsetup=mysql_fetch_array($resultsetup)){
		extract($rowsetup);
	}
	
	$now=current_server_time($connection);
	$dt=split(" ",$now[0]);
	$date = split("-",$dt[0]);
	$printdate = $date[2]."/".$date[1]."/".$date[0];
	$printtime = $dt[1]." ".$dt[2]. " ".$dt[3];

	$LogoStyle = "style='width:".$SetupLogoWidth."px; height:".$SetupLogoHeight."px;'";
	
	if($PageLayout=='landscape') { $tablewidth='1100px';}
	if($PageLayout=='portrait') { $tablewidth='700px';}
	//echo".";
	echo "<table border='0' cellspacing=0 cellpadding=0  style='table-layout:fixed;' width='$tablewidth'>";
		
	if($PageLayout=='landscape') { 
		echo"<col width=200>
		  <col width=250>
		  <col width=270>
		  <col width=200>
		  <col width=170>";
    }
		
	if($PageLayout=='portrait') { 
		echo"<col width=200>
		  <col width=245>
		  <col width=50>
		  <col width=80>
		  <col width=130>";
	}	
	
	echo"<tr height='25px'>";
	
	if($show_companylogo==1)
		echo"	<td align='left' rowspan='3'><img src='LogoFile.php' $LogoStyle /></td>";
	else 	echo"	<td align='center' rowspan='3'></td>";
	
	if($show_companyname==1)	
		echo"	<td align='left' colspan='2'><span style='font-size:14px; font-weight:bold; font-style:italic;'>".$CompanyName."</span></td>";
	else echo"	<td align='left' colspan='2'><span style='font-size:14px; font-weight:bold; font-style:italic;'></span></td>";
	
	
		echo"	<td align='center' rowspan='3' colspan='2' valign='top'><Table  cellspacing=0 cellpadding=0 style='border:1px solid #000000;'>";
		
		if($show_printdatetime==1){
			echo"<tr height='25px'><td style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>&nbsp; Printed Date: &nbsp;</span></td>";
			echo"	<td align='right' style='border-bottom:1px solid #000000;padding-right: 2px;' valign='middle'><span style='font-size:12px;font-family:arial;'>$now[1] ".$printdate." </span></td></tr>
		
			<tr height='25px'><td align='right'  style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>Time: &nbsp;</span></td>
			<td align='right'  style='border-bottom:1px solid #000000;padding-right: 2px; ' valign='middle'><span style='font-size:12px;font-family:arial;'>".$printtime."</span></td></tr>";
		}	
		if($show_user==1){
		
			echo"<tr height='25px'><td align='right' valign='middle' ><span style='font-size:11px; font-weight:bold;font-family:arial;'>User: &nbsp;</span></td>
			<td align='right' valign='middle' style='padding-right: 2px;' ><span style='font-size:12px;font-family:arial;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td></tr>";
		}	
		echo"</table></td>";
	echo"</tr>";

	if($show_companymemo==1){	
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='2'><span style='font-size:10px;'>".nl2br($CompanyMemo)."</span></td>";	
		echo"</tr>";
	}
	else {
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='2'><span style='font-size:10px;'></span></td>";	
		echo"</tr>";
	}
	
	echo"<tr height='25'>";
	//echo"	<td align='right'  valign='top'><span style='font-size:14px; font-weight:bold;'>User : &nbsp;</span></td>";
	//echo"	<td align='left' valign='top'><span style='font-size:14px;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td>";
	echo"</tr>";
	
	echo"<tr height='25'>";
	echo"	<td align='center' colspan='5'><span style='font-size:18px; font-weight:bold;'>".$title."</span></td>";
	echo"</tr>";
	
	echo "<tr height='5px'><td colspan='15'></td></tr>";
	echo"</table>";


}

// richard
function ShowReportHeader1($title,$PageLayout, $connection) {
	
	//Read From Setup Table							
	$querysetup= " select Company_Name as CompanyName, Company_Memo as CompanyMemo, Company_City as CompanyWebsite, Company_Country as CompanyEmail,  ".
				 " ifNull(Setup_LogoWidth, 80) as SetupLogoWidth, ifNull(Setup_LogoHeight, 50) as SetupLogoHeight, ".
				  " ifNull(show_companylogo,0) as show_companylogo, ifNull(show_companyname,0) as show_companyname, ".
				  " ifNull(show_printdatetime,0) as show_printdatetime, ifNull(show_user,0) as show_user, ".
				  " ifNull(show_companymemo, 0) as show_companymemo ".
					" From Setup ";
					
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	
	while($rowsetup=mysql_fetch_array($resultsetup)){
		extract($rowsetup);
	}
	
	$now=current_server_time($connection);
	$dt=split(" ",$now[0]);
	$date = split("-",$dt[0]);
	$printdate = $date[2]."/".$date[1]."/".$date[0];
	$printtime = $dt[1]." ".$dt[2]. " ".$dt[3];

	$LogoStyle = "style='width:".$SetupLogoWidth."px; height:".$SetupLogoHeight."px;'";
	
	if($PageLayout=='landscape') { $tablewidth='1100px';}
	if($PageLayout=='portrait') { $tablewidth='700px';}
	
	echo "<table border='0' cellspacing=0 cellpadding=0  style='table-layout:fixed;' width='$tablewidth'>";
		
	if($PageLayout=='landscape') { 
		echo"<col width=130>
		  <col width=330>
		  <col width=270>
		  <col width=200>
		  <col width=170>";
    }
		
	if($PageLayout=='portrait') { 
		echo"<col width=120>
		  <col width=200>
		  <col width=50>
		  <col width=80>
		  <col width=320>
		  <col width=300>";
	}	
	
	echo"<tr height='25px'>";
	
	if($show_companylogo==1)
		echo"	<td align='left' rowspan='3'><img src='LogoFile.php' $LogoStyle /></td>";
	else 	echo"	<td align='center' rowspan='3'></td>";
	
	if($show_companyname==1)	
		echo"	<td align='left' colspan='4'><span style='font-size:14px; font-weight:bold; font-style:italic;'>".$CompanyName."</span></td>";
	else echo"	<td align='left' colspan='4'><span style='font-size:14px; font-weight:bold; font-style:italic;'></span></td>";
	
	
		echo"	<td align='center' rowspan='3' colspan='2' valign='top'><Table  cellspacing=0 cellpadding=0 style='border:1px solid #000000;'>";
		
		if($show_printdatetime==1){
			echo"<tr height='25px'><td style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>&nbsp; Printed Date: &nbsp;</span></td>";
			echo"	<td align='left' style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:12px;font-family:arial;'>$now[1] ".$printdate." </span>&nbsp;</td></tr>
		
			<tr height='25px'><td align='right'  style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:11px; font-weight:bold;font-family:arial;'>Time: &nbsp;</span></td>
			<td align='left'  style='border-bottom:1px solid #000000;' valign='middle'><span style='font-size:12px;font-family:arial;'>".$printtime."</span></td></tr>";
		}	
		if($show_user==1){
		
			echo"<tr height='25px'><td align='right' valign='middle' ><span style='font-size:11px; font-weight:bold;font-family:arial;'>User: &nbsp;</span></td>
			<td align='left' valign='middle' ><span style='font-size:12px;font-family:arial;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td></tr>";
		}	
		echo"</table></td>";
	echo"</tr>";

	if($show_companymemo==1){	
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='3'><span style='font-size:10px;'>".nl2br($CompanyMemo)."</span></td>";	
		echo"</tr>";
	}
	else {
		echo"<tr height='10'>";
		echo"	<td align='left' rowspan='2' colspan='3'><span style='font-size:10px;'></span></td>";	
		echo"</tr>";
	}
	
	echo"<tr height='25'>";
	//echo"	<td align='right'  valign='top'><span style='font-size:14px; font-weight:bold;'>User : &nbsp;</span></td>";
	//echo"	<td align='left' valign='top'><span style='font-size:14px;'>".$_SESSION["useraccount"]." - ".$_SESSION["username"]."</span></td>";
	echo"</tr>";
	
	echo"<tr height='35'><td> </td>";
	echo"	<td align='center' colspan='5'><span style='font-size:18px; font-weight:bold;'>".$title."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td>";
	echo"</tr>";
	
	echo "<tr height='5px'><td colspan='15'></td></tr>";
	echo"</table>";


}



function ShowReportTitle($title,$PageLayout, $connection) {
	
	if($PageLayout=='landscape') { $tablewidth='1100px';}
	if($PageLayout=='portrait') { $tablewidth='650px';}
	
	echo "<table border='0' cellspacing=0 cellpadding=0  style='table-layout:fixed;' width='$tablewidth'>";
		
	if($PageLayout=='landscape') { 
		echo"<col width=130>
		  <col width=330>
		  <col width=270>
		  <col width=200>
		  <col width=170>";
    }
	if($PageLayout=='portrait') { 
		echo"<col width=120>
		  <col width=130>
		  <col width=170>
		  <col width=100>
		  <col width=130>";
	}	
	

	echo"<tr height='25'>";
	echo"	<td align='center' colspan='5'><span style='font-size:18px; font-weight:bold;'>".$title."</span></td>";
	echo"</tr>";
	
	echo "<tr height='5px'><td colspan='5'><h4></td></tr>";
	echo"</table>";
}

function RoundTotal($Amount, $Divisor, $connection) {
	$RAmount = 0;
	
	$Remain = round(fmod(abs($Amount),$Divisor),2); 
	$RAmount = abs($Amount) - $Remain;
	$Remain = round(abs($Remain)); 
	
	$sub_query = "Select CashRegisterRounding_Value From CashRegisterRounding Where ".$Remain." Between CashRegisterRounding_RangeFrom and CashRegisterRounding_RangeTo ";
	$sub_result = mysql_query($sub_query,$connection);
	$sub_row=mysql_fetch_row($sub_result);
	//$sub_result = selectData($connection, $sub_query);
	$Round = $sub_row[0];
	//echo $sub_query;
	if (!empty($Round) || $Round == 0) { 
		$RAmount = $RAmount + $Round;
		if ($Amount < 0) $RAmount = (-1)*($RAmount);
	} else {
		$RAmount = $Amount;
	}
	
	return $RAmount;
}


function SendNotification($Pnotificationtype , $Pnotificationaction , $Pnotificationdescription
                          , $Pnotificationreference, $Pnotificationcreateduser, $Pnotificationlevel
                          , $Pnotificationdate, $Pnotificationdone, $Pnotificationchecked,$connection){

$queryNot = " select Notification($Pnotificationtype , $Pnotificationaction , $Pnotificationdescription
                          , $Pnotificationreference, $Pnotificationcreateduser, $Pnotificationlevel
                          , $Pnotificationdate, $Pnotificationdone, $Pnotificationchecked) From Dual";
						  
$resultNot = mysql_query($queryNot,$connection) or die( mysql_error());

}

function time_in_out($fromdate,$todate,$connection){




}

function createviewer($FormName,$TransType,$Parameters,$connection){

if($FormName=='Retail' and $TransType=='D'){
	$query="Select SALE_NETTOTAL,Sale_Date,Sale_Reference from retail where sale_id = ".$Parameters['sale_id'];
	$func_result = mysql_query($query,$connection);
	$func_row=mysql_fetch_row($func_result);
	$sale_nettotal = $func_row[0];
	$sale_date = $func_row[1];
	$sale_reference = $func_row[2];
	
	
	$query="insert into viewerretail(viewer_date, user_account, cashregister_name, trans_id, trans_reference, trans_date	, trans_netamount1, FormName, Trans_Type)
			values ( 	 
					now()
					,".GetSQLValueString($Parameters['user_account'], 'text')."
					,".GetSQLValueString($Parameters['cashregister'], 'text')."
					,".GetSQLValueString($Parameters['sale_id'], 'double')."
					,".GetSQLValueString($sale_reference, 'double')."
					,".GetSQLValueString($sale_date, 'date')."
					,".GetSQLValueString($sale_nettotal, 'double')."
					,'Retail'
					,'D'	
					)";
	//echo $query;
	$func_result = mysql_query($query,$connection) or die( mysql_error());
}
else 
if($FormName=='Retail' and $TransType=='U'){
	
	$query   = " SELECT Sales_creditacc,Sales_CreditAmount,Sales_CashInBase1,Sales_CashOutBase1, sale_nettotal ,Sale_Date, Sale_Reference,discount_user,sale_discountamount,Sale_DiscountPer,Sale_Cash
				FROM Temp_Retail WHERE Sale_id = ".$Parameters['sale_id'];
	$func_result = mysql_query($query,$connection);
	$func_row	 = mysql_fetch_row($func_result);
	$new_sales_creditacc   = $func_row[0];
	$new_sales_creditamount 		 = $func_row[1];
	$new_sales_cashinbase1  = $func_row[2];
	$new_sales_cashoutbase1   = $func_row[3];
	$new_sale_nettotal   = $func_row[4];
	$new_sale_date   = $func_row[5];
	$new_sale_reference   = $func_row[6];
	$new_discount_user   = $func_row[7];
	$new_sale_discountamount   = $func_row[8];
	$new_sale_discountper   = $func_row[9];
	$new_sale_cash   = $func_row[10];
	
	
	
	$query   = " SELECT Sales_creditacc,Sales_CreditAmount,Sales_CashInBase1,Sales_CashOutBase1, sale_nettotal ,Sale_Date, Sale_Reference,discount_user,sale_discountamount,Sale_DiscountPer,Sale_Cash
				 FROM Retail WHERE Sale_id = ".$Parameters['sale_id'];
	$func_result = mysql_query($query,$connection);
	$func_row	 = mysql_fetch_row($func_result);
	$old_sales_creditacc   = $func_row[0];
	$old_sales_creditamount 		 = $func_row[1];
	$old_sales_cashinbase1  = $func_row[2];
	$old_sales_cashoutbase1   = $func_row[3];
	$old_sale_nettotal   = $func_row[4];
	$old_sale_date   = $func_row[5];
	$old_sale_reference   = $func_row[6];
	$old_discount_user   = $func_row[7];
	$old_sale_discountamount   = $func_row[8];
	$old_sale_discountper   = $func_row[9];
	$old_sale_cash   = $func_row[10];
	
	$query="insert into viewerretail(viewer_date, user_account, cashregister_name, trans_id, trans_reference, trans_date, trans_netamount1, trans_netamount2
										, trans_discountamount1, trans_discountamount2
										, trans_discountper1, trans_discountper2
										, user_discount, trans_account1
										, trans_account2, FormName, Trans_Type)
			values ( 	 
					now()
					,".GetSQLValueString($Parameters['user_account'], 'text')."
					,".GetSQLValueString($Parameters['cashregister'], 'text')."
					,".GetSQLValueString($Parameters['sale_id'], 'double')."
					,".GetSQLValueString($new_sale_reference, 'double')."
					,".GetSQLValueString($new_sale_date, 'date')."
					,".GetSQLValueString($old_sale_nettotal, 'double')."
					,".GetSQLValueString($new_sale_nettotal, 'double')."
					,".GetSQLValueString($old_sale_discountamount, 'double')."
					,".GetSQLValueString($new_sale_discountamount, 'double')."
					,".GetSQLValueString($old_sale_discountper, 'double')."
					,".GetSQLValueString($new_sale_discountper, 'double')."
					,".GetSQLValueString($new_discount_user, 'text')."
					,".GetSQLValueString($old_sales_creditacc, 'text')."
					,".GetSQLValueString($new_sales_creditacc, 'text')."
					,'Retail'
					,'U'					
					)";

	$func_result = mysql_query($query,$connection) or die( mysql_error());
}
else 
if($FormName=='Retail' and $TransType=='Ds'){
	
	$query   = " SELECT Sales_creditacc,Sales_CreditAmount,Sales_CashInBase1,Sales_CashOutBase1, sale_nettotal ,Sale_Date, Sale_Reference,discount_user,sale_discountamount,Sale_DiscountPer,Sale_Cash
				FROM Temp_Retail WHERE Sale_id = ".$Parameters['sale_id'];
	$func_result = mysql_query($query,$connection);
	$func_row	 = mysql_fetch_row($func_result);
	$new_sales_creditacc   = $func_row[0];
	$new_sales_creditamount 		 = $func_row[1];
	$new_sales_cashinbase1  = $func_row[2];
	$new_sales_cashoutbase1   = $func_row[3];
	$new_sale_nettotal   = $func_row[4];
	$new_sale_date   = $func_row[5];
	$new_sale_reference   = $func_row[6];
	$new_discount_user   = $func_row[7];
	$new_sale_discountamount   = $func_row[8];
	$new_sale_discountper   = $func_row[9];
	$new_sale_cash   = $func_row[10];
	
	
	
	$query   = " SELECT Sales_creditacc,Sales_CreditAmount,Sales_CashInBase1,Sales_CashOutBase1, sale_nettotal ,Sale_Date, Sale_Reference,discount_user,sale_discountamount,Sale_DiscountPer,Sale_Cash
				 FROM Retail WHERE Sale_id = ".$Parameters['sale_id'];
	$func_result = mysql_query($query,$connection);
	$func_row	 = mysql_fetch_row($func_result);
	$old_sales_creditacc   = $func_row[0];
	$old_sales_creditamount 		 = $func_row[1];
	$old_sales_cashinbase1  = $func_row[2];
	$old_sales_cashoutbase1   = $func_row[3];
	$old_sale_nettotal   = $func_row[4];
	$old_sale_date   = $func_row[5];
	$old_sale_reference   = $func_row[6];
	$old_discount_user   = $func_row[7];
	$old_sale_discountamount   = $func_row[8];
	$old_sale_discountper   = $func_row[9];
	$old_sale_cash   = $func_row[10];
	
	$query="insert into viewerretail(viewer_date, user_account, cashregister_name, trans_id, trans_reference, trans_date, trans_netamount1, trans_netamount2
										, trans_discountamount1, trans_discountamount2
										, trans_discountper1, trans_discountper2
										, user_discount, trans_account1
										, trans_account2, FormName, Trans_Type)
			values ( 	 
					now()
					,".GetSQLValueString($Parameters['user_account'], 'text')."
					,".GetSQLValueString($Parameters['cashregister'], 'text')."
					,".GetSQLValueString($Parameters['sale_id'], 'double')."
					,".GetSQLValueString($new_sale_reference, 'double')."
					,".GetSQLValueString($new_sale_date, 'date')."
					,".GetSQLValueString($old_sale_nettotal, 'double')."
					,".GetSQLValueString($new_sale_nettotal, 'double')."
					,".GetSQLValueString($old_sale_discountamount, 'double')."
					,".GetSQLValueString($new_sale_discountamount, 'double')."
					,".GetSQLValueString($old_sale_discountper, 'double')."
					,".GetSQLValueString($new_sale_discountper, 'double')."
					,".GetSQLValueString($new_discount_user, 'text')."
					,".GetSQLValueString($old_sales_creditacc, 'text')."
					,".GetSQLValueString($new_sales_creditacc, 'text')."
					,'Retail'
					,'Ds'					
					)";

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








}

function checkcashdiff($TransType,$TransId,$txtCurr,$base1,$base2,$connection){

		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$txtCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$txtCurr."' )";
		$result = selectData($connection, $query);
		$rate_dt = $result[0];
		
		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
		$result = selectData($connection, $query);
		$rate_dt1 = $result[0];
		
		$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
		$result = selectData($connection, $query);
		$rate_dt2 = $result[0];

		
		
if($TransType=='ReturnSales' or $TransType=='Sales' ){

		$query = " Select ( ifNull(Sale_Total,0) + IfNull(Sale_VAT,0) + IfNull(Sale_TransportAmount,0) 
						+ IfNull(Sale_BonusAmount,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))) 
						As Sale_NetTotal  ".
				 " , Round(ifNull(Sale_CashInBase1,0), 2)
				  , Round(ifNull(Sale_CashInBase2,0), 2)
				  , Round(ifNull(Sale_CashOutBase1,0), 2)
				  , Round(ifNull(Sale_CashOutBase2,0), 2)
				 , Round(ifNull(Sale_TipsBase1,0), 2)".
				 " From Temp_Sales As Sales ".
			     " Where Sales.Sale_Id = '".$TransId."'";
			   
		
		$result=mysql_query($query,$connection) or die(mysql_error());
		$rows=mysql_fetch_array($result);

		$vSaNetTotal    = $rows[0];
		$vCashInB1  = $rows[1];
		$vCashInB2  = $rows[2];
		$vCashOutB1  = $rows[3];
		$vCashOutB2  = $rows[4];
		$vTipsB1  = $rows[5];
		
		
		if ($txtCurr == $base1) 
			$vAmtPayed =   $vSaNetTotal - ($vCashInB1 + ($vCashInB2*$rate_dt2/$rate_dt1) + $vTipsB1) + $vCashOutB1 + ($vCashOutB2*$rate_dt2/$rate_dt1);	
		else if ($txtCurr == $base2)
			$vAmtPayed =   $vSaNetTotal - (($vCashInB1*$rate_dt1/$rate_dt2) + $vCashInB2 + ($vTipsB1*$rate_dt1/$rate_dt2)) + ($vCashOutB1*$rate_dt1/$rate_dt2) + $vCashOutB2;
		else 
			$vAmtPayed =   ($vSaNetTotal*$rate_dt/$rate_dt1) - ($vCashInB1 + ($vCashInB2*$rate_dt2/$rate_dt1) + $vTipsB1) + $vCashOutB1 + ($vCashOutB2*$rate_dt2/$rate_dt1);
	 
	  
		if($vCashInB1==0 and $vCashInB2==0)
			$SaveAllowed = 1;
		else
		if(Round($vAmtPayed,0) <> 0){
			$SaveAllowed = 3;
			
		}
		else $SaveAllowed = 1;

		return $SaveAllowed;
	}
	
	
	
if($TransType=='Delivery' or $TransType=='ReturnDelivery' ){

		$query = " Select ( ifNull(Delivery_Total,0) + IfNull(Delivery_VAT,0) + IfNull(Delivery_transportAmount,0) 
						+ IfNull(Delivery_BonusAmount,0) - IfNull(Delivery_DiscountAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))) 
						As Delivery_NetTotal  ".
				 " , Round(ifNull(Delivery_CashInBase1,0), 2)
				  , Round(ifNull(Delivery_CashInBase2,0), 2)
				  , Round(ifNull(Delivery_CashOutBase1,0), 2)
				  , Round(ifNull(Delivery_CashOutBase2,0), 2)
				 , Round(ifNull(Delivery_TipsBase1,0), 2)".
				 " From Temp_Deliveries  ".
			     " Where Delivery_Id = '".$TransId."'";
		$result=mysql_query($query,$connection) or die(mysql_error());
		$rows=mysql_fetch_array($result);

		$vSaNetTotal    = $rows[0];
		$vCashInB1  = $rows[1];
		$vCashInB2  = $rows[2];
		$vCashOutB1  = $rows[3];
		$vCashOutB2  = $rows[4];
		$vTipsB1  = $rows[5];
		
		
		if ($txtCurr == $base1) 
			$vAmtPayed =   $vSaNetTotal - ($vCashInB1 + ($vCashInB2*$rate_dt2/$rate_dt1) + $vTipsB1) + $vCashOutB1 + ($vCashOutB2*$rate_dt2/$rate_dt1);	
		else if ($txtCurr == $base2)
			$vAmtPayed =   $vSaNetTotal - (($vCashInB1*$rate_dt1/$rate_dt2) + $vCashInB2 + ($vTipsB1*$rate_dt1/$rate_dt2)) + ($vCashOutB1*$rate_dt1/$rate_dt2) + $vCashOutB2;
		else 
			$vAmtPayed =   ($vSaNetTotal*$rate_dt/$rate_dt1) - ($vCashInB1 + ($vCashInB2*$rate_dt2/$rate_dt1) + $vTipsB1) + $vCashOutB1 + ($vCashOutB2*$rate_dt2/$rate_dt1);
	 
	  
		if($vCashInB1==0 and $vCashInB2==0)
			$ErrorExists = 0;
		else
		if(Round($vAmtPayed,0) <> 0){
			$ErrorExists = 1;
			
		}
		else $ErrorExists = 0;

		return $ErrorExists;
	}
	
	
	
	
}

function checkfinancialdate($transdate,$txtdate,$accesslabels,$compyear,$connection){

	$querydir = "select if(count(*)>0,StartFinancialDate,'1900-01-01'),if(count(*)>0,EndFinancialDate,'1900-01-01') 
				from ".$accesslabels.".directories where concat(CompanyName,'_',Diryear)='".$compyear."' ";
    $resultdir=mysql_query($querydir,$connection) or die(mysql_error());
	$rowsdir=mysql_fetch_array($resultdir);
}

function GetDiscountValue($item_code,$Client_Code,$connection){

$sql = " select if( Idisc < CDIsc ,Idisc, CDIsc) as disc from (". 
" select ifnull(Client_DiscountPer,0) CDisc,(select item_discountper from items where item_code='".$item_code."') Idisc ".
" from clients where  Ledger_Number='".$Client_Code."' limit 1) as sqldisc ";

$sql_result = mysql_query($sql,$connection);
$sql_rows=mysql_fetch_row($sql_result);
return $sql_rows[0];
}

function draw_closingtemplate($connection){

	$closingtemplate="select * from closingtemplate";


$querytemplate= mysql_query($closingtemplate, $connection) or die(mysql_error());

$i=0;

echo"<table border='1' 	align='left' class='sample'  style='table-layout:fixed;border:0px solid black;font-size:10px;border-spacing:0;width:500px;' cellpadding='0' cellspacing='0'>
	";
echo"
	<col width=25>
	<col width=25>
	<col width=100>
	<col width=40>
	<col width=60>
	
	
	";	
	
	 echo"<tr bgcolor='#F6F6F6'>
		<td  height='25' class='detail-header-bg'  align='left'><span class='style6'><input type='hidden' name='empid' id='empid' value=''></span></td>
		<td  height='25'  class='detail-header-bg' align='left'><span class='style6'></span></td>
		<td  height='25'  class='detail-header-bg' align='left'><span class='style6'>Debit_LedgerNumber </span></td>
		<td  height='25'  class='detail-header-bg' align='left'><span class='style6'>Synchronized</span></td>
		<td  height='25'  class='detail-header-bg' align='left'><span class='style6'>TRANSACTION_TYPE</span></td>
		</tr>

";

while($templateresult=mysql_fetch_row($querytemplate)){
	if ($i%2==0)
		$bgcolor='#F5FAFF';
	else	
		$bgcolor='#FFFFFF';
	
	$credit[$i]=$templateresult[2];
	$transaction[$i]=$templateresult[0];
		$debitnumber[$i] = $templateresult[1];
		$synchronize[$i]=$templateresult[3];
		$type[$i]=$templateresult[4];
		//echo $type[$i].$i;
		
		
		if ($type[$i]=='op'){$typename[$i]="openning";}
		
		$imgSubmit[$i]='imgSubmit['.$i.']' ;
		


	
		
	
		//echo $i. $type[$i];
	
	
	
		
		
			
			echo"<tr height='20px' id='loan[0]' onclick=\"returncolor(this,'selectedtr_loan','selectedtrcolor_loan')\"  style='background-color:$bgcolor'>";
			echo"<td align='center'><img src='img/delete.png' onmouseover=\"style.cursor='pointer'\" name='' title='Delete' onclick=\"deleteclosetemplate('$debitnumber[$i]'); \"></td>";
			echo"<td align = 'center' ><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\"  id = '$debitnumber[$i]' name='$debitnumber[$i]' title='Save' onclick=\"updatetemplate('$i', '$debitnumber[$i]');\"></td>";

			echo"<td ><span class='style5' ><input type='text' style='width:100%'  name='$debitnumber[$i]' id='debit$i' value='$debitnumber[$i]'  class='roundedtext_countrycode'> </span>";
			
			echo "</td>
			<td align='center'><span class='style5'><input type='checkbox'  name='$synchronize[$i]' id='synchro$i' ";
			if ($synchronize[$i] == 1)  echo 'checked' ;
			echo "> </span></td>
			<td><span class='style5'>";
			$typequery="select distinct transaction_type from closingtemplate";
		
		$querytype= mysql_query($typequery, $connection) or die(mysql_error());
			echo "<select name='type$i' id='type$i' style='width:100%'>";
			while($typeresult=mysql_fetch_row($querytype)){
				if($typeresult[0]='op'){ $typename='openning';}
		echo "<option value='$typeresult[0]' ";
		//echo $typeresult[0].$type[$i];
		if ($typeresult[0]==$type[$i]) echo selected;
		 //selected;
			echo ">$typename</option>";}
			echo "
			</select> </span></td>";
			echo "<span class='style5'><input type='hidden'  id='transaction$i' name='$transaction[$i]' value='$transaction[$i]'></input>
			</tr>";
			
	
			
			$i+=1;
			
}
		
		echo"<tr height='20px'  onclick=\"returncolor(this,'selectedtr_loan','selectedtrcolor_loan')\"  style='background-color:$bgcolor'>";
		echo"<td align='center'><img src='img/undo.png' onmouseover=\"style.cursor='pointer'\" name='' title='Clear' onclick=\"clearloan('$i')\"></td>";
		echo"<td align = 'center' ><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" id = '$imgSubmit[$i]' name='$imgSubmit[$i]' title='Save' onclick=\"inserttemplate();\"></td>";
	
			echo"<td ><span class='style5' ><input type='text' style='width:100%'  name='debit' id='debit' value=''  class='roundedtext_countrycode'> </span>
			</td>
			";
			echo"<td align='center' ><span class='style5'><input type='checkbox'   name='synchro'  id='synchroinsert' > </span>
			</td>";
				
				$typequery="select distinct transaction_type from closingtemplate";
		
		$querytype= mysql_query($typequery, $connection) or die(mysql_error());
	
	echo"
			<td><span class='style5'><select name='type' id='type' style='width:100%'>";
			while($typeresult=mysql_fetch_row($querytype)){
				if($typeresult[0]='op'){ $typename='openning';}
			echo "<option value='$typeresult[0]' ";
		
		if ($typeresult[0]==$type[$i]) echo selected;
		 //selected;
			echo ">$typename</option>";}
			echo "</select> </span> </span></td>";

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

function SubmitUserEmails($email_id, $user_code, $connection){
	
if($email_id != ''){
	$query = "select email_address From  ".$_SESSION['dbLabel'].".users_emails where email_id = '$email_id';";
	$res = mysql_query($query,$connection);
	$rw = mysql_fetch_row($res);
	$user_emails = $rw[0];
}	
echo"<table border='0' align='left' valign='top' class='tablecontent' width='660px'>
		<col width=30><col width=30><col width=500><col width=100>";
		
echo "<tr height='25px' ><td></td><td></td><td colspan='2'><input type='text' name='user_emails' id='user_emails' value='$user_emails' style='width: 400; border-radius: 4px;'>";
echo" &nbsp; <button type='button' id='btnSave' name='btnSave' onclick=\"saveEmail('$email_id');return false;\" title='Save Email'><span><span>Save</span></span></button></td>";
echo "</tr>";	
echo "</table>";
	
}

function ShowUserEmails($email_id, $user_code, $connection){
echo"<table border='0' align='left' valign='top' class='tablecontent' width='660px'>
		<col width=30><col width=30><col width=500><col width=100>";
	$ct = 0;
	
	$queryAll = "select email_id, email_address From  ".$_SESSION['dbLabel'].".users_emails where user_account = '$user_code';";
	$resAll = mysql_query($queryAll, $connection);
	while($rw = mysql_fetch_row($resAll)){
		
		if($ct%2) $bgclr = "bgcolor='#FFFFFF'";
		else $bgclr = "bgcolor='#F5FAFF'";
		
		$stle = "style='color:black;'";
		
		if($email_id == $rw[0])  {
			$bgclr = "bgcolor='#1e87a5'";
			$stle = "style='color:white;'";
		}
		echo "<tr height='25px' $bgclr>
				<td align='center'><img src='img/edit.png' onmouseover=\"style.cursor='hand'\" onclick=\"editEmail('".$rw[0]."');\" title='Edit Email' style='width: 20px; height: 20px; cursor: pointer;'></td>
				<td align='center'><span class='style15'><img src='img/c_delete.png'  height='12' width='12' onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteEmail('".$rw[0]."');\" title='Delete Email'  ></span></td>
				<td><span class='style1'>$rw[1]</span></td><td></td>
			</tr>";	
		
		$ct++;
	}
	
	
	
echo "</table>";
	
}

function userHODGrid($hod_id, $user_code, $connection){
	$i = 0;
	$color = "#F5FAFF";
	echo "<table border='0' align='left' valign='top' class='tablecontent' width='410px'>
		<col width=30px><col width=30px><col width=150px><col width=200px>";
	$query = "select hod_id, HOD_Users.user_code, User_Name from ".$_SESSION['dbLabel'].".HOD_Users as HOD_Users
			  inner join ".$_SESSION['dbLabel'].".users as users on users.user_account = HOD_Users.user_code
			  where hod_code = '$user_code' ";
	$res = mysql_query($query, $connection);
	while ($row = mysql_fetch_array($res)){
		if ($i%2) 
			$color = "#F5FAFF";
		else 
			$color = "#FFFFFF";
		echo "<tr height='15px' bgcolor='$color'>
				<td align='center'><input type='hidden' name='hodid$i' id='hodid$i' value='$row[0]'>
					<img src='img/button_edit.png' onmouseover=\"style.cursor='hand';\" onclick=\"updateHOD('$i','$user_code');\" title='Edit Record' style='cursor: pointer;'></td>
				<td align='center'><img src='img/c_delete.png' onmouseover=\"style.cursor='hand';\" onclick=\"deleteHOD('$i','$user_code');\" title='Clear Record' style='cursor: pointer;'></td>
				<td align='center'>
					<input type='text' name='hodusercode$i' id='hodusercode$i' value='$row[1]' style='width: 300px;border-radius: 4px;'>
					<label><span class='style1'><img src='img/btnopen.jpg' title=' Select User ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistUsers('$i');\"  /></span></label>
				</td>
				<td align='center'><input type='text' name='hodusername$i' id='hodusername$i' value='$row[2]' style='width: 300px;border-radius: 4px;' readonly></td>
			 </tr>";	
		$i++;	 
	}
	echo "<tr height='15px' bgcolor='$color'>
				<td align='center'><input type='hidden' name='hodid$i' id='hodid$i' value=''>
					<img src='img/button_edit.png' onmouseover=\"style.cursor='hand';\" onclick=\"updateHOD('$i','$user_code');\" title='Add Record' style='cursor: pointer;'></td>
				<td align='center'><img src='img/undo-1.png' onmouseover=\"style.cursor='hand';\" onclick=\"clearHOD('$i','$user_code');\" height='14px' width='14px' title='Clear Record' style='cursor: pointer;'></td>
				<td align='center'><input type='text' name='hodusercode$i' id='hodusercode$i' value='' style='width: 300px;border-radius: 4px;'>
				<label><span class='style1'><img src='img/btnopen.jpg' title=' Select User ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistUsers('$i');\"  /></span></label>
				</td>
				<td align='center'><input type='text' name='hodusername$i' id='hodusername$i' value='' style='width: 300px;border-radius: 4px;' readonly></td>
			 </tr>";
	echo "</table>";
}

function costcentAccessGrid($cAccid, $user_code, $connection){
	if ($cAccid != ''){
		$queryEdit = "select costcentstk.costcent_code, Costcent_name, ifnull(costcent_include,0)
						from ".$_SESSION['dbLabel'].".costcentstk
						inner join ".$_SESSION['dbName'].".costcent on costcent.costcent_code = costcentstk.costcent_code
						where user_account = '$user_code' and id = $cAccid ";
		$resEdit = mysql_query($queryEdit, $connection);
		$rowEdit = mysql_fetch_row($resEdit);
		$acostcent_code   = $rowEdit[0];
		$acostcent_desc   = $rowEdit[1];
		$costcentincluded = $rowEdit[2];
	}
	echo"<table border='0' align='left' valign='top' class='tablecontent' width='1000px'>
		<col width=30px><col width=30px><col width=400px><col width=120px><col width=420px>";
	echo "<tr height='25px' >
				<td></td><td></td>
				<td align='center'><input type='text' name='acostcent_code' id='acostcent_code' value='$acostcent_code' style='width:180px;'>
					<input type='text' name='acostcent_desc' id='acostcent_desc' value='$acostcent_desc' style='width:200px;'>
					<img src='img/btnopen.jpg' title='Select a Costcenter' width=15 height=20 onclick=\"showlistcostcenter('10');\" onmouseover=\"style.cursor='hand';\" >
				</td>
				<td align='center'><input type='checkbox' name='costcentincluded' id='costcentincluded'  ".($costcentincluded == '1'?'checked':'')."  ></td>
				<td><button type='button' name='submitCostcent' onclick=\"saveCostcentAccess('1', '$cAccid','$user_code');return false;\" title='Save Record' ><span><span>Submit</span></span></button>
					<button type='button' name='clearCostcent' onclick=\"clearCostcentAccess('1','$user_code');return false;\"><span><span>Clear</span></span></button></td>
			</tr>";	
	echo "<tr height=30px><td colspan='5'></td></tr>";		
	$ct = 0;
	$queryAll = "select id, costcentstk.costcent_code, Costcent_name, ifnull(costcent_include,0)
					from ".$_SESSION['dbLabel'].".costcentstk
					inner join ".$_SESSION['dbName'].".costcent on costcent.costcent_code = costcentstk.costcent_code
					where user_account = '$user_code' ";		
	$resultAll = mysql_query($queryAll, $connection);
	while ($rowAll = mysql_fetch_array($resultAll)){
		
		if ($ct%2) $bgcolor = "bgcolor='#E6E6E6'";
		else $bgcolor = "bgcolor='#E6E6E6'";
		
		echo "<tr height='20px' $bgcolor>
			<td align='center'><img src='img/button_edit.png' onclick=\"editCostcentAcc('1','$rowAll[0]','$user_code');\" onmouseover=\"this.style.cursor='pointer';\" title='Edit Record'></td>
			<td align='center'><img src='img/c_delete.png' width='9' height='10' onclick=\"deleteCostcentAcc('1','$rowAll[0]','$user_code');\" onmouseover=\"this.style.cursor='pointer';\" title='Edit Record'></td>
			<td align='center'>$rowAll[1] - $rowAll[2]</td>
			<td align='center'>".($rowAll[3]=='1'?'&#10004;':'&#10006;')."</td>
			</tr>";
		$ct++;	
	}	
	echo "</table>";
	
}


function ProjectAccessGrid($cAccid, $user_code, $connection){
	if ($cAccid != ''){
		$queryEdit = "select projectstk.project_code, project_name, ifnull(project_include,0)
						from ".$_SESSION['dbLabel'].".projectstk
						inner join ".$_SESSION['dbName'].".project on project.project_code = projectstk.project_code
						where user_account = '$user_code' and id = $cAccid "; 
		$resEdit = mysql_query($queryEdit, $connection);
		$rowEdit = mysql_fetch_row($resEdit);
		$aproject_code   = $rowEdit[0];
		$aproject_desc   = $rowEdit[1];
		$projectincluded = $rowEdit[2];
	}
	echo"<table border='0' align='left' valign='top' class='tablecontent' width='1000px'>
		<col width=30px><col width=30px><col width=400px><col width=120px><col width=420px>";
	echo "<tr height='25px' >
				<td></td><td></td>
				<td align='center'><input type='text' name='aproject_code' id='aproject_code' value='$aproject_code' style='width:180px;'>
					<input type='text' name='aproject_desc' id='aproject_desc' value='$aproject_desc' style='width:200px;'>
					<img src='img/btnopen.jpg' title='Select a Project' width=15 height=20 onclick=\"showlistprojects('11');\" onmouseover=\"style.cursor='hand';\" >
				</td>
				<td align='center'><input type='checkbox' name='projectincluded' id='projectincluded'  ".($projectincluded == '1'?'checked':'')."  ></td>
				<td><button type='button' name='submitCostcent' onclick=\"saveCostcentAccess('2','$cAccid','$user_code');return false;\" title='Save Record' ><span><span>Submit</span></span></button>
					<button type='button' name='clearCostcent' onclick=\"clearCostcentAccess('2','$user_code');return false;\"><span><span>Clear</span></span></button></td>
			</tr>";	
	echo "<tr height=30px><td colspan='5'></td></tr>";		
	$ct = 0;
	$queryAll = "select id, projectstk.project_code, project_name, ifnull(project_include,0)
					from ".$_SESSION['dbLabel'].".projectstk
					inner join ".$_SESSION['dbName'].".project on project.project_code = projectstk.project_code
					where user_account = '$user_code' ";		
	$resultAll = mysql_query($queryAll, $connection);
	while ($rowAll = mysql_fetch_array($resultAll)){
		
		if ($ct%2) $bgcolor = "bgcolor='#E6E6E6'";
		else $bgcolor = "bgcolor='#E6E6E6'";
		
		echo "<tr height='20px' $bgcolor>
			<td align='center'><img src='img/button_edit.png' onclick=\"editCostcentAcc('2','$rowAll[0]','$user_code');\" onmouseover=\"this.style.cursor='pointer';\" title='Edit Record'></td>
			<td align='center'><img src='img/c_delete.png' width='9' height='10' onclick=\"deleteCostcentAcc('2','$rowAll[0]','$user_code');\" onmouseover=\"this.style.cursor='pointer';\" title='Edit Record'></td>
			<td align='center'>$rowAll[1] - $rowAll[2]</td>
			<td align='center'>".($rowAll[3]=='1'?'&#10004;':'&#10006;')."</td>
			</tr>";
		$ct++;	
	}	
	echo "</table>";
	
}

// Manifest Function to Calculate Demurrage Value 

function getDemurrageVal ($cntr_type, $demDays, $freeDay, $connection){
	$amountCollected = 0;
	
	// Get free days from db for container
	$queryFreeDays  = "select fromday from demarage_setup where container_type = ".GetSQLValueString($cntr_type,'text')." and ifnull(free_days,0) = 1 limit 1";
	$resultFreeDays = mysql_query($queryFreeDays, $connection);
	$rowFreeDays = mysql_fetch_row($resultFreeDays);
	
	$db_FreeDay = $rowFreeDays[0];
	
	// Calculate Extra Free Days
	$xtraDays = ($freeDay >= $db_FreeDay?$freeDay-$db_FreeDay:0);
	
	$queryDays = "select  fromday, if(ifnull(today,0) = 0,fromday, today),(ifnull(today,fromday)-fromday)+1, demarage_val from demarage_setup
				  where container_type = ".GetSQLValueString($cntr_type,'text')."  and ifnull(free_days,0) = 0 ";
	$resultDays = mysql_query($queryDays, $connection);
	while ($rowDays = mysql_fetch_array($resultDays)){
		$fromday = $rowDays[0];
		$today	 = $rowDays[1];
		$diff 	 = $rowDays[2];
		$demVal	 = $rowDays[3];
		
		if (($xtraDays < $diff || $diff == '1') && $demDays > 0){
			if ($diff > 1){
				$diff -= $xtraDays;
				$amountCollected += (($demDays>$diff?$diff:$demDays)*$demVal);
				$demDays = ($demDays>$diff?$demDays-$diff:0);
			}
			else {
				$amountCollected += ($demDays*$demVal);
				$demDays = 0;
			}
		}
		else {
			$xtraDays = ($xtraDays>=$diff?$xtraDays-$diff:0);			
		}
	} 
	if ($demVal > 0 && $demDays > 0){
		$amountCollected += ($demDays*$demVal);
	}
	return $amountCollected;
}


function drawInvoiceAndReturn($connection, $connection2, $salesman_code, $order_by_date, $TableOption){
	/*$sub_query = " Select TransactionCode, TransactionHeaders.ClientCode, Clients.ClientName, TransactionHeaders.CompanyCode, CurrencySymbol,
		TransactionHeaders.UserCode, TransactionHeaders.IssueDate, NetAmount ".
					 " From TransactionHeaders ".
					 " Inner Join Currencies on Currencies.CurrencyCode = TransactionHeaders.CurrencyCode ".
					 " Inner Join Clients on Clients.ClientCode = TransactionHeaders.ClientCode ".
					 " Where isNull(IsProcessed,0) = 2 ".
					 " And isNull(TransactionStatus,0) != 2 ";
			*/		 
		$sub_query = " Select delivery_id, deliveries.client_code, clients.ledger_name, deliveries.costcent_code, deliveries.currency_code, 
		deliveries.user_account,date_format(deliveries.delivery_date,'%d/%m/%Y') delivery_date,
		deliveries.delivery_total+deliveries.delivery_vat-Delivery_DiscountAmount as nettotal,delivery_reference ".
					 " From deliveries_temp as  deliveries".
					 " left Join Clients on Clients.ledger_number = deliveries.Client_Code ".
					 " Where 1=1 and deliveries.Synchronized=0 ";
					 
	    if($salesman_code != ""){
			$sub_query = $sub_query." and SALESMAN_cODE = '$salesman_code'";
		}
		if ($TableOption == 30) $sub_query .= " And deliveries.journal_type='DI' ";
		else if ($TableOption == 31) $sub_query .= " And deliveries.journal_type='DR'  ";
		$k=0;		
	//echo $sub_query ;//exit;		
		//$sub_stmt = sqlsrv_query( $conn, $sub_query );
		$sub_stmt = mysql_query($sub_query,$connection );
		if($order_by_date == 1){
			$checked = "checked";
		}
		else{
			$checked = "";
		}
		//$data = $sub_query."<br>";
		$data = "<input type='hidden' id='TableOption' name='TableOption' value='$TableOption'>";
		$data = $data."<table><tr><td style='font-size: 12px; font-weight: bold'>Filter By Salesman: </td><td style='font-size: 12px; font-weight: bold'><input type='text' id='salesman_code' name='salesman_code' value='$salesman_code' style='width: 50px;'>
		         &nbsp;<input type='text' id='salesman_name' name='salesman_name' value = '$salesman_name' style='width: 150px'>&nbsp;<img src='img/btnopen.jpg' width=20 onclick='openSalesmen()' style='cursor: pointer'>
				 &nbsp;<button type='button' id='btnReceive' name='btnReceive' onclick=\"searchInvoiceAndReturn($TableOption);\" title='Search'><span><span>Search</span></span></button> <button type='button' id='btnReceive' name='btnReceive' onclick=\"clearInvoiceAndReturn($TableOption);\" title='Clear'><span><span>Clear</span></span></button>
				 &nbsp;&nbsp;<input type='checkbox' name='chckorderbydate' onclick='checkByDateInvoiceAndReturn($TableOption);' $checked><input type='hidden' name='order_by_date' value='$order_by_date'> Order By Date
		        </td></tr>";
		if ($sub_stmt) {
			$data = $data."<span class='style7' style='padding-bottom:5px;'><a href='javascript:void();' onclick=\"ExportData('".$_GET['TableOption']."');\">Export Excel</a></span>
					 <table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed;' class='grid_data'>
					 <col width=25>";
			$query_login = "select FI_DeleteMobileSorders, FI_DeleteMobileInvoices, FI_DeleteMobileReturnInvoices 
					                from login
where user_account='".$_SESSION['useraccount']."' 
	       AND Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')";
		   $result_login = mysql_query($query_login, $connection2);
		   $row_login    = mysql_fetch_array($result_login);
		   extract($row_login);
		   if($TableOption == 30){
					 if($FI_DeleteMobileInvoices == 1){
						 $data = $data."<col width=20>";
					 }
		   }
		   else
			   if($TableOption == 31){
				   if($FI_DeleteMobileReturnInvoices == 1){
						 $data = $data."<col width=20>";
					 }
			   }
			$data = $data."<col width=100>
					 <col width=100>
					 <col width=100>
					 <col width=200>
					 <col width=100>
					 <col width=100>
					 <col width=60>
					 <col width=150>
					 <tr>
						<th align='center'>&nbsp;</th>";
						if($TableOption == 30){
					 if($FI_DeleteMobileInvoices == 1){
						 $data = $data."<th align='center'>&nbsp;</th>";
					 }
						}
						else
			   if($TableOption == 31){
				   if($FI_DeleteMobileReturnInvoices == 1){
					   $data = $data."<th align='center'>&nbsp;</th>";
				   }
			   }
			$data = $data."<th align='center'>Reference</th>
						<th align='center'>Date</th>
						<th align='center'>Client</th>
						<th align='center'>&nbsp;</th>
						<th align='center'>User Code</th>
						<th align='center'>Company Code</th>
						<th align='center'>Currency</th>
						<th align='center'>Amount</th>
					 </tr>";
		} else {
			$data = $data."<span class='message'>No Data Found.</span>";
		}
		
		while( $sub_rows = mysql_fetch_assoc( $sub_stmt) ) {
		
				
		//while( $sub_rows = sqlsrv_fetch_array( $sub_stmt, SQLSRV_FETCH_ASSOC) ) {
			$k += 1;
			$SelRecord  = 'R'.$k;
			$$SelRecord = $sub_rows['delivery_id'];
			
			$SlideDivId = 'displaydetails'.$sub_rows['delivery_id'];
				
			$checked = '';	
			//if($_GET['TableOption'] == "25") $checked = 'checked';
			
			$data .= "<tr>";
			$data .= " 	<td align='center'><input type='checkbox' name='$SelRecord' id='$SelRecord' value='".$$SelRecord."' onclick='SelectRecord();' $checked></td> ";
			if($TableOption == 30){
					 if($FI_DeleteMobileInvoices == 1){
						 $data = $data."<td align='center'><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteInvoiceAndReturn('".$sub_rows['delivery_id']."', '30')\"></td>";
					 }
						}
						else
			   if($TableOption == 31){
				   if($FI_DeleteMobileReturnInvoices == 1){
					   $data = $data."<td align='center'><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteInvoiceAndReturn('".$sub_rows['delivery_id']."', '31')\"></td>";
				   }
			   }
			$data .= "	<td align='left'><span class='style7' onMouseOver=\"style.cursor='hand'\" onclick=\"ShowHideDetails('".$sub_rows['delivery_id']."');\">".$sub_rows['delivery_reference']."</span></td>";
			$data .= "	<td align='left'><span class='style7'>".$sub_rows['delivery_date']."</span></td>";
			$data .= "	<td align='left' onMouseOver=\"style.cursor='hand'\"><span class='style7'>".$sub_rows['client_code']."</span></td>";
			$data .= "	<td align='left'><span class='style7'>".iconv('windows-1256', 'utf-8',$sub_rows['client_name'])."</span></td>";
			$data .= "	<td align='left'><span class='style7'>".$sub_rows['user_account']."</span></td>";
			$data .= "	<td align='left'><span class='style7'>".$sub_rows['costcent_code']."</span></td>";
			$data .= "	<td align='left'><span class='style7'>".$sub_rows['currency_code']."</span></td>";
			$data .= "	<td align='right'><span class='style7'>".number_format($sub_rows['nettotal'], $DecimalBase)."</span></td>";
			$data .= "</tr>";
			
			/*$sub_query1 = " Select TransactionDetails.ItemCode, Items.ItemName, BasicUnitQuantity, TransactionDetails.PriceSmall, DiscountPercentage, DiscountAmount, ItemTaxPercentage ".
						  " From TransactionDetails ".
						  " Inner Join Items on Items.ItemCode = TransactionDetails.ItemCode ".
						  " Where TransactionCode = ".GetSQLValueString($sub_rows['TransactionCode'],'text');
			*/		  
			$sub_query1 = " Select deliverydt.item_code, Items.item_description, deliverydt_quantity, deliverydt_price,
			              DeliveryDt_DiscountPer,(DeliveryDt_DiscountPer/100) as DiscountAmount,
						  (deliverydt_quantity*deliverydt_price*deliverydt.item_vat/100) deliverydt_vat ".
						  " From deliverydt_temp deliverydt".
						  " Inner Join Items on Items.Item_Code = deliverydt.Item_Code ".
						  " Where delivery_id = ".GetSQLValueString($sub_rows['delivery_id'],'text');
			//echo $sub_query1;exit;
			
			//$sub_stmt1 = sqlsrv_query( $conn, $sub_query1 );
			$sub_stmt1 = mysql_query($sub_query1,$connection );
			if ($sub_stmt1) {
				$data .= "<tr><td colspan='9'>";
				$data .= "	<div id='$SlideDivId' class='div-search-details' style='display:none; margin-left:40px;'>";
				$data .= "	<table border='0' style='table-layout:fixed' cellpadding='0' cellspacing='0' width='770px'>
							<col width=150>
							<col width=250>
							<col width=70>
							<col width=100>
							<col width=60>
							<col width=80>
							<col width=60>
							<tr>
								<th align='center'>Item</th>
								<th align='center'>Description</th>
								<th align='center'>Qty</th>
								<th align='center'>Price</th>
								<th align='center'>Disc %</th>
								<th align='center'>Disc</th>
								<th align='center'>VAT</th>
							</tr>";
			
			}
			
			$data1 = "";
			while( $sub_rows1 = mysql_fetch_assoc( $sub_stmt1) ) {
			//while( $sub_rows1 = sqlsrv_fetch_array( $sub_stmt1, SQLSRV_FETCH_ASSOC) ) {
				$data1 .= "<tr>";
				$data1 .= "	<td align='left'><span class='style7'>".$sub_rows1['item_code']."</span></td>";
				$data1 .= "	<td align='left'><span class='style7'>".iconv('windows-1256', 'utf-8',$sub_rows1['item_description'])."</span></td>";
				$data1 .= "	<td align='right'><span class='style7'>".number_format($sub_rows1['deliverydt_quantity'], $DecimalBase)."</span></td>";
				$data1 .= "	<td align='right'><span class='style7'>".number_format($sub_rows1['deliverydt_price'], $DecimalBase)."</span></td>";
				$data1 .= "	<td align='right'><span class='style7'>".number_format($sub_rows1['DeliveryDt_DiscountPer'], $DecimalBase)."</span></td>";
				$data1 .= "	<td align='right'><span class='style7'>".number_format($sub_rows1['DiscountAmount'], $DecimalBase)."</span></td>";
				$data1 .= "	<td align='right'><span class='style7'>".number_format($sub_rows1['deliverydt_vat'], $DecimalBase)."</span></td>";
				$data1 .= "</tr>";
			
			}
			
			if ($sub_stmt1) {
				$data1 .= "</table></div></td></tr>";
				$data .= $data1;
			}	
			
		}
		
		$data .= "<input type='hidden' name='ChkCounter' id='ChkCounter' value='$k'>";
		$data .= "</table>";
		return $data;
}



?>

