diff --git a/action.php b/action.php index 7619cf0..2e6f7fc 100644 --- a/action.php +++ b/action.php @@ -363,4 +363,64 @@ switch ($VARS['action']) { session_destroy(); header('Location: index.php?logout=1'); die("Logged out."); + case "addstock": + $insert = true; + + if (empty($VARS['stock'])) { + $VARS['stock'] = 1; + } else if (!is_numeric($VARS['stock'])) { + returnToSender('field_nan'); + } + + $user = $_SESSION['uid']; + + $data = [ + 'itemid' => $VARS['itemid'], + 'stock' => $VARS['stock'], + 'text1' => $VARS['text1'], + 'userid' => $user + ]; + + $database->insert('stock', $data); + + $currentqty = $database->get('items', 'qty', ['itemid' => $VARS['itemid']]); + $newqty = $currentqty + $VARS['stock']; + + $data = [ + 'qty' => $newqty + ]; + + $database->update('items', $data, ['itemid' => $VARS['itemid']]); + + returnToSender("stock_added"); + case "removestock": + $insert = true; + + if (empty($VARS['stock'])) { + $VARS['stock'] = -1; + } else if (!is_numeric($VARS['stock'])) { + returnToSender('field_nan'); + } + + $user = $_SESSION['uid']; + + $data = [ + 'itemid' => $VARS['itemid'], + 'stock' => -$VARS['stock'], + 'text1' => $VARS['text1'], + 'userid' => $user + ]; + + $database->insert('stock', $data); + + $currentqty = $database->get('items', 'qty', ['itemid' => $VARS['itemid']]); + $newqty = $currentqty - $VARS['stock']; + + $data = [ + 'qty' => $newqty + ]; + + $database->update('items', $data, ['itemid' => $VARS['itemid']]); + + returnToSender("stock_removed"); } diff --git a/database.sql b/database.sql index 5cac0af..14196f7 100644 --- a/database.sql +++ b/database.sql @@ -70,6 +70,27 @@ CREATE TABLE IF NOT EXISTS `items` ( ENGINE = InnoDB; +-- ----------------------------------------------------- +-- Table `stock` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `stock` ( + `stockid` INT NOT NULL AUTO_INCREMENT, + `timestamp` TIMESTAMP, + `itemid` INT NOT NULL, + `stock` INT NOT NULL, + `text1` TEXT(500) NOT NULL, + `userid` INT NOT NULL, + PRIMARY KEY (`stockid`), + -- INDEX `fk_items_stock_idx` (`stockid` ASC), + UNIQUE INDEX `stockid_UNIQUE` (`stockid` ASC), + CONSTRAINT `fk_items_stock` + FOREIGN KEY (`itemid`) + REFERENCES `items` (`itemid`) + ON DELETE NO ACTION + ON UPDATE NO ACTION) +ENGINE = InnoDB; + + -- ----------------------------------------------------- -- Table `labels` -- ----------------------------------------------------- @@ -148,4 +169,3 @@ INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (8, 'itemtext3', 'Text Va INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (9, 'catname', 'Category Name'); COMMIT; - diff --git a/langs/en/actions.json b/langs/en/actions.json index 3da53a6..0079195 100644 --- a/langs/en/actions.json +++ b/langs/en/actions.json @@ -4,5 +4,7 @@ "save": "Save", "delete": "Delete", "view": "View", - "show all items": "Show All Items" + "show all items": "Show All Items", + "addstock": "Add", + "removestock": "Remove" } diff --git a/langs/en/items.json b/langs/en/items.json index 15c6549..4e06df1 100644 --- a/langs/en/items.json +++ b/langs/en/items.json @@ -9,5 +9,9 @@ "cloning item": "Copying {oitem} {nitem}", "itemid": "Item ID", "id": "ID", - "Edit Item": "Edit Item" + "Edit Item": "Edit Item", + "Adding stock": "Adding stock", + "adding stock": "Adding stock for {item}", + "Removing stock": "Removing stock", + "removing stock": "Removing stock from {item}" } diff --git a/langs/en/messages.json b/langs/en/messages.json index c359087..036b246 100644 --- a/langs/en/messages.json +++ b/langs/en/messages.json @@ -17,5 +17,7 @@ "only showing understocked": "Only showing understocked items.", "missing name": "You need to enter a name.", "use the dropdowns": "Whoops, you need to use the category and location autocomplete boxes.", - "make categories and locations": "Please create at least one category and location before adding an item." + "make categories and locations": "Please create at least one category and location before adding an item.", + "stock added": "Stock added.", + "stock removed": "Stock removed." } diff --git a/langs/messages.php b/langs/messages.php index 3155be1..637c3b2 100644 --- a/langs/messages.php +++ b/langs/messages.php @@ -96,5 +96,13 @@ define("MESSAGES", [ "upload_success" => [ "string" => "Image uploaded.", "type" => "success" + ], + "stock_added" => [ + "string" => "stock added", + "type" => "success" + ], + "stock_removed" => [ + "string" => "stock removed", + "type" => "success" ] ]); diff --git a/lib/getitemtable.php b/lib/getitemtable.php index afe32b5..ab3f296 100644 --- a/lib/getitemtable.php +++ b/lib/getitemtable.php @@ -118,8 +118,12 @@ for ($i = 0; $i < count($items); $i++) { $user = new User($_SESSION['uid']); if ($user->hasPermission("INV_EDIT")) { $items[$i]["editbtn"] = ' ' . $Strings->get("edit", false) . ''; + $items[$i]["addstockbtn"] = ' ' . $Strings->get("addstock", false) . ''; + $items[$i]["removestockbtn"] = ' ' . $Strings->get("removestock", false) . ''; } else { $items[$i]["editbtn"] = ''; + $items[$i]["addstockbtn"] = ''; + $items[$i]["removestockbtn"] = ''; } $items[$i]["viewbtn"] = ' ' . $Strings->get("view", false) . ''; if (is_null($items[$i]['userid'])) { diff --git a/pages.php b/pages.php index e324a56..e4b8bcd 100644 --- a/pages.php +++ b/pages.php @@ -99,5 +99,27 @@ define("PAGES", [ ], "404" => [ "title" => "404 error" + ], + "addstock" => [ + "title" => "Add stock", + "navbar" => false, + "styles" => [ + "static/css/easy-autocomplete.min.css" + ], + "scripts" => [ + "static/js/jquery.easy-autocomplete.min.js", + "static/js/edititem.js" + ], + ], + "removestock" => [ + "title" => "Remove stock", + "navbar" => false, + "styles" => [ + "static/css/easy-autocomplete.min.css" + ], + "scripts" => [ + "static/js/jquery.easy-autocomplete.min.js", + "static/js/edititem.js" + ], ] ]); diff --git a/pages/addstock.php b/pages/addstock.php new file mode 100644 index 0000000..f6e4381 --- /dev/null +++ b/pages/addstock.php @@ -0,0 +1,77 @@ +count("locations") == 0 || $database->count("categories") == 0) { + header('Location: app.php?page=items&msg=noloccat'); + die(); +} + +$itemdata = [ + 'text1' => '', + 'qty' => '']; + +if (!empty($VARS['id'])) { + if ($database->has('items', ['itemid' => $VARS['id']])) { + $itemdata = $database->select( + 'items', [ + 'name', + 'qty', + ], [ + 'itemid' => $VARS['id'] + ])[0]; + } else { + // item id is invalid, redirect to a page that won't cause an error when pressing Save + header('Location: app.php?page=addstock'); + die(); + } +} +?> + +
+
+

