Serialize the allocated quantity for a purchase-order line item

This commit is contained in:
Oliver Walters 2020-04-21 11:41:08 +10:00
parent 7385099194
commit 8052a1989c
3 changed files with 18 additions and 1 deletions

View File

@ -335,6 +335,7 @@ class SOLineItemList(generics.ListCreateAPIView):
return queryset.prefetch_related(
'part',
'part__stock_items',
'stock_items',
'order',
)

View File

@ -5,7 +5,8 @@ Order model definitions
# -*- coding: utf-8 -*-
from django.db import models, transaction
from django.db.models import F
from django.db.models import F, Sum
from django.db.models.functions import Coalesce
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
@ -16,6 +17,7 @@ from markdownx.models import MarkdownxField
import os
from datetime import datetime
from decimal import Decimal
from stock.models import StockItem
from company.models import Company, SupplierPart
@ -372,6 +374,16 @@ class SalesOrderLineItem(OrderLineItem):
part = models.ForeignKey(Part, on_delete=models.SET_NULL, related_name='sales_order_line_items', null=True, help_text=_('Part'), limit_choices_to={'salable': True})
def allocated_quantity(self):
""" Return the total stock quantity allocated to this LineItem.
This is a summation of the quantity of each attached StockItem
"""
query = self.stock_items.aggregate(allocated=Coalesce(Sum('stock_item__quantity'), Decimal(0)))
return query['allocated']
class SalesOrderLineItemStockAssociation(models.Model):
"""

View File

@ -162,11 +162,15 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
order_detail = SalesOrderSerializer(source='order', many=False, read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
quantity = serializers.FloatField()
allocated = serializers.FloatField(source='allocated_quantity', read_only=True)
class Meta:
model = SalesOrderLineItem
fields = [
'pk',
'allocated',
'quantity',
'reference',
'notes',