diff --git a/app/uk/gov/hmrc/vatapi/resources/FinancialDataResource.scala b/app/uk/gov/hmrc/vatapi/resources/FinancialDataResource.scala index e322b3c1..2705efed 100644 --- a/app/uk/gov/hmrc/vatapi/resources/FinancialDataResource.scala +++ b/app/uk/gov/hmrc/vatapi/resources/FinancialDataResource.scala @@ -21,7 +21,6 @@ import play.api.Logger import play.api.libs.json.Json import play.api.mvc.{Action, AnyContent} import uk.gov.hmrc.domain.Vrn -import uk.gov.hmrc.play.microservice.controller.BaseController import uk.gov.hmrc.vatapi.audit.{AuditEvents, AuditService} import uk.gov.hmrc.vatapi.config.AppContext import uk.gov.hmrc.vatapi.connectors.FinancialDataConnector @@ -30,7 +29,7 @@ import uk.gov.hmrc.vatapi.services.AuthorisationService import scala.concurrent.ExecutionContext.Implicits.global -object FinancialDataResource extends FinancialDataResource{ +object FinancialDataResource extends FinancialDataResource { override val connector = FinancialDataConnector override val authService = AuthorisationService override val appContext = AppContext @@ -49,24 +48,26 @@ trait FinancialDataResource extends BaseResource { logger.debug(s"[FinancialDataResource][retrieveLiabilities] Retrieving Liabilities from DES") val result = fromDes { for { - response <- execute{_ => connector.getFinancialData(vrn, params)} + response <- execute { _ => connector.getFinancialData(vrn, params) } } yield response } onSuccess { response => response.filter { case 200 => response.getLiabilities(vrn) match { case Right(obj) => - val liabilities = Liabilities( - obj.liabilities.filter(_.taxPeriod.isEmpty) ++ obj.liabilities.filter(_.taxPeriod.isDefined).filterNot(_.taxPeriod.get.to isAfter params.to) + + val remainingLiabilities = obj.liabilities.filter(l => + l.`type` != "Payment on account" && (l.taxPeriod.isEmpty || l.taxPeriod.exists(l => !(l.to isAfter params.to))) ) - liabilities.liabilities match { + + remainingLiabilities match { case Seq() => logger.error(s"[FinancialDataResource][retrieveLiabilities] Retrieved liabilities from DES but exceeded the 'dateTo' query parameter range") NotFound(Json.toJson(Errors.NotFound)) case _ => logger.debug(s"[FinancialDataResource][retrieveLiabilities] Successfully retrieved Liabilities from DES") auditService.audit(AuditEvents.retrieveVatLiabilitiesAudit(response.getCorrelationId(), request.authContext.affinityGroup, getArn)) - Ok(Json.toJson(liabilities)) + Ok(Json.toJson(Liabilities(remainingLiabilities))) } case Left(ex) => logger.error(s"[FinancialDataResource][retrieveLiabilities] Error retrieving Liabilities from DES: ${ex.msg}") @@ -89,7 +90,7 @@ trait FinancialDataResource extends BaseResource { val result = fromDes { for { - response <- execute{_ => connector.getFinancialData(vrn, params)} + response <- execute { _ => connector.getFinancialData(vrn, params) } } yield response } onSuccess { response => response.filter { diff --git a/func/uk/gov/hmrc/assets/des/FinancialData.scala b/func/uk/gov/hmrc/assets/des/FinancialData.scala index 19c6ccbd..91661230 100644 --- a/func/uk/gov/hmrc/assets/des/FinancialData.scala +++ b/func/uk/gov/hmrc/assets/des/FinancialData.scala @@ -146,6 +146,107 @@ object FinancialData { | ] |}"""".stripMargin) + val multipleLiabilitiesWithPaymentOnAccount: JsValue = Json.parse( + """ + |{ + | "idType": "MTDBSA", + | "idNumber": "XQIT00000000001", + | "regimeType": "ITSA", + | "processingDate": "2017-03-07T09:30:00.000Z", + | "financialTransactions": [{ + | "chargeType": "VAT", + | "mainType": "2100", + | "periodKey": "13RL", + | "periodKeyDescription": "abcde", + | "taxPeriodFrom": "2017-01-01", + | "taxPeriodTo": "2017-04-05", + | "businessPartner": "6622334455", + | "contractAccountCategory": "02", + | "contractAccount": "D", + | "contractObjectType": "ABCD", + | "contractObject": "00000003000000002757", + | "sapDocumentNumber": "1040000872", + | "sapDocumentNumberItem": "XM00", + | "chargeReference": "XM002610011594", + | "mainTransaction": "1234", + | "subTransaction": "5678", + | "originalAmount": 463872, + | "outstandingAmount": 463872, + | "accruedInterest": 10000, + | "items": [{ + | "subItem": "001", + | "dueDate": "2017-03-08", + | "amount": 463872 + | }] + | }, + | { + | "chargeType": "Payment on account", + | "mainType": "VAT Return Charge", + | "periodKey": "15AD", + | "periodKeyDescription": "April 2017", + | "taxPeriodFrom": "2017-04-02", + | "taxPeriodTo": "2017-05-01", + | "businessPartner": "0100062914", + | "contractAccountCategory": "42", + | "contractAccount": "000917000429", + | "contractObjectType": "ZVAT", + | "contractObject": "00000018000000000104", + | "sapDocumentNumber": "003390002284", + | "sapDocumentNumberItem": "0001", + | "chargeReference": "XQ002750002150", + | "mainTransaction": "4700", + | "subTransaction": "1174", + | "originalAmount": 10.00, + | "outstandingAmount": 10.00, + | "accruedInterest": 10000, + | "items": [{ + | "subItem": "001", + | "dueDate": "2017-06-09", + | "amount": 10.00 + | }] + | }, + | { + | "chargeType": "VAT CA Charge", + | "mainType": "VAT Central Assessment", + | "periodKey": "15AA", + | "periodKeyDescription": "August 2017", + | "taxPeriodFrom": "2017-08-01", + | "taxPeriodTo": "2017-08-31", + | "businessPartner": "0100062914", + | "contractAccountCategory": "33", + | "contractAccount": "000917000429", + | "contractObjectType": "ZVAT", + | "contractObject": "00000018000000000104", + | "sapDocumentNumber": "003580002691", + | "sapDocumentNumberItem": "0001", + | "chargeReference": "XZ003100015596", + | "mainTransaction": "4720", + | "subTransaction": "1174", + | "originalAmount": 8493.38, + | "outstandingAmount": 7493.38, + | "items": [{ + | "subItem": "000", + | "dueDate": "2017-10-07", + | "amount": 8493.38, + | "clearingDate": "2017-10-07", + | "clearingReason": "01", + | "outgoingPaymentMethod": "A", + | "paymentLock": "a", + | "clearingLock": "A", + | "interestLock": "C", + | "dunningLock": "1", + | "returnFlag": true, + | "paymentReference": "a", + | "paymentMethod": "A", + | "paymentLot": "081203010024", + | "paymentLotItem": "000001", + | "clearingSAPDocument": "3350000253", + | "statisticalDocument": "A" + | }] + | } + | ] + |}"""".stripMargin) + val liabilitiesOverlapping: JsValue = Json.parse("""{ "idType": "MTDBSA", "idNumber": "XQIT00000000001", diff --git a/func/uk/gov/hmrc/support/Givens.scala b/func/uk/gov/hmrc/support/Givens.scala index bdcd8938..fdf9bd24 100644 --- a/func/uk/gov/hmrc/support/Givens.scala +++ b/func/uk/gov/hmrc/support/Givens.scala @@ -462,6 +462,19 @@ class Givens(httpVerbs: HttpVerbs) { ) givens } + + def multipleLiabilitiesWithPaymentOnAccountFor(vrn: Vrn): Givens = { + stubFor(any(urlMatching(s".*/VRN/$vrn.*")) + .willReturn( + aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBody(multipleLiabilitiesWithPaymentOnAccount.toString) + ) + ) + givens + } + def emptyLiabilitiesFor(vrn: Vrn): Givens = { stubFor(any(urlMatching(s".*/VRN/$vrn.*")) .willReturn( diff --git a/func/uk/gov/hmrc/vatapi/resources/FinancialDataResourceSpec.scala b/func/uk/gov/hmrc/vatapi/resources/FinancialDataResourceSpec.scala index dba790d3..6f0435d6 100644 --- a/func/uk/gov/hmrc/vatapi/resources/FinancialDataResourceSpec.scala +++ b/func/uk/gov/hmrc/vatapi/resources/FinancialDataResourceSpec.scala @@ -1,6 +1,7 @@ package uk.gov.hmrc.vatapi.resources import play.api.http.Status._ +import play.api.libs.json.Json import uk.gov.hmrc.assets.des.Errors import uk.gov.hmrc.support.BaseFunctionalSpec @@ -31,6 +32,42 @@ class FinancialDataResourceSpec extends BaseFunctionalSpec { .bodyIsLike(Jsons.FinancialData.oneLiability.toString) } + "retrieve a single liability where multiple liabilities exist with only one within the specific period to date - Param to date is after period to date" in { + given() + .stubAudit + .userIsFullyAuthorisedForTheResource + .des().FinancialData.singleLiabilityFor(vrn) + .when() + .get(s"/$vrn/liabilities?from=2017-01-01&to=2017-06-02") + .thenAssertThat() + .statusIs(OK) + .bodyIsLike(Jsons.FinancialData.oneLiability.toString) + } + + "retrieve a single liability where multiple liabilities exist with only one within the specific period to date - Param to date is equal to period to date " in { + given() + .stubAudit + .userIsFullyAuthorisedForTheResource + .des().FinancialData.singleLiabilityFor(vrn) + .when() + .get(s"/$vrn/liabilities?from=2017-01-01&to=2017-03-31") + .thenAssertThat() + .statusIs(OK) + .bodyIsLike(Jsons.FinancialData.oneLiability.toString) + } + + "retrieve a single liability where multiple liabilities exist with only one within the specific period to date - Param to date before period to date " in { + given() + .stubAudit + .userIsFullyAuthorisedForTheResource + .des().FinancialData.singleLiabilityFor(vrn) + .when() + .get(s"/$vrn/liabilities?from=2017-01-01&to=2017-03-30") + .thenAssertThat() + .statusIs(NOT_FOUND) + .bodyIsLike(Json.toJson(uk.gov.hmrc.vatapi.models.Errors.NotFound).toString()) + } + "retrieve a single liability where the minimum data exists" in { given() .stubAudit @@ -42,6 +79,7 @@ class FinancialDataResourceSpec extends BaseFunctionalSpec { .statusIs(OK) .bodyIsLike(Jsons.FinancialData.minLiability.toString) } + "retrieve a single liability if DES returns two liabilities and the second liability overlaps the supplied 'to' date" in { given() .stubAudit @@ -66,6 +104,18 @@ class FinancialDataResourceSpec extends BaseFunctionalSpec { .bodyIsLike(Jsons.FinancialData.multipleLiabilities.toString) } + "retrieve multiple liabilities where they exist excluding Payment on Account" in { + given() + .stubAudit + .userIsFullyAuthorisedForTheResource + .des().FinancialData.multipleLiabilitiesWithPaymentOnAccountFor(vrn) + .when() + .get(s"/$vrn/liabilities?from=2017-01-01&to=2017-12-31") + .thenAssertThat() + .statusIs(OK) + .bodyIsLike(Jsons.FinancialData.multipleLiabilitiesWithoutPaymentOnAccount.toString) + } + "return code 400 when idNumber parameter is invalid" in { given() .stubAudit diff --git a/test/uk/gov/hmrc/vatapi/resources/Jsons.scala b/test/uk/gov/hmrc/vatapi/resources/Jsons.scala index 9c019ac9..91e2cfc0 100644 --- a/test/uk/gov/hmrc/vatapi/resources/Jsons.scala +++ b/test/uk/gov/hmrc/vatapi/resources/Jsons.scala @@ -17,7 +17,7 @@ package uk.gov.hmrc.vatapi.resources import org.joda.time.LocalDate -import play.api.libs.json.{JsObject, JsValue, Json} +import play.api.libs.json.{JsValue, Json} import uk.gov.hmrc.domain.Vrn import uk.gov.hmrc.vatapi.models.{Liabilities, Liability, Payment, Payments, TaxPeriod} @@ -272,6 +272,29 @@ object Jsons { ) ) ) + lazy val multipleLiabilitiesWithoutPaymentOnAccount: JsValue = + Json.toJson( + Liabilities( + Seq( + Liability( + Some(TaxPeriod(from = LocalDate.parse("2017-01-01"), to = LocalDate.parse("2017-04-05"))), + `type` = "VAT", + originalAmount = 463872, + outstandingAmount = Some(463872), + due = Some(LocalDate.parse("2017-03-08")) + ), + Liability( + Some(TaxPeriod(from = LocalDate.parse("2017-08-01"), to = LocalDate.parse("2017-08-31"))), + `type` = "VAT CA Charge", + originalAmount = 8493.38, + outstandingAmount = Some(7493.38), + due = Some(LocalDate.parse("2017-10-07")) + ) + ) + ) + ) + + lazy val onePayment: JsValue = Json.toJson( Payments(Seq( Payment(