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