From c1952e98bc24f6326e0518f924c62db58051a458 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 6 Jan 2022 15:14:32 +0100 Subject: [PATCH] Show stock journal entries of deleted users / fixed default user handling (fixes #1725) --- changelog/66_UNRELEASED_xxxx-xx-xx.md | 1 + migrations/0157.sql | 79 +++++++++++++++++++++++++++ services/SessionService.php | 2 +- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 migrations/0157.sql diff --git a/changelog/66_UNRELEASED_xxxx-xx-xx.md b/changelog/66_UNRELEASED_xxxx-xx-xx.md index e8a3e90a..089b454b 100644 --- a/changelog/66_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/66_UNRELEASED_xxxx-xx-xx.md @@ -7,4 +7,5 @@ - Fixed that the barcode lookup for the "Stock by-barcode" API endpoints was case sensitive - Fixed that the logout button/menu was missing when using external authentication (e.g. LDAP) - Fixed that the consume page/dialog wasn't properly initialized when opening it from the stock entries page +- Fixed that entries for not existing users were missing on the stock journal - The API endpoint `/stock/shoppinglist/clear` has now a new optional request body parameter `done_only` (to only remove done items from the given shopping list, defaults to `false`) diff --git a/migrations/0157.sql b/migrations/0157.sql new file mode 100644 index 00000000..36dfc331 --- /dev/null +++ b/migrations/0157.sql @@ -0,0 +1,79 @@ +PRAGMA legacy_alter_table = ON; + +ALTER TABLE stock_log RENAME TO stock_log_old; + +-- user_id was introduced in migration 114 and set to 1 by default, +-- but this user could have been already deleted +-- => Change the user_id to the first existing user (which is the new defintion of "default user") for that case +UPDATE stock_log_old +SET user_id = (SELECT MIN(id) FROM users) +WHERE user_id NOT IN (SELECT id FROM users) + AND user_id = 1; + +CREATE TABLE stock_log ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + product_id INTEGER NOT NULL, + amount DECIMAL(15, 2) NOT NULL, + best_before_date DATE, + purchased_date DATE, + used_date DATE, + spoiled INTEGER NOT NULL DEFAULT 0, + stock_id TEXT NOT NULL, + transaction_type TEXT NOT NULL, + price DECIMAL(15, 2), + undone TINYINT NOT NULL DEFAULT 0 CHECK(undone IN (0, 1)), + undone_timestamp DATETIME, + opened_date DATETIME, + location_id INTEGER, + recipe_id INTEGER, + correlation_id TEXT, + transaction_id TEXT, + stock_row_id INTEGER, + shopping_location_id INTEGER, + user_id INTEGER NOT NULL, + row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime')) +); + +INSERT INTO stock_log + (product_id, amount, best_before_date, purchased_date, used_date, spoiled, stock_id, transaction_type, price, undone, undone_timestamp, opened_date, location_id, recipe_id, correlation_id, transaction_id, stock_row_id, shopping_location_id, user_id, row_created_timestamp) +SELECT product_id, amount, best_before_date, purchased_date, used_date, spoiled, stock_id, transaction_type, price, undone, undone_timestamp, opened_date, location_id, recipe_id, correlation_id, transaction_id, stock_row_id, shopping_location_id, user_id, row_created_timestamp +FROM stock_log_old; + +DROP TABLE stock_log_old; + +CREATE TRIGGER set_products_default_location_if_empty_stock_log AFTER INSERT ON stock_log +BEGIN + UPDATE stock_log + SET location_id = (SELECT location_id FROM products where id = product_id) + WHERE id = NEW.id + AND location_id IS NULL; +END; + +DROP VIEW uihelper_stock_journal; +CREATE VIEW uihelper_stock_journal +AS +SELECT + sl.id, + sl.row_created_timestamp, + sl.correlation_id, + sl.undone, + sl.undone_timestamp, + sl.transaction_type, + sl.spoiled, + sl.amount, + sl.location_id, + l.name AS location_name, + p.name AS product_name, + qu.name AS qu_name, + qu.name_plural AS qu_name_plural, + u.display_name AS user_display_name, + p.id AS product_id +FROM stock_log sl +LEFT JOIN users_dto u + ON sl.user_id = u.id +JOIN products p + ON sl.product_id = p.id +JOIN locations l + ON sl.location_id = l.id +JOIN quantity_units qu + ON p.qu_id_stock = qu.id; diff --git a/services/SessionService.php b/services/SessionService.php index 89013bbc..2c7c4b6b 100644 --- a/services/SessionService.php +++ b/services/SessionService.php @@ -32,7 +32,7 @@ class SessionService extends BaseService public function GetDefaultUser() { - return $this->getDatabase()->users(1); + return $this->getDatabase()->users()->orderBy('id')->limit(1)->fetch(); } public function GetUserBySessionKey($sessionKey)