Browse Source

Derive weekly charge fields

pull/379/head
baarkerlounger 3 years ago
parent
commit
25d3dc58d9
  1. 34
      app/models/case_log.rb
  2. 4
      db/migrate/20220311151611_add_derived_fields.rb
  3. 6
      db/schema.rb
  4. 8
      spec/fixtures/exports/case_logs.xml
  5. 252
      spec/models/case_log_spec.rb

34
app/models/case_log.rb

@ -85,28 +85,28 @@ class CaseLog < ApplicationRecord
end end
end end
def weekly_rent def weekly_value(field_value)
return unless brent && period return unless field_value && period
case rent_period case period_label
when "Every 2 weeks" when "Every 2 weeks"
brent / 2 field_value / 2
when "Every 4 weeks" when "Every 4 weeks"
brent / 4 field_value / 4
when "Every calendar month" when "Every calendar month"
brent * 12 / 52 field_value * 12 / 52
when "Weekly for 50 weeks" when "Weekly for 50 weeks"
brent / 52 * 50 field_value / 52 * 50
when "Weekly for 49 weeks" when "Weekly for 49 weeks"
brent / 52 * 49 field_value / 52 * 49
when "Weekly for 48 weeks" when "Weekly for 48 weeks"
brent / 52 * 48 field_value / 52 * 48
when "Weekly for 47 weeks" when "Weekly for 47 weeks"
brent / 52 * 47 field_value / 52 * 47
when "Weekly for 46 weeks" when "Weekly for 46 weeks"
brent / 52 * 46 field_value / 52 * 46
when "Weekly for 52 weeks" when "Weekly for 52 weeks"
brent field_value
end end
end end
@ -256,7 +256,7 @@ class CaseLog < ApplicationRecord
reason == 1 reason == 1
end end
def rent_period def period_label
return unless period return unless period
case period case period
@ -354,7 +354,6 @@ private
self.totchild = get_totchild self.totchild = get_totchild
self.totelder = get_totelder self.totelder = get_totelder
self.totadult = get_totadult 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? } if %i[brent scharge pscharge supcharg].any? { |f| public_send(f).present? }
self.brent ||= 0 self.brent ||= 0
self.scharge ||= 0 self.scharge ||= 0
@ -362,6 +361,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

4
db/migrate/20220311151611_add_derived_fields.rb

@ -4,8 +4,8 @@ class AddDerivedFields < ActiveRecord::Migration[7.0]
t.decimal :wrent, precision: 10, scale: 2 t.decimal :wrent, precision: 10, scale: 2
t.decimal :wscharge, precision: 10, scale: 2 t.decimal :wscharge, precision: 10, scale: 2
t.decimal :wpschrge, precision: 10, scale: 2 t.decimal :wpschrge, precision: 10, scale: 2
t.decimal :wsupchrge, precision: 10, scale: 2 t.decimal :wsupchrg, precision: 10, scale: 2
t.decimal :wtchrge, precision: 10, scale: 2 t.decimal :wtcharge, precision: 10, scale: 2
t.decimal :wtshortfall, precision: 10, scale: 2 t.decimal :wtshortfall, precision: 10, scale: 2
end end
end end

6
db/schema.rb

@ -221,8 +221,8 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.decimal "wrent", precision: 10, scale: 2 t.decimal "wrent", precision: 10, scale: 2
t.decimal "wscharge", precision: 10, scale: 2 t.decimal "wscharge", precision: 10, scale: 2
t.decimal "wpschrge", precision: 10, scale: 2 t.decimal "wpschrge", precision: 10, scale: 2
t.decimal "wsupchrge", precision: 10, scale: 2 t.decimal "wsupchrg", precision: 10, scale: 2
t.decimal "wtchrge", precision: 10, scale: 2 t.decimal "wtcharge", precision: 10, scale: 2
t.decimal "wtshortfall", 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"
@ -277,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

8
spec/fixtures/exports/case_logs.xml vendored

@ -161,10 +161,10 @@
<has_benefits>1</has_benefits> <has_benefits>1</has_benefits>
<renewal>0</renewal> <renewal>0</renewal>
<wrent>100.0</wrent> <wrent>100.0</wrent>
<wscharge/> <wscharge>25.0</wscharge>
<wpschrge/> <wpschrge>20.0</wpschrge>
<wsupchrge/> <wsupchrg>17.5</wsupchrg>
<wtchrge/> <wtcharge>162.5</wtcharge>
<wtshortfall/> <wtshortfall/>
</form> </form>
</forms> </forms>

252
spec/models/case_log_spec.rb

@ -306,6 +306,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(50.0) expect(case_log.wrent).to eq(50.0)
expect(record_from_db["wrent"]).to eq(50.0) expect(record_from_db["wrent"]).to eq(50.0)
end 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 end
context "when rent is paid every 4 weeks" do context "when rent is paid every 4 weeks" do
@ -315,6 +343,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(30.0) expect(case_log.wrent).to eq(30.0)
expect(record_from_db["wrent"]).to eq(30.0) expect(record_from_db["wrent"]).to eq(30.0)
end 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 end
context "when rent is paid every calendar month" do context "when rent is paid every calendar month" do
@ -324,6 +380,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(30.0) expect(case_log.wrent).to eq(30.0)
expect(record_from_db["wrent"]).to eq(30.0) expect(record_from_db["wrent"]).to eq(30.0)
end 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 end
context "when rent is paid weekly for 50 weeks" do context "when rent is paid weekly for 50 weeks" do
@ -333,6 +417,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(125.0) expect(case_log.wrent).to eq(125.0)
expect(record_from_db["wrent"]).to eq(125.0) expect(record_from_db["wrent"]).to eq(125.0)
end 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 end
context "when rent is paid weekly for 49 weeks" do context "when rent is paid weekly for 49 weeks" do
@ -342,6 +454,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(122.5) expect(case_log.wrent).to eq(122.5)
expect(record_from_db["wrent"]).to eq(122.5) expect(record_from_db["wrent"]).to eq(122.5)
end 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 end
context "when rent is paid weekly for 48 weeks" do context "when rent is paid weekly for 48 weeks" do
@ -351,6 +491,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(120.0) expect(case_log.wrent).to eq(120.0)
expect(record_from_db["wrent"]).to eq(120.0) expect(record_from_db["wrent"]).to eq(120.0)
end 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 end
context "when rent is paid weekly for 47 weeks" do context "when rent is paid weekly for 47 weeks" do
@ -360,6 +528,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(117.5) expect(case_log.wrent).to eq(117.5)
expect(record_from_db["wrent"]).to eq(117.5) expect(record_from_db["wrent"]).to eq(117.5)
end 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 end
context "when rent is paid weekly for 46 weeks" do context "when rent is paid weekly for 46 weeks" do
@ -369,6 +565,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(115.0) expect(case_log.wrent).to eq(115.0)
expect(record_from_db["wrent"]).to eq(115.0) expect(record_from_db["wrent"]).to eq(115.0)
end 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 end
context "when rent is paid weekly for 52 weeks" do context "when rent is paid weekly for 52 weeks" do
@ -378,6 +602,34 @@ RSpec.describe CaseLog do
expect(case_log.wrent).to eq(130.0) expect(case_log.wrent).to eq(130.0)
expect(record_from_db["wrent"]).to eq(130.0) expect(record_from_db["wrent"]).to eq(130.0)
end 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 end

Loading…
Cancel
Save