From fb9a78a608f0b6ae01e25db1be0c49bda41f9027 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 27 Jan 2022 09:42:12 +0000 Subject: [PATCH] Correctly add and display calculated total charge (#250) * correctly add rent and charges * Display correct calculated total charge --- app/models/case_log.rb | 2 +- .../controllers/numeric_question_controller.js | 2 +- spec/features/form/progressive_total_field_spec.rb | 4 ++-- spec/features/form/saving_data_spec.rb | 14 +++++++------- spec/models/case_log_spec.rb | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 898dc44af..e68190e10 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -263,7 +263,7 @@ private self.totchild = get_totchild self.totelder = get_totelder self.totadult = get_totadult - self.tcharge = brent.to_i + scharge.to_i + pscharge.to_i + supcharg.to_i + self.tcharge = brent.to_f + scharge.to_f + pscharge.to_f + supcharg.to_f self.has_benefits = get_has_benefits self.nocharge = household_charge == "Yes" ? "No" : "Yes" end diff --git a/app/webpacker/controllers/numeric_question_controller.js b/app/webpacker/controllers/numeric_question_controller.js index 9bbdf6f9e..ae2830750 100644 --- a/app/webpacker/controllers/numeric_question_controller.js +++ b/app/webpacker/controllers/numeric_question_controller.js @@ -12,7 +12,7 @@ export default class extends Controller { const affectedField = this.element.dataset.target; const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `case-log-${x.replaceAll("_","-")}-field`); const valuesToAdd = fieldsToAdd.map(x => document.getElementById(x).value).filter(x => x); - const newValue = valuesToAdd.map(x => parseInt(x)).reduce((a, b) => a + b, 0); + const newValue = valuesToAdd.map(x => parseFloat(x)).reduce((a, b) => a + b, 0).toFixed(2); const elementToUpdate = document.getElementById(affectedField); elementToUpdate.value = newValue; } diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index ba5131b9f..4d30bef38 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -28,8 +28,8 @@ RSpec.describe "Accessible Automcomplete" do visit("/logs/#{case_log.id}/rent") expect(page).to have_selector("#tcharge_div", visible: true) fill_in("case-log-brent-field", with: 5) - expect(find("#case-log-tcharge-field").value).to eq("5") + expect(find("#case-log-tcharge-field").value).to eq("5.00") fill_in("case-log-pscharge-field", with: 3) - expect(find("#case-log-tcharge-field").value).to eq("8") + expect(find("#case-log-tcharge-field").value).to eq("8.00") end end diff --git a/spec/features/form/saving_data_spec.rb b/spec/features/form/saving_data_spec.rb index 4dbff2ebb..2c27dfda3 100644 --- a/spec/features/form/saving_data_spec.rb +++ b/spec/features/form/saving_data_spec.rb @@ -60,17 +60,17 @@ RSpec.describe "Form Saving Data" do it "updates total value of the rent", js: true do visit("/logs/#{id}/rent") - fill_in("case-log-brent-field", with: 3) - expect(page).to have_field("case-log-tcharge-field", with: "3") + fill_in("case-log-brent-field", with: 3.02) + expect(page).to have_field("case-log-tcharge-field", with: "3.02") - fill_in("case-log-scharge-field", with: 2) - expect(page).to have_field("case-log-tcharge-field", with: "5") + fill_in("case-log-scharge-field", with: 2.8) + expect(page).to have_field("case-log-tcharge-field", with: "5.82") fill_in("case-log-pscharge-field", with: 1) - expect(page).to have_field("case-log-tcharge-field", with: "6") + expect(page).to have_field("case-log-tcharge-field", with: "6.82") - fill_in("case-log-supcharg-field", with: 4) - expect(page).to have_field("case-log-tcharge-field", with: "10") + fill_in("case-log-supcharg-field", with: 4.11) + expect(page).to have_field("case-log-tcharge-field", with: "10.93") end it "displays number answers in inputs if they are already saved" do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 2a0b5e43a..bd5ad8c23 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1138,16 +1138,16 @@ RSpec.describe Form, type: :model do CaseLog.create({ managing_organisation: organisation, owning_organisation: organisation, - brent: 5, - scharge: 10, + brent: 5.77, + scharge: 10.01, pscharge: 3, - supcharg: 12, + supcharg: 12.2, }) end it "correctly sums rental charges" do record_from_db = ActiveRecord::Base.connection.execute("select tcharge from case_logs where id=#{case_log.id}").to_a[0] - expect(record_from_db["tcharge"]).to eq(30) + expect(record_from_db["tcharge"]).to eq(30.98) end end