<?php

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


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



$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
$perpage = $_POST['perpage']; if($perpage == '') $perpage = $_GET['perpage'];
$LedgerGrp_Id     = $_POST["LedgerGrp_Id"]; if ($LedgerGrp_Id == ""){$LedgerGrp_Id = $_GET["LedgerGrp_Id"];}  
$LedgerType     = $_POST["LedgerType"]; if ($LedgerType == ""){$LedgerType = $_GET["LedgerType"];}  
$actionmode     = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}  
$mode     = $_POST["mode"]; if ($mode == ""){$mode = $_GET["mode"];}  
$message     = $_POST["message"]; if ($message == ""){$message = $_GET["message"];}  
$txtKeyword     = $_POST["txtKeyword"]; if ($txtKeyword == ""){$txtKeyword = $_GET["txtKeyword"];}  

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



if ($LedgerType==1)	
	$querylog="SELECT FI_ClientGrp as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
else
if($LedgerType==0)		
	$querylog="SELECT FI_SupplierGrp as loginaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
	 					   
	
	//User Access
$querylog=" select ifNull(FI_ClientGrpApp,0) As FI_ClientGrpApp, ifNull(FI_ClientGrpDel,0) As FI_ClientGrpDel, ".
          " ifNull(FI_ClientGrpMod,0) As FI_ClientGrpMod, ifNull(FI_SupplierGrpApp,0) As FI_SupplierGrpApp, ifNull(FI_SupplierGrpDel,0) As FI_SupplierGrpDel,".
		  " ifNull(FI_SupplierGrpMod,0) As FI_SupplierGrpMod,language from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
//echo $querylog;
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

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




$querylang="select language,creation_date, code, name, project_code from ".$_SESSION['dbLabel'].".ledgergrp_label 
where language='$language'";


$resultlang=mysql_query($querylang,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlang)){
		//extract($rows);
		
		$creation_date=$rows[1];
		$code=$rows[2];
		$name=$rows[3];
		
		$projectcode=$rows[4];

		//echo "sdfsf".$Category_CreationDate;
	}

$querylang="select language, buttonclear, save, print, find, clear, results, page from ".$_SESSION['dbLabel'].". buttons_label 
where language='$language'";


$resultlang=mysql_query($querylang,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlang)){
		//extract($rows);
		
		$buttonclear=$rows[1];
		$save=$rows[2];
		$print=$rows[3];
		$find=$rows[4];
		$clear=$rows[5];
		$results=$rows[6];
		$page=$rows[7];
		//echo "sdfsf".$Category_CreationDate;
	}

//Access Mode On Insert/Edit/Delete
$InsMode = '';
If ($LedgerType == 1) {
	if ($FI_ClientGrpApp== 0)
		$InsMode = ' disabled ';
}
else {
	if ($FI_SupplierGrpApp== 0)
		$InsMode = ' disabled ';
}	

if(!isset($actionmode))
$actionmode='';

If ($LedgerType== '1') {
$Title = 'Client Group';
}
else {
	$Title  = ' Supplier Group ';
}	
//echo $Title;

if($mode=='editmode' or $mode=='view'){

	$query_edit=" select LedgerGrp_Id, LedgerGrp_Code, LedgerGrp_Name, Project.project_code, Project.project_name, date_format(LedgerGrp_CreationDate,'%d-%m-%Y') ".
				" from LedgerGrp Left Join Project on LedgerGrp.project_code = Project.project_code ".
				" where LedgerGrp_Id='".$LedgerGrp_Id."'";

				//echo $query_edit;
	$resultedit = mysql_query($query_edit, $connection);
	while($row=mysql_fetch_array($resultedit)){
	
        $LedgerGrp_Id=$row[0];
		$LedgerGrp_Code=$row[1];
		$LedgerGrp_Name=$row[2];
		$project_code=$row[3];
		$project_name=$row[4];
		$LedgerGrp_CreationDate=$row[5];
	}
	$mode='edit';
}

if($mode=='') $mode='insert';

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

<script type="text/javascript" language="javascript">

