2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-10 22:00:56 +00:00

[BUG] Deleting a Customer Breaks Associated Sales Orders

Add special protected deleted company
Fixes #2788
This commit is contained in:
Matthias
2022-03-30 01:29:36 +02:00
parent c082cec3ee
commit aa30e62ad8
4 changed files with 63 additions and 3 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2022-03-29 22:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('company', '0042_supplierpricebreak_updated'),
]
operations = [
migrations.AddField(
model_name='company',
name='is_deleted',
field=models.BooleanField(default=False, help_text='Is this company a deleted placeholder?', verbose_name='is deleted'),
),
]

View File

@@ -9,7 +9,7 @@ import os
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
from django.core.exceptions import ValidationError, PermissionDenied
from django.db import models
from django.db.models import Sum, Q, UniqueConstraint
@@ -147,6 +147,8 @@ class Company(models.Model):
is_manufacturer = models.BooleanField(default=False, verbose_name=_('is manufacturer'), help_text=_('Does this company manufacture parts?'))
is_deleted = models.BooleanField(default=False, verbose_name=_('is deleted'), help_text=_('Is this company a deleted placeholder?'))
currency = models.CharField(
max_length=3,
verbose_name=_('Currency'),
@@ -266,6 +268,18 @@ class Company(models.Model):
return self.purchase_orders.filter(status__in=PurchaseOrderStatus.FAILED)
def save(self, *args, **kwargs):
"""Save the instance, unless it is the magic already deleted object"""
if self.pk and self.is_deleted:
raise PermissionDenied(_('This company is a placeholder and can not be updated'))
return super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
"""Delete the instance, unless it is the magic already deleted object"""
if self.is_deleted:
raise PermissionDenied(_('This company is a placeholder and can not be deleted'))
return super().delete(*args, **kwargs)
class Contact(models.Model):
""" A Contact represents a person who works at a particular company.