Stock Service Updates (#421)

* viewjs consume: implement location and update stock specific

* Transfer Products

* services StockService#GetProductStockEntriesByLocation: add method

* services StockService#AddProduct: check for stock and locations

* services StockService: include location_id

* services StockService#LocationExists: add method

* services StockService#UndoBooking: fix based on stockRow

* Reimplement StockServer->TransferProduct (one loop for the whole action to preserve stock_id)

* Ensure that the location_id is never NULL in the stock and stock_log table (checked by an INSERT trigger, sets the products default location if empty)

* Only consider stock amount at the given location on consume, if supplied

* Restore more/old display text for "specific stock entry"

* Don't allow transfering tare weight enabled products

* Various small changes (code style, missing OpenAPI endpoint, remove location_id null checking)

* Updated translations strings

* Added transaction_id and correlation_id to stock_log entries to group them together

* ProductCard - location to default location label change

* Also undo correlated bookings on undo

* Added API endpoints for listing and undoing transactions and use them on purchase/consume/inventory/stockoverview

* Initial Stock detail page

* Allow Undo for Tranfers

* Price step to .01

* Some localization string changes & fixes
This commit is contained in:
kriddles
2019-12-19 12:48:36 -06:00
committed by Bernd Bestel
parent 0be1994c02
commit 6c7420ea08
27 changed files with 2614 additions and 79 deletions

View File

@@ -1,15 +1,30 @@
$('#save-consume-button').on('click', function(e)
$(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");
if ($("#use_specific_stock_entry").is(":checked"))
{
jsonForm.amount = 1;
}
var apiUrl = 'stock/products/' + jsonForm.product_id + '/consume';
var jsonData = {};
@@ -21,6 +36,15 @@
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();
@@ -71,11 +95,11 @@
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
}
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
}
if (GetUriParam("embedded") !== undefined)
@@ -86,7 +110,6 @@
}
else
{
Grocy.FrontendHelpers.EndUiBusy("consume-form");
toastr.success(successMessage);
@@ -102,6 +125,10 @@
{
Grocy.Components.RecipePicker.Clear();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
$("#location_id").find("option").remove().end().append("<option></option>");
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.FrontendHelpers.ValidateForm('consume-form');
}
@@ -128,11 +155,6 @@ $('#save-mark-as-open-button').on('click', function(e)
var jsonForm = $('#consume-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("consume-form");
if ($("#use_specific_stock_entry").is(":checked"))
{
jsonForm.amount = 1;
}
var apiUrl = 'stock/products/' + jsonForm.product_id + '/open';
jsonData = { };
@@ -156,7 +178,7 @@ $('#save-mark-as-open-button').on('click', function(e)
}
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + result.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
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) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
$('#amount').val(Grocy.UserSettings.stock_default_consume_amount);
Grocy.Components.ProductPicker.Clear();
@@ -178,6 +200,69 @@ $('#save-mark-as-open-button').on('click', function(e)
);
});
$("#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("<option></option>");
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($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount: %1$s; Expires on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
}));
sumValue = sumValue + parseFloat(stockEntry.amount);
if (stockEntry.stock_id == stockId)
{
$("#specific_stock_entry").val(stockId);
}
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
}
});
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
@@ -185,6 +270,7 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
$("#use_specific_stock_entry").click();
}
$("#location_id").val("");
var productId = $(e.target).val();
@@ -195,9 +281,45 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
Grocy.Api.Get('stock/products/' + productId,
function(productDetails)
{
$('#amount').attr('max', productDetails.stock_amount);
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
$("#location_id").find("option").remove().end().append("<option></option>");
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($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name + " (" + __t("Default location") + ")"
}));
$("#location_id").val(productDetails.location.id);
$("#location_id").trigger('change');
setDefault = 1;
}
else
{
$("#location_id").append($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name
}));
}
if (setDefault == 0)
{
$("#location_id").val(stockLocation.location_id);
$("#location_id").trigger('change');
}
});
},
function(xhr)
{
console.error(xhr);
}
);
if (productDetails.product.allow_partial_units_in_stock == 1)
{
$("#amount").attr("min", "0.01");
@@ -251,44 +373,10 @@ Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
console.error(xhr);
}
);
Grocy.Api.Get("stock/products/" + productId + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
var openTxt = __t("Not opened");
if (stockEntry.open == 1)
{
openTxt = __t("Opened");
}
for (i = 0; i < stockEntry.amount; i++)
{
// Do this only for the first 50 entries to prevent a very long loop (is more anytime needed)?
if (i > 50)
{
break;
}
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
text: __t("Expires on %1$s; Bought on %2$s", moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
}));
}
});
},
function(xhr)
{
console.error(xhr);
}
);
}
});
$('#amount').val(Grocy.UserSettings.stock_default_consume_amount);
Grocy.Components.ProductPicker.GetPicker().trigger('change');
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.FrontendHelpers.ValidateForm('consume-form');
$('#amount').on('focus', function(e)
@@ -323,23 +411,59 @@ $('#consume-form input').keydown(function(event)
}
});
$("#specific_stock_entry").on("change", function(e)
{
if ($(e.target).val() == "")
{
var sumValue = 0;
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
if (stockEntry.location_id == $("#location_id").val() || stockEntry.location_id == "")
{
sumValue = sumValue + parseFloat(stockEntry.amount);
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", $('option:selected', this).attr('amount')));
$("#amount").attr("max", $('option:selected', this).attr('amount'));
}
});
$("#use_specific_stock_entry").on("change", function()
{
var value = $(this).is(":checked");
if (value)
{
$("#specific_stock_entry").removeAttr("disabled");
$("#amount").attr("disabled", "");
$("#amount").val(1);
$("#amount").removeAttr("required");
$("#specific_stock_entry").attr("required", "");
}
else
{
$("#specific_stock_entry").attr("disabled", "");
$("#amount").removeAttr("disabled");
$("#amount").attr("required", "");
$("#specific_stock_entry").removeAttr("required");
$("#specific_stock_entry").val("");
$("#location_id").trigger('change');
}
Grocy.FrontendHelpers.ValidateForm("consume-form");
@@ -358,3 +482,17 @@ function UndoStockBooking(bookingId)
}
);
};
function UndoStockTransaction(transactionId)
{
Grocy.Api.Post('stock/transactions/' + transactionId.toString() + '/undo', { },
function (result)
{
toastr.success(__t("Transaction successfully undone"));
},
function (xhr)
{
console.error(xhr);
}
);
};

