Skip to content

Commit

Permalink
HMA-3006 HMA-3007 remove 2019 and old Scottish rates (#41)
Browse files Browse the repository at this point in the history
* HMA-3006 HMA-3007 remove 2019 and old Scottish rates

* HMA-3006 remove duplicated assertion
  • Loading branch information
jvanderwee authored May 26, 2020
1 parent b5b5a5c commit 023f64c
Show file tree
Hide file tree
Showing 33 changed files with 243 additions and 2,623 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

[![Build Status](https://app.bitrise.io/app/cd7fb52c258b9273/status.svg?token=lntO8o4xz5AUEvLwVzbo3A&branch=master)](https://app.bitrise.io/app/cd7fb52c258b9273)
![LINE](https://img.shields.io/badge/line--coverage-98%25-brightgreen.svg)
![BRANCH](https://img.shields.io/badge/branch--coverage-92%25-brightgreen.svg)
![COMPLEXITY](https://img.shields.io/badge/complexity-1.54-brightgreen.svg)
![BRANCH](https://img.shields.io/badge/branch--coverage-93%25-brightgreen.svg)
![COMPLEXITY](https://img.shields.io/badge/complexity-1.51-brightgreen.svg)
[ ![Download](https://api.bintray.com/packages/hmrc/mobile-releases/tax-kalculator/images/download.svg) ](https://bintray.com/hmrc/mobile-releases/tax-kalculator/_latestVersion)

## Calculate take-home pay
Expand All @@ -18,7 +18,7 @@ val calculator = Calculator(
payPeriod = YEARLY, // Required
isPensionAge = false, // Optional (Default: false)
howManyAWeek = null, // Optional (Default: null)
taxYear = 2019 // Optional (Default: Current Tax Year)
taxYear = 2020 // Optional (Default: Current Tax Year)
)
```

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ kotlin {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit5"))
implementation("org.junit.jupiter:junit-jupiter-params:5.6.2")
}
}

Expand Down
33 changes: 11 additions & 22 deletions src/commonMain/kotlin/uk/gov/hmrc/calculator/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package uk.gov.hmrc.calculator

import com.soywiz.klock.DateTime
import kotlin.jvm.JvmOverloads
import uk.gov.hmrc.calculator.annotations.Throws
import uk.gov.hmrc.calculator.exception.InvalidHoursException
Expand Down Expand Up @@ -62,19 +61,6 @@ class Calculator @JvmOverloads constructor(
private val howManyAWeek: Double? = null,
private val taxYear: Int = TaxYear().currentTaxYear()
) {
private var currentDate: DateTime = DateTime.now()

internal constructor(
taxCode: String,
wages: Double,
payPeriod: PayPeriod,
isPensionAge: Boolean = false,
howManyAWeek: Double? = null,
taxYear: Int = TaxYear().currentTaxYear(),
currentDate: DateTime = DateTime.now()
) : this (taxCode, wages, payPeriod, isPensionAge, howManyAWeek, taxYear) {
this.currentDate = currentDate
}

private val bandBreakdown: MutableList<BandBreakdown> = mutableListOf()

Expand All @@ -89,7 +75,7 @@ class Calculator @JvmOverloads constructor(

val taxCode = this.taxCode.toTaxCode()

val taxBands = TaxBands(taxCode.country, taxYear, currentDate).bands
val taxBands = TaxBands.getBands(taxYear, taxCode.country)

val taxFreeAmount = adjustTaxBands(taxBands, taxCode)[0].upper
val amountToAddToWages = if (taxCode is KTaxCode) taxCode.amountToAddToWages else null
Expand All @@ -111,7 +97,6 @@ class Calculator @JvmOverloads constructor(
val employeesNI = employeeNIToPay(yearlyWages)
val employersNI = employerNIToPay(yearlyWages)
return CalculatorResponse(
taxCode = this.taxCode,
country = taxCode.country,
isKCode = taxCode is KTaxCode,
weekly = CalculatorResponsePayPeriod(
Expand Down Expand Up @@ -158,7 +143,7 @@ class Calculator @JvmOverloads constructor(
}

private fun taxToPay(yearlyWages: Double, taxCode: TaxCode): Double {
val taxBands = TaxBands(taxCode.country, taxYear, currentDate).bands
val taxBands = TaxBands.getBands(taxYear, taxCode.country)

return when (taxCode) {
is StandardTaxCode, is AdjustedTaxFreeTCode, is EmergencyTaxCode, is MarriageTaxCodes ->
Expand All @@ -179,7 +164,7 @@ class Calculator @JvmOverloads constructor(

private fun adjustTaxBands(taxBands: List<Band>, taxCode: TaxCode): List<Band> {
// The full tax free amount e.g. 12509
val bandAdjuster = getDefaultTaxAllowance(taxYear, taxCode.country, currentDate)
val bandAdjuster = getDefaultTaxAllowance(taxYear, taxCode.country)

taxBands[0].upper = taxCode.taxFreeAmount
taxBands[1].lower = taxCode.taxFreeAmount
Expand Down Expand Up @@ -224,12 +209,16 @@ class Calculator @JvmOverloads constructor(
private fun shouldAddBand(band: Band, percentage: Double) = band is TaxBand && percentage > 0.0

companion object {
fun getDefaultTaxCode() = "${(getDefaultTaxAllowance(TaxYear().currentTaxYear()) / 10)}L"

fun getDefaultTaxCode(): String {
val taxYear = TaxYear().currentTaxYear()
val defaultTaxAllowance = getDefaultTaxAllowance(taxYear)
return "${(defaultTaxAllowance / 10)}L"
}

internal fun getDefaultTaxAllowance(
taxYear: Int,
country: Country = ENGLAND,
currentDate: DateTime = DateTime.now()
) = TaxBands(country, taxYear, currentDate).bands[0].upper.toInt()
country: Country = ENGLAND
) = TaxBands.getBands(taxYear, country)[0].upper.toInt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class CalculatorResponsePayPeriod(
}

data class CalculatorResponse(
val taxCode: String,
val country: Country,
val isKCode: Boolean,
val weekly: CalculatorResponsePayPeriod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,8 @@ internal class EmployeeNIBands(taxYear: Int) {
EmployeeNIBand(50000.0, -1.0, 0.02)
)

private val employeeNIBands2019: List<EmployeeNIBand> = listOf(
EmployeeNIBand(0.0, 6136.00, 0.0),
EmployeeNIBand(6136.0, 8632.00, 0.0),
EmployeeNIBand(8632.0, 50000.00, 0.12),
EmployeeNIBand(50000.0, -1.0, 0.02)
)

internal val bands: List<EmployeeNIBand> = when (taxYear) {
2020 -> employeeNIBands2020
2019 -> employeeNIBands2019
else -> throw InvalidTaxYearException("$taxYear")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,8 @@ internal class EmployerNIBands(taxYear: Int) {
EmployerNIBand(50000.0, -1.0, 0.138)
)

private val employerNIBands2019: List<EmployerNIBand> = listOf(
EmployerNIBand(0.0, 6136.00, 0.0),
EmployerNIBand(6136.0, 8632.00, 0.0),
EmployerNIBand(8632.0, 50000.00, 0.138),
EmployerNIBand(50000.0, -1.0, 0.138)
)

internal val bands: List<EmployerNIBand> = when (taxYear) {
2020 -> employerNIBands2020
2019 -> employerNIBands2019
else -> throw InvalidTaxYearException("$taxYear")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,33 @@
*/
package uk.gov.hmrc.calculator.model.bands

import com.soywiz.klock.DateTime
import uk.gov.hmrc.calculator.exception.InvalidTaxYearException
import uk.gov.hmrc.calculator.model.Country
import uk.gov.hmrc.calculator.model.Country.SCOTLAND

internal class TaxBands(country: Country, taxYear: Int, currentDate: DateTime) {
internal object TaxBands {

// 2020 version 2 (release AFTER 11th May 2020)
private val scotland2020After11May: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.00, 14585.00, 0.19),
TaxBand(14585.00, 25158.00, 0.20),
TaxBand(25158.00, 43430.00, 0.21),
TaxBand(43430.00, 150000.00, 0.41),
TaxBand(150000.0, -1.0, 0.46)
)
// 2020
private val scotland2020: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.00, 14549.00, 0.19),
TaxBand(14549.00, 24944.00, 0.20),
TaxBand(24944.00, 43430.00, 0.21),
TaxBand(43430.00, 150000.00, 0.41),
TaxBand(150000.0, -1.0, 0.46)
)
private val other2020: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.0, 50000.00, 0.2),
TaxBand(50000.0, 150000.00, 0.4),
TaxBand(150000.0, -1.0, 0.45)
)

// 2019
private val scotland2019: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.00, 14549.00, 0.19),
TaxBand(14549.00, 24944.00, 0.20),
TaxBand(24944.00, 43430.00, 0.21),
TaxBand(43430.00, 150000.00, 0.41),
TaxBand(150000.0, -1.0, 0.46)
)
private val other2019: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.0, 50000.00, 0.2),
TaxBand(50000.0, 150000.00, 0.4),
TaxBand(150000.0, -1.0, 0.45)
)

private val isAfter11thMay = currentDate.month1 >= 5 && currentDate.dayOfMonth >= 11
private object Year2020 {
val scotland: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.00, 14585.00, 0.19),
TaxBand(14585.00, 25158.00, 0.20),
TaxBand(25158.00, 43430.00, 0.21),
TaxBand(43430.00, 150000.00, 0.41),
TaxBand(150000.0, -1.0, 0.46)
)
val other: List<Band> = listOf(
TaxBand(0.0, 12509.00, 0.0),
TaxBand(12509.0, 50000.00, 0.2),
TaxBand(50000.0, 150000.00, 0.4),
TaxBand(150000.0, -1.0, 0.45)
)
}

internal val bands: List<Band> = when (taxYear) {
fun getBands(taxYear: Int, country: Country) = when (taxYear) {
2020 -> when (country) {
SCOTLAND -> when (isAfter11thMay) {
true -> scotland2020After11May
false -> scotland2020
}
else -> other2020
}
2019 -> when (country) {
SCOTLAND -> scotland2019
else -> other2019
SCOTLAND -> Year2020.scotland
else -> Year2020.other
}
else -> throw InvalidTaxYearException("$taxYear")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ internal fun String.toTaxCode(): TaxCode {
Country.SCOTLAND -> formattedTaxCode.matchScottishTaxCode()
Country.WALES -> formattedTaxCode.matchWelshTaxCode()
Country.ENGLAND -> formattedTaxCode.matchEnglishTaxCode()
Country.NONE -> {
when (formattedTaxCode) {
"NT" -> NTCode()
else -> throw InvalidTaxCodeException("$this is an invalid tax code")
}
}
Country.NONE -> NTCode()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,5 @@ object HoursDaysValidator {

fun isBelowMaximumDaysPerWeek(days: Double) = days <= 7

private fun Double.isTwoDecimalPlacesOrFewer(): Boolean {
val splitByDecimalPoint = toString().split(".")
return splitByDecimalPoint.size == 2 && splitByDecimalPoint.last().length <= 2
}
internal fun Double.isTwoDecimalPlacesOrFewer() = toString().split(".").last().length <= 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package uk.gov.hmrc.calculator.utils.validation

import uk.gov.hmrc.calculator.utils.validation.HoursDaysValidator.isTwoDecimalPlacesOrFewer

object WageValidator {
fun isValidWages(wages: Double) =
isAboveMinimumWages(wages) && isBelowMaximumWages(
Expand All @@ -24,9 +26,4 @@ object WageValidator {
fun isAboveMinimumWages(wages: Double) = wages > 0

fun isBelowMaximumWages(wages: Double) = wages < 9999999.99

private fun Double.isTwoDecimalPlacesOrFewer(): Boolean {
val splitByDecimalPoint = toString().split(".")
return splitByDecimalPoint.size == 2 && splitByDecimalPoint.last().length <= 2
}
}
53 changes: 0 additions & 53 deletions src/commonTest/kotlin/uk/gov/hmrc/calculator/CalculatorTests.kt

This file was deleted.

Loading

0 comments on commit 023f64c

Please sign in to comment.