mirror of
https://github.com/grocy/grocy.git
synced 2025-08-19 20:11:16 +00:00
Finished qu unit conversion handling (closes #177)
This commit is contained in:
75
public/viewjs/components/productamountpicker.js
Normal file
75
public/viewjs/components/productamountpicker.js
Normal file
@@ -0,0 +1,75 @@
|
||||
Grocy.Components.ProductAmountPicker = {};
|
||||
Grocy.Components.ProductAmountPicker.AllowAnyQuEnabled = false;
|
||||
|
||||
Grocy.Components.ProductAmountPicker.Reload = function(productId, destinationQuId, forceInitialDisplayQu = false)
|
||||
{
|
||||
if (!Grocy.Components.ProductAmountPicker.AllowAnyQuEnabled)
|
||||
{
|
||||
var conversionsForProduct = FindAllObjectsInArrayByPropertyValue(Grocy.QuantityUnitConversionsResolved, 'product_id', productId);
|
||||
$("#qu_id").find("option").remove().end();
|
||||
$("#qu_id").attr("data-destination-qu-name", FindObjectInArrayByPropertyValue(Grocy.QuantityUnits, 'id', destinationQuId).name);
|
||||
conversionsForProduct.forEach(conversion =>
|
||||
{
|
||||
$("#qu_id").append('<option value="' + conversion.to_qu_id + '" data-qu-factor="' + conversion.factor + '">' + conversion.to_qu_name + '</option>');
|
||||
});
|
||||
}
|
||||
|
||||
if (!Grocy.Components.ProductAmountPicker.InitalValueSet || forceInitialDisplayQu)
|
||||
{
|
||||
$("#qu_id").val($("#qu_id").attr("data-inital-qu-id"));
|
||||
}
|
||||
|
||||
if (!Grocy.Components.ProductAmountPicker.InitalValueSet)
|
||||
{
|
||||
var convertedAmount = $("#display_amount").val() * $("#qu_id option:selected").attr("data-qu-factor");
|
||||
$("#display_amount").val(convertedAmount);
|
||||
|
||||
Grocy.Components.ProductAmountPicker.InitalValueSet = true;
|
||||
}
|
||||
|
||||
$(".input-group-productamountpicker").trigger("change");
|
||||
}
|
||||
|
||||
Grocy.Components.ProductAmountPicker.SetQuantityUnit = function()
|
||||
{
|
||||
$("#qu_id").val(quId);
|
||||
}
|
||||
|
||||
Grocy.Components.ProductAmountPicker.AllowAnyQu = function(quId)
|
||||
{
|
||||
Grocy.Components.ProductAmountPicker.AllowAnyQuEnabled = true;
|
||||
|
||||
$("#qu_id").find("option").remove().end();
|
||||
Grocy.QuantityUnits.forEach(qu =>
|
||||
{
|
||||
$("#qu_id").append('<option value="' + qu.id + '" data-qu-factor="1">' + qu.name + '</option>');
|
||||
});
|
||||
|
||||
$(".input-group-productamountpicker").trigger("change");
|
||||
}
|
||||
|
||||
$(".input-group-productamountpicker").on("change", function()
|
||||
{
|
||||
var destinationQuName = $("#qu_id").attr("data-destination-qu-name");
|
||||
var selectedQuName = $("#qu_id option:selected").text();
|
||||
var quFactor = $("#qu_id option:selected").attr("data-qu-factor");
|
||||
var amount = $("#display_amount").val();
|
||||
var destinationAmount = amount / quFactor;
|
||||
|
||||
if (destinationQuName == selectedQuName || Grocy.Components.ProductAmountPicker.AllowAnyQuEnabled)
|
||||
{
|
||||
$("#qu-conversion-info").addClass("d-none");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu-conversion-info").removeClass("d-none");
|
||||
$("#qu-conversion-info").text(__t("This equals %1$s %2$s in stock", destinationAmount.toLocaleString(), destinationQuName));
|
||||
}
|
||||
|
||||
$("#amount").val(destinationAmount);
|
||||
});
|
||||
|
||||
$("#display_amount").on("keyup", function()
|
||||
{
|
||||
$(".input-group-productamountpicker").trigger("change");
|
||||
});
|
@@ -55,7 +55,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);;
|
||||
}
|
||||
},
|
||||
function (xhr)
|
||||
@@ -73,7 +73,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -118,7 +118,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);;
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
@@ -136,7 +136,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -233,6 +233,29 @@ $('.input-group-qu').on('change', function(e)
|
||||
$('#product-form input').keyup(function(event)
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
|
||||
if (document.getElementById('product-form').checkValidity() === false) //There is at least one validation error
|
||||
{
|
||||
$("#qu-conversion-add-button").addClass("disabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu-conversion-add-button").removeClass("disabled");
|
||||
}
|
||||
});
|
||||
|
||||
$('#product-form select').change(function(event)
|
||||
{
|
||||
Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
|
||||
if (document.getElementById('product-form').checkValidity() === false) //There is at least one validation error
|
||||
{
|
||||
$("#qu-conversion-add-button").addClass("disabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu-conversion-add-button").removeClass("disabled");
|
||||
}
|
||||
});
|
||||
|
||||
$('#location_id').change(function(event)
|
||||
@@ -343,6 +366,7 @@ $('#qu-conversions-table tbody').removeClass("d-none");
|
||||
quConversionsTable.columns.adjust().draw();
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
||||
$("#name").trigger("keyup");
|
||||
$('#name').focus();
|
||||
$('.input-group-qu').trigger('change');
|
||||
Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
@@ -390,12 +414,12 @@ $(document).on('click', '.qu-conversion-delete-button', function(e)
|
||||
$(document).on('click', '.qu-conversion-edit-button', function (e)
|
||||
{
|
||||
var id = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/" + id.toString() + "?product=" + Grocy.EditObjectId.toString());
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/" + id.toString() + "?product=editobjectid");
|
||||
$('#save-product-button').click();
|
||||
});
|
||||
|
||||
$("#qu-conversion-add-button").on("click", function(e)
|
||||
{
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/new?product=" + Grocy.EditObjectId.toString());
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/new?product=editobjectid");
|
||||
$('#save-product-button').click();
|
||||
});
|
||||
|
@@ -5,6 +5,15 @@
|
||||
var jsonData = $('#quantityunit-form').serializeJSON();
|
||||
Grocy.FrontendHelpers.BeginUiBusy("quantityunit-form");
|
||||
|
||||
if (Grocy.QuantityUnitEditFormRedirectUri !== undefined)
|
||||
{
|
||||
redirectDestination = Grocy.QuantityUnitEditFormRedirectUri;
|
||||
}
|
||||
else
|
||||
{
|
||||
redirectDestination = U('/quantityunits');
|
||||
}
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.Api.Post('objects/quantity_units', jsonData,
|
||||
@@ -13,7 +22,14 @@
|
||||
Grocy.EditObjectId = result.created_object_id;
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
window.location.href = U('/quantityunits');
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);
|
||||
}
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
@@ -30,7 +46,14 @@
|
||||
{
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
window.location.href = U('/quantityunits');
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination.replace("editobjectid", Grocy.EditObjectId);
|
||||
}
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
@@ -44,6 +67,24 @@
|
||||
|
||||
$('#quantityunit-form input').keyup(function(event)
|
||||
{
|
||||
if (!$("#name").val().isEmpty())
|
||||
{
|
||||
$("#qu-conversion-headline-info").text(__t('1 %s is the same as...', $("#name").val()));
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu-conversion-headline-info").text("");
|
||||
}
|
||||
|
||||
if (document.getElementById('quantityunit-form').checkValidity() === false) //There is at least one validation error
|
||||
{
|
||||
$("#qu-conversion-add-button").addClass("disabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu-conversion-add-button").removeClass("disabled");
|
||||
}
|
||||
|
||||
Grocy.FrontendHelpers.ValidateForm('quantityunit-form');
|
||||
});
|
||||
|
||||
@@ -88,6 +129,7 @@ $('#qu-conversions-table tbody').removeClass("d-none");
|
||||
quConversionsTable.columns.adjust().draw();
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
||||
$("#name").trigger("keyup");
|
||||
$('#name').focus();
|
||||
Grocy.FrontendHelpers.ValidateForm('quantityunit-form');
|
||||
|
||||
@@ -114,7 +156,8 @@ $(document).on('click', '.qu-conversion-delete-button', function(e)
|
||||
Grocy.Api.Delete('objects/quantity_unit_conversions/' + objectId, { },
|
||||
function(result)
|
||||
{
|
||||
window.location.reload();
|
||||
Grocy.QuantityUnitEditFormRedirectUri = "reload";
|
||||
$('#save-quantityunit-button').click();
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
@@ -129,29 +172,12 @@ $(document).on('click', '.qu-conversion-delete-button', function(e)
|
||||
$(document).on('click', '.qu-conversion-edit-button', function (e)
|
||||
{
|
||||
var id = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
|
||||
Grocy.Api.Put('objects/quantity_units/' + Grocy.EditObjectId, $('#quantityunit-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = U("/quantityunitconversion/" + id.toString() + "?qu-unit=" + Grocy.EditObjectId.toString());
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
Grocy.QuantityUnitEditFormRedirectUri = U("/quantityunitconversion/" + id.toString() + "?qu-unit=editobjectid");
|
||||
$('#save-quantityunit-button').click();
|
||||
});
|
||||
|
||||
$("#qu-conversion-add-button").on("click", function(e)
|
||||
{
|
||||
Grocy.Api.Put('objects/quantity_units/' + Grocy.EditObjectId, $('#quantityunit-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = U("/quantityunitconversion/new?qu-unit=" + Grocy.EditObjectId.toString());
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
Grocy.QuantityUnitEditFormRedirectUri = U("/quantityunitconversion/new?qu-unit=editobjectid");
|
||||
$('#save-quantityunit-button').click();
|
||||
});
|
||||
|
@@ -1,9 +1,12 @@
|
||||
$('#save-recipe-pos-button').on('click', function(e)
|
||||
Grocy.RecipePosFormProductChangeCount = 0;
|
||||
|
||||
$('#save-recipe-pos-button').on('click', function (e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
var jsonData = $('#recipe-pos-form').serializeJSON({ checkboxUncheckedValue: "0" });
|
||||
jsonData.recipe_id = Grocy.EditObjectParentId;
|
||||
delete jsonData.display_amount;
|
||||
|
||||
Grocy.FrontendHelpers.BeginUiBusy("recipe-pos-form");
|
||||
|
||||
@@ -44,31 +47,37 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
|
||||
if (productId)
|
||||
{
|
||||
Grocy.Components.ProductCard.Refresh(productId);
|
||||
|
||||
|
||||
Grocy.Api.Get('stock/products/' + productId,
|
||||
function(productDetails)
|
||||
{
|
||||
if (!$("#only_check_single_unit_in_stock").is(":checked"))
|
||||
Grocy.RecipePosFormProductChangeCount++;
|
||||
console.log(Grocy.RecipePosFormProductChangeCount);
|
||||
if (Grocy.RecipePosFormProductChangeCount < 3) // This triggers twice on inital page load, however
|
||||
{
|
||||
$("#qu_id").val(productDetails.quantity_unit_stock.id);
|
||||
Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id);
|
||||
}
|
||||
|
||||
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()));
|
||||
$("#display_amount").attr("min", "0.01");
|
||||
$("#display_amount").attr("step", "0.01");
|
||||
$("#display_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'));
|
||||
$("#display_amount").attr("min", "1");
|
||||
$("#display_amount").attr("step", "1");
|
||||
$("#display_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', '1'));
|
||||
}
|
||||
|
||||
$("#not_check_stock_fulfillment").prop("checked", productDetails.product.not_check_stock_fulfillment_for_recipes == 1);
|
||||
|
||||
$('#amount').focus();
|
||||
$('#display_amount').focus();
|
||||
Grocy.FrontendHelpers.ValidateForm('recipe-pos-form');
|
||||
},
|
||||
function(xhr)
|
||||
@@ -87,7 +96,7 @@ if (Grocy.Components.ProductPicker.InProductAddWorkflow() === false)
|
||||
}
|
||||
Grocy.Components.ProductPicker.GetPicker().trigger('change');
|
||||
|
||||
$('#amount').on('focus', function(e)
|
||||
$('#display_amount').on('focus', function(e)
|
||||
{
|
||||
if (Grocy.Components.ProductPicker.GetValue().length === 0)
|
||||
{
|
||||
@@ -125,19 +134,19 @@ $("#only_check_single_unit_in_stock").on("click", function()
|
||||
{
|
||||
if (this.checked)
|
||||
{
|
||||
$("#qu_id").removeAttr("disabled");
|
||||
$("#amount").attr("min", "0.01");
|
||||
$("#amount").attr("step", "0.01");
|
||||
$("#amount").parent().find(".invalid-feedback").text(__t("This cannot be negative"));
|
||||
$("#display_amount").attr("min", "0.01");
|
||||
$("#display_amount").attr("step", "0.01");
|
||||
$("#display_amount").parent().find(".invalid-feedback").text(__t("This cannot be negative"));
|
||||
Grocy.Components.ProductAmountPicker.AllowAnyQu();
|
||||
Grocy.FrontendHelpers.ValidateForm("recipe-pos-form");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#qu_id").attr("disabled", "");
|
||||
$("#amount").attr("min", "0");
|
||||
$("#amount").attr("step", "1");
|
||||
$("#display_amount").attr("min", "0");
|
||||
$("#display_amount").attr("step", "1");
|
||||
Grocy.Components.ProductPicker.GetPicker().trigger("change"); // Selects the default quantity unit of the selected product
|
||||
$("#amount").parent().find(".invalid-feedback").text(__t("This cannot be negative and must be an integral number"));
|
||||
$("#display_amount").parent().find(".invalid-feedback").text(__t("This cannot be negative and must be an integral number"));
|
||||
Grocy.Components.ProductAmountPicker.AllowAnyQuEnabled = false;
|
||||
Grocy.FrontendHelpers.ValidateForm("recipe-pos-form");
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user