From 3058b895dd5ed2dea0ab5f5d6decf5c97fba4984 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 24 Jul 2019 20:24:12 +1000 Subject: [PATCH] Prevent auto-delete of stock items which have a serial number --- InvenTree/stock/models.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 359e9a0115..c92776a2fa 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -316,7 +316,15 @@ class StockItem(models.Model): infinite = models.BooleanField(default=False) def can_delete(self): - # TODO - Return FALSE if this item cannot be deleted! + """ Can this stock item be deleted? It can NOT be deleted under the following circumstances: + + - Has a serial number and is tracked + - Is installed inside another StockItem + """ + + if part.trackable and self.serial is not None: + return False + return True @property @@ -457,7 +465,7 @@ class StockItem(models.Model): self.quantity = quantity - if quantity <= 0 and self.delete_on_deplete: + if quantity <= 0 and self.delete_on_deplete and self.can_delete(): self.delete() return False else: