Browse Source

Clear/error all charges if any charges are missing (#2296)

pull/2278/head^2
kosiakkatrina 10 months ago committed by GitHub
parent
commit
7dba4b56ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 30
      app/services/bulk_upload/lettings/year2024/row_parser.rb
  2. 40
      spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb

30
app/services/bulk_upload/lettings/year2024/row_parser.rb

@ -408,13 +408,13 @@ class BulkUpload::Lettings::Year2024::RowParser
validate :validate_created_by_exists, on: :after_log validate :validate_created_by_exists, on: :after_log
validate :validate_created_by_related, on: :after_log validate :validate_created_by_related, on: :after_log
validate :validate_all_charges_given, on: :after_log, if: proc { is_carehome.zero? }
validate :validate_nulls, on: :after_log validate :validate_nulls, on: :after_log
validate :validate_uprn_exists_if_any_key_address_fields_are_blank, on: :after_log, unless: -> { supported_housing? } validate :validate_uprn_exists_if_any_key_address_fields_are_blank, on: :after_log, unless: -> { supported_housing? }
validate :validate_incomplete_soft_validations, on: :after_log validate :validate_incomplete_soft_validations, on: :after_log
validate :validate_all_charges_given, on: :after_log, if: proc { is_carehome.zero? }
validate :validate_nationality, on: :after_log validate :validate_nationality, on: :after_log
def self.question_for_field(field) def self.question_for_field(field)
@ -853,16 +853,28 @@ private
def validate_all_charges_given def validate_all_charges_given
return if supported_housing? && field_125 == 1 return if supported_housing? && field_125 == 1
{ field_125: "basic rent", blank_charge_fields, other_charge_fields = {
field_125: "basic rent",
field_126: "service charge", field_126: "service charge",
field_127: "personal service charge", field_127: "personal service charge",
field_128: "support charge" }.each do |field, charge| field_128: "support charge",
if public_send(field.to_sym).blank? }.partition { |field, _| public_send(field).blank? }.map(&:to_h)
errors.add(field, I18n.t("validations.financial.charges.missing_charges", question: charge))
blank_charge_fields.each do |field, charge|
errors.add(field, I18n.t("validations.financial.charges.missing_charges", question: charge))
end
other_charge_fields.each do |field, _charge|
blank_charge_fields.each do |_blank_field, blank_charge|
errors.add(field, I18n.t("validations.financial.charges.missing_charges", question: blank_charge))
end end
end end
end end
def all_charges_given?
field_125.present? && field_126.present? && field_127.present? && field_128.present?
end
def setup_question?(question) def setup_question?(question)
log.form.setup_sections[0].subsections[0].questions.include?(question) log.form.setup_sections[0].subsections[0].questions.include?(question)
end end
@ -1201,10 +1213,10 @@ private
attributes["benefits"] = field_121 attributes["benefits"] = field_121
attributes["period"] = field_123 attributes["period"] = field_123
attributes["brent"] = field_125 attributes["brent"] = field_125 if all_charges_given?
attributes["scharge"] = field_126 attributes["scharge"] = field_126 if all_charges_given?
attributes["pscharge"] = field_127 attributes["pscharge"] = field_127 if all_charges_given?
attributes["supcharg"] = field_128 attributes["supcharg"] = field_128 if all_charges_given?
attributes["chcharge"] = field_124 attributes["chcharge"] = field_124
attributes["is_carehome"] = is_carehome attributes["is_carehome"] = is_carehome
attributes["household_charge"] = supported_housing? ? field_122 : nil attributes["household_charge"] = supported_housing? ? field_122 : nil

40
spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb

@ -1653,7 +1653,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end end
context "when soft validation is triggered and not required" do context "when soft validation is triggered and not required" do
let(:attributes) { setup_section_params.merge({ field_125: 120, field_123: 1, field_29: 1, field_4: 1, field_11: "1", field_23: "E09000008" }) } let(:attributes) { setup_section_params.merge({ field_125: 120, field_126: 120, field_127: 120, field_128: 120, field_123: 1, field_29: 1, field_4: 1, field_11: "1", field_23: "E09000008" }) }
it "adds an error to the relevant fields" do it "adds an error to the relevant fields" do
expect(parser.errors.where(:field_125, category: :soft_validation)).to be_present expect(parser.errors.where(:field_125, category: :soft_validation)).to be_present
@ -2240,7 +2240,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end end
describe "#chcharge" do describe "#chcharge" do
let(:attributes) { { bulk_upload:, field_124: "123.45", field_125: "123.45", field_126: "123.45", field_127: "123.45", field_128: "123.45" } } let(:attributes) { setup_section_params.merge({ field_124: "123.45", field_125: "123.45", field_126: "123.45", field_127: "123.45", field_128: "123.45" }) }
it "sets value given" do it "sets value given" do
expect(parser.log.chcharge).to eq(123.45) expect(parser.log.chcharge).to eq(123.45)
@ -2261,7 +2261,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end end
describe "#supcharg" do describe "#supcharg" do
let(:attributes) { { bulk_upload:, field_128: "123.45" } } let(:attributes) { setup_section_params.merge({ field_125: "330", field_126: "0", field_127: "0", field_128: "123.45" }) }
it "sets value given" do it "sets value given" do
expect(parser.log.supcharg).to eq(123.45) expect(parser.log.supcharg).to eq(123.45)
@ -2269,7 +2269,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
context "when other charges are not given" do context "when other charges are not given" do
context "and it is carehome" do context "and it is carehome" do
let(:attributes) { { bulk_upload:, field_128: "123.45", field_124: "123.45", field_125: nil, field_126: nil, field_127: nil } } let(:attributes) { setup_section_params.merge({ field_128: "123.45", field_124: "123.45", field_125: nil, field_126: nil, field_127: nil }) }
it "does not set charges values" do it "does not set charges values" do
parser.log.save! parser.log.save!
@ -2290,7 +2290,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end end
context "and it is not carehome" do context "and it is not carehome" do
let(:attributes) { { bulk_upload:, field_128: "123.45", field_124: nil, field_125: nil, field_126: nil, field_127: nil } } let(:attributes) { setup_section_params.merge({ field_128: "123.45", field_124: nil, field_125: nil, field_126: nil, field_127: nil }) }
it "does not set charges values" do it "does not set charges values" do
parser.log.save! parser.log.save!
@ -2306,14 +2306,38 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
expect(parser.errors[:field_125]).to eql(["Please enter the basic rent. If there is no basic rent, please enter '0'."]) expect(parser.errors[:field_125]).to eql(["Please enter the basic rent. If there is no basic rent, please enter '0'."])
expect(parser.errors[:field_126]).to eql(["Please enter the service charge. If there is no service charge, please enter '0'."]) expect(parser.errors[:field_126]).to eql(["Please enter the service charge. If there is no service charge, please enter '0'."])
expect(parser.errors[:field_127]).to eql(["Please enter the personal service charge. If there is no personal service charge, please enter '0'."]) expect(parser.errors[:field_127]).to eql(["Please enter the personal service charge. If there is no personal service charge, please enter '0'."])
expect(parser.errors[:field_128]).to be_empty expect(parser.errors[:field_128]).to eql(["Please enter the basic rent. If there is no basic rent, please enter '0'.", "Please enter the service charge. If there is no service charge, please enter '0'.", "Please enter the personal service charge. If there is no personal service charge, please enter '0'."])
end
end
end
context "when supscharg is not given" do
context "and it is not carehome" do
let(:attributes) { setup_section_params.merge({ field_123: 1, field_124: nil, field_125: "350.45", field_126: "0", field_127: "0", field_128: nil }) }
it "does not set charges values" do
parser.log.save!
expect(parser.log.period).not_to be_nil
expect(parser.log.tcharge).to be_nil
expect(parser.log.brent).to be_nil
expect(parser.log.supcharg).to be_nil
expect(parser.log.pscharge).to be_nil
expect(parser.log.scharge).to be_nil
end
it "adds an error to all charges" do
parser.valid?
expect(parser.errors[:field_125]).to eql(["Please enter the support charge. If there is no support charge, please enter '0'."])
expect(parser.errors[:field_126]).to eql(["Please enter the support charge. If there is no support charge, please enter '0'."])
expect(parser.errors[:field_127]).to eql(["Please enter the support charge. If there is no support charge, please enter '0'."])
expect(parser.errors[:field_128]).to eql(["Please enter the support charge. If there is no support charge, please enter '0'."])
end end
end end
end end
end end
describe "#pscharge" do describe "#pscharge" do
let(:attributes) { { bulk_upload:, field_127: "123.45" } } let(:attributes) { { bulk_upload:, field_125: "111.45", field_126: "0", field_127: "123.45", field_128: "0" } }
it "sets value given" do it "sets value given" do
expect(parser.log.pscharge).to eq(123.45) expect(parser.log.pscharge).to eq(123.45)
@ -2321,7 +2345,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do
end end
describe "#scharge" do describe "#scharge" do
let(:attributes) { { bulk_upload:, field_126: "123.45" } } let(:attributes) { { bulk_upload:, field_125: "111.45", field_126: "123.45", field_127: "0", field_128: "0" } }
it "sets value given" do it "sets value given" do
expect(parser.log.scharge).to eq(123.45) expect(parser.log.scharge).to eq(123.45)

Loading…
Cancel
Save