<?php

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

include("config.inc.php");
include("Functions_Trans.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 FA_TransferFunds as loginaccess 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, ifNull(Client_ValidateProject,0) As Client_ValidateProject, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage 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_Delivery,0) As Module_Delivery, ifNull(Module_AcctrxPostRV,0) As Module_AcctrxPostRV, ".
			 " ifNull(Module_AcctrxPostCV,0) As Module_AcctrxPostCV ". 
             " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

$sub_query = "Select Ledger_Name From Ledger Where Ledger_Number = $fromledger";
$sub_result = selectData($connection, $sub_query);
$fromledgername = $sub_result[0];

$sub_query = "Select Ledger_Name From Ledger Where Ledger_Number = $toledger";
$sub_result = selectData($connection, $sub_query);
$toledgername = $sub_result[0];

$sub_query = "Select Project_Name From Project Where Project_Code = '".$fromproject."'";
$sub_result = selectData($connection, $sub_query);
$fromprojectname = $sub_result[0];

$sub_query = "Select Project_Name From Project Where Project_Code = '".$toproject."'";
$sub_result = selectData($connection, $sub_query);
$toprojectname = $sub_result[0];

$sub_query = "Select CostCent_Name From CostCent Where CostCent_Code = '".$fromcostcent."'";
$sub_result = selectData($connection, $sub_query);
$fromcostcentname = $sub_result[0];

$sub_query = "Select CostCent_Name From CostCent Where CostCent_Code = '".$tocostcent."'";
$sub_result = selectData($connection, $sub_query);
$tocostcentname = $sub_result[0];

if (empty($transdate)) $transdate = displayToday();
if (empty($transcurr)) $transcurr = $base1;

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


print <<< HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<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 SearchForm(){
	var x=document.invoices;
	
    XPos = (screen.availHeight - 560) / 2;
	YPos = (screen.availWidth - 970) / 2;
	
	
    win = window.open('Form_TransferFundsSearch.php', 'Form_TransferFundsSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=990,height=580,left='+YPos+',top='+XPos);
	win.creator = self;
	win.focus();		
			
}

function ClearForm(){
	var x = document.invoices;
		
	ClearFields();
	x.txtJourId.value='';
	x.action='Form_TransferFunds.php';
	x.submit();
}	

function ClearFields(){
    var x = document.invoices;
	
	x.transamount.value='';
	x.transdesc.value='';
	x.transcurr.value='';
	x.transdate.value='';
	
	x.fromledger.value='';
	x.fromledgername.value='';
	x.fromproject.value='';
	x.fromprojectname.value='';
	x.fromcostcent.value='';
	x.fromcostcentname.value='';
	x.toledger.value='';
	x.toledgername.value='';
	x.toproject.value='';
	x.toprojectname.value='';
	x.tocostcent.value='';
	x.tocostcentname.value='';
	
}

function SubmitForm() {
	var x=document.invoices;
	
	var vFromLedger  = x.fromledger.value;
	var vToLedger    = x.toledger.value;
	var vTransDate   = x.transdate.value;
	var vTransCurr   = x.transcurr.value;
	var vTransAmount = x.transamount.value;
	
		
	if (vFromLedger == "") {
		alert("Please select from ledger account.");
		x.fromledger.focus();		
	}
	else if (vToLedger == "") {
		alert("Please select to ledger account.");
		x.toledger.focus();		
	}	
	else if (vTransDate == "") {
		alert("Please select a certian date.");
		x.transdate.focus();		
	}
	else if (vTransCurr == "") {
		alert("Please select a certian currency.");
		x.transcurr.focus();
	}	
	else {
		if (document.getElementById('btnSubmit')) x.btnSubmit.disabled = true;
			
		x.action =  "Form_TransferFunds_Actions.php?mode=TransferFunds";
		x.submit();
		
		document.getElementById("imgDiv").style.display="block";
	}	

}


function showlistledger(str){

	var str1 = 0;
	window.open('select_accountno.php?item='+str+'&accountoption='+str1,'mywindow','width=710,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistProject(item) {
	window.open('List_Project_Reports.php?frm=2&item='+item,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(item){

  window.open('List_Costcenter_Reports.php?frm=2&item='+item,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


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 getRate(){
	var vDate = document.invoices.transdate.value;
	var vCur  = document.invoices.transcurr.value;
	if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText.split('[BRK]');
				document.getElementById("fundrate").value = data[1];			
			}
		}
	    
		xmlhttp.open("GET",'Form_TransferFunds_Actions.php?mode=getRate&txtDate='+vDate+'&currency_code='+vCur);
		xmlhttp.send();
}
</script>
<title>Transfer Funds</title>
<style type="text/css">


.style5 {color: #5151FF; font-size: 10px;}


div#imgDiv {
	font-size: 12px; 
    font-weight:bold; 
	color: #FF0000 ;
	text-align: center; 
	margin-top:10px;
	display:none;
}
</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='invoices' method='post' action=''>
<input type='hidden' name='HideMenu' value='$HideMenu'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='txtJourId' value='$txtJourId'>
<input type='hidden' name='message' value='$message' size='200'>
HERE;

//$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'>";
if ($HideMenu != 1) include_once("header.php");
echo" <tr><td  valign='top'>";

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



if ($txtJourId != '') {
	// Read From Temp Tables For Selected Record				 
	$query = " Select Journal_ID, Journal_RefGovernment, Journal_Folio, Journal_Type, Journal_TypeRef, ".
			 " Date_Format(Journal_Date,'%d/%m/%Y') As Journal_Date, Journal_Remark, Manual_No, ".
			 " ifNull(Amnt1_db,0), ifNull(Amnt2_db,0), ifNull(Amnt1_cd,0), ifNull(Amnt2_cd,0),  ".
			 " Project_code, Costcent_Code ".
			 " From Journal ".
			 " Where Journal_ID = ".$txtJourId;
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtJourId   = $rows[0];
	$txtJourRef  = $rows[1];
	$txtFolio    = $rows[2];
	$transdate   = $rows[5];
	$transdesc   = $rows[6];
	
	$sub_query1 = " Select JournalDt.Ledger_Number, Ledger.Ledger_Name, Journaldt_desc, CstJour.Project_Code, Project.Project_Name, ".
				  " CstJour.CostCent_Code, CostCent.CostCent_Name, JournalDt.Currency_Code, JournalDt.Journaldt_amount  ".
				  " From JournalDt ".
				  " Inner Join Ledger on Ledger.Ledger_Number = JournalDt.Ledger_Number ".
				  " Left Join CstJour on CstJour.JournalDt_ID = JournalDt.JournalDt_ID ".
				  " Left Join Project on CstJour.Project_Code = Project.Project_Code ".
				  " Left Join CostCent on CstJour.CostCent_Code = CostCent.CostCent_Code ".
				  " Where JournalDt.Journal_ID = '".$txtJourId."' ".
				  " And upper(JournalDt.Journaldt_dbcd) = 'C' ".
				  " Limit 1 ";
	$sub_result1 = mysql_query($sub_query1,$connection) or die(mysql_error());
    $sub_rows1 = mysql_fetch_array($sub_result1);
	
	$fromledger   	  = $sub_rows1[0];
	$fromledgername   = $sub_rows1[1];
	$transdesc   	  = $sub_rows1[2];
	$fromproject   	  = $sub_rows1[3];
	$fromprojectname  = $sub_rows1[4];
	$fromcostcent     = $sub_rows1[5];
	$fromcostcentname = $sub_rows1[6];
	$transcurr   	  = $sub_rows1[7];
	$transamount   	  = $sub_rows1[8];
	
	
	$sub_query2 = " Select JournalDt.Ledger_Number, Ledger.Ledger_Name, Journaldt_desc, CstJour.Project_Code, Project.Project_Name, ".
				  " CstJour.CostCent_Code, CostCent.CostCent_Name, JournalDt.Currency_Code, JournalDt.Journaldt_amount  ".
				  " From JournalDt ".
				  " Inner Join Ledger on Ledger.Ledger_Number = JournalDt.Ledger_Number ".
				  " Left Join CstJour on CstJour.JournalDt_ID = JournalDt.JournalDt_ID ".
				  " Left Join Project on CstJour.Project_Code = Project.Project_Code ".
				  " Left Join CostCent on CstJour.CostCent_Code = CostCent.CostCent_Code ".
				  " Where JournalDt.Journal_ID = '".$txtJourId."' ".
				  " And upper(JournalDt.Journaldt_dbcd) = 'D' ".
				  " Limit 1 ";
	$sub_result2 = mysql_query($sub_query2,$connection) or die(mysql_error());
    $sub_rows2 = mysql_fetch_array($sub_result2);
	
	$toledger   	= $sub_rows2[0];
	$toledgername   = $sub_rows2[1];
	$toproject   	= $sub_rows2[3];
	$toprojectname  = $sub_rows2[4];
	$tocostcent     = $sub_rows2[5];
	$tocostcentname = $sub_rows2[6];
	
}


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'>TRANSFER FUNDS</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'>";
				
echo"   		<button type='button' name='btnClear' id='btnClear' onclick='ClearForm();return false;' title='Search'><span><span>Clear Page</span></span></button>";
echo" 			<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm();return false;' title='Transfer Funds'><span><span>Transfer</span></span></button>";	
echo"   		<button type='button' name='btnSearch' id='btnSearch' onclick='SearchForm();return false;' title='Search'><span><span>Search</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=70>
		  <col width=150>
		  <col width=70>
		  <col width=160>
		  <col width=70>
		  <col width=150>
		  <col width=100>
		  <col width=200>

HERE;


$strt = $_SESSION['StartFinancial_Date'];
$end = $_SESSION['EndFinancial_Date'];
$ndate = explode('/', $transdate);
$dateFormat = $ndate[2]."-".$ndate[1]."-".$ndate[0];
			
echo" <tr height='25'>";	  
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>Date</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>";
echo"      <input type='text' name='transdate' value='$transdate' onBlur=\"checkDateFormat(this,'$strt','$end');\" onchange=\"getRate();\"    maxlength='10' style='width:100px;'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.invoices.transdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Amount</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>";
echo"      <input type='text' name='transamount' value='$transamount'  maxlength='10' style='width:120px;'></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Curr.</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'><select name='transcurr' id='transcurr' onchange=\"getRate();\" style='width:70px; height:22px;'>";
			echo"<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
			echo"<option ";
				if ($transcurr==$curf[0])
					echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"</select>";
echo"	</td>";
$queryrate = "select Curdt_parity from curdt 
			where currency_code='$transcurr' and date_format(curdt_date,'%Y-%m-%d') <= '$dateFormat'
			order by date_format(curdt_date,'%Y-%m-%d') desc limit 1; ";
$resrate = mysql_query($queryrate,$connection);
$rwrate = mysql_fetch_row($resrate);
$fundrate = $rwrate[0];
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Rate</span> <input type='text' name='fundrate' id ='fundrate' value='$fundrate'  style='width:50px;'></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Jour Ref.</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>$txtJourRef</span></td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td valign='middle' bgcolor='#F5F5F5' ><span class='style1'>Description</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='5'><span class='style1'>";
echo"      <input type='text' name='transdesc' value='$transdesc'  maxlength='100' style='width:530px;'></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>Folio</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5'><span class='style1'>$txtFolio</span></td>";
echo" </tr>";


echo" <tr height='25'>";
echo"   <td align='center' bgcolor='#F5F5F5' colspan='4'><span class='style1'>Transfer From</span></td>";
echo"   <td align='center' bgcolor='#F5F5F5' colspan='4'><span class='style1'>Transfer To</span></td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>Account</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo"   	<input name='fromledger' type='text' id='fromledger' value='".$fromledger."' style='width:100px;' readonly />
			<input name='fromledgername' type='text' id='fromledgername' value='".$fromledgername."' style='width:240px;' readonly />
		</span>
		<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select Ledger' 
             onClick='showlistledger(5); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo"   <td align='left' bgcolor='#F5F5F5'><span class='style1'>Account</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo" 	 	<input name='toledger' type='text' id='toledger' value='".$toledger."' style='width:100px;' readonly />
			<input name='toledgername' type='text' id='toledgername' value='".$toledgername."' style='width:240px;' readonly />
		</span>";
echo"	<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select Ledger' 
             onClick='showlistledger(4); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>Project</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo"   	<input name='fromproject' type='text' id='fromproject' value='".$fromproject."' style='width:100px;' readonly />
			<input name='fromprojectname' type='text' id='fromprojectname' value='".$fromprojectname."' style='width:240px;' readonly />
		</span>
		<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select Project' onClick='showlistProject(1); '>
		<img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo"   <td align='left' bgcolor='#F5F5F5'><span class='style1'>Project</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo" 	 	<input name='toproject' type='text' id='toproject' value='".$toproject."' style='width:100px;' readonly />
			<input name='toprojectname' type='text' id='toprojectname' value='".$toprojectname."' style='width:240px;' readonly />
		</span>";
echo"	<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select Project' onClick='showlistProject(2); '>
		<img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo" </tr>";

echo" <tr height='25'>";
echo"   <td align='left' bgcolor='#F5F5F5' ><span class='style1'>CostCenter</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo"   	<input name='fromcostcent' type='text' id='fromcostcent' value='".$fromcostcent."' style='width:100px;' readonly />
			<input name='fromcostcentname' type='text' id='fromcostcentname' value='".$fromcostcentname."' style='width:240px;' readonly />
		</span>
		<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select CostCenter' onClick='showlistCostcenter(1); '>
		<img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo"   <td align='left' bgcolor='#F5F5F5'><span class='style1'>CostCenter</span></td>";
echo"   <td valign='middle' bgcolor='#F5F5F5' colspan='3'><span class='style1'>";
echo" 	 	<input name='tocostcent' type='text' id='tocostcent' value='".$tocostcent."' style='width:100px;' readonly />
			<input name='tocostcentname' type='text' id='tocostcentname' value='".$tocostcentname."' style='width:240px;' readonly />
		</span>";
echo"	<label><span class='style1'><a href='javascript:void(0);' NAME='My Window Name' title='Select CostCenter' onClick='showlistCostcenter(2); '>
		<img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"	</td>";
echo" </tr>";

echo" <tr height='65'>";
echo"   <td colspan='8' align='center' valign='middle' bgcolor='#F5F5F5'>";
echo "		<div id='imgDiv'><p><image src='img/ajax-loader.gif' id='loader-img' /></p></div>";
echo"	</td>";
echo" </tr>";


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


function explodeword($date)
{

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

echo"  
</form>
</body>
</html>";

?>