Skip to content

Commit

Permalink
add: wxpay import
Browse files Browse the repository at this point in the history
  • Loading branch information
BaoXuebin committed Dec 14, 2021
1 parent d44a639 commit d97a905
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func RegisterRouter(router *gin.Engine) {
authorized.GET("/file/content", service.QueryLedgerSourceFileContent)
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
authorized.POST("/import/alipay", service.ImportAliPayCSV)
authorized.POST("/import/wx", service.ImportWxPayCSV)
}
}

Expand Down
49 changes: 49 additions & 0 deletions service/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,52 @@ func ImportAliPayCSV(c *gin.Context) {

OK(c, result)
}

func ImportWxPayCSV(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)

for {
lines, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
script.LogError(ledgerConfig.Mail, err.Error())
}
if len(lines) > 8 {
fields := strings.Fields(lines[0])
status := strings.Trim(lines[4], " ")
account := ""
if status == "收入" {
account = "Income:"
} else if status == "支出" {
account = "Expenses:"
} else {
continue
}

if len(fields) >= 2 {
result = append(result, Transaction{
Id: strings.Trim(lines[8], " "),
Date: strings.Trim(fields[0], " "),
Payee: strings.Trim(lines[2], " "),
Narration: strings.Trim(lines[3], " "),
Number: strings.Trim(lines[5], "¥"),
Account: account,
Currency: currency,
CurrencySymbol: currencySymbol,
})
}
}
}

OK(c, result)
}

0 comments on commit d97a905

Please sign in to comment.