Skip to content

Commit

Permalink
Merge branch 'refactor-minimum-wage-smart-answers'
Browse files Browse the repository at this point in the history
Merges PR #1856.

I've made a number of changes to the minimum-wage shared logic that's used by
both am-i-getting-minimum-wage and minimum-wage-calculator-employers.

There are quite a few commits but they're all fairly small and I hope the
commit message explains the motivation.

In broad terms, these are the changes I've made:

* Remove unused code:

 * From unused `precalculate` blocks. * From unused `save_input_as` values. *
From unused custom validation where the validation is already provided by the
`money_question`. * From parsing that's already handled by questions that use
(e.g.) the `parse: Float` option.

* Move formatting to templates, e.g. use `OutcomeHelper#format_money`.

* Use calculator values in templates and remove no-longer necessary calculate
blocks.

* Use `validate` blocks instead of custom validation in calculate blocks. Add
tests where they didn't exist previously.

* Set calculator attributes in `next_node` blocks rather than `calculate`
blocks whose return values aren't used.

* Consistently use `next_node` blocks rather than the shortcuts available to
`multiple_choice` questions.

* Instantiate the calculator early on in the flow and then add each response to
it as they come in, rather than instantiating the object in one go later on in
the flow. I had to add accessors to the calculator to make this possible.
Setting attributes directly on the calculator means that I don't have to
pollute the state object in order to share variables within the flow.

* Move validation logic out of the flow and into the calculator.

* Change some tests that were asserting against state variables so that they
now assert against values on the calculator. I started out deleting the tests
that were asserting against state variables that no longer existed but then
realised that it's possible I'd be losing some useful tests. These integration
tests that now assert against the calculator should move to the calculator
tests if they're deemed useful.

__NOTE__. All but the first two questions appear twice, once for determining
minimum wage for the current period and once for a previous period.

Having now gone through the process I can see a slightly better ordering of
commits but trying to reorder the commits in this branch would be a bit painful
and probably not worth doing.
  • Loading branch information
chrisroos committed Aug 6, 2015
2 parents cb908fd + 4bb2400 commit af48943
Show file tree
Hide file tree
Showing 239 changed files with 822 additions and 615 deletions.
56 changes: 47 additions & 9 deletions lib/smart_answer/calculators/minimum_wage_calculator.rb
Expand Up @@ -3,6 +3,13 @@ class MinimumWageCalculator

attr_accessor :overtime_hours, :overtime_hourly_rate, :accommodation_cost

attr_accessor :age, :date, :pay_frequency, :basic_hours, :basic_pay, :is_apprentice

def date=(date)
@date = date
@minimum_wage_data = minimum_wage_data_for_date(@date)
end

def initialize(params = {})
@age = params[:age]
@date = (params[:date].nil? ? Date.today : params[:date])
Expand All @@ -16,6 +23,30 @@ def initialize(params = {})
@minimum_wage_data = minimum_wage_data_for_date(@date)
end

def valid_age?(age)
age > 0 && age <= 200
end

def valid_pay_frequency?(pay_frequency)
pay_frequency >= 1 && pay_frequency <= 31
end

def valid_hours_worked?(hours_worked)
hours_worked > 0 && hours_worked <= (@pay_frequency * 16)
end

def valid_overtime_hours_worked?(overtime_hours_worked)
overtime_hours_worked >= 0
end

def valid_accommodation_charge?(accommodation_charge)
accommodation_charge > 0
end

def valid_accommodation_usage?(accommodation_usage)
accommodation_usage >= 0 && accommodation_usage <= 7
end

def basic_rate
rate = @basic_pay / @basic_hours
if overtime_hours > 0 and overtime_hourly_rate > 0 and rate > overtime_hourly_rate
Expand Down Expand Up @@ -50,10 +81,6 @@ def total_overtime_pay
(@overtime_hours * overtime_hourly_rate).round(2)
end

def total_working_pay
(basic_total + total_overtime_pay).round(2)
end

def total_pay
(basic_total + total_overtime_pay + @accommodation_cost).round(2)
end
Expand Down Expand Up @@ -125,15 +152,26 @@ def minimum_wage_data_for_date(date = Date.today)
end
end

def format_money(value)
# regex strips zeros
str = sprintf("%.#{2}f", value).to_s.sub(/\.0+$/, '')
end

def free_accommodation_rate
@minimum_wage_data[:accommodation_rate]
end

def apprentice_eligible_for_minimum_wage?
date >= Date.parse('2010-10-01')
end

def under_school_leaving_age?
age < 16
end

def historically_receiving_minimum_wage?
historical_adjustment <= 0
end

def any_overtime_hours_worked?
overtime_hours > 0
end

protected

def weekly_multiplier
Expand Down
Expand Up @@ -5,7 +5,7 @@

The National Minimum Wage per hour for your age | Your actual pay
- | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %>

If you work overtime or your employer provides you with [accommodation](/national-minimum-wage-accommodation), this has been added to the calculation.

Expand Down
Expand Up @@ -5,7 +5,7 @@

The National Minimum Wage per hour for your age | Your actual pay | For each pay period your employer owes you
- | - | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %> * | £<%= total_underpayment %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %> * | <%= format_money(calculator.total_underpayment) %>

\* This figure has been rounded to the nearest penny.

Expand Down
Expand Up @@ -7,7 +7,7 @@

The National Minimum Wage per hour at the time | Your actual pay
- | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %>

If you worked overtime or your employer provided you with [accommodation](/national-minimum-wage-accommodation), this has been added to the calculation.

Expand Down
Expand Up @@ -6,7 +6,7 @@

The National Minimum Wage per hour at the time | Your actual pay | For each pay period your employer owes you
- | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %> * | £<%= total_underpayment %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %> * | <%= format_money(calculator.historical_adjustment) %>

\* This figure has been rounded to the nearest penny.

Expand Down
Expand Up @@ -5,7 +5,7 @@

The National Minimum Wage per hour for the worker’s age | The worker’s actual pay
- | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %>

If the worker works overtime or you provide them with [accommodation](/national-minimum-wage-accommodation), this has been added to the calculation.

Expand Down
Expand Up @@ -5,7 +5,7 @@

The National Minimum Wage per hour for the worker’s age | The worker’s actual pay | For each pay period you owe the worker
- | - | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %> * | £<%= total_underpayment %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %> * | <%= format_money(calculator.total_underpayment) %>

\* This figure has been rounded to the nearest penny.

Expand Down
Expand Up @@ -7,7 +7,7 @@

The National Minimum Wage per hour at the time | The worker’s actual pay
- | -
£<%= minimum_hourly_rate%> | £<%= total_hourly_rate %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %>

If the worker worked overtime or you provided [accommodation](/national-minimum-wage-accommodation), this has been added to the calculation.

Expand Down
Expand Up @@ -6,7 +6,7 @@

The National Minimum Wage per hour at the time | The worker’s actual pay | For each pay period you owe the worker
- | -
£<%= minimum_hourly_rate %> | £<%= total_hourly_rate %> * | £<%= total_underpayment %>
<%= format_money(calculator.minimum_hourly_rate) %> | <%= format_money(calculator.total_hourly_rate) %> * | <%= format_money(calculator.historical_adjustment) %>

\* This figure has been rounded to the nearest penny.

Expand Down

0 comments on commit af48943

Please sign in to comment.