Browse Source

Derived rent and charges variables (#379)

* Derive weekly rent

* Derive weekly charge fields

* Change method to hash constant

* Unify calculation so we can remove case statement
pull/384/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
9be4178855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      app/models/case_log.rb
  2. 12
      db/migrate/20220311151611_add_derived_fields.rb
  3. 8
      db/schema.rb
  4. 6
      spec/fixtures/exports/case_logs.xml
  5. 333
      spec/models/case_log_spec.rb

15
app/models/case_log.rb

@ -40,6 +40,7 @@ class CaseLog < ApplicationRecord
RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze
HAS_BENEFITS_OPTIONS = [0, 1, 2, 3].freeze HAS_BENEFITS_OPTIONS = [0, 1, 2, 3].freeze
STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze
NUM_OF_WEEKS_FROM_PERIOD = { 0 => 26, 1 => 13, 2 => 12, 3 => 50, 4 => 49, 5 => 48, 6 => 47, 7 => 46, 8 => 52 }.freeze
enum status: STATUS enum status: STATUS
def form def form
@ -85,6 +86,13 @@ class CaseLog < ApplicationRecord
end end
end end
def weekly_value(field_value)
num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period]
return unless field_value && num_of_weeks
field_value / 52 * num_of_weeks
end
def applicable_income_range def applicable_income_range
return unless ecstat1 return unless ecstat1
@ -311,6 +319,13 @@ private
self.supcharg ||= 0 self.supcharg ||= 0
self.tcharge = brent.to_f + scharge.to_f + pscharge.to_f + supcharg.to_f self.tcharge = brent.to_f + scharge.to_f + pscharge.to_f + supcharg.to_f
end end
if period.present?
self.wrent = weekly_value(brent) if brent.present?
self.wscharge = weekly_value(scharge) if scharge.present?
self.wpschrge = weekly_value(pscharge) if pscharge.present?
self.wsupchrg = weekly_value(supcharg) if supcharg.present?
self.wtcharge = weekly_value(tcharge) if tcharge.present?
end
self.has_benefits = get_has_benefits self.has_benefits = get_has_benefits
self.nocharge = household_charge&.zero? ? 1 : 0 self.nocharge = household_charge&.zero? ? 1 : 0
self.underoccupation_benefitcap = 3 if renewal == 1 && year == 2021 self.underoccupation_benefitcap = 3 if renewal == 1 && year == 2021

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 :wsupchrg, precision: 10, scale: 2
t.decimal :wtcharge, precision: 10, scale: 2
t.decimal :wtshortfall, precision: 10, scale: 2
end
end
end

8
db/schema.rb

@ -218,6 +218,12 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.integer "rent_type" t.integer "rent_type"
t.integer "has_benefits" t.integer "has_benefits"
t.integer "renewal" 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 "wsupchrg", precision: 10, scale: 2
t.decimal "wtcharge", 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 ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"
end end
@ -271,10 +277,10 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.string "last_sign_in_ip" t.string "last_sign_in_ip"
t.integer "role" t.integer "role"
t.string "old_user_id" t.string "old_user_id"
t.string "phone"
t.integer "failed_attempts", default: 0 t.integer "failed_attempts", default: 0
t.string "unlock_token" t.string "unlock_token"
t.datetime "locked_at", precision: nil t.datetime "locked_at", precision: nil
t.string "phone"
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["organisation_id"], name: "index_users_on_organisation_id" t.index ["organisation_id"], name: "index_users_on_organisation_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

6
spec/fixtures/exports/case_logs.xml vendored

@ -160,5 +160,11 @@
<rent_type>1</rent_type> <rent_type>1</rent_type>
<has_benefits>1</has_benefits> <has_benefits>1</has_benefits>
<renewal>0</renewal> <renewal>0</renewal>
<wrent>100.0</wrent>
<wscharge>25.0</wscharge>
<wpschrge>20.0</wpschrge>
<wsupchrg>17.5</wsupchrg>
<wtcharge>162.5</wtcharge>
<wtshortfall/>
</form> </form>
</forms> </forms>

