From b53d1a076fd34c011da52b8b797f7ca8b5ad8da2 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Fri, 1 Apr 2022 17:04:09 +0200 Subject: [PATCH] Reviewed latest changes regarding price handling views --- changelog/67_UNRELEASED_xxxx-xx-xx.md | 4 +++- grocy.openapi.json | 16 ++++++++++++++-- migrations/0179.sql | 10 +++++----- services/StockService.php | 4 +++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/changelog/67_UNRELEASED_xxxx-xx-xx.md b/changelog/67_UNRELEASED_xxxx-xx-xx.md index 8cf27d6b..8298b042 100644 --- a/changelog/67_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/67_UNRELEASED_xxxx-xx-xx.md @@ -90,4 +90,6 @@ - Added a new endpoint `GET /stock/locations/{locationId}/entries` to get all stock entries of a given location (similar to the already existing endpoint `GET /stock/products/{productId}/entries`) - Endpoint `/recipes/{recipeId}/consume`: Fixed that consuming partially fulfilled recipes was possible, although an error was already returned in that case (and potentially some of the in-stock ingredients were consumed in fact) -- Endpoint `/stock/products/{productId}`: The property/field `oldest_price` has been removed (as this had no real sense) +- Endpoint `/stock/products/{productId}`: + - New field/property `current_price` which returns the current price of the corresponding products based on the stock entry to use next defined by the default consume rule (Opened first, then first due first, then first in first out) + - The field/property `oldest_price` is deprecated and will be removed in a future version (this had no real sense, currently returns the same as `current_price`) diff --git a/grocy.openapi.json b/grocy.openapi.json index bc5dd46f..4e3f5885 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -4751,10 +4751,21 @@ "format": "date-time" }, "last_price": { - "type": "number" + "type": "number", + "description": "The price of the last purchase of the corresponding product" }, "avg_price": { - "type": "number" + "type": "number", + "description": "The average price af all items currently in stock of the corresponding product" + }, + "current_price": { + "type": "number", + "description": "The current price of the corresponding products, based on the stock entry to use next defined by the default consume rule (Opened first, then first due first, then first in first out)" + }, + "oldest_price": { + "type": "number", + "description": "This field is deprecated and will be removed in a future version (currently returns the same as `current_price`)", + "deprecated": true }, "last_shopping_location_id": { "type": "integer" @@ -4826,6 +4837,7 @@ }, "last_price": null, "avg_price": null, + "current_price": null, "last_shopping_location_id": null, "next_due_date": "2019-07-07", "location": { diff --git a/migrations/0179.sql b/migrations/0179.sql index 07efeaa1..34066db1 100644 --- a/migrations/0179.sql +++ b/migrations/0179.sql @@ -6,12 +6,12 @@ AS Opened first, then first due first, then first in first out This orders the stock entries by that - => Lowest "priority" per product = the stock entry to use next + => Highest "priority" per product = the stock entry to use next */ SELECT - -1, -- Dummy - ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY open DESC, best_before_date ASC, purchased_date ASC) AS priority, + -1 AS id, -- Dummy + (ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY open DESC, best_before_date ASC, purchased_date ASC)) * -1 AS priority, product_id, stock_id, price @@ -27,14 +27,14 @@ AS */ SELECT - -1, -- Dummy, + -1 AS id, -- Dummy, p.id AS product_id, IFNULL(snu.price, plp.price) AS price FROM products p LEFT JOIN ( SELECT product_id, - MIN(priority), + MAX(priority), price -- Bare column, ref https://www.sqlite.org/lang_select.html#bare_columns_in_an_aggregate_query FROM stock_next_use GROUP BY product_id diff --git a/services/StockService.php b/services/StockService.php index 568f0f5b..e5e47ab2 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -716,7 +716,7 @@ class StockService extends BaseService $quStock = $this->getDatabase()->quantity_units($product->qu_id_stock); $location = $this->getDatabase()->locations($product->location_id); $averageShelfLifeDays = intval($this->getDatabase()->stock_average_product_shelf_life()->where('id', $productId)->fetch()->average_shelf_life_days); - $defaultShoppingLocation = null; + $currentPrice = $this->getDatabase()->products_current_price()->where('product_id', $productId)->fetch()->price; $consumeCount = $this->getDatabase()->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0')->sum('amount') * -1; $consumeCountSpoiled = $this->getDatabase()->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->where('undone = 0 AND spoiled = 1')->sum('amount') * -1; @@ -740,6 +740,8 @@ class StockService extends BaseService 'quantity_unit_stock' => $quStock, 'last_price' => $lastPrice, 'avg_price' => $avgPrice, + 'oldest_price' => $currentPrice, // Deprecated + 'current_price' => $currentPrice, 'last_shopping_location_id' => $lastShoppingLocation, 'default_shopping_location_id' => $product->shopping_location_id, 'next_due_date' => $nextDueDate,