No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

146 líneas
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. func (m *AiDecodeIncome) decodeAAAPdf(raw string) (e error) {
  33. m.AAA = make([]PayInAAAPeriod, 0, 10)
  34. lines := strings.Split(raw, "\n")
  35. currentDecoder := PayInAAAPeriod{}
  36. state := "start"
  37. for _, l := range lines { // DFA, wow, finally it's used. after years of learning
  38. switch state {
  39. case "start":
  40. state = currentDecoder.processStart(l)
  41. if state == "LookingForPeriod" {
  42. // determine column index, if their column is changing
  43. }
  44. break
  45. case "LookingForPeriod":
  46. state = currentDecoder.processPeriod(l)
  47. if state == "LookingForRows" {
  48. currentDecoder.Period, e = currentDecoder.getPeriod(l)
  49. currentDecoder.Rows = make([]PayInAAARow, 0, 10)
  50. if e != nil {
  51. log.Warn("cannot find period", l, e)
  52. state = "LookingForPeriod"
  53. } else {
  54. m.AAA = append(m.AAA, currentDecoder)
  55. }
  56. }
  57. break
  58. case "LookingForRows", "LookingForRowsSkipCurrent":
  59. nextState, row, valid := currentDecoder.processRow(l)
  60. if valid {
  61. currentDecoder.Rows = append(currentDecoder.Rows, row)
  62. }
  63. state = nextState
  64. if nextState == "start" {
  65. currentDecoder = PayInAAAPeriod{} //renew to a empty state
  66. }
  67. break
  68. }
  69. }
  70. return
  71. }
  72. func (m *PayInAAAPeriod) processStart(line string) (nextState string) {
  73. nextState = "start"
  74. if strings.Contains(line, "Loan Number") &&
  75. strings.Contains(line, "SettDate") &&
  76. strings.Contains(line, "Balance") &&
  77. strings.Contains(line, "IntTrail$") {
  78. nextState = "LookingForPeriod"
  79. }
  80. return
  81. }
  82. func (m *PayInAAAPeriod) processPeriod(line string) (nextState string) {
  83. nextState = "LookingForPeriod"
  84. if strings.Contains(line, "Period Servicing:") {
  85. nextState = "LookingForRows"
  86. }
  87. return
  88. }
  89. // Period Servicing: Feb 2020
  90. func (m *PayInAAAPeriod) getPeriod(line string) (p time.Time, e error) {
  91. idx := strings.Index(line, ":")
  92. subStr := strings.TrimSpace(line[idx+1:])
  93. p, e = time.Parse("Jan 2006", subStr)
  94. return
  95. }
  96. func (m *PayInAAAPeriod) processRow(line string) (nextState string, row PayInAAARow, valid bool) {
  97. nextState = "LookingForRows"
  98. valid = false
  99. allParts := strings.Split(line, " ")
  100. el := make([]string, 0, 10)
  101. for _, item := range allParts {
  102. if len(item) > 0 {
  103. el = append(el, item)
  104. }
  105. }
  106. if len(el) >= 5 {
  107. row.LoanNumber = el[0]
  108. row.Settlement, _ = time.Parse("02-Jan-06", el[1])
  109. row.LoanAmount = m.currencyToFloat64(el[2])
  110. row.Balance = m.currencyToFloat64(el[3])
  111. row.InTrail = m.currencyToFloat64(el[len(el)-1]) //last element
  112. valid = true
  113. } else {
  114. if strings.Contains(line, "Total:") {
  115. nextState = "start"
  116. } else {
  117. nextState = "LookingForRowsSkipCurrent"
  118. }
  119. }
  120. return
  121. }
  122. func (m *PayInAAAPeriod) currencyToFloat64(cur string) (ret float64) {
  123. cur = strings.ReplaceAll(cur, " ", "") //remove space
  124. cur = strings.ReplaceAll(cur, "$", "") //remove $
  125. cur = strings.ReplaceAll(cur, ",", "") //remove ,
  126. ret, _ = strconv.ParseFloat(cur, 64)
  127. return ret
  128. }