Skip to content

Commit

Permalink
Merge pull request #7424 from ministryofjustice/ap-3563/standardise-c…
Browse files Browse the repository at this point in the history
…urrency-handling-across-app

AP-3563/AP-5496: Standardise currency handling across the app
  • Loading branch information
jsugarman authored Nov 14, 2024
2 parents 883ea0d + 22989fd commit 5bc98b1
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def date_received
@date_received = attributes[:date_received] = date_received_fields.form_date
end

def attributes_to_clean
[:amount]
end

private

def exclude_from_model
Expand Down
4 changes: 4 additions & 0 deletions app/forms/providers/means/state_benefit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def frequency_options
RegularTransaction.frequencies_for(state_benefit_transaction_type)
end

def attributes_to_clean
%i[amount]
end

private

def state_benefit_transaction_type
Expand Down
10 changes: 7 additions & 3 deletions app/forms/providers/regular_transaction_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(params = {})
@legal_aid_application = params.delete(:legal_aid_application)
@transaction_type_ids = params["transaction_type_ids"] || existing_transaction_type_ids
owner.present?
assign_regular_transaction_attributes
assign_existing_regular_transaction_attributes

super
end
Expand Down Expand Up @@ -96,7 +96,7 @@ def existing_transaction_type_ids
.pluck(:transaction_type_id)
end

def assign_regular_transaction_attributes
def assign_existing_regular_transaction_attributes
regular_transactions.each do |transaction|
transaction_type = transaction.transaction_type
public_send(:"#{transaction_type.name}_amount=", transaction.amount)
Expand Down Expand Up @@ -168,7 +168,7 @@ def none_and_another_checkbox_checked?
def all_regular_transactions_valid
regular_transactions.each do |transaction|
transaction_type = transaction.transaction_type
transaction.amount = public_send(:"#{transaction_type.name}_amount")
transaction.amount = clean_amount(public_send(:"#{transaction_type.name}_amount"))
transaction.frequency = public_send(:"#{transaction_type.name}_frequency")

next if transaction.valid?
Expand All @@ -182,6 +182,10 @@ def all_regular_transactions_valid
end
end

def clean_amount(amount)
amount.to_s.tr("£,", "")
end

def add_regular_transaction_error_to_form(transaction_type, error)
attribute = if error.attribute.in?(%i[amount frequency])
:"#{transaction_type}_#{error.attribute}"
Expand Down
4 changes: 4 additions & 0 deletions app/forms/student_finances/base_student_finance_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ class BaseStudentFinanceForm < BaseForm
def student_finance?
student_finance.eql?("true")
end

def attributes_to_clean
%i[student_finance_amount]
end
end
end
13 changes: 12 additions & 1 deletion app/models/base_aggregated_cash_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ def transaction_date(month_number)
end

def update_cash_attributes(params)
params.each do |key, value|
cleaned_params = clean_attributes(params)
cleaned_params.each do |key, value|
__send__(:"#{key}=", value)
end
end

def clean_attributes(params)
params.each.with_object({}) do |(k, v), new_hash|
new_hash[k] = cash_transaction_amount_field?(k) ? v.to_s.tr("£,", "") : v
end
end

def cash_transaction_amount_field?(param_name)
self.class.cash_transaction_categories.map(&:to_s).any? { |category| param_name.start_with?(category) }
end

