<?php

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


include("config.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");

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//User Access
$querylog=" select ifNull(Fa_clientpaymentterms,0) As FA_ClientPaymentTerms, ifNull(Clientscreditlimit,0) As FA_ClientCreditLimit, ".
          " ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(FI_DeliverySalesPrice,0) As FI_DeliverySalesPrice, ifNull(ModifyVAT,0) As ModifyVAT ".
		  " 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);
}

$querylog="select ifNull(FA_FixedAssetsDel,0) As FA_FixedAssetsDel, ifNull(FA_FixedAssetsMod,0) As FA_FixedAssetsMod
			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);
		}
//Access Mode On Insert/Edit/Delete
$DelMode  = '';
$EditMode = '';
$PrevMode = '';

$PrevHint   = '';
$EditHint   = '';
$DelHint    = '';

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

$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

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


//Get Default Project
$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['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 ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 2;

if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;

	 $Title = 'Amortization';
	 
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<title>Search $Title</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  
  document.search.PRef.value='';
  document.search.PProjectCode.value='';
  document.search.PCostCentCode.value='';
  document.search.startrange.value = '';
  document.search.action='Form_AmortizationSearchGeneration.php';
  document.search.submit();

}

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

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

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


function DeleteRecord(AssetsId, AssetsRef){
		
	if (AssetsId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+AssetsRef+" ?") )
		{
			document.search.action="Form_AmortizationActions.php?mainaction=delete&SAssetsId="+AssetsId;
			document.search.submit();
		}
	}
	
}

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

function ChangeLocation()
{
	

    if (document.search.PMode.value == 1) {
	
		opener.document.amortization.message.value = document.search.message.value;
		opener.document.amortization.action='Form_Amortization.php';
		opener.document.amortization.submit();
		self.close();
	}	
}

function UpdateRecord(Amort_Increment,amortjournal_refgoverment,amortjournal_typeref,amortjournal_folio,amortamort_fromdate,amortamort_todate,amortamort_description,journal_id){
		
	Amort_Increment=decodeURIComponent(Amort_Increment);
	amortjournal_refgoverment=decodeURIComponent(amortjournal_refgoverment);
	amortamort_description=decodeURIComponent(amortamort_description);
		

	opener.document.amortization.amortamort_increment.value=Amort_Increment;
	opener.document.amortization.amortjournal_refgoverment.value=amortjournal_refgoverment;
	opener.document.amortization.amortjournal_typeref.value=amortjournal_typeref;
	opener.document.amortization.amortjournal_folio.value=amortjournal_folio;
	opener.document.amortization.amortamort_fromdate.value=amortamort_fromdate;
	opener.document.amortization.amortamort_todate.value=amortamort_todate;
	opener.document.amortization.amortamort_description.value=amortamort_description;
	
	
	
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)
    {
				{ 
					
					opener.document.getElementById('amortjournaldtgrid').innerHTML = xmlhttp.responseText;
					self.close();
				}
		}
  }

xmlhttp.open("GET","Form_AmortizationActions.php?journal_id="+journal_id+"&actionmode=drawamortjournaldtgrid",true);
xmlhttp.send();

	
	
	

}



function PrintPreview(AssetsId)
{
	var str = '';
	
	
	str = 'Form_AmortizationPreview.php?AssetsId='+AssetsId;
	window.open(str);
	
	self.close();

}



