mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +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:
		| @@ -920,18 +920,24 @@ class BuildAllocationSerializer(serializers.Serializer): | ||||
|                 if build_line.bom_item.consumable: | ||||
|                     continue | ||||
|  | ||||
|                 params = { | ||||
|                     "build_line": build_line, | ||||
|                     "stock_item": stock_item, | ||||
|                     "install_into": output, | ||||
|                 } | ||||
|  | ||||
|                 try: | ||||
|                     # Create a new BuildItem to allocate stock | ||||
|                     build_item, created = BuildItem.objects.get_or_create( | ||||
|                         build_line=build_line, | ||||
|                         stock_item=stock_item, | ||||
|                         install_into=output, | ||||
|                     ) | ||||
|                     if created: | ||||
|                         build_item.quantity = quantity | ||||
|                     else: | ||||
|                     if build_item := BuildItem.objects.filter(**params).first(): | ||||
|                         # Find an existing BuildItem for this stock item | ||||
|                         # If it exists, increase the 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: | ||||
|                     # Catch model errors and re-throw as DRF errors | ||||
|                     raise ValidationError(detail=serializers.as_serializer_error(exc)) | ||||
|   | ||||
| @@ -822,6 +822,58 @@ class BuildAllocationTest(BuildAPITest): | ||||
|         allocation.refresh_from_db() | ||||
|         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): | ||||
|     """Unit tests for over allocation of stock items against a build order. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user