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

@ -35,10 +35,12 @@ type Config struct {
type Account struct { type Account struct {
Acc string `json:"account"` Acc string `json:"account"`
StartDate string `json:"startDate"` StartDate string `json:"startDate"`
Currency string `json:"currency,omitempty"` // 货币 Currency string `json:"currency,omitempty"` // 货币
CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号 CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
ExRate string `json:"exRate,omitempty"` // 汇率 ExRate string `json:"exRate,omitempty"` // 汇率
ExDate string `json:"exDate,omitempty"` // 汇率日期 ExDate string `json:"exDate,omitempty"` // 汇率日期
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
IsCurrent bool `json:"isCurrent,omitempty"`
Positions []AccountPosition `json:"positions,omitempty"` Positions []AccountPosition `json:"positions,omitempty"`
MarketNumber string `json:"marketNumber,omitempty"` MarketNumber string `json:"marketNumber,omitempty"`
MarketCurrency string `json:"marketCurrency,omitempty"` MarketCurrency string `json:"marketCurrency,omitempty"`
@ -59,13 +61,12 @@ type AccountType struct {
} }
type LedgerCurrency struct { type LedgerCurrency struct {
Name string `json:"name"` Name string `json:"name"`
Currency string `json:"currency"` Currency string `json:"currency"`
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
IsCurrent bool `json:"isCurrent,omitempty"` // 是否是货币(非货币的为投资单位) Current bool `json:"current,omitempty"`
Current bool `json:"current,omitempty"` ExRate string `json:"exRate,omitempty"`
ExRate string `json:"exRate,omitempty"` Date string `json:"date,omitempty"`
Date string `json:"date,omitempty"`
} }
func GetServerConfig() Config { func GetServerConfig() Config {

View File

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