add beancount syntax check api

This commit is contained in:
BaoXuebin 2022-08-15 00:38:50 +08:00
parent 2839749306
commit 735e322942
4 changed files with 29 additions and 0 deletions

View File

@ -39,3 +39,8 @@ func GetLedgerPriceFilePath(dataPath string) string {
func GetLedgerMonthsFilePath(dataPath string) string { func GetLedgerMonthsFilePath(dataPath string) string {
return dataPath + "/month/months.bean" return dataPath + "/month/months.bean"
} }
func GetLedgerIndexFilePath(dataPath string) string {
LogInfo(dataPath, dataPath+"/index.bean")
return dataPath + "/index.bean"
}

View File

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

View File

@ -1,6 +1,7 @@
package service package service
import ( import (
"bytes"
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"io" "io"
@ -207,6 +208,27 @@ func DeleteLedger(c *gin.Context) {
OK(c, "OK") 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) { func createNewLedger(loginForm LoginForm, ledgerId string) (*script.Config, error) {
// create new ledger // create new ledger
serverConfig := script.GetServerConfig() serverConfig := script.GetServerConfig()

View File

@ -173,6 +173,7 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
} }
// 判断是否涉及多币种的转换 // 判断是否涉及多币种的转换
if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances { if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances {
autoBalance = true
// 根据 number 的正负来判断是买入还是卖出 // 根据 number 的正负来判断是买入还是卖出
if entry.Number.GreaterThan(decimal.NewFromInt(0)) { if entry.Number.GreaterThan(decimal.NewFromInt(0)) {
// {351.729 CNY, 2021-09-29} // {351.729 CNY, 2021-09-29}