From 9a30108b75c6e57a2ee8af2b1444b575ed719e22 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 5 Jan 2021 00:37:42 +1100 Subject: [PATCH] Auto-update the expiry date in the StockItem form when switching Part selection --- .../static/script/inventree/modals.js | 7 ++++++- InvenTree/part/serializers.py | 1 + InvenTree/templates/js/stock.js | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/static/script/inventree/modals.js b/InvenTree/InvenTree/static/script/inventree/modals.js index f731a5238b..12a496c481 100644 --- a/InvenTree/InvenTree/static/script/inventree/modals.js +++ b/InvenTree/InvenTree/static/script/inventree/modals.js @@ -151,12 +151,17 @@ function enableField(fieldName, enabled, options={}) { } function clearField(fieldName, options={}) { + + setFieldValue(fieldName, '', options); +} + +function setFieldValue(fieldName, value, options={}) { var modal = options.modal || '#modal-form'; var field = getFieldByName(modal, fieldName); - field.val(""); + field.val(value); } diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 0eebe6617d..05fc3091f7 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -289,6 +289,7 @@ class PartSerializer(InvenTreeModelSerializer): 'component', 'description', 'default_location', + 'default_expiry', 'full_name', 'image', 'in_stock', diff --git a/InvenTree/templates/js/stock.js b/InvenTree/templates/js/stock.js index af0c85fc02..ef3ddef33c 100644 --- a/InvenTree/templates/js/stock.js +++ b/InvenTree/templates/js/stock.js @@ -618,8 +618,8 @@ function loadStockTable(table, options) { if (action == 'move') { secondary.push({ field: 'destination', - label: 'New Location', - title: 'Create new location', + label: '{% trans "New Location" %}', + title: '{% trans "Create new location" %}', 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( `/api/part/${value}/`, {}, { success: function(response) { - + + // Disable serial number field if the part is not trackable enableField('serial_numbers', response.trackable); 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")); + } } } );