Commit 3fc65f3a authored by seykron's avatar seykron

Closes #23: discards cancelled bills from Balance

parent e1edb6ed
......@@ -2,12 +2,27 @@ package be.rlab.domino.application
import be.rlab.domino.application.model.BalanceDTO
import be.rlab.domino.application.model.BillDTO
import be.rlab.domino.domain.model.Balance
import be.rlab.domino.domain.model.TransactionType
import be.rlab.domino.domain.model.Value
import be.rlab.domino.application.model.BillingMode
import be.rlab.domino.application.model.MemorySlots.SERVICE_CONFIGURATIONS
import be.rlab.domino.application.model.ServiceConfig
import be.rlab.domino.domain.model.*
import be.rlab.domino.domain.model.Value.Companion.ZERO
import be.rlab.tehanu.domain.Memory
import java.util.*
class BalanceFactory {
class BalanceFactory(memory: Memory) {
private val serviceConfigurations: List<ServiceConfig> by memory.slot(SERVICE_CONFIGURATIONS, emptyList<ServiceConfig>())
data class BillInfo(
val bill: Bill,
val serviceConfig: ServiceConfig
) {
val billingMode: BillingMode = serviceConfig.billingMode
val creditorId: UUID = bill.creditor().id
val pending: Boolean = bill.pending
val debt: Value = bill.credit.debt
}
fun create(
previousBalance: Balance,
......@@ -48,9 +63,11 @@ class BalanceFactory {
}
private fun calculatePendingDebt(balance: Balance): Value {
return balance.bills.fold(ZERO) { pendingDebt, bill ->
if (bill.pending) {
pendingDebt + bill.debt.value
val pendingBills: List<BillInfo> = pendingBills(createBillsInfos(balance.bills))
return pendingBills.fold(ZERO) { pendingDebt, pendingBill ->
if (pendingBill.pending) {
pendingDebt + pendingBill.debt
} else {
pendingDebt
}
......@@ -72,4 +89,38 @@ class BalanceFactory {
total + transaction.value
}
}
}
\ No newline at end of file
private fun pendingBills(
bills: List<BillInfo>
): List<BillInfo> {
return bills.fold(listOf()) { pendingBills, bill ->
if (bill.pending && !billExists(pendingBills, bill)) {
pendingBills + bill
} else {
pendingBills
}
}
}
private fun billExists(
pendingBills: List<BillInfo>,
bill: BillInfo
): Boolean {
return when (bill.billingMode) {
BillingMode.SINGLE -> pendingBills.any { pendingBill ->
bill.creditorId == pendingBill.creditorId
}
BillingMode.MULTIPLE -> true
}
}
private fun createBillsInfos(bills: List<Bill>): List<BillInfo> {
return bills.map { bill ->
val serviceConfig: ServiceConfig = serviceConfigurations.find { serviceConfig ->
serviceConfig.id == bill.creditor().id
} ?: throw RuntimeException("Cannot find service config for bill $bill")
BillInfo(bill, serviceConfig)
}
}
}
......@@ -36,7 +36,7 @@
</p>
<p>
Si querés saber más cómo es el proceso de gestión financiera del hacklab, podés leer el
<a href="${base_path}/protocol">Protocolo de los Dineros de Cthulhu</a>.
<a href="/protocol">Protocolo de los Dineros de Cthulhu</a>.
</p>
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment