From 222dc6af3ace1eaa950f40622e7ef884f0dda1da Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Thu, 16 Feb 2023 13:43:39 +0800 Subject: [PATCH] fix: cannot include bean file #46 --- script/paths.go | 4 ++++ service/accounts.go | 13 +++++++++++-- service/bean.go | 25 +++++++++++++++++++++++++ service/transactions.go | 27 ++++++++------------------- 4 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 service/bean.go diff --git a/script/paths.go b/script/paths.go index b228732..d5d2120 100644 --- a/script/paths.go +++ b/script/paths.go @@ -48,6 +48,10 @@ func GetLedgerMonthsFilePath(dataPath string) string { return dataPath + "/month/months.bean" } +func GetLedgerMonthFilePath(dataPath string, month string) string { + return dataPath + "/month/" + month + ".bean" +} + func GetLedgerIndexFilePath(dataPath string) string { LogInfo(dataPath, dataPath+"/index.bean") return dataPath + "/index.bean" diff --git a/service/accounts.go b/service/accounts.go index 66eabc8..a3c098b 100644 --- a/service/accounts.go +++ b/service/accounts.go @@ -239,8 +239,17 @@ func BalanceAccount(c *gin.Context) { line := fmt.Sprintf("\r\n%s pad %s Equity:OpeningBalances", yesterdayStr, accountForm.Account) line += fmt.Sprintf("\r\n%s balance %s %s %s", todayStr, accountForm.Account, accountForm.Number, acc.Currency) - filePath := fmt.Sprintf("%s/month/%s.bean", ledgerConfig.DataPath, month) - err = script.AppendFileInNewLine(filePath, line) + // check month bean file exist + err = CreateMonthBeanFileIfNotExist(ledgerConfig.DataPath, month) + if err != nil { + if c != nil { + InternalError(c, err.Error()) + } + return + } + + // append padding content to month bean file + err = script.AppendFileInNewLine(script.GetLedgerMonthFilePath(ledgerConfig.DataPath, month), line) if err != nil { InternalError(c, err.Error()) return diff --git a/service/bean.go b/service/bean.go new file mode 100644 index 0000000..31fe0e4 --- /dev/null +++ b/service/bean.go @@ -0,0 +1,25 @@ +package service + +import ( + "errors" + "fmt" + "github.com/beancount-gs/script" +) + +// CreateMonthBeanFileIfNotExist create month bean file if not exist, otherwise return. +func CreateMonthBeanFileIfNotExist(ledgerDataPath string, month string) error { + // 文件不存在,则创建 + filePath := fmt.Sprintf("%s/month/%s.bean", ledgerDataPath, month) + if !script.FileIfExist(filePath) { + err := script.CreateFile(filePath) + if err != nil { + return errors.New("failed to create file") + } + // include ./2021-11.bean + err = script.AppendFileInNewLine(script.GetLedgerMonthsFilePath(ledgerDataPath), fmt.Sprintf("include \"./%s.bean\"", month)) + if err != nil { + return errors.New("failed to append content to months.bean") + } + } + return nil +} diff --git a/service/transactions.go b/service/transactions.go index f1d10a4..056a7a1 100644 --- a/service/transactions.go +++ b/service/transactions.go @@ -205,29 +205,18 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg } return errors.New("internal error") } - monthStr := month.Format("2006-01") - filePath := fmt.Sprintf("%s/month/%s.bean", ledgerConfig.DataPath, monthStr) - // 文件不存在,则创建 - if !script.FileIfExist(filePath) { - err = script.CreateFile(filePath) - if err != nil { - if c != nil { - InternalError(c, err.Error()) - } - return errors.New("internal error") - } - // include ./2021-11.bean - err = script.AppendFileInNewLine(script.GetLedgerMonthsFilePath(ledgerConfig.DataPath), fmt.Sprintf("include \"./%s.bean\"", monthStr)) - if err != nil { - if c != nil { - InternalError(c, err.Error()) - } - return errors.New("internal error") + // 交易的月份信息 + monthStr := month.Format("2006-01") + err = CreateMonthBeanFileIfNotExist(ledgerConfig.DataPath, monthStr) + if err != nil { + if c != nil { + InternalError(c, err.Error()) } + return err } - err = script.AppendFileInNewLine(filePath, line) + err = script.AppendFileInNewLine(script.GetLedgerMonthFilePath(ledgerConfig.DataPath, monthStr), line) if err != nil { if c != nil { InternalError(c, err.Error())