Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import uk.gov.hmrc.tai.model.domain.income.IncomeSource
import uk.gov.hmrc.tai.service._
import uk.gov.hmrc.tai.service.journeyCache.JourneyCacheService
import uk.gov.hmrc.tai.service.journeyCompletion.EstimatedPayJourneyCompletionService
import uk.gov.hmrc.tai.util.FormHelper
import uk.gov.hmrc.tai.util.FutureOps._
import uk.gov.hmrc.tai.util.constants._
import uk.gov.hmrc.tai.util.constants.journeyCache._
Expand Down Expand Up @@ -175,12 +176,12 @@ class IncomeUpdateCalculatorController @Inject()(
case Right((mandatorySeq, optionalSeq)) =>
val employer = IncomeSource(id = mandatorySeq(5).toInt, name = mandatorySeq(0))
val payPeriodFrequency = mandatorySeq(1)
val totalSalaryAmount = mandatorySeq(2)
val totalSalaryAmount = FormHelper.stripNumber(mandatorySeq(2))
val hasPayslipDeductions = mandatorySeq(3)
val hasBonusPayments = mandatorySeq(4)
val hasBonusPayments = FormHelper.stripNumber(mandatorySeq(4))

val taxablePay = optionalSeq.head
val bonusPaymentAmount = optionalSeq(1)
val taxablePay = optionalSeq.head.map(FormHelper.stripNumber)
val bonusPaymentAmount = optionalSeq(1).map(FormHelper.stripNumber)
val payPeriodInDays = optionalSeq(2)

val backUrl = bonusPaymentAmount match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class IncomeUpdatePayslipAmountController @Inject()(

val payPeriod = optionalSeq.head
val payPeriodInDays = optionalSeq(1)
val totalSalary = optionalSeq(2)
val totalSalary = FormHelper.stripNumber(optionalSeq(2))

val errorMessage = "tai.payslip.error.form.totalPay.input.mandatory"

Expand Down Expand Up @@ -123,7 +123,7 @@ class IncomeUpdatePayslipAmountController @Inject()(
val incomeSource = IncomeSource(id = mandatorySeq.head.toInt, name = mandatorySeq(1))
val payPeriod = optionalSeq.head
val payPeriodInDays = optionalSeq(1)
val taxablePayKey = optionalSeq(2)
val taxablePayKey = optionalSeq(2).map(FormHelper.stripNumber)

val form = TaxablePayslipForm.createForm().fill(TaxablePayslipForm(taxablePayKey))
TaxablePaySlipAmountViewModel(form, payPeriod, payPeriodInDays, incomeSource)
Expand Down
4 changes: 2 additions & 2 deletions app/uk/gov/hmrc/tai/util/FormHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ object FormHelper {

def isCurrency(stringValue: String, isWholeNumRequired: Boolean): Boolean = {
val currencyRegex: String = "^\\£?(([1-9]\\d{0,2}(,\\d{3})*)|(([1-9]\\d*)?\\d))?"
val regex: String = if (isWholeNumRequired) currencyRegex else currencyRegex + "(\\.\\d\\d)?"
val regex: String = if (isWholeNumRequired) currencyRegex else currencyRegex + "(\\.\\d{1,2})?"

stringValue matches regex.r.toString()
stringValue.replaceAll("\\s", "") matches regex.r.toString()
}

def convertCurrencyToInt(value: Option[String]): Int =
Expand Down
16 changes: 14 additions & 2 deletions test/uk/gov/hmrc/tai/util/FormHelperSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class FormHelperSpec extends PlaySpec {
FormHelper.stripNumber(Some("£90")) mustBe Some("90")
}

"return striped number without spaces" in {
FormHelper.stripNumber(Some("£90 000")) mustBe Some("90000")
}

"return striped number without commas" in {
FormHelper.stripNumber(Some("999,999")) mustBe Some("999999")
}
Expand Down Expand Up @@ -66,33 +70,40 @@ class FormHelperSpec extends PlaySpec {
}

" allow if pound symbol is at the start of the form" in {
FormHelper.isCurrency("£99 999", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("£99,999", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("£99,999.00", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("£99,999,999.00", isWholeNumRequired = false) mustBe true
}

" allowed plain positive number with only 2 decimal places when whole Number required is false " in {
FormHelper.isCurrency("110 000", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("110000", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("110000.00", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("110000.99", isWholeNumRequired = false) mustBe true

FormHelper.isCurrency("110000.9", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("110000.9", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("1 1 0 0 0 0 . 9", isWholeNumRequired = false) mustBe true
FormHelper.isCurrency("110000.999", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("-110000", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("-110000.00", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("-110 000.00", isWholeNumRequired = false) mustBe false
}

" allow only positive whole Number when wholeNumberRequired is true " in {
FormHelper.isCurrency("110 000", isWholeNumRequired = true) mustBe true
FormHelper.isCurrency("110000", isWholeNumRequired = true) mustBe true
FormHelper.isCurrency("110000.9", isWholeNumRequired = true) mustBe false
FormHelper.isCurrency("110000.99", isWholeNumRequired = true) mustBe false
FormHelper.isCurrency("110000.999", isWholeNumRequired = true) mustBe false
FormHelper.isCurrency("-110000", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("-110000.00", isWholeNumRequired = false) mustBe false
FormHelper.isCurrency("-110 000.00", isWholeNumRequired = false) mustBe false
}

"fail if value entered is not a number" in {
FormHelper.isCurrency("99.paul", isWholeNumRequired = true) mustBe false
FormHelper.isCurrency("9 9.paul", isWholeNumRequired = true) mustBe false
FormHelper.isCurrency("9.!!", isWholeNumRequired = false) mustBe false
}
}
Expand All @@ -117,9 +128,10 @@ class FormHelperSpec extends PlaySpec {
}

"areEqual" must {
"be the same, ignoring commmas and dots" in {
"be the same, ignoring spaces, commmas and dots" in {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and dots?

FormHelper.areEqual(Some("123"), Some("123")) mustBe true
FormHelper.areEqual(Some("1,23"), Some("1,23")) mustBe true
FormHelper.areEqual(Some("1 234"), Some("1234")) mustBe true
}

"not be the same" in {
Expand Down