Navigation Menu

Skip to content

Commit

Permalink
Move formatting of SSP weekly payments into outcome template
Browse files Browse the repository at this point in the history
* I removed the `#formatted_sick_pay_weekly_amounts` method from the
`SmartAnswer::Calculators::StatutorySickPayCalculator` and made the
`#weekly_payments` method public to take its place for use in the outcome
template.

* This in turn meant that I needed to change some of the calculator unit tests
to call the `#weekly_payments` method instead and to change the assertions
accordingly.

* I've used `SmartAnswer::OutcomeHelper#format_date` instead of the call to
`Date#strftime`, because we're trying to standardise on using the former. The
behaviour is unchanged.

* I would've liked to have used `SmartAnswer::OutcomeHelper#format_money` for
the weekly SSP amount, but there is a slight difference in behaviour between it
and the call to `Kernel#sprintf` in that the former doesn't display the decimal
places if there are no pence. I plan to ask the content team whether this is
intentional, so that we can standardise this as well.
  • Loading branch information
floehopper committed Nov 12, 2015
1 parent 5e5df9c commit a23e4c7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 124 deletions.
18 changes: 6 additions & 12 deletions lib/smart_answer/calculators/statutory_sick_pay_calculator.rb
Expand Up @@ -200,12 +200,6 @@ def days_that_can_be_paid_for_this_period
[max_days_that_can_be_paid - days_paid_in_linked_period, 0].max
end

def formatted_sick_pay_weekly_amounts
weekly_payments.map { |week|
[week.first.strftime("%e %B %Y"), sprintf("£%.2f", week.second)].join("|")
}.join("\n")
end

def ssp_payment
amount = BigDecimal.new(weekly_payments.map(&:last).sum.round(10).to_s).round(2, BigDecimal::ROUND_UP).to_f
Money.new(amount)
Expand Down Expand Up @@ -249,6 +243,12 @@ def self.dates_matching_pattern(from:, to:, pattern:)
matching_dates
end

def weekly_payments
payments = sick_pay_weekly_dates.map { |date| [date, weekly_payment(date)] }
payments.pop while payments.any? and payments.last.last == 0
payments
end

private

def weekly_rate_on(date)
Expand All @@ -272,12 +272,6 @@ def sick_pay_weekly_dates
(@sick_start_date..ssp_week_end).select { |day| day.wday == 6 }
end

def weekly_payments
payments = sick_pay_weekly_dates.map { |date| [date, weekly_payment(date)] }
payments.pop while payments.any? and payments.last.last == 0
payments
end

def weekly_payment(week_start_date)
pay = 0.0
((week_start_date - 6)..week_start_date).each do |date|
Expand Down
8 changes: 0 additions & 8 deletions lib/smart_answer_flows/calculate-statutory-sick-pay.rb
Expand Up @@ -357,14 +357,6 @@ def define
precalculate :normal_workdays_out do calculator.normal_workdays end
precalculate :pattern_days do calculator.pattern_days end
precalculate :pattern_days_total do calculator.pattern_days_total end

precalculate :formatted_sick_pay_weekly_amounts do
if calculator.ssp_payment > 0
calculator.formatted_sick_pay_weekly_amounts
else
""
end
end
end

# Answer 7
Expand Down
Expand Up @@ -10,7 +10,9 @@

Week ending | SSP amount
-|-
<%= formatted_sick_pay_weekly_amounts %>
<% calculator.weekly_payments.each do |week_ending, ssp_amount| %>
<%= format_date(week_ending) %>|<%= sprintf("£%.2f", ssp_amount) %>
<% end %>
| **Total SSP: <%= format_money(ssp_payment) %>**

##What you need to know
Expand Down
Expand Up @@ -375,24 +375,24 @@ class CalculateStatutorySickPayTest < ActiveSupport::TestCase
add_response "3,6"

