From a339e1afe83c365efd03cf9a36601f78410d85cd Mon Sep 17 00:00:00 2001 From: BaoXuebin Date: Mon, 4 Mar 2024 22:51:25 +0800 Subject: [PATCH] fix: #66 multi currency transactions --- service/transactions.go | 11 +++++++++-- tests/main.go | 17 ----------------- tests/main_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) delete mode 100644 tests/main.go create mode 100644 tests/main_test.go diff --git a/service/transactions.go b/service/transactions.go index f3187ed..cc6a461 100644 --- a/service/transactions.go +++ b/service/transactions.go @@ -177,13 +177,16 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg zero := decimal.NewFromInt(0) // 判断是否涉及多币种的转换 if account.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances { - autoBalance = true // 汇率值小于等于0,则不进行汇率转换 if entry.Price.LessThanOrEqual(zero) { continue } - _, isCurrency := currencyMap[account.Currency] + currency, isCurrency := currencyMap[account.Currency] + currencyPrice := entry.Price + if currencyPrice.Equal(zero) { + currencyPrice, _ = decimal.NewFromString(currency.Price) + } // 货币跳过汇率转换 if !isCurrency { // 根据 number 的正负来判断是买入还是卖出 @@ -194,6 +197,10 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg // {} @ 359.019 CNY line += fmt.Sprintf(" {} @ %s %s", entry.Price, ledgerConfig.OperatingCurrency) } + } else { + // 外币种格式:Assets:Fixed:三顿半咖啡 -1.00 SATURN_BIRD {5.61 CNY} + // fix issue #66 https://github.com/BaoXuebin/beancount-gs/issues/66 + line += fmt.Sprintf(" {%s %s}", currencyPrice, ledgerConfig.OperatingCurrency) } priceLine := fmt.Sprintf("%s price %s %s %s", addTransactionForm.Date, account.Currency, entry.Price, ledgerConfig.OperatingCurrency) diff --git a/tests/main.go b/tests/main.go deleted file mode 100644 index edb936c..0000000 --- a/tests/main.go +++ /dev/null @@ -1,17 +0,0 @@ -package tests - -import ( - "fmt" - "reflect" -) - -type Student struct { - Name string -} - -func Test(i interface{}) { - t := reflect.TypeOf(i) - k := t.Kind() - v := reflect.ValueOf(i) - fmt.Printf("type: %s, kind: %s, value: %s", t, k, v) -} diff --git a/tests/main_test.go b/tests/main_test.go new file mode 100644 index 0000000..27fee81 --- /dev/null +++ b/tests/main_test.go @@ -0,0 +1,38 @@ +package tests + +import ( + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" +) + +func TestPingRoute(t *testing.T) { + // 设置Gin的模式为测试模式 + gin.SetMode(gin.TestMode) + + // 创建一个Gin引擎 + r := gin.Default() + + // 定义测试路由处理程序 + r.GET("/ping", func(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{"message": "pong"}) + }) + + // 创建一个模拟的HTTP请求 + req, err := http.NewRequest(http.MethodGet, "/ping", nil) + assert.NoError(t, err) + + // 使用httptest包创建一个ResponseRecorder,用于记录响应 + w := httptest.NewRecorder() + + // 使用Gin的ServeHTTP方法处理请求 + r.ServeHTTP(w, req) + + // 断言状态码为200 + assert.Equal(t, http.StatusOK, w.Code) + + // 断言响应体中的内容 + assert.JSONEq(t, `{"message": "pong"}`, w.Body.String()) +}