From 07c0838a579170fff296e70ca6442d83db917596 Mon Sep 17 00:00:00 2001 From: T0jan Date: Thu, 11 May 2023 17:36:25 +0200 Subject: [PATCH 1/2] add methods to install and uninstall StockItems to/from others --- inventree/stock.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/inventree/stock.py b/inventree/stock.py index 0fd7b74c..1548c3e8 100644 --- a/inventree/stock.py +++ b/inventree/stock.py @@ -225,6 +225,41 @@ def assignStock(self, customer, **kwargs): **kwargs ) + def installStock(self, item, quantity=1, **kwargs): + """Install the given item into this stock item + + Arguments: + stockItem: A stockItem instance or integer ID value + quantity: quantity of installed items. defaults to 1. + notes: Optional transaction notes""" + + if isinstance(item, StockItem): + item = item.pk + kwargs['quantity'] = quantity + kwargs['stock_item'] = item + + url = f"stock/{self.pk}/install/" + + return self._api.post(url, data=kwargs) + + def uninstallStock(self, location, quantity=1, **kwargs): + """Uninstalls this item from any stock item + + Arguments: + location: A StockLocation instance or integer ID value + quantity: quantity of removed items. defaults to 1. + notes: Optional transaction notes""" + + if isinstance(location, StockLocation): + location = location.pk + kwargs['quantity'] = quantity + kwargs['stock_item'] = self.pk + kwargs['location'] = location + + url = f"stock/{self.pk}/uninstall/" + + return self._api.post(url, data=kwargs) + def getPart(self): """ Return the base Part object associated with this StockItem """ return inventree.part.Part(self._api, self.part) From 923ce133c35bf923241231ec5619ec0e4ebd8a57 Mon Sep 17 00:00:00 2001 From: T0jan Date: Fri, 12 May 2023 10:39:57 +0200 Subject: [PATCH 2/2] add unittest for installStock --- test/test_stock.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test_stock.py b/test/test_stock.py index 186156cb..51221af2 100644 --- a/test/test_stock.py +++ b/test/test_stock.py @@ -415,3 +415,44 @@ def test_assign_stock(self): # Check the item is assigned self.assertTrue(assignitem.customer == customer.pk) + + def test_install_stock(self): + """Test install and uninstall a stock item from another""" + + items = StockItem.list(self.api, available=True) + self.assertTrue(len(items) > 1) + + # get a parent and a child part + parent_part = part.Part.list( + self.api, + trackable=True, + assembly=True, + has_stock=True + )[0] + parent_stock = parent_part.getStockItems()[0] + child_stock = items[0] + child_part = child_stock.getPart() + + # make sure the child is in the bom of the parent + + items = parent_part.getBomItems(search=child_part.name) + if not items: + part.BomItem.create( + self.api, { + 'part': parent_part.pk, + 'sub_part': child_part.pk, + 'quantity': 1 + } + ) + + # install the child into the parent + parent_stock.installStock(child_stock) + + # and uninstall it again + location = StockLocation.list(self.api)[0] + child_stock.uninstallStock(location) + + # check if the location is set correctly to confirm the uninstall + child_stock.reload() + new_location = child_stock.getLocation() + self.assertTrue(new_location.pk == location.pk)