forked from miraisolutions/SmaRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaxBenefit.R
280 lines (251 loc) · 9.69 KB
/
TaxBenefit.R
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
#' @title getTaxAmount
#'
#' @rdname getTaxAmount
#'
#' @description This function uses 2 main sources for tax data.
#' At Kanton and Gemeinde level, the source is taxburden.list.
#' At federal level, we use the official taxrate table (BundessteueTabelle) and we try to aproximate the taxable income.
#'
#' @details
#' This function assumes the following objects on the global enviornment
#' * PLZGemeinden (includes Kirchensteuer)
#' * taxburden.list
#' * BundessteueTabelle
#' * BVGcontributionratesPath, BVGcontributionrates
#' * MaxBVG, MinBVG
#' * KinderabzugKG
#' * NBU, maxNBU
#' * AHL
#' * ALV, maxALV
#' * VersicherungsL, VersicherungsV, VersicherungsK
#' * BerufsauslagenTarif, BerufsauslagenMax, BerufsauslagenMin
#' @seealso swisstax
#'
#' @param Income Annual salary. `Numeric` scalar.
#' @param rate_group A (Single), B (Married), C (Married Double income) `Character`.
#' @param Age Age of the person. `Numeric`
#' @param NChildren Number of children. `Numeric` scalar.
#' @param postalcode Zip code `Character`
#' @param churchtax Y/N `Character` Y/N
#'
#' @import dplyr
#'
#' @return Tax Amount
#'
#' @examples
#' \dontrun{
#' getTaxAmount(Income = 200000, rate_group = "C", Age = 32,
#' NChildren = 5, postalcode = 8400, churchtax = "Y")
#' }
#' @export
getTaxAmount <- function(Income,
rate_group,
Age,
NChildren,
postalcode,
churchtax) {
# Find Kanton and Gemeinde
Kanton <- subset(PLZGemeinden, PLZ == postalcode, select = "Kanton")[1, 1]
GDENR <- subset(PLZGemeinden, PLZ == postalcode, "GDENR")[1, 1]
GDENAME <- subset(PLZGemeinden, PLZ == postalcode, "GDENAME")[1, 1]
# Get Tarif
Tarif <- ifelse(rate_group == "C", "DOPMK",
ifelse(rate_group == "A" & NChildren == 0, "Ledig",
ifelse(rate_group == "B" & NChildren == 0, "VOK", "VMK")
)
)
DOfactor <- ifelse(Tarif == "DOPMK", 2, 1)
# Select Tarif, Gemeinde and build Income Cuts
taxburden <- filter(taxburden.list[[grep(Tarif, names(taxburden.list))]], Gemeindenummer == GDENR)
# Get taxrate vector associated to one Gemeinde
idxNumCols <- !grepl("[a-z]", colnames(taxburden))
IncomeCuts <- gsub("([0-9])\\.([0-9])", "\\1\\2", colnames(taxburden)[idxNumCols]) %>%
as.numeric()
taxrate <- taxburden[1, idxNumCols] %>% as.vector()
# Constrain Income
Income <- Income %>%
max(0) %>%
min(1e+08)
# Calc adjustIncomeKG
# 1. Age adjustment because of BVG contributions
# Tax burden based on the Pensionkassebeitrage from the examples (5%). Therefore, an adjustment factor is applied accordingly.
AjustBVGContri <- BVGcontributionratesPath %>%
filter(years == Age) %>%
transmute(AjustBVGContri = (0.05 - BVGcontributionrates) * (min(Income, MaxBVG) - MinBVG))
# 2. NChildren ajustment (only for VMK and DOPMK)
# Tax burden based on 2 kids. Therefore, an adjustment factor is applied accordingly.
if (Tarif %in% c("DOPMK", "VMK")) {
OriKinderabzugKG <- sum(KinderabzugKG[row.names(KinderabzugKG) == Kanton, 1:2])
AjustKinderabzug <- OriKinderabzugKG - sum(KinderabzugKG[row.names(KinderabzugKG) == Kanton, 1:NChildren])
} else {
AjustKinderabzug <- 0
}
# 3. NBU (not applied on taxburden source)
NBUanzug <- min(DOfactor * maxNBU, Income * NBU)
IncomeKG <- Income + AjustKinderabzug + (DOfactor * AjustBVGContri[1, 1]) - NBUanzug
TaxAmountKGC <- max(0, IncomeKG * (stats::approx(x = IncomeCuts, y = taxrate, IncomeKG)$y) / 100)
# Church affiliation
# By default, assumed church affiliation. If not, there's a discount
if (churchtax != "Y") {
TaxAmountKGC <- TaxAmountKGC * Kirchensteuer[Kirchensteuer$Kanton == Kanton, "Kirchensteuer"]
}
# Get Taxable Federal Income
TaxableIncomeFederal <- BVGcontributionratesPath %>%
filter(years == Age) %>%
mutate(
DO = ifelse(Tarif == "DOPMK", DOV, 0),
BVG = DOfactor * (BVGcontributionrates * (min(Income, MaxBVG) - MinBVG)),
AHL = Income * AHL,
ALV = min(DOfactor * maxALV, Income * ALV),
NBU = min(DOfactor * maxNBU, Income * NBU),
NetSalary = Income - BVG - AHL - ALV - NBU,
Verheiratet = ifelse(Tarif == "Ledig", 0, Verheiratet),
Versicherung = ifelse(Tarif == "Ledig", VersicherungsL, VersicherungsV + NChildren * VersicherungsK),
Beruf = max(DOfactor * BerufsauslagenMin, min(DOfactor * BerufsauslagenMax, NetSalary * BerufsauslagenTarif)),
Kids = NChildren * Kinder
) %>%
transmute(AjustSalary = NetSalary - Verheiratet - Versicherung - DO - Beruf - Kids)
TaxAmountFederal <- max(0, lookupTaxAmount(TaxableIncomeFederal, BundessteueTabelle, rate_group) - 251 * NChildren)
TaxAmount <- TaxAmountFederal + TaxAmountKGC
return(TaxAmount)
}
#' @title lookupTaxAmount
#'
#' @rdname lookupTaxAmount
#'
#' @description Search the tax amount to be paig given one income on the tax tables.
#' @seealso swisstax
#'
#' @param Income Annual stipend.
#' @param Tabelle Income - Tax rate table at Federal level.
#' @param CivilStatus Marital status.
#'
#' @return Tax amount to be paid.
#' @examples
#' \dontrun{
#' lookupTaxAmount(Income = 100000, Tabelle = BundessteueTabelle, CivilStatus = "A")
#' }
#' @export
lookupTaxAmount <- function(Income, Tabelle, CivilStatus) {
# Define column to pick
if (CivilStatus == "A") {
CivilStatusColumn <- "taxAmountSingle"
} else {
CivilStatusColumn <- "taxAmountMarried"
}
# Get closest bin
salary_bins <- Tabelle$I
nearest_salary <- salary_bins[findInterval(Income, salary_bins)]
TaxAmount <- Tabelle[Tabelle$I == nearest_salary, CivilStatusColumn]
return(TaxAmount)
}
#' @title buildTaxBenefits
#'
#' @rdname buildTaxBenefits
#'
#' @description All inputs are scalars. Builds a data frame as long as the years to retirement.
#' Calls 'getTaxAmount()' through 'calcTaxBenefitSwiss()', therefore, it assumes objects on the global enviornment.
#' @seealso swisstax
#'
#' @inheritParams getTaxAmount
#' @param RetirementAge Age of retirement.
#' @template given_bday
#' @template P2
#' @template P3
#' @template salary
#'
#' @import dplyr
#'
#' @return data.frame tax benefits path.
#' @examples
#' \dontrun{buildTaxBenefits(
#' birthday,
#' TypePurchase,
#' P2purchase,
#' P3purchase,
#' returnP3,
#' Salary,
#' SalaryGrowthRate,
#' postalcode,
#' NChildren,
#' churchtax,
#' rate_group,
#' MaxContrTax,
#' givenday = today("UTC"),
#' RetirementAge = 65)
#' }
#' @export
buildTaxBenefits <- function(birthday,
TypePurchase,
P2purchase,
P3purchase,
returnP3,
Salary,
SalaryGrowthRate,
postalcode,
NChildren,
churchtax,
rate_group,
givenday = today("UTC"),
RetirementAge) {
TaxBenefitsPath <- data.frame(Calendar = getRetirementCalendar(birthday, givenday, RetirementAge = RetirementAge))
ncp <- nrow(TaxBenefitsPath)
TaxBenefitsPath <- TaxBenefitsPath %>%
mutate(
BVGpurchase = calcBVGpurchase(TypePurchase, P2purchase, ncp),
P3purchase = c(0, rep(P3purchase, ncp - 1)),
TotalContr = BVGpurchase + P3purchase,
ExpectedSalaryPath = calcExpectedSalaryPath(Salary, SalaryGrowthRate, ncp),
TaxableIncome = pmax(ExpectedSalaryPath - pmin(TotalContr, MaxContrTax), 0),
AgePath = as.integer(sapply(Calendar, calcAge, birthday = birthday)),
TaxBenefits = calcTaxBenefitSwiss(ExpectedSalaryPath, TaxableIncome, rate_group, AgePath, NChildren, postalcode, churchtax),
t = buildt(birthday, givenday, RetirementAge = RetirementAge),
TotalTax = calcAnnuityAcumPath(TaxBenefits, t, returnP3),
ReturnTax = TotalTax - cumsum(TaxBenefits),
DirectTax = cumsum(TaxBenefits)
) %>%
select(-c(ExpectedSalaryPath, P3purchase, BVGpurchase, TaxableIncome))
return(TaxBenefitsPath)
}
#' @title calcTaxBenefitSwiss
#'
#' @rdname calcTaxBenefitSwiss
#'
#' @description Calculates the tax benefits as a difference of the taxes paid with and without retirement contributions.
#' Calls 'getTaxAmount()', therefore, it assumes objects in the global environment.
#' @seealso [getTaxAmount()]
#' @seealso swisstax
#'
#' @param ExpectedSalaryPath Vector of annual salaries until retirement.
#' @param TaxableIncome Vector of annual taxable income until retirement.
#' @inheritParams getTaxAmount
#'
#' @return Single tax benefits (tax relief) of one contribution.
#' @examples
#' \dontrun{
#' calcTaxBenefitSwiss(ExpectedSalaryPath = seq(90000, 100000, 1000),
#' TaxableIncome = seq(88000, 98000, 1000),
#' rate_group = "A",
#' Age = seq(55, 65),
#' NChildren = 0,
#' postalcode = 8400,
#' churchtax = "Y")
#' }
#' @export
calcTaxBenefitSwiss <- function(ExpectedSalaryPath,
TaxableIncome,
rate_group,
Age,
NChildren,
postalcode,
churchtax) {
assertthat::are_equal(length(ExpectedSalaryPath), length(TaxableIncome))
TaxAmountGrossIncome <- sapply(seq_along(ExpectedSalaryPath), function(i) {
getTaxAmount(ExpectedSalaryPath[i], rate_group, Age[i], NChildren, postalcode, churchtax)
})
TaxAmountTaxableIncome <- sapply(seq_along(ExpectedSalaryPath), function(i) {
getTaxAmount(TaxableIncome[i], rate_group, Age[i], NChildren, postalcode, churchtax)
})
TaxBenefits <- TaxAmountGrossIncome - TaxAmountTaxableIncome
return(TaxBenefits)
}