summaryrefslogtreecommitdiff
path: root/sandbox/src/main/kotlin/tech/libeufin
diff options
context:
space:
mode:
authorMS <ms@taler.net>2021-05-27 15:34:20 +0200
committerMS <ms@taler.net>2021-05-27 15:34:20 +0200
commitd527df40d0553bbe6c20bfac0754e8a0253d02d4 (patch)
tree2eedfae56786f50058cfc5335edb37179b434ae9 /sandbox/src/main/kotlin/tech/libeufin
parent4c44480327c5ffc7cb925f2264dd401769d48ddb (diff)
downloadlibeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.tar.gz
libeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.tar.bz2
libeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.zip
Calculating balances in the Sandbox.
At this point, the Sandbox goes through the whole history of one account (= one IBAN) and calculates the balance for it. Later, a mechanism to calculate the balance only based on a subset of such transactions should be provided.
Diffstat (limited to 'sandbox/src/main/kotlin/tech/libeufin')
-rw-r--r--sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt2
-rw-r--r--sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt24
2 files changed, 24 insertions, 2 deletions
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
index 4df1853f..0cc34380 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
@@ -307,7 +307,7 @@ fun buildCamtString(type: Int, subscriberIban: String, history: List<RawPayment>
}
element("Amt") {
attribute("Ccy", "EUR")
- text(Amount(0).toPlainString())
+ text(balanceForAccount(subscriberIban).toString())
}
element("CdtDbtInd") {
// a temporary value to get the camt to validate.
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
index a0c0d16b..d55f0912 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
@@ -5,16 +5,38 @@ import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.Logger
import org.slf4j.LoggerFactory
+import tech.libeufin.sandbox.BankAccountTransactionsTable.amount
import tech.libeufin.util.RawPayment
import tech.libeufin.util.importDateFromMillis
import tech.libeufin.util.toDashedDate
+import java.math.BigInteger
private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
+fun balanceForAccount(iban: String): BigInteger {
+ logger.debug("Calculating balance for account: ${iban}")
+ var balance = BigInteger.ZERO
+ transaction {
+ BankAccountTransactionsTable.select {
+ BankAccountTransactionsTable.creditorIban eq iban
+ }.forEach {
+ val amount = BigInteger(it[amount])
+ balance += amount
+ }
+ BankAccountTransactionsTable.select {
+ BankAccountTransactionsTable.debtorIban eq iban
+ }.forEach {
+ val amount = BigInteger(it[amount])
+ balance -= amount
+ }
+ }
+ return balance
+}
+
fun historyForAccount(iban: String): List<RawPayment> {
val history = mutableListOf<RawPayment>()
+ logger.debug("Querying transactions involving: ${iban}")
transaction {
- logger.debug("Querying transactions involving: ${iban}")
BankAccountTransactionsTable.select {
BankAccountTransactionsTable.creditorIban eq iban or
(BankAccountTransactionsTable.debtorIban eq iban)