mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 01:32:38 +00:00
* 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
294 lines
8.6 KiB
JavaScript
294 lines
8.6 KiB
JavaScript
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);
|
|
}
|
|
});
|