diff --git a/src/backend/InvenTree/order/models.py b/src/backend/InvenTree/order/models.py index cf35229a47..1222d46a57 100644 --- a/src/backend/InvenTree/order/models.py +++ b/src/backend/InvenTree/order/models.py @@ -383,6 +383,13 @@ class Order( 'start_date': _('Start date must be before target date'), }) + # Check that the referenced 'address' matches the correct 'company' + if self.company and self.address: + if self.address.company != self.company: + raise ValidationError({ + 'address': _('Address does not match selected company') + }) + def clean_line_item(self, line): """Clean a line item for this order. diff --git a/src/backend/InvenTree/order/test_sales_order.py b/src/backend/InvenTree/order/test_sales_order.py index 7b70b3c1e7..789a9336e3 100644 --- a/src/backend/InvenTree/order/test_sales_order.py +++ b/src/backend/InvenTree/order/test_sales_order.py @@ -8,7 +8,7 @@ from django.core.exceptions import ValidationError import order.tasks from common.models import InvenTreeSetting, NotificationMessage -from company.models import Company +from company.models import Address, Company from InvenTree import status_codes as status from InvenTree.unit_test import InvenTreeTestCase, addUserPermission from order.models import ( @@ -26,7 +26,7 @@ from users.models import Owner class SalesOrderTest(InvenTreeTestCase): """Run tests to ensure that the SalesOrder model is working correctly.""" - fixtures = ['users'] + fixtures = ['company', 'users'] @classmethod def setUpTestData(cls): @@ -75,6 +75,38 @@ class SalesOrderTest(InvenTreeTestCase): quantity=1, order=cls.order, reference='Extra line' ) + def test_validate_address(self): + """Test validation of the linked Address.""" + order = SalesOrder.objects.first() + + # Create an address for a different company + company = Company.objects.exclude(pk=order.customer.pk).first() + self.assertIsNotNone(company) + address = Address.objects.create( + company=company, + primary=False, + line1='123 Different St', + line2='Elsewhere', + postal_code='99999', + country='US', + ) + + order.address = address + + with self.assertRaises(ValidationError) as err: + order.clean() + + self.assertIn('Address does not match selected company', str(err.exception)) + + # Update the address to match the correct company + address.company = order.customer + address.save() + + # Now validation should pass + order.address = address + order.clean() + order.save() + def test_so_reference(self): """Unit tests for sales order generation.""" # Test that a good reference is created when we have no existing orders diff --git a/src/backend/InvenTree/order/tests.py b/src/backend/InvenTree/order/tests.py index b055cf7e84..1712f68759 100644 --- a/src/backend/InvenTree/order/tests.py +++ b/src/backend/InvenTree/order/tests.py @@ -13,7 +13,8 @@ from djmoney.money import Money import common.models import order.tasks from common.settings import get_global_setting, set_global_setting -from company.models import Company, SupplierPart +from company.models import Company, Contact, SupplierPart +from InvenTree.helpers import current_date from InvenTree.unit_test import ( ExchangeRateMixin, PluginRegistryMixin, @@ -55,6 +56,49 @@ class OrderTest(ExchangeRateMixin, PluginRegistryMixin, TestCase): line = PurchaseOrderLineItem.objects.get(pk=1) self.assertEqual(str(line), '100 x ACME0001 - PO-0001 - ACME') + def test_validate_dates(self): + """Test for validation of date fields.""" + order = PurchaseOrder.objects.first() + + order.start_date = current_date() + order.target_date = current_date() - timedelta(days=5) + + with self.assertRaises(django_exceptions.ValidationError) as err: + order.clean() + + self.assertIn('Target date must be after start date', err.exception.messages) + self.assertIn('Start date must be before target date', err.exception.messages) + + order.target_date = current_date() + timedelta(days=5) + + # This should now pass + order.clean() + order.save() + + def test_validate_contact(self): + """Test for validation of linked Contact.""" + order = PurchaseOrder.objects.first() + + # Create a contact which does does not match the company + company = Company.objects.exclude(pk=order.supplier.pk).first() + self.assertIsNotNone(company) + contact = Contact.objects.create(company=company, name='Harold Henderson') + + order.contact = contact + + with self.assertRaises(django_exceptions.ValidationError) as err: + order.clean() + + self.assertIn('Contact does not match selected company', err.exception.messages) + + # Update the contact, point to the right company + contact.company = order.supplier + contact.save() + + order.contact = contact + order.clean() # Should not raise + order.save() + def test_rebuild_reference(self): """Test that the reference_int field is correctly updated when the model is saved.""" order = PurchaseOrder.objects.get(pk=1)