/
manu_recipe.php
executable file
·485 lines (413 loc) · 14.1 KB
/
manu_recipe.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
require ("settings.php");
if (isset($_REQUEST["key"])) {
$key = strtolower($_REQUEST["key"]);
switch ($key) {
case "recipe":
$OUTPUT = recipe();
break;
case "add":
$OUTPUT = add();
break;
case "remove":
$OUTPUT = remove();
break;
}
} else {
$OUTPUT = recipe();
}
$OUTPUT .= "
<center>
".mkQuickLinks(
ql("stock-add.php", "Add Stock"),
ql("stock-view.php", "View Stock")
)."
</center>";
require ("template.php");
function recipe()
{
extract($_REQUEST);
$fields = array();
$fields["m_stock_id"] = 0;
$fields["filter_store"] = 0;
$fields["filter_class"] = 0;
$fields["filter_cat"] = 0;
$fields["each_filter_store"] = 0;
$fields["each_filter_class"] = 0;
$fields["each_filter_cat"] = 0;
$fields["search_string"] = "";
$fields["each_search_string"] = "";
extract($fields, EXTR_SKIP);
$check_setting = getCSetting ("OPTIONAL_STOCK_FILTERS");
if (isset ($check_setting) AND $check_setting == "yes"){
if (isset ($filter_class) AND $filter_class != "0"){
$Wh .= " AND prdcls = '$filter_class'";
}
if (isset ($filter_cat) AND $filter_cat != "0"){
$Wh .= " AND catid = '$filter_cat'";
}
}
if (isset($filter_store) AND $filter_store != "0"){
$Wh .= " AND stock.whid = '$filter_store'";
}
if (isset ($search) OR (isset ($m_stock_id) AND $m_stock_id > 0)){
$dosearch = "TRUE";
}else {
$dosearch = "FALSE";
}
// Create the main stock item dropdown
$sql = "
SELECT stkid, stkcod, stkdes, whname
FROM cubit.stock
LEFT JOIN exten.warehouses ON stock.whid=warehouses.whid
WHERE $dosearch $Wh AND (stkcod ILIKE '%$search_string%' OR stkdes = '%$search_string%')
ORDER BY stkcod ASC";
$m_stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
$m_stock_sel = "<select name='m_stock_id' onchange='javascript:document.form.submit()' style='width: 100%'>";
$m_stock_sel.= "<option value='0'>[None / Display All Stores]</option>";
while ($m_stock_data = pg_fetch_array($m_stock_rslt)) {
if ($m_stock_id == $m_stock_data["stkid"]) {
$sel = "selected";
} else {
$sel = "";
}
$m_stock_sel .= "
<option value='$m_stock_data[stkid]' $sel>
($m_stock_data[whname]) ($m_stock_data[stkcod]) $m_stock_data[stkdes]
</option>";
}
$m_stock_sel .= "</select>";
// Just a dummy message to make the user feel good about his/herself :)
if (isset($save) && $save) {
$msg = "
<tr class='".bg_class()."'>
<td colspan='3'><li>The recipe has been successfully saved.</li></td>
</tr>";
} else {
$msg = "";
}
$optional_filter_setting = getCSetting ("OPTIONAL_STOCK_FILTERS");
if (isset ($optional_filter_setting) AND $optional_filter_setting == "yes"){
db_connect ();
$catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '".USER_DIV."' ORDER BY cat ASC";
$catRslt = db_exec($catsql);
if(pg_numrows($catRslt) < 1){
$cat_drop = "<input type='hidden' name='filter_cat' value='0'>";
}else{
$cat_drop = "<select name='filter_cat'>";
$cat_drop .= "<option value='0'>All Categories</option>";
while($cat = pg_fetch_array($catRslt)){
if (isset ($filter_cat) AND $filter_cat == $cat['catid']){
$cat_drop .= "<option value='$cat[catid]' selected>($cat[catcod]) $cat[cat]</option>";
}else {
$cat_drop .= "<option value='$cat[catid]'>($cat[catcod]) $cat[cat]</option>";
}
}
$cat_drop .= "</select>";
}
# Select classification
$classsql = "SELECT * FROM stockclass WHERE div = '".USER_DIV."' ORDER BY classname ASC";
$clasRslt = db_exec($classsql);
if(pg_numrows($clasRslt) < 1){
$class_drop = "<input type='hidden' name='filter_class' value='0'>";
}else{
$class_drop = "<select name='filter_class' style='width: 167'>";
$class_drop .= "<option value='0'>All Classifications</option>";
while($clas = pg_fetch_array($clasRslt)){
if (isset ($filter_class) AND $filter_class == $clas['clasid']){
$class_drop .= "<option value='$clas[clasid]' selected>$clas[classname]</option>";
}else {
$class_drop .= "<option value='$clas[clasid]'>$clas[classname]</option>";
}
}
$class_drop .= "</select>";
}
$display_optional_filters = "
<tr>
<th>Select Category</th>
<th>Select Classification</th>
</tr>
<tr class='".bg_class()."'>
<td align='center'>$cat_drop</td>
<td align='center'>$class_drop</td>
</tr>";
}
db_conn("exten");
$sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '".USER_DIV."' ORDER BY whname ASC";
$whRslt = db_exec($sql);
if(pg_numrows($whRslt) < 1){
$store_drop = "<input type='hidden' name='filter_store' value='0'>";
}else{
if (!isset ($filter_store)){
# check if setting exists
db_connect();
$sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '".USER_DIV."'";
$Rslt = db_exec ($sql) or errDie ("Unable to check database for existing settings.");
if (pg_numrows ($Rslt) > 0) {
$set = pg_fetch_array($Rslt);
$whid = $set['value'];
}
}
$store_drop = "<select name='filter_store'>";
$store_drop .= "<option value='0'>All Stores</option>";
while($wh = pg_fetch_array($whRslt)){
if (isset ($filter_store) AND $filter_store == $wh['whid']){
$store_drop .= "<option value='$wh[whid]' selected>($wh[whno]) $wh[whname]</option>";
}else {
$store_drop .= "<option value='$wh[whid]'>($wh[whno]) $wh[whname]</option>";
}
}
$store_drop .= "</select>";
}
$OUTPUT = "
<center>
<h3>Create Recipe</h3>
<form method='POST' action='".SELF."' name='form'>
<table ".TMPL_tblDflts.">
$msg
<tr>
<th colspan='2'>Search Stock</th>
</tr>
$display_optional_filters
<tr>
<th>Store</th>
<th>Search Code/Description</th>
</tr>
<tr class='".bg_class()."'>
<td align='center'>$store_drop</td>
<td align='center'><input type='text' name='search_string' value='$search_string'></td>
</tr>
<tr class='".bg_class()."'>
<td align='center' colspan='2'><input type='submit' name='search' value='Search'></td>
</tr>
<tr>
<th colspan='4'>Stock Item</th>
</tr>
<tr class='".bg_class()."'>
<td colspan='4'>$m_stock_sel</td>
</tr>
</table>
</form>
<table ".TMPL_tblDflts.">";
if ($m_stock_id) {
$check_setting = getCSetting ("OPTIONAL_STOCK_FILTERS");
if (isset ($check_setting) AND $check_setting == "yes"){
db_connect ();
$catsql = "SELECT catid, cat, catcod FROM stockcat WHERE div = '".USER_DIV."' ORDER BY cat ASC";
$catRslt = db_exec($catsql);
if(pg_numrows($catRslt) < 1){
$each_cat_drop = "<input type='hidden' name='each_filter_cat' value='0'>";
}else{
$each_cat_drop = "<select name='each_filter_cat'>";
$each_cat_drop .= "<option value='0'>All Categories</option>";
while($cat = pg_fetch_array($catRslt)){
if (isset ($each_filter_cat) AND $each_filter_cat == $cat['catid']){
$each_cat_drop .= "<option value='$cat[catid]' selected>($cat[catcod]) $cat[cat]</option>";
}else {
$each_cat_drop .= "<option value='$cat[catid]'>($cat[catcod]) $cat[cat]</option>";
}
}
$each_cat_drop .= "</select>";
}
# Select classification
$classsql = "SELECT * FROM stockclass WHERE div = '".USER_DIV."' ORDER BY classname ASC";
$clasRslt = db_exec($classsql);
if(pg_numrows($clasRslt) < 1){
$each_class_drop = "<input type='hidden' name='each_filter_class' value='0'>";
}else{
$each_class_drop = "<select name='each_filter_class' style='width: 167'>";
$each_class_drop .= "<option value='0'>All Classifications</option>";
while($clas = pg_fetch_array($clasRslt)){
if (isset ($each_filter_class) AND $each_filter_class == $clas['clasid']){
$each_class_drop .= "<option value='$clas[clasid]' selected>$clas[classname]</option>";
}else {
$each_class_drop .= "<option value='$clas[clasid]'>$clas[classname]</option>";
}
}
$each_class_drop .= "</select>";
}
$display_optional_filters_each1 = "
<th>Select Category</th>
<th>Select Classification</th>";
$display_optional_filters_each2 = "
<td align='center'>$each_cat_drop</td>
<td align='center'>$each_class_drop</td>";
}
db_conn("exten");
$sql = "SELECT whid, whname, whno FROM warehouses WHERE div = '".USER_DIV."' ORDER BY whname ASC";
$whRslt = db_exec($sql);
if(pg_numrows($whRslt) < 1){
$store_drop = "<input type='hidden' name='filter_store' value='0'>";
}else{
if (!isset ($filter_store)){
# check if setting exists
db_connect();
$sql = "SELECT value FROM set WHERE label = 'DEF_WH' AND div = '".USER_DIV."'";
$Rslt = db_exec ($sql) or errDie ("Unable to check database for existing settings.");
if (pg_numrows ($Rslt) > 0) {
$set = pg_fetch_array($Rslt);
$whid = $set['value'];
}
}
$each_store_drop = "<select name='each_filter_store'>";
$each_store_drop .= "<option value='0'>All Stores</option>";
while($wh = pg_fetch_array($whRslt)){
if (isset ($each_filter_store) AND $each_filter_store == $wh['whid']){
$each_store_drop .= "<option value='$wh[whid]' selected>($wh[whno]) $wh[whname]</option>";
}else {
$each_store_drop .= "<option value='$wh[whid]'>($wh[whno]) $wh[whname]</option>";
}
}
$each_store_drop .= "</select>";
}
if (isset ($check_setting) AND $check_setting == "yes"){
if (isset ($each_filter_class) AND $each_filter_class != "0"){
$Wh2 .= " AND prdcls = '$each_filter_class'";
}
if (isset ($each_filter_cat) AND $each_filter_cat != "0"){
$Wh2 .= " AND catid = '$each_filter_cat'";
}
}
if (isset($each_filter_store) AND $each_filter_store != "0"){
$Wh2 .= " AND stock.whid = '$each_filter_store'";
}
if (isset ($search_each)){
$do_each = "TRUE";
}else {
$do_each = "FALSE";
}
// Create the stock dropdown
$sql = "
SELECT stkid, stkcod, stkdes
FROM cubit.stock
WHERE
$do_each AND stkid!='$m_stock_id' $Wh2 AND
(stkcod ILIKE '%$each_search_string%' OR stkdes ILIKE '%$each_search_string%')
ORDER BY stkcod ASC";
$s_stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
$s_stock_sel = "<select name='s_stock_id' style='width: 100%'>";
$s_stock_sel.= "<option value='0'>[None]</option>";
while ($s_stock_data = pg_fetch_array($s_stock_rslt)) {
$s_stock_sel .= "
<option value='$s_stock_data[stkid]'>
($s_stock_data[stkcod]) $s_stock_data[stkdes]
</option>";
}
// Retrieve recipe for this item
$sql = "SELECT * FROM cubit.recipies WHERE m_stock_id='$m_stock_id' ORDER BY id DESC";
$recipe_rslt = db_exec($sql) or errDie("Unable to retrieve recipe.");
$recipe_out = "";
$cost_total = 0;
while ($recipe_data = pg_fetch_array($recipe_rslt)) {
// Retrieve stock
$sql = "SELECT stkid, stkcod, stkdes, csprice FROM cubit.stock WHERE stkid='$recipe_data[s_stock_id]'";
$stock_rslt = db_exec($sql) or errDie("Unable to retrieve stock.");
$stock_data = pg_fetch_array($stock_rslt);
$cost = $stock_data["csprice"] * $recipe_data["qty"];
$cost_total += $cost;
$recipe_out .= "
<tr class='".bg_class()."'>
<td>($stock_data[stkcod]) $stock_data[stkdes]</td>
<td align='center'>$recipe_data[qty]</td>
<td align='right'>".sprint($cost)."</td>
<td align='center'>
<input type='checkbox' name='rem[$recipe_data[id]]'
value='$recipe_data[id]'
onchange='javascript:document.form2.submit()' />
</td>
</tr>";
}
$OUTPUT .= "
<form method='post' action='".SELF."' name='form2'>
<input type='hidden' name='key' value='remove' />
<input type='hidden' name='m_stock_id' value='$m_stock_id' />
<input type='hidden' name='filter_store' value='$filter_store'>
<input type='hidden' name='filter_class' value='$filter_class'>
<input type='hidden' name='filter_cat' value='$filter_cat'>
<input type='hidden' name='search_string' value='$search_string'>
<tr>
<th colspan='4'>Stock Used in Manufacturing</th>
</tr>
<tr class='".bg_class()."'>
<td colspan='4'>
<table cellpadding='0' cellspacing='1'>
<tr>
$display_optional_filters_each1
<th>Store</th>
<th>Search Code/Description</th>
</tr>
<tr>
$display_optional_filters_each2
<td>$each_store_drop</td>
<td><input type='text' name='each_search_string' value='$each_search_string'></td>
<td><input type='submit' name='search_each' value='Search'></td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Stock</th>
<th>Qty</th>
<th>Cost</th>
<th>Add<br />Remove</th>
</tr>
<tr class='".bg_class()."'>
<td>$s_stock_sel</td>
<td><input type='text' name='qty' size='4' value='0' style='text-align: center'></td>
<td> </td>
<td><input type='submit' name='key' value='Add' style='width: 100%'></td>
</tr>
$recipe_out
<tr class='".bg_class()."'>
<td colspan='2'>Total</td>
<td align='right'>".sprint($cost_total)."</td>
<td> </td>
</tr>
</table>
</form>
<form method='POST' action='".SELF."'>
<input type='hidden' name='key' value='recipe' />
<input type='hidden' name='save' value='1' />
<input type='hidden' name='m_stock_id' value='$m_stock_id' />
<input type='submit' value='Save »' />
</form>
</center>";
} else {
$OUTPUT .= "
<tr class='".bg_class()."'>
<td><li>Please select a stock item to continue</li></td>
</tr>
</table>
</center>";
}
return $OUTPUT;
}
function add()
{
extract($_REQUEST);
if (is_numeric($qty) && $qty > 0 && $s_stock_id) {
$sql = "SELECT id FROM cubit.recipies WHERE s_stock_id='$s_stock_id' AND m_stock_id='$m_stock_id'";
$recipe_rslt = db_exec($sql) or errDie("Unable to retrieve recipe.");
$recipe_id = pg_fetch_result($recipe_rslt, 0);
if (pg_num_rows($recipe_rslt)) {
$sql = "UPDATE cubit.recipies SET qty=(qty + '$qty') WHERE m_stock_id='$m_stock_id' AND s_stock_id='$s_stock_id'";
db_exec($sql) or errDie("Unable to update recipe.");
} else {
$sql = "INSERT INTO cubit.recipies (m_stock_id, s_stock_id, qty) VALUES ('$m_stock_id', '$s_stock_id', '$qty')";
db_exec($sql) or errDie("Unable to create new recipe.");
}
}
return recipe();
}
function remove()
{
extract($_REQUEST);
if (isset($rem)) {
foreach ($rem as $id) {
$sql = "DELETE FROM cubit.recipies WHERE id='$id'";
db_exec($sql) or errDie("Unable to remove recipe.");
}
}
return recipe();
}
?>