diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 24ba9ce5c..185658eef 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -18,11 +18,15 @@ module DerivedVariables::SalesLogVariables self.hoyear = hodate.year end - if outright_sale? && mortgage_not_used? - self.deposit = value - elsif outright_sale? && mortgageused_changed?(from: 2, to: 1) - # Clear when switching mortgage used from no to yes - self.deposit = nil + if outright_sale? + if mortgage_not_used? + self.deposit = value + elsif mortgage_use_unknown? + self.deposit = nil + elsif mortgageused_changed?(from: 2, to: 1) + # Clear when switching mortgage used from no to yes + self.deposit = nil + end end if saledate && form.start_year_after_2024? && discounted_ownership_sale? @@ -108,6 +112,14 @@ private mortgage: 0, }, }, + { + conditions: { + mortgageused: 3, + }, + derived_values: { + mortgage: nil, + }, + }, ].freeze def number_of_household_members diff --git a/app/models/form/sales/questions/deposit_amount.rb b/app/models/form/sales/questions/deposit_amount.rb index 72faa818c..3cef0db8d 100644 --- a/app/models/form/sales/questions/deposit_amount.rb +++ b/app/models/form/sales/questions/deposit_amount.rb @@ -16,11 +16,7 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question end def derived?(log) - log.outright_sale? && log.mortgage_not_used? - end - - def selected_answer_option_is_derived?(_log) - true + log.outright_sale? && !log.mortgage_used? end QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = { diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 0654d3a77..f0cea588f 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :sales_log do - assigned_to { FactoryBot.create(:user) } + assigned_to { association :user } created_by { assigned_to } owning_organisation { assigned_to.organisation } managing_organisation { owning_organisation } @@ -28,6 +28,8 @@ FactoryBot.define do ownershipsch { 1 } type { 30 } jointpur { 2 } + noint { 2 } + privacynotice { 1 } end trait :outright_sale_setup_complete do saledate_today @@ -36,6 +38,9 @@ FactoryBot.define do companybuy { 2 } buylivein { 1 } jointpur { 2 } + noint { 2 } + privacynotice { 1 } + purchid { rand(999_999_999).to_s } end trait :duplicate do purchid { "PC123" } diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index 47726e2a8..8a34f86d2 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -83,7 +83,7 @@ RSpec.describe TasklistHelper do describe "with sales" do let(:empty_sales_log) { create(:sales_log, owning_organisation: nil) } - let(:completed_sales_log) { build(:sales_log, :completed) } + let(:completed_sales_log) { create(:sales_log, :completed) } describe "get sections count" do context "with an empty sales log" do diff --git a/spec/models/form/sales/questions/deposit_amount_spec.rb b/spec/models/form/sales/questions/deposit_amount_spec.rb index c08ad7e5e..cb32a7ee1 100644 --- a/spec/models/form/sales/questions/deposit_amount_spec.rb +++ b/spec/models/form/sales/questions/deposit_amount_spec.rb @@ -56,9 +56,9 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do expect(question.derived?(log)).to be true end - it "is not marked as derived when the mortgage use is unknown" do + it "is marked as derived when the mortgage use is unknown" do log.mortgageused = 3 - expect(question.derived?(log)).to be false + expect(question.derived?(log)).to be true end end diff --git a/spec/models/sales_log_derived_fields_spec.rb b/spec/models/sales_log_derived_fields_spec.rb new file mode 100644 index 000000000..ce34c8ca5 --- /dev/null +++ b/spec/models/sales_log_derived_fields_spec.rb @@ -0,0 +1,164 @@ +require "rails_helper" +require "shared/shared_examples_for_derived_fields" + +RSpec.describe SalesLog, type: :model do + include_examples "shared examples for derived fields", :sales_log + + describe "set_derived_fields!" do + it "correctly derives and saves exday, exmonth and exyear" do + log = build(:sales_log, exdate: Time.gm(2023, 5, 4)) + expect { log.set_derived_fields! }.to change(log, :exday).from(nil).to(4) + .and change(log, :exmonth).from(nil).to(5) + .and change(log, :exyear).from(nil).to(2023) + end + + it "correctly derives and saves pcode1 and pcode1 and pcode2" do + log = build(:sales_log, postcode_full: "W6 0SP") + expect { log.set_derived_fields! }.to change(log, :pcode1).from(nil).to("W6") + .and change(log, :pcode2).from(nil).to("0SP") + end + + it "sets pregblank field when no buyer organisation is selected" do + log = build(:sales_log, pregyrha: 0, pregla: 0, pregghb: 0, pregother: 0) + expect { log.set_derived_fields! }.to change(log, :pregblank).from(nil).to(1) + end + + %i[pregyrha pregla pregghb pregother].each do |field| + it "does not set pregblank field when #{field} is selected" do + log = build(:sales_log, pregyrha: 0, pregla: 0, pregghb: 0, pregother: 0) + log[field] = 1 + expect { log.set_derived_fields! }.to not_change(log, :pregblank) + end + end + + it "correctly derives nationality_all/nationality_all_buyer2 when _group is UK" do + log = build(:sales_log, nationality_all_group: 826, nationality_all_buyer2_group: 826) + expect { log.set_derived_fields! }.to change(log, :nationality_all).from(nil).to(826) + .and change(log, :nationality_all_buyer2).from(nil).to(826) + end + + it "correctly derives nationality_all/nationality_all_buyer2 when buyer prefers not to say" do + log = build(:sales_log, nationality_all_group: 0, nationality_all_buyer2_group: 0) + expect { log.set_derived_fields! }.to change(log, :nationality_all).from(nil).to(0) + .and change(log, :nationality_all_buyer2).from(nil).to(0) + end + + it "does not derive nationality_all/nationality_all_buyer2 when it is other" do + log = build(:sales_log, nationality_all_group: 12, nationality_all_buyer2_group: 12) + expect { log.set_derived_fields! }.to not_change(log, :nationality_all) + .and not_change(log, :nationality_all_buyer2) + end + + it "does not derive nationality_all/nationality_all_buyer2 when it is not given" do + log = build(:sales_log, nationality_all_group: nil, nationality_all_buyer2_group: nil) + expect { log.set_derived_fields! }.to not_change(log, :nationality_all) + .and not_change(log, :nationality_all_buyer2) + end + + it "derives a mortgage value of 0 when mortgage is not used" do + log = build(:sales_log, mortgage: 100_000, mortgageused: 2) + expect { log.set_derived_fields! }.to change(log, :mortgage).from(100_000).to(0) + end + + it "clears mortgage value if mortgage used is changed from no to yes" do + log = create(:sales_log, :completed, mortgageused: 2, grant: nil) + log.mortgageused = 1 + expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil) + end + + it "clears mortgage value if mortgage used is changed from no to don't know" do + log = create(:sales_log, :outright_sale_setup_complete, mortgage: 0, mortgageused: 2) + log.mortgageused = 3 + expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil) + end + + it "clears mortgage value if mortgage used is changed from yes to don't know" do + log = create(:sales_log, :outright_sale_setup_complete, mortgage: 50_000, mortgageused: 1) + log.mortgageused = 3 + expect { log.set_derived_fields! }.to change(log, :mortgage).from(50_000).to(nil) + end + + context "with a log that is not outright sales" do + it "does not derive deposit when mortgage used is no" do + log = build(:sales_log, :shared_ownership_setup_complete, value: 123_400, deposit: nil, mortgageused: 2) + expect { log.set_derived_fields! }.to not_change(log, :deposit) + end + end + + context "with an outright sales log" do + it "derives deposit as the value when mortgage used is no" do + log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: nil, mortgageused: 2) + expect { log.set_derived_fields! }.to change(log, :deposit).from(nil).to(123_400) + end + + it "does not derive deposit when mortgage used is yes" do + log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: nil, mortgageused: 1) + expect { log.set_derived_fields! }.to not_change(log, :deposit) + end + + it "sets deposit to nil when mortgage used is don't know" do + log = build(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 0, mortgageused: 3) + expect { log.set_derived_fields! }.to change(log, :deposit).from(0).to(nil) + end + + it "clears derived deposit when setting mortgage used to yes" do + log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2) + log.mortgageused = 1 + expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil) + end + + context "when buyers will live in the property" do + context "and the sale is not a joint purchase" do + it "derives that buyer 1 will live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(1) + end + + it "does not derive a value for whether buyer 2 will live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + log.set_derived_fields! + expect(log.buy2livein).to be_nil + end + + it "clears that buyer 1 will live in the property if joint purchase is updated" do + log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + log.jointpur = 1 + expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil) + end + end + + context "and the sale is a joint purchase" do + it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 1) + log.set_derived_fields! + expect(log.buy1livein).to be_nil + expect(log.buy2livein).to be_nil + end + end + end + + context "when buyers will not live in the property" do + context "and the sale is not a joint purchase" do + it "derives that buyer 1 will not live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 2) + expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(2) + end + + it "does not derive a value for whether buyer 2 will live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 2) + log.set_derived_fields! + expect(log.buy2livein).to be_nil + end + end + + context "and the sale is a joint purchase" do + it "derives that neither buyer 1 nor buyer 2 will live in the property" do + log = build(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur: 1) + expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(2) + .and change(log, :buy2livein).from(nil).to(2) + end + end + end + end + end +end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index a33899968..242fbd9d6 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -1,5 +1,4 @@ require "rails_helper" -require "shared/shared_examples_for_derived_fields" require "shared/shared_log_examples" # rubocop:disable RSpec/MessageChain @@ -17,7 +16,6 @@ RSpec.describe SalesLog, type: :model do Singleton.__init__(FormHandler) end - include_examples "shared examples for derived fields", :sales_log include_examples "shared log examples", :sales_log it "inherits from log" do @@ -535,165 +533,6 @@ RSpec.describe SalesLog, type: :model do end end - describe "derived variables" do - let(:sales_log) { create(:sales_log, :completed) } - - it "correctly derives and saves exday, exmonth and exyear" do - sales_log.update!(exdate: Time.gm(2023, 5, 4), saledate: Time.gm(2023, 7, 4), ownershipsch: 1, type: 18, staircase: 2, resale: 2, proplen: 0) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["exday"]).to eq(4) - expect(record_from_db["exmonth"]).to eq(5) - expect(record_from_db["exyear"]).to eq(2023) - end - - it "correctly derives and saves deposit for outright sales when no mortgage is used" do - sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["deposit"]).to eq(123_400) - end - - it "does not derive deposit if the sale isn't outright" do - sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 2) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["deposit"]).to eq(nil) - end - - it "does not derive deposit if the mortgage is used" do - sales_log.update!(value: 123_400, deposit: nil, mortgageused: 1, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["deposit"]).to eq(nil) - end - - it "does not derive deposit if the mortgage use is unknown" do - Timecop.freeze(2024, 5, 2) - sales_log.update!(value: 123_400, deposit: nil, saledate: Time.zone.local(2024, 5, 2), mortgageused: 3, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["deposit"]).to eq(nil) - end - - it "clears deposit when setting mortgage used to yes from no for outright sales" do - sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1) - sales_log.update!(mortgageused: 1) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["deposit"]).to eq(nil) - end - - it "correctly derives and saves pcode1 and pcode1 and pcode2" do - sales_log.update!(postcode_full: "W6 0SP") - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["pcode1"]).to eq("W6") - expect(record_from_db["pcode2"]).to eq("0SP") - end - - it "derives a mortgage value of 0 when mortgage is not used" do - # to avoid log failing validations when mortgage value is removed: - new_grant_value = sales_log.grant + sales_log.mortgage - sales_log.update!(mortgageused: 2, grant: new_grant_value) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["mortgage"]).to eq(0.0) - end - - it "clears mortgage value if mortgage used is changed from no to yes" do - # to avoid log failing validations when mortgage value is removed: - new_grant_value = sales_log.grant + sales_log.mortgage - sales_log.update!(mortgageused: 2, grant: new_grant_value) - sales_log.update!(mortgageused: 1) - record_from_db = described_class.find(sales_log.id) - expect(record_from_db["mortgage"]).to eq(nil) - end - - context "when outright sale and buyers will live in the property" do - let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur:) } - - context "and the sale is not a joint purchase" do - let(:jointpur) { 2 } - - it "derives that buyer 1 will live in the property" do - expect(sales_log.buy1livein).to be 1 - end - - it "does not derive a value for whether buyer 2 will live in the property" do - expect(sales_log.buy2livein).to be nil - end - - it "clears that buyer 1 will live in the property if joint purchase is updated" do - sales_log.update!(jointpur: 1) - expect(sales_log.buy1livein).to be nil - end - end - - context "and the sale is a joint purchase" do - let(:jointpur) { 1 } - - it "does not derive values for whether buyer 1 or buyer 2 will live in the property" do - expect(sales_log.buy1livein).to be nil - expect(sales_log.buy2livein).to be nil - end - end - end - - context "when outright sale and buyers will not live in the property" do - let(:sales_log) { create(:sales_log, :outright_sale_setup_complete, buylivein: 2, jointpur:) } - - context "and the sale is not a joint purchase" do - let(:jointpur) { 2 } - - it "derives that buyer 1 will not live in the property" do - expect(sales_log.buy1livein).to be 2 - end - - it "does not derive a value for whether buyer 2 will live in the property" do - expect(sales_log.buy2livein).to be nil - end - end - - context "and the sale is a joint purchase" do - let(:jointpur) { 1 } - - it "derives that neither buyer 1 nor buyer 2 will live in the property" do - expect(sales_log.buy1livein).to be 2 - expect(sales_log.buy2livein).to be 2 - end - end - end - - context "when deriving nationality variables" do - it "correctly derives nationality_all/nationality_all_buyer2 when it's UK" do - expect { sales_log.update!(nationality_all_group: 826) }.to change(sales_log, :nationality_all).to 826 - expect { sales_log.update!(nationality_all_buyer2_group: 826) }.to change(sales_log, :nationality_all_buyer2).to 826 - end - - it "correctly derives nationality_all/nationality_all_buyer2 when buyer prefers not to say" do - expect { sales_log.update!(nationality_all_group: 0) }.to change(sales_log, :nationality_all).to 0 - expect { sales_log.update!(nationality_all_buyer2_group: 0) }.to change(sales_log, :nationality_all_buyer2).to 0 - end - - it "does not derive nationality_all/nationality_all_buyer2 when it is other or not given" do - expect { sales_log.update!(nationality_all_group: 12) }.not_to change(sales_log, :nationality_all) - expect { sales_log.update!(nationality_all_buyer2_group: 12) }.not_to change(sales_log, :nationality_all_buyer2) - expect { sales_log.update!(nationality_all_group: nil) }.not_to change(sales_log, :nationality_all) - expect { sales_log.update!(nationality_all_buyer2_group: nil) }.not_to change(sales_log, :nationality_all_buyer2) - end - end - - it "sets pregblank field when no buyer organisation is selected" do - expect { - sales_log.update!(pregyrha: 0, - pregla: 0, - pregghb: 0, - pregother: 0) - }.to change(sales_log, :pregblank).to 1 - end - - %i[pregyrha pregla pregghb pregother].each do |field| - it "does not set pregblank field when #{field} is selected" do - expect { - sales_log.update!({ field => 1 }) - }.not_to change(sales_log, :pregblank) - end - end - end - context "when saving addresses" do before do stub_request(:get, /api.postcodes.io/)