+ build("adding stock", ['item' => "" . htmlspecialchars($itemdata['name']) . ""]); ?> +

+
+ +
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ + + + '; + } else { + echo ''; + } + ?> + + +
+
diff --git a/pages/edititem.php b/pages/edititem.php index f4da79e..c8f402d 100644 --- a/pages/edititem.php +++ b/pages/edititem.php @@ -164,7 +164,17 @@ if (!empty($VARS['id'])) {
- + + + + +
@@ -263,4 +273,4 @@ if (!empty($VARS['id'])) { ?>
- \ No newline at end of file + diff --git a/pages/removestock.php b/pages/removestock.php new file mode 100644 index 0000000..86d8551 --- /dev/null +++ b/pages/removestock.php @@ -0,0 +1,77 @@ +count("locations") == 0 || $database->count("categories") == 0) { + header('Location: app.php?page=items&msg=noloccat'); + die(); +} + +$itemdata = [ + 'text1' => '', + 'qty' => '']; + +if (!empty($VARS['id'])) { + if ($database->has('items', ['itemid' => $VARS['id']])) { + $itemdata = $database->select( + 'items', [ + 'name', + 'qty', + ], [ + 'itemid' => $VARS['id'] + ])[0]; + } else { + // item id is invalid, redirect to a page that won't cause an error when pressing Save + header('Location: app.php?page=removestock'); + die(); + } +} +?> + +
+
+

+ build("removing stock", ['item' => "" . htmlspecialchars($itemdata['name']) . ""]); ?> +

+
+ +
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ + + + '; + } else { + echo ''; + } + ?> + + +
+
diff --git a/static/js/items.js b/static/js/items.js index 298fa14..009bbca 100644 --- a/static/js/items.js +++ b/static/js/items.js @@ -49,7 +49,7 @@ var itemtable = $('#itemtable').DataTable({ json.items.forEach(function (row) { json.data.push([ "", - "" + row.viewbtn + " " + row.editbtn + "", + "" + row.viewbtn + " " + row.editbtn + " " + row.addstockbtn + " " + row.removestockbtn + "" row.name, row.catname, row.locname + " (" + row.loccode + ")", @@ -74,4 +74,4 @@ $(document).ready(function () { $(searchInput).trigger("input"); $(searchInput).trigger("change"); } -}); \ No newline at end of file +});