2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 20:45:44 +00:00

Build completion now handles unique serial numbers

Provide a method to test if a serial number matches for a given part
This commit is contained in:
Oliver Walters
2019-07-22 15:55:36 +10:00
parent 9a8e4d25d2
commit 23d03d6b9b
4 changed files with 93 additions and 27 deletions

View File

@ -199,7 +199,7 @@ class Build(models.Model):
build_item.save()
@transaction.atomic
def completeBuild(self, location, user):
def completeBuild(self, location, serial_numbers, user):
""" Mark the Build as COMPLETE
- Takes allocated items from stock
@ -227,19 +227,36 @@ class Build(models.Model):
self.completed_by = user
# Add stock of the newly created item
item = StockItem.objects.create(
part=self.part,
location=location,
quantity=self.quantity,
batch=str(self.batch) if self.batch else '',
notes='Built {q} on {now}'.format(
q=self.quantity,
now=str(datetime.now().date())
)
notes = 'Built {q} on {now}'.format(
q=self.quantity,
now=str(datetime.now().date())
)
item.save()
if self.part.trackable:
# Add new serial numbers
for serial in serial_numbers:
item = StockItem.objects.create(
part=self.part,
location=location,
quantity=1,
serial=serial,
batch=str(self.batch) if self.batch else '',
notes=notes
)
item.save()
else:
# Add stock of the newly created item
item = StockItem.objects.create(
part=self.part,
location=location,
quantity=self.quantity,
batch=str(self.batch) if self.batch else '',
notes=notes
)
item.save()
# Finally, mark the build as complete
self.status = BuildStatus.COMPLETE

View File

@ -225,7 +225,7 @@ class BuildComplete(AjaxUpdateView):
build = Build.objects.get(id=self.kwargs['pk'])
context = {}
# Build object
context['build'] = build
@ -263,21 +263,35 @@ class BuildComplete(AjaxUpdateView):
except StockLocation.DoesNotExist:
form.errors['location'] = ['Invalid location selected']
valid = False
serials = []
serials = request.POST.get('serial_numbers', '')
if build.part.trackable:
# A build for a trackable part must specify serial numbers
try:
serials = ExtractSerialNumbers(serials, build.quantity)
sn = request.POST.get('serial_numbers', '')
print(serials)
try:
# Exctract a list of provided serial numbers
serials = ExtractSerialNumbers(sn, build.quantity)
except ValidationError as e:
form.errors['serial_numbers'] = e.messages
valid = False
existing = []
for serial in serials:
if not StockItem.check_serial_number(build.part, serial):
existing.append(serial)
if len(existing) > 0:
exists = ",".join([str(x) for x in existing])
form.errors['serial_numbers'] = [_('The following serial numbers already exist: {sn}'.format(sn=exists))]
valid = False
except ValidationError as e:
form.errors['serial_numbers'] = e.messages
valid = False
if valid:
build.completeBuild(location, request.user)
build.completeBuild(location, serials, request.user)
data = {
'form_valid': valid,