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ů.

290 lines
15KB

  1. package main
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "regexp"
  5. "strings"
  6. "time"
  7. )
  8. type PepperRow struct {
  9. // Common
  10. InvoiceDate time.Time
  11. // Shared Fields for both upfront and Trailer
  12. LoanNumber string
  13. Customer string
  14. PrimaryIntroducer string
  15. NextGenNumber string
  16. Settlement time.Time
  17. NetAmount float64
  18. GST float64
  19. GrossAmount float64
  20. //Trailer Only
  21. Period time.Time
  22. Balance float64
  23. SettlementAmount float64
  24. TrailerMargin float64
  25. // Upfront only
  26. UpfrontRate float64
  27. // Derived
  28. IncomeType string
  29. IncomeAmount float64
  30. }
  31. func (m *AiDecodeIncome) isPepperXls(raw string) bool {
  32. keyword := "ABN86092110079"
  33. lines := strings.Split(raw, "\n")
  34. for i := 0; i < len(lines); i++ {
  35. lines[i] = strings.ReplaceAll(lines[i], " ", "") // remove space
  36. lines[i] = strings.ReplaceAll(lines[i], ",", "") // remove ,
  37. lines[i] = strings.ReplaceAll(lines[i], ":", "") // remove :
  38. }
  39. return m.isKeywordExist(keyword, lines)
  40. }
  41. func (m *AiDecodeIncome) decodePepperXls(raw []byte) (e error) {
  42. m.Pepper = make([]PepperRow, 0, 30)
  43. if m.decodePepperIsUpfront(string(raw)) {
  44. e = m.decodePepperXlsUpfront(raw)
  45. } else {
  46. e = m.decodePepperXlsTrailer(raw)
  47. }
  48. return
  49. }
  50. func (m *AiDecodeIncome) decodePepperIsUpfront(raw string) bool {
  51. keyword := "upfrontcommissions"
  52. lines := strings.Split(raw, "\n")
  53. for i := 0; i < len(lines); i++ {
  54. lines[i] = strings.ReplaceAll(lines[i], " ", "") //remove space
  55. lines[i] = strings.ReplaceAll(lines[i], ",", "") //remove ,
  56. lines[i] = strings.ToLower(lines[i]) //to lower case
  57. }
  58. return m.isKeywordExist(keyword, lines)
  59. }
  60. func (m *AiDecodeIncome) decodePepperXlsTrailer(raw []byte) (e error) {
  61. patternTrailer := `,([0-9]+),([ A-Za-z]+)[ ,]([ A-Za-z]+),(\d+),((?:(?:[12][0-9]|0[1-9])[/.-]02|(?:30|[12][0-9]|0[1-9])[/.-](?:0[469]|11)|(?:3[01]|[12][0-9]|0[1-9])[/.-](?:0[13578]|1[02]))[/.-][0-9]{4}),(\d+(?:\.\d{1,2})?),(\d+(?:\.\d{1,2})?),(\d+(?:\.\d{1,2})?),(\d+(?:\.\d{1,2})?),\d+\.?\d{1,2},\d+(?:\.\d{1,2})?,(\d+(?:\.\d{1,2})?),\d+(?:\.\d{1,2})?,(\d+(?:\.\d{1,2})?),\d+(?:\.\d{1,2})?,\d+(?:\.\d{1,2})?,\d+(?:\.\d{1,2})?`
  62. IncomeType := "Trail"
  63. InvoiceDate := m.decodePepperXlsGetInvoiceDate(raw)
  64. CurrentPeriod := m.decodePepperXlsGetTrailerPeriod(raw)
  65. validLine, e := regexp.Compile(patternTrailer) // error if regexp invalid
  66. if e != nil {
  67. return
  68. }
  69. matches := validLine.FindAllSubmatch(raw, -1)
  70. if matches == nil {
  71. log.Warn("pepper decode trailer found nothing no matches", IncomeType, string(raw))
  72. return
  73. }
  74. for _, v := range matches {
  75. pp := PepperRow{Period: CurrentPeriod, IncomeType: IncomeType}
  76. pp.LoanNumber = string(v[1])
  77. pp.Customer = string(v[2])
  78. pp.PrimaryIntroducer = string(v[3])
  79. pp.NextGenNumber = string(v[4])
  80. pp.Settlement, _ = time.Parse("02/01/2006", string(v[5]))
  81. pp.Balance = m.currencyToFloat64(string(v[6]))
  82. pp.SettlementAmount = m.currencyToFloat64(string(v[7]))
  83. pp.TrailerMargin = m.currencyToFloat64(string(v[8]))
  84. pp.NetAmount = m.currencyToFloat64(string(v[9]))
  85. pp.GST = m.currencyToFloat64(string(v[10]))
  86. pp.GrossAmount = m.currencyToFloat64(string(v[11]))
  87. pp.IncomeAmount = pp.NetAmount
  88. pp.InvoiceDate = InvoiceDate
  89. m.Pepper = append(m.Pepper, pp)
  90. }
  91. return
  92. }
  93. func (m *AiDecodeIncome) decodePepperXlsUpfront(raw []byte) (e error) {
  94. patternUpfront := `,(\d+),([ A-Za-z]+),([ A-Za-z]+),(\d+),((?:02[/.-](?:[12][0-9]|0[1-9])|(?:0[469]|11)[/.-](?:30|[12][0-9]|0[1-9])|(?:0[13578]|1[02])[/.-](?:3[01]|[12][0-9]|0[1-9]))[/.-][0-9]{4}),"(\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?)",(\d+(?:\.\d{1,2})?),-?\d+(?:\.\d{1,2})?,(-?\d+(?:\.\d{1,2})?),-?\d+(?:\.\d{1,2})?,(-?\d+(?:\.\d{1,2})?),-?\d+(?:\.\d{1,2})?,(-?\d+(?:\.\d{1,2})?)`
  95. IncomeType := "Upfront"
  96. InvoiceDate := m.decodePepperXlsGetInvoiceDate(raw)
  97. validLine, e := regexp.Compile(patternUpfront) // error if regexp invalid
  98. if e != nil {
  99. return
  100. }
  101. matches := validLine.FindAllSubmatch(raw, -1)
  102. if matches == nil {
  103. log.Warn("pepper decode upfront found nothing no matches", IncomeType, string(raw))
  104. return
  105. }
  106. for _, v := range matches {
  107. pp := PepperRow{IncomeType: IncomeType}
  108. pp.LoanNumber = string(v[1])
  109. pp.Customer = string(v[2])
  110. pp.PrimaryIntroducer = string(v[3])
  111. pp.NextGenNumber = string(v[4])
  112. pp.Settlement, _ = time.Parse("01/02/2006", string(v[5]))
  113. pp.Balance = m.currencyToFloat64(string(v[6]))
  114. pp.UpfrontRate = m.currencyToFloat64(string(v[7]))
  115. pp.NetAmount = m.currencyToFloat64(string(v[8]))
  116. pp.GST = m.currencyToFloat64(string(v[9]))
  117. pp.GrossAmount = m.currencyToFloat64(string(v[10]))
  118. pp.IncomeAmount = pp.NetAmount
  119. pp.InvoiceDate = InvoiceDate
  120. m.Pepper = append(m.Pepper, pp)
  121. }
  122. return
  123. }
  124. func (m *AiDecodeIncome) decodePepperXlsGetInvoiceDate(raw []byte) (ret time.Time) {
  125. invoiceDatePattern := `(?i)Date *:+ *((?:(?:[12][0-9]|0?[1-9])[/.-]0?2|(?:30|[12][0-9]|0?[1-9])[/.-](?:0?[469]|11)|(?:3[01]|[12][0-9]|0?[1-9])[/.-](?:0?[13578]|1[02]))[/.-][0-9]{4}|(?:(?:[12][0-9]|0?[1-9])-(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:30|[12][0-9]|0?[1-9])-(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)|(?:3[01]|[12][0-9]|0?[1-9])-(?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December))-[0-9]{4})`
  126. invoiceDateLine, e := regexp.Compile(invoiceDatePattern)
  127. if e != nil {
  128. return
  129. }
  130. matches := invoiceDateLine.FindSubmatch(raw)
  131. ret, _ = time.Parse("02-Jan-2006", string(matches[1]))
  132. return
  133. }
  134. func (m *AiDecodeIncome) decodePepperXlsGetTrailerPeriod(raw []byte) (ret time.Time) {
  135. period := `(?i)Trailer *Commissions[ ,]+Trail +((?:Jan|January|Feb|February|Mar|March|Apr|April|May|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)[\t ]+[0-9]{4})`
  136. periodLine, e := regexp.Compile(period)
  137. if e != nil {
  138. return
  139. }
  140. periods := periodLine.FindSubmatch(raw)
  141. if periods == nil {
  142. log.Error("pepper Trailer cannot find Period", string(raw), m)
  143. return
  144. }
  145. ret, _ = time.Parse("January 2006", string(periods[1]))
  146. return
  147. }
  148. var sample_pepper_trailer = `
  149. FORMAT XLONE REPORT,DefnSheetName=_defntmp_,,,,,,,,,,,,,,,,,,,,
  150. SET,,,,,,,,,,,,,,,,,,,,,
  151. *,,,,,,,,,,,,,,,,,,RECIPIENT CREATED TAX INVOICE,,,
  152. *,,,,,,,,,,,,,,,,,,,,,
  153. *,,,,,,,,,,,,,,,,,,Date: 15-Jul-2020,,,
  154. *,,,,,,,,,,,,,,,,,,,,,
  155. *,,,,,,,,,,,,,,,,,,,,,
  156. *,,,,,Pepper Homeloans Pty Limited ,,,,,,,,,,,,,,,,
  157. *,,,,,ABN: 86 092 110 079,,,,,,,,,,,,,,,,
  158. *,,,,,,,,,,,,,,,,,,,,,
  159. *,,,,,,,,,,,,,,,,,,,,,
  160. *,,,,,To:,Super Finance Markets,,,,,,,,,,,,,,,
  161. *,,,,,,ABN: 68 620 533 333,,,,,,,,,,,,,,,
  162. *,,,,,,"International Tower One, Suite 2304, Level 23 100 Barangaroo Avenue, ",,,,,,,,,,,,,,,
  163. *,,,,,,Barangaroo NSW 2000,,,,,,,,,,,,,,,
  164. *,,,,,,,,,,,,,,,,,,,,,
  165. *,,,,,,,,,,,,,,,,,,,,,
  166. *,,,,,Trailer Commissions,TRAIL JUNE 2020,,,,,,,,,,,,,,,
  167. *,,,,,,,,,,,,,,,,,,,,,
  168. *,,,,,,,,,,,,,,,,,,,,,
  169. *,,,,,,,,,,,,,,,,,,,,,
  170. *,Account Number,Email Address,Description,UploadDate,Loan Number,Customer,Primary Introducer,Franchisee \ Next Gen Number \ Notes,Settlement Date,Balance as at end of month,Settlement Amount,Trailer Comm Margin,Net Amount,Net Amount,GST,GST*,Gross Amount,Gross Amount,,,
  171. h.*,,,,,,,,,,,,,,,,,,,,,
  172. h.REPEAT,,,,,,,,,,,,,,,,,,,,,
  173. h.COLUMNS,,,,,,,,,,,,,,,,,,,,,
  174. h.REPEAT,,,,,,,,,,,,,,,,,,,,,
  175. *,,,,,,,,,,,,,,,,,,,,,
  176. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1239633,Benny Youbin He,LAW ANDREW,720478,25/07/2019,1497907.3,1500000,0.25,307.79,307.79,30.78,30.78,307.79,338.57,1497907.3,1500000,0.25
  177. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1244928,Yingjiao Zhang,LAW ANDREW,729657,05/02/2020,636177.21,739200,0.25,130.72,130.72,13.07,13.07,130.72,143.79,636177.21,739200,0.25
  178. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1245619,Darren Julian Cantrill,LAW ANDREW,730570,03/03/2020,650690.27,656900,0.25,133.7,133.7,13.37,13.37,133.7,147.07,650690.27,656900,0.25
  179. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1246270,Khanh Huyen Nguyen Cung,LAW ANDREW,731134,30/03/2020,344808.41,423000,0.25,70.85,70.85,7.09,7.09,70.85,77.94,344808.41,423000,0.25
  180. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247301,Phuong Moc Luu,LAW ANDREW,731481,07/05/2020,1198547.28,1200000,0.25,246.28,246.28,24.63,24.63,246.28,270.91,1198547.28,1200000,0.25
  181. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1246232,Monica Cao,LAW ANDREW,731538,25/03/2020,647250.56,650000,0.25,133,133,13.3,13.3,133,146.3,647250.56,650000,0.25
  182. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1245878,Teresa Thach Thao Le Nguyen,LAW ANDREW,731751,06/03/2020,256162.15,260000,0.25,52.64,52.64,5.26,5.26,52.64,57.9,256162.15,260000,0.25
  183. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1245983,Nuam Thiha,LAW ANDREW,731906,13/03/2020,597299.23,600000,0.25,122.73,122.73,12.27,12.27,122.73,135,597299.23,600000,0.25
  184. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247828,Diep Thi Ngoc Truong,LAW ANDREW,731951,16/06/2020,520000,520000,0.25,106.85,106.85,10.69,10.69,106.85,117.54,520000,520000,0.25
  185. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1246826,Thi Kim Lan Vo,CHEN LIN,732745,05/05/2020,449125.63,450000,0.25,92.29,92.29,9.23,9.23,92.29,101.52,449125.63,450000,0.25
  186. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247293,Vu Thuy Dung Phan,CHEN LIN,733940,04/05/2020,487267.77,488000,0.25,100.12,100.12,10.01,10.01,100.12,110.13,487267.77,488000,0.25
  187. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247314,Ngoc Dung Tran,CHEN LIN,734192,08/05/2020,612228.65,613000,0.25,125.8,125.8,12.58,12.58,125.8,138.38,612228.65,613000,0.25
  188. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247541,Trung Thuy Bui,CHEN LIN,734279,18/05/2020,361345.39,624000,0.25,74.25,74.25,7.43,7.43,74.25,81.68,361345.39,624000,0.25
  189. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247774,Kim Son Dang,CHEN LIN,734307,03/06/2020,364000,364000,0.25,74.79,74.79,7.48,7.48,74.79,82.27,364000,364000,0.25
  190. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247775,Kim Son Dang,CHEN LIN,734308,03/06/2020,616000,616000,0.25,126.57,126.57,12.66,12.66,126.57,139.23,616000,616000,0.25
  191. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247677,Helen Nguyen,CHEN LIN,734315,26/05/2020,417034.11,417506.4,0.25,85.69,85.69,8.57,8.57,85.69,94.26,417034.11,417506.4,0.25
  192. LIST,1002621011000000000000,,PHL Trail Commissio LenLMOgGG NA Not Applicable Cost of Sales,07/15/2020,1247977,Trieu Hong Nhung Nguyen,CHEN LIN,734403,23/06/2020,416000,416000,0.25,85.48,85.48,8.55,8.55,85.48,94.03,416000,416000,0.25
  193. *,,,,,,,,,,,,,,,,,,,,,
  194. *,,,,,,,,,,,,,,,,,,,,,
  195. h.REPEAT END,,,,,,,,,,,,,,,,,,,,,
  196. h.*,,,,,,,,,,,,,,,,,,,,,
  197. h.*,,,,,,,,,,,,,,,,,,,,,
  198. h.REPEAT END,,,,,,,,,,,,,,,,,,,,,
  199. *,,,,,,,,,,,,,,,,,,,,,
  200. *,,,,,,,,,,,,,2069.55,2069.55,206.97,206.97,2069.55,2276.52,,,
  201. *,,,,,,,,,,,,,,,,,,,,,
  202. *,,,,,* The GST shown above is payable by the Supplier,,,,,,,,,,,,,,,,
  203. *,,,,,,,,,,,,,,,,,,,,,
  204. *,,,,,,,,,,,,,,,,,,,,,
  205. *,,,,,,,,,,,,,,,,,,,,,
  206. *,,,,,,,,,,,,,,,,,,,,,
  207. *,,,,,Remittance Advice:,,,,,,,,,,,,,,,,
  208. *,,,,,,,,,,,,,,,,,,,,,
  209. *,,,,,Payment has been directly credited to your bank account as follows:,,,,,,,,,,,,,,,,
  210. *,,,,,,,,,,,,,,,,,,,,,
  211. *,,,,,Account Name:,Super Finance Markets,,,,,,,,,,,,,,,
  212. *,,,,,Bank:,&COLUMNDEFN1.F1CHA_BANKNAME,,,,,,,,,,,,,,,
  213. *,,,,,BSB:,082-401,,,,,,,,,,,,,,,
  214. *,,,,,Account Number:,267602721,,,,,,,,,,,,,,,
  215. *,,,,,Amount:,2276.52,,,,,,,,,,,,,,,
  216. *,,,,,,,,,,,,,,,,,,,,,
  217. *,,,,,,,,,,,,,,,,,,,,,
  218. *,,,,,,,,,,,,,,,,,,,,,
  219. *,,,,,,,,,,,,,,,,,,,,,
  220. `
  221. var sample_pepper_upfront = `
  222. FORMAT XLONE REPORT,DefnSheetName=_defntmp_,,,,,,,,,,,,,,,,
  223. SET,,,,,,,,,,,,,,,,,
  224. *,,,,,,,,,,,,,,,,,RECIPIENT CREATED TAX INVOICE
  225. *,,,,,,,,,,,,,,,,,
  226. *,,,,,,,,,,,,,,,,,Date: 30-Jun-2020
  227. *,,,,,,,,,,,,,,,,,
  228. *,,,,,,,,,,,,,,,,,
  229. *,,,,,Pepper Homeloans Pty Limited,,,,,,,,,,,,
  230. *,,,,,ABN: 86 092 110 079,,,,,,,,,,,,
  231. *,,,,,,,,,,,,,,,,,
  232. *,,,,,,,,,,,,,,,,,
  233. *,,,,,To:,Super Finance Markets,,,,,,,,,,,
  234. *,,,,,,ABN: 68 620 533 333,,,,,,,,,,,
  235. *,,,,,,"International Tower One, Suite 2304, Level 23 100 Barangaroo Avenue, ",,,,,,,,,,,
  236. *,,,,,,Barangaroo NSW 2000,,,,,,,,,,,
  237. *,,,,,,,,,,,,,,,,,
  238. *,,,,,,,,,,,,,,,,,
  239. *,,,,,Upfront Commissions,,,,,,,,,,,,
  240. *,,,,,,,,,,,,,,,,,
  241. *,,,,,,,,,,,,,,,,,
  242. *,,,,,,,,,,,,,,,,,
  243. *,Account Number,Email,Description,UploadDate,Loan Number,Customer,Primary Introducer,Notes\Next Gen Number,Settlement Date,Settlement Amount less Offset & Redraw,Upfront Rate,Net Amount,Net Amount,GST,GST*,Gross Amount,Gross Amount
  244. *,,,,,,,,,,,,,,,,,
  245. LIST,SUFMARCOM,andrew@supercredit.com.au,Super Finance Markets,06/30/2020,1247977,Trieu Hong Nhung Nguyen,CHEN LIN,734403,06/22/2020,"416,000.00",1.00,-4160,4160,-416,416,-4576,4576
  246. *,,,,,,,,,,,,,,,,,
  247. *,,,,,,,,,,,,-4160,4160,-416,416,-4576,4576
  248. *,,,,,,,,,,,,,,,,,
  249. *,,,,,* The GST shown above is payable by the Supplier,,,,,,,,,,,,
  250. *,,,,,,,,,,,,,,,,,
  251. *,,,,,,,,,,,,,,,,,
  252. *,,,,,,,,,,,,,,,,,
  253. *,,,,,,,,,,,,,,,,,
  254. *,,,,,Remittance Advice:,,,,,,,,,,,,
  255. *,,,,,,,,,,,,,,,,,
  256. *,,,,,Payment has been directly credited to your bank account as follows:,,,,,,,,,,,,
  257. *,,,,,,,,,,,,,,,,,
  258. *,,,,,Account Name:,Super Finance Markets,,,,,,,,,,,
  259. *,,,,,BSB:,082-401,,,,,,,,,,,
  260. *,,,,,Account Number:,267602721,,,,,,,,,,,
  261. *,,,,,Amount:,4576,,,,,,,,,,,
  262. *,,,,,,,,,,,,,,,,,
  263. *,,,,,,,,,,,,,,,,,
  264. `