add: event api
This commit is contained in:
parent
dfc2f1b36a
commit
cc66151531
|
|
@ -113,15 +113,6 @@ func BeanReportAllPrices(ledgerConfig *Config) string {
|
|||
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) {
|
||||
var bql string
|
||||
if selectBql == "" {
|
||||
|
|
|
|||
|
|
@ -9,31 +9,37 @@ import (
|
|||
|
||||
type Event struct {
|
||||
Date string `form:"date" binding:"required" json:"date"`
|
||||
Type string `form:"type" binding:"required" json:"type"`
|
||||
Stage string `form:"stage" json:"stage"`
|
||||
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) {
|
||||
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
||||
output := script.BeanReportAllEvents(ledgerConfig)
|
||||
script.LogInfo(ledgerConfig.Mail, output)
|
||||
|
||||
events := make([]Event, 0)
|
||||
lines := strings.Split(output, "\n")
|
||||
// foreach lines
|
||||
for idx, line := range lines {
|
||||
if idx < 2 || idx > len(lines)-3 {
|
||||
continue
|
||||
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)
|
||||
// foreach lines
|
||||
for _, line := range lines {
|
||||
if strings.Trim(line, " ") == "" {
|
||||
continue
|
||||
}
|
||||
// split line by " "
|
||||
words := strings.Fields(line)
|
||||
if words[1] != "event" {
|
||||
continue
|
||||
}
|
||||
events = append(events, Event{
|
||||
Date: words[0],
|
||||
Type: words[1],
|
||||
Description: words[2],
|
||||
Type: strings.ReplaceAll(words[2], "\"", ""),
|
||||
Description: strings.ReplaceAll(words[3], "\"", ""),
|
||||
})
|
||||
}
|
||||
OK(c, events)
|
||||
|
|
@ -49,14 +55,31 @@ func AddEvent(c *gin.Context) {
|
|||
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
||||
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}
|
||||
}
|
||||
|
||||
// 定义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, event)
|
||||
}
|
||||
}
|
||||
|
||||
OK(c, events)
|
||||
}
|
||||
|
||||
func DeleteEvent(c *gin.Context) {
|
||||
|
|
|
|||
|
|
@ -83,7 +83,8 @@ func sum(entries []AddTransactionEntryForm, openingBalances string) decimal.Deci
|
|||
if entry.Account == openingBalances {
|
||||
return decimal.NewFromInt(0)
|
||||
}
|
||||
if entry.Price.Exponent() == 0 {
|
||||
pVal, _ := entry.Price.Float64()
|
||||
if pVal == 0 {
|
||||
sumVal = entry.Number.Add(sumVal)
|
||||
} else {
|
||||
sumVal = entry.Number.Mul(entry.Price).Add(sumVal)
|
||||
|
|
|
|||
Loading…
Reference in New Issue