add: event api

This commit is contained in:
BaoXuebin 2024-03-10 22:30:56 +08:00
parent dfc2f1b36a
commit cc66151531
3 changed files with 44 additions and 29 deletions

View File

@ -113,15 +113,6 @@ func BeanReportAllPrices(ledgerConfig *Config) string {
return string(output) return string(output)
} }
func BeanReportAllEvents(ledgerConfig *Config) string {
beanFilePath := GetLedgerEventsFilePath(ledgerConfig.DataPath)
LogInfo(ledgerConfig.Mail, "bean-report "+beanFilePath+" events")
cmd := exec.Command("bean-report", beanFilePath, "events")
output, _ := cmd.Output()
return string(output)
}
func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) { func bqlRawQuery(ledgerConfig *Config, selectBql string, queryParamsPtr *QueryParams, queryResultPtr interface{}) (string, error) {
var bql string var bql string
if selectBql == "" { if selectBql == "" {

View File

@ -8,32 +8,38 @@ import (
) )
type Event struct { type Event struct {
Date string `form:"date" binding:"required" json:"date"` Date string `form:"date" binding:"required" json:"date"`
Type string `form:"type" binding:"required" json:"type"` Stage string `form:"stage" json:"stage"`
Description string `form:"description" binding:"required" json:"description"` Type string `form:"type" json:"type"`
Types []string `form:"types" json:"types"`
Description string `form:"description" binding:"required" json:"description"`
} }
func GetAllEvents(c *gin.Context) { func GetAllEvents(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c) ledgerConfig := script.GetLedgerConfigFromContext(c)
output := script.BeanReportAllEvents(ledgerConfig)
script.LogInfo(ledgerConfig.Mail, output)
beanFilePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath)
bytes, err := script.ReadFile(beanFilePath)
if err != nil {
InternalError(c, err.Error())
return
}
lines := strings.Split(string(bytes), "\n")
events := make([]Event, 0) events := make([]Event, 0)
lines := strings.Split(output, "\n")
// foreach lines // foreach lines
for idx, line := range lines { for _, line := range lines {
if idx < 2 || idx > len(lines)-3 {
continue
}
if strings.Trim(line, " ") == "" { if strings.Trim(line, " ") == "" {
continue continue
} }
// split line by " " // split line by " "
words := strings.Fields(line) words := strings.Fields(line)
if words[1] != "event" {
continue
}
events = append(events, Event{ events = append(events, Event{
Date: words[0], Date: words[0],
Type: words[1], Type: strings.ReplaceAll(words[2], "\"", ""),
Description: words[2], Description: strings.ReplaceAll(words[3], "\"", ""),
}) })
} }
OK(c, events) OK(c, events)
@ -49,14 +55,31 @@ func AddEvent(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c) ledgerConfig := script.GetLedgerConfigFromContext(c)
filePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath) filePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath)
line := fmt.Sprintf("%s event \"%s\" \"%s\"", event.Date, event.Type, event.Description) if event.Type != "" {
// 写入文件 event.Types = []string{event.Type}
err := script.AppendFileInNewLine(filePath, line)
if err != nil {
InternalError(c, err.Error())
return
} }
OK(c, event)
// 定义Event类型的数组
events := make([]Event, 0)
if event.Types != nil {
for _, t := range event.Types {
events = append(events, Event{
Date: event.Date,
Type: t,
Description: event.Description,
})
line := fmt.Sprintf("%s event \"%s\" \"%s\"", event.Date, t, event.Description)
// 写入文件
err := script.AppendFileInNewLine(filePath, line)
if err != nil {
InternalError(c, err.Error())
return
}
}
}
OK(c, events)
} }
func DeleteEvent(c *gin.Context) { func DeleteEvent(c *gin.Context) {

View File

@ -83,7 +83,8 @@ func sum(entries []AddTransactionEntryForm, openingBalances string) decimal.Deci
if entry.Account == openingBalances { if entry.Account == openingBalances {
return decimal.NewFromInt(0) return decimal.NewFromInt(0)
} }
if entry.Price.Exponent() == 0 { pVal, _ := entry.Price.Float64()
if pVal == 0 {
sumVal = entry.Number.Add(sumVal) sumVal = entry.Number.Add(sumVal)
} else { } else {
sumVal = entry.Number.Mul(entry.Price).Add(sumVal) sumVal = entry.Number.Mul(entry.Price).Add(sumVal)