forked from victorkagimu/KwaMoja
/
MRPDemandTypes.php
190 lines (158 loc) · 6.68 KB
/
MRPDemandTypes.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
<?php
include ('includes/session.php');
$Title = _('MRP Demand Types');
include ('includes/header.php');
//SelectedDT is the Selected MRPDemandType
if (isset($_POST['SelectedDT'])) {
$SelectedDT = trim(mb_strtoupper($_POST['SelectedDT']));
} elseif (isset($_GET['SelectedDT'])) {
$SelectedDT = trim(mb_strtoupper($_GET['SelectedDT']));
}
echo '<p class="page_title_text">
<img src="', $RootPath, '/css/', $_SESSION['Theme'], '/images/inventory.png" title="', _('Inventory'), '" 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 */
//first off validate inputs sensible
if (trim(mb_strtoupper($_POST['MRPDemandType']) == 'WO') or trim(mb_strtoupper($_POST['MRPDemandType']) == 'SO')) {
$InputError = 1;
prnMsg(_('The Demand Type is reserved for the system'), 'error');
}
if (mb_strlen($_POST['MRPDemandType']) < 1) {
$InputError = 1;
prnMsg(_('The Demand Type code must be at least 1 character long'), 'error');
}
if (mb_strlen($_POST['Description']) < 3) {
$InputError = 1;
prnMsg(_('The Demand Type description must be at least 3 characters long'), 'error');
}
if (isset($SelectedDT) and $InputError != 1) {
/*SelectedDT 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 mrpdemandtypes SET description = '" . $_POST['Description'] . "'
WHERE mrpdemandtype = '" . $SelectedDT . "'";
$Msg = _('The demand type record has been updated');
} elseif ($InputError != 1) {
//Selected demand type is null cos no item selected on first time round so must be adding a
//record must be submitting new entries in the new work centre form
$SQL = "INSERT INTO mrpdemandtypes (mrpdemandtype,
description)
VALUES ('" . trim(mb_strtoupper($_POST['MRPDemandType'])) . "',
'" . $_POST['Description'] . "'
)";
$Msg = _('The new demand type has been added to the database');
}
//run the SQL from either of the above possibilites
if ($InputError != 1) {
$Result = DB_query($SQL, _('The update/addition of the demand type failed because'));
prnMsg($Msg, 'success');
echo '<br />';
unset($_POST['Description']);
unset($_POST['MRPDemandType']);
unset($SelectedDT);
}
} elseif (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button
// PREVENT DELETES IF DEPENDENT RECORDS IN 'MRPDemands'
$SQL = "SELECT COUNT(*) FROM mrpdemands
WHERE mrpdemands.mrpdemandtype='" . $SelectedDT . "'
GROUP BY mrpdemandtype";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0] > 0) {
prnMsg(_('Cannot delete this demand type because MRP Demand records exist for this type') . '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('MRP Demands referring to this type'), 'warn');
} else {
$SQL = "DELETE FROM mrpdemandtypes WHERE mrpdemandtype='" . $SelectedDT . "'";
$Result = DB_query($SQL);
prnMsg(_('The selected demand type record has been deleted'), 'succes');
echo '<br />';
} // end of MRPDemands test
}
if (!isset($SelectedDT) or isset($_GET['delete'])) {
//It could still be the second time the page has been run and a record has been selected
//for modification SelectedDT 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 demand types 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 mrpdemandtype,
description
FROM mrpdemandtypes";
$Result = DB_query($SQL);
echo '<table>
<thead>
<tr>
<th class="SortedColumn">', _('Demand Type'), '</th>
<th class="SortedColumn">', _('Description'), '</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>';
while ($MyRow = DB_fetch_row($Result)) {
echo '<tr class="striped_row">
<td>', $MyRow[0], '</td>
<td>', $MyRow[1], '</td>
<td><a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '?SelectedDT=', urlencode($MyRow[0]), '">', _('Edit'), '</a></td>
<td><a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '?SelectedDT=', urlencode($MyRow[0]), '&delete=yes" onclick="return MakeConfirm(\'', _('Are you sure you wish to delete this account group?'), '\', \'Confirm Delete\', this);">', _('Delete'), '</a></td>
</tr>';
}
//END WHILE LIST LOOP
echo '</tbody>
</table>';
}
//end of ifs and buts!
if (isset($SelectedDT) and !isset($_GET['delete'])) {
echo '<div class="centre">
<a href="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '">', _('Show all Demand Types'), '</a>
</div>';
}
echo '<form method="post" action="', htmlspecialchars(basename(__FILE__), ENT_QUOTES, 'UTF-8'), '">';
echo '<input type="hidden" name="FormID" value="', $_SESSION['FormID'], '" />';
if (isset($SelectedDT) and !isset($_GET['delete'])) {
//editing an existing demand type
$SQL = "SELECT mrpdemandtype,
description
FROM mrpdemandtypes
WHERE mrpdemandtype='" . $SelectedDT . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['MRPDemandType'] = $MyRow['mrpdemandtype'];
$_POST['Description'] = $MyRow['description'];
echo '<input type="hidden" name="SelectedDT" value="', $SelectedDT, '" />';
echo '<input type="hidden" name="MRPDemandType" value="', $_POST['MRPDemandType'], '" />';
echo '<fieldset>
<legend>', _('Edit Demand Type'), '</legend>
<field>
<label for="MRPDemandType">', _('Demand Type'), ':</label>
<div class="fieldtext">', $_POST['MRPDemandType'], '</div>
</field>';
} else { //end of if $SelectedDT only do the else when a new record is being entered
if (!isset($_POST['MRPDemandType'])) {
$_POST['MRPDemandType'] = '';
}
echo '<fieldset>
<legend>', _('New Demand Type'), '</legend>
<field>
<label for="MRPDemandType">', _('Demand Type'), ':</label>
<input type="text" name="MRPDemandType" size="6" required="required" maxlength="5" value="', $_POST['MRPDemandType'], '" />
</field>';
}
if (!isset($_POST['Description'])) {
$_POST['Description'] = '';
}
echo '<field>
<label for="Description">', _('Demand Type Description'), ':</label>
<input type="text" name="Description" size="31" required="required" maxlength="30" value="', $_POST['Description'], '" /></td>
</field>';
echo '</fieldset>';
echo '<div class="centre">
<input type="submit" name="submit" value="', _('Enter Information'), '" />
</div>
</form>';
include ('includes/footer.php');
?>