diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 33800e2387..8e7e61303e 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1228,6 +1228,13 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'validator': bool, }, + 'STOCK_DELETE_DEPLETED_DEFAULT': { + 'name': _('Delete Depleted Stock'), + 'description': _('Determines default behaviour when a stock item is depleted'), + 'default': True, + 'validator': bool, + }, + 'STOCK_BATCH_CODE_TEMPLATE': { 'name': _('Batch Code Template'), 'description': _('Template for generating default batch codes for stock items'), diff --git a/InvenTree/stock/migrations/0091_alter_stockitem_delete_on_deplete.py b/InvenTree/stock/migrations/0091_alter_stockitem_delete_on_deplete.py new file mode 100644 index 0000000000..18ea2c0cd7 --- /dev/null +++ b/InvenTree/stock/migrations/0091_alter_stockitem_delete_on_deplete.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.16 on 2022-12-24 12:29 + +from django.db import migrations, models +import stock.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0090_stocklocation_structural'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitem', + name='delete_on_deplete', + field=models.BooleanField(default=stock.models.default_delete_on_deplete, help_text='Delete this Stock Item when stock is depleted', verbose_name='Delete on deplete'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index a4a9124a2a..00f20cf93c 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -13,6 +13,7 @@ from django.db import models, transaction from django.db.models import Q, Sum from django.db.models.functions import Coalesce from django.db.models.signals import post_delete, post_save, pre_delete +from django.db.utils import IntegrityError, OperationalError from django.dispatch import receiver from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -251,6 +252,20 @@ def generate_batch_code(): return Template(batch_template).render(context) +def default_delete_on_deplete(): + """Return a default value for the 'delete_on_deplete' field. + + Prior to 2022-12-24, this field was set to True by default. + Now, there is a user-configurable setting to govern default behaviour. + """ + + try: + return common.models.InvenTreeSetting.get_setting('STOCK_DELETE_DEPLETED_DEFAULT', True) + except (IntegrityError, OperationalError): + # Revert to original default behaviour + return True + + class StockItem(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel): """A StockItem object represents a quantity of physical instances of a part. @@ -760,7 +775,10 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, MPTTModel): review_needed = models.BooleanField(default=False) - delete_on_deplete = models.BooleanField(default=True, verbose_name=_('Delete on deplete'), help_text=_('Delete this Stock Item when stock is depleted')) + delete_on_deplete = models.BooleanField( + default=default_delete_on_deplete, + verbose_name=_('Delete on deplete'), help_text=_('Delete this Stock Item when stock is depleted') + ) status = models.PositiveIntegerField( default=StockStatus.OK, diff --git a/InvenTree/templates/InvenTree/settings/stock.html b/InvenTree/templates/InvenTree/settings/stock.html index f45bf16e3c..149fdd8e2f 100644 --- a/InvenTree/templates/InvenTree/settings/stock.html +++ b/InvenTree/templates/InvenTree/settings/stock.html @@ -13,6 +13,7 @@ {% include "InvenTree/settings/setting.html" with key="SERIAL_NUMBER_GLOBALLY_UNIQUE" icon="fa-hashtag" %} {% include "InvenTree/settings/setting.html" with key="SERIAL_NUMBER_AUTOFILL" icon="fa-magic" %} + {% include "InvenTree/settings/setting.html" with key="STOCK_DELETE_DEPLETED_DEFAULT" icon="fa-trash-alt" %} {% 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" %}