From 19dae50c072bdd4e240f64c7b7aaf25a1c8bce84 Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Thu, 7 Dec 2023 23:59:28 +0800 Subject: [PATCH] multi currency transaction --- script/config.go | 23 ++++++++++++----------- service/transactions.go | 23 +++++++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/script/config.go b/script/config.go index 4d893f9..95c53b6 100644 --- a/script/config.go +++ b/script/config.go @@ -35,10 +35,12 @@ type Config struct { type Account struct { Acc string `json:"account"` StartDate string `json:"startDate"` - Currency string `json:"currency,omitempty"` // 货币 - CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号 - ExRate string `json:"exRate,omitempty"` // 汇率 - ExDate string `json:"exDate,omitempty"` // 汇率日期 + Currency string `json:"currency,omitempty"` // 货币 + 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"` @@ -59,13 +61,12 @@ type AccountType struct { } 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"` + Name string `json:"name"` + Currency string `json:"currency"` + Symbol string `json:"symbol"` + Current bool `json:"current,omitempty"` + ExRate string `json:"exRate,omitempty"` + Date string `json:"date,omitempty"` } func GetServerConfig() Config { diff --git a/service/transactions.go b/service/transactions.go index 6fc1d70..8511f17 100644 --- a/service/transactions.go +++ b/service/transactions.go @@ -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,19 +176,24 @@ 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 } - // 根据 number 的正负来判断是买入还是卖出 - if entry.Number.GreaterThan(zero) { - // {351.729 CNY, 2021-09-29} - line += fmt.Sprintf(" {%s %s, %s}", entry.Price, ledgerConfig.OperatingCurrency, addTransactionForm.Date) - } else { - // {} @ 359.019 CNY - line += fmt.Sprintf(" {} @ %s %s", entry.Price, ledgerConfig.OperatingCurrency) + + // 货币跳过汇率转换 + if _, ok := currencyMap[account.Currency]; !ok { + // 根据 number 的正负来判断是买入还是卖出 + if entry.Number.GreaterThan(zero) { + // {351.729 CNY, 2021-09-29} + line += fmt.Sprintf(" {%s %s, %s}", entry.Price, ledgerConfig.OperatingCurrency, addTransactionForm.Date) + } else { + // {} @ 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 {