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

Commit

Permalink
Support for transfers as income/expense
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwa-Raghavendra committed Apr 25, 2022
1 parent 92337f6 commit 8e63b79
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.ivy.wallet.domain.pure.data.ClosedTimeRange
import com.ivy.wallet.domain.pure.data.IncomeExpensePair
import com.ivy.wallet.domain.pure.transaction.AccountValueFunctions
import com.ivy.wallet.domain.pure.transaction.foldTransactions
import java.math.BigDecimal
import javax.inject.Inject

class CalcAccIncomeExpenseAct @Inject constructor(
Expand All @@ -25,22 +26,25 @@ class CalcAccIncomeExpenseAct @Inject constructor(
arg = account.id,
valueFunctions = nonEmptyListOf(
AccountValueFunctions::income,
AccountValueFunctions::expense
AccountValueFunctions::expense,
AccountValueFunctions::transferIncome,
AccountValueFunctions::transferExpense
)
)
} then { values ->
Output(
account = account,
incomeExpensePair = IncomeExpensePair(
income = values[0],
expense = values[1]
income = values[0] + if (includeTransfersInCalc) values[2] else BigDecimal.ZERO,
expense = values[1] + if (includeTransfersInCalc) values[3] else BigDecimal.ZERO
)
)
}

data class Input(
val account: Account,
val range: ClosedTimeRange = ClosedTimeRange.allTimeIvy()
val range: ClosedTimeRange = ClosedTimeRange.allTimeIvy(),
val includeTransfersInCalc: Boolean = false
)

data class Output(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class AccountDataAct @Inject constructor(
val incomeExpensePair = calcAccIncomeExpenseAct(
CalcAccIncomeExpenseAct.Input(
account = acc,
range = range
range = range,
includeTransfersInCalc = includeTransfersInCalc
)
).incomeExpensePair

Expand All @@ -60,6 +61,7 @@ class AccountDataAct @Inject constructor(
data class Input(
val accounts: List<Account>,
val baseCurrency: String,
val range: ClosedTimeRange
val range: ClosedTimeRange,
val includeTransfersInCalc: Boolean = false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ object AccountValueFunctions {
amount else BigDecimal.ZERO
}

fun transferIncome(
transaction: Transaction,
accountId: UUID
): BigDecimal = with(transaction) {
if (this.toAccountId == accountId && type == TransactionType.TRANSFER)
toAmount else BigDecimal.ZERO
}

fun expense(
transaction: Transaction,
accountId: UUID
Expand All @@ -50,6 +58,15 @@ object AccountValueFunctions {
amount else BigDecimal.ZERO
}

fun transferExpense(
transaction: Transaction,
accountId: UUID
): BigDecimal = with(transaction) {
if (this.accountId == accountId && type == TransactionType.TRANSFER)
amount else BigDecimal.ZERO
}


fun incomeCount(
transaction: Transaction,
accountId: UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SharedPrefs(appContext: Context) {
const val START_DATE_OF_MONTH = "start_date_of_month"
const val SHOW_NOTIFICATIONS = "show_notifications"
const val HIDE_CURRENT_BALANCE = "hide_current_balance"
const val TRANSFERS_AS_INCOME_EXPENSE = "transfers_as_inc_exp"
//----------------------------- App Settings -----------------------------------------------

//-------------------------------- Customer Journey ----------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ivy.wallet.domain.deprecated.logic.AccountCreator
import com.ivy.wallet.domain.deprecated.sync.item.AccountSync
import com.ivy.wallet.domain.event.AccountsUpdatedEvent
import com.ivy.wallet.domain.pure.data.WalletDAOs
import com.ivy.wallet.io.persistence.SharedPrefs
import com.ivy.wallet.io.persistence.dao.AccountDao
import com.ivy.wallet.io.persistence.dao.SettingsDao
import com.ivy.wallet.ui.IvyWalletCtx
Expand All @@ -37,6 +38,7 @@ class AccountsViewModel @Inject constructor(
private val accountSync: AccountSync,
private val accountCreator: AccountCreator,
private val ivyContext: IvyWalletCtx,
private val sharedPrefs: SharedPrefs,
private val accountsAct: AccountsAct,
private val calcWalletBalanceAct: CalcWalletBalanceAct,
private val baseCurrencyAct: BaseCurrencyAct,
Expand Down Expand Up @@ -70,11 +72,15 @@ class AccountsViewModel @Inject constructor(
val baseCurrencyCode = baseCurrencyAct(Unit)
val accs = accountsAct(Unit)

val includeTransfersInCalc =
sharedPrefs.getBoolean(SharedPrefs.TRANSFERS_AS_INCOME_EXPENSE, false)

val accountsDataList = accountDataAct(
AccountDataAct.Input(
accounts = accs,
range = range.toCloseTimeRange(),
baseCurrency = baseCurrencyCode
baseCurrency = baseCurrencyCode,
includeTransfersInCalc = includeTransfersInCalc
)
)

Expand Down

0 comments on commit 8e63b79

Please sign in to comment.