diff --git a/app/models/case_log.rb b/app/models/case_log.rb
index d8b726842..4ed8ff077 100644
--- a/app/models/case_log.rb
+++ b/app/models/case_log.rb
@@ -368,6 +368,9 @@ private
self.wpschrge = weekly_value(pscharge) if pscharge.present?
self.wsupchrg = weekly_value(supcharg) if supcharg.present?
self.wtcharge = weekly_value(tcharge) if tcharge.present?
+ if is_supported_housing? && chcharge.present?
+ self.wchchrg = weekly_value(chcharge)
+ end
end
self.has_benefits = get_has_benefits
self.wtshortfall = if tshortfall && receives_housing_related_benefits?
diff --git a/db/migrate/20220322151056_add_wchchrg_derived_variable_field.rb b/db/migrate/20220322151056_add_wchchrg_derived_variable_field.rb
new file mode 100644
index 000000000..4725f09bd
--- /dev/null
+++ b/db/migrate/20220322151056_add_wchchrg_derived_variable_field.rb
@@ -0,0 +1,7 @@
+class AddWchchrgDerivedVariableField < ActiveRecord::Migration[7.0]
+ def change
+ change_table :case_logs, bulk: true do |t|
+ t.decimal :wchchrg, precision: 10, scale: 2
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e28e022c4..13a684bd2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -228,6 +228,7 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.integer "vday"
t.integer "vmonth"
t.integer "vyear"
+ t.decimal "wchchrg", precision: 10, scale: 2
t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"
end
diff --git a/spec/fixtures/exports/case_logs.xml b/spec/fixtures/exports/case_logs.xml
index 73c9fb3ce..49efe81b5 100644
--- a/spec/fixtures/exports/case_logs.xml
+++ b/spec/fixtures/exports/case_logs.xml
@@ -170,5 +170,6 @@
3
11
2019
+
diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb
index 15d0ef132..cb31c44d9 100644
--- a/spec/models/case_log_spec.rb
+++ b/spec/models/case_log_spec.rb
@@ -1404,6 +1404,160 @@ RSpec.describe CaseLog do
expect(record_from_db["housingneeds"]).to eq(3)
end
end
+
+ context "when it is supported housing and a care home charge has been supplied" do
+ let!(:case_log) do
+ described_class.create({
+ managing_organisation: organisation,
+ owning_organisation: organisation,
+ needstype: 0,
+ })
+ end
+
+ context "when the care home charge is paid bi-weekly" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 100, period: 2)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(50.0)
+ expect(record_from_db["wchchrg"]).to eq(50.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 2)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(50.06)
+ expect(record_from_db["wchchrg"]).to eq(50.06)
+ end
+ end
+
+ context "when the care home charge is paid every 4 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 120, period: 3)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(30.0)
+ expect(record_from_db["wchchrg"]).to eq(30.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 3)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(25.03)
+ expect(record_from_db["wchchrg"]).to eq(25.03)
+ end
+ end
+
+ context "when the care home charge is paid every calendar month" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 4)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(30.0)
+ expect(record_from_db["wchchrg"]).to eq(30.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 4)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(23.10)
+ expect(record_from_db["wchchrg"]).to eq(23.10)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 50 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 5)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(125.0)
+ expect(record_from_db["wchchrg"]).to eq(125.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 5)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(96.27)
+ expect(record_from_db["wchchrg"]).to eq(96.27)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 49 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 6)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(122.5)
+ expect(record_from_db["wchchrg"]).to eq(122.5)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 6)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(94.34)
+ expect(record_from_db["wchchrg"]).to eq(94.34)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 48 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 7)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(120.0)
+ expect(record_from_db["wchchrg"]).to eq(120.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 7)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(92.42)
+ expect(record_from_db["wchchrg"]).to eq(92.42)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 47 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 8)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(117.5)
+ expect(record_from_db["wchchrg"]).to eq(117.5)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 8)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(90.49)
+ expect(record_from_db["wchchrg"]).to eq(90.49)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 46 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 9)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(115.0)
+ expect(record_from_db["wchchrg"]).to eq(115.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 9)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(88.57)
+ expect(record_from_db["wchchrg"]).to eq(88.57)
+ end
+ end
+
+ context "when the care home charge is paid weekly for 52 weeks" do
+ it "correctly derives and saves weekly care home charge" do
+ case_log.update!(chcharge: 130, period: 1)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(130.0)
+ expect(record_from_db["wchchrg"]).to eq(130.0)
+ end
+
+ it "correctly derives floats" do
+ case_log.update!(chcharge: 100.12, period: 1)
+ record_from_db = ActiveRecord::Base.connection.execute("select wchchrg from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wchchrg).to eq(100.12)
+ expect(record_from_db["wchchrg"]).to eq(100.12)
+ end
+ end
+ end
end
describe "resetting invalidated fields" do