Test/review/rework (and hopefully finalized) new price handling

This commit is contained in:
Bernd Bestel
2020-11-10 18:11:33 +01:00
parent 33a6a28208
commit 68eeb07e5f
44 changed files with 461 additions and 495 deletions

View File

@@ -3,7 +3,6 @@
e.preventDefault();
var jsonForm = $('#purchase-form').serializeJSON();
jsonForm.qu_factor_purchase_to_stock = $("#qu_id option:selected").attr("data-qu-factor");
Grocy.FrontendHelpers.BeginUiBusy("purchase-form");
@@ -16,14 +15,14 @@
if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
jsonData.price = 0;
} else
}
else
{
// price is saved as 1 QU to stock
var price = parseFloat(jsonForm.price / jsonForm.qu_factor_purchase_to_stock).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
var price = parseFloat(jsonForm.price * $("#qu_id option:selected").attr("data-qu-factor")).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
if ($("input[name='price-type']:checked").val() == "total-price")
{
price = parseFloat(price / jsonForm.amount).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
price = parseFloat(price / jsonForm.display_amount).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
}
jsonData.price = price;
}
@@ -50,7 +49,6 @@
{
jsonData.location_id = Grocy.Components.LocationPicker.GetValue();
}
jsonData.qu_factor_purchase_to_stock = jsonForm.qu_factor_purchase_to_stock;
Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/add', jsonData,
function(result)
@@ -66,7 +64,6 @@
var jsonDataBarcode = {};
jsonDataBarcode.barcode = addBarcode;
jsonDataBarcode.product_id = jsonForm.product_id;
jsonDataBarcode.qu_factor_purchase_to_stock = jsonForm.qu_factor_purchase_to_stock;
jsonDataBarcode.shopping_location_id = jsonForm.shopping_location_id;
Grocy.Api.Post('objects/product_barcodes', jsonDataBarcode,
@@ -122,6 +119,12 @@
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
$('#price-hint').text("");
var priceTypeUnitPrice = $("#price-type-unit-price");
var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]");
priceTypeUnitPriceLabel.text(__t("Unit price"));
Grocy.FrontendHelpers.ValidateForm('purchase-form');
}
},
@@ -155,67 +158,17 @@ if (Grocy.Components.ProductPicker !== undefined)
{
Grocy.Components.ProductCard.Refresh(productId);
if (document.getElementById("product_id").getAttribute("barcode") != "null")
{
Grocy.Api.Get('productbarcodedetails/' + document.getElementById("product_id").getAttribute("barcode"),
function(resultBarcode)
{
if (resultBarcode != null)
{
$('#product_id').attr("barcode-qu-factor-purchase-to-stock", resultBarcode.qu_factor_purchase_to_stock);
$('#product_id').attr("barcode-shopping-location-id", resultBarcode.shopping_location_id);
}
else
{
$('#product_id').attr("barcode-qu-factor-purchase-to-stock", "null");
$('#product_id').attr("barcode-shopping-location-id", "null");
}
},
function(xhr)
{
console.error(xhr);
}
);
}
else
{
$('#product_id').attr("barcode-qu-factor-purchase-to-stock", "null");
$('#product_id').attr("barcode-shopping-location-id", "null");
}
Grocy.Api.Get('stock/products/' + productId,
function(productDetails)
{
Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id);
Grocy.Components.ProductAmountPicker.SetQuantityUnit(productDetails.quantity_unit_purchase.id);
var qu_factor_purchase_to_stock = null;
var barcode_shopping_location_id = null;
if (document.getElementById("product_id").getAttribute("barcode") != "null" && document.getElementById("product_id").getAttribute("barcode-qu-factor-purchase-to-stock") != "null")
{
qu_factor_purchase_to_stock = document.getElementById("product_id").getAttribute("barcode-qu-factor-purchase-to-stock");
barcode_shopping_location_id = document.getElementById("product_id").getAttribute("barcode-shopping-location-id");
}
else
{
if (productDetails.last_qu_factor_purchase_to_stock != null)
{
qu_factor_purchase_to_stock = productDetails.last_qu_factor_purchase_to_stock;
}
else
{
qu_factor_purchase_to_stock = productDetails.product.qu_factor_purchase_to_stock;
}
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
if (barcode_shopping_location_id != null)
{
Grocy.Components.ShoppingLocationPicker.SetId(barcode_shopping_location_id);
}
else if (productDetails.last_shopping_location_id != null)
if (productDetails.last_shopping_location_id != null)
{
Grocy.Components.ShoppingLocationPicker.SetId(productDetails.last_shopping_location_id);
}
@@ -230,11 +183,11 @@ if (Grocy.Components.ProductPicker !== undefined)
Grocy.Components.LocationPicker.SetId(productDetails.location.id);
}
$('#price').val(parseFloat(productDetails.last_price * qu_factor_purchase_to_stock).toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }));
$('#price').val(parseFloat(productDetails.last_price / $("#qu_id option:selected").attr("data-qu-factor")).toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }));
var priceTypeUnitPrice = $("#price-type-unit-price");
var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]");
priceTypeUnitPriceLabel.text(productDetails.quantity_unit_purchase.name + " price");
priceTypeUnitPriceLabel.text($("#qu_id option:selected").text() + " " + __t("price"));
refreshPriceHint();
@@ -253,7 +206,7 @@ if (Grocy.Components.ProductPicker !== undefined)
if (productDetails.product.enable_tare_weight_handling == 1)
{
var minAmount = parseFloat(productDetails.product.tare_weight) / qu_factor_purchase_to_stock + parseFloat(productDetails.stock_amount);
var minAmount = parseFloat(productDetails.product.tare_weight) / $("#qu_id option:selected").attr("data-qu-factor") + parseFloat(productDetails.stock_amount);
$("#display_amount").attr("min", minAmount);
$("#display_amount").attr("step", "." + "0".repeat(parseInt(Grocy.UserSettings.stock_decimal_places_amounts) - 1) + "1");
$("#display_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', minAmount.toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts })));
@@ -304,6 +257,40 @@ if (Grocy.Components.ProductPicker !== undefined)
Grocy.UISound.Error();
}
}
if (document.getElementById("product_id").getAttribute("barcode") != "null")
{
Grocy.Api.Get('objects/product_barcodes/search/?query[]=barcode=' + document.getElementById("product_id").getAttribute("barcode"),
function(barcodeResult)
{
if (barcodeResult != null)
{
var barcode = barcodeResult[0];
if (barcode.amount != null)
{
$("#display_amount").val(barcode.amount);
}
if (barcode.qu_id != null)
{
Grocy.Components.ProductAmountPicker.SetQuantityUnit(barcode.qu_id);
}
if (barcode.shopping_location_id != null)
{
Grocy.Components.ShoppingLocationPicker.SetId(barcode.shopping_location_id);
}
Grocy.FrontendHelpers.ValidateForm('purchase-form');
}
},
function(xhr)
{
console.error(xhr);
}
);
}
},
function(xhr)
{
@@ -409,12 +396,7 @@ if (GetUriParam("flow") === "shoppinglistitemtostock")
function refreshPriceHint()
{
if ($('#amount').val() == 0)
{
$('#price-hint').text("");
return;
}
if ($('#price').val() == 0)
if ($('#amount').val() == 0 || $('#price').val() == 0)
{
$('#price-hint').text("");
return;
@@ -422,29 +404,14 @@ function refreshPriceHint()
if ($("input[name='price-type']:checked").val() == "total-price")
{
var price = $('#price').val() / $("#qu_id option:selected").attr("data-qu-factor") / $('#amount').val();
var quprice = $('#price').val() / $('#amount').val();
var price = parseFloat($('#price').val() * $("#qu_id option:selected").attr("data-qu-factor")).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
price = parseFloat(price / $('#display_amount').val()).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
if ($("#qu_id option:selected").attr("data-qu-factor") > 1)
{
$('#price-hint').text(__t('means %1$s per %2$s and %3$s per %4$s', price.toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), $("#qu_id").attr("data-destination-qu-name"), quprice.toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), document.getElementById("amount_qu_unit").getAttribute("quantity-unit-purchase-name")));
}
else
{
$('#price-hint').text(__t('means %1$s per %2$s', price.toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), $("#qu_id").attr("data-destination-qu-name")));
}
$('#price-hint').text(__t('means %1$s per %2$s', price.toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), $("#qu_id").attr("data-destination-qu-name")));
}
else
{
if ($("#qu_id option:selected").attr("data-qu-factor") > 1)
{
var price = $('#price').val() / $("#qu_id option:selected").attr("data-qu-factor");
$('#price-hint').text(__t('means %1$s per %2$s', price.toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }), $("#qu_id").attr("data-destination-qu-name")));
}
else
{
$('#price-hint').text("");
}
$('#price-hint').text("");
}
};
@@ -520,3 +487,10 @@ $("#scan-mode-button").on("click", function(e)
$("#scan-mode-status").text(__t("off"));
}
});
$('#qu_id').on('change', function(e)
{
var priceTypeUnitPrice = $("#price-type-unit-price");
var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]");
priceTypeUnitPriceLabel.text($("#qu_id option:selected").text() + " " + __t("price"));
});