diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b4216c46c1..d6c19b2bfb 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -281,7 +281,47 @@ class StockItem(models.Model): track.save() @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: # TODO - Raise appropriate error (cannot move to blank location)