mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
Calculate 'fulfilled' quantity once a sales order is marked as shipped
- This allows us to delete the SalesOrderAllocation objects from the database
This commit is contained in:
@ -348,14 +348,17 @@ class SOLineItemList(generics.ListCreateAPIView):
|
||||
|
||||
queryset = super().get_queryset(*args, **kwargs)
|
||||
|
||||
return queryset.prefetch_related(
|
||||
queryset = queryset.prefetch_related(
|
||||
'part',
|
||||
'part__stock_items',
|
||||
'allocations',
|
||||
'allocations__item__location',
|
||||
'order',
|
||||
'order__stock_items',
|
||||
)
|
||||
|
||||
return queryset
|
||||
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
|
@ -324,8 +324,11 @@ class SalesOrder(Order):
|
||||
for allocation in line.allocations.all():
|
||||
allocation.complete_allocation(user)
|
||||
|
||||
# TODO - Remove the allocation from the database
|
||||
# allocation.delete()
|
||||
# Remove the allocation from the database once it has been 'fulfilled'
|
||||
if allocation.item.sales_order == self.order:
|
||||
allocation.delete()
|
||||
else:
|
||||
raise ValidationError("Could not complete order - allocation item not fulfilled")
|
||||
|
||||
# Ensure the order status is marked as "Shipped"
|
||||
self.status = SalesOrderStatus.SHIPPED
|
||||
@ -458,6 +461,15 @@ class SalesOrderLineItem(OrderLineItem):
|
||||
|
||||
part = models.ForeignKey('part.Part', on_delete=models.SET_NULL, related_name='sales_order_line_items', null=True, help_text=_('Part'), limit_choices_to={'salable': True})
|
||||
|
||||
def fulfilled_quantity(self):
|
||||
"""
|
||||
Return the total stock quantity fulfilled against this line item.
|
||||
"""
|
||||
|
||||
query = self.order.stock_items.filter(part=self.part).aggregate(fulfilled=Coalesce(Sum('quantity'), Decimal(0)))
|
||||
|
||||
return query['fulfilled']
|
||||
|
||||
def allocated_quantity(self):
|
||||
""" Return the total stock quantity allocated to this LineItem.
|
||||
|
||||
|
@ -211,6 +211,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
|
||||
|
||||
quantity = serializers.FloatField()
|
||||
allocated = serializers.FloatField(source='allocated_quantity', read_only=True)
|
||||
fulfilled = serializers.FloatField(source='fulfilled_quantity', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = SalesOrderLineItem
|
||||
@ -220,6 +221,7 @@ class SOLineItemSerializer(InvenTreeModelSerializer):
|
||||
'allocated',
|
||||
'allocations',
|
||||
'quantity',
|
||||
'fulfilled',
|
||||
'reference',
|
||||
'notes',
|
||||
'order',
|
||||
|
Reference in New Issue
Block a user