forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalesPeople.php
433 lines (374 loc) · 16.5 KB
/
SalesPeople.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
include('includes/session.php');
$Title = _('Sales People Maintenance');
$ViewTopic = 'SalesPeople';
$BookMark = 'SalesPeople';
if(isset($_GET['SelectedSalesPerson'])) {
$BookMark = 'SalespeopleEdit';
}// For Edit's screen.
if(isset($_GET['delete'])) {
$BookMark = 'SalespeopleDelete';
}// For Delete's ERROR Message Report.
include('includes/header.php');
if (isset($_GET['SelectedSalesPerson'])){
$SelectedSalesPerson =mb_strtoupper($_GET['SelectedSalesPerson']);
} elseif(isset($_POST['SelectedSalesPerson'])){
$SelectedSalesPerson =mb_strtoupper($_POST['SelectedSalesPerson']);
}
if (isset($Errors)) {
unset($Errors);
}
$Errors = array();
$CommissionPeriods[0] = _('No Commission');
$CommissionPeriods[1] = _('Monthly');
$CommissionPeriods[2] = _('Quarterly');
$CommissionPeriods[3] = _('Annually');
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Search') . '" alt="" />' . ' ' . $Title . '</p>';
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 sensible
if (mb_strlen($_POST['SalesmanCode']) > 3) {
$InputError = 1;
prnMsg(_('The salesperson code must be three characters or less long'),'error');
$Errors[$i] = 'SalesmanCode';
$i++;
} elseif (mb_strlen($_POST['SalesmanCode'])==0 OR $_POST['SalesmanCode']=='') {
$InputError = 1;
prnMsg(_('The salesperson code cannot be empty'),'error');
$Errors[$i] = 'SalesmanCode';
$i++;
} elseif (mb_strlen($_POST['SalesmanName']) > 30) {
$InputError = 1;
prnMsg(_('The salesperson name must be thirty characters or less long'),'error');
$Errors[$i] = 'SalesmanName';
$i++;
} elseif (mb_strlen($_POST['SManTel']) > 20) {
$InputError = 1;
prnMsg(_('The salesperson telephone number must be twenty characters or less long'),'error');
} elseif (mb_strlen($_POST['SManFax']) > 20) {
$InputError = 1;
prnMsg(_('The salesperson telephone number must be twenty characters or less long'),'error');
}
if (!isset($_POST['SManTel'])){
$_POST['SManTel']='';
}
if (!isset($_POST['SManFax'])){
$_POST['SManFax']='';
}
if (!isset($_POST['Current'])){
$_POST['Current']=0;
}
if (!isset($_POST['CommissionPeriod'])){
$_POST['CommissionPeriod']=0;
}
if (!isset($_POST['CommissionTypeID'])){
$_POST['CommissionTypeID']=0;
}
if (!isset($_POST['GLAccount'])){
$_POST['GLAccount']='';
}
if (isset($SelectedSalesPerson) AND $InputError !=1) {
/*SelectedSalesPerson 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*/
$SQL = "UPDATE salesman SET salesmanname='" . $_POST['SalesmanName'] . "',
smantel='" . $_POST['SManTel'] . "',
smanfax='" . $_POST['SManFax'] . "',
current='" . $_POST['Current'] . "',
commissionperiod='" . $_POST['CommissionPeriod'] . "',
commissiontypeid='" . $_POST['CommissionTypeID'] . "',
glaccount='" . $_POST['GLAccount'] . "'
WHERE salesmancode = '" . stripslashes($SelectedSalesPerson) . "'";
$Msg = _('Salesperson record for') . ' ' . $_POST['SalesmanName'] . ' ' . _('has been updated');
} elseif ($InputError !=1) {
/*Selected group is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new Sales-person form */
$SQL = "INSERT INTO salesman (salesmancode,
salesmanname,
smantel,
smanfax,
current,
commissionperiod,
commissiontypeid,
glaccount)
VALUES ('" . $_POST['SalesmanCode'] . "',
'" . $_POST['SalesmanName'] . "',
'" . $_POST['SManTel'] . "',
'" . $_POST['SManFax'] . "',
'" . $_POST['Current'] . "',
'" . $_POST['CommissionPeriod'] . "',
'" . $_POST['CommissionTypeID'] . "',
'" . $_POST['GLAccount'] . "'
)";
$Msg = _('A new salesperson record has been added for') . ' ' . $_POST['SalesmanName'];
}
if ($InputError !=1) {
//run the SQL from either of the above possibilites
$ErrMsg = _('The insert or update of the salesperson failed because');
$DbgMsg = _('The SQL that was used and failed was');
$Result = DB_query($SQL,$ErrMsg, $DbgMsg);
prnMsg($Msg , 'success');
unset($SelectedSalesPerson);
unset($_POST['SalesmanCode']);
unset($_POST['SalesmanName']);
unset($_POST['SManFax']);
unset($_POST['SManTel']);
unset($_POST['Current']);
unset($_POST['CommissionPeriod']);
unset($_POST['CommissionTypeID']);
unset($_POST['GLAccount']);
}
} elseif (isset($_GET['delete'])) {
$BookMark = 'SalespeopleDelete';
//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 custbranch WHERE custbranch.salesman='".$SelectedSalesPerson."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg(_('Cannot delete this salesperson because branches are set up referring to them') . ' - ' . _('first alter the branches concerned') . '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('branches that refer to this salesperson'),'error');
} else {
$SQL= "SELECT COUNT(*) FROM salesanalysis WHERE salesanalysis.salesperson='".$SelectedSalesPerson."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg(_('Cannot delete this salesperson because sales analysis records refer to them') , '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('sales analysis records that refer to this salesperson'),'error');
} else {
$SQL= "SELECT COUNT(*) FROM www_users WHERE salesman='".$SelectedSalesPerson."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg(_('Cannot delete this salesperson because') , '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('user records that refer to this salesperson') . '.' ._('First delete any users that refer to this sales person'),'error');
} else {
$SQL="DELETE FROM salesman WHERE salesmancode='". $SelectedSalesPerson."'";
$ErrMsg = _('The salesperson could not be deleted because');
$Result = DB_query($SQL,$ErrMsg);
prnMsg(_('Salesperson') . ' ' . $SelectedSalesPerson . ' ' . _('has been deleted from the database'),'success');
unset ($SelectedSalesPerson);
unset($Delete);
}
}
} //end if Sales-person used in GL accounts
}
if (!isset($SelectedSalesPerson)) {
/* It could still be the second time the page has been run and a record has been selected for modification - SelectedSalesPerson 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 Sales-persons 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 salesmancode,
salesmanname,
smantel,
smanfax,
current,
commissionperiod,
commissiontypeid,
glaccount
FROM salesman";
$Result = DB_query($SQL);
echo '<table class="selection">';
echo '<tr>
<th>' . _('Code') . '</th>
<th>' . _('Name') . '</th>
<th>' . _('Telephone') . '</th>
<th>' . _('Facsimile') . '</th>
<th class="SortedColumn">', _('Current'), '</th>
<th class="SortedColumn">', _('Commission Period'), '</th>
<th class="SortedColumn">', _('Commission Calculation Method'), '</th>
<th class="SortedColumn">', _('GL Account'), '</th>
<th colspan="3"></th>
</tr>';
while ($MyRow=DB_fetch_array($Result)) {
if ($MyRow['current'] == 1) {
$ActiveText = _('Yes');
} else {
$ActiveText = _('No');
}
if ($MyRow['commissiontypeid'] == 0) {
$TypeRow['commissiontypename'] = _('No Commission');
} else {
$SQL = "SELECT commissiontypename FROM salescommissiontypes WHERE commissiontypeid='" . $MyRow['commissiontypeid'] . "'";
$TypeResult = DB_query($SQL);
$TypeRow = DB_fetch_array($TypeResult);
}
$SQL = "SELECT accountname
FROM chartmaster
WHERE accountcode='" . $MyRow['glaccount'] . "'";
$GLResult = DB_query($SQL);
$GLRow = DB_fetch_array($GLResult);
echo'<tr class="striped_row">
<td>', $MyRow['salesmancode'], '</td>
<td>', $MyRow['salesmanname'], '</td>
<td>', $MyRow['smantel'], '</td>
<td>', $MyRow['smanfax'], '</td>
<td>', $ActiveText, '</td>
<td>', $CommissionPeriods[$MyRow['commissionperiod']], '</td>
<td>', $MyRow['commissiontypeid'], ' - ', $TypeRow['commissiontypename'], '</td>
<td>', $MyRow['glaccount'], ' - ', $GLRow['accountname'], '</td>
<td><a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '?SelectedSalesPerson=', urlencode($MyRow['salesmancode']), '">', _('Edit'), '</a></td>
<td><a href="', $RootPath, '/SalesCommissionRates.php?SelectedSalesPerson=', urlencode($MyRow['salesmancode']), '">', _('Edit Commission Rates'), '</a></td>
<td><a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '?SelectedSalesPerson=', urlencode($MyRow['salesmancode']), '&delete=1" onclick="return MakeConfirm(\'', _('Are you sure you wish to delete this sales person?'), '\', \'Confirm Delete\', this);">', _('Delete'), '</a></td>
</tr>';
} //END WHILE LIST LOOP
echo '</table><br />';
} //end of ifs and buts!
if (isset($SelectedSalesPerson)) {
echo '<div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Show All Sales People') . '</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($SelectedSalesPerson)) {
//editing an existing Sales-person
$SQL = "SELECT salesmancode,
salesmanname,
smantel,
smanfax,
current,
commissionperiod,
commissiontypeid,
glaccount
FROM salesman
WHERE salesmancode='".$SelectedSalesPerson."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['SalesmanCode'] = $MyRow['salesmancode'];
$_POST['SalesmanName'] = $MyRow['salesmanname'];
$_POST['SManTel'] = $MyRow['smantel'];
$_POST['SManFax'] = $MyRow['smanfax'];
$_POST['Current'] = $MyRow['current'];
$_POST['CommissionPeriod'] = $MyRow['commissionperiod'];
$_POST['CommissionTypeID'] = $MyRow['commissiontypeid'];
$_POST['GLAccount'] = $MyRow['glaccount'];
echo '<input type="hidden" name="SelectedSalesPerson" value="' . $SelectedSalesPerson . '" />';
echo '<input type="hidden" name="SalesmanCode" value="' . $_POST['SalesmanCode'] . '" />';
echo '<fieldset>
<legend>', _('Edit the details for'), ' ', $_POST['SalesmanCode'], ' - ', $_POST['SalesmanName'], '</legend>
<field>
<label for="SalesmanCode">', _('Salesperson code'), ':</label>
<div class="fieldtext">', $_POST['SalesmanCode'], '</div>
</field>';
} else { //end of if $SelectedSalesPerson only do the else when a new record is being entered
echo '<fieldset>
<legend>', _('Create a new sales person record'), '</legend>
<field>
<label for="SalesmanCode">', _('Salesperson code'), ':</label>
<input type="text" name="SalesmanCode" size="3" autofocus="autofocus" required="required" maxlength="3" />
<fieldhelp>', _('Enter a three character code for this sales person.'), '</fieldhelp>
</field>';
}
if (!isset($_POST['SalesmanName'])){
$_POST['SalesmanName']='';
}
if (!isset($_POST['SManTel'])){
$_POST['SManTel']='';
}
if (!isset($_POST['SManFax'])){
$_POST['SManFax']='';
}
if (!isset($_POST['CommissionPeriod'])){
$_POST['CommissionPeriod']=0;
}
if (!isset($_POST['CommissionTypeID'])){
$_POST['CommissionTypeID']=0;
}
if (!isset($_POST['GLAccount'])){
$_POST['GLAccount']='';
}
if (!isset($_POST['Current'])){
$_POST['Current']=1;
}
echo '<field>
<label for="SalesmanName">' . _('Salesperson Name') . ':</label>
<input type="text" '. (in_array('SalesmanName',$Errors) ? 'class="inputerror"' : '' ) .' name="SalesmanName" size="30" maxlength="30" value="' . $_POST['SalesmanName'] . '" />
<fieldhelp>', _('Enter the name by which this sales person will be known.'), '</fieldhelp>
</field>';
echo '<field>
<label for="SManTel">' . _('Telephone No') . ':</label>
<input type="text" name="SManTel" size="20" maxlength="20" value="' . $_POST['SManTel'] . '" />
<fieldhelp>', _('Contact telephone number for this sales person.'), '</fieldhelp>
</field>';
echo '<field>
<label for="SManFax">' . _('Facsimile No') . ':</label>
<input type="text" name="SManFax" size="20" maxlength="20" value="' . $_POST['SManFax'] . '" />
<fieldhelp>', _('Contact fax number for this sales person.'), '</fieldhelp>
</field>';
echo '<field>
<label for="CommissionPeriod">', _('Commission Period'), ':</label>
<select required="required" name="CommissionPeriod">';
foreach ($CommissionPeriods as $Index => $PeriodName) {
if ($Index == $_POST['CommissionPeriod']) {
echo '<option selected="selected" value="', $Index, '">', $PeriodName, '</option>';
} else {
echo '<option value="', $Index, '" />', $PeriodName, '</option>';
}
}
echo '</select>
<fieldhelp>', _('Select the period over which this sales person has there commission calculated.'), '</fieldhelp>
</field>';
echo '<field>
<label for="CommissionTypeID">', _('Commission Calculation Method'), ':</label>
<select name="CommissionTypeID">';
$SQL = "SELECT commissiontypeid, commissiontypename FROM salescommissiontypes ORDER BY commissiontypename";
$ErrMsg = _('An error occurred in retrieving the sales commission types from the database');
$DbgMsg = _('The SQL that was used to retrieve the commission type information and that failed in the process was');
$CommissionTypeResult = DB_query($SQL, $ErrMsg, $DbgMsg);
if (!isset($_POST['CommissionTypeID']) or $_POST['CommissionTypeID'] == 0) {
echo '<option selected="selected" value="0">', _('No Commission'), '</option>';
} else {
echo '<option value="0">', _('No Commission'), '</option>';
}
while ($CommissionTypeRow = DB_fetch_array($CommissionTypeResult)) {
if ($_POST['CommissionTypeID'] == $CommissionTypeRow['commissiontypeid']) {
echo '<option selected="selected" value="', $CommissionTypeRow['commissiontypeid'], '">', $CommissionTypeRow['commissiontypename'], ' (', $CommissionTypeRow['commissiontypeid'], ')</option>';
} else {
echo '<option value="', $CommissionTypeRow['commissiontypeid'], '">', $CommissionTypeRow['commissiontypename'], ' (', $CommissionTypeRow['commissiontypeid'], ')</option>';
}
}
echo '</select>
<fieldhelp>', _('Select the calculation method used to calculate commission for this sales person. This is only used if a commission period is selected.'), '</fieldhelp>
</field>';
$Result = DB_query("SELECT accountcode,
accountname
FROM chartmaster
INNER JOIN accountgroups
ON chartmaster.group_=accountgroups.groupname
WHERE accountgroups.pandl=1
ORDER BY chartmaster.accountcode");
echo '<field>
<label for="GLAccount">', _('GL Commission Account'), ':</label>
<select name="GLAccount">';
while ($MyRow = DB_fetch_row($Result)) {
if ($_POST['GLAccount'] == $MyRow[0]) {
echo '<option selected="selected" value="', $MyRow[0], '">', htmlspecialchars($MyRow[1], ENT_QUOTES, 'UTF-8'), ' (', $MyRow[0], ')</option>';
} else {
echo '<option value="', $MyRow[0], '">', htmlspecialchars($MyRow[1], ENT_QUOTES, 'UTF-8'), ' (', $MyRow[0], ')</option>';
}
} //end while loop
echo '</select>
<fieldhelp>', _('Select the general ledger account to post the sales commission for this sales person to.'), '</fieldhelp>
</field>';
echo '<field>
<label for="Current">', _('Current?'), ':</label>
<select required="required" name="Current">';
if ($_POST['Current'] == 1) {
echo '<option selected="selected" value="1">', _('Yes'), '</option>';
} else {
echo '<option value="1">', _('Yes'), '</option>';
}
if ($_POST['Current'] == 0) {
echo '<option selected="selected" value="0">', _('No'), '</option>';
} else {
echo '<option value="0">', _('No'), '</option>';
}
echo '</select>
<fieldhelp>', _('If this sales person is currently employed, select Yes, otherwise select No.'), '</fieldhelp>
</field>';
echo '</fieldset>';
echo '<div class="centre">
<input type="submit" name="submit" value="' . _('Enter Information') . '" />
</div>
</form>';
} //end if record deleted no point displaying form to add record
include('includes/footer.php');
?>