Logic fix for StockItem splitting

- The original is left in place
- The new item is moved
This commit is contained in:
Oliver Walters 2020-02-17 22:56:54 +11:00
parent 3715c5d637
commit e483b42df6

View File

@ -515,13 +515,14 @@ class StockItem(MPTTModel):
item.save() item.save()
@transaction.atomic @transaction.atomic
def splitStock(self, quantity, user): def splitStock(self, quantity, location, user):
""" Split this stock item into two items, in the same location. """ Split this stock item into two items, in the same location.
Stock tracking notes for this StockItem will be duplicated, Stock tracking notes for this StockItem will be duplicated,
and added to the new StockItem. and added to the new StockItem.
Args: Args:
quantity: Number of stock items to remove from this entity, and pass to the next quantity: Number of stock items to remove from this entity, and pass to the next
location: Where to move the new StockItem to
Notes: Notes:
The provided quantity will be subtracted from this item and given to the new one. The provided quantity will be subtracted from this item and given to the new one.
@ -551,6 +552,7 @@ class StockItem(MPTTModel):
new_stock.pk = None new_stock.pk = None
new_stock.parent = self new_stock.parent = self
new_stock.quantity = quantity new_stock.quantity = quantity
new_stock.location = location
new_stock.save() new_stock.save()
# Copy the transaction history of this part into the new one # Copy the transaction history of this part into the new one
@ -569,6 +571,11 @@ class StockItem(MPTTModel):
def move(self, location, notes, user, **kwargs): def move(self, location, notes, user, **kwargs):
""" Move part to a new location. """ Move part to a new location.
If less than the available quantity is to be moved,
a new StockItem is created, with the defined quantity,
and that new StockItem is moved.
The quantity is also subtracted from the existing StockItem.
Args: Args:
location: Destination location (cannot be null) location: Destination location (cannot be null)
notes: User notes notes: User notes
@ -596,8 +603,10 @@ class StockItem(MPTTModel):
if quantity < self.quantity: if quantity < self.quantity:
# We need to split the stock! # We need to split the stock!
# Leave behind certain quantity # Split the existing StockItem in two
self.splitStock(self.quantity - quantity, user) self.splitStock(quantity, location, user)
return
msg = "Moved to {loc}".format(loc=str(location)) msg = "Moved to {loc}".format(loc=str(location))