def save_cash_transaction_records
self.class.cash_transaction_categories.each do |category|
CashTransaction.where(
Expand Down
3 changes: 1 addition & 2 deletions app/views/shared/check_answers/_cash_payments.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% read_only = false unless local_assigns.key?(:read_only) %>

<div class="govuk-grid-row">
<div class="govuk-grid-row" id="app-check-your-answers__<%= individual.to_s.downcase %>__cash_<%= type.to_s.downcase %>">
<div class="govuk-grid-column-two-thirds">
<h3 class="govuk-heading-m"><%= t(".#{type}_heading", individual_with_determiner:) %></h3>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/check_answers/_student_finance.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="govuk-grid-row" id="app-check-your-answers__<%= individual.class.to_s.downcase %>_student_finance">
<div class="govuk-grid-row" id="app-check-your-answers__<%= individual.class.to_s.downcase %>__student_finance">
<div class="govuk-grid-column-two-thirds">
<h3 class="govuk-heading-m"><%= t(".heading") %></h3>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/partials/_revealing_checkbox.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<% end %>
<%= form.govuk_text_field "#{name}#{number}",
label: { text: model.period(number) },
value: gds_number_to_currency(model.__send__(:"#{name}#{number}"), unit: ""),
value: model.__send__(:"#{name}#{number}"),
prefix_text: defined?(input_prefix) ? input_prefix : nil,
form_group: { classes: error_class },
width: "one-third" %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Feature: Bank statement upload check your answers
And I click "Save and continue"
Then I should be on the "check_income_answers" page showing "Check your answers"

When I click Check Your Answers Change link for "applicant student finance"
When I click Check Your Answers Change link for applicant 'student_finance'
And I choose "Yes"
And I enter amount "5000"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Feature: Bank statement upload check your answers
And I click "Save and continue"
Then I should be on the "check_income_answers" page showing "Check your answers"

When I click Check Your Answers Change link for "partner student finance"
When I click Check Your Answers Change link for partner 'student_finance'
And I choose "Yes"
And I enter amount "5000"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
@javascript
Feature: Entering humanized monetary amounts on various forms
Scenario: I can enter humanized monetary amounts like 1,000 for cash income
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the "check_income_answers" page showing "Check your answers"

When I click Check Your Answers Change link for applicant 'cash_income'
And I select "Maintenance payments from a former partner"
And I fill "aggregated-cash-income-maintenance-in1-field" with "£2,654.33"
And I fill "aggregated-cash-income-maintenance-in2-field" with "£3,654.33"
And I fill "aggregated-cash-income-maintenance-in3-field" with "£4,654.33"

When I click 'Save and continue'
Then I should be on the 'check_income_answers' page showing 'Check your answers'
And I should see "£2,654.33"
And I should see "£3,654.33"
And I should see "£4,654.33"

Scenario: I can enter humanized monetary amounts like 1,000 for cash outgoings and housing benefit
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the "check_income_answers" page showing "Check your answers"

When I click Check Your Answers Change link for applicant 'cash_outgoings'
And I select "Housing payments"
And I fill "aggregated-cash-outgoings-rent-or-mortgage1-field" with "£2,275.43"
And I fill "aggregated-cash-outgoings-rent-or-mortgage2-field" with "£3,275.43"
And I fill "aggregated-cash-outgoings-rent-or-mortgage3-field" with "£4,275.43"

When I click 'Save and continue'
Then I should be on the 'housing_benefits' page showing "Does your client get Housing Benefit?"

When I choose "Yes"
And I fill "providers-means-housing-benefit-form-housing-benefit-amount-field" with "£1,322.55"
And I choose "Monthly"

When I click 'Save and continue'
Then I should be on the 'check_income_answers' page showing 'Check your answers'
And I should see "£2,275.43"
And I should see "£3,275.43"
And I should see "£4,275.43"
And I should see "£1,322.55"

Scenario: I can enter humanized monetary amounts like 1,000 for state benefits
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the "check_income_answers" page showing "Check your answers"

When I click Check Your Answers Change link for applicant 'state_benefits'
And I choose "Yes"
And I click 'Save and continue'

Then I should be on a page with title "Add benefit, charitable or government payment details"
And I fill 'regular-transaction-description-field' with "my government handout"
And I fill 'regular-transaction-amount-field' with "£1,222,333.44"
And I choose "Every week"

When I click 'Save and continue'
Then I should be on the 'add_other_state_benefits' page showing 'You added 1 benefit, charitable or government payment'
And I should see "£1,222,333.44"

Scenario: I can enter humanized monetary amounts like 1,000 for student finance
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the 'check_income_answers' page showing 'Check your answers'

When I click Check Your Answers Change link for applicant 'student_finance'
Then I should be on a page with title "Does your client get student finance?"
And I choose "Yes"
And I fill 'applicant-student-finance-amount-field' with "£5,432.11"

When I click 'Save and continue'
Then I should be on the 'check_income_answers' page showing 'Check your answers'
And I should see "£5,432.11"

Scenario: I can enter humanized monetary amounts like 1,000 for regular income
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the 'check_income_answers' page showing 'Check your answers'

When I click Check Your Answers Change link for "Payments your client receives"
Then I should be on a page with title "Which of these payments does your client get?"
And I select "Financial help from friends or family"
And I fill "Friends or family" with "£1,112.33"
And I choose the "Monthly" frequency for "Friends or family"

When I click 'Save and continue'
Then I should be on a page with title "Select payments your client receives in cash"
And I select "My client receives none of these payments in cash"
And I click 'Save and continue'
Then I should be on the 'check_income_answers' page showing 'Check your answers'
And I should see "£1,112.33"

Scenario: I can enter humanized monetary amounts like 1,000 for regular outgoings and housing benefit
Given csrf is enabled
And I have completed a non-passported employed application for "client" with bank statements as far as the end of the means income section
Then I should be on the 'check_income_answers' page showing 'Check your answers'

When I click Check Your Answers Change link for "Payments your client makes"
Then I should be on a page with title "Which of these payments does your client pay?"
And I select "Maintenance payments to a former partner"
And I fill "Maintenance out" with "£2,322.22"
And I choose the "Monthly" frequency for "Maintenance out"

When I click 'Save and continue'
Then I should be on a page with title "Select payments your client pays in cash"

When I select "None of the above"
And I click 'Save and continue'
Then I should be on a page with title "Does your client get Housing Benefit?"

When I click 'Save and continue'
Then I should be on the 'check_income_answers' page showing 'Check your answers'
And I should see "£2,322.22"

Scenario: I can enter humanized monetary amounts like 1,000 for mandatory capital disregards
Given the feature flag for means_test_review_a is enabled
And the MTR-A start date is in the past
And I have completed the income and capital sections of a non-passported application with bank statement uploads post-MTRA
When I am viewing the means capital check your answers page

When I click link "Change Budgeting Advances"
And I fill "capital-disregard-amount-field" with "£1,654.33"

When I click 'Save and continue'
Then I should be on the 'check_capital_answers' page showing 'Check your answers'
And I should see "£1,654.33"

Scenario: I can enter humanized monetary amounts like 1,000 for discretionary capital disregards
Given the feature flag for means_test_review_a is enabled
And the MTR-A start date is in the past
And I have completed the income and capital sections of a non-passported application with bank statement uploads post-MTRA
When I am viewing the means capital check your answers page

When I click link "Change Compensation, damages or ex-gratia payments for personal harm"
And I fill "capital-disregard-amount-field" with "£2,321.11"

When I click 'Save and continue'
Then I should be on the 'check_capital_answers' page showing 'Check your answers'
And I should see "£2,321.11"
1 change: 1 addition & 0 deletions features/step_definitions/bank_statement_upload_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
:legal_aid_application,
:with_proceedings,
:with_employed_applicant,
:with_maintenance_in_category,
:with_rent_or_mortgage_regular_transaction,
:with_housing_benefit_regular_transaction,
:with_savings_amount,
Expand Down
8 changes: 8 additions & 0 deletions features/step_definitions/civil_journey_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,14 @@
end
end

Given("I click Check Your Answers Change link for partner {string}") do |question|
question_id = question.parameterize(separator: "_")

within "#app-check-your-answers__partner__#{question_id}" do
click_on("Change")
end
end

Given("I click Check Your Answers Change link for {string}") do |question|
question_id = question.parameterize(separator: "_")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@
end

it "updates the capital_disregard" do
expect(application.capital_disregards.first.amount).to eq 123
expect(application.capital_disregards.first.account_name).to eq "Barclays"
expect(application.capital_disregards.first.date_received).to eq Date.new(2024, 2, 1)
expect(application.capital_disregards.first)
.to have_attributes(
amount: 123,
account_name: "Barclays",
date_received: Date.new(2024, 2, 1),
)
end

context "with humanized monetary value" do
let(:amount) { "£1,244.55" }

it "is valid" do
expect(form).to be_valid
end

it "saves the monetary result" do
expect(application.capital_disregards.first.amount).to eq(1_244.55)
end
end

context "when amount is missing" do
Expand Down
15 changes: 15 additions & 0 deletions spec/forms/providers/means/housing_benefit_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@
)
end

