1<?php
2
3include('includes/session.php');
4$Title = _('Maintenance Of Petty Cash Tabs');
5/* webERP manual links before header.php */
6$ViewTopic = 'PettyCash';
7$BookMark = 'PCTabSetup';
8include('includes/header.php');
9echo '<p class="page_title_text">
10		<img src="', $RootPath, '/css/', $_SESSION['Theme'], '/images/money_add.png" title="', _('Payment Entry'), '" alt="" />', ' ', $Title, '
11	</p>';
12if (isset($_POST['SelectedTab'])) {
13	$SelectedTab = mb_strtoupper($_POST['SelectedTab']);
14} elseif (isset($_GET['SelectedTab'])) {
15	$SelectedTab = mb_strtoupper($_GET['SelectedTab']);
16}
17if (isset($_POST['Cancel'])) {
18	unset($SelectedTab);
19	unset($_POST['TabCode']);
20	unset($_POST['SelectUser']);
21	unset($_POST['SelectTabs']);
22	unset($_POST['SelectCurrency']);
23	unset($_POST['TabLimit']);
24	unset($_POST['SelectAssigner']);
25	unset($_POST['SelectAuthoriserCash']);
26	unset($_POST['SelectAuthoriserExpenses']);
27	unset($_POST['GLAccountCash']);
28	unset($_POST['GLAccountPcashTab']);
29}
30if (isset($_POST['Submit'])) {
31	//initialise no input errors assumed initially before we test
32	$InputError = 0;
33	/* actions to take once the user has clicked the submit button
34	ie the page has called itself with some user input */
35	//first off validate inputs sensible
36	if ($_POST['TabCode'] == '' or $_POST['TabCode'] == ' ' or $_POST['TabCode'] == '  ') {
37		$InputError = 1;
38		prnMsg('<br />' . _('The Tab code cannot be an empty string or spaces'), 'error');
39	} elseif (mb_strlen($_POST['TabCode']) > 20) {
40		$InputError = 1;
41		echo prnMsg(_('The Tab code must be twenty characters or less long'), 'error');
42	} elseif (($_POST['SelectUser']) == '') {
43		$InputError = 1;
44		echo prnMsg(_('You must select a User for this tab'), 'error');
45	} elseif (($_POST['SelectTabs']) == '') {
46		$InputError = 1;
47		echo prnMsg(_('You must select a type of tab from the list'), 'error');
48	} elseif (($_POST['SelectAssigner']) == '') {
49		$InputError = 1;
50		echo prnMsg(_('You must select a User to assign cash to this tab'), 'error');
51	} elseif (($_POST['SelectAuthoriserCash']) == '') {
52		$InputError = 1;
53		echo prnMsg(_('You must select a User to authorise this tab'), 'error');
54	} elseif (($_POST['GLAccountCash']) == '') {
55		$InputError = 1;
56		echo prnMsg(_('You must select a General ledger code for the cash to be assigned from'), 'error');
57	} elseif (($_POST['GLAccountPcashTab']) == '') {
58		$InputError = 1;
59		echo prnMsg(_('You must select a General ledger code for this petty cash tab'), 'error');
60	} elseif (($_POST['TaxGroup']) === '0') {
61		$InputError = 1;
62		echo prnMsg(_('You must select a tax group'), 'error');
63	}
64	if (isset($SelectedTab) and $InputError != 1) {
65		$SQL = "UPDATE pctabs SET usercode = '" . $_POST['SelectUser'] . "',
66									typetabcode = '" . $_POST['SelectTabs'] . "',
67									currency = '" . $_POST['SelectCurrency'] . "',
68									tablimit = '" . filter_number_format($_POST['TabLimit']) . "',
69									assigner = '" . $_POST['SelectAssigner'] . "',
70									authorizer = '" . $_POST['SelectAuthoriserCash'] . "',
71									authorizerexpenses = '" . $_POST['SelectAuthoriserExpenses'] . "',
72									glaccountassignment = '" . $_POST['GLAccountCash'] . "',
73									glaccountpcash = '" . $_POST['GLAccountPcashTab'] . "',
74									defaulttag = '" . $_POST['DefaultTag'] . "',
75									taxgroupid='" . $_POST['TaxGroup'] . "'
76				WHERE tabcode = '" . $SelectedTab . "'";
77		$Msg = _('The Petty Cash Tab') . ' ' . $SelectedTab . ' ' . _('has been updated');
78	} elseif ($InputError != 1) {
79		// First check the type is not being duplicated
80		$CheckSQL = "SELECT count(*)
81					 FROM pctabs
82					 WHERE tabcode = '" . $_POST['TabCode'] . "'";
83		$CheckResult = DB_query($CheckSQL);
84		$CheckRow = DB_fetch_row($CheckResult);
85		if ($CheckRow[0] > 0) {
86			$InputError = 1;
87			prnMsg(_('The Tab ') . ' ' . $_POST['TabCode'] . ' ' . _(' already exists'), 'error');
88		} else {
89			// Add new record on submit
90			$SQL = "INSERT INTO pctabs	(tabcode,
91							 			 usercode,
92										 typetabcode,
93										 currency,
94										 tablimit,
95										 assigner,
96										 authorizer,
97										 authorizerexpenses,
98										 glaccountassignment,
99										 glaccountpcash,
100										 defaulttag,
101										 taxgroupid)
102								VALUES ('" . $_POST['TabCode'] . "',
103									'" . $_POST['SelectUser'] . "',
104									'" . $_POST['SelectTabs'] . "',
105									'" . $_POST['SelectCurrency'] . "',
106									'" . filter_number_format($_POST['TabLimit']) . "',
107									'" . $_POST['SelectAssigner'] . "',
108									'" . $_POST['SelectAuthoriserCash'] . "',
109									'" . $_POST['SelectAuthoriserExpenses'] . "',
110									'" . $_POST['GLAccountCash'] . "',
111									'" . $_POST['GLAccountPcashTab'] . "',
112									'" . $_POST['DefaultTag'] . "',
113									'" . $_POST['TaxGroup'] . "'
114								)";
115			$Msg = _('The Petty Cash Tab') . ' ' . $_POST['TabCode'] . ' ' . _('has been created');
116		}
117	}
118	if ($InputError != 1) {
119		//run the SQL from either of the above possibilites
120		$Result = DB_query($SQL);
121		prnMsg($Msg, 'success');
122		unset($SelectedTab);
123		unset($_POST['SelectUser']);
124		unset($_POST['TabCode']);
125		unset($_POST['SelectTabs']);
126		unset($_POST['SelectCurrency']);
127		unset($_POST['TabLimit']);
128		unset($_POST['SelectAssigner']);
129		unset($_POST['SelectAuthoriserCash']);
130		unset($_POST['GLAccountCash']);
131		unset($_POST['GLAccountPcashTab']);
132		unset($_POST['TaxGroup']);
133	}
134} elseif (isset($_GET['delete'])) {
135	$SQL = "DELETE FROM pctabs WHERE tabcode='" . $SelectedTab . "'";
136	$ErrMsg = _('The Tab record could not be deleted because');
137	$Result = DB_query($SQL, $ErrMsg);
138	prnMsg(_('The Petty Cash Tab') . ' ' . $SelectedTab . ' ' . _('has been deleted'), 'success');
139	unset($SelectedTab);
140	unset($_GET['delete']);
141}
142if (!isset($SelectedTab)) {
143	/* It could still be the second time the page has been run and a record has been selected for modification - SelectedTab will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
144	then none of the above are true and the list of sales types will be displayed with
145	links to delete or edit each. These will call the same page again and allow update/input
146	or deletion of the records*/
147	$SQL = "SELECT tabcode,
148					usercode,
149					typetabdescription,
150					currabrev,
151					tablimit,
152					assigner,
153					authorizer,
154					authorizerexpenses,
155					glaccountassignment,
156					glaccountpcash,
157					defaulttag,
158					currencies.decimalplaces,
159					chartmaster1.accountname AS glactassigntname,
160					chartmaster2.accountname AS glactpcashname,
161					taxgroupdescription
162				FROM pctabs
163				INNER JOIN currencies
164					ON pctabs.currency=currencies.currabrev
165				INNER JOIN pctypetabs
166					ON pctabs.typetabcode=pctypetabs.typetabcode
167				INNER JOIN chartmaster AS chartmaster1 ON
168					pctabs.glaccountassignment = chartmaster1.accountcode
169				INNER JOIN chartmaster AS chartmaster2 ON
170					pctabs.glaccountpcash = chartmaster2.accountcode
171				INNER JOIN taxgroups
172					ON pctabs.taxgroupid=taxgroups.taxgroupid
173				ORDER BY tabcode";
174	$Result = DB_query($SQL);
175	if (DB_num_rows($Result) > 0) {
176		echo '<table class="selection">
177				<tr>
178					<th>', _('Tab Code'), '</th>
179					<th>', _('User Name'), '</th>
180					<th>', _('Type Of Tab'), '</th>
181					<th>', _('Currency'), '</th>
182					<th>', _('Limit'), '</th>
183					<th>', _('Cash Assigner'), '</th>
184					<th>', _('Authoriser - Cash'), '</th>
185					<th>', _('Authoriser - Expenses'), '</th>
186					<th>', _('GL Account For Cash Assignment'), '</th>
187					<th>', _('GL Account Petty Cash Tab'), '</th>
188					<th>', _('Default Tag'), '</th>
189					<th>', _('Tax Group'), '</th>
190				</tr>';
191
192		while ($MyRow = DB_fetch_array($Result)) {
193			$TagSQL = "SELECT tagdescription FROM tags WHERE tagref='" . $MyRow['defaulttag'] . "'";
194			$TagResult = DB_query($TagSQL);
195			$TagRow = DB_fetch_array($TagResult);
196			echo '<tr class="striped_row">
197					<td>', $MyRow['tabcode'], '</td>
198					<td>', $MyRow['usercode'], '</td>
199					<td>', $MyRow['typetabdescription'], '</td>
200					<td>', $MyRow['currabrev'], '</td>
201					<td class="number">', locale_number_format($MyRow['tablimit'], $MyRow['decimalplaces']), '</td>
202					<td>', $MyRow['assigner'], '</td>
203					<td>', $MyRow['authorizer'], '</td>
204					<td>', $MyRow['authorizerexpenses'], '</td>
205					<td>', $MyRow['glaccountassignment'] . ' - ' . $MyRow['glactassigntname'], '</td>
206					<td>', $MyRow['glaccountpcash'] . ' - ' . $MyRow['glactpcashname'], '</td>
207					<td>', $TagRow['tagdescription'], '</td>
208					<td>', $MyRow['taxgroupdescription'], '</td>
209					<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '?SelectedTab=', $MyRow['tabcode'], '">' . _('Edit') . '</a></td>
210					<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '?SelectedTab=', $MyRow['tabcode'], '&amp;delete=yes" onclick=\' return confirm("' . _('Are you sure you wish to delete this tab code?') . '", \'Confirm Delete\', this);\'>' . _('Delete') . '</a></td>
211				</tr>';
212		}
213		//END WHILE LIST LOOP
214		echo '</table>';
215	} //if there are tabs to show
216}
217//end of ifs and buts!
218if (isset($SelectedTab)) {
219	echo '<div class="centre">
220			<a href="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '">', _('Show All Tabs Defined'), '</a>
221		</div>';
222}
223if (!isset($_GET['delete'])) {
224	echo '<form method="post" action="', htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'), '">';
225	echo '<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
226	if (isset($SelectedTab) and $SelectedTab != '') {
227		$SQL = "SELECT tabcode,
228						usercode,
229						typetabcode,
230						currency,
231						tablimit,
232						assigner,
233						authorizer,
234						authorizerexpenses,
235						glaccountassignment,
236						glaccountpcash,
237						defaulttag,
238						taxgroupid
239					FROM pctabs
240				WHERE tabcode='" . $SelectedTab . "'";
241		$Result = DB_query($SQL);
242		$MyRow = DB_fetch_array($Result);
243		$_POST['TabCode'] = $MyRow['tabcode'];
244		$_POST['SelectUser'] = $MyRow['usercode'];
245		$_POST['SelectTabs'] = $MyRow['typetabcode'];
246		$_POST['SelectCurrency'] = $MyRow['currency'];
247		$_POST['TabLimit'] = locale_number_format($MyRow['tablimit']);
248		$_POST['SelectAssigner'] = $MyRow['assigner'];
249		$_POST['SelectAuthoriserCash'] = $MyRow['authorizer'];
250		$_POST['SelectAuthoriserExpenses'] = $MyRow['authorizerexpenses'];
251		$_POST['GLAccountCash'] = $MyRow['glaccountassignment'];
252		$_POST['GLAccountPcashTab'] = $MyRow['glaccountpcash'];
253		$_POST['DefaultTag'] = $MyRow['defaulttag'];
254		$_POST['TaxGroup'] = $MyRow['taxgroupid'];
255		echo '<input type="hidden" name="SelectedTab" value="', $SelectedTab, '" />';
256		echo '<input type="hidden" name="TabCode" value="', $_POST['TabCode'], '" />';
257		echo '<table class="selection">
258				<tr>
259					<td>', _('Tab Code'), ':</td>
260					<td>', $_POST['TabCode'], '</td>
261				</tr>';
262	} else {
263		// This is a new type so the user may volunteer a type code
264		echo '<table class="selection">
265				<tr>
266					<td>', _('Tab Code'), ':</td>
267					<td><input type="text" required="required" maxlength="20" name="TabCode" /></td>
268				</tr>';
269	}
270	if (!isset($_POST['typetabdescription'])) {
271		$_POST['typetabdescription'] = '';
272	}
273	echo '<tr>
274			<td>', _('User Name'), ':</td>
275			<td><select required="required" name="SelectUser">';
276	$SQL = "SELECT userid,
277					realname
278			FROM www_users ORDER BY userid";
279	$Result = DB_query($SQL);
280	while ($MyRow = DB_fetch_array($Result)) {
281		if (isset($_POST['SelectUser']) and $MyRow['userid'] == $_POST['SelectUser']) {
282			echo '<option selected="selected" value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
283		} else {
284			echo '<option value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
285		}
286	} //end while loop get user
287	echo '</select>
288			</td>
289		</tr>';
290	echo '<tr>
291			<td>', _('Type Of Tab'), ':</td>
292			<td><select required="required" name="SelectTabs">';
293	$SQL = "SELECT typetabcode,
294					typetabdescription
295			FROM pctypetabs
296			ORDER BY typetabcode";
297	$Result = DB_query($SQL);
298	while ($MyRow = DB_fetch_array($Result)) {
299		if (isset($_POST['SelectTabs']) and $MyRow['typetabcode'] == $_POST['SelectTabs']) {
300			echo '<option selected="selected" value="', $MyRow['typetabcode'], '">', $MyRow['typetabcode'], ' - ', $MyRow['typetabdescription'], '</option>';
301		} else {
302			echo '<option value="', $MyRow['typetabcode'], '">', $MyRow['typetabcode'], ' - ', $MyRow['typetabdescription'], '</option>';
303		}
304	} //end while loop get type of tab
305	echo '</select>
306			</td>
307		</tr>';
308	echo '<tr>
309			<td>', _('Currency'), ':</td>
310			<td><select required="required" name="SelectCurrency">';
311	$SQL = "SELECT currency, currabrev FROM currencies";
312	$Result = DB_query($SQL);
313	while ($MyRow = DB_fetch_array($Result)) {
314		if (isset($_POST['SelectCurrency']) and $MyRow['currabrev'] == $_POST['SelectCurrency']) {
315			echo '<option selected="selected" value="', $MyRow['currabrev'], '">', $MyRow['currency'], '</option>';
316		} else {
317			echo '<option value="', $MyRow['currabrev'], '">', $MyRow['currency'], '</option>';
318		}
319	} //end while loop get type of tab
320	echo '</select>
321			</td>
322		</tr>';
323	if (!isset($_POST['TabLimit'])) {
324		$_POST['TabLimit'] = 0;
325	}
326	echo '<tr>
327			<td>', _('Limit Of Tab'), ':</td>
328			<td>
329				<input type="text" class="number" name="TabLimit" size="12" required="required" maxlength="11" value="', $_POST['TabLimit'], '" />
330			</td>
331		</tr>';
332	echo '<tr>
333			<td>', _('Cash Assigner'), ':</td>
334			<td><select required="required" name="SelectAssigner">';
335	$SQL = "SELECT userid,
336					realname
337			FROM www_users
338			ORDER BY userid";
339	$Result = DB_query($SQL);
340	while ($MyRow = DB_fetch_array($Result)) {
341		if (isset($_POST['SelectAssigner']) and $MyRow['userid'] == $_POST['SelectAssigner']) {
342			echo '<option selected="selected" value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
343		} else {
344			echo '<option value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
345		}
346	} //end while loop get assigner
347	echo '</select>
348			</td>
349		</tr>';
350	echo '<tr>
351			<td>', _('Authoriser - Cash'), ':</td>
352			<td><select required="required" name="SelectAuthoriserCash">';
353	$SQL = "SELECT userid,
354					realname
355			FROM www_users
356			ORDER BY userid";
357	$Result = DB_query($SQL);
358	while ($MyRow = DB_fetch_array($Result)) {
359		if (isset($_POST['SelectAuthoriserCash']) and $MyRow['userid'] == $_POST['SelectAuthoriserCash']) {
360			echo '<option selected="selected" value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
361		} else {
362			echo '<option value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
363		}
364	} //end while loop get authoriser
365	echo '</select>
366			</td>
367		</tr>';
368	echo '<tr>
369			<td>', _('Authoriser - Expenses'), ':</td>
370			<td><select required="required" name="SelectAuthoriserExpenses">';
371	$SQL = "SELECT userid,
372					realname
373			FROM www_users
374			ORDER BY userid";
375	$Result = DB_query($SQL);
376	while ($MyRow = DB_fetch_array($Result)) {
377		if (isset($_POST['SelectAuthoriserExpenses']) and $MyRow['userid'] == $_POST['SelectAuthoriserExpenses']) {
378			echo '<option selected="selected" value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
379		} else {
380			echo '<option value="', $MyRow['userid'], '">', $MyRow['userid'], ' - ', $MyRow['realname'], '</option>';
381		}
382	} //end while loop get authoriser
383	echo '</select>
384			</td>
385		</tr>';
386	echo '<tr>
387			<td>', _('GL Account Cash Assignment'), ':</td>
388			<td><select required="required" name="GLAccountCash">';
389	$SQL = "SELECT chartmaster.accountcode,
390					chartmaster.accountname
391			FROM chartmaster
392			INNER JOIN bankaccounts
393				ON chartmaster.accountcode = bankaccounts.accountcode
394			ORDER BY chartmaster.accountcode";
395	$Result = DB_query($SQL);
396	while ($MyRow = DB_fetch_array($Result)) {
397		if (isset($_POST['GLAccountCash']) and $MyRow['accountcode'] == $_POST['GLAccountCash']) {
398			echo '<option selected="selected" value="', $MyRow['accountcode'], '">', $MyRow['accountcode'], ' - ', htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false), '</option>';
399		} else {
400			echo '<option value="', $MyRow['accountcode'], '">', $MyRow['accountcode'], ' - ', htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false), '</option>';
401		}
402	} //end while loop
403	echo '</select>
404			</td>
405		</tr>';
406	echo '<tr>
407			<td>', _('GL Account Petty Cash Tab'), ':</td>
408			<td><select required="required" name="GLAccountPcashTab">';
409	$SQL = "SELECT accountcode,
410					accountname
411				FROM chartmaster
412				ORDER BY accountcode";
413	$Result = DB_query($SQL);
414	while ($MyRow = DB_fetch_array($Result)) {
415		if (isset($_POST['GLAccountPcashTab']) and $MyRow['accountcode'] == $_POST['GLAccountPcashTab']) {
416			echo '<option selected="selected" value="', $MyRow['accountcode'], '">', $MyRow['accountcode'], ' - ', htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false), '</option>';
417		} else {
418			echo '<option value="', $MyRow['accountcode'], '">', $MyRow['accountcode'], ' - ', htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false), '</option>';
419		}
420	} //end while loop
421	echo '</select>
422			</td>
423		</tr>';
424	//Select the tag
425	$SQL = "SELECT tagref,
426					tagdescription
427			FROM tags
428			ORDER BY tagref";
429	$Result = DB_query($SQL);
430	echo '<tr>
431			<td>', _('Default Tag'), ':</td>
432			<td><select name="DefaultTag">';
433	echo '<option value="0">0 - ', _('None'), '</option>';
434	while ($MyRow = DB_fetch_array($Result)) {
435		if (isset($_POST['DefaultTag']) and $_POST['DefaultTag'] == $MyRow['tagref']) {
436			echo '<option selected="selected" value="', $MyRow['tagref'], '">', $MyRow['tagref'], ' - ', $MyRow['tagdescription'], '</option>';
437		} else {
438			echo '<option value="', $MyRow['tagref'], '">', $MyRow['tagref'], ' - ', $MyRow['tagdescription'], '</option>';
439		}
440	}
441	echo '</select>
442			</td>
443		</tr>';
444	// End select tag
445	$SQL = "SELECT taxgroupid,
446					taxgroupdescription
447			FROM taxgroups
448			ORDER BY taxgroupdescription";
449	$Result = DB_query($SQL);
450	echo '<tr>
451			<td>', _('Tax Group'), ':</td>
452			<td><select name="TaxGroup">';
453	echo '<option value="0">0 - ', _('None'), '</option>';
454	while ($MyRow = DB_fetch_array($Result)) {
455		if (isset($_POST['TaxGroup']) and $_POST['TaxGroup'] == $MyRow['taxgroupid']) {
456			echo '<option selected="selected" value="', $MyRow['taxgroupid'], '">', $MyRow['taxgroupid'], ' - ', $MyRow['taxgroupdescription'], '</option>';
457		} else {
458			echo '<option value="', $MyRow['taxgroupid'], '">', $MyRow['taxgroupid'], ' - ', $MyRow['taxgroupdescription'], '</option>';
459		}
460	}
461	echo '</select>
462			</td>
463		</tr>';
464	// End select tag
465	echo '</table>'; // close main table
466	echo '<div class="centre">
467			<input type="submit" name="Submit" value="', _('Accept'), '" />
468			<input type="submit" name="Cancel" value="', _('Cancel'), '" />
469		</div>';
470	echo '</form>';
471} // end if user wish to delete
472include('includes/footer.php');
473?>