<?php

/* $Id: GLPostings.inc 6945 2014-10-27 07:20:48Z daintree $*/

/* This file contains the code to post GL transactions.

This file can be included on any page that needs GL postings to be posted eg inquiries or GL reports
GL posting thus becomes an invisible/automatic process to the user

The logic of GL posting consists of:


Then looping through all unposted GL transactions in GLTrans table and

1. Debit amounts increase the charge in the period for the account and credit amounts decrease the charge.
2. Chart Details records for all following periods have the b/fwd balance increased for debit amounts and decreased for credits.
3. Once these updates are done the GLTrans record is flagged as posted.


Notes:

ChartDetail records should already exist - they are created (from includes/DateFunctions.in GetPeriod) when a new period is created or when a new GL account is created for all periods in the periods table. However, we may need to create new ones if the user posts back to a period before periods are currently set up - which is not actually possible with the config parameter ProhibitGLPostingsBefore set (However, is a problem when it is not set)
*/


$FirstPeriodResult = DB_query("SELECT MIN(periodno) FROM periods");
$FirstPeriodRow = DB_fetch_row($FirstPeriodResult);
$CreateFrom = $FirstPeriodRow[0];

if (is_null($FirstPeriodRow[0])){
	//There are no periods defined
	$InsertFirstPeriodResult = DB_query("INSERT INTO periods VALUES (-1,'" . Date('Y-m-d',mktime(0,0,0,Date('m')-1,0,Date('Y'))) . "')",_('Could not insert first period'));
	$InsertFirstPeriodResult = DB_query("INSERT INTO periods VALUES (0,'" . Date('Y-m-d',mktime(0,0,0,Date('m')+1,0,Date('Y'))) . "')",_('Could not insert first period'));
	$InsertFirstPeriodResult = DB_query("INSERT INTO periods VALUES (1,'" . Date('Y-m-d',mktime(0,0,0,Date('m')+2,0,Date('Y'))) . "')",_('Could not insert second period'));
	$CreateFrom=-1;
}

$LastPeriodResult = DB_query("SELECT MAX(periodno) FROM periods");
$LastPeriodRow = DB_fetch_row($LastPeriodResult);


$CreateTo = $LastPeriodRow[0];

/*First off see if there are in fact any chartdetails */

$sql = "SELECT chartmaster.accountcode, MIN(periods.periodno) AS startperiod
				FROM (chartmaster CROSS JOIN periods)
				LEFT JOIN chartdetails ON chartmaster.accountcode = chartdetails.accountcode
				AND periods.periodno = chartdetails.period
				WHERE (periods.periodno BETWEEN '"  . $CreateFrom . "' AND '" . $CreateTo . "')
				AND chartdetails.actual IS NULL
				GROUP BY chartmaster.accountcode";

$ChartDetailsNotSetUpResult = DB_query($sql,_('Could not test to see that all chart detail records properly initiated'));

if(DB_num_rows($ChartDetailsNotSetUpResult)>0){

		/*Now insert the chartdetails records that do not already exist */
		$sql = "INSERT INTO chartdetails (accountcode, period)
					SELECT chartmaster.accountcode, periods.periodno
					FROM (chartmaster CROSS JOIN periods)
					LEFT JOIN chartdetails ON chartmaster.accountcode = chartdetails.accountcode
					AND periods.periodno = chartdetails.period
					WHERE (periods.periodno BETWEEN '"  . $CreateFrom . "' AND '" . $CreateTo . "')
					AND chartdetails.accountcode IS NULL";

		$ErrMsg = _('Inserting new chart details records required failed because');
		$InsChartDetailsRecords = DB_query($sql,$ErrMsg);
}



/*All the ChartDetail records should have been created now and be available to accept postings */

