From 2537007be076d9d1530f999a9564afc138f9b319 Mon Sep 17 00:00:00 2001 From: annadahmrc <177225793+annadahmrc@users.noreply.github.com> Date: Mon, 10 Feb 2025 12:20:23 +0000 Subject: [PATCH] HMA-9175 Fixed negative taxable income to default 0 when income is less than tax free amount. --- CHANGELOG.md | 1 + .../uk/gov/hmrc/calculator/Calculator.kt | 12 +++++-- .../uk/gov/hmrc/calculator/CalculatorTests.kt | 36 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9234b49..06f474e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- Fixed negative taxable income to default 0 when income is less than tax free amount. - Fixed default tax code comparison when Tapering not applied and yearly income above 100k. ## [2.14.1] - 2024-10-15Z diff --git a/src/commonMain/kotlin/uk/gov/hmrc/calculator/Calculator.kt b/src/commonMain/kotlin/uk/gov/hmrc/calculator/Calculator.kt index 9605f37..44f8a53 100644 --- a/src/commonMain/kotlin/uk/gov/hmrc/calculator/Calculator.kt +++ b/src/commonMain/kotlin/uk/gov/hmrc/calculator/Calculator.kt @@ -121,9 +121,11 @@ class Calculator @JvmOverloads constructor( val studentLoan = StudentLoanCalculation(taxYear, yearlyWages, studentLoanPlans) studentLoan.studentLoanClarification?.let { listOfClarification.add(it) } - val taxableIncome = amountToAddToWages?.let { kCodeAdjustedAmount -> - (yearlyWageAfterPension - taxFreeAmount) + kCodeAdjustedAmount - } ?: (yearlyWageAfterPension - taxFreeAmount) + val taxableIncome = ( + amountToAddToWages?.let { kCodeAdjustedAmount -> + (yearlyWageAfterPension - taxFreeAmount) + kCodeAdjustedAmount + } ?: (yearlyWageAfterPension - taxFreeAmount) + ).coerceAtLeast(ZERO) return createResponse( taxCodeType, @@ -414,4 +416,8 @@ class Calculator @JvmOverloads constructor( val method: PensionMethod, val contributionAmount: Double, ) + + private companion object { + const val ZERO = 0.0 + } } diff --git a/src/commonTest/kotlin/uk/gov/hmrc/calculator/CalculatorTests.kt b/src/commonTest/kotlin/uk/gov/hmrc/calculator/CalculatorTests.kt index a526588..2aa39e5 100644 --- a/src/commonTest/kotlin/uk/gov/hmrc/calculator/CalculatorTests.kt +++ b/src/commonTest/kotlin/uk/gov/hmrc/calculator/CalculatorTests.kt @@ -36,6 +36,38 @@ import kotlin.test.assertTrue internal class CalculatorTests { + @Test + fun `GIVEN TWENTY_TWENTY_FOUR WHEN wage is less than personal allowance THEN calculates response`() { + val result = Calculator( + taxCode = "1257L", + userSuppliedTaxCode = false, + wages = 7000.0, + payPeriod = PayPeriod.YEARLY, + taxYear = TaxYear.TWENTY_TWENTY_FOUR + ).run() + Logger.i(result.prettyPrintDataClass()) + + assertEquals(Country.ENGLAND, result.country) + assertFalse(result.isKCode) + + val yearly = result.yearly + assertEquals(PayPeriod.YEARLY, yearly.payPeriod) + assertEquals(ZERO, yearly.employeesNI) + assertEquals(ZERO, yearly.employersNI) + assertEquals(7000.00, yearly.wages) + assertEquals(12570.0, yearly.taxFree) + assertEquals(ZERO, yearly.taxToPay) + assertEquals(7000.0, yearly.takeHome) + assertEquals(ZERO, yearly.pensionContribution) + assertEquals(7000.0, yearly.wageAfterPensionDeduction) + assertEquals(ZERO, yearly.taperingAmountDeduction) + assertNull(yearly.studentLoanBreakdown) + assertEquals(ZERO, yearly.finalStudentLoanAmount) + assertEquals(ZERO, yearly.finalPostgraduateLoanAmount) + assertEquals(ZERO, yearly.otherAmount) + assertEquals(ZERO, yearly.taxableIncome) + } + @Test fun `GIVEN TWENTY_TWENTY_ONE WHEN 40000 wage THEN calculates response`() { val result = Calculator( @@ -1957,4 +1989,8 @@ internal class CalculatorTests { assertEquals(listOfExpectedResult, result.listOfClarification) } + + private companion object { + const val ZERO = 0.0 + } }