Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

183 lines
3.9KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/json"
  5. "errors"
  6. log "github.com/sirupsen/logrus"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. )
  12. type AiDecodeIncome struct {
  13. Id int64
  14. Input loan.Uploads
  15. ul uploadsOnDisk // internal data
  16. Mime string // mime actually detected.
  17. PayIn []loan.PayIn // generated PayIn Info
  18. DecodeIncomeDetails
  19. }
  20. type DecodeIncomeDetails struct {
  21. Funders []loan.FunderType
  22. AAA []PayInAAARow
  23. Connective_ANZ []ConnectiveRow
  24. Connective_BOC []ConnectiveRow
  25. Connective_CHLAB []ConnectiveRow
  26. Connective_HSL []ConnectiveRow
  27. Connective_ING []ConnectiveRow
  28. Connective_MEB []ConnectiveRow
  29. Connective_SGB []ConnectiveRow
  30. Connective_WPC []ConnectiveRow
  31. }
  32. type AiDecodeJson struct {
  33. Version string
  34. V1 DecodeIncomeDetails
  35. }
  36. func (m *AiDecodeIncome) decodeUploadToPayIn(ulMeta loan.Uploads, allowCachedResult bool) (e error) {
  37. m.Id = ulMeta.Id
  38. m.Input = ulMeta
  39. m.ul.Upload = ulMeta
  40. m.PayIn = make([]loan.PayIn, 0, 100) // finalized payIns, generated
  41. m.Funders = make([]loan.FunderType, 0, 50) // array of valid funders
  42. if allowCachedResult {
  43. e = m.ReadJson()
  44. if e == nil {
  45. return // already decoded
  46. } else {
  47. log.Warn("trying to read existing json failed ", e.Error())
  48. e = nil
  49. }
  50. }
  51. m.PayIn = make([]loan.PayIn, 0, 10)
  52. switch m.getFileType() {
  53. case "pdf":
  54. e = m.decodePdf()
  55. break
  56. case "excel", "opensheet":
  57. e = m.decodeXls()
  58. break
  59. default:
  60. e = errors.New("unknown format")
  61. }
  62. if e == nil {
  63. eJson := m.WriteJson()
  64. if eJson != nil {
  65. log.Error("failed to write analysis to json", eJson.Error())
  66. }
  67. }
  68. return
  69. }
  70. func (m *AiDecodeIncome) ReadJson() (e error) {
  71. if !fileExists(m.ul.jsonPath()) {
  72. return errors.New(m.ul.jsonPath() + " not found")
  73. }
  74. f, e := os.Open(m.ul.jsonPath())
  75. if e != nil {
  76. return
  77. }
  78. decoder := json.NewDecoder(f)
  79. data := AiDecodeJson{}
  80. e = decoder.Decode(&data)
  81. if e != nil {
  82. log.Error("failed load existing decode json", e.Error())
  83. return
  84. }
  85. return
  86. }
  87. func (m *AiDecodeIncome) WriteJson() (e error) {
  88. b, e := json.Marshal(m.DecodeIncomeDetails)
  89. if e != nil {
  90. return
  91. }
  92. ioutil.WriteFile(m.ul.jsonPath(), b, 0644)
  93. return
  94. }
  95. func (m *AiDecodeIncome) getFileType() (ret string) {
  96. strMime, e := GetFileContentType(m.ul.filePath())
  97. if e != nil {
  98. return
  99. }
  100. m.Mime = strMime
  101. ret, e = m.ul.GetFileType()
  102. if e != nil {
  103. ret = ""
  104. }
  105. return
  106. }
  107. func (m *AiDecodeIncome) decodePdf() (e error) {
  108. cmd := exec.Command("pdftotext", "-layout", m.ul.filePath(), "-")
  109. out, e := cmd.Output()
  110. if e != nil {
  111. log.Error("cannot convert pdf to text ", e)
  112. }
  113. raw := string(out)
  114. switch m.detectFunder(raw) {
  115. case loan.Funder_AAA:
  116. m.Funders = append(m.Funders, loan.Funder_AAA)
  117. e = m.decodeAAAPdf(raw)
  118. for _, row := range m.AAA {
  119. pi := loan.PayIn{}
  120. pi.Id = 0
  121. pi.Ts = row.Period
  122. pi.Amount = row.LoanAmount
  123. pi.Lender = loan.Funder_AAA
  124. pi.Settlement = row.Settlement
  125. pi.Balance = row.Balance
  126. pi.OffsetBalance = -1
  127. pi.IncomeAmount = row.InTrail
  128. pi.IncomeType = "Trail"
  129. m.PayIn = append(m.PayIn, pi)
  130. }
  131. log.Println("AAA final result", m.AAA)
  132. break
  133. case loan.Funder_Unknown:
  134. e = errors.New(loan.Funder_Unknown)
  135. break // not able to detect Funder
  136. }
  137. return
  138. }
  139. func (m *AiDecodeIncome) decodeXls() (e error) {
  140. e = errors.New("not implemented yet")
  141. return
  142. }
  143. func (m *AiDecodeIncome) detectFunder(raw string) loan.FunderType {
  144. if m.isAAA(raw) {
  145. return loan.Funder_AAA
  146. }
  147. return loan.Funder_Unknown
  148. }
  149. func (m *AiDecodeIncome) isAAA(raw string) bool {
  150. keyword := "AAA Financial Trail Report"
  151. lines := strings.Split(raw, "\n")
  152. return m.checkFunderKeyword(keyword, lines, 0, 3)
  153. }
  154. func (m *AiDecodeIncome) checkFunderKeyword(keyword string, lines []string, start int, end int) bool {
  155. for idx, line := range lines {
  156. // first 10 lines has Key word
  157. if strings.Contains(line, keyword) && idx >= start && idx <= 10 {
  158. return true
  159. }
  160. }
  161. return false
  162. }