assert_current_node :entitled_to_sick_pay
assert_state_variable :formatted_sick_pay_weekly_amounts,
["12 January 201342.93",
"19 January 201385.85",
"26 January 201385.85",
" 2 February 201385.85",
" 9 February 201385.85",
"16 February 201385.85",
"23 February 201385.85",
" 2 March 201385.85",
" 9 March 201385.85",
"16 March 201385.85",
"23 March 201385.85",
"30 March 201385.85",
" 6 April 201386.28",
"13 April 201386.70",
"20 April 201386.70",
"27 April 201386.70",
" 4 May 201343.35"].join("\n")
assert_equal current_state.calculator.weekly_payments,
[[Date.parse("12 January 2013"), 42.93],
[Date.parse("19 January 2013"), 85.85],
[Date.parse("26 January 2013"), 85.85],
[Date.parse(" 2 February 2013"), 85.85],
[Date.parse(" 9 February 2013"), 85.85],
[Date.parse("16 February 2013"), 85.85],
[Date.parse("23 February 2013"), 85.85],
[Date.parse(" 2 March 2013"), 85.85],
[Date.parse(" 9 March 2013"), 85.85],
[Date.parse("16 March 2013"), 85.85],
[Date.parse("23 March 2013"), 85.85],
[Date.parse("30 March 2013"), 85.85],
[Date.parse(" 6 April 2013"), 86.28],
[Date.parse("13 April 2013"), 86.70],
[Date.parse("20 April 2013"), 86.70],
[Date.parse("27 April 2013"), 86.70],
[Date.parse(" 4 May 2013"), 43.35]]
end

should "have consistent rates for all weekly rates that are produced" do
Expand All @@ -412,24 +412,24 @@ class CalculateStatutorySickPayTest < ActiveSupport::TestCase
add_response "2,3,4"

assert_current_node :entitled_to_sick_pay
assert_state_variable :formatted_sick_pay_weekly_amounts,
["12 January 201385.85",
"19 January 201385.85",
"26 January 201385.85",
" 2 February 201385.85",
" 9 February 201385.85",
"16 February 201385.85",
"23 February 201385.85",
" 2 March 201385.85",
" 9 March 201385.85",
"16 March 201385.85",
"23 March 201385.85",
"30 March 201385.85",
" 6 April 201385.85",
"13 April 201386.70",
"20 April 201386.70",
"27 April 201386.70",
" 4 May 201386.70"].join("\n")
assert_equal current_state.calculator.weekly_payments,
[[Date.parse("12 January 2013"), 85.85],
[Date.parse("19 January 2013"), 85.85],
[Date.parse("26 January 2013"), 85.85],
[Date.parse(" 2 February 2013"), 85.85],
[Date.parse(" 9 February 2013"), 85.85],
[Date.parse("16 February 2013"), 85.85],
[Date.parse("23 February 2013"), 85.85],
[Date.parse(" 2 March 2013"), 85.85],
[Date.parse(" 9 March 2013"), 85.85],
[Date.parse("16 March 2013"), 85.85],
[Date.parse("23 March 2013"), 85.85],
[Date.parse("30 March 2013"), 85.85],
[Date.parse(" 6 April 2013"), 85.85],
[Date.parse("13 April 2013"), 86.70],
[Date.parse("20 April 2013"), 86.70],
[Date.parse("27 April 2013"), 86.70],
[Date.parse(" 4 May 2013"), 86.70]]
end

should "show formatted weekly payment amounts with adjusted 3 days start amount for ordinary SPP" do
Expand All @@ -447,24 +447,24 @@ class CalculateStatutorySickPayTest < ActiveSupport::TestCase
add_response "1,2,3,4"

assert_current_node :entitled_to_sick_pay
assert_state_variable :formatted_sick_pay_weekly_amounts,
["12 January 201321.47",
"19 January 201385.85",
"26 January 201385.85",
" 2 February 201385.85",
" 9 February 201385.85",
"16 February 201385.85",
"23 February 201385.85",
" 2 March 201385.85",
" 9 March 201385.85",
"16 March 201385.85",
"23 March 201385.85",
"30 March 201385.85",
" 6 April 201385.85",
"13 April 201386.70",
"20 April 201386.70",
"27 April 201386.70",
" 4 May 201386.70"].join("\n")
assert_equal current_state.calculator.weekly_payments,
[[Date.parse("12 January 2013"), 21.47],
[Date.parse("19 January 2013"), 85.85],
[Date.parse("26 January 2013"), 85.85],
[Date.parse(" 2 February 2013"), 85.85],
[Date.parse(" 9 February 2013"), 85.85],
[Date.parse("16 February 2013"), 85.85],
[Date.parse("23 February 2013"), 85.85],
[Date.parse(" 2 March 2013"), 85.85],
[Date.parse(" 9 March 2013"), 85.85],
[Date.parse("16 March 2013"), 85.85],
[Date.parse("23 March 2013"), 85.85],
[Date.parse("30 March 2013"), 85.85],
[Date.parse(" 6 April 2013"), 85.85],
[Date.parse("13 April 2013"), 86.70],
[Date.parse("20 April 2013"), 86.70],
[Date.parse("27 April 2013"), 86.70],
[Date.parse(" 4 May 2013"), 86.70]]

