Inherited BOM fix (#3579)

* Fix logic for get_used_in_bom_item_filter

* Include scheduling information for inherited BOMs
This commit is contained in:
Oliver 2022-08-19 12:21:38 +10:00 committed by GitHub
parent c8de2efd9d
commit 89d5df4f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 13 deletions

View File

@ -574,9 +574,20 @@ class PartScheduling(RetrieveAPI):
# Grab a list of BomItem objects that this part might be used in
bom_items = BomItem.objects.filter(part.get_used_in_bom_item_filter())
# Track all outstanding build orders
seen_builds = set()
for bom_item in bom_items:
# Find a list of active builds for this BomItem
if bom_item.inherited:
# An "inherited" BOM item filters down to variant parts also
childs = bom_item.part.get_descendants(include_self=True)
builds = Build.objects.filter(
status__in=BuildStatus.ACTIVE_CODES,
part__in=childs,
)
else:
builds = Build.objects.filter(
status__in=BuildStatus.ACTIVE_CODES,
part=bom_item.part,
@ -584,6 +595,12 @@ class PartScheduling(RetrieveAPI):
for build in builds:
# Ensure we don't double-count any builds
if build in seen_builds:
continue
seen_builds.add(build)
if bom_item.sub_part.trackable:
# Trackable parts are allocated against the outputs
required_quantity = build.remaining * bom_item.quantity

View File

@ -1436,7 +1436,7 @@ class Part(MetadataMixin, MPTTModel):
return parts
def get_used_in_bom_item_filter(self, include_inherited=True, include_variants=True, include_substitutes=True):
def get_used_in_bom_item_filter(self, include_variants=True, include_substitutes=True):
"""Return a BomItem queryset which returns all BomItem instances which refer to *this* part.
As the BOM allocation logic is somewhat complicted, there are some considerations:
@ -1454,13 +1454,6 @@ class Part(MetadataMixin, MPTTModel):
# Case A: This part is directly specified in a BomItem (we always use this case)
query = Q(
sub_part=self,
inherited=False,
)
if include_inherited:
query |= Q(
sub_part__in=parents,
inherited=True
)
if include_variants:
@ -1468,7 +1461,6 @@ class Part(MetadataMixin, MPTTModel):
query |= Q(
allow_variants=True,
sub_part__in=parents,
inherited=False,
)
# Case C: This part is a *substitute* of a part which is directly specified in a BomItem