From 43a73595014ae590facde7ef39a7b97988f94f8b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 18 Feb 2021 15:44:43 +1100 Subject: [PATCH] Logic fix for "guessing" next build order number --- InvenTree/build/models.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index ae7ea14ece..2872fecb55 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -379,24 +379,32 @@ class Build(MPTTModel): if cls.objects.count() == 0: return None - build = cls.objects.last() + # Extract the "most recent" build order reference + builds = cls.objects.exclude(reference=None) + + if not builds.exists(): + return None + + build = builds.last() ref = build.reference if not ref: return None - tries = set() + tries = set(ref) + + new_ref = ref while 1: - new_ref = increment(ref) + new_ref = increment(new_ref) if new_ref in tries: # We are potentially stuck in a loop - simply return the original reference return ref + # Check if the existing build reference exists if cls.objects.filter(reference=new_ref).exists(): tries.add(new_ref) - new_ref = increment(new_ref) else: break