Fix consume page form validation limits for the amount input when tare weight handling is enabled (fixes #727)

This commit is contained in:
Bernd Bestel
2020-04-12 21:41:04 +02:00
parent b62d3b02e6
commit 855b24c515
2 changed files with 65 additions and 31 deletions

View File

@@ -14,6 +14,7 @@
- Fixed that best before dates were displayed on the stock overview and stock entries page even with disabled `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING` - Fixed that best before dates were displayed on the stock overview and stock entries page even with disabled `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING`
- Fixed that when editing a stock entry and setting a decimal amount, the decimal part was ignored (only possible when the product option "Allow partial units in stock" is enabled) - Fixed that when editing a stock entry and setting a decimal amount, the decimal part was ignored (only possible when the product option "Allow partial units in stock" is enabled)
- Fixed that "Default best before days" and "Default best before days after opened" on the product edit page were always shown regardless of the feature flags `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING` and `FEATURE_FLAG_STOCK_PRODUCT_OPENED_TRACKING` - Fixed that "Default best before days" and "Default best before days after opened" on the product edit page were always shown regardless of the feature flags `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING` and `FEATURE_FLAG_STOCK_PRODUCT_OPENED_TRACKING`
- Fixed that the form validation limits for the amount input and products with enabled tare weight handling were wrong
### Shopping list fixes ### Shopping list fixes
- Fixed that the "shopping list to stock workflow"-dialog was not visible in compact view - Fixed that the "shopping list to stock workflow"-dialog was not visible in compact view

View File

@@ -200,49 +200,82 @@ $("#location_id").on('change', function(e)
} }
if (locationId) if (locationId)
{ {
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries', Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries',
function(stockEntries) function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{ {
stockEntries.forEach(stockEntry => var openTxt = __t("Not opened");
if (stockEntry.open == 1)
{ {
var openTxt = __t("Not opened"); openTxt = __t("Opened");
if (stockEntry.open == 1) }
if (stockEntry.location_id == locationId)
{
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount: %1$s; Expires on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
}));
sumValue = sumValue + parseFloat(stockEntry.amount);
if (stockEntry.stock_id == stockId)
{ {
openTxt = __t("Opened"); $("#specific_stock_entry").val(stockId);
} }
}
});
if (stockEntry.location_id == locationId) Grocy.Api.Get('stock/products/' + Grocy.Components.ProductPicker.GetValue(),
function(productDetails)
{
if (productDetails.product.enable_tare_weight_handling == 1)
{ {
$("#specific_stock_entry").append($("<option>", { $("#amount").attr("min", productDetails.product.tare_weight);
value: stockEntry.stock_id, $('#amount').attr('max', sumValue + parseFloat(productDetails.product.tare_weight));
amount: stockEntry.amount, $("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', parseFloat(productDetails.product.tare_weight).toLocaleString(), (parseFloat(productDetails.stock_amount) + parseFloat(productDetails.product.tare_weight)).toLocaleString()));
text: __t("Amount: %1$s; Expires on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt $("#tare-weight-handling-info").removeClass("d-none");
})); }
sumValue = sumValue + parseFloat(stockEntry.amount); else
{
$("#tare-weight-handling-info").addClass("d-none");
if (stockEntry.stock_id == stockId) if (productDetails.product.allow_partial_units_in_stock == 1)
{ {
$("#specific_stock_entry").val(stockId); $("#amount").attr("min", "0.01");
$("#amount").attr("step", "0.01");
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', 0.01.toLocaleString(), parseFloat(productDetails.stock_amount).toLocaleString()));
}
else
{
$("#amount").attr("min", "1");
$("#amount").attr("step", "1");
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", parseFloat(productDetails.stock_amount).toLocaleString()));
}
$('#amount').attr('max', sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
} }
} }
}); },
$("#amount").attr("max", sumValue); function (xhr)
if (sumValue == 0)
{ {
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr); console.error(xhr);
} }
); );
} },
function(xhr)
{
console.error(xhr);
}
);
}
}); });
Grocy.Components.ProductPicker.GetPicker().on('change', function(e) Grocy.Components.ProductPicker.GetPicker().on('change', function(e)