From 2b02a32070ca33c0583d87a72a78ee30e23dc43c Mon Sep 17 00:00:00 2001 From: philippch Date: Sun, 4 Apr 2021 02:14:26 +0200 Subject: [PATCH 1/4] Create add and consume all buttons on unknown barcodes card. --- index.php | 130 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 46 deletions(-) diff --git a/index.php b/index.php index 9cd863c..783da99 100755 --- a/index.php +++ b/index.php @@ -210,63 +210,89 @@ function processButtons() { die(); } - if (isset($_POST["button_add"]) || isset($_POST["button_consume"])) { - if (isset($_POST["button_consume"])) { - $isConsume = true; - $id = $_POST["button_consume"]; - } else { - $isConsume = false; - $id = $_POST["button_add"]; + if (isset($_POST["button_add_all"]) || isset($_POST["button_consume_all"])) { + $isConsume = isset($_POST["button_consume_all"]); + $selectValues = array_filter($_POST, function($key, $value) { + return strpos($value, 'select_') !== false; + }, ARRAY_FILTER_USE_BOTH); + + foreach ($selectValues as $key => $value) { + preg_match('/select_(\d*)/', $key, $matches); + + if (count($matches) !== 2) { + continue; + } + + $id = $matches[1]; + $gidSelected = $value; + + addOrConsume($isConsume, $id, $gidSelected, $db); } + + //Hide POST, so we can refresh + header("Location: " . $CONFIG->getPhpSelfWithBaseUrl()); + die(); + } + + if (isset($_POST["button_add"]) || isset($_POST["button_consume"])) { + $isConsume = isset($_POST["button_consume"]); + $id = $isConsume ? $_POST["button_consume"] : $_POST["button_add"]; + checkIfNumeric($id); $gidSelected = $_POST["select_" . $id]; - if ($gidSelected != 0) { - $row = $db->getBarcodeById($id); - if ($row !== false) { - $barcode = sanitizeString($row["barcode"], true); - $amount = $row["amount"]; - checkIfNumeric($amount); - if (isset($_POST["tags"])) { - foreach ($_POST["tags"][$id] as $tag) { - TagManager::add(sanitizeString($tag), $gidSelected); - } + + addOrConsume($isConsume, $id, $gidSelected, $db); + + //Hide POST, so we can refresh + header("Location: " . $CONFIG->getPhpSelfWithBaseUrl()); + die(); + } +} + +function addOrConsume($isConsume, $id, $gidSelected, $db) { + if ($gidSelected != 0) { + $row = $db->getBarcodeById($id); + if ($row !== false) { + $barcode = sanitizeString($row["barcode"], true); + $amount = $row["amount"]; + checkIfNumeric($amount); + if (isset($_POST["tags"])) { + foreach ($_POST["tags"][$id] as $tag) { + TagManager::add(sanitizeString($tag), $gidSelected); } - $product = API::getProductInfo(sanitizeString($gidSelected)); - API::addBarcode($gidSelected, $barcode); - $log = new LogOutput("Associated barcode $barcode with " . $product->name, EVENT_TYPE_ASSOCIATE_PRODUCT); + } + $product = API::getProductInfo(sanitizeString($gidSelected)); + API::addBarcode($gidSelected, $barcode); + $log = new LogOutput("Associated barcode $barcode with " . $product->name, EVENT_TYPE_ASSOCIATE_PRODUCT); + $log->setVerbose()->dontSendWebsocket()->createLog(); + $db->deleteBarcode($id); + QuantityManager::syncBarcodeToGrocy($barcode); + if ($product->isTare) { + if (!$db->isUnknownBarcodeAlreadyStored($barcode)) + $db->insertActionRequiredBarcode($barcode, $row["bestBeforeInDays"], $row["price"]); + $log = new LogOutput("Action required: Enter weight for " . $product->name, EVENT_TYPE_ACTION_REQUIRED, $barcode); $log->setVerbose()->dontSendWebsocket()->createLog(); - $db->deleteBarcode($id); - QuantityManager::syncBarcodeToGrocy($barcode); - if ($product->isTare) { - if (!$db->isUnknownBarcodeAlreadyStored($barcode)) - $db->insertActionRequiredBarcode($barcode, $row["bestBeforeInDays"], $row["price"]); - $log = new LogOutput("Action required: Enter weight for " . $product->name, EVENT_TYPE_ACTION_REQUIRED, $barcode); + } else { + if ($isConsume) { + if ($product->stockAmount < $amount) + $amount = $product->stockAmount; + if ($amount > 0) { + API::consumeProduct($gidSelected, $amount); + $log = new LogOutput("Consuming $amount " . $product->unit . " of " . $product->name, EVENT_TYPE_ADD_KNOWN_BARCODE); + } else { + $log = new LogOutput("None in stock, not consuming: " . $product->name, EVENT_TYPE_ADD_KNOWN_BARCODE); + } $log->setVerbose()->dontSendWebsocket()->createLog(); } else { - if ($isConsume) { - if ($product->stockAmount < $amount) - $amount = $product->stockAmount; - if ($amount > 0) { - API::consumeProduct($gidSelected, $amount); - $log = new LogOutput("Consuming $amount " . $product->unit . " of " . $product->name, EVENT_TYPE_ADD_KNOWN_BARCODE); - } else { - $log = new LogOutput("None in stock, not consuming: " . $product->name, EVENT_TYPE_ADD_KNOWN_BARCODE); - } - $log->setVerbose()->dontSendWebsocket()->createLog(); - } else { - $additionalLog = ""; - if (!API::purchaseProduct($gidSelected, $amount, $row["bestBeforeInDays"], $row["price"])) { - $additionalLog = " [WARNING]: No default best before date set!"; - } - $log = new LogOutput("Adding $amount " . $product->unit . " of " . $product->name . $additionalLog, EVENT_TYPE_ADD_KNOWN_BARCODE); - $log->setVerbose()->dontSendWebsocket()->createLog(); + $additionalLog = ""; + if (!API::purchaseProduct($gidSelected, $amount, $row["bestBeforeInDays"], $row["price"])) { + $additionalLog = " [WARNING]: No default best before date set!"; } + $log = new LogOutput("Adding $amount " . $product->unit . " of " . $product->name . $additionalLog, EVENT_TYPE_ADD_KNOWN_BARCODE); + $log->setVerbose()->dontSendWebsocket()->createLog(); } } } - //Hide POST, so we can refresh - header("Location: " . $CONFIG->getPhpSelfWithBaseUrl()); - die(); } } @@ -418,6 +444,18 @@ function getHtmlMainMenuTableUnknown(array $barcodes): string { $html->addHtml("No unknown barcodes yet."); return $html->getHtml(); } else { + $html->addHtml($html->buildButton("button_add_all", "Add all") + ->setSubmit() + ->setRaised() + ->setIsAccent() + ->setId('button_add_all') + ->generate(true)); + $html->addHtml($html->buildButton("button_consume_all", "Consume all") + ->setSubmit() + ->setRaised() + ->setIsAccent() + ->setId('button_consume_all') + ->generate(true)); $table = new TableGenerator(array( "Barcode", "Look up", From 585eda4317d0d0dd62b80a5ae897e2d4e7845bd1 Mon Sep 17 00:00:00 2001 From: philippch Date: Sun, 4 Apr 2021 23:03:44 +0200 Subject: [PATCH 2/4] Add modify all buttons to known barcodes card. --- index.php | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/index.php b/index.php index 783da99..5c11fe6 100755 --- a/index.php +++ b/index.php @@ -372,6 +372,8 @@ function getHtmlMainMenuTableKnown(array $barcodes): string { if ($containsFederationName) array_splice($arrayTableEntries, 1, 0, array("Federation")); + addModifyAllButtons($html); + $table = new TableGenerator($arrayTableEntries); foreach ($barcodes['known'] as $item) { $isDisabled = "disabled"; @@ -444,18 +446,7 @@ function getHtmlMainMenuTableUnknown(array $barcodes): string { $html->addHtml("No unknown barcodes yet."); return $html->getHtml(); } else { - $html->addHtml($html->buildButton("button_add_all", "Add all") - ->setSubmit() - ->setRaised() - ->setIsAccent() - ->setId('button_add_all') - ->generate(true)); - $html->addHtml($html->buildButton("button_consume_all", "Consume all") - ->setSubmit() - ->setRaised() - ->setIsAccent() - ->setId('button_consume_all') - ->generate(true)); + addModifyAllButtons($html); $table = new TableGenerator(array( "Barcode", "Look up", @@ -504,6 +495,24 @@ function getHtmlMainMenuTableUnknown(array $barcodes): string { } +function addModifyAllButtons($html) { + $html->addHtml('
'); + $html->addHtml($html->buildButton("button_add_all", "Add all") + ->setSubmit() + ->setRaised() + ->setIsAccent() + ->setId('button_add_all') + ->generate(true)); + $html->addHtml($html->buildButton("button_consume_all", "Consume all") + ->setSubmit() + ->setRaised() + ->setIsAccent() + ->setId('button_consume_all') + ->generate(true)); + $html->addHtml('
'); +} + + /** * Outputs stored logs to the textarea * @return string From 83bc8d872711b8c41b96db8785935178b1f5e180 Mon Sep 17 00:00:00 2001 From: Philipp Christoph Date: Tue, 6 Apr 2021 15:02:11 +0200 Subject: [PATCH 3/4] Only add products with selected grocy ids. --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 5c11fe6..bb90478 100755 --- a/index.php +++ b/index.php @@ -212,8 +212,8 @@ function processButtons() { if (isset($_POST["button_add_all"]) || isset($_POST["button_consume_all"])) { $isConsume = isset($_POST["button_consume_all"]); - $selectValues = array_filter($_POST, function($key, $value) { - return strpos($value, 'select_') !== false; + $selectValues = array_filter($_POST, function($value, $key) { + return strpos($key, 'select_') !== false && $value !== 0; }, ARRAY_FILTER_USE_BOTH); foreach ($selectValues as $key => $value) { From c8630b8e0e069217da514b7699c8c51d99424f4e Mon Sep 17 00:00:00 2001 From: Philipp Christoph Date: Wed, 7 Apr 2021 10:08:28 +0200 Subject: [PATCH 4/4] Fix psalm. --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index bb90478..1406c96 100755 --- a/index.php +++ b/index.php @@ -212,7 +212,7 @@ function processButtons() { if (isset($_POST["button_add_all"]) || isset($_POST["button_consume_all"])) { $isConsume = isset($_POST["button_consume_all"]); - $selectValues = array_filter($_POST, function($value, $key) { + $selectValues = array_filter($_POST, function(int $value, string $key) { return strpos($key, 'select_') !== false && $value !== 0; }, ARRAY_FILTER_USE_BOTH);