mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 04:26:44 +00:00
Optionally auto-allocate stock items when creating a new build output
This commit is contained in:
parent
24c13b04b6
commit
07af0902a3
@ -646,11 +646,13 @@ class Build(MPTTModel, ReferenceIndexingMixin):
|
|||||||
batch: Override batch code
|
batch: Override batch code
|
||||||
serials: Serial numbers
|
serials: Serial numbers
|
||||||
location: Override location
|
location: Override location
|
||||||
|
auto_allocate: Automatically allocate stock with matching serial numbers
|
||||||
"""
|
"""
|
||||||
|
|
||||||
batch = kwargs.get('batch', self.batch)
|
batch = kwargs.get('batch', self.batch)
|
||||||
location = kwargs.get('location', self.destination)
|
location = kwargs.get('location', self.destination)
|
||||||
serials = kwargs.get('serials', None)
|
serials = kwargs.get('serials', None)
|
||||||
|
auto_allocate = kwargs.get('auto_allocate', False)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Determine if we can create a single output (with quantity > 0),
|
Determine if we can create a single output (with quantity > 0),
|
||||||
@ -672,6 +674,9 @@ class Build(MPTTModel, ReferenceIndexingMixin):
|
|||||||
Create multiple build outputs with a single quantity of 1
|
Create multiple build outputs with a single quantity of 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Quantity *must* be an integer at this point!
|
||||||
|
quantity = int(quantity)
|
||||||
|
|
||||||
for ii in range(quantity):
|
for ii in range(quantity):
|
||||||
|
|
||||||
if serials:
|
if serials:
|
||||||
@ -679,7 +684,7 @@ class Build(MPTTModel, ReferenceIndexingMixin):
|
|||||||
else:
|
else:
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
StockModels.StockItem.objects.create(
|
output = StockModels.StockItem.objects.create(
|
||||||
quantity=1,
|
quantity=1,
|
||||||
location=location,
|
location=location,
|
||||||
part=self.part,
|
part=self.part,
|
||||||
@ -689,6 +694,38 @@ class Build(MPTTModel, ReferenceIndexingMixin):
|
|||||||
is_building=True,
|
is_building=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if auto_allocate and serial is not None:
|
||||||
|
|
||||||
|
# Get a list of BomItem objects which point to "trackable" parts
|
||||||
|
|
||||||
|
for bom_item in self.part.get_trackable_parts():
|
||||||
|
|
||||||
|
parts = bom_item.get_valid_parts_for_allocation()
|
||||||
|
|
||||||
|
for part in parts:
|
||||||
|
|
||||||
|
items = StockModels.StockItem.objects.filter(
|
||||||
|
part=part,
|
||||||
|
serial=str(serial),
|
||||||
|
quantity=1,
|
||||||
|
).filter(StockModels.StockItem.IN_STOCK_FILTER)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test if there is a matching serial number!
|
||||||
|
"""
|
||||||
|
if items.exists() and items.count() == 1:
|
||||||
|
stock_item = items[0]
|
||||||
|
|
||||||
|
# Allocate the stock item
|
||||||
|
BuildItem.objects.create(
|
||||||
|
build=self,
|
||||||
|
bom_item=bom_item,
|
||||||
|
stock_item=stock_item,
|
||||||
|
quantity=quantity,
|
||||||
|
install_into=output,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
"""
|
"""
|
||||||
Create a single build output of the given quantity
|
Create a single build output of the given quantity
|
||||||
|
@ -285,7 +285,21 @@ class BuildOutputCreateSerializer(serializers.Serializer):
|
|||||||
"""
|
"""
|
||||||
Generate the new build output(s)
|
Generate the new build output(s)
|
||||||
"""
|
"""
|
||||||
...
|
|
||||||
|
data = self.validated_data
|
||||||
|
|
||||||
|
quantity = data['quantity']
|
||||||
|
batch_code = data.get('batch_code', '')
|
||||||
|
auto_allocate = data.get('auto_allocate', False)
|
||||||
|
|
||||||
|
build = self.get_build()
|
||||||
|
|
||||||
|
build.create_build_output(
|
||||||
|
quantity,
|
||||||
|
serials=self.serials,
|
||||||
|
batch=batch_code,
|
||||||
|
auto_allocate=auto_allocate,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BuildOutputDeleteSerializer(serializers.Serializer):
|
class BuildOutputDeleteSerializer(serializers.Serializer):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user