Browse Source

Derive weekly rent

pull/379/head
baarkerlounger 3 years ago
parent
commit
52c8f4bbe4
  1. 51
      app/models/case_log.rb
  2. 12
      db/migrate/20220311151611_add_derived_fields.rb
  3. 6
      db/schema.rb
  4. 6
      spec/fixtures/exports/case_logs.xml
  5. 81
      spec/models/case_log_spec.rb

51
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

12
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

6
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

6
spec/fixtures/exports/case_logs.xml vendored

@ -160,5 +160,11 @@
<rent_type>1</rent_type>
<has_benefits>1</has_benefits>
<renewal>0</renewal>
<wrent>100.0</wrent>
<wscharge/>
<wpschrge/>
<wsupchrge/>
<wtchrge/>
<wtshortfall/>
</form>
</forms>

81
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

Loading…
Cancel
Save