fix: #66 multi currency transactions

This commit is contained in:
BaoXuebin 2024-03-04 22:51:25 +08:00
parent 344a6782ae
commit a339e1afe8
3 changed files with 47 additions and 19 deletions

View File

@ -177,13 +177,16 @@ 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 = true
// 汇率值小于等于0则不进行汇率转换 // 汇率值小于等于0则不进行汇率转换
if entry.Price.LessThanOrEqual(zero) { if entry.Price.LessThanOrEqual(zero) {
continue continue
} }
_, isCurrency := currencyMap[account.Currency] currency, isCurrency := currencyMap[account.Currency]
currencyPrice := entry.Price
if currencyPrice.Equal(zero) {
currencyPrice, _ = decimal.NewFromString(currency.Price)
}
// 货币跳过汇率转换 // 货币跳过汇率转换
if !isCurrency { if !isCurrency {
// 根据 number 的正负来判断是买入还是卖出 // 根据 number 的正负来判断是买入还是卖出
@ -194,6 +197,10 @@ func saveTransaction(c *gin.Context, addTransactionForm AddTransactionForm, ledg
// {} @ 359.019 CNY // {} @ 359.019 CNY
line += fmt.Sprintf(" {} @ %s %s", entry.Price, ledgerConfig.OperatingCurrency) 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) priceLine := fmt.Sprintf("%s price %s %s %s", addTransactionForm.Date, account.Currency, entry.Price, ledgerConfig.OperatingCurrency)

View File

@ -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)
}

38
tests/main_test.go Normal file
View File

@ -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())
}