|
| 1 | +package hmda.publication.reports.aggregate |
| 2 | + |
| 3 | +import java.util.Calendar |
| 4 | + |
| 5 | +import akka.NotUsed |
| 6 | +import akka.stream.scaladsl.Source |
| 7 | +import hmda.model.publication.reports.ApplicantIncomeEnum._ |
| 8 | +import hmda.model.publication.reports.{ ApplicantIncome, Disposition, MSAReport } |
| 9 | +import hmda.publication.reports._ |
| 10 | +import hmda.publication.reports.national.NationalAggregateReport |
| 11 | +import hmda.publication.reports.util.DateUtil._ |
| 12 | +import hmda.publication.reports.util.ReportUtil._ |
| 13 | +import hmda.publication.reports.util.ReportsMetaDataLookup |
| 14 | +import hmda.query.model.filing.LoanApplicationRegisterQuery |
| 15 | + |
| 16 | +import scala.concurrent.Future |
| 17 | + |
| 18 | +case class N52( |
| 19 | + year: Int, |
| 20 | + reportDate: String, |
| 21 | + applicantIncomes: List[ApplicantIncome], |
| 22 | + total: List[Disposition], |
| 23 | + table: String = N52.metaData.reportTable, |
| 24 | + description: String = N52.metaData.description |
| 25 | + ) extends NationalAggregateReport |
| 26 | + |
| 27 | +object N52 { |
| 28 | + val metaData = ReportsMetaDataLookup.values("N52") |
| 29 | + val dispositions = metaData.dispositions |
| 30 | + |
| 31 | + // Table filters: |
| 32 | + // Loan Type 1 |
| 33 | + // Property Type 1,2 |
| 34 | + // Purpose of Loan 1 |
| 35 | + def generate[ec: EC, mat: MAT, as: AS](larSource: Source[LoanApplicationRegisterQuery, NotUsed]): Future[N52] = { |
| 36 | + val lars = larSource |
| 37 | + .filter { lar => |
| 38 | + (lar.loanType == 1) && |
| 39 | + (lar.propertyType == 1 || lar.propertyType == 2) && |
| 40 | + (lar.purpose == 1) |
| 41 | + } |
| 42 | + |
| 43 | + val larsWithIncome = lars.filter(lar => lar.income != "NA") |
| 44 | + val incomeIntervals = calculateMedianIncomeIntervals(fipsCode) |
| 45 | + |
| 46 | + val larsByIncome = larsByIncomeInterval(larsWithIncome, incomeIntervals) |
| 47 | + val borrowerCharacteristicsByIncomeF = borrowerCharacteristicsByIncomeInterval(larsByIncome, dispositions) |
| 48 | + |
| 49 | + val yearF = calculateYear(larSource) |
| 50 | + val totalF = calculateDispositions(lars, dispositions) |
| 51 | + |
| 52 | + for { |
| 53 | + lars50BorrowerCharacteristics <- borrowerCharacteristicsByIncomeF(LessThan50PercentOfMSAMedian) |
| 54 | + lars50To79BorrowerCharacteristics <- borrowerCharacteristicsByIncomeF(Between50And79PercentOfMSAMedian) |
| 55 | + lars80To99BorrowerCharacteristics <- borrowerCharacteristicsByIncomeF(Between80And99PercentOfMSAMedian) |
| 56 | + lars100To120BorrowerCharacteristics <- borrowerCharacteristicsByIncomeF(Between100And119PercentOfMSAMedian) |
| 57 | + lars120BorrowerCharacteristics <- borrowerCharacteristicsByIncomeF(GreaterThan120PercentOfMSAMedian) |
| 58 | + |
| 59 | + year <- yearF |
| 60 | + total <- totalF |
| 61 | + } yield { |
| 62 | + val income50 = ApplicantIncome( |
| 63 | + LessThan50PercentOfMSAMedian, |
| 64 | + lars50BorrowerCharacteristics |
| 65 | + ) |
| 66 | + val income50To79 = ApplicantIncome( |
| 67 | + Between50And79PercentOfMSAMedian, |
| 68 | + lars50To79BorrowerCharacteristics |
| 69 | + ) |
| 70 | + val income80To99 = ApplicantIncome( |
| 71 | + Between80And99PercentOfMSAMedian, |
| 72 | + lars80To99BorrowerCharacteristics |
| 73 | + ) |
| 74 | + val income100To120 = ApplicantIncome( |
| 75 | + Between100And119PercentOfMSAMedian, |
| 76 | + lars100To120BorrowerCharacteristics |
| 77 | + ) |
| 78 | + val income120 = ApplicantIncome( |
| 79 | + GreaterThan120PercentOfMSAMedian, |
| 80 | + lars120BorrowerCharacteristics |
| 81 | + ) |
| 82 | + |
| 83 | + val applicantIncomes = List( |
| 84 | + income50, |
| 85 | + income50To79, |
| 86 | + income80To99, |
| 87 | + income100To120, |
| 88 | + income120 |
| 89 | + ) |
| 90 | + |
| 91 | + N52( |
| 92 | + year, |
| 93 | + formatDate(Calendar.getInstance().toInstant), |
| 94 | + applicantIncomes, |
| 95 | + total |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments