1<?php
2
3include('includes/session.php');
4$Title=_('Debtors Control Integrity');
5include('includes/header.php');
6
7
8//
9//========[ SHOW OUR FORM ]===========
10//
11
12    // Context Navigation and Title
13    echo '<a href="'. $RootPath . '/index.php?&amp;Application=AR">' . _('Back to Customers') . '</a>';
14    echo '<div class="centre"><h3>' . $Title . '</h3></div>';
15
16	// Page Border
17	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .  '">';
18    echo '<div class="centre">';
19	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
20
21	echo '<table class="selection">';
22
23	$DefaultFromPeriod = ( !isset($_POST['FromPeriod']) OR $_POST['FromPeriod']=='' ) ? 1 : $_POST['FromPeriod'];
24
25	if ( !isset($_POST['ToPeriod']) OR $_POST['ToPeriod']=='' )
26	{
27			$SQL = "SELECT Max(periodno) FROM periods";
28			$prdResult = DB_query($SQL);
29			$MaxPrdrow = DB_fetch_row($prdResult);
30			DB_free_result($prdResult);
31			$DefaultToPeriod = $MaxPrdrow[0];
32	} else {
33			$DefaultToPeriod = $_POST['ToPeriod'];
34	}
35
36	echo '<tr>
37			<td>' . _('Start Period:') . '</td>
38			<td><select name="FromPeriod">';
39
40	$ToSelect = '<tr><td>' . _('End Period:')  . '</td>
41					<td><select name="ToPeriod">';
42
43	$SQL = "SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno";
44	$perResult = DB_query($SQL);
45
46	while ( $perRow=DB_fetch_array($perResult) ) {
47		$FromSelected = ( $perRow['periodno'] == $DefaultFromPeriod ) ? 'selected="selected"' : '';
48		echo '<option ' . $FromSelected . ' value="' . $perRow['periodno'] . '">' .MonthAndYearFromSQLDate($perRow['lastdate_in_period'])  . '</option>';
49
50		$ToSelected = ( $perRow['periodno'] == $DefaultToPeriod ) ? 'selected="selected"' : '';
51		$ToSelect .= '<option ' . $ToSelected . ' value="' . $perRow['periodno'] . '">' . MonthAndYearFromSQLDate($perRow['lastdate_in_period'])  . '</option>';
52	}
53	DB_free_result($perResult);
54	echo '</select></td></tr>';
55
56	echo $ToSelect . '</select></td></tr>';
57
58	echo '</table>';
59
60	echo '<br /><input type="submit" name="Show" value="'._('Accept').'" />';
61	echo '<input type="submit" value="' . _('Cancel') .'" />';
62
63
64	if ( isset($_POST['Show']) )	{
65		//
66		//========[ SHOW SYNOPSYS ]===========
67		//
68		echo '<br /><table border="1">';
69		echo '<tr>
70				<th>' . _('Period') . '</th>
71				<th>' . _('Bal B/F in GL') . '</th>
72				<th>' . _('Invoices') . '</th>
73				<th>' . _('Receipts') . '</th>
74				<th>' . _('Bal C/F in GL') . '</th>
75				<th>' . _('Calculated') . '</th>
76				<th>' . _('Difference') . '</th>
77			</tr>';
78
79		$CurPeriod = $_POST['FromPeriod'];
80		$GLOpening = $invTotal = $RecTotal = $GLClosing = $CalcTotal = $DiffTotal = 0;
81
82		while ( $CurPeriod <= $_POST['ToPeriod'] ) {
83			$SQL = "SELECT bfwd,
84					actual
85				FROM chartdetails
86				WHERE period = " . $CurPeriod . "
87				AND accountcode=" . $_SESSION['CompanyRecord']['debtorsact'];
88			$dtResult = DB_query($SQL);
89			$dtRow = DB_fetch_array($dtResult);
90			DB_free_result($dtResult);
91
92			$GLOpening += $dtRow['bfwd'];
93			$glMovement = $dtRow['bfwd'] + $dtRow['actual'];
94
95			echo '<tr class="striped_row">
96					<td>' . $CurPeriod . '</td>
97					<td class="number">' . locale_number_format($dtRow['bfwd'],2) . '</td>';
98
99			$SQL = "SELECT SUM((ovamount+ovgst+ovfreight+ovdiscount)/rate) AS totinvnetcrds
100					FROM debtortrans
101					WHERE prd = '" . $CurPeriod . "'
102					AND (type=10 OR type=11)";
103			$invResult = DB_query($SQL);
104			$invRow = DB_fetch_array($invResult);
105			DB_free_result($invResult);
106
107			$invTotal += $invRow['totinvnetcrds'];
108
109			echo '<td class="number">' . locale_number_format($invRow['totinvnetcrds'],2) . '</td>';
110
111			$SQL = "SELECT SUM((ovamount+ovgst+ovfreight+ovdiscount)/rate) AS totreceipts
112					FROM debtortrans
113					WHERE prd = '" . $CurPeriod . "'
114					AND type=12";
115			$recResult = DB_query($SQL);
116			$recRow = DB_fetch_array($recResult);
117			DB_free_result($recResult);
118
119			$RecTotal += $recRow['totreceipts'];
120			$CalcMovement = $dtRow['bfwd'] + $invRow['totinvnetcrds'] + $recRow['totreceipts'];
121
122			echo '<td class="number">' . locale_number_format($recRow['totreceipts'],2) . '</td>';
123
124			$GLClosing += $glMovement;
125			$CalcTotal += $CalcMovement;
126			$DiffTotal += $diff;
127
128			$diff = ( $dtRow['bfwd'] == 0 ) ? 0 : round($glMovement,2) - round($CalcMovement,2);
129			$color = ( $diff == 0 OR $dtRow['bfwd'] == 0 ) ? 'green' : 'red';
130
131			echo '<td class="number">' . locale_number_format($glMovement,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
132					<td class="number">' . locale_number_format(($CalcMovement),$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
133					<td class="number" style="background-color:white;color:' . $color . '">' . locale_number_format($diff,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
134			</tr>';
135			$CurPeriod++;
136		}
137
138		$difColor = ( $DiffTotal == 0 ) ? 'green' : 'red';
139
140		echo '<tr style="bgcolor:white">
141				<td>' . _('Total') . '</td>
142				<td class="number">' . locale_number_format($GLOpening,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
143				<td class="number">' . locale_number_format($invTotal,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
144				<td class="number">' . locale_number_format($RecTotal,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
145				<td class="number">' . locale_number_format($GLClosing,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
146				<td class="number">' . locale_number_format($CalcTotal,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
147				<td class="number" style="color=' . $difColor . '">' . locale_number_format($DiffTotal,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
148			</tr>';
149		echo '</table>';
150	}
151    echo '</div>
152          </form>';
153
154include('includes/footer.php');
155
156?>