function showlistProject(str){

  window.open('List_Project_orderbycode.php?str='+str,'mywindow','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function editmode () {

	document.LedgerGroup.action="Form_LedgerGroup.php?mode=edit";
	document.LedgerGroup.submit();
}

function AddLedgerGroup() {
ClearForm('All');
	
	document.LedgerGroup.mode.value='insert';
	document.LedgerGroup.action="Form_LedgerGroup.php";
	document.LedgerGroup.submit();
}


function SubmitRecord() {

	if (document.LedgerGroup.LedgerGrp_Code.value=='' )
	{
		alert('Unable to save : check missing field Code ');
		document.LedgerGroup.LedgerGrp_Code.focus();
		
	}
		
	else 
	if (document.LedgerGroup.LedgerGrp_Name.value=='' )
	{
		alert('Unable to save : check missing field Name ');
		document.LedgerGroup.LedgerGrp_Name.focus();
		
	}
		
	else {
		vLedgerType = document.LedgerGroup.LedgerType.value;
		
		document.LedgerGroup.action="Form_LedgerGroup_Actions.php?LedgerType="+vLedgerType;
		document.LedgerGroup.submit();
		
	}


}


function ClearPage(){
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		
		
		x.message.value = '';
		x.mode.value='';
		x.actionmode.value='';
		x.action='Form_LedgerGroup.php';
		x.submit();
	}	
}

//All= All form; D= Detail Part
function ClearForm(Option){
    x = document.LedgerGroup;
	x.LedgerGrp_Id.value='';
	x.LedgerGrp_Code.value='';
	x.LedgerGrp_Name.value='';
    x.project_code.value='';
	x.project_name.value='';
    x.LedgerGrp_CreationDate.value='';	
	}


function SearchForLedgerGroup(){
    ClearForm('All');
    x = document.LedgerGroup;
	vLedgerType = x.LedgerType.value;
    win = window.open('Form_LedgerGroupSearch.php?LedgerType='+vLedgerType, 'Form_LedgerGroupSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=775,height=450,top=100,left=100');		
	win.creator = self;
	win.focus();		
			
}
function PrintPreview() {
	var str = '';
	vLedgerType = document.LedgerGroup.LedgerType.value;
	
	str = 'Form_LedgerGroupPreview.php?LedgerType='+vLedgerType;
	window.open(str);

}

function SetGrp(GrpId){
	
	document.LedgerGroup.LedgerGrp_Id.value=GrpId;
	document.LedgerGroup.message.value='';
	document.LedgerGroup.actionmode.value='';
	
	document.LedgerGroup.mode.value='editmode';
	document.LedgerGroup.submit();
}

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

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

function SearchForGroup(){
	x = document.LedgerGroup;
	document.LedgerGroup.action='Form_LedgerGroup.php?startrange=0';
	document.LedgerGroup.submit();
}

function ClearKeyword(){
	x = document.LedgerGroup;
	x.txtKeyword.value='';
	document.LedgerGroup.submit();
}

function submitenter(myfield,e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   	document.LedgerGroup.action='Form_LedgerGroup.php?startrange=0';
	document.LedgerGroup.submit();
  
   return false;
   }
else
   return true;
}

function confirm_delete(Code) {
	
				
	if (confirm ( "Are you sure you want to Delete this Record " + "?")) {
		window.event.returnValue = true;
		document.LedgerGroup.mode.value='delete';	
		
		document.LedgerGroup.action='Form_LedgerGroup_Actions.php?LedgerGroupId='+Code;
		document.LedgerGroup.submit();
	}
					
}

function set_limit(){

	perpage=document.LedgerGroup.perpage.value;
	document.LedgerGroup.action='Form_LedgerGroup.php?perpage='+perpage+'&startrange=0';
	document.LedgerGroup.submit();
}

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

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<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='LedgerGroup' method='post'>
<input type=hidden name='mode' value='$mode'>
<input type=hidden name='actionmode' value='$actionmode'>
<input type=hidden name='message' value='$message'>
<input type=hidden name='LedgerType' value='$LedgerType'>
<input type=hidden name='LedgerGrp_Id' value='$LedgerGrp_Id'>
HERE;

if($mode==''){
$status='disabled';
$dis='disabled';

}


if($mode=='view'){

$status='readonly';
$dis='disabled';

}

if($mode=='insert')
$status='';

