From e973ce256b8b7d1e3c2af16f692ed300b83c864c Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 22 Sep 2020 06:57:18 +0200 Subject: [PATCH] Added conversion to BigDecimal directly to Amount --- .../kotlin/net/dankito/banking/fints/model/Amount.kt | 6 ++++++ .../kotlin/net/dankito/banking/fints/model/Money.kt | 6 ++++++ .../net/dankito/banking/extensions/AmountExtensions.kt | 8 -------- .../net/dankito/banking/mapper/fints4kModelMapper.kt | 9 ++++----- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Amount.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Amount.kt index df2cd723..636b2951 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Amount.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Amount.kt @@ -1,5 +1,7 @@ package net.dankito.banking.fints.model +import net.dankito.utils.multiplatform.BigDecimal + open class Amount( val string: String @@ -13,6 +15,10 @@ open class Amount( internal constructor() : this("") // for object deserializers + open val bigDecimal: BigDecimal + get() = BigDecimal(this.string.replace(',', '.')) + + override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Amount) return false diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Money.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Money.kt index 76232e3e..5db73b0c 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Money.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/model/Money.kt @@ -1,5 +1,7 @@ package net.dankito.banking.fints.model +import net.dankito.utils.multiplatform.BigDecimal + open class Money( val amount: Amount, @@ -12,6 +14,10 @@ open class Money( internal constructor() : this(Amount.Zero, "") // for object deserializers + + open val bigDecimal: BigDecimal + get() = amount.bigDecimal + open val displayString: String get() = "$amount $currency" diff --git a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/extensions/AmountExtensions.kt b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/extensions/AmountExtensions.kt index 94ddab8c..af71db4f 100644 --- a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/extensions/AmountExtensions.kt +++ b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/extensions/AmountExtensions.kt @@ -11,12 +11,4 @@ fun BigDecimal.toAmount(): Amount { fun BigDecimal.toMoney(): Money { return Money(this.toAmount(), "EUR") -} - -fun Amount.toBigDecimal(): BigDecimal { - return BigDecimal(this.string.replace(',', '.')) -} - -fun Money.toBigDecimal(): BigDecimal { - return this.amount.toBigDecimal() } \ No newline at end of file diff --git a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/mapper/fints4kModelMapper.kt b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/mapper/fints4kModelMapper.kt index dc8d5c97..bbcbb54c 100644 --- a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/mapper/fints4kModelMapper.kt +++ b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/mapper/fints4kModelMapper.kt @@ -1,6 +1,5 @@ package net.dankito.banking.mapper -import net.dankito.banking.extensions.toBigDecimal import net.dankito.banking.ui.model.* import net.dankito.banking.ui.model.responses.AddAccountResponse import net.dankito.banking.ui.model.responses.BankingClientResponse @@ -54,7 +53,7 @@ open class fints4kModelMapper(protected val modelCreator: IModelCreator) { return RetrievedAccountData( account, retrievedData.successfullyRetrievedData, - retrievedData.balance?.toBigDecimal(), + retrievedData.balance?.bigDecimal, mapTransactions(account, retrievedData.bookedTransactions), listOf(), // TODO: map unbooked transactions retrievedData.retrievedTransactionsFrom, @@ -198,7 +197,7 @@ open class fints4kModelMapper(protected val modelCreator: IModelCreator) { open fun mapTransaction(account: TypedBankAccount, transaction: net.dankito.banking.fints.model.AccountTransaction): IAccountTransaction { return modelCreator.createTransaction( account, - transaction.amount.toBigDecimal(), + transaction.amount.bigDecimal, transaction.amount.currency.code, transaction.unparsedUsage, transaction.bookingDate, @@ -209,8 +208,8 @@ open class fints4kModelMapper(protected val modelCreator: IModelCreator) { transaction.valueDate, transaction.statementNumber, transaction.sequenceNumber, - transaction.openingBalance?.toBigDecimal(), - transaction.closingBalance?.toBigDecimal(), + transaction.openingBalance?.bigDecimal, + transaction.closingBalance?.bigDecimal, transaction.endToEndReference, transaction.customerReference,