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( return queryset.prefetch_related(
'part', 'part',
'part__stock_items', 'part__stock_items',
'stock_items',
'order', 'order',
) )

View File

@ -5,7 +5,8 @@ Order model definitions
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.db import models, transaction 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.validators import MinValueValidator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -16,6 +17,7 @@ from markdownx.models import MarkdownxField
import os import os
from datetime import datetime from datetime import datetime
from decimal import Decimal
from stock.models import StockItem from stock.models import StockItem
from company.models import Company, SupplierPart 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}) 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): class SalesOrderLineItemStockAssociation(models.Model):
""" """

View File

@ -162,11 +162,15 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
order_detail = SalesOrderSerializer(source='order', many=False, read_only=True) order_detail = SalesOrderSerializer(source='order', many=False, read_only=True)
part_detail = PartBriefSerializer(source='part', 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: class Meta:
model = SalesOrderLineItem model = SalesOrderLineItem
fields = [ fields = [
'pk', 'pk',
'allocated',
'quantity', 'quantity',
'reference', 'reference',
'notes', 'notes',