1<?php
2
3include('includes/session.php');
4$Title = _('Tax Authorities');
5$ViewTopic = 'Tax';// Filename in ManualContents.php's TOC.
6$BookMark = 'TaxAuthorities';// Anchor's id in the manual's html document.
7include('includes/header.php');
8echo '<p class="page_title_text"><img alt="" src="' . $RootPath . '/css/' . $Theme .
9		'/images/maintenance.png" title="' .
10		_('Tax Authorities Maintenance') . '" />' . ' ' .
11		_('Tax Authorities Maintenance') . '</p>';
12
13if(isset($_POST['SelectedTaxAuthID'])) {
14	$SelectedTaxAuthID =$_POST['SelectedTaxAuthID'];
15} elseif(isset($_GET['SelectedTaxAuthID'])) {
16	$SelectedTaxAuthID =$_GET['SelectedTaxAuthID'];
17}
18
19if(isset($_POST['submit'])) {
20
21	/* actions to take once the user has clicked the submit button
22	ie the page has called itself with some user input */
23	if( trim( $_POST['Description'] ) == '' ) {
24		$InputError = 1;
25		prnMsg( _('The tax type description may not be empty'), 'error');
26	}
27
28	if(isset($SelectedTaxAuthID)) {
29
30		/*SelectedTaxAuthID could also exist if submit had not been clicked this code
31		would not run in this case cos submit is false of course  see the
32		delete code below*/
33
34		$sql = "UPDATE taxauthorities
35					SET taxglcode ='" . $_POST['TaxGLCode'] . "',
36					purchtaxglaccount ='" . $_POST['PurchTaxGLCode'] . "',
37					description = '" . $_POST['Description'] . "',
38					bank = '" . $_POST['Bank'] . "',
39					bankacctype = '". $_POST['BankAccType'] . "',
40					bankacc = '". $_POST['BankAcc'] . "',
41					bankswift = '". $_POST['BankSwift'] . "'
42				WHERE taxid = '" . $SelectedTaxAuthID . "'";
43
44		$ErrMsg = _('The update of this tax authority failed because');
45		$result = DB_query($sql,$ErrMsg);
46
47		$msg = _('The tax authority for record has been updated');
48
49	} elseif($InputError !=1) {
50
51	/*Selected tax authority is null cos no item selected on first time round so must be adding a	record must be submitting new entries in the new tax authority form */
52
53		$sql = "INSERT INTO taxauthorities (
54						taxglcode,
55						purchtaxglaccount,
56						description,
57						bank,
58						bankacctype,
59						bankacc,
60						bankswift)
61			VALUES (
62				'" . $_POST['TaxGLCode'] . "',
63				'" . $_POST['PurchTaxGLCode'] . "',
64				'" . $_POST['Description'] . "',
65				'" . $_POST['Bank'] . "',
66				'" . $_POST['BankAccType'] . "',
67				'" . $_POST['BankAcc'] . "',
68				'" . $_POST['BankSwift'] . "'
69				)";
70
71		$Errmsg = _('The addition of this tax authority failed because');
72		$result = DB_query($sql,$ErrMsg);
73
74		$msg = _('The new tax authority record has been added to the database');
75
76		$NewTaxID = DB_Last_Insert_ID('taxauthorities','taxid');
77
78		$sql = "INSERT INTO taxauthrates (
79					taxauthority,
80					dispatchtaxprovince,
81					taxcatid
82					)
83				SELECT
84					'" . $NewTaxID  . "',
85					taxprovinces.taxprovinceid,
86					taxcategories.taxcatid
87				FROM taxprovinces,
88					taxcategories";
89
90			$InsertResult = DB_query($sql);
91	}
92	//run the SQL from either of the above possibilites
93	if(isset($InputError) and $InputError !=1) {
94		unset( $_POST['TaxGLCode']);
95		unset( $_POST['PurchTaxGLCode']);
96		unset( $_POST['Description']);
97		unset( $SelectedTaxID );
98	}
99
100	prnMsg($msg);
101
102} elseif(isset($_GET['delete'])) {
103//the link to delete a selected record was clicked instead of the submit button
104
105// PREVENT DELETES IF DEPENDENT RECORDS IN OTHER TABLES
106
107	$sql= "SELECT COUNT(*)
108			FROM taxgrouptaxes
109		WHERE taxauthid='" . $SelectedTaxAuthID . "'";
110
111	$result = DB_query($sql);
112	$myrow = DB_fetch_row($result);
113	if($myrow[0]>0) {
114		prnmsg(_('Cannot delete this tax authority because there are tax groups defined that use it'),'warn');
115	} else {
116		/*Cascade deletes in TaxAuthLevels */
117		$result = DB_query("DELETE FROM taxauthrates WHERE taxauthority= '" . $SelectedTaxAuthID . "'");
118		$result = DB_query("DELETE FROM taxauthorities WHERE taxid= '" . $SelectedTaxAuthID . "'");
119		prnMsg(_('The selected tax authority record has been deleted'),'success');
120		unset ($SelectedTaxAuthID);
121	} // end of related records testing
122}
123
124if(!isset($SelectedTaxAuthID)) {
125
126/* It could still be the second time the page has been run and a record has been selected for modification - SelectedTaxAuthID will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters then none of the above are true and the list of tax authorities will be displayed with links to delete or edit each. These will call the same page again and allow update/input or deletion of the records*/
127
128	$sql = "SELECT taxid,
129				description,
130				taxglcode,
131				purchtaxglaccount,
132				bank,
133				bankacc,
134				bankacctype,
135				bankswift
136			FROM taxauthorities";
137
138	$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('The defined tax authorities could not be retrieved because');
139	$DbgMsg = _('The following SQL to retrieve the tax authorities was used');
140	$result = DB_query($sql,$ErrMsg,$DbgMsg);
141
142	echo '<table class="selection">
143		<thead>
144			<tr>
145				<th class="ascending" >' . _('ID') . '</th>
146				<th class="ascending" >' . _('Tax Authority') . '</th>
147				<th class="ascending" >' . _('Input Tax') . '<br />' . _('GL Account') . '</th>
148				<th class="ascending" >' . _('Output Tax') . '<br />' . _('GL Account') . '</th>
149				<th class="ascending" >' . _('Bank') . '</th>
150				<th class="ascending" >' . _('Bank Account') . '</th>
151				<th class="ascending" >' . _('Bank Act Type') . '</th>
152				<th class="ascending" >' . _('Bank Swift') . '</th>
153				<th colspan="4">&nbsp;</th>
154			</tr>
155		</thead>
156		<tbody>';
157
158	while($myrow = DB_fetch_row($result)) {
159		printf('<tr class="striped_row">
160				<td class="number">%s</td>
161				<td>%s</td>
162				<td class="number">%s</td>
163				<td class="number">%s</td>
164				<td>%s</td>
165				<td>%s</td>
166				<td>%s</td>
167				<td>%s</td>
168				<td><a href="%sSelectedTaxAuthID=%s">' . _('Edit') . '</a></td>
169				<td><a href="%sSelectedTaxAuthID=%s&amp;delete=yes" onclick="return confirm(\'' . _('Are you sure you wish to delete this tax authority?') . '\');">' . _('Delete') . '</a></td>
170				<td><a href="%sTaxAuthority=%s">' . _('Edit Rates') . '</a></td>
171				</tr>',
172				$myrow[0],
173				$myrow[1],
174				$myrow[3],
175				$myrow[2],
176				$myrow[4],
177				$myrow[5],
178				$myrow[6],
179				$myrow[7],
180				htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
181				$myrow[0],
182				htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
183				$myrow[0],
184				$RootPath . '/TaxAuthorityRates.php?',
185				$myrow[0]);
186
187	}
188	//END WHILE LIST LOOP
189
190	//end of ifs and buts!
191
192	echo '</tbody></table><br />';
193}
194
195
196
197if(isset($SelectedTaxAuthID)) {
198	echo '<div class="centre">
199			<a href="' .  htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .'">' . _('Review all defined tax authority records') . '</a>
200		</div>
201		<br />';
202}
203
204
205echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
206echo '<div>';
207echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
208
209if(isset($SelectedTaxAuthID)) {
210	//editing an existing tax authority
211
212	$sql = "SELECT taxglcode,
213				purchtaxglaccount,
214				description,
215				bank,
216				bankacc,
217				bankacctype,
218				bankswift
219			FROM taxauthorities
220			WHERE taxid='" . $SelectedTaxAuthID . "'";
221
222	$result = DB_query($sql);
223	$myrow = DB_fetch_array($result);
224
225	$_POST['TaxGLCode']	= $myrow['taxglcode'];
226	$_POST['PurchTaxGLCode']= $myrow['purchtaxglaccount'];
227	$_POST['Description']	= $myrow['description'];
228	$_POST['Bank']		= $myrow['bank'];
229	$_POST['BankAccType']	= $myrow['bankacctype'];
230	$_POST['BankAcc'] 	= $myrow['bankacc'];
231	$_POST['BankSwift']	= $myrow['bankswift'];
232
233
234	echo '<input type="hidden" name="SelectedTaxAuthID" value="' . $SelectedTaxAuthID . '" />';
235
236}  //end of if $SelectedTaxAuthID only do the else when a new record is being entered
237
238
239$SQL = "SELECT accountcode,
240				accountname
241		FROM chartmaster INNER JOIN accountgroups
242		ON chartmaster.group_=accountgroups.groupname
243		WHERE accountgroups.pandl=0
244		ORDER BY accountcode";
245$result = DB_query($SQL);
246
247if(!isset($_POST['Description'])) {
248	$_POST['Description']='';
249}
250echo '<table class="selection">
251		<tr>
252			<td>' . _('Tax Type Description') . ':</td>
253			<td><input type="text" pattern="(?!^ +$)[^><+-]+" title="'._('No illegal characters allowed and should not be blank').'" placeholder="'._('Within 20 characters').'" required="required" name="Description" size="21" maxlength="20" value="' . $_POST['Description'] . '" /></td>
254		</tr>
255		<tr>
256			<td>' . _('Input tax GL Account') . ':</td>
257			<td><select name="PurchTaxGLCode">';
258
259while($myrow = DB_fetch_array($result)) {
260	if(isset($_POST['PurchTaxGLCode']) and $myrow['accountcode']==$_POST['PurchTaxGLCode']) {
261		echo '<option selected="selected" value="';
262	} else {
263		echo '<option value="';
264	}
265	echo $myrow['accountcode'] . '">' . htmlspecialchars($myrow['accountname'], ENT_QUOTES, 'UTF-8', false) . ' ('.$myrow['accountcode'].')' . '</option>';
266
267} //end while loop
268
269echo '</select></td>
270	</tr>';
271
272DB_data_seek($result,0);
273
274echo '<tr>
275		<td>' . _('Output tax GL Account') . ':</td>
276		<td><select name="TaxGLCode">';
277
278while($myrow = DB_fetch_array($result)) {
279	if(isset($_POST['TaxGLCode']) and $myrow['accountcode']==$_POST['TaxGLCode']) {
280		echo '<option selected="selected" value="';
281	} else {
282		echo '<option value="';
283	}
284	echo $myrow['accountcode'] . '">' . htmlspecialchars($myrow['accountname'], ENT_QUOTES, 'UTF-8', false) . ' ('.$myrow['accountcode'].')' . '</option>';
285
286} //end while loop
287
288if(!isset($_POST['Bank'])) {
289	$_POST['Bank']='';
290}
291if(!isset($_POST['BankAccType'])) {
292	$_POST['BankAccType']='';
293}
294if(!isset($_POST['BankAcc'])) {
295	$_POST['BankAcc']='';
296}
297if(!isset($_POST['BankSwift'])) {
298	$_POST['BankSwift']='';
299}
300
301echo '</select></td>
302	</tr>
303	<tr>
304		<td>' . _('Bank Name') . ':</td>
305		<td><input type="text" name="Bank" size="41" maxlength="40" value="' . $_POST['Bank'] . '" placeholder="'._('Not more than 40 chacraters').'" /></td>
306	</tr>
307	<tr>
308		<td>' . _('Bank Account Type') . ':</td>
309		<td><input type="text" name="BankAccType" size="15" maxlength="20" value="' . $_POST['BankAccType'] . '" placeholder="'._('No more than 20 characters').'" /></td>
310	</tr>
311	<tr>
312		<td>' . _('Bank Account') . ':</td>
313		<td><input type="text" name="BankAcc" size="21" maxlength="20" value="' . $_POST['BankAcc'] . '" placeholder="'._('No more than 20 characters').'" /></td>
314	</tr>
315	<tr>
316		<td>' . _('Bank Swift No') . ':</td>
317		<td><input type="text" name="BankSwift" size="15" maxlength="14" value="' . $_POST['BankSwift'] . '" placeholder="'._('No more than 15 characters').'" /></td>
318	</tr>
319	</table>';
320
321echo '<br />
322		<div class="centre">
323			<input type="submit" name="submit" value="' . _('Enter Information') . '" />
324		</div>
325	</div>
326	</form>';
327
328echo '<br />
329	<div class="centre">
330		<a href="' . $RootPath . '/TaxGroups.php">' . _('Tax Group Maintenance') .  '</a><br />
331		<a href="' . $RootPath . '/TaxProvinces.php">' . _('Dispatch Tax Province Maintenance') .  '</a><br />
332		<a href="' . $RootPath . '/TaxCategories.php">' . _('Tax Category Maintenance') .  '</a>
333	</div>';
334
335include('includes/footer.php');
336?>
337