add multi stage transaction
This commit is contained in:
parent
a2a29702d3
commit
8fcdbb43e6
|
|
@ -3,13 +3,12 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/beancount-gs/script"
|
||||
"github.com/beancount-gs/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func InitServerFiles() error {
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ type AddTransactionForm struct {
|
|||
Payee string `form:"payee"`
|
||||
Desc string `form:"desc" binding:"required"`
|
||||
Tags []string `form:"tags"`
|
||||
DivideDateList []string `form:"divideDateList"`
|
||||
Entries []AddTransactionEntryForm `form:"entries"`
|
||||
}
|
||||
|
||||
|
|
@ -116,8 +117,28 @@ func AddTransactions(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
||||
err := saveTransaction(c, addTransactionForm, ledgerConfig)
|
||||
// 判断是否分期
|
||||
var err error
|
||||
var divideCount = len(addTransactionForm.DivideDateList)
|
||||
if divideCount <= 0 {
|
||||
err = saveTransaction(c, addTransactionForm, ledgerConfig)
|
||||
} else {
|
||||
for idx, entry := range addTransactionForm.Entries {
|
||||
// 保留 3 位小数
|
||||
addTransactionForm.Entries[idx].Number = entry.Number.Div(decimal.NewFromInt(int64(divideCount))).Round(3)
|
||||
}
|
||||
for _, date := range addTransactionForm.DivideDateList {
|
||||
addTransactionForm.Date = date
|
||||
err = saveTransaction(c, addTransactionForm, ledgerConfig)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
script.LogError(ledgerConfig.Mail, err.Error())
|
||||
InternalError(c, "failed to add transaction")
|
||||
return
|
||||
}
|
||||
OK(c, nil)
|
||||
|
|
@ -153,7 +174,6 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
|
|||
}
|
||||
// 判断是否涉及多币种的转换
|
||||
if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances {
|
||||
autoBalance = autoBalance && true
|
||||
// 根据 number 的正负来判断是买入还是卖出
|
||||
if entry.Number.GreaterThan(decimal.NewFromInt(0)) {
|
||||
// {351.729 CNY, 2021-09-29}
|
||||
|
|
|
|||
Loading…
Reference in New Issue