From 33d6396a4eeba699c3b81b5b1ad43be847147b91 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 6 Jan 2021 21:00:45 +1100 Subject: [PATCH] Exclude expired stock from builds --- InvenTree/build/models.py | 6 ++++++ InvenTree/build/views.py | 1 + InvenTree/stock/test_views.py | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 488a8b79e8..96101adafe 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -27,6 +27,8 @@ from InvenTree.helpers import increment, getSetting, normalize from InvenTree.validators import validate_build_order_reference from InvenTree.models import InvenTreeAttachment +from common.models import InvenTreeSetting + import InvenTree.fields from stock import models as StockModels @@ -819,6 +821,10 @@ class Build(MPTTModel): location__in=[loc for loc in self.take_from.getUniqueChildren()] ) + # Exclude expired stock items + if not InvenTreeSetting.get_setting('STOCK_ALLOW_EXPIRED_BUILD'): + items = items.exclude(StockModels.StockItem.EXPIRED_FILTER) + return items @property diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index 69d1dd9415..0887c49397 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -903,6 +903,7 @@ class BuildItemCreate(AjaxCreateView): if self.build and self.part: available_items = self.build.availableStockItems(self.part, self.output) + form.fields['stock_item'].queryset = available_items self.available_stock = form.fields['stock_item'].queryset.all() diff --git a/InvenTree/stock/test_views.py b/InvenTree/stock/test_views.py index 021ca9e138..b842de3836 100644 --- a/InvenTree/stock/test_views.py +++ b/InvenTree/stock/test_views.py @@ -5,6 +5,8 @@ from django.urls import reverse from django.contrib.auth import get_user_model from django.contrib.auth.models import Group +from common.models import InvenTreeSetting + import json from datetime import datetime, timedelta @@ -32,6 +34,9 @@ class StockViewTestCase(TestCase): password='password' ) + self.user.is_staff = True + self.user.save() + # Put the user into a group with the correct permissions group = Group.objects.create(name='mygroup') self.user.groups.add(group) @@ -163,6 +168,9 @@ class StockItemTest(StockViewTestCase): and should be in the future! """ + # First, ensure that the expiry date feature is enabled! + InvenTreeSetting.set_setting('STOCK_ENABLE_EXPIRY', True, self.user) + url = reverse('stock-item-create') response = self.client.get(url, {'part': 25}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')