Adds "overdue" annotation field to POLineItem serializer

This commit is contained in:
Oliver 2022-02-28 15:03:12 +11:00
parent 557aa44904
commit df7713f6c2
3 changed files with 29 additions and 8 deletions

View File

@ -274,7 +274,7 @@ class POLineItemFilter(rest_filters.FilterSet):
model = models.PurchaseOrderLineItem model = models.PurchaseOrderLineItem
fields = [ fields = [
'order', 'order',
'part' 'part',
] ]
pending = rest_filters.BooleanFilter(label='pending', method='filter_pending') pending = rest_filters.BooleanFilter(label='pending', method='filter_pending')
@ -391,6 +391,7 @@ class POLineItemList(generics.ListCreateAPIView):
'reference', 'reference',
'SKU', 'SKU',
'total_price', 'total_price',
'target_date',
] ]
search_fields = [ search_fields = [
@ -401,11 +402,6 @@ class POLineItemList(generics.ListCreateAPIView):
'reference', 'reference',
] ]
filter_fields = [
'order',
'part'
]
class POLineItemDetail(generics.RetrieveUpdateDestroyAPIView): class POLineItemDetail(generics.RetrieveUpdateDestroyAPIView):
""" """
@ -703,6 +699,7 @@ class SOLineItemList(generics.ListCreateAPIView):
'part__name', 'part__name',
'quantity', 'quantity',
'reference', 'reference',
'target_date',
] ]
search_fields = [ search_fields = [

View File

@ -799,6 +799,14 @@ class OrderLineItem(models.Model):
target_date: An (optional) date for expected shipment of this line item. target_date: An (optional) date for expected shipment of this line item.
""" """
"""
Query filter for determining if an individual line item is "overdue":
- Amount received is less than the required quantity
- Target date is not None
- Target date is in the past
"""
OVERDUE_FILTER = Q(received__lt=F('quantity')) & ~Q(target_date=None) & Q(target_date__lt=datetime.now().date())
class Meta: class Meta:
abstract = True abstract = True

View File

@ -5,12 +5,14 @@ JSON serializers for the Order API
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from datetime import datetime
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.db import models, transaction from django.db import models, transaction
from django.db.models import Case, When, Value from django.db.models import Case, When, Value
from django.db.models import BooleanField, ExpressionWrapper, F from django.db.models import BooleanField, ExpressionWrapper, F, Q
from rest_framework import serializers from rest_framework import serializers
from rest_framework.serializers import ValidationError from rest_framework.serializers import ValidationError
@ -26,7 +28,7 @@ from InvenTree.serializers import InvenTreeModelSerializer
from InvenTree.serializers import InvenTreeDecimalField from InvenTree.serializers import InvenTreeDecimalField
from InvenTree.serializers import InvenTreeMoneySerializer from InvenTree.serializers import InvenTreeMoneySerializer
from InvenTree.serializers import ReferenceIndexingSerializerMixin from InvenTree.serializers import ReferenceIndexingSerializerMixin
from InvenTree.status_codes import StockStatus from InvenTree.status_codes import StockStatus, PurchaseOrderStatus, SalesOrderStatus
import order.models import order.models
@ -126,6 +128,7 @@ class POLineItemSerializer(InvenTreeModelSerializer):
Add some extra annotations to this queryset: Add some extra annotations to this queryset:
- Total price = purchase_price * quantity - Total price = purchase_price * quantity
- "Overdue" status (boolean field)
""" """
queryset = queryset.annotate( queryset = queryset.annotate(
@ -135,6 +138,16 @@ class POLineItemSerializer(InvenTreeModelSerializer):
) )
) )
# Add an annotated 'overdue' field
queryset = queryset.annotate(
overdue=Case(
When(Q(order__status__in=PurchaseOrderStatus.OPEN) & order.models.OrderLineItem.OVERDUE_FILTER,
then=Value(True, output_field=BooleanField()),
),
default=Value(False, output_field=BooleanField()),
)
)
return queryset return queryset
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -155,6 +168,8 @@ class POLineItemSerializer(InvenTreeModelSerializer):
quantity = serializers.FloatField(default=1) quantity = serializers.FloatField(default=1)
received = serializers.FloatField(default=0) received = serializers.FloatField(default=0)
overdue = serializers.BooleanField()
total_price = serializers.FloatField(read_only=True) total_price = serializers.FloatField(read_only=True)
part_detail = PartBriefSerializer(source='get_base_part', many=False, read_only=True) part_detail = PartBriefSerializer(source='get_base_part', many=False, read_only=True)
@ -185,6 +200,7 @@ class POLineItemSerializer(InvenTreeModelSerializer):
'notes', 'notes',
'order', 'order',
'order_detail', 'order_detail',
'overdue',
'part', 'part',
'part_detail', 'part_detail',
'supplier_part_detail', 'supplier_part_detail',