forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupplierTypes.php
237 lines (186 loc) · 7.45 KB
/
SupplierTypes.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
<?php
include('includes/session.php');
$Title = _('Supplier Types') . ' / ' . _('Maintenance');
$ViewTopic = 'Setup';
$BookMark = '';
include('includes/header.php');
if (isset($_POST['SelectedType'])){
$SelectedType = mb_strtoupper($_POST['SelectedType']);
} elseif (isset($_GET['SelectedType'])){
$SelectedType = mb_strtoupper($_GET['SelectedType']);
}
if (isset($Errors)) {
unset($Errors);
}
$Errors = array();
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Supplier Types')
. '" alt="" />' . _('Supplier Type Setup') . '</p>
<div class="page_help_text">' . _('Add/edit/delete Supplier Types') . '</div>';
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
$i=1;
if (mb_strlen($_POST['TypeName']) >100) {
$InputError = 1;
prnMsg(_('The supplier type name description must be 100 characters or less long'),'error');
$Errors[$i] = 'SupplierType';
$i++;
}
if (mb_strlen(trim($_POST['TypeName']))==0) {
$InputError = 1;
prnMsg(_('The supplier type name description must contain at least one character'),'error');
$Errors[$i] = 'SupplierType';
$i++;
}
$CheckSQL = "SELECT count(*)
FROM suppliertype
WHERE typename = '" . $_POST['TypeName'] . "'";
$CheckResult=DB_query($CheckSQL);
$CheckRow=DB_fetch_row($CheckResult);
if ($CheckRow[0]>0 and !isset($_POST['Edit'])) {
$InputError = 1;
prnMsg(_('You already have a supplier type called').' '.$_POST['TypeName'],'error');
$Errors[$i] = 'SupplierName';
$i++;
}
if (isset($_POST['Edit']) AND $InputError !=1) {
$SQL = "UPDATE suppliertype
SET typename = '" . $_POST['TypeName'] . "'
WHERE typeid = '" . $SelectedType . "'";
prnMsg(_('The supplier type') . ' ' . $SelectedType . ' ' . _('has been updated'),'success');
} elseif ($InputError !=1){
// Add new record on submit
$SQL = "INSERT INTO suppliertype
(typename)
VALUES ('" . $_POST['TypeName'] . "')";
$Msg = _('Supplier type') . ' ' . $_POST['TypeName'] . ' ' . _('has been created');
$CheckSQL = "SELECT count(typeid) FROM suppliertype";
$Result = DB_query($CheckSQL);
$Row = DB_fetch_row($Result);
}
if ( $InputError !=1) {
//run the SQL from either of the above possibilites
$Result = DB_query($SQL);
// Fetch the default supplier type
$SQL = "SELECT confvalue
FROM config
WHERE confname='DefaultSupplierType'";
$Result = DB_query($SQL);
$SupplierTypeRow = DB_fetch_row($Result);
$DefaultSupplierType = $SupplierTypeRow[0];
// Does it exist
$CheckSQL = "SELECT count(*)
FROM suppliertype
WHERE typeid = '" . $DefaultSupplierType . "'";
$CheckResult = DB_query($CheckSQL);
$CheckRow = DB_fetch_row($CheckResult);
// If it doesnt then update config with newly created one.
if ($CheckRow[0] == 0) {
$SQL = "UPDATE config
SET confvalue='" . $_POST['TypeID'] . "'
WHERE confname='DefaultSupplierType'";
$Result = DB_query($SQL);
$_SESSION['DefaultSupplierType'] = $_POST['TypeID'];
}
unset($SelectedType);
unset($_POST['TypeID']);
unset($_POST['TypeName']);
}
} elseif ( isset($_GET['delete']) ) {
$SQL = "SELECT COUNT(*) FROM suppliers WHERE supptype='" . $SelectedType . "'";
$ErrMsg = _('The number of suppliers using this Type record could not be retrieved because');
$Result = DB_query($SQL,$ErrMsg);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg (_('Cannot delete this type because suppliers are currently set up to use this type') . '<br />' .
_('There are') . ' ' . $MyRow[0] . ' ' . _('suppliers with this type code'));
} else {
$SQL="DELETE FROM suppliertype WHERE typeid='" . $SelectedType . "'";
$ErrMsg = _('The Type record could not be deleted because');
$Result = DB_query($SQL,$ErrMsg);
prnMsg(_('Supplier type') . $SelectedType . ' ' . _('has been deleted') ,'success');
unset ($SelectedType);
unset($_GET['delete']);
}
}
if (!isset($SelectedType)){
/* It could still be the second time the page has been run and a record has been selected for modification - SelectedType 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 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 typeid, typename FROM suppliertype";
$Result = DB_query($SQL);
echo '<table class="selection">
<thead>
<tr>
<th class="SortedColumn" >' . _('Type ID') . '</th>
<th class="SortedColumn" >' . _('Type Name') . '</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($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedType=', $MyRow[0], '>' . _('Edit') . '</a></td>
<td><a href="', htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedType=', $MyRow[0], '&delete=yes" onclick="return confirm(\'' .
_('Are you sure you wish to delete this Supplier Type?') . '\');">' . _('Delete') . '</a></td>
</tr>';
}
//END WHILE LIST LOOP
echo '</tbody>
</table>';
}
//end of ifs and buts!
if (isset($SelectedType)) {
echo '<div class="centre">
<p><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Show All Types Defined') . '</a></p>
</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'] . '" />';
echo '<fieldset>'; //Main table
// The user wish to EDIT an existing type
if ( isset($SelectedType) AND $SelectedType!='' ) {
$SQL = "SELECT typeid,
typename
FROM suppliertype
WHERE typeid='" . $SelectedType . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['TypeID'] = $MyRow['typeid'];
$_POST['TypeName'] = $MyRow['typename'];
echo '<input type="hidden" name="Edit" value="' . $SelectedType . '" />';
echo '<input type="hidden" name="SelectedType" value="' . $SelectedType . '" />';
echo '<input type="hidden" name="TypeID" value="' . $_POST['TypeID'] . '" />';
// We dont allow the user to change an existing type code
echo '<legend>', _('Edit Supplier Type'), '</legend>
<field>
<label for="TypeID">' ._('Type ID') . ': </label>
<fieldtext>' . $_POST['TypeID'] . '</fieldtext>
</field>';
} else {
echo '<legend>', _('Create Supplier Type'), '</legend>';
}
if (!isset($_POST['TypeName'])) {
$_POST['TypeName']='';
}
echo '<field>
<label for="TypeName">' . _('Type Name') . ':</label>
<input type="text" required="true" pattern="(?!^\s+$)[^<>+-]{1,100}" title="" name="TypeName" placeholder="'._('less than 100 characters').'" value="' . $_POST['TypeName'] . '" />
<fieldhelp>'._('The input should not be over 100 characters and contains illegal characters') . ' ' . '" \' - & or a space'.'</fieldhelp>
</field>';
echo '</fieldset>';
echo '<div class="centre">
<input type="submit" name="submit" value="' . _('Accept') . '" />
</div>';
echo '</form>';
} // end if user wish to delete
include('includes/footer.php');
?>