Support transitive QU conversions on the frontend (references #2056, closes #1360)

This commit is contained in:
Bernd Bestel 2022-12-04 13:12:01 +01:00
parent df39f94fca
commit daa0a59c5f
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 42 additions and 23 deletions

View File

@ -151,20 +151,41 @@ AS (
AND c.path LIKE ('%/' || s.from_qu_id || '/' || s.to_qu_id || '/%')
)
SELECT DISTINCT
-1 AS id, -- Dummy, LessQL needs an id column
c.product_id,
c.from_qu_id,
qu_from.name AS from_qu_name,
qu_from.name_plural AS from_qu_name_plural,
c.to_qu_id,
qu_to.name AS to_qu_name,
qu_to.name_plural AS to_qu_name_plural,
FIRST_VALUE(factor) OVER (PARTITION BY product_id, from_qu_id, to_qu_id ORDER BY depth ASC) AS factor,
c.path AS source
FROM closure c
JOIN quantity_units qu_from
ON c.from_qu_id = qu_from.id
JOIN quantity_units qu_to
ON c.to_qu_id = qu_to.id
ORDER BY product_id, from_qu_id, to_qu_id;
SELECT *
FROM (
SELECT DISTINCT
-1 AS id, -- Dummy, LessQL needs an id column
c.product_id,
c.from_qu_id,
qu_from.name AS from_qu_name,
qu_from.name_plural AS from_qu_name_plural,
c.to_qu_id,
qu_to.name AS to_qu_name,
qu_to.name_plural AS to_qu_name_plural,
FIRST_VALUE(factor) OVER (PARTITION BY product_id, from_qu_id, to_qu_id ORDER BY depth ASC) AS factor,
c.path AS source
FROM closure c
JOIN quantity_units qu_from
ON c.from_qu_id = qu_from.id
JOIN quantity_units qu_to
ON c.to_qu_id = qu_to.id
UNION
-- Also return a conversion from/to the products stock QU (with a factor of 1)
SELECT
-1 AS id, -- Dummy, LessQL needs an id column
p.id AS product_id,
p.qu_id_stock AS from_qu_id,
qu.name AS from_qu_name,
qu.name_plural AS from_qu_name_plural,
p.qu_id_stock AS to_qu_id,
qu.name AS to_qu_name,
qu.name_plural AS to_qu_name_plural,
1.0 AS factor,
'/' || p.qu_id_stock AS source
FROM products p
JOIN quantity_units qu
ON p.qu_id_stock = qu.id
) x
ORDER BY x.product_id, x.from_qu_id, x.to_qu_id;

View File

@ -14,12 +14,10 @@ Grocy.Components.ProductAmountPicker.Reload = function(productId, destinationQuI
conversionsForProduct.forEach(conversion =>
{
var factor = parseFloat(conversion.factor);
if (conversion.to_qu_id == destinationQuId)
{
factor = 1;
}
if (!$('#qu_id option[value="' + conversion.to_qu_id + '"]').length) // Don't add the destination QU multiple times
// Only conversions related to the destination QU are needed
// + only add one conversion per to_qu_id (multiple ones can be a result of contradictory definitions = user input bullshit)
if (conversion.from_qu_id == destinationQuId && !$('#qu_id option[value="' + conversion.to_qu_id + '"]').length)
{
$("#qu_id").append('<option value="' + conversion.to_qu_id + '" data-qu-factor="' + factor + '" data-qu-name-plural="' + conversion.to_qu_name_plural + '">' + conversion.to_qu_name + '</option>');
}
@ -33,7 +31,7 @@ Grocy.Components.ProductAmountPicker.Reload = function(productId, destinationQuI
if (!Grocy.Components.ProductAmountPicker.InitialValueSet)
{
var convertedAmount = $("#display_amount").val() * $("#qu_id option:selected").attr("data-qu-factor");
var convertedAmount = ($("#display_amount").val() * $("#qu_id option:selected").attr("data-qu-factor")).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts });
$("#display_amount").val(convertedAmount);
Grocy.Components.ProductAmountPicker.InitialValueSet = true;