mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-27 01:58:44 +00:00
[bug] fix for stale stock filter (#13383)
- Observe the STOCK_ENABLE_EXPIRY setting - Refactor into shared helper functions - Do not annotate if setting is disabled
This commit is contained in:
1 parent
864375eba9
commit
4c06cfb237
5 files changed
+91
-38
No files matched your search
@@ -21,7 +21,6 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.serializers import ValidationError
|
from rest_framework.serializers import ValidationError
|
||||||
|
|
||||||
import common.filters
|
import common.filters
|
||||||
import common.models
|
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.permissions
|
import InvenTree.permissions
|
||||||
import stock.serializers as StockSerializers
|
import stock.serializers as StockSerializers
|
||||||
@@ -1040,18 +1039,11 @@ class StockFilter(FilterSet):
|
|||||||
|
|
||||||
def filter_stale(self, queryset, name, value):
|
def filter_stale(self, queryset, name, value):
|
||||||
"""Filter by stale stock items."""
|
"""Filter by stale stock items."""
|
||||||
stale_days = common.models.InvenTreeSetting.get_setting('STOCK_STALE_DAYS')
|
stale_filter = StockItem.get_stale_filter()
|
||||||
|
|
||||||
if stale_days <= 0:
|
if stale_filter is None:
|
||||||
# No filtering, does not make sense
|
# Stale stock detection is disabled: no items are stale
|
||||||
return queryset
|
return queryset.none() if str2bool(value) else queryset
|
||||||
|
|
||||||
stale_date = InvenTree.helpers.current_date() + timedelta(days=stale_days)
|
|
||||||
stale_filter = (
|
|
||||||
StockItem.IN_STOCK_FILTER
|
|
||||||
& ~Q(expiry_date=None)
|
|
||||||
& Q(expiry_date__lt=stale_date)
|
|
||||||
)
|
|
||||||
|
|
||||||
if str2bool(value):
|
if str2bool(value):
|
||||||
return queryset.filter(stale_filter)
|
return queryset.filter(stale_filter)
|
||||||
|
|||||||
@@ -693,6 +693,41 @@ class StockItem(
|
|||||||
& Q(expiry_date__lt=InvenTree.helpers.current_date())
|
& Q(expiry_date__lt=InvenTree.helpers.current_date())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def stale_days() -> int:
|
||||||
|
"""Return the number of days before expiry at which stock is considered "stale".
|
||||||
|
|
||||||
|
Returns 0 if stale stock detection is disabled, i.e. if either:
|
||||||
|
- The stock expiry feature is disabled (STOCK_ENABLE_EXPIRY)
|
||||||
|
- The STOCK_STALE_DAYS setting is zero (or negative)
|
||||||
|
"""
|
||||||
|
if not get_global_setting('STOCK_ENABLE_EXPIRY', False):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
days = int(get_global_setting('STOCK_STALE_DAYS', 0))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return max(days, 0)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_stale_filter(cls) -> Q | None:
|
||||||
|
"""A query filter which can be used to filter StockItem objects which are stale.
|
||||||
|
|
||||||
|
Returns None if stale stock detection is disabled (see stale_days).
|
||||||
|
"""
|
||||||
|
days = cls.stale_days()
|
||||||
|
|
||||||
|
if days <= 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
stale_date = InvenTree.helpers.current_date() + timedelta(days=days)
|
||||||
|
|
||||||
|
return (
|
||||||
|
cls.IN_STOCK_FILTER & ~Q(expiry_date=None) & Q(expiry_date__lt=stale_date)
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def allow_expired_stock(setting: str) -> bool:
|
def allow_expired_stock(setting: str) -> bool:
|
||||||
"""Return True if expired stock may be used for a particular purpose.
|
"""Return True if expired stock may be used for a particular purpose.
|
||||||
@@ -1537,7 +1572,7 @@ class StockItem(
|
|||||||
|
|
||||||
today = InvenTree.helpers.current_date()
|
today = InvenTree.helpers.current_date()
|
||||||
|
|
||||||
stale_days = get_global_setting('STOCK_STALE_DAYS')
|
stale_days = self.stale_days()
|
||||||
|
|
||||||
if stale_days <= 0:
|
if stale_days <= 0:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""JSON serializers for Stock app."""
|
"""JSON serializers for Stock app."""
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
@@ -586,20 +585,16 @@ class StockItemSerializer(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Add flag to indicate if the StockItem is stale
|
# Add flag to indicate if the StockItem is stale
|
||||||
stale_days = get_global_setting('STOCK_STALE_DAYS')
|
# (always False if stale stock detection is disabled)
|
||||||
stale_date = InvenTree.helpers.current_date() + timedelta(days=stale_days)
|
if (stale_filter := StockItem.get_stale_filter()) is not None:
|
||||||
stale_filter = (
|
stale_value = Case(
|
||||||
StockItem.IN_STOCK_FILTER
|
|
||||||
& ~Q(expiry_date=None)
|
|
||||||
& Q(expiry_date__lt=stale_date)
|
|
||||||
)
|
|
||||||
|
|
||||||
queryset = queryset.annotate(
|
|
||||||
stale=Case(
|
|
||||||
When(stale_filter, then=Value(True, output_field=BooleanField())),
|
When(stale_filter, then=Value(True, output_field=BooleanField())),
|
||||||
default=Value(False, output_field=BooleanField()),
|
default=Value(False, output_field=BooleanField()),
|
||||||
)
|
)
|
||||||
)
|
else:
|
||||||
|
stale_value = Value(False, output_field=BooleanField())
|
||||||
|
|
||||||
|
queryset = queryset.annotate(stale=stale_value)
|
||||||
|
|
||||||
# Annotate with the total number of "installed items"
|
# Annotate with the total number of "installed items"
|
||||||
queryset = queryset.annotate(installed_items=SubqueryCount('installed_parts'))
|
queryset = queryset.annotate(installed_items=SubqueryCount('installed_parts'))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""System settings for the stock app."""
|
"""System settings for the stock app."""
|
||||||
|
|
||||||
|
from django.core.validators import MinValueValidator
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
import common.validators
|
import common.validators
|
||||||
@@ -49,11 +50,11 @@ SYSTEM_SETTINGS: dict[str, InvenTreeSettingsKeyType] = {
|
|||||||
'STOCK_STALE_DAYS': {
|
'STOCK_STALE_DAYS': {
|
||||||
'name': _('Stock Stale Time'),
|
'name': _('Stock Stale Time'),
|
||||||
'description': _(
|
'description': _(
|
||||||
'Number of days stock items are considered stale before expiring'
|
'Number of days stock items are considered stale before expiring (set to zero to disable). Requires stock expiry to be enabled'
|
||||||
),
|
),
|
||||||
'default': 0,
|
'default': 0,
|
||||||
'units': _('days'),
|
'units': _('days'),
|
||||||
'validator': [int],
|
'validator': [int, MinValueValidator(0)],
|
||||||
},
|
},
|
||||||
'STOCK_ALLOW_EXPIRED_BUILD': {
|
'STOCK_ALLOW_EXPIRED_BUILD': {
|
||||||
'name': _('Build Expired Stock'),
|
'name': _('Build Expired Stock'),
|
||||||
|
|||||||
@@ -1232,21 +1232,51 @@ class StockItemListTest(StockAPITestCase):
|
|||||||
self.assertEqual(len(response), 28)
|
self.assertEqual(len(response), 28)
|
||||||
|
|
||||||
def test_filter_stale(self):
|
def test_filter_stale(self):
|
||||||
"""Filter StockItem by stale."""
|
"""Filter StockItem by stale, and check the 'stale' annotation."""
|
||||||
response = self.get_stock(stale=True)
|
|
||||||
self.assertEqual(len(response), 29)
|
|
||||||
|
|
||||||
response = self.get_stock(stale=False)
|
def check_disabled():
|
||||||
self.assertEqual(len(response), 29)
|
"""When stale detection is disabled, no items are stale."""
|
||||||
|
self.assertEqual(len(self.get_stock(stale=True)), 0)
|
||||||
|
|
||||||
|
response = self.get_stock(stale=False)
|
||||||
|
self.assertEqual(len(response), 29)
|
||||||
|
self.assertFalse(any(item['stale'] for item in response))
|
||||||
|
|
||||||
|
# Disabled by default (STOCK_STALE_DAYS = 0, STOCK_ENABLE_EXPIRY = False)
|
||||||
|
check_disabled()
|
||||||
|
|
||||||
|
# Stale days set, but stock expiry is disabled
|
||||||
|
set_global_setting('STOCK_STALE_DAYS', 10)
|
||||||
|
check_disabled()
|
||||||
|
|
||||||
|
# Stock expiry enabled, but stale days is zero
|
||||||
|
# Expired items must not be reported as stale (previously they were)
|
||||||
|
set_global_setting('STOCK_ENABLE_EXPIRY', True)
|
||||||
|
set_global_setting('STOCK_STALE_DAYS', 0)
|
||||||
|
self.assertGreater(len(self.get_stock(expired=True)), 0)
|
||||||
|
check_disabled()
|
||||||
|
|
||||||
|
# Negative values are not allowed
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
set_global_setting('STOCK_STALE_DAYS', -1)
|
||||||
|
|
||||||
# Enable the 'stale' feature
|
# Enable the 'stale' feature
|
||||||
set_global_setting('STOCK_STALE_DAYS', '10')
|
set_global_setting('STOCK_STALE_DAYS', 10)
|
||||||
response = self.get_stock(stale=True)
|
|
||||||
self.assertEqual(len(response), 1)
|
|
||||||
|
|
||||||
response = self.get_stock(stale=False)
|
stale = self.get_stock(stale=True)
|
||||||
self.assertEqual(len(response), 28)
|
self.assertEqual(len(stale), 1)
|
||||||
set_global_setting('STOCK_STALE_DAYS', '0')
|
self.assertTrue(all(item['stale'] for item in stale))
|
||||||
|
|
||||||
|
not_stale = self.get_stock(stale=False)
|
||||||
|
self.assertEqual(len(not_stale), 28)
|
||||||
|
self.assertFalse(any(item['stale'] for item in not_stale))
|
||||||
|
|
||||||
|
# The model method agrees with the API
|
||||||
|
for item in stale:
|
||||||
|
self.assertTrue(StockItem.objects.get(pk=item['pk']).is_stale())
|
||||||
|
|
||||||
|
set_global_setting('STOCK_STALE_DAYS', 0)
|
||||||
|
set_global_setting('STOCK_ENABLE_EXPIRY', False)
|
||||||
|
|
||||||
def test_paginate(self):
|
def test_paginate(self):
|
||||||
"""Test that we can paginate results correctly."""
|
"""Test that we can paginate results correctly."""
|
||||||
|
|||||||
Reference in new issue
Block a user