From 0c0ce55e2ecb2539a76928f57c61fa88b63362ff Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 13 Sep 2023 11:43:33 +0100 Subject: [PATCH] Add supcharg_over_soft_max method --- app/models/validations/soft_validations.rb | 12 + .../validations/soft_validations_spec.rb | 214 ++++++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index 32b8a1926..5720c0f0d 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -121,6 +121,18 @@ module Validations::SoftValidations weekly_value(pscharge) > max end + def supcharg_over_soft_max? + return unless supcharg && period && needstype + return if weekly_value(supcharg).blank? + + max = if needstype == 1 + owning_organisation.provider_type == "LA" ? 25 : 35 + else + owning_organisation.provider_type == "LA" ? 75 : 85 + end + weekly_value(supcharg) > max + end + private def details_known_or_lead_tenant?(tenant_number) diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 0a2b6a4dd..b001d5d4d 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -803,4 +803,218 @@ RSpec.describe Validations::SoftValidations do end end end + + describe "supcharg_over_soft_max?" do + context "and organisation is PRP" do + before do + record.owning_organisation.update(provider_type: "PRP") + end + + it "returns false if supcharg is not given" do + record.supcharg = nil + record.needstype = 1 + record.period = 1 + + expect(record).not_to be_supcharg_over_soft_max + end + + it "returns false if period is not given" do + record.supcharg = 201 + record.needstype = 1 + record.period = nil + + expect(record).not_to be_supcharg_over_soft_max + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 34, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 100, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 69, + }].each do |test_case| + it "returns false if supcharg is under soft max for general needs #{test_case[:period][:label]}(35)" do + record.supcharg = test_case[:supcharg] + record.needstype = 1 + record.period = test_case[:period][:value] + + expect(record).not_to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 84, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 320, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 169, + }].each do |test_case| + it "returns false if supcharg is under soft max for supported housing #{test_case[:period][:label]} (85)" do + record.supcharg = test_case[:supcharg] + record.needstype = 2 + record.period = test_case[:period][:value] + + expect(record).not_to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 36, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 180, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 71, + }].each do |test_case| + it "returns true if supcharg is over soft max for general needs #{test_case[:period][:label]} (35)" do + record.supcharg = test_case[:supcharg] + record.needstype = 1 + record.period = test_case[:period][:value] + + expect(record).to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 86, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 400, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 171, + }].each do |test_case| + it "returns true if supcharg is over soft max for supported housing #{test_case[:period][:label]} (85)" do + record.supcharg = test_case[:supcharg] + record.needstype = 2 + record.period = test_case[:period][:value] + + expect(record).to be_supcharg_over_soft_max + end + end + end + + context "and organisation is LA" do + before do + record.owning_organisation.update(provider_type: "LA") + end + + it "returns false if supcharg is not given" do + record.supcharg = nil + record.needstype = 1 + record.period = 1 + + expect(record).not_to be_supcharg_over_soft_max + end + + it "returns false if period is not given" do + record.supcharg = 201 + record.needstype = 1 + record.period = nil + + expect(record).not_to be_supcharg_over_soft_max + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 24, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 88, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 49, + }].each do |test_case| + it "returns false if supcharg is under soft max for general needs #{test_case[:period][:label]}(25)" do + record.supcharg = test_case[:supcharg] + record.needstype = 1 + record.period = test_case[:period][:value] + + expect(record).not_to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 74, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 250, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 149, + }].each do |test_case| + it "returns false if supcharg is under soft max for supported housing #{test_case[:period][:label]} (75)" do + record.supcharg = test_case[:supcharg] + record.needstype = 2 + record.period = test_case[:period][:value] + + expect(record).not_to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 26, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 120, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 51, + }].each do |test_case| + it "returns true if supcharg is over soft max for general needs #{test_case[:period][:label]} (25)" do + record.supcharg = test_case[:supcharg] + record.needstype = 1 + record.period = test_case[:period][:value] + + expect(record).to be_supcharg_over_soft_max + end + end + + [{ + period: { label: "weekly", value: 1 }, + supcharg: 76, + }, + { + period: { label: "monthly", value: 4 }, + supcharg: 350, + }, + { + period: { label: "every 2 weeks", value: 2 }, + supcharg: 151, + }].each do |test_case| + it "returns true if supcharg is over soft max for supported housing #{test_case[:period][:label]} (75)" do + record.supcharg = test_case[:supcharg] + record.needstype = 2 + record.period = test_case[:period][:value] + + expect(record).to be_supcharg_over_soft_max + end + end + end + end end