Skip to content

Commit

Permalink
TP2000-1619-Sub-quota-create-volume-bug (#1361)
Browse files Browse the repository at this point in the history
* Test solution

* Add current

* re-adding old business rule check

* rule check

* Distinct count error

* Remove some QA5 and QA6 checks for update form

* Update models.py

Remove comment as not confident enough to make this comment to all

* Rid initial volume none scenario

---------

Co-authored-by: Charles Prichard <[email protected]>
  • Loading branch information
mattjamc and CPrich905 authored Dec 17, 2024
1 parent fa48323 commit 26e153d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
24 changes: 16 additions & 8 deletions quotas/business_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,13 @@ def validate(self, association):
"coefficient not equal to 1"
),
)
if not check_QA5_equivalent_volumes(association.main_quota):
if (
association.main_quota.sub_quotas.values("volume")
.order_by("volume")
.distinct("volume")
.count()
> 1
):
raise self.violation(
model=association,
message=(
Expand All @@ -496,14 +502,16 @@ def check_QA5_equivalent_coefficient(coefficient):
return coefficient != Decimal("1.000")


def check_QA5_equivalent_volumes(original_definition, volume=None):
return (
original_definition.sub_quotas.values("volume")
.order_by("volume")
.distinct("volume")
.count()
<= 1
def check_QA5_equivalent_volumes(original_definition, initial_volume):
existing_initial_volumes = original_definition.sub_quotas.current().values(
"initial_volume",
)
if existing_initial_volumes.distinct().count() == 1:
return existing_initial_volumes[0]["initial_volume"] == initial_volume
elif existing_initial_volumes.distinct().count() < 1:
return True

return False


def check_QA5_normal_coefficient(coefficient):
Expand Down
66 changes: 64 additions & 2 deletions quotas/forms/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ def clean(self):
)
if not business_rules.check_QA5_equivalent_volumes(
self.original_definition,
volume=cleaned_data["volume"],
initial_volume=cleaned_data["initial_volume"],
):
raise ValidationError(
"Whenever a sub-quota is defined with the 'equivalent' "
"type, it must have the same volume as the ones associated"
"type, it must have the same volume as other sub-quotas associated"
" with the parent quota",
)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 26e153d

Please sign in to comment.