mirror of
https://github.com/grocy/grocy.git
synced 2025-08-19 20:11:16 +00:00
Started working on qu unit conversion handling (references #177)
This commit is contained in:
@@ -9,6 +9,11 @@
|
||||
redirectDestination = U(returnTo) + '?createdproduct=' + encodeURIComponent($('#name').val());
|
||||
}
|
||||
|
||||
if (Grocy.ProductEditFormRedirectUri !== undefined)
|
||||
{
|
||||
redirectDestination = Grocy.ProductEditFormRedirectUri;
|
||||
}
|
||||
|
||||
var jsonData = $('#product-form').serializeJSON({ checkboxUncheckedValue: "0" });
|
||||
var parentProductId = jsonData.product_id;
|
||||
delete jsonData.product_id;
|
||||
@@ -44,7 +49,14 @@
|
||||
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
|
||||
function(result)
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
}
|
||||
},
|
||||
function (xhr)
|
||||
{
|
||||
@@ -55,7 +67,14 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -93,7 +112,14 @@
|
||||
Grocy.Api.UploadFile($("#product-picture")[0].files[0], 'productpictures', jsonData.picture_file_name,
|
||||
function(result)
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
}
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
@@ -104,7 +130,14 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
if (redirectDestination == "reload")
|
||||
{
|
||||
window.location.reload();
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = redirectDestination;
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -189,6 +222,9 @@ $('.input-group-qu').on('change', function(e)
|
||||
$('#qu-conversion-info').addClass('d-none');
|
||||
}
|
||||
|
||||
$("#qu-conversion-headline-info").text(__t('1 %s is the same as...', $("#qu_id_stock option:selected").text()));
|
||||
quConversionsTable.column(4).search("from_qu_id xx" + $("#qu_id_stock").val().toString() + "xx").draw();
|
||||
|
||||
$("#tare_weight_qu_info").text($("#qu_id_stock option:selected").text());
|
||||
|
||||
Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
@@ -278,6 +314,34 @@ if (Grocy.EditMode === 'create')
|
||||
}
|
||||
}
|
||||
|
||||
var quConversionsTable = $('#qu-conversions-table').DataTable({
|
||||
'paginate': false,
|
||||
'order': [[1, 'asc']],
|
||||
"orderFixed": [[3, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 },
|
||||
{ 'visible': false, 'targets': 3 }
|
||||
],
|
||||
'language': JSON.parse(__t('datatables_localization')),
|
||||
'scrollY': false,
|
||||
'colReorder': true,
|
||||
'stateSave': true,
|
||||
'stateSaveParams': function(settings, data)
|
||||
{
|
||||
data.search.search = "";
|
||||
|
||||
data.columns.forEach(column =>
|
||||
{
|
||||
column.search.search = "";
|
||||
});
|
||||
},
|
||||
'rowGroup': {
|
||||
dataSrc: 3
|
||||
}
|
||||
});
|
||||
$('#qu-conversions-table tbody').removeClass("d-none");
|
||||
quConversionsTable.columns.adjust().draw();
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
||||
$('#name').focus();
|
||||
$('.input-group-qu').trigger('change');
|
||||
@@ -286,3 +350,52 @@ Grocy.FrontendHelpers.ValidateForm('product-form');
|
||||
// Click twice to trigger on-click but not change the actual checked state
|
||||
$("#allow_partial_units_in_stock").click();
|
||||
$("#allow_partial_units_in_stock").click();
|
||||
|
||||
$(document).on('click', '.qu-conversion-delete-button', function(e)
|
||||
{
|
||||
var objectId = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
|
||||
bootbox.confirm({
|
||||
message: __t('Are you sure to remove this conversion?'),
|
||||
buttons: {
|
||||
confirm: {
|
||||
label: __t('Yes'),
|
||||
className: 'btn-success'
|
||||
},
|
||||
cancel: {
|
||||
label: __t('No'),
|
||||
className: 'btn-danger'
|
||||
}
|
||||
},
|
||||
callback: function(result)
|
||||
{
|
||||
if (result === true)
|
||||
{
|
||||
Grocy.Api.Delete('objects/quantity_unit_conversions/' + objectId, { },
|
||||
function(result)
|
||||
{
|
||||
Grocy.ProductEditFormRedirectUri = "reload";
|
||||
$('#save-product-button').click();
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.qu-conversion-edit-button', function (e)
|
||||
{
|
||||
var id = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/" + id.toString() + "?product=" + Grocy.EditObjectId.toString());
|
||||
$('#save-product-button').click();
|
||||
});
|
||||
|
||||
$("#qu-conversion-add-button").on("click", function(e)
|
||||
{
|
||||
Grocy.ProductEditFormRedirectUri = U("/quantityunitconversion/new?product=" + Grocy.EditObjectId.toString());
|
||||
$('#save-product-button').click();
|
||||
});
|
||||
|
115
public/viewjs/quantityunitconversionform.js
Normal file
115
public/viewjs/quantityunitconversionform.js
Normal file
@@ -0,0 +1,115 @@
|
||||
$('#save-quconversion-button').on('click', function(e)
|
||||
{
|
||||
e.preventDefault();
|
||||
|
||||
var jsonData = $('#quconversion-form').serializeJSON();
|
||||
jsonData.from_qu_id = $("#from_qu_id").val();
|
||||
Grocy.FrontendHelpers.BeginUiBusy("quconversion-form");
|
||||
|
||||
if (Grocy.EditMode === 'create')
|
||||
{
|
||||
Grocy.Api.Post('objects/quantity_unit_conversions', jsonData,
|
||||
function(result)
|
||||
{
|
||||
Grocy.EditObjectId = result.created_object_id;
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
if (typeof GetUriParam("qu-unit") !== "undefined")
|
||||
{
|
||||
window.location.href = U("/quantityunit/" + GetUriParam("qu-unit"));
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = U("/product/" + GetUriParam("product"));
|
||||
}
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("quconversion-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Grocy.Api.Put('objects/quantity_unit_conversions/' + Grocy.EditObjectId, jsonData,
|
||||
function(result)
|
||||
{
|
||||
Grocy.Components.UserfieldsForm.Save(function()
|
||||
{
|
||||
if (typeof GetUriParam("qu-unit") !== "undefined")
|
||||
{
|
||||
window.location.href = U("/quantityunit/" + GetUriParam("qu-unit"));
|
||||
}
|
||||
else
|
||||
{
|
||||
window.location.href = U("/product/" + GetUriParam("product"));
|
||||
}
|
||||
});
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
Grocy.FrontendHelpers.EndUiBusy("quconversion-form");
|
||||
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$('#quconversion-form input').keyup(function(event)
|
||||
{
|
||||
$('.input-group-qu').trigger('change');
|
||||
Grocy.FrontendHelpers.ValidateForm('quconversion-form');
|
||||
});
|
||||
|
||||
$('#quconversion-form input').keydown(function(event)
|
||||
{
|
||||
if (event.keyCode === 13) //Enter
|
||||
{
|
||||
event.preventDefault();
|
||||
|
||||
if (document.getElementById('quconversion-form').checkValidity() === false) //There is at least one validation error
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$('#save-quconversion-button').click();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$('.input-group-qu').on('change', function(e)
|
||||
{
|
||||
var fromQuId = $("#from_qu_id").val();
|
||||
var toQuId = $("#to_qu_id").val();
|
||||
var factor = $('#factor').val();
|
||||
|
||||
if (fromQuId == toQuId)
|
||||
{
|
||||
$("#to_qu_id").parent().find(".invalid-feedback").text(__t('This cannot be equal to %s', $("#from_qu_id option:selected").text()));
|
||||
$("#to_qu_id")[0].setCustomValidity("error");
|
||||
}
|
||||
else
|
||||
{
|
||||
$("#to_qu_id")[0].setCustomValidity("");
|
||||
}
|
||||
|
||||
if (fromQuId && toQuId)
|
||||
{
|
||||
$('#qu-conversion-info').text(__t('This means 1 %1$s is the same as %2$s %3$s', $("#from_qu_id option:selected").text(), (1 * factor).toString(), $("#to_qu_id option:selected").text()));
|
||||
$('#qu-conversion-info').removeClass('d-none');
|
||||
}
|
||||
else
|
||||
{
|
||||
$('#qu-conversion-info').addClass('d-none');
|
||||
}
|
||||
|
||||
Grocy.FrontendHelpers.ValidateForm('quconversion-form');
|
||||
});
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
||||
$('.input-group-qu').trigger('change');
|
||||
$('#from_qu_id').focus();
|
||||
Grocy.FrontendHelpers.ValidateForm('quconversion-form');
|
@@ -64,6 +64,94 @@ $('#quantityunit-form input').keydown(function(event)
|
||||
}
|
||||
});
|
||||
|
||||
var quConversionsTable = $('#qu-conversions-table').DataTable({
|
||||
'paginate': false,
|
||||
'order': [[1, 'asc']],
|
||||
'columnDefs': [
|
||||
{ 'orderable': false, 'targets': 0 }
|
||||
],
|
||||
'language': JSON.parse(__t('datatables_localization')),
|
||||
'scrollY': false,
|
||||
'colReorder': true,
|
||||
'stateSave': true,
|
||||
'stateSaveParams': function(settings, data)
|
||||
{
|
||||
data.search.search = "";
|
||||
|
||||
data.columns.forEach(column =>
|
||||
{
|
||||
column.search.search = "";
|
||||
});
|
||||
}
|
||||
});
|
||||
$('#qu-conversions-table tbody').removeClass("d-none");
|
||||
quConversionsTable.columns.adjust().draw();
|
||||
|
||||
Grocy.Components.UserfieldsForm.Load();
|
||||
$('#name').focus();
|
||||
Grocy.FrontendHelpers.ValidateForm('quantityunit-form');
|
||||
|
||||
$(document).on('click', '.qu-conversion-delete-button', function(e)
|
||||
{
|
||||
var objectId = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
|
||||
bootbox.confirm({
|
||||
message: __t('Are you sure to remove this conversion?'),
|
||||
buttons: {
|
||||
confirm: {
|
||||
label: __t('Yes'),
|
||||
className: 'btn-success'
|
||||
},
|
||||
cancel: {
|
||||
label: __t('No'),
|
||||
className: 'btn-danger'
|
||||
}
|
||||
},
|
||||
callback: function(result)
|
||||
{
|
||||
if (result === true)
|
||||
{
|
||||
Grocy.Api.Delete('objects/quantity_unit_conversions/' + objectId, { },
|
||||
function(result)
|
||||
{
|
||||
window.location.reload();
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.qu-conversion-edit-button', function (e)
|
||||
{
|
||||
var id = $(e.currentTarget).attr('data-qu-conversion-id');
|
||||
|
||||
Grocy.Api.Put('objects/quantity_units/' + Grocy.EditObjectId, $('#quantityunit-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = U("/quantityunitconversion/" + id.toString() + "?qu-unit=" + Grocy.EditObjectId.toString());
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
$("#qu-conversion-add-button").on("click", function(e)
|
||||
{
|
||||
Grocy.Api.Put('objects/quantity_units/' + Grocy.EditObjectId, $('#quantityunit-form').serializeJSON(),
|
||||
function(result)
|
||||
{
|
||||
window.location.href = U("/quantityunitconversion/new?qu-unit=" + Grocy.EditObjectId.toString());
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.error(xhr);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
Reference in New Issue
Block a user