mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
Allocation quantity functions include variants
- For calculation of total "available_stock", variants are included - Update the allocation quantity functions to also include variants - Otherwise, the calculations are "out of sync"
This commit is contained in:
parent
bfb8e61df5
commit
35aa5f25be
@ -1313,19 +1313,31 @@ class Part(MPTTModel):
|
|||||||
|
|
||||||
return quantity
|
return quantity
|
||||||
|
|
||||||
def build_order_allocations(self):
|
def build_order_allocations(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Return all 'BuildItem' objects which allocate this part to Build objects
|
Return all 'BuildItem' objects which allocate this part to Build objects
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return BuildModels.BuildItem.objects.filter(stock_item__part__id=self.id)
|
include_variants = kwargs.get('include_variants', True)
|
||||||
|
|
||||||
def build_order_allocation_count(self):
|
queryset = BuildModels.BuildItem.objects.all()
|
||||||
|
|
||||||
|
if include_variants:
|
||||||
|
variants = self.get_descendants(include_self=True)
|
||||||
|
queryset = queryset.filter(
|
||||||
|
stock_item__part__in=variants,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
queryset = queryset.filter(stock_item__part=self)
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
def build_order_allocation_count(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Return the total amount of this part allocated to build orders
|
Return the total amount of this part allocated to build orders
|
||||||
"""
|
"""
|
||||||
|
|
||||||
query = self.build_order_allocations().aggregate(
|
query = self.build_order_allocations(**kwargs).aggregate(
|
||||||
total=Coalesce(
|
total=Coalesce(
|
||||||
Sum(
|
Sum(
|
||||||
'quantity',
|
'quantity',
|
||||||
@ -1343,7 +1355,19 @@ class Part(MPTTModel):
|
|||||||
Return all sales-order-allocation objects which allocate this part to a SalesOrder
|
Return all sales-order-allocation objects which allocate this part to a SalesOrder
|
||||||
"""
|
"""
|
||||||
|
|
||||||
queryset = OrderModels.SalesOrderAllocation.objects.filter(item__part__id=self.id)
|
include_variants = kwargs.get('include_variants', True)
|
||||||
|
|
||||||
|
queryset = OrderModels.SalesOrderAllocation.objects.all()
|
||||||
|
|
||||||
|
if include_variants:
|
||||||
|
# Include allocations for all variants
|
||||||
|
variants = self.get_descendants(include_self=True)
|
||||||
|
queryset = queryset.filter(
|
||||||
|
item__part__in=variants,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# Only look at this part
|
||||||
|
queryset = queryset.filter(item__part=self)
|
||||||
|
|
||||||
# Default behaviour is to only return *pending* allocations
|
# Default behaviour is to only return *pending* allocations
|
||||||
pending = kwargs.get('pending', True)
|
pending = kwargs.get('pending', True)
|
||||||
@ -1381,7 +1405,7 @@ class Part(MPTTModel):
|
|||||||
|
|
||||||
return query['total']
|
return query['total']
|
||||||
|
|
||||||
def allocation_count(self):
|
def allocation_count(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Return the total quantity of stock allocated for this part,
|
Return the total quantity of stock allocated for this part,
|
||||||
against both build orders and sales orders.
|
against both build orders and sales orders.
|
||||||
@ -1389,8 +1413,8 @@ class Part(MPTTModel):
|
|||||||
|
|
||||||
return sum(
|
return sum(
|
||||||
[
|
[
|
||||||
self.build_order_allocation_count(),
|
self.build_order_allocation_count(**kwargs),
|
||||||
self.sales_order_allocation_count(),
|
self.sales_order_allocation_count(**kwargs),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user