1<?php
2
3
4include('includes/session.php');
5
6$Title = _('Payment Terms Maintenance');
7
8include('includes/header.php');
9
10echo '<p class="page_title_text">
11		<img src="'.$RootPath.'/css/'.$Theme.'/images/money_add.png" title="' . _('Payment Terms') . '" alt="" />' . ' ' . $Title .
12	'</p>';
13
14if (isset($_GET['SelectedTerms'])){
15	$SelectedTerms = $_GET['SelectedTerms'];
16} elseif (isset($_POST['SelectedTerms'])){
17	$SelectedTerms = $_POST['SelectedTerms'];
18}
19
20if (isset($Errors)) {
21	unset($Errors);
22}
23
24$Errors = array();
25
26if (isset($_POST['submit'])) {
27
28	//initialise no input errors assumed initially before we test
29	$InputError = 0;
30
31	/* actions to take once the user has clicked the submit button
32	ie the page has called itself with some user input */
33	$i=1;
34
35	//first off validate inputs are sensible
36
37	if (mb_strlen($_POST['TermsIndicator']) < 1) {
38		$InputError = 1;
39		prnMsg(_('The payment terms name must exist'),'error');
40		$Errors[$i] = 'TermsIndicator';
41		$i++;
42	}
43	if (mb_strlen($_POST['TermsIndicator']) > 2) {
44		$InputError = 1;
45		prnMsg(_('The payment terms name must be two characters or less long'),'error');
46		$Errors[$i] = 'TermsIndicator';
47		$i++;
48	}
49	if (empty($_POST['DayNumber']) OR !is_numeric(filter_number_format($_POST['DayNumber'])) OR filter_number_format($_POST['DayNumber']) <= 0){
50		$InputError = 1;
51		prnMsg( _('The number of days or the day in the following month must be numeric') ,'error');
52		$Errors[$i] = 'DayNumber';
53		$i++;
54	}
55	if (empty($_POST['Terms']) OR mb_strlen($_POST['Terms']) > 40) {
56		$InputError = 1;
57		prnMsg( _('The terms description must be forty characters or less long') ,'error');
58		$Errors[$i] = 'Terms';
59		$i++;
60	}
61	/*
62	if ($_POST['DayNumber'] > 30 AND empty($_POST['DaysOrFoll'])) {
63		$InputError = 1;
64		prnMsg( _('When the check box is not checked to indicate a day in the following month is the due date') . ', ' . _('the due date cannot be a day after the 30th') . '. ' . _('A number between 1 and 30 is expected') ,'error');
65		$Errors[$i] = 'DayNumber';
66		$i++;
67	} */
68	if ($_POST['DayNumber']>360 AND !empty($_POST['DaysOrFoll'])) {
69		$InputError = 1;
70		prnMsg( _('When the check box is checked to indicate that the term expects a number of days after which accounts are due') . ', ' . _('the number entered should be less than 361 days') ,'error');
71		$Errors[$i] = 'DayNumber';
72		$i++;
73	}
74
75	if (isset($SelectedTerms) AND $InputError !=1) {
76
77		/*SelectedTerms could also exist if submit had not been clicked this code would not run in this case cos submit is false of course  see the delete code below*/
78
79		if (isset($_POST['DaysOrFoll']) AND $_POST['DaysOrFoll']=='on') {
80			$sql = "UPDATE paymentterms SET
81							terms='" . $_POST['Terms'] . "',
82							dayinfollowingmonth=0,
83							daysbeforedue='" . filter_number_format($_POST['DayNumber']) . "'
84					WHERE termsindicator = '" . $SelectedTerms . "'";
85		} else {
86			$sql = "UPDATE paymentterms SET
87							terms='" . $_POST['Terms'] . "',
88							dayinfollowingmonth='" . filter_number_format($_POST['DayNumber']) . "',
89							daysbeforedue=0
90						WHERE termsindicator = '" . $SelectedTerms . "'";
91		}
92
93		$msg = _('The payment terms definition record has been updated') . '.';
94	} else if ($InputError !=1) {
95
96	/*Selected terms is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new payment terms form */
97
98		if ($_POST['DaysOrFoll']=='on') {
99			$sql = "INSERT INTO paymentterms (termsindicator,
100								terms,
101								daysbeforedue,
102								dayinfollowingmonth)
103						VALUES (
104							'" . $_POST['TermsIndicator'] . "',
105							'" . $_POST['Terms'] . "',
106							'" . filter_number_format($_POST['DayNumber']) . "',
107							0
108						)";
109		} else {
110			$sql = "INSERT INTO paymentterms (termsindicator,
111								terms,
112								daysbeforedue,
113								dayinfollowingmonth)
114						VALUES (
115							'" . $_POST['TermsIndicator'] . "',
116							'" . $_POST['Terms'] . "',
117							0,
118							'" . filter_number_format($_POST['DayNumber']) . "'
119							)";
120		}
121
122		$msg = _('The payment terms definition record has been added') . '.';
123	}
124	if ($InputError !=1){
125		//run the SQL from either of the above possibilites
126		$result = DB_query($sql);
127		prnMsg($msg,'success');
128		unset($SelectedTerms);
129		unset($_POST['DaysOrFoll']);
130		unset($_POST['TermsIndicator']);
131		unset($_POST['Terms']);
132		unset($_POST['DayNumber']);
133	}
134
135} elseif (isset($_GET['delete'])) {
136//the link to delete a selected record was clicked instead of the submit button
137
138// PREVENT DELETES IF DEPENDENT RECORDS IN DebtorsMaster
139
140	$sql= "SELECT COUNT(*) FROM debtorsmaster WHERE debtorsmaster.paymentterms = '" . $SelectedTerms . "'";
141	$result = DB_query($sql);
142	$myrow = DB_fetch_row($result);
143	if ($myrow[0] > 0) {
144		prnMsg( _('Cannot delete this payment term because customer accounts have been created referring to this term'),'warn');
145		echo '<br /> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('customer accounts that refer to this payment term');
146	} else {
147		$sql= "SELECT COUNT(*) FROM suppliers WHERE suppliers.paymentterms = '" . $SelectedTerms . "'";
148		$result = DB_query($sql);
149		$myrow = DB_fetch_row($result);
150		if ($myrow[0] > 0) {
151			prnMsg( _('Cannot delete this payment term because supplier accounts have been created referring to this term'),'warn');
152			echo '<br /> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('supplier accounts that refer to this payment term');
153		} else {
154			//only delete if used in neither customer or supplier accounts
155
156			$sql="DELETE FROM paymentterms WHERE termsindicator='" . $SelectedTerms . "'";
157			$result = DB_query($sql);
158			prnMsg( _('The payment term definition record has been deleted') . '!','success');
159		}
160	}
161	//end if payment terms used in customer or supplier accounts
162
163}
164
165if (!isset($SelectedTerms)) {
166
167/* It could still be the second time the page has been run and a record has been selected for modification - SelectedTerms will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
168then none of the above are true and the list of payment termss will be displayed with
169links to delete or edit each. These will call the same page again and allow update/input
170or deletion of the records*/
171
172	$sql = "SELECT termsindicator, terms, daysbeforedue, dayinfollowingmonth FROM paymentterms";
173	$result = DB_query($sql);
174
175	echo '<table class="selection">';
176	echo '<tr>
177			<th colspan="6"><h3>' . _('Payment Terms.') . '</h3></th>
178		</tr>';
179	echo '<tr>
180			<th>' . _('Term Code') . '</th>
181			<th>' . _('Description') . '</th>
182			<th>' . _('Following Month On') . '</th>
183			<th>' . _('Due After (Days)') . '</th>
184		</tr>';
185
186	while ($myrow=DB_fetch_array($result)) {
187
188		if ($myrow['dayinfollowingmonth']==0) {
189			$FollMthText = _('N/A');
190		} else {
191			$FollMthText = $myrow['dayinfollowingmonth'] . _('th');
192		}
193
194		if ($myrow['daysbeforedue']==0) {
195			$DueAfterText = _('N/A');
196		} else {
197			$DueAfterText = $myrow['daysbeforedue'] . ' ' . _('days');
198		}
199
200	printf('<tr><td>%s</td>
201	        <td>%s</td>
202			<td>%s</td>
203			<td>%s</td>
204			<td><a href="%s?SelectedTerms=%s">' . _('Edit') . '</a></td>
205			<td><a href="%s?SelectedTerms=%s&amp;delete=1" onclick="return confirm(\'' . _('Are you sure you wish to delete this payment term?') . '\');">' . _('Delete') . '</a></td>
206			</tr>',
207			$myrow['termsindicator'],
208			$myrow['terms'],
209			$FollMthText,
210			$DueAfterText,
211			htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8'),
212			$myrow[0],
213			htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8'),
214			$myrow[0]);
215
216	} //END WHILE LIST LOOP
217	echo '</table><br />';
218} //end of ifs and buts!
219
220if (isset($SelectedTerms)) {
221	echo '<div class="centre">
222			<a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Show all Payment Terms Definitions') . '</a>
223		</div>';
224}
225
226if (!isset($_GET['delete'])) {
227
228	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
229    echo '<div>';
230	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
231
232	if (isset($SelectedTerms)) {
233		//editing an existing payment terms
234
235		$sql = "SELECT termsindicator,
236						terms,
237						daysbeforedue,
238						dayinfollowingmonth
239					FROM paymentterms
240					WHERE termsindicator='" . $SelectedTerms . "'";
241
242		$result = DB_query($sql);
243		$myrow = DB_fetch_array($result);
244
245		$_POST['TermsIndicator'] = $myrow['termsindicator'];
246		$_POST['Terms']  = $myrow['terms'];
247		$DaysBeforeDue  = $myrow['daysbeforedue'];
248		$DayInFollowingMonth  = $myrow['dayinfollowingmonth'];
249
250		echo '<input type="hidden" name="SelectedTerms" value="' . $SelectedTerms . '" />';
251		echo '<input type="hidden" name="TermsIndicator" value="' . $_POST['TermsIndicator'] . '" />';
252		echo '<br />
253			<table class="selection">';
254		echo '<tr>
255				<th colspan="6"><h3>' . _('Update Payment Terms.') . '</h3></th>
256			</tr>';
257		echo '<tr>
258				<td>' . _('Term Code') . ':</td>
259				<td>' . $_POST['TermsIndicator'] . '</td></tr>';
260
261	} else { //end of if $SelectedTerms only do the else when a new record is being entered
262
263		if (!isset($_POST['TermsIndicator'])) $_POST['TermsIndicator']='';
264		if (!isset($DaysBeforeDue)) {
265			$DaysBeforeDue=0;
266		}
267		//if (!isset($DayInFollowingMonth)) $DayInFollowingMonth=0;
268		unset($DayInFollowingMonth); // Rather unset for a new record
269		if (!isset($_POST['Terms'])) {
270			$_POST['Terms']='';
271		}
272
273		echo '<table class="selection">';
274		echo '<tr>
275				<th colspan="6"><h3>' . _('New Payment Terms.') . '</h3></th>
276			</tr>';
277		echo '<tr>
278				<td>' . _('Term Code') . ':</td>
279				<td><input type="text" name="TermsIndicator"' . (in_array('TermsIndicator',$Errors) ? 'class="inputerror"' : '' ) .' autofocus="autofocus" required="required" pattern="[0-9a-ZA-Z_]*" title="' . _('A 2 character code to identify this payment term. Any alpha-numeric characters can be used') . '" value="' . $_POST['TermsIndicator'] . '" size="3" maxlength="2" /></td>
280			</tr>';
281	}
282
283	echo '<tr>
284			<td>' .  _('Terms Description'). ':</td>
285			<td><input type="text"' . (in_array('Terms',$Errors) ? 'class="inputerror"' : '' ) .' name="Terms" ' . (isset($SelectedTerms)? 'autofocus="autofocus"': '') . ' required="required" value="'.$_POST['Terms']. '" title="' . _('A description of the payment terms is required') . '" size="35" maxlength="40" /></td>
286		</tr>
287		<tr>
288			<td>' . _('Due After A Given No. Of Days').':</td>
289			<td><input type="checkbox" name="DaysOrFoll" ';
290    if (isset($DayInFollowingMonth) AND !$DayInFollowingMonth) {
291         echo 'checked';
292    }
293    echo '  /></td>
294		</tr>
295		<tr><td>' . _('Days (Or Day In Following Month)').':</td>
296			<td><input type="text" ' . (in_array('DayNumber',$Errors) ? 'class="inputerror"' : '' ) .' name="DayNumber" required="required" class="integer"  size="4" maxlength="3" value="';
297	if ($DaysBeforeDue !=0) {
298		echo locale_number_format($DaysBeforeDue,0);
299	} else {
300		if (isset($DayInFollowingMonth)) {
301			echo locale_number_format($DayInFollowingMonth,0);
302		}
303	}
304	echo '" /></td>
305		</tr>
306		</table>
307		<br />
308		<div class="centre">
309			<input type="submit" name="submit" value="'._('Enter Information').'" />
310		</div>';
311    echo '</div>
312          </form>';
313} //end if record deleted no point displaying form to add record
314
315include('includes/footer.php');
316?>
317