Merge pull request #2791 from matmair/matmair/issue2788

Preserve orders if companies are deleted
This commit is contained in:
Oliver 2022-05-04 22:23:47 +10:00 committed by GitHub
commit 4053a91450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 70 additions and 14 deletions

View File

@ -290,9 +290,6 @@ class Contact(models.Model):
role = models.CharField(max_length=100, blank=True) role = models.CharField(max_length=100, blank=True)
company = models.ForeignKey(Company, related_name='contacts',
on_delete=models.CASCADE)
class ManufacturerPart(models.Model): class ManufacturerPart(models.Model):
""" Represents a unique part as provided by a Manufacturer """ Represents a unique part as provided by a Manufacturer

View File

@ -23,6 +23,8 @@
<span class='fas fa-shopping-cart'/> <span class='fas fa-shopping-cart'/>
</button> </button>
{% endif %} {% endif %}
{% define perms.company.change_company or perms.company.delete_company as has_permission %}
{% if has_permission %}
<button id='company-edit-actions' title='{% trans "Company actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'> <button id='company-edit-actions' title='{% trans "Company actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
<span class='fas fa-tools'></span> <span class='caret'></span> <span class='fas fa-tools'></span> <span class='caret'></span>
</button> </button>
@ -38,6 +40,7 @@
</a></li> </a></li>
{% endif %} {% endif %}
</ul> </ul>
{% endif %}
{% endblock actions %} {% endblock actions %}
{% block thumbnail %} {% block thumbnail %}

View File

@ -88,7 +88,14 @@ src="{% static 'img/blank_image.png' %}"
<tr> <tr>
<td><span class='fas fa-industry'></span></td> <td><span class='fas fa-industry'></span></td>
<td>{% trans "Manufacturer" %}</td> <td>{% trans "Manufacturer" %}</td>
<td><a href="{% url 'company-detail' part.manufacturer.id %}">{{ part.manufacturer.name }}</a>{% include "clip.html"%}</td> <td>
{% if part.manufacturer %}
<a href="{% url 'company-detail' part.manufacturer.id %}">{{ part.manufacturer.name }}</a>{% include "clip.html"%}
{% else %}
<em>{% trans "No manufacturer information available" %}</em>
{% endif %}
{% endif %}
</td>
</tr> </tr>
<tr> <tr>
<td><span class='fas fa-hashtag'></span></td> <td><span class='fas fa-hashtag'></span></td>
@ -329,7 +336,11 @@ $('#delete-part').click(function() {
deleteManufacturerPart({{ part.pk }}, { deleteManufacturerPart({{ part.pk }}, {
onSuccess: function() { onSuccess: function() {
{% if part.manufacturer %}
window.location.href = "{% url 'company-detail' part.manufacturer.id %}"; window.location.href = "{% url 'company-detail' part.manufacturer.id %}";
{% else%}
window.location.href = "{% url 'index' %}";
{% endif %}
} }
}); });
}); });

View File

@ -85,7 +85,13 @@ src="{% static 'img/blank_image.png' %}"
<tr> <tr>
<td><span class='fas fa-building'></span></td> <td><span class='fas fa-building'></span></td>
<td>{% trans "Supplier" %}</td> <td>{% trans "Supplier" %}</td>
<td><a href="{% url 'company-detail' part.supplier.id %}">{{ part.supplier.name }}</a>{% include "clip.html"%}</td></tr> <td>{% if part.supplier %}
<a href="{% url 'company-detail' part.supplier.id %}">{{ part.supplier.name }}</a>{% include "clip.html"%}
{% else %}
<em>{% trans "No supplier information available" %}</em>
{% endif %}
</td>
</tr>
<tr> <tr>
<td><span class='fas fa-hashtag'></span></td> <td><span class='fas fa-hashtag'></span></td>
<td>{% trans "SKU" %}</td> <td>{% trans "SKU" %}</td>
@ -356,7 +362,9 @@ $('#delete-part').click(function() {
deleteSupplierPart({{ part.pk }}, { deleteSupplierPart({{ part.pk }}, {
onSuccess: function() { onSuccess: function() {
{% if part.supplier %}
window.location.href = "{% url 'company-detail' part.supplier.id %}"; window.location.href = "{% url 'company-detail' part.supplier.id %}";
{% endif %}
} }
}); });
}); });