//$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'>$Title</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='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>";
echo"		<button type='button' id='btnClear' name='btnClear' onclick='ClearPage(); return false;' title='Clear Page'><span><span>$buttonclear</span></span></button>";


echo"		<button type='button' id='btnPost' name='btnPost' onclick='SubmitRecord(); return false;' title='Save Changes' $Insmode><span><span>$save</span></span></button>";
echo"		<button type='button' id='btnPrt' name='btnPrt' onclick='PrintPreview(); return false;' title='Print' ><span><span>$print</span></span></button>";


				
echo"   </td><td align='left' width='260px'>
	
	<input type='text' name='txtKeyword' value='$txtKeyword' style='width:110px;' onKeyPress='return submitenter(this,event)'>&nbsp;
	<button type='button' id='btnSearch' name='btnSearch' onclick='SearchForGroup(); return false;' title='Search Group'><span><span>$find</span></span></button>
	<button type='button' id='btnClear' name='btnClear' onclick='ClearKeyword(); return false;' title='Clear Keyword'><span><span>$clear</span></span></button>
	
	</td><td align='left'><span class='style1'><b>$results/$page</b></span>
	 <span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
	 
	if ($startrange=='')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
		
	for($i=10;$i<=$maxperpage;$i=$i+10){
		if($i==$perpage)
			echo "<option value='$i'  selected>$i</option>";
		else echo "<option value='$i'>$i</option>";
		
	} 
	 echo"</td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>
<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";
		
	echo"   <tr bgcolor='#F7F7F7'><td valign='top'>";	
	  
echo"<table border='0' style='table-layout:fixed' align='left' valign='top'>
      <col width=120>
	  <col width=145>
	  <col width=355>";
	  
	  echo"<tr bgcolor='#F7F7F7' height='25'>
             <td valign='center' align='left' ><span class='style1'>$creation_date&nbsp;<span class='style26'>*</span></span>";
	
			 echo"</td>";
			 
			 if($mode!=''){
			
					$today = date('d-m-Y');
					$LedgerGrp_CreationDate = $today;
					}			
				
					
			 echo"
		     <td align='left'>
			  <input type='text' name='LedgerGrp_CreationDate' id='LedgerGrp_CreationDate' value='$LedgerGrp_CreationDate' style='font-size:12px; width:100px; height:18px;' readonly />
			  ";
			
			  if ($mode!=''){
	         $LedgerGrp_CreationDate = "disabled";
	         }

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

