From 1ffe8a4b8b138d38dc921be6ef81f731c6249c01 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 15 Aug 2023 11:32:53 +0100 Subject: [PATCH] CLDC-2109 Add merged org validations to sales log (#1826) * Add merged orgs sale dat validations * Add organisation validation to sales log * Adjust dates * Update error message --- .../validations/sales/setup_validations.rb | 42 ++++++++ config/locales/en.yml | 4 + .../sales/setup_validations_spec.rb | 96 +++++++++++++++++++ 3 files changed, 142 insertions(+) diff --git a/app/models/validations/sales/setup_validations.rb b/app/models/validations/sales/setup_validations.rb index 661b5b058..bb99f02ed 100644 --- a/app/models/validations/sales/setup_validations.rb +++ b/app/models/validations/sales/setup_validations.rb @@ -24,6 +24,40 @@ module Validations::Sales::SetupValidations end end + def validate_merged_organisations_saledate(record) + return unless record.saledate && date_valid?("saledate", record) + + if merged_owning_organisation_inactive?(record) + record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_merged_organisations_saledate", + owning_organisation: record.owning_organisation.name, + owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date), + owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name) + end + + if absorbing_owning_organisation_inactive?(record) + record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_absorbing_organisations_saledate", + owning_organisation: record.owning_organisation.name, + owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) + end + end + + def validate_organisation(record) + return unless record.saledate && record.owning_organisation + + if record.owning_organisation.present? + if record.owning_organisation&.merge_date.present? && record.owning_organisation.merge_date <= record.saledate + record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_merged_organisation_sales", + owning_organisation: record.owning_organisation.name, + owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date), + owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name) + elsif record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.saledate.to_date + record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation_sales", + owning_organisation: record.owning_organisation.name, + owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) + end + end + end + private def active_collection_start_date @@ -64,4 +98,12 @@ private ) end end + + def merged_owning_organisation_inactive?(record) + record.owning_organisation&.merge_date.present? && record.owning_organisation.merge_date <= record.saledate + end + + def absorbing_owning_organisation_inactive?(record) + record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.saledate.to_date + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index f99d08c33..7ad173de8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -239,6 +239,8 @@ en: previous_and_current_collection_year: "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} collection years, which is between %{previous_start_year_long} and %{current_end_year_long}" year_not_two_digits: "Sale completion year must be 2 digits" + invalid_merged_organisations_saledate: "Enter a date when the owning organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." + invalid_absorbing_organisations_saledate: "Enter a date when the owning organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}." type: percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type" @@ -288,6 +290,8 @@ en: data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation." inactive_merged_organisation: "The owning organisation must be active on the tenancy start date. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." inactive_absorbing_organisation: "The owning organisation must be active on the tenancy start date. %{owning_organisation} became active on %{owning_organisation_available_from}." + inactive_merged_organisation_sales: "The owning organisation must be active on the sale completion date. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." + inactive_absorbing_organisation_sales: "The owning organisation must be active on the sale completion date. %{owning_organisation} became active on %{owning_organisation_available_from}." managing_organisation: invalid: "Please select the owning organisation or managing organisation that you belong to" data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the managing organisation." diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index ecbb7e0d3..f7f01eaf7 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -185,4 +185,100 @@ RSpec.describe Validations::Sales::SetupValidations do end end end + + describe "#validate_merged_organisations_saledate" do + let(:record) { build(:sales_log) } + let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") } + let(:merged_organisation) { create(:organisation, name: "Merged org") } + + around do |example| + Timecop.freeze(Time.zone.local(2023, 5, 1)) + example.run + Timecop.return + end + + before do + merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) + end + + context "and owning organisation is no longer active" do + it "does not allow saledate after organisation has been merged" do + record.saledate = Time.zone.local(2023, 3, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_merged_organisations_saledate(record) + expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.") + end + + it "allows saledate before organisation has been merged" do + record.saledate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_merged_organisations_saledate(record) + expect(record.errors["saledate"]).to be_empty + end + end + + context "and owning organisation is not yet active during the saledate" do + it "does not allow saledate before absorbing organisation has been created" do + record.saledate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_merged_organisations_saledate(record) + expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Absorbing org became active on 1 February 2023.") + end + + it "allows saledate after absorbing organisation has been created" do + record.saledate = Time.zone.local(2023, 2, 2) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_merged_organisations_saledate(record) + expect(record.errors["saledate"]).to be_empty + end + end + end + + describe "#validate_organisation" do + let(:record) { build(:sales_log) } + + context "when organisations are merged" do + let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") } + let(:merged_organisation) { create(:organisation, name: "Merged org") } + + around do |example| + Timecop.freeze(Time.zone.local(2023, 5, 1)) + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) + example.run + Timecop.return + end + + context "and owning organisation is no longer active" do + it "does not allow organisation that has been merged" do + record.saledate = Time.zone.local(2023, 3, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the sale completion date. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.") + end + + it "allows organisation before it has been merged" do + record.saledate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to be_empty + end + end + + context "and owning organisation is not yet active during the saledate" do + it "does not allow absorbing organisation before it had been created" do + record.saledate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the sale completion date. Absorbing org became active on 1 February 2023.") + end + + it "allows absorbing organisation after it has been created" do + record.saledate = Time.zone.local(2023, 2, 2) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to be_empty + end + end + end + end end