for ( $CurrPeriod = $CreateFrom; $CurrPeriod <= $CreateTo; $CurrPeriod++ ) {
	//get all the unposted transactions for the first and successive periods ordered by account
	
	
	$sql = "SELECT counterindex,
                	type,
					periodno,
					account,
					amount,
					departmentid
				FROM gltrans
				WHERE posted=0
				AND periodno='" . $CurrPeriod . "'
				AND type=10
				ORDER BY account";

	$UnpostedTransResult = DB_query($sql);

	$TransStart = DB_Txn_Begin();
	$CurrentAccount='0';
	$TotalAmount=0;
	
	
	
	
	
	while ($UnpostedTrans=DB_fetch_array($UnpostedTransResult)) {
		$departmentid=$UnpostedTrans['departmentid'];
		//Posting chartdetails if the department is GFS, that is 1
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==1) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualgfs = actualgfs + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdgfs = bfwdgfs + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
	
		//Posting chartdetails if the department is GFW, that is 2
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid== 2 ) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualgfw = actualgfw + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdgfw = bfwdgfw + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
		//Posting chartdetails if the department is GFN, that is 3
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==3) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualgfn = actualgfn + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdgfn = bfwdgfn + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		//Posting chartdetails if the department is Aviation South, that is 4
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==4) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualaviationsouth = actualaviationsouth + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							bfwdaviationsouth= bfwdaviationsouth+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
		//Posting chartdetails if the department is Aviation West, that is 5
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==5) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualaviationwest = actualaviationwest  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdaviationwest = bfwdaviationwest + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
		//Posting chartdetails if the department is Fire-Suva, that is 6
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==6) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualfiresuva = actualfiresuva  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdfiresuva = bfwdfiresuva + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
		//Posting chartdetails if the department is Fire-West, that is 7
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==7) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualfirewest = actualfirewest  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdfirewest = bfwdfirewest + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
			
		//Posting chartdetails if the department is Fire-North, that is 8
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==8) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualfirenorth = actualfirenorth  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdfirenorth = bfwdfirenorth + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
			//Posting chartdetails if the department is Security Installs Suva, that is 9
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==9) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualsecurityinstallssuva = actualsecurityinstallssuva  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdsecurityinstallssuva = bfwdsecurityinstallssuva + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
				//Posting chartdetails if the department is Security Installs West, that is 10
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==10) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualsecurityinstallswest = actualsecurityinstallswest  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdsecurityinstallswest = bfwdsecurityinstallswest + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
			//Posting chartdetails if the department is Monitoring Suva, that is 11
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==11) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualmonitoringsuva = actualmonitoringsuva  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdmonitoringsuva = bfwdmonitoringsuva + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
			//Posting chartdetails if the department is General Manager, that is 12
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==12) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualgm = actualgm  + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdgm = bfwdgm + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
			//Posting chartdetails if the department is Finance Suva, that is 13
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==13) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualfinancesuva = actualfinancesuva + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdfinancesuva = bfwdfinancesuva+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
				//Posting chartdetails if the department is Finance West, that is 14
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==14) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                   actualfinancewest = actualfinancewest + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							bfwdfinancewest = bfwdfinancewest+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
				//Posting chartdetails if the department is Properties Suva, that is 15
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==15) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualpropertiessuva= actualpropertiessuva + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdpropertiessuva = bfwdpropertiessuva+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
				//Posting chartdetails if the department is Properties west, that is 16
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==16) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                     actualpropertieswest= actualpropertieswest + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdpropertieswest = bfwdpropertieswest+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
			//Posting chartdetails if the department is Properties North, that is 17
		
		if($CurrentAccount != $UnpostedTrans['account'] AND $CurrentAccount!='0' AND $departmentid==17) {
			$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
					                    actualpropertiesnorth= actualpropertiesnorth + " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period= '" . $CurrPeriod . "'";
			$PostPrd = DB_query($sql);
			/*Update the BFwd for all following ChartDetail records */
			$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
			 							 bfwdpropertiesnorth = bfwdpropertiesnorth+ " . $TotalAmount . "
					WHERE accountcode = '" . $CurrentAccount . "'
					AND period > '" . $CurrPeriod . "'";
			$PostBFwds = DB_query($sql);
			$TotalAmount = 0;
			
		}
		
		
		$CurrentAccount = $UnpostedTrans['account'];
		$TotalAmount += $UnpostedTrans['amount'];
		
	} 
	// There will be one account still to post after the loop
	if($CurrentAccount != 0 AND $departmentid==1) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									actualgfs = actualgfs + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									bfwdgfs = bfwdgfs + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	if($CurrentAccount != 0 AND $departmentid==2) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualgfw = actualgfw + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									bfwdgfw = bfwdgfw + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
	if($CurrentAccount != 0 AND $departmentid==3) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualgfn = actualgfn + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdgfn = bfwdgfn + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
	if($CurrentAccount != 0 AND $departmentid==4) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualaviationsouth = actualaviationsouth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdaviationsouth = bfwdaviationsouth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
	if($CurrentAccount != 0 AND $departmentid==5) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualaviationwest = actualaviationwest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdaviationwest = bfwdaviationwest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==6) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualfiresuva = actualfiresuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdfiresuva = bfwdfiresuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==7) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualfirewest = actualfirewest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									bfwdfirewest = bfwdfirewest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		if($CurrentAccount != 0 AND $departmentid==8) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									actualfirenorth = actualfirenorth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdfirenorth = bfwdfirenorth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==9) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualsecurityinstallssuva = actualsecurityinstallssuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdsecurityinstallssuva = bfwdsecurityinstallssuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==10) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualsecurityinstallswest = actualsecurityinstallswest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdsecurityinstallswest = bfwdsecurityinstallswest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==11) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualmonitoringsuva = actualmonitoringsuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdmonitoringsuva = bfwdmonitoringsuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		if($CurrentAccount != 0 AND $departmentid==12) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualgm = actualgm + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									bfwdgm = bfwdgm + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		if($CurrentAccount != 0 AND $departmentid==13) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									actualfinancesuva = actualfinancesuva+ " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdfinancesuva = bfwdfinancesuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		if($CurrentAccount != 0 AND $departmentid==14) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualfinancewest = actualfinancewest+ " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . "
									 bfwdfinancewest = bfwdfinancewest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
	
		if($CurrentAccount != 0 AND $departmentid==15) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualpropertiessuva = actualpropertiessuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . "
									 bfwdpropertiessuva = bfwdpropertiessuva + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		if($CurrentAccount != 0 AND $departmentid==16) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualpropertieswest = actualpropertieswest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									bfwdpropertieswest = bfwdpropertieswest + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}	if($CurrentAccount != 0 AND $departmentid==17) {
		$sql = "UPDATE chartdetails SET actual = actual + " . $TotalAmount . ",
									 actualpropertiesnorth = actualpropertiesnorth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period= '" . $CurrPeriod . "'";
		$PostPrd = DB_query($sql);
		/*Update the BFwd for all following ChartDetail records */  
		$sql = "UPDATE chartdetails SET bfwd = bfwd + " . $TotalAmount . ",
									 bfwdpropertiesnorth = bfwdpropertiesnorth + " . $TotalAmount . "
				WHERE accountcode = '" . $CurrentAccount . "'
				AND period > '" . $CurrPeriod . "'";
		$PostBFwds = DB_query($sql);
	}
		
	
	
	$sql = "UPDATE gltrans SET posted = 1 WHERE periodno = '" . $CurrPeriod . "' AND posted=0";
	$Posted = DB_query($sql);
	
	$TransCommit = DB_Txn_Commit();
}

?>