grocy/public/viewjs/stockoverview.js

83 lines
2.3 KiB
JavaScript

var stockOverviewTable = $('#stock-overview-table').DataTable({
'bPaginate': false,
'order': [[3, 'asc']],
'columnDefs': [
{ 'orderable': false, 'targets': 0 },
{ 'visible': false, 'targets': 4 }
],
'language': JSON.parse(L('datatables_localization')),
"dom": '<"filter-by-location">f'
});
$("div.filter-by-location").html('<div class="dataTables_filter"><label>' + L('Filter by location') + ':<select id="location-filter" class="form-control input-sm" style="margin-left: 0.5em;"></label></div>');
$('#stock-overview-table_wrapper').on("DOMSubtreeModified", function()
{
$('#stock-overview-table_wrapper').off("DOMSubtreeModified");
Grocy.Api.Get('get-objects/locations',
function(locations)
{
$('#location-filter').append($('<option></option>').val("all").html(L("All")));
$.each(locations, function(index)
{
var locationName = locations[index].name;
$('#location-filter').append($('<option></option>').val(locationName).html(locationName));
});
},
function(xhr)
{
console.error(xhr);
}
);
$("#location-filter").on("change", function()
{
var value = $(this).val();
if (value === "all")
{
value = "";
}
stockOverviewTable.column(4).search(value).draw();
});
});
$(document).on('click', '.product-consume-button', function(e)
{
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 consumeAmount = $(e.currentTarget).attr('data-consume-amount');
Grocy.Api.Get('stock/consume-product/' + productId + '/' + consumeAmount,
function(result)
{
var oldAmount = parseInt($('#product-' + productId + '-amount').text());
var newAmount = oldAmount - consumeAmount;
if (newAmount === 0)
{
$('#product-' + productId + '-row').fadeOut(500, function()
{
$(this).remove();
});
}
else
{
$('#product-' + productId + '-amount').parent().effect('highlight', { }, 500);
$('#product-' + productId + '-amount').fadeOut(500, function()
{
$(this).text(newAmount).fadeIn(500);
});
$('#product-' + productId + '-consume-all-button').attr('data-consume-amount', newAmount);
}
toastr.success(L('Removed #1 #2 of #3 from stock', consumeAmount, productQuName, productName));
},
function(xhr)
{
console.error(xhr);
}
);
});