Added function to split stock

- Specify the quantity to move into the NEW stock item
- The left over quantity will remain in the current stock item
This commit is contained in:
Oliver Walters 2019-05-11 00:37:54 +10:00
parent 6561a613d7
commit c376f38a8d

View File

@ -281,7 +281,47 @@ class StockItem(models.Model):
track.save() track.save()
@transaction.atomic @transaction.atomic
def move(self, location, notes, user): def splitStock(self, quantity, user):
""" Split this stock item into two items, in the same location.
Stock tracking notes for this StockItem will be duplicated,
and added to the new StockItem.
Args:
quantity: Number of stock items to remove from this entity, and pass to the next
Notes:
The provided quantity will be subtracted from this item and given to the new one.
The new item will have a different StockItem ID, while this will remain the same.
"""
# Doesn't make sense for a zero quantity
if quantity <= 0:
return
# Also doesn't make sense to split the full amount
if quantity >= self.quantity:
return
# Create a new StockItem object, duplicating relevant fields
new_stock = StockItem.objects.create(
part=self.part,
quantity=quantity,
supplier_part=self.supplier_part,
location=self.location,
batch=self.batch,
delete_on_deplete=self.delete_on_deplete
)
new_stock.save()
# Add a new tracking item for the new stock item
new_stock.addTransactionNote(
"Split from existing stock",
user,
"Split {n} from existing stock item".format(n=quantity))
# Remove the specified quantity from THIS stock item
self.take_stock(quantity, user, 'Split {n} items into new stock item'.format(n=quantity))
if location is None: if location is None:
# TODO - Raise appropriate error (cannot move to blank location) # TODO - Raise appropriate error (cannot move to blank location)