From 3b564294e34cbba481a9eba24c40803a1ee5b9ba Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 24 Aug 2020 20:25:50 +0200 Subject: [PATCH] Allow decimal amounts in general for the shopping list --- changelog/60_UNRELEASED_2020-xx-xx.md | 1 + public/viewjs/shoppinglistitemform.js | 15 ++------------- services/RecipesService.php | 4 ++-- services/StockService.php | 2 +- views/shoppinglistitemform.blade.php | 5 +++-- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/changelog/60_UNRELEASED_2020-xx-xx.md b/changelog/60_UNRELEASED_2020-xx-xx.md index 0fa9ff07..a19f7a65 100644 --- a/changelog/60_UNRELEASED_2020-xx-xx.md +++ b/changelog/60_UNRELEASED_2020-xx-xx.md @@ -30,6 +30,7 @@ ### Shopping list improvements +- Decimal amounts are now allowed (for any product, rounded by two decimal places) - "Add products that are below defined min. stock amount" always rounded up the missing amount to an integral number, this now allows decimal numbers ### Recipe improvements/fixes diff --git a/public/viewjs/shoppinglistitemform.js b/public/viewjs/shoppinglistitemform.js index 9f2f0656..c8868109 100644 --- a/public/viewjs/shoppinglistitemform.js +++ b/public/viewjs/shoppinglistitemform.js @@ -116,19 +116,6 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e) function (productDetails) { $('#amount_qu_unit').text(productDetails.quantity_unit_purchase.name); - - if (productDetails.product.allow_partial_units_in_stock == 1) - { - $("#amount").attr("min", "0.01"); - $("#amount").attr("step", "0.01"); - $("#amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', 0.01.toLocaleString())); - } - else - { - $("#amount").attr("min", "1"); - $("#amount").attr("step", "1"); - $("#amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', '1')); - } $('#amount').focus(); Grocy.FrontendHelpers.ValidateForm('shoppinglist-form'); @@ -194,3 +181,5 @@ if (GetUriParam("amount") !== undefined) $("#amount").val(parseFloat(GetUriParam("amount")).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: 4 })); Grocy.FrontendHelpers.ValidateForm('shoppinglist-form'); } + +$("#amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', 0.01.toLocaleString())); diff --git a/services/RecipesService.php b/services/RecipesService.php index 0c1f9b52..18dfeaed 100644 --- a/services/RecipesService.php +++ b/services/RecipesService.php @@ -38,10 +38,10 @@ class RecipesService extends BaseService { $product = $this->getDataBase()->products($recipePosition->product_id); - $toOrderAmount = ceil(($recipePosition->missing_amount - $recipePosition->amount_on_shopping_list) / $product->qu_factor_purchase_to_stock); + $toOrderAmount = round(($recipePosition->missing_amount - $recipePosition->amount_on_shopping_list) / $product->qu_factor_purchase_to_stock, 2); if ($recipe->not_check_shoppinglist == 1) { - $toOrderAmount = ceil($recipePosition->missing_amount / $product->qu_factor_purchase_to_stock); + $toOrderAmount = round($recipePosition->missing_amount / $product->qu_factor_purchase_to_stock, 2); } if($toOrderAmount > 0) diff --git a/services/StockService.php b/services/StockService.php index e2c50fed..aba72d4d 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -905,7 +905,7 @@ class StockService extends BaseService if ($productRow != null && !empty($productRow)) { $newAmount = $productRow->amount - $amount; - if ($newAmount < 1) + if ($newAmount < 0.01) { $productRow->delete(); } diff --git a/views/shoppinglistitemform.blade.php b/views/shoppinglistitemform.blade.php index e6254f76..7d3fa722 100644 --- a/views/shoppinglistitemform.blade.php +++ b/views/shoppinglistitemform.blade.php @@ -51,9 +51,10 @@ 'id' => 'amount', 'label' => 'Amount', 'hintId' => 'amount_qu_unit', - 'min' => 0, + 'min' => 0.01, + 'step' => 0.01, 'value' => $value, - 'invalidFeedback' => $__t('The amount cannot be lower than %s', '1') + 'invalidFeedback' => $__t('The amount cannot be lower than %s', '0.01') ))