From cab34df2d6ec29621b6236ecb815ce9670d5332e Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Wed, 6 Apr 2022 21:27:47 +0200 Subject: [PATCH] Related the price on /inventory to the selected QU instead of QU stock (closes #1346) --- changelog/67_UNRELEASED_xxxx-xx-xx.md | 1 + public/viewjs/inventory.js | 51 +++++++++++++++++++++++++-- views/inventory.blade.php | 7 ++-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/changelog/67_UNRELEASED_xxxx-xx-xx.md b/changelog/67_UNRELEASED_xxxx-xx-xx.md index 17ef3b74..f554209f 100644 --- a/changelog/67_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/67_UNRELEASED_xxxx-xx-xx.md @@ -44,6 +44,7 @@ - The location content sheet can now optionally list also out of stock products (at the products default location, new checkbox "Show only in-stock products " at the top of the page, defaults to enabled) - Added a location filter to the stock entries page - Added the product grocycode as a (hidden by default) column to the products list (master data) +- The price entered on the inventory page is now related to the selected quantity unit (like on the purchase page, was always related to the products stock QU before) - Fixed that consuming via the consume page was not possible when `FEATURE_FLAG_STOCK_LOCATION_TRACKING` was disabled ### Shopping list diff --git a/public/viewjs/inventory.js b/public/viewjs/inventory.js index b834fdf4..0bbcd61c 100644 --- a/public/viewjs/inventory.js +++ b/public/viewjs/inventory.js @@ -1,4 +1,6 @@ -$('#save-inventory-button').on('click', function(e) +var CurrentProductDetails; + +$('#save-inventory-button').on('click', function(e) { e.preventDefault(); @@ -21,7 +23,7 @@ var price = ""; if (!jsonForm.price.toString().isEmpty()) { - price = parseFloat(jsonForm.price).toFixed(Grocy.UserSettings.stock_decimal_places_prices); + price = parseFloat(jsonForm.price * $("#qu_id option:selected").attr("data-qu-factor")).toFixed(Grocy.UserSettings.stock_decimal_places_prices); } var jsonData = {}; @@ -150,6 +152,7 @@ $(".input-group-productamountpicker").trigger("change"); $('#price').val(''); $('#note').val(''); + $('#price-hint').text(""); Grocy.Components.DateTimePicker.Clear(); Grocy.Components.ProductPicker.SetValue(''); if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING) @@ -202,6 +205,8 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) Grocy.Api.Get('stock/products/' + productId, function(productDetails) { + CurrentProductDetails = productDetails; + Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id); Grocy.Components.ProductAmountPicker.SetQuantityUnit(productDetails.quantity_unit_stock.id); @@ -219,7 +224,15 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) $("#tare-weight-handling-info").addClass("d-none"); } - $('#price').val(parseFloat(productDetails.last_price)); + if (productDetails.last_price != null && !productDetails.last_price.isEmpty()) + { + $('#price').val(parseFloat(productDetails.last_price / $("#qu_id option:selected").attr("data-qu-factor"))); + } + else + { + $('#price').val(""); + } + RefreshLocaleNumberInput(); if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING) { @@ -293,6 +306,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) $(".input-group-productamountpicker").trigger("change"); $('#display_amount').focus(); $('#display_amount').trigger('keyup'); + refreshPriceHint(); }, function(xhr) { @@ -302,6 +316,31 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) } }); +function refreshPriceHint() +{ + if ($('#amount').val() == 0 || $('#price').val() == 0) + { + $('#price-hint').text(""); + return; + } + + if ($("#qu_id").attr("data-destination-qu-name") != $("#qu_id option:selected").text()) + { + var amount = $('#display_amount').val(); + if (BoolVal(CurrentProductDetails.product.enable_tare_weight_handling)) + { + amount -= parseFloat(CurrentProductDetails.product.tare_weight); + } + + var price = parseFloat($('#price').val() * $("#qu_id option:selected").attr("data-qu-factor")).toFixed(Grocy.UserSettings.stock_decimal_places_prices); + $('#price-hint').text(__t('means %1$s per %2$s', price.toLocaleString(undefined, { style: "currency", currency: Grocy.Currency, minimumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), $("#qu_id").attr("data-destination-qu-name"))); + } + else + { + $('#price-hint').text(""); + } +}; + $('#display_amount').val(''); $(".input-group-productamountpicker").trigger("change"); Grocy.FrontendHelpers.ValidateForm('inventory-form'); @@ -371,6 +410,11 @@ Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e) Grocy.FrontendHelpers.ValidateForm('inventory-form'); }); +$('#price').on('focus', function(e) +{ + $(this).select(); +}); + $('#display_amount').on('keyup', function(e) { var productId = Grocy.Components.ProductPicker.GetValue(); @@ -426,6 +470,7 @@ $('#display_amount').on('keyup', function(e) Grocy.Components.DateTimePicker.GetInputElement().removeAttr('required'); } + refreshPriceHint(); Grocy.FrontendHelpers.ValidateForm('inventory-form'); }, function(xhr) diff --git a/views/inventory.blade.php b/views/inventory.blade.php index 32787d1e..a69ab26d 100644 --- a/views/inventory.blade.php +++ b/views/inventory.blade.php @@ -85,11 +85,8 @@ 'min' => '0.' . str_repeat('0', $userSettings['stock_decimal_places_prices']), 'decimals' => $userSettings['stock_decimal_places_prices'], 'value' => '', - 'hint' => $__t('Per stock quantity unit', GROCY_CURRENCY), - 'additionalHtmlContextHelp' => '', + 'contextInfoId' => 'price-hint', + 'hint' => $__t('This will apply to added products'), 'isRequired' => false, 'additionalCssClasses' => 'locale-number-input locale-number-currency' ))