333
spec/models/case_log_spec.rb

@ -298,6 +298,339 @@ RSpec.describe CaseLog do
expect(record_from_db["lettype"]).to eq(1) expect(record_from_db["lettype"]).to eq(1)
end end
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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 100, period: 0)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(50.0)
expect(record_from_db["wscharge"]).to eq(50.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 100, period: 0)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(50.0)
expect(record_from_db["wpschrge"]).to eq(50.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 100, period: 0)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(50.0)
expect(record_from_db["wsupchrg"]).to eq(50.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 100, period: 0)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(50.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 120, period: 1)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(30.0)
expect(record_from_db["wscharge"]).to eq(30.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 120, period: 1)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(30.0)
expect(record_from_db["wpschrge"]).to eq(30.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 120, period: 1)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(30.0)
expect(record_from_db["wsupchrg"]).to eq(30.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 120, period: 1)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(30.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 2)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(30.0)
expect(record_from_db["wscharge"]).to eq(30.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 2)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(30.0)
expect(record_from_db["wpschrge"]).to eq(30.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 2)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(30.0)
expect(record_from_db["wsupchrg"]).to eq(30.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 2)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(30.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 3)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(125.0)
expect(record_from_db["wscharge"]).to eq(125.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 3)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(125.0)
expect(record_from_db["wpschrge"]).to eq(125.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 3)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(125.0)
expect(record_from_db["wsupchrg"]).to eq(125.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 3)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(125.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 4)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(122.5)
expect(record_from_db["wscharge"]).to eq(122.5)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 4)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(122.5)
expect(record_from_db["wpschrge"]).to eq(122.5)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 4)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(122.5)
expect(record_from_db["wsupchrg"]).to eq(122.5)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 4)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(122.5)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 5)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(120.0)
expect(record_from_db["wscharge"]).to eq(120.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 5)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(120.0)
expect(record_from_db["wpschrge"]).to eq(120.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 5)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(120.0)
expect(record_from_db["wsupchrg"]).to eq(120.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 5)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(120.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 6)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(117.5)
expect(record_from_db["wscharge"]).to eq(117.5)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 6)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(117.5)
expect(record_from_db["wpschrge"]).to eq(117.5)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 6)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(117.5)
expect(record_from_db["wsupchrg"]).to eq(117.5)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 6)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(117.5)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 7)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(115.0)
expect(record_from_db["wscharge"]).to eq(115.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 7)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(115.0)
expect(record_from_db["wpschrge"]).to eq(115.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 7)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(115.0)
expect(record_from_db["wsupchrg"]).to eq(115.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 7)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(115.0)
expect(record_from_db["wtcharge"]).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
it "correctly derives and saves weekly service charge" do
case_log.update!(scharge: 130, period: 8)
record_from_db = ActiveRecord::Base.connection.execute("select wscharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wscharge).to eq(130.0)
expect(record_from_db["wscharge"]).to eq(130.0)
end
it "correctly derives and saves weekly personal service charge" do
case_log.update!(pscharge: 130, period: 8)
record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wpschrge).to eq(130.0)
expect(record_from_db["wpschrge"]).to eq(130.0)
end
it "correctly derives and saves weekly support charge" do
case_log.update!(supcharg: 130, period: 8)
record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wsupchrg).to eq(130.0)
expect(record_from_db["wsupchrg"]).to eq(130.0)
end
it "correctly derives and saves weekly total charge" do
case_log.update!(tcharge: 130, period: 8)
record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.wtcharge).to eq(130.0)
expect(record_from_db["wtcharge"]).to eq(130.0)
end
end
end end
context "when the owning organisation is an LA" do context "when the owning organisation is an LA" do

Loading…
Cancel
Save