diff --git a/app/services/merge/merge_organisations_service.rb b/app/services/merge/merge_organisations_service.rb index a6a148b21..ee0adc9cc 100644 --- a/app/services/merge/merge_organisations_service.rb +++ b/app/services/merge/merge_organisations_service.rb @@ -88,21 +88,34 @@ private lettings_log.location = location_to_set if location_to_set.present? end lettings_log.owning_organisation = @absorbing_organisation - lettings_log.skip_dpo_validation = true - lettings_log.save! + lettings_log.managing_organisation = @absorbing_organisation if lettings_log.managing_organisation == merging_organisation + if lettings_log.collection_period_open? + lettings_log.skip_dpo_validation = true + lettings_log.save! + else + lettings_log.save!(validate: false) + end end merging_organisation.managed_lettings_logs.after_date(@merge_date.to_time).each do |lettings_log| lettings_log.managing_organisation = @absorbing_organisation - lettings_log.skip_dpo_validation = true - lettings_log.save! + if lettings_log.collection_period_open? + lettings_log.skip_dpo_validation = true + lettings_log.save! + else + lettings_log.save!(validate: false) + end end end def merge_sales_logs(merging_organisation) merging_organisation.sales_logs.after_date(@merge_date.to_time).each do |sales_log| sales_log.owning_organisation = @absorbing_organisation - sales_log.skip_dpo_validation = true - sales_log.save! + if sales_log.collection_period_open? + sales_log.skip_dpo_validation = true + sales_log.save! + else + sales_log.save!(validate: false) + end end end diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index 5f2cf5e83..a042856b1 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/spec/services/merge/merge_organisations_service_spec.rb @@ -199,7 +199,7 @@ RSpec.describe Merge::MergeOrganisationsService do end context "and merging sales logs" do - let!(:sales_log) { create(:sales_log, saledate: Time.zone.tomorrow, owning_organisation: merging_organisation) } + let!(:sales_log) { create(:sales_log, :completed, saledate: Time.zone.tomorrow, owning_organisation: merging_organisation) } before do create(:sales_log, saledate: Time.zone.yesterday, owning_organisation: merging_organisation) @@ -224,6 +224,20 @@ RSpec.describe Merge::MergeOrganisationsService do expect(absorbing_organisation.sales_logs.count).to eq(0) expect(sales_log.owning_organisation).to eq(merging_organisation) end + + context "with merge date in closed collection year" do + subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: Time.zone.local(2021, 3, 3)) } + + it "does not validate saledate for closed collection years" do + sales_log.saledate = Time.zone.local(2022, 5, 1) + sales_log.save!(validate: false) + merge_organisations_service.call + + absorbing_organisation.reload + sales_log.reload + expect(sales_log.owning_organisation).to eq(absorbing_organisation) + end + end end context "and merge date is provided" do @@ -267,7 +281,7 @@ RSpec.describe Merge::MergeOrganisationsService do context "and merging lettings logs" do let(:owning_organisation) { create(:organisation, holds_own_stock: true) } - let!(:owned_lettings_log) { create(:lettings_log, startdate: Time.zone.today, owning_organisation: merging_organisation, created_by: merging_organisation_user) } + let!(:owned_lettings_log) { create(:lettings_log, :completed, startdate: Time.zone.today, owning_organisation: merging_organisation, managing_organisation: merging_organisation, created_by: merging_organisation_user) } let!(:managed_lettings_log) { create(:lettings_log, startdate: Time.zone.today) } before do @@ -300,6 +314,32 @@ RSpec.describe Merge::MergeOrganisationsService do expect(owned_lettings_log.owning_organisation).to eq(merging_organisation) expect(managed_lettings_log.managing_organisation).to eq(merging_organisation) end + + it "does not clear any answers if the owning and managing organisation are the same" do + expect(owned_lettings_log.status).to eq("completed") + merge_organisations_service.call + + absorbing_organisation.reload + owned_lettings_log.reload + expect(owned_lettings_log.status).to eq("completed") + expect(owned_lettings_log.owning_organisation).to eq(absorbing_organisation) + expect(owned_lettings_log.managing_organisation).to eq(absorbing_organisation) + end + + context "with merge date in closed collection year" do + subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: Time.zone.local(2021, 3, 3)) } + + it "does not validate startdate for closed collection years" do + owned_lettings_log.startdate = Time.zone.local(2022, 4, 1) + owned_lettings_log.save!(validate: false) + merge_organisations_service.call + + absorbing_organisation.reload + owned_lettings_log.reload + expect(owned_lettings_log.owning_organisation).to eq(absorbing_organisation) + expect(owned_lettings_log.managing_organisation).to eq(absorbing_organisation) + end + end end context "and merging organisation schemes and locations" do