<?php

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


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


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


$querylog=" SELECT ifNull(FI_Charges,0) as loginaccess, ifNull(FI_ChargesApp,0) As FI_ChargesApp, ifNull(Login_FastPrint,0) As Login_FastPrint ".
		  " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')	";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select 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_PostToJournal,0) As Module_PostToJournal, ifNull(Module_ChargesShowProject,0) As Module_ChargesShowProject, ".
			 " ifNull(Module_ChargesShowCostCent,0) As Module_ChargesShowCostCent ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}





$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}   
$mainaction = $_POST["mainaction"]; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   


if (empty($mainaction))	$mainaction = 'insert';

$message = ereg_replace("'"," ",$message);

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" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" language="javascript">



function CheckFields(){
  
    x = document.charges;
	
	var ChgDate     = x.txtDate.value;
	
	
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	
	if (ChgDate=='' )
	{
		alert("Date Column is mandatory!");
		x.txtDate.focus();
		return 0;
	}
	
	else
	return 1 ;	
}




function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		x = document.charges;
		x.mainaction.value='insert';
		x.actionmode.value='';
		x.action='Form_Charges.php';
		x.submit();
	}	
}

function ClearPage1(){
	x = document.charges;
	var msg = x.message.value;
	
	ClearForm('All');
	
	x.mainaction.value='insert';
	x.actionmode.value='';
	x.message.value=msg;
	x.action='Form_Charges.php';
	x.submit();
}

function CancelRecord(){
	x = document.charges;
	var vmode  = x.mainaction.value;
	var vmode1 = x.actionmode.value;
	var ChgId = x.txtChgId.value;
	
	if (confirm ( "Are you sure you want to Delete current pending transaction permenantly?"))
	{
		ClearForm('All');
		x.txtChgId.value = ChgId;
		x.mainaction.value='insert';
		x.actionmode.value='cancel';
		x.action='Form_Charges.php';
		x.submit();
	}
    
}



function PostChanges() {
	x = document.charges;
	var ChgId  = x.txtChgId.value;
	var vMainMode = x.mainaction.value;
	
	
	if (x.txtCountDetail.value == 0 || x.txtCountDetail.value == '') {
		alert('Save not allowed. Please add at least one item.');
	}
	else
	{
		if (confirm ( "Are you sure you want to Save current transaction?"))
		{
			x.btnPost.disabled = true;
			x.btnCancel.disabled = true;
			x.btnClear.disabled = true;
			
			if (document.getElementById('btnLoadPurchases'))
				x.btnLoadPurchases.disabled = true;
			if (document.getElementById('btnLoadCharges'))
				x.btnLoadCharges.disabled = true;
			if (document.getElementById('btnDistributeCharges'))
				x.btnDistributeCharges.disabled = true;
			
				
			x.actionmode.value='post';
			x.action="Form_Charges_Actions.php?mode=post&mainmode="+vMainMode+"&SChgId="+ChgId;
			x.submit();
		}
	}
}


//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.charges;
	var vmode = x.actionmode.value;
	
	if (Option == 'All') {
	
		x.txtChgId.value='';
		x.txtRef.value='';
		x.txtDate.value='';
		x.txtJvRef.value='';
		x.txtJvFolio.value='';
		x.txtRemark.value='';
	
		x.message.value='';
		x.txtCountDetail.value='';
		
		x.actionmode.value = '';
		x.mainaction.value = '';
	} 
}

function LoadCharges() {
	x = document.charges;
	var result = CheckFields();

	var vChgId    = x.txtChgId.value;
	var vMainMode = x.mainaction.value;
	
	
	if (result == 1 && vChgId != "") {
		if (confirm ( "Are you sure you want to replace existing charges?"))
		{
			x.action="Form_Charges_Actions.php?mode=loadcharges&mainmode="+vMainMode+"&SChgId="+vChgId;
			x.submit();		
		}	
	}	
	else if (result == 1) {
		x.action="Form_Charges_Actions.php?mode=loadcharges&mainmode="+vMainMode+"&SChgId="+vChgId;
		x.submit();		
	}
    
}

function DistributeCharges() {
	x = document.charges;
	
	var vChgId    = x.txtChgId.value;
	var vMainMode = x.mainaction.value;

	if (vChgId != "") {
		if (confirm ( "Are you sure you want to distribute charges?"))
		{
			x.action="Form_Charges_Actions.php?mode=distributecharges&mainmode="+vMainMode+"&SChgId="+vChgId;
			x.submit();		
		}	
	}	
	else {
		alert("Please press Load Charges button!");
	}
}

