generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincome.py
491 lines (457 loc) · 19.6 KB
/
income.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
from app.means_test.fields import MoneyIntervalField, MoneyIntervalWidget
from app.means_test.forms import BaseMeansTestForm
from flask_babel import lazy_gettext as _
from flask import session
from app.means_test.money_interval import MoneyInterval
from app.means_test.validators import MoneyIntervalAmountRequired, ValidateIfSession
class SelfEmployedMoneyIntervalField(MoneyIntervalField):
"""Subclass of the MoneyIntervalField which supports alternate hint_text depending on if the user is self-employed"""
def __init__(self, *args, self_employed_hint_text=None, **kwargs):
super().__init__(*args, **kwargs)
self.self_employed_hint_text = self_employed_hint_text
self._hint_text = ""
@staticmethod
def get_employment_status():
is_employed = session.get_eligibility().is_employed
is_self_employed = session.get_eligibility().is_self_employed
if is_employed and is_self_employed:
return "both"
if is_self_employed:
return "self_employed"
if is_employed:
return "employed"
return "neither"
@property
def hint_text(self):
return self.self_employed_hint_text.get(self.get_employment_status(), None)
@hint_text.setter
def hint_text(self, value):
self._hint_text = value
class IncomeForm(BaseMeansTestForm):
@property
def page_title(self):
has_partner = session.get_eligibility().has_partner
employed = (
session.get_eligibility().is_employed
or session.get_eligibility().is_self_employed
)
if has_partner:
if employed:
return _("You and your partner’s income and tax")
return _("You and your partner’s money coming in")
elif employed:
return _("Your income and tax")
return _("Your money coming in")
template = "means_test/income.html"
@classmethod
def should_show(cls) -> bool:
return not session.get("eligibility").has_passported_benefits
earnings = SelfEmployedMoneyIntervalField(
_("Wages before tax"),
self_employed_hint_text={
"employed": _("For example, £32.18 per week"),
"self_employed": _("This includes any earnings from self-employment"),
"both": _("This includes all wages and any earnings from self-employment"),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("is_employed_or_self_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much you receive in wages"),
freq_message=_("Tell us how often you receive wages"),
amount_message=_("Tell us how much you receive in wages"),
),
],
exclude_intervals=["per_2week"],
)
income_tax = SelfEmployedMoneyIntervalField(
label=_("Income tax"),
self_employed_hint_text={
"employed": _("Tax paid directly out of wages"),
"self_employed": _("Any tax paid on self-employed earnings"),
"both": _(
"Tax paid directly out of wages and any tax paid on self-employed earnings"
),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("is_employed_or_self_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much income tax you pay"),
freq_message=_("Tell us how often you pay income tax"),
amount_message=_("Tell us how much income tax you pay"),
),
],
exclude_intervals=["per_2week"],
)
national_insurance = SelfEmployedMoneyIntervalField(
_("National Insurance contributions"),
self_employed_hint_text={
"employed": _("Check the payslip"),
"self_employed": _("Check the National Insurance statement"),
"both": _(
"Check the payslip or National Insurance statement if self-employed"
),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("is_employed_or_self_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much National Insurance you pay"),
freq_message=_("Tell us how often you pay National Insurance"),
amount_message=_("Tell us how much National Insurance you pay"),
),
],
exclude_intervals=["per_2week"],
)
working_tax_credit = MoneyIntervalField(
_("Working Tax Credit"),
hint_text=_(
"Extra money for people who work and have a low income, enter 0 if this doesn't apply to you"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("is_employed_or_self_employed", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the Working Tax Credit you receive, or 0 if this doesn't apply to you"
),
freq_message=_("Tell us how often you receive Working Tax Credit"),
amount_message=_("Tell us how much Working Tax Credit you receive"),
),
],
exclude_intervals=["per_2week"],
)
child_tax_credit = MoneyIntervalField(
_("Child Tax Credit"),
hint_text=_(
"The total amount you get for all your children, enter 0 if this doesn't apply to you"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("is_employed_or_self_employed", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the Child Tax Credit you receive, or 0 if this doesn't apply to you"
),
freq_message=_("Tell us how often you receive Child Tax Credit"),
amount_message=_("Tell us how much Child Tax Credit you receive"),
),
],
exclude_intervals=["per_2week"],
)
maintenance_received = MoneyIntervalField(
_("Maintenance received"),
hint_text=_(
"Payments you get from an ex-partner, or enter 0 if this doesn't apply to you"
),
widget=MoneyIntervalWidget(),
validators=[
MoneyIntervalAmountRequired(
message=_(
"Enter the total amount of maintenance you receive, or 0 if this doesn't apply to you"
),
freq_message=_("Tell us how often you receive maintenance"),
amount_message=_("Tell us how much maintenance you receive"),
)
],
exclude_intervals=["per_2week"],
)
pension = MoneyIntervalField(
_("Pension received"),
hint_text=_(
"Payments you receive if you're retired, enter 0 if this doesn't apply to you"
),
widget=MoneyIntervalWidget(),
validators=[
MoneyIntervalAmountRequired(
message=_(
"Enter the pension you receive, or 0 if this doesn't apply to you"
),
freq_message=_("Tell us how often you receive your pension"),
amount_message=_("Tell us how much pension you receive"),
)
],
exclude_intervals=["per_2week"],
)
other_income = MoneyIntervalField(
_("Any other income"),
hint_text=_(
"For example, student grants, income from trust funds, dividends, or enter 0 if this doesn't apply to you"
),
widget=MoneyIntervalWidget(),
validators=[
MoneyIntervalAmountRequired(
message=_(
"Enter the total amount of other income you receive, or 0 if this doesn't apply to you"
),
freq_message=_("Tell us how often you receive this other income"),
amount_message=_("Tell us how much other income you receive"),
)
],
exclude_intervals=["per_2week"],
)
# Partner-specific fields
partner_earnings = SelfEmployedMoneyIntervalField(
_("Wages before tax"),
self_employed_hint_text={
"employed": _("For example, £32.18 per week"),
"self_employed": _("This includes any earnings from self-employment"),
"both": _("This includes all wages and any earnings from self-employment"),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
ValidateIfSession("is_partner_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much your partner receives in wages"),
freq_message=_("Tell us how often your partner receives wages"),
amount_message=_("Tell us how much your partner receives in wages"),
),
],
exclude_intervals=["per_2week"],
)
partner_income_tax = SelfEmployedMoneyIntervalField(
label=_("Income tax"),
self_employed_hint_text={
"employed": _("Tax paid directly out of wages"),
"self_employed": _("Any tax paid on self-employed earnings"),
"both": _(
"Tax paid directly out of wages and any tax paid on self-employed earnings"
),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
ValidateIfSession("is_partner_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much income tax your partner pays"),
freq_message=_("Tell us how often your partner pays income tax"),
amount_message=_("Tell us how much income tax your partner pays"),
),
],
exclude_intervals=["per_2week"],
)
partner_national_insurance = SelfEmployedMoneyIntervalField(
_("National Insurance contributions"),
self_employed_hint_text={
"employed": _("Check the payslip"),
"self_employed": _("Check the National Insurance statement"),
"both": _(
"Check the payslip or National Insurance statement if self-employed"
),
},
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
ValidateIfSession("is_partner_employed", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much National Insurance your partner pays"),
freq_message=_(
"Tell us how often your partner pays National Insurance"
),
amount_message=_(
"Tell us how much National Insurance your partner pays"
),
),
],
exclude_intervals=["per_2week"],
)
partner_working_tax_credit = MoneyIntervalField(
_("Working Tax Credit"),
hint_text=_(
"Extra money for people who work and have a low income, enter 0 if this doesn't apply to your partner"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
ValidateIfSession("is_partner_employed", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the Working Tax Credit your partner receives, or 0 if it doesn't apply"
),
freq_message=_(
"Tell us how often your partner receives Working Tax Credit"
),
amount_message=_(
"Tell us how much Working Tax Credit your partner receives"
),
),
],
)
partner_maintenance_received = MoneyIntervalField(
_("Maintenance received"),
hint_text=_(
"Payments your partner gets from an ex-partner, or enter 0 if this doesn't apply to your partner"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the total amount of maintenance your partner receives, or 0 if this doesn't apply"
),
freq_message=_("Tell us how often your partner receives maintenance"),
amount_message=_("Tell us how much maintenance your partner receives"),
),
],
exclude_intervals=["per_2week"],
)
partner_pension = MoneyIntervalField(
_("Pension received"),
hint_text=_(
"Payments your partner receives if they're retired, enter 0 if this doesn't apply to your partner"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the pension your partner receives, or 0 if this doesn't apply"
),
freq_message=_("Tell us how often your partner receives their pension"),
amount_message=_("Tell us how much pension your partner receives"),
),
],
exclude_intervals=["per_2week"],
)
partner_other_income = MoneyIntervalField(
_("Any other income"),
hint_text=_(
"For example, student grants, income from trust funds, dividends, or enter 0 if this doesn't apply to your partner"
),
widget=MoneyIntervalWidget(),
validators=[
ValidateIfSession("has_partner", True),
MoneyIntervalAmountRequired(
message=_(
"Enter the other income your partner receives, or 0 if this doesn't apply"
),
freq_message=_(
"Tell us how often your partner receives this other income"
),
amount_message=_("Tell us how much other income your partner receives"),
),
],
exclude_intervals=["per_2week"],
)
@property
def shown_fields(self):
fields = {"self": [], "partner": []}
if (
session.get_eligibility().is_employed
or session.get_eligibility().is_self_employed
):
fields["self"].extend(
[self.earnings, self.income_tax, self.working_tax_credit]
)
fields["self"].extend(
[self.maintenance_received, self.pension, self.other_income]
)
if session.get_eligibility().has_partner:
if (
session.get_eligibility().is_partner_employed
or session.get_eligibility().is_partner_self_employed
):
fields["partner"].extend(
[
self.partner_earnings,
self.partner_income_tax,
self.partner_working_tax_credit,
]
)
fields["partner"].extend(
[
self.partner_maintenance_received,
self.partner_pension,
self.partner_other_income,
]
)
return fields
def get_payload(
self,
employed: bool | None = False,
self_employed: bool | None = False,
partner_employed: bool | None = False,
partner_self_employed: bool | None = False,
) -> dict:
"""Returns the income and deductions payload for the user and the partner.
If a field can not be found the default of MoneyField(0) will be used.
"""
# Child tax credit only applies to the client and not their partner
child_tax_credits = MoneyInterval(self.data.get("child_tax_credit", 0))
payload = {
"you": {
"income": {
"earnings": MoneyInterval(self.data.get("earnings", 0))
if not self_employed
else MoneyInterval(
0
), # If the person is self_employed their earnings are counted towards their self_employment_drawings instead
"self_employment_drawings": MoneyInterval(
self.data.get("earnings", 0)
)
if self_employed
else MoneyInterval(0),
"tax_credits": MoneyInterval(self.data.get("working_tax_credit", 0))
+ child_tax_credits,
"maintenance_received": MoneyInterval(
self.data.get("maintenance_received", 0)
),
"pension": MoneyInterval(self.data.get("pension", 0)),
"other_income": MoneyInterval(self.data.get("other_income", 0)),
"self_employed": self_employed,
},
"deductions": {
"income_tax": MoneyInterval(self.data.get("income_tax", 0)),
"national_insurance": MoneyInterval(
self.data.get("national_insurance", 0)
),
},
},
"partner": {
"income": {
"earnings": MoneyInterval(self.data.get("partner_earnings", 0))
if not partner_self_employed
else MoneyInterval(0),
# If the person is self_employed their earnings are counted towards their self_employment_drawings instead
"self_employment_drawings": MoneyInterval(
self.data.get("partner_earnings", 0)
)
if partner_self_employed
else MoneyInterval(0),
"tax_credits": MoneyInterval(
self.data.get("partner_working_tax_credit", 0)
),
"maintenance_received": MoneyInterval(
self.data.get("partner_maintenance_received", 0)
),
"pension": MoneyInterval(self.data.get("partner_pension", 0)),
"other_income": MoneyInterval(
self.data.get("partner_other_income", 0)
), # TODO: Add income from rent here?
"self_employed": partner_self_employed,
},
"deductions": {
"income_tax": MoneyInterval(self.data.get("partner_income_tax", 0)),
"national_insurance": MoneyInterval(
self.data.get("partner_national_insurance", 0)
),
},
},
}
if not employed and not self_employed:
# If the person is not employed these fields should be set to 0
payload["you"]["income"]["earnings"] = MoneyInterval(0)
payload["you"]["income"]["self_employment_drawings"] = MoneyInterval(0)
payload["you"]["income"]["tax_credits"] = MoneyInterval(0)
payload["you"]["deductions"]["income_tax"] = MoneyInterval(0)
payload["you"]["deductions"]["national_insurance"] = MoneyInterval(0)
if not partner_employed and not partner_self_employed:
# If the partner is not employed these fields should be set to 0
payload["partner"]["income"]["earnings"] = MoneyInterval(0)
payload["partner"]["income"]["self_employment_drawings"] = MoneyInterval(0)
payload["partner"]["income"]["tax_credits"] = MoneyInterval(0)
payload["partner"]["deductions"]["income_tax"] = MoneyInterval(0)
payload["partner"]["deductions"]["national_insurance"] = MoneyInterval(0)
return payload