diff --git a/inventree/build.py b/inventree/build.py index 8bc0c5e..d20fb3d 100644 --- a/inventree/build.py +++ b/inventree/build.py @@ -2,6 +2,7 @@ import inventree.base import inventree.report +import inventree.stock class Build( @@ -48,3 +49,41 @@ def complete( def finish(self, *args, **kwargs): """Alias for complete""" return self.complete(*args, **kwargs) + + def getLines(self, **kwargs): + """ Return the build line items associated with this build order """ + return BuildLine.list(self._api, build=self.pk, **kwargs) + + +class BuildLine( + inventree.base.InventreeObject, +): + """ Class representing the BuildLine database model """ + + URL = 'build/line/' + MODEL_TYPE = 'buildline' + + def getBuild(self): + """Return the Build object associated with this line item""" + return Build(self._api, self.build) + + +class BuildItem( + inventree.base.InventreeObject, +): + """ Class representing the BuildItem database model """ + + URL = 'build/item/' + MODEL_TYPE = 'builditem' + + def getBuild(self): + """Return the Build object associated with this build item""" + return Build(self._api, self.build) + + def getBuildLine(self): + """Return the BuildLine object associated with this build item""" + return BuildLine(self._api, self.build_line) + + def getStockItem(self): + """Return the StockItem object associated with this build item""" + return inventree.stock.StockItem(self._api, self.stock_item) diff --git a/test/test_build.py b/test/test_build.py index 45929bb..b5e7d56 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -12,7 +12,7 @@ from test_api import InvenTreeTestCase # noqa: E402 from inventree.base import Attachment # noqa: E402 -from inventree.build import Build # noqa: E402 +from inventree.build import Build, BuildLine # noqa: E402 class BuildOrderTest(InvenTreeTestCase): @@ -36,9 +36,9 @@ def get_build(self): self.api, { "title": "Automated test build", - "part": 25, + "part": 100, "quantity": 100, - "reference": f"BO-{n+1:04d}", + "reference": f"BO-{n + 1:04d}", } ) else: @@ -93,7 +93,7 @@ def test_build_cancel(self): "title": "Automated test build", "part": 25, "quantity": 100, - "reference": f"BO-{n+1:04d}" + "reference": f"BO-{n + 1:04d}" } ) @@ -118,7 +118,7 @@ def test_build_complete(self): "title": "Automated test build", "part": 25, "quantity": 100, - "reference": f"BO-{n+1:04d}" + "reference": f"BO-{n + 1:04d}" } ) @@ -144,3 +144,16 @@ def test_build_complete(self): # Check status self.assertEqual(build.status, 40) self.assertEqual(build.status_text, 'Complete') + + def test_build_lines(self): + """ Test retrieval of build line items. """ + + build = self.get_build() + + lines = build.getLines() + + self.assertGreater(len(lines), 0) + + for line in lines: + self.assertEqual(line.build, build.pk) + self.assertIsInstance(line, BuildLine)