View File

@@ -64,7 +64,7 @@
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(result)
{
var successMessage = __t('Stock amount of %1$s is now %2$s', result.product.name, result.stock_amount + " " + __n(result.stock_amount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural)) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
var successMessage = __t('Stock amount of %1$s is now %2$s', result.product.name, result.stock_amount + " " + __n(result.stock_amount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural)) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
if (GetUriParam("embedded") !== undefined)
{
@@ -299,3 +299,17 @@ function UndoStockBooking(bookingId)
}
);
};
function UndoStockTransaction(transactionId)
{
Grocy.Api.Post('stock/transactions/' + transactionId.toString() + '/undo', { },
function (result)
{
toastr.success(__t("Transaction successfully undone"));
},
function (xhr)
{
console.error(xhr);
}
);
};

View File

@@ -70,7 +70,7 @@
);
}
var successMessage = __t('Added %1$s of %2$s to stock', result.amount + " " +__n(result.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + result.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
var successMessage = __t('Added %1$s of %2$s to stock', result.amount + " " + __n(result.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + result.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
if (GetUriParam("embedded") !== undefined)
{
@@ -309,3 +309,28 @@ function UndoStockBooking(bookingId)
}
);
};
function UndoStockTransaction(transactionId)
{
Grocy.Api.Post('stock/transactions/' + transactionId.toString() + '/undo', { },
function(result)
{
toastr.success(__t("Transaction successfully undone"));
Grocy.Api.Get('stock/transactions/' + transactionId.toString(),
function(result)
{
window.postMessage(WindowMessageBag("ProductChanged", result[0].product_id), Grocy.BaseUrl);
},
function (xhr)
{
console.error(xhr);
}
);
},
function(xhr)
{
console.error(xhr);
}
);
};

View File

@@ -0,0 +1,293 @@
var stockDetailTable = $('#stock-detail-table').DataTable({
'order': [[2, 'asc']],
'columnDefs': [
{ 'orderable': false, 'targets': 0 },
],
});
$('#stock-detail-table tbody').removeClass("d-none");
function bootBoxModal(message) {
bootbox.dialog({
message: message,
size: 'large',
backdrop: true,
closeButton: false,
buttons: {
cancel: {
label: __t('Cancel'),
className: 'btn-secondary responsive-button',
callback: function()
{
bootbox.hideAll();
}
}
}
});
}
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex ) {
var productId = Grocy.Components.ProductPicker.GetValue();
if ( ( isNaN( productId ) ||
productId == "" ||
//assume productId is in the first column
productId == data[1] ) )
{
return true;
}
return false;
}
);
$(document).ready(function() {
Grocy.Components.ProductPicker.GetPicker().trigger('change');
} );
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
stockDetailTable.draw();
});
$(document).on('click', '.stock-consume-button', function(e)
{
e.preventDefault();
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document.activeElement.blur();
Grocy.FrontendHelpers.BeginUiBusy();
var productId = $(e.currentTarget).attr('data-product-id');
var locationId = $(e.currentTarget).attr('data-location-id');
var specificStockEntryId = $(e.currentTarget).attr('data-stock-id');
var stockRowId = $(e.currentTarget).attr('data-stockrow-id');
var consumeAmount = $(e.currentTarget).attr('data-consume-amount');
var wasSpoiled = $(e.currentTarget).hasClass("product-consume-button-spoiled");
Grocy.Api.Post('stock/products/' + productId + '/consume', { 'amount': consumeAmount, 'spoiled': wasSpoiled, 'location_id': locationId, 'stock_entry_id': specificStockEntryId},
function(bookingResponse)
{
Grocy.Api.Get('stock/products/' + productId,
function(result)
{
var toastMessage = __t('Removed %1$s of %2$s from stock', consumeAmount.toString() + " " + __n(consumeAmount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural), result.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
if (wasSpoiled)
{
toastMessage += " (" + __t("Spoiled") + ")";
}
Grocy.FrontendHelpers.EndUiBusy();
toastr.success(toastMessage);
RefreshStockDetailRow(stockRowId);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
});
$(document).on('click', '.product-open-button', function(e)
{
e.preventDefault();
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document.activeElement.blur();
Grocy.FrontendHelpers.BeginUiBusy();
var productId = $(e.currentTarget).attr('data-product-id');
var productName = $(e.currentTarget).attr('data-product-name');
var productQuName = $(e.currentTarget).attr('data-product-qu-name');
var button = $(e.currentTarget);
Grocy.Api.Post('stock/products/' + productId + '/open', { 'amount': 1 },
function(bookingResponse)
{
Grocy.Api.Get('stock/products/' + productId,
function(result)
{
if (result.stock_amount == result.stock_amount_opened)
{
button.addClass("disabled");
}
Grocy.FrontendHelpers.EndUiBusy();
toastr.success(__t('Marked %1$s of %2$s as opened', 1 + " " + productQuName, productName) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
RefreshProductRow(productId);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
});
$(document).on("click", ".stock-name-cell", function(e)
{
Grocy.Components.ProductCard.Refresh($(e.currentTarget).attr("data-stock-id"));
$("#stockdetail-productcard-modal").modal("show");
});
$(document).on("click", ".product-purchase-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/purchase?embedded&product=") + productId.toString() + '"></iframe>');
});
$(document).on("click", ".product-transfer-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
var locationId = $(e.currentTarget).attr('data-location-id');
var specificStockEntryId = $(e.currentTarget).attr('data-stock-id');
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/transfer?embedded&product=") + productId.toString() + '&locationId=' + locationId.toString() + '&stockId=' + specificStockEntryId.toString() + '"></iframe>');
});
$(document).on("click", ".product-consume-custom-amount-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
var locationId = $(e.currentTarget).attr('data-location-id');
var specificStockEntryId = $(e.currentTarget).attr('data-stock-id');
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/consume?embedded&product=") + productId.toString() + '&locationId=' + locationId.toString() + '&stockId=' + specificStockEntryId.toString() + '"></iframe>');
});
$(document).on("click", ".product-inventory-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/inventory?embedded&product=") + productId.toString() + '"></iframe>');
});
$(document).on("click", ".product-stockedit-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
var stockRowId = $(e.currentTarget).attr("data-id");
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/stockedit?embedded&product=") + productId.toString() + '&stockRowId=' + stockRowId.toString() + '"></iframe>');
});
$(document).on("click", ".product-add-to-shopping-list-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
bootBoxModal('<iframe height="650px" class="embed-responsive" src="' + U("/shoppinglistitem/new?embedded&updateexistingproduct&product=") + productId.toString() + '"></iframe>');
});
function RefreshStockDetailRow(stockRowId)
{
Grocy.Api.Get("objects/stock/" + stockRowId,
function(result)
{
var stockRow = $('#stock-' + stockRowId + '-row');
var now = moment();
stockRow.removeClass("table-warning");
stockRow.removeClass("table-danger");
stockRow.removeClass("table-info");
stockRow.removeClass("d-none");
stockRow.removeAttr("style");
if (result == null || result.amount == 0)
{
stockRow.fadeOut(500, function()
{
//$(this).tooltip("hide");
$(this).addClass("d-none");
});
}
else
{
$('#stock-' + stockRowId + '-amount').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-amount').fadeOut(500, function ()
{
$(this).text(result.amount).fadeIn(500);
});
$('#stock-' + stockRowId + '-best-before-date').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-best-before-date').fadeOut(500, function()
{
$(this).text(result.best_before_date).fadeIn(500);
});
$('#stock-' + stockRowId + '-location').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-location').fadeOut(500, function()
{
//TODO grab location name instead of id
$(this).text(result.location_id).fadeIn(500);
});
$('#stock-' + stockRowId + '-price').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-price').fadeOut(500, function()
{
$(this).text(result.price).fadeIn(500);
});
$('#stock-' + stockRowId + '-purchased-date').parent().effect('highlight', { }, 500);
$('#stock-' + stockRowId + '-purchased-date').fadeOut(500, function()
{
$(this).text(result.purchased_date).fadeIn(500);
});
}
setTimeout(function()
{
RefreshContextualTimeago();
RefreshLocaleNumberDisplay();
}, 600);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
}
$(window).on("message", function(e)
{
var data = e.originalEvent.data;
if (data.Message === "StockDetailChanged")
{
RefreshStockDetailRow(data.Payload);
}
});

