2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

New logic for completing a SalesOrderShipment

This commit is contained in:
Oliver 2021-11-29 23:11:21 +11:00
parent da6c723184
commit c6b11b5e38

View File

@ -595,6 +595,14 @@ class SalesOrder(Order):
return False return False
def is_completed(self):
"""
Check if this order is "shipped" (all line items delivered),
and mark it as "shipped" if so.
"""
return all([line.is_completed() for line in self.lines.all()])
@transaction.atomic @transaction.atomic
def ship_order(self, user): def ship_order(self, user):
""" Mark this order as 'shipped' """ """ Mark this order as 'shipped' """
@ -786,13 +794,15 @@ class PurchaseOrderLineItem(OrderLineItem):
) )
def get_destination(self): def get_destination(self):
"""Show where the line item is or should be placed""" """
# NOTE: If a line item gets split when recieved, only an arbitrary Show where the line item is or should be placed
# stock items location will be reported as the location for the
# entire line. NOTE: If a line item gets split when recieved, only an arbitrary
for stock in stock_models.StockItem.objects.filter( stock items location will be reported as the location for the
supplier_part=self.part, purchase_order=self.order entire line.
): """
for stock in stock_models.StockItem.objects.filter(supplier_part=self.part, purchase_order=self.order):
if stock.location: if stock.location:
return stock.location return stock.location
if self.destination: if self.destination:
@ -882,6 +892,13 @@ class SalesOrderLineItem(OrderLineItem):
""" Return True if this line item is over allocated """ """ Return True if this line item is over allocated """
return self.allocated_quantity() > self.quantity return self.allocated_quantity() > self.quantity
def is_completed(self):
"""
Return True if this line item is completed (has been fully shipped)
"""
return self.shipped >= self.quantity
def get_next_shipment_number(): def get_next_shipment_number():
""" """
@ -980,7 +997,7 @@ class SalesOrderShipment(models.Model):
) )
@transaction.atomic @transaction.atomic
def complete_shipment(self): def complete_shipment(self, user):
""" """
Complete this particular shipment: Complete this particular shipment:
@ -989,16 +1006,25 @@ class SalesOrderShipment(models.Model):
3. Set the "shipment_date" to now 3. Set the "shipment_date" to now
""" """
if self.shipment_date:
# Ignore, shipment has already been sent!
return
# Iterate through each stock item assigned to this shipment # Iterate through each stock item assigned to this shipment
for allocation in self.allocations.all(): for allocation in self.allocations.all():
pass
# Mark the allocation as "complete"
allocation.complete_allocation(user)
# Update the "shipment" date # Update the "shipment" date
self.shipment_date = datetime.now() self.shipment_date = datetime.now()
self.shipped_by = user
self.save() self.save()
# Finally, check if the order is fully shipped
if self.order.is_completed():
self.order.status = SalesOrderStatus.SHIPPED
self.order.save()
class SalesOrderAllocation(models.Model): class SalesOrderAllocation(models.Model):
@ -1134,6 +1160,10 @@ class SalesOrderAllocation(models.Model):
user=user user=user
) )
# Update the 'shipped' quantity
self.line.shipped += self.quantity
self.line.save()
# Update our own reference to the StockItem # Update our own reference to the StockItem
# (It may have changed if the stock was split) # (It may have changed if the stock was split)
self.item = item self.item = item