mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Generate default batch code for stock items
This commit is contained in:
		| @@ -8,6 +8,8 @@ from __future__ import unicode_literals | |||||||
|  |  | ||||||
| import os | import os | ||||||
|  |  | ||||||
|  | from jinja2 import Template | ||||||
|  |  | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
| from django.core.exceptions import ValidationError, FieldError | from django.core.exceptions import ValidationError, FieldError | ||||||
| from django.urls import reverse | 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): | class StockItem(MPTTModel): | ||||||
|     """ |     """ | ||||||
|     A StockItem object represents a quantity of physical instances of a part. |     A StockItem object represents a quantity of physical instances of a part. | ||||||
| @@ -644,7 +670,8 @@ class StockItem(MPTTModel): | |||||||
|     batch = models.CharField( |     batch = models.CharField( | ||||||
|         verbose_name=_('Batch Code'), |         verbose_name=_('Batch Code'), | ||||||
|         max_length=100, blank=True, null=True, |         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( |     quantity = models.DecimalField( | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user