From 3d2f362106eeaecc8356a841c610c453ae1da60f Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 14 Apr 2023 11:07:09 +0100 Subject: [PATCH] CLDC-2013 Add percentage discount validation (#1516) * Add percentage_discount_value_check column * Add percentage discount form elements * Add soft validation method * Add validation message * Update import --- .../pages/percentage_discount_value_check.rb | 15 ++ .../percentage_discount_value_check.rb | 23 +++ .../discounted_ownership_scheme.rb | 1 + .../sales/subsections/property_information.rb | 1 + .../validations/sales/soft_validations.rb | 11 ++ .../imports/sales_logs_import_service.rb | 4 +- config/locales/en.yml | 2 + ...138_add_percentage_discount_value_check.rb | 5 + db/schema.rb | 1 + .../percentage_discount_value_check_spec.rb | 48 ++++++ .../percentage_discount_value_check_spec.rb | 57 ++++++++ .../discounted_ownership_scheme_spec.rb | 1 + .../subsections/property_information_spec.rb | 2 + .../sales/soft_validations_spec.rb | 138 ++++++++++++++++++ 14 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 app/models/form/sales/pages/percentage_discount_value_check.rb create mode 100644 app/models/form/sales/questions/percentage_discount_value_check.rb create mode 100644 db/migrate/20230405074138_add_percentage_discount_value_check.rb create mode 100644 spec/models/form/sales/pages/percentage_discount_value_check_spec.rb create mode 100644 spec/models/form/sales/questions/percentage_discount_value_check_spec.rb diff --git a/app/models/form/sales/pages/percentage_discount_value_check.rb b/app/models/form/sales/pages/percentage_discount_value_check.rb new file mode 100644 index 000000000..1e3beb3e6 --- /dev/null +++ b/app/models/form/sales/pages/percentage_discount_value_check.rb @@ -0,0 +1,15 @@ +class Form::Sales::Pages::PercentageDiscountValueCheck < ::Form::Page + def initialize(id, hsh, subsection) + super + @title_text = { + "translation" => "soft_validations.percentage_discount_value.title_text", + "arguments" => [{ "key" => "discount", "label" => true, "i18n_template" => "discount" }], + } + @informative_text = {} + @depends_on = [{ "percentage_discount_invalid?" => true }] + end + + def questions + @questions ||= [Form::Sales::Questions::PercentageDiscountValueCheck.new(nil, nil, self)] + end +end diff --git a/app/models/form/sales/questions/percentage_discount_value_check.rb b/app/models/form/sales/questions/percentage_discount_value_check.rb new file mode 100644 index 000000000..9ac5547b4 --- /dev/null +++ b/app/models/form/sales/questions/percentage_discount_value_check.rb @@ -0,0 +1,23 @@ +class Form::Sales::Questions::PercentageDiscountValueCheck < ::Form::Question + def initialize(id, hsh, page) + super + @id = "percentage_discount_value_check" + @check_answer_label = "Percentage discount confirmation" + @header = "Are you sure this is correct?" + @type = "interruption_screen" + @answer_options = { + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + } + @hidden_in_check_answers = { + "depends_on" => [ + { + "percentage_discount_value_check" => 0, + }, + { + "percentage_discount_value_check" => 1, + }, + ], + } + end +end diff --git a/app/models/form/sales/subsections/discounted_ownership_scheme.rb b/app/models/form/sales/subsections/discounted_ownership_scheme.rb index 09813339d..52ee9e95c 100644 --- a/app/models/form/sales/subsections/discounted_ownership_scheme.rb +++ b/app/models/form/sales/subsections/discounted_ownership_scheme.rb @@ -11,6 +11,7 @@ class Form::Sales::Subsections::DiscountedOwnershipScheme < ::Form::Subsection Form::Sales::Pages::LivingBeforePurchase.new("living_before_purchase_discounted_ownership", nil, self, ownershipsch: 2), Form::Sales::Pages::AboutPriceRtb.new(nil, nil, self), Form::Sales::Pages::ExtraBorrowingValueCheck.new("extra_borrowing_price_value_check", nil, self), + Form::Sales::Pages::PercentageDiscountValueCheck.new("percentage_discount_value_check", nil, self), Form::Sales::Pages::AboutPriceNotRtb.new(nil, nil, self), Form::Sales::Pages::GrantValueCheck.new(nil, nil, self), Form::Sales::Pages::PurchasePriceOutrightOwnership.new("purchase_price_discounted_ownership", nil, self, ownershipsch: 2), diff --git a/app/models/form/sales/subsections/property_information.rb b/app/models/form/sales/subsections/property_information.rb index cca34a764..95d8485c8 100644 --- a/app/models/form/sales/subsections/property_information.rb +++ b/app/models/form/sales/subsections/property_information.rb @@ -13,6 +13,7 @@ class Form::Sales::Subsections::PropertyInformation < ::Form::Subsection Form::Sales::Pages::AboutPriceValueCheck.new("about_price_bedrooms_value_check", nil, self), Form::Sales::Pages::PropertyUnitType.new(nil, nil, self), Form::Sales::Pages::MonthlyChargesValueCheck.new("monthly_charges_property_type_value_check", nil, self), + Form::Sales::Pages::PercentageDiscountValueCheck.new("percentage_discount_proptype_value_check", nil, self), Form::Sales::Pages::PropertyBuildingType.new(nil, nil, self), postcode_and_la_questions, Form::Sales::Pages::AboutPriceValueCheck.new("about_price_la_value_check", nil, self), diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index 354bc7fd7..19d150c7e 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -136,6 +136,17 @@ module Validations::Sales::SoftValidations (discounted_ownership_sale? || shared_ownership_scheme?) && buy2livein == 2 end + def percentage_discount_invalid? + return unless discount && proptype + + case proptype + when 1, 2 + discount > 50 + when 3, 4, 9 + discount > 35 + end + end + private def sale_range diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index e02d7d23c..2d6551189 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -156,6 +156,7 @@ module Imports attributes["student_not_child_value_check"] = 0 attributes["discounted_sale_value_check"] = 0 attributes["buyer_livein_value_check"] = 0 + attributes["percentage_discount_value_check"] = 0 # Sets the log creator owner_id = meta_field_value(xml_doc, "owner-user-id").strip @@ -280,7 +281,8 @@ module Imports saledate_check student_not_child_value_check discounted_sale_value_check - buyer_livein_value_check] + buyer_livein_value_check + percentage_discount_value_check] end def check_status_completed(sales_log, previous_status) diff --git a/config/locales/en.yml b/config/locales/en.yml index d333accd8..f0b52ce9f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -555,6 +555,8 @@ en: title_text: "You told us that buyer 1 will not live in the property. For %{ownership_scheme} types, the buyer usually lives in the property." buyer2_livein_wrong_for_ownership_type: title_text: "You told us that buyer 2 will not live in the property. For %{ownership_scheme} types, the buyer usually lives in the property." + percentage_discount_value: + title_text: "You told us that the percentage discount was %{discount}. This seems high for this type of property." devise: two_factor_authentication: diff --git a/db/migrate/20230405074138_add_percentage_discount_value_check.rb b/db/migrate/20230405074138_add_percentage_discount_value_check.rb new file mode 100644 index 000000000..adce0fe58 --- /dev/null +++ b/db/migrate/20230405074138_add_percentage_discount_value_check.rb @@ -0,0 +1,5 @@ +class AddPercentageDiscountValueCheck < ActiveRecord::Migration[7.0] + def change + add_column :sales_logs, :percentage_discount_value_check, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 8a14eed1d..e121fda09 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -574,6 +574,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.integer "discounted_sale_value_check" t.integer "student_not_child_value_check" t.integer "buyer_livein_value_check" + t.integer "percentage_discount_value_check" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["old_id"], name: "index_sales_logs_on_old_id", unique: true diff --git a/spec/models/form/sales/pages/percentage_discount_value_check_spec.rb b/spec/models/form/sales/pages/percentage_discount_value_check_spec.rb new file mode 100644 index 000000000..d933e3278 --- /dev/null +++ b/spec/models/form/sales/pages/percentage_discount_value_check_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::PercentageDiscountValueCheck, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { "percentage_discount_value_check" } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[percentage_discount_value_check]) + end + + it "has the correct id" do + expect(page.id).to eq("percentage_discount_value_check") + end + + it "has the correct header" do + expect(page.header).to be_nil + end + + it "has the correct title_text" do + expect(page.title_text).to eq({ + "translation" => "soft_validations.percentage_discount_value.title_text", + "arguments" => [{ "key" => "discount", "label" => true, "i18n_template" => "discount" }], + }) + end + + it "has the correct informative_text" do + expect(page.informative_text).to eq({}) + end + + it "is interruption screen page" do + expect(page.interruption_screen?).to eq(true) + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { + "percentage_discount_invalid?" => true, + }, + ]) + end +end diff --git a/spec/models/form/sales/questions/percentage_discount_value_check_spec.rb b/spec/models/form/sales/questions/percentage_discount_value_check_spec.rb new file mode 100644 index 000000000..72f4f0cbb --- /dev/null +++ b/spec/models/form/sales/questions/percentage_discount_value_check_spec.rb @@ -0,0 +1,57 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::PercentageDiscountValueCheck, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("percentage_discount_value_check") + end + + it "has the correct header" do + expect(question.header).to eq("Are you sure this is correct?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Percentage discount confirmation") + end + + it "has the correct type" do + expect(question.type).to eq("interruption_screen") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint" do + expect(question.hint_text).to be_nil + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + }) + end + + it "has the correct hidden_in_check_answers" do + expect(question.hidden_in_check_answers).to eq({ + "depends_on" => [ + { + "percentage_discount_value_check" => 0, + }, + { + "percentage_discount_value_check" => 1, + }, + ], + }) + end +end diff --git a/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb b/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb index 5c0dbfea0..9aa0aa102 100644 --- a/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb +++ b/spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb @@ -17,6 +17,7 @@ RSpec.describe Form::Sales::Subsections::DiscountedOwnershipScheme, type: :model living_before_purchase_discounted_ownership about_price_rtb extra_borrowing_price_value_check + percentage_discount_value_check about_price_not_rtb grant_value_check purchase_price_discounted_ownership diff --git a/spec/models/form/sales/subsections/property_information_spec.rb b/spec/models/form/sales/subsections/property_information_spec.rb index 1c049ea97..7a0c57448 100644 --- a/spec/models/form/sales/subsections/property_information_spec.rb +++ b/spec/models/form/sales/subsections/property_information_spec.rb @@ -24,6 +24,7 @@ RSpec.describe Form::Sales::Subsections::PropertyInformation, type: :model do about_price_bedrooms_value_check property_unit_type monthly_charges_property_type_value_check + percentage_discount_proptype_value_check property_building_type property_postcode property_local_authority @@ -49,6 +50,7 @@ RSpec.describe Form::Sales::Subsections::PropertyInformation, type: :model do about_price_bedrooms_value_check property_unit_type monthly_charges_property_type_value_check + percentage_discount_proptype_value_check property_building_type about_price_la_value_check property_wheelchair_accessible diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index ae20e1111..f63f6fbcc 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -938,6 +938,32 @@ RSpec.describe Validations::Sales::SoftValidations do end end end + end + + describe "#percentage_discount_invalid?" do + context "when property type is Flat (1)" do + let(:record) { FactoryBot.build(:sales_log, proptype: 1) } + + context "and discount is under 50%" do + before do + record.discount = 49 + end + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "and discount is over 50%" do + before do + record.discount = 51 + end + + it "returns true" do + expect(record).to be_percentage_discount_invalid + end + end + end context "when it's a discounted ownership" do let(:record) { FactoryBot.build(:sales_log, ownershipsch: 2) } @@ -963,6 +989,30 @@ RSpec.describe Validations::Sales::SoftValidations do end end + context "when property type is masionette or bedsit (2)" do + let(:record) { FactoryBot.build(:sales_log, proptype: 2) } + + context "and discount is under 50%" do + before do + record.discount = 49 + end + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "and discount is over 50%" do + before do + record.discount = 51 + end + + it "returns true" do + expect(record).to be_percentage_discount_invalid + end + end + end + context "when it's a outright sale" do let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3) } @@ -987,6 +1037,30 @@ RSpec.describe Validations::Sales::SoftValidations do end end + context "when property type is House (3)" do + let(:record) { FactoryBot.build(:sales_log, proptype: 3) } + + context "and discount is under 35%" do + before do + record.discount = 34 + end + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "and discount is over 35%" do + before do + record.discount = 36 + end + + it "returns true" do + expect(record).to be_percentage_discount_invalid + end + end + end + context "when ownership is not given" do let(:record) { FactoryBot.build(:sales_log, ownershipsch: 3) } @@ -1095,5 +1169,69 @@ RSpec.describe Validations::Sales::SoftValidations do expect(record).not_to be_buyer2_livein_wrong_for_ownership_type end end + + context "when property type is Bungalow (4)" do + let(:record) { FactoryBot.build(:sales_log, proptype: 4) } + + context "and discount is under 35%" do + before do + record.discount = 34 + end + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "and discount is over 35%" do + before do + record.discount = 36 + end + + it "returns true" do + expect(record).to be_percentage_discount_invalid + end + end + end + + context "when property type is Other (9)" do + let(:record) { FactoryBot.build(:sales_log, proptype: 9) } + + context "and discount is under 35%" do + before do + record.discount = 34 + end + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "and discount is over 35%" do + before do + record.discount = 36 + end + + it "returns true" do + expect(record).to be_percentage_discount_invalid + end + end + end + + context "when discount is not given" do + let(:record) { FactoryBot.build(:sales_log, proptype: 1, discount: nil) } + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end + + context "when property type is not given" do + let(:record) { FactoryBot.build(:sales_log, proptype: nil, discount: 51) } + + it "returns false" do + expect(record).not_to be_percentage_discount_invalid + end + end end end