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