Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions inventree/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions test/test_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)