RefundAvailability.kt
/*
* This file is part of GNU Taler
* (C) 2026 Taler Systems S.A.
*
* GNU Taler is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3, or (at your option) any later version.
*
* GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
package net.taler.merchantpos.refund
import net.taler.common.Amount
import net.taler.common.ExternalPayment
import net.taler.merchantlib.OrderHistoryEntry
import net.taler.merchantpos.checkout.Checkout
internal fun OrderHistoryEntry.talerRefundRemaining(): Amount
{
val zero = Amount.zero(amount.currency)
if (!paid ||
!refundable ||
hasPendingRefund)
{
return zero
}
val refunded = refundAmount ?: if (refunded) amount else zero
return if (refunded >= amount) zero else amount - refunded
}
/** Count a locally recorded refund only once when it also appears on the backend. */
internal fun OrderHistoryEntry.externalRefundRemaining(
payment: ExternalPayment,
checkout: Checkout?,
): Amount
{
val zero = Amount.zero(payment.amount.currency)
val refunds =
refundsExternal
.filter {
it.paymentId == payment.id ||
(it.paymentId == null && it.method == payment.method)
}.associate { it.id to it.amount }
.toMutableMap()
checkout?.refunds?.filter { it.paymentId == payment.id }?.forEach {
refunds[it.id] = maxOf(refunds[it.id] ?: zero, it.amount)
}
val returned = refunds.values.fold(zero) { sum, value -> sum + value }
return if (returned >= payment.amount) zero else payment.amount - returned
}