Implemented notes and Userfields for stock entries (closes #443)

This commit is contained in:
Bernd Bestel
2022-03-30 17:32:53 +02:00
parent 2983687f34
commit d3a39270de
18 changed files with 438 additions and 107 deletions

View File

@@ -171,6 +171,80 @@ Grocy.Components.UserfieldsForm.Load = function()
);
}
Grocy.Components.UserfieldsForm.Clear = function()
{
if (!$("#userfields-form").length)
{
return;
}
Grocy.Api.Get('objects/userfields?query[]=entity=' + $("#userfields-form").data("entity"),
function(result)
{
$.each(result, function(key, userfield)
{
var input = $(".userfield-input[data-userfield-name='" + userfield.name + "']");
if (input.attr("type") == "checkbox")
{
input.prop("checked", false);
}
else if (input.hasAttr("multiple"))
{
input.val("");
$(".selectpicker").selectpicker("render");
}
else if (input.attr('type') == "file")
{
var formGroup = input.parent().parent().parent();
formGroup.find("label.custom-file-label").text("");
formGroup.find(".userfield-file-show").attr('href', U('/files/userfiles/' + value));
formGroup.find('.userfield-file-show').removeClass('d-none');
formGroup.find('img.userfield-current-file')
.attr('src', U('/files/userfiles/' + value + '?force_serve_as=picture&best_fit_width=250&best_fit_height=250'));
LoadImagesLazy();
formGroup.find('.userfield-file-delete').click(
function()
{
formGroup.find("label.custom-file-label").text(__t("No file selected"));
formGroup.find(".userfield-file-show").addClass('d-none');
input.attr('data-old-file', "");
}
);
input.on("change", function(e)
{
formGroup.find(".userfield-file-show").addClass('d-none');
});
}
else if (input.attr("data-userfield-type") == "link")
{
var formRow = input.parent().parent();
formRow.find(".userfield-link-title").val(data.title);
formRow.find(".userfield-link-link").val(data.link);
input.val("");
}
else
{
input.val("");
}
});
$("form").each(function()
{
Grocy.FrontendHelpers.ValidateForm(this.id);
});
},
function(xhr)
{
console.error(xhr);
}
);
}
$(".userfield-link").keyup(function(e)
{
var formRow = $(this).parent().parent();

View File

@@ -286,10 +286,16 @@ function OnLocationChange(locationId, stockId)
{
if ($("#specific_stock_entry option[value='" + stockEntry.stock_id + "']").length == 0)
{
var noteTxt = "";
if (stockEntry.note != null && !stockEntry.note.isEmpty())
{
noteTxt = " " + stockEntry.note;
}
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount: %1$s; Due on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
text: __t("Amount: %1$s; Due on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt + noteTxt
}));
}

View File

@@ -27,6 +27,7 @@
var jsonData = {};
jsonData.new_amount = jsonForm.amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue();
jsonData.note = jsonForm.note;
jsonData.stock_label_type = jsonForm.stock_label_type;
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
@@ -116,6 +117,7 @@
}
}
Grocy.EditObjectId = result[0].transaction_id;
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(result)
{
@@ -123,39 +125,48 @@
if (GetUriParam("embedded") !== undefined)
{
window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
Grocy.Components.UserfieldsForm.Save(function()
{
window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
});
}
else
{
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
toastr.success(successMessage);
Grocy.Components.ProductPicker.FinishFlow();
Grocy.Components.ProductAmountPicker.Reset();
$('#inventory-change-info').addClass('d-none');
$("#tare-weight-handling-info").addClass("d-none");
$("#display_amount").attr("min", "0");
$('#display_amount').val('');
$('#display_amount').removeAttr("data-not-equal");
$(".input-group-productamountpicker").trigger("change");
$('#price').val('');
Grocy.Components.DateTimePicker.Clear();
Grocy.Components.ProductPicker.SetValue('');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
Grocy.Components.UserfieldsForm.Save(function()
{
Grocy.Components.ShoppingLocationPicker.SetValue('');
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
toastr.success(successMessage);
Grocy.Components.ProductPicker.FinishFlow();
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABEL_PRINTER)
{
$("#stock_label_type").val(0);
}
Grocy.Components.ProductAmountPicker.Reset();
$('#inventory-change-info').addClass('d-none');
$("#tare-weight-handling-info").addClass("d-none");
$("#display_amount").attr("min", "0");
$('#display_amount').val('');
$('#display_amount').removeAttr("data-not-equal");
$(".input-group-productamountpicker").trigger("change");
$('#price').val('');
$('#note').val('');
Grocy.Components.DateTimePicker.Clear();
Grocy.Components.ProductPicker.SetValue('');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
Grocy.Components.ShoppingLocationPicker.SetValue('');
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
Grocy.Components.UserfieldsForm.Clear();
Grocy.FrontendHelpers.ValidateForm('inventory-form');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABEL_PRINTER)
{
$("#stock_label_type").val(0);
}
Grocy.FrontendHelpers.ValidateForm('inventory-form');
});
}
},
function(xhr)

View File

