mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Bug fix for allocating items to build (#6532)
* Bug fix for allocating items to build - Handle case where allocated quantity is less than 1 - Add unit tests - Closes https://github.com/inventree/InvenTree/issues/6145 * Remove debug messgae
This commit is contained in:
parent
7694092935
commit
a310437dc7
@ -920,18 +920,24 @@ class BuildAllocationSerializer(serializers.Serializer):
|
|||||||
if build_line.bom_item.consumable:
|
if build_line.bom_item.consumable:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
params = {
|
||||||
|
"build_line": build_line,
|
||||||
|
"stock_item": stock_item,
|
||||||
|
"install_into": output,
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create a new BuildItem to allocate stock
|
if build_item := BuildItem.objects.filter(**params).first():
|
||||||
build_item, created = BuildItem.objects.get_or_create(
|
# Find an existing BuildItem for this stock item
|
||||||
build_line=build_line,
|
# If it exists, increase the quantity
|
||||||
stock_item=stock_item,
|
|
||||||
install_into=output,
|
|
||||||
)
|
|
||||||
if created:
|
|
||||||
build_item.quantity = quantity
|
|
||||||
else:
|
|
||||||
build_item.quantity += quantity
|
build_item.quantity += quantity
|
||||||
build_item.save()
|
build_item.save()
|
||||||
|
else:
|
||||||
|
# Create a new BuildItem to allocate stock
|
||||||
|
build_item = BuildItem.objects.create(
|
||||||
|
quantity=quantity,
|
||||||
|
**params
|
||||||
|
)
|
||||||
except (ValidationError, DjangoValidationError) as exc:
|
except (ValidationError, DjangoValidationError) as exc:
|
||||||
# Catch model errors and re-throw as DRF errors
|
# Catch model errors and re-throw as DRF errors
|
||||||
raise ValidationError(detail=serializers.as_serializer_error(exc))
|
raise ValidationError(detail=serializers.as_serializer_error(exc))
|
||||||
|
@ -822,6 +822,58 @@ class BuildAllocationTest(BuildAPITest):
|
|||||||
allocation.refresh_from_db()
|
allocation.refresh_from_db()
|
||||||
self.assertEqual(allocation.quantity, 5000)
|
self.assertEqual(allocation.quantity, 5000)
|
||||||
|
|
||||||
|
def test_fractional_allocation(self):
|
||||||
|
"""Test allocation of a fractional quantity of stock items.
|
||||||
|
|
||||||
|
Ref: https://github.com/inventree/InvenTree/issues/6508
|
||||||
|
"""
|
||||||
|
|
||||||
|
si = StockItem.objects.get(pk=2)
|
||||||
|
|
||||||
|
# Find line item
|
||||||
|
line = self.build.build_lines.all().filter(bom_item__sub_part=si.part).first()
|
||||||
|
|
||||||
|
# Test a fractional quantity when the *available* quantity is greater than 1
|
||||||
|
si.quantity = 100
|
||||||
|
si.save()
|
||||||
|
|
||||||
|
response = self.post(
|
||||||
|
self.url,
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"build_line": line.pk,
|
||||||
|
"stock_item": si.pk,
|
||||||
|
"quantity": 0.1616,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
expected_code=201
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test a fractional quantity when the *available* quantity is less than 1
|
||||||
|
si = StockItem.objects.create(
|
||||||
|
part=si.part,
|
||||||
|
quantity=0.3159,
|
||||||
|
tree_id=0,
|
||||||
|
level=0,
|
||||||
|
lft=0, rght=0
|
||||||
|
)
|
||||||
|
|
||||||
|
response = self.post(
|
||||||
|
self.url,
|
||||||
|
{
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"build_line": line.pk,
|
||||||
|
"stock_item": si.pk,
|
||||||
|
"quantity": 0.1616,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
expected_code=201,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BuildOverallocationTest(BuildAPITest):
|
class BuildOverallocationTest(BuildAPITest):
|
||||||
"""Unit tests for over allocation of stock items against a build order.
|
"""Unit tests for over allocation of stock items against a build order.
|
||||||
|
Loading…
Reference in New Issue
Block a user