mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add annotation for "allocated_to_sales_orders"
This commit is contained in:
parent
6eab3dbf09
commit
4e100643f5
@ -24,7 +24,10 @@ from InvenTree.serializers import (DataFileUploadSerializer,
|
|||||||
InvenTreeAttachmentSerializer,
|
InvenTreeAttachmentSerializer,
|
||||||
InvenTreeMoneySerializer)
|
InvenTreeMoneySerializer)
|
||||||
|
|
||||||
from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus
|
from InvenTree.status_codes import (BuildStatus,
|
||||||
|
PurchaseOrderStatus,
|
||||||
|
SalesOrderStatus)
|
||||||
|
|
||||||
from stock.models import StockItem
|
from stock.models import StockItem
|
||||||
|
|
||||||
from .models import (BomItem, BomItemSubstitute,
|
from .models import (BomItem, BomItemSubstitute,
|
||||||
@ -363,6 +366,26 @@ class PartSerializer(InvenTreeModelSerializer):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Annotate with the number of stock items allocated to sales orders.
|
||||||
|
This annotation is modeled on Part.sales_order_allocations() method:
|
||||||
|
|
||||||
|
- Only look for "open" orders
|
||||||
|
- Stock items have not been "shipped"
|
||||||
|
"""
|
||||||
|
so_allocation_filter = Q(
|
||||||
|
line__order__status__in=SalesOrderStatus.OPEN, # LineItem points to an OPEN order
|
||||||
|
shipment=None, # Allocated item has *not* been shipped out
|
||||||
|
)
|
||||||
|
|
||||||
|
queryset = queryset.annotate(
|
||||||
|
allocated_to_sales_orders=Coalesce(
|
||||||
|
SubquerySum('stock_items__sales_order_allocations__quantity', filter=so_allocation_filter),
|
||||||
|
Decimal(0),
|
||||||
|
output_field=models.DecimalField(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def get_starred(self, part):
|
def get_starred(self, part):
|
||||||
@ -376,9 +399,10 @@ class PartSerializer(InvenTreeModelSerializer):
|
|||||||
category_detail = CategorySerializer(source='category', many=False, read_only=True)
|
category_detail = CategorySerializer(source='category', many=False, read_only=True)
|
||||||
|
|
||||||
# Calculated fields
|
# Calculated fields
|
||||||
|
allocated_to_sales_orders = serializers.FloatField(read_only=True)
|
||||||
|
building = serializers.FloatField(read_only=True)
|
||||||
in_stock = serializers.FloatField(read_only=True)
|
in_stock = serializers.FloatField(read_only=True)
|
||||||
ordering = serializers.FloatField(read_only=True)
|
ordering = serializers.FloatField(read_only=True)
|
||||||
building = serializers.FloatField(read_only=True)
|
|
||||||
stock_item_count = serializers.IntegerField(read_only=True)
|
stock_item_count = serializers.IntegerField(read_only=True)
|
||||||
suppliers = serializers.IntegerField(read_only=True)
|
suppliers = serializers.IntegerField(read_only=True)
|
||||||
|
|
||||||
@ -399,7 +423,7 @@ class PartSerializer(InvenTreeModelSerializer):
|
|||||||
partial = True
|
partial = True
|
||||||
fields = [
|
fields = [
|
||||||
'active',
|
'active',
|
||||||
|
'allocated_to_sales_orders',
|
||||||
'assembly',
|
'assembly',
|
||||||
'category',
|
'category',
|
||||||
'category_detail',
|
'category_detail',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user