Stock item duplication now works with the API forms

This commit is contained in:
Oliver 2021-11-03 07:28:21 +11:00
parent aaf27d4098
commit ad4c4f2a6d
2 changed files with 27 additions and 7 deletions

View File

@ -463,11 +463,9 @@ $("#print-label").click(function() {
{% if roles.stock.change %} {% if roles.stock.change %}
$("#stock-duplicate").click(function() { $("#stock-duplicate").click(function() {
createNewStockItem({ // Duplicate a stock item
duplicateStockItem({{ item.pk }}, {
follow: true, follow: true,
data: {
copy: {{ item.id }},
}
}); });
}); });

View File

@ -106,6 +106,8 @@ function createStockLocation(options={}) {
function stockItemFields(options={}) { function stockItemFields(options={}) {
var fields = { var fields = {
part: { part: {
// Hide the part field unless we are "creating" a new stock item
hidden: !options.create,
onSelect: function(data, field, opts) { onSelect: function(data, field, opts) {
// Callback when a new "part" is selected // Callback when a new "part" is selected
@ -205,6 +207,29 @@ function stockItemGroups(options={}) {
} }
/*
* Launch a modal form to duplicate a given StockItem
*/
function duplicateStockItem(pk, options) {
// First, we need the StockItem informatino
inventreeGet(`/api/stock/${pk}/`, {}, {
success: function(data) {
options.create = true;
options.data = data;
options.method = 'POST';
options.fields = stockItemFields(options);
options.groups = stockItemGroups(options);
options.title = '{% trans "Duplicate Stock Item" %}';
constructForm('{% url "api-stock-list" %}', options);
}
});
}
/* /*
* Launch a modal form to edit a given StockItem * Launch a modal form to edit a given StockItem
*/ */
@ -212,9 +237,6 @@ function editStockItem(pk, options={}) {
var url = `/api/stock/${pk}/`; var url = `/api/stock/${pk}/`;
// Prevent editing of the "part"
fields.part.hidden = true;
options.create = false; options.create = false;
options.fields = stockItemFields(options); options.fields = stockItemFields(options);