echo"<tr bgcolor='#F7F7F7' height='25'><td valign='center' align='left' ><span class='style1'>$code&nbsp;<span class='style26'>*</span></span></td>";
echo"<td colspan='2' align='left'><input type='text' name='LedgerGrp_Code'  value='".htmlspecialchars($LedgerGrp_Code, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status maxlength='15' /></td></tr>";

echo"<tr bgcolor='#F7F7F7' height='25'><td valign='center' align='left' ><span class='style1'>$name&nbsp;<span class='style26'>*</span></span></td>";
echo"<td colspan='2' align='left'><input type='text' name='LedgerGrp_Name' value='".htmlspecialchars($LedgerGrp_Name, ENT_QUOTES)."' style='font-size:12px; width:408px; height:18px;'  $status maxlength='100' /></td></tr>";

echo"<tr bgcolor='#F7F7F7' height='25'><td valign='center' align='left' ><span class='style1'>$projectcode &nbsp;<span class='style26'></span></span></td>";
echo"<td align='left'><input type='text' name='project_code' value='".htmlspecialchars($project_code, ENT_QUOTES)."' style='font-size:12px; width:100px; height:18px;'  $status maxlength='15' />";

if($mode!='view' && $mode!='')
	echo"&nbsp;<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Project '  width='15' height='17' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(4); return false' /></span></label></td>";

echo"<td align='left'><input type='text' name='project_name' value='".htmlspecialchars($project_name, ENT_QUOTES)."' readonly size='31' style='font-size:12px; width:261px; height:18px;'></td></tr>";	
	
echo"</table></td><td  valign='top'>";

echo"<table style='width:380px;' border='0' cellspacing=0><col width=100><col width=150>
	<tr><td id='search-top' style='width:380px;height:15px;' colspan='4'></td>
	</tr>
	 <tr><td  valign='top' colspan=3 id='search-content' style='width:380px;'><table style='width:380px;' border='0' cellspacing=0><col width=30><col width=80><col width=140><col width=130>
	<tr height='30px'><td>&nbsp;</td>
	<td align='center'><span class='style1'><b>$code</b></td><td align='center'><span class='style1'><b>$name</b></td></tr></table></td></tr>";
	 
	echo"<tr><td colspan=3 id='search-content'><div style='overflow-y:scroll;height:320px;width:370px;border:0px solid #000000;padding-left:5px;'><table  border='0' width='352px'>
	<col width=30><col width=160><col width=170>";
	
	
	$querysearch="select LedgerGrp_Id, LedgerGrp_Code, LedgerGrp_Name from  LedgerGrp ";
	$querysearch=$querysearch." where (LedgerGrp_Code like"."('%".$txtKeyword."%') or LedgerGrp_Name like"."('%".$txtKeyword."%')) and LedgerGrp_Type='".$LedgerType."'";

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

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	while($rows=mysql_fetch_array($resultsearch)){
		$ct+=1;
		$stylefont='style15';
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				
		if($mode=='edit' && $LedgerGrp_Id==$rows[0]){
			
			$Color="style='background-color:#1e87a5'";
			$stylefont='style28';
		}	
		
if ($LedgerType==1)	{
	$ModAllowed=$FI_ClientGrpMod;
	$DelAllowed=$FI_ClientGrpDel;
}

else if($LedgerType==0)	{
	$ModAllowed=$FI_SupplierGrpMod;
	$DelAllowed=$FI_SupplierGrpDel;
}	
	
	echo'<tr '.$Color.' height="20px" >';
	
	if($DelAllowed==1)	
print<<<HERE

<td align=center ><img border='0' src='img/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover="this.style.cursor='pointer'" OnClick="confirm_delete('$rows[0]')" $DelMode></td>

HERE;
else 
	print<<<HERE
<td align=center >&nbsp;</td>
HERE;



	echo"<td onMouseOver=\"style.cursor='hand'\" ";if($ModAllowed==1) echo " ondblclick=SetGrp('$rows[0]');";echo"><span class='$stylefont' >$rows[1]&nbsp;</span></font></td>
		<td onMouseOver=\"style.cursor='hand'\" ";if($ModAllowed==1) echo " ondblclick=SetGrp('$rows[0]');";echo"><font face='Arial' size='2' color='#186194'><span class='$stylefont'>$rows[2]&nbsp;</span></font></td>";
	echo"";
	


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

echo"<tr height='50px'><td id='search-content' colspan='2'><table width=100% border='0'><tr><td align='center' width=50%>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr><tr>
					<td id='search-bottom' style='width:380px;height:20px;' colspan='6'></td>
				</tr></table></td></tr>";

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

/*echo"<td><tr><table border=0 align=left width=100%> <tr bgcolor=#D8D8D8 height='35'>";
			
echo"<tr bgcolor=#D8D8D8 height='35'> ";				

	
			echo"	<td align=center><input type='button'  value='Add Ledger Group'";  	
			
			if($mode=='insert' or $mode=='edit')
			echo "disabled ";
			
			echo"onclick='AddLedgerGroup()' $style title='Add Ledger Group' $InsMode>";
				
echo"           <input type='button' id='btnClear' name='BtnClear' value='Clear Page'   onclick='ClearPage();' $style title='Clear Page'>";

				
				
		if($mode=='view'){
echo"           <input type='button' name='btnEdit' id='btnEdit' value='Edit' ";//Edit
				
				
				echo" onclick='editmode();' $style	title='Edit SubGroup' />";
	}
else	{
echo"           <input type='button' name='btnPost' id='btnPost' value='Save' ";//post
				if ($mode=='')
						echo " disabled ";
				
				echo" onclick='SubmitRecord();' $style	title='Save Changes' />";}

				
echo" 			<input name='btnPrint' type='button' id='btnPrint' value='Print' onclick='PrintPreview();' $style title='Print' ";
				if ($mode=='edit' or $mode=='insert') echo " disabled ";
						echo" onclick='PrintPreview();' $style	title='Print' />";

						echo"</td>";
				

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

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


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

HERE;

?>