From 52e311d8471eef09afb9cc1db220475b9655887c Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 21 Apr 2017 13:21:09 +0200 Subject: [PATCH] Show missing products on dashboard --- GrocyDbMigrator.php | 21 +++++++++++++++++++++ GrocyDemoDataGenerator.php | 4 ++-- GrocyLogicStock.php | 8 +++++++- index.php | 5 +++-- style.css | 4 ++++ views/dashboard.php | 3 ++- 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/GrocyDbMigrator.php b/GrocyDbMigrator.php index a9105a5a..ea3142a2 100644 --- a/GrocyDbMigrator.php +++ b/GrocyDbMigrator.php @@ -71,6 +71,27 @@ class GrocyDbMigrator INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct1', 'This is the first default product, edit or delete it', 1, 1, 1, 1); INSERT INTO products (name, description, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('DefaultProduct2', 'This is the second default product, edit or delete it', 1, 1, 1, 1);" ); + + self::ExecuteMigrationWhenNeeded($pdo, 7, " + CREATE VIEW stock_missing_products + AS + SELECT p.id, MAX(p.name) AS name, p.min_stock_amount - IFNULL(SUM(s.amount), 0) AS amount_missing + FROM products p + LEFT JOIN stock s + ON p.id = s.product_id + WHERE p.min_stock_amount != 0 + GROUP BY p.id + HAVING SUM(s.amount) < p.min_stock_amount;" + ); + + self::ExecuteMigrationWhenNeeded($pdo, 8, " + CREATE VIEW stock_current + AS + SELECT product_id, SUM(amount) AS amount, MIN(best_before_date) AS best_before_date + from stock + GROUP BY product_id + ORDER BY MIN(best_before_date) ASC;" + ); } private static function ExecuteMigrationWhenNeeded(PDO $pdo, int $migrationId, string $sql) diff --git a/GrocyDemoDataGenerator.php b/GrocyDemoDataGenerator.php index 26bd1f3c..cb9fb702 100644 --- a/GrocyDemoDataGenerator.php +++ b/GrocyDemoDataGenerator.php @@ -21,8 +21,8 @@ class GrocyDemoDataGenerator INSERT INTO quantity_units (name) VALUES ('Bund'); --6 DELETE FROM products WHERE id IN (1, 2); - INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Gummibärchen', 2, 2, 2, 1); --3 - INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Chips', 2, 2, 2, 1); --4 + INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock, min_stock_amount) VALUES ('Gummibärchen', 2, 2, 2, 1, 8); --3 + INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock, min_stock_amount) VALUES ('Chips', 2, 2, 2, 1, 10); --4 INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Eier', 1, 2, 1, 10); --5 INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Nudeln', 1, 2, 2, 1); --6 INSERT INTO products (name, location_id, qu_id_purchase, qu_id_stock, qu_factor_purchase_to_stock) VALUES ('Essiggurken', 3, 3, 3, 1); --7 diff --git a/GrocyLogicStock.php b/GrocyLogicStock.php index 48b6ead3..76fe93ba 100644 --- a/GrocyLogicStock.php +++ b/GrocyLogicStock.php @@ -8,7 +8,13 @@ class GrocyLogicStock public static function GetCurrentStock() { - $sql = 'SELECT product_id, SUM(amount) AS amount, MIN(best_before_date) AS best_before_date from stock GROUP BY product_id ORDER BY MIN(best_before_date) ASC'; + $sql = 'SELECT * from stock_current'; + return Grocy::ExecuteDbQuery(Grocy::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ); + } + + public static function GetMissingProducts() + { + $sql = 'SELECT * from stock_missing_products'; return Grocy::ExecuteDbQuery(Grocy::GetDbConnectionRaw(), $sql)->fetchAll(PDO::FETCH_OBJ); } diff --git a/index.php b/index.php index 4a9dcac6..1eaba6b0 100644 --- a/index.php +++ b/index.php @@ -37,12 +37,13 @@ $db = Grocy::GetDbConnection(); $app->get('/', function(Request $request, Response $response) use($db) { $db = Grocy::GetDbConnection(true); //For database schema migration - + return $this->renderer->render($response, '/layout.php', [ 'title' => 'Dashboard', 'contentPage' => 'dashboard.php', 'products' => $db->products(), - 'currentStock' => GrocyLogicStock::GetCurrentStock() + 'currentStock' => GrocyLogicStock::GetCurrentStock(), + 'missingProducts' => GrocyLogicStock::GetMissingProducts() ]); }); diff --git a/style.css b/style.css index 498edb04..7f587f4a 100644 --- a/style.css +++ b/style.css @@ -119,6 +119,10 @@ background-color: #f2dede !important; } +.info-bg { + background-color: #afd9ee !important; +} + .discrete-content-separator { padding-top: 5px; padding-bottom: 5px; diff --git a/views/dashboard.php b/views/dashboard.php index a126cf1c..af4831a3 100644 --- a/views/dashboard.php +++ b/views/dashboard.php @@ -8,6 +8,7 @@

products expiring within the next 5 days

products are already expired

+

products are below defined min. stock amount

@@ -24,7 +25,7 @@ - + product_id)->name; ?>