$('#save-consume-button').on('click', function(e) {
e.preventDefault();
if ($(".combobox-menu-visible").length) {
return;
}
var jsonForm = $('#consume-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("consume-form");
var apiUrl = 'stock/products/' + jsonForm.product_id + '/consume';
var jsonData = {};
jsonData.amount = jsonForm.amount;
jsonData.exact_amount = $('#consume-exact-amount').is(':checked');
jsonData.spoiled = $('#spoiled').is(':checked');
jsonData.allow_subproduct_substitution = true;
if ($("#use_specific_stock_entry").is(":checked")) {
jsonData.stock_entry_id = jsonForm.specific_stock_entry;
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) {
jsonData.location_id = $("#location_id").val();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES && Grocy.Components.RecipePicker.GetValue().toString().length > 0) {
jsonData.recipe_id = Grocy.Components.RecipePicker.GetValue();
}
var bookingResponse = null;
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(productDetails) {
Grocy.Api.Post(apiUrl, jsonData,
function(result) {
if (BoolVal(Grocy.UserSettings.scan_mode_consume_enabled)) {
Grocy.UISound.Success();
}
bookingResponse = result;
if (GetUriParam("flow") === "InplaceAddBarcodeToExistingProduct") {
var jsonDataBarcode = {};
jsonDataBarcode.barcode = GetUriParam("barcode");
jsonDataBarcode.product_id = jsonForm.product_id;
Grocy.Api.Post('objects/product_barcodes', jsonDataBarcode,
function(result) {
$("#flow-info-InplaceAddBarcodeToExistingProduct").addClass("d-none");
$('#barcode-lookup-disabled-hint').addClass('d-none');
$('#barcode-lookup-hint').removeClass('d-none');
window.history.replaceState({}, document.title, U("/consume"));
},
function(xhr) {
Grocy.FrontendHelpers.EndUiBusy("consume-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
}
);
}
$("#specific_stock_entry").find("option").remove().end().append("");
if ($("#use_specific_stock_entry").is(":checked")) {
$("#use_specific_stock_entry").click();
}
if (productDetails.product.enable_tare_weight_handling == 1 && !jsonData.exact_amount) {
var successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount - (parseFloat(productDetails.product.tare_weight) + parseFloat(productDetails.stock_amount))) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural, true), productDetails.product.name) + '
' + __t("Undo") + '';
} else {
var successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural, true), productDetails.product.name) + '
' + __t("Undo") + '';
}
if (GetUriParam("embedded") !== undefined) {
window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
} else {
Grocy.FrontendHelpers.EndUiBusy("consume-form");
toastr.success(successMessage);
Grocy.Components.ProductPicker.FinishFlow();
Grocy.Components.ProductAmountPicker.Reset();
$("#display_amount").attr("min", Grocy.DefaultMinAmount);
$("#display_amount").removeAttr("max");
if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) {
$('#display_amount').val(productDetails.product.quick_consume_amount);
} else {
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount));
}
RefreshLocaleNumberInput();
$(".input-group-productamountpicker").trigger("change");
$("#tare-weight-handling-info").addClass("d-none");
Grocy.Components.ProductPicker.Clear();
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_RECIPES) {
Grocy.Components.RecipePicker.Clear();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING) {
$("#location_id").find("option").remove().end().append("");
}
Grocy.Components.ProductPicker.Focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
Grocy.FrontendHelpers.ValidateForm('consume-form');
$("#consume-exact-amount-group").addClass("d-none");
}
},
function(xhr) {
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
Grocy.FrontendHelpers.EndUiBusy("consume-form");
console.error(xhr);
}
);
},
function(xhr) {
Grocy.FrontendHelpers.EndUiBusy("consume-form");
console.error(xhr);
}
);
});
$('#save-mark-as-open-button').on('click', function(e) {
e.preventDefault();
if ($(".combobox-menu-visible").length) {
return;
}
var jsonForm = $('#consume-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("consume-form");
var apiUrl = 'stock/products/' + jsonForm.product_id + '/open';
jsonData = {};
jsonData.amount = jsonForm.amount;
jsonData.allow_subproduct_substitution = true;
if ($("#use_specific_stock_entry").is(":checked")) {
jsonData.stock_entry_id = jsonForm.specific_stock_entry;
}
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(productDetails) {
Grocy.Api.Post(apiUrl, jsonData,
function(result) {
$("#specific_stock_entry").find("option").remove().end().append("");
if ($("#use_specific_stock_entry").is(":checked")) {
$("#use_specific_stock_entry").click();
}
Grocy.FrontendHelpers.EndUiBusy("consume-form");
toastr.success(__t('Marked %1$s of %2$s as opened', parseFloat(jsonForm.amount).toLocaleString({
minimumFractionDigits: 0,
maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts
}) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural, true), productDetails.product.name) + '
' + __t("Undo") + '');
if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) {
$('#display_amount').val(productDetails.product.quick_consume_amount);
} else {
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount));
}
RefreshLocaleNumberInput();
$(".input-group-productamountpicker").trigger("change");
Grocy.Components.ProductPicker.Clear();
Grocy.Components.ProductPicker.Focus();
Grocy.FrontendHelpers.ValidateForm('consume-form');
},
function(xhr) {
Grocy.FrontendHelpers.EndUiBusy("consume-form");
console.error(xhr);
}
);
},
function(xhr) {
Grocy.FrontendHelpers.EndUiBusy("consume-form");
console.error(xhr);
}
);
});
var sumValue = 0;
$("#location_id").on('change', function(e) {
var locationId = $(e.target).val();
$("#specific_stock_entry").find("option").remove().end().append("");
if ($("#use_specific_stock_entry").is(":checked")) {
$("#use_specific_stock_entry").trigger('click');
}
if (GetUriParam("embedded") !== undefined) {
OnLocationChange(locationId, GetUriParam('stockId'));
} else {
// try to get stock id from grocycode
if (Grocy.Components.ProductPicker.GetState('grocycode')) {
var gc = Grocy.Components.ProductPicker.GetState('barcode').split(':');
if (gc.length == 4) {
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries?query%5B%5D=stock_id%3D' + gc[3],
function(stockEntries) {
OnLocationChange(stockEntries[0].location_id, gc[3]);
$('#display_amount').val(stockEntries[0].amount);
},
function(xhr) {
console.error(xhr);
}
);
}
} else {
OnLocationChange(locationId, null);
}
}
});
function OnLocationChange(locationId, stockId) {
sumValue = 0;
if (locationId) {
if ($("#location_id").val() != locationId) {
$("#location_id").val(locationId);
}
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries?include_sub_products=true',
function(stockEntries) {
stockEntries.forEach(stockEntry => {
var openTxt = __t("Not opened");
if (stockEntry.open == 1) {
openTxt = __t("Opened");
}
if (stockEntry.location_id == locationId) {
if ($("#specific_stock_entry option[value='" + stockEntry.stock_id + "']").length == 0) {
$("#specific_stock_entry").append($("");
if ($("#use_specific_stock_entry").is(":checked")) {
$("#use_specific_stock_entry").click();
}
$("#location_id").val("");
var productId = $(e.target).val();
if (productId) {
Grocy.Components.ProductCard.Refresh(productId);
Grocy.Api.Get('stock/products/' + productId,
function(productDetails) {
current_productDetails = productDetails;
Grocy.Components.ProductAmountPicker.Reload(productDetails.product.id, productDetails.quantity_unit_stock.id);
Grocy.Components.ProductAmountPicker.SetQuantityUnit(productDetails.quantity_unit_stock.id);
if (BoolVal(Grocy.UserSettings.stock_default_consume_amount_use_quick_consume_amount)) {
$('#display_amount').val(productDetails.product.quick_consume_amount);
} else {
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_consume_amount));
}
RefreshLocaleNumberInput();
$(".input-group-productamountpicker").trigger("change");
$("#location_id").find("option").remove().end().append("");
Grocy.Api.Get("stock/products/" + productId + '/locations?include_sub_products=true',
function(stockLocations) {
var setDefault = 0;
stockLocations.forEach(stockLocation => {
if (productDetails.location.id == stockLocation.location_id) {
$("#location_id").append($("