fix: cannot include bean file #46

This commit is contained in:
BaoXuebin 2023-02-16 13:43:39 +08:00
parent 7375c2b7d4
commit 222dc6af3a
4 changed files with 48 additions and 21 deletions

View File

@ -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"

View File

@ -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

25
service/bean.go Normal file
View File

@ -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
}

View File

@ -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())