Browse Source

Update discounted_ownership_value_invalid?

pull/1429/head
Kat 3 years ago
parent
commit
0dbfca2800
  1. 12
      app/models/validations/sales/soft_validations.rb
  2. 115
      spec/models/validations/sales/soft_validations_spec.rb

12
app/models/validations/sales/soft_validations.rb

@ -115,7 +115,17 @@ module Validations::Sales::SoftValidations
end end
def discounted_ownership_value_invalid? def discounted_ownership_value_invalid?
false return unless saledate && collection_start_year <= 2023
return unless value && deposit && ownershipsch
return unless mortgage || mortgageused == 2
return unless discount || grant || type == 29
discount_amount = discount ? value * discount / 100 : 0
grant_amount = grant || 0
mortgage_amount = mortgage || 0
value_with_discount = (value - discount_amount)
mortgage_amount + deposit + grant_amount != value_with_discount && discounted_ownership_sale?
end end
private private

115
spec/models/validations/sales/soft_validations_spec.rb

@ -798,4 +798,119 @@ RSpec.describe Validations::Sales::SoftValidations do
expect(record).to be_person_3_student_not_child expect(record).to be_person_3_student_not_child
end end
end end
describe "#discounted_ownership_value_invalid?" do
context "when grant is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: Time.zone.local(2023, 4, 3)) }
context "and not provided" do
before do
record.grant = nil
end
it "returns false" do
expect(record).not_to be_discounted_ownership_value_invalid
end
end
context "and is provided" do
it "returns true if mortgage, deposit and grant total does not equal market value" do
record.grant = 3_000
expect(record).to be_discounted_ownership_value_invalid
end
it "returns false if mortgage, deposit and grant total equals market value" do
record.grant = 15_000
expect(record).not_to be_discounted_ownership_value_invalid
end
end
end
context "when discount is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: Time.zone.local(2023, 4, 3)) }
context "and not provided" do
before do
record.discount = nil
end
it "returns false" do
expect(record).not_to be_discounted_ownership_value_invalid
end
end
context "and is provided" do
it "returns true if mortgage and deposit total does not equal market value - discount" do
record.discount = 10
expect(record).to be_discounted_ownership_value_invalid
end
it "returns false if mortgage and deposit total equals market value - discount" do
record.discount = 50
expect(record).not_to be_discounted_ownership_value_invalid
end
end
end
context "when neither discount nor grant is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: Time.zone.local(2023, 4, 3)) }
it "returns true if mortgage and deposit total does not equal market value" do
record.deposit = 2_000
expect(record).to be_discounted_ownership_value_invalid
end
it "returns false if mortgage and deposit total equals market value" do
record.deposit = 20_000
expect(record).not_to be_discounted_ownership_value_invalid
end
end
context "when mortgage is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 3)) }
context "and not provided" do
before do
record.mortgage = nil
end
it "returns false" do
expect(record).not_to be_discounted_ownership_value_invalid
end
end
context "and is provided" do
it "returns true if mortgage, grant and deposit total does not equal market value - discount" do
record.mortgage = 10
expect(record).to be_discounted_ownership_value_invalid
end
it "returns false if mortgage, grant and deposit total equals market value - discount" do
record.mortgage = 10_000
expect(record).not_to be_discounted_ownership_value_invalid
end
end
end
context "when mortgage is not routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 3)) }
it "returns true if grant and deposit total does not equal market value - discount" do
expect(record).to be_discounted_ownership_value_invalid
end
it "returns false if mortgage, grant and deposit total equals market value - discount" do
record.grant = 13_000
expect(record).not_to be_discounted_ownership_value_invalid
end
end
context "when ownership is not discounted" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: Time.zone.local(2023, 4, 3)) }
it "returns false" do
expect(record).not_to be_discounted_ownership_value_invalid
end
end
end
end end

Loading…
Cancel
Save