View File

@ -0,0 +1,20 @@
# Generated by Django 3.2.13 on 2022-04-30 22:11
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('company', '0042_supplierpricebreak_updated'),
('order', '0065_alter_purchaseorderlineitem_part'),
]
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}, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='purchase_orders', to='company.company', verbose_name='Supplier'),
),
]

View File

@ -228,7 +228,7 @@ class PurchaseOrder(Order):
prefix = getSetting('PURCHASEORDER_REFERENCE_PREFIX') prefix = getSetting('PURCHASEORDER_REFERENCE_PREFIX')
return f"{prefix}{self.reference} - {self.supplier.name}" return f"{prefix}{self.reference} - {self.supplier.name if self.supplier else _('deleted')}"
reference = models.CharField( reference = models.CharField(
unique=True, unique=True,
@ -243,7 +243,8 @@ 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_NULL,
null=True,
limit_choices_to={ limit_choices_to={
'is_supplier': True, 'is_supplier': True,
}, },
@ -575,7 +576,7 @@ class SalesOrder(Order):
prefix = getSetting('SALESORDER_REFERENCE_PREFIX') prefix = getSetting('SALESORDER_REFERENCE_PREFIX')
return f"{prefix}{self.reference} - {self.customer.name}" return f"{prefix}{self.reference} - {self.customer.name if self.customer else _('deleted')}"
def get_absolute_url(self): def get_absolute_url(self):
return reverse('so-detail', kwargs={'pk': self.id}) return reverse('so-detail', kwargs={'pk': self.id})
@ -938,7 +939,7 @@ class PurchaseOrderLineItem(OrderLineItem):
return "{n} x {part} from {supplier} (for {po})".format( return "{n} x {part} from {supplier} (for {po})".format(
n=decimal2string(self.quantity), n=decimal2string(self.quantity),
part=self.part.SKU if self.part else 'unknown part', part=self.part.SKU if self.part else 'unknown part',
supplier=self.order.supplier.name, supplier=self.order.supplier.name if self.order.supplier else _('deleted'),
po=self.order) po=self.order)
order = models.ForeignKey( order = models.ForeignKey(

View File

@ -67,7 +67,7 @@
{% block thumbnail %} {% block thumbnail %}
<img class='part-thumb' <img class='part-thumb'
{% if order.supplier.image %} {% if order.supplier and order.supplier.image %}
src="{{ order.supplier.image.url }}" src="{{ order.supplier.image.url }}"
{% else %} {% else %}
src="{% static 'img/blank_image.png' %}" src="{% static 'img/blank_image.png' %}"
@ -110,7 +110,13 @@ src="{% static 'img/blank_image.png' %}"
<tr> <tr>
<td><span class='fas fa-building'></span></td> <td><span class='fas fa-building'></span></td>
<td>{% trans "Supplier" %}</td> <td>{% trans "Supplier" %}</td>
<td><a href="{% url 'company-detail' order.supplier.id %}">{{ order.supplier.name }}</a>{% include "clip.html"%}</td> <td>
{% if order.supplier %}
<a href="{% url 'company-detail' order.supplier.id %}">{{ order.supplier.name }}</a>{% include "clip.html"%}
{% else %}
<em>{% trans "No suppplier information available" %}</em>
{% endif %}
</td>
</tr> </tr>
{% if order.supplier_reference %} {% if order.supplier_reference %}
<tr> <tr>

View File

@ -170,10 +170,12 @@ $('#new-po-line').click(function() {
var fields = poLineItemFields({ var fields = poLineItemFields({
order: {{ order.pk }}, order: {{ order.pk }},
{% if order.supplier %}
supplier: {{ order.supplier.pk }}, supplier: {{ order.supplier.pk }},
{% if order.supplier.currency %} {% if order.supplier.currency %}
currency: '{{ order.supplier.currency }}', currency: '{{ order.supplier.currency }}',
{% endif %} {% endif %}
{% endif %}
}); });
constructForm('{% url "api-po-line-list" %}', { constructForm('{% url "api-po-line-list" %}', {
@ -210,7 +212,9 @@ $('#new-po-line').click(function() {
loadPurchaseOrderLineItemTable('#po-line-table', { loadPurchaseOrderLineItemTable('#po-line-table', {
order: {{ order.pk }}, order: {{ order.pk }},
{% if order.supplier %}
supplier: {{ order.supplier.pk }}, supplier: {{ order.supplier.pk }},
{% endif %}
{% if roles.purchase_order.change %} {% if roles.purchase_order.change %}
allow_edit: true, allow_edit: true,
{% else %} {% else %}

View File

@ -43,7 +43,7 @@
<p>{% blocktrans with count=part.manufacturer_parts.all|length %}There are {{count}} manufacturers defined for this part. If you delete this part, the following manufacturer parts will also be deleted:{% endblocktrans %} <p>{% blocktrans with count=part.manufacturer_parts.all|length %}There are {{count}} manufacturers defined for this part. If you delete this part, the following manufacturer parts will also be deleted:{% endblocktrans %}
<ul class='list-group'> <ul class='list-group'>
{% for spart in part.manufacturer_parts.all %} {% for spart in part.manufacturer_parts.all %}
<li class='list-group-item'>{{ spart.manufacturer.name }} - {{ spart.MPN }}</li> <li class='list-group-item'>{% if spart.manufacturer %}{{ spart.manufacturer.name }} - {% endif %}{{ spart.MPN }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
</p> </p>
@ -54,7 +54,9 @@
<p>{% blocktrans with count=part.supplier_parts.all|length %}There are {{count}} suppliers defined for this part. If you delete this part, the following supplier parts will also be deleted:{% endblocktrans %} <p>{% blocktrans with count=part.supplier_parts.all|length %}There are {{count}} suppliers defined for this part. If you delete this part, the following supplier parts will also be deleted:{% endblocktrans %}
<ul class='list-group'> <ul class='list-group'>
{% for spart in part.supplier_parts.all %} {% for spart in part.supplier_parts.all %}
{% if spart.supplier %}
<li class='list-group-item'>{{ spart.supplier.name }} - {{ spart.SKU }}</li> <li class='list-group-item'>{{ spart.supplier.name }} - {{ spart.SKU }}</li>
{% endif %}
{% endfor %} {% endfor %}
</ul> </ul>
</p> </p>

View File

@ -74,7 +74,7 @@ table td.expand {
<div class='header-right'> <div class='header-right'>
<h3>{% trans "Purchase Order" %} {{ prefix }}{{ reference }}</h3> <h3>{% trans "Purchase Order" %} {{ prefix }}{{ reference }}</h3>
{{ supplier.name }} {% if supplier %}{{ supplier.name }}{% endif %}{% else %}{% trans "Supplier was deleted" %}{% endif %}
</div> </div>
{% endblock %} {% endblock %}

View File

@ -379,7 +379,11 @@
<tr> <tr>
<td><span class='fas fa-building'></span></td> <td><span class='fas fa-building'></span></td>
<td>{% trans "Supplier" %}</td> <td>{% trans "Supplier" %}</td>
<td><a href="{% url 'company-detail' item.supplier_part.supplier.id %}">{{ item.supplier_part.supplier.name }}</a></td> <td>
{% if item.supplier_part.supplier %}
<a href="{% url 'company-detail' item.supplier_part.supplier.id %}">{{ item.supplier_part.supplier.name }}</a>
{% endif %}
</td>
</tr> </tr>
<tr> <tr>
<td><span class='fas fa-shapes'></span></td> <td><span class='fas fa-shapes'></span></td>