Annotate stock queryset with stale status

This commit is contained in:
Oliver Walters 2021-01-06 23:06:49 +11:00
parent a5b18640af
commit 1d6a049c5a
4 changed files with 30 additions and 1 deletions

View File

@ -13,11 +13,15 @@ from django.db.models.functions import Coalesce
from django.db.models import Case, When, Value
from django.db.models import BooleanField
from django.db.models import Q
from sql_util.utils import SubquerySum, SubqueryCount
from decimal import Decimal
from datetime import datetime, timedelta
import common.models
from company.serializers import SupplierPartSerializer
from part.serializers import PartBriefSerializer
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
@ -119,6 +123,20 @@ class StockItemSerializer(InvenTreeModelSerializer):
)
)
# Add flag to indicate if the StockItem is stale
stale_days = common.models.InvenTreeSetting.get_setting('STOCK_STALE_DAYS')
stale_date = datetime.now().date() + timedelta(days=stale_days)
stale_filter = 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()),
),
default=Value(False, output_field=BooleanField()),
)
)
return queryset
status_text = serializers.CharField(source='get_status_display', read_only=True)
@ -137,6 +155,8 @@ class StockItemSerializer(InvenTreeModelSerializer):
expired = serializers.BooleanField(required=False, read_only=True)
stale = serializers.BooleanField(required=False, read_only=True)
serial = serializers.CharField(required=False)
required_tests = serializers.IntegerField(source='required_test_count', read_only=True, required=False)
@ -185,6 +205,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
'required_tests',
'sales_order',
'serial',
'stale',
'status',
'status_text',
'supplier_part',

View File

@ -102,6 +102,7 @@ loadStockTable($("#expired-stock-table"), {
loadStockTable($("#stale-stock-table"), {
params: {
stale: true,
expired: false,
location_detail: true,
part_detail: true,
},

View File

@ -534,7 +534,9 @@ function loadStockTable(table, options) {
}
if (row.expired) {
html += makeIconBadge('fa-stopwatch icon-red', '{% trans "Stock item has expired" %}');
html += makeIconBadge('fa-calendar-times icon-red', '{% trans "Stock item has expired" %}');
} else if (row.stale) {
html += makeIconBadge('fa-stopwatch', '{% trans "Stock item will expire soon" %}');
}
if (row.allocated) {

View File

@ -111,6 +111,11 @@ function getAvailableTableFilters(tableKey) {
title: '{% trans "Expired" %}',
description: '{% trans "Show stock items which have expired" %}',
},
stale: {
type: 'bool',
title: '{% trans "Stale" %}',
description: '{% trans "Show stock which is close to expiring" %}',
},
in_stock: {
type: 'bool',
title: '{% trans "In Stock" %}',