Files
grocy/public/viewjs/inventory.js

331 lines
11 KiB
JavaScript
Raw Normal View History

2017-04-20 17:10:21 +02:00
$('#save-inventory-button').on('click', function(e)
{
e.preventDefault();
var jsonForm = $('#inventory-form').serializeJSON();
Grocy.FrontendHelpers.BeginUiBusy("inventory-form");
2017-04-20 17:10:21 +02:00
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(productDetails)
2017-04-20 17:10:21 +02:00
{
var price = "";
if (!jsonForm.price.toString().isEmpty())
{
price = parseFloat(jsonForm.price).toFixed(Grocy.UserSettings.stock_decimal_places_prices);
}
2020-08-30 12:18:16 +02:00
var jsonData = {};
jsonData.new_amount = jsonForm.new_amount;
jsonData.best_before_date = Grocy.Components.DateTimePicker.GetValue();
2020-08-30 12:18:16 +02:00
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
jsonData.shopping_location_id = Grocy.Components.ShoppingLocationPicker.GetValue();
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
jsonData.location_id = Grocy.Components.LocationPicker.GetValue();
}
if (Grocy.UserSettings.show_purchased_date_on_purchase)
{
jsonData.purchased_date = Grocy.Components.DateTimePicker2.GetValue();
}
jsonData.price = price;
var bookingResponse = null;
Grocy.Api.Post('stock/products/' + jsonForm.product_id + '/inventory', jsonData,
function(result)
{
bookingResponse = result;
2018-04-14 11:10:38 +02:00
var addBarcode = GetUriParam('addbarcodetoselection');
if (addBarcode !== undefined)
{
Qu factor purchase to stock & Product Barcode Details (#801) * Puchase add qu_factor_to_stock * qu_factor_purchase_to_stock for stock edit * product barcodes with QU and Stores * remove product barcode tags * migrations/0103 add value and factor_puchase_amount to stock_current and stock_current_location_content * Remove unused method * StockService#GetProductDetails: include stock_value * productcard: include stock_value * Add Purchase Factor to Stock Overview * update demo data with stock qu_factor_purchase_to_stock * recipes_pos_resolved update * avg_price and oldest_price in product details * add average price to product card * hint for recipe costs not included if not in stock * Round value and factor_purchas_amount. Include currency for stock value * Add factor_purchase_amount to product card stock amount * Allow editing qu_factor_purchase_to_stock for stock entries * fix update qu_factor_purchase_to_stock for Transfers * Add barcode to existing product update to add to product_barcodes table * Add barcode to new product workflow update to add to product_barcodes table * *** Price now saved as 1 QU to stock in stock tables *** * remove column product barcode and use product_barcodes * Allow products to be deactivated instead of deleted * Embedded barcode and qu-conversion with page reload on change * Save current product barcode into new product_barcodes table * Embedded popup for product group add/edit * barcode scanner added to product barcodes input * Edit product qu_stock is unavailable after first purchase * StockOverview: Filters break when columns are reordered so for now just disable colReorder * view stockoverview.blade: display product_group column * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-08-17 14:47:33 -05:00
var jsonDataBarcode = {};
jsonDataBarcode.barcode = addBarcode;
jsonDataBarcode.product_id = jsonForm.product_id;
2020-08-30 12:18:16 +02:00
jsonDataBarcode.qu_factor_purchase_to_stock = productDetails.product.qu_factor_purchase_to_stock;
Qu factor purchase to stock & Product Barcode Details (#801) * Puchase add qu_factor_to_stock * qu_factor_purchase_to_stock for stock edit * product barcodes with QU and Stores * remove product barcode tags * migrations/0103 add value and factor_puchase_amount to stock_current and stock_current_location_content * Remove unused method * StockService#GetProductDetails: include stock_value * productcard: include stock_value * Add Purchase Factor to Stock Overview * update demo data with stock qu_factor_purchase_to_stock * recipes_pos_resolved update * avg_price and oldest_price in product details * add average price to product card * hint for recipe costs not included if not in stock * Round value and factor_purchas_amount. Include currency for stock value * Add factor_purchase_amount to product card stock amount * Allow editing qu_factor_purchase_to_stock for stock entries * fix update qu_factor_purchase_to_stock for Transfers * Add barcode to existing product update to add to product_barcodes table * Add barcode to new product workflow update to add to product_barcodes table * *** Price now saved as 1 QU to stock in stock tables *** * remove column product barcode and use product_barcodes * Allow products to be deactivated instead of deleted * Embedded barcode and qu-conversion with page reload on change * Save current product barcode into new product_barcodes table * Embedded popup for product group add/edit * barcode scanner added to product barcodes input * Edit product qu_stock is unavailable after first purchase * StockOverview: Filters break when columns are reordered so for now just disable colReorder * view stockoverview.blade: display product_group column * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-08-17 14:47:33 -05:00
jsonDataBarcode.shopping_location_id = jsonForm.shopping_location_id;
Qu factor purchase to stock & Product Barcode Details (#801) * Puchase add qu_factor_to_stock * qu_factor_purchase_to_stock for stock edit * product barcodes with QU and Stores * remove product barcode tags * migrations/0103 add value and factor_puchase_amount to stock_current and stock_current_location_content * Remove unused method * StockService#GetProductDetails: include stock_value * productcard: include stock_value * Add Purchase Factor to Stock Overview * update demo data with stock qu_factor_purchase_to_stock * recipes_pos_resolved update * avg_price and oldest_price in product details * add average price to product card * hint for recipe costs not included if not in stock * Round value and factor_purchas_amount. Include currency for stock value * Add factor_purchase_amount to product card stock amount * Allow editing qu_factor_purchase_to_stock for stock entries * fix update qu_factor_purchase_to_stock for Transfers * Add barcode to existing product update to add to product_barcodes table * Add barcode to new product workflow update to add to product_barcodes table * *** Price now saved as 1 QU to stock in stock tables *** * remove column product barcode and use product_barcodes * Allow products to be deactivated instead of deleted * Embedded barcode and qu-conversion with page reload on change * Save current product barcode into new product_barcodes table * Embedded popup for product group add/edit * barcode scanner added to product barcodes input * Edit product qu_stock is unavailable after first purchase * StockOverview: Filters break when columns are reordered so for now just disable colReorder * view stockoverview.blade: display product_group column * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-08-17 14:47:33 -05:00
Grocy.Api.Post('objects/product_barcodes', jsonDataBarcode,
function(result)
{
$("#flow-info-addbarcodetoselection").addClass("d-none");
$('#barcode-lookup-disabled-hint').addClass('d-none');
2020-11-09 21:30:22 +01:00
$('#barcode-lookup-hint').removeClass('d-none');
2020-08-30 12:18:16 +02:00
window.history.replaceState({}, document.title, U("/inventory"));
},
function(xhr)
{
Qu factor purchase to stock & Product Barcode Details (#801) * Puchase add qu_factor_to_stock * qu_factor_purchase_to_stock for stock edit * product barcodes with QU and Stores * remove product barcode tags * migrations/0103 add value and factor_puchase_amount to stock_current and stock_current_location_content * Remove unused method * StockService#GetProductDetails: include stock_value * productcard: include stock_value * Add Purchase Factor to Stock Overview * update demo data with stock qu_factor_purchase_to_stock * recipes_pos_resolved update * avg_price and oldest_price in product details * add average price to product card * hint for recipe costs not included if not in stock * Round value and factor_purchas_amount. Include currency for stock value * Add factor_purchase_amount to product card stock amount * Allow editing qu_factor_purchase_to_stock for stock entries * fix update qu_factor_purchase_to_stock for Transfers * Add barcode to existing product update to add to product_barcodes table * Add barcode to new product workflow update to add to product_barcodes table * *** Price now saved as 1 QU to stock in stock tables *** * remove column product barcode and use product_barcodes * Allow products to be deactivated instead of deleted * Embedded barcode and qu-conversion with page reload on change * Save current product barcode into new product_barcodes table * Embedded popup for product group add/edit * barcode scanner added to product barcodes input * Edit product qu_stock is unavailable after first purchase * StockOverview: Filters break when columns are reordered so for now just disable colReorder * view stockoverview.blade: display product_group column * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
2020-08-17 14:47:33 -05:00
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response);
}
);
}
Grocy.Api.Get('stock/products/' + jsonForm.product_id,
function(result)
{
var successMessage = __t('Stock amount of %1$s is now %2$s', result.product.name, result.stock_amount + " " + __n(result.stock_amount, result.quantity_unit_stock.name, result.quantity_unit_stock.name_plural)) + '<br><a class="btn btn-secondary btn-sm mt-2" href="#" onclick="UndoStockTransaction(\'' + bookingResponse.transaction_id + '\')"><i class="fas fa-undo"></i> ' + __t("Undo") + '</a>';
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);
}
else
{
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
toastr.success(successMessage);
2017-04-20 17:10:21 +02:00
$('#inventory-change-info').addClass('d-none');
$("#tare-weight-handling-info").addClass("d-none");
$("#new_amount").attr("min", "0");
$("#new_amount").attr("step", "1");
$("#new_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %s', '0'));
$('#new_amount').val('');
$('#new_amount_qu_unit').text("");
$('#price').val('');
Grocy.Components.DateTimePicker.Clear();
Grocy.Components.ProductPicker.SetValue('');
2020-08-30 12:18:16 +02:00
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
Grocy.Components.ShoppingLocationPicker.SetValue('');
}
Grocy.Components.ProductPicker.GetInputElement().focus();
2020-01-17 11:13:43 -06:00
Grocy.Components.ProductCard.Refresh(jsonForm.product_id);
Grocy.FrontendHelpers.ValidateForm('inventory-form');
}
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy();
console.error(xhr);
}
);
2017-04-20 17:10:21 +02:00
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
2017-04-20 17:10:21 +02:00
console.error(xhr);
}
);
},
function(xhr)
{
Grocy.FrontendHelpers.EndUiBusy("inventory-form");
2017-04-20 17:10:21 +02:00
console.error(xhr);
}
);
});
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetPicker().on('change', function(e)
2017-04-20 17:10:21 +02:00
{
var productId = $(e.target).val();
if (productId)
{
2018-04-14 11:10:38 +02:00
Grocy.Components.ProductCard.Refresh(productId);
Grocy.Api.Get('stock/products/' + productId,
2017-04-20 17:10:21 +02:00
function(productDetails)
{
$('#new_amount').attr('data-not-equal', productDetails.stock_amount);
2017-04-20 17:10:21 +02:00
$('#new_amount_qu_unit').text(productDetails.quantity_unit_stock.name);
if (productDetails.product.allow_partial_units_in_stock == 1)
{
$("#new_amount").attr("min", "0." + "0".repeat(parseInt(Grocy.UserSettings.stock_decimal_places_amounts) - 1) + "1");
$("#amount").attr("step", "." + "0".repeat(parseInt(Grocy.UserSettings.stock_decimal_places_amounts) - 1) + "1");
$("#new_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %1$s or equal %2$s', "0." + "0".repeat(parseInt(Grocy.UserSettings.stock_decimal_places_amounts) - 1) + "1", productDetails.stock_amount.toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts })));
}
else
{
$("#new_amount").attr("min", "0");
$("#new_amount").attr("step", "1");
$("#new_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %1$s or equal %2$s', '0', productDetails.stock_amount.toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts })));
}
if (productDetails.product.enable_tare_weight_handling == 1)
{
$("#new_amount").attr("min", productDetails.product.tare_weight);
$("#new_amount").parent().find(".invalid-feedback").text(__t('The amount cannot be lower than %1$s or equal %2$s', parseFloat(productDetails.product.tare_weight).toLocaleString({ minimumFractionDigits: 0, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_amounts }), (parseFloat(productDetails.stock_amount) + parseFloat(productDetails.product.tare_weight)).toLocaleString()));
$("#tare-weight-handling-info").removeClass("d-none");
}
else
{
$("#tare-weight-handling-info").addClass("d-none");
}
$('#price').val(parseFloat(productDetails.last_price).toLocaleString({ minimumFractionDigits: 2, maximumFractionDigits: Grocy.UserSettings.stock_decimal_places_prices }));
2020-08-30 12:18:16 +02:00
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_PRICE_TRACKING)
{
Grocy.Components.ShoppingLocationPicker.SetId(productDetails.last_shopping_location_id);
}
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
Grocy.Components.LocationPicker.SetId(productDetails.location.id);
}
$('#new_amount').val(productDetails.stock_amount);
$('#new_amount').focus();
$('#new_amount').trigger('keyup');
2017-04-20 17:10:21 +02:00
},
function(xhr)
{
console.error(xhr);
}
);
}
});
2018-04-16 19:11:32 +02:00
$('#new_amount').val('');
Grocy.FrontendHelpers.ValidateForm('inventory-form');
2017-04-20 17:10:21 +02:00
2018-07-14 14:43:57 +02:00
if (Grocy.Components.ProductPicker.InProductAddWorkflow() === false)
{
Grocy.Components.ProductPicker.GetInputElement().focus();
}
else
{
Grocy.Components.ProductPicker.GetPicker().trigger('change');
}
2018-04-16 19:11:32 +02:00
$('#new_amount').on('focus', function(e)
{
2018-07-14 14:43:57 +02:00
if (Grocy.Components.ProductPicker.GetValue().length === 0)
{
2018-07-14 14:43:57 +02:00
Grocy.Components.ProductPicker.GetInputElement().focus();
2018-04-16 19:11:32 +02:00
}
else
{
$(this).select();
}
});
2020-08-30 12:18:16 +02:00
$('#inventory-form input').keyup(function(event)
{
Grocy.FrontendHelpers.ValidateForm('inventory-form');
});
2018-04-16 19:11:32 +02:00
$('#inventory-form input').keydown(function(event)
{
if (event.keyCode === 13) //Enter
{
event.preventDefault();
if (document.getElementById('inventory-form').checkValidity() === false) //There is at least one validation error
{
2018-04-16 19:11:32 +02:00
return false;
}
else
{
$('#save-inventory-button').click();
}
}
2018-04-16 19:11:32 +02:00
});
2017-11-10 22:45:53 +01:00
$('#new_amount').on('keypress', function(e)
{
$('#new_amount').trigger('change');
});
Grocy.Components.DateTimePicker.GetInputElement().on('change', function(e)
2017-04-20 17:10:21 +02:00
{
Grocy.FrontendHelpers.ValidateForm('inventory-form');
2017-04-20 17:10:21 +02:00
});
Grocy.Components.DateTimePicker.GetInputElement().on('keypress', function(e)
2017-04-20 17:10:21 +02:00
{
Grocy.FrontendHelpers.ValidateForm('inventory-form');
2017-04-20 17:10:21 +02:00
});
$('#new_amount').on('keyup', function(e)
2017-04-20 17:10:21 +02:00
{
2018-07-14 14:43:57 +02:00
var productId = Grocy.Components.ProductPicker.GetValue();
var newAmount = parseInt($('#new_amount').val());
2017-04-20 17:10:21 +02:00
if (productId)
{
Grocy.Api.Get('stock/products/' + productId,
2017-04-20 17:10:21 +02:00
function(productDetails)
{
var productStockAmount = parseFloat(productDetails.stock_amount || parseFloat('0'));
var containerWeight = parseFloat("0");
if (productDetails.product.enable_tare_weight_handling == 1)
{
containerWeight = parseFloat(productDetails.product.tare_weight);
}
var estimatedBookingAmount = Math.abs(newAmount - productStockAmount - containerWeight);
$('#inventory-change-info').removeClass('d-none');
if (productDetails.product.enable_tare_weight_handling == 1 && newAmount < containerWeight)
2017-04-20 17:10:21 +02:00
{
$('#inventory-change-info').addClass('d-none');
2017-04-20 17:10:21 +02:00
}
else if (newAmount > productStockAmount + containerWeight)
2017-04-20 17:10:21 +02:00
{
$('#inventory-change-info').text(__t('This means %s will be added to stock', estimatedBookingAmount.toLocaleString() + ' ' + __n(estimatedBookingAmount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural)));
Grocy.Components.DateTimePicker.GetInputElement().attr('required', '');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
Grocy.Components.LocationPicker.GetInputElement().attr('required', '');
}
2017-04-20 17:10:21 +02:00
}
else if (newAmount < productStockAmount + containerWeight)
2017-04-20 17:10:21 +02:00
{
$('#inventory-change-info').text(__t('This means %s will be removed from stock', estimatedBookingAmount.toLocaleString() + ' ' + __n(estimatedBookingAmount, productDetails.quantity_unit_stock.name, productDetails.quantity_unit_stock.name_plural)));
Grocy.Components.DateTimePicker.GetInputElement().removeAttr('required');
if (Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_LOCATION_TRACKING)
{
Grocy.Components.LocationPicker.GetInputElement().removeAttr('required');
}
}
if (!Grocy.FeatureFlags.GROCY_FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_TRACKING)
{
Grocy.Components.DateTimePicker.GetInputElement().removeAttr('required');
2017-04-20 17:10:21 +02:00
}
Grocy.FrontendHelpers.ValidateForm('inventory-form');
2017-04-20 17:10:21 +02:00
},
function(xhr)
{
console.error(xhr);
}
);
}
});
function UndoStockBooking(bookingId)
{
2020-08-30 12:18:16 +02:00
Grocy.Api.Post('stock/bookings/' + bookingId.toString() + '/undo', {},
function(result)
{
toastr.success(__t("Booking successfully undone"));
},
function(xhr)
{
console.error(xhr);
}
);
};
function UndoStockTransaction(transactionId)
{
2020-08-30 12:18:16 +02:00
Grocy.Api.Post('stock/transactions/' + transactionId.toString() + '/undo', {},
function(result)
{
toastr.success(__t("Transaction successfully undone"));
},
2020-08-30 12:18:16 +02:00
function(xhr)
{
console.error(xhr);
}
);
};