2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-19 13:35:40 +00:00

Merge pull request #2198 from SchrodingersGat/stock-item-forms

Stock item forms
This commit is contained in:
Oliver
2021-11-05 07:37:33 +11:00
committed by GitHub
20 changed files with 777 additions and 532 deletions

View File

@ -111,7 +111,13 @@ $(document).ready(function () {
// notifications
{% if messages %}
{% for message in messages %}
showAlertOrCache('{{ message }}', 'info', true);
showAlertOrCache(
'{{ message }}',
true,
{
style: 'info',
}
);
{% endfor %}
{% endif %}

View File

@ -10,7 +10,6 @@
modalSetSubmitText,
modalShowSubmitButton,
modalSubmit,
showAlertOrCache,
showQuestionDialog,
*/
@ -480,10 +479,13 @@ function barcodeCheckIn(location_id) {
$(modal).modal('hide');
if (status == 'success' && 'success' in response) {
showAlertOrCache(response.success, 'success', true);
addCachedAlert(response.success);
location.reload();
} else {
showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
showMessage('{% trans "Error transferring stock" %}', {
style: 'danger',
icon: 'fas fa-times-circle',
});
}
}
}
@ -604,10 +606,12 @@ function scanItemsIntoLocation(item_id_list, options={}) {
$(modal).modal('hide');
if (status == 'success' && 'success' in response) {
showAlertOrCache(response.success, 'success', true);
addCachedAlert(response.success);
location.reload();
} else {
showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false);
showMessage('{% trans "Error transferring stock" %}', {
style: 'danger',
});
}
}
}

View File

@ -25,7 +25,12 @@
*/
/* exported
setFormGroupVisibility
clearFormInput,
disableFormInput,
enableFormInput,
hideFormInput,
setFormGroupVisibility,
showFormInput,
*/
/**
@ -113,6 +118,10 @@ function canDelete(OPTIONS) {
*/
function getApiEndpointOptions(url, callback) {
if (!url) {
return;
}
// Return the ajax request object
$.ajax({
url: url,
@ -182,6 +191,7 @@ function constructChangeForm(fields, options) {
// Request existing data from the API endpoint
$.ajax({
url: options.url,
data: options.params || {},
type: 'GET',
contentType: 'application/json',
dataType: 'json',
@ -197,6 +207,17 @@ function constructChangeForm(fields, options) {
fields[field].value = data[field];
}
}
// An optional function can be provided to process the returned results,
// before they are rendered to the form
if (options.processResults) {
var processed = options.processResults(data, fields, options);
// If the processResults function returns data, it will be stored
if (processed) {
data = processed;
}
}
// Store the entire data object
options.instance = data;
@ -713,6 +734,8 @@ function submitFormData(fields, options) {
break;
default:
$(options.modal).modal('hide');
console.log(`upload error at ${options.url}`);
showApiError(xhr, options.url);
break;
}
@ -890,19 +913,19 @@ function handleFormSuccess(response, options) {
// Display any messages
if (response && response.success) {
showAlertOrCache(response.success, 'success', cache);
showAlertOrCache(response.success, cache, {style: 'success'});
}
if (response && response.info) {
showAlertOrCache(response.info, 'info', cache);
showAlertOrCache(response.info, cache, {style: 'info'});
}
if (response && response.warning) {
showAlertOrCache(response.warning, 'warning', cache);
showAlertOrCache(response.warning, cache, {style: 'warning'});
}
if (response && response.danger) {
showAlertOrCache(response.danger, 'dagner', cache);
showAlertOrCache(response.danger, cache, {style: 'danger'});
}
if (options.onSuccess) {
@ -1241,6 +1264,35 @@ function initializeGroups(fields, options) {
}
}
// Clear a form input
function clearFormInput(name, options) {
updateFieldValue(name, null, {}, options);
}
// Disable a form input
function disableFormInput(name, options) {
$(options.modal).find(`#id_${name}`).prop('disabled', true);
}
// Enable a form input
function enableFormInput(name, options) {
$(options.modal).find(`#id_${name}`).prop('disabled', false);
}
// Hide a form input
function hideFormInput(name, options) {
$(options.modal).find(`#div_id_${name}`).hide();
}
// Show a form input
function showFormInput(name, options) {
$(options.modal).find(`#div_id_${name}`).show();
}
// Hide a form group
function hideFormGroup(group, options) {
$(options.modal).find(`#form-panel-${group}`).hide();

View File

@ -399,19 +399,19 @@ function afterForm(response, options) {
// Display any messages
if (response.success) {
showAlertOrCache(response.success, 'success', cache);
showAlertOrCache(response.success, cache, {style: 'success'});
}
if (response.info) {
showAlertOrCache(response.info, 'info', cache);
showAlertOrCache(response.info, cache, {style: 'info'});
}
if (response.warning) {
showAlertOrCache(response.warning, 'warning', cache);
showAlertOrCache(response.warning, cache, {style: 'warning'});
}
if (response.danger) {
showAlertOrCache(response.danger, 'danger', cache);
showAlertOrCache(response.danger, cache, {style: 'danger'});
}
// Was a callback provided?

View File

@ -4,9 +4,6 @@
/* globals
attachSelect,
enableField,
clearField,
clearFieldOptions,
closeModal,
constructField,
constructFormBody,
@ -33,10 +30,8 @@
printStockItemLabels,
printTestReports,
renderLink,
reloadFieldOptions,
scanItemsIntoLocation,
showAlertDialog,
setFieldValue,
setupFilterList,
showApiError,
stockStatusDisplay,
@ -44,6 +39,10 @@
/* exported
createNewStockItem,
createStockLocation,
duplicateStockItem,
editStockItem,
editStockLocation,
exportStock,
loadInstalledInTable,
loadStockLocationTable,
@ -51,20 +50,318 @@
loadStockTestResultsTable,
loadStockTrackingTable,
loadTableFilters,
locationFields,
removeStockRow,
serializeStockItem,
stockItemFields,
stockLocationFields,
stockStatusCodes,
*/
function locationFields() {
return {
/*
* Launches a modal form to serialize a particular StockItem
*/
function serializeStockItem(pk, options={}) {
var url = `/api/stock/${pk}/serialize/`;
options.method = 'POST';
options.title = '{% trans "Serialize Stock Item" %}';
options.fields = {
quantity: {},
serial_numbers: {
icon: 'fa-hashtag',
},
destination: {
icon: 'fa-sitemap',
},
notes: {},
};
constructForm(url, options);
}
function stockLocationFields(options={}) {
var fields = {
parent: {
help_text: '{% trans "Parent stock location" %}',
},
name: {},
description: {},
};
if (options.parent) {
fields.parent.value = options.parent;
}
return fields;
}
/*
* Launch an API form to edit a stock location
*/
function editStockLocation(pk, options={}) {
var url = `/api/stock/location/${pk}/`;
options.fields = stockLocationFields(options);
constructForm(url, options);
}
/*
* Launch an API form to create a new stock location
*/
function createStockLocation(options={}) {
var url = '{% url "api-location-list" %}';
options.method = 'POST';
options.fields = stockLocationFields(options);
options.title = '{% trans "New Stock Location" %}';
constructForm(url, options);
}
function stockItemFields(options={}) {
var fields = {
part: {
// Hide the part field unless we are "creating" a new stock item
hidden: !options.create,
onSelect: function(data, field, opts) {
// Callback when a new "part" is selected
// If we are "creating" a new stock item,
// change the available fields based on the part properties
if (options.create) {
// If a "trackable" part is selected, enable serial number field
if (data.trackable) {
enableFormInput('serial_numbers', opts);
// showFormInput('serial_numbers', opts);
} else {
clearFormInput('serial_numbers', opts);
disableFormInput('serial_numbers', opts);
}
// Enable / disable fields based on purchaseable status
if (data.purchaseable) {
enableFormInput('supplier_part', opts);
enableFormInput('purchase_price', opts);
enableFormInput('purchase_price_currency', opts);
} else {
clearFormInput('supplier_part', opts);
clearFormInput('purchase_price', opts);
disableFormInput('supplier_part', opts);
disableFormInput('purchase_price', opts);
disableFormInput('purchase_price_currency', opts);
}
}
}
},
supplier_part: {
icon: 'fa-building',
filters: {
part_detail: true,
supplier_detail: true,
},
adjustFilters: function(query, opts) {
var part = getFormFieldValue('part', {}, opts);
if (part) {
query.part = part;
}
return query;
}
},
location: {
icon: 'fa-sitemap',
},
quantity: {
help_text: '{% trans "Enter initial quantity for this stock item" %}',
},
serial_numbers: {
icon: 'fa-hashtag',
type: 'string',
label: '{% trans "Serial Numbers" %}',
help_text: '{% trans "Enter serial numbers for new stock (or leave blank)" %}',
required: false,
},
serial: {
icon: 'fa-hashtag',
},
status: {},
expiry_date: {},
batch: {},
purchase_price: {
icon: 'fa-dollar-sign',
},
purchase_price_currency: {},
packaging: {
icon: 'fa-box',
},
link: {
icon: 'fa-link',
},
owner: {},
delete_on_deplete: {},
};
if (options.create) {
// Use special "serial numbers" field when creating a new stock item
delete fields['serial'];
} else {
// These fields cannot be edited once the stock item has been created
delete fields['serial_numbers'];
delete fields['quantity'];
delete fields['location'];
}
// Remove stock expiry fields if feature is not enabled
if (!global_settings.STOCK_ENABLE_EXPIRY) {
delete fields['expiry_date'];
}
// Remove ownership field if feature is not enanbled
if (!global_settings.STOCK_OWNERSHIP_CONTROL) {
delete fields['owner'];
}
return fields;
}
function stockItemGroups(options={}) {
return {
};
}
/*
* 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) {
// Do not duplicate the serial number
delete data['serial'];
options.data = data;
options.create = true;
options.fields = stockItemFields(options);
options.groups = stockItemGroups(options);
options.method = 'POST';
options.title = '{% trans "Duplicate Stock Item" %}';
constructForm('{% url "api-stock-list" %}', options);
}
});
}
/*
* Launch a modal form to edit a given StockItem
*/
function editStockItem(pk, options={}) {
var url = `/api/stock/${pk}/`;
options.create = false;
options.fields = stockItemFields(options);
options.groups = stockItemGroups(options);
options.title = '{% trans "Edit Stock Item" %}';
// Query parameters for retrieving stock item data
options.params = {
part_detail: true,
supplier_part_detail: true,
};
// Augment the rendered form when we receive information about the StockItem
options.processResults = function(data, fields, options) {
if (data.part_detail.trackable) {
delete options.fields.delete_on_deplete;
} else {
// Remove serial number field if part is not trackable
delete options.fields.serial;
}
// Remove pricing fields if part is not purchaseable
if (!data.part_detail.purchaseable) {
delete options.fields.supplier_part;
delete options.fields.purchase_price;
delete options.fields.purchase_price_currency;
}
};
constructForm(url, options);
}
/*
* Launch an API form to contsruct a new stock item
*/
function createNewStockItem(options={}) {
var url = '{% url "api-stock-list" %}';
options.title = '{% trans "New Stock Item" %}';
options.method = 'POST';
options.create = true;
options.fields = stockItemFields(options);
options.groups = stockItemGroups(options);
if (!options.onSuccess) {
options.onSuccess = function(response) {
// If a single stock item has been created, follow it!
if (response.pk) {
var url = `/stock/item/${response.pk}/`;
addCachedAlert('{% trans "Created new stock item" %}', {
icon: 'fas fa-boxes',
});
window.location.href = url;
} else {
// Multiple stock items have been created (i.e. serialized stock)
var details = `
<br>{% trans "Quantity" %}: ${response.quantity}
<br>{% trans "Serial Numbers" %}: ${response.serial_numbers}
`;
showMessage('{% trans "Created multiple stock items" %}', {
icon: 'fas fa-boxes',
details: details,
});
var table = options.table || '#stock-table';
// Reload the table
$(table).bootstrapTable('refresh');
}
};
}
constructForm(url, options);
}
@ -1810,79 +2107,6 @@ function loadStockTrackingTable(table, options) {
}
function createNewStockItem(options) {
/* Launch a modal form to create a new stock item.
*
* This is really just a helper function which calls launchModalForm,
* but it does get called a lot, so here we are ...
*/
// Add in some funky options
options.callback = [
{
field: 'part',
action: function(value) {
if (!value) {
// No part chosen
clearFieldOptions('supplier_part');
enableField('serial_numbers', false);
enableField('purchase_price_0', false);
enableField('purchase_price_1', false);
return;
}
// Reload options for supplier part
reloadFieldOptions(
'supplier_part',
{
url: '{% url "api-supplier-part-list" %}',
params: {
part: value,
pretty: true,
},
text: function(item) {
return item.pretty_name;
}
}
);
// Request part information from the server
inventreeGet(
`/api/part/${value}/`, {},
{
success: function(response) {
// Disable serial number field if the part is not trackable
enableField('serial_numbers', response.trackable);
clearField('serial_numbers');
enableField('purchase_price_0', response.purchaseable);
enableField('purchase_price_1', response.purchaseable);
// Populate the expiry date
if (response.default_expiry <= 0) {
// No expiry date
clearField('expiry_date');
} else {
var expiry = moment().add(response.default_expiry, 'days');
setFieldValue('expiry_date', expiry.format('YYYY-MM-DD'));
}
}
}
);
}
},
];
launchModalForm('{% url "stock-item-create" %}', options);
}
function loadInstalledInTable(table, options) {
/*
* Display a table showing the stock items which are installed in this stock item.

View File

@ -10,17 +10,10 @@
<div id='{{ prefix }}button-toolbar'>
<div class='button-toolbar container-fluid' style='float: right;'>
<div class='btn-group'>
<div class='btn-group' role='group'>
<button class='btn btn-outline-secondary' id='stock-export' title='{% trans "Export Stock Information" %}'>
<span class='fas fa-download'></span>
</button>
{% if owner_control.value == "True" and user in owners or user.is_superuser or owner_control.value == "False" %}
{% if not read_only and not prevent_new_stock and roles.stock.add %}
<button class="btn btn-success" id='item-create' title='{% trans "New Stock Item" %}'>
<span class='fas fa-plus-circle'></span>
</button>
{% endif %}
{% if barcodes %}
<!-- Barcode actions menu -->
<div class='btn-group' role='group'>
@ -46,7 +39,7 @@
</div>
{% if not read_only %}
{% if roles.stock.change or roles.stock.delete %}
<div class="btn-group">
<div class="btn-group" role="group">
<button id='stock-options' class="btn btn-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" title='{% trans "Stock Options" %}'>
<span class='fas fa-boxes'></span> <span class="caret"></span>
</button>
@ -66,7 +59,6 @@
</div>
{% endif %}
{% endif %}
{% endif %}
{% include "filter_list.html" with id="stock" %}
</div>
</div>