2020-08-17 14:47:33 -05:00
|
|
|
|
$('#save-barcode-button').on('click', function(e)
|
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2022-03-26 10:34:00 +01:00
|
|
|
|
if (!Grocy.FrontendHelpers.ValidateForm("barcode-form", true))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 13:45:14 +01:00
|
|
|
|
if ($(".combobox-menu-visible").length)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 14:47:33 -05:00
|
|
|
|
var jsonData = $('#barcode-form').serializeJSON();
|
2020-11-10 18:11:33 +01:00
|
|
|
|
jsonData.amount = jsonData.display_amount;
|
|
|
|
|
delete jsonData.display_amount;
|
2021-06-27 19:04:09 +02:00
|
|
|
|
jsonData.qu_id = $("#qu_id").val();
|
2020-11-10 18:11:33 +01:00
|
|
|
|
|
2020-08-17 14:47:33 -05:00
|
|
|
|
Grocy.FrontendHelpers.BeginUiBusy("barcode-form");
|
|
|
|
|
|
|
|
|
|
if (Grocy.EditMode === 'create')
|
|
|
|
|
{
|
|
|
|
|
Grocy.Api.Post('objects/product_barcodes', jsonData,
|
|
|
|
|
function(result)
|
|
|
|
|
{
|
2020-11-17 19:11:02 +01:00
|
|
|
|
Grocy.EditObjectId = result.created_object_id;
|
|
|
|
|
Grocy.Components.UserfieldsForm.Save()
|
|
|
|
|
|
2025-01-10 17:15:09 +01:00
|
|
|
|
window.parent.postMessage(WindowMessageBag("ProductBarcodesChanged"), Grocy.BaseUrl);
|
|
|
|
|
window.parent.postMessage(WindowMessageBag("CloseLastModal"), Grocy.BaseUrl);
|
2020-08-17 14:47:33 -05:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("barcode-form");
|
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-17 19:11:02 +01:00
|
|
|
|
Grocy.Components.UserfieldsForm.Save();
|
2020-08-17 14:47:33 -05:00
|
|
|
|
Grocy.Api.Put('objects/product_barcodes/' + Grocy.EditObjectId, jsonData,
|
|
|
|
|
function(result)
|
|
|
|
|
{
|
2025-01-10 17:15:09 +01:00
|
|
|
|
window.parent.postMessage(WindowMessageBag("ProductBarcodesChanged"), Grocy.BaseUrl);
|
|
|
|
|
window.parent.postMessage(WindowMessageBag("CloseLastModal"), Grocy.BaseUrl);
|
2020-08-17 14:47:33 -05:00
|
|
|
|
},
|
|
|
|
|
function(xhr)
|
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.EndUiBusy("barcode-form");
|
|
|
|
|
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-10 18:11:33 +01:00
|
|
|
|
$('#barcode').on('keyup', function(e)
|
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('barcode-form');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#qu_id').on('change', function(e)
|
2020-08-17 14:47:33 -05:00
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('barcode-form');
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-10 18:11:33 +01:00
|
|
|
|
$('#display_amount').on('keyup', function(e)
|
2020-08-17 14:47:33 -05:00
|
|
|
|
{
|
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('barcode-form');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#barcode-form input').keydown(function(event)
|
|
|
|
|
{
|
2022-03-30 18:00:28 +02:00
|
|
|
|
if (event.keyCode === 13) // Enter
|
2020-08-17 14:47:33 -05:00
|
|
|
|
{
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
2022-03-30 18:00:28 +02:00
|
|
|
|
if (!Grocy.FrontendHelpers.ValidateForm('barcode-form'))
|
2020-08-17 14:47:33 -05:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$('#save-barcode-button').click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-11-10 18:11:33 +01:00
|
|
|
|
|
|
|
|
|
Grocy.Components.ProductAmountPicker.Reload(Grocy.EditObjectProduct.id, Grocy.EditObjectProduct.qu_id_purchase);
|
|
|
|
|
if (Grocy.EditMode == "edit")
|
|
|
|
|
{
|
|
|
|
|
$("#display_amount").val(Grocy.EditObject.amount);
|
2020-11-12 22:47:00 +01:00
|
|
|
|
$(".input-group-productamountpicker").trigger("change");
|
2025-01-12 14:27:19 +01:00
|
|
|
|
|
|
|
|
|
if (Grocy.EditObject.qu_id)
|
|
|
|
|
{
|
|
|
|
|
Grocy.Components.ProductAmountPicker.SetQuantityUnit(Grocy.EditObject.qu_id);
|
|
|
|
|
}
|
2020-11-10 18:11:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 14:47:33 -05:00
|
|
|
|
Grocy.FrontendHelpers.ValidateForm('barcode-form');
|
2021-11-08 21:59:02 +01:00
|
|
|
|
setTimeout(function()
|
|
|
|
|
{
|
|
|
|
|
$('#barcode').focus();
|
2025-01-31 15:35:34 +01:00
|
|
|
|
}, Grocy.FormFocusDelay);
|
2020-11-16 19:10:29 +01:00
|
|
|
|
RefreshLocaleNumberInput();
|
2020-11-17 19:11:02 +01:00
|
|
|
|
Grocy.Components.UserfieldsForm.Load()
|
2020-12-22 19:06:41 +01:00
|
|
|
|
|
|
|
|
|
$(document).on("Grocy.BarcodeScanned", function(e, barcode, target)
|
|
|
|
|
{
|
|
|
|
|
if (target !== "#barcode")
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$("#barcode").val(barcode);
|
|
|
|
|
});
|