fix incorrect transaction amount in exported data

This commit is contained in:
MaysWind
2024-09-01 22:15:56 +08:00
parent 2fc6a6ca77
commit 366311edbb
3 changed files with 81 additions and 22 deletions
+2 -22
View File
@@ -50,7 +50,7 @@ func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator
subCategory := e.replaceDelimiters(e.getTransactionSubCategoryName(transaction.CategoryId, categoryMap), separator)
account := e.replaceDelimiters(e.getAccountName(transaction.AccountId, accountMap), separator)
accountCurrency := e.getAccountCurrency(transaction.AccountId, accountMap)
amount := e.getDisplayAmount(transaction.Amount)
amount := utils.FormatAmount(transaction.Amount)
account2 := ""
account2Currency := ""
account2Amount := ""
@@ -59,7 +59,7 @@ func (e *EzBookKeepingPlainFileExporter) toExportedContent(uid int64, separator
if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_OUT {
account2 = e.replaceDelimiters(e.getAccountName(transaction.RelatedAccountId, accountMap), separator)
account2Currency = e.getAccountCurrency(transaction.RelatedAccountId, accountMap)
account2Amount = e.getDisplayAmount(transaction.RelatedAccountAmount)
account2Amount = utils.FormatAmount(transaction.RelatedAccountAmount)
}
if transaction.GeoLongitude != 0 || transaction.GeoLatitude != 0 {
@@ -139,26 +139,6 @@ func (e *EzBookKeepingPlainFileExporter) getAccountCurrency(accountId int64, acc
}
}
func (e *EzBookKeepingPlainFileExporter) getDisplayAmount(amount int64) string {
displayAmount := utils.Int64ToString(amount)
integer := utils.SubString(displayAmount, 0, len(displayAmount)-2)
decimals := utils.SubString(displayAmount, -2, 2)
if integer == "" {
integer = "0"
} else if integer == "-" {
integer = "-0"
}
if len(decimals) == 0 {
decimals = "00"
} else if len(decimals) == 1 {
decimals = "0" + decimals
}
return integer + "." + decimals
}
func (e *EzBookKeepingPlainFileExporter) getTags(transactionId int64, allTagIndexes map[int64][]int64, tagMap map[int64]*models.TransactionTag) string {
tagIndexes, exists := allTagIndexes[transactionId]
+29
View File
@@ -94,3 +94,32 @@ func Float64ToString(num float64) string {
func StringToFloat64(str string) (float64, error) {
return strconv.ParseFloat(str, 64)
}
// FormatAmount returns a textual representation of amount
func FormatAmount(amount int64) string {
displayAmount := Int64ToString(amount)
negative := displayAmount[0] == '-'
if negative {
displayAmount = displayAmount[1:]
}
integer := SubString(displayAmount, 0, len(displayAmount)-2)
decimals := SubString(displayAmount, -2, 2)
if integer == "" {
integer = "0"
}
if len(decimals) == 0 {
decimals = "00"
} else if len(decimals) == 1 {
decimals = "0" + decimals
}
if negative {
return "-" + integer + "." + decimals
}
return integer + "." + decimals
}
+50
View File
@@ -122,3 +122,53 @@ func TestStringToFloat64(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatAmount(t *testing.T) {
expectedValue := "0.00"
actualValue := FormatAmount(0)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "0.00"
actualValue = FormatAmount(-0)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "0.01"
actualValue = FormatAmount(1)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "-0.01"
actualValue = FormatAmount(-1)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "0.10"
actualValue = FormatAmount(10)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "-0.10"
actualValue = FormatAmount(-10)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "0.12"
actualValue = FormatAmount(12)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "-0.12"
actualValue = FormatAmount(-12)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "1.23"
actualValue = FormatAmount(123)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "-1.23"
actualValue = FormatAmount(-123)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "12.34"
actualValue = FormatAmount(1234)
assert.Equal(t, expectedValue, actualValue)
expectedValue = "-12.34"
actualValue = FormatAmount(-1234)
assert.Equal(t, expectedValue, actualValue)
}