Skip to content

Commit

Permalink
Merge pull request #47 from hmrc/feature/YTA-3227/API-Link_from_annua…
Browse files Browse the repository at this point in the history
…l_statement_to_payment_history_and_payment_allocations

YTA-3227 API: Link from annual statement to payment history and payme…
  • Loading branch information
cybersaurus authored Mar 9, 2018
2 parents 5befc9d + 77ef77b commit 86f308f
Show file tree
Hide file tree
Showing 29 changed files with 389 additions and 1,009 deletions.
8 changes: 1 addition & 7 deletions app/uk/gov/hmrc/epayeapi/connectors/EpayeConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,9 @@ case class EpayeConnector @Inject() (
}

def getPaymentHistory(empRef: EmpRef, taxYear: TaxYear, headers: HeaderCarrier): Future[EpayeResponse[EpayePaymentHistory]] = {
val url = s"${config.epayeBaseUrl}/epaye/${empRef.encodedValue}/api/v1/payment-history/${taxYear.asString}"

get[EpayePaymentHistory](url, headers)
}

def getPaymentHistoryWithAllocations(empRef: EmpRef, taxYear: TaxYear, headers: HeaderCarrier): Future[EpayeResponse[EpayePaymentHistoryWithAllocations]] = {
val url = s"${config.epayeBaseUrl}/epaye/${empRef.encodedValue}/api/v1/payment-history-with-allocations/${taxYear.asString}"

get[EpayePaymentHistoryWithAllocations](url, headers)
get[EpayePaymentHistory](url, headers)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package uk.gov.hmrc.epayeapi.controllers

import javax.inject.{Inject, Singleton}

import akka.stream.Materializer
import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import play.api.mvc.{Action, EssentialAction, Result}
import uk.gov.hmrc.auth.core.AuthConnector
Expand All @@ -32,7 +31,7 @@ import uk.gov.hmrc.epayeapi.models.out.PaymentHistoryJson
import scala.concurrent.ExecutionContext

@Singleton
case class GetPaymentHistoryController @Inject() (
case class GetPaymentHistoryController @Inject()(
config: EpayeApiConfig,
authConnector: AuthConnector,
epayeConnector: EpayeConnector,
Expand Down

This file was deleted.

34 changes: 31 additions & 3 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayePaymentHistory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,39 @@
package uk.gov.hmrc.epayeapi.models.in

import org.joda.time.LocalDate
import uk.gov.hmrc.epayeapi.models.TaxYear

case class EpayePaymentHistory(payments: Seq[EpayePaymentHistoryPayment])
case class EpayePaymentHistory(
taxYear: TaxYear,
payments: Seq[EpayePaymentHistoryPayment]
)

case class EpayePaymentHistoryPayment(
dateOfPayment: Option[LocalDate],
amount: BigDecimal
paymentDate: Option[LocalDate],
method: Option[String],
amount: BigDecimal,
allocatedAmount: BigDecimal,
allocations: Seq[EpayePaymentAllocation]
)

trait EpayePaymentAllocation {
def period: EpayeTaxPeriod
def amount: BigDecimal
def code: Option[EpayeCode]

lazy val taxYear: TaxYear = TaxYear(period.startingTaxYear)
lazy val taxMonth: EpayeTaxMonth = EpayeTaxMonth.fromLocalDate(period.taxFrom)
}

case class EpayeRtiPaymentAllocation(
period: EpayeTaxPeriod,
amount: BigDecimal
) extends EpayePaymentAllocation {
lazy val code: Option[EpayeCode] = None
}

case class EpayeNonRtiPaymentAllocation(
period: EpayeTaxPeriod,
amount: BigDecimal,
code: Option[EpayeCode]
) extends EpayePaymentAllocation

This file was deleted.

3 changes: 0 additions & 3 deletions app/uk/gov/hmrc/epayeapi/models/in/EpayeReads.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ trait EpayeReads {

implicit lazy val epayePaymentHistoryReads: Reads[EpayePaymentHistory] = reads[EpayePaymentHistory]
implicit lazy val epayePaymentHistoryPaymentReads: Reads[EpayePaymentHistoryPayment] = reads[EpayePaymentHistoryPayment]

implicit lazy val epayePaymentHistoryWithAllocationsReads: Reads[EpayePaymentHistoryWithAllocations] = reads[EpayePaymentHistoryWithAllocations]
implicit lazy val epayePaymentHistoryWithAllocationsPaymentReads: Reads[EpayePaymentHistoryWithAllocationsPayment] = reads[EpayePaymentHistoryWithAllocationsPayment]
implicit lazy val epayeRtiPaymentAllocationReads: Reads[EpayeRtiPaymentAllocation] = reads[EpayeRtiPaymentAllocation]
implicit lazy val epayeNonRtiPaymentAllocationReads: Reads[EpayeNonRtiPaymentAllocation] = reads[EpayeNonRtiPaymentAllocation]
implicit lazy val epayePaymentAllocationReads: Reads[EpayePaymentAllocation] = new Reads[EpayePaymentAllocation]() {
Expand Down
6 changes: 4 additions & 2 deletions app/uk/gov/hmrc/epayeapi/models/out/AnnualStatementJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ case class AnnualStatementLinksJson(
statements: Link,
self: Link,
next: Link,
previous: Link
previous: Link,
paymentHistory: Link
)

case class AnnualStatementJson(
Expand Down Expand Up @@ -151,7 +152,8 @@ object AnnualStatementJson {
statements = Link.statementsLink(empRef),
self = Link.annualStatementLink(empRef, taxYear),
next = Link.annualStatementLink(empRef, taxYear.next),
previous = Link.annualStatementLink(empRef, taxYear.previous)
previous = Link.annualStatementLink(empRef, taxYear.previous),
paymentHistory = Link.paymentHistoryLink(empRef, taxYear)
)
)

Expand Down
7 changes: 2 additions & 5 deletions app/uk/gov/hmrc/epayeapi/models/out/JsonWrites.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ trait JsonWrites {
implicit lazy val statementsLinksJsonWrites: Writes[StatementLinks] = writes[StatementLinks]

implicit lazy val paymentHistoryJsonWrites: Writes[PaymentHistoryJson] = writes[PaymentHistoryJson]
implicit lazy val paymentHistoryLinksJsonWrites: Writes[PaymentHistoryLinks] = writes[PaymentHistoryLinks]

implicit lazy val paymentHistoryWithAllocationsJsonWrites: Writes[PaymentHistoryWithAllocationsJson] = writes[PaymentHistoryWithAllocationsJson]
implicit lazy val paymentWithAllocationsJsonWrites: Writes[PaymentWithAllocations] = writes[PaymentWithAllocations]
implicit lazy val paymentJsonWrites: Writes[PaymentJson] = writes[PaymentJson]
implicit lazy val paymentAllocationJsonWrites: Writes[PaymentAllocation] = writes[PaymentAllocation]
implicit lazy val paymentHistoryWithAllocationsLinksJsonWrites: Writes[PaymentHistoryWithAllocationsLinks] = writes[PaymentHistoryWithAllocationsLinks]
implicit lazy val paymentHistoryLinksJsonWrites: Writes[PaymentHistoryLinks] = writes[PaymentHistoryLinks]
}
3 changes: 0 additions & 3 deletions app/uk/gov/hmrc/epayeapi/models/out/Link.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,4 @@ object Link {

def paymentHistoryLink(empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/payment-history/${taxYear.asString}")

def paymentHistoryWithAllocationsLink(empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/payment-history/${taxYear.asString}/allocations")
}
100 changes: 90 additions & 10 deletions app/uk/gov/hmrc/epayeapi/models/out/PaymentHistoryJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,131 @@

package uk.gov.hmrc.epayeapi.models.out

import org.joda.time.LocalDate
import play.api.Logger
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.models.TaxYear
import uk.gov.hmrc.epayeapi.models.in.EpayePaymentHistory
import uk.gov.hmrc.epayeapi.models.{TaxMonth, TaxYear}
import uk.gov.hmrc.epayeapi.models.in.{EpayePaymentAllocation, EpayePaymentHistory, EpayePaymentHistoryPayment, EpayeRtiPaymentAllocation}

case class PaymentHistoryJson (
case class PaymentHistoryJson(
taxOfficeNumber: String,
taxOfficeReference: String,
taxYear: TaxYear,
payments: Seq[Payment],
payments: Seq[PaymentJson],
_links: PaymentHistoryLinks
)

case class PaymentHistoryLinks (
case class PaymentJson(
paymentDate: LocalDate,
method: Option[String],
amount: BigDecimal,
allocatedAmount: BigDecimal,
allocations: Seq[PaymentAllocation]
)

case class PaymentAllocation(
taxYear: TaxYear,
taxMonth: TaxMonth,
amount: BigDecimal,
code: Option[String]
)

object PaymentAllocation {
def transform(epayePaymentAllocation: EpayePaymentAllocation): PaymentAllocation = {
PaymentAllocation(
epayePaymentAllocation.taxYear,
TaxMonth(epayePaymentAllocation.taxYear, epayePaymentAllocation.taxMonth.month),
epayePaymentAllocation.amount,
epayePaymentAllocation.code.map { _.name }
)
}
}

case class PaymentHistoryLinks(
empRefs: Link,
summary: Link,
statements: Link,
statement: Link,
self: Link,
next: Link,
previous: Link
)

object PaymentJson {
import uk.gov.hmrc.epayeapi.models.ImplicitOrderings.localDateDescendingOrdering

implicit val paymentDescendingOrdering = Ordering.by[PaymentJson, (LocalDate, BigDecimal)](payment => (payment.paymentDate, -payment.amount))

private lazy val paymentMethods: Map[String, String] = Map(
"TPS RECEIPTS BY DEBIT CARD" -> "Debit Card",
"PAYMENTS MADE BY CHEQUE" -> "Cheque",
"CHEQUE RECEIPTS" -> "Cheque",
"BACS RECEIPTS" -> "BACS",
"CHAPS" -> "CHAPS",
"TPS RECEIPTS BY CREDIT CARD" -> "Credit Card",
"NATIONAL DIRECT DEBIT RECEIPTS" -> "Direct Debit",
"BILLPAY/OLPG/GIROBANK" -> "Online Payment",
"BANK LODGEMENT PAYMENT" -> "Bank Lodgement",
"BANK GIRO RECEIPTS" -> "Giro Receipts",
"BANK GIRO IN CREDITS" -> "Giro Credits",
"FPS RECEIPTS" -> "FPS Receipts",
"CREDIT FOR INTERNET RECEIPTS" -> "Internet Receipts",
"GIROBANK RECEIPTS" -> "Girobank",
"GIROBANK/ POST OFFICE" -> "Post Office",
"NIL DECLARATIONS" -> "Nil Declarations",
"PAYMASTER" -> "Paymaster",
"VOLUNTARY DIRECT PAYMENTS" -> "Voluntary Payments"
) withDefault { unknown =>
Logger.warn(s"Invalid payment method: [${unknown}].")
"UNKNOWN"
}

def transform(paymentMethodMaybe: Option[String]): Option[String] = {
paymentMethodMaybe.map { paymentMethods }
}
}

object PaymentHistoryJson {
def transform(
apiBaseUrl: String,
empRef: EmpRef,
taxYear: TaxYear,
epayePaymentHistory: EpayePaymentHistory): PaymentHistoryJson = {
epayePaymentHistory: EpayePaymentHistory
): PaymentHistoryJson = {

val payments: Seq[Payment] = for {
val payments: Seq[PaymentJson] = for {
epayePayment <- epayePaymentHistory.payments
} yield Payment(epayePayment.dateOfPayment, epayePayment.amount)
epayePaymentDate <- epayePayment.paymentDate
} yield toPayment(epayePayment, epayePaymentDate)

PaymentHistoryJson(
empRef.taxOfficeNumber,
empRef.taxOfficeReference,
taxYear,
payments.sorted(Payment.paymentDescendingOrdering),
payments.sorted(PaymentJson.paymentDescendingOrdering),
PaymentHistoryLinks(
empRefs = Link.empRefsLink,
summary = Link.summaryLink(empRef),
statements = Link.statementsLink(empRef),
statement = Link.annualStatementLink(empRef, taxYear),
self = Link.paymentHistoryLink(empRef, taxYear),
next = Link.paymentHistoryLink(empRef, taxYear.next),
previous = Link.paymentHistoryLink(empRef, taxYear.previous)
)
)
}
}

private def toPayment(
epayePaymentHistoryPayment: EpayePaymentHistoryPayment,
epayePaymentDate: LocalDate
): PaymentJson = {

PaymentJson(
epayePaymentDate,
PaymentJson.transform(epayePaymentHistoryPayment.method),
epayePaymentHistoryPayment.amount,
epayePaymentHistoryPayment.allocatedAmount,
epayePaymentHistoryPayment.allocations.map { PaymentAllocation.transform }
)
}
}
Loading

0 comments on commit 86f308f

Please sign in to comment.