From c6925ba4c3f6696af106168dd5188c7fb6605033 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 21 Apr 2017 15:36:04 +0200 Subject: [PATCH] Started working on shopping list feature --- GrocyDbMigrator.php | 12 +++++- GrocyLogicStock.php | 28 ++++++++++++ grocy.phpproj | 4 ++ index.php | 43 ++++++++++++++++++- views/consumption.php | 2 +- views/layout.php | 6 +++ views/shoppinglist.js | 38 +++++++++++++++++ views/shoppinglist.php | 45 ++++++++++++++++++++ views/shoppinglistform.js | 87 ++++++++++++++++++++++++++++++++++++++ views/shoppinglistform.php | 34 +++++++++++++++ 10 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 views/shoppinglist.js create mode 100644 views/shoppinglist.php create mode 100644 views/shoppinglistform.js create mode 100644 views/shoppinglistform.php diff --git a/GrocyDbMigrator.php b/GrocyDbMigrator.php index ea3142a2..6fe1d517 100644 --- a/GrocyDbMigrator.php +++ b/GrocyDbMigrator.php @@ -81,7 +81,7 @@ class GrocyDbMigrator ON p.id = s.product_id WHERE p.min_stock_amount != 0 GROUP BY p.id - HAVING SUM(s.amount) < p.min_stock_amount;" + HAVING IFNULL(SUM(s.amount), 0) < p.min_stock_amount;" ); self::ExecuteMigrationWhenNeeded($pdo, 8, " @@ -92,6 +92,16 @@ class GrocyDbMigrator GROUP BY product_id ORDER BY MIN(best_before_date) ASC;" ); + + self::ExecuteMigrationWhenNeeded($pdo, 9, " + CREATE TABLE shopping_list ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + product_id INTEGER NOT NULL UNIQUE, + amount INTEGER NOT NULL DEFAULT 0, + amount_autoadded INTEGER NOT NULL DEFAULT 0, + row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime')) + )" + ); } private static function ExecuteMigrationWhenNeeded(PDO $pdo, int $migrationId, string $sql) diff --git a/GrocyLogicStock.php b/GrocyLogicStock.php index 76fe93ba..4a00f3e0 100644 --- a/GrocyLogicStock.php +++ b/GrocyLogicStock.php @@ -160,4 +160,32 @@ class GrocyLogicStock return true; } + + public static function AddMissingProductsToShoppingList() + { + $db = Grocy::GetDbConnection(); + + $missingProducts = self::GetMissingProducts(); + foreach ($missingProducts as $missingProduct) + { + $product = $db->products()->where('id', $missingProduct->id)->fetch(); + $amount = ceil($missingProduct->amount_missing / $product->qu_factor_purchase_to_stock); + + $alreadyExistingEntry = $db->shopping_list()->where('product_id', $missingProduct->id)->fetch(); + if ($alreadyExistingEntry) //Update + { + $alreadyExistingEntry->update(array( + 'amount_autoadded' => $amount + )); + } + else //Insert + { + $shoppinglistRow = $db->shopping_list()->createRow(array( + 'product_id' => $missingProduct->id, + 'amount_autoadded' => $amount + )); + $shoppinglistRow->save(); + } + } + } } diff --git a/grocy.phpproj b/grocy.phpproj index 89063966..931beaa3 100644 --- a/grocy.phpproj +++ b/grocy.phpproj @@ -25,6 +25,8 @@ + + @@ -51,6 +53,8 @@ + + diff --git a/index.php b/index.php index 1eaba6b0..5275d027 100644 --- a/index.php +++ b/index.php @@ -37,7 +37,7 @@ $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', @@ -74,6 +74,18 @@ $app->get('/inventory', function(Request $request, Response $response) use($db) ]); }); +$app->get('/shoppinglist', function(Request $request, Response $response) use($db) +{ + return $this->renderer->render($response, '/layout.php', [ + 'title' => 'Shopping list', + 'contentPage' => 'shoppinglist.php', + 'listItems' => $db->shopping_list(), + 'products' => $db->products(), + 'quantityunits' => $db->quantity_units(), + 'missingProducts' => GrocyLogicStock::GetMissingProducts() + ]); +}); + $app->get('/products', function(Request $request, Response $response) use($db) { return $this->renderer->render($response, '/layout.php', [ @@ -170,6 +182,29 @@ $app->get('/quantityunit/{quantityunitId}', function(Request $request, Response } }); +$app->get('/shoppinglist/{itemId}', function(Request $request, Response $response, $args) use($db) +{ + if ($args['itemId'] == 'new') + { + return $this->renderer->render($response, '/layout.php', [ + 'title' => 'Add shopping list item', + 'contentPage' => 'shoppinglistform.php', + 'products' => $db->products(), + 'mode' => 'create' + ]); + } + else + { + return $this->renderer->render($response, '/layout.php', [ + 'title' => 'Edit shopping list item', + 'contentPage' => 'shoppinglistform.php', + 'listItem' => $db->shopping_list($args['itemId']), + 'products' => $db->products(), + 'mode' => 'edit' + ]); + } +}); + $app->group('/api', function() use($db) { $this->get('/get-objects/{entity}', function(Request $request, Response $response, $args) use($db) @@ -260,6 +295,12 @@ $app->group('/api', function() use($db) { echo json_encode(GrocyLogicStock::GetCurrentStock()); }); + + $this->get('/stock/add-missing-products-to-shoppinglist', function(Request $request, Response $response) + { + GrocyLogicStock::AddMissingProductsToShoppingList(); + echo json_encode(array('success' => true)); + }); })->add(function($request, $response, $next) { $response = $next($request, $response); diff --git a/views/consumption.php b/views/consumption.php index e5147f73..0953c638 100644 --- a/views/consumption.php +++ b/views/consumption.php @@ -6,7 +6,7 @@
- diff --git a/views/layout.php b/views/layout.php index fb482c7e..889c0759 100644 --- a/views/layout.php +++ b/views/layout.php @@ -52,6 +52,9 @@
  •  Inventory
  • +
  • +  Shopping list +