Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Fix planned payments bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 28, 2022
1 parent 5098967 commit f518270
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import com.ivy.wallet.domain.pure.exchange.exchangeInBaseCurrency
import com.ivy.wallet.domain.pure.transaction.expenses
import com.ivy.wallet.domain.pure.transaction.incomes
import com.ivy.wallet.domain.pure.transaction.sumTrns
import com.ivy.wallet.utils.timeNowUTC
import java.time.LocalDateTime
import com.ivy.wallet.utils.dateNowUTC
import java.time.LocalDate
import javax.inject.Inject

class DueTrnsInfoAct @Inject constructor(
Expand All @@ -29,11 +29,11 @@ class DueTrnsInfoAct @Inject constructor(
override suspend fun Input.compose(): suspend () -> Output = suspend {
range
} then dueTrnsAct then { trns ->
val timeNow = timeNowUTC()
val dateNow = dateNowUTC()
trns.filter {
this.dueFilter(it, timeNow)
this.dueFilter(it, dateNow)
}
} then { upcomingTrns ->
} then { dueTrns ->
//We have due transactions in different currencies
val exchangeArg = ExchangeTrnArgument(
baseCurrency = baseCurrency,
Expand All @@ -45,25 +45,25 @@ class DueTrnsInfoAct @Inject constructor(
Output(
dueIncomeExpense = IncomeExpensePair(
income = sumTrns(
incomes(upcomingTrns),
incomes(dueTrns),
::exchangeInBaseCurrency,
exchangeArg
),
expense = sumTrns(
expenses(upcomingTrns),
expenses(dueTrns),
::exchangeInBaseCurrency,
exchangeArg
)
),
dueTrns = upcomingTrns
dueTrns = dueTrns
)
}
}

data class Input(
val range: ClosedTimeRange,
val baseCurrency: String,
val dueFilter: (Transaction, LocalDateTime) -> Boolean
val dueFilter: (Transaction, LocalDate) -> Boolean
)

data class Output(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package com.ivy.wallet.domain.pure.account
import com.ivy.wallet.domain.data.core.Account

fun filterExcluded(accounts: List<Account>): List<Account> =
accounts.filter { it.includeInBalance }
accounts.filter { it.includeInBalance }

fun accountCurrency(account: Account, baseCurrency: String): String =
account.currency ?: baseCurrency
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ivy.fp.Pure
import com.ivy.fp.SideEffect
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.Transaction
import com.ivy.wallet.domain.pure.account.accountCurrency
import com.ivy.wallet.domain.pure.transaction.trnCurrency
import java.math.BigDecimal
import java.util.*
Expand All @@ -25,7 +26,9 @@ suspend fun exchangeInBaseCurrency(
transaction: Transaction,
arg: ExchangeTrnArgument
): BigDecimal {
val fromCurrency = arg.getAccount(transaction.accountId)?.currency.toOption()
val fromCurrency = arg.getAccount(transaction.accountId)?.let {
accountCurrency(it, arg.baseCurrency)
}.toOption()

return exchangeInCurrency(
transaction = transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import com.ivy.fp.Pure
import com.ivy.wallet.domain.data.TransactionType
import com.ivy.wallet.domain.data.core.Account
import com.ivy.wallet.domain.data.core.Transaction
import java.time.LocalDateTime
import com.ivy.wallet.domain.pure.account.accountCurrency
import java.time.LocalDate

@Pure
fun expenses(transactions: List<Transaction>): List<Transaction> {
Expand All @@ -24,23 +25,24 @@ fun transfers(transactions: List<Transaction>): List<Transaction> {
}

@Pure
fun isUpcoming(transaction: Transaction, timeNowUTC: LocalDateTime): Boolean =
timeNowUTC.isBefore(transaction.dueDate)

@Pure
fun isOverdue(transaction: Transaction, timeNowUTC: LocalDateTime): Boolean =
timeNowUTC.isAfter(transaction.dueDate)
fun isUpcoming(transaction: Transaction, dateNow: LocalDate): Boolean {
val dueDate = transaction.dueDate?.toLocalDate() ?: return false
return dateNow.isBefore(dueDate) || dateNow.isEqual(dueDate)
}

@Pure
fun trnCurrency(
transaction: Transaction,
accounts: List<Account>
): Option<String> = accounts.find { it.id == transaction.accountId }?.currency.toOption()
fun isOverdue(transaction: Transaction, dateNow: LocalDate): Boolean {
val dueDate = transaction.dueDate?.toLocalDate() ?: return false
return dateNow.isAfter(dueDate)
}

@Pure
fun trnCurrency(
transaction: Transaction,
accounts: List<Account>,
baseCurrency: String
): Option<String> =
((accounts.find { it.id == transaction.accountId }?.currency) ?: baseCurrency).toOption()
): Option<String> {
val account = accounts.find { it.id == transaction.accountId }
?: return baseCurrency.toOption()
return accountCurrency(account, baseCurrency).toOption()
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class BudgetViewModel @Inject constructor(
ExchangeAct.Input(
data = ExchangeData(
baseCurrency = baseCurrencyCode,
fromCurrency = trnCurrency(it, accounts)
fromCurrency = trnCurrency(it, accounts, baseCurrencyCode)
),
amount = it.amount
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class ReportViewModel @Inject constructor(
ExchangeAct.Input(
data = ExchangeData(
baseCurrency = baseCurrency,
fromCurrency = trnCurrency(it, accounts),
fromCurrency = trnCurrency(it, accounts, baseCurrency),
),
amount = it.amount
)
Expand Down Expand Up @@ -328,7 +328,7 @@ class ReportViewModel @Inject constructor(
ExchangeAct.Input(
data = ExchangeData(
baseCurrency = baseCurrency,
fromCurrency = trnCurrency(trn, accounts),
fromCurrency = trnCurrency(trn, accounts, baseCurrency),
),
amount = trn.amount
)
Expand All @@ -346,7 +346,7 @@ class ReportViewModel @Inject constructor(
ExchangeAct.Input(
data = ExchangeData(
baseCurrency = baseCurrency,
fromCurrency = trnCurrency(trn, accounts),
fromCurrency = trnCurrency(trn, accounts, baseCurrency),
),
amount = trn.amount
)
Expand Down

0 comments on commit f518270

Please sign in to comment.