From ce4d7cb919116cccb52499715fa967184a097afd Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Tue, 24 Jan 2023 12:41:35 +0000 Subject: [PATCH] fix rebase conflicts --- .../sales/financial_validations.rb | 8 ++++++ config/locales/en.yml | 2 ++ .../sales/financial_validations_spec.rb | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 233f32b6b..1e404aac4 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -27,4 +27,12 @@ module Validations::Sales::FinancialValidations record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid") end end + + def validate_percentage(record) + return unless record.discount + + if record.discount.negative? || record.discount > 100 + record.errors.add :discount, I18n.t("validations.financial.percentage.invalid_percentage") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index ff6101978..daa8116b4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -275,6 +275,8 @@ en: out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" not_provided: "Enter how much rent and other charges the household pays %{period}" cash_discount_invalid: "Cash discount must be £0 - £999,999" + percentage: + invalid_percentage: "Percentage discount must be 0 - 100" household: reasonpref: not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 4bc19afbe..d6d735296 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -99,4 +99,31 @@ RSpec.describe Validations::Sales::FinancialValidations do expect(record.errors["cashdis"]).to be_empty end end + + describe "#validate_percentage" do + let(:record) { FactoryBot.create(:sales_log, ownershipsch: 2, type: 9) } + + it "does not add an error when percentage discount is not yet given" do + financial_validator.validate_percentage(record) + expect(record.errors["discount"]).to be_empty + end + + it "does not add an error when percentage discount is in correct range" do + record.discount = 2 + financial_validator.validate_percentage(record) + expect(record.errors["discount"]).to be_empty + end + + it "adds an error when percentage declared is below zero" do + record.discount = -2 + financial_validator.validate_percentage(record) + expect(record.errors["discount"]).to include(match I18n.t("validations.financial.percentage.invalid_percentage")) + end + + it "adds an error when percentage declared is above 100" do + record.discount = 102 + financial_validator.validate_percentage(record) + expect(record.errors["discount"]).to include(match I18n.t("validations.financial.percentage.invalid_percentage")) + end + end end