From 735e32294201b67f16a67aa2e4dc135015cae400 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Mon, 15 Aug 2022 00:38:50 +0800 Subject: [PATCH] add beancount syntax check api --- script/paths.go | 5 +++++ server.go | 1 + service/ledger.go | 22 ++++++++++++++++++++++ service/transactions.go | 1 + 4 files changed, 29 insertions(+) diff --git a/script/paths.go b/script/paths.go index e0abbe4..3c29b3c 100644 --- a/script/paths.go +++ b/script/paths.go @@ -39,3 +39,8 @@ func GetLedgerPriceFilePath(dataPath string) string { func GetLedgerMonthsFilePath(dataPath string) string { return dataPath + "/month/months.bean" } + +func GetLedgerIndexFilePath(dataPath string) string { + LogInfo(dataPath, dataPath+"/index.bean") + return dataPath + "/index.bean" +} diff --git a/server.go b/server.go index 91c395c..0a639c3 100644 --- a/server.go +++ b/server.go @@ -90,6 +90,7 @@ func RegisterRouter(router *gin.Engine) { authorized.POST("/file", service.UpdateLedgerSourceFileContent) authorized.POST("/import/alipay", service.ImportAliPayCSV) authorized.POST("/import/wx", service.ImportWxPayCSV) + authorized.GET("/ledger/check", service.CheckLedger) authorized.DELETE("/ledger", service.DeleteLedger) } } diff --git a/service/ledger.go b/service/ledger.go index 5ab5cd5..5039193 100644 --- a/service/ledger.go +++ b/service/ledger.go @@ -1,6 +1,7 @@ package service import ( + "bytes" "crypto/sha1" "encoding/hex" "io" @@ -207,6 +208,27 @@ func DeleteLedger(c *gin.Context) { OK(c, "OK") } +func CheckLedger(c *gin.Context) { + var stderr bytes.Buffer + ledgerConfig := script.GetLedgerConfigFromContext(c) + cmd := exec.Command("bean-check", script.GetLedgerIndexFilePath(ledgerConfig.DataPath)) + cmd.Stderr = &stderr + output, err := cmd.Output() + if err != nil { + errors := strings.Split(stderr.String(), "\r\n") + result := make([]string, 0) + for _, e := range errors { + if e == "" { + continue + } + result = append(result, e) + } + OK(c, result) + } else { + OK(c, string(output)) + } +} + func createNewLedger(loginForm LoginForm, ledgerId string) (*script.Config, error) { // create new ledger serverConfig := script.GetServerConfig() diff --git a/service/transactions.go b/service/transactions.go index 21c5a52..f1d10a4 100644 --- a/service/transactions.go +++ b/service/transactions.go @@ -173,6 +173,7 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg } // 判断是否涉及多币种的转换 if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances { + autoBalance = true // 根据 number 的正负来判断是买入还是卖出 if entry.Number.GreaterThan(decimal.NewFromInt(0)) { // {351.729 CNY, 2021-09-29}