diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index d2d6ec51ee..533aca7596 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -27,9 +27,11 @@ from mptt.models import MPTTModel, TreeForeignKey from djmoney.models.fields import MoneyField from decimal import Decimal, InvalidOperation -from datetime import datetime +from datetime import datetime, timedelta from InvenTree import helpers +import common.models + from InvenTree.status_codes import StockStatus from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeURLField @@ -471,9 +473,38 @@ class StockItem(MPTTModel): help_text=_('Single unit purchase price at time of purchase'), ) + def is_stale(self): + """ + Returns True if this Stock item is "stale". + + To be "stale", the following conditions must be met: + + - Expiry date is not None + - Expiry date will "expire" within the configured stale date + - The StockItem is otherwise "in stock" + """ + + if self.expiry_date is None: + return False + + if not self.in_stock: + return False + + today = datetime.now().date() + + stale_days = common.models.InvenTreeSetting.get_setting('STOCK_STALE_DAYS') + + if stale_days <= 0: + return False + + expiry_date = today + timedelta(days=stale_days) + + return self.expiry_date < expiry_date + + def is_expired(self): """ - Returns true if this StockItem is "expired" + Returns True if this StockItem is "expired". To be "expired", the following conditions must be met: diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index b8c143fb4f..cbb6eeafb2 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -74,6 +74,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} {% trans "Expired" %} {% else %} {% stock_status_label item.status large=True %} + {% if item.is_stale %} + {% trans "Stale" %} + {% endif %} {% endif %}
@@ -304,7 +307,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} {{ item.expiry_date }} {% if item.is_expired %} - {% trans "Expired" %} + {% trans "Expired" %} + {% elif item.is_stale %} + {% trans "Stale" %} {% endif %}