diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index bb7de56e99..8bd100dcc9 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -932,6 +932,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'validator': bool, }, + 'STOCK_BATCH_CODE_TEMPLATE': { + 'name': _('Batch Code Template'), + 'description': _('Template for generating default batch codes for stock items'), + 'default': '', + }, + 'STOCK_ENABLE_EXPIRY': { 'name': _('Stock Expiry'), 'description': _('Enable stock expiry functionality'), diff --git a/InvenTree/stock/migrations/0074_alter_stockitem_batch.py b/InvenTree/stock/migrations/0074_alter_stockitem_batch.py new file mode 100644 index 0000000000..646e25199a --- /dev/null +++ b/InvenTree/stock/migrations/0074_alter_stockitem_batch.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.12 on 2022-04-26 10:19 + +from django.db import migrations, models +import stock.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0073_alter_stockitem_belongs_to'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitem', + name='batch', + field=models.CharField(blank=True, default=stock.models.generate_batch_code, help_text='Batch code for this stock item', max_length=100, null=True, verbose_name='Batch Code'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index fa343e9a9c..39697c1bca 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -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,32 @@ 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() + + # Pass context data through to the template randering. + # The folowing context variables are availble for custom batch code generation + 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 +672,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( diff --git a/InvenTree/templates/InvenTree/settings/stock.html b/InvenTree/templates/InvenTree/settings/stock.html index a3c0940c1f..f0cd403d68 100644 --- a/InvenTree/templates/InvenTree/settings/stock.html +++ b/InvenTree/templates/InvenTree/settings/stock.html @@ -11,6 +11,7 @@ + {% include "InvenTree/settings/setting.html" with key="STOCK_BATCH_CODE_TEMPLATE" icon="fa-layer-group" %} {% include "InvenTree/settings/setting.html" with key="STOCK_ENABLE_EXPIRY" icon="fa-stopwatch" %} {% include "InvenTree/settings/setting.html" with key="STOCK_STALE_DAYS" icon="fa-calendar" %} {% include "InvenTree/settings/setting.html" with key="STOCK_ALLOW_EXPIRED_SALE" icon="fa-truck" %}