Generate default batch code for stock items

This commit is contained in:
Oliver Walters 2022-04-26 20:18:33 +10:00
parent 3f92f009e4
commit 314cec5ad0

View File

@ -8,6 +8,8 @@ from __future__ import unicode_literals
import os
from jinja2 import Template
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError, FieldError
from django.urls import reverse
@ -213,6 +215,30 @@ class StockItemManager(TreeManager):
)
def generate_batch_code():
"""
Generate a default 'batch code' for a new StockItem.
This uses the value of the 'STOCK_BATCH_CODE_TEMPLATE' setting (if configured),
which can be passed through a simple template.
"""
batch_template = common.models.InvenTreeSetting.get_setting('STOCK_BATCH_CODE_TEMPLATE', '')
now = datetime.now()
context = {
'date': now,
'year': now.year,
'month': now.month,
'day': now.day,
'hour': now.minute,
'minute': now.minute,
}
return Template(batch_template).render(context)
class StockItem(MPTTModel):
"""
A StockItem object represents a quantity of physical instances of a part.
@ -644,7 +670,8 @@ class StockItem(MPTTModel):
batch = models.CharField(
verbose_name=_('Batch Code'),
max_length=100, blank=True, null=True,
help_text=_('Batch code for this stock item')
help_text=_('Batch code for this stock item'),
default=generate_batch_code,
)
quantity = models.DecimalField(