Add default value for BuildOrder reference

This commit is contained in:
Oliver 2021-07-09 00:15:49 +10:00
parent 245c04367b
commit 5016d44b83
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,20 @@
# Generated by Django 3.2.4 on 2021-07-08 14:14
import InvenTree.validators
import build.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('build', '0029_auto_20210601_1525'),
]
operations = [
migrations.AlterField(
model_name='build',
name='reference',
field=models.CharField(default=build.models.get_next_build_number, help_text='Build Order Reference', max_length=64, unique=True, validators=[InvenTree.validators.validate_build_order_reference], verbose_name='Reference'),
),
]

View File

@ -37,6 +37,35 @@ from part import models as PartModels
from users import models as UserModels
def get_next_build_number():
"""
Returns the next available BuildOrder reference number
"""
if Build.objects.count() == 0:
return
build = Build.objects.exclude(reference=None).last()
attempts = set([build.reference])
reference = build.reference
while 1:
reference = increment(build.reference)
if reference in attempts:
# Escape infinite recursion
return reference
if Build.objects.filter(reference=reference).exists():
attempts.add(reference)
else:
break
return reference
class Build(MPTTModel):
""" A Build object organises the creation of new StockItem objects from other existing StockItem objects.
@ -130,6 +159,7 @@ class Build(MPTTModel):
blank=False,
help_text=_('Build Order Reference'),
verbose_name=_('Reference'),
default=get_next_build_number,
validators=[
validate_build_order_reference
]