@@ -28,6 +28,7 @@ $('#save-purchase-button').on('click', function(e)
{
var jsonData = {};
jsonData.amount = jsonForm.amount;
jsonData.note = jsonForm.note;
jsonData.stock_label_type = jsonForm.stock_label_type;
if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
@@ -165,61 +166,70 @@ $('#save-purchase-button').on('click', function(e)
}
}
Grocy.EditObjectId = result[0].transaction_id;
if (GetUriParam("embedded") !== undefined)
{
window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("AfterItemAdded", GetUriParam("listitemid")), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("Ready"), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
Grocy.Components.UserfieldsForm.Save(function()
{
window.parent.postMessage(WindowMessageBag("ProductChanged", jsonForm.product_id), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("AfterItemAdded", GetUriParam("listitemid")), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("ShowSuccessMessage", successMessage), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("Ready"), Grocy.BaseUrl);
window.parent.postMessage(WindowMessageBag("CloseAllModals"), Grocy.BaseUrl);
});
}
else
{
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
toastr.success(successMessage);
Grocy.Components.ProductPicker.FinishFlow();
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING && BoolVal(Grocy.UserSettings.show_warning_on_purchase_when_due_date_is_earlier_than_next))
Grocy.Components.UserfieldsForm.Save(function()
{
if (moment(jsonData.best_before_date).isBefore(CurrentProductDetails.next_due_date))
Grocy.FrontendHelpers.EndUiBusy("purchase-form");
toastr.success(successMessage);
Grocy.Components.ProductPicker.FinishFlow();
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING && BoolVal(Grocy.UserSettings.show_warning_on_purchase_when_due_date_is_earlier_than_next))
{
toastr.warning(__t("This is due earlier than already in-stock items"));
if (moment(jsonData.best_before_date).isBefore(CurrentProductDetails.next_due_date))
{
toastr.warning(__t("This is due earlier than already in-stock items"));
}
}
}
Grocy.Components.ProductAmountPicker.Reset();
$("#purchase-form").removeAttr("data-used-barcode");
$("#display_amount").attr("min", Grocy.DefaultMinAmount);
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_purchase_amount));
$(".input-group-productamountpicker").trigger("change");
$('#price').val('');
$("#tare-weight-handling-info").addClass("d-none");
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
Grocy.Components.LocationPicker.Clear();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
Grocy.Components.DateTimePicker.Clear();
}
Grocy.Components.ProductPicker.SetValue('');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
Grocy.Components.ShoppingLocationPicker.SetValue('');
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABEL_PRINTER)
{
$("#stock_label_type").val(0);
}
Grocy.Components.ProductAmountPicker.Reset();
$("#purchase-form").removeAttr("data-used-barcode");
$("#display_amount").attr("min", Grocy.DefaultMinAmount);
$('#display_amount').val(parseFloat(Grocy.UserSettings.stock_default_purchase_amount));
$(".input-group-productamountpicker").trigger("change");
$('#price').val('');
$("#tare-weight-handling-info").addClass("d-none");
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
Grocy.Components.LocationPicker.Clear();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
Grocy.Components.DateTimePicker.Clear();
}
Grocy.Components.ProductPicker.SetValue('');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
Grocy.Components.ShoppingLocationPicker.SetValue('');
}
Grocy.Components.ProductPicker.GetInputElement().focus();
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_LABEL_PRINTER)
{
$("#stock_label_type").val(0);
}
$('#price-hint').text("");
var priceTypeUnitPrice = $("#price-type-unit-price");
var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]");
priceTypeUnitPriceLabel.text(__t("Unit price"));
$('#price-hint').text("");
$('#note').val("");
var priceTypeUnitPrice = $("#price-type-unit-price");
var priceTypeUnitPriceLabel = $("[for=" + priceTypeUnitPrice.attr("id") + "]");
priceTypeUnitPriceLabel.text(__t("Unit price"));
Grocy.Components.UserfieldsForm.Clear();
Grocy.FrontendHelpers.ValidateForm('purchase-form');
Grocy.FrontendHelpers.ValidateForm('purchase-form');
});
}
},
function(xhr)

View File

@@ -217,6 +217,7 @@ function RefreshStockEntryRow(stockRowId)
);
$('#stock-' + stockRowId + '-price').text(result.price);
$('#stock-' + stockRowId + '-note').text(result.note);
$('#stock-' + stockRowId + '-purchased-date').text(result.purchased_date);
$('#stock-' + stockRowId + '-purchased-date-timeago').attr('datetime', result.purchased_date + ' 23:59:59');

View File

@@ -24,6 +24,7 @@
jsonData.amount = jsonForm.amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue();
jsonData.purchased_date = Grocy.Components.DateTimePicker2.GetValue();
jsonData.note = jsonForm.note;
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
jsonData.shopping_location_id = Grocy.Components.ShoppingLocationPicker.GetValue();

View File

@@ -315,10 +315,16 @@ $("#location_id_from").on('change', function(e)
{
if ($("#specific_stock_entry option[value='" + stockEntry.stock_id + "']").length == 0)
{
var noteTxt = "";
if (stockEntry.note != null && !stockEntry.note.isEmpty())
{
noteTxt = " " + stockEntry.note;
}
$("#specific_stock_entry").append($("<option>", {
value: stockEntry.stock_id,
amount: stockEntry.amount,
text: __t("Amount: %1$s; Due on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt
text: __t("Amount: %1$s; Due on %2$s; Bought on %3$s", stockEntry.amount, moment(stockEntry.best_before_date).format("YYYY-MM-DD"), moment(stockEntry.purchased_date).format("YYYY-MM-DD")) + "; " + openTxt + noteTxt
}));
}