151
public/viewjs/stockedit.js Normal file
View File

@@ -0,0 +1,151 @@
$(document).ready(function() {
var stockRowId = GetUriParam('stockRowId');
Grocy.Api.Get("objects/stock/" + stockRowId,
function(stockEntry)
{
Grocy.Components.LocationPicker.SetId(stockEntry.location_id);
$('#amount').val(stockEntry.amount);
$('#price').val(stockEntry.price);
Grocy.Components.DateTimePicker.SetValue(stockEntry.best_before_date);
Grocy.Api.Get('stock/products/' + stockEntry.product_id,
function(productDetails)
{
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
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 %1$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 %1$s', '1'));
}
if (productDetails.product.enable_tare_weight_handling == 1)
{
$("#amount").attr("min", productDetails.product.tare_weight);
$("#amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %1$s', parseFloat(productDetails.product.tare_weight).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: 2 })));
$("#tare-weight-handling-info").removeClass("d-none");
}
else
{
$("#tare-weight-handling-info").addClass("d-none");
}
},
function(xhr)
{
console.error(xhr);
}
);
},
function(xhr)
{
console.error(xhr);
}
);
} );
$('#save-stockedit-button').on('click', function(e)
{
e.preventDefault();
var jsonForm = $('#stockedit-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("stockedit-form");
if (!jsonForm.price.toString().isEmpty())
{
price = parseFloat(jsonForm.price).toFixed(2);
}
var jsonData = { };
jsonData.amount = jsonForm.amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue();
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
jsonData.location_id = Grocy.Components.LocationPicker.GetValue();
}
else
{
jsonData.location_id = 1;
}
jsonData.price = price;
var bookingResponse = null;
var stockRowId = GetUriParam('stockRowId');
jsonData.stock_row_id = stockRowId;
Grocy.Api.Put("stock", jsonData,
function(result)
{
var successMessage = __t('Stock entry successfully updated') + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(\'' + result.id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
window.parent.postMessage(WindowMessageBag("StockDetailChanged", stockRowId), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("Ready"), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("stockedit-form");
console.error(xhr);
}
);
});
Grocy.FrontendHelpers.ValidateForm('stockedit-form');
$('#stockedit-form input').keyup(function (event)
{
Grocy.FrontendHelpers.ValidateForm('stockedit-form');
});
$('#stockedit-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('stockedit-form').checkValidity() === false) //There is at least one validation error
{
return false;
}
else
{
$('#save-stockedit-button').click();
}
}
});
if (Grocy.Components.DateTimePicker)
{
Grocy.Components.DateTimePicker.GetInputElement().on('change', function(e)
{
Grocy.FrontendHelpers.ValidateForm('stockedit-form');
});
Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e)
{
Grocy.FrontendHelpers.ValidateForm('stockedit-form');
});
}
function UndoStockBooking(bookingId)
{
Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', { },
function(result)
{
toastr.success(__t("Booking successfully undone"));
},
function(xhr)
{
console.error(xhr);
}
);
};

