From fe7392f152e618a8c34ef9e765e7a0a25b2960af Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 25 Jul 2019 11:05:09 +1000 Subject: [PATCH] Prevent stock adjustments for serialized stock items --- InvenTree/stock/models.py | 29 +++++++++++++++++++++++ InvenTree/stock/templates/stock/item.html | 15 ++++++++++-- InvenTree/stock/views.py | 3 +++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 446ab65a56..b5a88d4e66 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -122,6 +122,11 @@ class StockItem(models.Model): system=True ) + @property + def serialized(self): + """ Return True if this StockItem is serialized """ + return self.serial is not None and self.quantity == 1 + @classmethod def check_serial_number(cls, part, serial_number): """ Check if a new stock item can be created with the provided part_id @@ -358,6 +363,14 @@ class StockItem(models.Model): track.save() + @transaction.atomic + def serializeStock(self, serials, user): + """ Split this stock item into unique serial numbers. + """ + + # TODO + pass + @transaction.atomic def splitStock(self, quantity, user): """ Split this stock item into two items, in the same location. @@ -372,6 +385,10 @@ class StockItem(models.Model): The new item will have a different StockItem ID, while this will remain the same. """ + # Do not split a serialized part + if self.serialized: + return + # Doesn't make sense for a zero quantity if quantity <= 0: return @@ -461,6 +478,10 @@ class StockItem(models.Model): - False if the StockItem was deleted """ + # Do not adjust quantity of a serialized part + if self.serialized: + return + if quantity < 0: quantity = 0 @@ -504,6 +525,10 @@ class StockItem(models.Model): or by manually adding the items to the stock location """ + # Cannot add items to a serialized part + if self.serialized: + return False + quantity = int(quantity) # Ignore amounts that do not make sense @@ -524,6 +549,10 @@ class StockItem(models.Model): """ Remove items from stock """ + # Cannot remove items from a serialized part + if self.serialized: + return False + quantity = int(quantity) if quantity <= 0 or self.infinite: diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index f6847d5ae1..97895fd4d2 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -5,11 +5,16 @@

Stock Item Details

+ {% if item.serialized %} +

{{ item.part.full_name}} # {{ item.serial }}

+ {% else %}

{{ item.quantity }} × {{ item.part.full_name }}

+ {% endif %}

{% include "qr_button.html" %} {% if item.in_stock %} + {% if not item.serialized %} @@ -19,6 +24,7 @@ + {% endif %} @@ -34,6 +40,11 @@

+ {% if item.serialized %} +
+ This stock item is serialized - it has a unique serial number and the quantity cannot be adjusted. +
+ {% endif %}
@@ -54,9 +65,9 @@ {{ item.location.name }} {% endif %} - {% if item.serial %} + {% if item.serialized %} - Serial + Serial Number {{ item.serial }} {% else %} diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index b7fbb9f9e4..6fbea9d885 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -429,6 +429,9 @@ class StockItemEdit(AjaxUpdateView): query = query.filter(part=item.part.id) form.fields['supplier_part'].queryset = query + if not item.part.trackable: + form.fields.pop('serial') + return form