Auto-update the expiry date in the StockItem form when switching Part selection

This commit is contained in:
Oliver Walters 2021-01-05 00:37:42 +11:00
parent 9dc9c0fcb7
commit 9a30108b75
3 changed files with 22 additions and 5 deletions

View File

@ -152,11 +152,16 @@ function enableField(fieldName, enabled, options={}) {
function clearField(fieldName, options={}) { function clearField(fieldName, options={}) {
setFieldValue(fieldName, '', options);
}
function setFieldValue(fieldName, value, options={}) {
var modal = options.modal || '#modal-form'; var modal = options.modal || '#modal-form';
var field = getFieldByName(modal, fieldName); var field = getFieldByName(modal, fieldName);
field.val(""); field.val(value);
} }

View File

@ -289,6 +289,7 @@ class PartSerializer(InvenTreeModelSerializer):
'component', 'component',
'description', 'description',
'default_location', 'default_location',
'default_expiry',
'full_name', 'full_name',
'image', 'image',
'in_stock', 'in_stock',

View File

@ -618,8 +618,8 @@ function loadStockTable(table, options) {
if (action == 'move') { if (action == 'move') {
secondary.push({ secondary.push({
field: 'destination', field: 'destination',
label: 'New Location', label: '{% trans "New Location" %}',
title: 'Create new location', title: '{% trans "Create new location" %}',
url: "/stock/location/new/", url: "/stock/location/new/",
}); });
} }
@ -837,14 +837,25 @@ function createNewStockItem(options) {
} }
); );
// Disable serial number field if the part is not trackable // Request part information from the server
inventreeGet( inventreeGet(
`/api/part/${value}/`, {}, `/api/part/${value}/`, {},
{ {
success: function(response) { success: function(response) {
// Disable serial number field if the part is not trackable
enableField('serial_numbers', response.trackable); enableField('serial_numbers', response.trackable);
clearField('serial_numbers'); clearField('serial_numbers');
// 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"));
}
} }
} }
); );