end

Expand All @@ -483,24 +483,24 @@ class CalculateStatutorySickPayTest < ActiveSupport::TestCase
add_response "1,2,3,4"

assert_current_node :entitled_to_sick_pay
assert_state_variable :formatted_sick_pay_weekly_amounts,
["12 January 201321.47",
"19 January 201385.85",
"26 January 201385.85",
" 2 February 201385.85",
" 9 February 201385.85",
"16 February 201385.85",
"23 February 201385.85",
" 2 March 201385.85",
" 9 March 201385.85",
"16 March 201385.85",
"23 March 201385.85",
"30 March 201385.85",
" 6 April 201385.85",
"13 April 201386.70",
"20 April 201386.70",
"27 April 201386.70",
" 4 May 201386.70"].join("\n")
assert_equal current_state.calculator.weekly_payments,
[[Date.parse("12 January 2013"), 21.47],
[Date.parse("19 January 2013"), 85.85],
[Date.parse("26 January 2013"), 85.85],
[Date.parse(" 2 February 2013"), 85.85],
[Date.parse(" 9 February 2013"), 85.85],
[Date.parse("16 February 2013"), 85.85],
[Date.parse("23 February 2013"), 85.85],
[Date.parse(" 2 March 2013"), 85.85],
[Date.parse(" 9 March 2013"), 85.85],
[Date.parse("16 March 2013"), 85.85],
[Date.parse("23 March 2013"), 85.85],
[Date.parse("30 March 2013"), 85.85],
[Date.parse(" 6 April 2013"), 85.85],
[Date.parse("13 April 2013"), 86.70],
[Date.parse("20 April 2013"), 86.70],
[Date.parse("27 April 2013"), 86.70],
[Date.parse(" 4 May 2013"), 86.70]]

end
end
Expand Down
31 changes: 0 additions & 31 deletions test/unit/calculators/statutory_sick_pay_calculator_test.rb
Expand Up @@ -730,37 +730,6 @@ class StatutorySickPayCalculatorTest < ActiveSupport::TestCase
end
end

context "formatted_sick_pay_weekly_amounts" do
should "produce a markdown (value) formatted string of weekly SSP dates and pay rates" do
calculator = StatutorySickPayCalculator.new(
sick_start_date: Date.parse("7 January 2013"),
sick_end_date: Date.parse("3 May 2013"),
days_of_the_week_worked: %w(2 3 4),
has_linked_sickness: true,
linked_sickness_start_date: Date.parse("Fri, 21 Sep 2012"),
linked_sickness_end_date: Date.parse("Fri, 28 Dec 2012")
)

assert_equal 42, calculator.prev_sick_days
assert_equal ["12 January 2013|£85.85",
"19 January 2013|£85.85",
"26 January 2013|£85.85",
" 2 February 2013|£85.85",
" 9 February 2013|£85.85",
"16 February 2013|£85.85",
"23 February 2013|£85.85",
" 2 March 2013|£85.85",
" 9 March 2013|£85.85",
"16 March 2013|£85.85",
"23 March 2013|£85.85",
"30 March 2013|£85.85",
" 6 April 2013|£85.85",
"13 April 2013|£86.70",
"20 April 2013|£86.70"].join("\n"),
calculator.formatted_sick_pay_weekly_amounts
end
end

context "average weekly earnings for new employees who fell sick before first payday" do
should "give the average weekly earnings" do
pay = SmartAnswer::Money.new(100)
Expand Down

0 comments on commit a23e4c7

Please sign in to comment.