diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index c36487a7dd..1c59a0bb31 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -1766,9 +1766,6 @@ class ReturnOrder(TotalPriceMixin, Order): stock_item = line.item - # Remove any allocations against the returned StockItem - stock_item.clearAllocations() - deltas = { 'status': StockStatus.QUARANTINED, 'returnorder': self.pk, diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 09b49fb4d3..0d3a28d09f 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -33,7 +33,8 @@ from InvenTree.fields import (InvenTreeModelMoneyField, InvenTreeNotesField, InvenTreeURLField) from InvenTree.models import (InvenTreeAttachment, InvenTreeBarcodeMixin, InvenTreeTree, extract_int) -from InvenTree.status_codes import StockHistoryCode, StockStatus +from InvenTree.status_codes import (SalesOrderStatus, StockHistoryCode, + StockStatus) from part import models as PartModels from plugin.events import trigger_event from plugin.models import MetadataMixin @@ -1013,7 +1014,6 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, common.models.MetaMixin, M location=location ) - self.clearAllocations() self.customer = None self.belongs_to = None self.sales_order = None @@ -1059,9 +1059,33 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, common.models.MetaMixin, M return total - def sales_order_allocation_count(self): + def get_sales_order_allocations(self, active=True): + """Return a queryset for SalesOrderAllocations against this StockItem, with optional filters. + + Arguments: + active: Filter by 'active' status of the allocation + """ + query = self.sales_order_allocations.all() + + if active is True: + query = query.filter( + line__order__status__in=SalesOrderStatus.OPEN, + shipment__shipment_date=None + ) + elif active is False: + query = query.exclude( + line__order__status__in=SalesOrderStatus.OPEN + ).exclude( + shipment__shipment_date=None + ) + + return query + + def sales_order_allocation_count(self, active=True): """Return the total quantity allocated to SalesOrders.""" - query = self.sales_order_allocations.aggregate(q=Coalesce(Sum('quantity'), Decimal(0))) + + query = self.get_sales_order_allocations(active=active) + query = query.aggregate(q=Coalesce(Sum('quantity'), Decimal(0))) total = query['q'] diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index 71df55c5b1..a886e71b6c 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -59,7 +59,7 @@ {% include "filter_list.html" with id="salesorderallocation" %} -