diff options
Diffstat (limited to 'sandbox/src/main')
-rw-r--r-- | sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt | 2 | ||||
-rw-r--r-- | sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | 24 |
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) |