選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

148 行
3.9KB

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