function showlistProject(str){

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

function showlistCostcenter(str){

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

function SelectOrderBy()
{
  document.search.startrange.value = '';
  //document.search.PRef.value='';
  
  document.search.submit();

}

function SelectSearchBy()
{	
  document.search.startrange.value = '';
  //document.search.PRef.value='';
  
  document.search.submit();

}

function SubmitForm()
{	
  document.search.startrange.value = '';
  //document.search.PRef.value='';
  
  document.search.submit();

}

function kd(e)
{
	x=document.search;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}


</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
}
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;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.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: #000000 ;font-size: 10px;}

</style>

</head>
<body  onLoad='LoadForm();'>
<form name='search' method='post' action="">

HERE;

echo "<input type='hidden' name='message' value='".htmlspecialchars($message, ENT_QUOTES)."'>";

echo"<table border='0' cellspacing='1'  style='table-layout:fixed'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=65>
	  <col width=100>
	  <col width=150>  
	  <col width=75>
	  <col width=180>
	  
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='12'><span class='style16'>List of $Title</span></td></tr>
	  <tr bgcolor='#DDDDDD'>";
	  
if ($Module_Project == 1) {
	echo" <td align='left' colspan='4'><span class='style11'>Project</span></td>
	      <td align='left' colspan='2'><span class='style11'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else {
	echo" <td align='left' colspan='7'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";

}			

if ($Module_CostCenter == 1) {		  
	echo" <td align='left' ><span class='style11'>Costcenter</span></td>		
		  <td align='left'  colspan='2'><span class='style11'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='15' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
} else {
	echo" <td align='left' colspan='2'><input type='hidden' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' /></td>";

}	  


echo" <td align='right' ><span class='style11'>Order By</span></td>		
		  <td align='left' colspan=2><span class='style11'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();'>";
		
		if ($orderby==1)
			$s11 = 'selected';
		else if ($orderby==2)
			$s12 = 'selected';
			
		echo"<option ".$s11." value='1'>Date</option>
			 <option ".$s12." value='2'>Reference</option>
			 </select></span></td>";
		  
echo" </tr>";
echo" <tr bgcolor='#DDDDDD'>	  
	    <td align='left' colspan='4'><span class='style11'>Search By</span></td>
		<td align='left' colspan='5'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
			<input type='hidden' name='startrange' value='$startrange'>
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)
				$s21 = 'selected';
			else if ($searchby==2)
				$s22 = 'selected';
			else if ($searchby==3)
				$s23 = 'selected';
			else if ($searchby==4)
				$s24 = 'selected';		
				
			echo"<option ".$s21." value='1'>Reference</option>
				 <option ".$s22." value='2'>Type</option>
				 <option ".$s23." value='3'>Folio</option>
				 </select>";	
				 
echo"       <input type='submit' name='btnSubmit' value='Find' onclick='SubmitForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' valign='center'><span class='style11'>Result per page</span></td><td  colspan=2><span class='style11'><select name='perpage' id='perpage' value='$perpage'>";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span></td>';	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='12'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style25'>Reference </span></td>
		<td  height='25' align='center'><span class='style25'>Folio </span></td>
		<td  height='25' align='center'><span class='style25'>From Date </span></td>
		<td  height='25' align='center'><span class='style25'>To Date </span></td>
		<td  height='25' align='center'  colspan=4><span class='style25'>Description</span></td>
	  </tr>";
if (empty($PRef))   
   $PRef = 'Null';
else
   $PRef = "'".$PRef."'"; 

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			 
$querysearch=" select distinct amort_increment, date_format(amort_fromdate,'%d-%m-%Y') as amort_fromdate, date_format(amort_todate,'%d-%m-%Y') as amort_todate, ".
             "  amort_description,  ".
			 " amortization.journal_id, amort_belongstogroup,journal_refgovernment,journal_typeref,journal_folio ".
			 " From amortization,Journal where amortization.Journal_ID = Journal.Journal_ID ".
			 
$querysearch= $querysearch.											 
			 " And ( ($searchby = 1 and ifNull(Journal_refgovernment,-1) Like Concat(ifNull($PRef, ifNull(Journal_refgovernment,-1)),'%'))  Or ".		 
			 "      ($searchby = 2 and Journal.Journal_folio Like Concat(ifNull($PRef, Journal.Journal_folio),'%')) or ".
			 "      ($searchby = 3 and Journal.journal_typeref Like Concat(ifNull($PRef, Journal.journal_typeref),'%')) )".
			 " And ifNull(amortization.Amort_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
			 


	$querysearch= $querysearch." Order By Journal_refgovernment ";	
	
			 
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
	
$querysearch = $querysearch." limit ".$startrange.",".$perpage;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		

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

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

	$PrevHint   = 'Preview';
	$EditHint   = 'Edit Record';
	$DelHint    = 'Delete Record';
	
	
	
    if ($FA_FixedAssetsMod == 0)	$EditMode = ' disabled '; 
	
	
	
	if ($FA_FixedAssetsDel == 0)	$DelMode = ' disabled '; 
	
	
			
		   
	echo'<tr '.$Color.'>';
	        
			/*if($PrevMode=='')	
				echo'<td align="center"><span class="style15"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$amort_increment.');"  title="'.$PrevHint.'" style="width: 15px; height: 15px; " '.$PrevMode.'></span></td>';
			else*/
				echo'<td align="center"></td>';
		

		$amortamort_description = rawurlencode($amort_description);
		$amort_increment = rawurlencode($amort_increment);
		$amortjournal_refgoverment = rawurlencode($journal_refgovernment);
		$amortjournal_typeref = rawurlencode($journal_typeref);
		$amortjournal_folio = rawurlencode($journal_folio);
		$amortamort_fromdate = ($amort_fromdate);
		$amortamort_todate = ($amort_todate);
		
		
		
			if($EditMode=='')
				echo"<td align='center'><span class='style15'><img src='img/button_edit.png' onMouseOver='style.cursor=\"hand\"' 
				onclick=\"UpdateRecord('$amort_increment','$amortjournal_refgoverment','$amortjournal_typeref','$amortjournal_folio'
				,'$amortamort_fromdate'	,'$amortamort_todate','$amortamort_description','$journal_id')\" title='".$EditHint."' ".$EditMode." ></span></td>";
			else
				echo'<td align="center"></td>';
			
			/*if($DelMode=='')
				echo'<td align="center"><span class="style15"><img src="img/c_delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$amort_increment.', '.$journal_refgovernment.');" title="'.$DelHint.'" '.$DelMode.' ></span></td>';
			else
				*/
				echo'<td align="center"></td>';
	

				
			echo'<td align="left"><span class="style15">'.$journal_refgovernment.'</span></td>
			<td align="center"><span class="style15">'.$journal_typeref.'</span></td>
			<td align="center"><span class="style15">'.$journal_folio.'</span></td>
			<td align="center"><span class="style15">'.$amort_todate.'</span></td>
			<td align="center"><span class="style15">'.$amort_fromdate.'</span></td>
			<td align="left" colspan=4><span class="style15">'.$amort_description.'</span></td>
		</tr>';
}
							

			
		
echo"   <tr><td colspan='12'>&nbsp;</td></tr>
		<tr><td colspan='12'><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"<table>";

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


          

		  

print <<<HERE

</form>
</body>

HERE;

?>