From c45e66935a0074eb12c7447b1e3177e31cee7b5d Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 5 May 2023 12:55:31 +1000 Subject: [PATCH] Stock transfer same location (#4757) * Allow stock items to be transferred into the same location * Add new code when moving into same location * Update unit test * Further unit test fixes --- InvenTree/InvenTree/status_codes.py | 2 ++ InvenTree/stock/models.py | 16 +++++++++++----- InvenTree/stock/tests.py | 7 +++---- InvenTree/templates/js/translated/stock.js | 8 ++++---- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py index cb410c4f5c..d2c6afeee1 100644 --- a/InvenTree/InvenTree/status_codes.py +++ b/InvenTree/InvenTree/status_codes.py @@ -267,6 +267,7 @@ class StockHistoryCode(StatusCode): # Location operations STOCK_MOVE = 20 + STOCK_UPDATE = 25 # Installation operations INSTALLED_INTO_ASSEMBLY = 30 @@ -316,6 +317,7 @@ class StockHistoryCode(StatusCode): STOCK_REMOVE: _('Stock manually removed'), STOCK_MOVE: _('Location changed'), + STOCK_UPDATE: _('Stock updated'), INSTALLED_INTO_ASSEMBLY: _('Installed into assembly'), REMOVED_FROM_ASSEMBLY: _('Removed from assembly'), diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index a79bb2febc..ca083cee3b 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1665,9 +1665,6 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo if location is None: # TODO - Raise appropriate error (cannot move to blank location) return False - elif self.location and (location.pk == self.location.pk) and (quantity == self.quantity): - # TODO - Raise appropriate error (cannot move to same location) - return False # Test for a partial movement if quantity < self.quantity: @@ -1678,16 +1675,25 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo return True + # Moving into the same location triggers a different history code + same_location = location == self.location + self.location = location tracking_info = {} + tracking_code = StockHistoryCode.STOCK_MOVE + + if same_location: + tracking_code = StockHistoryCode.STOCK_UPDATE + else: + tracking_info['location'] = location.pk + self.add_tracking_entry( - StockHistoryCode.STOCK_MOVE, + tracking_code, user, notes=notes, deltas=tracking_info, - location=location, ) self.save() diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index c3d870b605..05e7e201e0 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -376,15 +376,14 @@ class StockTest(StockTestBase): self.assertEqual(track.notes, 'Moved to the bathroom') def test_self_move(self): - """Test moving stock to itself does not work.""" - # Try to move an item to its current location (should fail) + """Test moving stock to its current location.""" it = StockItem.objects.get(pk=1) n = it.tracking_info.count() - self.assertFalse(it.move(it.location, 'Moved to same place', None)) + self.assertTrue(it.move(it.location, 'Moved to same place', None)) # Ensure tracking info was not added - self.assertEqual(it.tracking_info.count(), n) + self.assertEqual(it.tracking_info.count(), n + 1) def test_partial_move(self): """Test partial stock moving.""" diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 4770dcadfb..e37375d23a 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -2648,13 +2648,13 @@ function loadStockTrackingTable(table, options) { field: 'deltas', title: '{% trans "Details" %}', formatter: function(details, row) { - var html = ``; - if (!details) { - html += '
'; - return html; + if (!details || !Object.keys(details).length) { + return `{% trans "No changes" %}`; } + let html = ``; + // Part information if (details.part) { html += `
{% trans "Part" %}`;