Cleanup models

This commit is contained in:
Oliver 2021-12-21 08:49:53 +11:00
parent 785c5edf96
commit dd53748f9f
4 changed files with 36 additions and 42 deletions

View File

@ -0,0 +1,19 @@
# Generated by Django 3.2.10 on 2021-12-20 21:49
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('stock', '0072_remove_stockitem_scheduled_for_deletion'),
]
operations = [
migrations.AlterField(
model_name='stockitem',
name='belongs_to',
field=models.ForeignKey(blank=True, help_text='Is this item installed in another item?', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='installed_parts', to='stock.stockitem', verbose_name='Installed In'),
),
]

View File

@ -1,20 +0,0 @@
# Generated by Django 3.2.10 on 2021-12-20 13:54
from django.db import migrations
import django.db.models.deletion
import mptt.fields
class Migration(migrations.Migration):
dependencies = [
('stock', '0072_remove_stockitem_scheduled_for_deletion'),
]
operations = [
migrations.AlterField(
model_name='stockitem',
name='parent',
field=mptt.fields.TreeForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='stock.stockitem', verbose_name='Parent Stock Item'),
),
]

View File

@ -455,10 +455,11 @@ class StockItem(MPTTModel):
uid = models.CharField(blank=True, max_length=128, help_text=("Unique identifier field"))
# Note: When a StockItem is deleted, a pre_delete signal handles the parent/child relationship
parent = TreeForeignKey(
'self',
verbose_name=_('Parent Stock Item'),
on_delete=models.SET_NULL,
on_delete=models.DO_NOTHING,
blank=True, null=True,
related_name='children'
)
@ -477,6 +478,7 @@ class StockItem(MPTTModel):
help_text=_('Select a matching supplier part for this stock item')
)
# Note: When a StockLocation is deleted, stock items are updated via a signal
location = TreeForeignKey(
StockLocation, on_delete=models.DO_NOTHING,
verbose_name=_('Stock Location'),
@ -492,10 +494,11 @@ class StockItem(MPTTModel):
help_text=_('Packaging this stock item is stored in')
)
# When deleting a stock item with installed items, those installed items are also installed
belongs_to = models.ForeignKey(
'self',
verbose_name=_('Installed In'),
on_delete=models.DO_NOTHING,
on_delete=models.CASCADE,
related_name='installed_parts', blank=True, null=True,
help_text=_('Is this item installed in another item?')
)
@ -800,14 +803,14 @@ class StockItem(MPTTModel):
def can_delete(self):
""" Can this stock item be deleted? It can NOT be deleted under the following circumstances:
- Has child StockItems
- Has installed stock items
- Has a serial number and is tracked
- Is installed inside another StockItem
- It has been assigned to a SalesOrder
- It has been assigned to a BuildOrder
"""
if self.child_count > 0:
if self.installed_item_count() > 0:
return False
if self.part.trackable and self.serial is not None:
@ -853,20 +856,13 @@ class StockItem(MPTTModel):
return installed
def installedItemCount(self):
def installed_item_count(self):
"""
Return the number of stock items installed inside this one.
"""
return self.installed_parts.count()
def hasInstalledItems(self):
"""
Returns true if this stock item has other stock items installed in it.
"""
return self.installedItemCount() > 0
@transaction.atomic
def installStockItem(self, other_item, quantity, user, notes):
"""
@ -1225,6 +1221,8 @@ class StockItem(MPTTModel):
location = kwargs.get('location', None)
notes = kwargs.get('notes', None)
parent_id = self.parent.pk if self.parent else None
for other in other_items:
# If the stock item cannot be merged, return
if not self.can_merge(other, raise_error=raise_error, **kwargs):
@ -1246,7 +1244,11 @@ class StockItem(MPTTModel):
allocation.stock_item = self()
allocation.save()
# Delete the other stock item
# Prevent atomicity issues when we are merging our own "parent" part in
if parent_id and parent_id == other.pk:
self.parent = None
self.save()
other.delete()
self.add_tracking_entry(
@ -1757,7 +1759,8 @@ class StockItem(MPTTModel):
@receiver(pre_delete, sender=StockItem, dispatch_uid='stock_item_pre_delete_log')
def before_delete_stock_item(sender, instance, using, **kwargs):
""" Receives pre_delete signal from StockItem object.
"""
Receives pre_delete signal from StockItem object.
Before a StockItem is deleted, ensure that each child object is updated,
to point to the new parent item.

View File

@ -274,14 +274,6 @@
<div class='alert alert-block alert-warning'>
{% trans "This stock item is serialized - it has a unique serial number and the quantity cannot be adjusted." %}
</div>
{% elif item.child_count > 0 %}
<div class='alert alert-block alert-warning'>
{% trans "This stock item cannot be deleted as it has child items" %}
</div>
{% elif item.delete_on_deplete and item.can_delete %}
<div class='alert alert-block alert-warning'>
{% trans "This stock item will be automatically deleted when all stock is depleted." %}
</div>
{% endif %}
</div>