Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

147 lines
3.8KB

  1. package main
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. /* Sample Text
  9. AAA Financial Trail Report
  10. Super Finance Markets Pty Ltd
  11. Loan Number SettDate Loan Balance Arrears DisDate IntTrail$ Comments
  12. Facility
  13. Columbus
  14. Period Servicing: Feb 2020
  15. 400053440 02-Sep-19 $552,463 552,579.52 $32.19
  16. 400063271 19-Feb-20 $832,000 832,000.00 $0.00
  17. Columbus Total: $32.19
  18. Grand Total: $32.19
  19. Super Finance Markets Pty Ltd
  20. */
  21. type PayInAAARow struct {
  22. LoanNumber string
  23. Settlement time.Time
  24. LoanAmount float64
  25. Balance float64
  26. InTrail float64
  27. }
  28. type PayInAAAPeriod struct {
  29. Period time.Time
  30. Rows []PayInAAARow
  31. }
  32. type PayInAAAData struct {
  33. Periods []PayInAAAPeriod
  34. }
  35. func (m *PayInAAAData) decodeAAAPdf(raw string) (e error) {
  36. m.Periods = make([]PayInAAAPeriod, 0, 10)
  37. lines := strings.Split(raw, "\n")
  38. currentPeriod := -1
  39. state := "start"
  40. for _, l := range lines { // DFA, wow, finally it's used. after years of learning
  41. switch state {
  42. case "start":
  43. state = m.processStart(l)
  44. if state == "LookingForPeriod" {
  45. // determine column index, if their column is changing
  46. }
  47. break
  48. case "LookingForPeriod":
  49. state = m.processPeriod(l)
  50. if state == "LookingForRows" {
  51. batch := PayInAAAPeriod{}
  52. m.Periods = append(m.Periods, batch)
  53. currentPeriod++ //move index to next , or 0 for a start
  54. m.Periods[currentPeriod].Period, e = m.getPeriod(l)
  55. m.Periods[currentPeriod].Rows = make([]PayInAAARow, 0, 10)
  56. if e != nil {
  57. log.Warn("cannot find period", l, e)
  58. state = "LookingForPeriod"
  59. }
  60. }
  61. break
  62. case "LookingForRows", "LookingForRowsSkipCurrent":
  63. nextState, row, valid := m.processRow(l)
  64. if valid {
  65. m.Periods[currentPeriod].Rows = append(m.Periods[currentPeriod].Rows, row)
  66. }
  67. state = nextState
  68. break
  69. }
  70. }
  71. return
  72. }
  73. func (m *PayInAAAData) processStart(line string) (nextState string) {
  74. nextState = "start"
  75. if strings.Contains(line, "Loan Number") &&
  76. strings.Contains(line, "SettDate") &&
  77. strings.Contains(line, "Balance") &&
  78. strings.Contains(line, "IntTrail$") {
  79. nextState = "LookingForPeriod"
  80. }
  81. return
  82. }
  83. func (m *PayInAAAData) processPeriod(line string) (nextState string) {
  84. nextState = "LookingForPeriod"
  85. if strings.Contains(line, "Period Servicing:") {
  86. nextState = "LookingForRows"
  87. }
  88. return
  89. }
  90. // Period Servicing: Feb 2020
  91. func (m *PayInAAAData) getPeriod(line string) (p time.Time, e error) {
  92. idx := strings.Index(line, ":")
  93. subStr := strings.TrimSpace(line[idx+1:])
  94. p, e = time.Parse("Jan 2006", subStr)
  95. return
  96. }
  97. func (m *PayInAAAData) processRow(line string) (nextState string, row PayInAAARow, valid bool) {
  98. nextState = "LookingForRows"
  99. valid = false
  100. allParts := strings.Split(line, " ")
  101. el := make([]string, 0, 10)
  102. for _, item := range allParts {
  103. if len(item) > 0 {
  104. el = append(el, item)
  105. }
  106. }
  107. if len(el) >= 5 {
  108. row.LoanNumber = el[0]
  109. row.Settlement, _ = time.Parse("02-Jan-06", el[1])
  110. row.LoanAmount = m.currencyToFloat64(el[2])
  111. row.Balance = m.currencyToFloat64(el[3])
  112. row.InTrail = m.currencyToFloat64(el[len(el)-1]) //last element
  113. valid = true
  114. } else {
  115. if strings.Contains(line, "Total:") {
  116. nextState = "start"
  117. } else {
  118. nextState = "LookingForRowsSkipCurrent"
  119. }
  120. }
  121. return
  122. }
  123. func (m *PayInAAAData) currencyToFloat64(cur string) (ret float64) {
  124. cur = strings.ReplaceAll(cur, " ", "") //remove space
  125. cur = strings.ReplaceAll(cur, "$", "") //remove $
  126. cur = strings.ReplaceAll(cur, ",", "") //remove ,
  127. ret, _ = strconv.ParseFloat(cur, 64)
  128. return ret
  129. }