Browse Source

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
pull/1541/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
3d2f362106
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      app/models/form/sales/pages/percentage_discount_value_check.rb
  2. 23
      app/models/form/sales/questions/percentage_discount_value_check.rb
  3. 1
      app/models/form/sales/subsections/discounted_ownership_scheme.rb
  4. 1
      app/models/form/sales/subsections/property_information.rb
  5. 11
      app/models/validations/sales/soft_validations.rb
  6. 4
      app/services/imports/sales_logs_import_service.rb
  7. 2
      config/locales/en.yml
  8. 5
      db/migrate/20230405074138_add_percentage_discount_value_check.rb
  9. 1
      db/schema.rb
  10. 48
      spec/models/form/sales/pages/percentage_discount_value_check_spec.rb
  11. 57
      spec/models/form/sales/questions/percentage_discount_value_check_spec.rb
  12. 1
      spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb
  13. 2
      spec/models/form/sales/subsections/property_information_spec.rb
  14. 138
      spec/models/validations/sales/soft_validations_spec.rb

15
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

23
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

1
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),

1
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),

11
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

4
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)

2
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:

5
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

1
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

48
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

57
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

1
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

2
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

138
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

Loading…
Cancel
Save