From c6d06beb5ced9afb21e7681674890f8415b4e37b Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 27 Feb 2024 11:17:20 +0000 Subject: [PATCH] Check soft rent validation for closed collection --- .../update_locations_from_csv_service.rb | 38 +++++++++--- ...ate_schemes_and_locations_from_csv_spec.rb | 62 +++++++++++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb b/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb index 5d01578d1..1cb1bef63 100644 --- a/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb +++ b/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb @@ -144,16 +144,36 @@ private def clear_invalid_rent_fields(logs) logs.each do |log| - log.validate - if log.errors["brent"].any? - Rails.logger.info("Log #{log.id} went from completed to in progress.") if log.status == "completed" - log.brent = nil - log.scharge = nil - log.pscharge = nil - log.supcharg = nil + if log.rent_in_soft_min_range? || log.rent_in_soft_max_range? + complete_or_log_soft_invalidated_log(log) + else + log.validate + if log.errors["brent"].any? + Rails.logger.info("Log #{log.id} went from completed to in progress.") if log.status == "completed" + log.brent = nil + log.scharge = nil + log.pscharge = nil + log.supcharg = nil + end + log.values_updated_at = Time.zone.now + log.save!(validate: false) end - log.values_updated_at = Time.zone.now - log.save!(validate: false) end end + + def complete_or_log_soft_invalidated_log(log) + return if log.rent_value_check.present? + + editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date + if log.startdate < editable_from_date + log.rent_value_check = 0 + Rails.logger.info("Confirmed rent value check for log #{log.id}.") + elsif log.status == "completed" + Rails.logger.info("Log #{log.id} went from completed to in progress.") + else + Rails.logger.info("Log #{log.id} stayed in progress, triggering soft rent value check.") + end + log.values_updated_at = Time.zone.now + log.save!(validate: false) + end end diff --git a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb index 075283dd5..76e9b4cb2 100644 --- a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb +++ b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb @@ -571,11 +571,73 @@ RSpec.describe "bulk_update" do period: 1) end + before do + allow(storage_service).to receive(:get_file_io) + .with("updated_locations.csv") + .and_return(StringIO.new(replace_entity_ids_for_locations(locations[0], locations[1], locations[2], scheme, scheme, { id: "non existent scheme id" }, File.open("./spec/fixtures/files/updated_locations.csv").read))) + end + it "does not clear the charges values and marks the log in progress" do expect(lettings_log.status).to eq("completed") task.invoke(original_locations_csv_path, updated_locations_csv_path) lettings_log.reload expect(lettings_log.status).to eq("in_progress") + expect(lettings_log.rent_value_check).to eq(nil) + expect(lettings_log.brent).to eq(100) + expect(lettings_log.scharge).to eq(50) + expect(lettings_log.pscharge).to eq(50) + expect(lettings_log.supcharg).to eq(50) + end + end + + context "when new LA triggers soft rent ranges validations for closed collection period" do + let!(:lettings_log) do + FactoryBot.create(:lettings_log, + :completed, + :sh, + location: locations[0], + scheme:, + values_updated_at: nil, + owning_organisation: scheme.owning_organisation, + brent: 100, + scharge: 50, + pscharge: 50, + supcharg: 50, + beds: 4, + lettype: 1, + voiddate: Time.zone.local(2020, 4, 1), + mrcdate: Time.zone.local(2020, 4, 1), + period: 1) + end + + before do + LaRentRange.create!( + ranges_rent_id: "1", + la: "E09000033", + beds: 0, + lettype: 8, + soft_min: 12.41, + soft_max: 89.54, + hard_min: 9.87, + hard_max: 100.99, + start_year: 2022, + ) + + allow(storage_service).to receive(:get_file_io) + .with("updated_locations.csv") + .and_return(StringIO.new(replace_entity_ids_for_locations(locations[0], locations[1], locations[2], scheme, scheme, { id: "non existent scheme id" }, File.open("./spec/fixtures/files/updated_locations.csv").read))) + + lettings_log.startdate = Time.zone.local(2022, 4, 1) + lettings_log.owning_organisation = scheme.owning_organisation + lettings_log.save!(validate: false) + end + + it "does not clear the charges values and confirms the rent value check" do + expect(lettings_log.status).to eq("completed") + task.invoke(original_locations_csv_path, updated_locations_csv_path) + lettings_log.reload + expect(lettings_log.status).to eq("completed") + expect(lettings_log.rent_value_check).to eq(0) expect(lettings_log.brent).to eq(100) expect(lettings_log.scharge).to eq(50) expect(lettings_log.pscharge).to eq(50)