From 06968ac28998a6a63686d98a2b78665b2828f701 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 18 Aug 2022 21:29:19 +0200 Subject: [PATCH] Fixed stock entry Userfield edit handling (fixes #1969) --- changelog/69_UNRELEASED_xxxx-xx-xx.md | 1 + migrations/0196.sql | 21 +++++++++++++++++++++ public/viewjs/stockentryform.js | 1 + 3 files changed, 23 insertions(+) create mode 100644 migrations/0196.sql diff --git a/changelog/69_UNRELEASED_xxxx-xx-xx.md b/changelog/69_UNRELEASED_xxxx-xx-xx.md index ccb88b8c..1795d44f 100644 --- a/changelog/69_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/69_UNRELEASED_xxxx-xx-xx.md @@ -13,6 +13,7 @@ - Fixed that when the stock setting "Decimal places allowed for amounts" was set to `0`, unit conversion (if any) failed when adding the corresponding product to stock - Fixed that consuming a parent product which is not in stock itself (so essentially using any of the child products) may failed when unit conversions were involved (the current stock amount check was wrong in that case) - Fixed that the status button counters on the stock overview page ("X products are overdue" and so on) included products which have the option `Never show on stock overview` enabled +- Fixed that adding Userfields to existing stock entries was not possible (only editing existing Userfield values, e.g. added during purchase or inventory, was possible) ### Shopping list diff --git a/migrations/0196.sql b/migrations/0196.sql new file mode 100644 index 00000000..9db24abd --- /dev/null +++ b/migrations/0196.sql @@ -0,0 +1,21 @@ +DROP TRIGGER userfield_values_special_handling_INS; +CREATE TRIGGER userfield_values_special_handling_INS AFTER INSERT ON userfield_values +BEGIN + -- Entity stock: + -- object_id is the transaction_id on insert -> replace it by the corresponding stock_id + INSERT OR REPLACE INTO userfield_values + (field_id, object_id, value) + SELECT uv.field_id, sl.stock_id, uv.value + FROM userfield_values uv + JOIN stock_log sl + ON uv.object_id = sl.transaction_id + AND sl.transaction_type IN ('purchase', 'inventory-correction', 'stock-edit-new') + WHERE uv.field_id IN (SELECT id FROM userfields WHERE entity = 'stock') + AND uv.field_id = NEW.field_id + AND uv.object_id = NEW.object_id; + + DELETE FROM userfield_values + WHERE field_id IN (SELECT id FROM userfields WHERE entity = 'stock') + AND field_id = NEW.field_id + AND object_id = NEW.object_id; +END; diff --git a/public/viewjs/stockentryform.js b/public/viewjs/stockentryform.js index 273e4131..7cd3831b 100644 --- a/public/viewjs/stockentryform.js +++ b/public/viewjs/stockentryform.js @@ -44,6 +44,7 @@ Grocy.Api.Put("stock/entry/" + Grocy.EditObjectRowId, jsonData, function(result) { + Grocy.EditObjectId = result[0].transaction_id; Grocy.Components.UserfieldsForm.Save(function() { var successMessage = __t('Stock entry successfully updated') + '
' + __t("Undo") + '';