View File

@@ -84,7 +84,7 @@ $(document).on('click', '.product-consume-button', function(e)
Grocy.Api.Get('stock/products/' + productId,
function(result)
{
var toastMessage = __t('Removed %1$s of %2$s from stock', consumeAmount.toString() + " " + __n(consumeAmount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural), result.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
var toastMessage = __t('Removed %1$s of %2$s from stock', consumeAmount.toString() + " " + __n(consumeAmount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural), result.product.name) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
if (wasSpoiled)
{
toastMessage += " (" + __t("Spoiled") + ")";
@@ -137,7 +137,7 @@ $(document).on('click', '.product-open-button', function(e)
}
Grocy.FrontendHelpers.EndUiBusy();
toastr.success(__t('Marked %1$s of %2$s as opened', 1 + " " + productQuName, productName) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockBooking(' + bookingResponse.id + ')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
toastr.success(__t('Marked %1$s of %2$s as opened', 1 + " " + productQuName, productName) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>');
RefreshStatistics();
RefreshProductRow(productId);
},
@@ -219,6 +219,30 @@ $(document).on("click", ".product-purchase-button", function(e)
});
});
$(document).on("click", ".product-transfer-button", function(e)
{
e.preventDefault();
var productId = $(e.currentTarget).attr("data-product-id");
bootbox.dialog({
message: '<iframe height="650px" class="embed-responsive" src="' + U("/transfer?embedded&product=") + productId.toString() + '"></iframe>',
size: 'large',
backdrop: true,
closeButton: false,
buttons: {
cancel: {
label: __t('Cancel'),
className: 'btn-secondary responsive-button',
callback: function()
{
bootbox.hideAll();
}
}
}
});
});
$(document).on("click", ".product-consume-custom-amount-button", function(e)
{
e.preventDefault();

441
public/viewjs/transfer.js Normal file
View File

@@ -0,0 +1,441 @@
$(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_from").val(locationId);
$("#location_id_from").trigger('change');
$("#use_specific_stock_entry").click();
$("#use_specific_stock_entry").trigger('change');
}
}
});
$('#save-transfer-button').on('click', function(e)
{
e.preventDefault();
var jsonForm = $('#transfer-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("transfer-form");
var apiUrl = 'stock/products/' + jsonForm.product_id + '/transfer';
var jsonData = {};
jsonData.amount = jsonForm.amount;
jsonData.location_id_to = $("#location_id_to").val();
jsonData.location_id_from = $("#location_id_from").val();
if ($("#use_specific_stock_entry").is(":checked"))
{
jsonData.stock_entry_id = jsonForm.specific_stock_entry;
}
var bookingResponse = null;
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(productDetails)
{
Grocy.Api.Post(apiUrl, jsonData,
function(result)
{
var addBarcode = GetUriParam('addbarcodetoselection');
bookingResponse = result;
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("/transfer"));
},
function(xhr)
{
console.error(xhr);
}
);
}
if (productDetails.product.enable_tare_weight_handling == 1)
{
var successMessage = __t('Transfered %1$s of %2$s from %3$s to %4$s', 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,$('option:selected', "#location_id_from").text(), $('option:selected', "#location_id_to").text()) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
}
else
{
var successMessage =__t('Transfered %1$s of %2$s from %3$s to %4$s', Math.abs(jsonForm.amount) + " " + __n(jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural), productDetails.product.name, $('option:selected', "#location_id_from").text(), $('option:selected', "#location_id_to").text()) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
}
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("transfer-form");
toastr.success(successMessage);
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
$("#specific_stock_entry").attr("disabled", "");
$("#specific_stock_entry").removeAttr("required");
if ($("#use_specific_stock_entry").is(":checked"))
{
$("#use_specific_stock_entry").click();
}
$("#location_id_from").find("option").remove().end().append("<option></option>");
$("#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_transfer_amount);
$('#amount_qu_unit').text("");
$("#tare-weight-handling-info").addClass("d-none");
Grocy.Components.ProductPicker.Clear();
$("#location_id_to").val("");
$("#location_id_from").val("");
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.FrontendHelpers.ValidateForm('transfer-form');
}
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("transfer-form");
console.error(xhr);
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("transfer-form");
console.error(xhr);
}
);
});
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
{
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
if ($("#use_specific_stock_entry").is(":checked"))
{
$("#use_specific_stock_entry").click();
}
$("#location_id_to").val("");
$("#location_id_from").val("");
var productId = $(e.target).val();
if (productId)
{
Grocy.Components.ProductCard.Refresh(productId);
Grocy.Api.Get('stock/products/' + productId,
function(productDetails)
{
if (productDetails.product.enable_tare_weight_handling == 1) {
Grocy.Components.ProductPicker.GetPicker().parent().find(".invalid-feedback").text(__t('Products with Tare weight enabled are currently not supported for Transfer. Please select another product.'));
Grocy.Components.ProductPicker.Clear();
return;
}
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
$("#location_id_from").find("option").remove().end().append("<option></option>");
Grocy.Api.Get("stock/products/" + productId + '/locations',
function(stockLocations)
{
var setDefault = 0;
stockLocations.forEach(stockLocation =>
{
if (productDetails.location.id == stockLocation.location_id)
{
$("#location_id_from").append($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name + " (" + __t("Default location") + ")"
}));
$("#location_id_from").val(productDetails.location.id);
$("#location_id_from").trigger('change');
setDefault = 1;
}
else
{
$("#location_id_from").append($("<option>", {
value: stockLocation.location_id,
text: stockLocation.location_name
}));
}
if (setDefault == 0)
{
$("#location_id_from").val(stockLocation.location_id);
$("#location_id_from").trigger('change');
}
});
},
function(xhr)
{
console.error(xhr);
}
);
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 must be between %1$s and %2$s', 0.01.toLocaleString(), parseFloat(productDetails.stock_amount).toLocaleString()));
}
else
{
$("#amount").attr("min", "1");
$("#amount").attr("step", "1");
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", parseFloat(productDetails.stock_amount).toLocaleString()));
}
if (productDetails.product.enable_tare_weight_handling == 1)
{
$("#amount").attr("min", productDetails.product.tare_weight);
$("#tare-weight-handling-info").removeClass("d-none");
}
else
{
$("#tare-weight-handling-info").addClass("d-none");
}
if ((parseFloat(productDetails.stock_amount) || 0) === 0)
{
Grocy.Components.ProductPicker.Clear();
Grocy.FrontendHelpers.ValidateForm('transfer-form');
Grocy.Components.ProductPicker.ShowCustomError(__t('This product is not in stock'));
Grocy.Components.ProductPicker.GetInputElement().focus();
}
else
{
Grocy.Components.ProductPicker.HideCustomError();
Grocy.FrontendHelpers.ValidateForm('transfer-form');
$('#amount').focus();
}
},
function(xhr)
{
console.error(xhr);
}
);
}
});
$('#amount').val(Grocy.UserSettings.stock_default_transfer_amount);
Grocy.FrontendHelpers.ValidateForm('transfer-form');
$("#location_id_from").on('change', function(e)
{
var locationId = $(e.target).val();
var sumValue = 0;
var stockId = null;
if (locationId == $("#location_id_to").val())
{
$("#location_id_to").val("");
}
if (GetUriParam("embedded") !== undefined)
{
stockId = GetUriParam('stockId');
}
$("#specific_stock_entry").find("option").remove().end().append("<option></option>");
if ($("#use_specific_stock_entry").is(":checked"))
{
$("#use_specific_stock_entry").click();
}
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($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount: %1$s; Expires on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
}));
if (stockEntry.stock_id == stockId)
{
$("#specific_stock_entry").val(stockId);
}
sumValue = sumValue + parseFloat(stockEntry.amount);
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
}
});
$("#location_id_to").on('change', function(e)
{
var locationId = $(e.target).val();
if (locationId == $("#location_id_from").val())
{
$("#location_id_to").parent().find(".invalid-feedback").text(__t('This cannot be the same as the "From" location'));
$("#location_id_to").val("");
}
});
$('#amount').on('focus', function(e)
{
$(this).select();
});
$('#transfer-form input').keyup(function(event)
{
Grocy.FrontendHelpers.ValidateForm('transfer-form');
});
$('#transfer-form select').change(function(event)
{
Grocy.FrontendHelpers.ValidateForm('transfer-form');
});
$('#transfer-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('transfer-form').checkValidity() === false) //There is at least one validation error
{
return false;
}
else
{
$('#save-transfer-button').click();
}
}
});
$("#specific_stock_entry").on("change", function(e)
{
if ($(e.target).val() == "")
{
var sumValue = 0;
Grocy.Api.Get("stock/products/" + Grocy.Components.ProductPicker.GetValue() + '/entries',
function(stockEntries)
{
stockEntries.forEach(stockEntry =>
{
if (stockEntry.location_id == $("#location_id_from").val() || stockEntry.location_id == "")
{
sumValue = sumValue + parseFloat(stockEntry.amount);
}
});
$("#amount").attr("max", sumValue);
if (sumValue == 0)
{
$("#amount").parent().find(".invalid-feedback").text(__t('There are no units available at this location'));
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", sumValue));
}
},
function(xhr)
{
console.error(xhr);
}
);
}
else
{
$("#amount").parent().find(".invalid-feedback").text(__t('The amount must be between %1$s and %2$s', "1", $('option:selected', this).attr('amount')));
$("#amount").attr("max", $('option:selected', this).attr('amount'));
}
});
$("#use_specific_stock_entry").on("change", function()
{
var value = $(this).is(":checked");
if (value)
{
$("#specific_stock_entry").removeAttr("disabled");
$("#specific_stock_entry").attr("required", "");
}
else
{
$("#specific_stock_entry").attr("disabled", "");
$("#specific_stock_entry").removeAttr("required");
$("#specific_stock_entry").val("");
$("#location_id_from").trigger('change');
}
Grocy.FrontendHelpers.ValidateForm("transfer-form");
});
function UndoStockBooking(bookingId)
{
Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', { },
function(result)
{
toastr.success(__t("Booking successfully undone"));
},
function(xhr)
{
console.error(xhr);
}
);
};
function UndoStockTransaction(transactionId)
{
Grocy.Api.Post('stock/transactions/' + transactionId.toString() + '/undo', { },
function (result)
{
toastr.success(__t("Transaction successfully undone"));
},
function (xhr)
{
console.error(xhr);
}
);
};