diff --git a/app/uk/gov/hmrc/vatapi/models/VatReturn.scala b/app/uk/gov/hmrc/vatapi/models/VatReturn.scala index fa87d745..2bfdcc20 100644 --- a/app/uk/gov/hmrc/vatapi/models/VatReturn.scala +++ b/app/uk/gov/hmrc/vatapi/models/VatReturn.scala @@ -46,7 +46,7 @@ object VatReturn { (__ \ "vatDueAcquisitions").read[Amount](vatAmountValidator) and (__ \ "totalVatDue").read[Amount](vatAmountValidator) and (__ \ "vatReclaimedCurrPeriod").read[Amount](vatAmountValidator) and - (__ \ "netVatDue").read[Amount](vatAmountValidator) and + (__ \ "netVatDue").read[Amount](vatNonNegativeAmountValidator) and (__ \ "totalValueSalesExVAT").read[Amount](vatWholeAmountValidator) and (__ \ "totalValuePurchasesExVAT") .read[Amount](vatWholeAmountValidator) and @@ -67,9 +67,9 @@ object VatReturn { Validation[VatReturn]( JsPath \ "netVatDue", vatReturn => - vatReturn.netVatDue == vatReturn.totalVatDue - vatReturn.vatReclaimedCurrPeriod, + vatReturn.netVatDue == (vatReturn.totalVatDue - vatReturn.vatReclaimedCurrPeriod).abs, JsonValidationError( - "netVatDue should be equal to totalVatDue - vatReclaimedCurrPeriod", + "netVatDue should be the difference between the largest and the smallest values among totalVatDue and vatReclaimedCurrPeriod", ErrorCode.VAT_NET_VALUE) ) ) diff --git a/public/api/conf/1.0/application.raml b/public/api/conf/1.0/application.raml index 4d4003bc..810869ef 100644 --- a/public/api/conf/1.0/application.raml +++ b/public/api/conf/1.0/application.raml @@ -144,3 +144,12 @@ traits: description: Invalid request value: code: INVALID_REQUEST + invalidTotalValue: + description: totalVatDue should be equal to the sum of vatDueSales and vatDueAcquisitions + value: + code: VAT_TOTAL_VALUE + invalidNetValue: + description: netVatDue should be the difference between the largest and the smallest values among totalVatDue and vatReclaimedCurrPeriod + value: + code: VAT_NET_VALUE + diff --git a/test/uk/gov/hmrc/vatapi/models/VatReturnSpec.scala b/test/uk/gov/hmrc/vatapi/models/VatReturnSpec.scala index f7c95613..2952d68c 100644 --- a/test/uk/gov/hmrc/vatapi/models/VatReturnSpec.scala +++ b/test/uk/gov/hmrc/vatapi/models/VatReturnSpec.scala @@ -59,6 +59,25 @@ class VatReturnSpec extends UnitSpec with JsonSpec { ) } + "reject VAT returns with negative amounts where non-negative amounts are expected" in { + assertValidationErrorWithCode( + VatReturn( + periodKey = "#001", + vatDueSales = 10.00, + vatDueAcquisitions = 100.30, + totalVatDue = BigDecimal(10.00) + BigDecimal(100.30), + vatReclaimedCurrPeriod = 450.00, + netVatDue = BigDecimal(10.00) + BigDecimal(100.30) - BigDecimal(450.00), + totalValueSalesExVAT = 1000, + totalValuePurchasesExVAT = 200.00, + totalValueGoodsSuppliedExVAT = 100.00, + totalAcquisitionsExVAT = 540.00 + ), + "/netVatDue", + ErrorCode.INVALID_MONETARY_AMOUNT + ) + } + "accept VAT returns with negative amounts where non" in { assertValidationPasses( VatReturn(