function EditChargesDt(ChgDtId) {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	
	win = window.open('Form_ChargesDetail.php?mainaction='+vMainMode+'&actionmode=&PChgId='+vChgId+'&PChgDtId='+ChgDtId,
					  'Form_ChargesDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=700,height=300,top=100');		
	win.creator = self;
	win.focus();
	
}

function DeleteChargesDt(ChgDtId) {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	var vDelMsg   = "Are you sure you want to Delete this record ?";
	   
	if (confirm ( vDelMsg ))
	{
		x.action="Form_Charges_Actions.php?mode=deletedetail&mainmode="+vMainMode+"&SChgId="+vChgId+"&SChgDtId="+ChgDtId;
		x.submit();
	}

}

function LoadPurchases() {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	
	if (vChgId != "") {
		win = window.open('Form_Charges_LoadPurchases.php?PChgId='+vChgId, 'Form_Charges_LoadPurchases', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=960,height=580,top=100,left=20');		
		win.creator = self;
		win.focus();	
	}	
	else {
		alert("Please press Load Charges button!");
	}
	
}

function UpdateRecord(ChgItemId) {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	
	win = window.open('Form_ChargesItems.php?mainaction='+vMainMode+'&actionmode=&PChgId='+vChgId+'&PChgItemId='+ChgItemId,
					  'Form_ChargesItems', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=700,height=300,top=100');		
	win.creator = self;
	win.focus();
	
}
function DeleteRecord(ChgItemId, ItemCode) {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	var vDelMsg   = "Are you sure you want to Delete this Item ("+ItemCode+") ?";
	   
	if (confirm ( vDelMsg ))
	{
		x.action="Form_Charges_Actions.php?mode=deleteitem&mainmode="+vMainMode+"&SChgId="+vChgId+"&SChgItemId="+ChgItemId;
		x.submit();
	}

}

function ShowDetailCharges(ChgItemId) {
	x = document.charges;
	
	var vMainMode = x.mainaction.value;
	var vChgId    = x.txtChgId.value;
	
	win = window.open('Form_ChargesItemsDt.php?mainaction='+vMainMode+'&actionmode=&PChgId='+vChgId+'&PChgItemId='+ChgItemId,
					  'Form_ChargesItemsDt', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=680,height=270,top=100');		
	win.creator = self;
	win.focus();	
	
}

function SearchForRef(){
    //ClearForm('All');
    x = document.charges;
    win = window.open('Form_ChargesSearch.php', 'Form_ChargesSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=600,top=100,left=10');		
	win.creator = self;
	win.focus();		
			
}

function SearchForPending(){
    //ClearForm('All');
    x = document.charges;
    win = window.open('Form_ChargesSearchPending.php', 'Form_ChargesSearchPending', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=900,height=560,top=100,left=10');		
	win.creator = self;
	win.focus();		
			
}




function PrintCharges(ChgId, ChgRef) {
	var str = '';
	
	if (confirm ( "Do you want to Print current transaction with reference "+ChgRef+" ?"))
	{
		str = 'Form_ChargesPreview.php?ChargesId='+ChgId;
		window.open(str);
	}	
}

function roundNumber(num, dec) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  newnumber = parseFloat(newnumber); 
  
  return newnumber;
}

function formatNumber(num, dec) { // Arguments: number to format, number of decimal places
	return num.toFixed(dec);
}




</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Charges</title>



<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body>
<form name='charges' method='post'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>

HERE;


	
//Get Default Project
$querydflproject="select project_code as defaultproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

	
if ((empty($txtProjectCode)) && $Module_Project == 1)
	$txtProjectCode = $defaultproject;
if ((empty($txtCostCentCode)) && $Module_CostCenter == 1)
	$txtCostCentCode = $defaultcostcent;	
if (empty($txtCurr))
	$txtCurr = $base1;
if (empty($txtDate))	
	$txtDate = displayToday();

		
if ($actionmode != "cancel" && $txtChgId != '') {
	// Read From Temp Tables For Selected Record				 
	$query = " Select Charges_ID, Charges_Reference, Date_Format(Charges_Date,'%d/%m/%Y') As Charges_Date, Charges_Remark, ".
			 " Journal.Journal_folio, Journal.Journal_refgovernment ".
			 " From Temp_ChargesHD As ChargesHD ".
			 " Left Join Journal on ChargesHD.Journal_ID = Journal.Journal_ID ".
	         " Where Charges_ID = '".$txtChgId."' ";
			 //" And Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
			 
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtChgId       = $rows[0];
	$txtRef         = $rows[1];
	$txtDate        = $rows[2];
	$txtRemark      = $rows[3];
	$txtJvFolio	    = $rows[4];
	$txtJvRef 	    = $rows[5];
	
	
	$query = " Select Count(ChargesItems_Id) FROM Temp_ChargesItems ".
			 " Inner Join Temp_ChargesPurchases on Temp_ChargesPurchases.ChargesPurchases_Id = Temp_ChargesItems.ChargesPurchases_Id  ".
			 " Where Temp_ChargesPurchases.Charges_Id = '".$txtChgId."'";
	$result = selectData($connection, $query);
	$txtCountDetail = $result[0];
	if($txtCountDetail == '') $txtCountDetail = 0;
	
}



//Search/Cancel Mode	
if ($actionmode == "cancel" && $txtChgId != '') {
	
		
	// Delete From Temp Tables Already Filled By Search
	$query = " DELETE FROM Temp_ChargesItemsDt ".
			 " WHERE ChargesItems_Id In (Select ChargesItems_Id From Temp_ChargesItems Where ChargesPurchases_Id In (Select ChargesPurchases_Id From Temp_ChargesPurchases Where Charges_Id = '".$txtChgId."' )) ";
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))		 
		$message  = mysql_error(); 
		
	$query = " DELETE FROM Temp_ChargesItems ".
			 " WHERE ChargesPurchases_Id In (Select ChargesPurchases_Id From Temp_ChargesPurchases Where Charges_Id = '".$txtChgId."' ) ";
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))		 
		$message  = mysql_error(); 
	
	$query = " DELETE FROM Temp_ChargesPurchases ".
			 " WHERE Charges_Id = '".$txtChgId."'";
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))
		$message  = mysql_error(); 
	  
	
	$query = " DELETE FROM Temp_ChargesDT ".
			 " WHERE Charges_Id = '".$txtChgId."'";
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))
		$message  = mysql_error();   

		
	$query = " DELETE FROM Temp_ChargesHD ".
			 " WHERE Charges_Id = '".$txtChgId."'";
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	if (!updateData($connection, $query, true))
		$message  = mysql_error();   

	$txtChgId = '';
}


