multi currency transaction

This commit is contained in:
BaoXuebin 2023-12-07 23:59:28 +08:00
parent 89f287fe90
commit 19dae50c07
2 changed files with 27 additions and 19 deletions

View File

@ -39,6 +39,8 @@ type Account struct {
CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
ExRate string `json:"exRate,omitempty"` // 汇率
ExDate string `json:"exDate,omitempty"` // 汇率日期
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
IsCurrent bool `json:"isCurrent,omitempty"`
Positions []AccountPosition `json:"positions,omitempty"`
MarketNumber string `json:"marketNumber,omitempty"`
MarketCurrency string `json:"marketCurrency,omitempty"`
@ -62,7 +64,6 @@ type LedgerCurrency struct {
Name string `json:"name"`
Currency string `json:"currency"`
Symbol string `json:"symbol"`
IsCurrent bool `json:"isCurrent,omitempty"` // 是否是货币(非货币的为投资单位)
Current bool `json:"current,omitempty"`
ExRate string `json:"exRate,omitempty"`
Date string `json:"date,omitempty"`

View File

@ -162,6 +162,8 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
}
}
currencyMap := script.GetLedgerCurrencyMap(ledgerConfig.Id)
var autoBalance bool
for _, entry := range addTransactionForm.Entries {
account := script.GetLedgerAccount(ledgerConfig.Id, entry.Account)
@ -174,11 +176,14 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
zero := decimal.NewFromInt(0)
// 判断是否涉及多币种的转换
if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances {
autoBalance = false
autoBalance = true
// 汇率值小于等于0则不进行汇率转换
if entry.Price.LessThanOrEqual(zero) {
continue
}
// 货币跳过汇率转换
if _, ok := currencyMap[account.Currency]; !ok {
// 根据 number 的正负来判断是买入还是卖出
if entry.Number.GreaterThan(zero) {
// {351.729 CNY, 2021-09-29}
@ -187,6 +192,8 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
// {} @ 359.019 CNY
line += fmt.Sprintf(" {} @ %s %s", entry.Price, ledgerConfig.OperatingCurrency)
}
}
priceLine := fmt.Sprintf("%s price %s %s %s", addTransactionForm.Date, account.Currency, entry.Price, ledgerConfig.OperatingCurrency)
err := script.AppendFileInNewLine(script.GetLedgerPriceFilePath(ledgerConfig.DataPath), priceLine)
if err != nil {