forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitsOfMeasure.php
244 lines (207 loc) · 8.57 KB
/
UnitsOfMeasure.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
<?php
include('includes/session.php');
$Title = _('Units Of Measure');
$ViewTopic = 'Setup';
$BookMark = '';
include('includes/header.php');
echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/magnifier.png" title="' .
_('Search') . '" alt="" />' . ' ' . $Title . '</p>';
if ( isset($_GET['SelectedMeasureID']) )
$SelectedMeasureID = $_GET['SelectedMeasureID'];
elseif (isset($_POST['SelectedMeasureID']))
$SelectedMeasureID = $_POST['SelectedMeasureID'];
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 (ContainsIllegalCharacters($_POST['MeasureName'])) {
$InputError = 1;
prnMsg( _('The unit of measure cannot contain any of the illegal characters') . ' ' . '" \' - & or a space' ,'error');
}
if (trim($_POST['MeasureName']) == '') {
$InputError = 1;
prnMsg( _('The unit of measure may not be empty'), 'error');
}
if (isset($_POST['SelectedMeasureID']) AND $_POST['SelectedMeasureID']!='' AND $InputError !=1) {
/*SelectedMeasureID 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*/
// Check the name does not clash
$SQL = "SELECT count(*) FROM unitsofmeasure
WHERE unitid <> '" . $SelectedMeasureID ."'
AND unitname ".LIKE." '" . $_POST['MeasureName'] . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ( $MyRow[0] > 0 ) {
$InputError = 1;
prnMsg( _('The unit of measure can not be renamed because another with the same name already exist.'),'error');
} else {
// Get the old name and check that the record still exist neet to be very carefull here
// idealy this is one of those sets that should be in a stored procedure simce even the checks are
// relavant
$SQL = "SELECT unitname FROM unitsofmeasure
WHERE unitid = '" . $SelectedMeasureID . "'";
$Result = DB_query($SQL);
if ( DB_num_rows($Result) != 0 ) {
// This is probably the safest way there is
$MyRow = DB_fetch_row($Result);
$OldMeasureName = $MyRow[0];
$SQL = array();
$SQL[] = "UPDATE unitsofmeasure
SET unitname='" . $_POST['MeasureName'] . "'
WHERE unitname ".LIKE." '".$OldMeasureName."'";
$SQL[] = "UPDATE stockmaster
SET units='" . $_POST['MeasureName'] . "'
WHERE units ".LIKE." '" . $OldMeasureName . "'";
} else {
$InputError = 1;
prnMsg( _('The unit of measure no longer exist.'),'error');
}
}
$Msg = _('Unit of measure changed');
} elseif ($InputError !=1) {
/*SelectedMeasureID is null cos no item selected on first time round so must be adding a record*/
$SQL = "SELECT count(*) FROM unitsofmeasure
WHERE unitname " .LIKE. " '".$_POST['MeasureName'] ."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ( $MyRow[0] > 0 ) {
$InputError = 1;
prnMsg( _('The unit of measure can not be created because another with the same name already exists.'),'error');
} else {
$SQL = "INSERT INTO unitsofmeasure (unitname )
VALUES ('" . $_POST['MeasureName'] ."')";
}
$Msg = _('New unit of measure added');
}
if ($InputError!=1){
//run the SQL from either of the above possibilites
if (is_array($SQL)) {
DB_Txn_Begin();
$TmpErr = _('Could not update unit of measure');
$tmpDbg = _('The sql that failed was') . ':';
foreach ($SQL as $stmt ) {
$Result = DB_query($stmt, $TmpErr,$tmpDbg,true);
if(!$Result) {
$InputError = 1;
break;
}
}
if ($InputError!=1){
DB_Txn_Commit();
} else {
DB_Txn_Rollback();
}
} else {
$Result = DB_query($SQL);
}
prnMsg($Msg,'success');
}
unset ($SelectedMeasureID);
unset ($_POST['SelectedMeasureID']);
unset ($_POST['MeasureName']);
} elseif (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button
// PREVENT DELETES IF DEPENDENT RECORDS IN 'stockmaster'
// Get the original name of the unit of measure the ID is just a secure way to find the unit of measure
$SQL = "SELECT unitname FROM unitsofmeasure
WHERE unitid = '" . $SelectedMeasureID . "'";
$Result = DB_query($SQL);
if ( DB_num_rows($Result) == 0 ) {
// This is probably the safest way there is
prnMsg( _('Cannot delete this unit of measure because it no longer exist'),'warn');
} else {
$MyRow = DB_fetch_row($Result);
$OldMeasureName = $MyRow[0];
$SQL= "SELECT COUNT(*) FROM stockmaster WHERE units ".LIKE." '" . $OldMeasureName . "'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg( _('Cannot delete this unit of measure because inventory items have been created using this unit of measure'),'warn');
echo '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('inventory items that refer to this unit of measure') . '</font>';
} else {
$SQL="DELETE FROM unitsofmeasure WHERE unitname ".LIKE."'" . $OldMeasureName . "'";
$Result = DB_query($SQL);
prnMsg( $OldMeasureName . ' ' . _('unit of measure has been deleted') . '!','success');
}
} //end if account group used in GL accounts
unset ($SelectedMeasureID);
unset ($_GET['SelectedMeasureID']);
unset($_GET['delete']);
unset ($_POST['SelectedMeasureID']);
unset ($_POST['MeasureID']);
unset ($_POST['MeasureName']);
}
if (!isset($SelectedMeasureID)) {
/* An unit of measure could be posted when one has been edited and is being updated
or GOT when selected for modification
SelectedMeasureID will exist because it was sent with the page in a GET .
If its the first time the page has been displayed with no parameters
then none of the above are true and the list of account groups 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 unitid,
unitname
FROM unitsofmeasure
ORDER BY unitid";
$ErrMsg = _('Could not get unit of measures because');
$Result = DB_query($SQL,$ErrMsg);
echo '<table class="selection">
<thead>
<tr>
<th class="SortedColumn">' . _('Units of Measure') . '</th>
</tr>
</thead>
<tbody>';
while ($MyRow = DB_fetch_row($Result)) {
echo '<tr class="striped_row">
<td>' . $MyRow[1] . '</td>
<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedMeasureID=' . $MyRow[0] . '">' . _('Edit') . '</a></td>
<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?SelectedMeasureID=' . $MyRow[0] . '&delete=1" onclick="return confirm(\'' . _('Are you sure you wish to delete this unit of measure?') . '\');">' . _('Delete') . '</a></td>
</tr>';
} //END WHILE LIST LOOP
echo '</tbody></table>';
} //end of ifs and buts!
if (isset($SelectedMeasureID)) {
echo '<div class="centre">
<a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Review Units of Measure') . '</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($SelectedMeasureID)) {
//editing an existing section
$SQL = "SELECT unitid,
unitname
FROM unitsofmeasure
WHERE unitid='" . $SelectedMeasureID . "'";
$Result = DB_query($SQL);
if ( DB_num_rows($Result) == 0 ) {
prnMsg( _('Could not retrieve the requested unit of measure, please try again.'),'warn');
unset($SelectedMeasureID);
} else {
$MyRow = DB_fetch_array($Result);
$_POST['MeasureID'] = $MyRow['unitid'];
$_POST['MeasureName'] = $MyRow['unitname'];
echo '<input type="hidden" name="SelectedMeasureID" value="' . $_POST['MeasureID'] . '" />';
echo '<fieldset>
<legend>', _('Edit Unit of Measure'), '</legend>';
}
} else {
$_POST['MeasureName']='';
echo '<fieldset>
<legend>', _('Create Unit of Measure'), '</legend>';
}
echo '<field>
<td>' . _('Unit of Measure') . ':' . '</td>
<td><input required="required" pattern="(?!^ *$)[^+<>-]{1,}" type="text" name="MeasureName" title="'._('Cannot be blank or contains illegal characters').'" placeholder="'._('More than one character').'" size="30" maxlength="30" value="' . $_POST['MeasureName'] . '" /></td>
</field>';
echo '</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');
?>