-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcalculate-married-couples-allowance.rb
180 lines (153 loc) · 5.04 KB
/
calculate-married-couples-allowance.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module SmartAnswer
class CalculateMarriedCouplesAllowanceFlow < Flow
def define
content_id "e04dc5fe-9a31-4229-9de9-884dd0c0a8ce"
name 'calculate-married-couples-allowance'
status :published
satisfies_need "101007"
multiple_choice :were_you_or_your_partner_born_on_or_before_6_april_1935? do
option :yes
option :no
calculate :is_before_april_changes do
nil
end
calculate :gross_pension_contributions do
nil
end
calculate :net_pension_contributions do
nil
end
calculate :age_related_allowance_chooser do
rates = SmartAnswer::Calculators::RatesQuery.from_file('married_couples_allowance').rates
AgeRelatedAllowanceChooser.new(
personal_allowance: rates.personal_allowance,
over_65_allowance: rates.over_65_allowance,
over_75_allowance: rates.over_75_allowance
)
end
calculate :calculator do
Calculators::MarriedCouplesAllowanceCalculator.new(validate_income: false)
end
next_node do |response|
case response
when 'yes'
question :did_you_marry_or_civil_partner_before_5_december_2005?
when 'no'
outcome :sorry
end
end
end
multiple_choice :did_you_marry_or_civil_partner_before_5_december_2005? do
option :yes
option :no
calculate :income_measure do |response|
case response
when 'yes'
"husband"
when 'no'
"highest earner"
else
raise SmartAnswer::InvalidResponse
end
end
next_node do |response|
case response
when 'yes'
question :whats_the_husbands_date_of_birth?
when 'no'
question :whats_the_highest_earners_date_of_birth?
end
end
end
date_question :whats_the_husbands_date_of_birth? do
from { Date.today.end_of_year }
to { Date.parse('1 Jan 1896') }
save_input_as :birth_date
next_node do
question :whats_the_husbands_income?
end
end
date_question :whats_the_highest_earners_date_of_birth? do
to { Date.parse('1 Jan 1896') }
from { Date.today.end_of_year }
save_input_as :birth_date
next_node do
question :whats_the_highest_earners_income?
end
end
money_question :whats_the_husbands_income? do
save_input_as :income
validate { |response| response > 0 }
next_node do |response|
limit = (is_before_april_changes ? 26100.0 : 27000.0)
if response.to_f >= limit
question :paying_into_a_pension?
else
outcome :husband_done
end
end
end
money_question :whats_the_highest_earners_income? do
save_input_as :income
validate { |response| response > 0 }
next_node do |response|
limit = (is_before_april_changes ? 26100.0 : 27000.0)
if response.to_f >= limit
question :paying_into_a_pension?
else
outcome :highest_earner_done
end
end
end
multiple_choice :paying_into_a_pension? do
option :yes
option :no
next_node do |response|
case response
when 'yes'
question :how_much_expected_contributions_before_tax?
when 'no'
question :how_much_expected_gift_aided_donations?
end
end
end
money_question :how_much_expected_contributions_before_tax? do
save_input_as :gross_pension_contributions
next_node do
question :how_much_expected_contributions_with_tax_relief?
end
end
money_question :how_much_expected_contributions_with_tax_relief? do
save_input_as :net_pension_contributions
next_node do
question :how_much_expected_gift_aided_donations?
end
end
money_question :how_much_expected_gift_aided_donations? do
calculate :income do |response|
calculator.calculate_adjusted_net_income(income.to_f, (gross_pension_contributions.to_f || 0), (net_pension_contributions.to_f || 0), response)
end
next_node do
if income_measure == "husband"
outcome :husband_done
else
outcome :highest_earner_done
end
end
end
outcome :husband_done do
precalculate :allowance do
age_related_allowance = age_related_allowance_chooser.get_age_related_allowance(birth_date)
calculator.calculate_allowance(age_related_allowance, income)
end
end
outcome :highest_earner_done do
precalculate :allowance do
age_related_allowance = age_related_allowance_chooser.get_age_related_allowance(birth_date)
calculator.calculate_allowance(age_related_allowance, income)
end
end
outcome :sorry
end
end
end