multi currency transaction
This commit is contained in:
parent
89f287fe90
commit
19dae50c07
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue