2017-04-22 15:47:55 +02:00
|
|
|
|
$('#save-consume-button').on('click', function(e)
|
2017-04-15 23:16:20 +02:00
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2017-04-22 15:47:55 +02:00
|
|
|
|
var jsonForm = $('#consume-form').serializeJSON();
|
2017-04-15 23:16:20 +02:00
|
|
|
|
|
|
|
|
|
var spoiled = 0;
|
|
|
|
|
if ($('#spoiled').is(':checked'))
|
|
|
|
|
{
|
|
|
|
|
spoiled = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 19:03:39 +02:00
|
|
|
|
Grocy.Api.Get('stock/get-product-details/' + jsonForm.product_id,
|
2017-04-17 16:51:49 +02:00
|
|
|
|
function (productDetails)
|
2017-04-15 23:16:20 +02:00
|
|
|
|
{
|
2018-04-18 19:03:39 +02:00
|
|
|
|
Grocy.Api.Get('stock/consume-product/' + jsonForm.product_id + '/' + jsonForm.amount + '?spoiled=' + spoiled,
|
2017-04-17 16:51:49 +02:00
|
|
|
|
function(result)
|
|
|
|
|
{
|
2018-05-12 16:30:10 +02:00
|
|
|
|
toastr.success(L('Removed #1 #2 of #3 from stock', jsonForm.amount, productDetails.quantity_unit_stock.name, productDetails.product.name));
|
2017-04-17 16:51:49 +02:00
|
|
|
|
|
|
|
|
|
$('#amount').val(1);
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.SetValue('');
|
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().focus();
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
2017-04-17 16:51:49 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-04-15 23:16:20 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
|
2017-04-15 23:16:20 +02:00
|
|
|
|
{
|
|
|
|
|
var productId = $(e.target).val();
|
|
|
|
|
|
2017-04-16 23:11:03 +02:00
|
|
|
|
if (productId)
|
|
|
|
|
{
|
2018-04-14 11:10:38 +02:00
|
|
|
|
Grocy.Components.ProductCard.Refresh(productId);
|
|
|
|
|
|
2018-04-18 19:03:39 +02:00
|
|
|
|
Grocy.Api.Get('stock/get-product-details/' + productId,
|
2017-04-20 17:10:21 +02:00
|
|
|
|
function (productDetails)
|
2017-04-16 23:11:03 +02:00
|
|
|
|
{
|
2017-04-20 17:10:21 +02:00
|
|
|
|
$('#amount').attr('max', productDetails.stock_amount);
|
2017-04-21 19:02:00 +02:00
|
|
|
|
$('#amount_qu_unit').text(productDetails.quantity_unit_stock.name);
|
2017-04-16 23:11:03 +02:00
|
|
|
|
|
2017-04-20 17:10:21 +02:00
|
|
|
|
if ((productDetails.stock_amount || 0) === 0)
|
2017-04-17 16:51:49 +02:00
|
|
|
|
{
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.SetValue('');
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.ShowCustomError(L('This product is not in stock'));
|
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().focus();
|
2017-04-17 16:51:49 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.HideCustomError();
|
2018-07-11 19:43:05 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
2017-11-05 11:29:17 +01:00
|
|
|
|
$('#amount').focus();
|
2017-04-17 16:51:49 +02:00
|
|
|
|
}
|
2017-04-16 23:11:03 +02:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
console.error(xhr);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-04-15 23:16:20 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#amount').val(1);
|
2018-07-14 14:43:57 +02:00
|
|
|
|
Grocy.Components.ProductPicker.GetInputElement().focus();
|
2018-07-12 19:12:31 +02:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
2017-04-16 23:11:03 +02:00
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#amount').on('focus', function(e)
|
|
|
|
|
{
|
2018-07-12 19:12:31 +02:00
|
|
|
|
$(this).select();
|
2018-04-16 19:11:32 +02:00
|
|
|
|
});
|
2017-04-20 17:10:21 +02:00
|
|
|
|
|
2018-07-11 19:43:05 +02:00
|
|
|
|
$('#consume-form input').keyup(function (event)
|
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('consume-form');
|
|
|
|
|
});
|
|
|
|
|
|
2018-04-16 19:11:32 +02:00
|
|
|
|
$('#consume-form input').keydown(function(event)
|
|
|
|
|
{
|
|
|
|
|
if (event.keyCode === 13) //Enter
|
2017-04-17 16:51:49 +02:00
|
|
|
|
{
|
2018-07-11 19:43:05 +02:00
|
|
|
|
if (document.getElementById('consume-form').checkValidity() === false) //There is at least one validation error
|
2017-04-17 16:51:49 +02:00
|
|
|
|
{
|
2018-04-16 19:11:32 +02:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
return false;
|
2017-04-17 16:51:49 +02:00
|
|
|
|
}
|
2018-07-11 19:43:05 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#save-consume-button').click();
|
|
|
|
|
}
|
2018-04-16 19:11:32 +02:00
|
|
|
|
}
|
2017-04-15 23:16:20 +02:00
|
|
|
|
});
|