add: icbc and abc batch import

This commit is contained in:
BaoXuebin 2022-12-27 23:55:53 +08:00
parent 46ea0c1460
commit 1da322e093
3 changed files with 119 additions and 1 deletions

View File

@ -23,7 +23,7 @@
- [X] 多币种
- [X] 标签
- [X] 投资管理(FIFO)
- [X] 第三方账单导入(支付宝,微信支付)
- [X] 第三方账单导入(支付宝,微信,工商银行,农业银行)
- [X] 分期记账
- [ ] 事件

View File

@ -90,6 +90,8 @@ func RegisterRouter(router *gin.Engine) {
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
authorized.POST("/import/alipay", service.ImportAliPayCSV)
authorized.POST("/import/wx", service.ImportWxPayCSV)
authorized.POST("/import/icbc", service.ImportICBCCSV)
authorized.POST("/import/abc", service.ImportABCCSV)
authorized.GET("/ledger/check", service.CheckLedger)
authorized.DELETE("/ledger", service.DeleteLedger)
}

View File

@ -8,7 +8,9 @@ import (
"github.com/gin-gonic/gin"
"golang.org/x/text/encoding/simplifiedchinese"
"io"
"strconv"
"strings"
"time"
)
func ImportAliPayCSV(c *gin.Context) {
@ -160,3 +162,117 @@ func ImportWxPayCSV(c *gin.Context) {
OK(c, result)
}
func ImportICBCCSV(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
file, _ := c.FormFile("file")
f, _ := file.Open()
reader := csv.NewReader(bufio.NewReader(f))
result := make([]Transaction, 0)
currency := "CNY"
currencySymbol := script.GetCommoditySymbol(currency)
id := 0
for {
lines, err := reader.Read()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
script.LogError(ledgerConfig.Mail, err.Error())
}
if len(lines) >= 13 && lines[0] != "交易日期" {
incomeAmount := formatStr(lines[8])
expensesAmount := formatStr(lines[9])
account := ""
number := ""
switch {
case incomeAmount != "":
account = "Income:"
number = strings.ReplaceAll(incomeAmount, ",", "")
case expensesAmount != "":
account = "Expenses:"
number = strings.ReplaceAll(expensesAmount, ",", "")
default:
continue
}
id++
result = append(result, Transaction{
Id: strconv.Itoa(id),
Date: formatStr(lines[0]),
Payee: formatStr(lines[12]),
Narration: formatStr(lines[1]),
Number: number,
Account: account,
Currency: currency,
CurrencySymbol: currencySymbol,
})
}
}
OK(c, result)
}
func ImportABCCSV(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
file, _ := c.FormFile("file")
f, _ := file.Open()
reader := csv.NewReader(bufio.NewReader(f))
result := make([]Transaction, 0)
currency := "CNY"
currencySymbol := script.GetCommoditySymbol(currency)
id := 0
for {
lines, err := reader.Read()
if errors.Is(err, io.EOF) {
break
} else if err != nil {
script.LogError(ledgerConfig.Mail, err.Error())
}
if len(lines) >= 11 && lines[0] != "交易日期" {
amount := formatStr(lines[2])
account := ""
number := ""
switch {
case strings.HasPrefix(amount, "+"):
account = "Income:"
number = strings.ReplaceAll(amount, "+", "")
case strings.HasPrefix(amount, "-"):
account = "Expenses:"
number = strings.ReplaceAll(amount, "-", "")
default:
continue
}
id++
date, err := time.Parse("20060102", formatStr(lines[0]))
if err != nil {
continue
}
result = append(result, Transaction{
Id: strconv.Itoa(id),
Date: date.Format("2006-01-02"),
Payee: formatStr(lines[10]),
Narration: formatStr(lines[9]),
Number: number,
Account: account,
Currency: currency,
CurrencySymbol: currencySymbol,
})
}
}
OK(c, result)
}
func formatStr(str string) string {
str = strings.Trim(str, "\t")
return strings.Trim(str, " ")
}