//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>CHARGES</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";


if ($FI_ChargesApp == 1) {

	echo" <button type='button' id='btnLoadCharges' name='btnLoadCharges' onclick='LoadCharges();return false;' title='Load Charges'><span><span>Load Charges</span></span></button>";	
	echo" <button type='button' id='btnDistributeCharges' name='btnDistributeCharges' onclick='DistributeCharges();return false;' title='Distribute Charges'><span><span>Distribute Charges</span></span></button>";	

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

		
echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";				
print <<<HERE

		<table border="0" style='table-layout:fixed'>
		  <col width=80>
		  <col width=80>
		  <col width=60>
		  <col width=110>
		  <col width=60>
		  <col width=80>
		  <col width=60>
		  <col width=80>
		  <col width=360>

		<tr bgcolor="#F7F7F7" >
          <td valign="center" align='left' ><span class="style1">Reference&nbsp;<span class="style16">*</span></span></td>
		  <td><font face='Verdana' size='2'><b>&nbsp;$txtRef</b></font>
			<input type='hidden' name='txtCountDetail' value='$txtCountDetail' >
			<input type='hidden' name='txtChgId' value='$txtChgId' >
			<input type="hidden" name="txtRef" value="$txtRef" ></td>
			<input type="hidden" name="txtJvFolio" value="$txtJvFolio" >
  		    <input type="hidden" name="txtJvRef" value="$txtJvRef" >
		  <td align="left"><span class="style1">Date&nbsp;<span class="style16">*</span></span></td>
		  <td><input type='text' name='txtDate' value='$txtDate' maxlength='10'  style='width:80px;' >
		                   <A HREF="#" onClick="displayCalendar(document.charges.txtDate,'dd/mm/yyyy',this); return false;" NAME="anchor1" ID="anchor1"><img src='./img/cal.gif' border=0 align=center></A>
			  
		  </td>
						   
HERE;

if ($Module_PostToJournal == 1) {
	echo" <td align='left'><span class='style1'>Folio</span></td>	
		  <td><font face='Verdana' size='2'><b>&nbsp;$txtJvFolio</b></font></td>
		  <td align='left'><span class='style1'>JV Ref.</span></td>	
		  <td><font face='Verdana' size='2'><b>&nbsp;$txtJvRef</b></font></td>";
} else 	{
	echo" <td align='left' colspan='4'>&nbsp;</td>";
}		  
		  
		  
echo"     <td align='left'>&nbsp;</td>";
echo"   </tr>";

echo"   <tr bgcolor='#F7F7F7' >
		  <td align='left' ><span class='style1'>Remark</span></td><td colspan='7' > <span class='style1'>
		  <input type='text' name='txtRemark' value='".htmlspecialchars($txtRemark, ENT_QUOTES)."'  style='width:385px;' maxlength='100' ></td>";

echo"     <td align='left'>&nbsp;</td>";
echo"   </tr>";	


		
echo"   <tr bgcolor='#F7F7F7'>";
echo"     <td align='left'>&nbsp;</td>";
echo"     <td align='center' colspan='7'>";
/*
if ($FI_ChargesApp == 1) {
	echo"	<input type='button' id='btnLoadCharges' name='btnLoadCharges' value='Load Charges'  onclick='LoadCharges()' $style title='Load Charges'>";
	echo"	<input type='button' id='btnDistributeCharges' name='btnDistributeCharges' value='Distribute Charges'  onclick='DistributeCharges()' $style title='Distribute Charges'>";
}	*/
echo"     </td>";
echo"     <td align='left'>&nbsp;</td>";
echo"   </tr>";

if ($txtChgId != '' ) {
	$sub_query = " Select ChargesDt_Id, ChargesDt_DebitAccount, LedgerDb.Ledger_Name As LedgerDbName, ChargesDt_CreditAccount, ".
				 " LedgerCr.Ledger_Name As LedgerCrName, ChargesDT.Currency_Code, ifNull(ChargesDt_Total,0), ".
				 " if(ChargesDt_DistOption=1, 'Amount', if(ChargesDt_DistOption=2, 'Quantity', if(ChargesDt_DistOption=3, 'Percentage',  ".
				 " if(ChargesDt_DistOption=4, 'Volume', if(ChargesDt_DistOption=5, 'Detail Amount', ''))))) As DistOption  ".
				 " From Temp_ChargesDT As ChargesDT ".
				 " Left Join Ledger as LedgerDb on ChargesDT.ChargesDt_DebitAccount = LedgerDb.Ledger_Number ".
				 " Left Join Ledger as LedgerCr on ChargesDT.ChargesDt_CreditAccount = LedgerCr.Ledger_Number ".
				 " Where ChargesDT.Charges_Id = '".$txtChgId."'";
	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
	$sub_totalrows = mysql_num_rows($sub_result);

	if ($sub_totalrows > 0) {
		
		echo"<tr bgcolor='#F7F7F7'>";
		echo"   <td align='left'>&nbsp;</td>";
		echo"	<td align='left' colspan='8'>";
		echo"	<table border='0' style='table-layout:fixed' class='grid_data'>
				  <col width=20>
				  <col width=20>
				  <col width=90>
				  <col width=150>
				  <col width=90>
				  <col width=150>
				  <col width=50>
				  <col width=100>
				  <col width=100>";
		echo"		<tr>";
		echo"  			<th bgcolor='#F7F7F7' align='left' colspan='2'>&nbsp;</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>Debit</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>&nbsp;</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>Credit</th>";		  
		echo"  			<th bgcolor='#F7F7F7' align='center'>&nbsp;</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>Currency</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>Amount</th>";
		echo"  			<th bgcolor='#F7F7F7' align='center'>Method</th>";
		echo"		</tr>";
		
		while($sub_rows=mysql_fetch_array($sub_result)){
			$ct+=1;

			if($ct%2==0)
				$Color="style='background-color:".$grid_color1."'";
			else			
				$Color="style='background-color:".$grid_color2."'";	
						
					
			echo "<tr ".$Color." >";
			echo "	<td align='center' valign='center'><span class='style5'><img src='img/edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"EditChargesDt('".$sub_rows[0]."')\" title='Edit Record'  ></span></td>";
			echo "	<td align='center' valign='center'><span class='style5'><img src='img/delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteChargesDt('".$sub_rows[0]."')\" title='Delete Record'  ></span></td>";
			echo "	<td align='left'><span class='style5'>".$sub_rows[1]."</span></td>";
			echo "	<td align='left'><span class='style5'>".$sub_rows[2]."</span></td>";
			echo "	<td align='left'><span class='style5'>".$sub_rows[3]."</span></td>";
			echo "	<td align='left'><span class='style5'>".$sub_rows[4]."</span></td>";
			echo "	<td align='center'><span class='style5'>".$sub_rows[5]."</span></td>";
			echo "	<td align='right'><span class='style5'>".number_format($sub_rows[6], $DecimalBase)."</span></td>";
			echo "	<td align='left'><span class='style5'>".$sub_rows[7]."</span></td>";
			echo "</tr>";
									
		}
		
		echo"	</table></td>";
		echo"</tr>";

	}	

}

echo"   <tr bgcolor='#F7F7F7'>";
echo"     <td align='left'>&nbsp;</td>";
echo"     <td align='center' colspan='7'>";
echo"     <td align='left'>&nbsp;</td>";
echo"   </tr>";


echo"   <tr><td colspan='9' valign='top'>";
echo"     <table border='0' cellspacing='1' style='table-layout:fixed' class='grid_data'>
          <col width=20>
		  <col width=20>
		  <col width=20>
		  <col width=70>
		  <col width=80>
		  <col width=80>
		  <col width=110>
		  <col width=140>
		  <col width=70>
		  <col width=70>
		  <col width=80>
		  <col width=70>
		  <col width=60>
		  <col width=80>";
		  
	$vItemCodeCols = 1; $vItemDescCols = 1;	  
	if ($Module_ChargesShowProject == 0 && $Module_ChargesShowCostCent == 0) {
		$vItemCodeCols = 2;
		$vItemDescCols = 2;
	} else	if ($Module_ChargesShowProject == 0 || $Module_ChargesShowCostCent == 0) {
		$vItemCodeCols = 2;
		$vItemDescCols = 1;
	}

	echo"<tr>";
	echo"  <th bgcolor='#F7F7F7' align='left'>&nbsp;</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='left'>&nbsp;</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='left'>&nbsp;</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='left'>Pur Ref.</span></th>";
	
	if ($Module_ChargesShowProject == 1)
		echo"  <th bgcolor='#F7F7F7' align='left'>Project</span></th>";
	if ($Module_ChargesShowCostCent == 1)	
		echo"  <th bgcolor='#F7F7F7' align='left'>Costcenter</span></th>";
		
    echo"  <th bgcolor='#F7F7F7' align='left' colspan='$vItemCodeCols'>Item Code</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='left' colspan='$vItemDescCols'>Description</span></th>";
		
	echo"  <th bgcolor='#F7F7F7' align='center'>Warehouse</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='center'>Quantity</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='center'>Price</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='center'>Volume</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='center'>Charges(%)</span></th>";
	echo"  <th bgcolor='#F7F7F7' align='center'>Charges Amt</span></th>";
    echo"</tr>";
		  
		  
	if ($txtChgId != '' ) {
		$i = 0;
			
		$querydt = " Select ChargesPurchases.ChargesPurchases_Id, Purchases.Purchase_Reference, ChargesItems.ChargesItems_ID, ".
				   " Purchases.Supplier_Code, PurchasesDt.Project_Code, PurchasesDt.CostCent_Code,  ".
				   " PurchasesDt.Item_CodePacked, PurchasesDt.Item_Code, Items.Item_Description, PurchasesDt.Warehouse_Code, ".
				   " ifNull(PurchaseDt_Quantity,0), ifNull(PurchasesDt.PurchaseDt_Price,0), ChargesItems.ChargesItems_Volume, ".
				   " ChargesItems_Amount, ChargesItems_AmountB1, ChargesItems_AmountB2, ifNull(ChargesItems_Perc,0) ".
				   " From Temp_ChargesPurchases As ChargesPurchases ".
				   " Inner Join Purchases on ChargesPurchases.Purchase_Id = Purchases.Purchase_Id ".
				   " Inner Join Temp_ChargesItems As ChargesItems on ChargesItems.ChargesPurchases_Id = ChargesPurchases.ChargesPurchases_Id ".
				   " Inner Join PurchasesDt on ChargesItems.PurchaseDt_Id = PurchasesDt.PurchaseDt_Id ".
				   " Inner Join Items on PurchasesDt.Item_Code = Items.Item_code ".
				   " Where ChargesPurchases.Charges_Id = '".$txtChgId."' ".
				   " Order By ChargesItems.ChargesItems_ID Desc ";
		//echo $querydt;
		$resultdt=mysql_query($querydt,$connection) or die(mysql_error());
		while($rowsdt=mysql_fetch_array($resultdt)){
			$ct+=1;

			if($ct%2==0)
				$Color="style='background-color:".$grid_color1."'";
			else			
				$Color="style='background-color:".$grid_color2."'";	
					
				
			echo "<tr ".$Color." >";
			echo "	<td align='center' valign='center'><span class='style5'><img src='img/edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateRecord('".$rowsdt[2]."') title='Edit Record'  ></span></td>";
			echo "	<td align='center' valign='center'><span class='style5'><img src='img/delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteRecord('".$rowsdt[2]."','".htmlentities($rowsdt[7])."')\" title='Delete Record'  ></span></td>";
			echo "	<td align='center' valign='center'><span class='style5'><img src='img/details.gif'  onMouseOver=\"style.cursor='hand'\" onclick=ShowDetailCharges('".$rowsdt[2]."') title='Show Details'  ></span></td>";
			echo "	<td align='left'><span class='style5'>".$rowsdt[1]."</span></td>";
			
			if ($Module_ChargesShowProject == 1)
				echo "	<td align='left'><span class='style5'>".$rowsdt[4]."</span></td>";
			if ($Module_ChargesShowCostCent == 1)	
				echo "	<td align='left'><span class='style5'>".$rowsdt[5]."</span></td>"; 
			
			echo "	<td align='left' colspan='$vItemCodeCols'><span class='style5'>".$rowsdt[7]."</span></td>";
			echo "	<td align='left' colspan='$vItemDescCols'><span class='style5'>".$rowsdt[8]."</span></td>";
			
			echo "	<td align='left'><span class='style5'>".$rowsdt[9]."</span></td>";
			echo "	<td align='right'><span class='style5'>".$rowsdt[10]."</span></td>";
			echo "	<td align='right'><span class='style5'>".number_format($rowsdt[11], $DecimalBase)."</span></td>";
			echo "	<td align='left'><span class='style5'>".$rowsdt[12]."</span></td>";
			echo "	<td align='right'><span class='style5'>".number_format($rowsdt[16], $DecimalBase)."</span></td>";
			echo "	<td align='right'><span class='style5'>".number_format($rowsdt[13], $DecimalBase)."</span></td>";
			echo "</tr>";
		}
		
		
	}
	

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


echo"   <tr bgcolor='#D8D8D8' height='30'><td colspan='9' align='center'>";

if ($FI_ChargesApp == 1)
	echo" <button type='button' id='btnLoadPurchases' name='btnLoadPurchases' onclick='LoadPurchases();return false;' title='Load Purchase(s)'><span><span>Load Purchase(s)</span></span></button>";	
	
				
echo" <button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";

		
echo"   <button type='button' name='btnCancel' id='btnCancel'  ";//cancel
		if ($actionmode=='cancel' || ($actionmode=='' && $mainaction!='edit'))
			echo " disabled ";
		echo" onclick='CancelRecord();return false;' title='Cancel Changes'><span><span>Cancel</span></span></button>";	
				
echo"   <button type='button' name='btnPost' id='btnPost' value='Save' ";//post
		if (($actionmode=='' || $actionmode=='cancel') && $mainaction!='edit')
			echo " disabled ";
		echo" onclick='PostChanges();return false;' title='Save Changes'><span><span>Save</span></span></button>";	


		
echo"   <button type='button' name='btnSearch'  id='btnSearch' value='Search' onclick='SearchForRef();return false;' title='Search By Reference' ";
        if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit') 
		    echo " disabled ";
		echo " ><span><span>Search</span></span></button>";	
				
echo"   <button name='btnPending' type='button' id='btnPending'  onclick='SearchForPending();'  title='Pending Charges' ";
        if (($actionmode!='' && $actionmode!='cancel') || $mainaction=='edit')
		    echo " disabled ";
		echo " ><span><span>Pending</span></span></button>";				

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

echo"   </table></td></tr>"; 
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo"   </table></td></tr>"; 

echo" </table>  ";//end of last table menu
	
if ($actionmode == "post" && $PrChgId != '') {
	echo '<script type="text/javascript" language="javascript">';
	
	if ($Login_FastPrint == 1) 
		echo ' PrintCharges(\''.$PrChgId.'\', \''.$PrChgRef.'\'); ';
		
	echo ' ClearPage1(); ';
	echo '</script>';

}

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

HERE;

?>