From aa6ce31c0ea896d1b33af9fdb8f95453bff62413 Mon Sep 17 00:00:00 2001 From: Matthew McKenzie Date: Tue, 17 Dec 2024 10:12:16 +0000 Subject: [PATCH] Remove some QA5 and QA6 checks for update form --- quotas/forms/definitions.py | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/quotas/forms/definitions.py b/quotas/forms/definitions.py index 996fb46c1..66a0f552e 100644 --- a/quotas/forms/definitions.py +++ b/quotas/forms/definitions.py @@ -554,6 +554,68 @@ def init_fields(self): self.fields["volume"].disabled = True self.fields["measurement_unit"].disabled = True + def clean(self): + cleaned_data = ValidityPeriodForm.clean(self) + """ + Carrying out business rule checks here to prevent erroneous + associations, see: + + https://uktrade.github.io/tariff-data-manual/documentation/data-structures/quota-associations.html#validation-rules + + This does not include QA5 and QA6 checks which compare data against other sub-quotas so as not to block users from editing sub-quotas one by one. + Any errors here will be caught in the business rule check. + """ + original_definition = self.original_definition + if cleaned_data["valid_between"].upper is None: + raise ValidationError("An end date must be supplied") + + if not business_rules.check_QA2_dict( + sub_definition_valid_between=cleaned_data["valid_between"], + main_definition_valid_between=original_definition.valid_between, + ): + raise ValidationError( + "QA2: Validity period for sub-quota must be within the " + "validity period of the main quota", + ) + + if not business_rules.check_QA3_dict( + main_definition_unit=self.original_definition.measurement_unit, + sub_definition_unit=cleaned_data["measurement_unit"], + main_definition_volume=original_definition.volume, + sub_definition_volume=cleaned_data["volume"], + main_initial_volume=original_definition.initial_volume, + sub_initial_volume=cleaned_data["initial_volume"], + ): + raise ValidationError( + "QA3: When converted to the measurement unit of the main " + "quota, the volume of a sub-quota must always be lower than " + "or equal to the volume of the main quota", + ) + + if not business_rules.check_QA4_dict(cleaned_data["coefficient"]): + raise ValidationError( + "QA4: A coefficient must be a positive decimal number", + ) + + if cleaned_data["relationship_type"] == "NM": + if not business_rules.check_QA5_normal_coefficient( + cleaned_data["coefficient"], + ): + raise ValidationError( + "QA5: Where the relationship type is Normal, the " + "coefficient value must be 1", + ) + elif cleaned_data["relationship_type"] == "EQ": + if not business_rules.check_QA5_equivalent_coefficient( + cleaned_data["coefficient"], + ): + raise ValidationError( + "QA5: Where the relationship type is Equivalent, the " + "coefficient value must be something other than 1", + ) + + return cleaned_data + class QuotaAssociationUpdateForm(forms.ModelForm): class Meta: