$(document).ready(function() { if (GetUriParam("embedded") !== undefined) { var locationId = GetUriParam('locationId'); if (typeof locationId === 'undefined') { Grocy.Components.ProductPicker.GetPicker().trigger('change'); Grocy.Components.ProductPicker.GetInputElement().focus(); } else { $("#location_id").val(locationId); $("#location_id").trigger('change'); $("#use_specific_stock_entry").click(); $("#use_specific_stock_entry").trigger('change'); } } }); $('#save-consume-button').on('click', function(e) { e.preventDefault(); 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.spoiled = $('#spoiled').is(':checked'); 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(); } else { jsonData.location_id = 1; } 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) { bookingResponse = result; var addBarcode = GetUriParam('addbarcodetoselection'); if (addBarcode !== undefined) { var existingBarcodes = productDetails.product.barcode || ''; if (existingBarcodes.length === 0) { productDetails.product.barcode = addBarcode; } else { productDetails.product.barcode += ',' + addBarcode; } Grocy.Api.Put('objects/products/' + productDetails.product.id, productDetails.product, function(result) { $("#flow-info-addbarcodetoselection").addClass("d-none"); $('#barcode-lookup-disabled-hint').addClass('d-none'); window.history.replaceState({ }, document.title, U("/consume")); }, function(xhr) { console.error(xhr); } ); } $("#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) { var successMessage = __t('Removed %1$s of %2$s from stock', Math.abs(jsonForm.amount - parseFloat(productDetails.product.tare_weight)) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), 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), 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); $("#amount").attr("min", "1"); $("#amount").attr("max", "999999"); $("#amount").attr("step", "1"); $("#amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', '1')); $('#amount').val(Grocy.UserSettings.stock_default_consume_amount); $('#amount_qu_unit').text(""); $("#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.GetInputElement().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); } ); }); $('#save-mark-as-open-button').on('click', function(e) { e.preventDefault(); var jsonForm = $('#consume-form').serializeJSON(); Grocy.FrontendHelpers.BeginUiBusy("consume-form"); var apiUrl = 'stock/products/' + jsonForm.product_id + '/open'; jsonData = { }; jsonData.amount = jsonForm.amount; 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', jsonForm.amount + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '
' + __t("Undo") + ''); $('#amount').val(Grocy.UserSettings.stock_default_consume_amount); Grocy.Components.ProductPicker.Clear(); Grocy.Components.ProductPicker.GetInputElement().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); } ); }); $("#location_id").on('change', function(e) { var locationId = $(e.target).val(); var sumValue = 0; var stockId = null; $("#specific_stock_entry").find("option").remove().end().append(""); if ($("#use_specific_stock_entry").is(":checked")) { $("#use_specific_stock_entry").click(); } if (GetUriParam("embedded") !== undefined) { stockId = GetUriParam('stockId'); } if (locationId) { Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries', function(stockEntries) { stockEntries.forEach(stockEntry => { var openTxt = __t("Not opened"); if (stockEntry.open == 1) { openTxt = __t("Opened"); } if (stockEntry.location_id == locationId) { $("#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) { $('#amount_qu_unit').text(productDetails.quantity_unit_stock.name); $("#location_id").find("option").remove().end().append(""); Grocy.Api.Get("stock/products/" + productId + '/locations', function(stockLocations) { var setDefault = 0; stockLocations.forEach(stockLocation => { if (productDetails.location.id == stockLocation.location_id) { $("#location_id").append($("