[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
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
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.utils.translation import ugettext_lazy as _
from django.core.validators import MinValueValidator 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 import models
from django.db.models import Sum, Q, UniqueConstraint 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_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( currency = models.CharField(
max_length=3, max_length=3,
verbose_name=_('Currency'), verbose_name=_('Currency'),
@ -266,6 +268,18 @@ class Company(models.Model):
return self.purchase_orders.filter(status__in=PurchaseOrderStatus.FAILED) 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): class Contact(models.Model):
""" A Contact represents a person who works at a particular company. """ A Contact represents a person who works at a particular company.

View File

@ -0,0 +1,25 @@
# Generated by Django 3.2.12 on 2022-03-29 22:46
from django.db import migrations, models
import order.models
class Migration(migrations.Migration):
dependencies = [
('company', '0043_company_is_deleted'),
('order', '0063_alter_purchaseorderlineitem_unique_together'),
]
operations = [
migrations.AlterField(
model_name='purchaseorder',
name='supplier',
field=models.ForeignKey(help_text='Company from which the items are being ordered', limit_choices_to={'is_supplier': True}, on_delete=models.SET(order.models.get_deleted_company), related_name='purchase_orders', to='company.company', verbose_name='Supplier'),
),
migrations.AlterField(
model_name='salesorder',
name='customer',
field=models.ForeignKey(help_text='Company to which the items are being sold', limit_choices_to={'is_customer': True}, null=True, on_delete=models.SET(order.models.get_deleted_company), related_name='sales_orders', to='company.company', verbose_name='Customer'),
),
]

View File

@ -92,6 +92,9 @@ def get_next_so_number():
return reference return reference
def get_deleted_company():
return Company.objects.get_or_create(name='deleted', email='deleted',is_deleted=True)[0]
class Order(ReferenceIndexingMixin): class Order(ReferenceIndexingMixin):
""" Abstract model for an order. """ Abstract model for an order.
@ -219,7 +222,7 @@ class PurchaseOrder(Order):
help_text=_('Purchase order status')) help_text=_('Purchase order status'))
supplier = models.ForeignKey( supplier = models.ForeignKey(
Company, on_delete=models.CASCADE, Company, on_delete=models.SET(get_deleted_company),
limit_choices_to={ limit_choices_to={
'is_supplier': True, 'is_supplier': True,
}, },
@ -567,7 +570,7 @@ class SalesOrder(Order):
customer = models.ForeignKey( customer = models.ForeignKey(
Company, Company,
on_delete=models.SET_NULL, on_delete=models.SET(get_deleted_company),
null=True, null=True,
limit_choices_to={'is_customer': True}, limit_choices_to={'is_customer': True},
related_name='sales_orders', related_name='sales_orders',