it "cleans the housing benefit regular transactions amount of humanized characters" do
legal_aid_application = create(:legal_aid_application, :with_applicant)
transaction_type = create(:transaction_type, :housing_benefit)
params = {
"transaction_type_ids" => transaction_type.id,
"housing_benefit_amount" => "£1,543.66",
"housing_benefit_frequency" => "weekly",
legal_aid_application:,
}
form = described_class.new(params)
form.save

expect(legal_aid_application.regular_transactions.first).to have_attributes(amount: 1_543.66)
end

context "when a housing benefit regular transaction already exists" do
it "does not create another legal aid application transaction type" do
legal_aid_application = create(:legal_aid_application, :with_applicant)
Expand Down
15 changes: 15 additions & 0 deletions spec/forms/providers/means/regular_income_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,21 @@
outgoing_cash_transaction,
)
end

it "cleans the regular transaction amount of humanized characters" do
legal_aid_application = create(:legal_aid_application, :with_applicant)
pension = create(:transaction_type, :pension)
params = {
"transaction_type_ids" => ["", pension.id],
"pension_amount" => "£2,333.66",
"pension_frequency" => "monthly",
}.merge(legal_aid_application:)

form = described_class.new(params)
form.save

expect(legal_aid_application.regular_transactions.first).to have_attributes(amount: 2_333.66)
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/forms/providers/means/regular_outgoings_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@
.to contain_exactly([rent_or_mortgage.id, 250.50, "weekly"], [child_care.id, 100, "monthly"])
end

it "cleans the regular transaction amount of humanized characters" do
legal_aid_application = create(:legal_aid_application, :with_applicant)
rent_or_mortgage = create(:transaction_type, :rent_or_mortgage)
params = {
"transaction_type_ids" => ["", rent_or_mortgage.id],
"rent_or_mortgage_amount" => "£2,333.55",
"rent_or_mortgage_frequency" => "monthly",
}.merge(legal_aid_application:)

form = described_class.new(params)
form.save

expect(legal_aid_application.regular_transactions.first).to have_attributes(amount: 2_333.55)
end

it "destroys any existing housing benefit transactions if housing " \
"payments are not selected" do
legal_aid_application = create(:legal_aid_application, :with_applicant)
Expand Down
Loading

0 comments on commit 5bc98b1

Please sign in to comment.