diff --git a/app/models/case_log.rb b/app/models/case_log.rb
index b9bbdc2ad..495b91e7a 100644
--- a/app/models/case_log.rb
+++ b/app/models/case_log.rb
@@ -85,6 +85,31 @@ class CaseLog < ApplicationRecord
end
end
+ def weekly_rent
+ return unless brent && period
+
+ case rent_period
+ when "Every 2 weeks"
+ brent / 2
+ when "Every 4 weeks"
+ brent / 4
+ when "Every calendar month"
+ brent * 12 / 52
+ when "Weekly for 50 weeks"
+ brent / 52 * 50
+ when "Weekly for 49 weeks"
+ brent / 52 * 49
+ when "Weekly for 48 weeks"
+ brent / 52 * 48
+ when "Weekly for 47 weeks"
+ brent / 52 * 47
+ when "Weekly for 46 weeks"
+ brent / 52 * 46
+ when "Weekly for 52 weeks"
+ brent
+ end
+ end
+
def applicable_income_range
return unless ecstat1
@@ -231,6 +256,31 @@ class CaseLog < ApplicationRecord
reason == 1
end
+ def rent_period
+ return unless period
+
+ case period
+ when 0
+ "Every 2 weeks"
+ when 1
+ "Every 4 weeks"
+ when 2
+ "Every calendar month"
+ when 3
+ "Weekly for 50 weeks"
+ when 4
+ "Weekly for 49 weeks"
+ when 5
+ "Weekly for 48 weeks"
+ when 6
+ "Weekly for 47 weeks"
+ when 7
+ "Weekly for 46 weeks"
+ when 8
+ "Weekly for 52 weeks"
+ end
+ end
+
private
PIO = Postcodes::IO.new
@@ -304,6 +354,7 @@ private
self.totchild = get_totchild
self.totelder = get_totelder
self.totadult = get_totadult
+ self.wrent = weekly_rent if brent.present? && period.present?
if %i[brent scharge pscharge supcharg].any? { |f| public_send(f).present? }
self.brent ||= 0
self.scharge ||= 0
diff --git a/db/migrate/20220311151611_add_derived_fields.rb b/db/migrate/20220311151611_add_derived_fields.rb
new file mode 100644
index 000000000..58e63c9b1
--- /dev/null
+++ b/db/migrate/20220311151611_add_derived_fields.rb
@@ -0,0 +1,12 @@
+class AddDerivedFields < ActiveRecord::Migration[7.0]
+ def change
+ change_table :case_logs, bulk: true do |t|
+ t.decimal :wrent, precision: 10, scale: 2
+ t.decimal :wscharge, precision: 10, scale: 2
+ t.decimal :wpschrge, precision: 10, scale: 2
+ t.decimal :wsupchrge, precision: 10, scale: 2
+ t.decimal :wtchrge, precision: 10, scale: 2
+ t.decimal :wtshortfall, precision: 10, scale: 2
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 3d485704b..fbeccb685 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -218,6 +218,12 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.integer "rent_type"
t.integer "has_benefits"
t.integer "renewal"
+ t.decimal "wrent", precision: 10, scale: 2
+ t.decimal "wscharge", precision: 10, scale: 2
+ t.decimal "wpschrge", precision: 10, scale: 2
+ t.decimal "wsupchrge", precision: 10, scale: 2
+ t.decimal "wtchrge", precision: 10, scale: 2
+ t.decimal "wtshortfall", 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 14e68a723..0d1b688be 100644
--- a/spec/fixtures/exports/case_logs.xml
+++ b/spec/fixtures/exports/case_logs.xml
@@ -160,5 +160,11 @@
1
1
0
+ 100.0
+
+
+
+
+
diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb
index 3b9d2e4a2..4e97ab30f 100644
--- a/spec/models/case_log_spec.rb
+++ b/spec/models/case_log_spec.rb
@@ -298,6 +298,87 @@ RSpec.describe CaseLog do
expect(record_from_db["lettype"]).to eq(1)
end
end
+
+ context "when rent is paid bi-weekly" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 100, period: 0)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(50.0)
+ expect(record_from_db["wrent"]).to eq(50.0)
+ end
+ end
+
+ context "when rent is paid every 4 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 120, period: 1)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(30.0)
+ expect(record_from_db["wrent"]).to eq(30.0)
+ end
+ end
+
+ context "when rent is paid every calendar month" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 2)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(30.0)
+ expect(record_from_db["wrent"]).to eq(30.0)
+ end
+ end
+
+ context "when rent is paid weekly for 50 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 3)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(125.0)
+ expect(record_from_db["wrent"]).to eq(125.0)
+ end
+ end
+
+ context "when rent is paid weekly for 49 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 4)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(122.5)
+ expect(record_from_db["wrent"]).to eq(122.5)
+ end
+ end
+
+ context "when rent is paid weekly for 48 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 5)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(120.0)
+ expect(record_from_db["wrent"]).to eq(120.0)
+ end
+ end
+
+ context "when rent is paid weekly for 47 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 6)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(117.5)
+ expect(record_from_db["wrent"]).to eq(117.5)
+ end
+ end
+
+ context "when rent is paid weekly for 46 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 7)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(115.0)
+ expect(record_from_db["wrent"]).to eq(115.0)
+ end
+ end
+
+ context "when rent is paid weekly for 52 weeks" do
+ it "correctly derives and saves weekly rent" do
+ case_log.update!(brent: 130, period: 8)
+ record_from_db = ActiveRecord::Base.connection.execute("select wrent from case_logs where id=#{case_log.id}").to_a[0]
+ expect(case_log.wrent).to eq(130.0)
+ expect(record_from_db["wrent"]).to eq(130.0)
+ end
+ end
end
context "when the owning organisation is an LA" do