1<?php
2
3
4include ('includes/session.php');
5$Title = _('Recalculation of Brought Forward Balances in Chart Details Table');
6include('includes/header.php');
7
8echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
9echo '<div>';
10echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
11
12if ($_POST['FromPeriod'] > $_POST['ToPeriod']){
13	prnMsg(_('The selected period from is actually after the period to') . '. ' . _('Please re-select the reporting period'),'error');
14	unset ($_POST['FromPeriod']);
15	unset ($_POST['ToPeriod']);
16
17}
18
19if (!isset($_POST['FromPeriod']) OR !isset($_POST['ToPeriod'])){
20
21
22/*Show a form to allow input of criteria for TB to show */
23	echo '<table><tr><td>' . _('Select Period From') . ':</td><td><select name="FromPeriod">';
24
25	$sql = "SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno";
26	$Periods = DB_query($sql);
27
28
29	while ($myrow=DB_fetch_array($Periods)){
30		echo '<option value="' . $myrow['periodno'] . '">' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']) . '</option>';
31	}
32
33	echo '</select></td></tr>';
34
35	$sql = "SELECT MAX(periodno) FROM periods";
36	$MaxPrd = DB_query($sql);
37	$MaxPrdrow = DB_fetch_row($MaxPrd);
38
39	$DefaultToPeriod = (int) ($MaxPrdrow[0]-1);
40
41	echo '<tr><td>' . _('Select Period To') . ':</td><td><select name="ToPeriod">';
42
43	$RetResult = DB_data_seek($Periods,0);
44
45	while ($myrow=DB_fetch_array($Periods)){
46
47		if($myrow['periodno']==$DefaultToPeriod){
48			echo '<option selected="selected" value="' . $myrow['periodno'] . '">' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']) . '</option>';
49		} else {
50			echo '<option value="' . $myrow['periodno'] . '">' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']) . '</option>';
51		}
52	}
53	echo '</select></td></tr></table>';
54
55	echo '<div class="centre"><input type="submit" name="recalc" value="' . _('Do the Recalculation') . '" /></div>
56        </div>
57        </form>';
58
59} else {  /*OK do the updates */
60
61	for ($i=$_POST['FromPeriod'];$i<=$_POST['ToPeriod'];$i++){
62
63		$sql="SELECT accountcode,
64					period,
65					budget,
66					actual,
67					bfwd,
68					bfwdbudget
69				FROM chartdetails
70				WHERE period ='" . $i . "'";
71
72		$ErrMsg = _('Could not retrieve the ChartDetail records because');
73		$result = DB_query($sql,$ErrMsg);
74
75		while ($myrow=DB_fetch_array($result)){
76
77			$CFwd = $myrow['bfwd'] + $myrow['actual'];
78			$CFwdBudget = $myrow['bfwdbudget'] + $myrow['budget'];
79
80			echo '<br />' . _('Account Code') . ': ' . $myrow['accountcode'] . ' ' . _('Period') .': ' . $myrow['period'];
81
82			$sql = "UPDATE chartdetails SET bfwd='" . $CFwd . "',
83										bfwdbudget='" . $CFwdBudget . "'
84					WHERE period='" . ($myrow['period'] +1) . "'
85					AND  accountcode = '" . $myrow['accountcode'] . "'";
86
87			$ErrMsg =_('Could not update the chartdetails record because');
88			$updresult = DB_query($sql,$ErrMsg);
89		}
90	} /* end of for loop */
91}
92
93include('includes/footer.php');
94?>